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