summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_db_access.c
blob: a102607664e23ea73890d3f4a314195d0557d13d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/******************************************************************************
 * Project         Persistency
 * (c) copyright   2012
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_data_access.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of persistence database access
 *                 Library provides an API to access persistent data
 * @see            
 */

#include "persistence_client_library_db_access.h"
#include "persistence_client_library_custom_loader.h"
#include "persistence_client_library_dbus_service.h"
#include "persistence_client_library_prct_access.h"

#include <persComErrors.h>
#include <persComDataOrg.h>
#include <persComDbAccess.h>

#include <dbus/dbus.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>



/// btree array
static int gHandlesDB[DbTableSize][PersistencePolicy_LastEntry];
static int gHandlesDBCreated[DbTableSize][PersistencePolicy_LastEntry] = { {0} };


// function prototype
int pers_send_Notification_Signal(const char* key, PersistenceDbContext_s* context, unsigned int reason);


char* pers_get_raw_key(char *key)
{
   char *temp = NULL;
   char *rawKey = key;

   temp = strrchr(key, (int)'/');
   
   if (NULL != temp)
   {
   	  rawKey = temp + 1;
   }

   return rawKey;
}


int pers_db_open_default(const char* dbPath, PersDefaultType_e DefaultType)
{
   int ret = 0;
   char path[DbPathMaxLen] = {0};

   if (PersDefaultType_Configurable == DefaultType)
   {
      snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalConfigurableDefault);
   }
   else if (PersDefaultType_Factory== DefaultType)
   {
      snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalFactoryDefault);
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_open_default ==> unknown DefaultType"));
      ret = EPERS_COMMON;
   }

   if (EPERS_COMMON != ret)
   {
      ret = persComDbOpen(path, 0);
      if (ret < 0)
      {
         ret = EPERS_COMMON;
         DLT_LOG(gPclDLTContext, DLT_LOG_WARN,
                              DLT_STRING("pers_db_open_default() -> persComDbOpen() -> problem open db: "),
                              DLT_STRING(path),
                              DLT_STRING(" Code: "),
                              DLT_INT(ret));
      }
   }

   return ret;
}


int pers_get_defaults(char* dbPath, char* key, unsigned char* buffer, unsigned int buffer_size, PersGetDefault_e job)
{
   PersDefaultType_e i = PersDefaultType_Configurable;
   int handleDefaultDB = -1;
   int read_size = EPERS_NOKEY;

   key = pers_get_raw_key(key); /* We need only the raw key without a prefixed '/node/' or '/user/1/seat/0' etc... */

   for(i=0; i<PersDefaultType_LastEntry; i++)
   {
      handleDefaultDB = pers_db_open_default(dbPath, i);
      if(handleDefaultDB >= 0)
      {
         if (PersGetDefault_Data == job)
         {
         }
         else if (PersGetDefault_Size == job)
         {
            read_size = persComDbGetKeySize(handleDefaultDB, key);
         }
         else
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_get_defaults ==> unknown job"));
            break;
         }

         if (0 > persComDbClose(handleDefaultDB))
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_get_defaults ==> persComDbClose returned with error"));
         }

         if(read_size < 0) // check read_size
         {
            if (PersDefaultType_Configurable == i)
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Key '"),
                                                  DLT_STRING(key),
                                                  DLT_STRING("' not found in "),
                                                  DLT_STRING(dbPath),
                                                  DLT_STRING(gLocalConfigurableDefault));
            }
            else if (PersDefaultType_Factory == i)
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Key '"),
                                                  DLT_STRING(key),
                                                  DLT_STRING("' not found in "),
                                                  DLT_STRING(dbPath),
                                                  DLT_STRING(gLocalFactoryDefault));
            }
            else
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_get_defaults ==> unknown PersDefaultType: "),
                                                   DLT_INT(i));
            }

            if(PERS_COM_ERR_NOT_FOUND == read_size)
            {
               read_size = EPERS_NOKEY;
            }
         }
         else
         {
            break;
         }
      }
   }

   return read_size;

}


