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