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