Changed file and key-value API according GENIVI naming conventions
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_key.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
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 ******************************************************************************/
11  /**
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
17  * @see            
18  */
19
20 #include "persistence_client_library_key.h"
21
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"
25
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"
30
31
32 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
34 // function with handle
35 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
37
38 int pclKeyHandleOpen(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
39 {
40    int handle = 0;
41    PersistenceInfo_s dbContext;
42
43    char dbKey[DbKeyMaxLen];      // database key
44    char dbPath[DbPathMaxLen];    // database location
45
46    memset(dbKey, 0, DbKeyMaxLen);
47    memset(dbPath, 0, DbPathMaxLen);
48
49    dbContext.context.ldbid   = ldbid;
50    dbContext.context.seat_no = seat_no;
51    dbContext.context.user_no = user_no;
52
53    // get database context: database path and database key
54    handle = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
55    if(handle >= 0)
56    {
57       if(dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry)  // check if store policy is valid
58       {
59          if(PersistenceStorage_custom ==  dbContext.configKey.storage)
60          {
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  );
64
65             if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_open != NULL) )
66             {
67                int flag = 0, mode = 0;
68                handle = gPersCustomFuncs[idx].custom_plugin_handle_open(workaroundPath, flag, mode);
69             }
70             else
71             {
72                handle = EPERS_NOPLUGINFUNCT;
73             }
74          }
75          else
76          {
77             handle = get_persistence_handle_idx();
78          }
79
80          if(handle < MaxPersHandle && handle != -1)
81          {
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;
86          }
87          else
88          {
89             printf("key_handle_open: error - handleId out of bounds [%d]\n", handle);
90          }
91       }
92    }
93
94
95    return handle;
96 }
97
98
99
100 int pclKeyHandleClose(int key_handle)
101 {
102    int rval = 0;
103
104    if(key_handle < MaxPersHandle)
105    {
106       if(PersistenceStorage_custom == gHandleArray[key_handle].info.configKey.storage )
107       {
108          int idx =  custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
109
110          if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_close) )
111          {
112             gPersCustomFuncs[idx].custom_plugin_handle_close(key_handle);
113          }
114          else
115          {
116             rval = EPERS_NOPLUGINFUNCT;
117          }
118       }
119       else
120       {
121          set_persistence_handle_close_idx(key_handle);
122       }
123
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;
128    }
129    else
130    {
131       rval = -1;
132    }
133
134    return rval;
135 }
136
137
138
139 int pclKeyHandleGetSize(int key_handle)
140 {
141    int size = 0;
142
143    if(key_handle < MaxPersHandle)
144    {
145       if(PersistenceStorage_custom ==  gHandleArray[key_handle].info.configKey.storage)
146       {
147          int idx =  custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
148
149          if(idx < PersCustomLib_LastEntry && &(gPersCustomFuncs[idx].custom_plugin_get_size) != NULL)
150          {
151             gPersCustomFuncs[idx].custom_plugin_get_size(gHandleArray[key_handle].dbPath);
152          }
153          else
154          {
155             size = EPERS_NOPLUGINFUNCT;
156          }
157       }
158       else
159       {
160          size = pers_db_get_key_size(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey,
161                                           &gHandleArray[key_handle].info);
162       }
163    }
164
165    return size;
166 }
167
168
169
170 int pclKeyHandleReadData(int key_handle, unsigned char* buffer, int buffer_size)
171 {
172    int size = 0;
173    if(key_handle < MaxPersHandle)
174    {
175       if(PersistenceStorage_custom ==  gHandleArray[key_handle].info.configKey.storage)
176       {
177          int idx =  custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
178
179          if(idx < PersCustomLib_LastEntry && &(gPersCustomFuncs[idx].custom_plugin_handle_get_data) != NULL)
180          {
181             gPersCustomFuncs[idx].custom_plugin_handle_get_data(key_handle, (char*)buffer, buffer_size-1);
182          }
183          else
184          {
185             size = EPERS_NOPLUGINFUNCT;
186          }
187       }
188       else
189       {
190          size = pers_db_read_key(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey,
191                                      &gHandleArray[key_handle].info, buffer, buffer_size);
192       }
193    }
194
195    return size;
196 }
197
198
199
200 int pclKeyHandleRegisterNotifyOnChange(int key_handle)
201 {
202    int rval = -1;
203
204    return rval;
205 }
206
207
208
209 int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size)
210 {
211    int size = 0;
212
213    if(AccessNoLock != isAccessLocked() )     // check if access to persistent data is locked
214    {
215       if(buffer_size <= gMaxKeyValDataSize)  // check data size
216       {
217          if(key_handle < MaxPersHandle)
218          {
219             if(PersistenceStorage_custom ==  gHandleArray[key_handle].info.configKey.storage)
220             {
221                int idx =  custom_client_name_to_id(gHandleArray[key_handle].dbPath, 1);
222
223                if(idx < PersCustomLib_LastEntry && *gPersCustomFuncs[idx].custom_plugin_handle_set_data != NULL)
224                {
225                   gPersCustomFuncs[idx].custom_plugin_handle_set_data(key_handle, (char*)buffer, buffer_size-1);
226                }
227                else
228                {
229                   size = EPERS_NOPLUGINFUNCT;
230                }
231             }
232             else
233             {
234                size = pers_db_write_key(gHandleArray[key_handle].dbPath, gHandleArray[key_handle].dbKey,
235                                            &gHandleArray[key_handle].info, buffer, buffer_size);
236             }
237          }
238          else
239          {
240             size = EPERS_MAXHANDLE;
241          }
242       }
243       else
244       {
245          printf("key_handle_write_data: error - buffer_size to big, limit is [%d] bytes\n", gMaxKeyValDataSize);
246       }
247    }
248    else
249    {
250       size = EPERS_LOCKFS;
251    }
252
253    return size;
254 }
255
256
257
258
259
260 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
262 // functions to be used directly without a handle
263 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
265
266 int pclKeyDelete(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
267 {
268    int rval = 0;
269
270    if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
271    {
272       PersistenceInfo_s dbContext;
273
274      char dbKey[DbKeyMaxLen];      // database key
275      char dbPath[DbPathMaxLen];    // database location
276
277      memset(dbKey, 0, DbKeyMaxLen);
278      memset(dbPath, 0, DbPathMaxLen);
279
280      dbContext.context.ldbid   = ldbid;
281      dbContext.context.seat_no = seat_no;
282      dbContext.context.user_no = user_no;
283
284      // get database context: database path and database key
285      rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
286      if(rval >= 0)
287      {
288         if(   dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry
289            && dbContext.configKey.storage >= PersistenceStorage_local)   // check if store policy is valid
290         {
291            rval = pers_db_delete_key(dbPath, dbKey, &dbContext);
292         }
293         else
294         {
295           rval = EPERS_BADPOL;
296         }
297      }
298    }
299    else
300    {
301       rval = EPERS_LOCKFS;
302    }
303
304    return rval;
305 }
306
307
308
309 // status: OK
310 int pclKeyGetSize(unsigned int ldbid, const char* resource_id, unsigned int user_no, unsigned int seat_no)
311 {
312    int data_size = 0;
313    PersistenceInfo_s dbContext;
314
315    char dbKey[DbKeyMaxLen];      // database key
316    char dbPath[DbPathMaxLen];    // database location
317
318    memset(dbKey, 0, DbKeyMaxLen);
319    memset(dbPath, 0, DbPathMaxLen);
320
321    dbContext.context.ldbid   = ldbid;
322    dbContext.context.seat_no = seat_no;
323    dbContext.context.user_no = user_no;
324
325    // get database context: database path and database key
326    data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
327    if(data_size >= 0)
328    {
329       if(   dbContext.configKey.storage < PersistenceStoragePolicy_LastEntry
330          && dbContext.configKey.storage >= PersistenceStorage_local)   // check if store policy is valid
331       {
332          data_size = pers_db_get_key_size(dbPath, dbKey, &dbContext);
333       }
334       else
335       {
336         data_size = EPERS_BADPOL;
337       }
338    }
339    else
340    {
341      data_size = EPERS_BADPOL;
342    }
343
344    return data_size;
345 }
346
347
348
349 // status: OK
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)
352 {
353    int data_size = 0;
354
355    if(AccessNoLock != isAccessLocked() ) // check if access to persistent data is locked
356    {
357       PersistenceInfo_s dbContext;
358
359       char dbKey[DbKeyMaxLen];      // database key
360       char dbPath[DbPathMaxLen];    // database location
361
362       memset(dbKey, 0, DbKeyMaxLen);
363       memset(dbPath, 0, DbPathMaxLen);
364
365       dbContext.context.ldbid   = ldbid;
366       dbContext.context.seat_no = seat_no;
367       dbContext.context.user_no = user_no;
368
369       // get database context: database path and database key
370       data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
371       if(data_size >= 0)
372       {
373
374          if(   dbContext.configKey.storage <  PersistenceStoragePolicy_LastEntry
375             && dbContext.configKey.storage >= PersistenceStorage_local)   // check if store policy is valid
376          {
377             data_size = pers_db_read_key(dbPath, dbKey, &dbContext, buffer, buffer_size);
378          }
379          else
380          {
381             data_size = EPERS_BADPOL;
382          }
383       }
384    }
385    else
386    {
387       data_size = EPERS_LOCKFS;
388    }
389
390    return data_size;
391 }
392
393
394
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)
397 {
398    int data_size = 0;
399
400    if(AccessNoLock != isAccessLocked() )     // check if access to persistent data is locked
401    {
402       if(buffer_size <= gMaxKeyValDataSize)  // check data size
403       {
404          PersistenceInfo_s dbContext;
405
406          unsigned int hash_val_data = 0;
407
408          char dbKey[DbKeyMaxLen];      // database key
409          char dbPath[DbPathMaxLen];    // database location
410
411          memset(dbKey, 0, DbKeyMaxLen);
412          memset(dbPath, 0, DbPathMaxLen);
413
414          dbContext.context.ldbid   = ldbid;
415          dbContext.context.seat_no = seat_no;
416          dbContext.context.user_no = user_no;
417
418          // get database context: database path and database key
419          data_size = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
420          if(data_size >= 0)
421          {
422             // get hash value of data to verify storing
423             hash_val_data = crc32(hash_val_data, buffer, buffer_size);
424
425             // store data
426             if(   dbContext.configKey.storage <  PersistenceStoragePolicy_LastEntry
427                && dbContext.configKey.storage >= PersistenceStorage_local)   // check if store policy is valid
428             {
429                data_size = pers_db_write_key(dbPath, dbKey, &dbContext, buffer, buffer_size);
430             }
431             else
432             {
433                data_size = EPERS_BADPOL;
434             }
435          }
436       }
437       else
438       {
439          data_size = EPERS_BUFLIMIT;
440          printf("key_write_data: error - buffer_size to big, limit is [%d] bytes\n", gMaxKeyValDataSize);
441       }
442    }
443    else
444    {
445       data_size = EPERS_LOCKFS;
446    }
447
448    return data_size;
449 }
450
451
452
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)
455 {
456    int rval = 0;
457    PersistenceInfo_s dbContext;
458
459    //   unsigned int hash_val_data = 0;
460    char dbKey[DbKeyMaxLen];      // database key
461    char dbPath[DbPathMaxLen];    // database location
462
463    memset(dbKey, 0, DbKeyMaxLen);
464    memset(dbPath, 0, DbPathMaxLen);
465
466    dbContext.context.ldbid   = ldbid;
467    dbContext.context.seat_no = seat_no;
468    dbContext.context.user_no = user_no;
469
470    // get database context: database path and database key
471    rval = get_db_context(&dbContext, resource_id, ResIsNoFile, dbKey, dbPath);
472
473    // registration is only on shared key possible
474    if(PersistenceStorage_shared == rval)
475    {
476       rval = persistence_reg_notify_on_change(dbPath, dbKey);
477    }
478
479    return rval;
480 }
481
482
483
484
485
486
487
488