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 BLUEKITCHEN 24 * GMBH 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 1052 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1053 hfp_connection->ag_indicators[i].status_changed = 0; 1054 hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1055 } 1056 1057 // restore volume settings 1058 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1059 hfp_connection->send_speaker_gain = 1; 1060 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1061 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1062 hfp_connection->send_microphone_gain = 1; 1063 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1064 // enable all indicators 1065 for (i=0; i < hfp_hf_indicators_nr; i++){ 1066 hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1067 hfp_connection->generic_status_indicators[i].state = 1; 1068 } 1069 } 1070 1071 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1072 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 1073 // Codec supported, confirm 1074 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1075 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1076 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 1077 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 1078 1079 hfp_connection->hf_send_codec_confirm = true; 1080 } else { 1081 // Codec not supported, send supported codecs 1082 hfp_connection->codec_confirmed = 0; 1083 hfp_connection->suggested_codec = 0; 1084 hfp_connection->negotiated_codec = 0; 1085 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 1086 1087 hfp_connection->hf_send_supported_codecs = true; 1088 } 1089 } 1090 1091 static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1092 bool event_emited = true; 1093 1094 switch (hfp_connection->response_pending_for_command){ 1095 case HFP_CMD_TURN_OFF_EC_AND_NR: 1096 hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1097 break; 1098 default: 1099 event_emited = false; 1100 1101 switch (hfp_connection->state){ 1102 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1103 if (has_codec_negotiation_feature(hfp_connection)){ 1104 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 1105 break; 1106 } 1107 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 1108 break; 1109 1110 case HFP_W4_NOTIFY_ON_CODECS: 1111 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 1112 break; 1113 1114 case HFP_W4_RETRIEVE_INDICATORS: 1115 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 1116 break; 1117 1118 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1119 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 1120 break; 1121 1122 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1123 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1124 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 1125 break; 1126 } 1127 if (has_hf_indicators_feature(hfp_connection)){ 1128 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 1129 break; 1130 } 1131 hfp_ag_slc_established(hfp_connection); 1132 break; 1133 1134 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1135 if (has_hf_indicators_feature(hfp_connection)){ 1136 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 1137 break; 1138 } 1139 hfp_ag_slc_established(hfp_connection); 1140 break; 1141 1142 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1143 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 1144 break; 1145 1146 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1147 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 1148 break; 1149 1150 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1151 hfp_ag_slc_established(hfp_connection); 1152 break; 1153 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1154 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1155 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1156 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1157 break; 1158 } 1159 1160 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1161 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1162 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1163 break; 1164 } 1165 1166 switch (hfp_connection->hf_query_operator_state){ 1167 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1168 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1169 break; 1170 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1171 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1172 hfp_emit_network_operator_event(hfp_connection); 1173 break; 1174 default: 1175 break; 1176 } 1177 1178 if (hfp_connection->enable_extended_audio_gateway_error_report){ 1179 hfp_connection->enable_extended_audio_gateway_error_report = 0; 1180 break; 1181 } 1182 1183 switch (hfp_connection->codecs_state){ 1184 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1185 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 1186 break; 1187 case HFP_CODECS_HF_CONFIRMED_CODEC: 1188 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1189 break; 1190 default: 1191 break; 1192 } 1193 hfp_hf_voice_recognition_state_machine(hfp_connection); 1194 break; 1195 case HFP_AUDIO_CONNECTION_ESTABLISHED: 1196 hfp_hf_voice_recognition_state_machine(hfp_connection); 1197 break; 1198 default: 1199 break; 1200 } 1201 break; 1202 } 1203 1204 // done 1205 hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1206 hfp_connection->ok_pending = 0; 1207 hfp_connection->command = HFP_CMD_NONE; 1208 return event_emited; 1209 } 1210 1211 static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1212 switch (callsetup_status){ 1213 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1214 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1215 return true; 1216 default: 1217 return false; 1218 } 1219 } 1220 1221 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 1222 uint16_t i; 1223 1224 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1225 if (hfp_connection->ag_indicators[i].status_changed) { 1226 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1227 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1228 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1229 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1230 if (ringing_old != ringing_new){ 1231 if (ringing_new){ 1232 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1233 } else { 1234 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1235 } 1236 } 1237 hfp_hf_callsetup_status = new_hf_callsetup_status; 1238 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1239 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1240 // avoid set but not used warning 1241 (void) hfp_hf_callheld_status; 1242 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1243 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1244 if (hfp_hf_call_status != new_hf_call_status){ 1245 if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1246 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1247 } else { 1248 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1249 } 1250 } 1251 hfp_hf_call_status = new_hf_call_status; 1252 } 1253 hfp_connection->ag_indicators[i].status_changed = 0; 1254 hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1255 break; 1256 } 1257 } 1258 } 1259 1260 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1261 int value; 1262 int i; 1263 bool event_emited; 1264 1265 switch (hfp_connection->command){ 1266 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1267 hfp_connection->command = HFP_CMD_NONE; 1268 hfp_hf_emit_subscriber_information(hfp_connection, 0); 1269 break; 1270 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1271 hfp_connection->command = HFP_CMD_NONE; 1272 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1273 break; 1274 case HFP_CMD_LIST_CURRENT_CALLS: 1275 hfp_connection->command = HFP_CMD_NONE; 1276 hfp_hf_emit_enhanced_call_status(hfp_connection); 1277 break; 1278 case HFP_CMD_SET_SPEAKER_GAIN: 1279 hfp_connection->command = HFP_CMD_NONE; 1280 value = btstack_atoi((char*)hfp_connection->line_buffer); 1281 hfp_hf_speaker_gain = value; 1282 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1283 break; 1284 case HFP_CMD_SET_MICROPHONE_GAIN: 1285 hfp_connection->command = HFP_CMD_NONE; 1286 value = btstack_atoi((char*)hfp_connection->line_buffer); 1287 hfp_hf_microphone_gain = value; 1288 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1289 break; 1290 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1291 hfp_connection->command = HFP_CMD_NONE; 1292 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1293 break; 1294 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1295 hfp_connection->command = HFP_CMD_NONE; 1296 hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1297 break; 1298 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1299 hfp_connection->command = HFP_CMD_NONE; 1300 hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1301 break; 1302 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1303 hfp_connection->command = HFP_CMD_NONE; 1304 hfp_connection->ok_pending = 0; 1305 hfp_connection->extended_audio_gateway_error = 0; 1306 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1307 break; 1308 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1309 break; 1310 case HFP_CMD_ERROR: 1311 switch (hfp_connection->state){ 1312 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1313 switch (hfp_connection->codecs_state){ 1314 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1315 hfp_reset_context_flags(hfp_connection); 1316 hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 1317 return; 1318 default: 1319 break; 1320 } 1321 break; 1322 default: 1323 break; 1324 } 1325 1326 // handle error response for voice activation (HF initiated) 1327 switch(hfp_connection->vra_state_requested){ 1328 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1329 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1330 break; 1331 default: 1332 if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1333 break; 1334 } 1335 hfp_connection->vra_state_requested = hfp_connection->vra_state; 1336 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1337 hfp_reset_context_flags(hfp_connection); 1338 return; 1339 } 1340 event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1341 if (!event_emited){ 1342 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1343 } 1344 hfp_reset_context_flags(hfp_connection); 1345 break; 1346 1347 case HFP_CMD_OK: 1348 hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1349 break; 1350 case HFP_CMD_RING: 1351 hfp_connection->command = HFP_CMD_NONE; 1352 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1353 break; 1354 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1355 hfp_connection->command = HFP_CMD_NONE; 1356 hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1357 break; 1358 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1359 hfp_connection->command = HFP_CMD_NONE; 1360 // report status after SLC established 1361 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1362 break; 1363 } 1364 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1365 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1366 } 1367 break; 1368 case HFP_CMD_AG_SUGGESTED_CODEC: 1369 hfp_connection->command = HFP_CMD_NONE; 1370 hfp_hf_handle_suggested_codec(hfp_connection); 1371 break; 1372 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1373 hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE)); 1374 default: 1375 break; 1376 } 1377 } 1378 1379 static int hfp_parser_is_end_of_line(uint8_t byte){ 1380 return (byte == '\n') || (byte == '\r'); 1381 } 1382 1383 static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1384 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1385 if (size < 1) return; 1386 1387 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1388 #ifdef ENABLE_HFP_AT_MESSAGES 1389 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1390 #endif 1391 1392 // process messages byte-wise 1393 uint8_t pos; 1394 for (pos = 0; pos < size; pos++){ 1395 hfp_parse(hfp_connection, packet[pos], 1); 1396 // parse until end of line "\r" or "\n" 1397 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1398 hfp_hf_handle_rfcomm_command(hfp_connection); 1399 } 1400 } 1401 1402 static void hfp_hf_run(void){ 1403 btstack_linked_list_iterator_t it; 1404 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1405 while (btstack_linked_list_iterator_has_next(&it)){ 1406 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1407 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1408 hfp_hf_run_for_context(hfp_connection); 1409 } 1410 } 1411 1412 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1413 hfp_connection_t * hfp_connection; 1414 switch (packet_type){ 1415 case RFCOMM_DATA_PACKET: 1416 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1417 if (!hfp_connection) return; 1418 hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 1419 hfp_hf_run_for_context(hfp_connection); 1420 return; 1421 case HCI_EVENT_PACKET: 1422 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1423 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1424 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1425 return; 1426 } 1427 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1428 break; 1429 default: 1430 break; 1431 } 1432 hfp_hf_run(); 1433 } 1434 1435 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1436 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1437 hfp_hf_run(); 1438 } 1439 1440 static void hfp_hf_set_defaults(void){ 1441 hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1442 hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1443 hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1444 hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1445 hfp_hf_codecs_nr = 0; 1446 hfp_hf_speaker_gain = 9; 1447 hfp_hf_microphone_gain = 9; 1448 hfp_hf_indicators_nr = 0; 1449 } 1450 1451 uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1452 uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1453 if (status != ERROR_CODE_SUCCESS){ 1454 return status; 1455 } 1456 1457 hfp_init(); 1458 hfp_hf_set_defaults(); 1459 1460 hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 1461 hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 1462 1463 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1464 hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1465 return ERROR_CODE_SUCCESS; 1466 } 1467 1468 void hfp_hf_deinit(void){ 1469 hfp_deinit(); 1470 hfp_hf_set_defaults(); 1471 1472 hfp_hf_callback = NULL; 1473 (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1474 (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1475 } 1476 1477 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 1478 btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 1479 1480 hfp_hf_codecs_nr = codecs_nr; 1481 int i; 1482 for (i=0; i<codecs_nr; i++){ 1483 hfp_hf_codecs[i] = codecs[i]; 1484 } 1485 } 1486 1487 void hfp_hf_init_supported_features(uint32_t supported_features){ 1488 hfp_hf_supported_features = supported_features; 1489 } 1490 1491 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1492 btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 1493 1494 hfp_hf_indicators_nr = indicators_nr; 1495 int i; 1496 for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1497 hfp_hf_indicators[i] = indicators[i]; 1498 } 1499 } 1500 1501 uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1502 return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1503 } 1504 1505 uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1506 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1507 if (!hfp_connection){ 1508 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1509 } 1510 hfp_trigger_release_service_level_connection(hfp_connection); 1511 hfp_hf_run_for_context(hfp_connection); 1512 return ERROR_CODE_SUCCESS; 1513 } 1514 1515 static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1516 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1517 if (!hfp_connection) { 1518 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1519 } 1520 hfp_connection->enable_status_update_for_ag_indicators = enable; 1521 hfp_hf_run_for_context(hfp_connection); 1522 return ERROR_CODE_SUCCESS; 1523 } 1524 1525 uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1526 return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1527 } 1528 1529 uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1530 return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1531 } 1532 1533 // TODO: returned ERROR - wrong format 1534 uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1535 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1536 if (!hfp_connection) { 1537 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1538 } 1539 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1540 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1541 hfp_hf_run_for_context(hfp_connection); 1542 return ERROR_CODE_SUCCESS; 1543 } 1544 1545 uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1546 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1547 if (!hfp_connection) { 1548 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1549 } 1550 1551 switch (hfp_connection->hf_query_operator_state){ 1552 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1553 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1554 break; 1555 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1556 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1557 break; 1558 default: 1559 return ERROR_CODE_COMMAND_DISALLOWED; 1560 } 1561 hfp_hf_run_for_context(hfp_connection); 1562 return ERROR_CODE_SUCCESS; 1563 } 1564 1565 static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1566 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1567 if (!hfp_connection) { 1568 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1569 } 1570 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1571 hfp_hf_run_for_context(hfp_connection); 1572 return ERROR_CODE_SUCCESS; 1573 } 1574 1575 1576 uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1577 return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1578 } 1579 1580 uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1581 return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1582 } 1583 1584 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1585 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 1586 } 1587 1588 uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1589 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1590 if (!hfp_connection) { 1591 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1592 } 1593 1594 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1595 return ERROR_CODE_COMMAND_DISALLOWED; 1596 } 1597 1598 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 1599 return ERROR_CODE_COMMAND_DISALLOWED; 1600 } 1601 if (has_codec_negotiation_feature(hfp_connection)) { 1602 switch (hfp_connection->codecs_state) { 1603 case HFP_CODECS_W4_AG_COMMON_CODEC: 1604 break; 1605 case HFP_CODECS_EXCHANGED: 1606 hfp_connection->trigger_codec_exchange = 1; 1607 break; 1608 default: 1609 hfp_connection->codec_confirmed = 0; 1610 hfp_connection->suggested_codec = 0; 1611 hfp_connection->negotiated_codec = 0; 1612 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 1613 hfp_connection->trigger_codec_exchange = 1; 1614 break; 1615 } 1616 } else { 1617 log_info("no codec negotiation feature, use CVSD"); 1618 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1619 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1620 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1621 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1622 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1623 hfp_connection->establish_audio_connection = 1; 1624 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1625 } 1626 1627 hfp_hf_run_for_context(hfp_connection); 1628 return ERROR_CODE_SUCCESS; 1629 } 1630 1631 uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1632 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1633 if (!hfp_connection) { 1634 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1635 } 1636 if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 1637 return ERROR_CODE_COMMAND_DISALLOWED; 1638 } 1639 uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 1640 if (status == ERROR_CODE_SUCCESS){ 1641 hfp_hf_run_for_context(hfp_connection); 1642 } 1643 return ERROR_CODE_SUCCESS; 1644 } 1645 1646 uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1647 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1648 if (!hfp_connection) { 1649 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1650 } 1651 1652 if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1653 hfp_connection->hf_answer_incoming_call = 1; 1654 hfp_hf_run_for_context(hfp_connection); 1655 } else { 1656 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 1657 return ERROR_CODE_COMMAND_DISALLOWED; 1658 } 1659 return ERROR_CODE_SUCCESS; 1660 } 1661 1662 uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1663 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1664 if (!hfp_connection) { 1665 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1666 } 1667 hfp_connection->hf_send_chup = 1; 1668 hfp_hf_run_for_context(hfp_connection); 1669 return ERROR_CODE_SUCCESS; 1670 } 1671 1672 uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1673 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1674 if (!hfp_connection) { 1675 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1676 } 1677 1678 if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1679 hfp_connection->hf_send_chup = 1; 1680 hfp_hf_run_for_context(hfp_connection); 1681 } 1682 return ERROR_CODE_SUCCESS; 1683 } 1684 1685 uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1686 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1687 if (!hfp_connection) { 1688 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1689 } 1690 1691 if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1692 hfp_connection->hf_send_chld_0 = 1; 1693 hfp_hf_run_for_context(hfp_connection); 1694 } 1695 return ERROR_CODE_SUCCESS; 1696 } 1697 1698 uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1699 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1700 if (!hfp_connection) { 1701 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1702 } 1703 1704 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1705 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1706 hfp_connection->hf_send_chld_1 = 1; 1707 hfp_hf_run_for_context(hfp_connection); 1708 } 1709 return ERROR_CODE_SUCCESS; 1710 } 1711 1712 uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1713 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1714 if (!hfp_connection) { 1715 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1716 } 1717 1718 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1719 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1720 hfp_connection->hf_send_chld_2 = 1; 1721 hfp_hf_run_for_context(hfp_connection); 1722 } 1723 return ERROR_CODE_SUCCESS; 1724 } 1725 1726 uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1727 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1728 if (!hfp_connection) { 1729 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1730 } 1731 1732 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1733 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1734 hfp_connection->hf_send_chld_3 = 1; 1735 hfp_hf_run_for_context(hfp_connection); 1736 } 1737 return ERROR_CODE_SUCCESS; 1738 } 1739 1740 uint8_t hfp_hf_connect_calls(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 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1744 } 1745 1746 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1747 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1748 hfp_connection->hf_send_chld_4 = 1; 1749 hfp_hf_run_for_context(hfp_connection); 1750 } 1751 return ERROR_CODE_SUCCESS; 1752 } 1753 1754 uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1755 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1756 if (!hfp_connection) { 1757 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1758 } 1759 1760 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1761 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1762 hfp_connection->hf_send_chld_x = 1; 1763 hfp_connection->hf_send_chld_x_index = 10 + index; 1764 hfp_hf_run_for_context(hfp_connection); 1765 } 1766 return ERROR_CODE_SUCCESS; 1767 } 1768 1769 uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1770 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1771 if (!hfp_connection) { 1772 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1773 } 1774 1775 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1776 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1777 hfp_connection->hf_send_chld_x = 1; 1778 hfp_connection->hf_send_chld_x_index = 20 + index; 1779 hfp_hf_run_for_context(hfp_connection); 1780 } 1781 return ERROR_CODE_SUCCESS; 1782 } 1783 1784 uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1785 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1786 if (!hfp_connection) { 1787 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1788 } 1789 1790 hfp_connection->hf_initiate_outgoing_call = 1; 1791 snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 1792 hfp_hf_run_for_context(hfp_connection); 1793 return ERROR_CODE_SUCCESS; 1794 } 1795 1796 uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1797 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1798 if (!hfp_connection) { 1799 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1800 } 1801 1802 hfp_connection->hf_initiate_memory_dialing = 1; 1803 hfp_connection->memory_id = memory_id; 1804 1805 hfp_hf_run_for_context(hfp_connection); 1806 return ERROR_CODE_SUCCESS; 1807 } 1808 1809 uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1810 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1811 if (!hfp_connection) { 1812 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1813 } 1814 1815 hfp_connection->hf_initiate_redial_last_number = 1; 1816 hfp_hf_run_for_context(hfp_connection); 1817 return ERROR_CODE_SUCCESS; 1818 } 1819 1820 uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1821 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1822 if (!hfp_connection) { 1823 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1824 } 1825 1826 hfp_connection->hf_activate_call_waiting_notification = 1; 1827 hfp_hf_run_for_context(hfp_connection); 1828 return ERROR_CODE_SUCCESS; 1829 } 1830 1831 1832 uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1833 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1834 if (!hfp_connection) { 1835 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1836 } 1837 1838 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1839 hfp_hf_run_for_context(hfp_connection); 1840 return ERROR_CODE_SUCCESS; 1841 } 1842 1843 1844 uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1845 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1846 if (!hfp_connection) { 1847 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1848 } 1849 1850 hfp_connection->hf_activate_calling_line_notification = 1; 1851 hfp_hf_run_for_context(hfp_connection); 1852 return ERROR_CODE_SUCCESS; 1853 } 1854 1855 uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1856 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1857 if (!hfp_connection) { 1858 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1859 } 1860 1861 hfp_connection->hf_deactivate_calling_line_notification = 1; 1862 hfp_hf_run_for_context(hfp_connection); 1863 return ERROR_CODE_SUCCESS; 1864 } 1865 1866 static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 1867 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1868 int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 1869 return hf && ag; 1870 } 1871 1872 uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1873 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1874 if (!hfp_connection) { 1875 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1876 } 1877 if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 1878 return ERROR_CODE_COMMAND_DISALLOWED; 1879 } 1880 1881 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1882 hfp_hf_run_for_context(hfp_connection); 1883 return ERROR_CODE_SUCCESS; 1884 } 1885 1886 uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1887 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1888 if (!hfp_connection) { 1889 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1890 } 1891 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1892 return ERROR_CODE_COMMAND_DISALLOWED; 1893 } 1894 1895 bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1896 bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1897 if (!enhanced_vra_supported && !legacy_vra_supported){ 1898 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1899 } 1900 1901 switch (hfp_connection->vra_state){ 1902 case HFP_VRA_VOICE_RECOGNITION_OFF: 1903 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1904 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1905 hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1906 break; 1907 case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1908 hfp_connection->activate_voice_recognition = true; 1909 break; 1910 default: 1911 return ERROR_CODE_COMMAND_DISALLOWED; 1912 } 1913 1914 hfp_hf_run_for_context(hfp_connection); 1915 return ERROR_CODE_SUCCESS; 1916 } 1917 1918 uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1919 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1920 if (!hfp_connection) { 1921 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1922 } 1923 1924 if (hfp_connection->emit_vra_enabled_after_audio_established){ 1925 return ERROR_CODE_COMMAND_DISALLOWED; 1926 } 1927 1928 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 1929 return ERROR_CODE_COMMAND_DISALLOWED; 1930 } 1931 1932 bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1933 if (!enhanced_vra_supported){ 1934 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1935 } 1936 1937 switch (hfp_connection->vra_state){ 1938 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1939 case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1940 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1941 break; 1942 default: 1943 return ERROR_CODE_COMMAND_DISALLOWED; 1944 } 1945 1946 hfp_hf_run_for_context(hfp_connection); 1947 return ERROR_CODE_SUCCESS; 1948 } 1949 1950 1951 uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1952 // return deactivate_voice_recognition(acl_handle, false); 1953 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1954 if (!hfp_connection) { 1955 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1956 } 1957 1958 if (hfp_connection->emit_vra_enabled_after_audio_established){ 1959 return ERROR_CODE_COMMAND_DISALLOWED; 1960 } 1961 1962 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1963 hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1964 return ERROR_CODE_COMMAND_DISALLOWED; 1965 } 1966 1967 bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1968 bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1969 if (!enhanced_vra_supported && !legacy_vra_supported){ 1970 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1971 } 1972 1973 switch (hfp_connection->vra_state){ 1974 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1975 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1976 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1977 case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1978 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1979 break; 1980 1981 case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1982 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1983 hfp_connection->deactivate_voice_recognition = true; 1984 break; 1985 1986 case HFP_VRA_VOICE_RECOGNITION_OFF: 1987 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1988 case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1989 default: 1990 return ERROR_CODE_COMMAND_DISALLOWED; 1991 } 1992 1993 hfp_hf_run_for_context(hfp_connection); 1994 return ERROR_CODE_SUCCESS; 1995 } 1996 1997 uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1998 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1999 if (!hfp_connection) { 2000 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2001 } 2002 2003 if (hfp_connection->microphone_gain == gain) { 2004 return ERROR_CODE_SUCCESS; 2005 } 2006 2007 if ((gain < 0) || (gain > 15)){ 2008 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2009 return ERROR_CODE_COMMAND_DISALLOWED; 2010 } 2011 2012 hfp_connection->microphone_gain = gain; 2013 hfp_connection->send_microphone_gain = 1; 2014 hfp_hf_run_for_context(hfp_connection); 2015 return ERROR_CODE_SUCCESS; 2016 } 2017 2018 uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 2019 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2020 if (!hfp_connection) { 2021 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2022 } 2023 2024 if (hfp_connection->speaker_gain == gain){ 2025 return ERROR_CODE_SUCCESS; 2026 } 2027 2028 if ((gain < 0) || (gain > 15)){ 2029 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2030 return ERROR_CODE_COMMAND_DISALLOWED; 2031 } 2032 2033 hfp_connection->speaker_gain = gain; 2034 hfp_connection->send_speaker_gain = 1; 2035 hfp_hf_run_for_context(hfp_connection); 2036 return ERROR_CODE_SUCCESS; 2037 } 2038 2039 uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 2040 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2041 if (!hfp_connection) { 2042 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2043 } 2044 hfp_connection->hf_send_dtmf_code = code; 2045 hfp_hf_run_for_context(hfp_connection); 2046 return ERROR_CODE_SUCCESS; 2047 } 2048 2049 uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 2050 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2051 if (!hfp_connection) { 2052 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2053 } 2054 hfp_connection->hf_send_binp = 1; 2055 hfp_hf_run_for_context(hfp_connection); 2056 return ERROR_CODE_SUCCESS; 2057 } 2058 2059 uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 2060 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2061 if (!hfp_connection) { 2062 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2063 } 2064 hfp_connection->hf_send_clcc = 1; 2065 hfp_hf_run_for_context(hfp_connection); 2066 return ERROR_CODE_SUCCESS; 2067 } 2068 2069 2070 uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 2071 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2072 if (!hfp_connection) { 2073 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2074 } 2075 hfp_connection->hf_send_rrh = 1; 2076 hfp_connection->hf_send_rrh_command = '?'; 2077 hfp_hf_run_for_context(hfp_connection); 2078 return ERROR_CODE_SUCCESS; 2079 } 2080 2081 uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 2082 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2083 if (!hfp_connection) { 2084 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2085 } 2086 hfp_connection->hf_send_rrh = 1; 2087 hfp_connection->hf_send_rrh_command = '0'; 2088 hfp_hf_run_for_context(hfp_connection); 2089 return ERROR_CODE_SUCCESS; 2090 } 2091 2092 uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 2093 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2094 if (!hfp_connection) { 2095 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2096 } 2097 hfp_connection->hf_send_rrh = 1; 2098 hfp_connection->hf_send_rrh_command = '1'; 2099 hfp_hf_run_for_context(hfp_connection); 2100 return ERROR_CODE_SUCCESS; 2101 } 2102 2103 uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 2104 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2105 if (!hfp_connection) { 2106 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2107 } 2108 hfp_connection->hf_send_rrh = 1; 2109 hfp_connection->hf_send_rrh_command = '2'; 2110 hfp_hf_run_for_context(hfp_connection); 2111 return ERROR_CODE_SUCCESS; 2112 } 2113 2114 uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 2115 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2116 if (!hfp_connection) { 2117 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2118 } 2119 hfp_connection->hf_send_cnum = 1; 2120 hfp_hf_run_for_context(hfp_connection); 2121 return ERROR_CODE_SUCCESS; 2122 } 2123 2124 uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 2125 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2126 if (!hfp_connection) { 2127 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2128 } 2129 // find index for assigned number 2130 int i; 2131 for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2132 if (hfp_hf_indicators[i] == assigned_number){ 2133 // set value 2134 hfp_hf_indicators_value[i] = value; 2135 // mark for update 2136 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2137 hfp_connection->generic_status_update_bitmap |= (1<<i); 2138 // send update 2139 hfp_hf_run_for_context(hfp_connection); 2140 } 2141 return ERROR_CODE_SUCCESS; 2142 } 2143 } 2144 return ERROR_CODE_SUCCESS; 2145 } 2146 2147 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2148 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2149 if (!hfp_connection) { 2150 return 0; 2151 } 2152 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2153 } 2154 2155 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){ 2156 if (!name){ 2157 name = hfp_hf_default_service_name; 2158 } 2159 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 2160 2161 // Construct SupportedFeatures for SDP bitmap: 2162 // 2163 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 2164 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 2165 // 2166 // Wide band speech (bit 5) requires Codec negotiation 2167 // 2168 uint16_t sdp_features = supported_features & 0x1f; 2169 if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 2170 sdp_features |= 1 << 5; 2171 } 2172 2173 if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 2174 sdp_features |= 1 << 6; 2175 } 2176 2177 if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 2178 sdp_features |= 1 << 7; 2179 } 2180 2181 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 2182 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 2183 } 2184 2185 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 2186 btstack_assert(callback != NULL); 2187 2188 hfp_hf_callback = callback; 2189 hfp_set_hf_callback(callback); 2190 } 2191