int database_get(PersistenceInfo_s* info, const char* dbPath)
{
   int arrayIdx = 0;
   int handleDB = -1;

   // create array index: index is a combination of resource config table type and group
   arrayIdx = info->configKey.storage + info->context.ldbid ;

   //if(arrayIdx <= DbTableSize)
   if(arrayIdx < DbTableSize)
   {
      if(gHandlesDBCreated[arrayIdx][info->configKey.policy] == 0)
      {

         char path[DbPathMaxLen] = {0};

         if(PersistencePolicy_wt == info->configKey.policy)
         {
            snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalWt);
         }
         else if(PersistencePolicy_wc == info->configKey.policy)
         {
            snprintf(path, DbPathMaxLen, "%s%s", dbPath, gLocalCached);
         }
         else
         {
            handleDB = -2;
         }

         if (handleDB == -1)
         {
            handleDB = persComDbOpen(path, 0x01);
            if(handleDB >= 0)
            {
               gHandlesDB[arrayIdx][info->configKey.policy] = handleDB ;
               gHandlesDBCreated[arrayIdx][info->configKey.policy] = 1;
            }
            else
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_get ==> persComDbOpen() failed"));
            }
         }
         else
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_get ==> wrong policy! Cannot extend dbPath wit database."));
         }
      }
      else
      {
         handleDB = gHandlesDB[arrayIdx][info->configKey.policy];
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_get ==> invalid storage type"), DLT_STRING(dbPath));
   }



   return handleDB;
}


void database_close(PersistenceInfo_s* info)
{
   int arrayIdx = info->configKey.storage + info->context.ldbid;

   if(info->configKey.storage <= PersistenceStorage_shared )
   {
      int iErrorCode = persComDbClose(gHandlesDB[arrayIdx][info->configKey.policy]) ;
      if (iErrorCode < 0)
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close ==> persComDbClose() failed"));
      }
      else
      {
        gHandlesDBCreated[arrayIdx][info->configKey.policy] = 0;
      }
   }
   else
   {
      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close ==> invalid storage type"), DLT_INT(info->context.ldbid ));
   }
}

void database_close_all()
{
   int i = 0;

   for(i=0; i<DbTableSize; i++)
   {
      // close write cached database
      if(gHandlesDBCreated[i][PersistencePolicy_wc] == 1)
      {
         int iErrorCode = persComDbClose(gHandlesDB[i][PersistencePolicy_wc]);
         if (iErrorCode < 0)
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close_all => failed to close db => persComDbClose"));
         }
         else
         {
             gHandlesDBCreated[i][PersistencePolicy_wc] = 0;
         }
      }

      // close write through database
      if(gHandlesDBCreated[i][PersistencePolicy_wt] == 1)
      {
         int iErrorCode = persComDbClose(gHandlesDB[i][PersistencePolicy_wt]);
         if (iErrorCode < 0)
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("database_close_all => failed to close db => persComDbClose"));
         }
         else
         {
            gHandlesDBCreated[i][PersistencePolicy_wt] = 0;
         }
      }
   }
}



int persistence_get_data(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size)
{
   int read_size = -1;
   int ret_defaults = -1;

   if(   PersistenceStorage_shared == info->configKey.storage
      || PersistenceStorage_local == info->configKey.storage)
   {
      int handleDB = database_get(info, dbPath);
      if(handleDB >= 0)
      {
         read_size = persComDbReadKey(handleDB, key, (char*)buffer, buffer_size) ;
         if(read_size < 0)
         {
            read_size = pers_get_defaults(dbPath, key, buffer, buffer_size, PersGetDefault_Data); /* 0 ==> Get data */
         }
      }
   }
   else if(PersistenceStorage_custom == info->configKey.storage)   // custom storage implementation via custom library
   {
      int idx =  custom_client_name_to_id(dbPath, 1);
      char workaroundPath[128];  // workaround, because /sys/ can not be accessed on host!!!!
      snprintf(workaroundPath, 128, "%s%s", "/Data", dbPath  );

      if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_get_data != NULL) )
      {
         char pathKeyString[128] = {0};
         if(info->configKey.customID[0] == '\0')   // if we have not a customID we use the key
         {
            snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
         }
         else
         {
            snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
         }
         read_size = gPersCustomFuncs[idx].custom_plugin_get_data(pathKeyString, (char*)buffer, buffer_size);
      }
      else
      {
         read_size = EPERS_NOPLUGINFUNCT;
      }

      if (1 > read_size)
      {
         info->configKey.policy = PersistencePolicy_wc;			/* Set the policy */
         info->configKey.type   = PersistenceResourceType_key;  /* Set the type */
         (void)get_db_path_and_key(info, key, NULL, dbPath);
         DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Custom Key not available. Try to get default keys from:"),
                                            DLT_STRING(dbPath),
                                            DLT_STRING(key));
         ret_defaults = pers_get_defaults(dbPath, key, buffer, buffer_size, PersGetDefault_Data);
         if (0 < ret_defaults)
         {
        	 read_size = ret_defaults;
         }
      }
   }
   return read_size;
}



