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.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
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"
34 #include <dlt/dlt_common.h>
36 #include <dbus/dbus.h>
40 /// debug log and trace (DLT) setup
41 DLT_DECLARE_CONTEXT(gDLTContext);
43 static int gShutdownMode = 0;
45 /// loical function declaration
46 void invalidateCustomPlugin(int idx);
48 int pclInitLibrary(const char* appName, int shutdownMode)
53 if(gPclInitialized == PCLnotInitialized)
57 gShutdownMode = shutdownMode;
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) );
63 /// environment variable for on demand loading of custom libraries
64 const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND");
66 /// environment variable for max key value data
67 const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
71 gMaxKeyValDataSize = atoi(pDataSize);
74 setup_dbus_mainloop();
77 // register for lifecycle and persistence admin service dbus messages
78 register_lifecycle(shutdownMode);
79 register_pers_admin_service();
82 // clear the open file descriptor array
83 memset(gOpenFdArray, 0, MaxPersHandle * sizeof(int));
85 /// get custom library names to load
86 status = get_custom_libraries();
89 // initialize custom library structure
90 for(i = 0; i < PersCustomLib_LastEntry; i++)
92 invalidateCustomPlugin(i);
95 if(pOnDemandLoad == NULL) // load all available libraries now
97 for(i=0; i < PersCustomLib_LastEntry; i++ )
99 if(check_valid_idx(i) != -1)
101 if(load_custom_library(i, &gPersCustomFuncs[i] ) == 1)
103 if( (gPersCustomFuncs[i].custom_plugin_init) != NULL)
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();
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)));
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)));
130 DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("pclInit => Failed to load custom library config table => error number:"), DLT_INT(status));
134 // assign application name
135 strncpy(gAppId, appName, MaxAppNameLen);
136 gAppId[MaxAppNameLen-1] = '\0';
139 pthread_mutex_destroy(&gDbusInitializedMtx);
140 pthread_cond_destroy(&gDbusInitializedCond);
144 else if(gPclInitialized >= PCLinitialized)
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) );
156 int pclDeinitLibrary(void)
160 if(gPclInitialized == PCLinitialized)
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));
165 // unregister for lifecycle and persistence admin service dbus messages
167 unregister_lifecycle(gShutdownMode);
168 unregister_pers_admin_service();
171 // unload custom client libraries
172 for(i=0; i<PersCustomLib_LastEntry; i++)
174 if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
176 // deinitialize plugin
177 gPersCustomFuncs[i].custom_plugin_deinit();
178 // close library handle
179 dlclose(gPersCustomFuncs[i].handle);
181 invalidateCustomPlugin(i);
185 gPclInitialized = PCLnotInitialized;
187 DLT_UNREGISTER_CONTEXT(gDLTContext);
189 else if(gPclInitialized > PCLinitialized)
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
200 void invalidateCustomPlugin(int idx)
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;