diff --git a/doc/manual/update_apis.py b/doc/manual/update_apis.py index 21a2fb529..c1d48b04f 100755 --- a/doc/manual/update_apis.py +++ b/doc/manual/update_apis.py @@ -18,7 +18,7 @@ apis = [ ["src/classic/bnep.h", "BNEP", "bnep"], ["src/classic/pan.h", "PAN", "pan"], - ["src/classic/remote_device_db.h","Remote Device DB","rdevDb"], + ["src/classic/btstack_link_key_db.h","Remote Device DB","rdevDb"], ["src/classic/rfcomm.h", "RFCOMM", "rfcomm"], ["src/classic/sdp.h", "SDP", "sdp"], ["src/classic/sdp_client.h", "SDP Client", "sdpClient"], diff --git a/example/embedded/Makefile.inc b/example/embedded/Makefile.inc index da02564ca..23ad7e58a 100644 --- a/example/embedded/Makefile.inc +++ b/example/embedded/Makefile.inc @@ -21,7 +21,7 @@ COMMON += \ hci_dump.c \ l2cap.c \ l2cap_signaling.c \ - remote_device_db_memory.c \ + btstack_link_key_db_memory.c \ sdp_util.c \ rfcomm.c \ bnep.c \ diff --git a/platform/cocoa/btstack_device_name_db_cocoa.h b/platform/cocoa/btstack_device_name_db_cocoa.h new file mode 100644 index 000000000..2a73235f4 --- /dev/null +++ b/platform/cocoa/btstack_device_name_db_cocoa.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2014 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 + * + */ + +#ifndef __BTSTACK_LINK_KEY_DB_FS_H +#define __BTSTACK_LINK_KEY_DB_FS_H + +#include "classic/btstack_link_key_db.h" + +#if defined __cplusplus +extern "C" { +#endif + +/* + * @brief Get basic link key db implementation that stores link keys in /tmp + */ +const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void); + +/* API_END */ + +#if defined __cplusplus +} +#endif + +#endif // __BTSTACK_LINK_KEY_DB_FS_H diff --git a/platform/cocoa/btstack_device_name_db_cocoa.m b/platform/cocoa/btstack_device_name_db_cocoa.m new file mode 100644 index 000000000..739c3a2e3 --- /dev/null +++ b/platform/cocoa/btstack_device_name_db_cocoa.m @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2009-2012 by Matthias Ringwald + * + * 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 MATTHIAS RINGWALD 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 btstack@ringwald.ch + * + */ +#include "classic/btstack_link_key_db.h" +#include "btstack_debug.h" + +#import + +#define BTdaemonID "ch.ringwald.btdaemon" +#define BTDaemonPrefsPath "Library/Preferences/ch.ringwald.btdaemon.plist" + +#define DEVICES_KEY "devices" +#define PREFS_REMOTE_NAME @"RemoteName" +#define PREFS_LINK_KEY @"LinkKey" + +static void put_name(bd_addr_t bd_addr, device_name_t *device_name); + +static NSMutableDictionary *remote_devices = nil; + +// Device info +static void db_open(void){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + // NSUserDefaults didn't work + // + // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + // NSDictionary * dict = [defaults persistentDomainForName:BTdaemonID]; + + // NSDictionary * dict = (NSDictionary*) CFPreferencesCopyAppValue(CFSTR(DEVICES_KEY), CFSTR(BTdaemonID)); + NSDictionary * dict; + dict = (NSDictionary*) CFPreferencesCopyAppValue(CFSTR(DEVICES_KEY), CFSTR(BTdaemonID)); + remote_devices = [[NSMutableDictionary alloc] initWithCapacity:([dict count]+5)]; + + // copy entries + for (id key in dict) { + NSDictionary *value = [dict objectForKey:key]; + NSMutableDictionary *deviceEntry = [NSMutableDictionary dictionaryWithCapacity:[value count]]; + [deviceEntry addEntriesFromDictionary:value]; + [remote_devices setObject:deviceEntry forKey:key]; + } + + log_info("read prefs for %u devices\n", (unsigned int) [dict count]); + + [pool release]; +} + +static void db_synchronize(void){ + log_info("stored prefs for %u devices\n", (unsigned int) [remote_devices count]); + + // 3 different ways + + // Core Foundation + CFPreferencesSetValue(CFSTR(DEVICES_KEY), (CFPropertyListRef) remote_devices, CFSTR(BTdaemonID), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); + CFPreferencesSynchronize(CFSTR(BTdaemonID), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); + + // NSUserDefaults didn't work + // + // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + // [defaults setPersistentDomain:remote_devices forName:BTdaemonID]; + // [defaults synchronize]; +} + +static void db_close(void){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + // don't call db_synchronize(); + // a) we're calling db_synchronize() after each change already + // b) db_close is called during the SIGINT handler which causes a corrupt prefs file + + [remote_devices release]; + remote_devices = nil; + [pool release]; +} + +static NSString * stringForAddress(bd_addr_t addr) { + return [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], + addr[3], addr[4], addr[5]]; +} + +static void set_value(bd_addr_t bd_addr, NSString *key, id value){ + NSString *devAddress = stringForAddress(bd_addr); + NSMutableDictionary * deviceDict = [remote_devices objectForKey:devAddress]; + if (!deviceDict){ + deviceDict = [NSMutableDictionary dictionaryWithCapacity:3]; + [remote_devices setObject:deviceDict forKey:devAddress]; + } + [deviceDict setObject:value forKey:key]; + db_synchronize(); +} + +static void delete_value(bd_addr_t bd_addr, NSString *key){ + NSString *devAddress = stringForAddress(bd_addr); + NSMutableDictionary * deviceDict = [remote_devices objectForKey:devAddress]; + [deviceDict removeObjectForKey:key]; + db_synchronize(); + +} + +static id get_value(bd_addr_t bd_addr, NSString *key){ + NSString *devAddress = stringForAddress(bd_addr); + NSMutableDictionary * deviceDict = [remote_devices objectForKey:devAddress]; + if (!deviceDict) return nil; + return [deviceDict objectForKey:key]; +} + +static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) { + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSData *linkKey = get_value(bd_addr, PREFS_LINK_KEY); + if ([linkKey length] == LINK_KEY_LEN){ + memcpy(link_key, [linkKey bytes], LINK_KEY_LEN); + if (link_key_type){ + *link_key_type = COMBINATION_KEY; + } + } + [pool release]; + return (linkKey != nil); +} + +static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSData *linkKey = [NSData dataWithBytes:link_key length:16]; + set_value(bd_addr, PREFS_LINK_KEY, linkKey); + [pool release]; +} + +static void delete_link_key(bd_addr_t bd_addr){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + delete_value(bd_addr, PREFS_LINK_KEY); + [pool release]; +} + +static void put_name(bd_addr_t bd_addr, device_name_t *device_name){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSString *remoteName = [NSString stringWithUTF8String:(char*)device_name]; + if (!remoteName){ + remoteName = [NSString stringWithCString:(char*)device_name encoding:NSISOLatin1StringEncoding]; + } + if (remoteName) { + set_value(bd_addr, PREFS_REMOTE_NAME, remoteName); + } + [pool release]; +} + +static void delete_name(bd_addr_t bd_addr){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + delete_value(bd_addr, PREFS_REMOTE_NAME); + [pool release]; +} + +static int get_name(bd_addr_t bd_addr, device_name_t *device_name) { + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSString *remoteName = get_value(bd_addr, PREFS_REMOTE_NAME); + if (remoteName){ + memset(device_name, 0, sizeof(device_name_t)); + strncpy((char*) device_name, [remoteName UTF8String], sizeof(device_name_t)-1); + } + [pool release]; + return (remoteName != nil); +} + +remote_device_db_t remote_device_db_iphone = { + db_open, + db_close, + get_link_key, + put_link_key, + delete_link_key, + get_name, + put_name, + delete_name, +}; + diff --git a/platform/cocoa/btstack_link_key_db_cocoa.h b/platform/cocoa/btstack_link_key_db_cocoa.h new file mode 100644 index 000000000..55d3a9bab --- /dev/null +++ b/platform/cocoa/btstack_link_key_db_cocoa.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2014 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 + * + */ + +#ifndef __BTSTACK_LINK_KEY_DB_FS_H +#define __BTSTACK_LINK_KEY_DB_FS_H + +#include "classic/btstack_link_key_db.h" + +#if defined __cplusplus +extern "C" { +#endif + +/* + * @brief Get basic link key db implementation that stores link keys preferences + */ +const btstack_link_key_db_t * btstack_link_key_db_cocoa_instance(void); + +/* API_END */ + +#if defined __cplusplus +} +#endif + +#endif // __BTSTACK_LINK_KEY_DB_FS_H diff --git a/platform/cocoa/btstack_link_key_db_cocoa.m b/platform/cocoa/btstack_link_key_db_cocoa.m new file mode 100644 index 000000000..90e065e5c --- /dev/null +++ b/platform/cocoa/btstack_link_key_db_cocoa.m @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2009-2012 by Matthias Ringwald + * + * 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 MATTHIAS RINGWALD 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 btstack@ringwald.ch + * + */ +#include "btstack_link_key_db_cocoa.h" +#include "btstack_debug.h" + +#import + +#define BTdaemonID "ch.ringwald.btdaemon" +#define BTDaemonPrefsPath "Library/Preferences/ch.ringwald.btdaemon.plist" + +#define DEVICES_KEY "devices" +#define PREFS_LINK_KEY @"LinkKey" + +static NSMutableDictionary *remote_devices = nil; + +// Device info +static void db_open(void){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + // NSUserDefaults didn't work + // + // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + // NSDictionary * dict = [defaults persistentDomainForName:BTdaemonID]; + + // NSDictionary * dict = (NSDictionary*) CFPreferencesCopyAppValue(CFSTR(DEVICES_KEY), CFSTR(BTdaemonID)); + NSDictionary * dict; + dict = (NSDictionary*) CFPreferencesCopyAppValue(CFSTR(DEVICES_KEY), CFSTR(BTdaemonID)); + remote_devices = [[NSMutableDictionary alloc] initWithCapacity:([dict count]+5)]; + + // copy entries + for (id key in dict) { + NSDictionary *value = [dict objectForKey:key]; + NSMutableDictionary *deviceEntry = [NSMutableDictionary dictionaryWithCapacity:[value count]]; + [deviceEntry addEntriesFromDictionary:value]; + [remote_devices setObject:deviceEntry forKey:key]; + } + + log_info("read prefs for %u devices\n", (unsigned int) [dict count]); + + [pool release]; +} + +static void db_synchronize(void){ + log_info("stored prefs for %u devices\n", (unsigned int) [remote_devices count]); + + // 3 different ways + + // Core Foundation + CFPreferencesSetValue(CFSTR(DEVICES_KEY), (CFPropertyListRef) remote_devices, CFSTR(BTdaemonID), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); + CFPreferencesSynchronize(CFSTR(BTdaemonID), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost); + + // NSUserDefaults didn't work + // + // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + // [defaults setPersistentDomain:remote_devices forName:BTdaemonID]; + // [defaults synchronize]; +} + +static void db_close(void){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + // don't call db_synchronize(); + // a) we're calling db_synchronize() after each change already + // b) db_close is called during the SIGINT handler which causes a corrupt prefs file + + [remote_devices release]; + remote_devices = nil; + [pool release]; +} + +static NSString * stringForAddress(bd_addr_t addr) { + return [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], + addr[3], addr[4], addr[5]]; +} + +static void set_value(bd_addr_t bd_addr, NSString *key, id value){ + NSString *devAddress = stringForAddress(bd_addr); + NSMutableDictionary * deviceDict = [remote_devices objectForKey:devAddress]; + if (!deviceDict){ + deviceDict = [NSMutableDictionary dictionaryWithCapacity:3]; + [remote_devices setObject:deviceDict forKey:devAddress]; + } + [deviceDict setObject:value forKey:key]; + db_synchronize(); +} + +static void delete_value(bd_addr_t bd_addr, NSString *key){ + NSString *devAddress = stringForAddress(bd_addr); + NSMutableDictionary * deviceDict = [remote_devices objectForKey:devAddress]; + [deviceDict removeObjectForKey:key]; + db_synchronize(); + +} + +static id get_value(bd_addr_t bd_addr, NSString *key){ + NSString *devAddress = stringForAddress(bd_addr); + NSMutableDictionary * deviceDict = [remote_devices objectForKey:devAddress]; + if (!deviceDict) return nil; + return [deviceDict objectForKey:key]; +} + +static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) { + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSData *linkKey = get_value(bd_addr, PREFS_LINK_KEY); + if ([linkKey length] == LINK_KEY_LEN){ + memcpy(link_key, [linkKey bytes], LINK_KEY_LEN); + if (link_key_type){ + *link_key_type = COMBINATION_KEY; + } + } + [pool release]; + return (linkKey != nil); +} + +static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSData *linkKey = [NSData dataWithBytes:link_key length:16]; + set_value(bd_addr, PREFS_LINK_KEY, linkKey); + [pool release]; +} + +static void delete_link_key(bd_addr_t bd_addr){ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + delete_value(bd_addr, PREFS_LINK_KEY); + [pool release]; +} + +const btstack_link_key_db_t btstack_link_key_db_cocoa = { + db_open, + db_close, + get_link_key, + put_link_key, + delete_link_key, +}; + +const btstack_link_key_db_t * btstack_link_key_db_cocoa_instance(void){ + return &btstack_link_key_db_cocoa; +} diff --git a/platform/cocoa/remote_device_db_cocoa.m b/platform/cocoa/remote_device_db_cocoa.m index eda1a578e..739c3a2e3 100644 --- a/platform/cocoa/remote_device_db_cocoa.m +++ b/platform/cocoa/remote_device_db_cocoa.m @@ -33,7 +33,7 @@ * Please inquire about commercial licensing options at btstack@ringwald.ch * */ -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "btstack_debug.h" #import diff --git a/src/classic/remote_device_db.h b/platform/daemon/btstack_device_name_db.h similarity index 63% rename from src/classic/remote_device_db.h rename to platform/daemon/btstack_device_name_db.h index e05affac6..e25f76344 100644 --- a/src/classic/remote_device_db.h +++ b/platform/daemon/btstack_device_name_db.h @@ -39,8 +39,8 @@ * interface to provide link key and remote name storage */ -#ifndef __REMOTE_DEVICE_DB_H -#define __REMOTE_DEVICE_DB_H +#ifndef __BTSTACK_DEVICE_NAME_DB_H +#define __BTSTACK_DEVICE_NAME_DB_H #include "btstack_util.h" #include "gap.h" @@ -56,69 +56,18 @@ typedef struct { // management void (*open)(void); void (*close)(void); - - // link key - int (*get_link_key)(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type); - void (*put_link_key)(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t type); - void (*delete_link_key)(bd_addr_t bd_addr); - + // remote name int (*get_name)(bd_addr_t bd_addr, device_name_t *device_name); void (*put_name)(bd_addr_t bd_addr, device_name_t *device_name); void (*delete_name)(bd_addr_t bd_addr); -} remote_device_db_t; - -/* - * @brief - */ -extern remote_device_db_t remote_device_db_iphone; - -/* - * @brief - */ -extern const remote_device_db_t remote_device_db_memory; - -/* - * @brief - */ -extern const remote_device_db_t remote_device_db_fs; - -const remote_device_db_t * remote_device_db_fs_instance(void); +} btstack_device_name_db_t; /* API_END */ -// MARK: non-persistent implementation -#include "btstack_linked_list.h" -#define MAX_NAME_LEN 32 -typedef struct { - // linked list - assert: first field - btstack_linked_item_t item; - - bd_addr_t bd_addr; -} db_mem_device_t; - -typedef struct { - db_mem_device_t device; - link_key_t link_key; - link_key_type_t link_key_type; -} db_mem_device_link_key_t; - -typedef struct { - db_mem_device_t device; - char device_name[MAX_NAME_LEN]; -} db_mem_device_name_t; - -typedef struct { - // linked list - assert: first field - btstack_linked_item_t item; - - char service_name[MAX_NAME_LEN]; - uint8_t channel; -} db_mem_service_t; - #if defined __cplusplus } #endif -#endif // __REMOTE_DEVICE_DB_H +#endif // __BTSTACK_DEVICE_NAME_DB_H diff --git a/platform/daemon/daemon.c b/platform/daemon/daemon.c index 63a8384da..ee0aebaed 100644 --- a/platform/daemon/daemon.c +++ b/platform/daemon/daemon.c @@ -58,12 +58,13 @@ #include "btstack.h" #include "btstack_client.h" #include "btstack_debug.h" +#include "btstack_device_name_db.h" #include "btstack_event.h" #include "btstack_linked_list.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" #include "btstack_version.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "classic/rfcomm.h" #include "classic/sdp.h" #include "classic/sdp_client.h" @@ -187,7 +188,8 @@ static void (*bluetooth_status_handler)(BLUETOOTH_STATE state) = dummy_bluetooth static int global_enable = 0; -static remote_device_db_t const * remote_device_db = NULL; +static btstack_link_key_db_t const * btstack_link_key_db = NULL; +static btstack_device_name_db_t const * btstack_device_name_db = NULL; // static int rfcomm_channel_generator = 1; static uint8_t attribute_value[1000]; @@ -1513,7 +1515,7 @@ static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16 return; case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: - if (!remote_device_db) break; + if (!btstack_device_name_db) break; if (packet[2]) break; // status not ok bt_flip_addr(addr, &packet[3]); @@ -1525,12 +1527,12 @@ static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16 } } packet[9+248] = 0; - remote_device_db->put_name(addr, (device_name_t *)&packet[9]); + btstack_device_name_db->put_name(addr, (device_name_t *)&packet[9]); break; case HCI_EVENT_INQUIRY_RESULT: case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ - if (!remote_device_db) break; + if (!btstack_device_name_db) break; // first send inq result packet daemon_emit_packet(connection, packet_type, channel, packet, size); @@ -1539,7 +1541,7 @@ static void daemon_packet_handler(void * connection, uint8_t packet_type, uint16 int offset = 3; for (i=0; iget_name(addr, (device_name_t *) &remote_name_event[9])){ + if (btstack_device_name_db->get_name(addr, (device_name_t *) &remote_name_event[9])){ remote_name_event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; remote_name_event[1] = sizeof(remote_name_event) - 2 - 1; remote_name_event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE @@ -2015,8 +2017,8 @@ int main (int argc, char * const * argv){ platform_iphone_register_preferences_changed(preferences_changed_callback); #endif -#ifdef REMOTE_DEVICE_DB - remote_device_db = &REMOTE_DEVICE_DB; +#ifdef btstack_link_key_db + btstack_link_key_db = &btstack_link_key_db; #endif btstack_run_loop_init(btstack_run_loop_posix_get_instance()); @@ -2040,7 +2042,7 @@ int main (int argc, char * const * argv){ log_info("version %s, build %s", BTSTACK_VERSION, BTSTACK_DATE); // init HCI - hci_init(transport, config, remote_device_db); + hci_init(transport, config, btstack_link_key_db); if (control){ hci_set_control(control); } diff --git a/platform/posix/remote_device_db_fs.c b/platform/posix/remote_device_db_fs.c deleted file mode 100644 index 8b27bf331..000000000 --- a/platform/posix/remote_device_db_fs.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) 2014 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 - * - */ - -#include -#include -#include -#include - -#include "classic/remote_device_db.h" -#include "btstack_debug.h" - -#include "btstack_util.h" - -#define LINK_KEY_PATH "/tmp/" -#define LINK_KEY_PREFIX "btstack_link_key_" -#define LINK_KEY_SUFIX ".txt" - -static char keypath[sizeof(LINK_KEY_PATH) + sizeof(LINK_KEY_PREFIX) + 17 + sizeof(LINK_KEY_SUFIX) + 1]; - -static char bd_addr_to_dash_str_buffer[6*3]; // 12-45-78-01-34-67\0 -static char * bd_addr_to_dash_str(bd_addr_t addr){ - char * p = bd_addr_to_dash_str_buffer; - int i; - for (i = 0; i < 6 ; i++) { - *p++ = char_for_nibble((addr[i] >> 4) & 0x0F); - *p++ = char_for_nibble((addr[i] >> 0) & 0x0F); - *p++ = '-'; - } - *--p = 0; - return (char *) bd_addr_to_dash_str_buffer; -} - -static void set_path(bd_addr_t bd_addr){ - strcpy(keypath, LINK_KEY_PATH); - strcat(keypath, LINK_KEY_PREFIX); - strcat(keypath, bd_addr_to_dash_str(bd_addr)); - strcat(keypath, LINK_KEY_SUFIX); -} - -// Device info -static void db_open(void){ -} - -static void db_close(void){ -} - -static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ - set_path(bd_addr); - char * link_key_str = link_key_to_str(link_key); - char * link_key_type_str = link_key_type_to_str(link_key_type); - - FILE * wFile = fopen(keypath,"w+"); - fwrite(link_key_str, strlen(link_key_str), 1, wFile); - fwrite(link_key_type_str, strlen(link_key_type_str), 1, wFile); - fclose(wFile); -} - -static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) { - set_path(bd_addr); - if (access(keypath, R_OK)) return 0; - - char link_key_str[LINK_KEY_STR_LEN + 1]; - char link_key_type_str[2]; - - FILE * rFile = fopen(keypath,"r+"); - size_t objects_read = fread(link_key_str, LINK_KEY_STR_LEN, 1, rFile ); - if (objects_read == 1){ - link_key_str[LINK_KEY_STR_LEN] = 0; - log_info("Found link key %s\n", link_key_str); - objects_read = fread(link_key_type_str, 1, 1, rFile ); - } - fclose(rFile); - - if (objects_read != 1) return 0; - link_key_type_str[1] = 0; - log_info("Found link key type %s\n", link_key_type_str); - - int scan_result = sscan_link_key(link_key_str, link_key); - if (scan_result == 0 ) return 0; - - int link_key_type_buffer; - scan_result = sscanf( (char *) link_key_type_str, "%d", &link_key_type_buffer); - if (scan_result == 0 ) return 0; - *link_key_type = (link_key_type_t) link_key_type_buffer; - return 1; -} - -static void delete_link_key(bd_addr_t bd_addr){ - set_path(bd_addr); - if (access(keypath, R_OK)) return; - - if(remove(keypath) != 0){ - log_error("File %s could not be deleted.\n", keypath); - } -} - -static int get_name(bd_addr_t bd_addr, device_name_t *device_name) { - return 0; -} - -static void delete_name(bd_addr_t bd_addr){ -} - -static void put_name(bd_addr_t bd_addr, device_name_t *device_name){ -} - -const remote_device_db_t remote_device_db_fs = { - db_open, - db_close, - get_link_key, - put_link_key, - delete_link_key, - get_name, - put_name, - delete_name -}; - -const remote_device_db_t * remote_device_db_fs_instance(void){ - return &remote_device_db_fs; -} diff --git a/port/daemon/configure.ac b/port/daemon/configure.ac index 8eb12f306..ec69e04fe 100644 --- a/port/daemon/configure.ac +++ b/port/daemon/configure.ac @@ -65,8 +65,8 @@ case "$host_os" in LDFLAGS="$LDFLAGS -framework CoreFoundation -framework Foundation" BTSTACK_LIB_LDFLAGS="-dynamiclib -install_name \$(prefix)/lib/libBTstack.dylib" BTSTACK_LIB_EXTENSION="dylib" - REMOTE_DEVICE_DB_SOURCES="remote_device_db_cocoa.m rfcomm_service_db_cocoa.m" - REMOTE_DEVICE_DB="remote_device_db_iphone" + REMOTE_DEVICE_DB_SOURCES="btstack_link_key_db_cocoa.m rfcomm_service_db_cocoa.m" + REMOTE_DEVICE_DB="remote_device_db_cocoa" HAVE_SO_NOSIGPIPE="yes"; ;; mingw*) @@ -74,14 +74,14 @@ case "$host_os" in LDFLAGS="$LDFLAGS -lws2_32" BTSTACK_LIB_LDFLAGS="-shared" BTSTACK_LIB_EXTENSION="dll" - REMOTE_DEVICE_DB_SOURCES="remote_device_db_memory.c rfcomm_service_db_memory.c" - REMOTE_DEVICE_DB="remote_device_db_memory" + REMOTE_DEVICE_DB_SOURCES="btstack_link_key_db_memory.c rfcomm_service_db_memory.c" + REMOTE_DEVICE_DB="btstack_link_key_db_memory" ;; *) BTSTACK_LIB_LDFLAGS="-shared -Wl,-rpath,\$(prefix)/lib" BTSTACK_LIB_EXTENSION="so" - REMOTE_DEVICE_DB_SOURCES="remote_device_db_memory.c rfcomm_service_db_memory.c" - REMOTE_DEVICE_DB="remote_device_db_memory" + REMOTE_DEVICE_DB_SOURCES="btstack_link_key_db_memory.c rfcomm_service_db_memory.c" + REMOTE_DEVICE_DB="btstack_link_key_db_memory" ;; esac diff --git a/port/ez430-rf2560/example/ant_test.c b/port/ez430-rf2560/example/ant_test.c index 795da0ce9..bd6e80bc1 100644 --- a/port/ez430-rf2560/example/ant_test.c +++ b/port/ez430-rf2560/example/ant_test.c @@ -65,7 +65,7 @@ #include "hci.h" #include "l2cap.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "classic/rfcomm.h" #include "classic/sdp.h" #include "btstack_config.h" diff --git a/port/ez430-rf2560/example/spp_accel.c b/port/ez430-rf2560/example/spp_accel.c index 6f6797ce3..7fc4f6521 100644 --- a/port/ez430-rf2560/example/spp_accel.c +++ b/port/ez430-rf2560/example/spp_accel.c @@ -61,7 +61,7 @@ #include "hci.h" #include "l2cap.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "classic/rfcomm.h" #include "classic/sdp.h" #include "btstack_config.h" diff --git a/port/ez430-rf2560/src/main.c b/port/ez430-rf2560/src/main.c index d942f71bb..2d450b03f 100644 --- a/port/ez430-rf2560/src/main.c +++ b/port/ez430-rf2560/src/main.c @@ -60,7 +60,7 @@ #include "hci.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "btstack_config.h" int btstack_main(int argc, const char * argv[]); diff --git a/port/libusb/Makefile b/port/libusb/Makefile index bd88a0a28..972b50548 100644 --- a/port/libusb/Makefile +++ b/port/libusb/Makefile @@ -3,7 +3,7 @@ BTSTACK_ROOT = ../.. CORE += main.c stdin_support.c -COMMON += hci_transport_h2_libusb.c btstack_run_loop_posix.c remote_device_db_fs.c +COMMON += hci_transport_h2_libusb.c btstack_run_loop_posix.c btstack_link_key_db_fs.c include ${BTSTACK_ROOT}/example/embedded/Makefile.inc diff --git a/port/libusb/main.c b/port/libusb/main.c index d37051677..d8b1933fd 100644 --- a/port/libusb/main.c +++ b/port/libusb/main.c @@ -49,12 +49,12 @@ #include "btstack_config.h" +#include "btstack_debug.h" +#include "btstack_link_key_db_fs.h" +#include "btstack_memory.h" #include "btstack_run_loop.h" #include "btstack_run_loop_posix.h" #include "hal_led.h" - -#include "btstack_debug.h" -#include "btstack_memory.h" #include "hci.h" #include "hci_dump.h" #include "stdin_support.h" @@ -92,8 +92,7 @@ int main(int argc, const char * argv[]){ // init HCI const hci_transport_t * transport = hci_transport_usb_instance(); - remote_device_db_t * remote_db = (remote_device_db_t *) &remote_device_db_fs; - hci_init(transport, NULL, remote_db); + hci_init(transport, NULL, btstack_link_key_db_fs_instance()); // handle CTRL-c signal(SIGINT, sigint_handler); diff --git a/port/msp-exp430f5438-cc2564b/example/ant_test.c b/port/msp-exp430f5438-cc2564b/example/ant_test.c index 388b02862..c0da210ce 100644 --- a/port/msp-exp430f5438-cc2564b/example/ant_test.c +++ b/port/msp-exp430f5438-cc2564b/example/ant_test.c @@ -65,7 +65,7 @@ #include "hci.h" #include "l2cap.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "classic/rfcomm.h" #include "classic/sdp.h" #include "btstack_config.h" diff --git a/port/msp-exp430f5438-cc2564b/example/spp_accel.c b/port/msp-exp430f5438-cc2564b/example/spp_accel.c index d299ede84..51bb0e2d6 100644 --- a/port/msp-exp430f5438-cc2564b/example/spp_accel.c +++ b/port/msp-exp430f5438-cc2564b/example/spp_accel.c @@ -61,7 +61,7 @@ #include "hci.h" #include "l2cap.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "classic/rfcomm.h" #include "classic/sdp.h" #include "btstack_config.h" diff --git a/port/msp-exp430f5438-cc2564b/src/main.c b/port/msp-exp430f5438-cc2564b/src/main.c index be92f6543..97d342b70 100644 --- a/port/msp-exp430f5438-cc2564b/src/main.c +++ b/port/msp-exp430f5438-cc2564b/src/main.c @@ -59,7 +59,7 @@ #include "hci.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "btstack_config.h" static void hw_setup(void){ diff --git a/port/msp430f5229lp-cc2564b/src/main.c b/port/msp430f5229lp-cc2564b/src/main.c index a50f4c93d..260cac4ab 100644 --- a/port/msp430f5229lp-cc2564b/src/main.c +++ b/port/msp430f5229lp-cc2564b/src/main.c @@ -60,7 +60,7 @@ #include "hci.h" #include "hci_dump.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "btstack_config.h" static void hw_setup(void){ diff --git a/port/stm32-f103rb-nucleo/main.c b/port/stm32-f103rb-nucleo/main.c index 970df8af3..5af4a45f2 100644 --- a/port/stm32-f103rb-nucleo/main.c +++ b/port/stm32-f103rb-nucleo/main.c @@ -53,7 +53,7 @@ #include "hci.h" #include "btstack_chipset_cc256x.h" #include "btstack_memory.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" // STDOUT_FILENO and STDERR_FILENO are defined by with GCC // (this is a hack for IAR) diff --git a/src/btstack.h b/src/btstack.h index 148b700a8..46a87447d 100644 --- a/src/btstack.h +++ b/src/btstack.h @@ -84,7 +84,7 @@ #include "classic/hsp_ag.h" #include "classic/hsp_hs.h" #include "classic/pan.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "classic/rfcomm.h" #include "classic/sdp.h" #include "classic/sdp_client.h" diff --git a/src/btstack_memory.c b/src/btstack_memory.c index 9db584df8..23805e8ee 100644 --- a/src/btstack_memory.c +++ b/src/btstack_memory.c @@ -37,11 +37,11 @@ /* - * btstsack_memory.h + * btstack_memory.h * * @brief BTstack memory management via configurable memory pools * - * @note code semi-atuomatically generated by tools/btstack_memory_generator.py + * @note code generated by tool/btstack_memory_generator.py * */ @@ -247,99 +247,35 @@ void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){ -// MARK: db_mem_device_name_t -#ifdef MAX_NO_DB_MEM_DEVICE_NAMES -#if MAX_NO_DB_MEM_DEVICE_NAMES > 0 -static db_mem_device_name_t db_mem_device_name_storage[MAX_NO_DB_MEM_DEVICE_NAMES]; -static btstack_memory_pool_t db_mem_device_name_pool; -db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ - return (db_mem_device_name_t *) btstack_memory_pool_get(&db_mem_device_name_pool); +// MARK: btstack_link_key_db_memory_t +#ifdef MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS +#if MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS > 0 +static btstack_link_key_db_memory_t btstack_link_key_db_memory_storage[MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS]; +static btstack_memory_pool_t btstack_link_key_db_memory_pool; +btstack_link_key_db_memory_t * btstack_memory_btstack_link_key_db_memory_get(void){ + return (btstack_link_key_db_memory_t *) btstack_memory_pool_get(&btstack_link_key_db_memory_pool); } -void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ - btstack_memory_pool_free(&db_mem_device_name_pool, db_mem_device_name); +void btstack_memory_btstack_link_key_db_memory_free(btstack_link_key_db_memory_t *btstack_link_key_db_memory){ + btstack_memory_pool_free(&btstack_link_key_db_memory_pool, btstack_link_key_db_memory); } #else -db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ +btstack_link_key_db_memory_t * btstack_memory_btstack_link_key_db_memory_get(void){ return NULL; } -void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ +void btstack_memory_btstack_link_key_db_memory_free(btstack_link_key_db_memory_t *btstack_link_key_db_memory){ // silence compiler warning about unused parameter in a portable way - (void) db_mem_device_name; + (void) btstack_link_key_db_memory; }; #endif #elif defined(HAVE_MALLOC) -db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){ - return (db_mem_device_name_t*) malloc(sizeof(db_mem_device_name_t)); +btstack_link_key_db_memory_t * btstack_memory_btstack_link_key_db_memory_get(void){ + return (btstack_link_key_db_memory_t*) malloc(sizeof(btstack_link_key_db_memory_t)); } -void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){ - free(db_mem_device_name); +void btstack_memory_btstack_link_key_db_memory_free(btstack_link_key_db_memory_t *btstack_link_key_db_memory){ + free(btstack_link_key_db_memory); } #else -#error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_NAMES for struct db_mem_device_name is defined. Please, edit the config file." -#endif - - -// MARK: db_mem_device_link_key_t -#ifdef MAX_NO_DB_MEM_DEVICE_LINK_KEYS -#if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 -static db_mem_device_link_key_t db_mem_device_link_key_storage[MAX_NO_DB_MEM_DEVICE_LINK_KEYS]; -static btstack_memory_pool_t db_mem_device_link_key_pool; -db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ - return (db_mem_device_link_key_t *) btstack_memory_pool_get(&db_mem_device_link_key_pool); -} -void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ - btstack_memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key); -} -#else -db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ - return NULL; -} -void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ - // silence compiler warning about unused parameter in a portable way - (void) db_mem_device_link_key; -}; -#endif -#elif defined(HAVE_MALLOC) -db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void){ - return (db_mem_device_link_key_t*) malloc(sizeof(db_mem_device_link_key_t)); -} -void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key){ - free(db_mem_device_link_key); -} -#else -#error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_DEVICE_LINK_KEYS for struct db_mem_device_link_key is defined. Please, edit the config file." -#endif - - -// MARK: db_mem_service_t -#ifdef MAX_NO_DB_MEM_SERVICES -#if MAX_NO_DB_MEM_SERVICES > 0 -static db_mem_service_t db_mem_service_storage[MAX_NO_DB_MEM_SERVICES]; -static btstack_memory_pool_t db_mem_service_pool; -db_mem_service_t * btstack_memory_db_mem_service_get(void){ - return (db_mem_service_t *) btstack_memory_pool_get(&db_mem_service_pool); -} -void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ - btstack_memory_pool_free(&db_mem_service_pool, db_mem_service); -} -#else -db_mem_service_t * btstack_memory_db_mem_service_get(void){ - return NULL; -} -void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ - // silence compiler warning about unused parameter in a portable way - (void) db_mem_service; -}; -#endif -#elif defined(HAVE_MALLOC) -db_mem_service_t * btstack_memory_db_mem_service_get(void){ - return (db_mem_service_t*) malloc(sizeof(db_mem_service_t)); -} -void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){ - free(db_mem_service); -} -#else -#error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES for struct db_mem_service is defined. Please, edit the config file." +#error "Neither HAVE_MALLOC nor MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS for struct btstack_link_key_db_memory is defined. Please, edit the config file." #endif @@ -625,14 +561,8 @@ void btstack_memory_init(void){ #if MAX_NO_RFCOMM_CHANNELS > 0 btstack_memory_pool_create(&rfcomm_channel_pool, rfcomm_channel_storage, MAX_NO_RFCOMM_CHANNELS, sizeof(rfcomm_channel_t)); #endif -#if MAX_NO_DB_MEM_DEVICE_NAMES > 0 - btstack_memory_pool_create(&db_mem_device_name_pool, db_mem_device_name_storage, MAX_NO_DB_MEM_DEVICE_NAMES, sizeof(db_mem_device_name_t)); -#endif -#if MAX_NO_DB_MEM_DEVICE_LINK_KEYS > 0 - btstack_memory_pool_create(&db_mem_device_link_key_pool, db_mem_device_link_key_storage, MAX_NO_DB_MEM_DEVICE_LINK_KEYS, sizeof(db_mem_device_link_key_t)); -#endif -#if MAX_NO_DB_MEM_SERVICES > 0 - btstack_memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t)); +#if MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS > 0 + btstack_memory_pool_create(&btstack_link_key_db_memory_pool, btstack_link_key_db_memory_storage, MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS, sizeof(btstack_link_key_db_memory_t)); #endif #if MAX_NO_BNEP_SERVICES > 0 btstack_memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NO_BNEP_SERVICES, sizeof(bnep_service_t)); diff --git a/src/btstack_memory.h b/src/btstack_memory.h index f3b536271..b488c0751 100644 --- a/src/btstack_memory.h +++ b/src/btstack_memory.h @@ -60,7 +60,8 @@ extern "C" { // Classic #include "classic/bnep.h" #include "classic/hfp.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" +#include "classic/btstack_link_key_db_memory.h" #include "classic/rfcomm.h" #include "classic/sdp.h" @@ -97,13 +98,9 @@ void btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service); rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void); void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel); -// db_mem_device_name, db_mem_device_link_key, db_mem_service -db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void); -void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name); -db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void); -void btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key); -db_mem_service_t * btstack_memory_db_mem_service_get(void); -void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service); +// btstack_link_key_db_memory +btstack_link_key_db_memory_t * btstack_memory_btstack_link_key_db_memory_get(void); +void btstack_memory_btstack_link_key_db_memory_free(btstack_link_key_db_memory_t *btstack_link_key_db_memory); // bnep_service, bnep_channel bnep_service_t * btstack_memory_bnep_service_get(void); diff --git a/src/classic/btstack_link_key_db.h b/src/classic/btstack_link_key_db.h new file mode 100644 index 000000000..73c8545ae --- /dev/null +++ b/src/classic/btstack_link_key_db.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2014 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 + * + */ + +/** + * interface to provide link key storage + */ + +#ifndef __BTSTACK_LINK_KEY_DB_H +#define __BTSTACK_LINK_KEY_DB_H + +#include "btstack_util.h" +#include "gap.h" + +#if defined __cplusplus +extern "C" { +#endif + +/* API_START */ + +typedef struct { + + // management + void (*open)(void); + void (*close)(void); + + // link key + int (*get_link_key)(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type); + void (*put_link_key)(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t type); + void (*delete_link_key)(bd_addr_t bd_addr); + +} btstack_link_key_db_t; + +/* API_END */ + +#if defined __cplusplus +} +#endif + +#endif // __BTSTACK_LINK_KEY_DB_H diff --git a/src/classic/remote_device_db_memory.c b/src/classic/btstack_link_key_db_memory.c similarity index 58% rename from src/classic/remote_device_db_memory.c rename to src/classic/btstack_link_key_db_memory.c index 247125c6e..0e27e9190 100644 --- a/src/classic/remote_device_db_memory.c +++ b/src/classic/btstack_link_key_db_memory.c @@ -38,16 +38,15 @@ #include #include -#include "classic/remote_device_db.h" -#include "btstack_memory.h" +#include "classic/btstack_link_key_db_memory.h" + #include "btstack_debug.h" - -#include "btstack_util.h" #include "btstack_linked_list.h" +#include "btstack_memory.h" +#include "btstack_util.h" -// This lists should be only accessed by tests. +// This list should be directly accessed only by tests btstack_linked_list_t db_mem_link_keys = NULL; -btstack_linked_list_t db_mem_names = NULL; // Device info static void db_open(void){ @@ -56,10 +55,10 @@ static void db_open(void){ static void db_close(void){ } -static db_mem_device_t * get_item(btstack_linked_list_t list, bd_addr_t bd_addr) { +static btstack_link_key_db_memory_t * get_item(btstack_linked_list_t list, bd_addr_t bd_addr) { btstack_linked_item_t *it; for (it = (btstack_linked_item_t *) list; it ; it = it->next){ - db_mem_device_t * item = (db_mem_device_t *) it; + btstack_link_key_db_memory_t * item = (btstack_link_key_db_memory_t *) it; if (BD_ADDR_CMP(item->bd_addr, bd_addr) == 0) { return item; } @@ -67,21 +66,8 @@ static db_mem_device_t * get_item(btstack_linked_list_t list, bd_addr_t bd_addr) return NULL; } -static int get_name(bd_addr_t bd_addr, device_name_t *device_name) { - db_mem_device_name_t * item = (db_mem_device_name_t *) get_item(db_mem_names, bd_addr); - - if (!item) return 0; - - strncpy((char*)device_name, item->device_name, MAX_NAME_LEN); - - btstack_linked_list_remove(&db_mem_names, (btstack_linked_item_t *) item); - btstack_linked_list_add(&db_mem_names, (btstack_linked_item_t *) item); - - return 1; -} - static int get_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 * item = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr); + btstack_link_key_db_memory_t * item = get_item(db_mem_link_keys, bd_addr); if (!item) return 0; @@ -96,31 +82,31 @@ static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t } static void delete_link_key(bd_addr_t bd_addr){ - db_mem_device_t * item = get_item(db_mem_link_keys, bd_addr); + btstack_link_key_db_memory_t * item = get_item(db_mem_link_keys, bd_addr); if (!item) return; btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t *) item); - btstack_memory_db_mem_device_link_key_free((db_mem_device_link_key_t*)item); + btstack_memory_btstack_link_key_db_memory_free((btstack_link_key_db_memory_t*)item); } static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){ // check for existing record and remove if found - db_mem_device_link_key_t * record = (db_mem_device_link_key_t *) get_item(db_mem_link_keys, bd_addr); + btstack_link_key_db_memory_t * record = get_item(db_mem_link_keys, bd_addr); if (record){ btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t*) record); } // record not found, get new one from memory pool if (!record) { - record = btstack_memory_db_mem_device_link_key_get(); + record = btstack_memory_btstack_link_key_db_memory_get(); } // if none left, re-use last item and remove from list if (!record){ - record = (db_mem_device_link_key_t*)btstack_linked_list_get_last_item(&db_mem_link_keys); + record = (btstack_link_key_db_memory_t*) btstack_linked_list_get_last_item(&db_mem_link_keys); if (record) { btstack_linked_list_remove(&db_mem_link_keys, (btstack_linked_item_t*) record); } @@ -128,56 +114,22 @@ static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t if (!record) return; - memcpy(record->device.bd_addr, bd_addr, sizeof(bd_addr_t)); + memcpy(record->bd_addr, bd_addr, sizeof(bd_addr_t)); memcpy(record->link_key, link_key, LINK_KEY_LEN); record->link_key_type = link_key_type; btstack_linked_list_add(&db_mem_link_keys, (btstack_linked_item_t *) record); } -static void delete_name(bd_addr_t bd_addr){ - db_mem_device_t * item = get_item(db_mem_names, bd_addr); - - if (!item) return; - - btstack_linked_list_remove(&db_mem_names, (btstack_linked_item_t *) item); - btstack_memory_db_mem_device_name_free((db_mem_device_name_t*)item); -} - -static void put_name(bd_addr_t bd_addr, device_name_t *device_name){ - - // 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){ - btstack_linked_list_remove(&db_mem_names, (btstack_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*)btstack_linked_list_get_last_item(&db_mem_names); - if (record) { - btstack_linked_list_remove(&db_mem_names, (btstack_linked_item_t*) record); - } - } - - if (!record) return; - - memcpy(record->device.bd_addr, bd_addr, sizeof(bd_addr_t)); - strncpy(record->device_name, (const char*) device_name, MAX_NAME_LEN); - btstack_linked_list_add(&db_mem_names, (btstack_linked_item_t *) record); -} - -const remote_device_db_t remote_device_db_memory = { +const btstack_link_key_db_t btstack_link_key_db_memory = { db_open, db_close, get_link_key, put_link_key, delete_link_key, - get_name, - put_name, - delete_name, }; + +const btstack_link_key_db_t * btstack_link_key_db_memory_instance(void){ + return &btstack_link_key_db_memory; +} + + diff --git a/src/classic/btstack_link_key_db_memory.h b/src/classic/btstack_link_key_db_memory.h new file mode 100644 index 000000000..d8bca24d2 --- /dev/null +++ b/src/classic/btstack_link_key_db_memory.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2014 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 + * + */ + +/** + * interface to provide link key storage + */ + +#ifndef __BTSTACK_LINK_KEY_DB_MEMORY_H +#define __BTSTACK_LINK_KEY_DB_MEMORY_H + +#include "btstack_link_key_db.h" +#include "btstack_linked_list.h" + +#if defined __cplusplus +extern "C" { +#endif + +/* + * @brief + */ +const btstack_link_key_db_t * btstack_link_key_db_memory_instance(void); + +typedef struct { + btstack_linked_item_t item; + bd_addr_t bd_addr; + link_key_t link_key; + link_key_type_t link_key_type; +} btstack_link_key_db_memory_t; + +/* API_END */ + +#if defined __cplusplus +} +#endif + +#endif // __BTSTACK_LINK_KEY_DB_MEMORY_H diff --git a/src/hci.c b/src/hci.c index 208d9dcb1..a4ef70475 100644 --- a/src/hci.c +++ b/src/hci.c @@ -259,8 +259,8 @@ int hci_authentication_active_for_handle(hci_con_handle_t handle){ } void hci_drop_link_key_for_bd_addr(bd_addr_t addr){ - if (hci_stack->remote_device_db) { - hci_stack->remote_device_db->delete_link_key(addr); + if (hci_stack->link_key_db) { + hci_stack->link_key_db->delete_link_key(addr); } } @@ -1542,7 +1542,7 @@ static void event_handler(uint8_t *packet, int size){ log_info("HCI_EVENT_LINK_KEY_REQUEST"); hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST - if (hci_stack->bondable && !hci_stack->remote_device_db) break; + if (hci_stack->bondable && !hci_stack->link_key_db) break; hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); hci_run(); // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set @@ -1558,8 +1558,8 @@ static void event_handler(uint8_t *packet, int size){ if (link_key_type != CHANGED_COMBINATION_KEY){ conn->link_key_type = link_key_type; } - if (!hci_stack->remote_device_db) break; - hci_stack->remote_device_db->put_link_key(addr, &packet[8], conn->link_key_type); + if (!hci_stack->link_key_db) break; + hci_stack->link_key_db->put_link_key(addr, &packet[8], conn->link_key_type); // still forward event to allow dismiss of pairing dialog break; } @@ -1573,9 +1573,9 @@ static void event_handler(uint8_t *packet, int size){ return; } // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key - if (!hci_stack->remote_device_db) break; + if (!hci_stack->link_key_db) break; bt_flip_addr(addr, &packet[2]); - hci_stack->remote_device_db->delete_link_key(addr); + hci_stack->link_key_db->delete_link_key(addr); break; case HCI_EVENT_IO_CAPABILITY_REQUEST: @@ -1849,7 +1849,7 @@ static void hci_state_reset(void){ hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; } -void hci_init(const hci_transport_t *transport, void *config, remote_device_db_t const* remote_device_db){ +void hci_init(const hci_transport_t *transport, void *config, btstack_link_key_db_t const * link_key_db){ #ifdef HAVE_MALLOC if (!hci_stack) { @@ -1873,9 +1873,9 @@ void hci_init(const hci_transport_t *transport, void *config, remote_device_db_t hci_stack->packet_handler = dummy_handler; // store and open remote device db - hci_stack->remote_device_db = remote_device_db; - if (hci_stack->remote_device_db) { - hci_stack->remote_device_db->open(); + hci_stack->link_key_db = link_key_db; + if (hci_stack->link_key_db) { + hci_stack->link_key_db->open(); } // max acl payload size defined in config.h @@ -1928,8 +1928,8 @@ void hci_set_control(const btstack_control_t *hardware_control){ void hci_close(void){ // close remote device db - if (hci_stack->remote_device_db) { - hci_stack->remote_device_db->close(); + if (hci_stack->link_key_db) { + hci_stack->link_key_db->close(); } while (hci_stack->connections) { // cancel all l2cap connections @@ -2487,8 +2487,8 @@ void hci_run(void){ connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); link_key_t link_key; link_key_type_t link_key_type; - if ( hci_stack->remote_device_db - && hci_stack->remote_device_db->get_link_key(connection->address, link_key, &link_key_type) + if ( hci_stack->link_key_db + && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ connection->link_key_type = link_key_type; hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); @@ -2736,9 +2736,9 @@ int hci_send_cmd_packet(uint8_t *packet, int size){ } if (IS_COMMAND(packet, hci_delete_stored_link_key)){ - if (hci_stack->remote_device_db){ + if (hci_stack->link_key_db){ bt_flip_addr(addr, &packet[3]); - hci_stack->remote_device_db->delete_link_key(addr); + hci_stack->link_key_db->delete_link_key(addr); } } @@ -3107,10 +3107,10 @@ void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_ // TODO: figure out how to use it properly // would enabling ecnryption suffice (>= LEVEL_2)? - if (hci_stack->remote_device_db){ + if (hci_stack->link_key_db){ link_key_type_t link_key_type; link_key_t link_key; - if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ + if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; return; diff --git a/src/hci.h b/src/hci.h index 97ba2acd9..9a568f8e6 100644 --- a/src/hci.h +++ b/src/hci.h @@ -51,7 +51,7 @@ #include "btstack_control.h" #include "btstack_linked_list.h" #include "btstack_util.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" #include "hci_cmd.h" #include "hci_transport.h" @@ -577,8 +577,8 @@ typedef struct { /* callback for SCO data */ void (*sco_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); - /* remote device db */ - remote_device_db_t const*remote_device_db; + /* link key db */ + btstack_link_key_db_t const * link_key_db; /* hci state machine */ HCI_STATE state; @@ -762,7 +762,7 @@ void hci_local_bd_addr(bd_addr_t address_buffer); /** * @brief Set up HCI. Needs to be called before any other function. */ -void hci_init(const hci_transport_t *transport, void *config, remote_device_db_t const* remote_device_db); +void hci_init(const hci_transport_t *transport, void *config, btstack_link_key_db_t const * btstack_link_key_db); /** * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information. diff --git a/test/Makefile b/test/Makefile index 7c64dfdf5..39d2b57d5 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,7 +8,7 @@ SUBDIRS = \ gatt_client \ hfp \ linked_list \ - remote_device_db \ + btstack_link_key_db \ sdp_client \ security_manager \ diff --git a/test/remote_device_db/.gitignore b/test/btstack_link_key_db/.gitignore similarity index 100% rename from test/remote_device_db/.gitignore rename to test/btstack_link_key_db/.gitignore diff --git a/test/btstack_link_key_db/Makefile b/test/btstack_link_key_db/Makefile new file mode 100644 index 000000000..7889c7025 --- /dev/null +++ b/test/btstack_link_key_db/Makefile @@ -0,0 +1,52 @@ +CC=g++ + +# Requirements: cpputest.github.io + +BTSTACK_ROOT = ../.. +CPPUTEST_HOME = ${BTSTACK_ROOT}/test/cpputest + +CFLAGS = -g -Wall \ + -I. \ + -I.. \ + -I${BTSTACK_ROOT}/src \ + -I${BTSTACK_ROOT}/platform/posix + +LDFLAGS += -lCppUTest -lCppUTestExt + +VPATH += ${BTSTACK_ROOT}/src/classic +VPATH += ${BTSTACK_ROOT}/src +VPATH += ${BTSTACK_ROOT}/platform/posix + +FS = \ + btstack_util.c \ + hci_dump.c \ + btstack_link_key_db_fs.c + + +MEMORY = \ + btstack_util.c \ + btstack_memory_pool.c \ + btstack_memory.c \ + hci_dump.c \ + btstack_link_key_db_memory.c \ + btstack_linked_list.c + +FS_OBJ = $(FS:.c=.o) +MEMORY_OBJ = $(MEMORY:.c=.o) + +all: btstack_link_key_db_memory_test btstack_link_key_db_fs_test + +btstack_link_key_db_memory_test: ${MEMORY_OBJ} btstack_link_key_db_memory_test.c + ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ + +btstack_link_key_db_fs_test: ${FS_OBJ} btstack_link_key_db_fs_test.c + ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ + +test: all + ./btstack_link_key_db_memory_test + ./btstack_link_key_db_fs_test + +clean: + rm -f btstack_link_key_db_memory_test btstack_link_key_db_fs_test *.o ../src/*.o + rm -rf *.dSYM + diff --git a/test/remote_device_db/btstack_config.h b/test/btstack_link_key_db/btstack_config.h similarity index 92% rename from test/remote_device_db/btstack_config.h rename to test/btstack_link_key_db/btstack_config.h index be0b410ae..7ac94afe2 100644 --- a/test/remote_device_db/btstack_config.h +++ b/test/btstack_link_key_db/btstack_config.h @@ -33,8 +33,7 @@ #define MAX_NO_RFCOMM_CHANNELS 0 #define MAX_NO_BNEP_SERVICES 0 #define MAX_NO_BNEP_CHANNELS 0 -#define MAX_NO_DB_MEM_DEVICE_LINK_KEYS 2 -#define MAX_NO_DB_MEM_DEVICE_NAMES 2 +#define MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS 2 #define MAX_NO_DB_MEM_SERVICES 0 #define MAX_NO_HFP_CONNECTIONS 0 #define MAX_NO_WHITELIST_ENTRIES 0 diff --git a/test/remote_device_db/remote_device_db_fs_test.c b/test/btstack_link_key_db/btstack_link_key_db_fs_test.c similarity index 58% rename from test/remote_device_db/remote_device_db_fs_test.c rename to test/btstack_link_key_db/btstack_link_key_db_fs_test.c index ce30170e4..5b1abddcb 100644 --- a/test/remote_device_db/remote_device_db_fs_test.c +++ b/test/btstack_link_key_db/btstack_link_key_db_fs_test.c @@ -4,12 +4,11 @@ #include "CppUTest/TestHarness.h" #include "CppUTest/CommandLineTestRunner.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" +#include "btstack_link_key_db_fs.h" #include "btstack_config.h" -const remote_device_db_t * remote_device_db_fs_instance(); - TEST_GROUP(RemoteDeviceDB){ bd_addr_t bd_addr; link_key_t link_key; @@ -31,16 +30,16 @@ TEST(RemoteDeviceDB, SinglePutGetDeleteKey){ link_key_t test_link_key; link_key_type_t test_link_key_type; - remote_device_db_fs_instance()->delete_link_key(bd_addr); - CHECK(remote_device_db_fs_instance()->get_link_key(bd_addr, test_link_key, &test_link_key_type) == 0); + btstack_link_key_db_fs_instance()->delete_link_key(bd_addr); + CHECK(btstack_link_key_db_fs_instance()->get_link_key(bd_addr, test_link_key, &test_link_key_type) == 0); - remote_device_db_fs_instance()->put_link_key(bd_addr, link_key, link_key_type); - CHECK(remote_device_db_fs_instance()->get_link_key(bd_addr, test_link_key, &test_link_key_type) == 1); + btstack_link_key_db_fs_instance()->put_link_key(bd_addr, link_key, link_key_type); + CHECK(btstack_link_key_db_fs_instance()->get_link_key(bd_addr, test_link_key, &test_link_key_type) == 1); CHECK(strcmp(link_key_to_str(link_key), link_key_to_str(test_link_key)) == 0); - remote_device_db_fs_instance()->delete_link_key(bd_addr); - CHECK(remote_device_db_fs_instance()->get_link_key(bd_addr, test_link_key, &test_link_key_type) == 0); + btstack_link_key_db_fs_instance()->delete_link_key(bd_addr); + CHECK(btstack_link_key_db_fs_instance()->get_link_key(bd_addr, test_link_key, &test_link_key_type) == 0); } diff --git a/test/btstack_link_key_db/btstack_link_key_db_memory_test.c b/test/btstack_link_key_db/btstack_link_key_db_memory_test.c new file mode 100644 index 000000000..8e8bd37ce --- /dev/null +++ b/test/btstack_link_key_db/btstack_link_key_db_memory_test.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include "CppUTest/TestHarness.h" +#include "CppUTest/CommandLineTestRunner.h" + +#include "classic/btstack_link_key_db.h" +#include "btstack_memory.h" + +#include "btstack_config.h" + +extern btstack_linked_list_t db_mem_link_keys ; +extern btstack_linked_list_t db_mem_names ; +// const extern "C" db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void); +// const extern "C" void btstack_memory_init(void); + +void dump(btstack_linked_list_t list){ + printf("dump:\n"); + + int i; + btstack_linked_item_t *it; + for (it = (btstack_linked_item_t *) list, i = 1; it ; it = it->next, i++){ + // btstack_link_key_db_memory_t * item = (btstack_link_key_db_memory_t *) it; + // printf("%u. %s + %u\n", i, item->link_key, item->bd_addr[5]); + // TODO printf broken + } +} + + +TEST_GROUP(RemoteDeviceDB){ + bd_addr_t addr1, addr2, addr3; + device_name_t device_name; + link_key_t link_key; + link_key_type_t link_key_type; + + void setup(void){ + btstack_memory_init(); + + bd_addr_t addr_1 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }; + bd_addr_t addr_2 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x02 }; + bd_addr_t addr_3 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x03 }; + BD_ADDR_COPY(addr1, addr_1); + BD_ADDR_COPY(addr2, addr_2); + BD_ADDR_COPY(addr3, addr_3); + + link_key_type = (link_key_type_t)4; + sprintf((char*)link_key, "%d", 100); + } + + void teardown(void){} +}; + +TEST(RemoteDeviceDB, MemoryPool){ + CHECK(MAX_NO_BTSTACK_LINK_KEY_DB_MEMORYS == 2); + // void * item = btstack_memory_db_mem_device_name_get(); + // CHECK(item); +} + +// TEST(RemoteDeviceDB, SinglePutGetDeleteName){ +// sprintf((char*)device_name, "%d", 100); +// btstack_link_key_db_memory_instance()->put_name(addr1, &device_name); +// CHECK(btstack_link_key_db_memory_instance()->get_name(addr1, &device_name)); + +// btstack_link_key_db_memory_instance()->delete_name(addr1); +// CHECK(!btstack_link_key_db_memory_instance()->get_name(addr1, &device_name)); +// } + +// TEST(RemoteDeviceDB, SortByLastUsedName){ +// btstack_link_key_db_memory_instance()->put_name(addr1, (device_name_t*) "10"); +// // dump(db_mem_names); +// btstack_link_key_db_memory_instance()->put_name(addr2, (device_name_t*) "20"); +// // dump(db_mem_names); +// btstack_link_key_db_memory_instance()->put_name(addr3, (device_name_t*) "30"); +// // dump(db_mem_names); + +// CHECK(!btstack_link_key_db_memory_instance()->get_name(addr1, &device_name)); +// CHECK(btstack_link_key_db_memory_instance()->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, "20"); +// } + + +TEST(RemoteDeviceDB, SinglePutGetDeleteKey){ + sprintf((char*)link_key, "%d", 100); + btstack_link_key_db_memory_instance()->put_link_key(addr1, link_key, link_key_type); + // dump(db_mem_link_keys); + + CHECK(btstack_link_key_db_memory_instance()->get_link_key(addr1, link_key, &link_key_type)); + + btstack_link_key_db_memory_instance()->delete_link_key(addr1); + CHECK(!btstack_link_key_db_memory_instance()->get_link_key(addr1, link_key, &link_key_type)); +} + +TEST(RemoteDeviceDB, SortByLastUsedKey){ + sprintf((char*)link_key, "%d", 10); + btstack_link_key_db_memory_instance()->put_link_key(addr1, link_key, link_key_type); + // dump(db_mem_link_keys); + sprintf((char*)link_key, "%d", 20); + btstack_link_key_db_memory_instance()->put_link_key(addr2, link_key, link_key_type); + // dump(db_mem_link_keys); + sprintf((char*)link_key, "%d", 30); + btstack_link_key_db_memory_instance()->put_link_key(addr3, link_key, link_key_type); + // dump(db_mem_link_keys); + + CHECK(!btstack_link_key_db_memory_instance()->get_link_key(addr1, link_key, &link_key_type)); + CHECK(btstack_link_key_db_memory_instance()->get_link_key(addr2, link_key, &link_key_type)); + // dump(db_mem_link_keys); + + //get first element of the list + btstack_link_key_db_memory_t * item = (btstack_link_key_db_memory_t *) db_mem_link_keys; + STRCMP_EQUAL((char*)item->link_key, "20"); +} + +int main (int argc, const char * argv[]){ + return CommandLineTestRunner::RunAllTests(argc, argv); +} \ No newline at end of file diff --git a/test/remote_device_db/Makefile b/test/remote_device_db/Makefile deleted file mode 100644 index 6d0358977..000000000 --- a/test/remote_device_db/Makefile +++ /dev/null @@ -1,51 +0,0 @@ -CC=g++ - -# Requirements: cpputest.github.io - -BTSTACK_ROOT = ../.. -CPPUTEST_HOME = ${BTSTACK_ROOT}/test/cpputest - -CFLAGS = -g -Wall \ - -I. \ - -I.. \ - -I${BTSTACK_ROOT}/src - -LDFLAGS += -lCppUTest -lCppUTestExt - -VPATH += ${BTSTACK_ROOT}/src/classic -VPATH += ${BTSTACK_ROOT}/src -VPATH += ${BTSTACK_ROOT}/platform/posix - -FS = \ - btstack_util.c \ - hci_dump.c \ - remote_device_db_fs.c - - -MEMORY = \ - btstack_util.c \ - btstack_memory_pool.c \ - btstack_memory.c \ - hci_dump.c \ - remote_device_db_memory.c \ - btstack_linked_list.c - -FS_OBJ = $(FS:.c=.o) -MEMORY_OBJ = $(MEMORY:.c=.o) - -all: remote_device_db_memory_test remote_device_db_fs_test - -remote_device_db_memory_test: ${MEMORY_OBJ} remote_device_db_memory_test.c - ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ - -remote_device_db_fs_test: ${FS_OBJ} remote_device_db_fs_test.c - ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ - -test: all - ./remote_device_db_memory_test - ./remote_device_db_fs_test - -clean: - rm -f remote_device_db_memory_test remote_device_db_fs_test *.o ../src/*.o - rm -rf *.dSYM - diff --git a/test/remote_device_db/remote_device_db_memory_test.c b/test/remote_device_db/remote_device_db_memory_test.c deleted file mode 100644 index 29a1078c3..000000000 --- a/test/remote_device_db/remote_device_db_memory_test.c +++ /dev/null @@ -1,118 +0,0 @@ -#include -#include -#include -#include "CppUTest/TestHarness.h" -#include "CppUTest/CommandLineTestRunner.h" - -#include "classic/remote_device_db.h" -#include "btstack_memory.h" - -#include "btstack_config.h" - -extern btstack_linked_list_t db_mem_link_keys ; -extern btstack_linked_list_t db_mem_names ; -// const extern "C" db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void); -// const extern "C" void btstack_memory_init(void); - -void dump(btstack_linked_list_t list){ - printf("dump:\n"); - - int i; - btstack_linked_item_t *it; - for (it = (btstack_linked_item_t *) list, i = 1; it ; it = it->next, i++){ - 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_link_key_t * item2 = (db_mem_device_link_key_t *) it; - printf("%u. %s + %s + %u\n", i, item1->device_name, item2->link_key, item->bd_addr[5]); - } -} - - -TEST_GROUP(RemoteDeviceDB){ - bd_addr_t addr1, addr2, addr3; - device_name_t device_name; - link_key_t link_key; - link_key_type_t link_key_type; - - void setup(void){ - btstack_memory_init(); - - bd_addr_t addr_1 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x01 }; - bd_addr_t addr_2 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x02 }; - bd_addr_t addr_3 = {0x00, 0x01, 0x02, 0x03, 0x04, 0x03 }; - BD_ADDR_COPY(addr1, addr_1); - BD_ADDR_COPY(addr2, addr_2); - BD_ADDR_COPY(addr3, addr_3); - - link_key_type = (link_key_type_t)4; - sprintf((char*)link_key, "%d", 100); - } - - void teardown(void){} -}; - -TEST(RemoteDeviceDB, MemoryPool){ - CHECK(MAX_NO_DB_MEM_DEVICE_NAMES > 0); - void * item = btstack_memory_db_mem_device_name_get(); - CHECK(item); -} - -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(addr1); - CHECK(!remote_device_db_memory.get_name(addr1, &device_name)); -} - -TEST(RemoteDeviceDB, SortByLastUsedName){ - remote_device_db_memory.put_name(addr1, (device_name_t*) "10"); - // dump(db_mem_names); - remote_device_db_memory.put_name(addr2, (device_name_t*) "20"); - // dump(db_mem_names); - remote_device_db_memory.put_name(addr3, (device_name_t*) "30"); - // dump(db_mem_names); - - CHECK(!remote_device_db_memory.get_name(addr1, &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, "20"); -} - - -TEST(RemoteDeviceDB, SinglePutGetDeleteKey){ - sprintf((char*)link_key, "%d", 100); - remote_device_db_memory.put_link_key(addr1, link_key, link_key_type); - // dump(db_mem_link_keys); - - CHECK(remote_device_db_memory.get_link_key(addr1, link_key, &link_key_type)); - - remote_device_db_memory.delete_link_key(addr1); - CHECK(!remote_device_db_memory.get_link_key(addr1, link_key, &link_key_type)); -} - -TEST(RemoteDeviceDB, SortByLastUsedKey){ - sprintf((char*)link_key, "%d", 10); - remote_device_db_memory.put_link_key(addr1, link_key, link_key_type); - // dump(db_mem_link_keys); - sprintf((char*)link_key, "%d", 20); - remote_device_db_memory.put_link_key(addr2, link_key, link_key_type); - // dump(db_mem_link_keys); - sprintf((char*)link_key, "%d", 30); - remote_device_db_memory.put_link_key(addr3, link_key, link_key_type); - // 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)); - // dump(db_mem_link_keys); - - //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, "20"); -} - -int main (int argc, const char * argv[]){ - return CommandLineTestRunner::RunAllTests(argc, argv); -} \ No newline at end of file diff --git a/tool/btstack_memory_generator.py b/tool/btstack_memory_generator.py index ab8848389..5487e1086 100755 --- a/tool/btstack_memory_generator.py +++ b/tool/btstack_memory_generator.py @@ -63,7 +63,8 @@ extern "C" { // Classic #include "classic/bnep.h" #include "classic/hfp.h" -#include "classic/remote_device_db.h" +#include "classic/btstack_link_key_db.h" +#include "classic/btstack_link_key_db_memory.h" #include "classic/rfcomm.h" #include "classic/sdp.h" @@ -93,11 +94,11 @@ hfile_header_end = """ cfile_header_begin = """ /* - * btstsack_memory.h + * btstack_memory.h * * @brief BTstack memory management via configurable memory pools * - * @note code semi-atuomatically generated by tools/btstack_memory_generator.py + * @note code generated by tool/btstack_memory_generator.py * */ @@ -165,7 +166,7 @@ list_of_structs = [ ["hci_connection"], ["l2cap_service", "l2cap_channel"], ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], - ["db_mem_device_name", "db_mem_device_link_key", "db_mem_service"], + ["btstack_link_key_db_memory"], ["bnep_service", "bnep_channel"], ["hfp_connection"], ["service_record_item"]