diff --git a/src/remote_device_db_memory.c b/src/remote_device_db_memory.c index d8fee64d6..d82044e2b 100644 --- a/src/remote_device_db_memory.c +++ b/src/remote_device_db_memory.c @@ -48,28 +48,54 @@ static void db_open(void){ static void db_close(void){ } -static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) { +db_mem_device_t * _get_item(bd_addr_t *bd_addr) { linked_item_t *it; for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){ db_mem_device_t * item = (db_mem_device_t *) it; if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) { - memcpy(link_key, item->link_key, LINK_KEY_LEN); - return 1; + return item; } } - return 0; + return NULL; } -static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key){ - linked_item_t *it; - for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){ - db_mem_device_t * item = (db_mem_device_t *) it; - if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) { - memcpy(item->link_key, link_key, LINK_KEY_LEN); - return; - } - } +static void delete_item(bd_addr_t *bd_addr){ + device_name_t device_name; + db_mem_device_t * item = _get_item(bd_addr); + + if (!item) return; + + linked_list_remove(&db_mem_devices, (linked_item_t *) item); + btstack_memory_db_mem_device_free(item); +} + +static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) { + db_mem_device_t * item = _get_item(bd_addr); + + if (!item) return 0; + + strncpy((char*)device_name, item->device_name, MAX_NAME_LEN); + return 1; +} + +static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) { + db_mem_device_t * item = _get_item(bd_addr); + + if (!item) return 0; + + memcpy(link_key, item->link_key, LINK_KEY_LEN); + return 1; +} + +static void delete_link_key(bd_addr_t *bd_addr){ + delete_item(bd_addr); +} + + +static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key){ + if ( get_link_key(bd_addr, link_key) ) return; + // Record not found, create new one for this device db_mem_device_t * newItem = btstack_memory_db_mem_device_get(); @@ -81,30 +107,13 @@ static void put_link_key(bd_addr_t *bd_addr, link_key_t *link_key){ linked_list_add(&db_mem_devices, (linked_item_t *) newItem); } -static void delete_link_key(bd_addr_t *bd_addr){ - linked_item_t *it; - for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){ - db_mem_device_t * item = (db_mem_device_t *) it; - if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) { - // Found record, delete it - linked_list_remove(&db_mem_devices, (linked_item_t *) item); - btstack_memory_db_mem_device_free(item); - return; - } - } +static void delete_name(bd_addr_t *bd_addr){ + delete_item(bd_addr); } static void put_name(bd_addr_t *bd_addr, device_name_t *device_name){ - linked_item_t *it; - for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){ - db_mem_device_t * item = (db_mem_device_t *) it; - if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) { - // Found record, ammend it - strncpy(item->device_name, (const char*) device_name, MAX_NAME_LEN); - return; - } - } - + if (get_name(bd_addr, device_name)) return; + // Record not found, create a new one for this device db_mem_device_t * newItem = btstack_memory_db_mem_device_get(); @@ -113,34 +122,9 @@ static void put_name(bd_addr_t *bd_addr, device_name_t *device_name){ memcpy(newItem->bd_addr, bd_addr, sizeof(bd_addr_t)); strncpy(newItem->device_name, (const char*) device_name, MAX_NAME_LEN); memset(newItem->link_key, 0, LINK_KEY_LEN); - linked_list_add(&db_mem_devices, (linked_item_t *) newItem); } -static void delete_name(bd_addr_t *bd_addr){ - linked_item_t *it; - for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){ - db_mem_device_t * item = (db_mem_device_t *) it; - if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) { - // Found record, delete it - linked_list_remove(&db_mem_devices, (linked_item_t *) item); - btstack_memory_db_mem_device_free(item); - return; - } - } -} - -static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) { - linked_item_t *it; - for (it = (linked_item_t *) db_mem_devices; it ; it = it->next){ - db_mem_device_t * item = (db_mem_device_t *) it; - if (BD_ADDR_CMP(item->bd_addr, *bd_addr) == 0) { - strncpy((char*)device_name, item->device_name, MAX_NAME_LEN); - return 1; - } - } - return 0; -} // MARK: PERSISTENT RFCOMM CHANNEL ALLOCATION