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];
41 /// array with custom client library names
42 static PersCustomLibInfo gCustomLibArray[PersCustomLib_LastEntry];
45 PersistenceCustomLibs_e custom_client_name_to_id(const char* lib_name, int substring)
47 PersistenceCustomLibs_e libId = PersCustomLib_LastEntry;
51 if(0 == strncmp(lib_name, "early", PersCustomPathSize) )
53 libId = PersCustomLib_early;
55 else if (0 == strncmp(lib_name, "secure", PersCustomPathSize) )
57 libId = PersCustomLib_secure;
59 else if (0 == strncmp(lib_name, "emergency", PersCustomPathSize) )
61 libId = PersCustomLib_emergency;
63 else if (0 == strncmp(lib_name, "hwinfo", PersCustomPathSize) )
65 libId = PersCustomLib_HWinfo;
67 else if (0 == strncmp(lib_name, "custom1", PersCustomPathSize) )
69 libId = PersCustomLib_Custom1;
71 else if (0 == strncmp(lib_name, "custom2", PersCustomPathSize) )
73 libId = PersCustomLib_Custom2;
75 else if (0 == strncmp(lib_name, "custom3", PersCustomPathSize) )
77 libId = PersCustomLib_Custom3;
81 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("custom_libname_to_id - error - id not found for lib:"), DLT_STRING(lib_name));
86 if(NULL != strstr(lib_name, "early") )
88 libId = PersCustomLib_early;
90 else if (NULL != strstr(lib_name, "secure") )
92 libId = PersCustomLib_secure;
94 else if (NULL != strstr(lib_name, "emergency") )
96 libId = PersCustomLib_emergency;
98 else if (NULL != strstr(lib_name, "hwinfo") )
100 libId = PersCustomLib_HWinfo;
102 else if (NULL != strstr(lib_name, "custom1") )
104 libId = PersCustomLib_Custom1;
106 else if (NULL != strstr(lib_name, "custom2") )
108 libId = PersCustomLib_Custom2;
110 else if (NULL != strstr(lib_name, "custom3") )
112 libId = PersCustomLib_Custom3;
116 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("custom_libname_to_id - error - id not found for lib:"), DLT_STRING(lib_name));
125 int get_custom_libraries()
127 int rval = 0, fd = 0, j = 0;
130 char* delimiters = " \n"; // search for blank and end of line
131 char* configFileMap = 0;
133 const char *filename = getenv("PERS_CLIENT_LIB_CUSTOM_LOAD");
137 filename = "/etc/pclCustomLibConfigFile.cfg"; // use default filename
140 for(j=0; j<PersCustomLib_LastEntry; j++)
143 gCustomLibArray[j].valid = -1;
147 if(stat(filename, &buffer) != -1)
149 if(buffer.st_size > 20) // file needs to be at least bigger then 20 bytes
151 fd = open(filename, O_RDONLY);
154 configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
156 if(configFileMap != MAP_FAILED)
160 // get the library identifier (early, secure, emergency, ...)
161 token = strtok(configFileMap, delimiters);
162 libId = custom_client_name_to_id(token, 0);
165 if(libId < PersCustomLib_LastEntry)
167 gCustomLibArray[libId].valid = 1;
171 munmap(configFileMap, buffer.st_size); // @CB: Add
172 close(fd); // @CB: Add // close file descriptor before return
173 return EPERS_OUTOFBOUNDS; // out of array bounds
176 // get the library name
177 token = strtok (NULL, delimiters);
178 strncpy(gCustomLibArray[libId].libname, token, CustLibMaxLen);
179 gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination
181 while( token != NULL )
183 // get the library identifier (early, secure, emergency, ...)
184 token = strtok(NULL, delimiters);
187 libId = custom_client_name_to_id(token, 0);
188 if(libId < PersCustomLib_LastEntry)
190 gCustomLibArray[libId].valid = 1;
194 rval = EPERS_OUTOFBOUNDS;
203 // get the library name
204 token = strtok (NULL, delimiters);
207 strncpy(gCustomLibArray[libId].libname, token, CustLibMaxLen);
208 gCustomLibArray[libId].libname[CustLibMaxLen-1] = '\0'; // Ensures 0-Termination
216 munmap(configFileMap, buffer.st_size);
219 for(j=0; j<PersCustomLib_LastEntry; j++)
221 printf("Custom libraries => Name: %s | valid: %d \n", gCustomLibArray[j].libname, gCustomLibArray[j].valid);
227 rval = EPERS_CONFIGMAPFAILED;
228 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error - mapping of file failed"));
234 rval = EPERS_CONFIGNOTAVAILABLE;
235 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error - no file with plugins available:"), DLT_STRING(filename), DLT_STRING(strerror(errno)));
240 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load config file error - invalid file size"), DLT_STRING(filename), DLT_STRING(strerror(errno)));
245 rval = EPERS_CONFIGNOSTAT;
246 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("lload config file error - can't stat config file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)));
253 int load_custom_library(PersistenceCustomLibs_e customLib, Pers_custom_functs_s *customFuncts)
258 if(customLib < PersCustomLib_LastEntry)
260 void* handle = dlopen(gCustomLibArray[customLib].libname, RTLD_LAZY);
261 customFuncts->handle = handle;
265 dlerror(); // reset error
268 *(void **) (&customFuncts->custom_plugin_handle_close) = dlsym(handle, "plugin_handle_close");
269 if ((error = dlerror()) != NULL)
271 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
273 // custom_plugin_delete_data
274 *(void **) (&customFuncts->custom_plugin_delete_data) = dlsym(handle, "plugin_delete_data");
275 if ((error = dlerror()) != NULL)
277 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
279 // custom_plugin_get_data
280 *(void **) (&customFuncts->custom_plugin_handle_get_data) = dlsym(handle, "plugin_handle_get_data");
281 if ((error = dlerror()) != NULL)
283 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
285 // custom_plugin_get_data
286 *(void **) (&customFuncts->custom_plugin_get_data) = dlsym(handle, "plugin_get_data");
287 if ((error = dlerror()) != NULL)
289 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
291 // custom_plugin_init
292 *(void **) (&customFuncts->custom_plugin_init) = dlsym(handle, "plugin_init");
293 if ((error = dlerror()) != NULL)
295 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
297 // custom_plugin_deinit
298 *(void **) (&customFuncts->custom_plugin_deinit) = dlsym(handle, "plugin_deinit");
299 if ((error = dlerror()) != NULL)
301 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
303 // custom_plugin_open
304 *(void **) (&customFuncts->custom_plugin_handle_open) = dlsym(handle, "plugin_handle_open");
305 if ((error = dlerror()) != NULL)
307 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
309 // custom_plugin_set_data
310 *(void **) (&customFuncts->custom_plugin_handle_set_data) = dlsym(handle, "plugin_handle_set_data");
311 if ((error = dlerror()) != NULL)
313 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
315 // custom_plugin_set_data
316 *(void **) (&customFuncts->custom_plugin_set_data) = dlsym(handle, "plugin_set_data");
317 if ((error = dlerror()) != NULL)
319 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
321 // custom_plugin_get_size_handle
322 *(void **) (&customFuncts->custom_plugin_handle_get_size) = dlsym(handle, "plugin_handle_get_size");
323 if ((error = dlerror()) != NULL)
325 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
327 // custom_plugin_get_size
328 *(void **) (&customFuncts->custom_plugin_get_size) = dlsym(handle, "plugin_get_size");
329 if ((error = dlerror()) != NULL)
331 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
334 *(void **) (&customFuncts->custom_plugin_create_backup) = dlsym(handle, "plugin_create_backup");
335 if ((error = dlerror()) != NULL)
337 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
340 *(void **) (&customFuncts->custom_plugin_restore_backup) = dlsym(handle, "plugin_restore_backup");
341 if ((error = dlerror()) != NULL)
343 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
346 *(void **) (&customFuncts->custom_plugin_get_backup) = dlsym(handle, "plugin_get_backup");
347 if ((error = dlerror()) != NULL)
349 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
352 // custom_plugin_get_status_notification_clbk
353 *(void **) (&customFuncts->custom_plugin_get_status_notification_clbk) = dlsym(handle, "plugin_get_status_notification_clbk");
354 if ((error = dlerror()) != NULL)
356 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
359 // initialize plugin (non blocking)
360 *(void **) (&customFuncts->custom_plugin_init_async) = dlsym(handle, "plugin_init_async");
361 if ((error = dlerror()) != NULL)
363 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
367 *(void **) (&customFuncts->custom_plugin_clear_all_data) = dlsym(handle, "plugin_clear_all_data");
368 if ((error = dlerror()) != NULL)
370 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
374 *(void **) (&customFuncts->custom_plugin_sync) = dlsym(handle, "plugin_sync");
375 if ((error = dlerror()) != NULL)
377 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
383 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error:"), DLT_STRING(error));
384 rval = EPERS_DLOPENERROR;
389 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("load_custom_library - error: - customLib out of bounds"));
390 rval = EPERS_DLOPENERROR;
398 int load_all_custom_libraries()
403 for(i=0; i<PersCustomLib_LastEntry; i++)
405 rval = load_custom_library(i, &gPersCustomFuncs[i]);
415 char* get_custom_client_lib_name(int idx)
417 if(idx < PersCustomLib_LastEntry)
419 return gCustomLibArray[idx].libname;
427 //int get_custom_client_position_in_array(int customLibId)
428 int check_valid_idx(int idx)
432 if(idx < PersCustomLib_LastEntry)
434 rval = gCustomLibArray[idx].valid;
442 void invalidate_custom_plugin(int idx)
444 gPersCustomFuncs[idx].handle = NULL;
445 gPersCustomFuncs[idx].custom_plugin_init = NULL;
446 gPersCustomFuncs[idx].custom_plugin_deinit = NULL;
447 gPersCustomFuncs[idx].custom_plugin_handle_open = NULL;
448 gPersCustomFuncs[idx].custom_plugin_handle_close = NULL;
449 gPersCustomFuncs[idx].custom_plugin_handle_get_data = NULL;
450 gPersCustomFuncs[idx].custom_plugin_handle_set_data = NULL;
451 gPersCustomFuncs[idx].custom_plugin_get_data = NULL;
452 gPersCustomFuncs[idx].custom_plugin_set_data = NULL;
453 gPersCustomFuncs[idx].custom_plugin_delete_data = NULL;
454 gPersCustomFuncs[idx].custom_plugin_get_status_notification_clbk = NULL;
455 gPersCustomFuncs[idx].custom_plugin_handle_get_size = NULL;
456 gPersCustomFuncs[idx].custom_plugin_get_size = NULL;
457 gPersCustomFuncs[idx].custom_plugin_create_backup = NULL;
458 gPersCustomFuncs[idx].custom_plugin_get_backup = NULL;
459 gPersCustomFuncs[idx].custom_plugin_restore_backup = NULL;