mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-21 12:40:42 +00:00
posix: implement link key iterator for btstack_link_key_db_fs
This commit is contained in:
parent
98cd9557da
commit
d08566fb6e
@ -42,6 +42,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "tinydir.h"
|
||||||
|
|
||||||
#include "btstack_config.h"
|
#include "btstack_config.h"
|
||||||
#include "btstack_link_key_db_fs.h"
|
#include "btstack_link_key_db_fs.h"
|
||||||
#include "btstack_debug.h"
|
#include "btstack_debug.h"
|
||||||
@ -62,7 +64,7 @@
|
|||||||
#define LINK_KEY_STRING_LEN 17
|
#define LINK_KEY_STRING_LEN 17
|
||||||
|
|
||||||
static bd_addr_t local_addr;
|
static bd_addr_t local_addr;
|
||||||
// note: sizeof for string literals works at compile time while strlen only works with some optimizations turned on. sizeof inlcudes the \0
|
// note: sizeof for string literals works at compile time while strlen only works with some optimizations turned on. sizeof includes the \0
|
||||||
static char keypath[sizeof(LINK_KEY_PATH) + sizeof(LINK_KEY_PREFIX) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_FOR) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_SUFFIX) + 1];
|
static char keypath[sizeof(LINK_KEY_PATH) + sizeof(LINK_KEY_PREFIX) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_FOR) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_SUFFIX) + 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_buffer[6*3]; // 12-45-78-01-34-67\0
|
||||||
@ -151,14 +153,13 @@ static void put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t
|
|||||||
fclose(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) {
|
static int read_link_key(const char * path, link_key_t link_key, link_key_type_t * link_key_type){
|
||||||
set_path(bd_addr);
|
if (access(path, R_OK)) return 0;
|
||||||
if (access(keypath, R_OK)) return 0;
|
|
||||||
|
|
||||||
char link_key_str[LINK_KEY_STR_LEN + 1];
|
char link_key_str[LINK_KEY_STR_LEN + 1];
|
||||||
char link_key_type_str[2];
|
char link_key_type_str[2];
|
||||||
|
|
||||||
FILE * rFile = fopen(keypath,"r+");
|
FILE * rFile = fopen(path,"r+");
|
||||||
size_t objects_read = fread(link_key_str, LINK_KEY_STR_LEN, 1, rFile );
|
size_t objects_read = fread(link_key_str, LINK_KEY_STR_LEN, 1, rFile );
|
||||||
if (objects_read == 1){
|
if (objects_read == 1){
|
||||||
link_key_str[LINK_KEY_STR_LEN] = 0;
|
link_key_str[LINK_KEY_STR_LEN] = 0;
|
||||||
@ -181,15 +182,63 @@ static int get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
return read_link_key(keypath, link_key, link_key_type);
|
||||||
|
}
|
||||||
|
|
||||||
static void delete_link_key(bd_addr_t bd_addr){
|
static void delete_link_key(bd_addr_t bd_addr){
|
||||||
set_path(bd_addr);
|
set_path(bd_addr);
|
||||||
if (access(keypath, R_OK)) return;
|
if (access(keypath, R_OK)) return;
|
||||||
|
|
||||||
if(remove(keypath) != 0){
|
if(remove(keypath) != 0){
|
||||||
log_error("File %s could not be deleted.\n", keypath);
|
log_error("File %s could not be deleted.\n", keypath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int iterator_init(btstack_link_key_iterator_t * it){
|
||||||
|
tinydir_dir * dir = malloc(sizeof(tinydir_dir));
|
||||||
|
if (!dir) return 0;
|
||||||
|
it->context = dir;
|
||||||
|
tinydir_open(dir, LINK_KEY_PATH);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type){
|
||||||
|
(void)bd_addr;
|
||||||
|
(void)link_key;
|
||||||
|
UNUSED(type);
|
||||||
|
tinydir_dir * dir = (tinydir_dir*) it->context;
|
||||||
|
|
||||||
|
// construct prefix
|
||||||
|
strcpy(keypath, LINK_KEY_PREFIX);
|
||||||
|
strcat(keypath, bd_addr_to_dash_str(local_addr));
|
||||||
|
strcat(keypath, LINK_KEY_FOR);
|
||||||
|
|
||||||
|
while (dir->has_next) {
|
||||||
|
tinydir_file file;
|
||||||
|
tinydir_readfile(dir, &file);
|
||||||
|
tinydir_next(dir);
|
||||||
|
// compare
|
||||||
|
if (strncmp(keypath, file.name, strlen(keypath)) == 0){
|
||||||
|
// parse bd_addr
|
||||||
|
const int addr_offset = sizeof(LINK_KEY_PREFIX) + LINK_KEY_STRING_LEN + sizeof(LINK_KEY_FOR) - 2; // -1 for each sizeof
|
||||||
|
sscanf_bd_addr(&file.name[addr_offset], bd_addr);
|
||||||
|
// path found, read file
|
||||||
|
strcpy(keypath, LINK_KEY_PATH);
|
||||||
|
strcat(keypath, file.name);
|
||||||
|
read_link_key(keypath, link_key, type);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void iterator_done(btstack_link_key_iterator_t * it){
|
||||||
|
tinydir_close((tinydir_dir*)it->context);
|
||||||
|
free(it->context);
|
||||||
|
it->context = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static const btstack_link_key_db_t btstack_link_key_db_fs = {
|
static const btstack_link_key_db_t btstack_link_key_db_fs = {
|
||||||
&db_open,
|
&db_open,
|
||||||
&db_set_local_bd_addr,
|
&db_set_local_bd_addr,
|
||||||
@ -197,6 +246,9 @@ static const btstack_link_key_db_t btstack_link_key_db_fs = {
|
|||||||
&get_link_key,
|
&get_link_key,
|
||||||
&put_link_key,
|
&put_link_key,
|
||||||
&delete_link_key,
|
&delete_link_key,
|
||||||
|
&iterator_init,
|
||||||
|
&iterator_get_next,
|
||||||
|
&iterator_done,
|
||||||
};
|
};
|
||||||
|
|
||||||
const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void){
|
const btstack_link_key_db_t * btstack_link_key_db_fs_instance(void){
|
||||||
|
@ -11,7 +11,8 @@ CFLAGS += -g -Wall -Wmissing-prototypes -Wstrict-prototypes -Wshadow -Werror -W
|
|||||||
# CFLAGS += -Werror
|
# CFLAGS += -Werror
|
||||||
|
|
||||||
CFLAGS += -I${BTSTACK_ROOT}/platform/posix \
|
CFLAGS += -I${BTSTACK_ROOT}/platform/posix \
|
||||||
-I${BTSTACK_ROOT}/platform/embedded
|
-I${BTSTACK_ROOT}/platform/embedded \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
|
@ -22,6 +22,7 @@ CFLAGS += -g -Wall -Werror \
|
|||||||
-I$(BTSTACK_ROOT)/platform/posix \
|
-I$(BTSTACK_ROOT)/platform/posix \
|
||||||
-I$(BTSTACK_ROOT)/chipset/atwilc3000 \
|
-I$(BTSTACK_ROOT)/chipset/atwilc3000 \
|
||||||
-I$(BTSTACK_ROOT)/platform/embedded \
|
-I$(BTSTACK_ROOT)/platform/embedded \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
VPATH += ${BTSTACK_ROOT}/chipset/atwilc3000
|
VPATH += ${BTSTACK_ROOT}/chipset/atwilc3000
|
||||||
|
@ -20,6 +20,7 @@ CFLAGS += -g -Wall -Werror \
|
|||||||
-I$(BTSTACK_ROOT)/platform/posix \
|
-I$(BTSTACK_ROOT)/platform/posix \
|
||||||
-I$(BTSTACK_ROOT)/chipset/da14581 \
|
-I$(BTSTACK_ROOT)/chipset/da14581 \
|
||||||
-I$(BTSTACK_ROOT)/platform/embedded \
|
-I$(BTSTACK_ROOT)/platform/embedded \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
VPATH += ${BTSTACK_ROOT}/chipset/da14581
|
VPATH += ${BTSTACK_ROOT}/chipset/da14581
|
||||||
|
@ -18,7 +18,8 @@ CFLAGS += -g -Wall -Werror \
|
|||||||
-I$(BTSTACK_ROOT)/platform/embedded \
|
-I$(BTSTACK_ROOT)/platform/embedded \
|
||||||
-I$(BTSTACK_ROOT)/platform/posix \
|
-I$(BTSTACK_ROOT)/platform/posix \
|
||||||
-I$(BTSTACK_ROOT)/chipset/zephyr \
|
-I$(BTSTACK_ROOT)/chipset/zephyr \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
||||||
VPATH += ${BTSTACK_ROOT}/chipset/zephyr
|
VPATH += ${BTSTACK_ROOT}/chipset/zephyr
|
||||||
|
@ -41,6 +41,7 @@ CFLAGS += -g -Wall -Werror \
|
|||||||
-I$(BTSTACK_ROOT)/chipset/em9301 \
|
-I$(BTSTACK_ROOT)/chipset/em9301 \
|
||||||
-I$(BTSTACK_ROOT)/chipset/stlc2500d \
|
-I$(BTSTACK_ROOT)/chipset/stlc2500d \
|
||||||
-I$(BTSTACK_ROOT)/chipset/tc3566x \
|
-I$(BTSTACK_ROOT)/chipset/tc3566x \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
||||||
|
@ -24,6 +24,7 @@ CFLAGS += -g -Wall -Werror \
|
|||||||
-I$(BTSTACK_ROOT)/platform/embedded \
|
-I$(BTSTACK_ROOT)/platform/embedded \
|
||||||
-I$(BTSTACK_ROOT)/platform/posix \
|
-I$(BTSTACK_ROOT)/platform/posix \
|
||||||
-I$(BTSTACK_ROOT)/chipset/bcm \
|
-I$(BTSTACK_ROOT)/chipset/bcm \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
||||||
|
@ -34,6 +34,7 @@ CFLAGS += -g -Wall -Werror \
|
|||||||
-I$(BTSTACK_ROOT)/chipset/em9301 \
|
-I$(BTSTACK_ROOT)/chipset/em9301 \
|
||||||
-I$(BTSTACK_ROOT)/chipset/stlc2500d \
|
-I$(BTSTACK_ROOT)/chipset/stlc2500d \
|
||||||
-I$(BTSTACK_ROOT)/chipset/tc3566x \
|
-I$(BTSTACK_ROOT)/chipset/tc3566x \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
||||||
|
@ -22,6 +22,7 @@ CFLAGS += -I${BTSTACK_ROOT}/platform/posix \
|
|||||||
-I${BTSTACK_ROOT}/platform/windows \
|
-I${BTSTACK_ROOT}/platform/windows \
|
||||||
-I${BTSTACK_ROOT}/platform/embedded \
|
-I${BTSTACK_ROOT}/platform/embedded \
|
||||||
-I$(BTSTACK_ROOT)/chipset/zephyr \
|
-I$(BTSTACK_ROOT)/chipset/zephyr \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/windows
|
VPATH += ${BTSTACK_ROOT}/platform/windows
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
|
@ -40,6 +40,7 @@ CFLAGS += -I${BTSTACK_ROOT}/platform/posix \
|
|||||||
-I$(BTSTACK_ROOT)/chipset/em9301 \
|
-I$(BTSTACK_ROOT)/chipset/em9301 \
|
||||||
-I$(BTSTACK_ROOT)/chipset/stlc2500d \
|
-I$(BTSTACK_ROOT)/chipset/stlc2500d \
|
||||||
-I$(BTSTACK_ROOT)/chipset/tc3566x \
|
-I$(BTSTACK_ROOT)/chipset/tc3566x \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/windows
|
VPATH += ${BTSTACK_ROOT}/platform/windows
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
|
@ -13,7 +13,8 @@ CFLAGS += -g -Wall -Wmissing-prototypes -Wstrict-prototypes -Wshadow -Werror
|
|||||||
|
|
||||||
CFLAGS += -I${BTSTACK_ROOT}/platform/windows \
|
CFLAGS += -I${BTSTACK_ROOT}/platform/windows \
|
||||||
-I${BTSTACK_ROOT}/platform/posix \
|
-I${BTSTACK_ROOT}/platform/posix \
|
||||||
-I${BTSTACK_ROOT}/platform/embedded
|
-I${BTSTACK_ROOT}/platform/embedded \
|
||||||
|
-I${BTSTACK_ROOT}/3rd-party/tinydir
|
||||||
|
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
VPATH += ${BTSTACK_ROOT}/platform/embedded
|
||||||
VPATH += ${BTSTACK_ROOT}/platform/posix
|
VPATH += ${BTSTACK_ROOT}/platform/posix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user