xref: /btstack/src/classic/hsp_hs.c (revision eda0480f018f79db7599dc8db1ce251f24acf9dd)
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 // HSP Headset
41 //
42 // *****************************************************************************
43 
44 #include "btstack_config.h"
45 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "btstack_debug.h"
52 #include "btstack_event.h"
53 #include "btstack_memory.h"
54 #include "btstack_run_loop.h"
55 #include "classic/sdp_server.h"
56 #include "classic/sdp_query_rfcomm.h"
57 #include "hci.h"
58 #include "hci_cmd.h"
59 #include "hci_dump.h"
60 #include "hsp_hs.h"
61 #include "l2cap.h"
62 
63 #define HSP_AG_OK "OK"
64 #define HSP_AG_ERROR "ERROR"
65 #define HSP_AG_RING "RING"
66 #define HSP_MICROPHONE_GAIN "+VGM="
67 #define HSP_SPEAKER_GAIN "+VGS="
68 
69 #define HSP_HS_AT_CKPD "AT+CKPD=200\r\n"
70 #define HSP_HS_MICROPHONE_GAIN "AT+VGM"
71 #define HSP_HS_SPEAKER_GAIN "AT+VGS"
72 
73 static const char default_hsp_hs_service_name[] = "Headset";
74 
75 static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3};
76 static uint8_t channel_nr = 0;
77 
78 static uint16_t mtu;
79 static uint16_t rfcomm_cid = 0;
80 static uint16_t sco_handle = 0;
81 static uint16_t rfcomm_handle = 0;
82 
83 // static uint8_t connection_state = 0;
84 
85 static int hs_microphone_gain = -1;
86 static int hs_speaker_gain = -1;
87 
88 static uint8_t hs_send_button_press = 0;
89 static uint8_t hs_support_custom_indications = 0;
90 static uint8_t hs_outgoing_connection = 0;
91 
92 static btstack_packet_callback_registration_t hci_event_callback_registration;
93 
94 typedef enum {
95     HSP_IDLE,
96     HSP_SDP_QUERY_RFCOMM_CHANNEL,
97     HSP_W4_SDP_EVENT_QUERY_COMPLETE,
98     HSP_W4_RFCOMM_CONNECTED,
99     HSP_W4_USER_ACTION,
100     HSP_W2_CONNECT_SCO,
101     HSP_W4_SCO_CONNECTED,
102     HSP_ACTIVE,
103     HSP_W2_DISCONNECT_SCO,
104     HSP_W4_SCO_DISCONNECTED,
105     HSP_W2_DISCONNECT_RFCOMM,
106     HSP_W4_RFCOMM_DISCONNECTED,
107     HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN
108 } hsp_state_t;
109 
110 static hsp_state_t hsp_state = HSP_IDLE;
111 
112 static void hsp_run(void);
113 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
114 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
115 
116 static hsp_hs_callback_t hsp_hs_callback;
117 static void dummy_notify(uint8_t * event, uint16_t size){}
118 
119 void hsp_hs_register_packet_handler(hsp_hs_callback_t callback){
120     if (callback == NULL){
121         callback = &dummy_notify;
122     }
123     hsp_hs_callback = callback;
124 }
125 
126 static void emit_event(uint8_t event_subtype, uint8_t value){
127     if (!hsp_hs_callback) return;
128     uint8_t event[4];
129     event[0] = HCI_EVENT_HSP_META;
130     event[1] = sizeof(event) - 2;
131     event[2] = event_subtype;
132     event[3] = value; // status 0 == OK
133     (*hsp_hs_callback)(event, sizeof(event));
134 }
135 
136 static void emit_ring_event(void){
137     if (!hsp_hs_callback) return;
138     uint8_t event[3];
139     event[0] = HCI_EVENT_HSP_META;
140     event[1] = sizeof(event) - 2;
141     event[2] = HSP_SUBEVENT_RING;
142     (*hsp_hs_callback)(event, sizeof(event));
143 }
144 
145 static void emit_event_audio_connected(uint8_t status, uint16_t handle){
146     if (!hsp_hs_callback) return;
147     uint8_t event[6];
148     event[0] = HCI_EVENT_HSP_META;
149     event[1] = sizeof(event) - 2;
150     event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE;
151     event[3] = status;
152     little_endian_store_16(event, 4, handle);
153     (*hsp_hs_callback)(event, sizeof(event));
154 }
155 
156 // remote audio volume control
157 // AG +VGM=13 [0..15] ; HS AT+VGM=6 | AG OK
158 
159 static int hsp_hs_send_str_over_rfcomm(uint16_t cid, const char * command){
160     if (!rfcomm_can_send_packet_now(rfcomm_cid)) return 1;
161     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
162     if (err){
163         printf("rfcomm_send -> error 0X%02x", err);
164     }
165     return err;
166 }
167 
168 void hsp_hs_enable_custom_indications(int enable){
169     hs_support_custom_indications = enable;
170 }
171 
172 int hsp_hs_send_result(const char * result){
173     if (!hs_support_custom_indications) return 1;
174     return hsp_hs_send_str_over_rfcomm(rfcomm_cid, result);
175 }
176 
177 
178 void hsp_hs_create_sdp_record(uint8_t * service,  uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t have_remote_audio_control){
179     uint8_t* attribute;
180     de_create_sequence(service);
181 
182     // 0x0000 "Service Record Handle"
183     de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
184     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
185 
186     // 0x0001 "Service Class ID List"
187     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
188     attribute = de_push_sequence(service);
189     {
190         //  see Bluetooth Erratum #3507
191         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_HSP);          // 0x1108
192         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_Headset_HS);   // 0x1131
193         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); // 0x1203
194     }
195     de_pop_sequence(service, attribute);
196 
197     // 0x0004 "Protocol Descriptor List"
198     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
199     attribute = de_push_sequence(service);
200     {
201         uint8_t* l2cpProtocol = de_push_sequence(attribute);
202         {
203             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
204         }
205         de_pop_sequence(attribute, l2cpProtocol);
206 
207         uint8_t* rfcomm = de_push_sequence(attribute);
208         {
209             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol);  // rfcomm_service
210             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
211         }
212         de_pop_sequence(attribute, rfcomm);
213     }
214     de_pop_sequence(service, attribute);
215 
216     // 0x0005 "Public Browse Group"
217     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
218     attribute = de_push_sequence(service);
219     {
220         de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
221     }
222     de_pop_sequence(service, attribute);
223 
224     // 0x0009 "Bluetooth Profile Descriptor List"
225     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
226     attribute = de_push_sequence(service);
227     {
228         uint8_t *hsp_profile = de_push_sequence(attribute);
229         {
230             de_add_number(hsp_profile,  DE_UUID, DE_SIZE_16, SDP_HSP);
231             de_add_number(hsp_profile,  DE_UINT, DE_SIZE_16, 0x0102); // Verision 1.2
232         }
233         de_pop_sequence(attribute, hsp_profile);
234     }
235     de_pop_sequence(service, attribute);
236 
237     // 0x0100 "Service Name"
238     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
239     if (name){
240         de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
241     } else {
242         de_add_data(service,  DE_STRING, strlen(default_hsp_hs_service_name), (uint8_t *) default_hsp_hs_service_name);
243     }
244 
245     // Remote audio volume control
246     de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C);
247     de_add_number(service, DE_BOOL, DE_SIZE_8, have_remote_audio_control);
248 }
249 
250 static void hsp_hs_reset_state(void){
251     hsp_state = HSP_IDLE;
252 
253     rfcomm_cid = 0;
254     rfcomm_handle = 0;
255     sco_handle = 0;
256 
257     hs_microphone_gain = -1;
258     hs_speaker_gain = -1;
259 
260     hs_send_button_press = 0;
261     hs_support_custom_indications = 0;
262 }
263 
264 void hsp_hs_init(uint8_t rfcomm_channel_nr){
265     // register for HCI events
266     hci_event_callback_registration.callback = &packet_handler;
267     hci_add_event_handler(&hci_event_callback_registration);
268 
269     // init L2CAP
270     l2cap_init();
271 
272     rfcomm_init();
273     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);  // reserved channel, mtu limited by l2cap
274 
275     hsp_hs_reset_state();
276 }
277 
278 
279 void hsp_hs_connect(bd_addr_t bd_addr){
280     if (hsp_state != HSP_IDLE) return;
281     hs_outgoing_connection = 1;
282     hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL;
283     memcpy(remote, bd_addr, 6);
284     hsp_run();
285 }
286 
287 void hsp_hs_disconnect(bd_addr_t bd_addr){
288     switch (hsp_state){
289         case HSP_ACTIVE:
290             printf("HSP_W4_USER_ACTION\n");
291             hsp_state = HSP_W4_USER_ACTION;
292             hs_send_button_press = 1;
293             break;
294         case HSP_W4_RFCOMM_CONNECTED:
295             printf("HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN \n");
296             hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
297             break;
298         default:
299             return;
300     }
301     hsp_run();
302 }
303 
304 
305 void hsp_hs_set_microphone_gain(uint8_t gain){
306     if (gain < 0 || gain >15) {
307         printf("Gain must be in interval [0..15], it is given %d\n", gain);
308         return;
309     }
310     hs_microphone_gain = gain;
311     hsp_run();
312 }
313 
314 // AG +VGS=5  [0..15] ; HS AT+VGM=6 | AG OK
315 void hsp_hs_set_speaker_gain(uint8_t gain){
316     if (gain < 0 || gain >15) {
317         printf("Gain must be in interval [0..15], it is given %d\n", gain);
318         return;
319     }
320     hs_speaker_gain = gain;
321     hsp_run();
322 }
323 
324 
325 static void hsp_run(void){
326     int err;
327 
328     if (hs_send_button_press){
329         hs_send_button_press = 0;
330         err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD);
331         if (err) {
332             hs_send_button_press = 1;
333         }
334         return;
335     }
336 
337     switch (hsp_state){
338         case HSP_SDP_QUERY_RFCOMM_CHANNEL:
339             hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE;
340             sdp_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_Headset_AG);
341             break;
342 
343         case HSP_W2_CONNECT_SCO:
344             hsp_state = HSP_W4_SCO_CONNECTED;
345             break;
346 
347         case HSP_W2_DISCONNECT_SCO:
348             hsp_state = HSP_W4_SCO_DISCONNECTED;
349             break;
350 
351         case HSP_ACTIVE:
352 
353              if (hs_microphone_gain >= 0){
354                 int gain = hs_microphone_gain;
355                 hs_microphone_gain = -1;
356                 char buffer[20];
357                 sprintf(buffer, "%s=%d\r\n", HSP_HS_MICROPHONE_GAIN, gain);
358                 err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer);
359                 if (err) {
360                     hs_microphone_gain = gain;
361                 }
362                 break;
363             }
364 
365             if (hs_speaker_gain >= 0){
366                 int gain = hs_speaker_gain;
367                 hs_speaker_gain = -1;
368                 char buffer[20];
369                 sprintf(buffer, "%s=%d\r\n", HSP_HS_SPEAKER_GAIN, gain);
370                 err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer);
371                 if (err) {
372                     hs_speaker_gain = gain;
373                 }
374                 break;
375             }
376 
377             break;
378         default:
379             break;
380     }
381 }
382 
383 
384 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
385     // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
386     if (packet_type == RFCOMM_DATA_PACKET){
387         // skip over leading newline
388         while (size > 0 && (packet[0] == '\n' || packet[0] == '\r')){
389             size--;
390             packet++;
391         }
392         if (strncmp((char *)packet, HSP_AG_RING, strlen(HSP_AG_RING)) == 0){
393             emit_ring_event();
394         } else if (strncmp((char *)packet, HSP_AG_OK, strlen(HSP_AG_OK)) == 0){
395             printf("OK RECEIVED\n");
396             switch (hsp_state){
397                 case HSP_W4_RFCOMM_CONNECTED:
398                     hsp_state = HSP_W2_CONNECT_SCO;
399                     break;
400                 case HSP_W4_USER_ACTION:
401                     hsp_state = HSP_W2_DISCONNECT_SCO;
402                     break;
403                 default:
404                     break;
405             }
406         } else if (strncmp((char *)packet, HSP_MICROPHONE_GAIN, strlen(HSP_MICROPHONE_GAIN)) == 0){
407             uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_MICROPHONE_GAIN)]);
408             emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain);
409 
410         } else if (strncmp((char *)packet, HSP_SPEAKER_GAIN, strlen(HSP_SPEAKER_GAIN)) == 0){
411             uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_SPEAKER_GAIN)]);
412             emit_event(HSP_SUBEVENT_SPEAKER_GAIN_CHANGED, gain);
413         } else {
414             if (!hsp_hs_callback) return;
415             // strip trailing newline
416             while (size > 0 && (packet[size-1] == '\n' || packet[size-1] == '\r')){
417                 size--;
418             }
419             // add trailing \0
420             packet[size] = 0;
421             // re-use incoming buffer to avoid reserving large buffers - ugly but efficient
422             uint8_t * event = packet - 4;
423             event[0] = HCI_EVENT_HSP_META;
424             event[1] = size + 2;
425             event[2] = HSP_SUBEVENT_AG_INDICATION;
426             event[3] = size;
427             (*hsp_hs_callback)(event, size+4);
428         }
429         hsp_run();
430         return;
431     }
432 
433     if (packet_type != HCI_EVENT_PACKET) return;
434     uint8_t event = packet[0];
435     bd_addr_t event_addr;
436     uint16_t handle;
437 
438     switch (event) {
439         case BTSTACK_EVENT_STATE:
440             // bt stack activated, get started
441             if (packet[2] == HCI_STATE_WORKING){
442                 printf("BTstack activated, get started .\n");
443             }
444             hsp_hs_callback(packet, size);
445             break;
446 
447         case HCI_EVENT_PIN_CODE_REQUEST:
448             // inform about pin code request
449             printf("Pin code request - using '0000'\n\r");
450             reverse_bd_addr(&packet[2], event_addr);
451             hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
452             break;
453         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
454             int index = 2;
455             uint8_t status = packet[index++];
456             sco_handle = little_endian_read_16(packet, index);
457             index+=2;
458             bd_addr_t address;
459             memcpy(address, &packet[index], 6);
460             index+=6;
461             uint8_t link_type = packet[index++];
462             uint8_t transmission_interval = packet[index++];  // measured in slots
463             uint8_t retransmission_interval = packet[index++];// measured in slots
464             uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes
465             index+=2;
466             uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes
467             index+=2;
468             uint8_t air_mode = packet[index];
469 
470             if (status != 0){
471                 log_error("(e)SCO Connection failed, status %u", status);
472                 emit_event_audio_connected(status, sco_handle);
473                 break;
474             }
475             switch (link_type){
476                 case 0x00:
477                     printf("SCO Connection established. \n");
478                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
479                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
480                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
481                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
482                     break;
483                 case 0x02:
484                     printf("eSCO Connection established. \n");
485                     break;
486                 default:
487                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
488                     break;
489             }
490             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
491                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle,
492                  bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
493 
494             if (hsp_state == HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
495                 hsp_state = HSP_W2_DISCONNECT_SCO;
496                 break;
497             }
498 
499             // forward event to app
500             hsp_hs_callback(packet, size);
501 
502             hsp_state = HSP_ACTIVE;
503             emit_event_audio_connected(0, sco_handle);
504             break;
505         }
506 
507         case RFCOMM_EVENT_INCOMING_CONNECTION:
508             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
509             if (hsp_state != HSP_IDLE) return;
510 
511             reverse_bd_addr(&packet[2], event_addr);
512             rfcomm_cid = little_endian_read_16(packet, 9);
513             printf("RFCOMM channel %u requested for %s\n", packet[8], bd_addr_to_str(event_addr));
514             rfcomm_accept_connection(rfcomm_cid);
515 
516             hsp_state = HSP_W4_RFCOMM_CONNECTED;
517             break;
518 
519         case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
520             printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
521             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
522             if (packet[2]) {
523                 printf("RFCOMM channel open failed, status %u\n", packet[2]);
524                 hsp_hs_reset_state();
525                 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]);
526                 hs_outgoing_connection = 0;
527             } else {
528                 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
529                 rfcomm_handle = little_endian_read_16(packet, 9);
530                 rfcomm_cid = little_endian_read_16(packet, 12);
531                 mtu = little_endian_read_16(packet, 14);
532                 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, mtu);
533 
534                 if (hs_outgoing_connection){
535                     hs_outgoing_connection = 0;
536                     hs_send_button_press = 1;
537                 }
538 
539                 switch (hsp_state){
540                     case HSP_W4_RFCOMM_CONNECTED:
541                         hsp_state = HSP_W2_CONNECT_SCO;
542                         break;
543                     case HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
544                         hsp_state = HSP_W2_DISCONNECT_RFCOMM;
545                         break;
546                     default:
547                         break;
548                 }
549             }
550             break;
551 
552         case RFCOMM_EVENT_CAN_SEND_NOW:
553             hsp_hs_callback(packet, size);
554             break;
555 
556         case HCI_EVENT_DISCONNECTION_COMPLETE:
557             handle = little_endian_read_16(packet,3);
558             if (handle == sco_handle){
559                 sco_handle = 0;
560                 hsp_state = HSP_W2_DISCONNECT_RFCOMM;
561                 printf(" HSP_W2_DISCONNECT_RFCOMM\n");
562                 break;
563             }
564             break;
565         case RFCOMM_EVENT_CHANNEL_CLOSED:
566             printf("RFCOMM channel closed\n");
567             hsp_hs_reset_state();
568             emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0);
569             break;
570         default:
571             break;
572     }
573     hsp_run();
574 }
575 
576 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
577     switch (packet[0]){
578         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
579             channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
580             printf("** Service name: '%s', RFCOMM port %u\n", sdp_event_query_rfcomm_service_get_name(packet), channel_nr);
581             break;
582         case SDP_EVENT_QUERY_COMPLETE:
583             if (channel_nr > 0){
584                 hsp_state = HSP_W4_RFCOMM_CONNECTED;
585                 printf("RFCOMM create channel.\n");
586                 rfcomm_create_channel(packet_handler, remote, channel_nr, NULL);
587                 break;
588             }
589             hsp_hs_reset_state();
590             printf("Service not found, status %u.\n", sdp_event_query_complete_get_status(packet));
591             exit(0);
592             break;
593     }
594 }
595 
596 void hsp_hs_send_button_press(void){
597     hs_send_button_press = 1;
598     hsp_run();
599 }
600