btstack: unified handling of zephyr read static address command

le_audio: per application unique hci dump file names
This commit is contained in:
Dirk Helbig 2023-09-07 19:41:35 +02:00 committed by Matthias Ringwald
parent 0af82dbaa1
commit 9c228539c3
6 changed files with 107 additions and 68 deletions

View File

@ -87,8 +87,6 @@ static bd_addr_t local_addr;
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
static bd_addr_t static_address; static bd_addr_t static_address;
static int using_static_address; static int using_static_address;
@ -130,6 +128,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
const uint8_t * usb_path; const uint8_t * usb_path;
uint16_t product_id; uint16_t product_id;
uint16_t vendor_id; uint16_t vendor_id;
const uint8_t * params;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
@ -198,13 +197,22 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
} }
break; break;
case HCI_EVENT_COMMAND_COMPLETE: case HCI_EVENT_COMMAND_COMPLETE:
if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ switch (hci_event_command_complete_get_command_opcode(packet)){
case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
local_version_information_handler(packet); local_version_information_handler(packet);
} break;
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
reverse_48(&packet[7], static_address); params = hci_event_command_complete_get_return_parameters(packet);
if(params[0] != 0)
break;
if(size < 13)
break;
reverse_48(&params[2], static_address);
gap_random_address_set(static_address); gap_random_address_set(static_address);
using_static_address = 1; using_static_address = 1;
break;
default:
break;
} }
break; break;
default: default:

View File

@ -88,10 +88,10 @@ static hci_transport_config_uart_t config = {
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
static bd_addr_t static_address; static bd_addr_t static_address;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
const uint8_t *params;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
@ -119,9 +119,18 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
} }
break; break;
case HCI_EVENT_COMMAND_COMPLETE: case HCI_EVENT_COMMAND_COMPLETE:
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ switch (hci_event_command_complete_get_command_opcode(packet)){
reverse_48(&packet[7], static_address); case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
params = hci_event_command_complete_get_return_parameters(packet);
if(params[0] != 0)
break;
if(size < 13)
break;
reverse_48(&params[2], static_address);
gap_random_address_set(static_address); gap_random_address_set(static_address);
break;
default:
break;
} }
break; break;
default: default:

View File

@ -81,7 +81,6 @@ static hci_transport_config_uart_t config = {
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
static bd_addr_t static_address; static bd_addr_t static_address;
#define TLV_DB_PATH_PREFIX "btstack_" #define TLV_DB_PATH_PREFIX "btstack_"
@ -92,6 +91,7 @@ static btstack_tlv_windows_t tlv_context;
static bool shutdown_triggered; static bool shutdown_triggered;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
const uint8_t *params
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
@ -120,9 +120,18 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
} }
break; break;
case HCI_EVENT_COMMAND_COMPLETE: case HCI_EVENT_COMMAND_COMPLETE:
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ switch (hci_event_command_complete_get_command_opcode(packet)){
reverse_48(&packet[7], static_address); case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
params = hci_event_command_complete_get_return_parameters(packet);
if(params[0] != 0)
break;
if(size < 13)
break;
reverse_48(&params[2], static_address);
gap_random_address_set(static_address); gap_random_address_set(static_address);
break;
default:
break;
} }
break; break;
default: default:

View File

@ -35,10 +35,6 @@
#include "btstack_tlv_none.h" #include "btstack_tlv_none.h"
#include "ble/le_device_db_tlv.h" #include "ble/le_device_db_tlv.h"
#define HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS 0xFC09
const hci_cmd_t hci_zephyr_read_static_address = {
HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS, ""
};
static K_FIFO_DEFINE(tx_queue); static K_FIFO_DEFINE(tx_queue);
static K_FIFO_DEFINE(rx_queue); static K_FIFO_DEFINE(rx_queue);
@ -206,7 +202,7 @@ static const btstack_run_loop_t btstack_run_loop_zephyr = {
/** /**
* @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init * @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
*/ */
const btstack_run_loop_t * btstack_run_loop_zephyr_get_instance(void){ static const btstack_run_loop_t * btstack_run_loop_zephyr_get_instance(void){
return &btstack_run_loop_zephyr; return &btstack_run_loop_zephyr;
} }
@ -260,6 +256,7 @@ static void local_version_information_handler(uint8_t * packet){
} }
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
const uint8_t *params;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
@ -279,8 +276,13 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
local_version_information_handler(packet); local_version_information_handler(packet);
break; break;
case HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS: case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
reverse_48(&packet[7], local_addr); params = hci_event_command_complete_get_return_parameters(packet);
if(params[0] != 0)
break;
if(size < 13)
break;
reverse_48(&params[2], local_addr);
gap_random_address_set(local_addr); gap_random_address_set(local_addr);
break; break;
default: default:

