Changed file and key-value API according GENIVI naming conventions
[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
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <dlt/dlt.h>
31 #include <dlt/dlt_common.h>
32
33 #include <dbus/dbus.h>
34
35
36
37 #define ENABLE_DBUS_INTERFACE 1
38
39 extern char* __progname;
40
41 /// debug log and trace (DLT) setup
42 DLT_DECLARE_CONTEXT(persClientLibCtx);
43
44
45
46 /// library constructor
47 void pers_library_init(void) __attribute__((constructor));
48
49 /// library deconstructor
50 void pers_library_destroy(void) __attribute__((destructor));
51
52
53
54 void pers_library_init(void)
55 {
56    int status = 0;
57    int i = 0;
58
59    DLT_REGISTER_APP("Persistence Client Library","persClientLib");
60    DLT_REGISTER_CONTEXT(persClientLibCtx,"persClientLib","Context for Logging");
61
62    DLT_LOG(persClientLibCtx, DLT_LOG_ERROR, DLT_STRING("Initialize Persistence Client Library!!!!"));
63
64    /// environment variable for on demand loading of custom libraries
65    const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND");
66
67    /// environment variable for max key value data
68    const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
69
70    if(pDataSize != NULL)
71    {
72       gMaxKeyValDataSize = atoi(pDataSize);
73    }
74
75 #if ENABLE_DBUS_INTERFACE == 1
76    setup_dbus_mainloop();
77
78    // register for lifecycle and persistence admin service dbus messages
79    register_lifecycle();
80    register_pers_admin_service();
81 #endif
82
83    // clear the open file descriptor array
84    memset(gOpenFdArray, MaxPersHandle, sizeof(int));
85
86    /// get custom library names to load
87    status = get_custom_libraries();
88    if(status < 0)
89    {
90       printf("Failed to load custom library config table => error number %d\n", status );
91    }
92
93    // initialize custom library structure
94    for(i = 0; i<PersCustomLib_LastEntry; i++)
95    {
96       gPersCustomFuncs[i].handle  = NULL;
97       gPersCustomFuncs[i].custom_plugin_init = NULL;
98       gPersCustomFuncs[i].custom_plugin_deinit = NULL;
99       gPersCustomFuncs[i].custom_plugin_handle_open = NULL;
100       gPersCustomFuncs[i].custom_plugin_handle_close = NULL;
101       gPersCustomFuncs[i].custom_plugin_handle_get_data = NULL;
102       gPersCustomFuncs[i].custom_plugin_handle_set_data  = NULL;
103       gPersCustomFuncs[i].custom_plugin_get_data = NULL;
104       gPersCustomFuncs[i].custom_plugin_set_data = NULL;
105       gPersCustomFuncs[i].custom_plugin_delete_data = NULL;
106       gPersCustomFuncs[i].custom_plugin_get_status_notification_clbk = NULL;
107       gPersCustomFuncs[i].custom_plugin_handle_get_size = NULL;
108       gPersCustomFuncs[i].custom_plugin_get_size = NULL;
109       gPersCustomFuncs[i].custom_plugin_create_backup = NULL;
110       gPersCustomFuncs[i].custom_plugin_get_backup = NULL;
111       gPersCustomFuncs[i].custom_plugin_restore_backup = NULL;
112    }
113
114    if(pOnDemandLoad == NULL)  // load all available libraries now
115    {
116       for(i=0; i < get_num_custom_client_libs(); i++ )
117       {
118          if(load_custom_library(get_custom_client_position_in_array(i), &gPersCustomFuncs[i] ) == -1)
119          {
120             printf("E r r o r could not load plugin: %s \n", get_custom_client_lib_name(get_custom_client_position_in_array(i)));
121             break;
122          }
123          gPersCustomFuncs[i].custom_plugin_init();
124       }
125
126       /// just testing
127       //gPersCustomFuncs[PersCustomLib_early].custom_plugin_open("Hallo", 88, 99);
128       //gPersCustomFuncs[PersCustomLib_early].custom_plugin_close(17);
129    }
130
131    //printf("A p p l i c a t i o n   n a m e => %s \n", __progname /*program_invocation_short_name*/);   // TODO: only temp solution for application name
132    strncpy(gAppId, __progname, MaxAppNameLen);
133    // destory mutex
134    pthread_mutex_destroy(&gDbusInitializedMtx);
135    pthread_cond_destroy(&gDbusInitializedCond);
136 }
137
138
139
140 void pers_library_destroy(void)
141 {
142
143 #if ENABLE_DBUS_INTERFACE == 1
144    // unregister for lifecycle and persistence admin service dbus messages
145    unregister_lifecycle();
146    unregister_pers_admin_service();
147 #endif
148
149    DLT_UNREGISTER_CONTEXT(persClientLibCtx);
150    DLT_UNREGISTER_APP();
151    dlt_free();
152 }
153
154
155
156
157