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