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