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