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 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 634 } 635 break; 636 637 638 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 639 hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 640 hfp_connection->vra_state_requested = hfp_connection->vra_state; 641 hfp_connection->activate_voice_recognition = false; 642 if (hfp_connection->deactivate_voice_recognition){ 643 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle); 644 } else { 645 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 646 } 647 break; 648 649 default: 650 break; 651 } 652 return done; 653 } 654 655 656 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 657 if (hfp_connection->ok_pending) return 0; 658 659 if (hfp_connection->trigger_codec_exchange){ 660 hfp_connection->trigger_codec_exchange = 0; 661 662 hfp_connection->ok_pending = 1; 663 hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 664 return 1; 665 } 666 667 if (hfp_connection->hf_send_codec_confirm){ 668 hfp_connection->hf_send_codec_confirm = false; 669 670 hfp_connection->ok_pending = 1; 671 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 672 return 1; 673 } 674 675 if (hfp_connection->hf_send_supported_codecs){ 676 hfp_connection->hf_send_supported_codecs = false; 677 678 hfp_connection->ok_pending = 1; 679 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 680 return 1; 681 } 682 683 return 0; 684 } 685 686 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 687 if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 688 (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 689 690 if (hfp_connection->release_audio_connection){ 691 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 692 hfp_connection->release_audio_connection = 0; 693 gap_disconnect(hfp_connection->sco_handle); 694 return 1; 695 } 696 697 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 698 699 // run codecs exchange 700 int done = codecs_exchange_state_machine(hfp_connection); 701 if (done) return 1; 702 703 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 704 if (hfp_connection->establish_audio_connection){ 705 hfp_connection->state = HFP_W4_SCO_CONNECTED; 706 hfp_connection->establish_audio_connection = 0; 707 hfp_setup_synchronous_connection(hfp_connection); 708 return 1; 709 } 710 return 0; 711 } 712 713 714 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 715 716 if (hfp_connection->ok_pending) return 0; 717 718 if (hfp_connection->hf_answer_incoming_call){ 719 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 720 hfp_connection->hf_answer_incoming_call = 0; 721 return 1; 722 } 723 return 0; 724 } 725 726 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 727 728 btstack_assert(hfp_connection != NULL); 729 btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 730 731 // during SDP query, RFCOMM CID is not set 732 if (hfp_connection->rfcomm_cid == 0) return; 733 734 // assert command could be sent 735 if (hci_can_send_command_packet_now() == 0) return; 736 737 #ifdef ENABLE_CC256X_ASSISTED_HFP 738 // WBS Disassociate 739 if (hfp_connection->cc256x_send_wbs_disassociate){ 740 hfp_connection->cc256x_send_wbs_disassociate = false; 741 hci_send_cmd(&hci_ti_wbs_disassociate); 742 return; 743 } 744 // Write Codec Config 745 if (hfp_connection->cc256x_send_write_codec_config){ 746 hfp_connection->cc256x_send_write_codec_config = false; 747 hfp_cc256x_write_codec_config(hfp_connection); 748 return; 749 } 750 // WBS Associate 751 if (hfp_connection->cc256x_send_wbs_associate){ 752 hfp_connection->cc256x_send_wbs_associate = false; 753 hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 754 return; 755 } 756 #endif 757 #ifdef ENABLE_BCM_PCM_WBS 758 // Enable WBS 759 if (hfp_connection->bcm_send_enable_wbs){ 760 hfp_connection->bcm_send_enable_wbs = false; 761 hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 762 return; 763 } 764 // Write I2S/PCM params 765 if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 766 hfp_connection->bcm_send_write_i2spcm_interface_param = false; 767 hfp_bcm_write_i2spcm_interface_param(hfp_connection); 768 return; 769 } 770 // Disable WBS 771 if (hfp_connection->bcm_send_disable_wbs){ 772 hfp_connection->bcm_send_disable_wbs = false; 773 hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 774 return; 775 } 776 #endif 777 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 778 if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 779 hfp_finalize_connection_context(hfp_connection); 780 return; 781 } 782 #endif 783 784 if (hfp_connection->accept_sco){ 785 bool incoming_eSCO = hfp_connection->accept_sco == 2; 786 hfp_connection->accept_sco = 0; 787 // notify about codec selection if not done already 788 if (hfp_connection->negotiated_codec == 0){ 789 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 790 } 791 hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 792 return; 793 } 794 795 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 796 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 797 return; 798 } 799 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 800 if (!done){ 801 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 802 } 803 if (!done){ 804 done = hfp_hf_run_for_audio_connection(hfp_connection); 805 } 806 if (!done){ 807 done = hfp_hf_voice_recognition_state_machine(hfp_connection); 808 } 809 if (!done){ 810 done = call_setup_state_machine(hfp_connection); 811 } 812 813 // don't send a new command while ok still pending or SLC is not established 814 if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){ 815 return; 816 } 817 818 if (hfp_connection->send_microphone_gain){ 819 hfp_connection->send_microphone_gain = 0; 820 hfp_connection->ok_pending = 1; 821 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 822 return; 823 } 824 825 if (hfp_connection->send_speaker_gain){ 826 hfp_connection->send_speaker_gain = 0; 827 hfp_connection->ok_pending = 1; 828 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 829 return; 830 } 831 832 if (hfp_connection->hf_deactivate_calling_line_notification){ 833 hfp_connection->hf_deactivate_calling_line_notification = 0; 834 hfp_connection->ok_pending = 1; 835 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 836 return; 837 } 838 839 if (hfp_connection->hf_activate_calling_line_notification){ 840 hfp_connection->hf_activate_calling_line_notification = 0; 841 hfp_connection->ok_pending = 1; 842 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 843 return; 844 } 845 846 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 847 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 848 hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR; 849 hfp_connection->ok_pending = 1; 850 hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0); 851 return; 852 } 853 854 if (hfp_connection->hf_deactivate_call_waiting_notification){ 855 hfp_connection->hf_deactivate_call_waiting_notification = 0; 856 hfp_connection->ok_pending = 1; 857 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 858 return; 859 } 860 861 if (hfp_connection->hf_activate_call_waiting_notification){ 862 hfp_connection->hf_activate_call_waiting_notification = 0; 863 hfp_connection->ok_pending = 1; 864 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 865 return; 866 } 867 868 if (hfp_connection->hf_initiate_outgoing_call){ 869 hfp_connection->hf_initiate_outgoing_call = 0; 870 hfp_connection->ok_pending = 1; 871 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 872 return; 873 } 874 875 if (hfp_connection->hf_initiate_memory_dialing){ 876 hfp_connection->hf_initiate_memory_dialing = 0; 877 hfp_connection->ok_pending = 1; 878 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 879 return; 880 } 881 882 if (hfp_connection->hf_initiate_redial_last_number){ 883 hfp_connection->hf_initiate_redial_last_number = 0; 884 hfp_connection->ok_pending = 1; 885 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 886 return; 887 } 888 889 if (hfp_connection->hf_send_chup){ 890 hfp_connection->hf_send_chup = 0; 891 hfp_connection->ok_pending = 1; 892 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 893 return; 894 } 895 896 if (hfp_connection->hf_send_chld_0){ 897 hfp_connection->hf_send_chld_0 = 0; 898 hfp_connection->ok_pending = 1; 899 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 900 return; 901 } 902 903 if (hfp_connection->hf_send_chld_1){ 904 hfp_connection->hf_send_chld_1 = 0; 905 hfp_connection->ok_pending = 1; 906 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 907 return; 908 } 909 910 if (hfp_connection->hf_send_chld_2){ 911 hfp_connection->hf_send_chld_2 = 0; 912 hfp_connection->ok_pending = 1; 913 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 914 return; 915 } 916 917 if (hfp_connection->hf_send_chld_3){ 918 hfp_connection->hf_send_chld_3 = 0; 919 hfp_connection->ok_pending = 1; 920 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 921 return; 922 } 923 924 if (hfp_connection->hf_send_chld_4){ 925 hfp_connection->hf_send_chld_4 = 0; 926 hfp_connection->ok_pending = 1; 927 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 928 return; 929 } 930 931 if (hfp_connection->hf_send_chld_x){ 932 hfp_connection->hf_send_chld_x = 0; 933 hfp_connection->ok_pending = 1; 934 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 935 return; 936 } 937 938 if (hfp_connection->hf_send_dtmf_code){ 939 char code = hfp_connection->hf_send_dtmf_code; 940 hfp_connection->hf_send_dtmf_code = 0; 941 hfp_connection->ok_pending = 1; 942 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 943 return; 944 } 945 946 if (hfp_connection->hf_send_binp){ 947 hfp_connection->hf_send_binp = 0; 948 hfp_connection->ok_pending = 1; 949 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 950 return; 951 } 952 953 if (hfp_connection->hf_send_clcc){ 954 hfp_connection->hf_send_clcc = 0; 955 hfp_connection->ok_pending = 1; 956 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 957 return; 958 } 959 960 if (hfp_connection->hf_send_rrh){ 961 hfp_connection->hf_send_rrh = 0; 962 char buffer[20]; 963 switch (hfp_connection->hf_send_rrh_command){ 964 case '?': 965 snprintf(buffer, sizeof(buffer), "AT%s?\r", 966 HFP_RESPONSE_AND_HOLD); 967 buffer[sizeof(buffer) - 1] = 0; 968 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 969 return; 970 case '0': 971 case '1': 972 case '2': 973 snprintf(buffer, sizeof(buffer), "AT%s=%c\r", 974 HFP_RESPONSE_AND_HOLD, 975 hfp_connection->hf_send_rrh_command); 976 buffer[sizeof(buffer) - 1] = 0; 977 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 978 return; 979 default: 980 break; 981 } 982 return; 983 } 984 985 if (hfp_connection->hf_send_cnum){ 986 hfp_connection->hf_send_cnum = 0; 987 char buffer[20]; 988 snprintf(buffer, sizeof(buffer), "AT%s\r", 989 HFP_SUBSCRIBER_NUMBER_INFORMATION); 990 buffer[sizeof(buffer) - 1] = 0; 991 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 992 return; 993 } 994 995 // update HF indicators 996 if (hfp_connection->generic_status_update_bitmap){ 997 int i; 998 for (i=0; i < hfp_hf_indicators_nr; i++){ 999 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 1000 if (hfp_connection->generic_status_indicators[i].state){ 1001 hfp_connection->ok_pending = 1; 1002 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 1003 char buffer[30]; 1004 snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r", 1005 HFP_TRANSFER_HF_INDICATOR_STATUS, 1006 hfp_hf_indicators[i], 1007 (unsigned int)hfp_hf_indicators_value[i]); 1008 buffer[sizeof(buffer) - 1] = 0; 1009 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1010 } else { 1011 log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]); 1012 } 1013 return; 1014 } 1015 } 1016 } 1017 1018 if (done) return; 1019 // deal with disconnect 1020 switch (hfp_connection->state){ 1021 case HFP_W2_DISCONNECT_RFCOMM: 1022 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1023 rfcomm_disconnect(hfp_connection->rfcomm_cid); 1024 break; 1025 1026 default: 1027 break; 1028 } 1029 } 1030 1031 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 1032 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 1033 1034 hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 1035 1036 uint8_t i; 1037 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1038 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1039 } 1040 // restore volume settings 1041 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 1042 hfp_connection->send_speaker_gain = 1; 1043 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 1044 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 1045 hfp_connection->send_microphone_gain = 1; 1046 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 1047 // enable all indicators 1048 for (i=0; i < hfp_hf_indicators_nr; i++){ 1049 hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i]; 1050 hfp_connection->generic_status_indicators[i].state = 1; 1051 } 1052 } 1053 1054 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 1055 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){ 1056 // Codec supported, confirm 1057 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1058 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1059 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 1060 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 1061 1062 hfp_connection->hf_send_codec_confirm = true; 1063 } else { 1064 // Codec not supported, send supported codecs 1065 hfp_connection->codec_confirmed = 0; 1066 hfp_connection->suggested_codec = 0; 1067 hfp_connection->negotiated_codec = 0; 1068 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 1069 1070 hfp_connection->hf_send_supported_codecs = true; 1071 } 1072 } 1073 1074 static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){ 1075 bool event_emited = true; 1076 1077 switch (hfp_connection->response_pending_for_command){ 1078 case HFP_CMD_TURN_OFF_EC_AND_NR: 1079 hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 1080 break; 1081 default: 1082 event_emited = false; 1083 1084 switch (hfp_connection->state){ 1085 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 1086 if (has_codec_negotiation_feature(hfp_connection)){ 1087 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 1088 break; 1089 } 1090 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 1091 break; 1092 1093 case HFP_W4_NOTIFY_ON_CODECS: 1094 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 1095 break; 1096 1097 case HFP_W4_RETRIEVE_INDICATORS: 1098 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 1099 break; 1100 1101 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1102 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 1103 break; 1104 1105 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 1106 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 1107 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 1108 break; 1109 } 1110 if (has_hf_indicators_feature(hfp_connection)){ 1111 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 1112 break; 1113 } 1114 hfp_ag_slc_established(hfp_connection); 1115 break; 1116 1117 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 1118 if (has_hf_indicators_feature(hfp_connection)){ 1119 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 1120 break; 1121 } 1122 hfp_ag_slc_established(hfp_connection); 1123 break; 1124 1125 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 1126 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 1127 break; 1128 1129 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 1130 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 1131 break; 1132 1133 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 1134 hfp_ag_slc_established(hfp_connection); 1135 break; 1136 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1137 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 1138 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 1139 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1140 break; 1141 } 1142 1143 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 1144 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 1145 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 1146 break; 1147 } 1148 1149 switch (hfp_connection->hf_query_operator_state){ 1150 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1151 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1152 break; 1153 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1154 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1155 hfp_emit_network_operator_event(hfp_connection); 1156 break; 1157 default: 1158 break; 1159 } 1160 1161 if (hfp_connection->enable_extended_audio_gateway_error_report){ 1162 hfp_connection->enable_extended_audio_gateway_error_report = 0; 1163 break; 1164 } 1165 1166 switch (hfp_connection->codecs_state){ 1167 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1168 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 1169 break; 1170 case HFP_CODECS_HF_CONFIRMED_CODEC: 1171 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1172 break; 1173 default: 1174 break; 1175 } 1176 hfp_hf_voice_recognition_state_machine(hfp_connection); 1177 break; 1178 case HFP_AUDIO_CONNECTION_ESTABLISHED: 1179 hfp_hf_voice_recognition_state_machine(hfp_connection); 1180 break; 1181 default: 1182 break; 1183 } 1184 break; 1185 } 1186 1187 // done 1188 hfp_connection->response_pending_for_command = HFP_CMD_NONE; 1189 hfp_connection->ok_pending = 0; 1190 hfp_connection->command = HFP_CMD_NONE; 1191 return event_emited; 1192 } 1193 1194 static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){ 1195 switch (callsetup_status){ 1196 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1197 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1198 return true; 1199 default: 1200 return false; 1201 } 1202 } 1203 1204 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 1205 uint16_t i; 1206 1207 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1208 if (hfp_connection->ag_indicators[i].status_changed) { 1209 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1210 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1211 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status); 1212 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status); 1213 if (ringing_old != ringing_new){ 1214 if (ringing_new){ 1215 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING); 1216 } else { 1217 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING); 1218 } 1219 } 1220 hfp_hf_callsetup_status = new_hf_callsetup_status; 1221 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1222 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1223 // avoid set but not used warning 1224 (void) hfp_hf_callheld_status; 1225 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1226 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1227 if (hfp_hf_call_status != new_hf_call_status){ 1228 if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 1229 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1230 } else { 1231 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1232 } 1233 } 1234 hfp_hf_call_status = new_hf_call_status; 1235 } 1236 hfp_connection->ag_indicators[i].status_changed = 0; 1237 hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1238 break; 1239 } 1240 } 1241 } 1242 1243 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1244 int value; 1245 int i; 1246 bool event_emited; 1247 1248 switch (hfp_connection->command){ 1249 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1250 hfp_connection->command = HFP_CMD_NONE; 1251 hfp_hf_emit_subscriber_information(hfp_connection, 0); 1252 break; 1253 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1254 hfp_connection->command = HFP_CMD_NONE; 1255 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1256 break; 1257 case HFP_CMD_LIST_CURRENT_CALLS: 1258 hfp_connection->command = HFP_CMD_NONE; 1259 hfp_hf_emit_enhanced_call_status(hfp_connection); 1260 break; 1261 case HFP_CMD_SET_SPEAKER_GAIN: 1262 hfp_connection->command = HFP_CMD_NONE; 1263 value = btstack_atoi((char*)hfp_connection->line_buffer); 1264 hfp_hf_speaker_gain = value; 1265 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1266 break; 1267 case HFP_CMD_SET_MICROPHONE_GAIN: 1268 hfp_connection->command = HFP_CMD_NONE; 1269 value = btstack_atoi((char*)hfp_connection->line_buffer); 1270 hfp_hf_microphone_gain = value; 1271 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1272 break; 1273 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1274 hfp_connection->command = HFP_CMD_NONE; 1275 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1276 break; 1277 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1278 hfp_connection->command = HFP_CMD_NONE; 1279 hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION); 1280 break; 1281 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1282 hfp_connection->command = HFP_CMD_NONE; 1283 hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION); 1284 break; 1285 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1286 hfp_connection->command = HFP_CMD_NONE; 1287 hfp_connection->ok_pending = 0; 1288 hfp_connection->extended_audio_gateway_error = 0; 1289 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1290 break; 1291 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1292 break; 1293 case HFP_CMD_ERROR: 1294 switch (hfp_connection->state){ 1295 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1296 switch (hfp_connection->codecs_state){ 1297 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1298 hfp_reset_context_flags(hfp_connection); 1299 hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 1300 return; 1301 default: 1302 break; 1303 } 1304 break; 1305 default: 1306 break; 1307 } 1308 1309 // handle error response for voice activation (HF initiated) 1310 switch(hfp_connection->vra_state_requested){ 1311 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1312 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1313 break; 1314 default: 1315 if (hfp_connection->vra_state_requested == hfp_connection->vra_state){ 1316 break; 1317 } 1318 hfp_connection->vra_state_requested = hfp_connection->vra_state; 1319 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1320 hfp_reset_context_flags(hfp_connection); 1321 return; 1322 } 1323 event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 1324 if (!event_emited){ 1325 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1326 } 1327 hfp_reset_context_flags(hfp_connection); 1328 break; 1329 1330 case HFP_CMD_OK: 1331 hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS); 1332 break; 1333 case HFP_CMD_RING: 1334 hfp_connection->command = HFP_CMD_NONE; 1335 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1336 break; 1337 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1338 hfp_connection->command = HFP_CMD_NONE; 1339 hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1340 break; 1341 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1342 hfp_connection->command = HFP_CMD_NONE; 1343 // report status after SLC established 1344 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1345 break; 1346 } 1347 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1348 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]); 1349 } 1350 break; 1351 case HFP_CMD_AG_SUGGESTED_CODEC: 1352 hfp_connection->command = HFP_CMD_NONE; 1353 hfp_hf_handle_suggested_codec(hfp_connection); 1354 break; 1355 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1356 hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE)); 1357 default: 1358 break; 1359 } 1360 } 1361 1362 static int hfp_parser_is_end_of_line(uint8_t byte){ 1363 return (byte == '\n') || (byte == '\r'); 1364 } 1365 1366 static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 1367 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1368 if (size < 1) return; 1369 1370 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1371 #ifdef ENABLE_HFP_AT_MESSAGES 1372 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1373 #endif 1374 1375 // process messages byte-wise 1376 uint8_t pos; 1377 for (pos = 0; pos < size; pos++){ 1378 hfp_parse(hfp_connection, packet[pos], 1); 1379 // parse until end of line "\r" or "\n" 1380 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1381 hfp_hf_handle_rfcomm_command(hfp_connection); 1382 } 1383 } 1384 1385 static void hfp_hf_run(void){ 1386 btstack_linked_list_iterator_t it; 1387 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1388 while (btstack_linked_list_iterator_has_next(&it)){ 1389 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1390 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1391 hfp_hf_run_for_context(hfp_connection); 1392 } 1393 } 1394 1395 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1396 hfp_connection_t * hfp_connection; 1397 switch (packet_type){ 1398 case RFCOMM_DATA_PACKET: 1399 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1400 if (!hfp_connection) return; 1401 hfp_hf_handle_rfcomm_data(hfp_connection, packet, size); 1402 hfp_hf_run_for_context(hfp_connection); 1403 return; 1404 case HCI_EVENT_PACKET: 1405 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1406 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1407 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1408 return; 1409 } 1410 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1411 break; 1412 default: 1413 break; 1414 } 1415 hfp_hf_run(); 1416 } 1417 1418 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1419 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1420 hfp_hf_run(); 1421 } 1422 1423 static void hfp_hf_set_defaults(void){ 1424 hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1425 hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1426 hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1427 hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1428 hfp_hf_codecs_nr = 0; 1429 hfp_hf_speaker_gain = 9; 1430 hfp_hf_microphone_gain = 9; 1431 hfp_hf_indicators_nr = 0; 1432 } 1433 1434 uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){ 1435 uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1436 if (status != ERROR_CODE_SUCCESS){ 1437 return status; 1438 } 1439 1440 hfp_init(); 1441 hfp_hf_set_defaults(); 1442 1443 hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 1444 hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 1445 1446 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1447 hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1448 return ERROR_CODE_SUCCESS; 1449 } 1450 1451 void hfp_hf_deinit(void){ 1452 hfp_deinit(); 1453 hfp_hf_set_defaults(); 1454 1455 hfp_hf_callback = NULL; 1456 (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1457 (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number)); 1458 } 1459 1460 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){ 1461 btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS); 1462 1463 hfp_hf_codecs_nr = codecs_nr; 1464 int i; 1465 for (i=0; i<codecs_nr; i++){ 1466 hfp_hf_codecs[i] = codecs[i]; 1467 } 1468 } 1469 1470 void hfp_hf_init_supported_features(uint32_t supported_features){ 1471 hfp_hf_supported_features = supported_features; 1472 } 1473 1474 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){ 1475 btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS); 1476 1477 hfp_hf_indicators_nr = indicators_nr; 1478 int i; 1479 for (i = 0; i < hfp_hf_indicators_nr ; i++){ 1480 hfp_hf_indicators[i] = indicators[i]; 1481 } 1482 } 1483 1484 uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1485 return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1486 } 1487 1488 uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1489 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1490 if (!hfp_connection){ 1491 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1492 } 1493 hfp_trigger_release_service_level_connection(hfp_connection); 1494 hfp_hf_run_for_context(hfp_connection); 1495 return ERROR_CODE_SUCCESS; 1496 } 1497 1498 static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1499 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1500 if (!hfp_connection) { 1501 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1502 } 1503 hfp_connection->enable_status_update_for_ag_indicators = enable; 1504 hfp_hf_run_for_context(hfp_connection); 1505 return ERROR_CODE_SUCCESS; 1506 } 1507 1508 uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1509 return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1510 } 1511 1512 uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1513 return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1514 } 1515 1516 // TODO: returned ERROR - wrong format 1517 uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1518 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1519 if (!hfp_connection) { 1520 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1521 } 1522 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1523 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1524 hfp_hf_run_for_context(hfp_connection); 1525 return ERROR_CODE_SUCCESS; 1526 } 1527 1528 uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 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 1534 switch (hfp_connection->hf_query_operator_state){ 1535 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1536 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1537 break; 1538 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1539 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1540 break; 1541 default: 1542 return ERROR_CODE_COMMAND_DISALLOWED; 1543 } 1544 hfp_hf_run_for_context(hfp_connection); 1545 return ERROR_CODE_SUCCESS; 1546 } 1547 1548 static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1549 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1550 if (!hfp_connection) { 1551 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1552 } 1553 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1554 hfp_hf_run_for_context(hfp_connection); 1555 return ERROR_CODE_SUCCESS; 1556 } 1557 1558 1559 uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1560 return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1561 } 1562 1563 uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1564 return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1565 } 1566 1567 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1568 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4)); 1569 } 1570 1571 uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1572 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1573 if (!hfp_connection) { 1574 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1575 } 1576 1577 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1578 return ERROR_CODE_COMMAND_DISALLOWED; 1579 } 1580 1581 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 1582 return ERROR_CODE_COMMAND_DISALLOWED; 1583 } 1584 if (has_codec_negotiation_feature(hfp_connection)) { 1585 switch (hfp_connection->codecs_state) { 1586 case HFP_CODECS_W4_AG_COMMON_CODEC: 1587 break; 1588 case HFP_CODECS_EXCHANGED: 1589 hfp_connection->trigger_codec_exchange = 1; 1590 break; 1591 default: 1592 hfp_connection->codec_confirmed = 0; 1593 hfp_connection->suggested_codec = 0; 1594 hfp_connection->negotiated_codec = 0; 1595 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 1596 hfp_connection->trigger_codec_exchange = 1; 1597 break; 1598 } 1599 } else { 1600 log_info("no codec negotiation feature, use CVSD"); 1601 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1602 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1603 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1604 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1605 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1606 hfp_connection->establish_audio_connection = 1; 1607 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1608 } 1609 1610 hfp_hf_run_for_context(hfp_connection); 1611 return ERROR_CODE_SUCCESS; 1612 } 1613 1614 uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1615 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1616 if (!hfp_connection) { 1617 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1618 } 1619 if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 1620 return ERROR_CODE_COMMAND_DISALLOWED; 1621 } 1622 uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 1623 if (status == ERROR_CODE_SUCCESS){ 1624 hfp_hf_run_for_context(hfp_connection); 1625 } 1626 return ERROR_CODE_SUCCESS; 1627 } 1628 1629 uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1630 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1631 if (!hfp_connection) { 1632 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1633 } 1634 1635 if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1636 hfp_connection->hf_answer_incoming_call = 1; 1637 hfp_hf_run_for_context(hfp_connection); 1638 } else { 1639 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status); 1640 return ERROR_CODE_COMMAND_DISALLOWED; 1641 } 1642 return ERROR_CODE_SUCCESS; 1643 } 1644 1645 uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1646 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1647 if (!hfp_connection) { 1648 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1649 } 1650 hfp_connection->hf_send_chup = 1; 1651 hfp_hf_run_for_context(hfp_connection); 1652 return ERROR_CODE_SUCCESS; 1653 } 1654 1655 uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1656 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1657 if (!hfp_connection) { 1658 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1659 } 1660 1661 if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1662 hfp_connection->hf_send_chup = 1; 1663 hfp_hf_run_for_context(hfp_connection); 1664 } 1665 return ERROR_CODE_SUCCESS; 1666 } 1667 1668 uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1669 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1670 if (!hfp_connection) { 1671 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1672 } 1673 1674 if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1675 hfp_connection->hf_send_chld_0 = 1; 1676 hfp_hf_run_for_context(hfp_connection); 1677 } 1678 return ERROR_CODE_SUCCESS; 1679 } 1680 1681 uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1682 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1683 if (!hfp_connection) { 1684 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1685 } 1686 1687 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1688 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1689 hfp_connection->hf_send_chld_1 = 1; 1690 hfp_hf_run_for_context(hfp_connection); 1691 } 1692 return ERROR_CODE_SUCCESS; 1693 } 1694 1695 uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1696 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1697 if (!hfp_connection) { 1698 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1699 } 1700 1701 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1702 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1703 hfp_connection->hf_send_chld_2 = 1; 1704 hfp_hf_run_for_context(hfp_connection); 1705 } 1706 return ERROR_CODE_SUCCESS; 1707 } 1708 1709 uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1710 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1711 if (!hfp_connection) { 1712 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1713 } 1714 1715 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1716 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1717 hfp_connection->hf_send_chld_3 = 1; 1718 hfp_hf_run_for_context(hfp_connection); 1719 } 1720 return ERROR_CODE_SUCCESS; 1721 } 1722 1723 uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1724 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1725 if (!hfp_connection) { 1726 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1727 } 1728 1729 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1730 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1731 hfp_connection->hf_send_chld_4 = 1; 1732 hfp_hf_run_for_context(hfp_connection); 1733 } 1734 return ERROR_CODE_SUCCESS; 1735 } 1736 1737 uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1738 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1739 if (!hfp_connection) { 1740 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1741 } 1742 1743 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1744 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1745 hfp_connection->hf_send_chld_x = 1; 1746 hfp_connection->hf_send_chld_x_index = 10 + index; 1747 hfp_hf_run_for_context(hfp_connection); 1748 } 1749 return ERROR_CODE_SUCCESS; 1750 } 1751 1752 uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1753 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1754 if (!hfp_connection) { 1755 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1756 } 1757 1758 if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1759 (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1760 hfp_connection->hf_send_chld_x = 1; 1761 hfp_connection->hf_send_chld_x_index = 20 + index; 1762 hfp_hf_run_for_context(hfp_connection); 1763 } 1764 return ERROR_CODE_SUCCESS; 1765 } 1766 1767 uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1768 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1769 if (!hfp_connection) { 1770 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1771 } 1772 1773 hfp_connection->hf_initiate_outgoing_call = 1; 1774 snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number); 1775 hfp_hf_run_for_context(hfp_connection); 1776 return ERROR_CODE_SUCCESS; 1777 } 1778 1779 uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1780 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1781 if (!hfp_connection) { 1782 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1783 } 1784 1785 hfp_connection->hf_initiate_memory_dialing = 1; 1786 hfp_connection->memory_id = memory_id; 1787 1788 hfp_hf_run_for_context(hfp_connection); 1789 return ERROR_CODE_SUCCESS; 1790 } 1791 1792 uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1793 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1794 if (!hfp_connection) { 1795 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1796 } 1797 1798 hfp_connection->hf_initiate_redial_last_number = 1; 1799 hfp_hf_run_for_context(hfp_connection); 1800 return ERROR_CODE_SUCCESS; 1801 } 1802 1803 uint8_t hfp_hf_activate_call_waiting_notification(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_activate_call_waiting_notification = 1; 1810 hfp_hf_run_for_context(hfp_connection); 1811 return ERROR_CODE_SUCCESS; 1812 } 1813 1814 1815 uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1816 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1817 if (!hfp_connection) { 1818 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1819 } 1820 1821 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1822 hfp_hf_run_for_context(hfp_connection); 1823 return ERROR_CODE_SUCCESS; 1824 } 1825 1826 1827 uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1828 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1829 if (!hfp_connection) { 1830 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1831 } 1832 1833 hfp_connection->hf_activate_calling_line_notification = 1; 1834 hfp_hf_run_for_context(hfp_connection); 1835 return ERROR_CODE_SUCCESS; 1836 } 1837 1838 uint8_t hfp_hf_deactivate_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_deactivate_calling_line_notification = 1; 1845 hfp_hf_run_for_context(hfp_connection); 1846 return ERROR_CODE_SUCCESS; 1847 } 1848 1849 static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){ 1850 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION); 1851 int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION); 1852 return hf && ag; 1853 } 1854 1855 uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(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 if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){ 1861 return ERROR_CODE_COMMAND_DISALLOWED; 1862 } 1863 1864 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1865 hfp_hf_run_for_context(hfp_connection); 1866 return ERROR_CODE_SUCCESS; 1867 } 1868 1869 uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){ 1870 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1871 if (!hfp_connection) { 1872 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1873 } 1874 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1875 return ERROR_CODE_COMMAND_DISALLOWED; 1876 } 1877 1878 bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1879 bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1880 if (!enhanced_vra_supported && !legacy_vra_supported){ 1881 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1882 } 1883 1884 switch (hfp_connection->vra_state){ 1885 case HFP_VRA_VOICE_RECOGNITION_OFF: 1886 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1887 hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1888 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1889 hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 1890 break; 1891 case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1892 hfp_connection->activate_voice_recognition = true; 1893 break; 1894 default: 1895 return ERROR_CODE_COMMAND_DISALLOWED; 1896 } 1897 1898 hfp_hf_run_for_context(hfp_connection); 1899 return ERROR_CODE_SUCCESS; 1900 } 1901 1902 uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 1903 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1904 if (!hfp_connection) { 1905 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1906 } 1907 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 1908 return ERROR_CODE_COMMAND_DISALLOWED; 1909 } 1910 1911 bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1912 if (!enhanced_vra_supported){ 1913 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1914 } 1915 1916 switch (hfp_connection->vra_state){ 1917 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1918 case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1919 hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1920 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1921 break; 1922 default: 1923 return ERROR_CODE_COMMAND_DISALLOWED; 1924 } 1925 1926 hfp_hf_run_for_context(hfp_connection); 1927 return ERROR_CODE_SUCCESS; 1928 } 1929 1930 1931 uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 1932 // return deactivate_voice_recognition(acl_handle, false); 1933 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1934 if (!hfp_connection) { 1935 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1936 } 1937 1938 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 1939 hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 1940 return ERROR_CODE_COMMAND_DISALLOWED; 1941 } 1942 1943 bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection); 1944 bool legacy_vra_supported = hfp_hf_vra_flag_supported(hfp_connection); 1945 if (!enhanced_vra_supported && !legacy_vra_supported){ 1946 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1947 } 1948 1949 switch (hfp_connection->vra_state){ 1950 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 1951 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 1952 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1953 case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1954 hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION; 1955 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1956 break; 1957 1958 case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED: 1959 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 1960 hfp_connection->deactivate_voice_recognition = true; 1961 break; 1962 1963 case HFP_VRA_VOICE_RECOGNITION_OFF: 1964 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 1965 case HFP_VRA_W4_VOICE_RECOGNITION_OFF: 1966 default: 1967 return ERROR_CODE_COMMAND_DISALLOWED; 1968 } 1969 1970 hfp_hf_run_for_context(hfp_connection); 1971 return ERROR_CODE_SUCCESS; 1972 } 1973 1974 uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1975 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1976 if (!hfp_connection) { 1977 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1978 } 1979 1980 if (hfp_connection->microphone_gain == gain) { 1981 return ERROR_CODE_SUCCESS; 1982 } 1983 1984 if ((gain < 0) || (gain > 15)){ 1985 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1986 return ERROR_CODE_COMMAND_DISALLOWED; 1987 } 1988 1989 hfp_connection->microphone_gain = gain; 1990 hfp_connection->send_microphone_gain = 1; 1991 hfp_hf_run_for_context(hfp_connection); 1992 return ERROR_CODE_SUCCESS; 1993 } 1994 1995 uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1996 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1997 if (!hfp_connection) { 1998 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1999 } 2000 2001 if (hfp_connection->speaker_gain == gain){ 2002 return ERROR_CODE_SUCCESS; 2003 } 2004 2005 if ((gain < 0) || (gain > 15)){ 2006 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 2007 return ERROR_CODE_COMMAND_DISALLOWED; 2008 } 2009 2010 hfp_connection->speaker_gain = gain; 2011 hfp_connection->send_speaker_gain = 1; 2012 hfp_hf_run_for_context(hfp_connection); 2013 return ERROR_CODE_SUCCESS; 2014 } 2015 2016 uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 2017 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2018 if (!hfp_connection) { 2019 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2020 } 2021 hfp_connection->hf_send_dtmf_code = code; 2022 hfp_hf_run_for_context(hfp_connection); 2023 return ERROR_CODE_SUCCESS; 2024 } 2025 2026 uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 2027 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2028 if (!hfp_connection) { 2029 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2030 } 2031 hfp_connection->hf_send_binp = 1; 2032 hfp_hf_run_for_context(hfp_connection); 2033 return ERROR_CODE_SUCCESS; 2034 } 2035 2036 uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 2037 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2038 if (!hfp_connection) { 2039 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2040 } 2041 hfp_connection->hf_send_clcc = 1; 2042 hfp_hf_run_for_context(hfp_connection); 2043 return ERROR_CODE_SUCCESS; 2044 } 2045 2046 2047 uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 2048 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2049 if (!hfp_connection) { 2050 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2051 } 2052 hfp_connection->hf_send_rrh = 1; 2053 hfp_connection->hf_send_rrh_command = '?'; 2054 hfp_hf_run_for_context(hfp_connection); 2055 return ERROR_CODE_SUCCESS; 2056 } 2057 2058 uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 2059 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2060 if (!hfp_connection) { 2061 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2062 } 2063 hfp_connection->hf_send_rrh = 1; 2064 hfp_connection->hf_send_rrh_command = '0'; 2065 hfp_hf_run_for_context(hfp_connection); 2066 return ERROR_CODE_SUCCESS; 2067 } 2068 2069 uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 2070 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2071 if (!hfp_connection) { 2072 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2073 } 2074 hfp_connection->hf_send_rrh = 1; 2075 hfp_connection->hf_send_rrh_command = '1'; 2076 hfp_hf_run_for_context(hfp_connection); 2077 return ERROR_CODE_SUCCESS; 2078 } 2079 2080 uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 2081 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2082 if (!hfp_connection) { 2083 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2084 } 2085 hfp_connection->hf_send_rrh = 1; 2086 hfp_connection->hf_send_rrh_command = '2'; 2087 hfp_hf_run_for_context(hfp_connection); 2088 return ERROR_CODE_SUCCESS; 2089 } 2090 2091 uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 2092 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2093 if (!hfp_connection) { 2094 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2095 } 2096 hfp_connection->hf_send_cnum = 1; 2097 hfp_hf_run_for_context(hfp_connection); 2098 return ERROR_CODE_SUCCESS; 2099 } 2100 2101 uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 2102 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2103 if (!hfp_connection) { 2104 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2105 } 2106 // find index for assigned number 2107 int i; 2108 for (i = 0; i < hfp_hf_indicators_nr ; i++){ 2109 if (hfp_hf_indicators[i] == assigned_number){ 2110 // set value 2111 hfp_hf_indicators_value[i] = value; 2112 // mark for update 2113 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 2114 hfp_connection->generic_status_update_bitmap |= (1<<i); 2115 // send update 2116 hfp_hf_run_for_context(hfp_connection); 2117 } 2118 return ERROR_CODE_SUCCESS; 2119 } 2120 } 2121 return ERROR_CODE_SUCCESS; 2122 } 2123 2124 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 2125 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 2126 if (!hfp_connection) { 2127 return 0; 2128 } 2129 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 2130 } 2131 2132 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){ 2133 if (!name){ 2134 name = hfp_hf_default_service_name; 2135 } 2136 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 2137 2138 // Construct SupportedFeatures for SDP bitmap: 2139 // 2140 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 2141 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 2142 // 2143 // Wide band speech (bit 5) requires Codec negotiation 2144 // 2145 uint16_t sdp_features = supported_features & 0x1f; 2146 if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 2147 sdp_features |= 1 << 5; 2148 } 2149 2150 if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 2151 sdp_features |= 1 << 6; 2152 } 2153 2154 if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){ 2155 sdp_features |= 1 << 7; 2156 } 2157 2158 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 2159 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 2160 } 2161 2162 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 2163 btstack_assert(callback != NULL); 2164 2165 hfp_hf_callback = callback; 2166 hfp_set_hf_callback(callback); 2167 } 2168