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