From 360243be41f47158adff357b9fead2686419a2df Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 14 Sep 2017 15:34:11 +0200 Subject: [PATCH] posix-h4-zephyr: read static address via vendor specific command and use it --- chipset/zephyr/btstack_chipset_zephyr.c | 102 ++++++++++++++++++++++++ chipset/zephyr/btstack_chipset_zephyr.h | 64 +++++++++++++++ port/posix-h4-zephyr/Makefile | 6 +- port/posix-h4-zephyr/main.c | 14 +++- 4 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 chipset/zephyr/btstack_chipset_zephyr.c create mode 100644 chipset/zephyr/btstack_chipset_zephyr.h diff --git a/chipset/zephyr/btstack_chipset_zephyr.c b/chipset/zephyr/btstack_chipset_zephyr.c new file mode 100644 index 000000000..db11c50bf --- /dev/null +++ b/chipset/zephyr/btstack_chipset_zephyr.c @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2017 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 + * + */ + +#define __BTSTACK_FILE__ "btstack_chipset_zephyr.c" + +/* + * btstack_chipset_zephyr.c + * + * Adapter to use CSR-based chipsets with BTstack + * SCO over HCI doesn't work over H4 connection and BTM805 module from Microchip Bluetooth Audio Developer Kit (CSR8811) + */ + +#include "btstack_chipset_zephyr.h" + +#include /* NULL */ +#include +#include /* memcpy */ + +#include "btstack_control.h" +#include "btstack_debug.h" +#include "btstack_util.h" +#include "hci_transport.h" + +// minimal Zpehyr init script to read static random address +static const uint8_t init_script[] = { + 0x09, 0xfc, 0x00, +}; +static const uint16_t init_script_size = sizeof(init_script); + +// +static uint32_t init_script_offset = 0; + +static void chipset_init(const void * config){ + init_script_offset = 0; +} + +static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ + + while (1){ + + if (init_script_offset >= init_script_size) { + return BTSTACK_CHIPSET_DONE; + } + + // copy command header + memcpy(&hci_cmd_buffer[0], (uint8_t *) &init_script[init_script_offset], 3); + init_script_offset += 3; + int payload_len = hci_cmd_buffer[2]; + // copy command payload + memcpy(&hci_cmd_buffer[3], (uint8_t *) &init_script[init_script_offset], payload_len); + + return BTSTACK_CHIPSET_VALID_COMMAND; + } +} + + +static const btstack_chipset_t btstack_chipset_zephyr = { + "Zephyr", + chipset_init, + chipset_next_command, + NULL, // chipset_set_baudrate_command, + NULL, // chipset_set_bd_addr_command not supported or implemented +}; + +// MARK: public API +const btstack_chipset_t * btstack_chipset_zephyr_instance(void){ + return &btstack_chipset_zephyr; +} diff --git a/chipset/zephyr/btstack_chipset_zephyr.h b/chipset/zephyr/btstack_chipset_zephyr.h new file mode 100644 index 000000000..4e9f8a1b0 --- /dev/null +++ b/chipset/zephyr/btstack_chipset_zephyr.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2017 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 + * + */ + +/* + * btstack_chipset_zephyr.c + * + * Adapter to use Zephyr-based Bluetooth Controllers with BTstack + */ + +#ifndef __BTSTACK_CHIPSET_ZEPHYR_H +#define __BTSTACK_CHIPSET_ZEPHYR_H + +#if defined __cplusplus +extern "C" { +#endif + +#include +#include "btstack_chipset.h" +#include "bluetooth.h" + +/** + * Get chipset instance for Zephyr Controllers + */ +const btstack_chipset_t * btstack_chipset_zephyr_instance(void); + +#if defined __cplusplus +} +#endif + +#endif // __BTSTACK_CHIPSET_ZEPHYR_H diff --git a/port/posix-h4-zephyr/Makefile b/port/posix-h4-zephyr/Makefile index 78282a1bb..9a20af313 100644 --- a/port/posix-h4-zephyr/Makefile +++ b/port/posix-h4-zephyr/Makefile @@ -9,7 +9,7 @@ CORE += \ le_device_db_fs.c \ main.c \ btstack_stdin_posix.c \ - wav_util.c \ + btstack_chipset_zephyr.c \ # examples include ${BTSTACK_ROOT}/example/Makefile.inc @@ -17,9 +17,11 @@ include ${BTSTACK_ROOT}/example/Makefile.inc CFLAGS += -g -Wall -Werror \ -I$(BTSTACK_ROOT)/platform/embedded \ -I$(BTSTACK_ROOT)/platform/posix \ + -I$(BTSTACK_ROOT)/chipset/zephyr \ VPATH += ${BTSTACK_ROOT}/platform/posix VPATH += ${BTSTACK_ROOT}/platform/embedded +VPATH += ${BTSTACK_ROOT}/chipset/zephyr # use pkg-config for portaudio # CFLAGS += $(shell pkg-config portaudio-2.0 --cflags) -DHAVE_PORTAUDIO @@ -28,5 +30,5 @@ VPATH += ${BTSTACK_ROOT}/platform/embedded # CFLAGS += -I/usr/local/include -DHAVE_PORTAUDIO # LDFLAGS += -L/sw/lib -lportaudio -Wl,-framework,CoreAudio -Wl,-framework,AudioToolbox -Wl,-framework,AudioUnit -Wl,-framework,Carbon -all: ${EXAMPLES} +all: ${EXAMPLES_USING_LE} diff --git a/port/posix-h4-zephyr/main.c b/port/posix-h4-zephyr/main.c index c6855ce81..a403e0581 100644 --- a/port/posix-h4-zephyr/main.c +++ b/port/posix-h4-zephyr/main.c @@ -61,6 +61,7 @@ #include "hci.h" #include "hci_dump.h" #include "btstack_stdin.h" +#include "btstack_chipset_zephyr.h" int btstack_main(int argc, const char * argv[]); @@ -74,14 +75,22 @@ static hci_transport_config_uart_t config = { 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 void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ bd_addr_t addr; if (packet_type != HCI_EVENT_PACKET) return; switch (hci_event_packet_get_type(packet)){ case BTSTACK_EVENT_STATE: if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; - gap_local_bd_addr(addr); - printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); + printf("BTstack up and running as %s\n", bd_addr_to_str(static_address)); + break; + case HCI_EVENT_COMMAND_COMPLETE: + if (memcmp(packet, read_static_address_command_complete_prefix, sizeof(read_static_address_command_complete_prefix)) == 0){ + reverse_48(&packet[7], static_address); + gap_random_address_set(addr); + } break; default: break; @@ -136,6 +145,7 @@ int main(int argc, const char * argv[]){ const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance(); hci_init(transport, (void*) &config); hci_set_link_key_db(link_key_db); + hci_set_chipset(btstack_chipset_zephyr_instance()); // inform about BTstack state hci_event_callback_registration.callback = &packet_handler;