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