Removed compiler warnings
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_prct_access.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_prct_access.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of persistence resource configuration config
16  *                 table access functions
17  * @see
18  */
19
20
21 #include "persistence_client_library_prct_access.h"
22 #include "persistence_client_library_itzam_errors.h"
23 #include "../include_protected/persistence_client_library_db_access.h"
24 #include <stdlib.h>
25
26
27
28 /// pointer to resource table database
29 itzam_btree gResource_table[PrctDbTableSize] = {{0}};
30 /// array to hold the information of database is already open
31 int gResourceOpen[PrctDbTableSize] = {0};
32
33
34 PersistenceRCT_e get_table_id(int ldbid, int* groupId)
35 {
36    PersistenceRCT_e rctType = PersistenceRCT_LastEntry;
37
38    if(ldbid < 0x80)
39    {
40       // S H A R E D  database
41       if(ldbid != 0)
42       {
43          // shared  G R O U P  database * * * * * * * * * * * * *  * * * * * *
44          *groupId = ldbid;  // assign group ID
45          rctType = PersistenceRCT_shared_group;
46       }
47       else
48       {
49          // shared  P U B L I C  database * * * * * * * * * * * * *  * * * * *
50          *groupId = 0;      // no group ID for public data
51          rctType = PersistenceRCT_shared_public;
52       }
53    }
54    else
55    {
56       // L O C A L   database
57       *groupId = 0;      // no group ID for local data
58       rctType = PersistenceStorage_local;   // we have a local database
59    }
60    return rctType;
61 }
62
63
64 itzam_btree* get_resource_cfg_table_by_idx(int i)
65 {
66    return &gResource_table[i];
67 }
68
69 int get_resource_cfg_table_status(int i)
70 {
71    return gResourceOpen[i];
72 }
73
74 void invalidate_resource_cfg_table(int i)
75 {
76    gResourceOpen[i] = 0;
77 }
78
79 // status: OK
80 itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group)
81 {
82    int arrayIdx = 0;
83    itzam_btree* tree = NULL;
84
85    // create array index: index is a combination of resource config table type and group
86    arrayIdx = rct + group;
87
88    if(arrayIdx < PrctDbTableSize)
89    {
90       if(gResourceOpen[arrayIdx] == 0)   // check if database is already open
91       {
92          itzam_state  state;
93          char filename[DbPathMaxLen] = {0};
94
95          switch(rct)    // create db name
96          {
97          case PersistenceRCT_local:
98             snprintf(filename, DbPathMaxLen, gLocalWtPathKey, gAppId, gResTableCfg);
99             break;
100          case PersistenceRCT_shared_public:
101             snprintf(filename, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, gResTableCfg);
102             break;
103          case PersistenceRCT_shared_group:
104             snprintf(filename, DbPathMaxLen, gSharedWtPathKey, gAppId, group, gResTableCfg);
105             break;
106          default:
107             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - error: no valid PersistenceRCT_e"));
108             break;
109          }
110
111          state = itzam_btree_open(&gResource_table[arrayIdx], filename, itzam_comparator_string, error_handler, 0 , 0);
112          if(state != ITZAM_OKAY)
113          {
114             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table => itzam_btree_open => Itzam problem"), DLT_STRING(STATE_MESSAGES[state]) );
115             tree = NULL;
116          }
117          else
118          {
119             gResourceOpen[arrayIdx] = 1;  // remember the index has an DB entry
120             tree = &gResource_table[arrayIdx];
121          }
122       }
123       else
124       {
125          tree = &gResource_table[arrayIdx];
126       }
127
128    }
129
130    return tree;
131 }
132
133
134 // status: OK
135 int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
136 {
137    int rval = 0, resourceFound = 0, groupId = 0;
138
139    PersistenceRCT_e rct = PersistenceRCT_LastEntry;
140
141    rct = get_table_id(dbContext->context.ldbid, &groupId);
142
143    // get resource configuration table
144    itzam_btree* resource_table = get_resource_cfg_table(rct, groupId);
145
146    if(resource_table != NULL)
147    {
148       PersistenceRctEntry_s search;
149       // check if resouce id is in write through table
150       if(itzam_true == itzam_btree_find(resource_table, resource_id, &search))
151       {
152          memset(dbContext->configKey.reponsible,  0, MaxConfKeyLengthResp);
153          memset(dbContext->configKey.custom_name, 0, MaxConfKeyLengthCusName);
154          memset(dbContext->configKey.customID,    0,  MaxRctLengthCustom_ID);
155
156          dbContext->configKey.policy      = search.data.policy;
157          dbContext->configKey.storage     = search.data.storage;
158          dbContext->configKey.permission  = search.data.permission;
159          dbContext->configKey.max_size    = search.data.max_size;
160          dbContext->configKey.type        = search.data.type;
161          memcpy(dbContext->configKey.reponsible, search.data.reponsible, MaxConfKeyLengthResp);
162          memcpy(dbContext->configKey.custom_name, search.data.custom_name, MaxConfKeyLengthCusName);
163          memcpy(dbContext->configKey.customID, search.data.customID, MaxRctLengthCustom_ID);
164
165          if(dbContext->configKey.storage != PersistenceStorage_custom )
166          {
167             rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
168          }
169          else
170          {
171             // if customer storage, we use the custom name as dbPath
172             strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
173          }
174          resourceFound = 1;
175       }
176       else
177       {
178          DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("get_db_context => itzam_btree_open => resource_table: no value for key:"), DLT_STRING(resource_id) );
179          rval = EPERS_NOKEYDATA;
180       }
181    }  // resource table
182    else
183    {
184       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context =>error resource table"));
185       rval = EPERS_NOPRCTABLE;
186    }
187
188    if(resourceFound == 0)
189    {
190       //
191       // resource NOT found in resource table ==> default is local cached key
192       //
193       dbContext->configKey.policy      = PersistencePolicy_wc;
194       dbContext->configKey.storage     = PersistenceStorage_local;
195       dbContext->configKey.permission  = PersistencePermission_ReadWrite;
196       dbContext->configKey.max_size    = defaultMaxKeyValDataSize;
197       if(isFile == PersistenceResourceType_file)
198       {
199          dbContext->configKey.type = PersistenceResourceType_file;
200        }
201       else
202       {
203          dbContext->configKey.type  = PersistenceResourceType_key;
204       }
205
206       memcpy(dbContext->configKey.customID, "A_CUSTOM_ID", strlen("A_CUSTOM_ID"));
207       memcpy(dbContext->configKey.reponsible, "default", strlen("default"));
208       memcpy(dbContext->configKey.custom_name, "default", strlen("default"));
209
210       DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => create resource not in PRCT => key:"), DLT_STRING(resource_id) );
211
212       // send create notification
213       rval = pers_send_Notification_Signal(dbKey, &dbContext->context, pclNotifyStatus_created);
214
215       rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
216    }
217
218    return rval;
219 }
220
221
222
223 // status: OK
224 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
225 {
226    int storePolicy = PersistenceStorage_LastEntry;
227
228    //
229    // create resource database key
230    //
231    if((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF) )
232    {
233       // The LDBID is used to find the DBID in the resource table.
234       if((dbContext->context.user_no == 0) && (dbContext->context.seat_no == 0))
235       {
236          //
237          // Node is added in front of the resource ID as the key string.
238          //
239          snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
240       }
241       else
242       {
243          //
244          // Node is added in front of the resource ID as the key string.
245          //
246          if(dbContext->context.seat_no == 0)
247          {
248             // /User/<user_no_parameter> is added in front of the resource ID as the key string.
249             snprintf(dbKey, DbKeyMaxLen, "%s%d/%s", gUser, dbContext->context.user_no, resource_id);
250          }
251          else
252          {
253             // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string.
254             snprintf(dbKey, DbKeyMaxLen, "%s%d%s%d/%s", gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
255          }
256       }
257       storePolicy = PersistenceStorage_local;
258    }
259
260    if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF))
261    {
262       // The LDBID is used to find the DBID in the resource table.
263       // /<LDBID parameter> is added in front of the resource ID as the key string.
264       //  Rational: Creates a namespace within one data base.
265       //  Rational: Reduction of number of databases -> reduction of maintenance costs
266       // /User/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> if there are different than 0.
267
268       if(dbContext->context.seat_no != 0)
269       {
270          snprintf(dbKey, DbKeyMaxLen, "/%x%s%d%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
271       }
272       else
273       {
274          snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
275       }
276       storePolicy = PersistenceStorage_local;
277    }
278
279    //
280    // create resource database path
281    //
282    if(dbContext->context.ldbid < 0x80)
283    {
284       // S H A R E D  database
285
286       if(dbContext->context.ldbid != 0)
287       {
288          // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
289          //
290          // shared  G R O U P  database * * * * * * * * * * * * *  * * * * * *
291          //
292          if(PersistencePolicy_wc == dbContext->configKey.policy)
293          {
294             if(dbContext->configKey.type == PersistenceResourceType_key)
295                snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid);
296             else
297                snprintf(dbPath, DbPathMaxLen, gSharedCachePathKey, gAppId, dbContext->context.ldbid, dbKey);
298          }
299          else if(PersistencePolicy_wt == dbContext->configKey.policy)
300          {
301             if(dbContext->configKey.type == PersistenceResourceType_key)
302                snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid);
303             else
304                snprintf(dbPath, DbPathMaxLen, gSharedWtPathKey, gAppId, dbContext->context.ldbid, dbKey);
305          }
306       }
307       else
308       {
309          // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
310          //
311          // shared  P U B L I C  database * * * * * * * * * * * * *  * * * * *
312          //
313          if(PersistencePolicy_wc == dbContext->configKey.policy)
314          {
315             if(dbContext->configKey.type == PersistenceResourceType_key)
316                snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId);
317             else
318                snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePathKey, gAppId, dbKey);
319          }
320          else if(PersistencePolicy_wt == dbContext->configKey.policy)
321          {
322             if(dbContext->configKey.type == PersistenceResourceType_key)
323                snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId);
324             else
325                snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, dbKey);
326          }
327       }
328
329       storePolicy = PersistenceStorage_shared;   // we have a shared database
330    }
331    else
332    {
333       // L O C A L   database
334
335       if(PersistencePolicy_wc == dbContext->configKey.policy)
336       {
337          if(dbContext->configKey.type == PersistenceResourceType_key)
338             snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId);
339          else
340             snprintf(dbPath, DbPathMaxLen, gLocalCachePathKey, gAppId, dbKey);
341       }
342       else if(PersistencePolicy_wt == dbContext->configKey.policy)
343       {
344          if(dbContext->configKey.type == PersistenceResourceType_key)
345             snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId);
346          else
347             snprintf(dbPath, DbPathMaxLen, gLocalWtPathKey, gAppId, dbKey);
348       }
349
350       storePolicy = PersistenceStorage_local;   // we have a local database
351    }
352
353    //printf("get_db_path_and_key - dbKey  : [key ]: %s \n",  dbKey);
354    //printf("get_db_path_and_key - dbPath : [path]: %s\n", dbPath);
355    return storePolicy;
356 }
357
358
359
360
361
362