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 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1119 hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE)); 1120 default: 1121 break; 1122 } 1123 } 1124 1125 static int hfp_parser_is_end_of_line(uint8_t byte){ 1126 return (byte == '\n') || (byte == '\r'); 1127 } 1128 1129 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1130 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1131 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1132 if (size < 1) return; 1133 1134 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1135 if (!hfp_connection) return; 1136 1137 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1138 #ifdef ENABLE_HFP_AT_MESSAGES 1139 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1140 #endif 1141 1142 // process messages byte-wise 1143 int pos; 1144 for (pos = 0; pos < size; pos++){ 1145 hfp_parse(hfp_connection, packet[pos], 1); 1146 1147 // parse until end of line "\r" or "\n" 1148 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1149 1150 hfp_hf_handle_rfcomm_command(hfp_connection); 1151 } 1152 hfp_hf_run_for_context(hfp_connection); 1153 } 1154 1155 static void hfp_hf_run(void){ 1156 btstack_linked_list_iterator_t it; 1157 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1158 while (btstack_linked_list_iterator_has_next(&it)){ 1159 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1160 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1161 hfp_hf_run_for_context(hfp_connection); 1162 } 1163 } 1164 1165 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1166 switch (packet_type){ 1167 case RFCOMM_DATA_PACKET: 1168 hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 1169 break; 1170 case HCI_EVENT_PACKET: 1171 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1172 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1173 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1174 return; 1175 } 1176 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1177 break; 1178 default: 1179 break; 1180 } 1181 hfp_hf_run(); 1182 } 1183 1184 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1185 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1186 hfp_hf_run(); 1187 } 1188 1189 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1190 hfp_init(); 1191 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1192 hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1193 hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1194 hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1195 hfp_codecs_nr = 0; 1196 hfp_hf_speaker_gain = 9; 1197 hfp_hf_microphone_gain = 9; 1198 hfp_indicators_nr = 0; 1199 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1200 1201 hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 1202 hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 1203 1204 rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1205 1206 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1207 hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1208 } 1209 1210 void hfp_hf_deinit(void){ 1211 hfp_deinit(); 1212 (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1213 (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t)); 1214 (void) memset(phone_number, 0, sizeof(phone_number)); 1215 } 1216 1217 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 1218 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1219 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1220 return; 1221 } 1222 1223 hfp_codecs_nr = codecs_nr; 1224 int i; 1225 for (i=0; i<codecs_nr; i++){ 1226 hfp_codecs[i] = codecs[i]; 1227 } 1228 } 1229 1230 void hfp_hf_init_supported_features(uint32_t supported_features){ 1231 hfp_supported_features = supported_features; 1232 } 1233 1234 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1235 hfp_indicators_nr = indicators_nr; 1236 int i; 1237 for (i = 0; i < hfp_indicators_nr ; i++){ 1238 hfp_indicators[i] = indicators[i]; 1239 } 1240 } 1241 1242 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1243 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1244 } 1245 1246 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1247 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1248 if (!hfp_connection) { 1249 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1250 return; 1251 } 1252 hfp_release_service_level_connection(hfp_connection); 1253 hfp_hf_run_for_context(hfp_connection); 1254 } 1255 1256 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1257 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1258 if (!hfp_connection) { 1259 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1260 return; 1261 } 1262 hfp_connection->enable_status_update_for_ag_indicators = enable; 1263 hfp_hf_run_for_context(hfp_connection); 1264 } 1265 1266 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1267 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1268 } 1269 1270 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1271 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1272 } 1273 1274 // TODO: returned ERROR - wrong format 1275 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1276 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1277 if (!hfp_connection) { 1278 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1279 return; 1280 } 1281 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1282 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1283 hfp_hf_run_for_context(hfp_connection); 1284 } 1285 1286 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1287 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1288 if (!hfp_connection) { 1289 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1290 return; 1291 } 1292 switch (hfp_connection->hf_query_operator_state){ 1293 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1294 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1295 break; 1296 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1297 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1298 break; 1299 default: 1300 break; 1301 } 1302 hfp_hf_run_for_context(hfp_connection); 1303 } 1304 1305 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1306 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1307 if (!hfp_connection) { 1308 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1309 return; 1310 } 1311 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1312 hfp_hf_run_for_context(hfp_connection); 1313 } 1314 1315 1316 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1317 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1318 } 1319 1320 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1321 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1322 } 1323 1324 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1325 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 1326 } 1327 1328 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1329 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1330 if (!hfp_connection) { 1331 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1332 return; 1333 } 1334 1335 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1336 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1337 1338 if (has_codec_negotiation_feature(hfp_connection)) { 1339 switch (hfp_connection->codecs_state) { 1340 case HFP_CODECS_W4_AG_COMMON_CODEC: 1341 break; 1342 case HFP_CODECS_EXCHANGED: 1343 hfp_connection->trigger_codec_exchange = 1; 1344 break; 1345 default: 1346 hfp_connection->codec_confirmed = 0; 1347 hfp_connection->suggested_codec = 0; 1348 hfp_connection->negotiated_codec = 0; 1349 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 1350 hfp_connection->trigger_codec_exchange = 1; 1351 break; 1352 } 1353 } else { 1354 log_info("no codec negotiation feature, use CVSD"); 1355 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1356 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1357 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1358 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1359 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1360 hfp_connection->establish_audio_connection = 1; 1361 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1362 } 1363 1364 hfp_hf_run_for_context(hfp_connection); 1365 } 1366 1367 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1368 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1369 if (!hfp_connection) { 1370 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1371 return; 1372 } 1373 hfp_release_audio_connection(hfp_connection); 1374 hfp_hf_run_for_context(hfp_connection); 1375 } 1376 1377 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1378 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1379 if (!hfp_connection) { 1380 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1381 return; 1382 } 1383 1384 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1385 hfp_connection->hf_answer_incoming_call = 1; 1386 hfp_hf_run_for_context(hfp_connection); 1387 } else { 1388 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1389 } 1390 } 1391 1392 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1393 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1394 if (!hfp_connection) { 1395 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1396 return; 1397 } 1398 hfp_connection->hf_send_chup = 1; 1399 hfp_hf_run_for_context(hfp_connection); 1400 } 1401 1402 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1403 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1404 if (!hfp_connection) { 1405 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1406 return; 1407 } 1408 1409 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1410 hfp_connection->hf_send_chup = 1; 1411 hfp_hf_run_for_context(hfp_connection); 1412 } 1413 } 1414 1415 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1416 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1417 if (!hfp_connection) { 1418 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1419 return; 1420 } 1421 1422 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1423 hfp_connection->hf_send_chld_0 = 1; 1424 hfp_hf_run_for_context(hfp_connection); 1425 } 1426 } 1427 1428 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1429 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1430 if (!hfp_connection) { 1431 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1432 return; 1433 } 1434 1435 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1436 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1437 hfp_connection->hf_send_chld_1 = 1; 1438 hfp_hf_run_for_context(hfp_connection); 1439 } 1440 } 1441 1442 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1443 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1444 if (!hfp_connection) { 1445 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1446 return; 1447 } 1448 1449 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1450 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1451 hfp_connection->hf_send_chld_2 = 1; 1452 hfp_hf_run_for_context(hfp_connection); 1453 } 1454 } 1455 1456 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1457 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1458 if (!hfp_connection) { 1459 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1460 return; 1461 } 1462 1463 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1464 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1465 hfp_connection->hf_send_chld_3 = 1; 1466 hfp_hf_run_for_context(hfp_connection); 1467 } 1468 } 1469 1470 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1471 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1472 if (!hfp_connection) { 1473 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1474 return; 1475 } 1476 1477 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1478 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1479 hfp_connection->hf_send_chld_4 = 1; 1480 hfp_hf_run_for_context(hfp_connection); 1481 } 1482 } 1483 1484 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1485 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1486 if (!hfp_connection) { 1487 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1488 return; 1489 } 1490 1491 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1492 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1493 hfp_connection->hf_send_chld_x = 1; 1494 hfp_connection->hf_send_chld_x_index = 10 + index; 1495 hfp_hf_run_for_context(hfp_connection); 1496 } 1497 } 1498 1499 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1500 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1501 if (!hfp_connection) { 1502 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1503 return; 1504 } 1505 1506 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1507 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1508 hfp_connection->hf_send_chld_x = 1; 1509 hfp_connection->hf_send_chld_x_index = 20 + index; 1510 hfp_hf_run_for_context(hfp_connection); 1511 } 1512 } 1513 1514 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1515 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1516 if (!hfp_connection) { 1517 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1518 return; 1519 } 1520 1521 hfp_connection->hf_initiate_outgoing_call = 1; 1522 snprintf(phone_number, sizeof(phone_number), "%s", number); 1523 hfp_hf_run_for_context(hfp_connection); 1524 } 1525 1526 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1527 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1528 if (!hfp_connection) { 1529 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1530 return; 1531 } 1532 1533 hfp_connection->hf_initiate_memory_dialing = 1; 1534 hfp_connection->memory_id = memory_id; 1535 1536 hfp_hf_run_for_context(hfp_connection); 1537 } 1538 1539 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1540 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1541 if (!hfp_connection) { 1542 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1543 return; 1544 } 1545 1546 hfp_connection->hf_initiate_redial_last_number = 1; 1547 hfp_hf_run_for_context(hfp_connection); 1548 } 1549 1550 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1551 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1552 if (!hfp_connection) { 1553 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1554 return; 1555 } 1556 1557 hfp_connection->hf_activate_call_waiting_notification = 1; 1558 hfp_hf_run_for_context(hfp_connection); 1559 } 1560 1561 1562 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1563 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1564 if (!hfp_connection) { 1565 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1566 return; 1567 } 1568 1569 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1570 hfp_hf_run_for_context(hfp_connection); 1571 } 1572 1573 1574 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1575 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1576 if (!hfp_connection) { 1577 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1578 return; 1579 } 1580 1581 hfp_connection->hf_activate_calling_line_notification = 1; 1582 hfp_hf_run_for_context(hfp_connection); 1583 } 1584 1585 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1586 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1587 if (!hfp_connection) { 1588 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1589 return; 1590 } 1591 1592 hfp_connection->hf_deactivate_calling_line_notification = 1; 1593 hfp_hf_run_for_context(hfp_connection); 1594 } 1595 1596 1597 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1598 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1599 if (!hfp_connection) { 1600 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1601 return; 1602 } 1603 1604 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1605 hfp_hf_run_for_context(hfp_connection); 1606 } 1607 1608 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1609 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1610 if (!hfp_connection) { 1611 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1612 return; 1613 } 1614 1615 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1616 hfp_hf_run_for_context(hfp_connection); 1617 } 1618 1619 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1620 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1621 if (!hfp_connection) { 1622 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1623 return; 1624 } 1625 1626 hfp_connection->hf_activate_voice_recognition_notification = 1; 1627 hfp_hf_run_for_context(hfp_connection); 1628 } 1629 1630 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1631 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1632 if (!hfp_connection) { 1633 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1634 return; 1635 } 1636 1637 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1638 hfp_hf_run_for_context(hfp_connection); 1639 } 1640 1641 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1642 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1643 if (!hfp_connection) { 1644 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1645 return; 1646 } 1647 1648 if (hfp_connection->microphone_gain == gain) return; 1649 if ((gain < 0) || (gain > 15)){ 1650 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1651 return; 1652 } 1653 hfp_connection->microphone_gain = gain; 1654 hfp_connection->send_microphone_gain = 1; 1655 hfp_hf_run_for_context(hfp_connection); 1656 } 1657 1658 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1659 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1660 if (!hfp_connection) { 1661 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1662 return; 1663 } 1664 1665 if (hfp_connection->speaker_gain == gain) return; 1666 if ((gain < 0) || (gain > 15)){ 1667 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1668 return; 1669 } 1670 hfp_connection->speaker_gain = gain; 1671 hfp_connection->send_speaker_gain = 1; 1672 hfp_hf_run_for_context(hfp_connection); 1673 } 1674 1675 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1676 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1677 if (!hfp_connection) { 1678 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1679 return; 1680 } 1681 1682 hfp_connection->hf_send_dtmf_code = code; 1683 hfp_hf_run_for_context(hfp_connection); 1684 } 1685 1686 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1687 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1688 if (!hfp_connection) { 1689 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1690 return; 1691 } 1692 hfp_connection->hf_send_binp = 1; 1693 hfp_hf_run_for_context(hfp_connection); 1694 } 1695 1696 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1697 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1698 if (!hfp_connection) { 1699 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1700 return; 1701 } 1702 hfp_connection->hf_send_clcc = 1; 1703 hfp_hf_run_for_context(hfp_connection); 1704 } 1705 1706 1707 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1708 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1709 if (!hfp_connection) { 1710 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1711 return; 1712 } 1713 hfp_connection->hf_send_rrh = 1; 1714 hfp_connection->hf_send_rrh_command = '?'; 1715 hfp_hf_run_for_context(hfp_connection); 1716 } 1717 1718 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1719 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1720 if (!hfp_connection) { 1721 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1722 return; 1723 } 1724 hfp_connection->hf_send_rrh = 1; 1725 hfp_connection->hf_send_rrh_command = '0'; 1726 hfp_hf_run_for_context(hfp_connection); 1727 } 1728 1729 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1730 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1731 if (!hfp_connection) { 1732 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1733 return; 1734 } 1735 hfp_connection->hf_send_rrh = 1; 1736 hfp_connection->hf_send_rrh_command = '1'; 1737 hfp_hf_run_for_context(hfp_connection); 1738 } 1739 1740 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1741 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1742 if (!hfp_connection) { 1743 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1744 return; 1745 } 1746 hfp_connection->hf_send_rrh = 1; 1747 hfp_connection->hf_send_rrh_command = '2'; 1748 hfp_hf_run_for_context(hfp_connection); 1749 } 1750 1751 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1752 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1753 if (!hfp_connection) { 1754 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1755 return; 1756 } 1757 hfp_connection->hf_send_cnum = 1; 1758 hfp_hf_run_for_context(hfp_connection); 1759 } 1760 1761 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1762 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1763 if (!hfp_connection) { 1764 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1765 return; 1766 } 1767 // find index for assigned number 1768 int i; 1769 for (i = 0; i < hfp_indicators_nr ; i++){ 1770 if (hfp_indicators[i] == assigned_number){ 1771 // set value 1772 hfp_indicators_value[i] = value; 1773 // mark for update 1774 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1775 hfp_connection->generic_status_update_bitmap |= (1<<i); 1776 // send update 1777 hfp_hf_run_for_context(hfp_connection); 1778 } 1779 return; 1780 } 1781 } 1782 } 1783 1784 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1785 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1786 if (!hfp_connection) { 1787 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1788 return 0; 1789 } 1790 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1791 } 1792 1793 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){ 1794 if (!name){ 1795 name = default_hfp_hf_service_name; 1796 } 1797 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 1798 1799 // Construct SupportedFeatures for SDP bitmap: 1800 // 1801 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1802 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1803 // 1804 // Wide band speech (bit 5) requires Codec negotiation 1805 // 1806 uint16_t sdp_features = supported_features & 0x1f; 1807 if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 1808 sdp_features |= 1 << 5; 1809 } 1810 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1811 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 1812 } 1813 1814 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 1815 if (callback == NULL){ 1816 log_error("hfp_hf_register_packet_handler called with NULL callback"); 1817 return; 1818 } 1819 hfp_hf_callback = callback; 1820 hfp_set_hf_callback(callback); 1821 } 1822