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 <string.h> 50 51 #include "bluetooth_sdp.h" 52 #include "btstack_debug.h" 53 #include "btstack_event.h" 54 #include "btstack_memory.h" 55 #include "btstack_run_loop.h" 56 #include "classic/core.h" 57 #include "classic/hfp.h" 58 #include "classic/hfp_hf.h" 59 #include "classic/sdp_client_rfcomm.h" 60 #include "classic/sdp_server.h" 61 #include "classic/sdp_util.h" 62 #include "hci.h" 63 #include "hci_cmd.h" 64 #include "hci_dump.h" 65 #include "l2cap.h" 66 67 static btstack_packet_callback_registration_t hci_event_callback_registration; 68 69 static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 70 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 71 static uint8_t hfp_codecs_nr = 0; 72 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 73 74 static uint8_t hfp_indicators_nr = 0; 75 static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS]; 76 static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS]; 77 78 static uint8_t hfp_hf_speaker_gain = 9; 79 static uint8_t hfp_hf_microphone_gain = 9; 80 81 static btstack_packet_handler_t hfp_hf_callback; 82 83 static hfp_call_status_t hfp_call_status; 84 static hfp_callsetup_status_t hfp_callsetup_status; 85 static hfp_callheld_status_t hfp_callheld_status; 86 87 static char phone_number[25]; 88 89 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 90 btstack_linked_list_iterator_t it; 91 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 92 while (btstack_linked_list_iterator_has_next(&it)){ 93 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 94 if (hfp_connection->acl_handle != handle) continue; 95 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 96 return hfp_connection; 97 } 98 return NULL; 99 } 100 101 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 102 if (callback == NULL){ 103 log_error("hfp_hf_register_packet_handler called with NULL callback"); 104 return; 105 } 106 hfp_hf_callback = callback; 107 hfp_set_hf_callback(callback); 108 } 109 110 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){ 111 if (!callback) return; 112 uint8_t event[31]; 113 event[0] = HCI_EVENT_HFP_META; 114 event[1] = sizeof(event) - 2; 115 event[2] = event_subtype; 116 event[3] = status; 117 event[4] = bnip_type; 118 uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 6); 119 strncpy((char*)&event[5], bnip_number, size); 120 event[5 + size] = 0; 121 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 122 } 123 124 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){ 125 if (!callback) return; 126 uint8_t event[30]; 127 event[0] = HCI_EVENT_HFP_META; 128 event[1] = sizeof(event) - 2; 129 event[2] = event_subtype; 130 event[3] = bnip_type; 131 uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 5); 132 strncpy((char*)&event[4], bnip_number, size); 133 event[4 + size] = 0; 134 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 135 } 136 137 static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, hfp_connection_t * connection){ 138 if (!callback) return; 139 uint8_t event[36]; 140 int pos = 0; 141 event[pos++] = HCI_EVENT_HFP_META; 142 event[pos++] = sizeof(event) - 2; 143 event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 144 event[pos++] = connection->clcc_idx; 145 event[pos++] = connection->clcc_dir; 146 event[pos++] = connection->clcc_status; 147 event[pos++] = connection->clcc_mode; 148 event[pos++] = connection->clcc_mpty; 149 event[pos++] = connection->bnip_type; 150 uint16_t size = btstack_min(strlen(connection->bnip_number), sizeof(event) - pos); 151 strncpy((char*)&event[pos], connection->bnip_number, size); 152 pos += size; 153 event[pos++] = 0; 154 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 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, (char *) "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 if (hfp_connection->release_audio_connection){ 549 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 550 hfp_connection->release_audio_connection = 0; 551 gap_disconnect(hfp_connection->sco_handle); 552 return 1; 553 } 554 555 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 556 557 // run codecs exchange 558 int done = codecs_exchange_state_machine(hfp_connection); 559 if (done) return 1; 560 561 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 562 if (hfp_connection->establish_audio_connection){ 563 hfp_connection->state = HFP_W4_SCO_CONNECTED; 564 hfp_connection->establish_audio_connection = 0; 565 hfp_setup_synchronous_connection(hfp_connection); 566 return 1; 567 } 568 569 return 0; 570 } 571 572 573 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 574 if (hfp_connection->hf_answer_incoming_call){ 575 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 576 hfp_connection->hf_answer_incoming_call = 0; 577 return 1; 578 } 579 return 0; 580 } 581 582 static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 583 if (!hfp_connection) return; 584 if (!hfp_connection->rfcomm_cid) return; 585 586 if (hfp_connection->local_role != HFP_ROLE_HF) { 587 log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role); 588 return; 589 } 590 591 if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 592 593 bool eSCO = hfp_connection->hf_accept_sco == 2; 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 (eSCO && 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 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 959 break; 960 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 961 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 962 hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 963 break; 964 default: 965 break; 966 } 967 968 if (hfp_connection->enable_extended_audio_gateway_error_report){ 969 hfp_connection->enable_extended_audio_gateway_error_report = 0; 970 break; 971 } 972 973 switch (hfp_connection->codecs_state){ 974 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 975 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 976 break; 977 case HFP_CODECS_HF_CONFIRMED_CODEC: 978 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 979 break; 980 default: 981 break; 982 } 983 break; 984 default: 985 break; 986 } 987 988 // done 989 hfp_connection->command = HFP_CMD_NONE; 990 } 991 992 static int hfp_parser_is_end_of_line(uint8_t byte){ 993 return (byte == '\n') || (byte == '\r'); 994 } 995 996 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 997 uint16_t i; 998 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 999 if (hfp_connection->ag_indicators[i].status_changed) { 1000 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1001 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1002 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1003 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1004 // avoid set but not used warning 1005 (void) hfp_callheld_status; 1006 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1007 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1008 } 1009 hfp_connection->ag_indicators[i].status_changed = 0; 1010 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1011 break; 1012 } 1013 } 1014 } 1015 1016 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1017 int value; 1018 int i; 1019 switch (hfp_connection->command){ 1020 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1021 hfp_connection->command = HFP_CMD_NONE; 1022 hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1023 break; 1024 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1025 hfp_connection->command = HFP_CMD_NONE; 1026 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1027 break; 1028 case HFP_CMD_LIST_CURRENT_CALLS: 1029 hfp_connection->command = HFP_CMD_NONE; 1030 hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection); 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 hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1076 break; 1077 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1078 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1079 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1080 } 1081 hfp_connection->command = HFP_CMD_NONE; 1082 break; 1083 default: 1084 break; 1085 } 1086 } 1087 1088 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1089 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1090 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1091 if (size < 1) return; 1092 1093 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1094 if (!hfp_connection) return; 1095 1096 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1097 1098 // process messages byte-wise 1099 int pos; 1100 for (pos = 0; pos < size; pos++){ 1101 hfp_parse(hfp_connection, packet[pos], 1); 1102 1103 // parse until end of line "\r\n" 1104 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1105 1106 hfp_hf_handle_rfcomm_command(hfp_connection); 1107 } 1108 hfp_run_for_context(hfp_connection); 1109 } 1110 1111 static void hfp_run(void){ 1112 btstack_linked_list_iterator_t it; 1113 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1114 while (btstack_linked_list_iterator_has_next(&it)){ 1115 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1116 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1117 hfp_run_for_context(hfp_connection); 1118 } 1119 } 1120 1121 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1122 switch (packet_type){ 1123 case RFCOMM_DATA_PACKET: 1124 hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 1125 break; 1126 case HCI_EVENT_PACKET: 1127 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1128 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1129 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1130 return; 1131 } 1132 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1133 break; 1134 default: 1135 break; 1136 } 1137 hfp_run(); 1138 } 1139 1140 static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1141 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1142 1143 // allow for sco established -> ring transition and sco retry 1144 if (packet_type != HCI_EVENT_PACKET) return; 1145 if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 1146 hfp_run(); 1147 } 1148 1149 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1150 hfp_init(); 1151 1152 hci_event_callback_registration.callback = &hci_event_packet_handler; 1153 hci_add_event_handler(&hci_event_callback_registration); 1154 1155 rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1156 1157 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1158 hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler); 1159 1160 hfp_set_hf_run_for_context(hfp_run_for_context); 1161 1162 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1163 hfp_codecs_nr = 0; 1164 hfp_indicators_nr = 0; 1165 hfp_hf_speaker_gain = 9; 1166 hfp_hf_microphone_gain = 9; 1167 } 1168 1169 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 1170 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1171 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1172 return; 1173 } 1174 1175 hfp_codecs_nr = codecs_nr; 1176 int i; 1177 for (i=0; i<codecs_nr; i++){ 1178 hfp_codecs[i] = codecs[i]; 1179 } 1180 } 1181 1182 void hfp_hf_init_supported_features(uint32_t supported_features){ 1183 hfp_supported_features = supported_features; 1184 } 1185 1186 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 1187 hfp_indicators_nr = indicators_nr; 1188 int i; 1189 for (i = 0; i < hfp_indicators_nr ; i++){ 1190 hfp_indicators[i] = indicators[i]; 1191 } 1192 } 1193 1194 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1195 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1196 } 1197 1198 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1199 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1200 if (!hfp_connection) { 1201 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1202 return; 1203 } 1204 hfp_release_service_level_connection(hfp_connection); 1205 hfp_run_for_context(hfp_connection); 1206 } 1207 1208 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1209 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1210 if (!hfp_connection) { 1211 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1212 return; 1213 } 1214 hfp_connection->enable_status_update_for_ag_indicators = enable; 1215 hfp_run_for_context(hfp_connection); 1216 } 1217 1218 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1219 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1220 } 1221 1222 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1223 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1224 } 1225 1226 // TODO: returned ERROR - wrong format 1227 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1228 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1229 if (!hfp_connection) { 1230 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1231 return; 1232 } 1233 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1234 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1235 hfp_run_for_context(hfp_connection); 1236 } 1237 1238 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1239 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1240 if (!hfp_connection) { 1241 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1242 return; 1243 } 1244 switch (hfp_connection->hf_query_operator_state){ 1245 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1246 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1247 break; 1248 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1249 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1250 break; 1251 default: 1252 break; 1253 } 1254 hfp_run_for_context(hfp_connection); 1255 } 1256 1257 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1258 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1259 if (!hfp_connection) { 1260 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1261 return; 1262 } 1263 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1264 hfp_run_for_context(hfp_connection); 1265 } 1266 1267 1268 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1269 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1270 } 1271 1272 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1273 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1274 } 1275 1276 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1277 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 1278 } 1279 1280 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1281 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1282 if (!hfp_connection) { 1283 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1284 return; 1285 } 1286 hfp_connection->establish_audio_connection = 0; 1287 1288 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1289 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1290 1291 hfp_connection->trigger_codec_exchange = 0; 1292 hfp_connection->establish_audio_connection = 1; 1293 if (!has_codec_negotiation_feature(hfp_connection)){ 1294 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1295 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1296 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1297 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1298 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1299 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1300 hfp_connection->trigger_codec_exchange = 0; 1301 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1302 } else { 1303 switch (hfp_connection->codecs_state){ 1304 case HFP_CODECS_W4_AG_COMMON_CODEC: 1305 break; 1306 default: 1307 hfp_connection->trigger_codec_exchange = 1; 1308 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1309 break; 1310 } 1311 } 1312 1313 hfp_run_for_context(hfp_connection); 1314 } 1315 1316 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1317 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1318 if (!hfp_connection) { 1319 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1320 return; 1321 } 1322 hfp_release_audio_connection(hfp_connection); 1323 hfp_run_for_context(hfp_connection); 1324 } 1325 1326 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1327 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1328 if (!hfp_connection) { 1329 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1330 return; 1331 } 1332 1333 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1334 hfp_connection->hf_answer_incoming_call = 1; 1335 hfp_run_for_context(hfp_connection); 1336 } else { 1337 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1338 } 1339 } 1340 1341 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1342 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1343 if (!hfp_connection) { 1344 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1345 return; 1346 } 1347 hfp_connection->hf_send_chup = 1; 1348 hfp_run_for_context(hfp_connection); 1349 } 1350 1351 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1352 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1353 if (!hfp_connection) { 1354 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1355 return; 1356 } 1357 1358 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1359 hfp_connection->hf_send_chup = 1; 1360 hfp_run_for_context(hfp_connection); 1361 } 1362 } 1363 1364 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1365 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1366 if (!hfp_connection) { 1367 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1368 return; 1369 } 1370 1371 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1372 hfp_connection->hf_send_chld_0 = 1; 1373 hfp_run_for_context(hfp_connection); 1374 } 1375 } 1376 1377 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1378 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1379 if (!hfp_connection) { 1380 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1381 return; 1382 } 1383 1384 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1385 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1386 hfp_connection->hf_send_chld_1 = 1; 1387 hfp_run_for_context(hfp_connection); 1388 } 1389 } 1390 1391 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1392 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1393 if (!hfp_connection) { 1394 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1395 return; 1396 } 1397 1398 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1399 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1400 hfp_connection->hf_send_chld_2 = 1; 1401 hfp_run_for_context(hfp_connection); 1402 } 1403 } 1404 1405 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1406 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1407 if (!hfp_connection) { 1408 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1409 return; 1410 } 1411 1412 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1413 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1414 hfp_connection->hf_send_chld_3 = 1; 1415 hfp_run_for_context(hfp_connection); 1416 } 1417 } 1418 1419 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1420 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1421 if (!hfp_connection) { 1422 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1423 return; 1424 } 1425 1426 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1427 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1428 hfp_connection->hf_send_chld_4 = 1; 1429 hfp_run_for_context(hfp_connection); 1430 } 1431 } 1432 1433 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1434 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1435 if (!hfp_connection) { 1436 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1437 return; 1438 } 1439 1440 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1441 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1442 hfp_connection->hf_send_chld_x = 1; 1443 hfp_connection->hf_send_chld_x_index = 10 + index; 1444 hfp_run_for_context(hfp_connection); 1445 } 1446 } 1447 1448 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1449 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1450 if (!hfp_connection) { 1451 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1452 return; 1453 } 1454 1455 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1456 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1457 hfp_connection->hf_send_chld_x = 1; 1458 hfp_connection->hf_send_chld_x_index = 20 + index; 1459 hfp_run_for_context(hfp_connection); 1460 } 1461 } 1462 1463 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1464 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1465 if (!hfp_connection) { 1466 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1467 return; 1468 } 1469 1470 hfp_connection->hf_initiate_outgoing_call = 1; 1471 snprintf(phone_number, sizeof(phone_number), "%s", number); 1472 hfp_run_for_context(hfp_connection); 1473 } 1474 1475 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1476 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1477 if (!hfp_connection) { 1478 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1479 return; 1480 } 1481 1482 hfp_connection->hf_initiate_memory_dialing = 1; 1483 hfp_connection->memory_id = memory_id; 1484 1485 hfp_run_for_context(hfp_connection); 1486 } 1487 1488 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1489 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1490 if (!hfp_connection) { 1491 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1492 return; 1493 } 1494 1495 hfp_connection->hf_initiate_redial_last_number = 1; 1496 hfp_run_for_context(hfp_connection); 1497 } 1498 1499 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1500 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1501 if (!hfp_connection) { 1502 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1503 return; 1504 } 1505 1506 hfp_connection->hf_activate_call_waiting_notification = 1; 1507 hfp_run_for_context(hfp_connection); 1508 } 1509 1510 1511 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1512 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1513 if (!hfp_connection) { 1514 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1515 return; 1516 } 1517 1518 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1519 hfp_run_for_context(hfp_connection); 1520 } 1521 1522 1523 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1524 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1525 if (!hfp_connection) { 1526 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1527 return; 1528 } 1529 1530 hfp_connection->hf_activate_calling_line_notification = 1; 1531 hfp_run_for_context(hfp_connection); 1532 } 1533 1534 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1535 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1536 if (!hfp_connection) { 1537 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1538 return; 1539 } 1540 1541 hfp_connection->hf_deactivate_calling_line_notification = 1; 1542 hfp_run_for_context(hfp_connection); 1543 } 1544 1545 1546 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1547 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1548 if (!hfp_connection) { 1549 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1550 return; 1551 } 1552 1553 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1554 hfp_run_for_context(hfp_connection); 1555 } 1556 1557 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1558 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1559 if (!hfp_connection) { 1560 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1561 return; 1562 } 1563 1564 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1565 hfp_run_for_context(hfp_connection); 1566 } 1567 1568 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1569 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1570 if (!hfp_connection) { 1571 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1572 return; 1573 } 1574 1575 hfp_connection->hf_activate_voice_recognition_notification = 1; 1576 hfp_run_for_context(hfp_connection); 1577 } 1578 1579 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1580 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1581 if (!hfp_connection) { 1582 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1583 return; 1584 } 1585 1586 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1587 hfp_run_for_context(hfp_connection); 1588 } 1589 1590 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1591 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1592 if (!hfp_connection) { 1593 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1594 return; 1595 } 1596 1597 if (hfp_connection->microphone_gain == gain) return; 1598 if ((gain < 0) || (gain > 15)){ 1599 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1600 return; 1601 } 1602 hfp_connection->microphone_gain = gain; 1603 hfp_connection->send_microphone_gain = 1; 1604 hfp_run_for_context(hfp_connection); 1605 } 1606 1607 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1608 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1609 if (!hfp_connection) { 1610 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1611 return; 1612 } 1613 1614 if (hfp_connection->speaker_gain == gain) return; 1615 if ((gain < 0) || (gain > 15)){ 1616 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1617 return; 1618 } 1619 hfp_connection->speaker_gain = gain; 1620 hfp_connection->send_speaker_gain = 1; 1621 hfp_run_for_context(hfp_connection); 1622 } 1623 1624 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1625 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1626 if (!hfp_connection) { 1627 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1628 return; 1629 } 1630 1631 hfp_connection->hf_send_dtmf_code = code; 1632 hfp_run_for_context(hfp_connection); 1633 } 1634 1635 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1636 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1637 if (!hfp_connection) { 1638 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1639 return; 1640 } 1641 hfp_connection->hf_send_binp = 1; 1642 hfp_run_for_context(hfp_connection); 1643 } 1644 1645 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1646 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1647 if (!hfp_connection) { 1648 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1649 return; 1650 } 1651 hfp_connection->hf_send_clcc = 1; 1652 hfp_run_for_context(hfp_connection); 1653 } 1654 1655 1656 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1657 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1658 if (!hfp_connection) { 1659 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1660 return; 1661 } 1662 hfp_connection->hf_send_rrh = 1; 1663 hfp_connection->hf_send_rrh_command = '?'; 1664 hfp_run_for_context(hfp_connection); 1665 } 1666 1667 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1668 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1669 if (!hfp_connection) { 1670 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1671 return; 1672 } 1673 hfp_connection->hf_send_rrh = 1; 1674 hfp_connection->hf_send_rrh_command = '0'; 1675 hfp_run_for_context(hfp_connection); 1676 } 1677 1678 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1679 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1680 if (!hfp_connection) { 1681 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1682 return; 1683 } 1684 hfp_connection->hf_send_rrh = 1; 1685 hfp_connection->hf_send_rrh_command = '1'; 1686 hfp_run_for_context(hfp_connection); 1687 } 1688 1689 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1690 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1691 if (!hfp_connection) { 1692 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1693 return; 1694 } 1695 hfp_connection->hf_send_rrh = 1; 1696 hfp_connection->hf_send_rrh_command = '2'; 1697 hfp_run_for_context(hfp_connection); 1698 } 1699 1700 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1701 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1702 if (!hfp_connection) { 1703 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1704 return; 1705 } 1706 hfp_connection->hf_send_cnum = 1; 1707 hfp_run_for_context(hfp_connection); 1708 } 1709 1710 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1711 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1712 if (!hfp_connection) { 1713 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1714 return; 1715 } 1716 // find index for assigned number 1717 int i; 1718 for (i = 0; i < hfp_indicators_nr ; i++){ 1719 if (hfp_indicators[i] == assigned_number){ 1720 // set value 1721 hfp_indicators_value[i] = value; 1722 // mark for update 1723 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1724 hfp_connection->generic_status_update_bitmap |= (1<<i); 1725 // send update 1726 hfp_run_for_context(hfp_connection); 1727 } 1728 return; 1729 } 1730 } 1731 } 1732 1733 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1734 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1735 if (!hfp_connection) { 1736 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1737 return 0; 1738 } 1739 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1740 } 1741