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 int gResource_table[PrctDbTableSize] = {[0 ... PrctDbTableSize-1] = -1};
34 /// array to hold the information of database is already open
35 int gResourceOpen[PrctDbTableSize] = { [0 ... PrctDbTableSize-1] = 0 };
38 /// persistence resource config table type definition
39 typedef enum _PersistenceRCT_e
41 PersistenceRCT_local = 0,
42 PersistenceRCT_shared_public = 1,
43 PersistenceRCT_shared_group = 2,
45 PersistenceRCT_LastEntry // last Entry
51 PersistenceRCT_e get_table_id(int ldbid, int* groupId)
53 PersistenceRCT_e rctType = PersistenceRCT_LastEntry;
57 // S H A R E D database
60 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
61 *groupId = ldbid; // assign group ID
62 rctType = PersistenceRCT_shared_group;
66 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
67 *groupId = 0; // no group ID for public data
68 rctType = PersistenceRCT_shared_public;
74 *groupId = 0; // no group ID for local data
75 rctType = PersistenceRCT_local; // we have a local database
81 int get_resource_cfg_table_by_idx(int i)
83 return gResource_table[i];
87 void invalidate_resource_cfg_table(int i)
89 gResource_table[i] = -1;
93 int get_resource_cfg_table(PersistenceRCT_e rct, int group)
97 // create array index: index is a combination of resource config table type and group
98 arrayIdx = rct + group;
100 if(arrayIdx < PrctDbTableSize)
102 if(gResourceOpen[arrayIdx] == 0) // check if database is already open
104 char filename[DbPathMaxLen] = { [0 ... DbPathMaxLen-1] = 0};
106 switch(rct) // create db name
108 case PersistenceRCT_local:
109 snprintf(filename, DbPathMaxLen, gLocalWtPathKey, gAppId, gResTableCfg);
111 case PersistenceRCT_shared_public:
112 snprintf(filename, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, gResTableCfg);
114 case PersistenceRCT_shared_group:
115 snprintf(filename, DbPathMaxLen, gSharedWtPathKey, gAppId, group, gResTableCfg);
118 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - error: no valid PersistenceRCT_e"));
122 gResource_table[arrayIdx] = persComRctOpen(filename, 0x00);
124 if(gResource_table[arrayIdx] < 0)
126 gResourceOpen[arrayIdx] = -1;
127 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table => RCT problem"), DLT_INT(gResource_table[arrayIdx] ));
131 gResourceOpen[arrayIdx] = 1 ;
136 return gResource_table[arrayIdx];
141 int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
143 int rval = 0, resourceFound = 0, groupId = 0;
145 PersistenceRCT_e rct = PersistenceRCT_LastEntry;
147 rct = get_table_id(dbContext->context.ldbid, &groupId);
149 // get resource configuration table
150 int handleRCT = get_resource_cfg_table(rct, groupId);
154 PersistenceConfigurationKey_s sRctEntry ;
156 // check if resouce id is in write through table
157 int iErrCode = persComRctRead(handleRCT, resource_id, &sRctEntry) ;
159 if(sizeof(PersistenceConfigurationKey_s) == iErrCode)
161 //printf("get_db_context ==> data: %s\n", search.data);
162 memcpy(&dbContext->configKey, &sRctEntry, sizeof(dbContext->configKey)) ;
163 if(sRctEntry.storage != PersistenceStorage_custom )
165 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
169 // if customer storage, we use the custom name as dbPath
170 strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
172 // and resource_id as dbKey
173 strncpy(dbKey, resource_id, strlen(resource_id));
179 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("get_db_context => persComRctRead => resource_table: no value for key:"), DLT_STRING(resource_id) );
180 rval = EPERS_NOKEYDATA;
185 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context =>error resource table"));
186 rval = EPERS_NOPRCTABLE;
189 if((resourceFound == 0) && (dbContext->context.ldbid == 0xFF) ) // create only when the resource is local data
192 // resource NOT found in resource table ==> default is local cached key
194 dbContext->configKey.policy = PersistencePolicy_wc;
195 dbContext->configKey.storage = PersistenceStorage_local;
196 dbContext->configKey.permission = PersistencePermission_ReadWrite;
197 dbContext->configKey.max_size = defaultMaxKeyValDataSize;
198 if(isFile == PersistenceResourceType_file)
200 dbContext->configKey.type = PersistenceResourceType_file;
204 dbContext->configKey.type = PersistenceResourceType_key;
207 memcpy(dbContext->configKey.customID, "A_CUSTOM_ID", strlen("A_CUSTOM_ID"));
208 memcpy(dbContext->configKey.reponsible, "default", strlen("default"));
209 memcpy(dbContext->configKey.custom_name, "default", strlen("default"));
211 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => create resource not in PRCT => key:"), DLT_STRING(resource_id) );
213 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
215 /* rval contains the return value of function get_db_path_and_key() if positive structure content 'dbContext' is valid.
216 * rval can be 0,1 or 2 but get_db_context should only return '0' for success. */
228 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
230 int storePolicy = PersistenceStorage_LastEntry;
233 // create resource database key
235 if(((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF)) && (NULL != dbKey))
237 // The LDBID is used to find the DBID in the resource table.
238 if((dbContext->context.user_no == 0) && (dbContext->context.seat_no == 0))
241 // Node is added in front of the resource ID as the key string.
243 snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
248 // Node is added in front of the resource ID as the key string.
250 if(dbContext->context.seat_no == 0)
252 // /User/<user_no_parameter> is added in front of the resource ID as the key string.
253 snprintf(dbKey, DbKeyMaxLen, "%s%d/%s", gUser, dbContext->context.user_no, resource_id);
257 // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string.
258 snprintf(dbKey, DbKeyMaxLen, "%s%d%s%d/%s", gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
261 storePolicy = PersistenceStorage_local;
264 if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF) && (NULL != dbKey))
266 // The LDBID is used to find the DBID in the resource table.
267 // /<LDBID parameter> is added in front of the resource ID as the key string.
268 // Rational: Creates a namespace within one data base.
269 // Rational: Reduction of number of databases -> reduction of maintenance costs
270 // /User/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> if there are different than 0.
272 if(dbContext->context.seat_no != 0)
274 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
278 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
280 storePolicy = PersistenceStorage_local;
284 // create resource database path
286 if(dbContext->context.ldbid < 0x80)
288 // S H A R E D database
290 if(dbContext->context.ldbid != 0)
292 // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
294 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
296 if(PersistencePolicy_wc == dbContext->configKey.policy)
298 if(dbContext->configKey.type == PersistenceResourceType_key)
299 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, "");
301 snprintf(dbPath, DbPathMaxLen, gSharedCachePathKey, gAppId, dbContext->context.ldbid, dbKey);
303 else if(PersistencePolicy_wt == dbContext->configKey.policy)
305 if(dbContext->configKey.type == PersistenceResourceType_key)
306 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid);
308 snprintf(dbPath, DbPathMaxLen, gSharedWtPathKey, gAppId, dbContext->context.ldbid, dbKey);
313 // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
315 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
317 if(PersistencePolicy_wc == dbContext->configKey.policy)
319 if(dbContext->configKey.type == PersistenceResourceType_key)
320 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, "");
322 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePathKey, gAppId, dbKey);
324 else if(PersistencePolicy_wt == dbContext->configKey.policy)
326 if(dbContext->configKey.type == PersistenceResourceType_key)
327 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, "");
329 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, dbKey);
333 storePolicy = PersistenceStorage_shared; // we have a shared database
337 // L O C A L database
338 if(PersistencePolicy_wc == dbContext->configKey.policy)
340 if(dbContext->configKey.type == PersistenceResourceType_key)
341 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, "");
343 snprintf(dbPath, DbPathMaxLen, gLocalCachePathKey, gAppId, dbKey);
345 else if(PersistencePolicy_wt == dbContext->configKey.policy)
347 if(dbContext->configKey.type == PersistenceResourceType_key)
348 snprintf(dbPath, DbPathMaxLen-1, gLocalWtPath, gAppId, "");
350 snprintf(dbPath, DbPathMaxLen-1, gLocalWtPathKey, gAppId, dbKey);
353 storePolicy = PersistenceStorage_local; // we have a local database
356 //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey);
357 //printf("get_db_path_and_key - dbPath : [path]: %s\n", dbPath);