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_data_access.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of persistence database access
16 * Library provides an API to access persistent data
20 #include "persistence_client_library_db_access.h"
21 #include "persistence_client_library_custom_loader.h"
22 #include "persistence_client_library_dbus_service.h"
23 #include "persistence_client_library_prct_access.h"
25 #include <persComErrors.h>
26 #include <persComDataOrg.h>
27 #include <persComDbAccess.h>
29 #include <dbus/dbus.h>
38 static int gHandlesDB[DbTableSize][PersistencePolicy_LastEntry];
39 static int gHandlesDBCreated[DbTableSize][PersistencePolicy_LastEntry] = { {0} };
43 int pers_send_Notification_Signal(const char* key, PersistenceDbContext_s* context, unsigned int reason);
46 char* pers_get_raw_key(char *key)
51 temp = strrchr(key, (int)'/');
62 int pers_db_open_default(const char* dbPath, PersDefaultType_e DefaultType)
65 char path[DbPathMaxLen] = {0};
67 if (PersDefaultType_Configurable == DefaultType)
69 snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalConfigurableDefault);
71 else if (PersDefaultType_Factory== DefaultType)
73 snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalFactoryDefault);
77 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_open_default ==> unknown DefaultType"));
81 if (EPERS_COMMON != ret)
83 ret = persComDbOpen(path, 0);
87 DLT_LOG(gPclDLTContext, DLT_LOG_WARN,
88 DLT_STRING("pers_db_open_default() -> persComDbOpen() -> problem open db: "),
90 DLT_STRING(" Code: "),
99 int pers_get_defaults(char* dbPath, char* key, unsigned char* buffer, unsigned int buffer_size, PersGetDefault_e job)
101 PersDefaultType_e i = PersDefaultType_Configurable;
102 int handleDefaultDB = -1;
103 int read_size = EPERS_NOKEY;
105 key = pers_get_raw_key(key); /* We need only the raw key without a prefixed '/node/' or '/user/1/seat/0' etc... */
107 for(i=0; i<PersDefaultType_LastEntry; i++)
109 handleDefaultDB = pers_db_open_default(dbPath, i);
110 if(handleDefaultDB >= 0)
112 if (PersGetDefault_Data == job)
115 else if (PersGetDefault_Size == job)
117 read_size = persComDbGetKeySize(handleDefaultDB, key);
121 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_get_defaults ==> unknown job"));
125 if (0 > persComDbClose(handleDefaultDB))
127 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_get_defaults ==> persComDbClose returned with error"));
130 if(read_size < 0) // check read_size
132 if (PersDefaultType_Configurable == i)
134 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Key '"),
136 DLT_STRING("' not found in "),
138 DLT_STRING(gLocalConfigurableDefault));
140 else if (PersDefaultType_Factory == i)
142 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Key '"),
144 DLT_STRING("' not found in "),
146 DLT_STRING(gLocalFactoryDefault));
150 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_get_defaults ==> unknown PersDefaultType: "),
154 if(PERS_COM_ERR_NOT_FOUND == read_size)
156 read_size = EPERS_NOKEY;
171 int database_get(PersistenceInfo_s* info, const char* dbPath)
176 // create array index: index is a combination of resource config table type and group
177 arrayIdx = info->configKey.storage + info->context.ldbid ;
179 //if(arrayIdx <= DbTableSize)
180 if(arrayIdx < DbTableSize)
182 if(gHandlesDBCreated[arrayIdx][info->configKey.policy] == 0)
185 char path[DbPathMaxLen] = {0};
187 if(PersistencePolicy_wt == info->configKey.policy)
189 snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalWt);
191 else if(PersistencePolicy_wc == info->configKey.policy)
193 snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalCached);
202 handleDB = persComDbOpen(path, 0x01);
205 gHandlesDB[arrayIdx][info->configKey.policy] = handleDB ;
206 gHandlesDBCreated[arrayIdx][info->configKey.policy] = 1;
210 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_get ==> persComDbOpen() failed"));
215 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_get ==> wrong policy! Cannot extend dbPath wit database."));
220 handleDB = gHandlesDB[arrayIdx][info->configKey.policy];
225 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_get ==> invalid storage type"), DLT_STRING(dbPath));
234 void database_close(PersistenceInfo_s* info)
236 int arrayIdx = info->configKey.storage + info->context.ldbid;
238 if(info->configKey.storage <= PersistenceStorage_shared )
240 int iErrorCode = persComDbClose(gHandlesDB[arrayIdx][info->configKey.policy]) ;
243 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close ==> persComDbClose() failed"));
247 gHandlesDBCreated[arrayIdx][info->configKey.policy] = 0;
252 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close ==> invalid storage type"), DLT_INT(info->context.ldbid ));
256 void database_close_all()
260 for(i=0; i<DbTableSize; i++)
262 // close write cached database
263 if(gHandlesDBCreated[i][PersistencePolicy_wc] == 1)
265 int iErrorCode = persComDbClose(gHandlesDB[i][PersistencePolicy_wc]);
268 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close_all => failed to close db => persComDbClose"));
272 gHandlesDBCreated[i][PersistencePolicy_wc] = 0;
276 // close write through database
277 if(gHandlesDBCreated[i][PersistencePolicy_wt] == 1)
279 int iErrorCode = persComDbClose(gHandlesDB[i][PersistencePolicy_wt]);
282 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close_all => failed to close db => persComDbClose"));
286 gHandlesDBCreated[i][PersistencePolicy_wt] = 0;
294 int persistence_get_data(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size)
297 int ret_defaults = -1;
299 if( PersistenceStorage_shared == info->configKey.storage
300 || PersistenceStorage_local == info->configKey.storage)
302 int handleDB = database_get(info, dbPath);
305 read_size = persComDbReadKey(handleDB, key, (char*)buffer, buffer_size) ;
308 read_size = pers_get_defaults(dbPath, key, buffer, buffer_size, PersGetDefault_Data); /* 0 ==> Get data */
312 else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library
314 int idx = custom_client_name_to_id(dbPath, 1);
315 char workaroundPath[128]; // workaround, because /sys/ can not be accessed on host!!!!
316 snprintf(workaroundPath, 128, "%s%s", "/Data", dbPath );
318 if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_get_data != NULL) )
320 char pathKeyString[128] = {0};
321 if(info->configKey.customID[0] == '\0') // if we have not a customID we use the key
323 snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
327 snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
329 read_size = gPersCustomFuncs[idx].custom_plugin_get_data(pathKeyString, (char*)buffer, buffer_size);
333 read_size = EPERS_NOPLUGINFUNCT;
338 info->configKey.policy = PersistencePolicy_wc; /* Set the policy */
339 info->configKey.type = PersistenceResourceType_key; /* Set the type */
340 (void)get_db_path_and_key(info, key, NULL, dbPath);
341 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Custom Key not available. Try to get default keys from:"),
344 ret_defaults = pers_get_defaults(dbPath, key, buffer, buffer_size, PersGetDefault_Data);
345 if (0 < ret_defaults)
347 read_size = ret_defaults;
356 int persistence_set_data(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size)
360 if( PersistenceStorage_local == info->configKey.storage
361 || PersistenceStorage_shared == info->configKey.storage )
366 handleDB = database_get(info, dbPath);
369 write_size = persComDbWriteKey(handleDB, key, (char*)buffer, buffer_size) ;
372 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> persComDbWriteKey() failure"));
375 if(PersistenceStorage_shared == info->configKey.storage)
377 int rval = pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_changed);
380 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> failed to send notification signal"));
388 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> no resource config table"), DLT_STRING(dbPath), DLT_STRING(key));
389 write_size = EPERS_NOPRCTABLE;
392 else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library
394 int idx = custom_client_name_to_id(dbPath, 1);
395 if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_set_data != NULL) )
397 char pathKeyString[128] = {0};
398 if(info->configKey.customID[0] == '\0') // if we have not a customID we use the key
400 snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
404 snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
406 write_size = gPersCustomFuncs[idx].custom_plugin_set_data(pathKeyString, (char*)buffer, buffer_size);
408 if (write_size == buffer_size) /* Check return value and send notification if OK */
410 int rval = pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_changed);
413 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> failed to send notification signal"));
420 write_size = EPERS_NOPLUGINFUNCT;
428 int persistence_get_data_size(char* dbPath, char* key, PersistenceInfo_s* info)
431 int ret_defaults = -1;
433 if( PersistenceStorage_shared == info->configKey.storage
434 || PersistenceStorage_local == info->configKey.storage)
436 int handleDB = database_get(info, dbPath);
439 read_size = persComDbGetKeySize(handleDB, key);
442 read_size = pers_get_defaults(dbPath, key, NULL, 0, PersGetDefault_Size);
446 else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library
448 int idx = custom_client_name_to_id(dbPath, 1);
449 if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_get_size != NULL) )
451 char pathKeyString[128] = {0};
452 if(info->configKey.customID[0] == '\0') // if we have not a customID we use the key
454 snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
458 snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
460 read_size = gPersCustomFuncs[idx].custom_plugin_get_size(pathKeyString);
464 read_size = EPERS_NOPLUGINFUNCT;
469 info->configKey.policy = PersistencePolicy_wc; /* Set the policy */
470 info->configKey.type = PersistenceResourceType_key; /* Set the type */
471 (void)get_db_path_and_key(info, key, NULL, dbPath);
472 DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Custom Key not available. Try to get default keys from:"),
475 ret_defaults = pers_get_defaults(dbPath, key, NULL, 0, PersGetDefault_Size);
476 if (0 < ret_defaults)
478 read_size = ret_defaults;
487 int persistence_delete_data(char* dbPath, char* key, PersistenceInfo_s* info)
490 if(PersistenceStorage_custom != info->configKey.storage)
492 int handleDB = database_get(info, dbPath);
495 ret = persComDbDeleteKey(handleDB, key) ;
498 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_delete_data => persComDbDeleteKey failed: "), DLT_STRING(key));
499 if(PERS_COM_ERR_NOT_FOUND == ret)
505 ret = EPERS_DB_ERROR_INTERNAL ;
509 if(PersistenceStorage_shared == info->configKey.storage)
511 pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_deleted);
516 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_delete_data ==> no resource config table"), DLT_STRING(dbPath), DLT_STRING(key));
517 ret = EPERS_NOPRCTABLE;
520 else // custom storage implementation via custom library
522 int idx = custom_client_name_to_id(dbPath, 1);
523 if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_delete_data != NULL) )
525 char pathKeyString[128] = {0};
526 if(info->configKey.customID[0] == '\0') // if we have not a customID we use the key
528 snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
532 snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
534 ret = gPersCustomFuncs[idx].custom_plugin_delete_data(pathKeyString);
536 if(0 <= ret) /* Check return value and send notification if OK */
538 pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_deleted);
543 ret = EPERS_NOPLUGINFUNCT;
550 int persistence_notify_on_change(char* key, unsigned int ldbid, unsigned int user_no, unsigned int seat_no,
551 pclChangeNotifyCallback_t callback, PersNotifyRegPolicy_e regPolicy)
555 if(regPolicy < Notify_lastEntry)
557 snprintf(gNotifykey, DbKeyMaxLen, "%s", key);
558 gNotifyLdbid = ldbid; // to do: pass correct ==> JUST TESTING!!!!
559 gNotifyUserNo = user_no;
560 gNotifySeatNo = seat_no;
561 gNotifyReason = regPolicy;
563 if(regPolicy == Notify_register)
566 gChangeNotifyCallback = callback;
568 else if(regPolicy == Notify_unregister)
571 gChangeNotifyCallback = NULL;
574 if(-1 == deliverToMainloop(CMD_REG_NOTIFY_SIGNAL, 0, 0))
576 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_notify_on_change => failed to write to pipe"), DLT_INT(errno));
593 int pers_send_Notification_Signal(const char* key, PersistenceDbContext_s* context, pclNotifyStatus_e reason)
596 if(reason < pclNotifyStatus_lastEntry)
598 snprintf(gNotifykey, DbKeyMaxLen, "%s", key);
600 gNotifyLdbid = context->ldbid; // to do: pass correct ==> JUST TESTING!!!!
601 gNotifyUserNo = context->user_no;
602 gNotifySeatNo = context->seat_no;
603 gNotifyReason = reason;
605 if(-1 == deliverToMainloop(CMD_SEND_NOTIFY_SIGNAL, 0,0) )
607 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_send_Notification_Signal => failed to write to pipe"), DLT_INT(errno));
608 rval = EPERS_NOTIFY_SIG;
613 rval = EPERS_NOTIFY_SIG;
620 void pers_rct_close_all()
624 // close open persistence resource configuration table
625 for(i=0; i< PrctDbTableSize; i++)
627 if(gResource_table[i] != -1)
629 if(persComRctClose(i) == -1)
631 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close db => index:"), DLT_INT(i));
634 gResource_table[i] = -1;