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 Audio Gateway 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/core.h" 56 #include "classic/sdp_server.h" 57 #include "classic/sdp_client_rfcomm.h" 58 #include "classic/sdp_util.h" 59 #include "hci.h" 60 #include "hci_cmd.h" 61 #include "hci_dump.h" 62 #include "hsp_ag.h" 63 #include "l2cap.h" 64 65 #define HSP_HS_BUTTON_PRESS "AT+CKPD=200" 66 #define HSP_HS_AT_CKPD "AT+CKPD\r\n" 67 #define HSP_AG_OK "\r\nOK\r\n" 68 #define HSP_AG_ERROR "\r\nERROR\r\n" 69 #define HSP_AG_RING "\r\nRING\r\n" 70 #define HSP_MICROPHONE_GAIN "+VGM" 71 #define HSP_SPEAKER_GAIN "+VGS" 72 73 #define HSP_HS_MICROPHONE_GAIN "AT+VGM=" 74 #define HSP_HS_SPEAKER_GAIN "AT+VGS=" 75 76 static const char default_hsp_ag_service_name[] = "Audio Gateway"; 77 78 static bd_addr_t remote; 79 static uint8_t channel_nr = 0; 80 81 static uint16_t mtu; 82 static uint16_t rfcomm_cid = 0; 83 static uint16_t sco_handle = 0; 84 static uint16_t rfcomm_handle = 0; 85 static btstack_timer_source_t hs_timeout; 86 87 static int ag_microphone_gain = -1; 88 static int ag_speaker_gain = -1; 89 static uint8_t ag_ring = 0; 90 static uint8_t ag_send_ok = 0; 91 static uint8_t ag_send_error = 0; 92 static uint8_t ag_num_button_press_received = 0; 93 static uint8_t ag_support_custom_commands = 0; 94 95 static uint8_t hsp_disconnect_rfcomm = 0; 96 static uint8_t hsp_establish_audio_connection = 0; 97 static uint8_t hsp_release_audio_connection = 0; 98 99 static btstack_packet_callback_registration_t hci_event_callback_registration; 100 101 typedef enum { 102 HSP_IDLE, 103 HSP_SDP_QUERY_RFCOMM_CHANNEL, 104 HSP_W4_SDP_EVENT_QUERY_COMPLETE, 105 HSP_W4_RFCOMM_CONNECTED, 106 107 HSP_RFCOMM_CONNECTION_ESTABLISHED, 108 109 HSP_W4_RING_ANSWER, 110 HSP_W4_USER_ACTION, 111 HSP_W2_CONNECT_SCO, 112 HSP_W4_SCO_CONNECTED, 113 114 HSP_AUDIO_CONNECTION_ESTABLISHED, 115 116 HSP_W2_DISCONNECT_SCO, 117 HSP_W4_SCO_DISCONNECTED, 118 119 HSP_W2_DISCONNECT_RFCOMM, 120 HSP_W4_RFCOMM_DISCONNECTED, 121 HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN 122 } hsp_state_t; 123 124 static hsp_state_t hsp_state = HSP_IDLE; 125 126 127 static hsp_ag_callback_t hsp_ag_callback; 128 129 static void hsp_run(void); 130 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 131 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 132 133 static void dummy_notify(uint8_t * event, uint16_t size){} 134 135 void hsp_ag_register_packet_handler(hsp_ag_callback_t callback){ 136 if (callback == NULL){ 137 callback = &dummy_notify; 138 } 139 hsp_ag_callback = callback; 140 } 141 142 static void emit_event(uint8_t event_subtype, uint8_t value){ 143 if (!hsp_ag_callback) return; 144 uint8_t event[4]; 145 event[0] = HCI_EVENT_HSP_META; 146 event[1] = sizeof(event) - 2; 147 event[2] = event_subtype; 148 event[3] = value; // status 0 == OK 149 (*hsp_ag_callback)(event, sizeof(event)); 150 } 151 152 static void emit_event_audio_connected(uint8_t status, uint16_t handle){ 153 if (!hsp_ag_callback) return; 154 uint8_t event[6]; 155 event[0] = HCI_EVENT_HSP_META; 156 event[1] = sizeof(event) - 2; 157 event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE; 158 event[3] = status; 159 little_endian_store_16(event, 4, handle); 160 (*hsp_ag_callback)(event, sizeof(event)); 161 } 162 163 void hsp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name){ 164 uint8_t* attribute; 165 de_create_sequence(service); 166 167 // 0x0000 "Service Record Handle" 168 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 169 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 170 171 // 0x0001 "Service Class ID List" 172 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 173 attribute = de_push_sequence(service); 174 { 175 // "UUID for PAN Service" 176 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_Headset_AG); 177 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); 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_ag_service_name), (uint8_t *) default_hsp_ag_service_name); 227 } 228 } 229 230 static int hsp_ag_send_str_over_rfcomm(uint16_t cid, char * command){ 231 int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 232 if (err){ 233 log_error("rfcomm_send_internal -> error 0X%02x", err); 234 return err; 235 } 236 return err; 237 } 238 239 void hsp_ag_enable_custom_commands(int enable){ 240 ag_support_custom_commands = enable; 241 } 242 243 int hsp_ag_send_result(char * result){ 244 if (!ag_support_custom_commands) return 1; 245 return hsp_ag_send_str_over_rfcomm(rfcomm_cid, result); 246 } 247 248 static void hsp_ag_reset_state(void){ 249 hsp_state = HSP_IDLE; 250 251 rfcomm_cid = 0; 252 rfcomm_handle = 0; 253 sco_handle = 0; 254 255 ag_send_ok = 0; 256 ag_send_error = 0; 257 ag_ring = 0; 258 259 ag_num_button_press_received = 0; 260 ag_support_custom_commands = 0; 261 262 ag_microphone_gain = -1; 263 ag_speaker_gain = -1; 264 265 hsp_disconnect_rfcomm = 0; 266 hsp_establish_audio_connection = 0; 267 hsp_release_audio_connection = 0; 268 } 269 270 void hsp_ag_init(uint8_t rfcomm_channel_nr){ 271 // register for HCI events 272 hci_event_callback_registration.callback = &packet_handler; 273 hci_add_event_handler(&hci_event_callback_registration); 274 275 rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); // reserved channel, mtu limited by l2cap 276 277 hsp_ag_reset_state(); 278 } 279 280 void hsp_ag_connect(bd_addr_t bd_addr){ 281 if (hsp_state != HSP_IDLE) return; 282 hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL; 283 memcpy(remote, bd_addr, 6); 284 hsp_run(); 285 } 286 287 void hsp_ag_disconnect(void){ 288 hsp_ag_release_audio_connection(); 289 printf(" rfcomm diconnect %d\n", hsp_state); 290 if (hsp_state < HSP_W4_RFCOMM_CONNECTED){ 291 hsp_state = HSP_IDLE; 292 return; 293 } 294 295 if (hsp_state == HSP_W4_RFCOMM_CONNECTED){ 296 hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 297 return; 298 } 299 hsp_disconnect_rfcomm = 1; 300 hsp_run(); 301 } 302 303 void hsp_ag_establish_audio_connection(void){ 304 printf("hsp_ag_establish_audio_connection state %d\n", hsp_state); 305 306 switch (hsp_state){ 307 case HSP_RFCOMM_CONNECTION_ESTABLISHED: 308 printf("set flag hsp_establish_audio_connection\n"); 309 hsp_establish_audio_connection = 1; 310 hsp_state = HSP_W4_SCO_CONNECTED; 311 break; 312 case HSP_W4_RFCOMM_CONNECTED: 313 hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 314 break; 315 default: 316 break; 317 } 318 hsp_run(); 319 } 320 321 void hsp_ag_release_audio_connection(void){ 322 if (hsp_state >= HSP_W2_DISCONNECT_SCO) return; 323 if (hsp_state < HSP_AUDIO_CONNECTION_ESTABLISHED) return; 324 325 hsp_release_audio_connection = 1; 326 hsp_run(); 327 } 328 329 330 void hsp_ag_set_microphone_gain(uint8_t gain){ 331 if (gain < 0 || gain >15) { 332 log_error("Gain must be in interval [0..15], it is given %d", gain); 333 return; 334 } 335 ag_microphone_gain = gain; 336 hsp_run(); 337 } 338 339 // AG +VGS=5 [0..15] ; HS AT+VGM=6 | AG OK 340 void hsp_ag_set_speaker_gain(uint8_t gain){ 341 if (gain < 0 || gain >15) { 342 log_error("Gain must be in interval [0..15], it is given %d", gain); 343 return; 344 } 345 ag_speaker_gain = gain; 346 hsp_run(); 347 } 348 349 static void hsp_ringing_timeout_handler(btstack_timer_source_t * timer){ 350 ag_ring = 1; 351 btstack_run_loop_set_timer(&hs_timeout, 2000); // 2 seconds timeout 352 btstack_run_loop_add_timer(&hs_timeout); 353 } 354 355 static void hsp_ringing_timer_start(void){ 356 btstack_run_loop_remove_timer(&hs_timeout); 357 btstack_run_loop_set_timer_handler(&hs_timeout, hsp_ringing_timeout_handler); 358 btstack_run_loop_set_timer(&hs_timeout, 2000); // 2 seconds timeout 359 btstack_run_loop_add_timer(&hs_timeout); 360 } 361 362 static void hsp_ringing_timer_stop(void){ 363 btstack_run_loop_remove_timer(&hs_timeout); 364 } 365 366 void hsp_ag_start_ringing(void){ 367 if (hsp_state != HSP_W2_CONNECT_SCO) return; 368 ag_ring = 1; 369 hsp_state = HSP_W4_RING_ANSWER; 370 hsp_ringing_timer_start(); 371 } 372 373 void hsp_ag_stop_ringing(void){ 374 ag_ring = 0; 375 ag_num_button_press_received = 0; 376 hsp_state = HSP_W2_CONNECT_SCO; 377 hsp_ringing_timer_stop(); 378 } 379 380 static void hsp_run(void){ 381 if (!rfcomm_can_send_packet_now(rfcomm_cid)) return; 382 int err; 383 384 if (ag_send_ok){ 385 ag_send_ok = 0; 386 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_OK); 387 if (err){ 388 ag_send_ok = 1; 389 } 390 return; 391 } 392 393 if (ag_send_error){ 394 ag_send_error = 0; 395 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_ERROR); 396 if (err) { 397 ag_send_error = 1; 398 } 399 return; 400 } 401 402 if (hsp_establish_audio_connection){ 403 hsp_establish_audio_connection = 0; 404 hci_send_cmd(&hci_setup_synchronous_connection, rfcomm_handle, 8000, 8000, 0xFFFF, hci_get_sco_voice_setting(), 0xFF, 0x003F); 405 return; 406 } 407 408 if (hsp_release_audio_connection){ 409 hsp_release_audio_connection = 0; 410 gap_disconnect(sco_handle); 411 return; 412 } 413 414 if (hsp_disconnect_rfcomm){ 415 hsp_disconnect_rfcomm = 0; 416 hsp_establish_audio_connection = 0; 417 rfcomm_disconnect(rfcomm_cid); 418 return; 419 } 420 421 switch (hsp_state){ 422 case HSP_SDP_QUERY_RFCOMM_CHANNEL: 423 hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE; 424 log_info("Start SDP query %s, 0x%02x", bd_addr_to_str(remote), SDP_HSP); 425 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_HSP); 426 break; 427 428 case HSP_W4_RING_ANSWER: 429 if (ag_ring){ 430 ag_ring = 0; 431 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_RING); 432 if (err) { 433 ag_ring = 1; 434 } 435 break; 436 } 437 438 if (!ag_num_button_press_received) break; 439 ag_send_ok = 0; 440 441 ag_num_button_press_received = 0; 442 hsp_state = HSP_W2_CONNECT_SCO; 443 444 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_OK); 445 if (err) { 446 hsp_state = HSP_W4_RING_ANSWER; 447 ag_num_button_press_received = 1; 448 } 449 break; 450 451 case HSP_W2_CONNECT_SCO: 452 hsp_state = HSP_W4_SCO_CONNECTED; 453 hci_send_cmd(&hci_setup_synchronous_connection, rfcomm_handle, 8000, 8000, 0xFFFF, hci_get_sco_voice_setting(), 0xFF, 0x003F); 454 break; 455 456 case HSP_W2_DISCONNECT_SCO: 457 ag_num_button_press_received = 0; 458 459 hsp_state = HSP_W4_SCO_DISCONNECTED; 460 gap_disconnect(sco_handle); 461 break; 462 463 case HSP_W2_DISCONNECT_RFCOMM: 464 rfcomm_disconnect(rfcomm_cid); 465 break; 466 467 case HSP_AUDIO_CONNECTION_ESTABLISHED: 468 case HSP_RFCOMM_CONNECTION_ESTABLISHED: 469 470 if (ag_microphone_gain >= 0){ 471 int gain = ag_microphone_gain; 472 ag_microphone_gain = -1; 473 char buffer[10]; 474 sprintf(buffer, "%s=%d\r\n", HSP_MICROPHONE_GAIN, gain); 475 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); 476 if (err) { 477 ag_microphone_gain = gain; 478 } 479 break; 480 } 481 482 if (ag_speaker_gain >= 0){ 483 int gain = ag_speaker_gain; 484 ag_speaker_gain = -1; 485 char buffer[10]; 486 sprintf(buffer, "%s=%d\r\n", HSP_SPEAKER_GAIN, gain); 487 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); 488 if (err) { 489 ag_speaker_gain = gain; 490 } 491 break; 492 } 493 break; 494 default: 495 break; 496 } 497 } 498 499 500 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 501 if (packet_type == RFCOMM_DATA_PACKET){ 502 while (size > 0 && (packet[0] == '\n' || packet[0] == '\r')){ 503 size--; 504 packet++; 505 } 506 507 if (strncmp((char *)packet, HSP_HS_BUTTON_PRESS, strlen(HSP_HS_BUTTON_PRESS)) == 0){ 508 log_info("Received button press %s", HSP_HS_BUTTON_PRESS); 509 ag_send_ok = 1; 510 switch (hsp_state){ 511 case HSP_AUDIO_CONNECTION_ESTABLISHED: 512 hsp_release_audio_connection = 1; 513 break; 514 case HSP_RFCOMM_CONNECTION_ESTABLISHED: 515 hsp_establish_audio_connection = 1; 516 break; 517 default: 518 break; 519 } 520 } else if (strncmp((char *)packet, HSP_HS_MICROPHONE_GAIN, strlen(HSP_HS_MICROPHONE_GAIN)) == 0){ 521 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_HS_MICROPHONE_GAIN)]); 522 ag_send_ok = 1; 523 emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain); 524 525 } else if (strncmp((char *)packet, HSP_HS_SPEAKER_GAIN, strlen(HSP_HS_SPEAKER_GAIN)) == 0){ 526 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_HS_SPEAKER_GAIN)]); 527 ag_send_ok = 1; 528 emit_event(HSP_SUBEVENT_SPEAKER_GAIN_CHANGED, gain); 529 530 } else if (strncmp((char *)packet, "AT+", 3) == 0){ 531 ag_send_error = 1; 532 if (!hsp_ag_callback) return; 533 // re-use incoming buffer to avoid reserving large buffers - ugly but efficient 534 uint8_t * event = packet - 4; 535 event[0] = HCI_EVENT_HSP_META; 536 event[1] = size + 2; 537 event[2] = HSP_SUBEVENT_HS_COMMAND; 538 event[3] = size; 539 (*hsp_ag_callback)(event, size+4); 540 } 541 542 hsp_run(); 543 return; 544 } 545 546 if (packet_type != HCI_EVENT_PACKET) return; 547 uint8_t event = hci_event_packet_get_type(packet); 548 bd_addr_t event_addr; 549 uint16_t handle; 550 551 switch (event) { 552 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 553 int index = 2; 554 uint8_t status = packet[index++]; 555 sco_handle = little_endian_read_16(packet, index); 556 index+=2; 557 bd_addr_t address; 558 memcpy(address, &packet[index], 6); 559 index+=6; 560 uint8_t link_type = packet[index++]; 561 uint8_t transmission_interval = packet[index++]; // measured in slots 562 uint8_t retransmission_interval = packet[index++];// measured in slots 563 uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes 564 index+=2; 565 uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes 566 index+=2; 567 uint8_t air_mode = packet[index]; 568 569 if (status != 0){ 570 log_error("(e)SCO Connection failed, status %u", status); 571 emit_event_audio_connected(status, sco_handle); 572 break; 573 } 574 switch (link_type){ 575 case 0x00: 576 log_info("SCO Connection established."); 577 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 578 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 579 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 580 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 581 break; 582 case 0x02: 583 log_info("eSCO Connection established."); 584 break; 585 default: 586 log_error("(e)SCO reserved link_type 0x%2x", link_type); 587 break; 588 } 589 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 590 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, 591 bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 592 593 if (hsp_state == HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 594 hsp_state = HSP_W2_DISCONNECT_SCO; 595 break; 596 } 597 598 hsp_state = HSP_AUDIO_CONNECTION_ESTABLISHED; 599 emit_event_audio_connected(status, sco_handle); 600 break; 601 } 602 603 case RFCOMM_EVENT_INCOMING_CONNECTION: 604 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 605 if (hsp_state != HSP_IDLE) return; 606 607 reverse_bd_addr(&packet[2], event_addr); 608 rfcomm_cid = little_endian_read_16(packet, 9); 609 log_info("RFCOMM channel %u requested for %s", packet[8], bd_addr_to_str(event_addr)); 610 hsp_state = HSP_W4_RFCOMM_CONNECTED; 611 rfcomm_accept_connection(rfcomm_cid); 612 break; 613 614 case RFCOMM_EVENT_CHANNEL_OPENED: 615 log_info("RFCOMM_EVENT_CHANNEL_OPENED packet_handler type %u, packet[0] %x", packet_type, packet[0]); 616 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 617 if (packet[2]) { 618 log_info("RFCOMM channel open failed, status %u§", packet[2]); 619 hsp_ag_reset_state(); 620 hsp_state = HSP_IDLE; 621 } else { 622 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16) 623 rfcomm_handle = little_endian_read_16(packet, 9); 624 rfcomm_cid = little_endian_read_16(packet, 12); 625 mtu = little_endian_read_16(packet, 14); 626 log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d", rfcomm_cid, mtu, hsp_state); 627 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED; 628 } 629 emit_event(HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE, packet[2]); 630 break; 631 632 case HCI_EVENT_DISCONNECTION_COMPLETE: 633 handle = little_endian_read_16(packet,3); 634 if (handle == sco_handle){ 635 sco_handle = 0; 636 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED; 637 emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); 638 break; 639 } 640 if (handle == rfcomm_handle) { 641 rfcomm_handle = 0; 642 hsp_state = HSP_IDLE; 643 hsp_ag_reset_state(); 644 emit_event(HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE,0); 645 } 646 break; 647 648 case RFCOMM_EVENT_CHANNEL_CLOSED: 649 rfcomm_handle = 0; 650 hsp_state = HSP_IDLE; 651 hsp_ag_reset_state(); 652 emit_event(HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE,0); 653 break; 654 default: 655 break; 656 } 657 hsp_run(); 658 } 659 660 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 661 switch (packet[0]){ 662 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 663 channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 664 log_info("** Service name: '%s', RFCOMM port %u", sdp_event_query_rfcomm_service_get_name(packet), channel_nr); 665 break; 666 case SDP_EVENT_QUERY_COMPLETE: 667 if (channel_nr > 0){ 668 hsp_state = HSP_W4_RFCOMM_CONNECTED; 669 log_info("RFCOMM create channel. state %d", HSP_W4_RFCOMM_CONNECTED); 670 rfcomm_create_channel(packet_handler, remote, channel_nr, NULL); 671 break; 672 } 673 hsp_ag_reset_state(); 674 log_info("Service not found, status %u.\n", sdp_event_query_complete_get_status(packet)); 675 if (sdp_event_query_complete_get_status(packet)){ 676 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, sdp_event_query_complete_get_status(packet)); 677 } else { 678 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, SDP_SERVICE_NOT_FOUND); 679 } 680 break; 681 default: 682 break; 683 } 684 } 685 686 687