xref: /btstack/example/hsp_hs_demo.c (revision 7e80381ba16fc3d36aa6e74b4aa113ce05362aeb)
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 //
40 // Minimal test for HSP Headset (!! UNDER DEVELOPMENT !!)
41 //
42 // Requires ENABLE_SCO_OVER_HCI to be defined
43 //
44 // Tested working setups:
45 // - Ubuntu 14 64-bit, CC2564B connected via FTDI USB-2-UART adapter, 921600 baud
46 // - Ubuntu 14 64-bit, CSR dongle
47 // - OS X 10.11, CSR dongle
48 
49 // Broken setups:
50 // - OS X 10.11, CC2564B connected via FDTI USB-2-UART adapter, 921600 baud
51 //   - select(..) blocks > 400 ms -> num completed is received to late -> gaps between audio
52 //   - looks like bug in select->FTDI driver as it works correct on Linux
53 //
54 // SCO not routed over HCI (yet)
55 // - CSR UART dongle
56 // - Broadcom USB dongle
57 // - Broadcom UART chipset
58 // - ..
59 // *****************************************************************************
60 
61 #include "btstack_config.h"
62 
63 #include <stdint.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <math.h>
68 
69 #include "btstack.h"
70 
71 static btstack_packet_callback_registration_t hci_event_callback_registration;
72 
73 static uint8_t hsp_service_buffer[150];
74 static const uint8_t rfcomm_channel_nr = 1;
75 static const char    hsp_hs_service_name[] = "Headset Test";
76 static hci_con_handle_t sco_handle = 0;
77 
78 static char hs_cmd_buffer[100];
79 
80 static int phase = 0;
81 
82 // input signal: pre-computed sine wave, 160 Hz
83 static const uint8_t sine[] = {
84       0,  15,  31,  46,  61,  74,  86,  97, 107, 114,
85     120, 124, 126, 126, 124, 120, 114, 107,  97,  86,
86      74,  61,  46,  31,  15,   0, 241, 225, 210, 195,
87     182, 170, 159, 149, 142, 136, 132, 130, 130, 132,
88     136, 142, 149, 159, 170, 182, 195, 210, 225, 241,
89 };
90 
91 #if 0
92 //
93 // re-compute sine table for input signal
94 //
95 #include <math.h>
96 #define TABLE_SIZE 50
97 static uint8_t sine[TABLE_SIZE];
98 static void compute_signal(void){
99     #define TABLE_SIZE    (50)
100     // create sine wave table
101     int i;
102     printf("static const uint8_t sine[] = {");
103     for( i=0; i<TABLE_SIZE; i++ ) {
104         if ((i % 10) == 0) printf("\n    ");
105         sine[i] = (int8_t) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
106         printf("%3u, ", sine[i]);
107     }
108     printf("\n};\n");
109 }
110 #endif
111 
112 static void try_send_sco(void){
113     if (!sco_handle) return;
114 
115     while (hci_can_send_sco_packet_now()) {
116 
117         const int sco_packet_length = hci_get_sco_packet_length();
118         const int sco_payload_length = sco_packet_length - 3;
119         const int frames_per_packet = sco_payload_length;    // for 8-bit data. for 16-bit data it's /2
120 
121         hci_reserve_packet_buffer();
122         uint8_t * sco_packet = hci_get_outgoing_packet_buffer();
123         // set handle + flags
124         little_endian_store_16(sco_packet, 0, sco_handle);
125         // set len
126         sco_packet[2] = sco_payload_length;
127         int i;
128         for (i=0;i<frames_per_packet;i++){
129             sco_packet[3+i] = sine[phase];
130             phase++;
131             if (phase >= sizeof(sine)) phase = 0;
132         }
133         hci_send_sco_packet_buffer(sco_packet_length);
134         static int count = 0;
135         count++;
136         if ((count & 15) == 0) printf("Sent %u\n", count);
137     }
138 }
139 
140 static void sco_packet_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){
141     static int count = 0;
142     count++;
143     if ((count & 15)) return;
144     printf("SCO packets %u\n", count);
145 }
146 
147 static void packet_handler(uint8_t * event, uint16_t event_size){
148 
149     // printf("Packet handler event 0x%02x\n", event[0]);
150 
151     switch (event[0]) {
152         case BTSTACK_EVENT_STATE:
153             if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
154             printf("Working!\n");
155             break;
156         case HCI_EVENT_SCO_CAN_SEND_NOW:
157             try_send_sco();
158             break;
159         case HCI_EVENT_HSP_META:
160             switch (event[2]) {
161                 case HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE:
162                     if (hsp_subevent_audio_connection_complete_get_status(event)){
163                         sco_handle = hsp_subevent_audio_connection_complete_get_handle(event);
164                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
165                         try_send_sco();
166                     } else {
167                         printf("Audio connection establishment failed with status %u\n", hsp_subevent_audio_connection_complete_get_status(event));
168                         sco_handle = 0;
169                     }
170                     break;
171                 case HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE:
172                     printf("Audio connection released.\n\n");
173                     sco_handle = 0;
174                     break;
175                 case HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED:
176                     printf("Received microphone gain change %d\n", hsp_subevent_microphone_gain_changed_get_gain(event));
177                     break;
178                 case HSP_SUBEVENT_SPEAKER_GAIN_CHANGED:
179                     printf("Received speaker gain change %d\n", hsp_subevent_speaker_gain_changed_get_gain(event));
180                     break;
181                 case HSP_SUBEVENT_RING:
182                     printf("HS: RING RING!\n");
183                     break;
184                 case HSP_SUBEVENT_AG_INDICATION: {
185                     memset(hs_cmd_buffer, 0, sizeof(hs_cmd_buffer));
186                     int size = hsp_subevent_ag_indication_get_value_length(event);
187                     if (size >= sizeof(hs_cmd_buffer)-1){
188                         size =  sizeof(hs_cmd_buffer)-1;
189                     }
190                     memcpy(hs_cmd_buffer, hsp_subevent_ag_indication_get_value(event), size);
191                     printf("Received custom indication: \"%s\". \nExit code or call hsp_hs_send_result.\n", hs_cmd_buffer);
192 
193                 }
194                     break;
195                 default:
196                     printf("event not handled %u\n", event[2]);
197                     break;
198             }
199             break;
200         default:
201             break;
202     }
203 }
204 
205 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
206     packet_handler(packet, size);
207 }
208 
209 int btstack_main(int argc, const char * argv[]);
210 int btstack_main(int argc, const char * argv[]){
211 
212 #ifdef TABLE_SIZE
213     compute_signal();
214 #endif
215 
216     gap_discoverable_control(1);
217     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
218     gap_set_local_name("BTstack HSP HS");
219 
220     // register for HCI events
221     hci_event_callback_registration.callback = &handle_hci_event;
222     hci_add_event_handler(&hci_event_callback_registration);
223 
224     // register for SCO packets
225     hci_register_sco_packet_handler(&sco_packet_handler);
226 
227     hsp_hs_init(rfcomm_channel_nr);
228     hsp_hs_register_packet_handler(packet_handler);
229 
230     sdp_init();
231     memset(hsp_service_buffer, 0, sizeof(hsp_service_buffer));
232     hsp_hs_create_sdp_record(hsp_service_buffer, 0x10001, rfcomm_channel_nr, hsp_hs_service_name, 0);
233     sdp_register_service(hsp_service_buffer);
234 
235     // turn on!
236     hci_power_control(HCI_POWER_ON);
237 
238     return 0;
239 }
240