1ba9e989aSMatthias Ringwald /* 2ba9e989aSMatthias Ringwald * Copyright (C) 2016 BlueKitchen GmbH 3ba9e989aSMatthias Ringwald * 4ba9e989aSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5ba9e989aSMatthias Ringwald * modification, are permitted provided that the following conditions 6ba9e989aSMatthias Ringwald * are met: 7ba9e989aSMatthias Ringwald * 8ba9e989aSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9ba9e989aSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10ba9e989aSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11ba9e989aSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12ba9e989aSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13ba9e989aSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14ba9e989aSMatthias Ringwald * contributors may be used to endorse or promote products derived 15ba9e989aSMatthias Ringwald * from this software without specific prior written permission. 16ba9e989aSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17ba9e989aSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18ba9e989aSMatthias Ringwald * monetary gain. 19ba9e989aSMatthias Ringwald * 20ba9e989aSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21ba9e989aSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22ba9e989aSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23ba9e989aSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24ba9e989aSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25ba9e989aSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26ba9e989aSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27ba9e989aSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28ba9e989aSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29ba9e989aSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30ba9e989aSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31ba9e989aSMatthias Ringwald * SUCH DAMAGE. 32ba9e989aSMatthias Ringwald * 33ba9e989aSMatthias Ringwald * Please inquire about commercial licensing options at 34ba9e989aSMatthias Ringwald * [email protected] 35ba9e989aSMatthias Ringwald * 36ba9e989aSMatthias Ringwald */ 37ba9e989aSMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_link_key_db_static.c" 39ba9e989aSMatthias Ringwald 40ba9e989aSMatthias Ringwald /* 41ba9e989aSMatthias Ringwald * btstack_link_key_db_static.c 42ba9e989aSMatthias Ringwald * 43ba9e989aSMatthias Ringwald * Static Link Key implementation to use during development/porting: 44ba9e989aSMatthias Ringwald * - Link keys have to be manually added to this file to make them usable 45ba9e989aSMatthias Ringwald * + Link keys are preserved on reflash in constrast to the program flash based link key store 46ba9e989aSMatthias Ringwald */ 47ba9e989aSMatthias Ringwald 48ba9e989aSMatthias Ringwald #include "classic/btstack_link_key_db.h" 49ba9e989aSMatthias Ringwald 50ba9e989aSMatthias Ringwald #include "btstack_debug.h" 51ba9e989aSMatthias Ringwald #include "btstack_util.h" 52ba9e989aSMatthias Ringwald 53*3cfa4086SMatthias Ringwald #include <stdint.h> 54*3cfa4086SMatthias Ringwald #include <stdio.h> 55*3cfa4086SMatthias Ringwald #include <string.h> 56*3cfa4086SMatthias Ringwald 57ba9e989aSMatthias Ringwald typedef struct { 58ba9e989aSMatthias Ringwald const char * bd_addr; 59ba9e989aSMatthias Ringwald const char * link_key; 60ba9e989aSMatthias Ringwald int link_key_type; 61ba9e989aSMatthias Ringwald } link_key_entry_t; 62ba9e989aSMatthias Ringwald 63ba9e989aSMatthias Ringwald // fixed link key db 64ba9e989aSMatthias Ringwald static const link_key_entry_t link_key_db[] = { 65ba9e989aSMatthias Ringwald // Example enry 66ba9e989aSMatthias Ringwald { "11:22:33:44:55:66", "11223344556677889900112233445566", 1}, 67ba9e989aSMatthias Ringwald // Add new link keys here.. 68ba9e989aSMatthias Ringwald }; 69ba9e989aSMatthias Ringwald 70ba9e989aSMatthias Ringwald static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1]; // 11223344556677889900112233445566\0 71ba9e989aSMatthias Ringwald static char *link_key_to_str(link_key_t link_key){ 72ba9e989aSMatthias Ringwald char * p = link_key_to_str_buffer; 73ba9e989aSMatthias Ringwald int i; 74ba9e989aSMatthias Ringwald for (i = 0; i < LINK_KEY_LEN ; i++) { 75ba9e989aSMatthias Ringwald *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F); 76ba9e989aSMatthias Ringwald *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F); 77ba9e989aSMatthias Ringwald } 78ba9e989aSMatthias Ringwald *p = 0; 79ba9e989aSMatthias Ringwald return (char *) link_key_to_str_buffer; 80ba9e989aSMatthias Ringwald } 81ba9e989aSMatthias Ringwald 82ba9e989aSMatthias Ringwald static int sscanf_link_key(const char * link_key_string, link_key_t link_key){ 83ba9e989aSMatthias Ringwald unsigned int buffer[LINK_KEY_LEN]; 84ba9e989aSMatthias Ringwald 85ba9e989aSMatthias Ringwald // reset result buffer 86ba9e989aSMatthias Ringwald memset(&buffer, 0, sizeof(buffer)); 87ba9e989aSMatthias Ringwald 88ba9e989aSMatthias Ringwald // parse 89ba9e989aSMatthias Ringwald int result = sscanf( (char *) link_key_string, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 90ba9e989aSMatthias Ringwald &buffer[0], &buffer[1], &buffer[2], &buffer[3], 91ba9e989aSMatthias Ringwald &buffer[4], &buffer[5], &buffer[6], &buffer[7], 92ba9e989aSMatthias Ringwald &buffer[8], &buffer[9], &buffer[10], &buffer[11], 93ba9e989aSMatthias Ringwald &buffer[12], &buffer[13], &buffer[14], &buffer[15] ); 94ba9e989aSMatthias Ringwald 95ba9e989aSMatthias Ringwald if (result != LINK_KEY_LEN) return 0; 96ba9e989aSMatthias Ringwald 97ba9e989aSMatthias Ringwald // store 98ba9e989aSMatthias Ringwald int i; 99ba9e989aSMatthias Ringwald uint8_t *p = (uint8_t *) link_key; 100ba9e989aSMatthias Ringwald for (i=0; i<LINK_KEY_LEN; i++ ) { 101ba9e989aSMatthias Ringwald *p++ = (uint8_t) buffer[i]; 102ba9e989aSMatthias Ringwald } 103ba9e989aSMatthias Ringwald return 1; 104ba9e989aSMatthias Ringwald } 105ba9e989aSMatthias Ringwald 106ba9e989aSMatthias Ringwald static void link_key_db_init(void){ 107ba9e989aSMatthias Ringwald } 108ba9e989aSMatthias Ringwald 109ba9e989aSMatthias Ringwald static void link_key_db_close(void){ 110ba9e989aSMatthias Ringwald } 111ba9e989aSMatthias Ringwald 112ba9e989aSMatthias Ringwald 113ba9e989aSMatthias Ringwald // returns 1 if found 114ba9e989aSMatthias Ringwald static int link_key_db_get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) { 115ba9e989aSMatthias Ringwald int i; 116ba9e989aSMatthias Ringwald int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t); 117ba9e989aSMatthias Ringwald 118ba9e989aSMatthias Ringwald for (i=0;i<num_entries;i++){ 119ba9e989aSMatthias Ringwald if (strcmp(bd_addr_to_str(bd_addr), link_key_db[i].bd_addr)) continue; 120ba9e989aSMatthias Ringwald *link_key_type = (link_key_type_t) link_key_db[i].link_key_type; 121ba9e989aSMatthias Ringwald sscanf_link_key(link_key_db[i].link_key, link_key); 122ba9e989aSMatthias Ringwald return 1; 123ba9e989aSMatthias Ringwald } 124ba9e989aSMatthias Ringwald return 0; 125ba9e989aSMatthias Ringwald } 126ba9e989aSMatthias Ringwald 127ba9e989aSMatthias Ringwald static void link_key_db_delete_link_key(bd_addr_t bd_addr){ 128cebe3e9eSMatthias Ringwald (void) bd_addr; 129ba9e989aSMatthias Ringwald } 130ba9e989aSMatthias Ringwald 131ba9e989aSMatthias Ringwald 132ba9e989aSMatthias Ringwald static void link_key_db_put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ 133ba9e989aSMatthias Ringwald log_info("Please add the following line to btstack_link_key_db.c"); 134ba9e989aSMatthias Ringwald log_info("{ \"%s\", \"%s\", %u },\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key), (int) link_key_type); 135ba9e989aSMatthias Ringwald } 136ba9e989aSMatthias Ringwald 137ba9e989aSMatthias Ringwald static void link_key_db_set_local_bd_addr(bd_addr_t bd_addr){ 138cebe3e9eSMatthias Ringwald (void) bd_addr; 139ba9e989aSMatthias Ringwald } 140ba9e989aSMatthias Ringwald 141e3012924SMatthias Ringwald static int link_key_db_tlv_iterator_init(btstack_link_key_iterator_t * it){ 142e3012924SMatthias Ringwald it->context = (void*) 0; 143e3012924SMatthias Ringwald return 1; 144e3012924SMatthias Ringwald } 145e3012924SMatthias Ringwald 146e3012924SMatthias Ringwald static int 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){ 147e3012924SMatthias Ringwald uintptr_t i = (uintptr_t) it->context; 148e3012924SMatthias Ringwald 149cebe3e9eSMatthias Ringwald unsigned int num_entries = sizeof(link_key_db) / sizeof(link_key_entry_t); 150e3012924SMatthias Ringwald if (i >= num_entries) return 0; 151e3012924SMatthias Ringwald 152e3012924SMatthias Ringwald // fetch values 153e3012924SMatthias Ringwald *link_key_type = (link_key_type_t) link_key_db[i].link_key_type; 154e3012924SMatthias Ringwald sscanf_link_key(link_key_db[i].link_key, link_key); 155e3012924SMatthias Ringwald sscanf_bd_addr(link_key_db[i].bd_addr, bd_addr); 156e3012924SMatthias Ringwald 157e3012924SMatthias Ringwald // next 15838933cf9SMatthias Ringwald it->context = (void*) (i+1); 159e3012924SMatthias Ringwald return 1; 160e3012924SMatthias Ringwald } 161e3012924SMatthias Ringwald 162e3012924SMatthias Ringwald static void link_key_db_tlv_iterator_done(btstack_link_key_iterator_t * it){ 163e3012924SMatthias Ringwald UNUSED(it); 164e3012924SMatthias Ringwald } 165e3012924SMatthias Ringwald 166ecd2c7e0SMatthias Ringwald static const btstack_link_key_db_t btstack_link_key_db_static = { 167ba9e989aSMatthias Ringwald link_key_db_init, 168ba9e989aSMatthias Ringwald link_key_db_set_local_bd_addr, 169ba9e989aSMatthias Ringwald link_key_db_close, 170ba9e989aSMatthias Ringwald link_key_db_get_link_key, 171ba9e989aSMatthias Ringwald link_key_db_put_link_key, 172ba9e989aSMatthias Ringwald link_key_db_delete_link_key, 173e3012924SMatthias Ringwald link_key_db_tlv_iterator_init, 174e3012924SMatthias Ringwald link_key_db_tlv_iterator_get_next, 175e3012924SMatthias Ringwald link_key_db_tlv_iterator_done, 176ba9e989aSMatthias Ringwald }; 177ba9e989aSMatthias Ringwald 178ba9e989aSMatthias Ringwald const btstack_link_key_db_t * btstack_link_key_db_static_instance(void){ 179ba9e989aSMatthias Ringwald return &btstack_link_key_db_static; 180ba9e989aSMatthias Ringwald } 181