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"
27 /// pointer to resource table database
28 itzam_btree gResource_table[PrctDbTableSize];
29 /// array to hold the information of database is already open
30 int gResourceOpen[PrctDbTableSize] = {0};
33 PersistenceRCT_e get_table_id(int ldbid, int* groupId)
35 PersistenceRCT_e rctType = PersistenceRCT_LastEntry;
39 // S H A R E D database
42 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
43 *groupId = ldbid; // assign group ID
44 rctType = PersistenceRCT_shared_group;
48 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
49 *groupId = 0; // no group ID for public data
50 rctType = PersistenceRCT_shared_public;
56 *groupId = 0; // no group ID for local data
57 rctType = PersistenceStorage_local; // we have a local database
63 itzam_btree* get_resource_cfg_table_by_idx(int i)
65 return &gResource_table[i];
70 itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group)
73 itzam_btree* tree = NULL;
75 // create array index: index is a combination of resource config table type and group
76 arrayIdx = rct + group;
78 if(arrayIdx < PrctDbTableSize)
80 if(gResourceOpen[arrayIdx] == 0) // check if database is already open
83 char filename[DbPathMaxLen];
84 memset(filename, 0, DbPathMaxLen);
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 printf("get_resource_cfg_table - error: no valid PersistenceRCT_e\n");
102 //printf("get_resource_cfg_table => %s \n", filename);
103 state = itzam_btree_open(&gResource_table[arrayIdx], filename, itzam_comparator_string, error_handler, 0 , 0);
104 if(state != ITZAM_OKAY)
106 fprintf(stderr, "\nget_resource_cfg_table => Itzam problem: %s\n", STATE_MESSAGES[state]);
108 gResourceOpen[arrayIdx] = 1; // remember the index has an DB entry
110 tree = &gResource_table[arrayIdx];
118 int get_db_context(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
120 int rval = 0, resourceFound = 0, groupId = 0;
122 PersistenceRCT_e rct = PersistenceRCT_LastEntry;
124 rct = get_table_id(dbContext->context.ldbid, &groupId);
126 // get resource configuration table
127 itzam_btree* resource_table = get_resource_cfg_table(rct, groupId);
129 if(resource_table != NULL)
131 PersistenceRctEntry_s search;
132 // check if resouce id is in write through table
133 if(itzam_true == itzam_btree_find(resource_table, resource_id, &search))
135 //printf("get_db_context ==> data: %s\n", search.data);
136 memset(dbContext->configKey.reponsible, 0, MaxConfKeyLengthResp);
137 memset(dbContext->configKey.custom_name, 0, MaxConfKeyLengthCusName);
139 dbContext->configKey.policy = search.data.policy;
140 dbContext->configKey.storage = search.data.storage;
141 dbContext->configKey.permission = search.data.permission;
142 dbContext->configKey.max_size = search.data.max_size;
143 memcpy(dbContext->configKey.reponsible, search.data.reponsible, MaxConfKeyLengthResp);
144 memcpy(dbContext->configKey.custom_name, search.data.custom_name, MaxConfKeyLengthCusName);
146 if(dbContext->configKey.storage != PersistenceStorage_custom )
148 rval = get_db_path_and_key(dbContext, resource_id, isFile, dbKey, dbPath);
152 // if customer storage, we use the custom name as path
153 strncpy(dbPath, dbContext->configKey.custom_name, strlen(dbContext->configKey.custom_name));
159 printf("get_db_context - resource_table: no value for key: %s \n", resource_id);
160 rval = EPERS_NOKEYDATA;
165 printf("get_db_context - error resource table\n");
166 rval = EPERS_NOPRCTABLE;
169 if(resourceFound == 0)
172 // resource NOT found in resource table ==> default is local cached
174 dbContext->configKey.policy = PersistencePolicy_wc;
175 dbContext->configKey.storage = PersistenceStorage_local;
176 dbContext->configKey.permission = 0; // TODO define default permission
177 dbContext->configKey.max_size = defaultMaxKeyValDataSize;
178 memcpy(dbContext->configKey.reponsible, "default", MaxConfKeyLengthResp);
179 memcpy(dbContext->configKey.custom_name, "default", MaxConfKeyLengthCusName);
180 //printf("get_db_context ==> R E S O U R C E N O T found: %s \n", resource_id);
182 rval = get_db_path_and_key(dbContext, resource_id, isFile, dbKey, dbPath);
191 int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, unsigned int isFile, char dbKey[], char dbPath[])
193 int storePolicy = PersistenceStoragePolicy_LastEntry;
196 // create resource database key
198 if((dbContext->context.ldbid < 0x80) || (dbContext->context.ldbid == 0xFF) )
200 // The LDBID is used to find the DBID in the resource table.
201 if((dbContext->context.user_no == 0) && (dbContext->context.seat_no == 0))
204 // Node is added in front of the resource ID as the key string.
206 snprintf(dbKey, DbKeyMaxLen, "%s/%s", gNode, resource_id);
211 // Node is added in front of the resource ID as the key string.
213 if(dbContext->context.seat_no == 0)
215 // /User/<user_no_parameter> is added in front of the resource ID as the key string.
216 snprintf(dbKey, DbKeyMaxLen, "%s%d/%s", gUser, dbContext->context.user_no, resource_id);
220 // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string.
221 snprintf(dbKey, DbKeyMaxLen, "%s%d%s%d/%s", gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
224 storePolicy = PersistenceStorage_local;
227 if((dbContext->context.ldbid >= 0x80) && (dbContext->context.ldbid != 0xFF))
229 // The LDBID is used to find the DBID in the resource table.
230 // /<LDBID parameter> is added in front of the resource ID as the key string.
231 // Rational: Creates a namespace within one data base.
232 // Rational: Reduction of number of databases -> reduction of maintenance costs
233 // /User/<user_no_parameter> and /Seat/<seat_no_parameter> are add after /<LDBID parameter> if there are different than 0.
235 if(dbContext->context.seat_no != 0)
237 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, gSeat, dbContext->context.seat_no, resource_id);
241 snprintf(dbKey, DbKeyMaxLen, "/%x%s%d/%s", dbContext->context.ldbid, gUser, dbContext->context.user_no, resource_id);
243 storePolicy = PersistenceStorage_local;
247 // create resource database path
249 if(dbContext->context.ldbid < 0x80)
251 // S H A R E D database
253 if(dbContext->context.ldbid != 0)
255 // Additionally /GROUP/<LDBID_parameter> shall be added inside of the database path listed in the resource table. (Off target)
257 // shared G R O U P database * * * * * * * * * * * * * * * * * * *
259 if(PersistencePolicy_wc == dbContext->configKey.policy)
261 if(isFile == ResIsNoFile)
262 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, gSharedCached);
264 snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, dbKey);
266 else if(PersistencePolicy_wt == dbContext->configKey.policy)
268 if(isFile == ResIsNoFile)
269 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, gSharedWt);
271 snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, dbKey);
276 // Additionally /Shared/Public shall be added inside of the database path listed in the resource table. (Off target)
278 // shared P U B L I C database * * * * * * * * * * * * * * * * * *
280 if(PersistencePolicy_wc == dbContext->configKey.policy)
282 if(isFile == ResIsNoFile)
283 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, gSharedCached);
285 snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, dbKey);
287 else if(PersistencePolicy_wt == dbContext->configKey.policy)
289 if(isFile == ResIsNoFile)
290 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, gSharedWt);
292 snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, dbKey);
296 storePolicy = PersistenceStorage_shared; // we have a shared database
300 // L O C A L database
302 if(PersistencePolicy_wc == dbContext->configKey.policy)
304 if(isFile == ResIsNoFile)
305 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, gLocalCached);
307 snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, dbKey);
309 else if(PersistencePolicy_wt == dbContext->configKey.policy)
311 if(isFile == ResIsNoFile)
312 snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, gLocalWt);
314 snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, dbKey);
317 storePolicy = PersistenceStorage_local; // we have a local database
320 //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey);
321 //printf("get_db_path_and_key - dbPath : [path]: %s\n\n", dbPath);