1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "hsp_hs_demo.c" 39 40 /* 41 * hsp_hs_demo.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(hsp_hs_demo): HSP HS - Headset 46 * 47 * @text This example implements a HSP Headset device that sends and receives 48 * audio signal over HCI SCO. It demonstrates how to receive 49 * an output from a remote audio gateway (AG), and, 50 * if HAVE_BTSTACK_STDIN is defined, how to control the AG. 51 */ 52 // ***************************************************************************** 53 54 #include "btstack_config.h" 55 56 #include <stdint.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 61 #include "btstack.h" 62 #include "sco_demo_util.h" 63 #ifdef HAVE_BTSTACK_STDIN 64 #include "btstack_stdin.h" 65 #endif 66 67 static btstack_packet_callback_registration_t hci_event_callback_registration; 68 69 static uint8_t hsp_service_buffer[150]; 70 static const uint8_t rfcomm_channel_nr = 1; 71 static const char hsp_hs_service_name[] = "Headset Test"; 72 static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID; 73 74 static char hs_cmd_buffer[100]; 75 // mac 2013: 76 static const char * device_addr_string = "84:38:35:65:d1:15"; 77 static bd_addr_t device_addr; 78 79 /* @section Audio Transfer Setup 80 * 81 * @text A pre-computed sine wave (160Hz) is used as the input audio signal. 160 Hz. 82 * To send and receive an audio signal, ENABLE_SCO_OVER_HCI has to be defined. 83 * 84 * Tested working setups: 85 * - Ubuntu 14 64-bit, CC2564B connected via FTDI USB-2-UART adapter, 921600 baud 86 * - Ubuntu 14 64-bit, CSR USB dongle 87 * - OS X 10.11, CSR USB dongle 88 * 89 * Broken setups: 90 * - OS X 10.11, CC2564B connected via FDTI USB-2-UART adapter, 921600 baud 91 * - select(..) blocks > 400 ms -> num completed is received to late -> gaps between audio 92 * - looks like bug in select->FTDI driver as it works correct on Linux 93 * 94 * SCO not routed over HCI yet: 95 * - CSR UART dongle 96 * - Broadcom USB dongle 97 * - Broadcom UART chipset 98 * - .. 99 * 100 */ 101 102 103 static void show_usage(void){ 104 bd_addr_t iut_address; 105 gap_local_bd_addr(iut_address); 106 107 printf("\n--- Bluetooth HSP Headset Test Console %s ---\n", bd_addr_to_str(iut_address)); 108 printf("---\n"); 109 printf("c - Connect to %s\n", bd_addr_to_str(device_addr)); 110 printf("C - Disconnect\n"); 111 printf("a - establish audio connection\n"); 112 printf("A - release audio connection\n"); 113 printf("b - press user button\n"); 114 printf("z - set microphone gain 0\n"); 115 printf("m - set microphone gain 8\n"); 116 printf("M - set microphone gain 15\n"); 117 printf("o - set speaker gain 0\n"); 118 printf("s - set speaker gain 8\n"); 119 printf("S - set speaker gain 15\n"); 120 printf("\n"); 121 } 122 123 #ifdef HAVE_BTSTACK_STDIN 124 static void stdin_process(char c){ 125 switch (c){ 126 case 'c': 127 printf("Connect to %s\n", bd_addr_to_str(device_addr)); 128 hsp_hs_connect(device_addr); 129 break; 130 case 'C': 131 printf("Disconnect.\n"); 132 hsp_hs_disconnect(); 133 break; 134 case 'a': 135 printf("Establish audio connection\n"); 136 hsp_hs_establish_audio_connection(); 137 break; 138 case 'A': 139 printf("Release audio connection\n"); 140 hsp_hs_release_audio_connection(); 141 break; 142 143 case 'z': 144 printf("Setting microphone gain 0\n"); 145 hsp_hs_set_microphone_gain(0); 146 break; 147 case 'm': 148 printf("Setting microphone gain 8\n"); 149 hsp_hs_set_microphone_gain(8); 150 break; 151 case 'M': 152 printf("Setting microphone gain 15\n"); 153 hsp_hs_set_microphone_gain(15); 154 break; 155 case 'o': 156 printf("Setting speaker gain 0\n"); 157 hsp_hs_set_speaker_gain(0); 158 break; 159 case 's': 160 printf("Setting speaker gain 8\n"); 161 hsp_hs_set_speaker_gain(8); 162 break; 163 case 'S': 164 printf("Setting speaker gain 15\n"); 165 hsp_hs_set_speaker_gain(15); 166 break; 167 case 'b': 168 printf("Press user button\n"); 169 hsp_hs_send_button_press(); 170 break; 171 default: 172 show_usage(); 173 break; 174 } 175 } 176 #endif 177 178 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 179 UNUSED(channel); 180 uint8_t status; 181 182 switch (packet_type){ 183 case HCI_SCO_DATA_PACKET: 184 sco_demo_receive(event, event_size); 185 break; 186 case HCI_EVENT_PACKET: 187 switch (hci_event_packet_get_type(event)) { 188 case BTSTACK_EVENT_STATE: 189 if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break; 190 show_usage(); 191 break; 192 case HCI_EVENT_SCO_CAN_SEND_NOW: 193 if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break; 194 sco_demo_send(sco_handle); 195 break; 196 case HCI_EVENT_HSP_META: 197 switch (hci_event_hsp_meta_get_subevent_code(event)) { 198 case HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE: 199 status = hsp_subevent_rfcomm_connection_complete_get_status(event); 200 if (status != ERROR_CODE_SUCCESS){ 201 printf("RFCOMM connection establishment failed, status 0x%02x\n", status); 202 } else { 203 printf("RFCOMM connection established.\n"); 204 } 205 break; 206 case HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE: 207 printf("RFCOMM disconnected.\n"); 208 break; 209 case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE: 210 status = hsp_subevent_audio_connection_complete_get_status(event); 211 if (status != ERROR_CODE_SUCCESS){ 212 printf("Audio connection establishment failed, status 0x%02x\n", status); 213 } else { 214 sco_handle = hsp_subevent_audio_connection_complete_get_sco_handle(event); 215 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 216 hci_request_sco_can_send_now_event(); 217 } 218 break; 219 case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE: 220 printf("Audio connection released.\n\n"); 221 sco_handle = HCI_CON_HANDLE_INVALID; 222 break; 223 case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED: 224 printf("Received microphone gain change %d\n", hsp_subevent_microphone_gain_changed_get_gain(event)); 225 break; 226 case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED: 227 printf("Received speaker gain change %d\n", hsp_subevent_speaker_gain_changed_get_gain(event)); 228 break; 229 case HSP_SUBEVENT_RING: 230 printf("HS: RING RING!\n"); 231 break; 232 case HSP_SUBEVENT_AG_INDICATION: { 233 memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer)); 234 unsigned int size = hsp_subevent_ag_indication_get_value_length(event); 235 if (size >= sizeof(hs_cmd_buffer)-1){ 236 size = sizeof(hs_cmd_buffer)-1; 237 } 238 memcpy(hs_cmd_buffer, hsp_subevent_ag_indication_get_value(event), size); 239 printf("Received custom indication: \"%s\". \nExit code or call hsp_hs_send_result.\n", hs_cmd_buffer); 240 break; 241 } 242 default: 243 printf("event not handled 0x%02x\n", hci_event_hsp_meta_get_subevent_code(event)); 244 break; 245 } 246 break; 247 default: 248 break; 249 } 250 default: 251 break; 252 } 253 } 254 255 /* @section Main Application Setup 256 * 257 * @text Listing MainConfiguration shows main application code. 258 * To run a HSP Headset service you need to initialize the SDP, and to create and register HSP HS record with it. 259 * In this example, the SCO over HCI is used to receive and send an audio signal. 260 * 261 * Two packet handlers are registered: 262 * - The HCI SCO packet handler receives audio data. 263 * - The HSP HS packet handler is used to trigger sending of audio data and commands to the AG. It also receives the AG's answers. 264 * 265 * The stdin_process callback allows for sending commands to the AG. 266 * At the end the Bluetooth stack is started. 267 */ 268 269 /* LISTING_START(MainConfiguration): Setup HSP Headset */ 270 int btstack_main(int argc, const char * argv[]); 271 int btstack_main(int argc, const char * argv[]){ 272 (void)argc; 273 (void)argv; 274 275 sco_demo_init(); 276 sco_demo_set_codec(HFP_CODEC_CVSD); 277 278 l2cap_init(); 279 280 #ifdef ENABLE_BLE 281 // Initialize LE Security Manager. Needed for cross-transport key derivation 282 sm_init(); 283 #endif 284 285 sdp_init(); 286 memset(hsp_service_buffer, 0, sizeof(hsp_service_buffer)); 287 hsp_hs_create_sdp_record(hsp_service_buffer, 0x10001, rfcomm_channel_nr, hsp_hs_service_name, 0); 288 sdp_register_service(hsp_service_buffer); 289 290 rfcomm_init(); 291 292 hsp_hs_init(rfcomm_channel_nr); 293 294 // register for HCI events and SCO packets 295 hci_event_callback_registration.callback = &packet_handler; 296 hci_add_event_handler(&hci_event_callback_registration); 297 hci_register_sco_packet_handler(&packet_handler); 298 299 // register for HSP events 300 hsp_hs_register_packet_handler(packet_handler); 301 302 #ifdef HAVE_BTSTACK_STDIN 303 btstack_stdin_setup(stdin_process); 304 #endif 305 306 gap_set_local_name("HSP HS Demo 00:00:00:00:00:00"); 307 gap_discoverable_control(1); 308 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 309 gap_set_class_of_device(0x240404); 310 311 // Parse human readable Bluetooth address. 312 sscanf_bd_addr(device_addr_string, device_addr); 313 314 // turn on! 315 hci_power_control(HCI_POWER_ON); 316 return 0; 317 } 318 /* LISTING_END */ 319 /* EXAMPLE_END */ 320