View File

@ -48,6 +48,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <limits.h>
#include <libgen.h>
#include "btstack_config.h" #include "btstack_config.h"
@ -70,11 +72,7 @@
#include "hci_transport.h" #include "hci_transport.h"
#include "hci_transport_h4.h" #include "hci_transport_h4.h"
#include "btstack_chipset_bcm.h" #include "btstack_chipset_bcm.h"
#include "btstack_chipset_zephyr.h"
#define HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS 0xFC09
const hci_cmd_t hci_zephyr_read_static_address = {
HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS, ""
};
#define TLV_DB_PATH_PREFIX "/tmp/btstack_" #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
#define TLV_DB_PATH_POSTFIX ".tlv" #define TLV_DB_PATH_POSTFIX ".tlv"
@ -84,9 +82,10 @@ static btstack_tlv_posix_t tlv_context;
static btstack_packet_callback_registration_t hci_event_callback_registration; static btstack_packet_callback_registration_t hci_event_callback_registration;
static bool is_zephyr; static bd_addr_t static_address;
static bool zephyr_read_static_address;
static bd_addr_t zephyr_static_address; // random MAC address for the device, used if nothing else is available
static const bd_addr_t random_address = { 0xC1, 0x01, 0x01, 0x01, 0x01, 0x01 };
// shutdown // shutdown
static bool shutdown_triggered; static bool shutdown_triggered;
@ -118,18 +117,21 @@ static void setup_tlv(bd_addr_t addr){
} }
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
bd_addr_t local_addr;
const uint8_t *params;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE: case BTSTACK_EVENT_STATE:
switch(btstack_event_state_get_state(packet)){ switch(btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING: case HCI_STATE_WORKING:
if (is_zephyr){
zephyr_read_static_address = true;
} else {
bd_addr_t local_addr;
gap_local_bd_addr(local_addr); gap_local_bd_addr(local_addr);
setup_tlv(local_addr); if( btstack_is_null_bd_addr(local_addr) && !btstack_is_null_bd_addr(static_address) ) {
memcpy(local_addr, static_address, sizeof(bd_addr_t));
} else if( btstack_is_null_bd_addr(local_addr ) && btstack_is_null_bd_addr(static_address) ) {
memcpy(local_addr, random_address, sizeof(bd_addr_t));
gap_random_address_set(local_addr);
} }
setup_tlv(local_addr);
break; break;
case HCI_STATE_OFF: case HCI_STATE_OFF:
btstack_tlv_posix_deinit(&tlv_context); btstack_tlv_posix_deinit(&tlv_context);
@ -148,10 +150,14 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
local_version_information_handler(packet); local_version_information_handler(packet);
break; break;
case HCI_OPCODE_ZEPHYR_READ_STATIC_ADDRESS: case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
reverse_48(&packet[7], zephyr_static_address); params = hci_event_command_complete_get_return_parameters(packet);
gap_random_address_set(zephyr_static_address); if(params[0] != 0)
setup_tlv(zephyr_static_address); break;
if(size < 13)
break;
reverse_48(&params[2], static_address);
gap_random_address_set(static_address);
break; break;
default: default:
break; break;
@ -160,11 +166,6 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
default: default:
break; break;
} }
if (zephyr_read_static_address && hci_can_send_command_packet_now()){
zephyr_read_static_address = false;
hci_send_cmd(&hci_zephyr_read_static_address);
}
} }
static void trigger_shutdown(void){ static void trigger_shutdown(void){
@ -198,7 +199,8 @@ static void local_version_information_handler(uint8_t * packet){
break; break;
case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION: case BLUETOOTH_COMPANY_ID_THE_LINUX_FOUNDATION:
printf("Zephyr HCI Controller\n"); printf("Zephyr HCI Controller\n");
is_zephyr = true; printf("WARNING - untested probably won't work!\n");
hci_set_chipset(btstack_chipset_zephyr_instance());
break; break;
case BLUETOOTH_COMPANY_ID_INFINEON_TECHNOLOGIES_AG: case BLUETOOTH_COMPANY_ID_INFINEON_TECHNOLOGIES_AG:
case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION: case BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION:
@ -218,7 +220,6 @@ int main(int argc, const char * argv[]){
btstack_memory_init(); btstack_memory_init();
btstack_run_loop_init(btstack_run_loop_posix_get_instance()); btstack_run_loop_init(btstack_run_loop_posix_get_instance());
// pre-select serial device // pre-select serial device
config.device_name = "/dev/tty.usbmodemEA7EB9D612C31"; // BL654 with PTS Firmware config.device_name = "/dev/tty.usbmodemEA7EB9D612C31"; // BL654 with PTS Firmware
@ -233,13 +234,15 @@ int main(int argc, const char * argv[]){
printf("H4 device: %s\n", config.device_name); printf("H4 device: %s\n", config.device_name);
// log into file using HCI_DUMP_BTSNOOP format // log into file using HCI_DUMP_BTSNOOP format
char * pklg_path = "/tmp/hci_dump.btsnoop"; char *app_name = strndup( argv[0], PATH_MAX );
if (strcmp(config.device_name, "/dev/tty.usbmodemEF437DF524C51") == 0){ char *base_name = basename( app_name );
pklg_path = "/tmp/hci_dump_source.btsnoop"; const char *pklg_postfix = ".btsnoop";
} char pklg_path[PATH_MAX] = "/tmp/hci_dump_";
if (strcmp(config.device_name, "/dev/tty.usbmodemE6589B44933B1") == 0){
pklg_path = "/tmp/hci_dump_sink.btsnoop"; btstack_strcat( pklg_path, sizeof(pklg_path), base_name );
} btstack_strcat( pklg_path, sizeof(pklg_path), pklg_postfix );
free( app_name );
hci_dump_posix_fs_open(pklg_path, HCI_DUMP_BTSNOOP); hci_dump_posix_fs_open(pklg_path, HCI_DUMP_BTSNOOP);
const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance();
hci_dump_init(hci_dump_impl); hci_dump_init(hci_dump_impl);

View File

@ -86,8 +86,6 @@ static const char *app_argv[MAX_CMD_LINE_ITEMS] = { NULL };
int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]);
static const uint8_t read_static_address_command_complete_prefix[] = { 0x0e, 0x1b, 0x01, 0x09, 0xfc };
static bd_addr_t static_address; static bd_addr_t static_address;
static int using_static_address; static int using_static_address;
@ -122,6 +120,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
const uint8_t * usb_path; const uint8_t * usb_path;
uint16_t product_id; uint16_t product_id;
uint8_t i; uint8_t i;
const uint8_t *params;
if (packet_type != HCI_EVENT_PACKET) return; if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){ switch (hci_event_packet_get_type(packet)){
@ -164,13 +163,22 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
break; break;
case HCI_EVENT_COMMAND_COMPLETE: case HCI_EVENT_COMMAND_COMPLETE:
if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){ switch (hci_event_command_complete_get_command_opcode(packet)){
case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
local_version_information_handler(packet); local_version_information_handler(packet);
} break;
if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
reverse_48(&packet[7], static_address); params = hci_event_command_complete_get_return_parameters(packet);
if(params[0] != 0)
break;
if(size < 13)
break;
reverse_48(&params[2], static_address);
gap_random_address_set(static_address); gap_random_address_set(static_address);
using_static_address = 1; using_static_address = 1;
break;
default:
break;
} }
break; break;
case HCI_EVENT_TRANSPORT_USB_INFO: case HCI_EVENT_TRANSPORT_USB_INFO: