]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/interface/AliHLTExternalInterface.cxx
more secure string operations
[u/mrichter/AliRoot.git] / HLT / BASE / interface / AliHLTExternalInterface.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
10 //* Permission to use, copy, modify and distribute this software and its   *
11 //* documentation strictly for non-commercial purposes is hereby granted   *
12 //* without fee, provided that the above copyright notice appears in all   *
13 //* copies and that both the copyright notice and this permission notice   *
14 //* appear in the supporting documentation. The authors make no claims     *
15 //* about the suitability of this software for any purpose. It is          *
16 //* provided "as is" without express or implied warranty.                  *
17 //**************************************************************************
18
19 //  @file   AliHLTExternalInterface.cxx
20 //  @author Matthias Richter, Timm Steinbeck
21 //  @date   
22 //  @brief  Pure C interface to the AliRoot HLT analysis framework
23 //          The C interface is used from external applications like the HLT
24 //          online framework
25
26 #include "AliHLTExternalInterface.h"
27 #include "AliHLTComponentHandler.h"
28 #include "AliHLTComponent.h"
29 #include "AliHLTSystem.h"
30 #include "AliHLTMisc.h"
31 #include <cerrno>
32 #include <cstring>
33 /////////////////////////////////////////////////////////////////////////////////////
34 //
35 // AliHLT external interface functions
36 //
37
38 static AliHLTComponentHandler *gComponentHandler = NULL;
39 static AliHLTRunDesc gRunDesc=kAliHLTVoidRunDesc;
40 static char* gRunType=NULL;
41
42 int AliHLTAnalysisInitSystem( unsigned long version, AliHLTAnalysisEnvironment* externalEnv, unsigned long runNo, const char* runType )
43 {
44   // init the HLT system
45   if ( gComponentHandler ) {
46     return EINPROGRESS;
47   }
48
49   if (!externalEnv) {
50     return EINVAL;
51   }
52
53   if (!externalEnv->fStructSize) {
54     return EPERM;
55   }
56
57   // map the environment structure to the internal structure to
58   // support different versions of the interface
59   AliHLTAnalysisEnvironment mappedEnv;
60   memset(&mappedEnv, 0, sizeof(mappedEnv));
61   memcpy(&mappedEnv, externalEnv, sizeof(mappedEnv)<externalEnv->fStructSize?sizeof(mappedEnv):externalEnv->fStructSize);
62   mappedEnv.fStructSize=sizeof(mappedEnv);
63
64   gComponentHandler = new AliHLTComponentHandler(&mappedEnv);
65   if ( !gComponentHandler )
66     return EFAULT;
67   gComponentHandler->InitAliLogTrap(gComponentHandler);
68   gComponentHandler->AnnounceVersion();
69
70   if (version!=ALIHLT_DATA_TYPES_VERSION && externalEnv->fLoggingFunc) {
71     const int messagelength=100;
72     char message[messagelength+1];
73     message[messagelength]=0;
74     snprintf(message, messagelength, "interface definition does not match: internal %d - external %lu", ALIHLT_DATA_TYPES_VERSION, version);
75     externalEnv->fLoggingFunc(externalEnv->fParam, kHLTLogWarning, "AliHLTAnalysisInitSystem", "interface version", message);
76   }
77
78   gRunDesc.fRunNo=runNo;
79   if (runType && strlen(runType)>0) {
80     int runtypelength=strlen(runType);
81     gRunType=new char[runtypelength+1];
82     if (gRunType) {
83       gRunType[runtypelength]=0;
84       strncpy(gRunType, runType, runtypelength);
85     }
86   }
87
88   // the AliRoot dependent code is implemented by the
89   // AliHLTMiscImplementation class in libHLTrec
90   AliHLTMisc::Instance().InitCDB(getenv("ALIHLT_HCDBDIR"));
91   AliHLTMisc::Instance().SetCDBRunNo(gRunDesc.fRunNo);
92   AliHLTMisc::Instance().InitMagneticField();
93
94   return 0;
95 }
96
97 int AliHLTAnalysisDeinitSystem()
98 {
99   // De-init the HLT system and clean-up internal memory
100   if (gComponentHandler) delete gComponentHandler;
101   gComponentHandler = NULL;
102
103   if (gRunType) delete[] gRunType;
104   gRunType=NULL;
105
106   gRunDesc=kAliHLTVoidRunDesc;
107
108   return 0;
109 }
110
111 int AliHLTAnalysisLoadLibrary( const char* libraryPath )
112 {
113   // load a component library
114   if ( !gComponentHandler )
115     return ENXIO;
116   return gComponentHandler->LoadLibrary( libraryPath );
117 }
118
119 int AliHLTAnalysisUnloadLibrary( const char* /*libraryPath*/ )
120 {
121   // unload a component library
122   if ( !gComponentHandler )
123     return ENXIO;
124   // Matthias 26.10.2007
125   // Unloading of libraries has to be re-worked. It has been commented out here
126   // since the libraries will be unloaded at the destruction of the component
127   // handler instance anyway. So it has no effect to the operation in PubSub.
128   // With the introduction of the dynamic component registration via module
129   // agents we run into trouble when cleaning up the samples managed by the
130   // component handler. Destruction of the sample objects is done AFTER
131   // unloading of the library and thus the destructor is not present any 
132   // more.
133   //return gComponentHandler->UnloadLibrary( libraryPath );
134   return 0;
135 }
136
137 int AliHLTAnalysisCreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle, const char* description )
138 {
139   // create a component
140   if ( !gComponentHandler ) return ENXIO;
141   if (!handle) return EINVAL;
142
143   AliHLTComponent* comp=NULL;
144
145   int ret = gComponentHandler->CreateComponent( componentType, comp);
146   if (ret>=0 && comp) {
147     const AliHLTAnalysisEnvironment* comenv=gComponentHandler->GetEnvironment();
148     comp->SetComponentEnvironment(comenv, environParam);
149     if (comenv) {
150       if (description) {
151         comp->SetComponentDescription(description);
152       }
153       comp->SetRunDescription(&gRunDesc, gRunType);
154     }
155     ret=comp->Init(comenv, environParam, argc, argv);
156   }
157   *handle = reinterpret_cast<AliHLTComponentHandle>( comp );
158
159   return ret;
160 }
161
162 int AliHLTAnalysisDestroyComponent( AliHLTComponentHandle handle )
163 {
164   // destroy a component
165   if ( !handle )
166     return ENOENT;
167   
168   AliHLTComponent* pComp=reinterpret_cast<AliHLTComponent*>( handle );
169   pComp->Deinit();
170   delete pComp;
171   return 0;
172 }
173
174 int AliHLTAnalysisProcessEvent( AliHLTComponentHandle handle, const AliHLTComponentEventData* evtData, const AliHLTComponentBlockData* blocks, 
175                            AliHLTComponentTriggerData* trigData, AliHLTUInt8_t* outputPtr,
176                            AliHLTUInt32_t* size, AliHLTUInt32_t* outputBlockCnt, 
177                            AliHLTComponentBlockData** outputBlocks,
178                            AliHLTComponentEventDoneData** edd )
179 {
180   // process one event
181   if ( !handle ) return EINVAL;
182   AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
183   if (!comp) return ENXIO;
184   int ret=comp->ProcessEvent( *evtData, blocks, *trigData, outputPtr, *size, *outputBlockCnt, *outputBlocks, *edd );
185
186   // internally, return values <0 are errors, >=0 are success with some
187   // optional return vaue.
188   // externally everthing !=0 is error. This is also according to the
189   // common habit
190   if (ret>0) ret=0;
191   else if (ret<0) ret*=-1;
192   return ret;
193 }
194
195 int AliHLTAnalysisGetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
196 {
197   // get output data type of a component
198   if ( !handle ) return EINVAL;
199   AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
200   if (!comp) return ENXIO;
201   *dataType = comp->GetOutputDataType();
202   return 0;
203 }
204
205 int AliHLTAnalysisGetOutputSize( AliHLTComponentHandle handle, unsigned long* constEventBase, unsigned long* constBlockBase, double* inputBlockMultiplier )
206 {
207   // get output data size of a component
208   if ( !handle ) return EINVAL;
209   AliHLTComponent* comp = reinterpret_cast<AliHLTComponent*>( handle );
210   if (!comp) return ENXIO;
211   // TODO: extend component interface
212   if (constBlockBase) *constBlockBase=0;
213   comp->GetOutputDataSize( *constEventBase, *inputBlockMultiplier );
214   return 0;
215 }
216
217 struct AliHLTAnalysisInterfaceCall {
218   const char* fSignature;
219   void* fCall;
220 };
221
222 const AliHLTAnalysisInterfaceCall gAliHLTInterfaceCallSignatures[]={
223   //int AliHLTAnalysisInitSystem( unsigned long version, AliHLTAnalysisEnvironment* externalEnv, unsigned long runNo, const char* runType )
224   {"int AliHLTAnalysisInitSystem(unsigned long,AliHLTAnalysisEnvironment*,unsigned long,const char*)", (void*)AliHLTAnalysisInitSystem},
225
226   //int AliHLTAnalysisDeinitSystem()
227   {"int AliHLTAnalysisDeinitSystem()", (void*)AliHLTAnalysisDeinitSystem},
228
229   //int AliHLTAnalysisLoadLibrary( const char* libraryPath )
230   {"int AliHLTAnalysisLoadLibrary(const char*)", (void*)AliHLTAnalysisLoadLibrary},
231
232   //int AliHLTAnalysisUnloadLibrary( const char* /*libraryPath*/ )
233   {"int AliHLTAnalysisUnloadLibrary(const char*)", (void*)AliHLTAnalysisUnloadLibrary}, 
234
235   //int AliHLTAnalysisCreateComponent( const char* componentType, void* environParam, int argc, const char** argv, AliHLTComponentHandle* handle, const char* description )
236   {"int AliHLTAnalysisCreateComponent(const char*,void*,int,const char**,AliHLTComponentHandle*,const char*)", (void*)AliHLTAnalysisCreateComponent},
237
238   //int AliHLTAnalysisDestroyComponent( AliHLTComponentHandle handle )
239   {"int AliHLTAnalysisDestroyComponent(AliHLTComponentHandle)", (void*)AliHLTAnalysisDestroyComponent}, 
240
241   //int AliHLTAnalysisProcessEvent( AliHLTComponentHandle handle, const AliHLTComponentEventData* evtData, const AliHLTComponentBlockData* blocks, AliHLTComponentTriggerData* trigData, AliHLTUInt8_t* outputPtr, AliHLTUInt32_t* size, AliHLTUInt32_t* outputBlockCnt, AliHLTComponentBlockData** outputBlocks, AliHLTComponentEventDoneData** edd )
242   {"int AliHLTAnalysisProcessEvent(AliHLTComponentHandle,const AliHLTComponentEventData*,const AliHLTComponentBlockData*,AliHLTComponentTriggerData*,AliHLTUInt8_t*,AliHLTUInt32_t*,AliHLTUInt32_t*,AliHLTComponentBlockData**,AliHLTComponentEventDoneData**)", (void*)AliHLTAnalysisProcessEvent},
243
244   //int AliHLTAnalysisGetOutputDataType( AliHLTComponentHandle handle, AliHLTComponentDataType* dataType )
245   {"int AliHLTAnalysisGetOutputDataType(AliHLTComponentHandle,AliHLTComponentDataType*)", (void*)AliHLTAnalysisGetOutputDataType},
246
247   //int AliHLTAnalysisGetOutputSize( AliHLTComponentHandle handle, unsigned long* constEventBase, unsigned long* constBlockBase, double* inputBlockMultiplier )
248   {"int AliHLTAnalysisGetOutputSize(AliHLTComponentHandle,unsigned long*,unsigned long*,double*)", (void*)AliHLTAnalysisGetOutputSize},
249
250   {NULL, NULL}
251 };
252
253 void* AliHLTAnalysisGetInterfaceCall(const char* signature)
254 {
255   // get function pointer of an interface call
256   // the function pointer is selected according to the requested function signature
257   if (!signature) return NULL;
258   for (int i=0; gAliHLTInterfaceCallSignatures[i].fSignature!=NULL; i++) {
259     if (strcmp(gAliHLTInterfaceCallSignatures[i].fSignature, signature)==0) {
260       return gAliHLTInterfaceCallSignatures[i].fCall;
261     }
262   }
263   return NULL;
264 }
265
266 /////////////////////////////////////////////////////////////////////////////////////
267 //
268 // AliHLTSystem interface functions
269 //
270
271 int AliHLTSystemSetOptions(AliHLTSystem* pInstance, const char* options)
272 {
273   // deprecated helper function
274   int iResult=0;
275   if (pInstance) {
276     AliHLTSystem* pSystem=reinterpret_cast<AliHLTSystem*>(pInstance);
277     if (pSystem) {
278       iResult=pSystem->ScanOptions(options);
279     } else {
280       iResult=-EFAULT;
281     }
282   } else {
283     iResult=-EINVAL;
284   }
285   return iResult;
286 }
287
288 int AliHLTSystemProcessHLTOUT(AliHLTSystem* pInstance, AliHLTOUT* pHLTOUT, AliESDEvent* esd)
289 {
290   // deprecated helper function
291   int iResult=0;
292   if (pInstance) {
293     AliHLTSystem* pSystem=reinterpret_cast<AliHLTSystem*>(pInstance);
294     if (pSystem) {
295       iResult=pSystem->ProcessHLTOUT(pHLTOUT, esd);
296     } else {
297       iResult=-EFAULT;
298     }
299   } else {
300     iResult=-EINVAL;
301   }
302   return iResult;
303 }