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