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];
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];
71 itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group)
74 itzam_btree* tree = NULL;
76 // create array index: index is a combination of resource config table type and group
77 arrayIdx = rct + group;
79 if(arrayIdx < PrctDbTableSize)
81 if(gResourceOpen[arrayIdx] == 0) // check if database is already open
84 char filename[DbPathMaxLen] = {0};
86 switch(rct) // create db name
88 case PersistenceRCT_local:
89 snprintf(filename, DbPathMaxLen, gLocalWtPath, gAppId, gResTableCfg);
91 case PersistenceRCT_shared_public:
92 snprintf(filename, DbPathMaxLen, gSharedPublicWtPath, gAppId, gResTableCfg);
94 case PersistenceRCT_shared_group:
95 snprintf(filename, DbPathMaxLen, gSharedWtPath, gAppId, group, gResTableCfg);
98 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - error: no valid PersistenceRCT_e"));
102 state = itzam_btree_open(&gResource_table[arrayIdx], filename, itzam_comparator_string, error_handler, 0 , 0);
103 if(state != ITZAM_OKAY)
105 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table => itzam_btree_open => Itzam problem"), DLT_STRING(STATE_MESSAGES[state]) );
110 gResourceOpen[arrayIdx] = 1; // remember the index has an DB entry
111 tree = &gResource_table[arrayIdx];
116 tree = &gResource_table[arrayIdx];
126 int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
128 int rval = 0, resourceFound = 0, groupId = 0;
130 PersistenceRCT_e rct = PersistenceRCT_LastEntry;
132 rct = get_table_id(dbContext->context.ldbid, &groupId);
134 // get resource configuration table
135 itzam_btree* resource_table = get_resource_cfg_table(rct, groupId);
137 if(resource_table != NULL)
139 PersistenceRctEntry_s search;
140 // check if resouce id is in write through table
141 if(itzam_true == itzam_btree_find(resource_table, resource_id, &search))
143 memset(dbContext->configKey.reponsible, 0, MaxConfKeyLengthResp);
144 memset(dbContext->configKey.custom_name, 0, MaxConfKeyLengthCusName);
145 memset(dbContext->configKey.customID, 0, MaxRctLengthCustom_ID);
147 dbContext->configKey.policy = search.data.policy;
148 dbContext->configKey.storage = search.data.storage;
149 dbContext->configKey.permission = search.data.permission;
150 dbContext->configKey.max_size = search.data.max_size;
151 dbContext->configKey.type = search.data.type;
152 memcpy(dbContext->configKey.reponsible, search.data.reponsible, MaxConfKeyLengthResp);
153 memcpy(dbContext->configKey.custom_name, search.data.custom_name, MaxConfKeyLengthCusName);
154 memcpy(dbContext->configKey.customID, search.data.customID, MaxRctLengthCustom_ID);
156 if(dbContext->configKey.storage != PersistenceStorage_custom )
158 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
162 int len = strlen(resource_id);
164 // if customer storage, we use the custom name as dbPath
165 strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
167 if(len > DbKeyMaxLen)
171 // and the resource_id as dbKey
172 strncpy(dbKey, resource_id, len);
179 DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("get_db_context => itzam_btree_open => resource_table: no value for key:"), DLT_STRING(resource_id) );
180 rval = EPERS_NOKEYDATA;
185 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_db_context =>error resource table"));
186 rval = EPERS_NOPRCTABLE;
189 if(resourceFound == 0)
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 = 0; // TODO define default permission
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(gDLTContext, DLT_LOG_INFO, DLT_STRING("get_db_context => create resource not in PRCT => key:"), DLT_STRING(resource_id) );
213 // send create notification
214 rval = pers_send_Notification_Signal(dbKey, &dbContext->context, pclNotifyStatus_created);
216 rval = get_db_path_and_key(dbContext, resource_id, dbKey, dbPath);
225 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, char dbKey[], char dbPath[])
227 int storePolicy = PersistenceStorage_LastEntry;
230 // create resource database key
232 if((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF) )
234 // The LDBID is used to find the DBID in the resource table.
235 if((dbContext->context.user_no == 0) && (dbContext->context.seat_no == 0))
238 // Node is added in front of the resource ID as the key string.
240 snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
245 // Node is added in front of the resource ID as the key string.
247 if(dbContext->context.seat_no == 0)
249 // /User/<user_no_parameter> is added in front of the resource ID as the key string.
250 snprintf(dbKey, DbKeyMaxLen, "%s%d/%s", gUser, dbContext->context.user_no, resource_id);
254 // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string.
255 snprintf(dbKey, DbKeyMaxLen, "%s%d%s%d/%s", gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
258 storePolicy = PersistenceStorage_local;
261 if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF))
263 // The LDBID is used to find the DBID in the resource table.
264 // /<LDBID parameter> is added in front of the resource ID as the key string.
265 // Rational: Creates a namespace within one data base.
266 // Rational: Reduction of number of databases -> reduction of maintenance costs
267 // /User/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> if there are different than 0.
269 if(dbContext->context.seat_no != 0)
271 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
275 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
277 storePolicy = PersistenceStorage_local;
281 // create resource database path
283 if(dbContext->context.ldbid < 0x80)
285 // S H A R E D database
287 if(dbContext->context.ldbid != 0)
289 // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
291 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
293 if(PersistencePolicy_wc == dbContext->configKey.policy)
295 if(dbContext->configKey.type == PersistenceResourceType_key)
296 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, gSharedCached);
298 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, dbKey);
300 else if(PersistencePolicy_wt == dbContext->configKey.policy)
302 if(dbContext->configKey.type == PersistenceResourceType_key)
303 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, gSharedWt);
305 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, dbKey);
310 // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
312 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
314 if(PersistencePolicy_wc == dbContext->configKey.policy)
316 if(dbContext->configKey.type == PersistenceResourceType_key)
317 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, gSharedCached);
319 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, dbKey);
321 else if(PersistencePolicy_wt == dbContext->configKey.policy)
323 if(dbContext->configKey.type == PersistenceResourceType_key)
324 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, gSharedWt);
326 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, dbKey);
330 storePolicy = PersistenceStorage_shared; // we have a shared database
334 // L O C A L database
336 if(PersistencePolicy_wc == dbContext->configKey.policy)
338 if(dbContext->configKey.type == PersistenceResourceType_key)
339 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, gLocalCached);
341 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, dbKey);
343 else if(PersistencePolicy_wt == dbContext->configKey.policy)
345 if(dbContext->configKey.type == PersistenceResourceType_key)
346 snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, gLocalWt);
348 snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, dbKey);
351 storePolicy = PersistenceStorage_local; // we have a local database
354 //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey);
355 //printf("get_db_path_and_key - dbPath : [path]: %s\n\n", dbPath);