New release 0.1.4, for changes see ChangeLog
[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    Permission is hereby granted, free of charge, to any person obtaining
8    a copy of this software and associated documentation files (the "Software"),
9    to deal in the Software without restriction, including without limitation
10    the rights to use, copy, modify, merge, publish, distribute, sublicense,
11    and/or sell copies of the Software, and to permit persons to whom the
12    Software is furnished to do so, subject to the following conditions:
13
14    The above copyright notice and this permission notice shall be included
15    in all copies or substantial portions of the Software.
16
17    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIcacheON WITH THE SOFTWARE
23    OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ******************************************************************************/
25  /**
26  * @file           persistence_client_library.c
27  * @ingroup        Persistence client library
28  * @author         Ingo Huerner
29  * @brief          Implementation of the persistence client library.
30  *                 Library provides an API to access persistent data
31  * @see            
32  */
33
34
35 #include "persistence_client_library.h"
36 #include "persistence_client_library_lc_interface.h"
37 #include "persistence_client_library_pas_interface.h"
38 #include "persistence_client_library_dbus_service.h"
39 #include "persistence_client_library_handle.h"
40 #include "persistence_client_library_data_access.h"
41 #include "persistence_client_library_custom_loader.h"
42 #include "persistence_client_library_access_helper.h"
43
44 #include <string.h>
45 #include <errno.h>
46 #include <stdlib.h>
47
48 #include <dlt/dlt.h>
49 #include <dlt/dlt_common.h>
50
51 extern char* __progname;
52
53 /// debug log and trace (DLT) setup
54 DLT_DECLARE_CONTEXT(persClientLibCtx);
55
56
57 /// resource configuration table name
58 const char* gResTableCfg = "/resource-table-cfg.gvdb";
59
60 /// shared cached default database
61 //static const char* gSharedCachedDefault = "cached-default.dconf";
62 /// shared cached database
63 const char* gSharedCached        = "/cached.dconf";
64 /// shared write through default database
65 const char* gSharedWtDefault     = "wt-default.dconf";
66 /// shared write through database
67 const char* gSharedWt            = "/wt.dconf";
68
69 /// local cached default database
70 const char* gLocalCachedDefault  = "cached-default.gvdb";
71 /// local cached default database
72 const char* gLocalCached         = "/cached.gvdb";
73 /// local write through default database
74 const char* gLocalWtDefault      = "wt-default.gvdb";
75 /// local write through default database
76 const char* gLocalWt             = "/wt.gvdb";
77
78
79 /// directory structure node name defintion
80 const char* gNode = "/Node";
81 /// directory structure user name defintion
82 const char* gUser = "/User/";
83 /// directory structure seat name defintion
84 const char* gSeat = "/Seat/";
85
86
87 /// path prefic for local cached database: /Data/mnt_c/<appId>/<database_name>
88 const char* gLocalCachePath        = "/Data/mnt-c/%s%s";
89 /// path prefic for local write through database /Data/mnt_wt/<appId>/<database_name>
90 const char* gLocalWtPath           = "/Data/mnt-wt/%s%s";
91 /// path prefic for shared cached database: /Data/mnt_c/Shared/Group/<group_no>/<database_name>
92 const char* gSharedCachePath       = "/Data/mnt-c/shared/group/%x%s";
93 /// path prefic for shared write through database: /Data/mnt_wt/Shared/Group/<group_no>/<database_name>
94 const char* gSharedWtPath          = "/Data/mnt-wt/shared/group/%x%s";
95 /// path prefic for shared public cached database: /Data/mnt_c/Shared/Public//<database_name>
96 const char* gSharedPublicCachePath = "/Data/mnt-c/shared/public%s";
97 /// path prefic for shared public write through database: /Data/mnt_wt/Shared/Public/<database_name>
98 const char* gSharedPublicWtPath    = "/Data/mnt-wt/shared/public%s";
99
100
101 /// application id
102 char gAppId[maxAppNameLen];
103
104 /// max key value data size [default 16kB]
105 int gMaxKeyValDataSize = defaultMaxKeyValDataSize;
106
107
108 /// library constructor
109 void pers_library_init(void) __attribute__((constructor));
110
111 /// library deconstructor
112 void pers_library_destroy(void) __attribute__((destructor));
113
114
115
116 void pers_library_init(void)
117 {
118    DLT_REGISTER_APP("Persistence Client Library","persClientLib");
119    DLT_REGISTER_CONTEXT(persClientLibCtx,"persClientLib","Context for Logging");
120
121    DLT_LOG(persClientLibCtx, DLT_LOG_ERROR, DLT_STRING("Initialize Persistence Client Library!!!!"));
122
123    /// environment variable for on demand loading of custom libraries
124    const char *pOnDemandLoad = getenv("PERS_CUSTOM_LIB_LOAD_ON_DEMAND");
125
126    /// environment variable for max key value data
127    const char *pDataSize = getenv("PERS_MAX_KEY_VAL_DATA_SIZE");
128
129    if(pDataSize != NULL)
130    {
131       gMaxKeyValDataSize = atoi(pDataSize);
132    }
133
134    // initialize mutex
135    pthread_mutex_init(&gDbusInitializedMtx, NULL);
136
137    // setup dbus main dispatching loop
138    setup_dbus_mainloop();
139
140    // wain until dbus main loop has been setup and running
141    pthread_mutex_lock(&gDbusInitializedMtx);
142
143    // register for lifecycle and persistence admin service dbus messages
144    register_lifecycle();
145    register_pers_admin_service();
146
147    // clear the open file descriptor array
148    memset(gOpenFdArray, maxPersHandle, sizeof(int));
149
150    /// get custom library names to load
151    get_custom_libraries();
152
153    if(pOnDemandLoad == NULL)  // load all available libraries now
154    {
155       int i = 0;
156
157       for(i=0; i < get_num_custom_client_libs(); i++ )
158       {
159          if(load_custom_library(get_custom_client_position_in_array(i), &gPersCustomFuncs[i] ) == -1)
160          {
161             printf("E r r o r could not load plugin: %s \n", get_custom_client_lib_name(get_custom_client_position_in_array(i)));
162             break;
163          }
164          gPersCustomFuncs[i].custom_plugin_init();
165       }
166
167       /// just testing
168       //gPersCustomFuncs[PersCustomLib_early].custom_plugin_open("Hallo", 88, 99);
169       //gPersCustomFuncs[PersCustomLib_early].custom_plugin_close(17);
170    }
171
172    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
173    strncpy(gAppId, __progname, maxAppNameLen);
174 }
175
176
177
178 void pers_library_destroy(void)
179 {
180    // destory mutex
181    pthread_mutex_destroy(&gDbusInitializedMtx);
182
183    // unregister for lifecycle and persistence admin service dbus messages
184    unregister_lifecycle();
185    unregister_pers_admin_service();
186
187
188    DLT_UNREGISTER_CONTEXT(persClientLibCtx);
189    DLT_UNREGISTER_APP();
190    dlt_free();
191 }
192
193
194
195
196