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_ag_demo.c" 39 40 /* 41 * hsp_ag_demo.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(hsp_ag_demo): HSP AG - Audio Gateway 46 * 47 * @text This example implements a HSP Audio Gateway device that sends and receives 48 * audio signal over HCI SCO. It demonstrates how to receive 49 * an output from a remote headset (HS), and, 50 * if HAVE_BTSTACK_STDIN is defined, how to control the HS. 51 */ 52 // ***************************************************************************** 53 54 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 uint8_t hsp_service_buffer[150]; 68 static const uint8_t rfcomm_channel_nr = 1; 69 static const char hsp_ag_service_name[] = "Audio Gateway Test"; 70 static uint16_t sco_handle = HCI_CON_HANDLE_INVALID; 71 72 static char hs_cmd_buffer[100]; 73 74 static const char * device_name = "HSP AG Demo 00:00:00:00:00:00"; 75 static const char * device_addr_string = "00:1b:dc:07:32:ef"; 76 static bd_addr_t device_addr; 77 78 /* @section Audio Transfer Setup 79 * 80 * @text A pre-computed sine wave (160Hz) is used as the input audio signal. 160 Hz. 81 * To send and receive an audio signal, ENABLE_SCO_OVER_HCI has to be defined. 82 * 83 * Tested working setups: 84 * - Ubuntu 14 64-bit, CC2564B connected via FTDI USB-2-UART adapter, 921600 baud 85 * - Ubuntu 14 64-bit, CSR USB dongle 86 * - OS X 10.11, CSR USB dongle 87 * 88 * Broken setups: 89 * - OS X 10.11, CC2564B connected via FDTI USB-2-UART adapter, 921600 baud 90 * - select(..) blocks > 400 ms -> num completed is received to late -> gaps between audio 91 * - looks like bug in select->FTDI driver as it works correct on Linux 92 * 93 * SCO not routed over HCI yet: 94 * - CSR UART dongle 95 * - Broadcom USB dongle 96 * - Broadcom UART chipset 97 * - .. 98 * 99 */ 100 101 102 103 #ifdef HAVE_BTSTACK_STDIN 104 static void show_usage(void){ 105 bd_addr_t iut_address; 106 gap_local_bd_addr(iut_address); 107 108 printf("\n--- Bluetooth HSP Audio Gateway Test Console %s ---\n", bd_addr_to_str(iut_address)); 109 110 printf("---\n"); 111 printf("c - Connect to %s\n", device_addr_string); 112 printf("C - Disconnect\n"); 113 printf("a - establish audio connection\n"); 114 printf("A - release audio connection\n"); 115 printf("D - delete all link keys\n"); 116 printf("m - set microphone gain 8\n"); 117 printf("M - set microphone gain 15\n"); 118 printf("o - set speaker gain 0\n"); 119 printf("s - set speaker gain 8\n"); 120 printf("S - set speaker gain 15\n"); 121 printf("r - start ringing\n"); 122 printf("t - stop ringing\n"); 123 printf("\n"); 124 } 125 126 static void stdin_process(char c){ 127 switch (c){ 128 case 'a': 129 printf("Establish audio connection\n"); 130 hsp_ag_establish_audio_connection(); 131 break; 132 case 'A': 133 printf("Release audio connection\n"); 134 hsp_ag_release_audio_connection(); 135 break; 136 case 'c': 137 printf("Connect to %s\n", device_addr_string); 138 hsp_ag_connect(device_addr); 139 break; 140 case 'C': 141 printf("Disconnect.\n"); 142 hsp_ag_disconnect(); 143 break; 144 case 'D': 145 printf("Deleting all link keys\n"); 146 gap_delete_all_link_keys(); 147 break; 148 case 'm': 149 printf("Setting microphone gain 8\n"); 150 hsp_ag_set_microphone_gain(8); 151 break; 152 case 'M': 153 printf("Setting microphone gain 15\n"); 154 hsp_ag_set_microphone_gain(15); 155 break; 156 case 'o': 157 printf("Setting speaker gain 0\n"); 158 hsp_ag_set_speaker_gain(0); 159 break; 160 case 's': 161 printf("Setting speaker gain 8\n"); 162 hsp_ag_set_speaker_gain(8); 163 break; 164 case 'S': 165 printf("Setting speaker gain 15\n"); 166 hsp_ag_set_speaker_gain(15); 167 break; 168 case 'r': 169 printf("Start ringing\n"); 170 hsp_ag_start_ringing(); 171 break; 172 case 't': 173 printf("Stop ringing\n"); 174 hsp_ag_stop_ringing(); 175 break; 176 default: 177 show_usage(); 178 break; 179 } 180 } 181 #endif 182 183 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 184 UNUSED(channel); 185 uint8_t status; 186 187 switch (packet_type){ 188 case HCI_SCO_DATA_PACKET: 189 if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break; 190 sco_demo_receive(event, event_size); 191 break; 192 193 case HCI_EVENT_PACKET: 194 switch (hci_event_packet_get_type(event)) { 195 #ifndef HAVE_BTSTACK_STDIN 196 case BTSTACK_EVENT_STATE: 197 if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break; 198 printf("Establish HSP AG service to %s...\n", device_addr_string); 199 hsp_ag_connect(device_addr); 200 break; 201 #endif 202 case HCI_EVENT_SCO_CAN_SEND_NOW: 203 sco_demo_send(sco_handle); 204 break; 205 case HCI_EVENT_HSP_META: 206 switch (hci_event_hsp_meta_get_subevent_code(event)) { 207 case HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE: 208 status = hsp_subevent_rfcomm_connection_complete_get_status(event); 209 if (status != ERROR_CODE_SUCCESS){ 210 printf("RFCOMM connection establishement failed, status 0x%02x\n", status); 211 break; 212 } 213 printf("RFCOMM connection established.\n"); 214 #ifndef HAVE_BTSTACK_STDIN 215 printf("Establish Audio connection to %s...\n", device_addr_string); 216 hsp_ag_establish_audio_connection(); 217 #endif 218 break; 219 case HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE: 220 printf("RFCOMM disconnected.\n"); 221 break; 222 case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE: 223 status = hsp_subevent_audio_connection_complete_get_status(event); 224 if (status != ERROR_CODE_SUCCESS){ 225 printf("Audio connection establishment failed, status 0x%02x\n", status); 226 } else { 227 sco_handle = hsp_subevent_audio_connection_complete_get_sco_handle(event); 228 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 229 hci_request_sco_can_send_now_event(); 230 } 231 break; 232 case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE: 233 printf("Audio connection released.\n\n"); 234 sco_handle = HCI_CON_HANDLE_INVALID; 235 break; 236 case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED: 237 printf("Received microphone gain change %d\n", hsp_subevent_microphone_gain_changed_get_gain(event)); 238 break; 239 case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED: 240 printf("Received speaker gain change %d\n", hsp_subevent_speaker_gain_changed_get_gain(event)); 241 break; 242 case HSP_SUBEVENT_HS_COMMAND:{ 243 memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer)); 244 unsigned int cmd_length = hsp_subevent_hs_command_get_value_length(event); 245 unsigned int size = cmd_length <= sizeof(hs_cmd_buffer)? cmd_length : sizeof(hs_cmd_buffer); 246 memcpy(hs_cmd_buffer, hsp_subevent_hs_command_get_value(event), size - 1); 247 printf("Received custom command: \"%s\". \nExit code or call hsp_ag_send_result.\n", hs_cmd_buffer); 248 break; 249 } 250 case HSP_SUBEVENT_BUTTON_PRESSED: 251 if (sco_handle == HCI_CON_HANDLE_INVALID){ 252 printf("Button event -> establish audio\n"); 253 hsp_ag_establish_audio_connection(); 254 } else { 255 printf("Button event -> release audio\n"); 256 hsp_ag_release_audio_connection(); 257 } 258 break; 259 default: 260 printf("event not handled 0x%02x\n", event[2]); 261 break; 262 } 263 break; 264 default: 265 break; 266 } 267 break; 268 default: 269 break; 270 } 271 } 272 273 /* @section Main Application Setup 274 * 275 * @text Listing MainConfiguration shows main application code. 276 * To run a HSP Audio Gateway service you need to initialize the SDP, and to create and register HSP AG record with it. 277 * In this example, the SCO over HCI is used to receive and send an audio signal. 278 * 279 * Two packet handlers are registered: 280 * - The HCI SCO packet handler receives audio data. 281 * - The HSP AG packet handler is used to trigger sending of audio data and commands to the HS. It also receives the AG's answers. 282 * 283 * The stdin_process callback allows for sending commands to the AG. 284 * At the end the Bluetooth stack is started. 285 */ 286 287 /* LISTING_START(MainConfiguration): Setup HSP Audio Gateway */ 288 int btstack_main(int argc, const char * argv[]); 289 int btstack_main(int argc, const char * argv[]){ 290 (void)argc; 291 (void)argv; 292 293 sco_demo_init(); 294 sco_demo_set_codec(HFP_CODEC_CVSD); 295 296 l2cap_init(); 297 298 #ifdef ENABLE_BLE 299 // Initialize LE Security Manager. Needed for cross-transport key derivation 300 sm_init(); 301 #endif 302 303 sdp_init(); 304 305 memset((uint8_t *)hsp_service_buffer, 0, sizeof(hsp_service_buffer)); 306 hsp_ag_create_sdp_record(hsp_service_buffer, sdp_create_service_record_handle(), rfcomm_channel_nr, hsp_ag_service_name); 307 btstack_assert(de_get_len( hsp_service_buffer) <= sizeof(hsp_service_buffer)); 308 sdp_register_service(hsp_service_buffer); 309 310 rfcomm_init(); 311 312 hsp_ag_init(rfcomm_channel_nr); 313 hsp_ag_register_packet_handler(&packet_handler); 314 315 // register for SCO packets 316 hci_register_sco_packet_handler(&packet_handler); 317 318 // parse human readable Bluetooth address 319 sscanf_bd_addr(device_addr_string, device_addr); 320 321 #ifdef HAVE_BTSTACK_STDIN 322 btstack_stdin_setup(stdin_process); 323 #endif 324 325 gap_set_local_name(device_name); 326 gap_discoverable_control(1); 327 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 328 gap_set_class_of_device(0x400204); 329 330 // turn on! 331 hci_power_control(HCI_POWER_ON); 332 return 0; 333 } 334 /* LISTING_END */ 335 /* EXAMPLE_END */ 336