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_key.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of the persistence client library.
16 * Library provides an API to access persistent data
20 #include "persistence_client_library_key.h"
22 #include "../include_protected/persistence_client_library_db_access.h"
23 #include "../include_protected/persistence_client_library_rc_table.h"
24 #include "../include_protected/crc32.h"
26 #include "persistence_client_library_handle.h"
27 #include "persistence_client_library_pas_interface.h"
28 #include "persistence_client_library_prct_access.h"
29 #include "persistence_client_library_custom_loader.h"
32 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
34 // function with handle
35 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 int pclKeyHandleOpen(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
41 PersistenceInfo_s dbContext;
43 char dbKey[DbKeyMaxLen]; // database key
44 char dbPath[DbPathMaxLen]; // database location
46 memset(dbKey, 0, DbKeyMaxLen);
47 memset(dbPath, 0, DbPathMaxLen);
49 dbContext.context.ldbid = ldbid;
50 dbContext.context.seat_no = seat_no;
51 dbContext.context.user_no = user_no;
53 // get database context: database path and database key
54 handle = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
57 if(dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry) // check if store policy is valid
59 if(PersistenceStorage_custom == dbContext.configKey.storage)
61 int idx = custom_client_name_to_id(dbPath, 1);
62 char workaroundPath[128]; // workaround, because /sys/ can not be accessed on host!!!!
63 snprintf(workaroundPath, 128, "%s%s", "/Data", dbPath );
65 if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_open != NULL) )
67 int flag = 0, mode = 0;
68 handle = gPersCustomFuncs[idx].custom_plugin_handle_open(workaroundPath, flag, mode);
72 handle = EPERS_NOPLUGINFUNCT;
77 handle = get_persistence_handle_idx();
80 if(handle < MaxPersHandle && handle != -1)
82 // remember data in handle array
83 strncpy(gHandleArray[handle].dbPath, dbPath, DbPathMaxLen);
84 strncpy(gHandleArray[handle].dbKey, dbKey, DbKeyMaxLen);
85 gHandleArray[handle].info = dbContext;
89 printf("key_handle_open: error - handleId out of bounds [%d]\n", handle);
100 int pclKeyHandleClose(int key_handle)
104 if(key_handle < MaxPersHandle)
106 if(PersistenceStorage_custom == gHandleArray[key_handle].info.configKey.storage )
108 int idx = custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
110 if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_close) )
112 gPersCustomFuncs[idx].custom_plugin_handle_close(key_handle);
116 rval = EPERS_NOPLUGINFUNCT;
121 set_persistence_handle_close_idx(key_handle);
124 // invalidate entries
125 strncpy(gHandleArray[key_handle].dbPath, "", DbPathMaxLen);
126 strncpy(gHandleArray[key_handle].dbKey ,"", DbKeyMaxLen);
127 gHandleArray[key_handle].info.configKey.storage = -1;
139 int pclKeyHandleGetSize(int key_handle)
143 if(key_handle < MaxPersHandle)
145 if(PersistenceStorage_custom == gHandleArray[key_handle].info.configKey.storage)
147 int idx = custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
149 if(idx < PersCustomLib_LastEntry && &(gPersCustomFuncs[idx].custom_plugin_get_size) != NULL)
151 gPersCustomFuncs[idx].custom_plugin_get_size(gHandleArray[key_handle].dbPath);
155 size = EPERS_NOPLUGINFUNCT;
160 size = pers_db_get_key_size(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey,
161 &gHandleArray[key_handle].info);
170 int pclKeyHandleReadData(int key_handle, unsigned char* buffer, int buffer_size)
173 if(key_handle < MaxPersHandle)
175 if(PersistenceStorage_custom == gHandleArray[key_handle].info.configKey.storage)
177 int idx = custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
179 if(idx < PersCustomLib_LastEntry && &(gPersCustomFuncs[idx].custom_plugin_handle_get_data) != NULL)
181 gPersCustomFuncs[idx].custom_plugin_handle_get_data(key_handle, (char*)buffer, buffer_size-1);
185 size = EPERS_NOPLUGINFUNCT;
190 size = pers_db_read_key(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey,
191 &gHandleArray[key_handle].info, buffer, buffer_size);
200 int pclKeyHandleRegisterNotifyOnChange(int key_handle)
209 int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size)
213 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
215 if(buffer_size <= gMaxKeyValDataSize) // check data size
217 if(key_handle < MaxPersHandle)
219 if(PersistenceStorage_custom == gHandleArray[key_handle].info.configKey.storage)
221 int idx = custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
223 if(idx < PersCustomLib_LastEntry && *gPersCustomFuncs[idx].custom_plugin_handle_set_data != NULL)
225 gPersCustomFuncs[idx].custom_plugin_handle_set_data(key_handle, (char*)buffer, buffer_size-1);
229 size = EPERS_NOPLUGINFUNCT;
234 size = pers_db_write_key(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey,
235 &gHandleArray[key_handle].info, buffer, buffer_size);
240 size = EPERS_MAXHANDLE;
245 printf("key_handle_write_data: error - buffer_size to big, limit is [%d] bytes\n", gMaxKeyValDataSize);
260 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
262 // functions to be used directly without a handle
263 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 int pclKeyDelete(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
270 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
272 PersistenceInfo_s dbContext;
274 char dbKey[DbKeyMaxLen]; // database key
275 char dbPath[DbPathMaxLen]; // database location
277 memset(dbKey, 0, DbKeyMaxLen);
278 memset(dbPath, 0, DbPathMaxLen);
280 dbContext.context.ldbid = ldbid;
281 dbContext.context.seat_no = seat_no;
282 dbContext.context.user_no = user_no;
284 // get database context: database path and database key
285 rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
288 if( dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry
289 && dbContext.configKey.storage >= PersistenceStorage_local) // check if store policy is valid
291 rval = pers_db_delete_key(dbPath, dbKey, &dbContext);
310 int pclKeyGetSize(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
313 PersistenceInfo_s dbContext;
315 char dbKey[DbKeyMaxLen]; // database key
316 char dbPath[DbPathMaxLen]; // database location
318 memset(dbKey, 0, DbKeyMaxLen);
319 memset(dbPath, 0, DbPathMaxLen);
321 dbContext.context.ldbid = ldbid;
322 dbContext.context.seat_no = seat_no;
323 dbContext.context.user_no = user_no;
325 // get database context: database path and database key
326 data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
329 if( dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry
330 && dbContext.configKey.storage >= PersistenceStorage_local) // check if store policy is valid
332 data_size = pers_db_get_key_size(dbPath, dbKey, &dbContext);
336 data_size = EPERS_BADPOL;
341 data_size = EPERS_BADPOL;
350 int pclKeyReadData(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no,
351 unsigned char* buffer, int buffer_size)
355 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
357 PersistenceInfo_s dbContext;
359 char dbKey[DbKeyMaxLen]; // database key
360 char dbPath[DbPathMaxLen]; // database location
362 memset(dbKey, 0, DbKeyMaxLen);
363 memset(dbPath, 0, DbPathMaxLen);
365 dbContext.context.ldbid = ldbid;
366 dbContext.context.seat_no = seat_no;
367 dbContext.context.user_no = user_no;
369 // get database context: database path and database key
370 data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
374 if( dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry
375 && dbContext.configKey.storage >= PersistenceStorage_local) // check if store policy is valid
377 data_size = pers_db_read_key(dbPath, dbKey, &dbContext, buffer, buffer_size);
381 data_size = EPERS_BADPOL;
387 data_size = EPERS_LOCKFS;
395 int pclKeyWriteData(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no,
396 unsigned char* buffer, int buffer_size)
400 if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
402 if(buffer_size <= gMaxKeyValDataSize) // check data size
404 PersistenceInfo_s dbContext;
406 unsigned int hash_val_data = 0;
408 char dbKey[DbKeyMaxLen]; // database key
409 char dbPath[DbPathMaxLen]; // database location
411 memset(dbKey, 0, DbKeyMaxLen);
412 memset(dbPath, 0, DbPathMaxLen);
414 dbContext.context.ldbid = ldbid;
415 dbContext.context.seat_no = seat_no;
416 dbContext.context.user_no = user_no;
418 // get database context: database path and database key
419 data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
422 // get hash value of data to verify storing
423 hash_val_data = crc32(hash_val_data, buffer, buffer_size);
426 if( dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry
427 && dbContext.configKey.storage >= PersistenceStorage_local) // check if store policy is valid
429 data_size = pers_db_write_key(dbPath, dbKey, &dbContext, buffer, buffer_size);
433 data_size = EPERS_BADPOL;
439 data_size = EPERS_BUFLIMIT;
440 printf("key_write_data: error - buffer_size to big, limit is [%d] bytes\n", gMaxKeyValDataSize);
445 data_size = EPERS_LOCKFS;
453 // status: TODO implement register on change
454 int pclKeyRegisterNotifyOnChange(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
457 PersistenceInfo_s dbContext;
459 // unsigned int hash_val_data = 0;
460 char dbKey[DbKeyMaxLen]; // database key
461 char dbPath[DbPathMaxLen]; // database location
463 memset(dbKey, 0, DbKeyMaxLen);
464 memset(dbPath, 0, DbPathMaxLen);
466 dbContext.context.ldbid = ldbid;
467 dbContext.context.seat_no = seat_no;
468 dbContext.context.user_no = user_no;
470 // get database context: database path and database key
471 rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
473 // registration is only on shared key possible
474 if(PersistenceStorage_shared == rval)
476 rval = persistence_reg_notify_on_change(dbPath, dbKey);