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