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