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 // Minimal setup for HSP Audio Gateway (!! UNDER DEVELOPMENT !!) 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/sdp.h" 56 #include "classic/sdp_query_rfcomm.h" 57 #include "hci.h" 58 #include "hci_cmd.h" 59 #include "hci_dump.h" 60 #include "hsp_ag.h" 61 #include "l2cap.h" 62 63 #define RFCOMM_SERVER_CHANNEL 1 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 uint8_t connection_state = 0; 88 89 static int ag_microphone_gain = -1; 90 static int ag_speaker_gain = -1; 91 static uint8_t ag_ring = 0; 92 static uint8_t ag_send_ok = 0; 93 static uint8_t ag_send_error = 0; 94 static uint8_t ag_num_button_press_received = 0; 95 static uint8_t ag_support_custom_commands = 0; 96 97 typedef enum { 98 HSP_IDLE, 99 HSP_SDP_QUERY_RFCOMM_CHANNEL, 100 HSP_W4_SDP_EVENT_QUERY_COMPLETE, 101 HSP_W4_RFCOMM_CONNECTED, 102 HSP_W4_RING_ANSWER, 103 HSP_W4_USER_ACTION, 104 HSP_W2_CONNECT_SCO, 105 HSP_W4_SCO_CONNECTED, 106 HSP_ACTIVE, 107 HSP_W2_DISCONNECT_SCO, 108 HSP_W4_SCO_DISCONNECTED, 109 HSP_W2_DISCONNECT_RFCOMM, 110 HSP_W4_RFCOMM_DISCONNECTED, 111 HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN 112 } hsp_state_t; 113 114 static hsp_state_t hsp_state = HSP_IDLE; 115 116 117 static hsp_ag_callback_t hsp_ag_callback; 118 119 static void hsp_run(); 120 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 121 static void handle_query_rfcomm_event(uint8_t packet_type, uint8_t *packet, uint16_t size, void * context); 122 123 static void dummy_notify(uint8_t * event, uint16_t size){} 124 125 void hsp_ag_register_packet_handler(hsp_ag_callback_t callback){ 126 if (callback == NULL){ 127 callback = &dummy_notify; 128 } 129 hsp_ag_callback = callback; 130 } 131 132 static void emit_event(uint8_t event_subtype, uint8_t value){ 133 if (!hsp_ag_callback) return; 134 uint8_t event[4]; 135 event[0] = HCI_EVENT_HSP_META; 136 event[1] = sizeof(event) - 2; 137 event[2] = event_subtype; 138 event[3] = value; // status 0 == OK 139 (*hsp_ag_callback)(event, sizeof(event)); 140 } 141 142 static void emit_event_audio_connected(uint8_t status, uint16_t handle){ 143 if (!hsp_hs_callback) return; 144 uint8_t event[6]; 145 event[0] = HCI_EVENT_HSP_META; 146 event[1] = sizeof(event) - 2; 147 event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE; 148 event[3] = status; 149 little_endian_store_16(event, 4, handle); 150 (*hsp_hs_callback)(event, sizeof(event)); 151 } 152 153 void hsp_ag_create_service(uint8_t * service, int rfcomm_channel_nr, const char * name){ 154 uint8_t* attribute; 155 de_create_sequence(service); 156 157 // 0x0000 "Service Record Handle" 158 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 159 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 160 161 // 0x0001 "Service Class ID List" 162 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 163 attribute = de_push_sequence(service); 164 { 165 // "UUID for PAN Service" 166 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_Headset_AG); 167 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); 168 } 169 de_pop_sequence(service, attribute); 170 171 // 0x0004 "Protocol Descriptor List" 172 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 173 attribute = de_push_sequence(service); 174 { 175 uint8_t* l2cpProtocol = de_push_sequence(attribute); 176 { 177 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 178 } 179 de_pop_sequence(attribute, l2cpProtocol); 180 181 uint8_t* rfcomm = de_push_sequence(attribute); 182 { 183 de_add_number(rfcomm, DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol); // rfcomm_service 184 de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 185 } 186 de_pop_sequence(attribute, rfcomm); 187 } 188 de_pop_sequence(service, attribute); 189 190 // 0x0005 "Public Browse Group" 191 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 192 attribute = de_push_sequence(service); 193 { 194 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 195 } 196 de_pop_sequence(service, attribute); 197 198 // 0x0009 "Bluetooth Profile Descriptor List" 199 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 200 attribute = de_push_sequence(service); 201 { 202 uint8_t *sppProfile = de_push_sequence(attribute); 203 { 204 de_add_number(sppProfile, DE_UUID, DE_SIZE_16, SDP_HSP); 205 de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0102); // Verision 1.2 206 } 207 de_pop_sequence(attribute, sppProfile); 208 } 209 de_pop_sequence(service, attribute); 210 211 // 0x0100 "Service Name" 212 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 213 if (name){ 214 de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 215 } else { 216 de_add_data(service, DE_STRING, strlen(default_hsp_ag_service_name), (uint8_t *) default_hsp_ag_service_name); 217 } 218 } 219 220 static int hsp_ag_send_str_over_rfcomm(uint16_t cid, char * command){ 221 if (!rfcomm_can_send_packet_now(cid)) return 1; 222 int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 223 if (err){ 224 printf("rfcomm_send -> error 0X%02x", err); 225 return err; 226 } 227 printf("Send string: \"%s\"\n", command); 228 return err; 229 } 230 231 void hsp_ag_support_custom_commands(int enable){ 232 ag_support_custom_commands = enable; 233 } 234 235 int hsp_ag_send_result(char * result){ 236 if (!ag_support_custom_commands) return 1; 237 return hsp_ag_send_str_over_rfcomm(rfcomm_cid, result); 238 } 239 240 241 static void hsp_ag_reset_state(void){ 242 hsp_state = HSP_IDLE; 243 244 rfcomm_cid = 0; 245 rfcomm_handle = 0; 246 sco_handle = 0; 247 248 ag_send_ok = 0; 249 ag_send_error = 0; 250 ag_ring = 0; 251 252 ag_num_button_press_received = 0; 253 ag_support_custom_commands = 0; 254 255 ag_microphone_gain = -1; 256 ag_speaker_gain = -1; 257 } 258 259 void hsp_ag_init(uint8_t rfcomm_channel_nr){ 260 // init L2CAP 261 l2cap_init(); 262 l2cap_register_packet_handler(packet_handler); 263 264 rfcomm_init(); 265 rfcomm_register_packet_handler(packet_handler); 266 rfcomm_register_service(rfcomm_channel_nr, 0xffff); // reserved channel, mtu limited by l2cap 267 268 sdp_query_rfcomm_register_callback(handle_query_rfcomm_event, NULL); 269 270 hsp_ag_reset_state(); 271 } 272 273 void hsp_ag_connect(bd_addr_t bd_addr){ 274 if (hsp_state != HSP_IDLE) return; 275 hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL; 276 memcpy(remote, bd_addr, 6); 277 hsp_run(); 278 } 279 280 void hsp_ag_disconnect(void){ 281 switch (hsp_state){ 282 case HSP_ACTIVE: 283 hsp_state = HSP_W2_DISCONNECT_SCO; 284 break; 285 case HSP_W2_CONNECT_SCO: 286 hsp_state = HSP_W2_DISCONNECT_RFCOMM; 287 break; 288 289 case HSP_W4_RFCOMM_CONNECTED: 290 case HSP_W4_SCO_CONNECTED: 291 hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 292 break; 293 default: 294 return; 295 } 296 hsp_run(); 297 } 298 299 void hsp_ag_set_microphone_gain(uint8_t gain){ 300 if (gain < 0 || gain >15) { 301 printf("Gain must be in interval [0..15], it is given %d\n", gain); 302 return; 303 } 304 ag_microphone_gain = gain; 305 hsp_run(); 306 } 307 308 // AG +VGS=5 [0..15] ; HS AT+VGM=6 | AG OK 309 void hsp_ag_set_speaker_gain(uint8_t gain){ 310 if (gain < 0 || gain >15) { 311 printf("Gain must be in interval [0..15], it is given %d\n", gain); 312 return; 313 } 314 ag_speaker_gain = gain; 315 hsp_run(); 316 } 317 318 static void hsp_timeout_handler(btstack_timer_source_t * timer){ 319 ag_ring = 1; 320 } 321 322 static void hsp_timeout_start(void){ 323 btstack_run_loop_remove_timer(&hs_timeout); 324 btstack_run_loop_set_timer_handler(&hs_timeout, hsp_timeout_handler); 325 btstack_run_loop_set_timer(&hs_timeout, 2000); // 2 seconds timeout 326 btstack_run_loop_add_timer(&hs_timeout); 327 } 328 329 static void hsp_timeout_stop(void){ 330 btstack_run_loop_remove_timer(&hs_timeout); 331 } 332 333 void hsp_ag_start_ringing(void){ 334 if (hsp_state != HSP_W2_CONNECT_SCO) return; 335 ag_ring = 1; 336 hsp_state = HSP_W4_RING_ANSWER; 337 hsp_timeout_start(); 338 } 339 340 void hsp_ag_stop_ringing(void){ 341 ag_ring = 0; 342 ag_num_button_press_received = 0; 343 hsp_state = HSP_W2_CONNECT_SCO; 344 hsp_timeout_stop(); 345 } 346 347 static void hsp_run(void){ 348 int err; 349 350 if (ag_send_ok){ 351 ag_send_ok = 0; 352 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_OK); 353 if (err){ 354 ag_send_ok = 1; 355 } 356 return; 357 } 358 359 if (ag_send_error){ 360 ag_send_error = 0; 361 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_ERROR); 362 if (err) { 363 ag_send_error = 1; 364 } 365 return; 366 } 367 368 switch (hsp_state){ 369 case HSP_SDP_QUERY_RFCOMM_CHANNEL: 370 hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE; 371 printf("Start SDP query %s, 0x%02x\n", bd_addr_to_str(remote), SDP_HSP); 372 sdp_query_rfcomm_channel_and_name_for_uuid(remote, SDP_HSP); 373 break; 374 375 case HSP_W4_RING_ANSWER: 376 if (ag_ring){ 377 ag_ring = 0; 378 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_RING); 379 if (err) { 380 ag_ring = 1; 381 } 382 break; 383 } 384 385 if (!ag_num_button_press_received) break; 386 ag_send_ok = 0; 387 388 ag_num_button_press_received = 0; 389 hsp_state = HSP_W2_CONNECT_SCO; 390 391 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, HSP_AG_OK); 392 if (err) { 393 hsp_state = HSP_W4_RING_ANSWER; 394 ag_num_button_press_received = 1; 395 } 396 break; 397 case HSP_W2_CONNECT_SCO: 398 if (!hci_can_send_command_packet_now()) break; 399 hsp_state = HSP_W4_SCO_CONNECTED; 400 hci_send_cmd(&hci_setup_synchronous_connection, rfcomm_handle, 8000, 8000, 0xFFFF, hci_get_sco_voice_setting(), 0xFF, 0x003F); 401 break; 402 403 case HSP_W2_DISCONNECT_SCO: 404 ag_num_button_press_received = 0; 405 406 hsp_state = HSP_W4_SCO_DISCONNECTED; 407 gap_disconnect(sco_handle); 408 break; 409 410 case HSP_W2_DISCONNECT_RFCOMM: 411 hsp_state = HSP_W4_RFCOMM_DISCONNECTED; 412 rfcomm_disconnect(rfcomm_cid); 413 break; 414 case HSP_ACTIVE: 415 416 if (ag_microphone_gain >= 0){ 417 int gain = ag_microphone_gain; 418 ag_microphone_gain = -1; 419 char buffer[10]; 420 sprintf(buffer, "%s=%d\r\n", HSP_MICROPHONE_GAIN, gain); 421 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); 422 if (err) { 423 ag_microphone_gain = gain; 424 } 425 break; 426 } 427 428 if (ag_speaker_gain >= 0){ 429 int gain = ag_speaker_gain; 430 ag_speaker_gain = -1; 431 char buffer[10]; 432 sprintf(buffer, "%s=%d\r\n", HSP_SPEAKER_GAIN, gain); 433 err = hsp_ag_send_str_over_rfcomm(rfcomm_cid, buffer); 434 if (err) { 435 ag_speaker_gain = gain; 436 } 437 break; 438 } 439 break; 440 default: 441 break; 442 } 443 } 444 445 446 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 447 // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); 448 if (packet_type == RFCOMM_DATA_PACKET){ 449 while (size > 0 && (packet[0] == '\n' || packet[0] == '\r')){ 450 size--; 451 packet++; 452 } 453 454 if (strncmp((char *)packet, HSP_HS_BUTTON_PRESS, strlen(HSP_HS_BUTTON_PRESS)) == 0){ 455 printf("Received button press %s\n", HSP_HS_BUTTON_PRESS); 456 ag_num_button_press_received++; 457 ag_send_ok = 1; 458 if (hsp_state == HSP_ACTIVE && ag_num_button_press_received >=2){ 459 ag_num_button_press_received = 0; 460 hsp_state = HSP_W2_DISCONNECT_SCO; 461 } 462 } else if (strncmp((char *)packet, HSP_HS_MICROPHONE_GAIN, strlen(HSP_HS_MICROPHONE_GAIN)) == 0){ 463 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_HS_MICROPHONE_GAIN)]); 464 ag_send_ok = 1; 465 emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain); 466 467 } else if (strncmp((char *)packet, HSP_HS_SPEAKER_GAIN, strlen(HSP_HS_SPEAKER_GAIN)) == 0){ 468 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_HS_SPEAKER_GAIN)]); 469 ag_send_ok = 1; 470 emit_event(HSP_SUBEVENT_SPEAKER_GAIN_CHANGED, gain); 471 472 } else if (strncmp((char *)packet, "AT+", 3) == 0){ 473 ag_send_error = 1; 474 if (!hsp_ag_callback) return; 475 // re-use incoming buffer to avoid reserving large buffers - ugly but efficient 476 uint8_t * event = packet - 3; 477 event[0] = HCI_EVENT_HSP_META; 478 event[1] = size + 1; 479 event[2] = HSP_SUBEVENT_HS_COMMAND; 480 (*hsp_ag_callback)(event, size+3); 481 } 482 483 hsp_run(); 484 return; 485 } 486 487 if (packet_type != HCI_EVENT_PACKET) return; 488 uint8_t event = packet[0]; 489 bd_addr_t event_addr; 490 uint16_t handle; 491 492 switch (event) { 493 case BTSTACK_EVENT_STATE: 494 // BTstack activated, get started 495 if (packet[2] == HCI_STATE_WORKING){ 496 printf("BTstack activated, get started .\n"); 497 } 498 break; 499 500 case HCI_EVENT_PIN_CODE_REQUEST: 501 // inform about pin code request 502 printf("Pin code request - using '0000'\n\r"); 503 bt_flip_addr(event_addr, &packet[2]); 504 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 505 break; 506 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 507 int index = 2; 508 uint8_t status = packet[index++]; 509 sco_handle = little_endian_read_16(packet, index); 510 index+=2; 511 bd_addr_t address; 512 memcpy(address, &packet[index], 6); 513 index+=6; 514 uint8_t link_type = packet[index++]; 515 uint8_t transmission_interval = packet[index++]; // measured in slots 516 uint8_t retransmission_interval = packet[index++];// measured in slots 517 uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes 518 index+=2; 519 uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes 520 index+=2; 521 uint8_t air_mode = packet[index]; 522 523 if (status != 0){ 524 log_error("(e)SCO Connection failed, status %u", status); 525 emit_event_audio_connected(status, sco_handle); 526 break; 527 } 528 switch (link_type){ 529 case 0x00: 530 printf("SCO Connection established. \n"); 531 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 532 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 533 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 534 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 535 break; 536 case 0x02: 537 printf("eSCO Connection established. \n"); 538 break; 539 default: 540 log_error("(e)SCO reserved link_type 0x%2x", link_type); 541 break; 542 } 543 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 544 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, 545 bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 546 547 if (hsp_state == HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 548 hsp_state = HSP_W2_DISCONNECT_SCO; 549 break; 550 } 551 552 hsp_state = HSP_ACTIVE; 553 emit_event_audio_connected(status, sco_handle); 554 break; 555 } 556 557 case RFCOMM_EVENT_INCOMING_CONNECTION: 558 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 559 if (hsp_state != HSP_IDLE) return; 560 561 bt_flip_addr(event_addr, &packet[2]); 562 rfcomm_cid = little_endian_read_16(packet, 9); 563 printf("RFCOMM channel %u requested for %s\n", packet[8], bd_addr_to_str(event_addr)); 564 rfcomm_accept_connection(rfcomm_cid); 565 566 hsp_state = HSP_W4_RFCOMM_CONNECTED; 567 568 break; 569 570 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: 571 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); 572 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 573 if (packet[2]) { 574 printf("RFCOMM channel open failed, status %u\n", packet[2]); 575 hsp_ag_reset_state(); 576 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]); 577 } else { 578 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16) 579 rfcomm_handle = little_endian_read_16(packet, 9); 580 rfcomm_cid = little_endian_read_16(packet, 12); 581 mtu = little_endian_read_16(packet, 14); 582 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u, state %d\n", rfcomm_cid, mtu, hsp_state); 583 584 switch (hsp_state){ 585 case HSP_W4_RFCOMM_CONNECTED: 586 ag_num_button_press_received = 0; 587 hsp_state = HSP_W2_CONNECT_SCO; 588 break; 589 case HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 590 hsp_state = HSP_W2_DISCONNECT_RFCOMM; 591 break; 592 default: 593 printf("no valid state\n"); 594 break; 595 } 596 } 597 break; 598 case DAEMON_EVENT_HCI_PACKET_SENT: 599 case RFCOMM_EVENT_CREDITS: 600 break; 601 602 case HCI_EVENT_DISCONNECTION_COMPLETE: 603 if (hsp_state != HSP_W4_SCO_DISCONNECTED){ 604 log_info("received gap disconnect in wrong hsp state"); 605 } 606 handle = little_endian_read_16(packet,3); 607 if (handle == sco_handle){ 608 printf("SCO disconnected, w2 disconnect RFCOMM\n"); 609 sco_handle = 0; 610 hsp_state = HSP_W2_DISCONNECT_RFCOMM; 611 break; 612 } 613 break; 614 case RFCOMM_EVENT_CHANNEL_CLOSED: 615 if (hsp_state != HSP_W4_RFCOMM_DISCONNECTED){ 616 log_info("received RFCOMM disconnect in wrong hsp state"); 617 } 618 printf("RFCOMM channel closed\n"); 619 hsp_ag_reset_state(); 620 emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); 621 break; 622 default: 623 break; 624 } 625 hsp_run(); 626 } 627 628 static void handle_query_rfcomm_event(uint8_t packet_type, uint8_t *packet, uint16_t size, void * context){ 629 switch (event->type){ 630 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 631 channel_nr = sdp_query_rfcomm_service_event_get_rfcomm_channel(packet); 632 printf("** Service name: '%s', RFCOMM port %u\n", sdp_query_rfcomm_service_event_get_name(packet), channel_nr); 633 break; 634 case SDP_EVENT_QUERY_COMPLETE: 635 if (channel_nr > 0){ 636 hsp_state = HSP_W4_RFCOMM_CONNECTED; 637 printf("RFCOMM create channel. state %d\n", HSP_W4_RFCOMM_CONNECTED); 638 rfcomm_create_channel(remote, channel_nr, NULL); 639 break; 640 } 641 hsp_ag_reset_state(); 642 printf("Service not found, status %u.\n", sdp_query_complete_event_get_status(packet)); 643 if (sdp_query_complete_event_get_status(packet)){ 644 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, sdp_query_complete_event_get_status(packet)); 645 } else { 646 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, SDP_SERVICE_NOT_FOUND); 647 } 648 break; 649 default: 650 break; 651 } 652 } 653 654 655