1 /**
2  ****************************************************************************************
3  *
4  * @file nvds.h
5  *
6  * @brief Non Volatile Data Storage (NVDS) driver
7  *
8  * Copyright (C) RivieraWaves 2009-2015
9  *
10  ****************************************************************************************
11  */
12 #ifndef _NVDS_H_
13 #define _NVDS_H_
14 
15 /**
16  ****************************************************************************************
17  * @addtogroup NVDS
18  * @ingroup COMMON
19  * @brief Non Volatile Data Storage (NVDS)
20  *
21  *   Parameters management
22  *   there are two compilation options:
23  *     + NVDS_8BIT_TAGLENGTH :
24  *       if set, each TAG has a maximum length of 256 bytes
25  *       if not set, each TAG has a maximum length of 65536 bytes
26  *     + NVDS_PACKED :
27  *       if not set, all the TAG header structures and TAG data contents are stored with an
28  *       alignment on 32 bit boundary
29  *       if set, all the TAG header structures and TAG data contents are stored
30  *       consecutively without gaps (as would be a structure with pragma packed)
31  *     + NVDS_READ_WRITE :
32  *       if not set, only GET action on TAGs is provided.
33  *       if set, PUT/DEL/LOCK actions are provided in addition of GET action.
34  *
35  * @{
36  ****************************************************************************************
37  */
38 
39 /*
40  * INCLUDE FILES
41  ****************************************************************************************
42  */
43 #include <stdbool.h>           // boolean definition
44 #include <stdint.h>            // integer definition
45 
46 
47 /*
48  * DEFINES
49  ****************************************************************************************
50  */
51 
52 /// NVDS is defined as read-write
53 #define NVDS_READ_WRITE          1
54 
55 /// NVDS is defined as packed
56 #define NVDS_PACKED              1
57 
58 /// NVDS has 8-bit length tags
59 #define NVDS_8BIT_TAGLENGTH      1
60 
61 /// Type of the tag length (8 or 16 bits)
62 #if (NVDS_8BIT_TAGLENGTH)
63 typedef uint8_t  nvds_tag_len_t;
64 #else
65 typedef uint16_t nvds_tag_len_t;
66 #endif // NVDS_8BIT_TAGLENGTH
67 
68 //#define  FLASH_BASE_ADDR 0x400
69 //#define  FLASH_BLE_SIZE  0x400
70 /*
71  * ENUMERATION DEFINITIONS
72  ****************************************************************************************
73  */
74 
75 /// Possible Returned Status
76 enum NVDS_STATUS
77 {
78     /// NVDS status OK
79     NVDS_OK,
80     /// generic NVDS status KO
81     NVDS_FAIL,
82     /// NVDS TAG unrecognized
83     NVDS_TAG_NOT_DEFINED,
84     /// No space for NVDS
85     NVDS_NO_SPACE_AVAILABLE,
86     /// Length violation
87     NVDS_LENGTH_OUT_OF_RANGE,
88     /// NVDS parameter locked
89     NVDS_PARAM_LOCKED,
90     /// NVDS corrupted
91     NVDS_CORRUPT
92 };
93 
94 /// List of NVDS TAG identifiers
95 enum NVDS_TAG
96 {
97     /// Definition of the tag associated to each parameters
98     /// Local Bd Address
99     NVDS_TAG_BD_ADDRESS                 = 0x01,
100     /// Device Name
101     NVDS_TAG_DEVICE_NAME                = 0x02,
102     /// Radio Drift
103     NVDS_TAG_LPCLK_DRIFT                = 0x07,
104     /// Radio Jitter
105     NVDS_TAG_LPCLK_JITTER               = 0x08,
106     /// Radio Class
107     NVDS_TAG_RADIO_CLASS                = 0x09,
108     /// Bluejay specific Settings
109     NVDS_TAG_BJ_TXCNTL1                 = 0x0A,
110     /// External wake-up time
111     NVDS_TAG_EXT_WAKEUP_TIME            = 0x0D,
112     /// Oscillator wake-up time
113     NVDS_TAG_OSC_WAKEUP_TIME            = 0x0E,
114     /// Radio wake-up time
115     NVDS_TAG_RM_WAKEUP_TIME             = 0x0F,
116     /// UART baudrate
117     NVDS_TAG_UART_BAUDRATE              = 0x10,
118     /// Enable sleep mode
119     NVDS_TAG_SLEEP_ENABLE               = 0x11,
120     /// Enable External Wakeup
121     NVDS_TAG_EXT_WAKEUP_ENABLE          = 0x12,
122     /// SP Private Key 192
123     NVDS_TAG_SP_PRIVATE_KEY_P192        = 0x13,
124     /// SP Public Key 192
125     NVDS_TAG_SP_PUBLIC_KEY_P192         = 0x14,
126     /// Errata adopted check
127     NVDS_TAG_ERRATA_ADOPTED             = 0x15,
128     /// CQDDR Tags
129     NVDS_TAG_BASIC_THRESHOLD            = 0x16,
130     NVDS_TAG_EDR_THRESHOLD              = 0x17,
131     NVDS_TAG_BASIC_ALGORITHM            = 0x18,
132     NVDS_TAG_EDR_ALGORITHM              = 0x19,
133     NVDS_TAG_BASIC_PACKET_LUT           = 0x2A,
134     NVDS_TAG_EDR_PACKET_LUT             = 0x2B,
135     /// Synchronous links configuration
136     NVDS_TAG_SYNC_CONFIG                = 0x2C,
137     /// PCM Settings
138     NVDS_TAG_PCM_SETTINGS               = 0x2D,
139     /// Sleep algorithm duration
140     NVDS_TAG_SLEEP_ALGO_DUR             = 0x2E,
141 
142     /// Diagport configuration
143     NVDS_TAG_DIAG_BT_HW                 = 0x30,
144     NVDS_TAG_DIAG_BLE_HW                = 0x31,
145     NVDS_TAG_DIAG_SW                    = 0x32,
146     NVDS_TAG_DIAG_PLF                   = 0x34,
147     NVDS_TAG_IDCSEL_PLF                 = 0x37,
148 
149     /// RSSI threshold tags
150     NVDS_TAG_RSSI_HIGH_THR              = 0x3A,
151     NVDS_TAG_RSSI_LOW_THR               = 0x3B,
152     NVDS_TAG_RSSI_INTERF_THR            = 0x3C,
153 
154     /// BLE Channel Assessment tags
155     NVDS_TAG_BLE_CA_TIMER_DUR           = 0x40,
156     NVDS_TAG_BLE_CRA_TIMER_CNT          = 0x41,
157     NVDS_TAG_BLE_CA_MIN_THR             = 0x42,
158     NVDS_TAG_BLE_CA_MAX_THR             = 0x43,
159     NVDS_TAG_BLE_CA_NOISE_THR           = 0x44,
160 
161     /// AFH algorithm tags
162     NVDS_TAG_AFH_REASS_NBCH             = 0x51,
163     NVDS_TAG_AFH_WINLGTH                = 0x52,
164     NVDS_TAG_AFH_RSSIMIN                = 0x53,
165     NVDS_TAG_AFH_PERTHRESBAD            = 0x54,
166     NVDS_TAG_AFH_REASS_INT              = 0x55,
167     NVDS_TAG_AFH_NMIN                   = 0x56,
168     NVDS_TAG_AFH_MAXADAPT               = 0x57,
169     NVDS_TAG_AFH_THSMIN                 = 0x58,
170 
171 
172     NVDS_TAG_BT_LINK_KEY_FIRST          = 0x60,
173     NVDS_TAG_BT_LINK_KEY_LAST           = 0x67,
174 
175     NVDS_TAG_BLE_LINK_KEY_FIRST         = 0x70,
176     NVDS_TAG_BLE_LINK_KEY_LAST          = 0x7F,
177     /// SP Private Key (Low Energy)
178     NVDS_TAG_LE_PRIVATE_KEY_P256        = 0x80,
179     /// SP Public Key (Low Energy)
180     NVDS_TAG_LE_PUBLIC_KEY_P256         = 0x81,
181     /// SP Debug: Used Fixed Private Key from NVDS (Low Energy)
182     NVDS_TAG_LE_DBG_FIXED_P256_KEY_EN   = 0x82,
183     /// SP Private Key (classic BT)
184     NVDS_TAG_SP_PRIVATE_KEY_P256        = 0x83,
185     /// SP Public Key (classic BT)
186     NVDS_TAG_SP_PUBLIC_KEY_P256         = 0x84,
187 
188     /// Application specific
189     NVDS_TAG_APP_SPECIFIC_FIRST         = 0x90,
190     NVDS_TAG_APP_SPECIFIC_LAST          = 0xAF,
191 };
192 
193 /// List of NVDS Tag lengths
194 enum NVDS_LEN
195 {
196      // Definition of length associated to each parameters
197      /// Local Bd Address
198      NVDS_LEN_BD_ADDRESS                 = 6,
199      /// Device Name
200      NVDS_LEN_DEVICE_NAME                = 24,
201      /// Low power clock drift
202      NVDS_LEN_LPCLK_DRIFT                = 2,
203      /// Low power clock jitter
204      NVDS_LEN_LPCLK_JITTER               = 1,
205      /// Radio Class
206      NVDS_LEN_RADIO_CLASS                = 1,
207      /// Bluejay specific Settings
208      NVDS_LEN_BJ_TXCNTL1                 = 4,
209 
210 
211      /// External wake-up time
212      NVDS_LEN_EXT_WAKEUP_TIME            = 2,
213      /// Oscillator wake-up time
214      NVDS_LEN_OSC_WAKEUP_TIME            = 2,
215      /// Radio wake-up time
216      NVDS_LEN_RM_WAKEUP_TIME             = 2,
217      /// UART baudrate
218      NVDS_LEN_UART_BAUDRATE              = 4,
219      /// Enable sleep mode
220      NVDS_LEN_SLEEP_ENABLE               = 1,
221      /// Enable External Wakeup
222      NVDS_LEN_EXT_WAKEUP_ENABLE          = 1,
223      /// SP Private Key 192
224      NVDS_LEN_SP_PRIVATE_KEY_P192        = 24,
225      /// SP Public Key 192
226      NVDS_LEN_SP_PUBLIC_KEY_P192         = 48,
227      /// Errata adopted check
228      NVDS_LEN_ERRATA_ADOPTED             = 1,
229      /// CQDDR Tags
230      NVDS_LEN_BASIC_THRESHOLD            = 70,
231      NVDS_LEN_EDR_THRESHOLD              = 70,
232      NVDS_LEN_BASIC_ALGORITHM            = 21,
233      NVDS_LEN_EDR_ALGORITHM              = 21,
234      NVDS_LEN_BASIC_PACKET_LUT           = 16,
235      NVDS_LEN_EDR_PACKET_LUT             = 16,
236      /// Synchronous links configuration
237      NVDS_LEN_SYNC_CONFIG                = 2,
238      /// PCM Settings
239      NVDS_LEN_PCM_SETTINGS               = 8,
240      /// Sleep algorithm duration
241      NVDS_LEN_SLEEP_ALGO_DUR             = 2,
242      /// Diagport configuration
243      NVDS_LEN_DIAG_BT_HW                 = 4,
244      NVDS_LEN_DIAG_BLE_HW                = 4,
245      NVDS_LEN_DIAG_SW                    = 4,
246      NVDS_LEN_DIAG_PLF                   = 4,
247      NVDS_LEN_IDCSEL_PLF                 = 4,
248      /// RSSI thresholds
249      NVDS_LEN_RSSI_THR                   = 1,
250 
251 
252      NVDS_LEN_BLE_CA_TIMER_DUR           = 2,
253      NVDS_LEN_BLE_CRA_TIMER_CNT          = 1,
254      NVDS_LEN_BLE_CA_MIN_THR             = 1,
255      NVDS_LEN_BLE_CA_MAX_THR             = 1,
256      NVDS_LEN_BLE_CA_NOISE_THR           = 1,
257 
258      /// AFH algorithm tags
259      NVDS_LEN_AFH_REASS_NBCH             = 1,
260      NVDS_LEN_AFH_WINLGTH                = 1,
261      NVDS_LEN_AFH_RSSIMIN                = 1,
262      NVDS_LEN_AFH_PERTHRESBAD            = 1,
263      NVDS_LEN_AFH_REASS_INT              = 1,
264      NVDS_LEN_AFH_NMIN                   = 1,
265      NVDS_LEN_AFH_MAXADAPT               = 1,
266      NVDS_LEN_AFH_THSMIN                 = 1,
267      /// Link keys
268      NVDS_LEN_BT_LINK_KEY                = 22,
269      NVDS_LEN_BLE_LINK_KEY               = 48,
270 
271      /// P256
272      NVDS_LEN_LE_PRIVATE_KEY_P256        = 32,
273      NVDS_LEN_LE_PUBLIC_KEY_P256         = 64,
274      NVDS_LEN_LE_DBG_FIXED_P256_KEY_EN   = 1,
275      NVDS_LEN_SP_PRIVATE_KEY_P256        = 32,
276      NVDS_LEN_SP_PUBLIC_KEY_P256         = 64,
277 };
278 
279 
280 /*
281  * FUNCTION DECLARATIONS
282  ****************************************************************************************
283  */
284 
285 /**
286  ****************************************************************************************
287  * @brief Initialize NVDS.
288  * @return NVDS_OK
289  ****************************************************************************************
290  */
291 uint8_t nvds_init(uint8_t *base, uint32_t len);
292 
293 /**
294  ****************************************************************************************
295  * @brief Look for a specific tag and return, if found and matching (in length), the
296  *        DATA part of the TAG.
297  *
298  * If the length does not match, the TAG header structure is still filled, in order for
299  * the caller to be able to check the actual length of the TAG.
300  *
301  * @param[in]  tag     TAG to look for whose DATA is to be retrieved
302  * @param[in]  length  Expected length of the TAG
303  * @param[out] buf     A pointer to the buffer allocated by the caller to be filled with
304  *                     the DATA part of the TAG
305  *
306  * @return  NVDS_OK                  The read operation was performed
307  *          NVDS_LENGTH_OUT_OF_RANGE The length passed in parameter is different than the TAG's
308  ****************************************************************************************
309  */
310 uint8_t nvds_get(uint8_t tag, nvds_tag_len_t * lengthPtr, uint8_t *buf);
311 
312 #if (NVDS_READ_WRITE == 1)
313 
314 /**
315  ****************************************************************************************
316  * @brief Look for a specific tag and delete it (Status set to invalid)
317  *
318  * Implementation notes
319  * 1. The write function call return status is not handled
320  *
321  * @param[in]  tag    TAG to mark as deleted
322  *
323  * @return NVDS_OK                TAG found and deleted
324  *         NVDS_PARAM_LOCKED    TAG found but can not be deleted because it is locked
325  *         (others)        return values from function call @ref nvds_browse_tag
326  ****************************************************************************************
327  */
328 uint8_t nvds_del(uint8_t tag);
329 
330 /**
331  ****************************************************************************************
332  * @brief Look for a specific tag and lock it (Status lock bit set to LOCK).
333  *
334  * The write function call return status is not handled
335  *
336  * @param[in]  tag    TAG to mark as locked
337  *
338  * @return NVDS_OK    TAG found and locked
339  *         (others)        return values from function call @ref nvds_browse_tag
340  ****************************************************************************************
341  */
342 uint8_t nvds_lock(uint8_t tag);
343 
344 /**
345  ****************************************************************************************
346  * @brief This function adds a specific TAG to the NVDS.
347  *
348  * Steps:
349  * 1) parse all the TAGs to:
350  * 1.1) calculate the total size of all the valid TAGs
351  * 1.2) erase the existing TAGs that have the same ID
352  * 1.3) check if we can use the same TAG area in case of an EEPROM
353  * 1.4) check that the TAG is not locked
354  * 2) if we have to add the new TAG at the end fo the NVDS (cant use same area):
355  * 2.1) allocate the appropriate amount of memory
356  * 2.2) purge the NVDS
357  * 2.3) free the memory allocated
358  * 2.4) check that there is now enough room for the new TAG or return
359  *      NO_SPACE_AVAILABLE
360  * 3) add the new TAG
361  *
362  * @param[in]  tag     TAG to look for whose DATA is to be retrieved
363  * @param[in]  length  Expected length of the TAG
364  * @param[in]  buf     Pointer to the buffer containing the DATA part of the TAG to add to
365  *                     the NVDS
366  *
367  * @return NVDS_OK                  New TAG correctly written to the NVDS
368  *         NVDS_PARAM_LOCKED        New TAG is trying to overwrite a TAG that is locked
369  *         NO_SPACE_AVAILABLE       New TAG can not fit in the available space in the NVDS
370  ****************************************************************************************
371  */
372 uint8_t nvds_put(uint8_t tag, nvds_tag_len_t length, uint8_t *buf);
373 
374 #endif //(NVDS_READ_WRITE == 1)
375 
376 /// @} NVDS
377 
378 #endif // _NVDS_H_
379