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