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 Headset (!! 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_hs.h" 61 #include "l2cap.h" 62 63 #define HSP_AG_OK "OK" 64 #define HSP_AG_ERROR "ERROR" 65 #define HSP_AG_RING "RING" 66 #define HSP_MICROPHONE_GAIN "+VGM=" 67 #define HSP_SPEAKER_GAIN "+VGS=" 68 69 #define HSP_HS_AT_CKPD "AT+CKPD=200\r\n" 70 #define HSP_HS_MICROPHONE_GAIN "AT+VGM" 71 #define HSP_HS_SPEAKER_GAIN "AT+VGS" 72 73 static const char default_hsp_hs_service_name[] = "Headset"; 74 75 static bd_addr_t remote = {0x04, 0x0C, 0xCE, 0xE4, 0x85, 0xD3}; 76 static uint8_t channel_nr = 0; 77 78 static uint16_t mtu; 79 static uint16_t rfcomm_cid = 0; 80 static uint16_t sco_handle = 0; 81 static uint16_t rfcomm_handle = 0; 82 83 // static uint8_t connection_state = 0; 84 85 static int hs_microphone_gain = -1; 86 static int hs_speaker_gain = -1; 87 88 static uint8_t hs_send_button_press = 0; 89 static uint8_t hs_support_custom_indications = 0; 90 static uint8_t hs_outgoing_connection = 0; 91 92 typedef enum { 93 HSP_IDLE, 94 HSP_SDP_QUERY_RFCOMM_CHANNEL, 95 HSP_W4_SDP_EVENT_QUERY_COMPLETE, 96 HSP_W4_RFCOMM_CONNECTED, 97 HSP_W4_USER_ACTION, 98 HSP_W2_CONNECT_SCO, 99 HSP_W4_SCO_CONNECTED, 100 HSP_ACTIVE, 101 HSP_W2_DISCONNECT_SCO, 102 HSP_W4_SCO_DISCONNECTED, 103 HSP_W2_DISCONNECT_RFCOMM, 104 HSP_W4_RFCOMM_DISCONNECTED, 105 HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN 106 } hsp_state_t; 107 108 static hsp_state_t hsp_state = HSP_IDLE; 109 110 static void hsp_run(void); 111 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 112 static void handle_query_rfcomm_event(uint8_t packet_type, uint8_t *packet, uint16_t size, void * context); 113 114 static hsp_hs_callback_t hsp_hs_callback; 115 static void dummy_notify(uint8_t * event, uint16_t size){} 116 117 void hsp_hs_register_packet_handler(hsp_hs_callback_t callback){ 118 if (callback == NULL){ 119 callback = &dummy_notify; 120 } 121 hsp_hs_callback = callback; 122 } 123 124 static void emit_event(uint8_t event_subtype, uint8_t value){ 125 if (!hsp_hs_callback) return; 126 uint8_t event[4]; 127 event[0] = HCI_EVENT_HSP_META; 128 event[1] = sizeof(event) - 2; 129 event[2] = event_subtype; 130 event[3] = value; // status 0 == OK 131 (*hsp_hs_callback)(event, sizeof(event)); 132 } 133 134 static void emit_event_audio_connected(uint8_t status, uint16_t handle){ 135 if (!hsp_hs_callback) return; 136 uint8_t event[6]; 137 event[0] = HCI_EVENT_HSP_META; 138 event[1] = sizeof(event) - 2; 139 event[2] = HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE; 140 event[3] = status; 141 bt_store_16(event, 4, handle); 142 (*hsp_hs_callback)(event, sizeof(event)); 143 } 144 145 // remote audio volume control 146 // AG +VGM=13 [0..15] ; HS AT+VGM=6 | AG OK 147 148 static int hsp_hs_send_str_over_rfcomm(uint16_t cid, const char * command){ 149 if (!rfcomm_can_send_packet_now(rfcomm_cid)) return 1; 150 int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 151 if (err){ 152 printf("rfcomm_send -> error 0X%02x", err); 153 } 154 return err; 155 } 156 157 void hsp_hs_support_custom_indications(int enable){ 158 hs_support_custom_indications = enable; 159 } 160 161 // When support custom commands is enabled, AG will send HSP_SUBEVENT_HS_COMMAND. 162 // On occurance of this event, client's packet handler must send the result back 163 // by calling hsp_hs_send_result function. 164 int hsp_hs_send_result(char * result){ 165 if (!hs_support_custom_indications) return 1; 166 return hsp_hs_send_str_over_rfcomm(rfcomm_cid, result); 167 } 168 169 170 void hsp_hs_create_service(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t have_remote_audio_control){ 171 uint8_t* attribute; 172 de_create_sequence(service); 173 174 // 0x0000 "Service Record Handle" 175 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle); 176 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 177 178 // 0x0001 "Service Class ID List" 179 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList); 180 attribute = de_push_sequence(service); 181 { 182 // see Bluetooth Erratum #3507 183 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_HSP); // 0x1108 184 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_Headset_HS); // 0x1131 185 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio); // 0x1203 186 } 187 de_pop_sequence(service, attribute); 188 189 // 0x0004 "Protocol Descriptor List" 190 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList); 191 attribute = de_push_sequence(service); 192 { 193 uint8_t* l2cpProtocol = de_push_sequence(attribute); 194 { 195 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol); 196 } 197 de_pop_sequence(attribute, l2cpProtocol); 198 199 uint8_t* rfcomm = de_push_sequence(attribute); 200 { 201 de_add_number(rfcomm, DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol); // rfcomm_service 202 de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 203 } 204 de_pop_sequence(attribute, rfcomm); 205 } 206 de_pop_sequence(service, attribute); 207 208 // 0x0005 "Public Browse Group" 209 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group 210 attribute = de_push_sequence(service); 211 { 212 de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup); 213 } 214 de_pop_sequence(service, attribute); 215 216 // 0x0009 "Bluetooth Profile Descriptor List" 217 de_add_number(service, DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList); 218 attribute = de_push_sequence(service); 219 { 220 uint8_t *hsp_profile = de_push_sequence(attribute); 221 { 222 de_add_number(hsp_profile, DE_UUID, DE_SIZE_16, SDP_HSP); 223 de_add_number(hsp_profile, DE_UINT, DE_SIZE_16, 0x0102); // Verision 1.2 224 } 225 de_pop_sequence(attribute, hsp_profile); 226 } 227 de_pop_sequence(service, attribute); 228 229 // 0x0100 "Service Name" 230 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 231 if (name){ 232 de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 233 } else { 234 de_add_data(service, DE_STRING, strlen(default_hsp_hs_service_name), (uint8_t *) default_hsp_hs_service_name); 235 } 236 237 // Remote audio volume control 238 de_add_number(service, DE_UINT, DE_SIZE_16, 0x030C); 239 de_add_number(service, DE_BOOL, DE_SIZE_8, have_remote_audio_control); 240 } 241 242 static void hsp_hs_reset_state(void){ 243 hsp_state = HSP_IDLE; 244 245 rfcomm_cid = 0; 246 rfcomm_handle = 0; 247 sco_handle = 0; 248 249 hs_microphone_gain = -1; 250 hs_speaker_gain = -1; 251 252 hs_send_button_press = 0; 253 hs_support_custom_indications = 0; 254 } 255 256 void hsp_hs_init(uint8_t rfcomm_channel_nr){ 257 // init L2CAP 258 l2cap_init(); 259 l2cap_register_packet_handler(packet_handler); 260 261 rfcomm_init(); 262 rfcomm_register_packet_handler(packet_handler); 263 rfcomm_register_service(rfcomm_channel_nr, 0xffff); // reserved channel, mtu limited by l2cap 264 265 sdp_query_rfcomm_register_callback(handle_query_rfcomm_event, NULL); 266 267 hsp_hs_reset_state(); 268 } 269 270 271 void hsp_hs_connect(bd_addr_t bd_addr){ 272 if (hsp_state != HSP_IDLE) return; 273 hs_outgoing_connection = 1; 274 hsp_state = HSP_SDP_QUERY_RFCOMM_CHANNEL; 275 memcpy(remote, bd_addr, 6); 276 hsp_run(); 277 } 278 279 void hsp_hs_disconnect(bd_addr_t bd_addr){ 280 switch (hsp_state){ 281 case HSP_ACTIVE: 282 printf("HSP_W4_USER_ACTION\n"); 283 hsp_state = HSP_W4_USER_ACTION; 284 hs_send_button_press = 1; 285 break; 286 case HSP_W4_RFCOMM_CONNECTED: 287 printf("HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN \n"); 288 hsp_state = HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 289 break; 290 default: 291 return; 292 } 293 hsp_run(); 294 } 295 296 297 void hsp_hs_set_microphone_gain(uint8_t gain){ 298 if (gain < 0 || gain >15) { 299 printf("Gain must be in interval [0..15], it is given %d\n", gain); 300 return; 301 } 302 hs_microphone_gain = gain; 303 hsp_run(); 304 } 305 306 // AG +VGS=5 [0..15] ; HS AT+VGM=6 | AG OK 307 void hsp_hs_set_speaker_gain(uint8_t gain){ 308 if (gain < 0 || gain >15) { 309 printf("Gain must be in interval [0..15], it is given %d\n", gain); 310 return; 311 } 312 hs_speaker_gain = gain; 313 hsp_run(); 314 } 315 316 317 static void hsp_run(void){ 318 int err; 319 320 if (hs_send_button_press){ 321 hs_send_button_press = 0; 322 err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, HSP_HS_AT_CKPD); 323 if (err) { 324 hs_send_button_press = 1; 325 } 326 return; 327 } 328 329 switch (hsp_state){ 330 case HSP_SDP_QUERY_RFCOMM_CHANNEL: 331 hsp_state = HSP_W4_SDP_EVENT_QUERY_COMPLETE; 332 sdp_query_rfcomm_channel_and_name_for_uuid(remote, SDP_Headset_AG); 333 break; 334 335 case HSP_W2_CONNECT_SCO: 336 hsp_state = HSP_W4_SCO_CONNECTED; 337 break; 338 339 case HSP_W2_DISCONNECT_SCO: 340 hsp_state = HSP_W4_SCO_DISCONNECTED; 341 break; 342 343 case HSP_ACTIVE: 344 345 if (hs_microphone_gain >= 0){ 346 int gain = hs_microphone_gain; 347 hs_microphone_gain = -1; 348 char buffer[20]; 349 sprintf(buffer, "%s=%d\r\n", HSP_HS_MICROPHONE_GAIN, gain); 350 err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); 351 if (err) { 352 hs_microphone_gain = gain; 353 } 354 break; 355 } 356 357 if (hs_speaker_gain >= 0){ 358 int gain = hs_speaker_gain; 359 hs_speaker_gain = -1; 360 char buffer[20]; 361 sprintf(buffer, "%s=%d\r\n", HSP_HS_SPEAKER_GAIN, gain); 362 err = hsp_hs_send_str_over_rfcomm(rfcomm_cid, buffer); 363 if (err) { 364 hs_speaker_gain = gain; 365 } 366 break; 367 } 368 369 break; 370 default: 371 break; 372 } 373 } 374 375 376 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 377 // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); 378 if (packet_type == RFCOMM_DATA_PACKET){ 379 // skip over leading newline 380 while (size > 0 && (packet[0] == '\n' || packet[0] == '\r')){ 381 size--; 382 packet++; 383 } 384 if (strncmp((char *)packet, HSP_AG_RING, strlen(HSP_AG_RING)) == 0){ 385 emit_event(HSP_SUBEVENT_RING, 0); 386 } else if (strncmp((char *)packet, HSP_AG_OK, strlen(HSP_AG_OK)) == 0){ 387 printf("OK RECEIVED\n"); 388 switch (hsp_state){ 389 case HSP_W4_RFCOMM_CONNECTED: 390 hsp_state = HSP_W2_CONNECT_SCO; 391 break; 392 case HSP_W4_USER_ACTION: 393 hsp_state = HSP_W2_DISCONNECT_SCO; 394 break; 395 default: 396 break; 397 } 398 } else if (strncmp((char *)packet, HSP_MICROPHONE_GAIN, strlen(HSP_MICROPHONE_GAIN)) == 0){ 399 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_MICROPHONE_GAIN)]); 400 emit_event(HSP_SUBEVENT_MICROPHONE_GAIN_CHANGED, gain); 401 402 } else if (strncmp((char *)packet, HSP_SPEAKER_GAIN, strlen(HSP_SPEAKER_GAIN)) == 0){ 403 uint8_t gain = (uint8_t)atoi((char*)&packet[strlen(HSP_SPEAKER_GAIN)]); 404 emit_event(HSP_SUBEVENT_SPEAKER_GAIN_CHANGED, gain); 405 } else { 406 if (!hsp_hs_callback) return; 407 // strip trailing newline 408 while (size > 0 && (packet[size-1] == '\n' || packet[size-1] == '\r')){ 409 size--; 410 } 411 // add trailing \0 412 packet[size] = 0; 413 // re-use incoming buffer to avoid reserving large buffers - ugly but efficient 414 uint8_t * event = packet - 3; 415 event[0] = HCI_EVENT_HSP_META; 416 event[1] = size + 1; 417 event[2] = HSP_SUBEVENT_AG_INDICATION; 418 (*hsp_hs_callback)(event, size+3); 419 } 420 hsp_run(); 421 return; 422 } 423 424 if (packet_type != HCI_EVENT_PACKET) return; 425 uint8_t event = packet[0]; 426 bd_addr_t event_addr; 427 uint16_t handle; 428 429 switch (event) { 430 case BTSTACK_EVENT_STATE: 431 // bt stack activated, get started 432 if (packet[2] == HCI_STATE_WORKING){ 433 printf("BTstack activated, get started .\n"); 434 } 435 hsp_hs_callback(packet, size); 436 break; 437 438 case HCI_EVENT_PIN_CODE_REQUEST: 439 // inform about pin code request 440 printf("Pin code request - using '0000'\n\r"); 441 bt_flip_addr(event_addr, &packet[2]); 442 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 443 break; 444 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 445 int index = 2; 446 uint8_t status = packet[index++]; 447 sco_handle = READ_BT_16(packet, index); 448 index+=2; 449 bd_addr_t address; 450 memcpy(address, &packet[index], 6); 451 index+=6; 452 uint8_t link_type = packet[index++]; 453 uint8_t transmission_interval = packet[index++]; // measured in slots 454 uint8_t retransmission_interval = packet[index++];// measured in slots 455 uint16_t rx_packet_length = READ_BT_16(packet, index); // measured in bytes 456 index+=2; 457 uint16_t tx_packet_length = READ_BT_16(packet, index); // measured in bytes 458 index+=2; 459 uint8_t air_mode = packet[index]; 460 461 if (status != 0){ 462 log_error("(e)SCO Connection failed, status %u", status); 463 emit_event_audio_connected(status, sco_handle); 464 break; 465 } 466 switch (link_type){ 467 case 0x00: 468 printf("SCO Connection established. \n"); 469 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 470 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 471 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 472 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 473 break; 474 case 0x02: 475 printf("eSCO Connection established. \n"); 476 break; 477 default: 478 log_error("(e)SCO reserved link_type 0x%2x", link_type); 479 break; 480 } 481 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 482 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)", sco_handle, 483 bd_addr_to_str(address), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 484 485 if (hsp_state == HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 486 hsp_state = HSP_W2_DISCONNECT_SCO; 487 break; 488 } 489 490 // forward event to app 491 hsp_hs_callback(packet, size); 492 493 hsp_state = HSP_ACTIVE; 494 emit_event_audio_connected(0, sco_handle); 495 break; 496 } 497 498 case RFCOMM_EVENT_INCOMING_CONNECTION: 499 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 500 if (hsp_state != HSP_IDLE) return; 501 502 bt_flip_addr(event_addr, &packet[2]); 503 rfcomm_cid = READ_BT_16(packet, 9); 504 printf("RFCOMM channel %u requested for %s\n", packet[8], bd_addr_to_str(event_addr)); 505 rfcomm_accept_connection(rfcomm_cid); 506 507 hsp_state = HSP_W4_RFCOMM_CONNECTED; 508 break; 509 510 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: 511 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x\n", packet_type, packet[0]); 512 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 513 if (packet[2]) { 514 printf("RFCOMM channel open failed, status %u\n", packet[2]); 515 hsp_hs_reset_state(); 516 emit_event(HSP_SUBEVENT_AUDIO_CONNECTION_COMPLETE, packet[2]); 517 hs_outgoing_connection = 0; 518 } else { 519 // data: event(8) , len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16) 520 rfcomm_handle = READ_BT_16(packet, 9); 521 rfcomm_cid = READ_BT_16(packet, 12); 522 mtu = READ_BT_16(packet, 14); 523 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, mtu); 524 525 if (hs_outgoing_connection){ 526 hs_outgoing_connection = 0; 527 hs_send_button_press = 1; 528 } 529 530 switch (hsp_state){ 531 case HSP_W4_RFCOMM_CONNECTED: 532 hsp_state = HSP_W2_CONNECT_SCO; 533 break; 534 case HSP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 535 hsp_state = HSP_W2_DISCONNECT_RFCOMM; 536 break; 537 default: 538 break; 539 } 540 } 541 break; 542 case DAEMON_EVENT_HCI_PACKET_SENT: 543 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 544 case RFCOMM_EVENT_CREDITS: 545 hsp_hs_callback(packet, size); 546 break; 547 548 case HCI_EVENT_DISCONNECTION_COMPLETE: 549 printf("HCI_EVENT_DISCONNECTION_COMPLETE \n"); 550 // if (hsp_state != HSP_W4_SCO_DISCONNECTED){ 551 // printf("received gap disconnect in wrong hsp state\n"); 552 // } 553 handle = READ_BT_16(packet,3); 554 if (handle == sco_handle){ 555 sco_handle = 0; 556 hsp_state = HSP_W2_DISCONNECT_RFCOMM; 557 printf(" HSP_W2_DISCONNECT_RFCOMM\n"); 558 break; 559 } 560 break; 561 case RFCOMM_EVENT_CHANNEL_CLOSED: 562 printf("RFCOMM_EVENT_CHANNEL_CLOSED\n"); 563 // if (hsp_state != HSP_W4_RFCOMM_DISCONNECTED){ 564 // printf("received RFCOMM disconnect in wrong hsp state\n"); 565 // } 566 printf("RFCOMM channel closed\n"); 567 hsp_hs_reset_state(); 568 emit_event(HSP_SUBEVENT_AUDIO_DISCONNECTION_COMPLETE,0); 569 break; 570 default: 571 break; 572 } 573 hsp_run(); 574 } 575 576 static void handle_query_rfcomm_event(uint8_t packet_type, uint8_t *packet, uint16_t size, void * context){ 577 switch (packet[0]){ 578 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 579 channel_nr = sdp_query_rfcomm_service_event_get_rfcomm_channel(packet); 580 printf("** Service name: '%s', RFCOMM port %u\n", sdp_query_rfcomm_service_event_get_name(packet), channel_nr); 581 break; 582 case SDP_EVENT_QUERY_COMPLETE: 583 if (channel_nr > 0){ 584 hsp_state = HSP_W4_RFCOMM_CONNECTED; 585 printf("RFCOMM create channel.\n"); 586 rfcomm_create_channel(remote, channel_nr, NULL); 587 break; 588 } 589 hsp_hs_reset_state(); 590 printf("Service not found, status %u.\n", sdp_query_complete_event_get_status(packet)); 591 exit(0); 592 break; 593 } 594 } 595 596 void hsp_hs_press_button(void){ 597 hs_send_button_press = 1; 598 hsp_run(); 599 } 600 601 602 603 604