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 memcpy(&dbContext->configKey, &sRctEntry, sizeof(dbContext->configKey)) ;
162 if(sRctEntry.storage != PersistenceStorage_custom )
164 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
168 // if customer storage, we use the custom name as dbPath
169 strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
171 // and resource_id as dbKey
172 strncpy(dbKey, resource_id, strlen(resource_id));
178 DLT_LOG(gPclDLTContext, DLT_LOG_WARN, DLT_STRING("get_db_context => persComRctRead => resource_table: no value for key:"), DLT_STRING(resource_id) );
179 rval = EPERS_NOKEYDATA;
184 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context =>error resource table"));
185 rval = EPERS_NOPRCTABLE;
188 if((resourceFound == 0) && (dbContext->context.ldbid == 0xFF) ) // create only when the resource is local data
191 // resource NOT found in resource table ==> default is local cached key
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)
199 dbContext->configKey.type = PersistenceResourceType_file;
203 dbContext->configKey.type = PersistenceResourceType_key;
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"));
210 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => create resource not in PRCT => key:"), DLT_STRING(resource_id) );
212 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
214 /* rval contains the return value of function get_db_path_and_key() if positive structure content 'dbContext' is valid.
215 * rval can be 0,1 or 2 but get_db_context should only return '0' for success. */
227 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
229 int storePolicy = PersistenceStorage_LastEntry;
232 // create resource database key
234 if(((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF)) && (NULL != dbKey))
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))
240 // Node is added in front of the resource ID as the key string.
242 snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
247 // Node is added in front of the resource ID as the key string.
249 if(dbContext->context.seat_no == 0)
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);
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);
260 storePolicy = PersistenceStorage_local;
263 if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF) && (NULL != dbKey))
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.
271 if(dbContext->context.seat_no != 0)
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);
277 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
279 storePolicy = PersistenceStorage_local;
283 // create resource database path
285 if(dbContext->context.ldbid < 0x80)
287 // S H A R E D database
289 if(dbContext->context.ldbid != 0)
291 // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
293 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
295 if(PersistencePolicy_wc == dbContext->configKey.policy)
297 if(dbContext->configKey.type == PersistenceResourceType_key)
298 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, "");
300 snprintf(dbPath, DbPathMaxLen, gSharedCachePathKey, gAppId, dbContext->context.ldbid, dbKey);
302 else if(PersistencePolicy_wt == dbContext->configKey.policy)
304 if(dbContext->configKey.type == PersistenceResourceType_key)
305 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid);
307 snprintf(dbPath, DbPathMaxLen, gSharedWtPathKey, gAppId, dbContext->context.ldbid, dbKey);
312 // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
314 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
316 if(PersistencePolicy_wc == dbContext->configKey.policy)
318 if(dbContext->configKey.type == PersistenceResourceType_key)
319 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, "");
321 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePathKey, gAppId, dbKey);
323 else if(PersistencePolicy_wt == dbContext->configKey.policy)
325 if(dbContext->configKey.type == PersistenceResourceType_key)
326 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, "");
328 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, dbKey);
332 storePolicy = PersistenceStorage_shared; // we have a shared database
336 // L O C A L database
337 if(PersistencePolicy_wc == dbContext->configKey.policy)
339 if(dbContext->configKey.type == PersistenceResourceType_key)
340 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, "");
342 snprintf(dbPath, DbPathMaxLen, gLocalCachePathKey, gAppId, dbKey);
344 else if(PersistencePolicy_wt == dbContext->configKey.policy)
346 if(dbContext->configKey.type == PersistenceResourceType_key)
347 snprintf(dbPath, DbPathMaxLen-1, gLocalWtPath, gAppId, "");
349 snprintf(dbPath, DbPathMaxLen-1, gLocalWtPathKey, gAppId, dbKey);
352 storePolicy = PersistenceStorage_local; // we have a local database
355 //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey);
356 //printf("get_db_path_and_key - dbPath : [path]: %s\n", dbPath);