mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-30 16:20:24 +00:00
security_manager_sc: updated command line handling / support for clearing tlv storage
This commit is contained in:
parent
e8cbad6015
commit
169aa8795f
@ -42,7 +42,8 @@
|
||||
// minimal setup for HCI code
|
||||
//
|
||||
// *****************************************************************************
|
||||
|
||||
#include <getopt.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -73,6 +74,7 @@
|
||||
#define TLV_DB_PATH_PREFIX "/tmp/btstack_"
|
||||
#define TLV_DB_PATH_POSTFIX ".tlv"
|
||||
static char tlv_db_path[100];
|
||||
static bool tlv_clear = false;
|
||||
static const btstack_tlv_t * tlv_impl;
|
||||
static btstack_tlv_posix_t tlv_context;
|
||||
static bd_addr_t local_addr;
|
||||
@ -124,6 +126,16 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
|
||||
btstack_strcpy(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_PREFIX);
|
||||
btstack_strcat(tlv_db_path, sizeof(tlv_db_path), bd_addr_to_str(local_addr));
|
||||
btstack_strcat(tlv_db_path, sizeof(tlv_db_path), TLV_DB_PATH_POSTFIX);
|
||||
printf("TLV path: %s", tlv_db_path);
|
||||
if (tlv_clear){
|
||||
int rc = unlink(tlv_db_path);
|
||||
if((rc == 0) || ( errno == ENOENT )){
|
||||
printf(", clear ok\n");
|
||||
} else {
|
||||
printf(", clear failed because %s\n", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
|
||||
btstack_tlv_set_instance(tlv_impl, &tlv_context);
|
||||
#ifdef ENABLE_CLASSIC
|
||||
@ -178,15 +190,137 @@ void hal_led_toggle(void){
|
||||
printf("LED State %u\n", led_state);
|
||||
}
|
||||
|
||||
#include <string.h>
|
||||
static void btstack_log_cmd_line( int argc, const char *args[] )
|
||||
{
|
||||
char buf[2048] = "command line:";
|
||||
char *ptr = buf+strlen(buf);
|
||||
size_t len = sizeof(buf);
|
||||
for( int i=0; i<argc; ++i )
|
||||
{
|
||||
int ret = snprintf(ptr, len, " %s", args[i]);
|
||||
// avoid overflow
|
||||
if((ret < 0) || ( (size_t)ret >= len ))
|
||||
{
|
||||
log_error("command line truncated!");
|
||||
break;
|
||||
}
|
||||
// terminate string
|
||||
ptr[ret] = 0;
|
||||
len -= ret;
|
||||
ptr += ret;
|
||||
}
|
||||
log_info("%s", buf);
|
||||
|
||||
}
|
||||
|
||||
static const char short_options[] = "-hu:l:c";
|
||||
|
||||
static const struct option long_options[] = {
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{"logfile", required_argument, NULL, 'l'},
|
||||
{"clear-tlv", no_argument, NULL, 'c'},
|
||||
{"usbpath", required_argument, NULL, 'u'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
static const char *help_options[] = {
|
||||
"print (this) help.",
|
||||
"set file to store debug output and HCI trace.",
|
||||
"clear bonding information stored in TLV.",
|
||||
"set USB path to Bluetooth Controller.",
|
||||
};
|
||||
|
||||
static const char *option_arg_name[] = {
|
||||
"",
|
||||
"LOGFILE",
|
||||
"",
|
||||
"USBPATH",
|
||||
};
|
||||
|
||||
static void usage(const char *name){
|
||||
unsigned int i;
|
||||
printf( "usage:\n\t%s [options]\n", name );
|
||||
printf("valid options:\n");
|
||||
for( i=0; long_options[i].name != 0; i++) {
|
||||
printf("--%-10s| -%c %-10s\t\t%s\n", long_options[i].name, long_options[i].val, option_arg_name[i], help_options[i] );
|
||||
}
|
||||
}
|
||||
|
||||
static int app_argc = 0;
|
||||
static const char *app_argv[100] = { NULL };
|
||||
|
||||
static void clear_argv(int idx)
|
||||
{
|
||||
app_argv[idx] = NULL;
|
||||
}
|
||||
static void shift_argv(int idx)
|
||||
{
|
||||
for( int i=idx; i<app_argc-1; ++i )
|
||||
{
|
||||
app_argv[i] = app_argv[i+1];
|
||||
}
|
||||
app_argc--;
|
||||
}
|
||||
static void cleanup_argv()
|
||||
{
|
||||
for( int i=0; i<app_argc; )
|
||||
{
|
||||
if( app_argv[i] == NULL )
|
||||
shift_argv( i );
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
#define USB_MAX_PATH_LEN 7
|
||||
int main(int argc, const char * argv[]){
|
||||
|
||||
uint8_t usb_path[USB_MAX_PATH_LEN];
|
||||
int usb_path_len = 0;
|
||||
const char * usb_path_string = NULL;
|
||||
if (argc >= 3 && strcmp(argv[1], "-u") == 0){
|
||||
const char * log_file_path = NULL;
|
||||
|
||||
app_argc = argc;
|
||||
for( int i=0; i<argc; ++i )
|
||||
{
|
||||
app_argv[i] = argv[i];
|
||||
}
|
||||
|
||||
opterr = 0; // disable error message on unknown options
|
||||
// parse command line parameters
|
||||
while(true){
|
||||
int c = getopt_long( argc, (char* const *)argv, short_options, long_options, NULL );
|
||||
if (c < 0) {
|
||||
break;
|
||||
}
|
||||
switch (c) {
|
||||
case 'u':
|
||||
usb_path_string = optarg;
|
||||
clear_argv( optind - 1 );
|
||||
clear_argv( optind - 2 );
|
||||
break;
|
||||
case 'l':
|
||||
log_file_path = optarg;
|
||||
clear_argv( optind - 1 );
|
||||
clear_argv( optind - 2 );
|
||||
break;
|
||||
case 'c':
|
||||
tlv_clear = true;
|
||||
clear_argv( optind - 1 );
|
||||
break;
|
||||
case '?':
|
||||
case 1:
|
||||
break;
|
||||
case 'h':
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (usb_path_string != NULL){
|
||||
// parse command line options for "-u 11:22:33"
|
||||
usb_path_string = argv[2];
|
||||
printf("Specified USB Path: ");
|
||||
while (1){
|
||||
char * delimiter;
|
||||
@ -199,20 +333,20 @@ int main(int argc, const char * argv[]){
|
||||
usb_path_string = delimiter+1;
|
||||
}
|
||||
printf("\n");
|
||||
argc -= 2;
|
||||
memmove((void *) &argv[1], &argv[3], (argc-1) * sizeof(char *));
|
||||
}
|
||||
|
||||
/// GET STARTED with BTstack ///
|
||||
btstack_memory_init();
|
||||
// remove used up command line args
|
||||
cleanup_argv();
|
||||
|
||||
/// GET STARTED with BTstack ///
|
||||
btstack_memory_init();
|
||||
btstack_run_loop_init(btstack_run_loop_posix_get_instance());
|
||||
|
||||
|
||||
if (usb_path_len){
|
||||
hci_transport_usb_set_path(usb_path_len, usb_path);
|
||||
}
|
||||
|
||||
// use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
|
||||
|
||||
char pklg_path[100];
|
||||
btstack_strcpy(pklg_path, sizeof(pklg_path), "/tmp/hci_dump");
|
||||
if (usb_path_len){
|
||||
@ -221,13 +355,20 @@ int main(int argc, const char * argv[]){
|
||||
}
|
||||
btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg");
|
||||
|
||||
// use path from command line if given
|
||||
if( log_file_path != NULL )
|
||||
btstack_strcpy(pklg_path, sizeof(pklg_path), log_file_path );
|
||||
|
||||
// log into file using HCI_DUMP_PACKETLOGGER format
|
||||
hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER);
|
||||
hci_dump_init(hci_dump_posix_fs_get_instance());
|
||||
printf("Packet Log: %s\n", pklg_path);
|
||||
|
||||
// let the log contain the calling parameters
|
||||
btstack_log_cmd_line( app_argc, app_argv );
|
||||
|
||||
// init HCI
|
||||
hci_init(hci_transport_usb_instance(), NULL);
|
||||
hci_init(hci_transport_usb_instance(), NULL);
|
||||
|
||||
#ifdef HAVE_PORTAUDIO
|
||||
btstack_audio_sink_set_instance(btstack_audio_portaudio_sink_get_instance());
|
||||
@ -242,10 +383,10 @@ int main(int argc, const char * argv[]){
|
||||
signal(SIGINT, sigint_handler);
|
||||
|
||||
// setup app
|
||||
btstack_main(argc, argv);
|
||||
btstack_main(app_argc, app_argv);
|
||||
|
||||
// go
|
||||
btstack_run_loop_execute();
|
||||
btstack_run_loop_execute();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -72,9 +72,9 @@ void __gcov_reset(void);
|
||||
|
||||
const uint8_t adv_data[] = {
|
||||
// Flags general discoverable, BR/EDR not supported
|
||||
0x02, 0x01, 0x06,
|
||||
0x02, 0x01, 0x06,
|
||||
// Name
|
||||
0x0d, 0x09, 'S', 'M', ' ', 'P', 'e', 'r', 'i', 'p', 'h', 'e', 'a', 'l'
|
||||
0x0d, 0x09, 'S', 'M', ' ', 'P', 'e', 'r', 'i', 'p', 'h', 'e', 'a', 'l'
|
||||
};
|
||||
const uint8_t adv_data_len = sizeof(adv_data);
|
||||
|
||||
@ -136,7 +136,7 @@ static void heartbeat_handler(struct btstack_timer_source *ts){
|
||||
btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
|
||||
btstack_run_loop_add_timer(ts);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
static int get_oob_data_callback(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data){
|
||||
UNUSED(address_type);
|
||||
@ -215,8 +215,8 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
|
||||
if (packet[4] != 0){
|
||||
printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]);
|
||||
gap_disconnect(connection_handle);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
state = TC_W4_CHARACTERISTIC_RESULT;
|
||||
printf("Search for counter characteristic.\n");
|
||||
gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_counter_service, le_counter_characteristic_uuid);
|
||||
@ -225,7 +225,7 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case TC_W4_CHARACTERISTIC_RESULT:
|
||||
switch(hci_event_packet_get_type(packet)){
|
||||
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
|
||||
@ -235,8 +235,8 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
|
||||
if (packet[4] != 0){
|
||||
printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]);
|
||||
gap_disconnect(connection_handle);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
state = TC_W4_SUBSCRIBED;
|
||||
printf("Configure counter for notify.\n");
|
||||
gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &le_counter_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
|
||||
@ -474,7 +474,7 @@ int btstack_main(int argc, const char * argv[]);
|
||||
int btstack_main(int argc, const char * argv[]){
|
||||
|
||||
int arg = 1;
|
||||
|
||||
|
||||
while (arg < argc) {
|
||||
if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){
|
||||
arg++;
|
||||
@ -528,7 +528,7 @@ int btstack_main(int argc, const char * argv[]){
|
||||
|
||||
// set up l2cap_le
|
||||
l2cap_init();
|
||||
|
||||
|
||||
// setup le device db
|
||||
le_device_db_init();
|
||||
|
||||
@ -538,7 +538,7 @@ int btstack_main(int argc, const char * argv[]){
|
||||
// setup SM io capabilities & auth req
|
||||
sm_init();
|
||||
sm_set_io_capabilities(sm_io_capabilities);
|
||||
sm_set_authentication_requirements(sm_auth_req);
|
||||
sm_set_authentication_requirements(sm_auth_req);
|
||||
sm_register_oob_data_callback(get_oob_data_callback);
|
||||
sm_register_sc_oob_data_callback(get_sc_oob_data_callback);
|
||||
|
||||
@ -550,7 +550,7 @@ int btstack_main(int argc, const char * argv[]){
|
||||
sm_add_event_handler(&sm_event_callback_registration);
|
||||
|
||||
// setup ATT server
|
||||
att_server_init(profile_data, att_read_callback, att_write_callback);
|
||||
att_server_init(profile_data, att_read_callback, att_write_callback);
|
||||
att_server_register_packet_handler(&att_packet_handler);
|
||||
|
||||
btstack_stdin_setup(stdin_process);
|
||||
@ -562,7 +562,7 @@ int btstack_main(int argc, const char * argv[]){
|
||||
|
||||
// turn on!
|
||||
hci_power_control(HCI_POWER_ON);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@ class Node:
|
||||
return line
|
||||
|
||||
def start_process(self):
|
||||
args = ['build-coverage/sm_test', '-u', self.usb_path]
|
||||
args = ['build-coverage/sm_test', '-u', self.usb_path, '-c']
|
||||
if self.peer_addr != None:
|
||||
args.append('-a')
|
||||
args.append(self.peer_addr)
|
||||
|
Loading…
x
Reference in New Issue
Block a user