xref: /btstack/src/classic/hfp_hf.c (revision c5b64319fd1903e30aff6b2e3991ac91d814ea66)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald // *****************************************************************************
393deb3ec6SMatthias Ringwald //
403deb3ec6SMatthias Ringwald // Minimal setup for HFP Hands-Free (HF) unit (!! UNDER DEVELOPMENT !!)
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald #include "hci.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
563deb3ec6SMatthias Ringwald #include "hci_dump.h"
573deb3ec6SMatthias Ringwald #include "l2cap.h"
583edc84c5SMatthias Ringwald #include "classic/sdp_query_rfcomm.h"
593edc84c5SMatthias Ringwald #include "classic/sdp.h"
6016ece135SMatthias Ringwald #include "btstack_debug.h"
613edc84c5SMatthias Ringwald #include "classic/hfp.h"
623edc84c5SMatthias Ringwald #include "classic/hfp_hf.h"
633deb3ec6SMatthias Ringwald 
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
663deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
673deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
683deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
693deb3ec6SMatthias Ringwald 
703deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
72667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
73667ec068SMatthias Ringwald static uint16_t hfp_indicators_status;
74667ec068SMatthias Ringwald 
75667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
76667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
773deb3ec6SMatthias Ringwald 
783deb3ec6SMatthias Ringwald static hfp_callback_t hfp_callback;
793deb3ec6SMatthias Ringwald 
80ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
81ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
82ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
83ce263fc8SMatthias Ringwald 
84ce263fc8SMatthias Ringwald static char phone_number[25];
85ce263fc8SMatthias Ringwald 
86*c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
87*c5b64319SMatthias Ringwald 
883deb3ec6SMatthias Ringwald void hfp_hf_register_packet_handler(hfp_callback_t callback){
893deb3ec6SMatthias Ringwald     hfp_callback = callback;
903deb3ec6SMatthias Ringwald     if (callback == NULL){
913deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
923deb3ec6SMatthias Ringwald         return;
933deb3ec6SMatthias Ringwald     }
943deb3ec6SMatthias Ringwald     hfp_callback = callback;
953deb3ec6SMatthias Ringwald }
963deb3ec6SMatthias Ringwald 
973deb3ec6SMatthias Ringwald static int hfp_hf_supports_codec(uint8_t codec){
983deb3ec6SMatthias Ringwald     int i;
993deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_codecs_nr; i++){
1003deb3ec6SMatthias Ringwald         if (hfp_codecs[i] == codec) return 1;
1013deb3ec6SMatthias Ringwald     }
102ce263fc8SMatthias Ringwald     return HFP_CODEC_CVSD;
1033deb3ec6SMatthias Ringwald }
1043deb3ec6SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * connection){
1053deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1063deb3ec6SMatthias Ringwald     int ag = get_bit(connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1073deb3ec6SMatthias Ringwald     return hf && ag;
1083deb3ec6SMatthias Ringwald }
1093deb3ec6SMatthias Ringwald 
1103deb3ec6SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * connection){
1113deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1123deb3ec6SMatthias Ringwald     int ag = get_bit(connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1133deb3ec6SMatthias Ringwald     return hf && ag;
1143deb3ec6SMatthias Ringwald }
1153deb3ec6SMatthias Ringwald 
1163deb3ec6SMatthias Ringwald 
1173deb3ec6SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * connection){
1183deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
1193deb3ec6SMatthias Ringwald     int ag = get_bit(connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1203deb3ec6SMatthias Ringwald     return hf && ag;
1213deb3ec6SMatthias Ringwald }
1223deb3ec6SMatthias Ringwald 
123ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1243deb3ec6SMatthias Ringwald 
1252ef54b27SMatthias Ringwald 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){
1263deb3ec6SMatthias Ringwald     if (!name){
1273deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1283deb3ec6SMatthias Ringwald     }
1292ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name);
1303deb3ec6SMatthias Ringwald 
131aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
132aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
133aa4dd815SMatthias Ringwald }
1343deb3ec6SMatthias Ringwald 
1353deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
1363deb3ec6SMatthias Ringwald     char buffer[20];
1373deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
1383deb3ec6SMatthias Ringwald     // printf("exchange_supported_features %s\n", buffer);
1393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1403deb3ec6SMatthias Ringwald }
1413deb3ec6SMatthias Ringwald 
1423deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
1433deb3ec6SMatthias Ringwald     char buffer[30];
1443deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS);
1453deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr);
1463deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1473deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1483deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1493deb3ec6SMatthias Ringwald }
1503deb3ec6SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
1523deb3ec6SMatthias Ringwald     char buffer[20];
1533deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR);
1543deb3ec6SMatthias Ringwald     // printf("retrieve_indicators %s\n", buffer);
1553deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1563deb3ec6SMatthias Ringwald }
1573deb3ec6SMatthias Ringwald 
1583deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
1593deb3ec6SMatthias Ringwald     char buffer[20];
1603deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR);
1613deb3ec6SMatthias Ringwald     // printf("retrieve_indicators_status %s\n", buffer);
1623deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1633deb3ec6SMatthias Ringwald }
1643deb3ec6SMatthias Ringwald 
1653deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
1663deb3ec6SMatthias Ringwald     char buffer[20];
1673deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
1683deb3ec6SMatthias Ringwald     // printf("toggle_indicator_status_update %s\n", buffer);
1693deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1703deb3ec6SMatthias Ringwald }
1713deb3ec6SMatthias Ringwald 
1723deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
1733deb3ec6SMatthias Ringwald     char buffer[50];
1743deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
1753deb3ec6SMatthias Ringwald     offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr);
1763deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1773deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1783deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1793deb3ec6SMatthias Ringwald }
1803deb3ec6SMatthias Ringwald 
1813deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
1823deb3ec6SMatthias Ringwald     char buffer[20];
1833deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
1843deb3ec6SMatthias Ringwald     // printf("retrieve_can_hold_call %s\n", buffer);
1853deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1863deb3ec6SMatthias Ringwald }
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
1893deb3ec6SMatthias Ringwald     char buffer[30];
1903deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
1913deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr);
1923deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1933deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1943deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1953deb3ec6SMatthias Ringwald }
1963deb3ec6SMatthias Ringwald 
1973deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
1983deb3ec6SMatthias Ringwald     char buffer[20];
1993deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2003deb3ec6SMatthias Ringwald     // printf("retrieve_supported_generic_status_indicators %s\n", buffer);
2013deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2023deb3ec6SMatthias Ringwald }
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
2053deb3ec6SMatthias Ringwald     char buffer[20];
2063deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2073deb3ec6SMatthias Ringwald     // printf("list_initital_supported_generic_status_indicators %s\n", buffer);
2083deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2093deb3ec6SMatthias Ringwald }
2103deb3ec6SMatthias Ringwald 
2113deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
2123deb3ec6SMatthias Ringwald     char buffer[20];
2133deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION);
2143deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2153deb3ec6SMatthias Ringwald }
2163deb3ec6SMatthias Ringwald 
2173deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
2183deb3ec6SMatthias Ringwald     char buffer[20];
2193deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION);
2203deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2213deb3ec6SMatthias Ringwald }
2223deb3ec6SMatthias Ringwald 
2233deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
2243deb3ec6SMatthias Ringwald     char buffer[20];
2253deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
2263deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2273deb3ec6SMatthias Ringwald }
2283deb3ec6SMatthias Ringwald 
2293deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
2303deb3ec6SMatthias Ringwald     char buffer[20];
2313deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP);
2323deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2333deb3ec6SMatthias Ringwald }
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
2363deb3ec6SMatthias Ringwald     char buffer[20];
2373deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
2383deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2393deb3ec6SMatthias Ringwald }
2403deb3ec6SMatthias Ringwald 
241ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
242ce263fc8SMatthias Ringwald     char buffer[10];
243ce263fc8SMatthias Ringwald     sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED);
244ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
245ce263fc8SMatthias Ringwald }
246ce263fc8SMatthias Ringwald 
247ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
248ce263fc8SMatthias Ringwald     char buffer[40];
249ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
250ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
251ce263fc8SMatthias Ringwald }
252ce263fc8SMatthias Ringwald 
253ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
254ce263fc8SMatthias Ringwald     char buffer[40];
255ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
256ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
257ce263fc8SMatthias Ringwald }
258ce263fc8SMatthias Ringwald 
259ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
260ce263fc8SMatthias Ringwald     char buffer[40];
261ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate);
262ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
263ce263fc8SMatthias Ringwald }
264ce263fc8SMatthias Ringwald 
265ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
266ce263fc8SMatthias Ringwald     char buffer[40];
267ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate);
268ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
269ce263fc8SMatthias Ringwald }
270ce263fc8SMatthias Ringwald 
271ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
272ce263fc8SMatthias Ringwald     char buffer[40];
273ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate);
274ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
275ce263fc8SMatthias Ringwald }
276ce263fc8SMatthias Ringwald 
277ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
278ce263fc8SMatthias Ringwald     char buffer[40];
279ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
280ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
281ce263fc8SMatthias Ringwald }
282ce263fc8SMatthias Ringwald 
283ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
284ce263fc8SMatthias Ringwald     char buffer[40];
285ce263fc8SMatthias Ringwald     sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
286ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
287ce263fc8SMatthias Ringwald }
288ce263fc8SMatthias Ringwald 
289ce263fc8SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid){
290ce263fc8SMatthias Ringwald     char buffer[40];
291ce263fc8SMatthias Ringwald     sprintf(buffer, "%s>%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
292ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
293ce263fc8SMatthias Ringwald }
294ce263fc8SMatthias Ringwald 
295ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
296ce263fc8SMatthias Ringwald     char buffer[20];
297ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER);
298ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
299ce263fc8SMatthias Ringwald }
300ce263fc8SMatthias Ringwald 
301ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){
302ce263fc8SMatthias Ringwald     char buffer[20];
303ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL);
304ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
305ce263fc8SMatthias Ringwald }
306ce263fc8SMatthias Ringwald 
307ce263fc8SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, int number){
308ce263fc8SMatthias Ringwald     char buffer[20];
309ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
310ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
311ce263fc8SMatthias Ringwald }
312ce263fc8SMatthias Ringwald 
313ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
314ce263fc8SMatthias Ringwald     char buffer[20];
315ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
316ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
317ce263fc8SMatthias Ringwald }
318ce263fc8SMatthias Ringwald 
319ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
320ce263fc8SMatthias Ringwald     char buffer[20];
321ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG);
322ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
323ce263fc8SMatthias Ringwald }
324ce263fc8SMatthias Ringwald 
325667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
326667ec068SMatthias Ringwald     char buffer[20];
327667ec068SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS);
328667ec068SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
329667ec068SMatthias Ringwald }
330667ec068SMatthias Ringwald 
3313deb3ec6SMatthias Ringwald static void hfp_emit_ag_indicator_event(hfp_callback_t callback, int status, hfp_ag_indicator_t indicator){
3323deb3ec6SMatthias Ringwald     if (!callback) return;
333ce263fc8SMatthias Ringwald     uint8_t event[6+HFP_MAX_INDICATOR_DESC_SIZE+1];
3343deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3353deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3363deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
3373deb3ec6SMatthias Ringwald     event[3] = status;
3383deb3ec6SMatthias Ringwald     event[4] = indicator.index;
3393deb3ec6SMatthias Ringwald     event[5] = indicator.status;
340ce263fc8SMatthias Ringwald     strncpy((char*)&event[6], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
341ce263fc8SMatthias Ringwald     event[6+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
3423deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3433deb3ec6SMatthias Ringwald }
3443deb3ec6SMatthias Ringwald 
3453deb3ec6SMatthias Ringwald static void hfp_emit_network_operator_event(hfp_callback_t callback, int status, hfp_network_opearator_t network_operator){
3463deb3ec6SMatthias Ringwald     if (!callback) return;
3473deb3ec6SMatthias Ringwald     uint8_t event[24];
3483deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3493deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3503deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
3513deb3ec6SMatthias Ringwald     event[3] = status;
3523deb3ec6SMatthias Ringwald     event[4] = network_operator.mode;
3533deb3ec6SMatthias Ringwald     event[5] = network_operator.format;
3543deb3ec6SMatthias Ringwald     strcpy((char*)&event[6], network_operator.name);
3553deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3563deb3ec6SMatthias Ringwald }
3573deb3ec6SMatthias Ringwald 
3583deb3ec6SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * context){
3593deb3ec6SMatthias Ringwald     if (context->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
360aa4dd815SMatthias Ringwald     if (context->ok_pending) return 0;
361aa4dd815SMatthias Ringwald     int done = 1;
3623deb3ec6SMatthias Ringwald 
3633deb3ec6SMatthias Ringwald     switch (context->state){
3643deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
3653deb3ec6SMatthias Ringwald             context->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
366aa4dd815SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(context->rfcomm_cid);
3673deb3ec6SMatthias Ringwald             break;
3683deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
3693deb3ec6SMatthias Ringwald             context->state = HFP_W4_NOTIFY_ON_CODECS;
370aa4dd815SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
3713deb3ec6SMatthias Ringwald             break;
3723deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
3733deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_INDICATORS;
374aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(context->rfcomm_cid);
3753deb3ec6SMatthias Ringwald             break;
3763deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
3773deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
378aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(context->rfcomm_cid);
3793deb3ec6SMatthias Ringwald             break;
3803deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
3813deb3ec6SMatthias Ringwald             context->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
382aa4dd815SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, 1);
3833deb3ec6SMatthias Ringwald             break;
3843deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
3853deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
386aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(context->rfcomm_cid);
3873deb3ec6SMatthias Ringwald             break;
3883deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
3893deb3ec6SMatthias Ringwald             context->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
390aa4dd815SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(context->rfcomm_cid);
3913deb3ec6SMatthias Ringwald             break;
3923deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
3933deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
394aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(context->rfcomm_cid);
3953deb3ec6SMatthias Ringwald             break;
3963deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
3973deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
398aa4dd815SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(context->rfcomm_cid);
3993deb3ec6SMatthias Ringwald             break;
4003deb3ec6SMatthias Ringwald         default:
401aa4dd815SMatthias Ringwald             done = 0;
4023deb3ec6SMatthias Ringwald             break;
4033deb3ec6SMatthias Ringwald     }
4043deb3ec6SMatthias Ringwald     return done;
4053deb3ec6SMatthias Ringwald }
4063deb3ec6SMatthias Ringwald 
407ce263fc8SMatthias Ringwald 
408ce263fc8SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * context){
409ce263fc8SMatthias Ringwald     if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
410ce263fc8SMatthias Ringwald     if (context->ok_pending) return 0;
411ce263fc8SMatthias Ringwald 
412ce263fc8SMatthias Ringwald     int done = 0;
413ce263fc8SMatthias Ringwald     if (context->enable_status_update_for_ag_indicators != 0xFF){
414ce263fc8SMatthias Ringwald         context->ok_pending = 1;
415ce263fc8SMatthias Ringwald         done = 1;
416ce263fc8SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, context->enable_status_update_for_ag_indicators);
417ce263fc8SMatthias Ringwald         return done;
418ce263fc8SMatthias Ringwald     };
419ce263fc8SMatthias Ringwald     if (context->change_status_update_for_individual_ag_indicators){
420ce263fc8SMatthias Ringwald         context->ok_pending = 1;
421ce263fc8SMatthias Ringwald         done = 1;
422ce263fc8SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(context->rfcomm_cid,
423ce263fc8SMatthias Ringwald                 context->ag_indicators_status_update_bitmap,
424ce263fc8SMatthias Ringwald                 context->ag_indicators_nr);
425ce263fc8SMatthias Ringwald         return done;
426ce263fc8SMatthias Ringwald     }
427ce263fc8SMatthias Ringwald 
428ce263fc8SMatthias Ringwald     switch (context->hf_query_operator_state){
429ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
430ce263fc8SMatthias Ringwald             context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
431ce263fc8SMatthias Ringwald             context->ok_pending = 1;
432ce263fc8SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(context->rfcomm_cid);
433ce263fc8SMatthias Ringwald             return 1;
434ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
435ce263fc8SMatthias Ringwald             context->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
436ce263fc8SMatthias Ringwald             context->ok_pending = 1;
437ce263fc8SMatthias Ringwald             hfp_hf_cmd_query_operator_name(context->rfcomm_cid);
438ce263fc8SMatthias Ringwald             return 1;
439ce263fc8SMatthias Ringwald         default:
440ce263fc8SMatthias Ringwald             break;
441ce263fc8SMatthias Ringwald     }
442ce263fc8SMatthias Ringwald 
443ce263fc8SMatthias Ringwald     if (context->enable_extended_audio_gateway_error_report){
444ce263fc8SMatthias Ringwald         context->ok_pending = 1;
445ce263fc8SMatthias Ringwald         done = 1;
446ce263fc8SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(context->rfcomm_cid, context->enable_extended_audio_gateway_error_report);
447ce263fc8SMatthias Ringwald         return done;
448ce263fc8SMatthias Ringwald     }
449ce263fc8SMatthias Ringwald 
450ce263fc8SMatthias Ringwald     return done;
451ce263fc8SMatthias Ringwald }
452ce263fc8SMatthias Ringwald 
453ce263fc8SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * context){
454ce263fc8SMatthias Ringwald     /* events ( == commands):
455ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
456ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
457ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
458ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
459ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
460ce263fc8SMatthias Ringwald     */
461ce263fc8SMatthias Ringwald 
462ce263fc8SMatthias Ringwald     if (context->ok_pending) return 0;
463ce263fc8SMatthias Ringwald 
464ce263fc8SMatthias Ringwald     switch (context->command){
465ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
466ce263fc8SMatthias Ringwald             if (context->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
467ce263fc8SMatthias Ringwald 
468ce263fc8SMatthias Ringwald             context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
469ce263fc8SMatthias Ringwald             context->ok_pending = 1;
470ce263fc8SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
471ce263fc8SMatthias Ringwald             return 1;
472ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
473ce263fc8SMatthias Ringwald             context->codec_confirmed = 0;
474ce263fc8SMatthias Ringwald             context->suggested_codec = 0;
475ce263fc8SMatthias Ringwald             context->negotiated_codec = 0;
476ce263fc8SMatthias Ringwald 
477ce263fc8SMatthias Ringwald             context->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
478ce263fc8SMatthias Ringwald             context->ok_pending = 1;
479ce263fc8SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(context->rfcomm_cid);
480ce263fc8SMatthias Ringwald             break;
481ce263fc8SMatthias Ringwald 
482ce263fc8SMatthias Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:
483ce263fc8SMatthias Ringwald             if (hfp_hf_supports_codec(context->suggested_codec)){
484ce263fc8SMatthias Ringwald                 context->codec_confirmed = context->suggested_codec;
485ce263fc8SMatthias Ringwald                 context->ok_pending = 1;
486ce263fc8SMatthias Ringwald                 context->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
487ce263fc8SMatthias Ringwald                 hfp_hf_cmd_confirm_codec(context->rfcomm_cid, context->suggested_codec);
488ce263fc8SMatthias Ringwald             } else {
489ce263fc8SMatthias Ringwald                 context->codec_confirmed = 0;
490ce263fc8SMatthias Ringwald                 context->suggested_codec = 0;
491ce263fc8SMatthias Ringwald                 context->negotiated_codec = 0;
492ce263fc8SMatthias Ringwald                 context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
493ce263fc8SMatthias Ringwald                 context->ok_pending = 1;
494ce263fc8SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
495ce263fc8SMatthias Ringwald 
496ce263fc8SMatthias Ringwald             }
497ce263fc8SMatthias Ringwald             break;
498ce263fc8SMatthias Ringwald 
499ce263fc8SMatthias Ringwald         default:
500ce263fc8SMatthias Ringwald             break;
501ce263fc8SMatthias Ringwald     }
502ce263fc8SMatthias Ringwald     return 0;
503ce263fc8SMatthias Ringwald }
504ce263fc8SMatthias Ringwald 
505ce263fc8SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * context){
506ce263fc8SMatthias Ringwald     if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
507ce263fc8SMatthias Ringwald         context->state > HFP_W2_DISCONNECT_SCO) return 0;
508ce263fc8SMatthias Ringwald 
509ce263fc8SMatthias Ringwald 
510ce263fc8SMatthias Ringwald     if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED && context->release_audio_connection){
511ce263fc8SMatthias Ringwald         context->state = HFP_W4_SCO_DISCONNECTED;
512ce263fc8SMatthias Ringwald         context->release_audio_connection = 0;
513ce263fc8SMatthias Ringwald         gap_disconnect(context->sco_handle);
514ce263fc8SMatthias Ringwald         return 1;
515ce263fc8SMatthias Ringwald     }
516ce263fc8SMatthias Ringwald 
517ce263fc8SMatthias Ringwald     if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
518ce263fc8SMatthias Ringwald 
519ce263fc8SMatthias Ringwald     // run codecs exchange
520ce263fc8SMatthias Ringwald     int done = codecs_exchange_state_machine(context);
521ce263fc8SMatthias Ringwald     if (done) return 1;
522ce263fc8SMatthias Ringwald 
523ce263fc8SMatthias Ringwald     if (context->establish_audio_connection){
524ce263fc8SMatthias Ringwald         context->state = HFP_W4_SCO_CONNECTED;
525ce263fc8SMatthias Ringwald         context->establish_audio_connection = 0;
526ce263fc8SMatthias Ringwald         hfp_setup_synchronous_connection(context->con_handle, context->link_setting);
527ce263fc8SMatthias Ringwald         return 1;
528ce263fc8SMatthias Ringwald     }
529ce263fc8SMatthias Ringwald 
530ce263fc8SMatthias Ringwald     return 0;
531ce263fc8SMatthias Ringwald }
532ce263fc8SMatthias Ringwald 
533ce263fc8SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * context){
534ce263fc8SMatthias Ringwald     if (context->hf_answer_incoming_call){
535ce263fc8SMatthias Ringwald         hfp_hf_cmd_ata(context->rfcomm_cid);
536ce263fc8SMatthias Ringwald         context->hf_answer_incoming_call = 0;
537ce263fc8SMatthias Ringwald         return 1;
538ce263fc8SMatthias Ringwald     }
539ce263fc8SMatthias Ringwald     return 0;
540ce263fc8SMatthias Ringwald }
541ce263fc8SMatthias Ringwald 
542ce263fc8SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * context){
543ce263fc8SMatthias Ringwald     if (!context) return;
544ce263fc8SMatthias Ringwald     if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return;
545ce263fc8SMatthias Ringwald 
546ce263fc8SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(context);
547ce263fc8SMatthias Ringwald     if (!done){
548ce263fc8SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(context);
549ce263fc8SMatthias Ringwald     }
550ce263fc8SMatthias Ringwald     if (!done){
551ce263fc8SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(context);
552ce263fc8SMatthias Ringwald     }
553ce263fc8SMatthias Ringwald     if (!done){
554ce263fc8SMatthias Ringwald         done = call_setup_state_machine(context);
555ce263fc8SMatthias Ringwald     }
556ce263fc8SMatthias Ringwald 
557ce263fc8SMatthias Ringwald     if (context->send_microphone_gain){
558ce263fc8SMatthias Ringwald         context->send_microphone_gain = 0;
559ce263fc8SMatthias Ringwald         context->ok_pending = 1;
560ce263fc8SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(context->rfcomm_cid, context->microphone_gain);
561ce263fc8SMatthias Ringwald         return;
562ce263fc8SMatthias Ringwald     }
563ce263fc8SMatthias Ringwald 
564ce263fc8SMatthias Ringwald     if (context->send_speaker_gain){
565ce263fc8SMatthias Ringwald         context->send_speaker_gain = 0;
566ce263fc8SMatthias Ringwald         context->ok_pending = 1;
567ce263fc8SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(context->rfcomm_cid, context->speaker_gain);
568ce263fc8SMatthias Ringwald         return;
569ce263fc8SMatthias Ringwald     }
570ce263fc8SMatthias Ringwald 
571ce263fc8SMatthias Ringwald     if (context->hf_deactivate_calling_line_notification){
572ce263fc8SMatthias Ringwald         context->hf_deactivate_calling_line_notification = 0;
573ce263fc8SMatthias Ringwald         context->ok_pending = 1;
574ce263fc8SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(context->rfcomm_cid, 0);
575ce263fc8SMatthias Ringwald         return;
576ce263fc8SMatthias Ringwald     }
577ce263fc8SMatthias Ringwald 
578ce263fc8SMatthias Ringwald     if (context->hf_activate_calling_line_notification){
579ce263fc8SMatthias Ringwald         context->hf_activate_calling_line_notification = 0;
580ce263fc8SMatthias Ringwald         context->ok_pending = 1;
581ce263fc8SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(context->rfcomm_cid, 1);
582ce263fc8SMatthias Ringwald         return;
583ce263fc8SMatthias Ringwald     }
584ce263fc8SMatthias Ringwald 
585ce263fc8SMatthias Ringwald     if (context->hf_deactivate_echo_canceling_and_noise_reduction){
586ce263fc8SMatthias Ringwald         context->hf_deactivate_echo_canceling_and_noise_reduction = 0;
587ce263fc8SMatthias Ringwald         context->ok_pending = 1;
588ce263fc8SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(context->rfcomm_cid, 0);
589ce263fc8SMatthias Ringwald         return;
590ce263fc8SMatthias Ringwald     }
591ce263fc8SMatthias Ringwald 
592ce263fc8SMatthias Ringwald     if (context->hf_activate_echo_canceling_and_noise_reduction){
593ce263fc8SMatthias Ringwald         context->hf_activate_echo_canceling_and_noise_reduction = 0;
594ce263fc8SMatthias Ringwald         context->ok_pending = 1;
595ce263fc8SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(context->rfcomm_cid, 1);
596ce263fc8SMatthias Ringwald         return;
597ce263fc8SMatthias Ringwald     }
598ce263fc8SMatthias Ringwald 
599ce263fc8SMatthias Ringwald     if (context->hf_deactivate_voice_recognition_notification){
600ce263fc8SMatthias Ringwald         context->hf_deactivate_voice_recognition_notification = 0;
601ce263fc8SMatthias Ringwald         context->ok_pending = 1;
602ce263fc8SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(context->rfcomm_cid, 0);
603ce263fc8SMatthias Ringwald         return;
604ce263fc8SMatthias Ringwald     }
605ce263fc8SMatthias Ringwald 
606ce263fc8SMatthias Ringwald     if (context->hf_activate_voice_recognition_notification){
607ce263fc8SMatthias Ringwald         context->hf_activate_voice_recognition_notification = 0;
608ce263fc8SMatthias Ringwald         context->ok_pending = 1;
609ce263fc8SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(context->rfcomm_cid, 1);
610ce263fc8SMatthias Ringwald         return;
611ce263fc8SMatthias Ringwald     }
612ce263fc8SMatthias Ringwald 
613ce263fc8SMatthias Ringwald 
614ce263fc8SMatthias Ringwald     if (context->hf_deactivate_call_waiting_notification){
615ce263fc8SMatthias Ringwald         context->hf_deactivate_call_waiting_notification = 0;
616ce263fc8SMatthias Ringwald         context->ok_pending = 1;
617ce263fc8SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(context->rfcomm_cid, 0);
618ce263fc8SMatthias Ringwald         return;
619ce263fc8SMatthias Ringwald     }
620ce263fc8SMatthias Ringwald 
621ce263fc8SMatthias Ringwald     if (context->hf_activate_call_waiting_notification){
622ce263fc8SMatthias Ringwald         context->hf_activate_call_waiting_notification = 0;
623ce263fc8SMatthias Ringwald         context->ok_pending = 1;
624ce263fc8SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(context->rfcomm_cid, 1);
625ce263fc8SMatthias Ringwald         return;
626ce263fc8SMatthias Ringwald     }
627ce263fc8SMatthias Ringwald 
628ce263fc8SMatthias Ringwald     if (context->hf_initiate_outgoing_call){
629ce263fc8SMatthias Ringwald         context->hf_initiate_outgoing_call = 0;
630ce263fc8SMatthias Ringwald         context->ok_pending = 1;
631ce263fc8SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(context->rfcomm_cid);
632ce263fc8SMatthias Ringwald         return;
633ce263fc8SMatthias Ringwald     }
634ce263fc8SMatthias Ringwald 
635ce263fc8SMatthias Ringwald     if (context->hf_initiate_memory_dialing){
636ce263fc8SMatthias Ringwald         context->hf_initiate_memory_dialing = 0;
637ce263fc8SMatthias Ringwald         context->ok_pending = 1;
638ce263fc8SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(context->rfcomm_cid);
639ce263fc8SMatthias Ringwald         return;
640ce263fc8SMatthias Ringwald     }
641ce263fc8SMatthias Ringwald 
642ce263fc8SMatthias Ringwald     if (context->hf_initiate_redial_last_number){
643ce263fc8SMatthias Ringwald         context->hf_initiate_redial_last_number = 0;
644ce263fc8SMatthias Ringwald         context->ok_pending = 1;
645ce263fc8SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(context->rfcomm_cid);
646ce263fc8SMatthias Ringwald         return;
647ce263fc8SMatthias Ringwald     }
648ce263fc8SMatthias Ringwald 
649ce263fc8SMatthias Ringwald     if (context->hf_send_chup){
650ce263fc8SMatthias Ringwald         context->hf_send_chup = 0;
651ce263fc8SMatthias Ringwald         context->ok_pending = 1;
652ce263fc8SMatthias Ringwald         hfp_hf_send_chup(context->rfcomm_cid);
653ce263fc8SMatthias Ringwald         return;
654ce263fc8SMatthias Ringwald     }
655ce263fc8SMatthias Ringwald 
656ce263fc8SMatthias Ringwald     if (context->hf_send_chld_0){
657ce263fc8SMatthias Ringwald         context->hf_send_chld_0 = 0;
658ce263fc8SMatthias Ringwald         context->ok_pending = 1;
659ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 0);
660ce263fc8SMatthias Ringwald         return;
661ce263fc8SMatthias Ringwald     }
662ce263fc8SMatthias Ringwald 
663ce263fc8SMatthias Ringwald     if (context->hf_send_chld_1){
664ce263fc8SMatthias Ringwald         context->hf_send_chld_1 = 0;
665ce263fc8SMatthias Ringwald         context->ok_pending = 1;
666ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 1);
667ce263fc8SMatthias Ringwald         return;
668ce263fc8SMatthias Ringwald     }
669ce263fc8SMatthias Ringwald 
670ce263fc8SMatthias Ringwald     if (context->hf_send_chld_2){
671ce263fc8SMatthias Ringwald         context->hf_send_chld_2 = 0;
672ce263fc8SMatthias Ringwald         context->ok_pending = 1;
673ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 2);
674ce263fc8SMatthias Ringwald         return;
675ce263fc8SMatthias Ringwald     }
676ce263fc8SMatthias Ringwald 
677ce263fc8SMatthias Ringwald     if (context->hf_send_chld_3){
678ce263fc8SMatthias Ringwald         context->hf_send_chld_3 = 0;
679ce263fc8SMatthias Ringwald         context->ok_pending = 1;
680ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 3);
681ce263fc8SMatthias Ringwald         return;
682ce263fc8SMatthias Ringwald     }
683ce263fc8SMatthias Ringwald 
684ce263fc8SMatthias Ringwald     if (context->hf_send_chld_4){
685ce263fc8SMatthias Ringwald         context->hf_send_chld_4 = 0;
686ce263fc8SMatthias Ringwald         context->ok_pending = 1;
687ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 4);
688ce263fc8SMatthias Ringwald         return;
689ce263fc8SMatthias Ringwald     }
690ce263fc8SMatthias Ringwald 
691667ec068SMatthias Ringwald     if (context->hf_send_chld_x){
692667ec068SMatthias Ringwald         context->hf_send_chld_x = 0;
693667ec068SMatthias Ringwald         context->ok_pending = 1;
694667ec068SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, context->hf_send_chld_x_index);
695667ec068SMatthias Ringwald         return;
696667ec068SMatthias Ringwald     }
697667ec068SMatthias Ringwald 
698ce263fc8SMatthias Ringwald     if (context->hf_send_dtmf_code){
699ce263fc8SMatthias Ringwald         char code = context->hf_send_dtmf_code;
700ce263fc8SMatthias Ringwald         context->hf_send_dtmf_code = 0;
701ce263fc8SMatthias Ringwald         context->ok_pending = 1;
702ce263fc8SMatthias Ringwald         hfp_hf_send_dtmf(context->rfcomm_cid, code);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706ce263fc8SMatthias Ringwald     if (context->hf_send_binp){
707ce263fc8SMatthias Ringwald         context->hf_send_binp = 0;
708ce263fc8SMatthias Ringwald         context->ok_pending = 1;
709ce263fc8SMatthias Ringwald         hfp_hf_send_binp(context->rfcomm_cid);
710ce263fc8SMatthias Ringwald         return;
711ce263fc8SMatthias Ringwald     }
712ce263fc8SMatthias Ringwald 
713667ec068SMatthias Ringwald     if (context->hf_send_clcc){
714667ec068SMatthias Ringwald         context->hf_send_clcc = 0;
715667ec068SMatthias Ringwald         context->ok_pending = 1;
716667ec068SMatthias Ringwald         hfp_hf_send_clcc(context->rfcomm_cid);
717667ec068SMatthias Ringwald         return;
718667ec068SMatthias Ringwald     }
719667ec068SMatthias Ringwald 
720667ec068SMatthias Ringwald     if (context->hf_send_rrh){
721667ec068SMatthias Ringwald         context->hf_send_rrh = 0;
722667ec068SMatthias Ringwald         char buffer[20];
723667ec068SMatthias Ringwald         switch (context->hf_send_rrh_command){
724667ec068SMatthias Ringwald             case '?':
725667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
726667ec068SMatthias Ringwald                 send_str_over_rfcomm(context->rfcomm_cid, buffer);
727667ec068SMatthias Ringwald                 return;
728667ec068SMatthias Ringwald             case '0':
729667ec068SMatthias Ringwald             case '1':
730667ec068SMatthias Ringwald             case '2':
731667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, context->hf_send_rrh_command);
732667ec068SMatthias Ringwald                 send_str_over_rfcomm(context->rfcomm_cid, buffer);
733667ec068SMatthias Ringwald                 return;
734667ec068SMatthias Ringwald             default:
735667ec068SMatthias Ringwald                 break;
736667ec068SMatthias Ringwald         }
737667ec068SMatthias Ringwald         return;
738667ec068SMatthias Ringwald     }
739667ec068SMatthias Ringwald 
740667ec068SMatthias Ringwald     if (context->hf_send_cnum){
741667ec068SMatthias Ringwald         context->hf_send_cnum = 0;
742667ec068SMatthias Ringwald         char buffer[20];
743667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
744667ec068SMatthias Ringwald         send_str_over_rfcomm(context->rfcomm_cid, buffer);
745667ec068SMatthias Ringwald         return;
746667ec068SMatthias Ringwald     }
747667ec068SMatthias Ringwald 
748667ec068SMatthias Ringwald     // update HF indicators
749667ec068SMatthias Ringwald     if (context->generic_status_update_bitmap){
750667ec068SMatthias Ringwald         int i;
751667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
752667ec068SMatthias Ringwald             if (get_bit(context->generic_status_update_bitmap, i)){
753667ec068SMatthias Ringwald                 if (context->generic_status_indicators[i].state){
754667ec068SMatthias Ringwald                     context->ok_pending = 1;
755667ec068SMatthias Ringwald                     context->generic_status_update_bitmap = store_bit(context->generic_status_update_bitmap, i, 0);
756667ec068SMatthias Ringwald                     char buffer[30];
757667ec068SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]);
758667ec068SMatthias Ringwald                     send_str_over_rfcomm(context->rfcomm_cid, buffer);
759667ec068SMatthias Ringwald                 } else {
760667ec068SMatthias Ringwald                     printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]);
761667ec068SMatthias Ringwald                 }
762667ec068SMatthias Ringwald                 return;
763667ec068SMatthias Ringwald             }
764667ec068SMatthias Ringwald         }
765667ec068SMatthias Ringwald     }
766667ec068SMatthias Ringwald 
767ce263fc8SMatthias Ringwald     if (done) return;
768ce263fc8SMatthias Ringwald     // deal with disconnect
769ce263fc8SMatthias Ringwald     switch (context->state){
770ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
771ce263fc8SMatthias Ringwald             context->state = HFP_W4_RFCOMM_DISCONNECTED;
77228190c0bSMatthias Ringwald             rfcomm_disconnect(context->rfcomm_cid);
773ce263fc8SMatthias Ringwald             break;
774ce263fc8SMatthias Ringwald 
775ce263fc8SMatthias Ringwald         default:
776ce263fc8SMatthias Ringwald             break;
777ce263fc8SMatthias Ringwald     }
778ce263fc8SMatthias Ringwald }
779ce263fc8SMatthias Ringwald 
780ce263fc8SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * context){
781ce263fc8SMatthias Ringwald     // determine highest possible link setting
782ce263fc8SMatthias Ringwald     context->link_setting = HFP_LINK_SETTINGS_D1;
783ce263fc8SMatthias Ringwald     if (hci_remote_eSCO_supported(context->con_handle)){
784ce263fc8SMatthias Ringwald         context->link_setting = HFP_LINK_SETTINGS_S3;
785ce263fc8SMatthias Ringwald         if ((hfp_supported_features             & (1<<HFP_HFSF_ESCO_S4))
786ce263fc8SMatthias Ringwald         &&  (context->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){
787ce263fc8SMatthias Ringwald             context->link_setting = HFP_LINK_SETTINGS_S4;
788ce263fc8SMatthias Ringwald         }
789ce263fc8SMatthias Ringwald     }
790ce263fc8SMatthias Ringwald }
791ce263fc8SMatthias Ringwald 
792ce263fc8SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * context){
793ce263fc8SMatthias Ringwald     context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
794ce263fc8SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
795ce263fc8SMatthias Ringwald     hfp_init_link_settings(context);
796667ec068SMatthias Ringwald     // restore volume settings
797667ec068SMatthias Ringwald     context->speaker_gain = hfp_hf_speaker_gain;
798667ec068SMatthias Ringwald     context->send_speaker_gain = 1;
799667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
800667ec068SMatthias Ringwald     context->microphone_gain = hfp_hf_microphone_gain;
801667ec068SMatthias Ringwald     context->send_microphone_gain = 1;
802667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
803667ec068SMatthias Ringwald     // enable all indicators
804667ec068SMatthias Ringwald     int i;
805667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
806667ec068SMatthias Ringwald         context->generic_status_indicators[i].uuid = hfp_indicators[i];
807667ec068SMatthias Ringwald         context->generic_status_indicators[i].state = 1;
808667ec068SMatthias Ringwald     }
809ce263fc8SMatthias Ringwald }
810ce263fc8SMatthias Ringwald 
811ce263fc8SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *context){
812ce263fc8SMatthias Ringwald     context->ok_pending = 0;
813ce263fc8SMatthias Ringwald     int done = 1;
8143deb3ec6SMatthias Ringwald     switch (context->state){
8153deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
8163deb3ec6SMatthias Ringwald             if (has_codec_negotiation_feature(context)){
8173deb3ec6SMatthias Ringwald                 context->state = HFP_NOTIFY_ON_CODECS;
8183deb3ec6SMatthias Ringwald                 break;
8193deb3ec6SMatthias Ringwald             }
8203deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INDICATORS;
8213deb3ec6SMatthias Ringwald             break;
8223deb3ec6SMatthias Ringwald 
8233deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
8243deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INDICATORS;
8253deb3ec6SMatthias Ringwald             break;
8263deb3ec6SMatthias Ringwald 
8273deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
8283deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INDICATORS_STATUS;
8293deb3ec6SMatthias Ringwald             break;
8303deb3ec6SMatthias Ringwald 
8313deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
8323deb3ec6SMatthias Ringwald             context->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8333deb3ec6SMatthias Ringwald             break;
8343deb3ec6SMatthias Ringwald 
8353deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
8363deb3ec6SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(context)){
8373deb3ec6SMatthias Ringwald                 context->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8383deb3ec6SMatthias Ringwald                 break;
8393deb3ec6SMatthias Ringwald             }
8403deb3ec6SMatthias Ringwald             if (has_hf_indicators_feature(context)){
8413deb3ec6SMatthias Ringwald                 context->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8423deb3ec6SMatthias Ringwald                 break;
8433deb3ec6SMatthias Ringwald             }
844ce263fc8SMatthias Ringwald             hfp_ag_slc_established(context);
8453deb3ec6SMatthias Ringwald             break;
8463deb3ec6SMatthias Ringwald 
8473deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
8483deb3ec6SMatthias Ringwald             if (has_hf_indicators_feature(context)){
8493deb3ec6SMatthias Ringwald                 context->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8503deb3ec6SMatthias Ringwald                 break;
8513deb3ec6SMatthias Ringwald             }
852ce263fc8SMatthias Ringwald             hfp_ag_slc_established(context);
8533deb3ec6SMatthias Ringwald             break;
8543deb3ec6SMatthias Ringwald 
8553deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
8563deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8573deb3ec6SMatthias Ringwald             break;
8583deb3ec6SMatthias Ringwald 
8593deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
8603deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
8613deb3ec6SMatthias Ringwald             break;
8623deb3ec6SMatthias Ringwald 
8633deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
864ce263fc8SMatthias Ringwald             hfp_ag_slc_established(context);
8653deb3ec6SMatthias Ringwald             break;
866ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
8673deb3ec6SMatthias Ringwald             if (context->enable_status_update_for_ag_indicators != 0xFF){
8683deb3ec6SMatthias Ringwald                 context->enable_status_update_for_ag_indicators = 0xFF;
8693deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
870ce263fc8SMatthias Ringwald                 break;
871ce263fc8SMatthias Ringwald             }
8723deb3ec6SMatthias Ringwald 
8733deb3ec6SMatthias Ringwald             if (context->change_status_update_for_individual_ag_indicators == 1){
8743deb3ec6SMatthias Ringwald                 context->change_status_update_for_individual_ag_indicators = 0;
8753deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
876ce263fc8SMatthias Ringwald                 break;
8773deb3ec6SMatthias Ringwald             }
8783deb3ec6SMatthias Ringwald 
879ce263fc8SMatthias Ringwald             switch (context->hf_query_operator_state){
880ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
881ce263fc8SMatthias Ringwald                     printf("Format set, querying name\n");
882ce263fc8SMatthias Ringwald                     context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
883ce263fc8SMatthias Ringwald                     break;
884ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
885ce263fc8SMatthias Ringwald                     context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
8863deb3ec6SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, 0, context->network_operator);
887ce263fc8SMatthias Ringwald                     break;
888ce263fc8SMatthias Ringwald                 default:
889ce263fc8SMatthias Ringwald                     break;
8903deb3ec6SMatthias Ringwald             }
891ce263fc8SMatthias Ringwald 
8923deb3ec6SMatthias Ringwald             if (context->enable_extended_audio_gateway_error_report){
8933deb3ec6SMatthias Ringwald                 context->enable_extended_audio_gateway_error_report = 0;
894ce263fc8SMatthias Ringwald                 break;
8953deb3ec6SMatthias Ringwald             }
8963deb3ec6SMatthias Ringwald 
897aa4dd815SMatthias Ringwald             switch (context->codecs_state){
898aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
899aa4dd815SMatthias Ringwald                     context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9003deb3ec6SMatthias Ringwald                     break;
901ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
902ce263fc8SMatthias Ringwald                     context->codecs_state = HFP_CODECS_EXCHANGED;
903ce263fc8SMatthias Ringwald                     hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
904ce263fc8SMatthias Ringwald                     break;
9053deb3ec6SMatthias Ringwald                 default:
906ce263fc8SMatthias Ringwald                     done = 0;
9073deb3ec6SMatthias Ringwald                     break;
9083deb3ec6SMatthias Ringwald             }
9093deb3ec6SMatthias Ringwald             break;
9103deb3ec6SMatthias Ringwald         default:
911ce263fc8SMatthias Ringwald             done = 0;
9123deb3ec6SMatthias Ringwald             break;
9133deb3ec6SMatthias Ringwald     }
9143deb3ec6SMatthias Ringwald 
9153deb3ec6SMatthias Ringwald     // done
9163deb3ec6SMatthias Ringwald     context->command = HFP_CMD_NONE;
9173deb3ec6SMatthias Ringwald }
9183deb3ec6SMatthias Ringwald 
9193deb3ec6SMatthias Ringwald 
9203deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9213deb3ec6SMatthias Ringwald     hfp_connection_t * context = get_hfp_connection_context_for_rfcomm_cid(channel);
9223deb3ec6SMatthias Ringwald     if (!context) return;
9233deb3ec6SMatthias Ringwald 
9241e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9251e35c04dSMatthias Ringwald     packet[size-1] = 0;
9261e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9271e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9281e35c04dSMatthias Ringwald 
929667ec068SMatthias Ringwald     int pos, i, value;
9303deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
931aa4dd815SMatthias Ringwald         hfp_parse(context, packet[pos], 1);
932ce263fc8SMatthias Ringwald     }
9333deb3ec6SMatthias Ringwald 
934ce263fc8SMatthias Ringwald     switch (context->command){
935667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
936667ec068SMatthias Ringwald             context->command = HFP_CMD_NONE;
937667ec068SMatthias Ringwald             printf("Subscriber Number: number %s, type %u\n", context->bnip_number, context->bnip_type);
938667ec068SMatthias Ringwald             break;
939667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
940667ec068SMatthias Ringwald             context->command = HFP_CMD_NONE;
941667ec068SMatthias Ringwald             printf("Response and Hold status: %s\n", context->line_buffer);
942667ec068SMatthias Ringwald             break;
943667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
944667ec068SMatthias Ringwald             context->command = HFP_CMD_NONE;
945667ec068SMatthias Ringwald             printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
946667ec068SMatthias Ringwald                 context->clcc_idx, context->clcc_dir, context->clcc_status, context->clcc_mpty,
947667ec068SMatthias Ringwald                 context->bnip_number, context->bnip_type);
948667ec068SMatthias Ringwald             break;
949ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
950ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
951667ec068SMatthias Ringwald             value = atoi((char*)context->line_buffer);
952667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
953667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
954ce263fc8SMatthias Ringwald             break;
955ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
956ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
957667ec068SMatthias Ringwald             value = atoi((char*)context->line_buffer);
958667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
959667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
960ce263fc8SMatthias Ringwald             break;
961ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
962ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
963ce263fc8SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, context->bnip_number);
964ce263fc8SMatthias Ringwald             break;
965ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
966ce263fc8SMatthias Ringwald             context->ok_pending = 0;
967ce263fc8SMatthias Ringwald             context->extended_audio_gateway_error = 0;
968ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
969ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, context->extended_audio_gateway_error);
970ce263fc8SMatthias Ringwald             break;
971ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
972ce263fc8SMatthias Ringwald             context->ok_pending = 0;
973ce263fc8SMatthias Ringwald             hfp_reset_context_flags(context);
974ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
975ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
976ce263fc8SMatthias Ringwald             break;
977ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
978ce263fc8SMatthias Ringwald             hfp_hf_switch_on_ok(context);
979ce263fc8SMatthias Ringwald             break;
980ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
981ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RING, 0);
982ce263fc8SMatthias Ringwald             break;
983ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
9843deb3ec6SMatthias Ringwald             for (i = 0; i < context->ag_indicators_nr; i++){
9853deb3ec6SMatthias Ringwald                 if (context->ag_indicators[i].status_changed) {
986ce263fc8SMatthias Ringwald                     if (strcmp(context->ag_indicators[i].name, "callsetup") == 0){
987ce263fc8SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) context->ag_indicators[i].status;
988ce263fc8SMatthias Ringwald                     } else if (strcmp(context->ag_indicators[i].name, "callheld") == 0){
989ce263fc8SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) context->ag_indicators[i].status;
990ce263fc8SMatthias Ringwald                     } else if (strcmp(context->ag_indicators[i].name, "call") == 0){
991ce263fc8SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) context->ag_indicators[i].status;
992ce263fc8SMatthias Ringwald                     }
9933deb3ec6SMatthias Ringwald                     context->ag_indicators[i].status_changed = 0;
994aa4dd815SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, 0, context->ag_indicators[i]);
9953deb3ec6SMatthias Ringwald                     break;
9963deb3ec6SMatthias Ringwald                 }
9973deb3ec6SMatthias Ringwald             }
998ce263fc8SMatthias Ringwald             break;
999ce263fc8SMatthias Ringwald         default:
1000ce263fc8SMatthias Ringwald             break;
10013deb3ec6SMatthias Ringwald     }
1002ce263fc8SMatthias Ringwald     hfp_run_for_context(context);
10033deb3ec6SMatthias Ringwald }
10043deb3ec6SMatthias Ringwald 
10053deb3ec6SMatthias Ringwald static void hfp_run(){
1006665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1007665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1008665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1009665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
10103deb3ec6SMatthias Ringwald         hfp_run_for_context(connection);
10113deb3ec6SMatthias Ringwald     }
10123deb3ec6SMatthias Ringwald }
10133deb3ec6SMatthias Ringwald 
1014ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10153deb3ec6SMatthias Ringwald     switch (packet_type){
10163deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10173deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10183deb3ec6SMatthias Ringwald             break;
10193deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
10203deb3ec6SMatthias Ringwald             hfp_handle_hci_event(hfp_callback, packet_type, packet, size);
10213deb3ec6SMatthias Ringwald         default:
10223deb3ec6SMatthias Ringwald             break;
10233deb3ec6SMatthias Ringwald     }
10243deb3ec6SMatthias Ringwald     hfp_run();
10253deb3ec6SMatthias Ringwald }
10263deb3ec6SMatthias Ringwald 
1027*c5b64319SMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint8_t * packet, uint16_t size){
1028*c5b64319SMatthias Ringwald     packet_handler(packet_type, 0, packet, size);
1029*c5b64319SMatthias Ringwald }
1030*c5b64319SMatthias Ringwald 
10313deb3ec6SMatthias Ringwald void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr){
10323deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
10333deb3ec6SMatthias Ringwald         log_error("hfp_hf_set_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
10343deb3ec6SMatthias Ringwald         return;
10353deb3ec6SMatthias Ringwald     }
10363deb3ec6SMatthias Ringwald 
10373deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
10383deb3ec6SMatthias Ringwald     int i;
10393deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
10403deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
10413deb3ec6SMatthias Ringwald     }
10423deb3ec6SMatthias Ringwald 
10433deb3ec6SMatthias Ringwald     char buffer[30];
10443deb3ec6SMatthias Ringwald     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
10453deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1046665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1047665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1048665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1049665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
10503deb3ec6SMatthias Ringwald         if (!connection) continue;
1051aa4dd815SMatthias Ringwald         connection->command = HFP_CMD_AVAILABLE_CODECS;
10523deb3ec6SMatthias Ringwald         hfp_run_for_context(connection);
10533deb3ec6SMatthias Ringwald     }
10543deb3ec6SMatthias Ringwald }
10553deb3ec6SMatthias Ringwald 
10563deb3ec6SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, uint16_t * indicators, int indicators_nr, uint32_t indicators_status){
1057*c5b64319SMatthias Ringwald 
1058*c5b64319SMatthias Ringwald     // register for HCI events
1059*c5b64319SMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_handler;
1060*c5b64319SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1061*c5b64319SMatthias Ringwald 
1062aa4dd815SMatthias Ringwald     l2cap_init();
1063e4dd59a7SMatthias Ringwald     rfcomm_register_packet_handler(packet_handler);
10643deb3ec6SMatthias Ringwald     hfp_init(rfcomm_channel_nr);
10653deb3ec6SMatthias Ringwald 
10663deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
10673deb3ec6SMatthias Ringwald 
10683deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
10693deb3ec6SMatthias Ringwald     hfp_indicators_status = indicators_status;
10703deb3ec6SMatthias Ringwald     int i;
10713deb3ec6SMatthias Ringwald     for (i=0; i<indicators_nr; i++){
10723deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
10733deb3ec6SMatthias Ringwald     }
10743deb3ec6SMatthias Ringwald }
10753deb3ec6SMatthias Ringwald 
107674386ee0SMatthias Ringwald void hfp_hf_set_supported_features(uint32_t supported_features){
107774386ee0SMatthias Ringwald     hfp_supported_features = supported_features;
107874386ee0SMatthias Ringwald }
107974386ee0SMatthias Ringwald 
10803deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
10813deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
10823deb3ec6SMatthias Ringwald }
10833deb3ec6SMatthias Ringwald 
10843deb3ec6SMatthias Ringwald void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
10853deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
10863deb3ec6SMatthias Ringwald     hfp_release_service_level_connection(connection);
10873deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
10883deb3ec6SMatthias Ringwald }
10893deb3ec6SMatthias Ringwald 
1090ce263fc8SMatthias Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
10913deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
10923deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
10933deb3ec6SMatthias Ringwald     if (!connection){
10943deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
10953deb3ec6SMatthias Ringwald         return;
10963deb3ec6SMatthias Ringwald     }
10973deb3ec6SMatthias Ringwald     connection->enable_status_update_for_ag_indicators = enable;
10983deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
10993deb3ec6SMatthias Ringwald }
11003deb3ec6SMatthias Ringwald 
1101ce263fc8SMatthias Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1102ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1103ce263fc8SMatthias Ringwald }
1104ce263fc8SMatthias Ringwald 
1105ce263fc8SMatthias Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1106ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1107ce263fc8SMatthias Ringwald }
1108ce263fc8SMatthias Ringwald 
11093deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1110ce263fc8SMatthias Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
11113deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11123deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11133deb3ec6SMatthias Ringwald     if (!connection){
11143deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
11153deb3ec6SMatthias Ringwald         return;
11163deb3ec6SMatthias Ringwald     }
11173deb3ec6SMatthias Ringwald     connection->change_status_update_for_individual_ag_indicators = 1;
11183deb3ec6SMatthias Ringwald     connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
11193deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11203deb3ec6SMatthias Ringwald }
11213deb3ec6SMatthias Ringwald 
11223deb3ec6SMatthias Ringwald void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
11233deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11243deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11253deb3ec6SMatthias Ringwald     if (!connection){
11263deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
11273deb3ec6SMatthias Ringwald         return;
11283deb3ec6SMatthias Ringwald     }
1129ce263fc8SMatthias Ringwald     switch (connection->hf_query_operator_state){
1130ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1131ce263fc8SMatthias Ringwald             connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1132ce263fc8SMatthias Ringwald             break;
1133ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1134ce263fc8SMatthias Ringwald             connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1135ce263fc8SMatthias Ringwald             break;
1136ce263fc8SMatthias Ringwald         default:
1137ce263fc8SMatthias Ringwald             break;
1138ce263fc8SMatthias Ringwald     }
11393deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11403deb3ec6SMatthias Ringwald }
11413deb3ec6SMatthias Ringwald 
1142ce263fc8SMatthias Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
11433deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11443deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11453deb3ec6SMatthias Ringwald     if (!connection){
11463deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
11473deb3ec6SMatthias Ringwald         return;
11483deb3ec6SMatthias Ringwald     }
11493deb3ec6SMatthias Ringwald     connection->enable_extended_audio_gateway_error_report = enable;
11503deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11513deb3ec6SMatthias Ringwald }
11523deb3ec6SMatthias Ringwald 
1153ce263fc8SMatthias Ringwald 
1154ce263fc8SMatthias Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1155ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1156ce263fc8SMatthias Ringwald }
1157ce263fc8SMatthias Ringwald 
1158ce263fc8SMatthias Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1159ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1160ce263fc8SMatthias Ringwald }
1161ce263fc8SMatthias Ringwald 
1162ce263fc8SMatthias Ringwald 
11633deb3ec6SMatthias Ringwald void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
11643deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11653deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11663deb3ec6SMatthias Ringwald     connection->establish_audio_connection = 0;
1167ce263fc8SMatthias Ringwald 
11683deb3ec6SMatthias Ringwald     if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
11693deb3ec6SMatthias Ringwald     if (connection->state >= HFP_W2_DISCONNECT_SCO) return;
11703deb3ec6SMatthias Ringwald 
1171ce263fc8SMatthias Ringwald     if (!has_codec_negotiation_feature(connection)){
1172ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1173ce263fc8SMatthias Ringwald         connection->codecs_state = HFP_CODECS_EXCHANGED;
11743deb3ec6SMatthias Ringwald         connection->establish_audio_connection = 1;
1175ce263fc8SMatthias Ringwald     } else {
1176aa4dd815SMatthias Ringwald         switch (connection->codecs_state){
1177aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1178aa4dd815SMatthias Ringwald                 break;
1179aa4dd815SMatthias Ringwald             default:
1180aa4dd815SMatthias Ringwald                 connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1181aa4dd815SMatthias Ringwald                 break;
11823deb3ec6SMatthias Ringwald         }
1183ce263fc8SMatthias Ringwald     }
1184ce263fc8SMatthias Ringwald 
11853deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11863deb3ec6SMatthias Ringwald }
11873deb3ec6SMatthias Ringwald 
11883deb3ec6SMatthias Ringwald void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
11893deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11903deb3ec6SMatthias Ringwald     hfp_release_audio_connection(connection);
11913deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11923deb3ec6SMatthias Ringwald }
11933deb3ec6SMatthias Ringwald 
1194ce263fc8SMatthias Ringwald void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1195ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1196ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1197ce263fc8SMatthias Ringwald 
1198ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1199ce263fc8SMatthias Ringwald         connection->hf_answer_incoming_call = 1;
1200ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1201ce263fc8SMatthias Ringwald     } else {
1202ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1203ce263fc8SMatthias Ringwald     }
1204ce263fc8SMatthias Ringwald }
1205ce263fc8SMatthias Ringwald 
1206ce263fc8SMatthias Ringwald void hfp_hf_terminate_call(bd_addr_t bd_addr){
1207ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1208ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1209ce263fc8SMatthias Ringwald 
1210ce263fc8SMatthias Ringwald     // if (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1211ce263fc8SMatthias Ringwald     connection->hf_send_chup = 1;
1212ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1213ce263fc8SMatthias Ringwald     // } else {
1214ce263fc8SMatthias Ringwald     //     log_error("HFP HF: terminating incoming call with wrong call status %u", hfp_call_status);
1215ce263fc8SMatthias Ringwald     // }
1216ce263fc8SMatthias Ringwald }
1217ce263fc8SMatthias Ringwald 
1218ce263fc8SMatthias Ringwald void hfp_hf_reject_call(bd_addr_t bd_addr){
1219ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1220ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1221ce263fc8SMatthias Ringwald 
1222ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1223ce263fc8SMatthias Ringwald         connection->hf_send_chup = 1;
1224ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1225ce263fc8SMatthias Ringwald     }
1226ce263fc8SMatthias Ringwald }
1227ce263fc8SMatthias Ringwald 
1228ce263fc8SMatthias Ringwald void hfp_hf_user_busy(bd_addr_t addr){
1229ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1230ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1231ce263fc8SMatthias Ringwald 
1232ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1233ce263fc8SMatthias Ringwald         connection->hf_send_chld_0 = 1;
1234ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1235ce263fc8SMatthias Ringwald     }
1236ce263fc8SMatthias Ringwald }
1237ce263fc8SMatthias Ringwald 
1238ce263fc8SMatthias Ringwald void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1239ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1240ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1241ce263fc8SMatthias Ringwald 
1242ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1243ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1244ce263fc8SMatthias Ringwald         connection->hf_send_chld_1 = 1;
1245ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1246ce263fc8SMatthias Ringwald     }
1247ce263fc8SMatthias Ringwald }
1248ce263fc8SMatthias Ringwald 
1249ce263fc8SMatthias Ringwald void hfp_hf_swap_calls(bd_addr_t addr){
1250ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1251ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1252ce263fc8SMatthias Ringwald 
1253ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1254ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1255ce263fc8SMatthias Ringwald         connection->hf_send_chld_2 = 1;
1256ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1257ce263fc8SMatthias Ringwald     }
1258ce263fc8SMatthias Ringwald }
1259ce263fc8SMatthias Ringwald 
1260ce263fc8SMatthias Ringwald void hfp_hf_join_held_call(bd_addr_t addr){
1261ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1262ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1263ce263fc8SMatthias Ringwald 
1264ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1265ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1266ce263fc8SMatthias Ringwald         connection->hf_send_chld_3 = 1;
1267ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1268ce263fc8SMatthias Ringwald     }
1269ce263fc8SMatthias Ringwald }
1270ce263fc8SMatthias Ringwald 
1271ce263fc8SMatthias Ringwald void hfp_hf_connect_calls(bd_addr_t addr){
1272ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1273ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1274ce263fc8SMatthias Ringwald 
1275ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1276ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1277ce263fc8SMatthias Ringwald         connection->hf_send_chld_4 = 1;
1278ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1279ce263fc8SMatthias Ringwald     }
1280ce263fc8SMatthias Ringwald }
1281ce263fc8SMatthias Ringwald 
1282667ec068SMatthias Ringwald void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1283667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1284667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1285667ec068SMatthias Ringwald 
1286667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1287667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1288667ec068SMatthias Ringwald         connection->hf_send_chld_x = 1;
1289667ec068SMatthias Ringwald         connection->hf_send_chld_x_index = 10 + index;
1290667ec068SMatthias Ringwald         hfp_run_for_context(connection);
1291667ec068SMatthias Ringwald     }
1292667ec068SMatthias Ringwald }
1293667ec068SMatthias Ringwald 
1294667ec068SMatthias Ringwald void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1295667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1296667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1297667ec068SMatthias Ringwald 
1298667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1299667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1300667ec068SMatthias Ringwald         connection->hf_send_chld_x = 1;
1301667ec068SMatthias Ringwald         connection->hf_send_chld_x_index = 20 + index;
1302667ec068SMatthias Ringwald         hfp_run_for_context(connection);
1303667ec068SMatthias Ringwald     }
1304667ec068SMatthias Ringwald }
1305ce263fc8SMatthias Ringwald 
1306ce263fc8SMatthias Ringwald void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1307ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1308ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1309ce263fc8SMatthias Ringwald 
1310ce263fc8SMatthias Ringwald     connection->hf_initiate_outgoing_call = 1;
1311ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1312ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1313ce263fc8SMatthias Ringwald }
1314ce263fc8SMatthias Ringwald 
1315ce263fc8SMatthias Ringwald void hfp_hf_dial_memory(bd_addr_t bd_addr, char * number){
1316ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1317ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1318ce263fc8SMatthias Ringwald 
1319ce263fc8SMatthias Ringwald     connection->hf_initiate_memory_dialing = 1;
1320ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1321ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1322ce263fc8SMatthias Ringwald }
1323ce263fc8SMatthias Ringwald 
1324ce263fc8SMatthias Ringwald void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1325ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1326ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1327ce263fc8SMatthias Ringwald 
1328ce263fc8SMatthias Ringwald     connection->hf_initiate_redial_last_number = 1;
1329ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1330ce263fc8SMatthias Ringwald }
1331ce263fc8SMatthias Ringwald 
1332ce263fc8SMatthias Ringwald void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1333ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1334ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1335ce263fc8SMatthias Ringwald 
1336ce263fc8SMatthias Ringwald     connection->hf_activate_call_waiting_notification = 1;
1337ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1338ce263fc8SMatthias Ringwald }
1339ce263fc8SMatthias Ringwald 
1340ce263fc8SMatthias Ringwald 
1341ce263fc8SMatthias Ringwald void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1342ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1343ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1344ce263fc8SMatthias Ringwald 
1345ce263fc8SMatthias Ringwald     connection->hf_deactivate_call_waiting_notification = 1;
1346ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1347ce263fc8SMatthias Ringwald }
1348ce263fc8SMatthias Ringwald 
1349ce263fc8SMatthias Ringwald 
1350ce263fc8SMatthias Ringwald void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1351ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1352ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1353ce263fc8SMatthias Ringwald 
1354a5bdcda8SMatthias Ringwald     connection->hf_activate_calling_line_notification = 1;
1355ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1356ce263fc8SMatthias Ringwald }
1357ce263fc8SMatthias Ringwald 
1358ce263fc8SMatthias Ringwald /*
1359ce263fc8SMatthias Ringwald  * @brief
1360ce263fc8SMatthias Ringwald  */
1361ce263fc8SMatthias Ringwald void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1362ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1363ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1364ce263fc8SMatthias Ringwald 
1365ce263fc8SMatthias Ringwald     connection->hf_deactivate_calling_line_notification = 1;
1366ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1367ce263fc8SMatthias Ringwald }
1368ce263fc8SMatthias Ringwald 
1369ce263fc8SMatthias Ringwald 
1370ce263fc8SMatthias Ringwald /*
1371ce263fc8SMatthias Ringwald  * @brief
1372ce263fc8SMatthias Ringwald  */
1373ce263fc8SMatthias Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1374ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1375ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1376ce263fc8SMatthias Ringwald 
1377a5bdcda8SMatthias Ringwald     connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1378ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1379ce263fc8SMatthias Ringwald }
1380ce263fc8SMatthias Ringwald 
1381ce263fc8SMatthias Ringwald /*
1382ce263fc8SMatthias Ringwald  * @brief
1383ce263fc8SMatthias Ringwald  */
1384ce263fc8SMatthias Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1385ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1386ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1387ce263fc8SMatthias Ringwald 
1388ce263fc8SMatthias Ringwald     connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1389ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1390ce263fc8SMatthias Ringwald }
1391ce263fc8SMatthias Ringwald 
1392ce263fc8SMatthias Ringwald /*
1393ce263fc8SMatthias Ringwald  * @brief
1394ce263fc8SMatthias Ringwald  */
1395ce263fc8SMatthias Ringwald void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1396ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1397ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1398ce263fc8SMatthias Ringwald 
1399ce263fc8SMatthias Ringwald     connection->hf_activate_voice_recognition_notification = 1;
1400ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1401ce263fc8SMatthias Ringwald }
1402ce263fc8SMatthias Ringwald 
1403ce263fc8SMatthias Ringwald /*
1404ce263fc8SMatthias Ringwald  * @brief
1405ce263fc8SMatthias Ringwald  */
1406ce263fc8SMatthias Ringwald void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1407ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1408ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1409ce263fc8SMatthias Ringwald 
1410ce263fc8SMatthias Ringwald     connection->hf_deactivate_voice_recognition_notification = 1;
1411ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1412ce263fc8SMatthias Ringwald }
1413ce263fc8SMatthias Ringwald 
1414ce263fc8SMatthias Ringwald /*
1415ce263fc8SMatthias Ringwald  * @brief
1416ce263fc8SMatthias Ringwald  */
1417ce263fc8SMatthias Ringwald void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1418ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1419ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1420ce263fc8SMatthias Ringwald     if (connection->microphone_gain == gain) return;
1421ce263fc8SMatthias Ringwald 
1422ce263fc8SMatthias Ringwald     connection->microphone_gain = gain;
1423ce263fc8SMatthias Ringwald     connection->send_microphone_gain = 1;
1424ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1425ce263fc8SMatthias Ringwald }
1426ce263fc8SMatthias Ringwald 
1427ce263fc8SMatthias Ringwald /*
1428ce263fc8SMatthias Ringwald  * @brief
1429ce263fc8SMatthias Ringwald  */
1430ce263fc8SMatthias Ringwald void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1431ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1432ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1433ce263fc8SMatthias Ringwald     if (connection->speaker_gain == gain) return;
1434ce263fc8SMatthias Ringwald 
1435ce263fc8SMatthias Ringwald     connection->speaker_gain = gain;
1436ce263fc8SMatthias Ringwald     connection->send_speaker_gain = 1;
1437ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1438ce263fc8SMatthias Ringwald }
1439ce263fc8SMatthias Ringwald 
1440ce263fc8SMatthias Ringwald /*
1441ce263fc8SMatthias Ringwald  * @brief
1442ce263fc8SMatthias Ringwald  */
1443ce263fc8SMatthias Ringwald void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1444ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1445ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1446ce263fc8SMatthias Ringwald     connection->hf_send_dtmf_code = code;
1447ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1448ce263fc8SMatthias Ringwald }
1449ce263fc8SMatthias Ringwald 
1450ce263fc8SMatthias Ringwald /*
1451ce263fc8SMatthias Ringwald  * @brief
1452ce263fc8SMatthias Ringwald  */
1453ce263fc8SMatthias Ringwald void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1454ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1455ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1456ce263fc8SMatthias Ringwald     connection->hf_send_binp = 1;
1457ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1458ce263fc8SMatthias Ringwald }
14593deb3ec6SMatthias Ringwald 
1460667ec068SMatthias Ringwald /*
1461667ec068SMatthias Ringwald  * @brief
1462667ec068SMatthias Ringwald  */
1463667ec068SMatthias Ringwald void hfp_hf_query_current_call_status(bd_addr_t addr){
1464667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1465667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1466667ec068SMatthias Ringwald     connection->hf_send_clcc = 1;
1467667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1468667ec068SMatthias Ringwald }
1469667ec068SMatthias Ringwald 
1470667ec068SMatthias Ringwald 
1471667ec068SMatthias Ringwald /*
1472667ec068SMatthias Ringwald  * @brief
1473667ec068SMatthias Ringwald  */
1474667ec068SMatthias Ringwald void hfp_hf_rrh_query_status(bd_addr_t addr){
1475667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1476667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1477667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1478667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '?';
1479667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1480667ec068SMatthias Ringwald }
1481667ec068SMatthias Ringwald 
1482667ec068SMatthias Ringwald /*
1483667ec068SMatthias Ringwald  * @brief
1484667ec068SMatthias Ringwald  */
1485667ec068SMatthias Ringwald void hfp_hf_rrh_hold_call(bd_addr_t addr){
1486667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1487667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1488667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1489667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '0';
1490667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1491667ec068SMatthias Ringwald }
1492667ec068SMatthias Ringwald 
1493667ec068SMatthias Ringwald /*
1494667ec068SMatthias Ringwald  * @brief
1495667ec068SMatthias Ringwald  */
1496667ec068SMatthias Ringwald void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1497667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1498667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1499667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1500667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '1';
1501667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1502667ec068SMatthias Ringwald }
1503667ec068SMatthias Ringwald 
1504667ec068SMatthias Ringwald /*
1505667ec068SMatthias Ringwald  * @brief
1506667ec068SMatthias Ringwald  */
1507667ec068SMatthias Ringwald void hfp_hf_rrh_reject_held_call(bd_addr_t addr)
1508667ec068SMatthias Ringwald {
1509667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1510667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1511667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1512667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '2';
1513667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1514667ec068SMatthias Ringwald }
1515667ec068SMatthias Ringwald 
1516667ec068SMatthias Ringwald /*
1517667ec068SMatthias Ringwald  * @brief
1518667ec068SMatthias Ringwald  */
1519667ec068SMatthias Ringwald void hfp_hf_query_subscriber_number(bd_addr_t addr)
1520667ec068SMatthias Ringwald {
1521667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1522667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1523667ec068SMatthias Ringwald     connection->hf_send_cnum = 1;
1524667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1525667ec068SMatthias Ringwald }
1526667ec068SMatthias Ringwald 
1527667ec068SMatthias Ringwald /*
1528667ec068SMatthias Ringwald  * @brief
1529667ec068SMatthias Ringwald  */
1530667ec068SMatthias Ringwald void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1531667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1532667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1533667ec068SMatthias Ringwald     // find index for assigned number
1534667ec068SMatthias Ringwald     int i;
1535667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1536667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1537667ec068SMatthias Ringwald             // set value
1538667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1539667ec068SMatthias Ringwald             // mark for update
1540667ec068SMatthias Ringwald             connection->generic_status_update_bitmap |= (1<<i);
1541667ec068SMatthias Ringwald             // send update
1542667ec068SMatthias Ringwald             hfp_run_for_context(connection);
1543667ec068SMatthias Ringwald             return;
1544667ec068SMatthias Ringwald         }
1545667ec068SMatthias Ringwald     }
1546667ec068SMatthias Ringwald }
1547667ec068SMatthias Ringwald 
1548