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