1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "btstack_link_key_db_tlv.c" 39 40 #include <string.h> 41 #include <stdlib.h> 42 43 #include "classic/btstack_link_key_db_tlv.h" 44 45 #include "btstack_debug.h" 46 #include "btstack_util.h" 47 #include "classic/core.h" 48 49 // NVM_NUM_LINK_KEYS defines number of stored link keys 50 #ifndef NVM_NUM_LINK_KEYS 51 #error "Please set NVM_NUM_LINK_KEYS in btstack_config.h - number of link keys that can be stored in TLV" 52 #endif 53 54 typedef struct { 55 const btstack_tlv_t * btstack_tlv_impl; 56 void * btstack_tlv_context; 57 } btstack_link_key_db_tlv_h; 58 59 typedef struct link_key_nvm { 60 uint32_t seq_nr; // used for "least recently stored" eviction strategy 61 bd_addr_t bd_addr; 62 link_key_t link_key; 63 link_key_type_t link_key_type; 64 } link_key_nvm_t; // sizeof(link_key_nvm_t) = 27 bytes 65 66 static btstack_link_key_db_tlv_h singleton; 67 static btstack_link_key_db_tlv_h * self = &singleton; 68 69 static const char tag_0 = 'B'; 70 static const char tag_1 = 'T'; 71 static const char tag_2 = 'L'; 72 73 static uint32_t btstack_link_key_db_tag_for_index(uint8_t index){ 74 return (tag_0 << 24) | (tag_1 << 16) | (tag_2 << 8) | index; 75 } 76 77 // Device info 78 static void btstack_link_key_db_tlv_open(void){ 79 } 80 81 static void btstack_link_key_db_tlv_set_bd_addr(bd_addr_t bd_addr){ 82 (void)bd_addr; 83 } 84 85 static void btstack_link_key_db_tlv_close(void){ 86 } 87 88 static int btstack_link_key_db_tlv_get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) { 89 int i; 90 for (i=0;i<NVM_NUM_LINK_KEYS;i++){ 91 link_key_nvm_t entry; 92 uint32_t tag = btstack_link_key_db_tag_for_index(i); 93 int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry)); 94 if (size == 0) continue; 95 log_info("tag %x, addr %s", (unsigned int) tag, bd_addr_to_str(entry.bd_addr)); 96 if (memcmp(bd_addr, entry.bd_addr, 6)) continue; 97 // found, pass back 98 (void)memcpy(link_key, entry.link_key, 16); 99 *link_key_type = entry.link_key_type; 100 return 1; 101 } 102 return 0; 103 } 104 105 static void btstack_link_key_db_tlv_delete_link_key(bd_addr_t bd_addr){ 106 int i; 107 for (i=0;i<NVM_NUM_LINK_KEYS;i++){ 108 link_key_nvm_t entry; 109 uint32_t tag = btstack_link_key_db_tag_for_index(i); 110 int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry)); 111 if (size == 0) continue; 112 if (memcmp(bd_addr, entry.bd_addr, 6)) continue; 113 // found, delete tag 114 self->btstack_tlv_impl->delete_tag(self->btstack_tlv_context, tag); 115 break; 116 } 117 } 118 119 static void btstack_link_key_db_tlv_put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ 120 int i; 121 uint32_t highest_seq_nr = 0; 122 uint32_t lowest_seq_nr = 0; 123 uint32_t tag_for_lowest_seq_nr = 0; 124 uint32_t tag_for_addr = 0; 125 uint32_t tag_for_empty = 0; 126 127 for (i=0;i<NVM_NUM_LINK_KEYS;i++){ 128 link_key_nvm_t entry; 129 uint32_t tag = btstack_link_key_db_tag_for_index(i); 130 int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry)); 131 // empty/deleted tag 132 if (size == 0) { 133 tag_for_empty = tag; 134 continue; 135 } 136 // found addr? 137 if (memcmp(bd_addr, entry.bd_addr, 6) == 0){ 138 tag_for_addr = tag; 139 } 140 // update highest seq nr 141 if (entry.seq_nr > highest_seq_nr){ 142 highest_seq_nr = entry.seq_nr; 143 } 144 // find entry with lowest seq nr 145 if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){ 146 tag_for_lowest_seq_nr = tag; 147 lowest_seq_nr = entry.seq_nr; 148 } 149 } 150 151 log_info("tag_for_addr %x, tag_for_empy %x, tag_for_lowest_seq_nr %x", 152 (unsigned int) tag_for_addr, (unsigned int) tag_for_empty, (unsigned int) tag_for_lowest_seq_nr); 153 154 uint32_t tag_to_use = 0; 155 if (tag_for_addr){ 156 tag_to_use = tag_for_addr; 157 } else if (tag_for_empty){ 158 tag_to_use = tag_for_empty; 159 } else if (tag_for_lowest_seq_nr){ 160 tag_to_use = tag_for_lowest_seq_nr; 161 } else { 162 // should not happen 163 return; 164 } 165 166 log_info("store with tag %x", (unsigned int) tag_to_use); 167 168 link_key_nvm_t entry; 169 170 (void)memcpy(entry.bd_addr, bd_addr, 6); 171 (void)memcpy(entry.link_key, link_key, 16); 172 entry.link_key_type = link_key_type; 173 entry.seq_nr = highest_seq_nr + 1; 174 175 int result = self->btstack_tlv_impl->store_tag(self->btstack_tlv_context, tag_to_use, (uint8_t*) &entry, sizeof(entry)); 176 if (result != 0){ 177 log_error("store link key failed"); 178 } 179 } 180 181 static int btstack_link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it){ 182 it->context = (void*) 0; 183 return 1; 184 } 185 186 static int btstack_link_key_db_tlv_iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type){ 187 uint8_t i = (uint8_t)(uintptr_t) it->context; 188 int found = 0; 189 while (i<NVM_NUM_LINK_KEYS){ 190 link_key_nvm_t entry; 191 uint32_t tag = btstack_link_key_db_tag_for_index(i++); 192 int size = self->btstack_tlv_impl->get_tag(self->btstack_tlv_context, tag, (uint8_t*) &entry, sizeof(entry)); 193 if (size == 0) continue; 194 (void)memcpy(bd_addr, entry.bd_addr, 6); 195 (void)memcpy(link_key, entry.link_key, 16); 196 *link_key_type = entry.link_key_type; 197 found = 1; 198 break; 199 } 200 it->context = (void*)(uintptr_t) i; 201 return found; 202 } 203 204 static void btstack_link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it){ 205 UNUSED(it); 206 } 207 208 static const btstack_link_key_db_t btstack_link_key_db_tlv = { 209 btstack_link_key_db_tlv_open, 210 btstack_link_key_db_tlv_set_bd_addr, 211 btstack_link_key_db_tlv_close, 212 btstack_link_key_db_tlv_get_link_key, 213 btstack_link_key_db_tlv_put_link_key, 214 btstack_link_key_db_tlv_delete_link_key, 215 btstack_link_key_db_tlv_iterator_init, 216 btstack_link_key_db_tlv_iterator_get_next, 217 btstack_link_key_db_tlv_iterator_done, 218 }; 219 220 const btstack_link_key_db_t * btstack_link_key_db_tlv_get_instance(const btstack_tlv_t * btstack_tlv_impl, void * btstack_tlv_context){ 221 self->btstack_tlv_impl = btstack_tlv_impl; 222 self->btstack_tlv_context = btstack_tlv_context; 223 return &btstack_link_key_db_tlv; 224 } 225 226 227