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
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"
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};
34 PersistenceRCT_e get_table_id(int ldbid, int* groupId)
36 PersistenceRCT_e rctType = PersistenceRCT_LastEntry;
40 // S H A R E D database
43 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
44 *groupId = ldbid; // assign group ID
45 rctType = PersistenceRCT_shared_group;
49 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
50 *groupId = 0; // no group ID for public data
51 rctType = PersistenceRCT_shared_public;
57 *groupId = 0; // no group ID for local data
58 rctType = PersistenceStorage_local; // we have a local database
64 itzam_btree* get_resource_cfg_table_by_idx(int i)
66 return &gResource_table[i];
69 int get_resource_cfg_table_status(int i)
71 return gResourceOpen[i];
74 void invalidate_resource_cfg_table(int i)
80 itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group)
83 itzam_btree* tree = NULL;
85 // create array index: index is a combination of resource config table type and group
86 arrayIdx = rct + group;
88 if(arrayIdx < PrctDbTableSize)
90 if(gResourceOpen[arrayIdx] == 0) // check if database is already open
93 char filename[DbPathMaxLen] = {0};
95 switch(rct) // create db name
97 case PersistenceRCT_local:
98 snprintf(filename, DbPathMaxLen, gLocalWtPathKey, gAppId, gResTableCfg);
100 case PersistenceRCT_shared_public:
101 snprintf(filename, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, gResTableCfg);
103 case PersistenceRCT_shared_group:
104 snprintf(filename, DbPathMaxLen, gSharedWtPathKey, gAppId, group, gResTableCfg);
107 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - error: no valid PersistenceRCT_e"));
111 state = itzam_btree_open(&gResource_table[arrayIdx], filename, itzam_comparator_string, error_handler, 0 , 0);
112 if(state != ITZAM_OKAY)
114 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table => itzam_btree_open => Itzam problem"), DLT_STRING(STATE_MESSAGES[state]) );
119 gResourceOpen[arrayIdx] = 1; // remember the index has an DB entry
120 tree = &gResource_table[arrayIdx];
125 tree = &gResource_table[arrayIdx];
135 int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
137 int rval = 0, resourceFound = 0, groupId = 0;
139 PersistenceRCT_e rct = PersistenceRCT_LastEntry;
141 rct = get_table_id(dbContext->context.ldbid, &groupId);
143 // get resource configuration table
144 itzam_btree* resource_table = get_resource_cfg_table(rct, groupId);
146 if(resource_table != NULL)
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))
152 memset(dbContext->configKey.reponsible, 0, MaxConfKeyLengthResp);
153 memset(dbContext->configKey.custom_name, 0, MaxConfKeyLengthCusName);
154 memset(dbContext->configKey.customID, 0, MaxRctLengthCustom_ID);
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);
165 if(dbContext->configKey.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));
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;
184 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context =>error resource table"));
185 rval = EPERS_NOPRCTABLE;
188 if(resourceFound == 0)
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(gDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => create resource not in PRCT => key:"), DLT_STRING(resource_id) );
212 // send create notification
213 rval = pers_send_Notification_Signal(dbKey, &dbContext->context, pclNotifyStatus_created);
215 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
224 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
226 int storePolicy = PersistenceStorage_LastEntry;
229 // create resource database key
231 if((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF) )
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))
237 // Node is added in front of the resource ID as the key string.
239 snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
244 // Node is added in front of the resource ID as the key string.
246 if(dbContext->context.seat_no == 0)
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);
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);
257 storePolicy = PersistenceStorage_local;
260 if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF))
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.
268 if(dbContext->context.seat_no != 0)
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);
274 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
276 storePolicy = PersistenceStorage_local;
280 // create resource database path
282 if(dbContext->context.ldbid < 0x80)
284 // S H A R E D database
286 if(dbContext->context.ldbid != 0)
288 // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
290 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
292 if(PersistencePolicy_wc == dbContext->configKey.policy)
294 if(dbContext->configKey.type == PersistenceResourceType_key)
295 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid);
297 snprintf(dbPath, DbPathMaxLen, gSharedCachePathKey, gAppId, dbContext->context.ldbid, dbKey);
299 else if(PersistencePolicy_wt == dbContext->configKey.policy)
301 if(dbContext->configKey.type == PersistenceResourceType_key)
302 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid);
304 snprintf(dbPath, DbPathMaxLen, gSharedWtPathKey, gAppId, dbContext->context.ldbid, dbKey);
309 // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
311 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
313 if(PersistencePolicy_wc == dbContext->configKey.policy)
315 if(dbContext->configKey.type == PersistenceResourceType_key)
316 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId);
318 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePathKey, gAppId, dbKey);
320 else if(PersistencePolicy_wt == dbContext->configKey.policy)
322 if(dbContext->configKey.type == PersistenceResourceType_key)
323 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId);
325 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, dbKey);
329 storePolicy = PersistenceStorage_shared; // we have a shared database
333 // L O C A L database
335 if(PersistencePolicy_wc == dbContext->configKey.policy)
337 if(dbContext->configKey.type == PersistenceResourceType_key)
338 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId);
340 snprintf(dbPath, DbPathMaxLen, gLocalCachePathKey, gAppId, dbKey);
342 else if(PersistencePolicy_wt == dbContext->configKey.policy)
344 if(dbContext->configKey.type == PersistenceResourceType_key)
345 snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId);
347 snprintf(dbPath, DbPathMaxLen, gLocalWtPathKey, gAppId, dbKey);
350 storePolicy = PersistenceStorage_local; // we have a local database
353 //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey);
354 //printf("get_db_path_and_key - dbPath : [path]: %s\n", dbPath);