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