xref: /btstack/src/classic/hfp_hf.c (revision 16ece13520cb8efcf94cb63eab58a3eda87f40a2)
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 
443deb3ec6SMatthias 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 
513deb3ec6SMatthias Ringwald #include "hci_cmds.h"
523deb3ec6SMatthias Ringwald #include "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"
60*16ece135SMatthias 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 
863deb3ec6SMatthias Ringwald void hfp_hf_register_packet_handler(hfp_callback_t callback){
873deb3ec6SMatthias Ringwald     hfp_callback = callback;
883deb3ec6SMatthias Ringwald     if (callback == NULL){
893deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
903deb3ec6SMatthias Ringwald         return;
913deb3ec6SMatthias Ringwald     }
923deb3ec6SMatthias Ringwald     hfp_callback = callback;
933deb3ec6SMatthias Ringwald }
943deb3ec6SMatthias Ringwald 
953deb3ec6SMatthias Ringwald static int hfp_hf_supports_codec(uint8_t codec){
963deb3ec6SMatthias Ringwald     int i;
973deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_codecs_nr; i++){
983deb3ec6SMatthias Ringwald         if (hfp_codecs[i] == codec) return 1;
993deb3ec6SMatthias Ringwald     }
100ce263fc8SMatthias Ringwald     return HFP_CODEC_CVSD;
1013deb3ec6SMatthias Ringwald }
1023deb3ec6SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * connection){
1033deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1043deb3ec6SMatthias Ringwald     int ag = get_bit(connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1053deb3ec6SMatthias Ringwald     return hf && ag;
1063deb3ec6SMatthias Ringwald }
1073deb3ec6SMatthias Ringwald 
1083deb3ec6SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * connection){
1093deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1103deb3ec6SMatthias Ringwald     int ag = get_bit(connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1113deb3ec6SMatthias Ringwald     return hf && ag;
1123deb3ec6SMatthias Ringwald }
1133deb3ec6SMatthias Ringwald 
1143deb3ec6SMatthias Ringwald 
1153deb3ec6SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * connection){
1163deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
1173deb3ec6SMatthias Ringwald     int ag = get_bit(connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1183deb3ec6SMatthias Ringwald     return hf && ag;
1193deb3ec6SMatthias Ringwald }
1203deb3ec6SMatthias Ringwald 
121ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1223deb3ec6SMatthias Ringwald 
1232ef54b27SMatthias 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){
1243deb3ec6SMatthias Ringwald     if (!name){
1253deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1263deb3ec6SMatthias Ringwald     }
1272ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name);
1283deb3ec6SMatthias Ringwald 
129aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
130aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
131aa4dd815SMatthias Ringwald }
1323deb3ec6SMatthias Ringwald 
1333deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
1343deb3ec6SMatthias Ringwald     char buffer[20];
1353deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
1363deb3ec6SMatthias Ringwald     // printf("exchange_supported_features %s\n", buffer);
1373deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1383deb3ec6SMatthias Ringwald }
1393deb3ec6SMatthias Ringwald 
1403deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
1413deb3ec6SMatthias Ringwald     char buffer[30];
1423deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS);
1433deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr);
1443deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1453deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1463deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1473deb3ec6SMatthias Ringwald }
1483deb3ec6SMatthias Ringwald 
1493deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
1503deb3ec6SMatthias Ringwald     char buffer[20];
1513deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR);
1523deb3ec6SMatthias Ringwald     // printf("retrieve_indicators %s\n", buffer);
1533deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1543deb3ec6SMatthias Ringwald }
1553deb3ec6SMatthias Ringwald 
1563deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
1573deb3ec6SMatthias Ringwald     char buffer[20];
1583deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR);
1593deb3ec6SMatthias Ringwald     // printf("retrieve_indicators_status %s\n", buffer);
1603deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
1633deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
1643deb3ec6SMatthias Ringwald     char buffer[20];
1653deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
1663deb3ec6SMatthias Ringwald     // printf("toggle_indicator_status_update %s\n", buffer);
1673deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1683deb3ec6SMatthias Ringwald }
1693deb3ec6SMatthias Ringwald 
1703deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
1713deb3ec6SMatthias Ringwald     char buffer[50];
1723deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
1733deb3ec6SMatthias Ringwald     offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr);
1743deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1753deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1763deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1773deb3ec6SMatthias Ringwald }
1783deb3ec6SMatthias Ringwald 
1793deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
1803deb3ec6SMatthias Ringwald     char buffer[20];
1813deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
1823deb3ec6SMatthias Ringwald     // printf("retrieve_can_hold_call %s\n", buffer);
1833deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1843deb3ec6SMatthias Ringwald }
1853deb3ec6SMatthias Ringwald 
1863deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
1873deb3ec6SMatthias Ringwald     char buffer[30];
1883deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
1893deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr);
1903deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1913deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1923deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1933deb3ec6SMatthias Ringwald }
1943deb3ec6SMatthias Ringwald 
1953deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
1963deb3ec6SMatthias Ringwald     char buffer[20];
1973deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR);
1983deb3ec6SMatthias Ringwald     // printf("retrieve_supported_generic_status_indicators %s\n", buffer);
1993deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2003deb3ec6SMatthias Ringwald }
2013deb3ec6SMatthias Ringwald 
2023deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
2033deb3ec6SMatthias Ringwald     char buffer[20];
2043deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2053deb3ec6SMatthias Ringwald     // printf("list_initital_supported_generic_status_indicators %s\n", buffer);
2063deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2073deb3ec6SMatthias Ringwald }
2083deb3ec6SMatthias Ringwald 
2093deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
2103deb3ec6SMatthias Ringwald     char buffer[20];
2113deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION);
2123deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
2153deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
2163deb3ec6SMatthias Ringwald     char buffer[20];
2173deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION);
2183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2193deb3ec6SMatthias Ringwald }
2203deb3ec6SMatthias Ringwald 
2213deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
2223deb3ec6SMatthias Ringwald     char buffer[20];
2233deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
2243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
2273deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
2283deb3ec6SMatthias Ringwald     char buffer[20];
2293deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP);
2303deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2313deb3ec6SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald 
2333deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
2343deb3ec6SMatthias Ringwald     char buffer[20];
2353deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
2363deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2373deb3ec6SMatthias Ringwald }
2383deb3ec6SMatthias Ringwald 
239ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
240ce263fc8SMatthias Ringwald     char buffer[10];
241ce263fc8SMatthias Ringwald     sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED);
242ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
243ce263fc8SMatthias Ringwald }
244ce263fc8SMatthias Ringwald 
245ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
246ce263fc8SMatthias Ringwald     char buffer[40];
247ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
248ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
249ce263fc8SMatthias Ringwald }
250ce263fc8SMatthias Ringwald 
251ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
252ce263fc8SMatthias Ringwald     char buffer[40];
253ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
254ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
255ce263fc8SMatthias Ringwald }
256ce263fc8SMatthias Ringwald 
257ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
258ce263fc8SMatthias Ringwald     char buffer[40];
259ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate);
260ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
261ce263fc8SMatthias Ringwald }
262ce263fc8SMatthias Ringwald 
263ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
264ce263fc8SMatthias Ringwald     char buffer[40];
265ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate);
266ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
267ce263fc8SMatthias Ringwald }
268ce263fc8SMatthias Ringwald 
269ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
270ce263fc8SMatthias Ringwald     char buffer[40];
271ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate);
272ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
273ce263fc8SMatthias Ringwald }
274ce263fc8SMatthias Ringwald 
275ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
276ce263fc8SMatthias Ringwald     char buffer[40];
277ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
278ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
279ce263fc8SMatthias Ringwald }
280ce263fc8SMatthias Ringwald 
281ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
282ce263fc8SMatthias Ringwald     char buffer[40];
283ce263fc8SMatthias Ringwald     sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
284ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
285ce263fc8SMatthias Ringwald }
286ce263fc8SMatthias Ringwald 
287ce263fc8SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid){
288ce263fc8SMatthias Ringwald     char buffer[40];
289ce263fc8SMatthias Ringwald     sprintf(buffer, "%s>%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
290ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
291ce263fc8SMatthias Ringwald }
292ce263fc8SMatthias Ringwald 
293ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
294ce263fc8SMatthias Ringwald     char buffer[20];
295ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER);
296ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
297ce263fc8SMatthias Ringwald }
298ce263fc8SMatthias Ringwald 
299ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){
300ce263fc8SMatthias Ringwald     char buffer[20];
301ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL);
302ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
303ce263fc8SMatthias Ringwald }
304ce263fc8SMatthias Ringwald 
305ce263fc8SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, int number){
306ce263fc8SMatthias Ringwald     char buffer[20];
307ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
308ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
309ce263fc8SMatthias Ringwald }
310ce263fc8SMatthias Ringwald 
311ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
312ce263fc8SMatthias Ringwald     char buffer[20];
313ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
314ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
315ce263fc8SMatthias Ringwald }
316ce263fc8SMatthias Ringwald 
317ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
318ce263fc8SMatthias Ringwald     char buffer[20];
319ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG);
320ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
321ce263fc8SMatthias Ringwald }
322ce263fc8SMatthias Ringwald 
323667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
324667ec068SMatthias Ringwald     char buffer[20];
325667ec068SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS);
326667ec068SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
327667ec068SMatthias Ringwald }
328667ec068SMatthias Ringwald 
3293deb3ec6SMatthias Ringwald static void hfp_emit_ag_indicator_event(hfp_callback_t callback, int status, hfp_ag_indicator_t indicator){
3303deb3ec6SMatthias Ringwald     if (!callback) return;
331ce263fc8SMatthias Ringwald     uint8_t event[6+HFP_MAX_INDICATOR_DESC_SIZE+1];
3323deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3333deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3343deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
3353deb3ec6SMatthias Ringwald     event[3] = status;
3363deb3ec6SMatthias Ringwald     event[4] = indicator.index;
3373deb3ec6SMatthias Ringwald     event[5] = indicator.status;
338ce263fc8SMatthias Ringwald     strncpy((char*)&event[6], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
339ce263fc8SMatthias Ringwald     event[6+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
3403deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3413deb3ec6SMatthias Ringwald }
3423deb3ec6SMatthias Ringwald 
3433deb3ec6SMatthias Ringwald static void hfp_emit_network_operator_event(hfp_callback_t callback, int status, hfp_network_opearator_t network_operator){
3443deb3ec6SMatthias Ringwald     if (!callback) return;
3453deb3ec6SMatthias Ringwald     uint8_t event[24];
3463deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3473deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3483deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
3493deb3ec6SMatthias Ringwald     event[3] = status;
3503deb3ec6SMatthias Ringwald     event[4] = network_operator.mode;
3513deb3ec6SMatthias Ringwald     event[5] = network_operator.format;
3523deb3ec6SMatthias Ringwald     strcpy((char*)&event[6], network_operator.name);
3533deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3543deb3ec6SMatthias Ringwald }
3553deb3ec6SMatthias Ringwald 
3563deb3ec6SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * context){
3573deb3ec6SMatthias Ringwald     if (context->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
358aa4dd815SMatthias Ringwald     if (context->ok_pending) return 0;
359aa4dd815SMatthias Ringwald     int done = 1;
3603deb3ec6SMatthias Ringwald 
3613deb3ec6SMatthias Ringwald     switch (context->state){
3623deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
3633deb3ec6SMatthias Ringwald             context->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
364aa4dd815SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(context->rfcomm_cid);
3653deb3ec6SMatthias Ringwald             break;
3663deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
3673deb3ec6SMatthias Ringwald             context->state = HFP_W4_NOTIFY_ON_CODECS;
368aa4dd815SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
3693deb3ec6SMatthias Ringwald             break;
3703deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
3713deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_INDICATORS;
372aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(context->rfcomm_cid);
3733deb3ec6SMatthias Ringwald             break;
3743deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
3753deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
376aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(context->rfcomm_cid);
3773deb3ec6SMatthias Ringwald             break;
3783deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
3793deb3ec6SMatthias Ringwald             context->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
380aa4dd815SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, 1);
3813deb3ec6SMatthias Ringwald             break;
3823deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
3833deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
384aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(context->rfcomm_cid);
3853deb3ec6SMatthias Ringwald             break;
3863deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
3873deb3ec6SMatthias Ringwald             context->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
388aa4dd815SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(context->rfcomm_cid);
3893deb3ec6SMatthias Ringwald             break;
3903deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
3913deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
392aa4dd815SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(context->rfcomm_cid);
3933deb3ec6SMatthias Ringwald             break;
3943deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
3953deb3ec6SMatthias Ringwald             context->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
396aa4dd815SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(context->rfcomm_cid);
3973deb3ec6SMatthias Ringwald             break;
3983deb3ec6SMatthias Ringwald         default:
399aa4dd815SMatthias Ringwald             done = 0;
4003deb3ec6SMatthias Ringwald             break;
4013deb3ec6SMatthias Ringwald     }
4023deb3ec6SMatthias Ringwald     return done;
4033deb3ec6SMatthias Ringwald }
4043deb3ec6SMatthias Ringwald 
405ce263fc8SMatthias Ringwald 
406ce263fc8SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * context){
407ce263fc8SMatthias Ringwald     if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
408ce263fc8SMatthias Ringwald     if (context->ok_pending) return 0;
409ce263fc8SMatthias Ringwald 
410ce263fc8SMatthias Ringwald     int done = 0;
411ce263fc8SMatthias Ringwald     if (context->enable_status_update_for_ag_indicators != 0xFF){
412ce263fc8SMatthias Ringwald         context->ok_pending = 1;
413ce263fc8SMatthias Ringwald         done = 1;
414ce263fc8SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, context->enable_status_update_for_ag_indicators);
415ce263fc8SMatthias Ringwald         return done;
416ce263fc8SMatthias Ringwald     };
417ce263fc8SMatthias Ringwald     if (context->change_status_update_for_individual_ag_indicators){
418ce263fc8SMatthias Ringwald         context->ok_pending = 1;
419ce263fc8SMatthias Ringwald         done = 1;
420ce263fc8SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(context->rfcomm_cid,
421ce263fc8SMatthias Ringwald                 context->ag_indicators_status_update_bitmap,
422ce263fc8SMatthias Ringwald                 context->ag_indicators_nr);
423ce263fc8SMatthias Ringwald         return done;
424ce263fc8SMatthias Ringwald     }
425ce263fc8SMatthias Ringwald 
426ce263fc8SMatthias Ringwald     switch (context->hf_query_operator_state){
427ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
428ce263fc8SMatthias Ringwald             context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
429ce263fc8SMatthias Ringwald             context->ok_pending = 1;
430ce263fc8SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(context->rfcomm_cid);
431ce263fc8SMatthias Ringwald             return 1;
432ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
433ce263fc8SMatthias Ringwald             context->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
434ce263fc8SMatthias Ringwald             context->ok_pending = 1;
435ce263fc8SMatthias Ringwald             hfp_hf_cmd_query_operator_name(context->rfcomm_cid);
436ce263fc8SMatthias Ringwald             return 1;
437ce263fc8SMatthias Ringwald         default:
438ce263fc8SMatthias Ringwald             break;
439ce263fc8SMatthias Ringwald     }
440ce263fc8SMatthias Ringwald 
441ce263fc8SMatthias Ringwald     if (context->enable_extended_audio_gateway_error_report){
442ce263fc8SMatthias Ringwald         context->ok_pending = 1;
443ce263fc8SMatthias Ringwald         done = 1;
444ce263fc8SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(context->rfcomm_cid, context->enable_extended_audio_gateway_error_report);
445ce263fc8SMatthias Ringwald         return done;
446ce263fc8SMatthias Ringwald     }
447ce263fc8SMatthias Ringwald 
448ce263fc8SMatthias Ringwald     return done;
449ce263fc8SMatthias Ringwald }
450ce263fc8SMatthias Ringwald 
451ce263fc8SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * context){
452ce263fc8SMatthias Ringwald     /* events ( == commands):
453ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
454ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
455ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
456ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
457ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
458ce263fc8SMatthias Ringwald     */
459ce263fc8SMatthias Ringwald 
460ce263fc8SMatthias Ringwald     if (context->ok_pending) return 0;
461ce263fc8SMatthias Ringwald 
462ce263fc8SMatthias Ringwald     switch (context->command){
463ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
464ce263fc8SMatthias Ringwald             if (context->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
465ce263fc8SMatthias Ringwald 
466ce263fc8SMatthias Ringwald             context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
467ce263fc8SMatthias Ringwald             context->ok_pending = 1;
468ce263fc8SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
469ce263fc8SMatthias Ringwald             return 1;
470ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
471ce263fc8SMatthias Ringwald             context->codec_confirmed = 0;
472ce263fc8SMatthias Ringwald             context->suggested_codec = 0;
473ce263fc8SMatthias Ringwald             context->negotiated_codec = 0;
474ce263fc8SMatthias Ringwald 
475ce263fc8SMatthias Ringwald             context->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
476ce263fc8SMatthias Ringwald             context->ok_pending = 1;
477ce263fc8SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(context->rfcomm_cid);
478ce263fc8SMatthias Ringwald             break;
479ce263fc8SMatthias Ringwald 
480ce263fc8SMatthias Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:
481ce263fc8SMatthias Ringwald             if (hfp_hf_supports_codec(context->suggested_codec)){
482ce263fc8SMatthias Ringwald                 context->codec_confirmed = context->suggested_codec;
483ce263fc8SMatthias Ringwald                 context->ok_pending = 1;
484ce263fc8SMatthias Ringwald                 context->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
485ce263fc8SMatthias Ringwald                 hfp_hf_cmd_confirm_codec(context->rfcomm_cid, context->suggested_codec);
486ce263fc8SMatthias Ringwald             } else {
487ce263fc8SMatthias Ringwald                 context->codec_confirmed = 0;
488ce263fc8SMatthias Ringwald                 context->suggested_codec = 0;
489ce263fc8SMatthias Ringwald                 context->negotiated_codec = 0;
490ce263fc8SMatthias Ringwald                 context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
491ce263fc8SMatthias Ringwald                 context->ok_pending = 1;
492ce263fc8SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid);
493ce263fc8SMatthias Ringwald 
494ce263fc8SMatthias Ringwald             }
495ce263fc8SMatthias Ringwald             break;
496ce263fc8SMatthias Ringwald 
497ce263fc8SMatthias Ringwald         default:
498ce263fc8SMatthias Ringwald             break;
499ce263fc8SMatthias Ringwald     }
500ce263fc8SMatthias Ringwald     return 0;
501ce263fc8SMatthias Ringwald }
502ce263fc8SMatthias Ringwald 
503ce263fc8SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * context){
504ce263fc8SMatthias Ringwald     if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
505ce263fc8SMatthias Ringwald         context->state > HFP_W2_DISCONNECT_SCO) return 0;
506ce263fc8SMatthias Ringwald 
507ce263fc8SMatthias Ringwald 
508ce263fc8SMatthias Ringwald     if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED && context->release_audio_connection){
509ce263fc8SMatthias Ringwald         context->state = HFP_W4_SCO_DISCONNECTED;
510ce263fc8SMatthias Ringwald         context->release_audio_connection = 0;
511ce263fc8SMatthias Ringwald         gap_disconnect(context->sco_handle);
512ce263fc8SMatthias Ringwald         return 1;
513ce263fc8SMatthias Ringwald     }
514ce263fc8SMatthias Ringwald 
515ce263fc8SMatthias Ringwald     if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
516ce263fc8SMatthias Ringwald 
517ce263fc8SMatthias Ringwald     // run codecs exchange
518ce263fc8SMatthias Ringwald     int done = codecs_exchange_state_machine(context);
519ce263fc8SMatthias Ringwald     if (done) return 1;
520ce263fc8SMatthias Ringwald 
521ce263fc8SMatthias Ringwald     if (context->establish_audio_connection){
522ce263fc8SMatthias Ringwald         context->state = HFP_W4_SCO_CONNECTED;
523ce263fc8SMatthias Ringwald         context->establish_audio_connection = 0;
524ce263fc8SMatthias Ringwald         hfp_setup_synchronous_connection(context->con_handle, context->link_setting);
525ce263fc8SMatthias Ringwald         return 1;
526ce263fc8SMatthias Ringwald     }
527ce263fc8SMatthias Ringwald 
528ce263fc8SMatthias Ringwald     return 0;
529ce263fc8SMatthias Ringwald }
530ce263fc8SMatthias Ringwald 
531ce263fc8SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * context){
532ce263fc8SMatthias Ringwald     if (context->hf_answer_incoming_call){
533ce263fc8SMatthias Ringwald         hfp_hf_cmd_ata(context->rfcomm_cid);
534ce263fc8SMatthias Ringwald         context->hf_answer_incoming_call = 0;
535ce263fc8SMatthias Ringwald         return 1;
536ce263fc8SMatthias Ringwald     }
537ce263fc8SMatthias Ringwald     return 0;
538ce263fc8SMatthias Ringwald }
539ce263fc8SMatthias Ringwald 
540ce263fc8SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * context){
541ce263fc8SMatthias Ringwald     if (!context) return;
542ce263fc8SMatthias Ringwald     if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return;
543ce263fc8SMatthias Ringwald 
544ce263fc8SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(context);
545ce263fc8SMatthias Ringwald     if (!done){
546ce263fc8SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(context);
547ce263fc8SMatthias Ringwald     }
548ce263fc8SMatthias Ringwald     if (!done){
549ce263fc8SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(context);
550ce263fc8SMatthias Ringwald     }
551ce263fc8SMatthias Ringwald     if (!done){
552ce263fc8SMatthias Ringwald         done = call_setup_state_machine(context);
553ce263fc8SMatthias Ringwald     }
554ce263fc8SMatthias Ringwald 
555ce263fc8SMatthias Ringwald     if (context->send_microphone_gain){
556ce263fc8SMatthias Ringwald         context->send_microphone_gain = 0;
557ce263fc8SMatthias Ringwald         context->ok_pending = 1;
558ce263fc8SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(context->rfcomm_cid, context->microphone_gain);
559ce263fc8SMatthias Ringwald         return;
560ce263fc8SMatthias Ringwald     }
561ce263fc8SMatthias Ringwald 
562ce263fc8SMatthias Ringwald     if (context->send_speaker_gain){
563ce263fc8SMatthias Ringwald         context->send_speaker_gain = 0;
564ce263fc8SMatthias Ringwald         context->ok_pending = 1;
565ce263fc8SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(context->rfcomm_cid, context->speaker_gain);
566ce263fc8SMatthias Ringwald         return;
567ce263fc8SMatthias Ringwald     }
568ce263fc8SMatthias Ringwald 
569ce263fc8SMatthias Ringwald     if (context->hf_deactivate_calling_line_notification){
570ce263fc8SMatthias Ringwald         context->hf_deactivate_calling_line_notification = 0;
571ce263fc8SMatthias Ringwald         context->ok_pending = 1;
572ce263fc8SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(context->rfcomm_cid, 0);
573ce263fc8SMatthias Ringwald         return;
574ce263fc8SMatthias Ringwald     }
575ce263fc8SMatthias Ringwald 
576ce263fc8SMatthias Ringwald     if (context->hf_activate_calling_line_notification){
577ce263fc8SMatthias Ringwald         context->hf_activate_calling_line_notification = 0;
578ce263fc8SMatthias Ringwald         context->ok_pending = 1;
579ce263fc8SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(context->rfcomm_cid, 1);
580ce263fc8SMatthias Ringwald         return;
581ce263fc8SMatthias Ringwald     }
582ce263fc8SMatthias Ringwald 
583ce263fc8SMatthias Ringwald     if (context->hf_deactivate_echo_canceling_and_noise_reduction){
584ce263fc8SMatthias Ringwald         context->hf_deactivate_echo_canceling_and_noise_reduction = 0;
585ce263fc8SMatthias Ringwald         context->ok_pending = 1;
586ce263fc8SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(context->rfcomm_cid, 0);
587ce263fc8SMatthias Ringwald         return;
588ce263fc8SMatthias Ringwald     }
589ce263fc8SMatthias Ringwald 
590ce263fc8SMatthias Ringwald     if (context->hf_activate_echo_canceling_and_noise_reduction){
591ce263fc8SMatthias Ringwald         context->hf_activate_echo_canceling_and_noise_reduction = 0;
592ce263fc8SMatthias Ringwald         context->ok_pending = 1;
593ce263fc8SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(context->rfcomm_cid, 1);
594ce263fc8SMatthias Ringwald         return;
595ce263fc8SMatthias Ringwald     }
596ce263fc8SMatthias Ringwald 
597ce263fc8SMatthias Ringwald     if (context->hf_deactivate_voice_recognition_notification){
598ce263fc8SMatthias Ringwald         context->hf_deactivate_voice_recognition_notification = 0;
599ce263fc8SMatthias Ringwald         context->ok_pending = 1;
600ce263fc8SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(context->rfcomm_cid, 0);
601ce263fc8SMatthias Ringwald         return;
602ce263fc8SMatthias Ringwald     }
603ce263fc8SMatthias Ringwald 
604ce263fc8SMatthias Ringwald     if (context->hf_activate_voice_recognition_notification){
605ce263fc8SMatthias Ringwald         context->hf_activate_voice_recognition_notification = 0;
606ce263fc8SMatthias Ringwald         context->ok_pending = 1;
607ce263fc8SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(context->rfcomm_cid, 1);
608ce263fc8SMatthias Ringwald         return;
609ce263fc8SMatthias Ringwald     }
610ce263fc8SMatthias Ringwald 
611ce263fc8SMatthias Ringwald 
612ce263fc8SMatthias Ringwald     if (context->hf_deactivate_call_waiting_notification){
613ce263fc8SMatthias Ringwald         context->hf_deactivate_call_waiting_notification = 0;
614ce263fc8SMatthias Ringwald         context->ok_pending = 1;
615ce263fc8SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(context->rfcomm_cid, 0);
616ce263fc8SMatthias Ringwald         return;
617ce263fc8SMatthias Ringwald     }
618ce263fc8SMatthias Ringwald 
619ce263fc8SMatthias Ringwald     if (context->hf_activate_call_waiting_notification){
620ce263fc8SMatthias Ringwald         context->hf_activate_call_waiting_notification = 0;
621ce263fc8SMatthias Ringwald         context->ok_pending = 1;
622ce263fc8SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(context->rfcomm_cid, 1);
623ce263fc8SMatthias Ringwald         return;
624ce263fc8SMatthias Ringwald     }
625ce263fc8SMatthias Ringwald 
626ce263fc8SMatthias Ringwald     if (context->hf_initiate_outgoing_call){
627ce263fc8SMatthias Ringwald         context->hf_initiate_outgoing_call = 0;
628ce263fc8SMatthias Ringwald         context->ok_pending = 1;
629ce263fc8SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(context->rfcomm_cid);
630ce263fc8SMatthias Ringwald         return;
631ce263fc8SMatthias Ringwald     }
632ce263fc8SMatthias Ringwald 
633ce263fc8SMatthias Ringwald     if (context->hf_initiate_memory_dialing){
634ce263fc8SMatthias Ringwald         context->hf_initiate_memory_dialing = 0;
635ce263fc8SMatthias Ringwald         context->ok_pending = 1;
636ce263fc8SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(context->rfcomm_cid);
637ce263fc8SMatthias Ringwald         return;
638ce263fc8SMatthias Ringwald     }
639ce263fc8SMatthias Ringwald 
640ce263fc8SMatthias Ringwald     if (context->hf_initiate_redial_last_number){
641ce263fc8SMatthias Ringwald         context->hf_initiate_redial_last_number = 0;
642ce263fc8SMatthias Ringwald         context->ok_pending = 1;
643ce263fc8SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(context->rfcomm_cid);
644ce263fc8SMatthias Ringwald         return;
645ce263fc8SMatthias Ringwald     }
646ce263fc8SMatthias Ringwald 
647ce263fc8SMatthias Ringwald     if (context->hf_send_chup){
648ce263fc8SMatthias Ringwald         context->hf_send_chup = 0;
649ce263fc8SMatthias Ringwald         context->ok_pending = 1;
650ce263fc8SMatthias Ringwald         hfp_hf_send_chup(context->rfcomm_cid);
651ce263fc8SMatthias Ringwald         return;
652ce263fc8SMatthias Ringwald     }
653ce263fc8SMatthias Ringwald 
654ce263fc8SMatthias Ringwald     if (context->hf_send_chld_0){
655ce263fc8SMatthias Ringwald         context->hf_send_chld_0 = 0;
656ce263fc8SMatthias Ringwald         context->ok_pending = 1;
657ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 0);
658ce263fc8SMatthias Ringwald         return;
659ce263fc8SMatthias Ringwald     }
660ce263fc8SMatthias Ringwald 
661ce263fc8SMatthias Ringwald     if (context->hf_send_chld_1){
662ce263fc8SMatthias Ringwald         context->hf_send_chld_1 = 0;
663ce263fc8SMatthias Ringwald         context->ok_pending = 1;
664ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 1);
665ce263fc8SMatthias Ringwald         return;
666ce263fc8SMatthias Ringwald     }
667ce263fc8SMatthias Ringwald 
668ce263fc8SMatthias Ringwald     if (context->hf_send_chld_2){
669ce263fc8SMatthias Ringwald         context->hf_send_chld_2 = 0;
670ce263fc8SMatthias Ringwald         context->ok_pending = 1;
671ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 2);
672ce263fc8SMatthias Ringwald         return;
673ce263fc8SMatthias Ringwald     }
674ce263fc8SMatthias Ringwald 
675ce263fc8SMatthias Ringwald     if (context->hf_send_chld_3){
676ce263fc8SMatthias Ringwald         context->hf_send_chld_3 = 0;
677ce263fc8SMatthias Ringwald         context->ok_pending = 1;
678ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 3);
679ce263fc8SMatthias Ringwald         return;
680ce263fc8SMatthias Ringwald     }
681ce263fc8SMatthias Ringwald 
682ce263fc8SMatthias Ringwald     if (context->hf_send_chld_4){
683ce263fc8SMatthias Ringwald         context->hf_send_chld_4 = 0;
684ce263fc8SMatthias Ringwald         context->ok_pending = 1;
685ce263fc8SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, 4);
686ce263fc8SMatthias Ringwald         return;
687ce263fc8SMatthias Ringwald     }
688ce263fc8SMatthias Ringwald 
689667ec068SMatthias Ringwald     if (context->hf_send_chld_x){
690667ec068SMatthias Ringwald         context->hf_send_chld_x = 0;
691667ec068SMatthias Ringwald         context->ok_pending = 1;
692667ec068SMatthias Ringwald         hfp_hf_send_chld(context->rfcomm_cid, context->hf_send_chld_x_index);
693667ec068SMatthias Ringwald         return;
694667ec068SMatthias Ringwald     }
695667ec068SMatthias Ringwald 
696ce263fc8SMatthias Ringwald     if (context->hf_send_dtmf_code){
697ce263fc8SMatthias Ringwald         char code = context->hf_send_dtmf_code;
698ce263fc8SMatthias Ringwald         context->hf_send_dtmf_code = 0;
699ce263fc8SMatthias Ringwald         context->ok_pending = 1;
700ce263fc8SMatthias Ringwald         hfp_hf_send_dtmf(context->rfcomm_cid, code);
701ce263fc8SMatthias Ringwald         return;
702ce263fc8SMatthias Ringwald     }
703ce263fc8SMatthias Ringwald 
704ce263fc8SMatthias Ringwald     if (context->hf_send_binp){
705ce263fc8SMatthias Ringwald         context->hf_send_binp = 0;
706ce263fc8SMatthias Ringwald         context->ok_pending = 1;
707ce263fc8SMatthias Ringwald         hfp_hf_send_binp(context->rfcomm_cid);
708ce263fc8SMatthias Ringwald         return;
709ce263fc8SMatthias Ringwald     }
710ce263fc8SMatthias Ringwald 
711667ec068SMatthias Ringwald     if (context->hf_send_clcc){
712667ec068SMatthias Ringwald         context->hf_send_clcc = 0;
713667ec068SMatthias Ringwald         context->ok_pending = 1;
714667ec068SMatthias Ringwald         hfp_hf_send_clcc(context->rfcomm_cid);
715667ec068SMatthias Ringwald         return;
716667ec068SMatthias Ringwald     }
717667ec068SMatthias Ringwald 
718667ec068SMatthias Ringwald     if (context->hf_send_rrh){
719667ec068SMatthias Ringwald         context->hf_send_rrh = 0;
720667ec068SMatthias Ringwald         char buffer[20];
721667ec068SMatthias Ringwald         switch (context->hf_send_rrh_command){
722667ec068SMatthias Ringwald             case '?':
723667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
724667ec068SMatthias Ringwald                 send_str_over_rfcomm(context->rfcomm_cid, buffer);
725667ec068SMatthias Ringwald                 return;
726667ec068SMatthias Ringwald             case '0':
727667ec068SMatthias Ringwald             case '1':
728667ec068SMatthias Ringwald             case '2':
729667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, context->hf_send_rrh_command);
730667ec068SMatthias Ringwald                 send_str_over_rfcomm(context->rfcomm_cid, buffer);
731667ec068SMatthias Ringwald                 return;
732667ec068SMatthias Ringwald             default:
733667ec068SMatthias Ringwald                 break;
734667ec068SMatthias Ringwald         }
735667ec068SMatthias Ringwald         return;
736667ec068SMatthias Ringwald     }
737667ec068SMatthias Ringwald 
738667ec068SMatthias Ringwald     if (context->hf_send_cnum){
739667ec068SMatthias Ringwald         context->hf_send_cnum = 0;
740667ec068SMatthias Ringwald         char buffer[20];
741667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
742667ec068SMatthias Ringwald         send_str_over_rfcomm(context->rfcomm_cid, buffer);
743667ec068SMatthias Ringwald         return;
744667ec068SMatthias Ringwald     }
745667ec068SMatthias Ringwald 
746667ec068SMatthias Ringwald     // update HF indicators
747667ec068SMatthias Ringwald     if (context->generic_status_update_bitmap){
748667ec068SMatthias Ringwald         int i;
749667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
750667ec068SMatthias Ringwald             if (get_bit(context->generic_status_update_bitmap, i)){
751667ec068SMatthias Ringwald                 if (context->generic_status_indicators[i].state){
752667ec068SMatthias Ringwald                     context->ok_pending = 1;
753667ec068SMatthias Ringwald                     context->generic_status_update_bitmap = store_bit(context->generic_status_update_bitmap, i, 0);
754667ec068SMatthias Ringwald                     char buffer[30];
755667ec068SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]);
756667ec068SMatthias Ringwald                     send_str_over_rfcomm(context->rfcomm_cid, buffer);
757667ec068SMatthias Ringwald                 } else {
758667ec068SMatthias Ringwald                     printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]);
759667ec068SMatthias Ringwald                 }
760667ec068SMatthias Ringwald                 return;
761667ec068SMatthias Ringwald             }
762667ec068SMatthias Ringwald         }
763667ec068SMatthias Ringwald     }
764667ec068SMatthias Ringwald 
765ce263fc8SMatthias Ringwald     if (done) return;
766ce263fc8SMatthias Ringwald     // deal with disconnect
767ce263fc8SMatthias Ringwald     switch (context->state){
768ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
769ce263fc8SMatthias Ringwald             context->state = HFP_W4_RFCOMM_DISCONNECTED;
770ce263fc8SMatthias Ringwald             rfcomm_disconnect_internal(context->rfcomm_cid);
771ce263fc8SMatthias Ringwald             break;
772ce263fc8SMatthias Ringwald 
773ce263fc8SMatthias Ringwald         default:
774ce263fc8SMatthias Ringwald             break;
775ce263fc8SMatthias Ringwald     }
776ce263fc8SMatthias Ringwald }
777ce263fc8SMatthias Ringwald 
778ce263fc8SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * context){
779ce263fc8SMatthias Ringwald     // determine highest possible link setting
780ce263fc8SMatthias Ringwald     context->link_setting = HFP_LINK_SETTINGS_D1;
781ce263fc8SMatthias Ringwald     if (hci_remote_eSCO_supported(context->con_handle)){
782ce263fc8SMatthias Ringwald         context->link_setting = HFP_LINK_SETTINGS_S3;
783ce263fc8SMatthias Ringwald         if ((hfp_supported_features             & (1<<HFP_HFSF_ESCO_S4))
784ce263fc8SMatthias Ringwald         &&  (context->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){
785ce263fc8SMatthias Ringwald             context->link_setting = HFP_LINK_SETTINGS_S4;
786ce263fc8SMatthias Ringwald         }
787ce263fc8SMatthias Ringwald     }
788ce263fc8SMatthias Ringwald }
789ce263fc8SMatthias Ringwald 
790ce263fc8SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * context){
791ce263fc8SMatthias Ringwald     context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
792ce263fc8SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
793ce263fc8SMatthias Ringwald     hfp_init_link_settings(context);
794667ec068SMatthias Ringwald     // restore volume settings
795667ec068SMatthias Ringwald     context->speaker_gain = hfp_hf_speaker_gain;
796667ec068SMatthias Ringwald     context->send_speaker_gain = 1;
797667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
798667ec068SMatthias Ringwald     context->microphone_gain = hfp_hf_microphone_gain;
799667ec068SMatthias Ringwald     context->send_microphone_gain = 1;
800667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
801667ec068SMatthias Ringwald     // enable all indicators
802667ec068SMatthias Ringwald     int i;
803667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
804667ec068SMatthias Ringwald         context->generic_status_indicators[i].uuid = hfp_indicators[i];
805667ec068SMatthias Ringwald         context->generic_status_indicators[i].state = 1;
806667ec068SMatthias Ringwald     }
807ce263fc8SMatthias Ringwald }
808ce263fc8SMatthias Ringwald 
809ce263fc8SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *context){
810ce263fc8SMatthias Ringwald     context->ok_pending = 0;
811ce263fc8SMatthias Ringwald     int done = 1;
8123deb3ec6SMatthias Ringwald     switch (context->state){
8133deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
8143deb3ec6SMatthias Ringwald             if (has_codec_negotiation_feature(context)){
8153deb3ec6SMatthias Ringwald                 context->state = HFP_NOTIFY_ON_CODECS;
8163deb3ec6SMatthias Ringwald                 break;
8173deb3ec6SMatthias Ringwald             }
8183deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INDICATORS;
8193deb3ec6SMatthias Ringwald             break;
8203deb3ec6SMatthias Ringwald 
8213deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
8223deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INDICATORS;
8233deb3ec6SMatthias Ringwald             break;
8243deb3ec6SMatthias Ringwald 
8253deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
8263deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INDICATORS_STATUS;
8273deb3ec6SMatthias Ringwald             break;
8283deb3ec6SMatthias Ringwald 
8293deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
8303deb3ec6SMatthias Ringwald             context->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8313deb3ec6SMatthias Ringwald             break;
8323deb3ec6SMatthias Ringwald 
8333deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
8343deb3ec6SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(context)){
8353deb3ec6SMatthias Ringwald                 context->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8363deb3ec6SMatthias Ringwald                 break;
8373deb3ec6SMatthias Ringwald             }
8383deb3ec6SMatthias Ringwald             if (has_hf_indicators_feature(context)){
8393deb3ec6SMatthias Ringwald                 context->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8403deb3ec6SMatthias Ringwald                 break;
8413deb3ec6SMatthias Ringwald             }
842ce263fc8SMatthias Ringwald             hfp_ag_slc_established(context);
8433deb3ec6SMatthias Ringwald             break;
8443deb3ec6SMatthias Ringwald 
8453deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
8463deb3ec6SMatthias Ringwald             if (has_hf_indicators_feature(context)){
8473deb3ec6SMatthias Ringwald                 context->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8483deb3ec6SMatthias Ringwald                 break;
8493deb3ec6SMatthias Ringwald             }
850ce263fc8SMatthias Ringwald             hfp_ag_slc_established(context);
8513deb3ec6SMatthias Ringwald             break;
8523deb3ec6SMatthias Ringwald 
8533deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
8543deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8553deb3ec6SMatthias Ringwald             break;
8563deb3ec6SMatthias Ringwald 
8573deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
8583deb3ec6SMatthias Ringwald             context->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
8593deb3ec6SMatthias Ringwald             break;
8603deb3ec6SMatthias Ringwald 
8613deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
862ce263fc8SMatthias Ringwald             hfp_ag_slc_established(context);
8633deb3ec6SMatthias Ringwald             break;
864ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
8653deb3ec6SMatthias Ringwald             if (context->enable_status_update_for_ag_indicators != 0xFF){
8663deb3ec6SMatthias Ringwald                 context->enable_status_update_for_ag_indicators = 0xFF;
8673deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
868ce263fc8SMatthias Ringwald                 break;
869ce263fc8SMatthias Ringwald             }
8703deb3ec6SMatthias Ringwald 
8713deb3ec6SMatthias Ringwald             if (context->change_status_update_for_individual_ag_indicators == 1){
8723deb3ec6SMatthias Ringwald                 context->change_status_update_for_individual_ag_indicators = 0;
8733deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
874ce263fc8SMatthias Ringwald                 break;
8753deb3ec6SMatthias Ringwald             }
8763deb3ec6SMatthias Ringwald 
877ce263fc8SMatthias Ringwald             switch (context->hf_query_operator_state){
878ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
879ce263fc8SMatthias Ringwald                     printf("Format set, querying name\n");
880ce263fc8SMatthias Ringwald                     context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
881ce263fc8SMatthias Ringwald                     break;
882ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
883ce263fc8SMatthias Ringwald                     context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
8843deb3ec6SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, 0, context->network_operator);
885ce263fc8SMatthias Ringwald                     break;
886ce263fc8SMatthias Ringwald                 default:
887ce263fc8SMatthias Ringwald                     break;
8883deb3ec6SMatthias Ringwald             }
889ce263fc8SMatthias Ringwald 
8903deb3ec6SMatthias Ringwald             if (context->enable_extended_audio_gateway_error_report){
8913deb3ec6SMatthias Ringwald                 context->enable_extended_audio_gateway_error_report = 0;
892ce263fc8SMatthias Ringwald                 break;
8933deb3ec6SMatthias Ringwald             }
8943deb3ec6SMatthias Ringwald 
895aa4dd815SMatthias Ringwald             switch (context->codecs_state){
896aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
897aa4dd815SMatthias Ringwald                     context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
8983deb3ec6SMatthias Ringwald                     break;
899ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
900ce263fc8SMatthias Ringwald                     context->codecs_state = HFP_CODECS_EXCHANGED;
901ce263fc8SMatthias Ringwald                     hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
902ce263fc8SMatthias Ringwald                     break;
9033deb3ec6SMatthias Ringwald                 default:
904ce263fc8SMatthias Ringwald                     done = 0;
9053deb3ec6SMatthias Ringwald                     break;
9063deb3ec6SMatthias Ringwald             }
9073deb3ec6SMatthias Ringwald             break;
9083deb3ec6SMatthias Ringwald         default:
909ce263fc8SMatthias Ringwald             done = 0;
9103deb3ec6SMatthias Ringwald             break;
9113deb3ec6SMatthias Ringwald     }
9123deb3ec6SMatthias Ringwald 
9133deb3ec6SMatthias Ringwald     // done
9143deb3ec6SMatthias Ringwald     context->command = HFP_CMD_NONE;
9153deb3ec6SMatthias Ringwald }
9163deb3ec6SMatthias Ringwald 
9173deb3ec6SMatthias Ringwald 
9183deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9193deb3ec6SMatthias Ringwald     hfp_connection_t * context = get_hfp_connection_context_for_rfcomm_cid(channel);
9203deb3ec6SMatthias Ringwald     if (!context) return;
9213deb3ec6SMatthias Ringwald 
9221e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9231e35c04dSMatthias Ringwald     packet[size-1] = 0;
9241e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9251e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9261e35c04dSMatthias Ringwald 
927667ec068SMatthias Ringwald     int pos, i, value;
9283deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
929aa4dd815SMatthias Ringwald         hfp_parse(context, packet[pos], 1);
930ce263fc8SMatthias Ringwald     }
9313deb3ec6SMatthias Ringwald 
932ce263fc8SMatthias Ringwald     switch (context->command){
933667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
934667ec068SMatthias Ringwald             context->command = HFP_CMD_NONE;
935667ec068SMatthias Ringwald             printf("Subscriber Number: number %s, type %u\n", context->bnip_number, context->bnip_type);
936667ec068SMatthias Ringwald             break;
937667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
938667ec068SMatthias Ringwald             context->command = HFP_CMD_NONE;
939667ec068SMatthias Ringwald             printf("Response and Hold status: %s\n", context->line_buffer);
940667ec068SMatthias Ringwald             break;
941667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
942667ec068SMatthias Ringwald             context->command = HFP_CMD_NONE;
943667ec068SMatthias Ringwald             printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
944667ec068SMatthias Ringwald                 context->clcc_idx, context->clcc_dir, context->clcc_status, context->clcc_mpty,
945667ec068SMatthias Ringwald                 context->bnip_number, context->bnip_type);
946667ec068SMatthias Ringwald             break;
947ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
948ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
949667ec068SMatthias Ringwald             value = atoi((char*)context->line_buffer);
950667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
951667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
952ce263fc8SMatthias Ringwald             break;
953ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
954ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
955667ec068SMatthias Ringwald             value = atoi((char*)context->line_buffer);
956667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
957667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
958ce263fc8SMatthias Ringwald             break;
959ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
960ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
961ce263fc8SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, context->bnip_number);
962ce263fc8SMatthias Ringwald             break;
963ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
964ce263fc8SMatthias Ringwald             context->ok_pending = 0;
965ce263fc8SMatthias Ringwald             context->extended_audio_gateway_error = 0;
966ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
967ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, context->extended_audio_gateway_error);
968ce263fc8SMatthias Ringwald             break;
969ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
970ce263fc8SMatthias Ringwald             context->ok_pending = 0;
971ce263fc8SMatthias Ringwald             hfp_reset_context_flags(context);
972ce263fc8SMatthias Ringwald             context->command = HFP_CMD_NONE;
973ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
974ce263fc8SMatthias Ringwald             break;
975ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
976ce263fc8SMatthias Ringwald             hfp_hf_switch_on_ok(context);
977ce263fc8SMatthias Ringwald             break;
978ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
979ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RING, 0);
980ce263fc8SMatthias Ringwald             break;
981ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
9823deb3ec6SMatthias Ringwald             for (i = 0; i < context->ag_indicators_nr; i++){
9833deb3ec6SMatthias Ringwald                 if (context->ag_indicators[i].status_changed) {
984ce263fc8SMatthias Ringwald                     if (strcmp(context->ag_indicators[i].name, "callsetup") == 0){
985ce263fc8SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) context->ag_indicators[i].status;
986ce263fc8SMatthias Ringwald                     } else if (strcmp(context->ag_indicators[i].name, "callheld") == 0){
987ce263fc8SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) context->ag_indicators[i].status;
988ce263fc8SMatthias Ringwald                     } else if (strcmp(context->ag_indicators[i].name, "call") == 0){
989ce263fc8SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) context->ag_indicators[i].status;
990ce263fc8SMatthias Ringwald                     }
9913deb3ec6SMatthias Ringwald                     context->ag_indicators[i].status_changed = 0;
992aa4dd815SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, 0, context->ag_indicators[i]);
9933deb3ec6SMatthias Ringwald                     break;
9943deb3ec6SMatthias Ringwald                 }
9953deb3ec6SMatthias Ringwald             }
996ce263fc8SMatthias Ringwald             break;
997ce263fc8SMatthias Ringwald         default:
998ce263fc8SMatthias Ringwald             break;
9993deb3ec6SMatthias Ringwald     }
1000ce263fc8SMatthias Ringwald     hfp_run_for_context(context);
10013deb3ec6SMatthias Ringwald }
10023deb3ec6SMatthias Ringwald 
10033deb3ec6SMatthias Ringwald static void hfp_run(){
1004665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1005665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1006665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1007665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
10083deb3ec6SMatthias Ringwald         hfp_run_for_context(connection);
10093deb3ec6SMatthias Ringwald     }
10103deb3ec6SMatthias Ringwald }
10113deb3ec6SMatthias Ringwald 
1012ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10133deb3ec6SMatthias Ringwald     switch (packet_type){
10143deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10153deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10163deb3ec6SMatthias Ringwald             break;
10173deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
10183deb3ec6SMatthias Ringwald             hfp_handle_hci_event(hfp_callback, packet_type, packet, size);
10193deb3ec6SMatthias Ringwald         default:
10203deb3ec6SMatthias Ringwald             break;
10213deb3ec6SMatthias Ringwald     }
10223deb3ec6SMatthias Ringwald     hfp_run();
10233deb3ec6SMatthias Ringwald }
10243deb3ec6SMatthias Ringwald 
10253deb3ec6SMatthias Ringwald void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr){
10263deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
10273deb3ec6SMatthias Ringwald         log_error("hfp_hf_set_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
10283deb3ec6SMatthias Ringwald         return;
10293deb3ec6SMatthias Ringwald     }
10303deb3ec6SMatthias Ringwald 
10313deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
10323deb3ec6SMatthias Ringwald     int i;
10333deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
10343deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
10353deb3ec6SMatthias Ringwald     }
10363deb3ec6SMatthias Ringwald 
10373deb3ec6SMatthias Ringwald     char buffer[30];
10383deb3ec6SMatthias Ringwald     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
10393deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1040665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1041665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1042665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1043665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
10443deb3ec6SMatthias Ringwald         if (!connection) continue;
1045aa4dd815SMatthias Ringwald         connection->command = HFP_CMD_AVAILABLE_CODECS;
10463deb3ec6SMatthias Ringwald         hfp_run_for_context(connection);
10473deb3ec6SMatthias Ringwald     }
10483deb3ec6SMatthias Ringwald }
10493deb3ec6SMatthias Ringwald 
10503deb3ec6SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, uint16_t * indicators, int indicators_nr, uint32_t indicators_status){
1051aa4dd815SMatthias Ringwald     l2cap_init();
1052aa4dd815SMatthias Ringwald     l2cap_register_packet_handler(packet_handler);
1053e4dd59a7SMatthias Ringwald     rfcomm_register_packet_handler(packet_handler);
10543deb3ec6SMatthias Ringwald     hfp_init(rfcomm_channel_nr);
10553deb3ec6SMatthias Ringwald 
10563deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
10573deb3ec6SMatthias Ringwald 
10583deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
10593deb3ec6SMatthias Ringwald     hfp_indicators_status = indicators_status;
10603deb3ec6SMatthias Ringwald     int i;
10613deb3ec6SMatthias Ringwald     for (i=0; i<indicators_nr; i++){
10623deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
10633deb3ec6SMatthias Ringwald     }
10643deb3ec6SMatthias Ringwald }
10653deb3ec6SMatthias Ringwald 
106674386ee0SMatthias Ringwald void hfp_hf_set_supported_features(uint32_t supported_features){
106774386ee0SMatthias Ringwald     hfp_supported_features = supported_features;
106874386ee0SMatthias Ringwald }
106974386ee0SMatthias Ringwald 
10703deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
10713deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
10723deb3ec6SMatthias Ringwald }
10733deb3ec6SMatthias Ringwald 
10743deb3ec6SMatthias Ringwald void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
10753deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
10763deb3ec6SMatthias Ringwald     hfp_release_service_level_connection(connection);
10773deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
10783deb3ec6SMatthias Ringwald }
10793deb3ec6SMatthias Ringwald 
1080ce263fc8SMatthias Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
10813deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
10823deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
10833deb3ec6SMatthias Ringwald     if (!connection){
10843deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
10853deb3ec6SMatthias Ringwald         return;
10863deb3ec6SMatthias Ringwald     }
10873deb3ec6SMatthias Ringwald     connection->enable_status_update_for_ag_indicators = enable;
10883deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
10893deb3ec6SMatthias Ringwald }
10903deb3ec6SMatthias Ringwald 
1091ce263fc8SMatthias Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1092ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1093ce263fc8SMatthias Ringwald }
1094ce263fc8SMatthias Ringwald 
1095ce263fc8SMatthias Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1096ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1097ce263fc8SMatthias Ringwald }
1098ce263fc8SMatthias Ringwald 
10993deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1100ce263fc8SMatthias Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
11013deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11023deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11033deb3ec6SMatthias Ringwald     if (!connection){
11043deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
11053deb3ec6SMatthias Ringwald         return;
11063deb3ec6SMatthias Ringwald     }
11073deb3ec6SMatthias Ringwald     connection->change_status_update_for_individual_ag_indicators = 1;
11083deb3ec6SMatthias Ringwald     connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
11093deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11103deb3ec6SMatthias Ringwald }
11113deb3ec6SMatthias Ringwald 
11123deb3ec6SMatthias Ringwald void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
11133deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11143deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11153deb3ec6SMatthias Ringwald     if (!connection){
11163deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
11173deb3ec6SMatthias Ringwald         return;
11183deb3ec6SMatthias Ringwald     }
1119ce263fc8SMatthias Ringwald     switch (connection->hf_query_operator_state){
1120ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1121ce263fc8SMatthias Ringwald             connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1122ce263fc8SMatthias Ringwald             break;
1123ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1124ce263fc8SMatthias Ringwald             connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1125ce263fc8SMatthias Ringwald             break;
1126ce263fc8SMatthias Ringwald         default:
1127ce263fc8SMatthias Ringwald             break;
1128ce263fc8SMatthias Ringwald     }
11293deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11303deb3ec6SMatthias Ringwald }
11313deb3ec6SMatthias Ringwald 
1132ce263fc8SMatthias Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
11333deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11343deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11353deb3ec6SMatthias Ringwald     if (!connection){
11363deb3ec6SMatthias Ringwald         log_error("HFP HF: connection doesn't exist.");
11373deb3ec6SMatthias Ringwald         return;
11383deb3ec6SMatthias Ringwald     }
11393deb3ec6SMatthias Ringwald     connection->enable_extended_audio_gateway_error_report = enable;
11403deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11413deb3ec6SMatthias Ringwald }
11423deb3ec6SMatthias Ringwald 
1143ce263fc8SMatthias Ringwald 
1144ce263fc8SMatthias Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1145ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1146ce263fc8SMatthias Ringwald }
1147ce263fc8SMatthias Ringwald 
1148ce263fc8SMatthias Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1149ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1150ce263fc8SMatthias Ringwald }
1151ce263fc8SMatthias Ringwald 
1152ce263fc8SMatthias Ringwald 
11533deb3ec6SMatthias Ringwald void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
11543deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
11553deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11563deb3ec6SMatthias Ringwald     connection->establish_audio_connection = 0;
1157ce263fc8SMatthias Ringwald 
11583deb3ec6SMatthias Ringwald     if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
11593deb3ec6SMatthias Ringwald     if (connection->state >= HFP_W2_DISCONNECT_SCO) return;
11603deb3ec6SMatthias Ringwald 
1161ce263fc8SMatthias Ringwald     if (!has_codec_negotiation_feature(connection)){
1162ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1163ce263fc8SMatthias Ringwald         connection->codecs_state = HFP_CODECS_EXCHANGED;
11643deb3ec6SMatthias Ringwald         connection->establish_audio_connection = 1;
1165ce263fc8SMatthias Ringwald     } else {
1166aa4dd815SMatthias Ringwald         switch (connection->codecs_state){
1167aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1168aa4dd815SMatthias Ringwald                 break;
1169aa4dd815SMatthias Ringwald             default:
1170aa4dd815SMatthias Ringwald                 connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1171aa4dd815SMatthias Ringwald                 break;
11723deb3ec6SMatthias Ringwald         }
1173ce263fc8SMatthias Ringwald     }
1174ce263fc8SMatthias Ringwald 
11753deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11763deb3ec6SMatthias Ringwald }
11773deb3ec6SMatthias Ringwald 
11783deb3ec6SMatthias Ringwald void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
11793deb3ec6SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
11803deb3ec6SMatthias Ringwald     hfp_release_audio_connection(connection);
11813deb3ec6SMatthias Ringwald     hfp_run_for_context(connection);
11823deb3ec6SMatthias Ringwald }
11833deb3ec6SMatthias Ringwald 
1184ce263fc8SMatthias Ringwald void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1185ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1186ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1187ce263fc8SMatthias Ringwald 
1188ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1189ce263fc8SMatthias Ringwald         connection->hf_answer_incoming_call = 1;
1190ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1191ce263fc8SMatthias Ringwald     } else {
1192ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1193ce263fc8SMatthias Ringwald     }
1194ce263fc8SMatthias Ringwald }
1195ce263fc8SMatthias Ringwald 
1196ce263fc8SMatthias Ringwald void hfp_hf_terminate_call(bd_addr_t bd_addr){
1197ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1198ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1199ce263fc8SMatthias Ringwald 
1200ce263fc8SMatthias Ringwald     // if (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1201ce263fc8SMatthias Ringwald     connection->hf_send_chup = 1;
1202ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1203ce263fc8SMatthias Ringwald     // } else {
1204ce263fc8SMatthias Ringwald     //     log_error("HFP HF: terminating incoming call with wrong call status %u", hfp_call_status);
1205ce263fc8SMatthias Ringwald     // }
1206ce263fc8SMatthias Ringwald }
1207ce263fc8SMatthias Ringwald 
1208ce263fc8SMatthias Ringwald void hfp_hf_reject_call(bd_addr_t bd_addr){
1209ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1210ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1211ce263fc8SMatthias Ringwald 
1212ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1213ce263fc8SMatthias Ringwald         connection->hf_send_chup = 1;
1214ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1215ce263fc8SMatthias Ringwald     }
1216ce263fc8SMatthias Ringwald }
1217ce263fc8SMatthias Ringwald 
1218ce263fc8SMatthias Ringwald void hfp_hf_user_busy(bd_addr_t addr){
1219ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1220ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1221ce263fc8SMatthias Ringwald 
1222ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1223ce263fc8SMatthias Ringwald         connection->hf_send_chld_0 = 1;
1224ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1225ce263fc8SMatthias Ringwald     }
1226ce263fc8SMatthias Ringwald }
1227ce263fc8SMatthias Ringwald 
1228ce263fc8SMatthias Ringwald void hfp_hf_end_active_and_accept_other(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         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1234ce263fc8SMatthias Ringwald         connection->hf_send_chld_1 = 1;
1235ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1236ce263fc8SMatthias Ringwald     }
1237ce263fc8SMatthias Ringwald }
1238ce263fc8SMatthias Ringwald 
1239ce263fc8SMatthias Ringwald void hfp_hf_swap_calls(bd_addr_t addr){
1240ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1241ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1242ce263fc8SMatthias Ringwald 
1243ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1244ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1245ce263fc8SMatthias Ringwald         connection->hf_send_chld_2 = 1;
1246ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1247ce263fc8SMatthias Ringwald     }
1248ce263fc8SMatthias Ringwald }
1249ce263fc8SMatthias Ringwald 
1250ce263fc8SMatthias Ringwald void hfp_hf_join_held_call(bd_addr_t addr){
1251ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1252ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1253ce263fc8SMatthias Ringwald 
1254ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1255ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1256ce263fc8SMatthias Ringwald         connection->hf_send_chld_3 = 1;
1257ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1258ce263fc8SMatthias Ringwald     }
1259ce263fc8SMatthias Ringwald }
1260ce263fc8SMatthias Ringwald 
1261ce263fc8SMatthias Ringwald void hfp_hf_connect_calls(bd_addr_t addr){
1262ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1263ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1264ce263fc8SMatthias Ringwald 
1265ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1266ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1267ce263fc8SMatthias Ringwald         connection->hf_send_chld_4 = 1;
1268ce263fc8SMatthias Ringwald         hfp_run_for_context(connection);
1269ce263fc8SMatthias Ringwald     }
1270ce263fc8SMatthias Ringwald }
1271ce263fc8SMatthias Ringwald 
1272667ec068SMatthias Ringwald void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1273667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1274667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1275667ec068SMatthias Ringwald 
1276667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1277667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1278667ec068SMatthias Ringwald         connection->hf_send_chld_x = 1;
1279667ec068SMatthias Ringwald         connection->hf_send_chld_x_index = 10 + index;
1280667ec068SMatthias Ringwald         hfp_run_for_context(connection);
1281667ec068SMatthias Ringwald     }
1282667ec068SMatthias Ringwald }
1283667ec068SMatthias Ringwald 
1284667ec068SMatthias Ringwald void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1285667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1286667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1287667ec068SMatthias Ringwald 
1288667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1289667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1290667ec068SMatthias Ringwald         connection->hf_send_chld_x = 1;
1291667ec068SMatthias Ringwald         connection->hf_send_chld_x_index = 20 + index;
1292667ec068SMatthias Ringwald         hfp_run_for_context(connection);
1293667ec068SMatthias Ringwald     }
1294667ec068SMatthias Ringwald }
1295ce263fc8SMatthias Ringwald 
1296ce263fc8SMatthias Ringwald void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1297ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1298ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1299ce263fc8SMatthias Ringwald 
1300ce263fc8SMatthias Ringwald     connection->hf_initiate_outgoing_call = 1;
1301ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1302ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1303ce263fc8SMatthias Ringwald }
1304ce263fc8SMatthias Ringwald 
1305ce263fc8SMatthias Ringwald void hfp_hf_dial_memory(bd_addr_t bd_addr, char * number){
1306ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1307ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1308ce263fc8SMatthias Ringwald 
1309ce263fc8SMatthias Ringwald     connection->hf_initiate_memory_dialing = 1;
1310ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1311ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1312ce263fc8SMatthias Ringwald }
1313ce263fc8SMatthias Ringwald 
1314ce263fc8SMatthias Ringwald void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1315ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1316ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1317ce263fc8SMatthias Ringwald 
1318ce263fc8SMatthias Ringwald     connection->hf_initiate_redial_last_number = 1;
1319ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1320ce263fc8SMatthias Ringwald }
1321ce263fc8SMatthias Ringwald 
1322ce263fc8SMatthias Ringwald void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1323ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1324ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1325ce263fc8SMatthias Ringwald 
1326ce263fc8SMatthias Ringwald     connection->hf_activate_call_waiting_notification = 1;
1327ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1328ce263fc8SMatthias Ringwald }
1329ce263fc8SMatthias Ringwald 
1330ce263fc8SMatthias Ringwald 
1331ce263fc8SMatthias Ringwald void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1332ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1333ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1334ce263fc8SMatthias Ringwald 
1335ce263fc8SMatthias Ringwald     connection->hf_deactivate_call_waiting_notification = 1;
1336ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1337ce263fc8SMatthias Ringwald }
1338ce263fc8SMatthias Ringwald 
1339ce263fc8SMatthias Ringwald 
1340ce263fc8SMatthias Ringwald void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1341ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1342ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1343ce263fc8SMatthias Ringwald 
1344a5bdcda8SMatthias Ringwald     connection->hf_activate_calling_line_notification = 1;
1345ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1346ce263fc8SMatthias Ringwald }
1347ce263fc8SMatthias Ringwald 
1348ce263fc8SMatthias Ringwald /*
1349ce263fc8SMatthias Ringwald  * @brief
1350ce263fc8SMatthias Ringwald  */
1351ce263fc8SMatthias Ringwald void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1352ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1353ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1354ce263fc8SMatthias Ringwald 
1355ce263fc8SMatthias Ringwald     connection->hf_deactivate_calling_line_notification = 1;
1356ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1357ce263fc8SMatthias Ringwald }
1358ce263fc8SMatthias Ringwald 
1359ce263fc8SMatthias Ringwald 
1360ce263fc8SMatthias Ringwald /*
1361ce263fc8SMatthias Ringwald  * @brief
1362ce263fc8SMatthias Ringwald  */
1363ce263fc8SMatthias Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1364ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1365ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1366ce263fc8SMatthias Ringwald 
1367a5bdcda8SMatthias Ringwald     connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1368ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1369ce263fc8SMatthias Ringwald }
1370ce263fc8SMatthias Ringwald 
1371ce263fc8SMatthias Ringwald /*
1372ce263fc8SMatthias Ringwald  * @brief
1373ce263fc8SMatthias Ringwald  */
1374ce263fc8SMatthias Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1375ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1376ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1377ce263fc8SMatthias Ringwald 
1378ce263fc8SMatthias Ringwald     connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1379ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1380ce263fc8SMatthias Ringwald }
1381ce263fc8SMatthias Ringwald 
1382ce263fc8SMatthias Ringwald /*
1383ce263fc8SMatthias Ringwald  * @brief
1384ce263fc8SMatthias Ringwald  */
1385ce263fc8SMatthias Ringwald void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1386ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1387ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1388ce263fc8SMatthias Ringwald 
1389ce263fc8SMatthias Ringwald     connection->hf_activate_voice_recognition_notification = 1;
1390ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1391ce263fc8SMatthias Ringwald }
1392ce263fc8SMatthias Ringwald 
1393ce263fc8SMatthias Ringwald /*
1394ce263fc8SMatthias Ringwald  * @brief
1395ce263fc8SMatthias Ringwald  */
1396ce263fc8SMatthias Ringwald void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1397ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1398ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1399ce263fc8SMatthias Ringwald 
1400ce263fc8SMatthias Ringwald     connection->hf_deactivate_voice_recognition_notification = 1;
1401ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1402ce263fc8SMatthias Ringwald }
1403ce263fc8SMatthias Ringwald 
1404ce263fc8SMatthias Ringwald /*
1405ce263fc8SMatthias Ringwald  * @brief
1406ce263fc8SMatthias Ringwald  */
1407ce263fc8SMatthias Ringwald void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1408ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1409ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1410ce263fc8SMatthias Ringwald     if (connection->microphone_gain == gain) return;
1411ce263fc8SMatthias Ringwald 
1412ce263fc8SMatthias Ringwald     connection->microphone_gain = gain;
1413ce263fc8SMatthias Ringwald     connection->send_microphone_gain = 1;
1414ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1415ce263fc8SMatthias Ringwald }
1416ce263fc8SMatthias Ringwald 
1417ce263fc8SMatthias Ringwald /*
1418ce263fc8SMatthias Ringwald  * @brief
1419ce263fc8SMatthias Ringwald  */
1420ce263fc8SMatthias Ringwald void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1421ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1422ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1423ce263fc8SMatthias Ringwald     if (connection->speaker_gain == gain) return;
1424ce263fc8SMatthias Ringwald 
1425ce263fc8SMatthias Ringwald     connection->speaker_gain = gain;
1426ce263fc8SMatthias Ringwald     connection->send_speaker_gain = 1;
1427ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1428ce263fc8SMatthias Ringwald }
1429ce263fc8SMatthias Ringwald 
1430ce263fc8SMatthias Ringwald /*
1431ce263fc8SMatthias Ringwald  * @brief
1432ce263fc8SMatthias Ringwald  */
1433ce263fc8SMatthias Ringwald void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1434ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1435ce263fc8SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1436ce263fc8SMatthias Ringwald     connection->hf_send_dtmf_code = code;
1437ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1438ce263fc8SMatthias Ringwald }
1439ce263fc8SMatthias Ringwald 
1440ce263fc8SMatthias Ringwald /*
1441ce263fc8SMatthias Ringwald  * @brief
1442ce263fc8SMatthias Ringwald  */
1443ce263fc8SMatthias Ringwald void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
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_binp = 1;
1447ce263fc8SMatthias Ringwald     hfp_run_for_context(connection);
1448ce263fc8SMatthias Ringwald }
14493deb3ec6SMatthias Ringwald 
1450667ec068SMatthias Ringwald /*
1451667ec068SMatthias Ringwald  * @brief
1452667ec068SMatthias Ringwald  */
1453667ec068SMatthias Ringwald void hfp_hf_query_current_call_status(bd_addr_t addr){
1454667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1455667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1456667ec068SMatthias Ringwald     connection->hf_send_clcc = 1;
1457667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1458667ec068SMatthias Ringwald }
1459667ec068SMatthias Ringwald 
1460667ec068SMatthias Ringwald 
1461667ec068SMatthias Ringwald /*
1462667ec068SMatthias Ringwald  * @brief
1463667ec068SMatthias Ringwald  */
1464667ec068SMatthias Ringwald void hfp_hf_rrh_query_status(bd_addr_t addr){
1465667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1466667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1467667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1468667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '?';
1469667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1470667ec068SMatthias Ringwald }
1471667ec068SMatthias Ringwald 
1472667ec068SMatthias Ringwald /*
1473667ec068SMatthias Ringwald  * @brief
1474667ec068SMatthias Ringwald  */
1475667ec068SMatthias Ringwald void hfp_hf_rrh_hold_call(bd_addr_t addr){
1476667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1477667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1478667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1479667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '0';
1480667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1481667ec068SMatthias Ringwald }
1482667ec068SMatthias Ringwald 
1483667ec068SMatthias Ringwald /*
1484667ec068SMatthias Ringwald  * @brief
1485667ec068SMatthias Ringwald  */
1486667ec068SMatthias Ringwald void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1487667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1488667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1489667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1490667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '1';
1491667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1492667ec068SMatthias Ringwald }
1493667ec068SMatthias Ringwald 
1494667ec068SMatthias Ringwald /*
1495667ec068SMatthias Ringwald  * @brief
1496667ec068SMatthias Ringwald  */
1497667ec068SMatthias Ringwald void hfp_hf_rrh_reject_held_call(bd_addr_t addr)
1498667ec068SMatthias Ringwald {
1499667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1500667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1501667ec068SMatthias Ringwald     connection->hf_send_rrh = 1;
1502667ec068SMatthias Ringwald     connection->hf_send_rrh_command = '2';
1503667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1504667ec068SMatthias Ringwald }
1505667ec068SMatthias Ringwald 
1506667ec068SMatthias Ringwald /*
1507667ec068SMatthias Ringwald  * @brief
1508667ec068SMatthias Ringwald  */
1509667ec068SMatthias Ringwald void hfp_hf_query_subscriber_number(bd_addr_t addr)
1510667ec068SMatthias Ringwald {
1511667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1512667ec068SMatthias Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1513667ec068SMatthias Ringwald     connection->hf_send_cnum = 1;
1514667ec068SMatthias Ringwald     hfp_run_for_context(connection);
1515667ec068SMatthias Ringwald }
1516667ec068SMatthias Ringwald 
1517667ec068SMatthias Ringwald /*
1518667ec068SMatthias Ringwald  * @brief
1519667ec068SMatthias Ringwald  */
1520667ec068SMatthias Ringwald void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
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     // find index for assigned number
1524667ec068SMatthias Ringwald     int i;
1525667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1526667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1527667ec068SMatthias Ringwald             // set value
1528667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1529667ec068SMatthias Ringwald             // mark for update
1530667ec068SMatthias Ringwald             connection->generic_status_update_bitmap |= (1<<i);
1531667ec068SMatthias Ringwald             // send update
1532667ec068SMatthias Ringwald             hfp_run_for_context(connection);
1533667ec068SMatthias Ringwald             return;
1534667ec068SMatthias Ringwald         }
1535667ec068SMatthias Ringwald     }
1536667ec068SMatthias Ringwald }
1537667ec068SMatthias Ringwald 
1538