/* * Copyright (C) 2017 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #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 #include #include "btstack_debug.h" // LE Device DB Implementation storing entries in btstack_tlv // Local cache is used to keep track of deleted entries in TLV #define INVALID_ENTRY_ADDR_TYPE 0xff // Single stored entry typedef struct le_device_db_entry_t { // Identification int addr_type; bd_addr_t addr; sm_key_t irk; // Stored pairing information allows to re-establish an enncrypted connection // with a peripheral that doesn't have any persistent memory sm_key_t ltk; uint16_t ediv; uint8_t rand[8]; uint8_t key_size; uint8_t authenticated; uint8_t authorized; #ifdef ENABLE_LE_SIGNED_WRITE // Signed Writes by remote sm_key_t remote_csrk; uint32_t remote_counter; // Signed Writes by us 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 // only stores if entry present 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 const char tag_0 = 'B'; static const char tag_1 = 'T'; static const char tag_2 = 'D'; static uint32_t le_device_db_tlv_tag_for_index(uint8_t index){ return (tag_0 << 24) | (tag_1 << 16) | (tag_2 << 8) | index; } // @returns success // @param index = entry_pos static int le_device_db_tlv_fetch(int index, le_device_db_entry_t * entry){ if (index < 0 || index >= NVM_NUM_DEVICE_DB_ENTRIES){ log_error("le_device_db_tlv_fetch called with invalid index %d", index); return 0; } 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 != 0; } // @returns success // @param index = entry_pos static int le_device_db_tlv_store(int index, le_device_db_entry_t * entry){ if (index < 0 || index >= NVM_NUM_DEVICE_DB_ENTRIES){ log_error("le_device_db_tlv_store called with invalid index %d", index); return 0; } uint32_t tag = le_device_db_tlv_tag_for_index(index); 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 1; } // @param index = entry_pos static int le_device_db_tlv_delete(int index){ if (index < 0 || index >= NVM_NUM_DEVICE_DB_ENTRIES){ log_error("le_device_db_tlv_delete called with invalid index %d", index); return 0; } 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 1; } void le_device_db_init(void){ int i; num_valid_entries = 0; memset(entry_map, 0, sizeof(entry_map)); for (i=0;i