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