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 btstack_packet_handler_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 packet_type, uint16_t channel, uint8_t * event, uint16_t size){} 134 135 void hsp_ag_register_packet_handler(btstack_packet_handler_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)(HCI_EVENT_PACKET, 0, 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)(HCI_EVENT_PACKET, 0, 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(const 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 hsp_establish_audio_connection = 1; 309 hsp_state = HSP_W4_SCO_CONNECTED; 310 break; 311 case HSP_W4_RFCOMM_CONNECTED: 312 hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 313 break; 314 default: 315 break; 316 } 317 hsp_run(); 318 } 319 320 void hsp_ag_release_audio_connection(void){ 321 if (hsp_state >= HSP_W2_DISCONNECT_SCO) return; 322 if (hsp_state < HSP_AUDIO_CONNECTION_ESTABLISHED) return; 323 324 hsp_release_audio_connection = 1; 325 hsp_run(); 326 } 327 328 329 void hsp_ag_set_microphone_gain(uint8_t gain){ 330 if (gain < 0 || gain >15) { 331 log_error("Gain must be in interval [0..15], it is given %d", gain); 332 return; 333 } 334 ag_microphone_gain = gain; 335 hsp_run(); 336 } 337 338 // AG +VGS=5 [0..15] ; HS AT+VGM=6 | AG OK 339 void hsp_ag_set_speaker_gain(uint8_t gain){ 340 if (gain < 0 || gain >15) { 341 log_error("Gain must be in interval [0..15], it is given %d", gain); 342 return; 343 } 344 ag_speaker_gain = gain; 345 hsp_run(); 346 } 347 348 static void hsp_ringing_timeout_handler(btstack_timer_source_t * timer){ 349 ag_ring = 1; 350 btstack_run_loop_set_timer(&hs_timeout, 2000); // 2 seconds timeout 351 btstack_run_loop_add_timer(&hs_timeout); 352 hsp_run(); 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 ag_ring = 1; 368 if (hsp_state == HSP_W2_CONNECT_SCO) { 369 hsp_state = HSP_W4_RING_ANSWER; 370 } 371 hsp_ringing_timer_start(); 372 } 373 374 void hsp_ag_stop_ringing(void){ 375 ag_ring = 0; 376 if (hsp_state == HSP_W4_RING_ANSWER){ 377 hsp_state = HSP_W2_CONNECT_SCO; 378 } 379 hsp_ringing_timer_stop(); 380 } 381 382 static void hsp_run(void){ 383 384 if (ag_send_ok){ 385 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 386 rfcomm_request_can_send_now_event(rfcomm_cid); 387 return; 388 } 389 ag_send_ok = 0; 390 hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_OK); 391 return; 392 } 393 394 if (ag_send_error){ 395 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 396 rfcomm_request_can_send_now_event(rfcomm_cid); 397 return; 398 } 399 ag_send_error = 0; 400 hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_ERROR); 401 return; 402 } 403 404 if (ag_ring){ 405 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 406 rfcomm_request_can_send_now_event(rfcomm_cid); 407 return; 408 } 409 ag_ring = 0; 410 hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_RING); 411 return; 412 } 413 414 if (hsp_establish_audio_connection){ 415 if (!hci_can_send_command_packet_now()) return; 416 hsp_establish_audio_connection = 0; 417 hci_send_cmd(&hci_setup_synchronous_connection, rfcomm_handle, 8000, 8000, 0xFFFF, hci_get_sco_voice_setting(), 0xFF, 0x003F); 418 return; 419 } 420 421 if (hsp_release_audio_connection){ 422 hsp_release_audio_connection = 0; 423 gap_disconnect(sco_handle); 424 return; 425 } 426 427 if (hsp_disconnect_rfcomm){ 428 hsp_disconnect_rfcomm = 0; 429 hsp_establish_audio_connection = 0; 430 rfcomm_disconnect(rfcomm_cid); 431 return; 432 } 433 434 switch (hsp_state){ 435 case HSP_SDP_QUERY_RFCOMM_CHANNEL: 436 hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE; 437 log_info("Start SDP query %s, 0x%02x", bd_addr_to_str(remote), SDP_HSP); 438 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, SDP_HSP); 439 break; 440 441 case HSP_W4_RING_ANSWER: 442 if (!ag_num_button_press_received) break; 443 444 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 445 rfcomm_request_can_send_now_event(rfcomm_cid); 446 return; 447 } 448 449 ag_send_ok = 0; 450 ag_num_button_press_received = 0; 451 hsp_state = HSP_W2_CONNECT_SCO; 452 453 hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_OK); 454 break; 455 456 case HSP_W2_CONNECT_SCO: 457 if (!hci_can_send_command_packet_now()) return; 458 hsp_state = HSP_W4_SCO_CONNECTED; 459 hci_send_cmd(&hci_setup_synchronous_connection, rfcomm_handle, 8000, 8000, 0xFFFF, hci_get_sco_voice_setting(), 0xFF, 0x003F); 460 break; 461 462 case HSP_W2_DISCONNECT_SCO: 463 ag_num_button_press_received = 0; 464 465 hsp_state = HSP_W4_SCO_DISCONNECTED; 466 gap_disconnect(sco_handle); 467 break; 468 469 case HSP_W2_DISCONNECT_RFCOMM: 470 rfcomm_disconnect(rfcomm_cid); 471 break; 472 473 case HSP_AUDIO_CONNECTION_ESTABLISHED: 474 case HSP_RFCOMM_CONNECTION_ESTABLISHED: 475 476 if (ag_microphone_gain >= 0){ 477 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 478 rfcomm_request_can_send_now_event(rfcomm_cid); 479 return; 480 } 481 int gain = ag_microphone_gain; 482 ag_microphone_gain = -1; 483 char buffer[10]; 484 sprintf(buffer, "%s=%d\r\n", HSP_MICROPHONE_GAIN, gain); 485 hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); 486 break; 487 } 488 489 if (ag_speaker_gain >= 0){ 490 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 491 rfcomm_request_can_send_now_event(rfcomm_cid); 492 return; 493 } 494 int gain = ag_speaker_gain; 495 ag_speaker_gain = -1; 496 char buffer[10]; 497 sprintf(buffer, "%s=%d\r\n", HSP_SPEAKER_GAIN, gain); 498 hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); 499 break; 500 } 501 break; 502 default: 503 break; 504 } 505 } 506 507 508 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 509 if (packet_type == RFCOMM_DATA_PACKET){ 510 while (size > 0 && (packet[0] == '\n' || packet[0] == '\r')){ 511 size--; 512 packet++; 513 } 514 515 if (strncmp((char *)packet, HSP_HS_BUTTON_PRESS, strlen(HSP_HS_BUTTON_PRESS)) == 0){ 516 log_info("Received button press %s", HSP_HS_BUTTON_PRESS); 517 ag_send_ok = 1; 518 switch (hsp_state){ 519 case HSP_AUDIO_CONNECTION_ESTABLISHED: 520 hsp_release_audio_connection = 1; 521 break; 522 case HSP_RFCOMM_CONNECTION_ESTABLISHED: 523 hsp_establish_audio_connection = 1; 524 break; 525 default: 526 break; 527 } 528 } else if (strncmp((char *)packet, HSP_HS_MICROPHONE_GAIN, strlen(HSP_HS_MICROPHONE_GAIN)) == 0){ 529 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_HS_MICROPHONE_GAIN)]); 530 ag_send_ok = 1; 531 emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain); 532 533 } else if (strncmp((char *)packet, HSP_HS_SPEAKER_GAIN, strlen(HSP_HS_SPEAKER_GAIN)) == 0){ 534 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_HS_SPEAKER_GAIN)]); 535 ag_send_ok = 1; 536 emit_event(HSP_SUBEVENT_SPEAKER_GAIN_CHANGED, gain); 537 538 } else if (strncmp((char *)packet, "AT+", 3) == 0){ 539 ag_send_error = 1; 540 if (!hsp_ag_callback) return; 541 // re-use incoming buffer to avoid reserving large buffers - ugly but efficient 542 uint8_t * event = packet - 4; 543 event[0] = HCI_EVENT_HSP_META; 544 event[1] = size + 2; 545 event[2] = HSP_SUBEVENT_HS_COMMAND; 546 event[3] = size; 547 (*hsp_ag_callback)(HCI_EVENT_PACKET, 0, event, size+4); 548 } 549 550 hsp_run(); 551 return; 552 } 553 554 if (packet_type != HCI_EVENT_PACKET) return; 555 uint8_t event = hci_event_packet_get_type(packet); 556 bd_addr_t event_addr; 557 uint16_t handle; 558 559 switch (event) { 560 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 561 int index = 2; 562 uint8_t status = packet[index++]; 563 sco_handle = little_endian_read_16(packet, index); 564 index+=2; 565 bd_addr_t address; 566 memcpy(address, &packet[index], 6); 567 index+=6; 568 uint8_t link_type = packet[index++]; 569 uint8_t transmission_interval = packet[index++]; // measured in slots 570 uint8_t retransmission_interval = packet[index++];// measured in slots 571 uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes 572 index+=2; 573 uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes 574 index+=2; 575 uint8_t air_mode = packet[index]; 576 577 if (status != 0){ 578 log_error("(e)SCO Connection failed, status %u", status); 579 emit_event_audio_connected(status, sco_handle); 580 break; 581 } 582 switch (link_type){ 583 case 0x00: 584 log_info("SCO Connection established."); 585 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 586 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 587 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 588 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 589 break; 590 case 0x02: 591 log_info("eSCO Connection established."); 592 break; 593 default: 594 log_error("(e)SCO reserved link_type 0x%2x", link_type); 595 break; 596 } 597 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 598 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, 599 bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 600 601 if (hsp_state == HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 602 hsp_state = HSP_W2_DISCONNECT_SCO; 603 break; 604 } 605 606 hsp_state = HSP_AUDIO_CONNECTION_ESTABLISHED; 607 emit_event_audio_connected(status, sco_handle); 608 break; 609 } 610 611 case RFCOMM_EVENT_INCOMING_CONNECTION: 612 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 613 if (hsp_state != HSP_IDLE) return; 614 615 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 616 rfcomm_cid = rfcomm_event_incoming_connection_get_server_channel(packet); 617 log_info("RFCOMM channel %u requested for %s", packet[8], bd_addr_to_str(event_addr)); 618 hsp_state = HSP_W4_RFCOMM_CONNECTED; 619 rfcomm_accept_connection(rfcomm_cid); 620 break; 621 622 case RFCOMM_EVENT_CHANNEL_OPENED: 623 log_info("RFCOMM_EVENT_CHANNEL_OPENED packet_handler type %u, packet[0] %x", packet_type, packet[0]); 624 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 625 if (rfcomm_event_channel_opened_get_status(packet)) { 626 log_info("RFCOMM channel open failed, status %u§", rfcomm_event_channel_opened_get_status(packet)); 627 hsp_ag_reset_state(); 628 hsp_state = HSP_IDLE; 629 } else { 630 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16) 631 rfcomm_handle = rfcomm_event_channel_opened_get_con_handle(packet); 632 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 633 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 634 log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d", rfcomm_cid, mtu, hsp_state); 635 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED; 636 } 637 emit_event(HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE, packet[2]); 638 break; 639 640 case RFCOMM_EVENT_CHANNEL_CLOSED: 641 rfcomm_handle = 0; 642 hsp_state = HSP_IDLE; 643 hsp_ag_reset_state(); 644 emit_event(HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE,0); 645 break; 646 647 case RFCOMM_EVENT_CAN_SEND_NOW: 648 hsp_run(); 649 break; 650 651 case HCI_EVENT_DISCONNECTION_COMPLETE: 652 handle = little_endian_read_16(packet,3); 653 if (handle == sco_handle){ 654 sco_handle = 0; 655 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED; 656 emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); 657 break; 658 } 659 if (handle == rfcomm_handle) { 660 rfcomm_handle = 0; 661 hsp_state = HSP_IDLE; 662 hsp_ag_reset_state(); 663 emit_event(HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE,0); 664 } 665 break; 666 667 default: 668 break; 669 } 670 671 hsp_run(); 672 } 673 674 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 675 switch (packet[0]){ 676 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 677 channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 678 log_info("** Service name: '%s', RFCOMM port %u", sdp_event_query_rfcomm_service_get_name(packet), channel_nr); 679 break; 680 case SDP_EVENT_QUERY_COMPLETE: 681 if (channel_nr > 0){ 682 hsp_state = HSP_W4_RFCOMM_CONNECTED; 683 log_info("RFCOMM create channel. state %d", HSP_W4_RFCOMM_CONNECTED); 684 rfcomm_create_channel(packet_handler, remote, channel_nr, NULL); 685 break; 686 } 687 hsp_ag_reset_state(); 688 log_info("Service not found, status %u.\n", sdp_event_query_complete_get_status(packet)); 689 if (sdp_event_query_complete_get_status(packet)){ 690 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, sdp_event_query_complete_get_status(packet)); 691 } else { 692 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, SDP_SERVICE_NOT_FOUND); 693 } 694 break; 695 default: 696 break; 697 } 698 } 699 700 701