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