Contiki 2.5
settings.h
1 #ifndef __AVR_SETTINGS_H__
2 #define __AVR_SETTINGS_H__
3 
4 #include <inttypes.h>
5 #include <stdlib.h>
6 #include <stdbool.h>
7 
8 typedef uint16_t settings_key_t;
9 
10 #define SETTINGS_KEY_EUI64 'E'*256+'8' //!< Value always 8 bytes long
11 #define SETTINGS_KEY_EUI48 'E'*256+'6' //!< Value always 8 bytes long
12 #define SETTINGS_KEY_CHANNEL 'C'*256+'H' //!< Value always 1 byte long
13 #define SETTINGS_KEY_TXPOWER 'T'*256+'P' //!< Value always 1 byte long
14 #define SETTINGS_KEY_PAN_ID 'P'*256+'N' //!< Value always 2 bytes long
15 #define SETTINGS_KEY_PAN_ADDR 'P'*256+'A' //!< Value always 2 bytes long
16 #define SETTINGS_KEY_AES128KEY 'S'*256+'K' //!< Value always 16 bytes long
17 #define SETTINGS_KEY_AES128ENABLED 'S'*256+'E' //!< Value always 16 bytes long
18 
19 typedef enum {
20  SETTINGS_STATUS_OK=0,
21  SETTINGS_STATUS_INVALID_ARGUMENT,
22  SETTINGS_STATUS_NOT_FOUND,
23  SETTINGS_STATUS_OUT_OF_SPACE,
24  SETTINGS_STATUS_UNIMPLEMENTED,
25  SETTINGS_STATUS_FAILURE,
26 } settings_status_t;
27 
28 // Use this when you want to retrieve the last item
29 #define SETTINGS_LAST_INDEX (0xFF)
30 
31 #define SETTINGS_INVALID_KEY (0x00)
32 
33 #define SETTINGS_MAX_VALUE_SIZE (0x3FFF) // 16383 bytes
34 
35 extern settings_status_t settings_get(settings_key_t key,uint8_t index,unsigned char* value,size_t* value_size);
36 extern settings_status_t settings_add(settings_key_t key,const unsigned char* value,size_t value_size);
37 extern bool settings_check(settings_key_t key,uint8_t index);
38 extern void settings_wipe(void);
39 
40 extern settings_status_t settings_set(settings_key_t key,const unsigned char* value,size_t value_size);
41 extern settings_status_t settings_delete(settings_key_t key,uint8_t index);
42 
43 //#pragma mark -
44 //#pragma mark Inline convenience functions
45 
46 static inline uint8_t
47 settings_get_uint8(settings_key_t key,uint8_t index) {
48  uint8_t ret = 0;
49  size_t sizeof_uint8 = sizeof(uint8_t);
50  settings_get(key,index,(unsigned char*)&ret,&sizeof_uint8);
51  return ret;
52 }
53 
54 static inline settings_status_t
55 settings_add_uint8(settings_key_t key,uint8_t value) {
56  return settings_add(key,(const unsigned char*)&value,sizeof(uint8_t));
57 }
58 
59 static inline settings_status_t
60 settings_set_uint8(settings_key_t key,uint8_t value) {
61  return settings_set(key,(const unsigned char*)&value,sizeof(uint8_t));
62 }
63 
64 static inline uint16_t
65 settings_get_uint16(settings_key_t key,uint8_t index) {
66  uint16_t ret = 0;
67  size_t sizeof_uint16 = sizeof(uint16_t);
68  settings_get(key,index,(unsigned char*)&ret,&sizeof_uint16);
69  return ret;
70 }
71 
72 static inline settings_status_t
73 settings_add_uint16(settings_key_t key,uint16_t value) {
74  return settings_add(key,(const unsigned char*)&value,sizeof(uint16_t));
75 }
76 
77 static inline settings_status_t
78 settings_set_uint16(settings_key_t key,uint16_t value) {
79  return settings_set(key,(const unsigned char*)&value,sizeof(uint16_t));
80 }
81 
82 static inline uint32_t
83 settings_get_uint32(settings_key_t key,uint8_t index) {
84  uint32_t ret = 0;
85  size_t sizeof_uint32 = sizeof(uint32_t);
86  settings_get(key,index,(unsigned char*)&ret,&sizeof_uint32);
87  return ret;
88 }
89 
90 static inline settings_status_t
91 settings_add_uint32(settings_key_t key,uint32_t value) {
92  return settings_add(key,(const unsigned char*)&value,sizeof(uint32_t));
93 }
94 
95 static inline settings_status_t
96 settings_set_uint32(settings_key_t key,uint32_t value) {
97  return settings_set(key,(const unsigned char*)&value,sizeof(uint32_t));
98 }
99 
100 static inline uint64_t
101 settings_get_uint64(settings_key_t key,uint8_t index) {
102  uint64_t ret = 0;
103  size_t sizeof_uint64 = sizeof(uint64_t);
104  settings_get(key,index,(unsigned char*)&ret,&sizeof_uint64);
105  return ret;
106 }
107 
108 static inline settings_status_t
109 settings_add_uint64(settings_key_t key,uint64_t value) {
110  return settings_add(key,(const unsigned char*)&value,sizeof(uint64_t));
111 }
112 
113 static inline settings_status_t
114 settings_set_uint64(settings_key_t key,uint64_t value) {
115  return settings_set(key,(const unsigned char*)&value,sizeof(uint64_t));
116 }
117 
118 #endif