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