SmartSnippets DA1459x SDK
storage.h
Go to the documentation of this file.
1 
43 #ifndef STORAGE_H_
44 #define STORAGE_H_
45 
46 #include <stdbool.h>
47 #include "ble_common.h"
48 #include "ble_gap.h"
49 #include "ble_storage.h"
50 #include "sdk_queue.h"
51 
52 /*
53  * Following are internal storage keys (for app values) to be used internally - the should be
54  * outside ranges allowed to be set by application, so start them with 0xF0000000.
55  */
56 enum {
57  STORAGE_KEY_SVC_CHANGED_CCC = 0xF0000000,
58 };
59 
60 typedef struct {
61  void *next;
62 
64 
65  bool persistent;
66 
67  uint16_t length;
68  void *ptr;
69 
70  ble_storage_free_cb_t free_cb;
71 } app_value_t;
72 
73 typedef struct {
74  void *next;
75 
76  uint16_t handle;
77 } pending_event_t;
78 
79 typedef struct {
80  uint64_t rand;
81  uint16_t ediv;
82  uint8_t key[16];
83  uint8_t key_size;
84 } key_ltk_t;
85 
86 typedef struct {
87  uint8_t key[16];
88 } key_irk_t;
89 
90 typedef struct {
91  uint8_t key[16];
92  uint32_t sign_cnt;
93 } key_csrk_t;
94 
95 typedef struct {
96  void *next;
97 
98  bd_address_t addr;
99  uint16_t conn_idx;
100 
101  // state flags
102  bool connecting:1;
103  bool connected:1;
104  bool master:1;
105  bool paired:1;
106  bool bonded:1;
107 #if (dg_configBLE_PERIPHERAL == 1)
108  bool security_req_pending:1;
109 #endif /* (dg_configBLE_PERIPHERAL == 1) */
110  bool encrypted:1;
111  bool mitm:1;
112  bool resolving:1;
113 #if (dg_configBLE_SECURE_CONNECTIONS == 1)
114  bool secure:1;
115 #endif /* (dg_configBLE_SECURE_CONNECTIONS == 1) */
116  bool updating:1;
117 
118  // parameters
119  uint16_t mtu;
120  gap_sec_level_t sec_level;
121  gap_sec_level_t sec_level_req;
122  uint16_t ce_len_min;
123  uint16_t ce_len_max;
124 #if (dg_configBLE_2MBIT_PHY == 1)
125  ble_gap_phy_t tx_phy;
126  ble_gap_phy_t rx_phy;
127 #endif /* (dg_configBLE_2MBIT_PHY == 1) */
128 
129  // pairing information
130  key_ltk_t *ltk;
131  key_ltk_t *remote_ltk;
132  key_irk_t *irk;
133  key_csrk_t *csrk;
134  key_csrk_t *remote_csrk;
135 
136  // custom values set from application
137  queue_t app_value;
138  queue_t pending_events;
139 
140  // disconnection status (disconnection event is pending if other than zero)
141  uint8_t discon_reason;
142 } device_t;
143 
144 typedef void (* device_cb_t) (device_t *dev, void *ud);
145 
146 typedef bool (* device_match_cb_t) (const device_t *dev, void *ud);
147 
148 device_t *find_device_by_addr(const bd_address_t *addr, bool create);
149 
150 device_t *find_device_by_conn_idx(uint16_t conn_idx);
151 
152 device_t *find_device(device_match_cb_t cb, void *ud);
153 
154 void storage_init(void);
155 
156 void storage_cleanup(void);
157 
158 void storage_acquire(void);
159 
160 void storage_release(void);
161 
162 void storage_mark_dirty(bool flush_now);
163 
164 void device_foreach(device_cb_t cb, void *ud);
165 
166 void device_move_front(device_t *dev);
167 
168 void device_remove(device_t *dev);
169 
170 void device_remove_pairing(device_t *dev);
171 
172 void app_value_put(device_t *dev, ble_storage_key_t key, uint16_t length, void *ptr,
173  ble_storage_free_cb_t free_cb, bool persistent);
174 
175 bool app_value_get(device_t *dev, ble_storage_key_t key, uint16_t *length, void **ptr);
176 
177 void app_value_remove(device_t *dev, ble_storage_key_t key);
178 
179 void app_value_remove_np(device_t *dev);
180 
181 void pending_events_put_handle(device_t *dev, uint16_t handle);
182 
183 void pending_events_remove_handle(device_t *dev, uint16_t handle);
184 
185 bool pending_events_has_handle(const device_t *dev, uint16_t handle);
186 
187 void pending_events_clear_handles(device_t *dev);
188 
189 #endif /* STORAGE_H_ */
190 
sdk_queue.h
Simple helper to manage queue.
ble_gap_phy_t
ble_gap_phy_t
Definition: ble_gap.h:550
ble_storage_key_t
uint32_t ble_storage_key_t
Storage key (for indexing values in storage)
Definition: ble_storage.h:76
ble_storage_free_cb_t
void(* ble_storage_free_cb_t)(void *ptr)
Callback to free buffer stored in storage.
Definition: ble_storage.h:66
gap_sec_level_t
gap_sec_level_t
Definition: ble_gap.h:531
ble_common.h
Common definitions for BLE API.
ble_gap.h
BLE GAP API.
bd_address
Definition: ble_common.h:195
ble_storage.h
BLE persistent storage API.