posix-h4: support zephyr read static address, set random address if chip doesn't have any

This commit is contained in:
Matthias Ringwald 2024-08-08 12:08:29 +02:00
parent 5cc4aee33b
commit 1c61f11625

View File

@ -62,6 +62,7 @@
#include "btstack_chipset_em9301.h"
#include "btstack_chipset_stlc2500d.h"
#include "btstack_chipset_tc3566x.h"
#include "btstack_chipset_zephyr.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
@ -85,7 +86,10 @@ static char tlv_db_path[100];
static bool tlv_reset;
static const btstack_tlv_t * tlv_impl;
static btstack_tlv_posix_t tlv_context;
static bd_addr_t local_addr;
static bd_addr_t 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 };
static int is_bcm;
// shutdown
@ -105,12 +109,21 @@ static hci_transport_config_uart_t config = {
static btstack_packet_callback_registration_t hci_event_callback_registration;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
static bd_addr_t local_addr;
const uint8_t *params;
if (packet_type != HCI_EVENT_PACKET) return;
switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE:
switch(btstack_event_state_get_state(packet)){
case HCI_STATE_WORKING:
gap_local_bd_addr(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);
}
printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr));
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_with_delimiter(local_addr, '-'));
@ -129,7 +142,7 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
btstack_tlv_set_instance(tlv_impl, &tlv_context);
#ifdef ENABLE_CLASSIC
hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
#endif
#endif
#ifdef ENABLE_BLE
le_device_db_tlv_configure(tlv_impl, &tlv_context);
#endif
@ -143,23 +156,40 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
exit(0);
break;
default:
break;
break;
}
break;
case HCI_EVENT_COMMAND_COMPLETE:
if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_NAME){
if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
// terminate, name 248 chars
packet[6+248] = 0;
printf("Local name: %s\n", &packet[6]);
if (is_bcm){
btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
}
}
if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION){
local_version_information_handler(packet);
switch (hci_event_command_complete_get_command_opcode(packet)){
case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION:
local_version_information_handler(packet);
break;
case HCI_OPCODE_HCI_ZEPHYR_READ_STATIC_ADDRESS:
log_info("Zephyr read static address available");
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);
break;
case HCI_OPCODE_HCI_READ_LOCAL_NAME:
params = hci_event_command_complete_get_return_parameters(packet);
if(params[0] != 0)
break;
// terminate, name 248 chars
packet[6+248] = 0;
printf("Local name: %s\n", &packet[6]);
if (is_bcm){
btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
}
break;
default:
break;
}
break;
default:
break;
}
@ -243,6 +273,7 @@ static void local_version_information_handler(uint8_t * packet){
break;
case BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC:
printf("PacketCraft HCI Controller\n");
hci_set_chipset(btstack_chipset_zephyr_instance());
break;
default:
printf("Unknown manufacturer / manufacturer not supported yet.\n");