xref: /btstack/src/ble/le_device_db_tlv.c (revision e309e9332a54304ca01b5ff25faafe10c7b3d13c)
11f5ff433SMatthias Ringwald /*
21f5ff433SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
31f5ff433SMatthias Ringwald  *
41f5ff433SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
51f5ff433SMatthias Ringwald  * modification, are permitted provided that the following conditions
61f5ff433SMatthias Ringwald  * are met:
71f5ff433SMatthias Ringwald  *
81f5ff433SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
91f5ff433SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
101f5ff433SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111f5ff433SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
121f5ff433SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
131f5ff433SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
141f5ff433SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
151f5ff433SMatthias Ringwald  *    from this software without specific prior written permission.
161f5ff433SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
171f5ff433SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
181f5ff433SMatthias Ringwald  *    monetary gain.
191f5ff433SMatthias Ringwald  *
201f5ff433SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211f5ff433SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221f5ff433SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251f5ff433SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261f5ff433SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271f5ff433SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281f5ff433SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291f5ff433SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301f5ff433SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311f5ff433SMatthias Ringwald  * SUCH DAMAGE.
321f5ff433SMatthias Ringwald  *
331f5ff433SMatthias Ringwald  * Please inquire about commercial licensing options at
341f5ff433SMatthias Ringwald  * [email protected]
351f5ff433SMatthias Ringwald  *
361f5ff433SMatthias Ringwald  */
371f5ff433SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "le_device_db_tlv.c"
391f5ff433SMatthias Ringwald 
401f5ff433SMatthias Ringwald #include "ble/le_device_db.h"
411f5ff433SMatthias Ringwald #include "ble/le_device_db_tlv.h"
421f5ff433SMatthias Ringwald 
431f5ff433SMatthias Ringwald #include "ble/core.h"
441f5ff433SMatthias Ringwald 
451f5ff433SMatthias Ringwald #include <string.h>
461f5ff433SMatthias Ringwald #include "btstack_debug.h"
471f5ff433SMatthias Ringwald 
481f5ff433SMatthias Ringwald // LE Device DB Implementation storing entries in btstack_tlv
491f5ff433SMatthias Ringwald 
501f5ff433SMatthias Ringwald // Local cache is used to keep track of deleted entries in TLV
511f5ff433SMatthias Ringwald 
521f5ff433SMatthias Ringwald #define INVALID_ENTRY_ADDR_TYPE 0xff
531f5ff433SMatthias Ringwald 
541f5ff433SMatthias Ringwald // Single stored entry
551f5ff433SMatthias Ringwald typedef struct le_device_db_entry_t {
561f5ff433SMatthias Ringwald 
57d18177feSMatthias Ringwald     uint32_t seq_nr;    // used for "least recently stored" eviction strategy
58d18177feSMatthias Ringwald 
591f5ff433SMatthias Ringwald     // Identification
601f5ff433SMatthias Ringwald     int addr_type;
611f5ff433SMatthias Ringwald     bd_addr_t addr;
621f5ff433SMatthias Ringwald     sm_key_t irk;
631f5ff433SMatthias Ringwald 
641f5ff433SMatthias Ringwald     // Stored pairing information allows to re-establish an enncrypted connection
651f5ff433SMatthias Ringwald     // with a peripheral that doesn't have any persistent memory
661f5ff433SMatthias Ringwald     sm_key_t ltk;
671f5ff433SMatthias Ringwald     uint16_t ediv;
681f5ff433SMatthias Ringwald     uint8_t  rand[8];
693dc3a67dSMatthias Ringwald 
701f5ff433SMatthias Ringwald     uint8_t  key_size;
711f5ff433SMatthias Ringwald     uint8_t  authenticated;
721f5ff433SMatthias Ringwald     uint8_t  authorized;
733dc3a67dSMatthias Ringwald     uint8_t  secure_connection;
741f5ff433SMatthias Ringwald 
751f5ff433SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
761f5ff433SMatthias Ringwald     // Signed Writes by remote
771f5ff433SMatthias Ringwald     sm_key_t remote_csrk;
781f5ff433SMatthias Ringwald     uint32_t remote_counter;
791f5ff433SMatthias Ringwald 
801f5ff433SMatthias Ringwald     // Signed Writes by us
811f5ff433SMatthias Ringwald     sm_key_t local_csrk;
821f5ff433SMatthias Ringwald     uint32_t local_counter;
831f5ff433SMatthias Ringwald #endif
841f5ff433SMatthias Ringwald 
851f5ff433SMatthias Ringwald } le_device_db_entry_t;
861f5ff433SMatthias Ringwald 
871f5ff433SMatthias Ringwald 
88d2d1c6bbSMatthias Ringwald #ifndef NVM_NUM_DEVICE_DB_ENTRIES
89d2d1c6bbSMatthias Ringwald #error "NVM_NUM_DEVICE_DB_ENTRIES not defined, please define in btstack_config.h"
901f5ff433SMatthias Ringwald #endif
911f5ff433SMatthias Ringwald 
92d2d1c6bbSMatthias Ringwald #if NVM_NUM_DEVICE_DB_ENTRIES == 0
93d2d1c6bbSMatthias Ringwald #error "NVM_NUM_DEVICE_DB_ENTRIES must not be 0, please update in btstack_config.h"
941f5ff433SMatthias Ringwald #endif
951f5ff433SMatthias Ringwald 
9642133918SMatthias Ringwald // only stores if entry present
97d2d1c6bbSMatthias Ringwald static uint8_t  entry_map[NVM_NUM_DEVICE_DB_ENTRIES];
981f5ff433SMatthias Ringwald static uint32_t num_valid_entries;
991f5ff433SMatthias Ringwald 
1001f5ff433SMatthias Ringwald static const btstack_tlv_t * le_device_db_tlv_btstack_tlv_impl;
1011f5ff433SMatthias Ringwald static       void *          le_device_db_tlv_btstack_tlv_context;
1021f5ff433SMatthias Ringwald 
1038334d3d8SMatthias Ringwald 
le_device_db_tlv_tag_for_index(uint8_t index)1048334d3d8SMatthias Ringwald static uint32_t le_device_db_tlv_tag_for_index(uint8_t index){
1051f5ff433SMatthias Ringwald     static const char tag_0 = 'B';
1061f5ff433SMatthias Ringwald     static const char tag_1 = 'T';
1071f5ff433SMatthias Ringwald     static const char tag_2 = 'D';
1081f5ff433SMatthias Ringwald 
1094ea43905SMatthias Ringwald     return (tag_0 << 24u) | (tag_1 << 16u) | (tag_2 << 8u) | index;
1101f5ff433SMatthias Ringwald }
1111f5ff433SMatthias Ringwald 
1126b65794dSMilanka Ringwald // @return success
1131f5ff433SMatthias Ringwald // @param index = entry_pos
le_device_db_tlv_fetch(int index,le_device_db_entry_t * entry)1140fe46bc1SMatthias Ringwald static bool le_device_db_tlv_fetch(int index, le_device_db_entry_t * entry){
115674b0e20SMatthias Ringwald     btstack_assert(le_device_db_tlv_btstack_tlv_impl != NULL);
116674b0e20SMatthias Ringwald     btstack_assert(index >= 0);
117674b0e20SMatthias Ringwald     btstack_assert(index < NVM_NUM_DEVICE_DB_ENTRIES);
118674b0e20SMatthias Ringwald 
1191f5ff433SMatthias Ringwald     uint32_t tag = le_device_db_tlv_tag_for_index(index);
1201f5ff433SMatthias Ringwald     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));
121a82b242fSMatthias Ringwald 	return size == sizeof(le_device_db_entry_t);
1221f5ff433SMatthias Ringwald }
1231f5ff433SMatthias Ringwald 
1246b65794dSMilanka Ringwald // @return success
1251f5ff433SMatthias Ringwald // @param index = entry_pos
le_device_db_tlv_store(int index,le_device_db_entry_t * entry)1260fe46bc1SMatthias Ringwald static bool le_device_db_tlv_store(int index, le_device_db_entry_t * entry){
127674b0e20SMatthias Ringwald     btstack_assert(le_device_db_tlv_btstack_tlv_impl != NULL);
128674b0e20SMatthias Ringwald     btstack_assert(index >= 0);
129674b0e20SMatthias Ringwald     btstack_assert(index < NVM_NUM_DEVICE_DB_ENTRIES);
130674b0e20SMatthias Ringwald 
1311f5ff433SMatthias Ringwald     uint32_t tag = le_device_db_tlv_tag_for_index(index);
1320fe46bc1SMatthias Ringwald     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));
1330fe46bc1SMatthias Ringwald     return result == 0;
1341f5ff433SMatthias Ringwald }
1351f5ff433SMatthias Ringwald 
1361f5ff433SMatthias Ringwald // @param index = entry_pos
le_device_db_tlv_delete(int index)1370fe46bc1SMatthias Ringwald static bool le_device_db_tlv_delete(int index){
138674b0e20SMatthias Ringwald     btstack_assert(le_device_db_tlv_btstack_tlv_impl != NULL);
139674b0e20SMatthias Ringwald     btstack_assert(index >= 0);
140674b0e20SMatthias Ringwald     btstack_assert(index < NVM_NUM_DEVICE_DB_ENTRIES);
141674b0e20SMatthias Ringwald 
1421f5ff433SMatthias Ringwald     uint32_t tag = le_device_db_tlv_tag_for_index(index);
1431f5ff433SMatthias Ringwald     le_device_db_tlv_btstack_tlv_impl->delete_tag(le_device_db_tlv_btstack_tlv_context, tag);
1440fe46bc1SMatthias Ringwald 	return true;
1451f5ff433SMatthias Ringwald }
1461f5ff433SMatthias Ringwald 
le_device_db_tlv_scan(void)147751da889SMatthias Ringwald static void le_device_db_tlv_scan(void){
1481f5ff433SMatthias Ringwald     int i;
1491f5ff433SMatthias Ringwald     num_valid_entries = 0;
1501f5ff433SMatthias Ringwald     memset(entry_map, 0, sizeof(entry_map));
151d2d1c6bbSMatthias Ringwald     for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
1521f5ff433SMatthias Ringwald         // lookup entry
1531f5ff433SMatthias Ringwald         le_device_db_entry_t entry;
15442133918SMatthias Ringwald         if (!le_device_db_tlv_fetch(i, &entry)) continue;
15542133918SMatthias Ringwald 
15642133918SMatthias Ringwald         entry_map[i] = 1;
15742133918SMatthias Ringwald         num_valid_entries++;
1581f5ff433SMatthias Ringwald     }
15997d2cfbcSMatthias Ringwald     log_info("num valid le device entries %u", (unsigned int) num_valid_entries);
1601f5ff433SMatthias Ringwald }
1611f5ff433SMatthias Ringwald 
le_device_db_init(void)162751da889SMatthias Ringwald void le_device_db_init(void){
163dba24cedSMatthias Ringwald     if (!le_device_db_tlv_btstack_tlv_impl) {
164dba24cedSMatthias Ringwald         log_error("btstack_tlv not initialized");
165dba24cedSMatthias Ringwald     }
166751da889SMatthias Ringwald }
167751da889SMatthias Ringwald 
1681f5ff433SMatthias Ringwald // not used
le_device_db_set_local_bd_addr(bd_addr_t bd_addr)1691f5ff433SMatthias Ringwald void le_device_db_set_local_bd_addr(bd_addr_t bd_addr){
1701f5ff433SMatthias Ringwald     (void)bd_addr;
1711f5ff433SMatthias Ringwald }
1721f5ff433SMatthias Ringwald 
1736b65794dSMilanka Ringwald // @return number of device in db
le_device_db_count(void)1741f5ff433SMatthias Ringwald int le_device_db_count(void){
1751f5ff433SMatthias Ringwald 	return num_valid_entries;
1761f5ff433SMatthias Ringwald }
1771f5ff433SMatthias Ringwald 
le_device_db_max_count(void)1786fc9dda1SMatthias Ringwald int le_device_db_max_count(void){
1796fc9dda1SMatthias Ringwald     return NVM_NUM_DEVICE_DB_ENTRIES;
1806fc9dda1SMatthias Ringwald }
1816fc9dda1SMatthias Ringwald 
le_device_db_remove(int index)1821f5ff433SMatthias Ringwald void le_device_db_remove(int index){
183db5aaf2aSMilanka Ringwald     btstack_assert(index >= 0);
184db5aaf2aSMilanka Ringwald     btstack_assert(index < le_device_db_max_count());
185db5aaf2aSMilanka Ringwald 
18675130320SMatthias Ringwald     // check if entry exists
1874ea43905SMatthias Ringwald     if (entry_map[index] == 0u) return;
18875130320SMatthias Ringwald 
1891f5ff433SMatthias Ringwald 	// delete entry in TLV
19042133918SMatthias Ringwald 	le_device_db_tlv_delete(index);
1911f5ff433SMatthias Ringwald 
19242133918SMatthias Ringwald 	// mark as unused
19342133918SMatthias Ringwald     entry_map[index] = 0;
1941f5ff433SMatthias Ringwald 
1951f5ff433SMatthias Ringwald     // keep track
1961f5ff433SMatthias Ringwald     num_valid_entries--;
1971f5ff433SMatthias Ringwald }
1981f5ff433SMatthias Ringwald 
le_device_db_add(int addr_type,bd_addr_t addr,sm_key_t irk)1991f5ff433SMatthias Ringwald int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk){
200d18177feSMatthias Ringwald 
201d18177feSMatthias Ringwald     uint32_t highest_seq_nr = 0;
2025ce1359eSMatthias Ringwald     uint32_t lowest_seq_nr  = 0xFFFFFFFFU;
203d18177feSMatthias Ringwald     int index_for_lowest_seq_nr = -1;
204d18177feSMatthias Ringwald     int index_for_addr  = -1;
205d18177feSMatthias Ringwald     int index_for_empty = -1;
20699f3ea79SMatthias Ringwald     bool new_entry = false;
207d18177feSMatthias Ringwald 
2081f5ff433SMatthias Ringwald 	// find unused entry in the used list
2091f5ff433SMatthias Ringwald     int i;
21042133918SMatthias Ringwald     for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
211f688bdb8SMatthias Ringwald          if (entry_map[i] != 0u) {
212d18177feSMatthias Ringwald             le_device_db_entry_t entry;
213d18177feSMatthias Ringwald             le_device_db_tlv_fetch(i, &entry);
214d18177feSMatthias Ringwald             // found addr?
215c1ab6cc1SMatthias Ringwald             if ((memcmp(addr, entry.addr, 6) == 0) && (addr_type == entry.addr_type)){
216d18177feSMatthias Ringwald                 index_for_addr = i;
217d18177feSMatthias Ringwald             }
218d18177feSMatthias Ringwald             // update highest seq nr
219d18177feSMatthias Ringwald             if (entry.seq_nr > highest_seq_nr){
220d18177feSMatthias Ringwald                 highest_seq_nr = entry.seq_nr;
221d18177feSMatthias Ringwald             }
222d18177feSMatthias Ringwald             // find entry with lowest seq nr
2239650eb2fSMatthias Ringwald             if ((index_for_lowest_seq_nr == -1) || (entry.seq_nr < lowest_seq_nr)){
224d18177feSMatthias Ringwald                 index_for_lowest_seq_nr = i;
225d18177feSMatthias Ringwald                 lowest_seq_nr = entry.seq_nr;
226d18177feSMatthias Ringwald             }
227d18177feSMatthias Ringwald         } else {
228d18177feSMatthias Ringwald             index_for_empty = i;
229d18177feSMatthias Ringwald         }
2301f5ff433SMatthias Ringwald     }
2311f5ff433SMatthias Ringwald 
232d18177feSMatthias Ringwald     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);
2331f5ff433SMatthias Ringwald 
234d18177feSMatthias Ringwald     uint32_t index_to_use = 0;
235d18177feSMatthias Ringwald     if (index_for_addr >= 0){
236d18177feSMatthias Ringwald         index_to_use = index_for_addr;
237d18177feSMatthias Ringwald     } else if (index_for_empty >= 0){
23899f3ea79SMatthias Ringwald         new_entry = true;
239d18177feSMatthias Ringwald         index_to_use = index_for_empty;
240d18177feSMatthias Ringwald     } else if (index_for_lowest_seq_nr >= 0){
241d18177feSMatthias Ringwald         index_to_use = index_for_lowest_seq_nr;
242d18177feSMatthias Ringwald     } else {
243d18177feSMatthias Ringwald         // should not happen
244d18177feSMatthias Ringwald         return -1;
245d18177feSMatthias Ringwald     }
246d18177feSMatthias Ringwald 
24797d2cfbcSMatthias Ringwald     log_info("new entry for index %u", (unsigned int) index_to_use);
2481f5ff433SMatthias Ringwald 
24942133918SMatthias Ringwald     // store entry at index
2501f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
2511f5ff433SMatthias Ringwald     log_info("LE Device DB adding type %u - %s", addr_type, bd_addr_to_str(addr));
2521f5ff433SMatthias Ringwald     log_info_key("irk", irk);
2531f5ff433SMatthias Ringwald 
2546c8ec5fdSMatthias Ringwald     memset(&entry, 0, sizeof(le_device_db_entry_t));
255539b14edSMatthias Ringwald 
2561f5ff433SMatthias Ringwald     entry.addr_type = addr_type;
2576535961aSMatthias Ringwald     (void)memcpy(entry.addr, addr, 6);
2586535961aSMatthias Ringwald     (void)memcpy(entry.irk, irk, 16);
2594ea43905SMatthias Ringwald     entry.seq_nr = highest_seq_nr + 1u;
2601f5ff433SMatthias Ringwald  #ifdef ENABLE_LE_SIGNED_WRITE
2611f5ff433SMatthias Ringwald     entry.remote_counter = 0;
2621f5ff433SMatthias Ringwald #endif
2631f5ff433SMatthias Ringwald 
2641f5ff433SMatthias Ringwald     // store
2650fe46bc1SMatthias Ringwald     bool ok = le_device_db_tlv_store(index_to_use, &entry);
2660fe46bc1SMatthias Ringwald     if (!ok){
2670fe46bc1SMatthias Ringwald         log_error("tag store failed");
2680fe46bc1SMatthias Ringwald         return -1;
2690fe46bc1SMatthias Ringwald     }
27042133918SMatthias Ringwald     // set in entry_mape
271d18177feSMatthias Ringwald     entry_map[index_to_use] = 1;
27242133918SMatthias Ringwald 
27399f3ea79SMatthias Ringwald     // keep track - don't increase if old entry found or replaced
27499f3ea79SMatthias Ringwald     if (new_entry){
2751f5ff433SMatthias Ringwald         num_valid_entries++;
276d18177feSMatthias Ringwald     }
2771f5ff433SMatthias Ringwald 
278d18177feSMatthias Ringwald     return index_to_use;
2791f5ff433SMatthias Ringwald }
2801f5ff433SMatthias Ringwald 
2811f5ff433SMatthias Ringwald 
2821f5ff433SMatthias Ringwald // get device information: addr type and address
le_device_db_info(int index,int * addr_type,bd_addr_t addr,sm_key_t irk)2831f5ff433SMatthias Ringwald void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk){
2841f5ff433SMatthias Ringwald 
2851df78158SMatthias Ringwald 	// fetch entry
2861f5ff433SMatthias Ringwald     le_device_db_entry_t entry;
2871df78158SMatthias Ringwald     int ok = le_device_db_tlv_fetch(index, &entry);
2881df78158SMatthias Ringwald 
2891df78158SMatthias Ringwald     // set defaults if not found
2901df78158SMatthias Ringwald     if (!ok) {
29146d9fae3SMatthias Ringwald         memset(&entry, 0, sizeof(le_device_db_entry_t));
29246d9fae3SMatthias Ringwald         entry.addr_type = BD_ADDR_TYPE_UNKNOWN;
2931df78158SMatthias Ringwald     }
29446d9fae3SMatthias Ringwald 
2951df78158SMatthias Ringwald     // setup return values
2969305033eSMatthias Ringwald     if (addr_type != NULL) *addr_type = entry.addr_type;
2979305033eSMatthias Ringwald     if (addr != NULL) (void)memcpy(addr, entry.addr, 6);
2989305033eSMatthias Ringwald     if (irk != NULL) (void)memcpy(irk, entry.irk, 16);
2991f5ff433SMatthias Ringwald }
3001f5ff433SMatthias Ringwald 
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)3013dc3a67dSMatthias Ringwald 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){
3021f5ff433SMatthias Ringwald 
3031f5ff433SMatthias Ringwald 	// fetch entry
3041f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
30542133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
3061f5ff433SMatthias Ringwald 	if (!ok) return;
3071f5ff433SMatthias Ringwald 
3081f5ff433SMatthias Ringwald 	// update
3093dc3a67dSMatthias Ringwald     log_info("LE Device DB set encryption for %u, ediv x%04x, key size %u, authenticated %u, authorized %u, secure connection %u",
3103dc3a67dSMatthias Ringwald         index, ediv, key_size, authenticated, authorized, secure_connection);
3111f5ff433SMatthias Ringwald     entry.ediv = ediv;
312*e309e933SMatthias Ringwald     if (rand != NULL){
313*e309e933SMatthias Ringwald         (void)memcpy(entry.rand, rand, 8);
314*e309e933SMatthias Ringwald     }
315*e309e933SMatthias Ringwald     if (ltk != 0) {
316*e309e933SMatthias Ringwald         (void)memcpy(entry.ltk, ltk, 16);
317*e309e933SMatthias Ringwald     }
3181f5ff433SMatthias Ringwald     entry.key_size = key_size;
3191f5ff433SMatthias Ringwald     entry.authenticated = authenticated;
3201f5ff433SMatthias Ringwald     entry.authorized = authorized;
3213dc3a67dSMatthias Ringwald     entry.secure_connection = secure_connection;
3221f5ff433SMatthias Ringwald 
3231f5ff433SMatthias Ringwald     // store
3240fe46bc1SMatthias Ringwald     ok = le_device_db_tlv_store(index, &entry);
3250fe46bc1SMatthias Ringwald     if (!ok){
3260fe46bc1SMatthias Ringwald         log_error("Set encryption data failed");
3270fe46bc1SMatthias Ringwald     }
3281f5ff433SMatthias Ringwald }
3291f5ff433SMatthias Ringwald 
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)3303dc3a67dSMatthias Ringwald 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){
3311f5ff433SMatthias Ringwald 
3321f5ff433SMatthias Ringwald 	// fetch entry
3331f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
33442133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
3351f5ff433SMatthias Ringwald 	if (!ok) return;
3361f5ff433SMatthias Ringwald 
3371f5ff433SMatthias Ringwald 	// update user fields
3383dc3a67dSMatthias Ringwald     log_info("LE Device DB encryption for %u, ediv x%04x, keysize %u, authenticated %u, authorized %u, secure connection %u",
3393dc3a67dSMatthias Ringwald         index, entry.ediv, entry.key_size, entry.authenticated, entry.authorized, entry.secure_connection);
3409305033eSMatthias Ringwald     if (ediv != NULL) *ediv = entry.ediv;
3419305033eSMatthias Ringwald     if (rand != NULL) (void)memcpy(rand, entry.rand, 8);
3429305033eSMatthias Ringwald     if (ltk != NULL)  (void)memcpy(ltk, entry.ltk, 16);
3439305033eSMatthias Ringwald     if (key_size != NULL) *key_size = entry.key_size;
3449305033eSMatthias Ringwald     if (authenticated != NULL) *authenticated = entry.authenticated;
3459305033eSMatthias Ringwald     if (authorized != NULL) *authorized = entry.authorized;
3469305033eSMatthias Ringwald     if (secure_connection != NULL) *secure_connection = entry.secure_connection;
3471f5ff433SMatthias Ringwald }
3481f5ff433SMatthias Ringwald 
3491f5ff433SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3501f5ff433SMatthias Ringwald 
3511f5ff433SMatthias Ringwald // get signature key
le_device_db_remote_csrk_get(int index,sm_key_t csrk)3521f5ff433SMatthias Ringwald void le_device_db_remote_csrk_get(int index, sm_key_t csrk){
3531f5ff433SMatthias Ringwald 
3541f5ff433SMatthias Ringwald 	// fetch entry
3551f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
35642133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
3571f5ff433SMatthias Ringwald 	if (!ok) return;
3581f5ff433SMatthias Ringwald 
3596535961aSMatthias Ringwald     if (csrk) (void)memcpy(csrk, entry.remote_csrk, 16);
3601f5ff433SMatthias Ringwald }
3611f5ff433SMatthias Ringwald 
le_device_db_remote_csrk_set(int index,sm_key_t csrk)3621f5ff433SMatthias Ringwald void le_device_db_remote_csrk_set(int index, sm_key_t csrk){
3631f5ff433SMatthias Ringwald 
3641f5ff433SMatthias Ringwald 	// fetch entry
3651f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
36642133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
3671f5ff433SMatthias Ringwald 	if (!ok) return;
3681f5ff433SMatthias Ringwald 
3691f5ff433SMatthias Ringwald     if (!csrk) return;
3701f5ff433SMatthias Ringwald 
3711f5ff433SMatthias Ringwald     // update
3726535961aSMatthias Ringwald     (void)memcpy(entry.remote_csrk, csrk, 16);
3731f5ff433SMatthias Ringwald 
3741f5ff433SMatthias Ringwald     // store
37542133918SMatthias Ringwald     le_device_db_tlv_store(index, &entry);
3761f5ff433SMatthias Ringwald }
3771f5ff433SMatthias Ringwald 
le_device_db_local_csrk_get(int index,sm_key_t csrk)3781f5ff433SMatthias Ringwald void le_device_db_local_csrk_get(int index, sm_key_t csrk){
3791f5ff433SMatthias Ringwald 
3801f5ff433SMatthias Ringwald 	// fetch entry
3811f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
38242133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
3831f5ff433SMatthias Ringwald 	if (!ok) return;
3841f5ff433SMatthias Ringwald 
3851f5ff433SMatthias Ringwald     if (!csrk) return;
3861f5ff433SMatthias Ringwald 
3871f5ff433SMatthias Ringwald     // fill
3886535961aSMatthias Ringwald     (void)memcpy(csrk, entry.local_csrk, 16);
3891f5ff433SMatthias Ringwald }
3901f5ff433SMatthias Ringwald 
le_device_db_local_csrk_set(int index,sm_key_t csrk)3911f5ff433SMatthias Ringwald void le_device_db_local_csrk_set(int index, sm_key_t csrk){
3921f5ff433SMatthias Ringwald 
3931f5ff433SMatthias Ringwald 	// fetch entry
3941f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
39542133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
3961f5ff433SMatthias Ringwald 	if (!ok) return;
3971f5ff433SMatthias Ringwald 
3981f5ff433SMatthias Ringwald     if (!csrk) return;
3991f5ff433SMatthias Ringwald 
4001f5ff433SMatthias Ringwald     // update
4016535961aSMatthias Ringwald     (void)memcpy(entry.local_csrk, csrk, 16);
4021f5ff433SMatthias Ringwald 
4031f5ff433SMatthias Ringwald     // store
40442133918SMatthias Ringwald     le_device_db_tlv_store(index, &entry);
4051f5ff433SMatthias Ringwald }
4061f5ff433SMatthias Ringwald 
4071f5ff433SMatthias Ringwald // query last used/seen signing counter
le_device_db_remote_counter_get(int index)4081f5ff433SMatthias Ringwald uint32_t le_device_db_remote_counter_get(int index){
4091f5ff433SMatthias Ringwald 
4101f5ff433SMatthias Ringwald 	// fetch entry
4111f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
41242133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
4131f5ff433SMatthias Ringwald 	if (!ok) return 0;
4141f5ff433SMatthias Ringwald 
4151f5ff433SMatthias Ringwald     return entry.remote_counter;
4161f5ff433SMatthias Ringwald }
4171f5ff433SMatthias Ringwald 
4181f5ff433SMatthias Ringwald // update signing counter
le_device_db_remote_counter_set(int index,uint32_t counter)4191f5ff433SMatthias Ringwald void le_device_db_remote_counter_set(int index, uint32_t counter){
4201f5ff433SMatthias Ringwald 
4211f5ff433SMatthias Ringwald 	// fetch entry
4221f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
42342133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
4241f5ff433SMatthias Ringwald 	if (!ok) return;
4251f5ff433SMatthias Ringwald 
4261f5ff433SMatthias Ringwald     entry.remote_counter = counter;
4271f5ff433SMatthias Ringwald 
4281f5ff433SMatthias Ringwald     // store
42942133918SMatthias Ringwald     le_device_db_tlv_store(index, &entry);
4301f5ff433SMatthias Ringwald }
4311f5ff433SMatthias Ringwald 
4321f5ff433SMatthias Ringwald // query last used/seen signing counter
le_device_db_local_counter_get(int index)4331f5ff433SMatthias Ringwald uint32_t le_device_db_local_counter_get(int index){
4341f5ff433SMatthias Ringwald 
4351f5ff433SMatthias Ringwald 	// fetch entry
4361f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
43742133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
4381f5ff433SMatthias Ringwald 	if (!ok) return 0;
4391f5ff433SMatthias Ringwald 
4401f5ff433SMatthias Ringwald     return entry.local_counter;
4411f5ff433SMatthias Ringwald }
4421f5ff433SMatthias Ringwald 
4431f5ff433SMatthias Ringwald // update signing counter
le_device_db_local_counter_set(int index,uint32_t counter)4441f5ff433SMatthias Ringwald void le_device_db_local_counter_set(int index, uint32_t counter){
4451f5ff433SMatthias Ringwald 
4461f5ff433SMatthias Ringwald 	// fetch entry
4471f5ff433SMatthias Ringwald 	le_device_db_entry_t entry;
44842133918SMatthias Ringwald 	int ok = le_device_db_tlv_fetch(index, &entry);
4491f5ff433SMatthias Ringwald 	if (!ok) return;
4501f5ff433SMatthias Ringwald 
4511f5ff433SMatthias Ringwald 	// update
4521f5ff433SMatthias Ringwald     entry.local_counter = counter;
4531f5ff433SMatthias Ringwald 
4541f5ff433SMatthias Ringwald     // store
45542133918SMatthias Ringwald     le_device_db_tlv_store(index, &entry);
4561f5ff433SMatthias Ringwald }
4571f5ff433SMatthias Ringwald 
4581f5ff433SMatthias Ringwald #endif
4591f5ff433SMatthias Ringwald 
le_device_db_dump(void)4601f5ff433SMatthias Ringwald void le_device_db_dump(void){
4611f5ff433SMatthias Ringwald     log_info("LE Device DB dump, devices: %d", le_device_db_count());
462f4780d30SMatthias Ringwald     uint32_t i;
463751da889SMatthias Ringwald 
464751da889SMatthias Ringwald     for (i=0;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
465751da889SMatthias Ringwald         if (!entry_map[i]) continue;
4661f5ff433SMatthias Ringwald 		// fetch entry
4671f5ff433SMatthias Ringwald 		le_device_db_entry_t entry;
46842133918SMatthias Ringwald 		le_device_db_tlv_fetch(i, &entry);
46997d2cfbcSMatthias Ringwald         log_info("%u: %u %s", (unsigned int) i, entry.addr_type, bd_addr_to_str(entry.addr));
470e4c7c6faSMatthias Ringwald         log_info_key("ltk", entry.ltk);
4711f5ff433SMatthias Ringwald         log_info_key("irk", entry.irk);
4721f5ff433SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4731f5ff433SMatthias Ringwald         log_info_key("local csrk", entry.local_csrk);
4741f5ff433SMatthias Ringwald         log_info_key("remote csrk", entry.remote_csrk);
4751f5ff433SMatthias Ringwald #endif
4761f5ff433SMatthias Ringwald     }
4771f5ff433SMatthias Ringwald }
4781f5ff433SMatthias Ringwald 
le_device_db_tlv_configure(const btstack_tlv_t * btstack_tlv_impl,void * btstack_tlv_context)4791f5ff433SMatthias Ringwald void le_device_db_tlv_configure(const btstack_tlv_t * btstack_tlv_impl, void * btstack_tlv_context){
4801f5ff433SMatthias Ringwald 	le_device_db_tlv_btstack_tlv_impl = btstack_tlv_impl;
4811f5ff433SMatthias Ringwald 	le_device_db_tlv_btstack_tlv_context = btstack_tlv_context;
482751da889SMatthias Ringwald     le_device_db_tlv_scan();
4831f5ff433SMatthias Ringwald }
484