summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_handle.c
blob: 2550b41c5a72be5b099d8e35dc251a53b120755c (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
/******************************************************************************
 * 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_handle.c
 * @ingroup        Persistence client library handle
 * @author         Ingo Huerner
 * @brief          Implementation of the persistence client library handle
 *                 Library provides an API to access persistent data
 * @see
 */


#include "persistence_client_library_handle.h"
#include "persistence_client_library_custom_loader.h"

#include <pthread.h>
#include <stdlib.h>

/// handle index
static int gHandleIdx = 1;

static int gInitialized = 0;

/// open file descriptor handle array
int gOpenFdArray[MaxPersHandle] = {0};

/// persistence key handle array
PersistenceKeyHandle_s gKeyHandleArray[MaxPersHandle];


/// persistence key handle array
PersistenceFileHandle_s gFileHandleArray[MaxPersHandle];


/// free handle array
int gFreeHandleArray[MaxPersHandle] = {0};

int gFreeHandleIdxHead = 0;

pthread_mutex_t gMtx;


/// get persistence handle
int get_persistence_handle_idx(char* dbPath, char* key, PersistenceInfo_s* info)
{
   int handle = 0;

   if(gInitialized == 0)
   {
      gInitialized = 1;
      pthread_mutex_init(&gMtx, 0);
   }

   if(pthread_mutex_lock(&gMtx) == 0)
   {
      if(   PersistenceStorage_shared == info->configKey.storage
         || PersistenceStorage_local == info->configKey.storage)
      {
         if(gFreeHandleIdxHead > 0)   // check if we have a free spot in the array before the current max
         {
            handle = gFreeHandleArray[--gFreeHandleIdxHead];
         }
         else
         {
            if(gHandleIdx < MaxPersHandle-1)
            {
               handle = gHandleIdx++;  // no free spot before current max, increment handle index
            }
            else
            {
               handle = -1;
               DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_persistence_handle_idx => Reached maximum of open handles: "), DLT_INT(MaxPersHandle));
            }
         }
      }
      else if(PersistenceStorage_custom ==  info->configKey.storage)
      {
         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_handle_open != NULL) )
         {
            int flag = 0, mode = 0;
            handle = gPersCustomFuncs[idx].custom_plugin_handle_open(workaroundPath, flag, mode);
         }
         else
         {
            handle = EPERS_NOPLUGINFUNCT;
         }
      }
      pthread_mutex_unlock(&gMtx);
   }
   return handle;
}


/// close persistence handle
int set_persistence_handle_close_idx(int handle, char* dbPath, char* key, PersistenceInfo_s* info)
{
   int rval = 0;

   if(pthread_mutex_lock(&gMtx) == 0)
   {
      if(   PersistenceStorage_shared == info->configKey.storage
         || PersistenceStorage_local == info->configKey.storage)
      {
         if(gFreeHandleIdxHead < MaxPersHandle)
         {
            gFreeHandleArray[gFreeHandleIdxHead++] = handle;
         }
      }
      else if(PersistenceStorage_custom == gKeyHandleArray[handle].info.configKey.storage )
      {
         int idx =  custom_client_name_to_id(gKeyHandleArray[handle].dbPath, 1);

         if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_close != NULL) )
         {
            rval = gPersCustomFuncs[idx].custom_plugin_handle_close(handle);
         }
         else
         {
            rval = EPERS_NOPLUGINFUNCT;
         }
      }

      pthread_mutex_unlock(&gMtx);
   }
}