int persistence_set_data(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size)
{
   int write_size = -1;

   if(   PersistenceStorage_local == info->configKey.storage
      || PersistenceStorage_shared == info->configKey.storage )
   {
      int handleDB = -1 ;


      handleDB = database_get(info, dbPath);
      if(handleDB >= 0)
      {
         write_size = persComDbWriteKey(handleDB, key, (char*)buffer, buffer_size) ;
         if(write_size < 0)
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> persComDbWriteKey() failure"));
         }

         if(PersistenceStorage_shared == info->configKey.storage)
         {
            int rval = pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_changed);
            if(rval <= 0)
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> failed to send notification signal"));
               write_size = rval;
            }
         }

      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> no resource config table"), DLT_STRING(dbPath), DLT_STRING(key));
         write_size = EPERS_NOPRCTABLE;
      }
   }
   else if(PersistenceStorage_custom == info->configKey.storage)   // custom storage implementation via custom library
   {
      int idx = custom_client_name_to_id(dbPath, 1);
      if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_set_data != NULL) )
      {
         char pathKeyString[128] = {0};
         if(info->configKey.customID[0] == '\0')   // if we have not a customID we use the key
         {
            snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
         }
         else
         {
            snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
         }
         write_size = gPersCustomFuncs[idx].custom_plugin_set_data(pathKeyString, (char*)buffer, buffer_size);

         if (write_size == buffer_size) /* Check return value and send notification if OK */
         {
            int rval = pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_changed);
            if(rval <= 0)
            {
               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_set_data ==> failed to send notification signal"));
               write_size = rval;
            }
         }
      }
      else
      {
         write_size = EPERS_NOPLUGINFUNCT;
      }
   }
   return write_size;
}



int persistence_get_data_size(char* dbPath, char* key, PersistenceInfo_s* info)
{
   int read_size = -1;
   int ret_defaults = -1;

   if(   PersistenceStorage_shared == info->configKey.storage
      || PersistenceStorage_local == info->configKey.storage)
   {
      int handleDB = database_get(info, dbPath);
      if(handleDB >= 0)
      {
         read_size = persComDbGetKeySize(handleDB, key);
         if(read_size < 0)
         {
            read_size = pers_get_defaults(dbPath, key, NULL, 0, PersGetDefault_Size);
         }
      }
   }
   else if(PersistenceStorage_custom == info->configKey.storage)   // custom storage implementation via custom library
   {
      int idx = custom_client_name_to_id(dbPath, 1);
      if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_get_size != NULL) )
      {
         char pathKeyString[128] = {0};
         if(info->configKey.customID[0] == '\0')   // if we have not a customID we use the key
         {
            snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
         }
         else
         {
            snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
         }
         read_size = gPersCustomFuncs[idx].custom_plugin_get_size(pathKeyString);
      }
      else
      {
         read_size = EPERS_NOPLUGINFUNCT;
      }

      if (1 > read_size)
      {
         info->configKey.policy = PersistencePolicy_wc;			/* Set the policy */
         info->configKey.type   = PersistenceResourceType_key;  /* Set the type */
         (void)get_db_path_and_key(info, key, NULL, dbPath);
         DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Custom Key not available. Try to get default keys from:"),
                                            DLT_STRING(dbPath),
                                            DLT_STRING(key));
         ret_defaults = pers_get_defaults(dbPath, key, NULL, 0, PersGetDefault_Size);
         if (0 < ret_defaults)
         {
        	 read_size = ret_defaults;
         }
      }
   }
   return read_size;
}



