add re-used records from device db in front, extend tests a bit

This commit is contained in:
Matthias Ringwald 2015-05-14 10:20:44 +02:00
parent 3e35d521bf
commit 522ba429ec
3 changed files with 58 additions and 41 deletions

View File

@ -107,25 +107,32 @@ static void delete_link_key(bd_addr_t bd_addr){
static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
db_mem_device_link_key_t * existingRecord = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
// check for existing record and remove if found
if (existingRecord){ db_mem_device_link_key_t * record = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr);
memcpy(existingRecord->link_key, link_key, LINK_KEY_LEN); if (record){
return; linked_list_remove(&db_mem_link_keys, (linked_item_t*) record);
} }
// Record not found, create new one for this device // record not found, get new one from memory pool
db_mem_device_link_key_t * newItem = btstack_memory_db_mem_device_link_key_get(); if (!record) {
if (!newItem){ record = btstack_memory_db_mem_device_link_key_get();
newItem = (db_mem_device_link_key_t*)linked_list_get_last_item(&db_mem_link_keys);
} }
// if none left, re-use last item and remove from list
if (!record){
record = (db_mem_device_link_key_t*)linked_list_get_last_item(&db_mem_link_keys);
if (record) {
linked_list_remove(&db_mem_link_keys, (linked_item_t*) record);
}
}
if (!record) return;
if (!newItem) return; memcpy(record->device.bd_addr, bd_addr, sizeof(bd_addr_t));
memcpy(record->link_key, link_key, LINK_KEY_LEN);
memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t)); record->link_key_type = link_key_type;
memcpy(newItem->link_key, link_key, LINK_KEY_LEN); linked_list_add(&db_mem_link_keys, (linked_item_t *) record);
newItem->link_key_type = link_key_type;
linked_list_add(&db_mem_link_keys, (linked_item_t *) newItem);
} }
static void delete_name(bd_addr_t bd_addr){ static void delete_name(bd_addr_t bd_addr){
@ -138,24 +145,31 @@ static void delete_name(bd_addr_t bd_addr){
} }
static void put_name(bd_addr_t bd_addr, device_name_t *device_name){ static void put_name(bd_addr_t bd_addr, device_name_t *device_name){
db_mem_device_name_t * existingRecord = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
if (existingRecord){
strncpy(existingRecord->device_name, (const char*) device_name, MAX_NAME_LEN);
return;
}
// Record not found, create a new one for this device
db_mem_device_name_t * newItem = btstack_memory_db_mem_device_name_get();
if (!newItem) {
newItem = (db_mem_device_name_t*)linked_list_get_last_item(&db_mem_names);
};
if (!newItem) return; // check for existing record and remove if found
db_mem_device_name_t * record = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr);
if (record){
linked_list_remove(&db_mem_names, (linked_item_t*) record);
}
// record not found, get new one from memory pool
if (!record) {
record = btstack_memory_db_mem_device_name_get();
}
// if none left, re-use last item and remove from list
if (!record){
record = (db_mem_device_name_t*)linked_list_get_last_item(&db_mem_names);
if (record) {
linked_list_remove(&db_mem_names, (linked_item_t*) record);
}
}
if (!record) return;
memcpy(newItem->device.bd_addr, bd_addr, sizeof(bd_addr_t)); memcpy(record->device.bd_addr, bd_addr, sizeof(bd_addr_t));
strncpy(newItem->device_name, (const char*) device_name, MAX_NAME_LEN); strncpy(record->device_name, (const char*) device_name, MAX_NAME_LEN);
linked_list_add(&db_mem_names, (linked_item_t *) newItem); linked_list_add(&db_mem_names, (linked_item_t *) record);
} }

View File

@ -24,7 +24,7 @@
#define MAX_NO_BNEP_SERVICES 1 #define MAX_NO_BNEP_SERVICES 1
#define MAX_NO_BNEP_CHANNELS MAX_SPP_CONNECTIONS #define MAX_NO_BNEP_CHANNELS MAX_SPP_CONNECTIONS
#define MAX_NO_DB_MEM_DEVICE_LINK_KEYS 2 #define MAX_NO_DB_MEM_DEVICE_LINK_KEYS 2
#define MAX_NO_DB_MEM_DEVICE_NAMES 10 #define MAX_NO_DB_MEM_DEVICE_NAMES 2
#define MAX_NO_DB_MEM_SERVICES 1 #define MAX_NO_DB_MEM_SERVICES 1
#define HAVE_MALLOC #define HAVE_MALLOC

View File

@ -17,12 +17,13 @@ extern linked_list_t db_mem_names ;
void dump(linked_list_t list){ void dump(linked_list_t list){
printf("dump:\n"); printf("dump:\n");
int i;
linked_item_t *it; linked_item_t *it;
for (it = (linked_item_t *) list; it ; it = it->next){ for (it = (linked_item_t *) list, i = 1; it ; it = it->next, i++){
db_mem_device_t * item = (db_mem_device_t *) it; db_mem_device_t * item = (db_mem_device_t *) it;
db_mem_device_name_t * item1 = (db_mem_device_name_t *) it; db_mem_device_name_t * item1 = (db_mem_device_name_t *) it;
db_mem_device_link_key_t * item2 = (db_mem_device_link_key_t *) it; db_mem_device_link_key_t * item2 = (db_mem_device_link_key_t *) it;
printf("%s + %s + %u\n", item1->device_name, item2->link_key, item->bd_addr[5]); printf("%u. %s + %s + %u\n", i, item1->device_name, item2->link_key, item->bd_addr[5]);
} }
} }
@ -66,13 +67,14 @@ TEST(RemoteDeviceDB, SinglePutGetDeleteName){
} }
TEST(RemoteDeviceDB, SortByLastUsedName){ TEST(RemoteDeviceDB, SortByLastUsedName){
sprintf((char*)device_name, "%d", 10); remote_device_db_memory.put_name(addr1, (device_name_t*) "10");
remote_device_db_memory.put_name(addr1, &device_name); // dump(db_mem_names);
sprintf((char*)device_name, "%d", 20); remote_device_db_memory.put_name(addr2, (device_name_t*) "20");
remote_device_db_memory.put_name(addr2, &device_name); // dump(db_mem_names);
sprintf((char*)device_name, "%d", 30); remote_device_db_memory.put_name(addr3, (device_name_t*) "30");
remote_device_db_memory.put_name(addr3, &device_name); // dump(db_mem_names);
CHECK(!remote_device_db_memory.get_name(addr1, &device_name));
CHECK(remote_device_db_memory.get_name(addr2, &device_name)); CHECK(remote_device_db_memory.get_name(addr2, &device_name));
//get first element of the list //get first element of the list
db_mem_device_name_t * item = (db_mem_device_name_t *) db_mem_names; db_mem_device_name_t * item = (db_mem_device_name_t *) db_mem_names;
@ -102,6 +104,7 @@ TEST(RemoteDeviceDB, SortByLastUsedKey){
remote_device_db_memory.put_link_key(addr3, link_key, link_key_type); remote_device_db_memory.put_link_key(addr3, link_key, link_key_type);
// dump(db_mem_link_keys); // dump(db_mem_link_keys);
CHECK(!remote_device_db_memory.get_link_key(addr1, link_key, &link_key_type));
CHECK(remote_device_db_memory.get_link_key(addr2, link_key, &link_key_type)); CHECK(remote_device_db_memory.get_link_key(addr2, link_key, &link_key_type));
// dump(db_mem_link_keys); // dump(db_mem_link_keys);