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__ "hfp.c" 39 40 41 #include "btstack_config.h" 42 43 #include <stdint.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <inttypes.h> 48 49 #include "bluetooth_sdp.h" 50 #include "btstack_debug.h" 51 #include "btstack_event.h" 52 #include "btstack_memory.h" 53 #include "btstack_run_loop.h" 54 #include "classic/core.h" 55 #include "classic/sdp_client_rfcomm.h" 56 #include "classic/sdp_server.h" 57 #include "classic/sdp_util.h" 58 #include "hci.h" 59 #include "hci_cmd.h" 60 #include "hci_dump.h" 61 #include "l2cap.h" 62 63 #define HFP_HF_FEATURES_SIZE 10 64 #define HFP_AG_FEATURES_SIZE 12 65 66 67 static const char * hfp_hf_features[] = { 68 "EC and/or NR function", 69 "Three-way calling", 70 "CLI presentation capability", 71 "Voice recognition activation", 72 "Remote volume control", 73 74 "Enhanced call status", 75 "Enhanced call control", 76 77 "Codec negotiation", 78 79 "HF Indicators", 80 "eSCO S4 (and T2) Settings Supported", 81 "Reserved for future definition" 82 }; 83 84 static const char * hfp_ag_features[] = { 85 "Three-way calling", 86 "EC and/or NR function", 87 "Voice recognition function", 88 "In-band ring tone capability", 89 "Attach a number to a voice tag", 90 "Ability to reject a call", 91 "Enhanced call status", 92 "Enhanced call control", 93 "Extended Error Result Codes", 94 "Codec negotiation", 95 "HF Indicators", 96 "eSCO S4 (and T2) Settings Supported", 97 "Reserved for future definition" 98 }; 99 100 static void parse_sequence(hfp_connection_t * context); 101 102 static btstack_linked_list_t hfp_connections = NULL; 103 104 static btstack_packet_handler_t hfp_hf_callback; 105 static btstack_packet_handler_t hfp_ag_callback; 106 107 static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler; 108 static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler; 109 110 static void (*hfp_hf_run_for_context)(hfp_connection_t * hfp_connection); 111 112 static hfp_connection_t * sco_establishment_active; 113 114 const char * hfp_hf_feature(int index){ 115 if (index > HFP_HF_FEATURES_SIZE){ 116 return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 117 } 118 return hfp_hf_features[index]; 119 } 120 121 const char * hfp_ag_feature(int index){ 122 if (index > HFP_AG_FEATURES_SIZE){ 123 return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 124 } 125 return hfp_ag_features[index]; 126 } 127 128 int send_str_over_rfcomm(uint16_t cid, char * command){ 129 if (!rfcomm_can_send_packet_now(cid)) return 1; 130 log_info("HFP_TX %s", command); 131 int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 132 if (err){ 133 log_error("rfcomm_send -> error 0x%02x \n", err); 134 } 135 return 1; 136 } 137 138 int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 139 140 // mSBC requires support for eSCO connections 141 if (codec == HFP_CODEC_MSBC && !hci_extended_sco_link_supported()) return 0; 142 143 int i; 144 for (i = 0; i < codecs_nr; i++){ 145 if (codecs[i] != codec) continue; 146 return 1; 147 } 148 return 0; 149 } 150 151 void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 152 if (hci_extended_sco_link_supported()) return; 153 uint8_t tmp_codecs[HFP_MAX_NUM_CODECS]; 154 int i; 155 int tmp_codec_nr = 0; 156 for (i=0; i < *codecs_nr; i++){ 157 if (codecs[i] == HFP_CODEC_MSBC) continue; 158 tmp_codecs[tmp_codec_nr++] = codecs[i]; 159 } 160 *codecs_nr = tmp_codec_nr; 161 memcpy(codecs, tmp_codecs, tmp_codec_nr); 162 } 163 164 // UTILS 165 int get_bit(uint16_t bitmap, int position){ 166 return (bitmap >> position) & 1; 167 } 168 169 int store_bit(uint32_t bitmap, int position, uint8_t value){ 170 if (value){ 171 bitmap |= 1 << position; 172 } else { 173 bitmap &= ~ (1 << position); 174 } 175 return bitmap; 176 } 177 178 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 179 if (buffer_size < values_nr * 3) return 0; 180 int i; 181 int offset = 0; 182 for (i = 0; i < values_nr-1; i++) { 183 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 184 } 185 if (i<values_nr){ 186 offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 187 } 188 return offset; 189 } 190 191 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 192 if (buffer_size < values_nr * 3) return 0; 193 194 int i; 195 int offset = 0; 196 for (i = 0; i < values_nr-1; i++) { 197 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 198 } 199 200 if (i<values_nr){ 201 offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 202 } 203 return offset; 204 } 205 206 static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 207 if (!hfp_connection) return; 208 btstack_packet_handler_t callback = NULL; 209 switch (hfp_connection->local_role){ 210 case HFP_ROLE_HF: 211 callback = hfp_hf_callback; 212 break; 213 case HFP_ROLE_AG: 214 callback = hfp_ag_callback; 215 break; 216 default: 217 return; 218 } 219 (*callback)(HCI_EVENT_PACKET, 0, packet, size); 220 } 221 222 void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 223 uint8_t event[3]; 224 event[0] = HCI_EVENT_HFP_META; 225 event[1] = sizeof(event) - 2; 226 event[2] = event_subtype; 227 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 228 } 229 230 void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 231 uint8_t event[4]; 232 event[0] = HCI_EVENT_HFP_META; 233 event[1] = sizeof(event) - 2; 234 event[2] = event_subtype; 235 event[3] = value; // status 0 == OK 236 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 237 } 238 239 void hfp_emit_slc_connection_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ 240 uint8_t event[12]; 241 int pos = 0; 242 event[pos++] = HCI_EVENT_HFP_META; 243 event[pos++] = sizeof(event) - 2; 244 event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 245 event[pos++] = status; // status 0 == OK 246 little_endian_store_16(event, pos, con_handle); 247 pos += 2; 248 reverse_bd_addr(addr,&event[pos]); 249 pos += 6; 250 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 251 } 252 253 static void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t negotiated_codec){ 254 uint8_t event[13]; 255 int pos = 0; 256 event[pos++] = HCI_EVENT_HFP_META; 257 event[pos++] = sizeof(event) - 2; 258 event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 259 event[pos++] = status; // status 0 == OK 260 little_endian_store_16(event, pos, con_handle); 261 pos += 2; 262 reverse_bd_addr(addr,&event[pos]); 263 pos += 6; 264 event[pos++] = negotiated_codec; 265 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 266 } 267 268 void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 269 uint8_t event[40]; 270 event[0] = HCI_EVENT_HFP_META; 271 event[1] = sizeof(event) - 2; 272 event[2] = event_subtype; 273 int size = ( strlen(value) < sizeof(event) - 4) ? (int) strlen(value) : (int) sizeof(event) - 4; 274 strncpy((char*)&event[3], value, size); 275 event[3 + size] = 0; 276 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 277 } 278 279 btstack_linked_list_t * hfp_get_connections(void){ 280 return (btstack_linked_list_t *) &hfp_connections; 281 } 282 283 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 284 btstack_linked_list_iterator_t it; 285 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 286 while (btstack_linked_list_iterator_has_next(&it)){ 287 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 288 if (hfp_connection->rfcomm_cid == cid){ 289 return hfp_connection; 290 } 291 } 292 return NULL; 293 } 294 295 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){ 296 btstack_linked_list_iterator_t it; 297 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 298 while (btstack_linked_list_iterator_has_next(&it)){ 299 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 300 if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) { 301 return hfp_connection; 302 } 303 } 304 return NULL; 305 } 306 307 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){ 308 btstack_linked_list_iterator_t it; 309 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 310 while (btstack_linked_list_iterator_has_next(&it)){ 311 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 312 if (hfp_connection->sco_handle == handle){ 313 return hfp_connection; 314 } 315 } 316 return NULL; 317 } 318 319 hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle){ 320 btstack_linked_list_iterator_t it; 321 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 322 while (btstack_linked_list_iterator_has_next(&it)){ 323 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 324 if (hfp_connection->acl_handle == handle){ 325 return hfp_connection; 326 } 327 } 328 return NULL; 329 } 330 331 void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 332 if (!hfp_connection) return; 333 hfp_connection->ok_pending = 0; 334 hfp_connection->send_error = 0; 335 336 hfp_connection->keep_byte = 0; 337 338 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 339 hfp_connection->operator_name_changed = 0; 340 341 hfp_connection->enable_extended_audio_gateway_error_report = 0; 342 hfp_connection->extended_audio_gateway_error = 0; 343 344 // establish codecs hfp_connection 345 hfp_connection->suggested_codec = 0; 346 hfp_connection->negotiated_codec = 0; 347 hfp_connection->codec_confirmed = 0; 348 349 hfp_connection->establish_audio_connection = 0; 350 hfp_connection->call_waiting_notification_enabled = 0; 351 hfp_connection->command = HFP_CMD_NONE; 352 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 353 } 354 355 static hfp_connection_t * create_hfp_connection_context(void){ 356 hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 357 if (!hfp_connection) return NULL; 358 // init state 359 memset(hfp_connection,0, sizeof(hfp_connection_t)); 360 361 hfp_connection->state = HFP_IDLE; 362 hfp_connection->call_state = HFP_CALL_IDLE; 363 hfp_connection->codecs_state = HFP_CODECS_IDLE; 364 365 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 366 hfp_connection->command = HFP_CMD_NONE; 367 368 hfp_reset_context_flags(hfp_connection); 369 370 btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 371 return hfp_connection; 372 } 373 374 static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){ 375 btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 376 btstack_memory_hfp_connection_free(hfp_connection); 377 } 378 379 static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){ 380 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 381 if (hfp_connection) return hfp_connection; 382 hfp_connection = create_hfp_connection_context(); 383 memcpy(hfp_connection->remote_addr, bd_addr, 6); 384 hfp_connection->local_role = local_role; 385 return hfp_connection; 386 } 387 388 /* @param network. 389 * 0 == no ability to reject a call. 390 * 1 == ability to reject a call. 391 */ 392 393 /* @param suported_features 394 * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 395 * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 396 * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 397 * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 398 * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 399 * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 400 */ 401 /* Bit position: 402 * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 403 * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 404 * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 405 * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 406 * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 407 * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 408 */ 409 410 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){ 411 uint8_t* attribute; 412 de_create_sequence(service); 413 414 // 0x0000 "Service Record Handle" 415 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 416 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 417 418 // 0x0001 "Service Class ID List" 419 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 420 attribute = de_push_sequence(service); 421 { 422 // "UUID for Service" 423 de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 424 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 425 } 426 de_pop_sequence(service, attribute); 427 428 // 0x0004 "Protocol Descriptor List" 429 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 430 attribute = de_push_sequence(service); 431 { 432 uint8_t* l2cpProtocol = de_push_sequence(attribute); 433 { 434 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 435 } 436 de_pop_sequence(attribute, l2cpProtocol); 437 438 uint8_t* rfcomm = de_push_sequence(attribute); 439 { 440 de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 441 de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 442 } 443 de_pop_sequence(attribute, rfcomm); 444 } 445 de_pop_sequence(service, attribute); 446 447 448 // 0x0005 "Public Browse Group" 449 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 450 attribute = de_push_sequence(service); 451 { 452 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 453 } 454 de_pop_sequence(service, attribute); 455 456 // 0x0009 "Bluetooth Profile Descriptor List" 457 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 458 attribute = de_push_sequence(service); 459 { 460 uint8_t *sppProfile = de_push_sequence(attribute); 461 { 462 de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 463 de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7 464 } 465 de_pop_sequence(attribute, sppProfile); 466 } 467 de_pop_sequence(service, attribute); 468 469 // 0x0100 "Service Name" 470 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 471 de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 472 } 473 474 static hfp_connection_t * connection_doing_sdp_query = NULL; 475 476 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 477 UNUSED(packet_type); // ok: handling own sdp events 478 UNUSED(channel); // ok: no channel 479 UNUSED(size); // ok: handling own sdp events 480 481 hfp_connection_t * hfp_connection = connection_doing_sdp_query; 482 if (!hfp_connection) { 483 log_error("handle_query_rfcomm_event, no connection"); 484 return; 485 } 486 487 switch (hci_event_packet_get_type(packet)){ 488 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 489 hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 490 break; 491 case SDP_EVENT_QUERY_COMPLETE: 492 connection_doing_sdp_query = NULL; 493 if (hfp_connection->rfcomm_channel_nr > 0){ 494 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 495 log_info("HFP: SDP_EVENT_QUERY_COMPLETE context %p, addr %s, state %d", hfp_connection, bd_addr_to_str( hfp_connection->remote_addr), hfp_connection->state); 496 btstack_packet_handler_t packet_handler; 497 switch (hfp_connection->local_role){ 498 case HFP_ROLE_AG: 499 packet_handler = hfp_ag_rfcomm_packet_handler; 500 break; 501 case HFP_ROLE_HF: 502 packet_handler = hfp_hf_rfcomm_packet_handler; 503 break; 504 default: 505 log_error("Role %x", hfp_connection->local_role); 506 return; 507 } 508 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 509 break; 510 } 511 hfp_connection->state = HFP_IDLE; 512 hfp_emit_slc_connection_event(hfp_connection, sdp_event_query_complete_get_status(packet), HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr); 513 log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet)); 514 break; 515 default: 516 break; 517 } 518 } 519 520 static void hfp_handle_failed_sco_connection(uint8_t status){ 521 522 if (!sco_establishment_active){ 523 log_error("(e)SCO Connection failed but not started by us"); 524 return; 525 } 526 log_error("(e)SCO Connection failed status 0x%02x", status); 527 528 // invalid params / unspecified error 529 if (status != 0x11 && status != 0x1f) return; 530 531 switch (sco_establishment_active->link_setting){ 532 case HFP_LINK_SETTINGS_D0: 533 return; // no other option left 534 case HFP_LINK_SETTINGS_D1: 535 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0; 536 break; 537 case HFP_LINK_SETTINGS_S1: 538 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 539 break; 540 case HFP_LINK_SETTINGS_S2: 541 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1; 542 break; 543 case HFP_LINK_SETTINGS_S3: 544 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S2; 545 break; 546 case HFP_LINK_SETTINGS_S4: 547 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3; 548 break; 549 case HFP_LINK_SETTINGS_T1: 550 log_info("T1 failed, fallback to CVSD - D1"); 551 sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 552 sco_establishment_active->sco_for_msbc_failed = 1; 553 sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC; 554 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 555 break; 556 case HFP_LINK_SETTINGS_T2: 557 sco_establishment_active->link_setting = HFP_LINK_SETTINGS_T1; 558 break; 559 } 560 sco_establishment_active->establish_audio_connection = 1; 561 sco_establishment_active = 0; 562 } 563 564 565 void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 566 UNUSED(channel); // ok: no channel 567 568 bd_addr_t event_addr; 569 hci_con_handle_t handle; 570 hfp_connection_t * hfp_connection = NULL; 571 uint8_t status; 572 573 log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 574 575 switch (hci_event_packet_get_type(packet)) { 576 577 case HCI_EVENT_CONNECTION_REQUEST: 578 switch(hci_event_connection_request_get_link_type(packet)){ 579 case 0: // SCO 580 case 2: // eSCO 581 hci_event_connection_request_get_bd_addr(packet, event_addr); 582 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 583 if (!hfp_connection) break; 584 log_info("hf accept sco\n"); 585 hfp_connection->hf_accept_sco = 1; 586 if (!hfp_hf_run_for_context) break; 587 (*hfp_hf_run_for_context)(hfp_connection); 588 break; 589 default: 590 break; 591 } 592 break; 593 594 case HCI_EVENT_COMMAND_STATUS: 595 if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 596 status = hci_event_command_status_get_status(packet); 597 if (status) { 598 hfp_handle_failed_sco_connection(hci_event_command_status_get_status(packet)); 599 } 600 } 601 break; 602 603 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 604 hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 605 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 606 if (!hfp_connection) { 607 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 608 return; 609 } 610 611 status = hci_event_synchronous_connection_complete_get_status(packet); 612 if (status != 0){ 613 hfp_connection->hf_accept_sco = 0; 614 hfp_handle_failed_sco_connection(status); 615 break; 616 } 617 618 uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 619 uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 620 uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 621 uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 622 uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 623 uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 624 uint8_t air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet); 625 626 switch (link_type){ 627 case 0x00: 628 log_info("SCO Connection established."); 629 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 630 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 631 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 632 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 633 break; 634 case 0x02: 635 log_info("eSCO Connection established. \n"); 636 break; 637 default: 638 log_error("(e)SCO reserved link_type 0x%2x", link_type); 639 break; 640 } 641 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 642 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 643 bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode); 644 645 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 646 647 if (!hfp_connection) { 648 log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr)); 649 break; 650 } 651 652 if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 653 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 654 hfp_connection->state = HFP_W2_DISCONNECT_SCO; 655 break; 656 } 657 hfp_connection->sco_handle = sco_handle; 658 hfp_connection->establish_audio_connection = 0; 659 hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 660 hfp_emit_sco_event(hfp_connection, packet[2], sco_handle, event_addr, hfp_connection->negotiated_codec); 661 break; 662 } 663 664 case HCI_EVENT_DISCONNECTION_COMPLETE: 665 handle = little_endian_read_16(packet,3); 666 hfp_connection = get_hfp_connection_context_for_sco_handle(handle); 667 668 if (!hfp_connection) break; 669 670 if (hfp_connection->state != HFP_W4_SCO_DISCONNECTED){ 671 log_info("Received gap disconnect in wrong hfp state"); 672 } 673 log_info("Check SCO handle: incoming 0x%02x, hfp_connection 0x%02x\n", handle, hfp_connection->sco_handle); 674 675 if (handle == hfp_connection->sco_handle){ 676 log_info("SCO disconnected, w2 disconnect RFCOMM\n"); 677 hfp_connection->sco_handle = 0; 678 hfp_connection->release_audio_connection = 0; 679 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 680 hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0); 681 break; 682 } 683 break; 684 685 default: 686 break; 687 } 688 } 689 690 void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 691 UNUSED(channel); // ok: no channel 692 693 bd_addr_t event_addr; 694 uint16_t rfcomm_cid; 695 hfp_connection_t * hfp_connection = NULL; 696 uint8_t status; 697 698 log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 699 700 switch (hci_event_packet_get_type(packet)) { 701 702 case RFCOMM_EVENT_INCOMING_CONNECTION: 703 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 704 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 705 hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role); 706 if (!hfp_connection){ 707 log_info("hfp: no memory to accept incoming connection - decline"); 708 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 709 return; 710 } 711 if (hfp_connection->state != HFP_IDLE) { 712 log_error("hfp: incoming connection but state != HFP_IDLE"); 713 return; 714 } 715 716 hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 717 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 718 // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr)); 719 rfcomm_accept_connection(hfp_connection->rfcomm_cid); 720 break; 721 722 case RFCOMM_EVENT_CHANNEL_OPENED: 723 // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16) 724 725 rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 726 status = rfcomm_event_channel_opened_get_status(packet); 727 728 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr); 729 if (!hfp_connection || hfp_connection->state != HFP_W4_RFCOMM_CONNECTED) return; 730 731 if (status) { 732 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr); 733 remove_hfp_connection_context(hfp_connection); 734 } else { 735 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 736 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 737 bd_addr_copy(hfp_connection->remote_addr, event_addr); 738 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 739 // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu); 740 741 switch (hfp_connection->state){ 742 case HFP_W4_RFCOMM_CONNECTED: 743 hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 744 break; 745 case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN: 746 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 747 // printf("Shutting down RFCOMM.\n"); 748 break; 749 default: 750 break; 751 } 752 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 753 } 754 break; 755 756 case RFCOMM_EVENT_CHANNEL_CLOSED: 757 rfcomm_cid = little_endian_read_16(packet,2); 758 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 759 if (!hfp_connection) break; 760 if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){ 761 hfp_connection->state = HFP_IDLE; 762 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 763 break; 764 } 765 766 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 767 remove_hfp_connection_context(hfp_connection); 768 break; 769 770 default: 771 break; 772 } 773 } 774 // translates command string into hfp_command_t CMD 775 static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 776 int offset = isHandsFree ? 0 : 2; 777 778 if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){ 779 return HFP_CMD_LIST_CURRENT_CALLS; 780 } 781 782 if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){ 783 return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION; 784 } 785 786 if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){ 787 if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER; 788 return HFP_CMD_HF_REQUEST_PHONE_NUMBER; 789 } 790 791 if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){ 792 return HFP_CMD_TRANSMIT_DTMF_CODES; 793 } 794 795 if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){ 796 return HFP_CMD_SET_MICROPHONE_GAIN; 797 } 798 799 if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){ 800 return HFP_CMD_SET_SPEAKER_GAIN; 801 } 802 803 if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){ 804 if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 805 return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 806 } 807 808 if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){ 809 return HFP_CMD_TURN_OFF_EC_AND_NR; 810 } 811 812 if (strncmp(line_buffer, HFP_ANSWER_CALL, strlen(HFP_ANSWER_CALL)) == 0){ 813 return HFP_CMD_CALL_ANSWERED; 814 } 815 816 if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 817 return HFP_CMD_CALL_PHONE_NUMBER; 818 } 819 820 if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){ 821 return HFP_CMD_REDIAL_LAST_NUMBER; 822 } 823 824 if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){ 825 return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 826 } 827 828 if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){ 829 return HFP_CMD_HANG_UP_CALL; 830 } 831 832 if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){ 833 return HFP_CMD_ERROR; 834 } 835 836 if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){ 837 return HFP_CMD_RING; 838 } 839 840 if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){ 841 return HFP_CMD_OK; 842 } 843 844 if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){ 845 return HFP_CMD_SUPPORTED_FEATURES; 846 } 847 848 if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){ 849 return HFP_CMD_HF_INDICATOR_STATUS; 850 } 851 852 if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){ 853 if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){ 854 return HFP_CMD_RESPONSE_AND_HOLD_QUERY; 855 } 856 if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){ 857 return HFP_CMD_RESPONSE_AND_HOLD_COMMAND; 858 } 859 return HFP_CMD_RESPONSE_AND_HOLD_STATUS; 860 } 861 862 if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){ 863 if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){ 864 return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 865 } 866 867 if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){ 868 return HFP_CMD_RETRIEVE_AG_INDICATORS; 869 } 870 } 871 872 if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){ 873 return HFP_CMD_AVAILABLE_CODECS; 874 } 875 876 if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){ 877 return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE; 878 } 879 880 if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){ 881 if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION; 882 return HFP_CMD_ENABLE_CLIP; 883 } 884 885 if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){ 886 if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE; 887 return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION; 888 } 889 890 if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){ 891 892 if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 893 894 if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){ 895 return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES; 896 } 897 if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){ 898 return HFP_CMD_CALL_HOLD; 899 } 900 901 return HFP_CMD_UNKNOWN; 902 } 903 904 if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){ 905 if (isHandsFree) { 906 return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS; 907 } 908 if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){ 909 return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 910 } 911 if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){ 912 return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 913 } 914 return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 915 } 916 917 if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){ 918 return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE; 919 } 920 921 922 if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){ 923 if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){ 924 return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT; 925 } 926 return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME; 927 } 928 929 if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){ 930 return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS; 931 } 932 933 if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 934 return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR; 935 } 936 937 if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){ 938 return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR; 939 } 940 941 if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){ 942 return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 943 } 944 945 if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){ 946 if (isHandsFree){ 947 return HFP_CMD_AG_SUGGESTED_CODEC; 948 } else { 949 return HFP_CMD_HF_CONFIRMED_CODEC; 950 } 951 } 952 953 if (strncmp(line_buffer+offset, "AT+", 3) == 0){ 954 log_info("process unknown HF command %s \n", line_buffer); 955 return HFP_CMD_UNKNOWN; 956 } 957 958 if (strncmp(line_buffer+offset, "+", 1) == 0){ 959 log_info(" process unknown AG command %s \n", line_buffer); 960 return HFP_CMD_UNKNOWN; 961 } 962 963 if (strncmp(line_buffer+offset, "NOP", 3) == 0){ 964 return HFP_CMD_NONE; 965 } 966 967 return HFP_CMD_NONE; 968 } 969 970 static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 971 // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size); 972 // TODO: add limit 973 hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 974 hfp_connection->line_buffer[hfp_connection->line_size] = 0; 975 } 976 static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 977 return hfp_connection->line_size == 0; 978 } 979 980 static int hfp_parser_is_end_of_line(uint8_t byte){ 981 return byte == '\n' || byte == '\r'; 982 } 983 984 static int hfp_parser_is_end_of_header(uint8_t byte){ 985 return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?'; 986 } 987 988 static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){ 989 if (hfp_connection->keep_byte == 1) return 1; 990 991 int found_separator = byte == ',' || byte == '\n'|| byte == '\r'|| 992 byte == ')' || byte == '(' || byte == ':' || 993 byte == '-' || byte == '"' || byte == '?'|| byte == '='; 994 return found_separator; 995 } 996 997 static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){ 998 hfp_connection->line_size = 0; 999 if (hfp_parser_is_end_of_line(byte)){ 1000 hfp_connection->parser_item_index = 0; 1001 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 1002 return; 1003 } 1004 switch (hfp_connection->parser_state){ 1005 case HFP_PARSER_CMD_HEADER: 1006 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1007 if (hfp_connection->keep_byte == 1){ 1008 hfp_parser_store_byte(hfp_connection, byte); 1009 hfp_connection->keep_byte = 0; 1010 } 1011 break; 1012 case HFP_PARSER_CMD_SEQUENCE: 1013 switch (hfp_connection->command){ 1014 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1015 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1016 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1017 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1018 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1019 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1020 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1021 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1022 case HFP_CMD_HF_INDICATOR_STATUS: 1023 hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 1024 break; 1025 default: 1026 break; 1027 } 1028 break; 1029 case HFP_PARSER_SECOND_ITEM: 1030 hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 1031 break; 1032 case HFP_PARSER_THIRD_ITEM: 1033 if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 1034 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1035 break; 1036 } 1037 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 1038 break; 1039 } 1040 } 1041 1042 void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1043 // handle ATD<dial_string>; 1044 if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){ 1045 // check for end-of-line or ';' 1046 if (byte == ';' || hfp_parser_is_end_of_line(byte)){ 1047 hfp_connection->line_buffer[hfp_connection->line_size] = 0; 1048 hfp_connection->line_size = 0; 1049 hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER; 1050 } else { 1051 hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1052 } 1053 return; 1054 } 1055 1056 // TODO: handle space inside word 1057 if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return; 1058 1059 if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 1060 if (hfp_connection->line_size == 0){ 1061 hfp_connection->line_buffer[0] = 0; 1062 hfp_connection->ignore_value = 1; 1063 parse_sequence(hfp_connection); 1064 return; 1065 } 1066 } 1067 1068 if (!hfp_parser_found_separator(hfp_connection, byte)){ 1069 hfp_parser_store_byte(hfp_connection, byte); 1070 return; 1071 } 1072 1073 if (hfp_parser_is_end_of_line(byte)) { 1074 if (hfp_parser_is_buffer_empty(hfp_connection)){ 1075 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 1076 } 1077 } 1078 if (hfp_parser_is_buffer_empty(hfp_connection)) return; 1079 1080 switch (hfp_connection->parser_state){ 1081 case HFP_PARSER_CMD_HEADER: // header 1082 if (byte == '='){ 1083 hfp_connection->keep_byte = 1; 1084 hfp_parser_store_byte(hfp_connection, byte); 1085 return; 1086 } 1087 1088 if (byte == '?'){ 1089 hfp_connection->keep_byte = 0; 1090 hfp_parser_store_byte(hfp_connection, byte); 1091 return; 1092 } 1093 1094 if (byte == ','){ 1095 hfp_connection->resolve_byte = 1; 1096 } 1097 1098 // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 1099 if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){ 1100 // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte); 1101 char * line_buffer = (char *)hfp_connection->line_buffer; 1102 hfp_connection->command = parse_command(line_buffer, isHandsFree); 1103 1104 /* resolve command name according to hfp_connection */ 1105 if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1106 switch(hfp_connection->state){ 1107 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1108 hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS; 1109 break; 1110 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1111 hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS; 1112 break; 1113 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1114 hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE; 1115 break; 1116 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1117 hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1118 break; 1119 case HFP_W4_RETRIEVE_INDICATORS: 1120 hfp_connection->send_ag_indicators_segment = 0; 1121 hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1122 break; 1123 default: 1124 break; 1125 } 1126 } 1127 } 1128 break; 1129 1130 case HFP_PARSER_CMD_SEQUENCE: 1131 parse_sequence(hfp_connection); 1132 break; 1133 case HFP_PARSER_SECOND_ITEM: 1134 switch (hfp_connection->command){ 1135 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1136 log_info("format %s, ", hfp_connection->line_buffer); 1137 hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1138 break; 1139 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1140 log_info("format %s \n", hfp_connection->line_buffer); 1141 hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1142 break; 1143 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1144 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1145 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1146 hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1147 break; 1148 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1149 hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1150 log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1151 hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1152 break; 1153 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1154 hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1155 log_info("%s, ", hfp_connection->line_buffer); 1156 break; 1157 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1158 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1159 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1160 hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1161 break; 1162 default: 1163 break; 1164 } 1165 break; 1166 1167 case HFP_PARSER_THIRD_ITEM: 1168 switch (hfp_connection->command){ 1169 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1170 strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 1171 hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0; 1172 log_info("name %s\n", hfp_connection->line_buffer); 1173 break; 1174 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1175 hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 1176 hfp_connection->parser_item_index++; 1177 hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1178 log_info("%s)\n", hfp_connection->line_buffer); 1179 break; 1180 default: 1181 break; 1182 } 1183 break; 1184 } 1185 hfp_parser_next_state(hfp_connection, byte); 1186 1187 if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){ 1188 hfp_connection->resolve_byte = 0; 1189 hfp_connection->ignore_value = 1; 1190 parse_sequence(hfp_connection); 1191 hfp_connection->line_buffer[0] = 0; 1192 hfp_connection->line_size = 0; 1193 } 1194 } 1195 1196 static void parse_sequence(hfp_connection_t * hfp_connection){ 1197 int value; 1198 switch (hfp_connection->command){ 1199 case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 1200 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1201 int i; 1202 switch (hfp_connection->parser_item_index){ 1203 case 0: 1204 for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1205 if (hfp_connection->generic_status_indicators[i].uuid == value){ 1206 hfp_connection->parser_indicator_index = i; 1207 break; 1208 } 1209 } 1210 break; 1211 case 1: 1212 if (hfp_connection->parser_indicator_index <0) break; 1213 hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1214 log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1215 hfp_connection->parser_item_index, value); 1216 break; 1217 default: 1218 break; 1219 } 1220 hfp_connection->parser_item_index++; 1221 break; 1222 1223 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1224 switch(hfp_connection->parser_item_index){ 1225 case 0: 1226 strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1227 hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1228 break; 1229 case 1: 1230 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1231 hfp_connection->bnip_type = value; 1232 break; 1233 default: 1234 break; 1235 } 1236 hfp_connection->parser_item_index++; 1237 break; 1238 case HFP_CMD_LIST_CURRENT_CALLS: 1239 switch(hfp_connection->parser_item_index){ 1240 case 0: 1241 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1242 hfp_connection->clcc_idx = value; 1243 break; 1244 case 1: 1245 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1246 hfp_connection->clcc_dir = value; 1247 break; 1248 case 2: 1249 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1250 hfp_connection->clcc_status = value; 1251 break; 1252 case 3: 1253 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1254 hfp_connection->clcc_mpty = value; 1255 break; 1256 case 4: 1257 strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1258 hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1259 break; 1260 case 5: 1261 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1262 hfp_connection->bnip_type = value; 1263 break; 1264 default: 1265 break; 1266 } 1267 hfp_connection->parser_item_index++; 1268 break; 1269 case HFP_CMD_SET_MICROPHONE_GAIN: 1270 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1271 hfp_connection->microphone_gain = value; 1272 log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1273 break; 1274 case HFP_CMD_SET_SPEAKER_GAIN: 1275 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1276 hfp_connection->speaker_gain = value; 1277 log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1278 break; 1279 case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 1280 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1281 hfp_connection->ag_activate_voice_recognition = value; 1282 log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value); 1283 break; 1284 case HFP_CMD_TURN_OFF_EC_AND_NR: 1285 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1286 hfp_connection->ag_echo_and_noise_reduction = value; 1287 log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1288 break; 1289 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1290 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1291 hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1292 log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1293 break; 1294 case HFP_CMD_HF_CONFIRMED_CODEC: 1295 hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1296 log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 1297 break; 1298 case HFP_CMD_AG_SUGGESTED_CODEC: 1299 hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1300 log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 1301 break; 1302 case HFP_CMD_SUPPORTED_FEATURES: 1303 hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1304 log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 1305 break; 1306 case HFP_CMD_AVAILABLE_CODECS: 1307 log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1308 hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 1309 hfp_connection->parser_item_index++; 1310 hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 1311 break; 1312 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1313 strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE); 1314 hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0; 1315 hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1316 log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1317 break; 1318 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1319 log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 1320 hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 1321 hfp_connection->parser_item_index++; 1322 break; 1323 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1324 hfp_connection->parser_item_index++; 1325 if (hfp_connection->parser_item_index != 4) break; 1326 log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 1327 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1328 hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 1329 break; 1330 case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1331 log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1332 if (hfp_connection->line_size > 2 ) break; 1333 strncpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE); 1334 hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1335 hfp_connection->remote_call_services_nr++; 1336 break; 1337 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1338 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1339 log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 1340 hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 1341 hfp_connection->parser_item_index++; 1342 hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 1343 break; 1344 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1345 // HF parses inital AG gen. ind. state 1346 log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 1347 hfp_connection->parser_item_index = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1348 break; 1349 case HFP_CMD_HF_INDICATOR_STATUS: 1350 hfp_connection->parser_indicator_index = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1351 log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1352 break; 1353 case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1354 // AG parses new gen. ind. state 1355 if (hfp_connection->ignore_value){ 1356 hfp_connection->ignore_value = 0; 1357 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1358 hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1359 } 1360 else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1361 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1362 hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1363 } else { 1364 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1365 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1366 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1367 hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 1368 } 1369 hfp_connection->parser_item_index++; 1370 break; 1371 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1372 // indicators are indexed starting with 1 1373 hfp_connection->parser_item_index = btstack_atoi((char *)&hfp_connection->line_buffer[0]) - 1; 1374 log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 1375 break; 1376 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1377 hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1378 log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1379 break; 1380 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1381 if (hfp_connection->line_buffer[0] == '3'){ 1382 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 1383 break; 1384 } 1385 // TODO emit ERROR, wrong format 1386 log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 1387 break; 1388 case HFP_CMD_ERROR: 1389 break; 1390 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1391 hfp_connection->extended_audio_gateway_error = 1; 1392 hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1393 break; 1394 case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1395 hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1396 hfp_connection->ok_pending = 1; 1397 hfp_connection->extended_audio_gateway_error = 0; 1398 break; 1399 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1400 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1401 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1402 strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number)); 1403 hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0; 1404 break; 1405 default: 1406 break; 1407 } 1408 } 1409 1410 void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 1411 hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role); 1412 log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection); 1413 1414 if (!hfp_connection) { 1415 log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr)); 1416 return; 1417 } 1418 1419 switch (hfp_connection->state){ 1420 case HFP_W2_DISCONNECT_RFCOMM: 1421 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 1422 return; 1423 case HFP_W4_RFCOMM_DISCONNECTED: 1424 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART; 1425 return; 1426 case HFP_IDLE: 1427 memcpy(hfp_connection->remote_addr, bd_addr, 6); 1428 hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1429 connection_doing_sdp_query = hfp_connection; 1430 hfp_connection->service_uuid = service_uuid; 1431 sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid); 1432 break; 1433 default: 1434 break; 1435 } 1436 } 1437 1438 void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){ 1439 if (!hfp_connection) return; 1440 hfp_release_audio_connection(hfp_connection); 1441 1442 if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1443 hfp_connection->state = HFP_IDLE; 1444 return; 1445 } 1446 1447 if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1448 hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 1449 return; 1450 } 1451 1452 if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1453 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 1454 return; 1455 } 1456 1457 if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1458 hfp_connection->state = HFP_W2_DISCONNECT_SCO; 1459 return; 1460 } 1461 1462 return; 1463 } 1464 1465 void hfp_release_audio_connection(hfp_connection_t * hfp_connection){ 1466 if (!hfp_connection) return; 1467 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1468 hfp_connection->release_audio_connection = 1; 1469 } 1470 1471 static const struct link_settings { 1472 const uint16_t max_latency; 1473 const uint8_t retransmission_effort; 1474 const uint16_t packet_types; 1475 } hfp_link_settings [] = { 1476 { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0, HV1 1477 { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1, HV3 1478 { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1, EV3 1479 { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3 1480 { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3 1481 { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3 1482 { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1, EV3 1483 { 0x000d, 0x02, 0x0380 } // HFP_LINK_SETTINGS_T2, 2-EV3 1484 }; 1485 1486 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1487 // all packet types, fixed bandwidth 1488 int setting = hfp_connection->link_setting; 1489 log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1490 sco_establishment_active = hfp_connection; 1491 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1492 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1493 sco_voice_setting = 0x0043; // Transparent data 1494 } 1495 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1496 sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380 1497 } 1498 1499 void hfp_set_hf_callback(btstack_packet_handler_t callback){ 1500 hfp_hf_callback = callback; 1501 } 1502 1503 void hfp_set_ag_callback(btstack_packet_handler_t callback){ 1504 hfp_ag_callback = callback; 1505 } 1506 1507 void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1508 hfp_ag_rfcomm_packet_handler = handler; 1509 } 1510 1511 void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1512 hfp_hf_rfcomm_packet_handler = handler; 1513 } 1514 1515 void hfp_set_hf_run_for_context(void (*callback)(hfp_connection_t * hfp_connection)){ 1516 hfp_hf_run_for_context = callback; 1517 } 1518 1519 void hfp_init(void){ 1520 } 1521