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_hf.c" 39 40 // ***************************************************************************** 41 // 42 // HFP Hands-Free (HF) unit 43 // 44 // ***************************************************************************** 45 46 #include "btstack_config.h" 47 48 #include <stdint.h> 49 #include <string.h> 50 51 #include "bluetooth_sdp.h" 52 #include "btstack_debug.h" 53 #include "btstack_event.h" 54 #include "btstack_memory.h" 55 #include "btstack_run_loop.h" 56 #include "classic/core.h" 57 #include "classic/hfp.h" 58 #include "classic/hfp_hf.h" 59 #include "classic/sdp_client_rfcomm.h" 60 #include "classic/sdp_server.h" 61 #include "classic/sdp_util.h" 62 #include "hci.h" 63 #include "hci_cmd.h" 64 #include "hci_dump.h" 65 #include "l2cap.h" 66 67 // const 68 static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 69 70 // globals 71 static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration; 72 73 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 74 static uint8_t hfp_codecs_nr; 75 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 76 77 static uint8_t hfp_indicators_nr; 78 static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS]; 79 static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS]; 80 81 static uint8_t hfp_hf_speaker_gain; 82 static uint8_t hfp_hf_microphone_gain; 83 84 static btstack_packet_handler_t hfp_hf_callback; 85 86 static hfp_call_status_t hfp_call_status; 87 static hfp_callsetup_status_t hfp_callsetup_status; 88 static hfp_callheld_status_t hfp_callheld_status; 89 90 static char phone_number[25]; 91 92 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 93 int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 94 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 95 return hf && ag; 96 } 97 98 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 99 int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 100 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 101 return hf && ag; 102 } 103 104 105 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 106 int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 107 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 108 return hf && ag; 109 } 110 111 112 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 113 btstack_linked_list_iterator_t it; 114 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 115 while (btstack_linked_list_iterator_has_next(&it)){ 116 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 117 if (hfp_connection->acl_handle != handle) continue; 118 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 119 return hfp_connection; 120 } 121 return NULL; 122 } 123 124 /* emit functinos */ 125 126 static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){ 127 if (hfp_hf_callback == NULL) return; 128 uint8_t event[33]; 129 event[0] = HCI_EVENT_HFP_META; 130 event[1] = sizeof(event) - 2; 131 event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION; 132 little_endian_store_16(event, 3, hfp_connection->acl_handle); 133 event[5] = status; 134 event[6] = hfp_connection->bnip_type; 135 uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8); 136 strncpy((char*)&event[7], hfp_connection->bnip_number, size); 137 event[7 + size] = 0; 138 (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 139 } 140 141 static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){ 142 if (hfp_hf_callback == NULL) return; 143 uint8_t event[32]; 144 event[0] = HCI_EVENT_HFP_META; 145 event[1] = sizeof(event) - 2; 146 event[2] = event_subtype; 147 little_endian_store_16(event, 3, hfp_connection->acl_handle); 148 event[5] = hfp_connection->bnip_type; 149 uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7); 150 strncpy((char*)&event[6], hfp_connection->bnip_number, size); 151 event[6 + size] = 0; 152 (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 153 } 154 155 static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){ 156 if (hfp_hf_callback == NULL) return; 157 uint8_t event[38]; 158 int pos = 0; 159 event[pos++] = HCI_EVENT_HFP_META; 160 event[pos++] = sizeof(event) - 2; 161 event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 162 little_endian_store_16(event, pos, hfp_connection->acl_handle); 163 pos += 2; 164 event[pos++] = hfp_connection->clcc_idx; 165 event[pos++] = hfp_connection->clcc_dir; 166 event[pos++] = hfp_connection->clcc_status; 167 event[pos++] = hfp_connection->clcc_mode; 168 event[pos++] = hfp_connection->clcc_mpty; 169 event[pos++] = hfp_connection->bnip_type; 170 uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1); 171 strncpy((char*)&event[pos], hfp_connection->bnip_number, size); 172 pos += size; 173 event[pos++] = 0; 174 (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos); 175 } 176 177 178 static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){ 179 if (hfp_hf_callback == NULL) return; 180 uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1]; 181 int pos = 0; 182 event[pos++] = HCI_EVENT_HFP_META; 183 event[pos++] = sizeof(event) - 2; 184 event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 185 little_endian_store_16(event, pos, hfp_connection->acl_handle); 186 pos += 2; 187 event[pos++] = indicator->index; 188 event[pos++] = indicator->status; 189 event[pos++] = indicator->min_range; 190 event[pos++] = indicator->max_range; 191 event[pos++] = indicator->mandatory; 192 event[pos++] = indicator->enabled; 193 event[pos++] = indicator->status_changed; 194 strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE); 195 pos += HFP_MAX_INDICATOR_DESC_SIZE; 196 event[pos] = 0; 197 (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 198 } 199 200 static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){ 201 if (hfp_hf_callback == NULL) return; 202 uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 203 event[0] = HCI_EVENT_HFP_META; 204 event[1] = sizeof(event) - 2; 205 event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 206 little_endian_store_16(event, 3, hfp_connection->acl_handle); 207 event[5] = hfp_connection->network_operator.mode; 208 event[6] = hfp_connection->network_operator.format; 209 strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 210 event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 211 (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 212 } 213 214 /* send commands */ 215 216 static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 217 char buffer[20]; 218 snprintf(buffer, sizeof(buffer), "AT%s\r", cmd); 219 return send_str_over_rfcomm(cid, buffer); 220 } 221 222 static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 223 char buffer[20]; 224 snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark); 225 return send_str_over_rfcomm(cid, buffer); 226 } 227 228 static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 229 char buffer[40]; 230 snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value); 231 return send_str_over_rfcomm(cid, buffer); 232 } 233 234 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 235 char buffer[30]; 236 const int size = sizeof(buffer); 237 int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 238 offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr); 239 offset += snprintf(buffer+offset, size-offset, "\r"); 240 return send_str_over_rfcomm(cid, buffer); 241 } 242 243 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 244 char buffer[50]; 245 const int size = sizeof(buffer); 246 int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 247 offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 248 offset += snprintf(buffer+offset, size-offset, "\r"); 249 return send_str_over_rfcomm(cid, buffer); 250 } 251 252 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 253 char buffer[30]; 254 const int size = sizeof(buffer); 255 int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 256 offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr); 257 offset += snprintf(buffer+offset, size-offset, "\r"); 258 return send_str_over_rfcomm(cid, buffer); 259 } 260 261 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 262 char buffer[20]; 263 snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 264 return send_str_over_rfcomm(cid, buffer); 265 } 266 267 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 268 char buffer[40]; 269 snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number); 270 return send_str_over_rfcomm(cid, buffer); 271 } 272 273 static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 274 char buffer[40]; 275 snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id); 276 return send_str_over_rfcomm(cid, buffer); 277 } 278 279 static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 280 char buffer[40]; 281 snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 282 return send_str_over_rfcomm(cid, buffer); 283 } 284 285 static int hfp_hf_send_dtmf(uint16_t cid, char code){ 286 char buffer[20]; 287 snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code); 288 return send_str_over_rfcomm(cid, buffer); 289 } 290 291 static int hfp_hf_cmd_ata(uint16_t cid){ 292 return send_str_over_rfcomm(cid, (char *) "ATA\r"); 293 } 294 295 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 296 return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features); 297 } 298 299 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 300 return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 301 } 302 303 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 304 return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 305 } 306 307 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 308 return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 309 } 310 311 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 312 return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 313 } 314 315 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 316 return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 317 } 318 319 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 320 return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 321 } 322 323 static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 324 return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 325 } 326 327 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 328 return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 329 } 330 331 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 332 return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 333 } 334 335 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 336 return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 337 } 338 339 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 340 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 341 } 342 343 static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 344 return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate); 345 } 346 347 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 348 return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 349 } 350 351 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 352 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 353 } 354 355 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 356 return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 357 } 358 359 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 360 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 361 } 362 363 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 364 return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 365 } 366 367 static int hfp_hf_send_chup(uint16_t cid){ 368 return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 369 } 370 371 static int hfp_hf_send_binp(uint16_t cid){ 372 return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 373 } 374 375 static int hfp_hf_send_clcc(uint16_t cid){ 376 return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 377 } 378 379 /* state machines */ 380 381 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 382 if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 383 if (hfp_connection->ok_pending) return 0; 384 int done = 1; 385 386 switch (hfp_connection->state){ 387 case HFP_EXCHANGE_SUPPORTED_FEATURES: 388 hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 389 hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 390 hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 391 break; 392 case HFP_NOTIFY_ON_CODECS: 393 hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 394 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 395 break; 396 case HFP_RETRIEVE_INDICATORS: 397 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 398 hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 399 break; 400 case HFP_RETRIEVE_INDICATORS_STATUS: 401 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 402 hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 403 break; 404 case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 405 hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 406 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 407 break; 408 case HFP_RETRIEVE_CAN_HOLD_CALL: 409 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 410 hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 411 break; 412 case HFP_LIST_GENERIC_STATUS_INDICATORS: 413 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 414 hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 415 break; 416 case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 417 hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 418 hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 419 break; 420 case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 421 hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 422 hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 423 break; 424 default: 425 done = 0; 426 break; 427 } 428 return done; 429 } 430 431 432 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 433 if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 434 if (hfp_connection->ok_pending) return 0; 435 436 int done = 0; 437 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 438 hfp_connection->ok_pending = 1; 439 done = 1; 440 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 441 return done; 442 }; 443 if (hfp_connection->change_status_update_for_individual_ag_indicators){ 444 hfp_connection->ok_pending = 1; 445 done = 1; 446 hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 447 hfp_connection->ag_indicators_status_update_bitmap, 448 hfp_connection->ag_indicators_nr); 449 return done; 450 } 451 452 switch (hfp_connection->hf_query_operator_state){ 453 case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 454 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 455 hfp_connection->ok_pending = 1; 456 hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 457 return 1; 458 case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 459 hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 460 hfp_connection->ok_pending = 1; 461 hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 462 return 1; 463 default: 464 break; 465 } 466 467 if (hfp_connection->enable_extended_audio_gateway_error_report){ 468 hfp_connection->ok_pending = 1; 469 done = 1; 470 hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 471 return done; 472 } 473 474 return done; 475 } 476 477 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 478 479 if (hfp_connection->ok_pending) return 0; 480 481 if (hfp_connection->trigger_codec_exchange){ 482 hfp_connection->trigger_codec_exchange = 0; 483 484 hfp_connection->ok_pending = 1; 485 hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 486 return 1; 487 } 488 489 if (hfp_connection->hf_send_codec_confirm){ 490 hfp_connection->hf_send_codec_confirm = false; 491 492 hfp_connection->ok_pending = 1; 493 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 494 return 1; 495 } 496 497 if (hfp_connection->hf_send_supported_codecs){ 498 hfp_connection->hf_send_supported_codecs = false; 499 500 hfp_connection->ok_pending = 1; 501 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 502 return 1; 503 } 504 505 return 0; 506 } 507 508 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 509 if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 510 (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 511 512 if (hfp_connection->release_audio_connection){ 513 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 514 hfp_connection->release_audio_connection = 0; 515 gap_disconnect(hfp_connection->sco_handle); 516 return 1; 517 } 518 519 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 520 521 // run codecs exchange 522 int done = codecs_exchange_state_machine(hfp_connection); 523 if (done) return 1; 524 525 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 526 if (hfp_connection->establish_audio_connection){ 527 hfp_connection->state = HFP_W4_SCO_CONNECTED; 528 hfp_connection->establish_audio_connection = 0; 529 hfp_setup_synchronous_connection(hfp_connection); 530 return 1; 531 } 532 533 return 0; 534 } 535 536 537 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 538 539 if (hfp_connection->ok_pending) return 0; 540 541 if (hfp_connection->hf_answer_incoming_call){ 542 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 543 hfp_connection->hf_answer_incoming_call = 0; 544 return 1; 545 } 546 return 0; 547 } 548 549 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 550 551 btstack_assert(hfp_connection != NULL); 552 btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 553 554 // during SDP query, RFCOMM CID is not set 555 if (hfp_connection->rfcomm_cid == 0) return; 556 557 // assert command could be sent 558 if (hci_can_send_command_packet_now() == 0) return; 559 560 #ifdef ENABLE_CC256X_ASSISTED_HFP 561 // WBS Disassociate 562 if (hfp_connection->cc256x_send_wbs_disassociate){ 563 hfp_connection->cc256x_send_wbs_disassociate = false; 564 hci_send_cmd(&hci_ti_wbs_disassociate); 565 return; 566 } 567 // Write Codec Config 568 if (hfp_connection->cc256x_send_write_codec_config){ 569 hfp_connection->cc256x_send_write_codec_config = false; 570 hfp_cc256x_write_codec_config(hfp_connection); 571 return; 572 } 573 // WBS Associate 574 if (hfp_connection->cc256x_send_wbs_associate){ 575 hfp_connection->cc256x_send_wbs_associate = false; 576 hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 577 return; 578 } 579 #endif 580 #ifdef ENABLE_BCM_PCM_WBS 581 // Enable WBS 582 if (hfp_connection->bcm_send_enable_wbs){ 583 hfp_connection->bcm_send_enable_wbs = false; 584 hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 585 return; 586 } 587 // Write I2S/PCM params 588 if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 589 hfp_connection->bcm_send_write_i2spcm_interface_param = false; 590 hfp_bcm_write_i2spcm_interface_param(hfp_connection); 591 return; 592 } 593 // Disable WBS 594 if (hfp_connection->bcm_send_disable_wbs){ 595 hfp_connection->bcm_send_disable_wbs = false; 596 hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 597 return; 598 } 599 #endif 600 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 601 if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 602 hfp_finalize_connection_context(hfp_connection); 603 return; 604 } 605 #endif 606 607 if (hfp_connection->accept_sco){ 608 bool incoming_eSCO = hfp_connection->accept_sco == 2; 609 hfp_connection->accept_sco = 0; 610 // notify about codec selection if not done already 611 if (hfp_connection->negotiated_codec == 0){ 612 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 613 } 614 hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 615 return; 616 } 617 618 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 619 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 620 return; 621 } 622 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 623 if (!done){ 624 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 625 } 626 if (!done){ 627 done = hfp_hf_run_for_audio_connection(hfp_connection); 628 } 629 if (!done){ 630 done = call_setup_state_machine(hfp_connection); 631 } 632 633 // don't send a new command while ok still pending 634 if (hfp_connection->ok_pending) return; 635 636 if (hfp_connection->send_microphone_gain){ 637 hfp_connection->send_microphone_gain = 0; 638 hfp_connection->ok_pending = 1; 639 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 640 return; 641 } 642 643 if (hfp_connection->send_speaker_gain){ 644 hfp_connection->send_speaker_gain = 0; 645 hfp_connection->ok_pending = 1; 646 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 647 return; 648 } 649 650 if (hfp_connection->hf_deactivate_calling_line_notification){ 651 hfp_connection->hf_deactivate_calling_line_notification = 0; 652 hfp_connection->ok_pending = 1; 653 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 654 return; 655 } 656 657 if (hfp_connection->hf_activate_calling_line_notification){ 658 hfp_connection->hf_activate_calling_line_notification = 0; 659 hfp_connection->ok_pending = 1; 660 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 661 return; 662 } 663 664 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 665 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 666 hfp_connection->ok_pending = 1; 667 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 668 return; 669 } 670 671 if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 672 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 673 hfp_connection->ok_pending = 1; 674 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 675 return; 676 } 677 678 if (hfp_connection->hf_deactivate_voice_recognition_notification){ 679 hfp_connection->hf_deactivate_voice_recognition_notification = 0; 680 hfp_connection->ok_pending = 1; 681 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 682 return; 683 } 684 685 if (hfp_connection->hf_activate_voice_recognition_notification){ 686 hfp_connection->hf_activate_voice_recognition_notification = 0; 687 hfp_connection->ok_pending = 1; 688 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 689 return; 690 } 691 692 693 if (hfp_connection->hf_deactivate_call_waiting_notification){ 694 hfp_connection->hf_deactivate_call_waiting_notification = 0; 695 hfp_connection->ok_pending = 1; 696 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 697 return; 698 } 699 700 if (hfp_connection->hf_activate_call_waiting_notification){ 701 hfp_connection->hf_activate_call_waiting_notification = 0; 702 hfp_connection->ok_pending = 1; 703 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 704 return; 705 } 706 707 if (hfp_connection->hf_initiate_outgoing_call){ 708 hfp_connection->hf_initiate_outgoing_call = 0; 709 hfp_connection->ok_pending = 1; 710 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 711 return; 712 } 713 714 if (hfp_connection->hf_initiate_memory_dialing){ 715 hfp_connection->hf_initiate_memory_dialing = 0; 716 hfp_connection->ok_pending = 1; 717 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 718 return; 719 } 720 721 if (hfp_connection->hf_initiate_redial_last_number){ 722 hfp_connection->hf_initiate_redial_last_number = 0; 723 hfp_connection->ok_pending = 1; 724 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 725 return; 726 } 727 728 if (hfp_connection->hf_send_chup){ 729 hfp_connection->hf_send_chup = 0; 730 hfp_connection->ok_pending = 1; 731 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 732 return; 733 } 734 735 if (hfp_connection->hf_send_chld_0){ 736 hfp_connection->hf_send_chld_0 = 0; 737 hfp_connection->ok_pending = 1; 738 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 739 return; 740 } 741 742 if (hfp_connection->hf_send_chld_1){ 743 hfp_connection->hf_send_chld_1 = 0; 744 hfp_connection->ok_pending = 1; 745 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 746 return; 747 } 748 749 if (hfp_connection->hf_send_chld_2){ 750 hfp_connection->hf_send_chld_2 = 0; 751 hfp_connection->ok_pending = 1; 752 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 753 return; 754 } 755 756 if (hfp_connection->hf_send_chld_3){ 757 hfp_connection->hf_send_chld_3 = 0; 758 hfp_connection->ok_pending = 1; 759 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 760 return; 761 } 762 763 if (hfp_connection->hf_send_chld_4){ 764 hfp_connection->hf_send_chld_4 = 0; 765 hfp_connection->ok_pending = 1; 766 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 767 return; 768 } 769 770 if (hfp_connection->hf_send_chld_x){ 771 hfp_connection->hf_send_chld_x = 0; 772 hfp_connection->ok_pending = 1; 773 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 774 return; 775 } 776 777 if (hfp_connection->hf_send_dtmf_code){ 778 char code = hfp_connection->hf_send_dtmf_code; 779 hfp_connection->hf_send_dtmf_code = 0; 780 hfp_connection->ok_pending = 1; 781 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 782 return; 783 } 784 785 if (hfp_connection->hf_send_binp){ 786 hfp_connection->hf_send_binp = 0; 787 hfp_connection->ok_pending = 1; 788 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 789 return; 790 } 791 792 if (hfp_connection->hf_send_clcc){ 793 hfp_connection->hf_send_clcc = 0; 794 hfp_connection->ok_pending = 1; 795 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 796 return; 797 } 798 799 if (hfp_connection->hf_send_rrh){ 800 hfp_connection->hf_send_rrh = 0; 801 char buffer[20]; 802 switch (hfp_connection->hf_send_rrh_command){ 803 case '?': 804 snprintf(buffer, sizeof(buffer), "AT%s?\r", 805 HFP_RESPONSE_AND_HOLD); 806 buffer[sizeof(buffer) - 1] = 0; 807 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 808 return; 809 case '0': 810 case '1': 811 case '2': 812 snprintf(buffer, sizeof(buffer), "AT%s=%c\r", 813 HFP_RESPONSE_AND_HOLD, 814 hfp_connection->hf_send_rrh_command); 815 buffer[sizeof(buffer) - 1] = 0; 816 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 817 return; 818 default: 819 break; 820 } 821 return; 822 } 823 824 if (hfp_connection->hf_send_cnum){ 825 hfp_connection->hf_send_cnum = 0; 826 char buffer[20]; 827 snprintf(buffer, sizeof(buffer), "AT%s\r", 828 HFP_SUBSCRIBER_NUMBER_INFORMATION); 829 buffer[sizeof(buffer) - 1] = 0; 830 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 831 return; 832 } 833 834 // update HF indicators 835 if (hfp_connection->generic_status_update_bitmap){ 836 int i; 837 for (i=0;i<hfp_indicators_nr;i++){ 838 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 839 if (hfp_connection->generic_status_indicators[i].state){ 840 hfp_connection->ok_pending = 1; 841 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 842 char buffer[30]; 843 snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r", 844 HFP_TRANSFER_HF_INDICATOR_STATUS, 845 hfp_indicators[i], 846 (unsigned int)hfp_indicators_value[i]); 847 buffer[sizeof(buffer) - 1] = 0; 848 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 849 } else { 850 log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 851 } 852 return; 853 } 854 } 855 } 856 857 if (done) return; 858 // deal with disconnect 859 switch (hfp_connection->state){ 860 case HFP_W2_DISCONNECT_RFCOMM: 861 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 862 rfcomm_disconnect(hfp_connection->rfcomm_cid); 863 break; 864 865 default: 866 break; 867 } 868 } 869 870 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 871 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 872 873 hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 874 875 // restore volume settings 876 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 877 hfp_connection->send_speaker_gain = 1; 878 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 879 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 880 hfp_connection->send_microphone_gain = 1; 881 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 882 // enable all indicators 883 int i; 884 for (i=0;i<hfp_indicators_nr;i++){ 885 hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 886 hfp_connection->generic_status_indicators[i].state = 1; 887 } 888 } 889 890 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 891 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 892 // Codec supported, confirm 893 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 894 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 895 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 896 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 897 898 hfp_connection->hf_send_codec_confirm = true; 899 } else { 900 // Codec not supported, send supported codecs 901 hfp_connection->codec_confirmed = 0; 902 hfp_connection->suggested_codec = 0; 903 hfp_connection->negotiated_codec = 0; 904 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 905 906 hfp_connection->hf_send_supported_codecs = true; 907 } 908 } 909 910 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 911 hfp_connection->ok_pending = 0; 912 switch (hfp_connection->state){ 913 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 914 if (has_codec_negotiation_feature(hfp_connection)){ 915 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 916 break; 917 } 918 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 919 break; 920 921 case HFP_W4_NOTIFY_ON_CODECS: 922 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 923 break; 924 925 case HFP_W4_RETRIEVE_INDICATORS: 926 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 927 break; 928 929 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 930 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 931 break; 932 933 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 934 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 935 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 936 break; 937 } 938 if (has_hf_indicators_feature(hfp_connection)){ 939 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 940 break; 941 } 942 hfp_ag_slc_established(hfp_connection); 943 break; 944 945 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 946 if (has_hf_indicators_feature(hfp_connection)){ 947 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 948 break; 949 } 950 hfp_ag_slc_established(hfp_connection); 951 break; 952 953 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 954 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 955 break; 956 957 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 958 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 959 break; 960 961 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 962 hfp_ag_slc_established(hfp_connection); 963 break; 964 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 965 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 966 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 967 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 968 break; 969 } 970 971 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 972 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 973 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 974 break; 975 } 976 977 switch (hfp_connection->hf_query_operator_state){ 978 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 979 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 980 break; 981 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 982 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 983 hfp_emit_network_operator_event(hfp_connection); 984 break; 985 default: 986 break; 987 } 988 989 if (hfp_connection->enable_extended_audio_gateway_error_report){ 990 hfp_connection->enable_extended_audio_gateway_error_report = 0; 991 break; 992 } 993 994 switch (hfp_connection->codecs_state){ 995 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 996 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 997 break; 998 case HFP_CODECS_HF_CONFIRMED_CODEC: 999 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1000 break; 1001 default: 1002 break; 1003 } 1004 break; 1005 default: 1006 break; 1007 } 1008 1009 // done 1010 hfp_connection->command = HFP_CMD_NONE; 1011 } 1012 1013 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 1014 uint16_t i; 1015 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1016 if (hfp_connection->ag_indicators[i].status_changed) { 1017 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1018 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1019 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1020 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1021 // avoid set but not used warning 1022 (void) hfp_callheld_status; 1023 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1024 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1025 } 1026 hfp_connection->ag_indicators[i].status_changed = 0; 1027 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1028 break; 1029 } 1030 } 1031 } 1032 1033 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1034 int value; 1035 int i; 1036 switch (hfp_connection->command){ 1037 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1038 hfp_connection->command = HFP_CMD_NONE; 1039 hfp_hf_emit_subscriber_information(hfp_connection, 0); 1040 break; 1041 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1042 hfp_connection->command = HFP_CMD_NONE; 1043 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1044 break; 1045 case HFP_CMD_LIST_CURRENT_CALLS: 1046 hfp_connection->command = HFP_CMD_NONE; 1047 hfp_hf_emit_enhanced_call_status(hfp_connection); 1048 break; 1049 case HFP_CMD_SET_SPEAKER_GAIN: 1050 hfp_connection->command = HFP_CMD_NONE; 1051 value = btstack_atoi((char*)hfp_connection->line_buffer); 1052 hfp_hf_speaker_gain = value; 1053 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1054 break; 1055 case HFP_CMD_SET_MICROPHONE_GAIN: 1056 hfp_connection->command = HFP_CMD_NONE; 1057 value = btstack_atoi((char*)hfp_connection->line_buffer); 1058 hfp_hf_microphone_gain = value; 1059 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1060 break; 1061 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1062 hfp_connection->command = HFP_CMD_NONE; 1063 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1064 break; 1065 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1066 hfp_connection->command = HFP_CMD_NONE; 1067 hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1068 break; 1069 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1070 hfp_connection->command = HFP_CMD_NONE; 1071 hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1072 break; 1073 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1074 hfp_connection->command = HFP_CMD_NONE; 1075 hfp_connection->ok_pending = 0; 1076 hfp_connection->extended_audio_gateway_error = 0; 1077 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1078 break; 1079 case HFP_CMD_ERROR: 1080 hfp_connection->command = HFP_CMD_NONE; 1081 hfp_connection->ok_pending = 0; 1082 hfp_reset_context_flags(hfp_connection); 1083 switch (hfp_connection->state){ 1084 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1085 switch (hfp_connection->codecs_state){ 1086 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1087 hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 1088 return; 1089 default: 1090 break; 1091 } 1092 default: 1093 break; 1094 } 1095 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1096 break; 1097 case HFP_CMD_OK: 1098 hfp_hf_switch_on_ok(hfp_connection); 1099 break; 1100 case HFP_CMD_RING: 1101 hfp_connection->command = HFP_CMD_NONE; 1102 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1103 break; 1104 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1105 hfp_connection->command = HFP_CMD_NONE; 1106 hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1107 break; 1108 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1109 hfp_connection->command = HFP_CMD_NONE; 1110 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1111 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1112 } 1113 break; 1114 case HFP_CMD_AG_SUGGESTED_CODEC: 1115 hfp_connection->command = HFP_CMD_NONE; 1116 hfp_hf_handle_suggested_codec(hfp_connection); 1117 break; 1118 default: 1119 break; 1120 } 1121 } 1122 1123 static int hfp_parser_is_end_of_line(uint8_t byte){ 1124 return (byte == '\n') || (byte == '\r'); 1125 } 1126 1127 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1128 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1129 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1130 if (size < 1) return; 1131 1132 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1133 if (!hfp_connection) return; 1134 1135 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1136 #ifdef ENABLE_HFP_AT_MESSAGES 1137 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1138 #endif 1139 1140 // process messages byte-wise 1141 int pos; 1142 for (pos = 0; pos < size; pos++){ 1143 hfp_parse(hfp_connection, packet[pos], 1); 1144 1145 // parse until end of line "\r" or "\n" 1146 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1147 1148 hfp_hf_handle_rfcomm_command(hfp_connection); 1149 } 1150 hfp_hf_run_for_context(hfp_connection); 1151 } 1152 1153 static void hfp_hf_run(void){ 1154 btstack_linked_list_iterator_t it; 1155 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1156 while (btstack_linked_list_iterator_has_next(&it)){ 1157 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1158 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1159 hfp_hf_run_for_context(hfp_connection); 1160 } 1161 } 1162 1163 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1164 switch (packet_type){ 1165 case RFCOMM_DATA_PACKET: 1166 hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 1167 break; 1168 case HCI_EVENT_PACKET: 1169 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1170 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1171 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1172 return; 1173 } 1174 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1175 break; 1176 default: 1177 break; 1178 } 1179 hfp_hf_run(); 1180 } 1181 1182 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1183 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1184 hfp_hf_run(); 1185 } 1186 1187 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1188 hfp_init(); 1189 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1190 hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1191 hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1192 hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1193 hfp_codecs_nr = 0; 1194 hfp_hf_speaker_gain = 9; 1195 hfp_hf_microphone_gain = 9; 1196 hfp_indicators_nr = 0; 1197 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1198 1199 hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 1200 hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 1201 1202 rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1203 1204 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1205 hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1206 } 1207 1208 void hfp_hf_deinit(void){ 1209 hfp_deinit(); 1210 (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1211 (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t)); 1212 (void) memset(phone_number, 0, sizeof(phone_number)); 1213 } 1214 1215 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 1216 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1217 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1218 return; 1219 } 1220 1221 hfp_codecs_nr = codecs_nr; 1222 int i; 1223 for (i=0; i<codecs_nr; i++){ 1224 hfp_codecs[i] = codecs[i]; 1225 } 1226 } 1227 1228 void hfp_hf_init_supported_features(uint32_t supported_features){ 1229 hfp_supported_features = supported_features; 1230 } 1231 1232 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1233 hfp_indicators_nr = indicators_nr; 1234 int i; 1235 for (i = 0; i < hfp_indicators_nr ; i++){ 1236 hfp_indicators[i] = indicators[i]; 1237 } 1238 } 1239 1240 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1241 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1242 } 1243 1244 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1245 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1246 if (!hfp_connection) { 1247 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1248 return; 1249 } 1250 hfp_release_service_level_connection(hfp_connection); 1251 hfp_hf_run_for_context(hfp_connection); 1252 } 1253 1254 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1255 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1256 if (!hfp_connection) { 1257 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1258 return; 1259 } 1260 hfp_connection->enable_status_update_for_ag_indicators = enable; 1261 hfp_hf_run_for_context(hfp_connection); 1262 } 1263 1264 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1265 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1266 } 1267 1268 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1269 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1270 } 1271 1272 // TODO: returned ERROR - wrong format 1273 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1274 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1275 if (!hfp_connection) { 1276 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1277 return; 1278 } 1279 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1280 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1281 hfp_hf_run_for_context(hfp_connection); 1282 } 1283 1284 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1285 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1286 if (!hfp_connection) { 1287 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1288 return; 1289 } 1290 switch (hfp_connection->hf_query_operator_state){ 1291 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1292 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1293 break; 1294 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1295 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1296 break; 1297 default: 1298 break; 1299 } 1300 hfp_hf_run_for_context(hfp_connection); 1301 } 1302 1303 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1304 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1305 if (!hfp_connection) { 1306 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1307 return; 1308 } 1309 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1310 hfp_hf_run_for_context(hfp_connection); 1311 } 1312 1313 1314 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1315 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1316 } 1317 1318 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1319 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1320 } 1321 1322 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1323 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 1324 } 1325 1326 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1327 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1328 if (!hfp_connection) { 1329 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1330 return; 1331 } 1332 hfp_connection->establish_audio_connection = 0; 1333 1334 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1335 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1336 1337 hfp_connection->trigger_codec_exchange = 0; 1338 if (!has_codec_negotiation_feature(hfp_connection)){ 1339 log_info("no codec negotiation feature, using NBS"); 1340 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1341 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1342 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1343 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1344 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1345 hfp_connection->establish_audio_connection = 1; 1346 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1347 } else { 1348 switch (hfp_connection->codecs_state){ 1349 case HFP_CODECS_W4_AG_COMMON_CODEC: 1350 break; 1351 default: 1352 hfp_connection->codec_confirmed = 0; 1353 hfp_connection->suggested_codec = 0; 1354 hfp_connection->negotiated_codec = 0; 1355 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 1356 hfp_connection->trigger_codec_exchange = 1; 1357 break; 1358 } 1359 } 1360 1361 hfp_hf_run_for_context(hfp_connection); 1362 } 1363 1364 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1365 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1366 if (!hfp_connection) { 1367 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1368 return; 1369 } 1370 hfp_release_audio_connection(hfp_connection); 1371 hfp_hf_run_for_context(hfp_connection); 1372 } 1373 1374 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1375 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1376 if (!hfp_connection) { 1377 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1378 return; 1379 } 1380 1381 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1382 hfp_connection->hf_answer_incoming_call = 1; 1383 hfp_hf_run_for_context(hfp_connection); 1384 } else { 1385 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1386 } 1387 } 1388 1389 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1390 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1391 if (!hfp_connection) { 1392 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1393 return; 1394 } 1395 hfp_connection->hf_send_chup = 1; 1396 hfp_hf_run_for_context(hfp_connection); 1397 } 1398 1399 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1400 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1401 if (!hfp_connection) { 1402 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1403 return; 1404 } 1405 1406 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1407 hfp_connection->hf_send_chup = 1; 1408 hfp_hf_run_for_context(hfp_connection); 1409 } 1410 } 1411 1412 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1413 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1414 if (!hfp_connection) { 1415 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1416 return; 1417 } 1418 1419 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1420 hfp_connection->hf_send_chld_0 = 1; 1421 hfp_hf_run_for_context(hfp_connection); 1422 } 1423 } 1424 1425 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1426 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1427 if (!hfp_connection) { 1428 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1429 return; 1430 } 1431 1432 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1433 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1434 hfp_connection->hf_send_chld_1 = 1; 1435 hfp_hf_run_for_context(hfp_connection); 1436 } 1437 } 1438 1439 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1440 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1441 if (!hfp_connection) { 1442 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1443 return; 1444 } 1445 1446 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1447 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1448 hfp_connection->hf_send_chld_2 = 1; 1449 hfp_hf_run_for_context(hfp_connection); 1450 } 1451 } 1452 1453 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1454 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1455 if (!hfp_connection) { 1456 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1457 return; 1458 } 1459 1460 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1461 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1462 hfp_connection->hf_send_chld_3 = 1; 1463 hfp_hf_run_for_context(hfp_connection); 1464 } 1465 } 1466 1467 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1468 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1469 if (!hfp_connection) { 1470 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1471 return; 1472 } 1473 1474 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1475 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1476 hfp_connection->hf_send_chld_4 = 1; 1477 hfp_hf_run_for_context(hfp_connection); 1478 } 1479 } 1480 1481 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1482 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1483 if (!hfp_connection) { 1484 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1485 return; 1486 } 1487 1488 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1489 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1490 hfp_connection->hf_send_chld_x = 1; 1491 hfp_connection->hf_send_chld_x_index = 10 + index; 1492 hfp_hf_run_for_context(hfp_connection); 1493 } 1494 } 1495 1496 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1497 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1498 if (!hfp_connection) { 1499 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1500 return; 1501 } 1502 1503 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1504 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1505 hfp_connection->hf_send_chld_x = 1; 1506 hfp_connection->hf_send_chld_x_index = 20 + index; 1507 hfp_hf_run_for_context(hfp_connection); 1508 } 1509 } 1510 1511 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1512 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1513 if (!hfp_connection) { 1514 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1515 return; 1516 } 1517 1518 hfp_connection->hf_initiate_outgoing_call = 1; 1519 snprintf(phone_number, sizeof(phone_number), "%s", number); 1520 hfp_hf_run_for_context(hfp_connection); 1521 } 1522 1523 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1524 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1525 if (!hfp_connection) { 1526 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1527 return; 1528 } 1529 1530 hfp_connection->hf_initiate_memory_dialing = 1; 1531 hfp_connection->memory_id = memory_id; 1532 1533 hfp_hf_run_for_context(hfp_connection); 1534 } 1535 1536 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1537 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1538 if (!hfp_connection) { 1539 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1540 return; 1541 } 1542 1543 hfp_connection->hf_initiate_redial_last_number = 1; 1544 hfp_hf_run_for_context(hfp_connection); 1545 } 1546 1547 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1548 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1549 if (!hfp_connection) { 1550 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1551 return; 1552 } 1553 1554 hfp_connection->hf_activate_call_waiting_notification = 1; 1555 hfp_hf_run_for_context(hfp_connection); 1556 } 1557 1558 1559 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1560 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1561 if (!hfp_connection) { 1562 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1563 return; 1564 } 1565 1566 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1567 hfp_hf_run_for_context(hfp_connection); 1568 } 1569 1570 1571 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1572 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1573 if (!hfp_connection) { 1574 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1575 return; 1576 } 1577 1578 hfp_connection->hf_activate_calling_line_notification = 1; 1579 hfp_hf_run_for_context(hfp_connection); 1580 } 1581 1582 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1583 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1584 if (!hfp_connection) { 1585 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1586 return; 1587 } 1588 1589 hfp_connection->hf_deactivate_calling_line_notification = 1; 1590 hfp_hf_run_for_context(hfp_connection); 1591 } 1592 1593 1594 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1595 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1596 if (!hfp_connection) { 1597 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1598 return; 1599 } 1600 1601 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1602 hfp_hf_run_for_context(hfp_connection); 1603 } 1604 1605 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1606 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1607 if (!hfp_connection) { 1608 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1609 return; 1610 } 1611 1612 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1613 hfp_hf_run_for_context(hfp_connection); 1614 } 1615 1616 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1617 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1618 if (!hfp_connection) { 1619 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1620 return; 1621 } 1622 1623 hfp_connection->hf_activate_voice_recognition_notification = 1; 1624 hfp_hf_run_for_context(hfp_connection); 1625 } 1626 1627 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1628 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1629 if (!hfp_connection) { 1630 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1631 return; 1632 } 1633 1634 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1635 hfp_hf_run_for_context(hfp_connection); 1636 } 1637 1638 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1639 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1640 if (!hfp_connection) { 1641 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1642 return; 1643 } 1644 1645 if (hfp_connection->microphone_gain == gain) return; 1646 if ((gain < 0) || (gain > 15)){ 1647 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1648 return; 1649 } 1650 hfp_connection->microphone_gain = gain; 1651 hfp_connection->send_microphone_gain = 1; 1652 hfp_hf_run_for_context(hfp_connection); 1653 } 1654 1655 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1656 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1657 if (!hfp_connection) { 1658 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1659 return; 1660 } 1661 1662 if (hfp_connection->speaker_gain == gain) return; 1663 if ((gain < 0) || (gain > 15)){ 1664 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1665 return; 1666 } 1667 hfp_connection->speaker_gain = gain; 1668 hfp_connection->send_speaker_gain = 1; 1669 hfp_hf_run_for_context(hfp_connection); 1670 } 1671 1672 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1673 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1674 if (!hfp_connection) { 1675 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1676 return; 1677 } 1678 1679 hfp_connection->hf_send_dtmf_code = code; 1680 hfp_hf_run_for_context(hfp_connection); 1681 } 1682 1683 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1684 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1685 if (!hfp_connection) { 1686 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1687 return; 1688 } 1689 hfp_connection->hf_send_binp = 1; 1690 hfp_hf_run_for_context(hfp_connection); 1691 } 1692 1693 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1694 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1695 if (!hfp_connection) { 1696 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1697 return; 1698 } 1699 hfp_connection->hf_send_clcc = 1; 1700 hfp_hf_run_for_context(hfp_connection); 1701 } 1702 1703 1704 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1705 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1706 if (!hfp_connection) { 1707 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1708 return; 1709 } 1710 hfp_connection->hf_send_rrh = 1; 1711 hfp_connection->hf_send_rrh_command = '?'; 1712 hfp_hf_run_for_context(hfp_connection); 1713 } 1714 1715 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1716 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1717 if (!hfp_connection) { 1718 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1719 return; 1720 } 1721 hfp_connection->hf_send_rrh = 1; 1722 hfp_connection->hf_send_rrh_command = '0'; 1723 hfp_hf_run_for_context(hfp_connection); 1724 } 1725 1726 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1727 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1728 if (!hfp_connection) { 1729 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1730 return; 1731 } 1732 hfp_connection->hf_send_rrh = 1; 1733 hfp_connection->hf_send_rrh_command = '1'; 1734 hfp_hf_run_for_context(hfp_connection); 1735 } 1736 1737 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1738 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1739 if (!hfp_connection) { 1740 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1741 return; 1742 } 1743 hfp_connection->hf_send_rrh = 1; 1744 hfp_connection->hf_send_rrh_command = '2'; 1745 hfp_hf_run_for_context(hfp_connection); 1746 } 1747 1748 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1749 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1750 if (!hfp_connection) { 1751 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1752 return; 1753 } 1754 hfp_connection->hf_send_cnum = 1; 1755 hfp_hf_run_for_context(hfp_connection); 1756 } 1757 1758 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1759 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1760 if (!hfp_connection) { 1761 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1762 return; 1763 } 1764 // find index for assigned number 1765 int i; 1766 for (i = 0; i < hfp_indicators_nr ; i++){ 1767 if (hfp_indicators[i] == assigned_number){ 1768 // set value 1769 hfp_indicators_value[i] = value; 1770 // mark for update 1771 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1772 hfp_connection->generic_status_update_bitmap |= (1<<i); 1773 // send update 1774 hfp_hf_run_for_context(hfp_connection); 1775 } 1776 return; 1777 } 1778 } 1779 } 1780 1781 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1782 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1783 if (!hfp_connection) { 1784 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1785 return 0; 1786 } 1787 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1788 } 1789 1790 void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){ 1791 if (!name){ 1792 name = default_hfp_hf_service_name; 1793 } 1794 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 1795 1796 // Construct SupportedFeatures for SDP bitmap: 1797 // 1798 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1799 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1800 // 1801 // Wide band speech (bit 5) requires Codec negotiation 1802 // 1803 uint16_t sdp_features = supported_features & 0x1f; 1804 if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 1805 sdp_features |= 1 << 5; 1806 } 1807 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1808 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 1809 } 1810 1811 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 1812 if (callback == NULL){ 1813 log_error("hfp_hf_register_packet_handler called with NULL callback"); 1814 return; 1815 } 1816 hfp_hf_callback = callback; 1817 hfp_set_hf_callback(callback); 1818 } 1819