xref: /btstack/example/hsp_hs_demo.c (revision 25234a7e05f3e69524e0ba3eec834a229a86d91b)
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_hs_demo.c
40  */
41 
42 // *****************************************************************************
43 /* EXAMPLE_START(hsp_hs_demo): HSP Headset Demo
44  *
45  * @text This example implements a HSP Headset device that sends and receives
46  * audio signal over HCI SCO. It demonstrates how to receive
47  * an output from a remote audio gateway (AG), and,
48  * if HAVE_POSIX_STDIN is defined, how to control the AG.
49  */
50 // *****************************************************************************
51 
52 #include "btstack_config.h"
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 btstack_packet_callback_registration_t hci_event_callback_registration;
67 
68 static uint8_t hsp_service_buffer[150];
69 static const uint8_t rfcomm_channel_nr = 1;
70 static const char    hsp_hs_service_name[] = "Headset Test";
71 static hci_con_handle_t sco_handle = 0;
72 
73 static char hs_cmd_buffer[100];
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 Headset Test Console %s ---\n", bd_addr_to_str(iut_address));
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("b - press user button\n");
111     printf("z - set microphone gain 0\n");
112     printf("m - set microphone gain 8\n");
113     printf("M - set microphone gain 15\n");
114     printf("o - set speaker gain 0\n");
115     printf("s - set speaker gain 8\n");
116     printf("S - set speaker gain 15\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_hs_connect(device_addr);
131             break;
132         case 'C':
133             printf("Disconnect.\n");
134             hsp_hs_disconnect();
135             break;
136         case 'a':
137             printf("Establish audio connection\n");
138             hsp_hs_establish_audio_connection();
139             break;
140         case 'A':
141             printf("Release audio connection\n");
142             hsp_hs_release_audio_connection();
143             break;
144 
145         case 'z':
146             printf("Setting microphone gain 0\n");
147             hsp_hs_set_microphone_gain(0);
148             break;
149         case 'm':
150             printf("Setting microphone gain 8\n");
151             hsp_hs_set_microphone_gain(8);
152             break;
153         case 'M':
154             printf("Setting microphone gain 15\n");
155             hsp_hs_set_microphone_gain(15);
156             break;
157         case 'o':
158             printf("Setting speaker gain 0\n");
159             hsp_hs_set_speaker_gain(0);
160             break;
161         case 's':
162             printf("Setting speaker gain 8\n");
163             hsp_hs_set_speaker_gain(8);
164             break;
165         case 'S':
166             printf("Setting speaker gain 15\n");
167             hsp_hs_set_speaker_gain(15);
168             break;
169         case 'b':
170             printf("Press user button\n");
171             hsp_hs_send_button_press();
172             break;
173         default:
174             show_usage();
175             break;
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     switch (packet_type){
182         case HCI_SCO_DATA_PACKET:
183             sco_demo_receive(event, event_size);
184             break;
185         case HCI_EVENT_PACKET:
186             switch (event[0]) {
187                 case BTSTACK_EVENT_STATE:
188                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
189                     show_usage();
190                     break;
191                 case HCI_EVENT_SCO_CAN_SEND_NOW:
192                     sco_demo_send(sco_handle);
193                     break;
194                 case HCI_EVENT_HSP_META:
195                     switch (event[2]) {
196                         case HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE:
197                             if (hsp_subevent_rfcomm_connection_complete_get_status(event)){
198                                 printf("RFCOMM connection establishement failed with status %u\n", hsp_subevent_rfcomm_connection_complete_get_status(event));
199                             } else {
200                                 printf("RFCOMM connection established.\n");
201                             }
202                             break;
203                         case HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE:
204                             if (hsp_subevent_rfcomm_disconnection_complete_get_status(event)){
205                                 printf("RFCOMM disconnection failed with status %u.\n", hsp_subevent_rfcomm_disconnection_complete_get_status(event));
206                             } else {
207                                 printf("RFCOMM disconnected.\n");
208                             }
209                             break;
210                         case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE:
211                             if (hsp_subevent_audio_connection_complete_get_status(event)){
212                                 printf("Audio connection establishment failed with status %u\n", hsp_subevent_audio_connection_complete_get_status(event));
213                                 sco_handle = 0;
214                             } else {
215                                 sco_handle = hsp_subevent_audio_connection_complete_get_handle(event);
216                                 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
217                                 hci_request_sco_can_send_now_event();
218                             }
219                             break;
220                         case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE:
221                             if (hsp_subevent_audio_disconnection_complete_get_status(event)){
222                                 printf("Audio connection releasing failed with status %u\n", hsp_subevent_audio_disconnection_complete_get_status(event));
223                             } else {
224                                 printf("Audio connection released.\n\n");
225                                 sco_handle = 0;
226                             }
227                             break;
228                         case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED:
229                             printf("Received microphone gain change %d\n", hsp_subevent_microphone_gain_changed_get_gain(event));
230                             break;
231                         case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED:
232                             printf("Received speaker gain change %d\n", hsp_subevent_speaker_gain_changed_get_gain(event));
233                             break;
234                         case HSP_SUBEVENT_RING:
235                             printf("HS: RING RING!\n");
236                             break;
237                         case HSP_SUBEVENT_AG_INDICATION: {
238                             memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer));
239                             int size = hsp_subevent_ag_indication_get_value_length(event);
240                             if (size >= sizeof(hs_cmd_buffer)-1){
241                                 size =  sizeof(hs_cmd_buffer)-1;
242                             }
243                             memcpy(hs_cmd_buffer, hsp_subevent_ag_indication_get_value(event), size);
244                             printf("Received custom indication: \"%s\". \nExit code or call hsp_hs_send_result.\n", hs_cmd_buffer);
245 
246                         }
247                             break;
248                         default:
249                             printf("event not handled %u\n", event[2]);
250                             break;
251                     }
252                     break;
253                 default:
254                     break;
255             }
256         default:
257             break;
258     }
259 }
260 
261 /* @section Main Application Setup
262  *
263  * @text Listing MainConfiguration shows main application code.
264  * To run a HSP Headset service you need to initialize the SDP, and to create and register HSP HS record with it.
265  * In this example, the SCO over HCI is used to receive and send an audio signal.
266  *
267  * Two packet handlers are registered:
268  * - The HCI SCO packet handler receives audio data.
269  * - 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.
270  *
271  * The stdin_process callback allows for sending commands to the AG.
272  * At the end the Bluetooth stack is started.
273  */
274 
275 /* LISTING_START(MainConfiguration): Setup HSP Headset */
276 int btstack_main(int argc, const char * argv[]);
277 int btstack_main(int argc, const char * argv[]){
278 
279     sco_demo_init();
280 
281     // register for HCI events
282     hci_event_callback_registration.callback = &packet_handler;
283     hci_add_event_handler(&hci_event_callback_registration);
284     hci_register_sco_packet_handler(&packet_handler);
285 
286     l2cap_init();
287 
288     sdp_init();
289     memset(hsp_service_buffer, 0, sizeof(hsp_service_buffer));
290     hsp_hs_create_sdp_record(hsp_service_buffer, 0x10001, rfcomm_channel_nr, hsp_hs_service_name, 0);
291     sdp_register_service(hsp_service_buffer);
292 
293     rfcomm_init();
294 
295     hsp_hs_init(rfcomm_channel_nr);
296     hsp_hs_register_packet_handler(packet_handler);
297 
298 #ifdef HAVE_POSIX_STDIN
299     btstack_stdin_setup(stdin_process);
300 #endif
301 
302     gap_set_local_name("BTstack HSP HS");
303     gap_discoverable_control(1);
304     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
305     gap_set_class_of_device(0x240404);
306 
307     // turn on!
308     hci_power_control(HCI_POWER_ON);
309     return 0;
310 }
311 /* LISTING_END */
312 /* EXAMPLE_END */
313