summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsDataServer.c
blob: 530ac0a7502687753a1ffe6dbde94e4d20601f24 (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
/***************************************************************************//**
  \file pdsDataServer.c

  \brief Persistence Data Server implementation

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:
    22/01/08 A. Khromykh - Created
    01/11/10 A. Razinkov - Modified
*****************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <pdsMemAbstract.h>
#include <appTimer.h>
#include <pdsDataServer.h>
#include <pdsCrcService.h>
#include <pdsAuxService.h>
#include <leds.h>
#include <pdsWriteData.h>
#include <csPersistentMem.h>
#include <taskManager.h>

#ifdef _COMMISSIONING_
/******************************************************************************
                   Prototypes section
******************************************************************************/

/******************************************************************************
                   External variables section
******************************************************************************/
/* Number of records in csPersistentMemTable*/
extern const uint8_t csPersistentItemsAmount;

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
\brief Persist Data Server initialization procedure
******************************************************************************/
void PDS_Init(void)
{
  PDS_DataServerState_t status;

#ifdef USE_LED
  BSP_OpenLeds();
#endif

  /* Check if any valid data exists in persist memory */
  status = pdsCheckPersistMemory();

  /* Valid data exists - try to update from persist memory */
  if (PDS_SUCCESS == status)
  {
    if (PDS_EEPROM_ERROR != pdsUpdate())
    {
      pdsStartPersistServer();
    }
  }

  else if (PDS_EEPROM_ERROR != status)
  {
    pdsStartPersistServer();
  }
}

/******************************************************************************
\brief Set the default system area values and store persistent data to persist
      memory

\return operation result
******************************************************************************/
PDS_DataServerState_t PDS_SetToDefault(void)
{
  MEMORY_DESCRIPTOR descriptor;
  PDS_DataServerState_t status;

  CS_PdsDefaultValue();
  /* Store all persistent parameters */
  for (uint8_t index = 0; index < csPersistentItemsAmount; index++)
  {
    pdsPrepareMemoryAccess(index, &descriptor);
    status = pdsWrite(&descriptor, pdsDummyCallback);
    if (status != PDS_SUCCESS)
      return status;
  }

  /* Reset CRC area and rewrite CRC*/
  status = pdsClearCrcArea();
  if (status != PDS_SUCCESS)
    return status;
  status = pdsWriteCrc();
  if (status != PDS_SUCCESS)
    return status;

  return PDS_SUCCESS;
}

/******************************************************************************
\brief On-demand data storing in persist memory
******************************************************************************/
void PDS_FlushData(void)
{
  if (SAVE_IS_STOPED == savingIsStarted)
  {
    /* Force timer to finish - store data immediately */
    HAL_StopAppTimer(&pdsEepromSaveServiceTimer);
    pdsOnTimerSave();
  }

  while (SAVE_IS_STARTED == savingIsStarted)
  {
    SYS_ForceRunTask();
  }
}

/***************************************************************************//**
\brief Must be called only from ::APL_TaskHandler() function.\n
       Stops persist data server.
*******************************************************************************/
void PDS_Stop(void)
{
  while (SAVE_IS_STARTED == savingIsStarted)
  {
    SYS_ForceRunTask();
  }

  HAL_StopAppTimer(&pdsEepromSaveServiceTimer);
}
#endif /* _COMMISSIONING_ */

/******************************************************************************
\brief Read data from user area in persist memory

\param[in] offset - data offset in persist memory
\param[in] length - data lenght
\param[out] data - pointer to user data area in RAM
\param[out] callback - callback to read-finished event handler

\return operation result
******************************************************************************/
PDS_DataServerState_t PDS_ReadUserData(uint16_t offset, uint8_t *data, uint16_t length, void (*callback)(void))
{
  MEMORY_DESCRIPTOR descriptor;
  PDS_DataServerState_t status;

  descriptor.address = USER_BASE_EEPROM_ADDRESS + offset;
  descriptor.length = length;
  descriptor.data = data;

  while(SAVE_IS_STARTED == savingIsStarted)
  {
    SYS_ForceRunTask();
  }

  if (NULL == callback)
    callback = pdsDummyCallback;

  status = pdsRead(&descriptor, callback);
  if (status != PDS_SUCCESS)
    return status;

  return PDS_SUCCESS;
}

/******************************************************************************
\brief Write data to user area in persist memory
\
\param[in] offset - data offset in persist memory
\param[in] length - data lenght
\param[out] data - pointer to user data area
\param[out] callback - callback to read-finished event handler

\return operation result
******************************************************************************/
PDS_DataServerState_t PDS_WriteUserData(uint16_t offset, uint8_t *data, uint16_t length, void (*callback)(void))
{
  MEMORY_DESCRIPTOR descriptor;
  PDS_DataServerState_t status;

  descriptor.address = USER_BASE_EEPROM_ADDRESS + offset;
  descriptor.length = length;
  descriptor.data = data;

  while(SAVE_IS_STARTED == savingIsStarted)
  {
    SYS_ForceRunTask();
  }

  if (NULL == callback)
    callback = pdsDummyCallback;

  status = pdsWrite(&descriptor, callback);
  if (status != PDS_SUCCESS)
    return status;

  return PDS_SUCCESS;
}
// eof pdsDataServer.c