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