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 "../include_protected/persistence_client_library_data_organization.h"
32 // array containing the id of the custom arrays
33 static int gCustomLibIdArray[PersCustomLib_LastEntry];
35 /// array with custom client library names
36 static char gCustomLibArray[PersCustomLib_LastEntry][CustLibMaxLen];
37 // number of libraries loaded
38 static int gNumOfCustomLibraries = 0;
41 PersistenceCustomLibs_e custom_client_name_to_id(const char* lib_name, int substring)
43 PersistenceCustomLibs_e libId = PersCustomLib_LastEntry;
47 if(0 == strncmp(lib_name, "early", PersCustomPathSize) )
49 libId = PersCustomLib_early;
51 else if (0 == strncmp(lib_name, "secure", PersCustomPathSize) )
53 libId = PersCustomLib_secure;
55 else if (0 == strncmp(lib_name, "emergency", PersCustomPathSize) )
57 libId = PersCustomLib_emergency;
59 else if (0 == strncmp(lib_name, "hwinfo", PersCustomPathSize) )
61 libId = PersCustomLib_HWinfo;
63 else if (0 == strncmp(lib_name, "custom1", PersCustomPathSize) )
65 libId = PersCustomLib_Custom1;
67 else if (0 == strncmp(lib_name, "custom2", PersCustomPathSize) )
69 libId = PersCustomLib_Custom2;
71 else if (0 == strncmp(lib_name, "custom3", PersCustomPathSize) )
73 libId = PersCustomLib_Custom3;
77 printf("custom_libname_to_id - error - id not found for lib: %s \n", lib_name);
82 if(NULL != strstr(lib_name, "early") )
84 libId = PersCustomLib_early;
86 else if (NULL != strstr(lib_name, "secure") )
88 libId = PersCustomLib_secure;
90 else if (NULL != strstr(lib_name, "emergency") )
92 libId = PersCustomLib_emergency;
94 else if (NULL != strstr(lib_name, "hwinfo") )
96 libId = PersCustomLib_HWinfo;
98 else if (NULL != strstr(lib_name, "custom1") )
100 libId = PersCustomLib_Custom1;
102 else if (NULL != strstr(lib_name, "custom2") )
104 libId = PersCustomLib_Custom2;
106 else if (NULL != strstr(lib_name, "custom3") )
108 libId = PersCustomLib_Custom3;
112 printf("custom_libname_to_id - error - id not found for lib: %s \n", lib_name);
121 int get_custom_libraries()
128 char* delimiters = " \n"; // search for blank and end of line
129 char* configFileMap = 0;
131 const char *filename = getenv("PERS_CLIENT_LIB_CUSTOM_LOAD");
135 filename = "customLibConfigFile.cfg"; // use default filename
138 if(stat(filename, &buffer) != -1)
140 fd = open(filename, O_RDONLY);
143 configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
145 if(configFileMap != MAP_FAILED)
149 // get the library identifier (early, secure, emergency, ...)
150 token = strtok(configFileMap, delimiters);
151 libId = custom_client_name_to_id(token, 0);
153 if(libId < PersCustomLib_LastEntry)
155 gCustomLibIdArray[libId] = i;
159 return EPERS_OUTOFBOUNDS; // out of array bounds
162 // get the library name
163 token = strtok (NULL, delimiters);
164 strncpy(gCustomLibArray[i++], token, CustLibMaxLen);
166 while( token != NULL )
168 // get the library identifier (early, secure, emergency, ...)
169 token = strtok(NULL, delimiters);
172 libId = custom_client_name_to_id(token, 0);
173 if(libId < PersCustomLib_LastEntry)
175 gCustomLibIdArray[libId] = i;
179 rval = EPERS_OUTOFBOUNDS;
188 // get the library name
189 token = strtok (NULL, delimiters);
192 strncpy(gCustomLibArray[i++], token, CustLibMaxLen);
199 gNumOfCustomLibraries = i; // remember the number of loaded libraries
201 munmap(configFileMap, buffer.st_size);
205 /* printf("get_custom_libraries - found [ %d ] libraries \n", gNumOfCustomLibraries);
206 for(i=0; i< gNumOfCustomLibraries; i++)
207 printf("get_custom_libraries - names: %s\n", gCustomLibArray[i]);
209 for(i=0; i<PersCustomLib_LastEntry; i++)
210 printf("get_custom_libraries - id: %d | pos: %d \n", i, gCustomLibIdArray[i]); */
214 rval = EPERS_CONFIGMAPFAILED;
215 printf("load config file error - mapping of file failed");
220 rval = EPERS_CONFIGNOTAVAILABLE;
221 printf("load config file error - no file with plugins available -> filename: %s | error: %s \n", filename, strerror(errno) );
226 rval = EPERS_CONFIGNOSTAT;
227 printf("load config file error - can't stat config file: %s | %s \n", filename, strerror(errno));
234 int load_custom_library(PersistenceCustomLibs_e customLib, Pers_custom_functs_s *customFuncts)
239 if(customLib < PersCustomLib_LastEntry)
241 void* handle = dlopen(gCustomLibArray[customLib], RTLD_LAZY);
242 customFuncts->handle = handle;
245 dlerror(); // reset error
248 *(void **) (&customFuncts->custom_plugin_handle_close) = dlsym(handle, "plugin_handle_close");
249 if ((error = dlerror()) != NULL)
251 printf("load_custom_library - error: %s\n", error);
253 // custom_plugin_delete_data
254 *(void **) (&customFuncts->custom_plugin_delete_data) = dlsym(handle, "plugin_delete_data");
255 if ((error = dlerror()) != NULL)
257 printf("load_custom_library - error: %s\n", error);
259 // custom_plugin_get_data
260 *(void **) (&customFuncts->custom_plugin_handle_get_data) = dlsym(handle, "plugin_handle_get_data");
261 if ((error = dlerror()) != NULL)
263 printf("load_custom_library - error: %s\n", error);
266 // custom_plugin_get_data
267 *(void **) (&customFuncts->custom_plugin_get_data) = dlsym(handle, "plugin_get_data");
268 if ((error = dlerror()) != NULL)
270 printf("load_custom_library - error: %s\n", error);
272 // custom_plugin_init
273 *(void **) (&customFuncts->custom_plugin_init) = dlsym(handle, "plugin_init");
274 if ((error = dlerror()) != NULL)
276 printf("load_custom_library - error: %s\n", error);
279 // custom_plugin_deinit
280 *(void **) (&customFuncts->custom_plugin_deinit) = dlsym(handle, "plugin_deinit");
281 if ((error = dlerror()) != NULL)
283 printf("load_custom_library - error: %s\n", error);
285 // custom_plugin_open
286 *(void **) (&customFuncts->custom_plugin_handle_open) = dlsym(handle, "plugin_handle_open");
287 if ((error = dlerror()) != NULL)
289 printf("load_custom_library - error: %s\n", error);
291 // custom_plugin_set_data
292 *(void **) (&customFuncts->custom_plugin_handle_set_data) = dlsym(handle, "plugin_handle_set_data");
293 if ((error = dlerror()) != NULL)
295 printf("load_custom_library - error: %s\n", error);
297 // custom_plugin_set_data
298 *(void **) (&customFuncts->custom_plugin_set_data) = dlsym(handle, "plugin_set_data");
299 if ((error = dlerror()) != NULL)
301 printf("load_custom_library - error: %s\n", error);
303 // custom_plugin_get_size_handle
304 *(void **) (&customFuncts->custom_plugin_handle_get_size) = dlsym(handle, "plugin_get_size_handle");
305 if ((error = dlerror()) != NULL)
307 printf("load_custom_library - error: %s\n", error);
309 // custom_plugin_get_size
310 *(void **) (&customFuncts->custom_plugin_get_size) = dlsym(handle, "plugin_get_size");
311 if ((error = dlerror()) != NULL)
313 printf("load_custom_library - error: %s\n", error);
316 *(void **) (&customFuncts->custom_plugin_create_backup) = dlsym(handle, "plugin_create_backup");
317 if ((error = dlerror()) != NULL)
319 printf("load_custom_library - error: %s\n", error);
322 *(void **) (&customFuncts->custom_plugin_restore_backup) = dlsym(handle, "plugin_restore_backup");
323 if ((error = dlerror()) != NULL)
325 printf("load_custom_library - error: %s\n", error);
328 *(void **) (&customFuncts->custom_plugin_get_backup) = dlsym(handle, "plugin_get_backup");
329 if ((error = dlerror()) != NULL)
331 printf("load_custom_library - error: %s\n", error);
334 // custom_plugin_get_status_notification_clbk
335 *(void **) (&customFuncts->custom_plugin_get_status_notification_clbk) = dlsym(handle, "plugin_get_status_notification_clbk");
336 if ((error = dlerror()) != NULL)
338 printf("load_custom_library - error: %s\n", error);
343 printf("load_custom_library - error: %s\n", dlerror());
344 rval = EPERS_DLOPENERROR;
353 int load_all_custom_libraries()
358 for(i=0; i<gNumOfCustomLibraries; i++)
360 rval = load_custom_library(i, &gPersCustomFuncs[i]);
363 // printf("load_all_custom_libraries - error loading library number [%d] \n", i);
371 char* get_custom_client_lib_name(int idx)
373 if(idx < PersCustomLib_LastEntry)
375 return gCustomLibArray[idx];
383 int get_custom_client_position_in_array(PersistenceCustomLibs_e customLibId)
385 //printf("get_position_in_array - id: %d | position: %d \n", customLibId, gCustomLibIdArray[(int)customLibId]);
386 if(customLibId < PersCustomLib_LastEntry)
388 return gCustomLibIdArray[(int)customLibId];
397 int get_num_custom_client_libs()
399 return gNumOfCustomLibraries;