Changed file and key-value API according GENIVI naming conventions
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_custom_loader.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
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 ******************************************************************************/
11  /**
12  * @file           persistence_client_library_custom_loader.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of persistence custom loadedr
16  * @see
17  */
18
19 #include "persistence_client_library_custom_loader.h"
20 #include "../include_protected/persistence_client_library_data_organization.h"
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <dlfcn.h>
31
32 // array containing the id of the custom arrays
33 static int gCustomLibIdArray[PersCustomLib_LastEntry];
34
35 /// array with custom client library names
36 static char gCustomLibArray[PersCustomLib_LastEntry][CustLibMaxLen];
37 // number of libraries loaded
38 static int gNumOfCustomLibraries = 0;
39
40
41 PersistenceCustomLibs_e custom_client_name_to_id(const char* lib_name, int substring)
42 {
43    PersistenceCustomLibs_e libId = PersCustomLib_LastEntry;
44
45    if(substring == 0)
46    {
47       if(0 == strncmp(lib_name, "early", PersCustomPathSize) )
48       {
49          libId = PersCustomLib_early;
50       }
51       else if (0 == strncmp(lib_name, "secure", PersCustomPathSize) )
52       {
53          libId = PersCustomLib_secure;
54       }
55       else if (0 == strncmp(lib_name, "emergency", PersCustomPathSize) )
56       {
57          libId = PersCustomLib_emergency;
58       }
59       else if (0 == strncmp(lib_name, "hwinfo", PersCustomPathSize) )
60       {
61          libId = PersCustomLib_HWinfo;
62       }
63       else if (0 == strncmp(lib_name, "custom1", PersCustomPathSize) )
64       {
65          libId = PersCustomLib_Custom1;
66       }
67       else if (0 == strncmp(lib_name, "custom2", PersCustomPathSize) )
68       {
69          libId = PersCustomLib_Custom2;
70       }
71       else if (0 == strncmp(lib_name, "custom3", PersCustomPathSize) )
72       {
73          libId = PersCustomLib_Custom3;
74       }
75       else
76       {
77          printf("custom_libname_to_id - error - id not found for lib: %s \n", lib_name);
78       }
79    }
80    else
81    {
82       if(NULL != strstr(lib_name, "early") )
83       {
84          libId = PersCustomLib_early;
85       }
86       else if (NULL != strstr(lib_name, "secure") )
87       {
88          libId = PersCustomLib_secure;
89       }
90       else if (NULL != strstr(lib_name, "emergency") )
91       {
92          libId = PersCustomLib_emergency;
93       }
94       else if (NULL != strstr(lib_name, "hwinfo") )
95       {
96          libId = PersCustomLib_HWinfo;
97       }
98       else if (NULL != strstr(lib_name, "custom1") )
99       {
100          libId = PersCustomLib_Custom1;
101       }
102       else if (NULL != strstr(lib_name, "custom2") )
103       {
104          libId = PersCustomLib_Custom2;
105       }
106       else if (NULL != strstr(lib_name, "custom3") )
107       {
108          libId = PersCustomLib_Custom3;
109       }
110       else
111       {
112          printf("custom_libname_to_id - error - id not found for lib: %s \n", lib_name);
113       }
114
115    }
116    return libId;
117 }
118
119
120
121 int get_custom_libraries()
122 {
123    int rval = 0,
124          fd = 0,
125           i = 0;
126
127    struct stat buffer;
128    char* delimiters = " \n";   // search for blank and end of line
129    char* configFileMap = 0;
130    char* token = 0;
131    const char *filename = getenv("PERS_CLIENT_LIB_CUSTOM_LOAD");
132
133    if(filename == NULL)
134    {
135       filename = "customLibConfigFile.cfg";  // use default filename
136    }
137
138    if(stat(filename, &buffer) != -1)
139    {
140       fd = open(filename, O_RDONLY);
141       if (fd != -1)
142       {
143          configFileMap = (char*)mmap(0, buffer.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
144
145          if(configFileMap != MAP_FAILED)
146          {
147             int libId = 0;
148
149             // get the library identifier (early, secure, emergency, ...)
150             token = strtok(configFileMap, delimiters);
151             libId = custom_client_name_to_id(token, 0);
152
153             if(libId < PersCustomLib_LastEntry)
154             {
155                gCustomLibIdArray[libId] = i;
156             }
157             else
158             {
159                 return EPERS_OUTOFBOUNDS; // out of array bounds
160             }
161
162             // get the library name
163             token  = strtok (NULL, delimiters);
164             strncpy(gCustomLibArray[i++], token, CustLibMaxLen);
165
166             while( token != NULL )
167             {
168                // get the library identifier (early, secure, emergency, ...)
169                token = strtok(NULL, delimiters);
170                if(token != NULL)
171                {
172                   libId = custom_client_name_to_id(token, 0);
173                   if(libId < PersCustomLib_LastEntry)
174                   {
175                      gCustomLibIdArray[libId] = i;
176                   }
177                   else
178                   {
179                      rval = EPERS_OUTOFBOUNDS;
180                      break;
181                   }
182                }
183                else
184                {
185                   break;
186                }
187
188                // get the library name
189                token  = strtok (NULL, delimiters);
190                if(token != NULL)
191                {
192                   strncpy(gCustomLibArray[i++], token, CustLibMaxLen);
193                }
194                else
195                {
196                   break;
197                }
198             }
199             gNumOfCustomLibraries = i;    // remember the number of loaded libraries
200
201             munmap(configFileMap, buffer.st_size);
202             close(fd);
203
204             // debugging only
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]);
208
209             for(i=0; i<PersCustomLib_LastEntry; i++)
210                printf("get_custom_libraries - id: %d | pos: %d \n", i, gCustomLibIdArray[i]); */
211          }
212          else
213          {
214             rval = EPERS_CONFIGMAPFAILED;
215             printf("load config file error - mapping of file failed");
216          }
217       }
218       else
219       {
220          rval = EPERS_CONFIGNOTAVAILABLE;
221          printf("load config file error - no file with plugins available -> filename: %s | error: %s \n", filename, strerror(errno) );
222       }
223    }
224    else
225    {
226       rval = EPERS_CONFIGNOSTAT;
227       printf("load config file error - can't stat config file: %s | %s \n", filename, strerror(errno));
228    }
229    return rval;
230 }
231
232
233
234 int load_custom_library(PersistenceCustomLibs_e customLib, Pers_custom_functs_s *customFuncts)
235 {
236    int rval = 0;
237    char *error;
238
239    if(customLib < PersCustomLib_LastEntry)
240    {
241       void* handle = dlopen(gCustomLibArray[customLib], RTLD_LAZY);
242       customFuncts->handle = handle;
243       if(handle != NULL)
244       {
245          dlerror();    // reset error
246
247          // plugin_close
248          *(void **) (&customFuncts->custom_plugin_handle_close) = dlsym(handle, "plugin_handle_close");
249          if ((error = dlerror()) != NULL)
250          {
251               printf("load_custom_library - error: %s\n", error);
252          }
253          // custom_plugin_delete_data
254          *(void **) (&customFuncts->custom_plugin_delete_data) = dlsym(handle, "plugin_delete_data");
255          if ((error = dlerror()) != NULL)
256          {
257               printf("load_custom_library - error: %s\n", error);
258           }
259          // custom_plugin_get_data
260          *(void **) (&customFuncts->custom_plugin_handle_get_data) = dlsym(handle, "plugin_handle_get_data");
261          if ((error = dlerror()) != NULL)
262          {
263               printf("load_custom_library - error: %s\n", error);
264
265           }
266          // custom_plugin_get_data
267          *(void **) (&customFuncts->custom_plugin_get_data) = dlsym(handle, "plugin_get_data");
268          if ((error = dlerror()) != NULL)
269          {
270               printf("load_custom_library - error: %s\n", error);
271           }
272          // custom_plugin_init
273          *(void **) (&customFuncts->custom_plugin_init) = dlsym(handle, "plugin_init");
274          if ((error = dlerror()) != NULL)
275          {
276               printf("load_custom_library - error: %s\n", error);
277
278           }
279          // custom_plugin_deinit
280          *(void **) (&customFuncts->custom_plugin_deinit) = dlsym(handle, "plugin_deinit");
281          if ((error = dlerror()) != NULL)
282          {
283               printf("load_custom_library - error: %s\n", error);
284           }
285          // custom_plugin_open
286          *(void **) (&customFuncts->custom_plugin_handle_open) = dlsym(handle, "plugin_handle_open");
287          if ((error = dlerror()) != NULL)
288          {
289               printf("load_custom_library - error: %s\n", error);
290           }
291          // custom_plugin_set_data
292          *(void **) (&customFuncts->custom_plugin_handle_set_data) = dlsym(handle, "plugin_handle_set_data");
293          if ((error = dlerror()) != NULL)
294          {
295               printf("load_custom_library - error: %s\n", error);
296           }
297          // custom_plugin_set_data
298          *(void **) (&customFuncts->custom_plugin_set_data) = dlsym(handle, "plugin_set_data");
299          if ((error = dlerror()) != NULL)
300          {
301               printf("load_custom_library - error: %s\n", error);
302          }
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)
306          {
307             printf("load_custom_library - error: %s\n", error);
308          }
309          // custom_plugin_get_size
310          *(void **) (&customFuncts->custom_plugin_get_size) = dlsym(handle, "plugin_get_size");
311          if ((error = dlerror()) != NULL)
312          {
313             printf("load_custom_library - error: %s\n", error);
314          }
315           // create backup
316          *(void **) (&customFuncts->custom_plugin_create_backup) = dlsym(handle, "plugin_create_backup");
317          if ((error = dlerror()) != NULL)
318          {
319             printf("load_custom_library - error: %s\n", error);
320          }
321          // restore backup
322          *(void **) (&customFuncts->custom_plugin_restore_backup) = dlsym(handle, "plugin_restore_backup");
323          if ((error = dlerror()) != NULL)
324          {
325              printf("load_custom_library - error: %s\n", error);
326          }
327          // restore backup
328          *(void **) (&customFuncts->custom_plugin_get_backup) = dlsym(handle, "plugin_get_backup");
329          if ((error = dlerror()) != NULL)
330          {
331              printf("load_custom_library - error: %s\n", error);
332          }
333
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)
337          {
338               printf("load_custom_library - error: %s\n", error);
339           }
340       }
341       else
342       {
343          printf("load_custom_library - error: %s\n", dlerror());
344          rval = EPERS_DLOPENERROR;
345       }
346    }
347
348    return rval;
349 }
350
351
352
353 int load_all_custom_libraries()
354 {
355    int rval = 0,
356           i = 0;
357
358    for(i=0; i<gNumOfCustomLibraries; i++)
359    {
360       rval = load_custom_library(i, &gPersCustomFuncs[i]);
361       if( rval < 0)
362       {
363          // printf("load_all_custom_libraries - error loading library number [%d] \n", i);
364          break;
365       }
366    }
367    return rval;
368 }
369
370
371 char* get_custom_client_lib_name(int idx)
372 {
373    if(idx < PersCustomLib_LastEntry)
374    {
375       return gCustomLibArray[idx];
376    }
377    else
378    {
379       return NULL;
380    }
381 }
382
383 int get_custom_client_position_in_array(PersistenceCustomLibs_e customLibId)
384 {
385    //printf("get_position_in_array - id: %d | position: %d \n", customLibId, gCustomLibIdArray[(int)customLibId]);
386    if(customLibId < PersCustomLib_LastEntry)
387    {
388       return gCustomLibIdArray[(int)customLibId];
389    }
390    else
391    {
392       return -1;
393    }
394 }
395
396
397 int get_num_custom_client_libs()
398 {
399    return gNumOfCustomLibraries;
400 }