52168ba720da50e3989d786c8e4baa2929ddea9a
[profile/ivi/persistence-client-library.git] / src / persistence_client_library.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.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of the persistence client library.
16  *                 Library provides an API to access persistent data
17  * @see            
18  */
19
20
21 #include "persistence_client_library_lc_interface.h"
22 #include "persistence_client_library_pas_interface.h"
23 #include "persistence_client_library_dbus_service.h"
24 #include "persistence_client_library_handle.h"
25 #include "persistence_client_library_custom_loader.h"
26 #include "persistence_client_library.h"
27
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <dlfcn.h>
32
33 #include <dlt/dlt.h>
34 #include <dlt/dlt_common.h>
35
36 #include <dbus/dbus.h>
37
38 #define USE_DBUS 1
39
40 /// debug log and trace (DLT) setup
41 DLT_DECLARE_CONTEXT(gDLTContext);
42
43 static int gShutdownMode = 0;
44
45 /// loical function declaration
46 void invalidateCustomPlugin(int idx);
47
48 int pclInitLibrary(const char* appName, int shutdownMode)
49 {
50    int status = 0;
51    int i = 0, rval = 0;
52
53    if(gPclInitialized == PCLnotInitialized)
54    {
55       gPclInitialized++;
56
57       gShutdownMode = shutdownMode;
58
59       DLT_REGISTER_CONTEXT(gDLTContext,"pers","Context for persistence client library logging");
60       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
61                            DLT_STRING("- init counter: "), DLT_INT(gPclInitialized) );
62
63       /// environment variable for on demand loading of custom libraries
64       const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND");
65
66       /// environment variable for max key value data
67       const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
68
69       if(pDataSize != NULL)
70       {
71          gMaxKeyValDataSize = atoi(pDataSize);
72       }
73
74       setup_dbus_mainloop();
75
76 #if USE_DBUS
77       // register for lifecycle and persistence admin service dbus messages
78       register_lifecycle(shutdownMode);
79       register_pers_admin_service();
80 #endif
81
82       // clear the open file descriptor array
83       memset(gOpenFdArray, 0, MaxPersHandle * sizeof(int));
84
85       /// get custom library names to load
86       status = get_custom_libraries();
87       if(status >= 0)
88       {
89          // initialize custom library structure
90          for(i = 0; i < PersCustomLib_LastEntry; i++)
91          {
92             invalidateCustomPlugin(i);
93          }
94
95          if(pOnDemandLoad == NULL)  // load all available libraries now
96          {
97             for(i=0; i < PersCustomLib_LastEntry; i++ )
98             {
99                if(check_valid_idx(i) != -1)
100                {
101                   if(load_custom_library(i, &gPersCustomFuncs[i] ) == 1)
102                   {
103                      if( (gPersCustomFuncs[i].custom_plugin_init) != NULL)
104                      {
105                         DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => Loaded plugin: "),
106                                                            DLT_STRING(get_custom_client_lib_name(i)));
107                         gPersCustomFuncs[i].custom_plugin_init();
108                      }
109                      else
110                      {
111                         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin functions: "),
112                                                             DLT_STRING(get_custom_client_lib_name(i)));
113                      }
114                   }
115                   else
116                   {
117                      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclInitLibrary => E r r o r could not load plugin: "),
118                                           DLT_STRING(get_custom_client_lib_name(i)));
119                   }
120                }
121                else
122                {
123                   continue;
124                }
125             }
126          }
127       }
128       else
129       {
130          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInit => Failed to load custom library config table => error number:"), DLT_INT(status));
131       }
132
133
134       // assign application name
135       strncpy(gAppId, appName, MaxAppNameLen);
136       gAppId[MaxAppNameLen-1] = '\0';
137
138       // destory mutex
139       pthread_mutex_destroy(&gDbusInitializedMtx);
140       pthread_cond_destroy(&gDbusInitializedCond);
141
142       rval = 1;
143    }
144    else if(gPclInitialized >= PCLinitialized)
145    {
146       gPclInitialized++; // increment init counter
147       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclInitLibrary => I N I T  Persistence Client Library - "), DLT_STRING(gAppId),
148                            DLT_STRING("- ONLY INCREMENT init counter: "), DLT_INT(gPclInitialized) );
149    }
150
151    return rval;
152 }
153
154
155
156 int pclDeinitLibrary(void)
157 {
158    int i = 0, rval = 1;
159
160    if(gPclInitialized == PCLinitialized)
161    {
162       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
163                                            DLT_STRING("- init counter: "), DLT_INT(gPclInitialized));
164
165       // unregister for lifecycle and persistence admin service dbus messages
166    #if USE_DBUS
167       unregister_lifecycle(gShutdownMode);
168       unregister_pers_admin_service();
169    #endif
170
171       // unload custom client libraries
172       for(i=0; i<PersCustomLib_LastEntry; i++)
173       {
174          if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
175          {
176             // deinitialize plugin
177             gPersCustomFuncs[i].custom_plugin_deinit();
178             // close library handle
179             dlclose(gPersCustomFuncs[i].handle);
180
181             invalidateCustomPlugin(i);
182          }
183       }
184
185       gPclInitialized = PCLnotInitialized;
186
187       DLT_UNREGISTER_CONTEXT(gDLTContext);
188    }
189    else if(gPclInitialized > PCLinitialized)
190    {
191       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclDeinitLibrary -> D E I N I T  client library - "), DLT_STRING(gAppId),
192                                            DLT_STRING("- ONLY DECREMENT init counter: "), DLT_INT(gPclInitialized));
193       gPclInitialized--;   // decrement init counter
194    }
195
196    return rval;
197 }
198
199
200 void invalidateCustomPlugin(int idx)
201 {
202    gPersCustomFuncs[idx].handle  = NULL;
203    gPersCustomFuncs[idx].custom_plugin_init = NULL;
204    gPersCustomFuncs[idx].custom_plugin_deinit = NULL;
205    gPersCustomFuncs[idx].custom_plugin_handle_open = NULL;
206    gPersCustomFuncs[idx].custom_plugin_handle_close = NULL;
207    gPersCustomFuncs[idx].custom_plugin_handle_get_data = NULL;
208    gPersCustomFuncs[idx].custom_plugin_handle_set_data  = NULL;
209    gPersCustomFuncs[idx].custom_plugin_get_data = NULL;
210    gPersCustomFuncs[idx].custom_plugin_set_data = NULL;
211    gPersCustomFuncs[idx].custom_plugin_delete_data = NULL;
212    gPersCustomFuncs[idx].custom_plugin_get_status_notification_clbk = NULL;
213    gPersCustomFuncs[idx].custom_plugin_handle_get_size = NULL;
214    gPersCustomFuncs[idx].custom_plugin_get_size = NULL;
215    gPersCustomFuncs[idx].custom_plugin_create_backup = NULL;
216    gPersCustomFuncs[idx].custom_plugin_get_backup = NULL;
217    gPersCustomFuncs[idx].custom_plugin_restore_backup = NULL;
218 }
219
220
221
222
223