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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
167
168
169
172
173
174
177
178
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
207
208
209
210
211
212
213
214
215
218
219
222
223
227
230
231
232
233
234
235
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
270
271
272
273
274
277
278
279
280
281
282
283
284
285
286
287
288
289
290
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
315
318
319
320
321
322
323
324
325
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
475
476
477
478
479
/* ... */
#define BTSTACK_FILE__ "le_device_db_tlv.c"
#include "ble/le_device_db.h"
#include "ble/le_device_db_tlv.h"
#include "ble/core.h"
#include <string.h>
#include "btstack_debug.h"
5 includes
#define INVALID_ENTRY_ADDR_TYPE 0xff
typedef struct le_device_db_entry_t {
uint32_t seq_nr;
int addr_type;
bd_addr_t addr;
sm_key_t irk;
sm_key_t ltk;
uint16_t ediv;
uint8_t rand[8];
uint8_t key_size;
uint8_t authenticated;
uint8_t authorized;
uint8_t secure_connection;
#ifdef ENABLE_LE_SIGNED_WRITE
sm_key_t remote_csrk;
uint32_t remote_counter;
sm_key_t local_csrk;
uint32_t local_counter;/* ... */
#endif
...} le_device_db_entry_t;
#ifndef NVM_NUM_DEVICE_DB_ENTRIES
#error "NVM_NUM_DEVICE_DB_ENTRIES not defined, please define in btstack_config.h"
#endif
#if NVM_NUM_DEVICE_DB_ENTRIES == 0
#error "NVM_NUM_DEVICE_DB_ENTRIES must not be 0, please update in btstack_config.h"
#endif
static uint8_t entry_map[NVM_NUM_DEVICE_DB_ENTRIES];
static uint32_t num_valid_entries;
static const btstack_tlv_t * le_device_db_tlv_btstack_tlv_impl;
static void * le_device_db_tlv_btstack_tlv_context;
static uint32_t le_device_db_tlv_tag_for_index(uint8_t index){
static const char tag_0 = 'B';
static const char tag_1 = 'T';
static const char tag_2 = 'D';
return (tag_0 << 24u) | (tag_1 << 16u) | (tag_2 << 8u) | index;
}{ ... }
static bool le_device_db_tlv_fetch(int index, le_device_db_entry_t * entry){
btstack_assert(le_device_db_tlv_btstack_tlv_impl != NULL);
btstack_assert(index >= 0);
btstack_assert(index < NVM_NUM_DEVICE_DB_ENTRIES);
uint32_t tag = le_device_db_tlv_tag_for_index(index);
int size = le_device_db_tlv_btstack_tlv_impl->get_tag(le_device_db_tlv_btstack_tlv_context, tag, (uint8_t*) entry, sizeof(le_device_db_entry_t));
return size == sizeof(le_device_db_entry_t);
}{ ... }
static bool le_device_db_tlv_store(int index, le_device_db_entry_t * entry){
btstack_assert(le_device_db_tlv_btstack_tlv_impl != NULL);
btstack_assert(index >= 0);
btstack_assert(index < NVM_NUM_DEVICE_DB_ENTRIES);
uint32_t tag = le_device_db_tlv_tag_for_index(index);
int result = le_device_db_tlv_btstack_tlv_impl->store_tag(le_device_db_tlv_btstack_tlv_context, tag, (uint8_t*) entry, sizeof(le_device_db_entry_t));
return result == 0;
}{ ... }
static bool le_device_db_tlv_delete(int index){
btstack_assert(le_device_db_tlv_btstack_tlv_impl != NULL);
btstack_assert(index >= 0);
btstack_assert(index < NVM_NUM_DEVICE_DB_ENTRIES);
uint32_t tag = le_device_db_tlv_tag_for_index(index);
le_device_db_tlv_btstack_tlv_impl->delete_tag(le_device_db_tlv_btstack_tlv_context, tag);
return true;
}{ ... }
static void le_device_db_tlv_scan(void){
int i;
num_valid_entries = 0;
memset(entry_map, 0, sizeof(entry_map));
for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
le_device_db_entry_t entry;
if (!le_device_db_tlv_fetch(i, &entry)) continue;
entry_map[i] = 1;
num_valid_entries++;
}for (i=0;i
log_info("num valid le device entries %u", (unsigned int) num_valid_entries);
}{ ... }
void le_device_db_init(void){
if (!le_device_db_tlv_btstack_tlv_impl) {
log_error("btstack_tlv not initialized");
}if (!le_device_db_tlv_btstack_tlv_impl) { ... }
}{ ... }
void le_device_db_set_local_bd_addr(bd_addr_t bd_addr){
(void)bd_addr;
}{ ... }
int le_device_db_count(void){
return num_valid_entries;
}{ ... }
int le_device_db_max_count(void){
return NVM_NUM_DEVICE_DB_ENTRIES;
}{ ... }
void le_device_db_remove(int index){
btstack_assert(index >= 0);
btstack_assert(index < le_device_db_max_count());
if (entry_map[index] == 0u) return;
le_device_db_tlv_delete(index);
entry_map[index] = 0;
num_valid_entries--;
}{ ... }
int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
uint32_t highest_seq_nr = 0;
uint32_t lowest_seq_nr = 0xFFFFFFFFU;
int index_for_lowest_seq_nr = -1;
int index_for_addr = -1;
int index_for_empty = -1;
bool new_entry = false;
int i;
for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
if (entry_map[i] != 0u) {
le_device_db_entry_t entry;
le_device_db_tlv_fetch(i, &entry);
if ((memcmp(addr, entry.addr, 6) == 0) && (addr_type == entry.addr_type)){
index_for_addr = i;
}if ((memcmp(addr, entry.addr, 6) == 0) && (addr_type == entry.addr_type)) { ... }
if (entry.seq_nr > highest_seq_nr){
highest_seq_nr = entry.seq_nr;
}if (entry.seq_nr > highest_seq_nr) { ... }
if ((index_for_lowest_seq_nr == -1) || (entry.seq_nr < lowest_seq_nr)){
index_for_lowest_seq_nr = i;
lowest_seq_nr = entry.seq_nr;
}if ((index_for_lowest_seq_nr == -1) || (entry.seq_nr < lowest_seq_nr)) { ... }
}if (entry_map[i] != 0u) { ... } else {
index_for_empty = i;
}else { ... }
}for (i=0;i
log_info("index_for_addr %x, index_for_empy %x, index_for_lowest_seq_nr %x", index_for_addr, index_for_empty, index_for_lowest_seq_nr);
uint32_t index_to_use = 0;
if (index_for_addr >= 0){
index_to_use = index_for_addr;
}if (index_for_addr >= 0) { ... } else if (index_for_empty >= 0){
new_entry = true;
index_to_use = index_for_empty;
}else if (index_for_empty >= 0) { ... } else if (index_for_lowest_seq_nr >= 0){
index_to_use = index_for_lowest_seq_nr;
}else if (index_for_lowest_seq_nr >= 0) { ... } else {
return -1;
}else { ... }
log_info("new entry for index %u", (unsigned int) index_to_use);
le_device_db_entry_t entry;
log_info("LE Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr));
log_info_key("irk", irk);
memset(&entry, 0, sizeof(le_device_db_entry_t));
entry.addr_type = addr_type;
(void)memcpy(entry.addr, addr, 6);
(void)memcpy(entry.irk, irk, 16);
entry.seq_nr = highest_seq_nr + 1u;
#ifdef ENABLE_LE_SIGNED_WRITE
entry.remote_counter = 0;
#endif
bool ok = le_device_db_tlv_store(index_to_use, &entry);
if (!ok){
log_error("tag store failed");
return -1;
}if (!ok) { ... }
entry_map[index_to_use] = 1;
if (new_entry){
num_valid_entries++;
}if (new_entry) { ... }
return index_to_use;
}{ ... }
void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) {
memset(&entry, 0, sizeof(le_device_db_entry_t));
entry.addr_type = BD_ADDR_TYPE_UNKNOWN;
}if (!ok) { ... }
if (addr_type != NULL) *addr_type = entry.addr_type;
if (addr != NULL) (void)memcpy(addr, entry.addr, 6);
if (irk != NULL) (void)memcpy(irk, entry.irk, 16);
}{ ... }
void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk, int key_size, int authenticated, int authorized, int secure_connection){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
log_info("LE Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u, secure connection %u",
index, ediv, key_size, authenticated, authorized, secure_connection);
entry.ediv = ediv;
if (rand != NULL){
(void)memcpy(entry.rand, rand, 8);
}if (rand != NULL) { ... }
if (ltk != 0) {
(void)memcpy(entry.ltk, ltk, 16);
}if (ltk != 0) { ... }
entry.key_size = key_size;
entry.authenticated = authenticated;
entry.authorized = authorized;
entry.secure_connection = secure_connection;
ok = le_device_db_tlv_store(index, &entry);
if (!ok){
log_error("Set encryption data failed");
}if (!ok) { ... }
}{ ... }
void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk, int * key_size, int * authenticated, int * authorized, int * secure_connection){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
log_info("LE Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u, secure connection %u",
index, entry.ediv, entry.key_size, entry.authenticated, entry.authorized, entry.secure_connection);
if (ediv != NULL) *ediv = entry.ediv;
if (rand != NULL) (void)memcpy(rand, entry.rand, 8);
if (ltk != NULL) (void)memcpy(ltk, entry.ltk, 16);
if (key_size != NULL) *key_size = entry.key_size;
if (authenticated != NULL) *authenticated = entry.authenticated;
if (authorized != NULL) *authorized = entry.authorized;
if (secure_connection != NULL) *secure_connection = entry.secure_connection;
}{ ... }
#ifdef ENABLE_LE_SIGNED_WRITE
void le_device_db_remote_csrk_get(int index, sm_key_t csrk){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
if (csrk) (void)memcpy(csrk, entry.remote_csrk, 16);
}le_device_db_remote_csrk_get (int index, sm_key_t csrk) { ... }
void le_device_db_remote_csrk_set(int index, sm_key_t csrk){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
if (!csrk) return;
(void)memcpy(entry.remote_csrk, csrk, 16);
le_device_db_tlv_store(index, &entry);
}le_device_db_remote_csrk_set (int index, sm_key_t csrk) { ... }
void le_device_db_local_csrk_get(int index, sm_key_t csrk){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
if (!csrk) return;
(void)memcpy(csrk, entry.local_csrk, 16);
}le_device_db_local_csrk_get (int index, sm_key_t csrk) { ... }
void le_device_db_local_csrk_set(int index, sm_key_t csrk){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
if (!csrk) return;
(void)memcpy(entry.local_csrk, csrk, 16);
le_device_db_tlv_store(index, &entry);
}le_device_db_local_csrk_set (int index, sm_key_t csrk) { ... }
uint32_t le_device_db_remote_counter_get(int index){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return 0;
return entry.remote_counter;
}le_device_db_remote_counter_get (int index) { ... }
void le_device_db_remote_counter_set(int index, uint32_t counter){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
entry.remote_counter = counter;
le_device_db_tlv_store(index, &entry);
}le_device_db_remote_counter_set (int index, uint32_t counter) { ... }
uint32_t le_device_db_local_counter_get(int index){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return 0;
return entry.local_counter;
}le_device_db_local_counter_get (int index) { ... }
void le_device_db_local_counter_set(int index, uint32_t counter){
le_device_db_entry_t entry;
int ok = le_device_db_tlv_fetch(index, &entry);
if (!ok) return;
entry.local_counter = counter;
le_device_db_tlv_store(index, &entry);
}le_device_db_local_counter_set (int index, uint32_t counter) { ... }
/* ... */
#endif
void le_device_db_dump(void){
log_info("LE Device DB dump, devices: %d", le_device_db_count());
uint32_t i;
for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
if (!entry_map[i]) continue;
le_device_db_entry_t entry;
le_device_db_tlv_fetch(i, &entry);
log_info("%u: %u %s", (unsigned int) i, entry.addr_type, bd_addr_to_str(entry.addr));
log_info_key("ltk", entry.ltk);
log_info_key("irk", entry.irk);
#ifdef ENABLE_LE_SIGNED_WRITE
log_info_key("local csrk", entry.local_csrk);
log_info_key("remote csrk", entry.remote_csrk);/* ... */
#endif
}for (i=0;i
}{ ... }
void le_device_db_tlv_configure(const btstack_tlv_t * btstack_tlv_impl, void * btstack_tlv_context){
le_device_db_tlv_btstack_tlv_impl = btstack_tlv_impl;
le_device_db_tlv_btstack_tlv_context = btstack_tlv_context;
le_device_db_tlv_scan();
}{ ... }