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 // ***************************************************************************** 39 // 40 // HFP Hands-Free (HF) unit 41 // 42 // ***************************************************************************** 43 44 #include "btstack_config.h" 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "btstack_debug.h" 52 #include "btstack_memory.h" 53 #include "btstack_run_loop.h" 54 #include "classic/core.h" 55 #include "classic/hfp.h" 56 #include "classic/hfp_hf.h" 57 #include "classic/sdp_client_rfcomm.h" 58 #include "classic/sdp_server.h" 59 #include "classic/sdp_util.h" 60 #include "hci.h" 61 #include "hci_cmd.h" 62 #include "hci_dump.h" 63 #include "l2cap.h" 64 65 static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 66 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 67 static uint8_t hfp_codecs_nr = 0; 68 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 69 70 static uint8_t hfp_indicators_nr = 0; 71 static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 72 static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 73 74 static uint8_t hfp_hf_speaker_gain = 9; 75 static uint8_t hfp_hf_microphone_gain = 9; 76 77 static btstack_packet_handler_t hfp_callback; 78 79 static hfp_call_status_t hfp_call_status; 80 static hfp_callsetup_status_t hfp_callsetup_status; 81 static hfp_callheld_status_t hfp_callheld_status; 82 83 static char phone_number[25]; 84 85 static btstack_packet_callback_registration_t hci_event_callback_registration; 86 87 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 88 hfp_callback = callback; 89 if (callback == NULL){ 90 log_error("hfp_hf_register_packet_handler called with NULL callback"); 91 return; 92 } 93 hfp_callback = callback; 94 hfp_set_callback(callback); 95 } 96 97 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){ 98 if (!callback) return; 99 uint8_t event[31]; 100 event[0] = HCI_EVENT_HFP_META; 101 event[1] = sizeof(event) - 2; 102 event[2] = event_subtype; 103 event[3] = status; 104 event[4] = bnip_type; 105 int size = (strlen(bnip_number) < sizeof(event) - 6) ? strlen(bnip_number) : sizeof(event) - 6; 106 strncpy((char*)&event[5], bnip_number, size); 107 event[5 + size] = 0; 108 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 109 } 110 111 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){ 112 if (!callback) return; 113 uint8_t event[30]; 114 event[0] = HCI_EVENT_HFP_META; 115 event[1] = sizeof(event) - 2; 116 event[2] = event_subtype; 117 event[3] = bnip_type; 118 int size = (strlen(bnip_number) < sizeof(event) - 5) ? strlen(bnip_number) : sizeof(event) - 5; 119 strncpy((char*)&event[4], bnip_number, size); 120 event[4 + size] = 0; 121 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 122 } 123 124 static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir, 125 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){ 126 if (!callback) return; 127 uint8_t event[35]; 128 event[0] = HCI_EVENT_HFP_META; 129 event[1] = sizeof(event) - 2; 130 event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 131 event[3] = clcc_idx; 132 event[4] = clcc_dir; 133 event[6] = clcc_status; 134 event[7] = clcc_mpty; 135 event[8] = bnip_type; 136 int size = (strlen(bnip_number) < sizeof(event) - 10) ? strlen(bnip_number) : sizeof(event) - 10; 137 strncpy((char*)&event[9], bnip_number, size); 138 event[9 + size] = 0; 139 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 140 } 141 142 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 143 int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 144 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 145 printf("local %d, remote %d\n", hf, ag); 146 return hf && ag; 147 } 148 149 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 150 int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 151 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 152 return hf && ag; 153 } 154 155 156 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 157 int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 158 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 159 return hf && ag; 160 } 161 162 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 163 164 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){ 165 if (!name){ 166 name = default_hfp_hf_service_name; 167 } 168 hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name); 169 170 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 171 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 172 } 173 174 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 175 char buffer[20]; 176 sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 177 // printf("exchange_supported_features %s\n", buffer); 178 return send_str_over_rfcomm(cid, buffer); 179 } 180 181 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 182 char buffer[30]; 183 int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS); 184 offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr); 185 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 186 buffer[offset] = 0; 187 return send_str_over_rfcomm(cid, buffer); 188 } 189 190 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 191 char buffer[20]; 192 sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR); 193 // printf("retrieve_indicators %s\n", buffer); 194 return send_str_over_rfcomm(cid, buffer); 195 } 196 197 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 198 char buffer[20]; 199 sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR); 200 // printf("retrieve_indicators_status %s\n", buffer); 201 return send_str_over_rfcomm(cid, buffer); 202 } 203 204 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 205 char buffer[20]; 206 sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 207 // printf("toggle_indicator_status_update %s\n", buffer); 208 return send_str_over_rfcomm(cid, buffer); 209 } 210 211 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 212 char buffer[50]; 213 int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 214 offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr); 215 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 216 buffer[offset] = 0; 217 return send_str_over_rfcomm(cid, buffer); 218 } 219 220 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 221 char buffer[20]; 222 sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 223 // printf("retrieve_can_hold_call %s\n", buffer); 224 return send_str_over_rfcomm(cid, buffer); 225 } 226 227 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 228 char buffer[30]; 229 int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 230 offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr); 231 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 232 buffer[offset] = 0; 233 return send_str_over_rfcomm(cid, buffer); 234 } 235 236 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 237 char buffer[20]; 238 sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR); 239 // printf("retrieve_supported_generic_status_indicators %s\n", buffer); 240 return send_str_over_rfcomm(cid, buffer); 241 } 242 243 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 244 char buffer[20]; 245 sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR); 246 // printf("list_initital_supported_generic_status_indicators %s\n", buffer); 247 return send_str_over_rfcomm(cid, buffer); 248 } 249 250 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 251 char buffer[20]; 252 sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION); 253 return send_str_over_rfcomm(cid, buffer); 254 } 255 256 static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 257 char buffer[20]; 258 sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION); 259 return send_str_over_rfcomm(cid, buffer); 260 } 261 262 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 263 char buffer[20]; 264 sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 265 return send_str_over_rfcomm(cid, buffer); 266 } 267 268 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 269 char buffer[20]; 270 sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP); 271 return send_str_over_rfcomm(cid, buffer); 272 } 273 274 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 275 char buffer[20]; 276 sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 277 return send_str_over_rfcomm(cid, buffer); 278 } 279 280 static int hfp_hf_cmd_ata(uint16_t cid){ 281 char buffer[10]; 282 sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED); 283 return send_str_over_rfcomm(cid, buffer); 284 } 285 286 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 287 char buffer[40]; 288 sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 289 return send_str_over_rfcomm(cid, buffer); 290 } 291 292 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 293 char buffer[40]; 294 sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 295 return send_str_over_rfcomm(cid, buffer); 296 } 297 298 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 299 char buffer[40]; 300 sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate); 301 return send_str_over_rfcomm(cid, buffer); 302 } 303 304 static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 305 char buffer[40]; 306 sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate); 307 return send_str_over_rfcomm(cid, buffer); 308 } 309 310 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 311 char buffer[40]; 312 sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate); 313 return send_str_over_rfcomm(cid, buffer); 314 } 315 316 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 317 char buffer[40]; 318 sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 319 return send_str_over_rfcomm(cid, buffer); 320 } 321 322 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 323 char buffer[40]; 324 sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 325 return send_str_over_rfcomm(cid, buffer); 326 } 327 328 static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 329 char buffer[40]; 330 sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 331 return send_str_over_rfcomm(cid, buffer); 332 } 333 334 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 335 char buffer[20]; 336 sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER); 337 return send_str_over_rfcomm(cid, buffer); 338 } 339 340 static int hfp_hf_send_chup(uint16_t cid){ 341 char buffer[20]; 342 sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL); 343 return send_str_over_rfcomm(cid, buffer); 344 } 345 346 static int hfp_hf_send_chld(uint16_t cid, int number){ 347 char buffer[20]; 348 sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 349 return send_str_over_rfcomm(cid, buffer); 350 } 351 352 static int hfp_hf_send_dtmf(uint16_t cid, char code){ 353 char buffer[20]; 354 sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 355 return send_str_over_rfcomm(cid, buffer); 356 } 357 358 static int hfp_hf_send_binp(uint16_t cid){ 359 char buffer[20]; 360 sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG); 361 return send_str_over_rfcomm(cid, buffer); 362 } 363 364 static int hfp_hf_send_clcc(uint16_t cid){ 365 char buffer[20]; 366 sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS); 367 return send_str_over_rfcomm(cid, buffer); 368 } 369 370 static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 371 if (!callback) return; 372 uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1]; 373 event[0] = HCI_EVENT_HFP_META; 374 event[1] = sizeof(event) - 2; 375 event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 376 event[3] = indicator.index; 377 event[4] = indicator.status; 378 strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 379 event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 380 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 381 } 382 383 static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 384 if (!callback) return; 385 uint8_t event[24]; 386 event[0] = HCI_EVENT_HFP_META; 387 event[1] = sizeof(event) - 2; 388 event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 389 event[3] = network_operator.mode; 390 event[4] = network_operator.format; 391 strcpy((char*)&event[5], network_operator.name); 392 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 393 } 394 395 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 396 if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 397 if (hfp_connection->ok_pending) return 0; 398 int done = 1; 399 400 switch (hfp_connection->state){ 401 case HFP_EXCHANGE_SUPPORTED_FEATURES: 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_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 525 } else { 526 hfp_connection->codec_confirmed = 0; 527 hfp_connection->suggested_codec = 0; 528 hfp_connection->negotiated_codec = 0; 529 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 530 hfp_connection->ok_pending = 1; 531 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 532 533 } 534 break; 535 } 536 default: 537 break; 538 } 539 return 0; 540 } 541 542 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 543 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 544 hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 545 546 547 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 548 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 549 hfp_connection->release_audio_connection = 0; 550 gap_disconnect(hfp_connection->sco_handle); 551 return 1; 552 } 553 554 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 555 556 // run codecs exchange 557 int done = codecs_exchange_state_machine(hfp_connection); 558 if (done) return 1; 559 560 if (hfp_connection->establish_audio_connection){ 561 hfp_connection->state = HFP_W4_SCO_CONNECTED; 562 hfp_connection->establish_audio_connection = 0; 563 hfp_setup_synchronous_connection(hfp_connection); 564 return 1; 565 } 566 567 return 0; 568 } 569 570 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 571 if (hfp_connection->hf_answer_incoming_call){ 572 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 573 hfp_connection->hf_answer_incoming_call = 0; 574 return 1; 575 } 576 return 0; 577 } 578 579 static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 580 if (!hfp_connection) return; 581 if (!hfp_connection->rfcomm_cid) return; 582 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return; 583 584 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 585 if (!done){ 586 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 587 } 588 if (!done){ 589 done = hfp_hf_run_for_audio_connection(hfp_connection); 590 } 591 if (!done){ 592 done = call_setup_state_machine(hfp_connection); 593 } 594 595 if (hfp_connection->send_microphone_gain){ 596 hfp_connection->send_microphone_gain = 0; 597 hfp_connection->ok_pending = 1; 598 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 599 return; 600 } 601 602 if (hfp_connection->send_speaker_gain){ 603 hfp_connection->send_speaker_gain = 0; 604 hfp_connection->ok_pending = 1; 605 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 606 return; 607 } 608 609 if (hfp_connection->hf_deactivate_calling_line_notification){ 610 hfp_connection->hf_deactivate_calling_line_notification = 0; 611 hfp_connection->ok_pending = 1; 612 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 613 return; 614 } 615 616 if (hfp_connection->hf_activate_calling_line_notification){ 617 hfp_connection->hf_activate_calling_line_notification = 0; 618 hfp_connection->ok_pending = 1; 619 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 620 return; 621 } 622 623 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 624 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 625 hfp_connection->ok_pending = 1; 626 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 627 return; 628 } 629 630 if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 631 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 632 hfp_connection->ok_pending = 1; 633 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 634 return; 635 } 636 637 if (hfp_connection->hf_deactivate_voice_recognition_notification){ 638 hfp_connection->hf_deactivate_voice_recognition_notification = 0; 639 hfp_connection->ok_pending = 1; 640 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 641 return; 642 } 643 644 if (hfp_connection->hf_activate_voice_recognition_notification){ 645 hfp_connection->hf_activate_voice_recognition_notification = 0; 646 hfp_connection->ok_pending = 1; 647 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 648 return; 649 } 650 651 652 if (hfp_connection->hf_deactivate_call_waiting_notification){ 653 hfp_connection->hf_deactivate_call_waiting_notification = 0; 654 hfp_connection->ok_pending = 1; 655 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 656 return; 657 } 658 659 if (hfp_connection->hf_activate_call_waiting_notification){ 660 hfp_connection->hf_activate_call_waiting_notification = 0; 661 hfp_connection->ok_pending = 1; 662 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 663 return; 664 } 665 666 if (hfp_connection->hf_initiate_outgoing_call){ 667 hfp_connection->hf_initiate_outgoing_call = 0; 668 hfp_connection->ok_pending = 1; 669 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 670 return; 671 } 672 673 if (hfp_connection->hf_initiate_memory_dialing){ 674 hfp_connection->hf_initiate_memory_dialing = 0; 675 hfp_connection->ok_pending = 1; 676 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 677 return; 678 } 679 680 if (hfp_connection->hf_initiate_redial_last_number){ 681 hfp_connection->hf_initiate_redial_last_number = 0; 682 hfp_connection->ok_pending = 1; 683 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 684 return; 685 } 686 687 if (hfp_connection->hf_send_chup){ 688 hfp_connection->hf_send_chup = 0; 689 hfp_connection->ok_pending = 1; 690 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 691 return; 692 } 693 694 if (hfp_connection->hf_send_chld_0){ 695 hfp_connection->hf_send_chld_0 = 0; 696 hfp_connection->ok_pending = 1; 697 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 698 return; 699 } 700 701 if (hfp_connection->hf_send_chld_1){ 702 hfp_connection->hf_send_chld_1 = 0; 703 hfp_connection->ok_pending = 1; 704 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 705 return; 706 } 707 708 if (hfp_connection->hf_send_chld_2){ 709 hfp_connection->hf_send_chld_2 = 0; 710 hfp_connection->ok_pending = 1; 711 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 712 return; 713 } 714 715 if (hfp_connection->hf_send_chld_3){ 716 hfp_connection->hf_send_chld_3 = 0; 717 hfp_connection->ok_pending = 1; 718 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 719 return; 720 } 721 722 if (hfp_connection->hf_send_chld_4){ 723 hfp_connection->hf_send_chld_4 = 0; 724 hfp_connection->ok_pending = 1; 725 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 726 return; 727 } 728 729 if (hfp_connection->hf_send_chld_x){ 730 hfp_connection->hf_send_chld_x = 0; 731 hfp_connection->ok_pending = 1; 732 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 733 return; 734 } 735 736 if (hfp_connection->hf_send_dtmf_code){ 737 char code = hfp_connection->hf_send_dtmf_code; 738 hfp_connection->hf_send_dtmf_code = 0; 739 hfp_connection->ok_pending = 1; 740 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 741 return; 742 } 743 744 if (hfp_connection->hf_send_binp){ 745 hfp_connection->hf_send_binp = 0; 746 hfp_connection->ok_pending = 1; 747 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 748 return; 749 } 750 751 if (hfp_connection->hf_send_clcc){ 752 hfp_connection->hf_send_clcc = 0; 753 hfp_connection->ok_pending = 1; 754 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 755 return; 756 } 757 758 if (hfp_connection->hf_send_rrh){ 759 hfp_connection->hf_send_rrh = 0; 760 char buffer[20]; 761 switch (hfp_connection->hf_send_rrh_command){ 762 case '?': 763 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 764 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 765 return; 766 case '0': 767 case '1': 768 case '2': 769 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 770 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 771 return; 772 default: 773 break; 774 } 775 return; 776 } 777 778 if (hfp_connection->hf_send_cnum){ 779 hfp_connection->hf_send_cnum = 0; 780 char buffer[20]; 781 sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 782 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 783 return; 784 } 785 786 // update HF indicators 787 if (hfp_connection->generic_status_update_bitmap){ 788 int i; 789 for (i=0;i<hfp_indicators_nr;i++){ 790 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 791 if (hfp_connection->generic_status_indicators[i].state){ 792 hfp_connection->ok_pending = 1; 793 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 794 char buffer[30]; 795 sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]); 796 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 797 } else { 798 printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]); 799 } 800 return; 801 } 802 } 803 } 804 805 if (done) return; 806 // deal with disconnect 807 switch (hfp_connection->state){ 808 case HFP_W2_DISCONNECT_RFCOMM: 809 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 810 rfcomm_disconnect(hfp_connection->rfcomm_cid); 811 break; 812 813 default: 814 break; 815 } 816 } 817 818 static void hfp_init_link_settings(hfp_connection_t * hfp_connection){ 819 // determine highest possible link setting 820 hfp_connection->link_setting = HFP_LINK_SETTINGS_D1; 821 if (hci_remote_esco_supported(hfp_connection->acl_handle)){ 822 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3; 823 if ((hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)) 824 && (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){ 825 hfp_connection->link_setting = HFP_LINK_SETTINGS_S4; 826 } 827 } 828 } 829 830 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 831 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 832 833 hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0, hfp_connection->acl_handle, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 834 hfp_init_link_settings(hfp_connection); 835 // restore volume settings 836 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 837 hfp_connection->send_speaker_gain = 1; 838 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 839 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 840 hfp_connection->send_microphone_gain = 1; 841 hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 842 // enable all indicators 843 int i; 844 for (i=0;i<hfp_indicators_nr;i++){ 845 hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 846 hfp_connection->generic_status_indicators[i].state = 1; 847 } 848 } 849 850 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 851 hfp_connection->ok_pending = 0; 852 switch (hfp_connection->state){ 853 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 854 if (has_codec_negotiation_feature(hfp_connection)){ 855 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 856 break; 857 } 858 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 859 break; 860 861 case HFP_W4_NOTIFY_ON_CODECS: 862 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 863 break; 864 865 case HFP_W4_RETRIEVE_INDICATORS: 866 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 867 break; 868 869 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 870 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 871 break; 872 873 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 874 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 875 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 876 break; 877 } 878 if (has_hf_indicators_feature(hfp_connection)){ 879 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 880 break; 881 } 882 hfp_ag_slc_established(hfp_connection); 883 break; 884 885 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 886 if (has_hf_indicators_feature(hfp_connection)){ 887 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 888 break; 889 } 890 hfp_ag_slc_established(hfp_connection); 891 break; 892 893 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 894 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 895 break; 896 897 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 898 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 899 break; 900 901 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 902 hfp_ag_slc_established(hfp_connection); 903 break; 904 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 905 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 906 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 907 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 908 break; 909 } 910 911 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 912 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 913 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 914 break; 915 } 916 917 switch (hfp_connection->hf_query_operator_state){ 918 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 919 printf("Format set, querying name\n"); 920 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 921 break; 922 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 923 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 924 hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator); 925 break; 926 default: 927 break; 928 } 929 930 if (hfp_connection->enable_extended_audio_gateway_error_report){ 931 hfp_connection->enable_extended_audio_gateway_error_report = 0; 932 break; 933 } 934 935 switch (hfp_connection->codecs_state){ 936 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 937 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 938 break; 939 case HFP_CODECS_HF_CONFIRMED_CODEC: 940 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 941 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 942 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 943 hfp_emit_codec_event(hfp_callback, 0, hfp_connection->negotiated_codec); 944 break; 945 default: 946 break; 947 } 948 break; 949 default: 950 break; 951 } 952 953 // done 954 hfp_connection->command = HFP_CMD_NONE; 955 } 956 957 958 static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 959 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 960 if (!hfp_connection) return; 961 962 char last_char = packet[size-1]; 963 packet[size-1] = 0; 964 log_info("HFP_RX %s", packet); 965 packet[size-1] = last_char; 966 967 int pos, i, value; 968 for (pos = 0; pos < size ; pos++){ 969 hfp_parse(hfp_connection, packet[pos], 1); 970 } 971 972 switch (hfp_connection->command){ 973 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 974 hfp_connection->command = HFP_CMD_NONE; 975 // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type); 976 hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 977 break; 978 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 979 hfp_connection->command = HFP_CMD_NONE; 980 // printf("Response and Hold status: %s\n", hfp_connection->line_buffer); 981 hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, atoi((char *)&hfp_connection->line_buffer[0])); 982 break; 983 case HFP_CMD_LIST_CURRENT_CALLS: 984 hfp_connection->command = HFP_CMD_NONE; 985 // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 986 // hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 987 // hfp_connection->bnip_number, hfp_connection->bnip_type); 988 hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx, 989 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 990 hfp_connection->bnip_type, hfp_connection->bnip_number); 991 break; 992 case HFP_CMD_SET_SPEAKER_GAIN: 993 hfp_connection->command = HFP_CMD_NONE; 994 value = atoi((char*)hfp_connection->line_buffer); 995 hfp_hf_speaker_gain = value; 996 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value); 997 break; 998 case HFP_CMD_SET_MICROPHONE_GAIN: 999 hfp_connection->command = HFP_CMD_NONE; 1000 value = atoi((char*)hfp_connection->line_buffer); 1001 hfp_hf_microphone_gain = value; 1002 hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1003 break; 1004 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1005 hfp_connection->command = HFP_CMD_NONE; 1006 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1007 break; 1008 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1009 hfp_connection->command = HFP_CMD_NONE; 1010 hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1011 break; 1012 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1013 hfp_connection->command = HFP_CMD_NONE; 1014 hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1015 break; 1016 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1017 hfp_connection->ok_pending = 0; 1018 hfp_connection->command = HFP_CMD_NONE; 1019 hfp_connection->extended_audio_gateway_error = 0; 1020 hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1021 break; 1022 case HFP_CMD_ERROR: 1023 hfp_connection->ok_pending = 0; 1024 hfp_reset_context_flags(hfp_connection); 1025 hfp_connection->command = HFP_CMD_NONE; 1026 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1); 1027 break; 1028 case HFP_CMD_OK: 1029 hfp_hf_switch_on_ok(hfp_connection); 1030 break; 1031 case HFP_CMD_RING: 1032 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING); 1033 break; 1034 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1035 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1036 if (hfp_connection->ag_indicators[i].status_changed) { 1037 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1038 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1039 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1040 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1041 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1042 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1043 } 1044 hfp_connection->ag_indicators[i].status_changed = 0; 1045 hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]); 1046 break; 1047 } 1048 } 1049 break; 1050 default: 1051 break; 1052 } 1053 hfp_run_for_context(hfp_connection); 1054 } 1055 1056 static void hfp_run(void){ 1057 btstack_linked_list_iterator_t it; 1058 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1059 while (btstack_linked_list_iterator_has_next(&it)){ 1060 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1061 hfp_run_for_context(hfp_connection); 1062 } 1063 } 1064 1065 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1066 switch (packet_type){ 1067 case RFCOMM_DATA_PACKET: 1068 hfp_handle_rfcomm_event(packet_type, channel, packet, size); 1069 break; 1070 case HCI_EVENT_PACKET: 1071 hfp_handle_hci_event(packet_type, channel, packet, size); 1072 default: 1073 break; 1074 } 1075 hfp_run(); 1076 } 1077 1078 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1079 // register for HCI events 1080 hci_event_callback_registration.callback = &packet_handler; 1081 hci_add_event_handler(&hci_event_callback_registration); 1082 1083 rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); 1084 1085 hfp_set_packet_handler_for_rfcomm_connections(&packet_handler); 1086 1087 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1088 hfp_codecs_nr = 0; 1089 hfp_indicators_nr = 0; 1090 hfp_hf_speaker_gain = 9; 1091 hfp_hf_microphone_gain = 9; 1092 } 1093 1094 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 1095 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1096 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1097 return; 1098 } 1099 1100 hfp_codecs_nr = codecs_nr; 1101 int i; 1102 for (i=0; i<codecs_nr; i++){ 1103 hfp_codecs[i] = codecs[i]; 1104 } 1105 1106 char buffer[30]; 1107 int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr); 1108 buffer[offset] = 0; 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) continue; 1114 hfp_connection->command = HFP_CMD_AVAILABLE_CODECS; 1115 hfp_run_for_context(hfp_connection); 1116 } 1117 } 1118 1119 void hfp_hf_init_supported_features(uint32_t supported_features){ 1120 hfp_supported_features = supported_features; 1121 } 1122 1123 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 1124 hfp_indicators_nr = indicators_nr; 1125 int i; 1126 for (i = 0; i < hfp_indicators_nr ; i++){ 1127 hfp_indicators[i] = indicators[i]; 1128 } 1129 } 1130 1131 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1132 hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway); 1133 } 1134 1135 void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){ 1136 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1137 hfp_release_service_level_connection(hfp_connection); 1138 hfp_run_for_context(hfp_connection); 1139 } 1140 1141 static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){ 1142 hfp_hf_establish_service_level_connection(bd_addr); 1143 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1144 if (!hfp_connection){ 1145 log_error("HFP HF: hfp_connection doesn't exist."); 1146 return; 1147 } 1148 hfp_connection->enable_status_update_for_ag_indicators = enable; 1149 hfp_run_for_context(hfp_connection); 1150 } 1151 1152 void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){ 1153 hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1); 1154 } 1155 1156 void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){ 1157 hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0); 1158 } 1159 1160 // TODO: returned ERROR - wrong format 1161 void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){ 1162 hfp_hf_establish_service_level_connection(bd_addr); 1163 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1164 if (!hfp_connection){ 1165 log_error("HFP HF: hfp_connection doesn't exist."); 1166 return; 1167 } 1168 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1169 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1170 hfp_run_for_context(hfp_connection); 1171 } 1172 1173 void hfp_hf_query_operator_selection(bd_addr_t bd_addr){ 1174 hfp_hf_establish_service_level_connection(bd_addr); 1175 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1176 if (!hfp_connection){ 1177 log_error("HFP HF: hfp_connection doesn't exist."); 1178 return; 1179 } 1180 switch (hfp_connection->hf_query_operator_state){ 1181 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1182 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1183 break; 1184 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1185 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1186 break; 1187 default: 1188 break; 1189 } 1190 hfp_run_for_context(hfp_connection); 1191 } 1192 1193 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){ 1194 hfp_hf_establish_service_level_connection(bd_addr); 1195 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1196 if (!hfp_connection){ 1197 log_error("HFP HF: hfp_connection doesn't exist."); 1198 return; 1199 } 1200 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1201 hfp_run_for_context(hfp_connection); 1202 } 1203 1204 1205 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){ 1206 hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1); 1207 } 1208 1209 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){ 1210 hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0); 1211 } 1212 1213 1214 void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){ 1215 hfp_hf_establish_service_level_connection(bd_addr); 1216 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1217 hfp_connection->establish_audio_connection = 0; 1218 1219 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1220 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1221 1222 if (!has_codec_negotiation_feature(hfp_connection)){ 1223 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1224 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1225 hfp_connection->establish_audio_connection = 1; 1226 } else { 1227 switch (hfp_connection->codecs_state){ 1228 case HFP_CODECS_W4_AG_COMMON_CODEC: 1229 break; 1230 default: 1231 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1232 break; 1233 } 1234 } 1235 1236 hfp_run_for_context(hfp_connection); 1237 } 1238 1239 void hfp_hf_release_audio_connection(bd_addr_t bd_addr){ 1240 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1241 hfp_release_audio_connection(hfp_connection); 1242 hfp_run_for_context(hfp_connection); 1243 } 1244 1245 void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){ 1246 hfp_hf_establish_service_level_connection(bd_addr); 1247 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1248 1249 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1250 hfp_connection->hf_answer_incoming_call = 1; 1251 hfp_run_for_context(hfp_connection); 1252 } else { 1253 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1254 } 1255 } 1256 1257 void hfp_hf_terminate_call(bd_addr_t bd_addr){ 1258 hfp_hf_establish_service_level_connection(bd_addr); 1259 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1260 1261 hfp_connection->hf_send_chup = 1; 1262 hfp_run_for_context(hfp_connection); 1263 } 1264 1265 void hfp_hf_reject_incoming_call(bd_addr_t bd_addr){ 1266 hfp_hf_establish_service_level_connection(bd_addr); 1267 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1268 1269 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1270 hfp_connection->hf_send_chup = 1; 1271 hfp_run_for_context(hfp_connection); 1272 } 1273 } 1274 1275 void hfp_hf_user_busy(bd_addr_t addr){ 1276 hfp_hf_establish_service_level_connection(addr); 1277 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1278 1279 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1280 hfp_connection->hf_send_chld_0 = 1; 1281 hfp_run_for_context(hfp_connection); 1282 } 1283 } 1284 1285 void hfp_hf_end_active_and_accept_other(bd_addr_t addr){ 1286 hfp_hf_establish_service_level_connection(addr); 1287 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1288 1289 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1290 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1291 hfp_connection->hf_send_chld_1 = 1; 1292 hfp_run_for_context(hfp_connection); 1293 } 1294 } 1295 1296 void hfp_hf_swap_calls(bd_addr_t addr){ 1297 hfp_hf_establish_service_level_connection(addr); 1298 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1299 1300 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1301 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1302 hfp_connection->hf_send_chld_2 = 1; 1303 hfp_run_for_context(hfp_connection); 1304 } 1305 } 1306 1307 void hfp_hf_join_held_call(bd_addr_t addr){ 1308 hfp_hf_establish_service_level_connection(addr); 1309 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1310 1311 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1312 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1313 hfp_connection->hf_send_chld_3 = 1; 1314 hfp_run_for_context(hfp_connection); 1315 } 1316 } 1317 1318 void hfp_hf_connect_calls(bd_addr_t addr){ 1319 hfp_hf_establish_service_level_connection(addr); 1320 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1321 1322 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1323 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1324 hfp_connection->hf_send_chld_4 = 1; 1325 hfp_run_for_context(hfp_connection); 1326 } 1327 } 1328 1329 void hfp_hf_release_call_with_index(bd_addr_t addr, int index){ 1330 hfp_hf_establish_service_level_connection(addr); 1331 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1332 1333 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1334 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1335 hfp_connection->hf_send_chld_x = 1; 1336 hfp_connection->hf_send_chld_x_index = 10 + index; 1337 hfp_run_for_context(hfp_connection); 1338 } 1339 } 1340 1341 void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){ 1342 hfp_hf_establish_service_level_connection(addr); 1343 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1344 1345 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1346 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1347 hfp_connection->hf_send_chld_x = 1; 1348 hfp_connection->hf_send_chld_x_index = 20 + index; 1349 hfp_run_for_context(hfp_connection); 1350 } 1351 } 1352 1353 void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){ 1354 hfp_hf_establish_service_level_connection(bd_addr); 1355 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1356 1357 hfp_connection->hf_initiate_outgoing_call = 1; 1358 snprintf(phone_number, sizeof(phone_number), "%s", number); 1359 hfp_run_for_context(hfp_connection); 1360 } 1361 1362 void hfp_hf_dial_memory(bd_addr_t bd_addr, int memory_id){ 1363 hfp_hf_establish_service_level_connection(bd_addr); 1364 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1365 1366 hfp_connection->hf_initiate_memory_dialing = 1; 1367 hfp_connection->memory_id = memory_id; 1368 1369 hfp_run_for_context(hfp_connection); 1370 } 1371 1372 void hfp_hf_redial_last_number(bd_addr_t bd_addr){ 1373 hfp_hf_establish_service_level_connection(bd_addr); 1374 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1375 1376 hfp_connection->hf_initiate_redial_last_number = 1; 1377 hfp_run_for_context(hfp_connection); 1378 } 1379 1380 void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){ 1381 hfp_hf_establish_service_level_connection(bd_addr); 1382 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1383 1384 hfp_connection->hf_activate_call_waiting_notification = 1; 1385 hfp_run_for_context(hfp_connection); 1386 } 1387 1388 1389 void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){ 1390 hfp_hf_establish_service_level_connection(bd_addr); 1391 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1392 1393 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1394 hfp_run_for_context(hfp_connection); 1395 } 1396 1397 1398 void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){ 1399 hfp_hf_establish_service_level_connection(bd_addr); 1400 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1401 1402 hfp_connection->hf_activate_calling_line_notification = 1; 1403 hfp_run_for_context(hfp_connection); 1404 } 1405 1406 void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){ 1407 hfp_hf_establish_service_level_connection(bd_addr); 1408 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1409 1410 hfp_connection->hf_deactivate_calling_line_notification = 1; 1411 hfp_run_for_context(hfp_connection); 1412 } 1413 1414 1415 void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){ 1416 hfp_hf_establish_service_level_connection(bd_addr); 1417 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1418 1419 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1420 hfp_run_for_context(hfp_connection); 1421 } 1422 1423 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){ 1424 hfp_hf_establish_service_level_connection(bd_addr); 1425 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1426 1427 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1428 hfp_run_for_context(hfp_connection); 1429 } 1430 1431 void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){ 1432 hfp_hf_establish_service_level_connection(bd_addr); 1433 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1434 1435 hfp_connection->hf_activate_voice_recognition_notification = 1; 1436 hfp_run_for_context(hfp_connection); 1437 } 1438 1439 void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){ 1440 hfp_hf_establish_service_level_connection(bd_addr); 1441 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1442 1443 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1444 hfp_run_for_context(hfp_connection); 1445 } 1446 1447 void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){ 1448 hfp_hf_establish_service_level_connection(bd_addr); 1449 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1450 if (hfp_connection->microphone_gain == gain) return; 1451 if (gain < 0 || gain > 15){ 1452 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1453 return; 1454 } 1455 hfp_connection->microphone_gain = gain; 1456 hfp_connection->send_microphone_gain = 1; 1457 hfp_run_for_context(hfp_connection); 1458 } 1459 1460 void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){ 1461 hfp_hf_establish_service_level_connection(bd_addr); 1462 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1463 if (hfp_connection->speaker_gain == gain) return; 1464 if (gain < 0 || gain > 15){ 1465 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1466 return; 1467 } 1468 hfp_connection->speaker_gain = gain; 1469 hfp_connection->send_speaker_gain = 1; 1470 hfp_run_for_context(hfp_connection); 1471 } 1472 1473 void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){ 1474 hfp_hf_establish_service_level_connection(addr); 1475 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1476 hfp_connection->hf_send_dtmf_code = code; 1477 hfp_run_for_context(hfp_connection); 1478 } 1479 1480 void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){ 1481 hfp_hf_establish_service_level_connection(addr); 1482 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1483 hfp_connection->hf_send_binp = 1; 1484 hfp_run_for_context(hfp_connection); 1485 } 1486 1487 void hfp_hf_query_current_call_status(bd_addr_t addr){ 1488 hfp_hf_establish_service_level_connection(addr); 1489 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1490 hfp_connection->hf_send_clcc = 1; 1491 hfp_run_for_context(hfp_connection); 1492 } 1493 1494 1495 void hfp_hf_rrh_query_status(bd_addr_t addr){ 1496 hfp_hf_establish_service_level_connection(addr); 1497 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1498 hfp_connection->hf_send_rrh = 1; 1499 hfp_connection->hf_send_rrh_command = '?'; 1500 hfp_run_for_context(hfp_connection); 1501 } 1502 1503 void hfp_hf_rrh_hold_call(bd_addr_t addr){ 1504 hfp_hf_establish_service_level_connection(addr); 1505 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1506 hfp_connection->hf_send_rrh = 1; 1507 hfp_connection->hf_send_rrh_command = '0'; 1508 hfp_run_for_context(hfp_connection); 1509 } 1510 1511 void hfp_hf_rrh_accept_held_call(bd_addr_t addr){ 1512 hfp_hf_establish_service_level_connection(addr); 1513 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1514 hfp_connection->hf_send_rrh = 1; 1515 hfp_connection->hf_send_rrh_command = '1'; 1516 hfp_run_for_context(hfp_connection); 1517 } 1518 1519 void hfp_hf_rrh_reject_held_call(bd_addr_t addr){ 1520 hfp_hf_establish_service_level_connection(addr); 1521 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1522 hfp_connection->hf_send_rrh = 1; 1523 hfp_connection->hf_send_rrh_command = '2'; 1524 hfp_run_for_context(hfp_connection); 1525 } 1526 1527 void hfp_hf_query_subscriber_number(bd_addr_t addr){ 1528 hfp_hf_establish_service_level_connection(addr); 1529 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1530 hfp_connection->hf_send_cnum = 1; 1531 hfp_run_for_context(hfp_connection); 1532 } 1533 1534 void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){ 1535 hfp_hf_establish_service_level_connection(addr); 1536 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr); 1537 // find index for assigned number 1538 int i; 1539 for (i = 0; i < hfp_indicators_nr ; i++){ 1540 if (hfp_indicators[i] == assigned_number){ 1541 // set value 1542 hfp_indicators_value[i] = value; 1543 // mark for update 1544 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1545 hfp_connection->generic_status_update_bitmap |= (1<<i); 1546 // send update 1547 hfp_run_for_context(hfp_connection); 1548 } 1549 return; 1550 } 1551 } 1552 } 1553 1554