keep db_mem_link_keys and db_mem_names lists sorted by last used item

This commit is contained in:
mila@ringwald.ch 2011-09-08 22:15:33 +00:00
parent fe739638d3
commit 208d2a4d0f
2 changed files with 64 additions and 19 deletions

View File

@ -39,8 +39,9 @@
#include <btstack/utils.h>
#include <btstack/linked_list.h>
static linked_list_t db_mem_link_keys = NULL;
static linked_list_t db_mem_names = NULL;
// This lists should be only accessed by tests.
linked_list_t db_mem_link_keys = NULL;
linked_list_t db_mem_names = NULL;
static linked_list_t db_mem_services = NULL;
// Device info
@ -67,7 +68,11 @@ static int get_name(bd_addr_t *bd_addr, device_name_t *device_name) {
if (!item) return 0;
strncpy((char*)device_name, item->device_name, MAX_NAME_LEN);
return 1;
linked_list_remove(&db_mem_names, (linked_item_t *) item);
linked_list_add(&db_mem_names, (linked_item_t *) item);
return 1;
}
static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) {
@ -76,7 +81,11 @@ static int get_link_key(bd_addr_t *bd_addr, link_key_t *link_key) {
if (!item) return 0;
memcpy(link_key, item->link_key, LINK_KEY_LEN);
return 1;
linked_list_remove(&db_mem_link_keys, (linked_item_t *) item);
linked_list_add(&db_mem_link_keys, (linked_item_t *) item);
return 1;
}
static void delete_link_key(bd_addr_t *bd_addr){
@ -90,7 +99,7 @@ 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){
if ( get_link_key(bd_addr, link_key) ) return;
if ( get_link_key(bd_addr, link_key) ) return;
// Record not found, create new one for this device
db_mem_device_link_key_t * newItem = (db_mem_device_link_key_t *)btstack_memory_db_mem_device_link_key_get();

View File

@ -1,38 +1,74 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "remote_device_db.h"
extern linked_list_t db_mem_link_keys ;
extern linked_list_t db_mem_names ;
TEST_GROUP(RemoteDeviceDB){
bd_addr_t addr;
bd_addr_t addr1, addr2, addr3;
device_name_t device_name;
link_key_t link_key;
void setup(){
bd_addr_t addr = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
strcpy((char*)device_name, "matthias");
strcpy((char*)link_key, "matthias");
bd_addr_t addr1 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x01 };
bd_addr_t addr2 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x02 };
bd_addr_t addr3 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x03 };
}
void teardown(){}
};
TEST(RemoteDeviceDB, PutGetDeleteName){
remote_device_db_memory.put_name(&addr, &device_name);
CHECK(remote_device_db_memory.get_name(&addr, &device_name));
TEST(RemoteDeviceDB, SinglePutGetDeleteName){
sprintf((char*)device_name, "%d", 100);
remote_device_db_memory.put_name(&addr1, &device_name);
CHECK(remote_device_db_memory.get_name(&addr1, &device_name));
remote_device_db_memory.delete_name(&addr);
CHECK(!remote_device_db_memory.get_name(&addr, &device_name));
remote_device_db_memory.delete_name(&addr1);
CHECK(!remote_device_db_memory.get_name(&addr1, &device_name));
}
TEST(RemoteDeviceDB, PutGetDeleteKey){
remote_device_db_memory.put_link_key(&addr, &link_key);
CHECK(remote_device_db_memory.get_link_key(&addr, &link_key));
TEST(RemoteDeviceDB, SortByLastUsedName){
sprintf((char*)device_name, "%d", 10);
remote_device_db_memory.put_name(&addr1, &device_name);
sprintf((char*)device_name, "%d", 20);
remote_device_db_memory.put_name(&addr2, &device_name);
sprintf((char*)device_name, "%d", 30);
remote_device_db_memory.put_name(&addr3, &device_name);
CHECK(remote_device_db_memory.get_name(&addr2, &device_name));
//get first element of the list
db_mem_device_name_t * item = (db_mem_device_name_t *) db_mem_names;
STRCMP_EQUAL((char*)item->device_name, "10");
}
TEST(RemoteDeviceDB, SinglePutGetDeleteKey){
sprintf((char*)link_key, "%d", 100);
remote_device_db_memory.put_link_key(&addr1, &link_key);
CHECK(remote_device_db_memory.get_link_key(&addr1, &link_key));
remote_device_db_memory.delete_link_key(&addr);
CHECK(!remote_device_db_memory.get_link_key(&addr, &link_key));
remote_device_db_memory.delete_link_key(&addr1);
CHECK(!remote_device_db_memory.get_link_key(&addr1, &link_key));
}
TEST(RemoteDeviceDB, SortByLastUsedKey){
sprintf((char*)link_key, "%d", 10);
remote_device_db_memory.put_link_key(&addr1, &link_key);
sprintf((char*)link_key, "%d", 20);
remote_device_db_memory.put_link_key(&addr2, &link_key);
sprintf((char*)link_key, "%d", 30);
remote_device_db_memory.put_link_key(&addr3, &link_key);
CHECK(remote_device_db_memory.get_link_key(&addr2, &link_key));
//get first element of the list
db_mem_device_link_key_t * item = (db_mem_device_link_key_t *) db_mem_link_keys;
STRCMP_EQUAL((char*)item->link_key, "10");
}
int main (int argc, const char * argv[]){