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_prct_access.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of persistence resource configuration config
16 * table access functions
22 #include "persistence_client_library_prct_access.h"
23 #include "persistence_client_library_db_access.h"
27 #include <persComRct.h>
28 #include <persComDbAccess.h>
29 #include <persComErrors.h>
32 /// pointer to resource table database
33 static int gResource_table[PrctDbTableSize] = {[0 ... PrctDbTableSize-1] = -1};
34 /// array to hold the information of database is already open
35 static int gResourceOpen[PrctDbTableSize] = { [0 ... PrctDbTableSize-1] = 0 };
38 /// persistence resource config table type definition
39 typedef enum _PersistenceRCT_e
41 /// resource is local (only visible for an application)
42 PersistenceRCT_local = 0,
43 /// resource is visible for public
44 PersistenceRCT_shared_public = 1,
45 /// resource is visible for a group
46 PersistenceRCT_shared_group = 2,
48 PersistenceRCT_LastEntry // last Entry
53 PersistenceRCT_e get_table_id(int ldbid, int* groupId)
55 PersistenceRCT_e rctType = PersistenceRCT_LastEntry;
59 // S H A R E D database
62 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
63 *groupId = ldbid; // assign group ID
64 rctType = PersistenceRCT_shared_group;
68 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
69 *groupId = 0; // no group ID for public data
70 rctType = PersistenceRCT_shared_public;
76 *groupId = 0; // no group ID for local data
77 rctType = PersistenceRCT_local; // we have a local database
83 int get_resource_cfg_table_by_idx(int i)
85 return gResource_table[i];
89 void invalidate_resource_cfg_table(int i)
91 gResource_table[i] = -1;
96 int get_resource_cfg_table(PersistenceRCT_e rct, int group)
100 // create array index: index is a combination of resource config table type and group
101 arrayIdx = rct + group;
103 if(arrayIdx < PrctDbTableSize)
105 if(gResourceOpen[arrayIdx] == 0) // check if database is already open
107 char filename[DbPathMaxLen] = { [0 ... DbPathMaxLen-1] = 0};
109 switch(rct) // create db name
111 case PersistenceRCT_local:
112 snprintf(filename, DbPathMaxLen, gLocalWtPathKey, gAppId, gResTableCfg);
114 case PersistenceRCT_shared_public:
115 snprintf(filename, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, gResTableCfg);
117 case PersistenceRCT_shared_group:
118 snprintf(filename, DbPathMaxLen, gSharedWtPathKey, gAppId, group, gResTableCfg);
121 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - no valid PersistenceRCT_e"));
125 gResource_table[arrayIdx] = persComRctOpen(filename, 0x00);
127 if(gResource_table[arrayIdx] < 0)
129 gResourceOpen[arrayIdx] = 0;
130 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - RCT problem"), DLT_INT(gResource_table[arrayIdx] ));
134 gResourceOpen[arrayIdx] = 1 ;
139 return gResource_table[arrayIdx];
144 int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
146 int rval = 0, resourceFound = 0, groupId = 0;
148 PersistenceRCT_e rct = PersistenceRCT_LastEntry;
150 rct = get_table_id(dbContext->context.ldbid, &groupId);
152 // get resource configuration table
153 int handleRCT = get_resource_cfg_table(rct, groupId);
157 PersistenceConfigurationKey_s sRctEntry ;
159 // check if resouce id is in write through table
160 int iErrCode = persComRctRead(handleRCT, resource_id, &sRctEntry) ;
162 if(sizeof(PersistenceConfigurationKey_s) == iErrCode)
164 memcpy(&dbContext->configKey, &sRctEntry, sizeof(dbContext->configKey)) ;
165 if(sRctEntry.storage != PersistenceStorage_custom )
167 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
171 // if customer storage, we use the custom name as dbPath
172 strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
174 // and resource_id as dbKey
175 strncpy(dbKey, resource_id, strlen(resource_id));
181 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("get_db_context - resource_table: no value for key:"), DLT_STRING(resource_id) );
182 rval = EPERS_NOKEYDATA;
187 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context - resource table"));
188 rval = EPERS_NOPRCTABLE;
191 if((resourceFound == 0) && (dbContext->context.ldbid == 0xFF) ) // create only when the resource is local data
194 // resource NOT found in resource table ==> default is local cached key
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)
202 dbContext->configKey.type = PersistenceResourceType_file;
206 dbContext->configKey.type = PersistenceResourceType_key;
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"));
213 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context - create resource not in PRCT => key:"), DLT_STRING(resource_id) );
215 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
217 /* rval contains the return value of function get_db_path_and_key() if positive structure content 'dbContext' is valid.
218 * rval can be 0,1 or 2 but get_db_context should only return '0' for success. */
229 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
231 int storePolicy = PersistenceStorage_LastEntry;
234 // create resource database key
236 if(((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF)) && (NULL != dbKey))
238 // The LDBID is used to find the DBID in the resource table.
239 if((dbContext->context.user_no == 0) && (dbContext->context.seat_no == 0))
242 // Node is added in front of the resource ID as the key string.
244 snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
249 // Node is added in front of the resource ID as the key string.
251 if(dbContext->context.seat_no == 0)
253 // /User/<user_no_parameter> is added in front of the resource ID as the key string.
254 snprintf(dbKey, DbKeyMaxLen, "%s%d/%s", gUser, dbContext->context.user_no, resource_id);
258 // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string.
259 snprintf(dbKey, DbKeyMaxLen, "%s%d%s%d/%s", gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
262 storePolicy = PersistenceStorage_local;
265 if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF) && (NULL != dbKey))
267 // The LDBID is used to find the DBID in the resource table.
268 // /<LDBID parameter> is added in front of the resource ID as the key string.
269 // Rational: Creates a namespace within one data base.
270 // Rational: Reduction of number of databases -> reduction of maintenance costs
271 // /User/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> if there are different than 0.
273 if(dbContext->context.seat_no != 0)
275 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
279 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
281 storePolicy = PersistenceStorage_local;
285 // create resource database path
287 if(dbContext->context.ldbid < 0x80)
289 // S H A R E D database
291 if(dbContext->context.ldbid != 0)
293 // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
295 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
297 if(PersistencePolicy_wc == dbContext->configKey.policy)
299 if(dbContext->configKey.type == PersistenceResourceType_key)
300 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, "");
302 snprintf(dbPath, DbPathMaxLen, gSharedCachePathKey, gAppId, dbContext->context.ldbid, dbKey);
304 else if(PersistencePolicy_wt == dbContext->configKey.policy)
306 if(dbContext->configKey.type == PersistenceResourceType_key)
307 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid);
309 snprintf(dbPath, DbPathMaxLen, gSharedWtPathKey, gAppId, dbContext->context.ldbid, dbKey);
314 // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
316 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
318 if(PersistencePolicy_wc == dbContext->configKey.policy)
320 if(dbContext->configKey.type == PersistenceResourceType_key)
321 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, "");
323 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePathKey, gAppId, dbKey);
325 else if(PersistencePolicy_wt == dbContext->configKey.policy)
327 if(dbContext->configKey.type == PersistenceResourceType_key)
328 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, "");
330 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, dbKey);
334 storePolicy = PersistenceStorage_shared; // we have a shared database
338 // L O C A L database
339 if(PersistencePolicy_wc == dbContext->configKey.policy)
341 if(dbContext->configKey.type == PersistenceResourceType_key)
342 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, "");
344 snprintf(dbPath, DbPathMaxLen, gLocalCachePathKey, gAppId, dbKey);
346 else if(PersistencePolicy_wt == dbContext->configKey.policy)
348 if(dbContext->configKey.type == PersistenceResourceType_key)
349 snprintf(dbPath, DbPathMaxLen-1, gLocalWtPath, gAppId, "");
351 snprintf(dbPath, DbPathMaxLen-1, gLocalWtPathKey, gAppId, dbKey);
354 storePolicy = PersistenceStorage_local; // we have a local database
357 //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey);
358 //printf("get_db_path_and_key - dbPath : [path]: %s\n", dbPath);