int persistence_delete_data(char* dbPath, char* key, PersistenceInfo_s* info)
{
   int ret = 0;
   if(PersistenceStorage_custom != info->configKey.storage)
   {
      int handleDB = database_get(info, dbPath);
      if(handleDB >= 0)
      {
         ret = persComDbDeleteKey(handleDB, key) ;
         if(ret < 0)
         {
            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_delete_data => persComDbDeleteKey failed: "), DLT_STRING(key));
            if(PERS_COM_ERR_NOT_FOUND == ret)
            {
                ret = EPERS_NOKEY ;
            }
            else
            {
                ret = EPERS_DB_ERROR_INTERNAL ;
            }
         }

         if(PersistenceStorage_shared == info->configKey.storage)
         {
            pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_deleted);
         }
      }
      else
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_delete_data ==> no resource config table"), DLT_STRING(dbPath), DLT_STRING(key));
         ret = EPERS_NOPRCTABLE;
      }
   }
   else   // custom storage implementation via custom library
   {
      int idx = custom_client_name_to_id(dbPath, 1);
      if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_delete_data != NULL) )
      {
         char pathKeyString[128] = {0};
         if(info->configKey.customID[0] == '\0')   // if we have not a customID we use the key
         {
            snprintf(pathKeyString, 128, "0x%08X/%s/%s", info->context.ldbid, info->configKey.custom_name, key);
         }
         else
         {
            snprintf(pathKeyString, 128, "0x%08X/%s", info->context.ldbid, info->configKey.customID);
         }
         ret = gPersCustomFuncs[idx].custom_plugin_delete_data(pathKeyString);

         if(0 <= ret) /* Check return value and send notification if OK */
         {
            pers_send_Notification_Signal(key, &info->context, pclNotifyStatus_deleted);
         }
      }
      else
      {
         ret = EPERS_NOPLUGINFUNCT;
      }
   }
   return ret;
}


int persistence_notify_on_change(char* key, unsigned int ldbid, unsigned int user_no, unsigned int seat_no,
                                 pclChangeNotifyCallback_t callback, PersNotifyRegPolicy_e regPolicy)
{
   int rval = 0;

   if(regPolicy < Notify_lastEntry)
   {
      snprintf(gNotifykey, DbKeyMaxLen, "%s", key);
      gNotifyLdbid  = ldbid;     // to do: pass correct ==> JUST TESTING!!!!
      gNotifyUserNo = user_no;
      gNotifySeatNo = seat_no;
      gNotifyReason = regPolicy;

      if(regPolicy == Notify_register)
      {
         // assign callback
         gChangeNotifyCallback = callback;
      }
      else if(regPolicy == Notify_unregister)
      {
         // remove callback
         gChangeNotifyCallback = NULL;
      }

      if(-1 == deliverToMainloop(CMD_REG_NOTIFY_SIGNAL, 0, 0))
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("persistence_notify_on_change => failed to write to pipe"), DLT_INT(errno));
         rval = -1;
      }
   }
   else
   {
      rval = -1;
   }

   return rval;
}






int pers_send_Notification_Signal(const char* key, PersistenceDbContext_s* context, pclNotifyStatus_e reason)
{
   int rval = 1;
   if(reason < pclNotifyStatus_lastEntry)
   {
      snprintf(gNotifykey,  DbKeyMaxLen,       "%s", key);

      gNotifyLdbid  = context->ldbid;     // to do: pass correct ==> JUST TESTING!!!!
      gNotifyUserNo = context->user_no;
      gNotifySeatNo = context->seat_no;
      gNotifyReason = reason;

      if(-1 == deliverToMainloop(CMD_SEND_NOTIFY_SIGNAL, 0,0) )
      {
         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_send_Notification_Signal => failed to write to pipe"), DLT_INT(errno));
         rval = EPERS_NOTIFY_SIG;
      }
   }
   else
   {
      rval = EPERS_NOTIFY_SIG;
   }

   return rval;
}


void pers_rct_close_all()
{
   int i = 0;

   // close open persistence resource configuration table
   for(i=0; i< PrctDbTableSize; i++)
   {
   	if(gResource_table[i] != 0)
   	{
			if(persComRctClose(i) != 0)
			{
				DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close db => index:"), DLT_INT(i));
			}

			gResource_table[i] = 0;
   	}
   }
}