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