1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 * This Source Code Form is subject to the terms of the
8 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
9 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
12 * @file persistence_client_library_custom_loader.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of persistence custom loadedr
19 #include "persistence_client_library_custom_loader.h"
20 #include "persistence_client_library_data_organization.h"
33 /// type definition of persistence custom library information
34 typedef struct sPersCustomLibInfo
36 char libname[CustLibMaxLen];
38 PersInitType_e initFunction;
39 PersLoadingType_e loadingType;
43 /// array with custom client library names
44 static PersCustomLibInfo gCustomLibArray[PersCustomLib_LastEntry];
46 char* gpCustomConfigFileMap = 0;
47 char* gpCustomTokenArray[TOKENARRAYSIZE];
48 int gCustomTokenCounter = 0;
49 unsigned int gCustomConfigFileSize = 0;
53 void fillCustomCharTokenArray()
57 char* tmpPointer = gpCustomConfigFileMap;
59 // set the first pointer to the start of the file
60 gpCustomTokenArray[blankCount] = tmpPointer;
63 while(i < gCustomConfigFileSize)
65 if(1 != gCharLookup[(int)*tmpPointer])
69 // check if we are at the end of the token array
70 if(blankCount >= TOKENARRAYSIZE)
74 gpCustomTokenArray[blankCount] = tmpPointer+1;
76 gCustomTokenCounter++;
84 PersLoadingType_e getCustomLoadingType(int i)
86 return gCustomLibArray[i].loadingType;
89 PersLoadingType_e getLoadingType(const char* type)
91 PersLoadingType_e persLoadingType = LoadType_Undefined;
93 if(0 == strcmp(type, "init") )
95 persLoadingType = LoadType_PclInit;
97 else if(0 == strcmp(type, "od") )
99 persLoadingType = LoadType_OnDemand;
102 return persLoadingType;
105 PersInitType_e getCustomInitType(int i)
107 return gCustomLibArray[i].initFunction;
110 PersInitType_e getInitType(const char* policy)
112 PersInitType_e persInitType = Init_Undefined;
114 if(0 == strcmp(policy, "sync"))
116 persInitType = Init_Synchronous;
118 else if (0 == strcmp(policy, "async"))
120 persInitType = Init_Asynchronous;
130 PersistenceCustomLibs_e custom_client_name_to_id(const char* lib_name, int substring)
132 PersistenceCustomLibs_e libId = PersCustomLib_LastEntry;
136 if(0 == strncmp(lib_name, "early", PersCustomPathSize) )
138 libId = PersCustomLib_early;
140 else if (0 == strncmp(lib_name, "secure", PersCustomPathSize) )
142 libId = PersCustomLib_secure;
144 else if (0 == strncmp(lib_name, "emergency", PersCustomPathSize) )
146 libId = PersCustomLib_emergency;
148 else if (0 == strncmp(lib_name, "hwinfo", PersCustomPathSize) )
150 libId = PersCustomLib_HWinfo;
152 else if (0 == strncmp(lib_name, "custom1", PersCustomPathSize) )
154 libId = PersCustomLib_Custom1;
156 else if (0 == strncmp(lib_name, "custom2", PersCustomPathSize) )
158 libId = PersCustomLib_Custom2;
160 else if (0 == strncmp(lib_name, "custom3", PersCustomPathSize) )
162 libId = PersCustomLib_Custom3;
166 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("custom_libname_to_id - error - id not found for lib:"), DLT_STRING(lib_name));
171 if(NULL != strstr(lib_name, "early") )
173 libId = PersCustomLib_early;
175 else if (NULL != strstr(lib_name, "secure") )
177 libId = PersCustomLib_secure;
179 else if (NULL != strstr(lib_name, "emergency") )
181 libId = PersCustomLib_emergency;
183 else if (NULL != strstr(lib_name, "hwinfo") )
185 libId = PersCustomLib_HWinfo;
187 else if (NULL != strstr(lib_name, "custom1") )
189 libId = PersCustomLib_Custom1;
191 else if (NULL != strstr(lib_name, "custom2") )
193 libId = PersCustomLib_Custom2;
195 else if (NULL != strstr(lib_name, "custom3") )
197 libId = PersCustomLib_Custom3;
201 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("custom_libname_to_id - error - id not found for lib:"), DLT_STRING(lib_name));
210 int get_custom_libraries()
212 int rval = 0, fd = 0, j = 0, i= 0;
216 const char *filename = getenv("PERS_CLIENT_LIB_CUSTOM_LOAD");
220 filename = "/etc/pclCustomLibConfigFile.cfg"; // use default filename
223 for(j=0; j<PersCustomLib_LastEntry; j++)
226 gCustomLibArray[j].valid = -1;
229 memset(&buffer, 0, sizeof(buffer));
230 status = stat(filename, &buffer);
233 gCustomConfigFileSize = buffer.st_size;
236 fd = open(filename, O_RDONLY);
239 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error ==> Error file open: "),
240 DLT_STRING(filename), DLT_STRING("err msg: "), DLT_STRING(strerror(errno)) );
245 // check for empty file
246 if(gCustomConfigFileSize >= 0)
248 // map the config file into memory
249 gpCustomConfigFileMap = (char*)mmap(0, gCustomConfigFileSize, PROT_WRITE, MAP_PRIVATE, fd, 0);
251 if (gpCustomConfigFileMap == MAP_FAILED)
253 gpCustomConfigFileMap = 0;
255 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error ==> Error mapping the file"));
259 // reset the token counter
260 gCustomTokenCounter = 0;
262 fillCustomCharTokenArray();
264 while( i < TOKENARRAYSIZE )
266 if(gpCustomTokenArray[i] != 0 && gpCustomTokenArray[i+1] != 0 && gpCustomTokenArray[i+2] != 0 &&gpCustomTokenArray[i+3] != 0 )
268 int libId = custom_client_name_to_id(gpCustomTokenArray[i], 0); // get the custom libID
270 // assign the libraryname
271 strncpy(gCustomLibArray[libId].libname, gpCustomTokenArray[i+1], CustLibMaxLen);
272 gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination
274 gCustomLibArray[libId].loadingType = getLoadingType(gpCustomTokenArray[i+2]);
275 gCustomLibArray[libId].initFunction = getInitType(gpCustomTokenArray[i+3]);
276 gCustomLibArray[libId].valid = 1; // marks as valid;
279 printf(" 1. => %s => %d \n", gpCustomTokenArray[i], libId);
280 printf(" 2. => %s => %s \n", gpCustomTokenArray[i+1], gCustomLibArray[libId].libname);
281 printf(" 3. => %s => %d \n", gpCustomTokenArray[i+2], (int)gCustomLibArray[libId].initFunction);
282 printf(" 4. => %s => %d \n\n", gpCustomTokenArray[i+3], (int)gCustomLibArray[libId].loadingType);
289 i+=4; // move to the next configuration file entry
296 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error ==> Error file size is 0"));
306 int load_custom_library(PersistenceCustomLibs_e customLib, Pers_custom_functs_s *customFuncts)
311 if(customLib < PersCustomLib_LastEntry)
313 void* handle = dlopen(gCustomLibArray[customLib].libname, RTLD_LAZY);
314 customFuncts->handle = handle;
318 dlerror(); // reset error
321 *(void **) (&customFuncts->custom_plugin_handle_close) = dlsym(handle, "plugin_handle_close");
322 if ((error = dlerror()) != NULL)
324 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
326 // custom_plugin_delete_data
327 *(void **) (&customFuncts->custom_plugin_delete_data) = dlsym(handle, "plugin_delete_data");
328 if ((error = dlerror()) != NULL)
330 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
332 // custom_plugin_get_data
333 *(void **) (&customFuncts->custom_plugin_handle_get_data) = dlsym(handle, "plugin_handle_get_data");
334 if ((error = dlerror()) != NULL)
336 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
338 // custom_plugin_get_data
339 *(void **) (&customFuncts->custom_plugin_get_data) = dlsym(handle, "plugin_get_data");
340 if ((error = dlerror()) != NULL)
342 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
344 // custom_plugin_init
345 *(void **) (&customFuncts->custom_plugin_init) = dlsym(handle, "plugin_init");
346 if ((error = dlerror()) != NULL)
348 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
350 // custom_plugin_deinit
351 *(void **) (&customFuncts->custom_plugin_deinit) = dlsym(handle, "plugin_deinit");
352 if ((error = dlerror()) != NULL)
354 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
356 // custom_plugin_open
357 *(void **) (&customFuncts->custom_plugin_handle_open) = dlsym(handle, "plugin_handle_open");
358 if ((error = dlerror()) != NULL)
360 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
362 // custom_plugin_set_data
363 *(void **) (&customFuncts->custom_plugin_handle_set_data) = dlsym(handle, "plugin_handle_set_data");
364 if ((error = dlerror()) != NULL)
366 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
368 // custom_plugin_set_data
369 *(void **) (&customFuncts->custom_plugin_set_data) = dlsym(handle, "plugin_set_data");
370 if ((error = dlerror()) != NULL)
372 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
374 // custom_plugin_get_size_handle
375 *(void **) (&customFuncts->custom_plugin_handle_get_size) = dlsym(handle, "plugin_handle_get_size");
376 if ((error = dlerror()) != NULL)
378 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
380 // custom_plugin_get_size
381 *(void **) (&customFuncts->custom_plugin_get_size) = dlsym(handle, "plugin_get_size");
382 if ((error = dlerror()) != NULL)
384 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
387 *(void **) (&customFuncts->custom_plugin_create_backup) = dlsym(handle, "plugin_create_backup");
388 if ((error = dlerror()) != NULL)
390 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
393 *(void **) (&customFuncts->custom_plugin_restore_backup) = dlsym(handle, "plugin_restore_backup");
394 if ((error = dlerror()) != NULL)
396 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
399 *(void **) (&customFuncts->custom_plugin_get_backup) = dlsym(handle, "plugin_get_backup");
400 if ((error = dlerror()) != NULL)
402 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
405 // custom_plugin_get_status_notification_clbk
406 *(void **) (&customFuncts->custom_plugin_get_status_notification_clbk) = dlsym(handle, "plugin_get_status_notification_clbk");
407 if ((error = dlerror()) != NULL)
409 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
412 // initialize plugin (non blocking)
413 *(void **) (&customFuncts->custom_plugin_init_async) = dlsym(handle, "plugin_init_async");
414 if ((error = dlerror()) != NULL)
416 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
420 *(void **) (&customFuncts->custom_plugin_clear_all_data) = dlsym(handle, "plugin_clear_all_data");
421 if ((error = dlerror()) != NULL)
423 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
427 *(void **) (&customFuncts->custom_plugin_sync) = dlsym(handle, "plugin_sync");
428 if ((error = dlerror()) != NULL)
430 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
436 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
437 rval = EPERS_DLOPENERROR;
442 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error: - customLib out of bounds"));
443 rval = EPERS_DLOPENERROR;
450 char* get_custom_client_lib_name(int idx)
452 if(idx < PersCustomLib_LastEntry)
454 return gCustomLibArray[idx].libname;
462 //int get_custom_client_position_in_array(int customLibId)
463 int check_valid_idx(int idx)
467 if(idx < PersCustomLib_LastEntry)
469 rval = gCustomLibArray[idx].valid;
477 void invalidate_custom_plugin(int idx)
479 gPersCustomFuncs[idx].handle = NULL;
480 gPersCustomFuncs[idx].custom_plugin_init = NULL;
481 gPersCustomFuncs[idx].custom_plugin_deinit = NULL;
482 gPersCustomFuncs[idx].custom_plugin_handle_open = NULL;
483 gPersCustomFuncs[idx].custom_plugin_handle_close = NULL;
484 gPersCustomFuncs[idx].custom_plugin_handle_get_data = NULL;
485 gPersCustomFuncs[idx].custom_plugin_handle_set_data = NULL;
486 gPersCustomFuncs[idx].custom_plugin_get_data = NULL;
487 gPersCustomFuncs[idx].custom_plugin_set_data = NULL;
488 gPersCustomFuncs[idx].custom_plugin_delete_data = NULL;
489 gPersCustomFuncs[idx].custom_plugin_get_status_notification_clbk = NULL;
490 gPersCustomFuncs[idx].custom_plugin_handle_get_size = NULL;
491 gPersCustomFuncs[idx].custom_plugin_get_size = NULL;
492 gPersCustomFuncs[idx].custom_plugin_create_backup = NULL;
493 gPersCustomFuncs[idx].custom_plugin_get_backup = NULL;
494 gPersCustomFuncs[idx].custom_plugin_restore_backup = NULL;