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 <string.h> 50 51 #include "bluetooth_sdp.h" 52 #include "btstack_debug.h" 53 #include "btstack_event.h" 54 #include "btstack_memory.h" 55 #include "btstack_run_loop.h" 56 #include "classic/core.h" 57 #include "classic/sdp_server.h" 58 #include "classic/sdp_client_rfcomm.h" 59 #include "classic/sdp_util.h" 60 #include "hci.h" 61 #include "hci_cmd.h" 62 #include "hci_dump.h" 63 #include "hsp_hs.h" 64 #include "l2cap.h" 65 66 #define HSP_AG_OK "OK" 67 #define HSP_AG_ERROR "ERROR" 68 #define HSP_AG_RING "RING" 69 #define HSP_MICROPHONE_GAIN "+VGM=" 70 #define HSP_SPEAKER_GAIN "+VGS=" 71 72 #define HSP_HS_AT_CKPD "AT+CKPD=200\r\n" 73 #define HSP_HS_MICROPHONE_GAIN "AT+VGM" 74 #define HSP_HS_SPEAKER_GAIN "AT+VGS" 75 76 static const char default_hsp_hs_service_name[] = "Headset"; 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 = HCI_CON_HANDLE_INVALID; 85 86 // static uint8_t connection_state = 0; 87 88 static int hs_microphone_gain = -1; 89 static int hs_speaker_gain = -1; 90 91 static uint8_t hs_send_button_press = 0; 92 static uint8_t wait_ok = 0; 93 static uint8_t hs_accept_sco_connection = 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 // ok: no code 131 UNUSED(packet_type); 132 UNUSED(channel); 133 UNUSED(event); 134 UNUSED(size); 135 } 136 137 void hsp_hs_register_packet_handler(btstack_packet_handler_t callback){ 138 if (callback == NULL){ 139 callback = &dummy_notify; 140 } 141 hsp_hs_callback = callback; 142 } 143 144 static void emit_event(uint8_t event_subtype, uint8_t value){ 145 if (!hsp_hs_callback) return; 146 uint8_t event[4]; 147 event[0] = HCI_EVENT_HSP_META; 148 event[1] = sizeof(event) - 2; 149 event[2] = event_subtype; 150 event[3] = value; // status 0 == OK 151 (*hsp_hs_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 152 } 153 154 static void emit_ring_event(void){ 155 if (!hsp_hs_callback) return; 156 uint8_t event[3]; 157 event[0] = HCI_EVENT_HSP_META; 158 event[1] = sizeof(event) - 2; 159 event[2] = HSP_SUBEVENT_RING; 160 (*hsp_hs_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 161 } 162 163 static void emit_event_audio_connected(uint8_t status, uint16_t handle){ 164 if (!hsp_hs_callback) return; 165 uint8_t event[6]; 166 event[0] = HCI_EVENT_HSP_META; 167 event[1] = sizeof(event) - 2; 168 event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE; 169 event[3] = status; 170 little_endian_store_16(event, 4, handle); 171 (*hsp_hs_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 172 } 173 174 // remote audio volume control 175 // AG +VGM=13 [0..15] ; HS AT+VGM=6 | AG OK 176 177 static int hsp_hs_send_str_over_rfcomm(uint16_t cid, const char * command){ 178 if (!rfcomm_can_send_packet_now(rfcomm_cid)) return 1; 179 int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 180 if (err){ 181 log_info("rfcomm_send_internal -> error 0X%02x", err); 182 } 183 return err; 184 } 185 186 void hsp_hs_enable_custom_indications(int enable){ 187 hs_support_custom_indications = enable; 188 } 189 190 int hsp_hs_send_result(const char * result){ 191 if (!hs_support_custom_indications) return 1; 192 return hsp_hs_send_str_over_rfcomm(rfcomm_cid, result); 193 } 194 195 196 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){ 197 uint8_t* attribute; 198 de_create_sequence(service); 199 200 // 0x0000 "Service Record Handle" 201 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 202 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 203 204 // 0x0001 "Service Class ID List" 205 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 206 attribute = de_push_sequence(service); 207 { 208 // see Bluetooth Erratum #3507 209 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HEADSET); // 0x1108 210 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HEADSET_HS); // 0x1131 211 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); // 0x1203 212 } 213 de_pop_sequence(service, attribute); 214 215 // 0x0004 "Protocol Descriptor List" 216 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 217 attribute = de_push_sequence(service); 218 { 219 uint8_t* l2cpProtocol = de_push_sequence(attribute); 220 { 221 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 222 } 223 de_pop_sequence(attribute, l2cpProtocol); 224 225 uint8_t* rfcomm = de_push_sequence(attribute); 226 { 227 de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 228 de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 229 } 230 de_pop_sequence(attribute, rfcomm); 231 } 232 de_pop_sequence(service, attribute); 233 234 // 0x0005 "Public Browse Group" 235 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 236 attribute = de_push_sequence(service); 237 { 238 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 239 } 240 de_pop_sequence(service, attribute); 241 242 // 0x0009 "Bluetooth Profile Descriptor List" 243 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 244 attribute = de_push_sequence(service); 245 { 246 uint8_t *hsp_profile = de_push_sequence(attribute); 247 { 248 de_add_number(hsp_profile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HEADSET); 249 de_add_number(hsp_profile, DE_UINT, DE_SIZE_16, 0x0102); // Verision 1.2 250 } 251 de_pop_sequence(attribute, hsp_profile); 252 } 253 de_pop_sequence(service, attribute); 254 255 // 0x0100 "Service Name" 256 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 257 if (name){ 258 de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 259 } else { 260 de_add_data(service, DE_STRING, strlen(default_hsp_hs_service_name), (uint8_t *) default_hsp_hs_service_name); 261 } 262 263 // Remote audio volume control 264 de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C); 265 de_add_number(service, DE_BOOL, DE_SIZE_8, have_remote_audio_control); 266 } 267 268 static void hsp_hs_reset_state(void){ 269 hsp_state = HSP_IDLE; 270 hs_microphone_gain = -1; 271 hs_speaker_gain = -1; 272 273 hs_send_button_press = 0; 274 wait_ok = 0; 275 hs_support_custom_indications = 0; 276 277 hsp_disconnect_rfcomm = 0; 278 hsp_establish_audio_connection = 0; 279 hsp_release_audio_connection = 0; 280 } 281 282 void hsp_hs_init(uint8_t rfcomm_channel_nr){ 283 // register for HCI events 284 hci_event_callback_registration.callback = &packet_handler; 285 hci_add_event_handler(&hci_event_callback_registration); 286 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 (void)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 hsp_establish_audio_connection = 0; 314 rfcomm_disconnect(rfcomm_cid); 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 >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 >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 static void hsp_run_handle_state(void){ 360 switch (hsp_state){ 361 case HSP_SDP_QUERY_RFCOMM_CHANNEL: 362 hsp_state = HSP_W4_SDP_QUERY_COMPLETE; 363 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, remote, BLUETOOTH_SERVICE_CLASS_HEADSET_AUDIO_GATEWAY_AG); 364 break; 365 366 case HSP_AUDIO_CONNECTION_ESTABLISHED: 367 case HSP_RFCOMM_CONNECTION_ESTABLISHED: 368 369 if (hs_microphone_gain >= 0){ 370 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 371 rfcomm_request_can_send_now_event(rfcomm_cid); 372 return; 373 } 374 char buffer[20]; 375 sprintf(buffer, "%s=%d\r\n", HSP_HS_MICROPHONE_GAIN, hs_microphone_gain); 376 hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); 377 hs_microphone_gain = -1; 378 break; 379 } 380 381 if (hs_speaker_gain >= 0){ 382 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 383 rfcomm_request_can_send_now_event(rfcomm_cid); 384 return; 385 } 386 char buffer[20]; 387 sprintf(buffer, "%s=%d\r\n", HSP_HS_SPEAKER_GAIN, hs_speaker_gain); 388 hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); 389 hs_speaker_gain = -1; 390 break; 391 } 392 break; 393 case HSP_W4_RFCOMM_DISCONNECTED: 394 rfcomm_disconnect(rfcomm_cid); 395 break; 396 default: 397 break; 398 } 399 } 400 401 static void hsp_run(void){ 402 403 if (wait_ok) return; 404 405 if (hs_accept_sco_connection && hci_can_send_command_packet_now()){ 406 407 bool eSCO = hs_accept_sco_connection == 2; 408 hs_accept_sco_connection = 0; 409 410 log_info("HSP: sending hci_accept_connection_request."); 411 // remote supported feature eSCO is set if link type is eSCO 412 // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 413 uint16_t max_latency; 414 uint8_t retransmission_effort; 415 uint16_t packet_types; 416 417 if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(rfcomm_handle)){ 418 max_latency = 0x000c; 419 retransmission_effort = 0x02; 420 packet_types = 0x388; 421 } else { 422 max_latency = 0xffff; 423 retransmission_effort = 0xff; 424 packet_types = 0x003f; 425 } 426 427 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 428 429 log_info("HFP: sending hci_accept_connection_request, sco_voice_setting %02x", sco_voice_setting); 430 hci_send_cmd(&hci_accept_synchronous_connection, remote, 8000, 8000, max_latency, 431 sco_voice_setting, retransmission_effort, packet_types); 432 return; 433 } 434 435 if (hsp_release_audio_connection){ 436 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 437 rfcomm_request_can_send_now_event(rfcomm_cid); 438 return; 439 } 440 hsp_release_audio_connection = 0; 441 wait_ok = 1; 442 hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD); 443 return; 444 } 445 446 if (hsp_establish_audio_connection){ 447 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 448 rfcomm_request_can_send_now_event(rfcomm_cid); 449 return; 450 } 451 hsp_establish_audio_connection = 0; 452 wait_ok = 1; 453 hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD); 454 return; 455 } 456 457 if (hs_send_button_press){ 458 if (!rfcomm_can_send_packet_now(rfcomm_cid)) { 459 rfcomm_request_can_send_now_event(rfcomm_cid); 460 return; 461 } 462 hs_send_button_press = 0; 463 wait_ok = 1; 464 hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD); 465 return; 466 } 467 468 hsp_run_handle_state(); 469 } 470 471 472 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 473 UNUSED(channel); // ok: no channel for HCI_EVENT_PACKET and only single active RFCOMM channel 474 if (packet_type == RFCOMM_DATA_PACKET){ 475 // skip over leading newline 476 while ((size > 0) && ((packet[0] == '\n') || (packet[0] == '\r'))){ 477 size--; 478 packet++; 479 } 480 if (strncmp((char *)packet, HSP_AG_RING, strlen(HSP_AG_RING)) == 0){ 481 emit_ring_event(); 482 } else if (strncmp((char *)packet, HSP_AG_OK, strlen(HSP_AG_OK)) == 0){ 483 wait_ok = 0; 484 } else if (strncmp((char *)packet, HSP_MICROPHONE_GAIN, strlen(HSP_MICROPHONE_GAIN)) == 0){ 485 uint8_t gain = (uint8_t)btstack_atoi((char*)&packet[strlen(HSP_MICROPHONE_GAIN)]); 486 emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain); 487 488 } else if (strncmp((char *)packet, HSP_SPEAKER_GAIN, strlen(HSP_SPEAKER_GAIN)) == 0){ 489 uint8_t gain = (uint8_t)btstack_atoi((char*)&packet[strlen(HSP_SPEAKER_GAIN)]); 490 emit_event(HSP_SUBEVENT_SPEAKER_GAIN_CHANGED, gain); 491 } else { 492 if (!hsp_hs_callback) return; 493 // strip trailing newline 494 while ((size > 0) && ((packet[size-1] == '\n') || (packet[size-1] == '\r'))){ 495 size--; 496 } 497 // add trailing \0 498 packet[size] = 0; 499 // re-use incoming buffer to avoid reserving large buffers - ugly but efficient 500 uint8_t * event = packet - 4; 501 event[0] = HCI_EVENT_HSP_META; 502 event[1] = size + 2; 503 event[2] = HSP_SUBEVENT_AG_INDICATION; 504 event[3] = size; 505 (*hsp_hs_callback)(HCI_EVENT_PACKET, 0, event, size+4); 506 } 507 hsp_run(); 508 return; 509 } 510 511 if (packet_type != HCI_EVENT_PACKET) return; 512 513 uint8_t event = hci_event_packet_get_type(packet); 514 bd_addr_t event_addr; 515 uint16_t handle; 516 switch (event) { 517 case HCI_EVENT_CONNECTION_REQUEST: 518 switch(hci_event_connection_request_get_link_type(packet)){ 519 case 0: // SCO 520 case 2: // eSCO 521 hci_event_connection_request_get_bd_addr(packet, event_addr); 522 if (bd_addr_cmp(event_addr, remote) == 0){ 523 if (hci_event_connection_request_get_link_type(packet) == 2){ 524 hs_accept_sco_connection = 2; 525 } else { 526 hs_accept_sco_connection = 1; 527 } 528 log_info("hs_accept_sco_connection %u", hs_accept_sco_connection); 529 } 530 break; 531 default: 532 break; 533 } 534 break; 535 536 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 537 if (hsp_state < HSP_RFCOMM_CONNECTION_ESTABLISHED) return; 538 hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 539 uint8_t status = hci_event_synchronous_connection_complete_get_status(packet); 540 sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 541 uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 542 uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 543 uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet); // measured in slots 544 uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 545 uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 546 uint8_t air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet); 547 548 if (status != 0){ 549 log_error("(e)SCO Connection failed, status %u", status); 550 emit_event_audio_connected(status, sco_handle); 551 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED ; 552 break; 553 } 554 555 switch (link_type){ 556 case 0x00: 557 log_info("SCO Connection established."); 558 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 559 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 560 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 561 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 562 break; 563 case 0x02: 564 log_info("eSCO Connection established."); 565 break; 566 default: 567 log_error("(e)SCO reserved link_type 0x%2x", link_type); 568 break; 569 } 570 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 571 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, 572 bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 573 574 hsp_state = HSP_AUDIO_CONNECTION_ESTABLISHED; 575 emit_event_audio_connected(status, sco_handle); 576 break; 577 } 578 579 case RFCOMM_EVENT_INCOMING_CONNECTION: 580 if (hsp_state != HSP_IDLE) return; 581 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 582 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 583 log_info("RFCOMM channel %u requested for %s", rfcomm_event_incoming_connection_get_server_channel(packet), bd_addr_to_str(event_addr)); 584 hsp_state = HSP_W4_RFCOMM_CONNECTED; 585 rfcomm_accept_connection(rfcomm_cid); 586 break; 587 588 case RFCOMM_EVENT_CHANNEL_OPENED: 589 if (hsp_state != HSP_W4_RFCOMM_CONNECTED) return; 590 if (rfcomm_event_channel_opened_get_status(packet)) { 591 log_info("RFCOMM channel open failed, status %u", rfcomm_event_channel_opened_get_status(packet)); 592 hsp_state = HSP_IDLE; 593 hsp_hs_reset_state(); 594 } else { 595 rfcomm_event_channel_opened_get_bd_addr(packet, remote); 596 rfcomm_handle = rfcomm_event_channel_opened_get_con_handle(packet); 597 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 598 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 599 log_info("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, handle %02x", rfcomm_cid, mtu, rfcomm_handle); 600 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED; 601 } 602 emit_event(HSP_SUBEVENT_RFCOMM_CONNECTION_COMPLETE, rfcomm_event_channel_opened_get_status(packet)); 603 break; 604 605 case RFCOMM_EVENT_CAN_SEND_NOW: 606 hsp_run(); 607 break; 608 609 case HCI_EVENT_DISCONNECTION_COMPLETE: 610 handle = hci_event_disconnection_complete_get_connection_handle(packet); 611 if (handle == sco_handle){ 612 sco_handle = 0; 613 hsp_state = HSP_RFCOMM_CONNECTION_ESTABLISHED; 614 emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); 615 break; 616 } 617 break; 618 619 case RFCOMM_EVENT_CHANNEL_CLOSED: 620 hsp_state = HSP_IDLE; 621 hsp_hs_reset_state(); 622 emit_event(HSP_SUBEVENT_RFCOMM_DISCONNECTION_COMPLETE,0); 623 break; 624 625 default: 626 break; 627 } 628 629 hsp_run(); 630 } 631 632 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 633 UNUSED(packet_type); // ok: handling own sdp events 634 UNUSED(channel); // ok: no channel 635 UNUSED(size); // ok: handling own sdp events 636 637 switch (hci_event_packet_get_type(packet)){ 638 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 639 channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 640 log_info("** Service name: '%s', RFCOMM port %u", sdp_event_query_rfcomm_service_get_name(packet), channel_nr); 641 break; 642 case SDP_EVENT_QUERY_COMPLETE: 643 if (channel_nr > 0){ 644 hsp_state = HSP_W4_RFCOMM_CONNECTED; 645 log_info("HSP: SDP_QUERY_COMPLETE. RFCOMM create channel, addr %s, rfcomm channel nr %d", bd_addr_to_str(remote), channel_nr); 646 rfcomm_create_channel(packet_handler, remote, channel_nr, NULL); 647 break; 648 } 649 hsp_hs_reset_state(); 650 log_info("Service not found, status %u.", sdp_event_query_complete_get_status(packet)); 651 if (sdp_event_query_complete_get_status(packet)){ 652 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, sdp_event_query_complete_get_status(packet)); 653 } else { 654 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, SDP_SERVICE_NOT_FOUND); 655 } 656 break; 657 } 658 } 659 660 void hsp_hs_send_button_press(void){ 661 if ((hsp_state < HSP_RFCOMM_CONNECTION_ESTABLISHED) || (hsp_state >= HSP_W4_RFCOMM_DISCONNECTED)) return; 662 hs_send_button_press = 1; 663 hsp_run(); 664 } 665