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