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