diff --git a/example/embedded/Makefile.inc b/example/embedded/Makefile.inc index 87d74a0c6..28fb766c6 100644 --- a/example/embedded/Makefile.inc +++ b/example/embedded/Makefile.inc @@ -72,6 +72,7 @@ EXAMPLES = \ spp_counter \ spp_streamer \ gap_le_advertisements \ + hsp_hs_test \ EXAMPLES_USING_LE = \ ancs_client \ @@ -167,6 +168,8 @@ ble_client: ${CORE_OBJ} ${COMMON_OBJ} ${ATT_OBJ} ${GATT_CLIENT_OBJ} ${SM_REAL} b gap_le_advertisements: ${CORE_OBJ} ${COMMON_OBJ} ad_parser.c gap_le_advertisements.c ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ +hsp_hs_test: ${CORE_OBJ} ${COMMON_OBJ} ${SDP_CLIENT} hsp_hs.o hsp_hs_test.c + ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ clean: rm -f ${EXAMPLES} diff --git a/example/embedded/hsp_hs_test.c b/example/embedded/hsp_hs_test.c new file mode 100644 index 000000000..567e4b906 --- /dev/null +++ b/example/embedded/hsp_hs_test.c @@ -0,0 +1,238 @@ +/* + * Copyright (C) 2014 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 + * + */ + +// ***************************************************************************** +// +// Minimal test for HSP Headset (!! UNDER DEVELOPMENT !!) +// +// Requires HAVE_SCO and HAVE_SCO_OVER_HCI to be defined +// +// Tested working setups: +// - Ubuntu 14 64-bit, CC2564B connected via FTDI USB-2-UART adapter, 921600 baud +// +// Non working setups: +// - OS X 10.11, CC2564B connected via FDTI USB-2-UART adapter, 921600 baud +// - select(..) blocks > 400 ms -> num completed is received to late -> gaps between audio +// - looks like bug in select->FTDI driver as it works correct on Linux +// - OS X 10.11, USB Bluetooth Dongles... +// ***************************************************************************** + +#include "btstack-config.h" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "sdp.h" +#include "hsp_hs.h" + +#include "hci.h" +#include "l2cap.h" +#include "debug.h" + +static uint32_t hsp_service_buffer[150/4]; // implicit alignment to 4-byte memory address +static const uint8_t rfcomm_channel_nr = 1; +static const char hsp_hs_service_name[] = "Headset Test"; +static uint16_t sco_handle = 0; + +static char hs_cmd_buffer[100]; + +static int phase = 0; + +// input signal: pre-computed sine wave, 160 Hz +static const uint8_t sine[] = { + 0, 15, 31, 46, 61, 74, 86, 97, 107, 114, + 120, 124, 126, 126, 124, 120, 114, 107, 97, 86, + 74, 61, 46, 31, 15, 0, 241, 225, 210, 195, + 182, 170, 159, 149, 142, 136, 132, 130, 130, 132, + 136, 142, 149, 159, 170, 182, 195, 210, 225, 241, +}; + +#if 0 +// +// re-compute sine table for input signal +// +#include +#define TABLE_SIZE 50 +static uint8_t sine[TABLE_SIZE]; +static void compute_signal(void){ + #define TABLE_SIZE (50) + // create sine wave table + int i; + printf("static const uint8_t sine[] = {"); + for( i=0; i= sizeof(sine)) phase = 0; + } + hci_send_sco_packet_buffer(3 + frames_per_packet); + static int count = 0; + count++; + if ((count & 15) == 0) printf("Sent %u\n", count); +} + +static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){ + static int count = 0; + count++; + if ((count & 15)) return; + printf("SCO packets %u\n", count); + // hexdumpf(packet, size); +} + +static void packet_handler(uint8_t * event, uint16_t event_size){ + + // printf("Packet handler event 0x%02x\n", event[0]); + + try_send_sco(); + + switch (event[0]) { + case BTSTACK_EVENT_STATE: + if (event[2] != HCI_STATE_WORKING) break; + printf("Working!\n"); + break; + case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: + // printf("HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS\n"); + // try_send_sco(); + break; + case DAEMON_EVENT_HCI_PACKET_SENT: + // printf("DAEMON_EVENT_HCI_PACKET_SENT\n"); + // try_send_sco(); + break; + case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: + // printf("HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE status %u, %x\n", event[2], READ_BT_16(event, 3)); + if (event[2]) break; + sco_handle = READ_BT_16(event, 3); + break; + case HCI_EVENT_HSP_META: + switch (event[2]) { + case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE: + if (event[3] == 0){ + printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); + // try_send_sco(); + } else { + printf("Audio connection establishment failed with status %u\n", event[3]); + } + break; + case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE: + if (event[3] == 0){ + printf("Audio connection released.\n\n"); + sco_handle = 0; + } else { + printf("Audio connection releasing failed with status %u\n", event[3]); + } + break; + case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED: + printf("Received microphone gain change %d\n", event[3]); + break; + case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED: + printf("Received speaker gain change %d\n", event[3]); + break; + case HSP_SUBEVENT_RING: + printf("HS: RING RING!\n"); + break; + case HSP_SUBEVENT_AG_INDICATION: + memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer)); + int size = event_size <= sizeof(hs_cmd_buffer)? event_size : sizeof(hs_cmd_buffer); + memcpy(hs_cmd_buffer, &event[3], size - 1); + printf("Received custom indication: \"%s\". \nExit code or call hsp_hs_send_result.\n", hs_cmd_buffer); + break; + default: + printf("event not handled %u\n", event[2]); + break; + } + break; + default: + break; + } +} + +int btstack_main(int argc, const char * argv[]); +int btstack_main(int argc, const char * argv[]){ + +#ifdef TABLE_SIZE + compute_signal(); +#endif + + hci_register_sco_packet_handler(&sco_packet_handler); + + hci_discoverable_control(1); + hci_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); + gap_set_local_name("BTstack HSP HS"); + + hsp_hs_init(rfcomm_channel_nr); + hsp_hs_register_packet_handler(packet_handler); + + sdp_init(); + memset((uint8_t *)hsp_service_buffer, 0, sizeof(hsp_service_buffer)); + hsp_hs_create_service((uint8_t *)hsp_service_buffer, rfcomm_channel_nr, hsp_hs_service_name, 0); + + sdp_register_service_internal(NULL, (uint8_t *)hsp_service_buffer); + + // turn on! + hci_power_control(HCI_POWER_ON); + + return 0; +} diff --git a/platforms/posix-cc2564b/Makefile b/platforms/posix-cc2564b/Makefile index 9b9791495..fb57cd9a3 100644 --- a/platforms/posix-cc2564b/Makefile +++ b/platforms/posix-cc2564b/Makefile @@ -13,9 +13,10 @@ include ${BTSTACK_ROOT}/chipset-cc256x/Makefile.inc CFLAGS += -I$(BTSTACK_ROOT)/chipset-cc256x -# CC = gcc-fsf-4.9 CFLAGS += -g -Wall -# CFLAGS += -Werror + +# +CFLAGS += -Wmissing-prototypes -Wstrict-prototypes -Wshadow -Werror VPATH += ${BTSTACK_ROOT}/platforms/posix/src VPATH += ${BTSTACK_ROOT}/chipset-cc256x diff --git a/platforms/posix-cc2564b/btstack-config.h b/platforms/posix-cc2564b/btstack-config.h index 0448f2826..6aa63e669 100644 --- a/platforms/posix-cc2564b/btstack-config.h +++ b/platforms/posix-cc2564b/btstack-config.h @@ -3,22 +3,23 @@ #ifndef __BTSTACK_CONFIG #define __BTSTACK_CONFIG -#define HAVE_TRANSPORT_USB +#define ENABLE_LOG_ERROR +#define ENABLE_LOG_INFO #define HAVE_BLE -#define USE_POSIX_RUN_LOOP -#define HAVE_SDP +#define HAVE_BZERO +#define HAVE_HCI_DUMP +#define HAVE_MALLOC #define HAVE_RFCOMM -#define REMOTE_DEVICE_DB remote_device_db_iphone +#define HAVE_SDP #define HAVE_SO_NOSIGPIPE #define HAVE_TIME -#define HAVE_MALLOC -#define HAVE_BZERO -#define SDP_DES_DUMP -#define ENABLE_LOG_INFO -#define ENABLE_LOG_ERROR -#define HCI_INCOMING_PRE_BUFFER_SIZE 14 // sizeof benep heade, avoid memcpy +#define HAVE_SCO +#define HAVE_SCO_OVER_HCI #define HCI_ACL_PAYLOAD_SIZE (1691 + 4) -#define HAVE_HCI_DUMP +#define HCI_INCOMING_PRE_BUFFER_SIZE 14 // sizeof BNEP header, avoid memcpy +#define REMOTE_DEVICE_DB remote_device_db_iphone #define SDP_DES_DUMP +#define SDP_DES_DUMP +#define USE_POSIX_RUN_LOOP #endif diff --git a/platforms/posix-cc2564b/main.c b/platforms/posix-cc2564b/main.c index b37a630d1..f1da871e9 100644 --- a/platforms/posix-cc2564b/main.c +++ b/platforms/posix-cc2564b/main.c @@ -93,11 +93,23 @@ int main(int argc, const char * argv[]){ btstack_memory_init(); run_loop_init(RUN_LOOP_POSIX); +#if 0 + // Ubuntu + + // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT + hci_dump_open("hci_dump.pklg", HCI_DUMP_PACKETLOGGER); + + // pick serial port + hci_uart_config_cc256x.device_name = "/dev/ttyUSB0"; +#else + // OS X + // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER); // pick serial port hci_uart_config_cc256x.device_name = "/dev/tty.usbserial-A900K0VK"; +#endif // init HCI hci_transport_t * transport = hci_transport_h4_instance(); diff --git a/src/hci.c b/src/hci.c index 93848d67c..c485e5361 100644 --- a/src/hci.c +++ b/src/hci.c @@ -1000,6 +1000,10 @@ static void hci_initializing_run(void){ hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; break; + case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: + hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; + hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled + break; #ifdef HAVE_BLE // LE INIT case HCI_INIT_LE_READ_BUFFER_SIZE: @@ -1188,17 +1192,34 @@ static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ return; } break; + case HCI_INIT_W4_WRITE_PAGE_TIMEOUT: + break; case HCI_INIT_W4_LE_READ_BUFFER_SIZE: // skip write le host if not supported (e.g. on LE only EM9301) if (hci_stack->local_supported_commands[0] & 0x02) break; hci_stack->substate = HCI_INIT_LE_SET_SCAN_PARAMETERS; return; + +#ifdef HAVE_SCO_OVER_HCI + case HCI_INIT_W4_WRITE_SCAN_ENABLE: + // just go to next state + break; + case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: + if (!hci_le_supported()){ + // SKIP LE init for Classic only configuration + hci_stack->substate = HCI_INIT_DONE; + return; + } + break; +#else case HCI_INIT_W4_WRITE_SCAN_ENABLE: if (!hci_le_supported()){ // SKIP LE init for Classic only configuration hci_stack->substate = HCI_INIT_DONE; return; } +#endif + break; default: break; } @@ -1303,6 +1324,11 @@ static void event_handler(uint8_t *packet, int size){ (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0X80) >> 7 | // Octet 14, bit 7 (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5; // Octet 24, bit 6 } + if (COMMAND_COMPLETE_EVENT(packet, hci_write_synchronous_flow_control_enable)){ + if (packet[5] == 0){ + hci_stack->synchronous_flow_control_enabled = 1; + } + } break; case HCI_EVENT_COMMAND_STATUS: diff --git a/src/hci.h b/src/hci.h index 1cd24df9a..99cbf7bba 100644 --- a/src/hci.h +++ b/src/hci.h @@ -591,15 +591,16 @@ typedef enum hci_init_state{ HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE, HCI_INIT_WRITE_PAGE_TIMEOUT, HCI_INIT_W4_WRITE_PAGE_TIMEOUT, - // HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, - // HCI_INIT_W4_SYNCHRONOUS_FLOW_CONTROL_ENABLE, - HCI_INIT_WRITE_CLASS_OF_DEVICE, HCI_INIT_W4_WRITE_CLASS_OF_DEVICE, HCI_INIT_WRITE_LOCAL_NAME, HCI_INIT_W4_WRITE_LOCAL_NAME, HCI_INIT_WRITE_SCAN_ENABLE, HCI_INIT_W4_WRITE_SCAN_ENABLE, + + HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, + HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE, + HCI_INIT_LE_READ_BUFFER_SIZE, HCI_INIT_W4_LE_READ_BUFFER_SIZE, HCI_INIT_WRITE_LE_HOST_SUPPORTED, diff --git a/src/hsp_hs.c b/src/hsp_hs.c index a8f037ed2..4b413e78d 100644 --- a/src/hsp_hs.c +++ b/src/hsp_hs.c @@ -168,9 +168,10 @@ void hsp_hs_create_service(uint8_t * service, int rfcomm_channel_nr, const char de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); attribute = de_push_sequence(service); { - // "UUID for PAN Service" - de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_Headset_HS); - de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); + // see Bluetooth Erratum #3507 + de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_HSP); // 0x1108 + de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_Headset_HS); // 0x1131 + de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); // 0x1203 } de_pop_sequence(service, attribute); @@ -205,12 +206,12 @@ void hsp_hs_create_service(uint8_t * service, int rfcomm_channel_nr, const char de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); attribute = de_push_sequence(service); { - uint8_t *sppProfile = de_push_sequence(attribute); + uint8_t *hsp_profile = de_push_sequence(attribute); { - de_add_number(sppProfile, DE_UUID, DE_SIZE_16, SDP_HSP); - de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0102); // Verision 1.2 + de_add_number(hsp_profile, DE_UUID, DE_SIZE_16, SDP_HSP); + de_add_number(hsp_profile, DE_UINT, DE_SIZE_16, 0x0102); // Verision 1.2 } - de_pop_sequence(attribute, sppProfile); + de_pop_sequence(attribute, hsp_profile); } de_pop_sequence(service, attribute);