xref: /btstack/src/classic/hfp.c (revision ff38a59e09fdf9ebdc0c73693a844307f9ad49d7)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
417907f069SMatthias Ringwald #include "btstack_config.h"
423deb3ec6SMatthias Ringwald 
433deb3ec6SMatthias Ringwald #include <stdint.h>
443cfa4086SMatthias Ringwald #include <stdio.h>
453deb3ec6SMatthias Ringwald #include <string.h>
463deb3ec6SMatthias Ringwald #include <inttypes.h>
473deb3ec6SMatthias Ringwald 
48235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
49023f2764SMatthias Ringwald #include "btstack_debug.h"
5059c6af15SMatthias Ringwald #include "btstack_event.h"
5159c6af15SMatthias Ringwald #include "btstack_memory.h"
5259c6af15SMatthias Ringwald #include "btstack_run_loop.h"
53efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
54746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
55023f2764SMatthias Ringwald #include "classic/sdp_util.h"
561d9c9c90SMilanka Ringwald #include "classic/sdp_client.h"
5759c6af15SMatthias Ringwald #include "hci.h"
5859c6af15SMatthias Ringwald #include "hci_cmd.h"
5959c6af15SMatthias Ringwald #include "hci_dump.h"
603deb3ec6SMatthias Ringwald 
613721a235SMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM)
623721a235SMatthias Ringwald #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
633721a235SMatthias Ringwald #endif
643721a235SMatthias Ringwald 
65689d4323SMatthias Ringwald #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM)
66689d4323SMatthias Ringwald #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
67689d4323SMatthias Ringwald #endif
68689d4323SMatthias Ringwald 
693deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
703deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
713deb3ec6SMatthias Ringwald 
7220b2edb6SMatthias Ringwald typedef struct {
7320b2edb6SMatthias Ringwald     hfp_role_t local_role;
7420b2edb6SMatthias Ringwald     bd_addr_t  remote_address;
7520b2edb6SMatthias Ringwald } hfp_sdp_query_context_t;
7620b2edb6SMatthias Ringwald 
7720b2edb6SMatthias Ringwald // globals
7820b2edb6SMatthias Ringwald 
7920b2edb6SMatthias Ringwald static btstack_linked_list_t hfp_connections ;
8020b2edb6SMatthias Ringwald 
8120b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
8220b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
8320b2edb6SMatthias Ringwald 
8420b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
8520b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
8620b2edb6SMatthias Ringwald 
8721df969bSMatthias Ringwald static uint8_t  hfp_hf_indicators_nr;
8821df969bSMatthias Ringwald static const uint8_t * hfp_hf_indicators;
8921df969bSMatthias Ringwald 
9020b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types;
91aeb0f0feSMatthias Ringwald static hfp_connection_t * hfp_sco_establishment_active;
92aeb0f0feSMatthias Ringwald 
93aeb0f0feSMatthias Ringwald static hfp_sdp_query_context_t                 hfp_sdp_query_context;
94aeb0f0feSMatthias Ringwald static btstack_context_callback_registration_t hfp_sdp_query_request;
95aeb0f0feSMatthias Ringwald 
96aeb0f0feSMatthias Ringwald 
97aeb0f0feSMatthias Ringwald 
98aeb0f0feSMatthias Ringwald 
99aeb0f0feSMatthias Ringwald 
100471dea41SMatthias Ringwald // custom commands
101471dea41SMatthias Ringwald static btstack_linked_list_t hfp_custom_commands_ag;
1027404dce3SMatthias Ringwald static btstack_linked_list_t hfp_custom_commands_hf;
10320b2edb6SMatthias Ringwald 
10420b2edb6SMatthias Ringwald // prototypes
10520b2edb6SMatthias Ringwald static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported);
10620b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
10720b2edb6SMatthias Ringwald 
1084e01e802SMatthias Ringwald 
10964126f36SMatthias Ringwald #define CODEC_MASK_CVSD   (1 << HFP_CODEC_CVSD)
11013ea5944SMatthias Ringwald #define CODEC_MASK_OTHER ((1 << HFP_CODEC_MSBC) | (1 << HFP_CODEC_LC3_SWB))
11113ea5944SMatthias Ringwald 
11213ea5944SMatthias Ringwald static const struct {
1134e01e802SMatthias Ringwald     const uint16_t max_latency;
1144e01e802SMatthias Ringwald     const uint8_t  retransmission_effort;
1154e01e802SMatthias Ringwald     const uint16_t packet_types;
11664126f36SMatthias Ringwald     const uint8_t  codec_mask;
1174e01e802SMatthias Ringwald } hfp_link_settings [] = {
118b4320e7fSMatthias Ringwald         {0x0004, 0xff, SCO_PACKET_TYPES_HV1,  CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_D0
119b4320e7fSMatthias Ringwald         {0x0005, 0xff, SCO_PACKET_TYPES_HV3,  CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_D1
120b4320e7fSMatthias Ringwald         {0x0007, 0x01, SCO_PACKET_TYPES_EV3,  CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S1
121b4320e7fSMatthias Ringwald         {0x0007, 0x01, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S2
122b4320e7fSMatthias Ringwald         {0x000a, 0x01, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S3
123b4320e7fSMatthias Ringwald         {0x000c, 0x02, SCO_PACKET_TYPES_2EV3, CODEC_MASK_CVSD }, // HFP_LINK_SETTINGS_S4
124b4320e7fSMatthias Ringwald         {0x0008, 0x02, SCO_PACKET_TYPES_EV3,  CODEC_MASK_OTHER}, // HFP_LINK_SETTINGS_T1
125b4320e7fSMatthias Ringwald         {0x000d, 0x02, SCO_PACKET_TYPES_2EV3, CODEC_MASK_OTHER}  // HFP_LINK_SETTINGS_T2
12613ea5944SMatthias Ringwald };
1273ad75286SMatthias Ringwald 
128c169b70dSMatthias Ringwald #ifdef ENABLE_HFP_HF_SAFE_SETTINGS
129c169b70dSMatthias Ringwald // HFP v1.9, table 6.10 'mandatory safe settings' for eSCO + similar entries for SCO
13032459254SMatthias Ringwald static const struct hfp_mandatory_safe_setting {
13132459254SMatthias Ringwald     const uint8_t codec_mask;
13232459254SMatthias Ringwald     const bool secure_connection_in_use;
13332459254SMatthias Ringwald     hfp_link_settings_t link_setting;
13432459254SMatthias Ringwald } hfp_mandatory_safe_settings[] = {
13532459254SMatthias Ringwald         { CODEC_MASK_CVSD,  false, HFP_LINK_SETTINGS_D1},
13632459254SMatthias Ringwald         { CODEC_MASK_CVSD,  true,  HFP_LINK_SETTINGS_D1},
13732459254SMatthias Ringwald         { CODEC_MASK_CVSD,  false, HFP_LINK_SETTINGS_S1},
13832459254SMatthias Ringwald         { CODEC_MASK_CVSD,  true,  HFP_LINK_SETTINGS_S4},
13932459254SMatthias Ringwald         { CODEC_MASK_OTHER, false, HFP_LINK_SETTINGS_T1},
14032459254SMatthias Ringwald         { CODEC_MASK_OTHER, true,  HFP_LINK_SETTINGS_T2},
14132459254SMatthias Ringwald };
142c169b70dSMatthias Ringwald #endif
14332459254SMatthias Ringwald 
1443deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
1453deb3ec6SMatthias Ringwald         "EC and/or NR function",
1463deb3ec6SMatthias Ringwald         "Three-way calling",
1473deb3ec6SMatthias Ringwald         "CLI presentation capability",
1483deb3ec6SMatthias Ringwald         "Voice recognition activation",
1493deb3ec6SMatthias Ringwald         "Remote volume control",
1503deb3ec6SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald         "Enhanced call status",
1523deb3ec6SMatthias Ringwald         "Enhanced call control",
1533deb3ec6SMatthias Ringwald 
1543deb3ec6SMatthias Ringwald         "Codec negotiation",
1553deb3ec6SMatthias Ringwald 
1563deb3ec6SMatthias Ringwald         "HF Indicators",
1573deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1583deb3ec6SMatthias Ringwald         "Reserved for future definition"
1593deb3ec6SMatthias Ringwald };
1603deb3ec6SMatthias Ringwald 
1613deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
1623deb3ec6SMatthias Ringwald         "Three-way calling",
1633deb3ec6SMatthias Ringwald         "EC and/or NR function",
1643deb3ec6SMatthias Ringwald         "Voice recognition function",
1653deb3ec6SMatthias Ringwald         "In-band ring tone capability",
1663deb3ec6SMatthias Ringwald         "Attach a number to a voice tag",
1673deb3ec6SMatthias Ringwald         "Ability to reject a call",
1683deb3ec6SMatthias Ringwald         "Enhanced call status",
1693deb3ec6SMatthias Ringwald         "Enhanced call control",
1703deb3ec6SMatthias Ringwald         "Extended Error Result Codes",
1713deb3ec6SMatthias Ringwald         "Codec negotiation",
1723deb3ec6SMatthias Ringwald         "HF Indicators",
1733deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1743deb3ec6SMatthias Ringwald         "Reserved for future definition"
1753deb3ec6SMatthias Ringwald };
1763deb3ec6SMatthias Ringwald 
1770aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1780aee97efSMilanka Ringwald         "outgoing",
1790aee97efSMilanka Ringwald         "incoming"
1800aee97efSMilanka Ringwald };
1810aee97efSMilanka Ringwald 
1820aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1830aee97efSMilanka Ringwald         "active",
1840aee97efSMilanka Ringwald         "held",
1850aee97efSMilanka Ringwald         "outgoing dialing",
1860aee97efSMilanka Ringwald         "outgoing alerting",
1870aee97efSMilanka Ringwald         "incoming",
1880aee97efSMilanka Ringwald         "incoming waiting",
1890aee97efSMilanka Ringwald         "call held by response and hold"
1900aee97efSMilanka Ringwald };
1910aee97efSMilanka Ringwald 
1920aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1930aee97efSMilanka Ringwald         "voice",
1940aee97efSMilanka Ringwald         "data",
1950aee97efSMilanka Ringwald         "fax"
1960aee97efSMilanka Ringwald };
1970aee97efSMilanka Ringwald 
1980aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1990aee97efSMilanka Ringwald         "not a conference call",
2000aee97efSMilanka Ringwald         "conference call"
2010aee97efSMilanka Ringwald };
2020aee97efSMilanka Ringwald 
2030aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
2040aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
2050aee97efSMilanka Ringwald     return "not defined";
2060aee97efSMilanka Ringwald }
2070aee97efSMilanka Ringwald 
2080aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
2090aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
2100aee97efSMilanka Ringwald     return "not defined";
2110aee97efSMilanka Ringwald }
2120aee97efSMilanka Ringwald 
2130aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
2140aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
2150aee97efSMilanka Ringwald     return "not defined";
2160aee97efSMilanka Ringwald }
2170aee97efSMilanka Ringwald 
2180aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
2190aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
2200aee97efSMilanka Ringwald     return "not defined";
2210aee97efSMilanka Ringwald }
2220aee97efSMilanka Ringwald 
22325789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
22425789943SMilanka Ringwald     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
22525789943SMilanka Ringwald 
22625789943SMilanka Ringwald     if (index > HFP_MAX_NUM_INDICATORS){
22725789943SMilanka Ringwald         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
22825789943SMilanka Ringwald         return HFP_MAX_NUM_INDICATORS - 1;
22925789943SMilanka Ringwald     }
23025789943SMilanka Ringwald 
23125789943SMilanka Ringwald     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
23225789943SMilanka Ringwald     if (index > 0){
23325789943SMilanka Ringwald         index -= 1;
23425789943SMilanka Ringwald     } else {
23525789943SMilanka Ringwald         log_info("ignoring invalid indicator index 0");
23625789943SMilanka Ringwald         return 0;
23725789943SMilanka Ringwald     }
23825789943SMilanka Ringwald     return index;
23925789943SMilanka Ringwald }
24025789943SMilanka Ringwald 
24125789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
24225789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
24325789943SMilanka Ringwald         hfp_connection->parser_item_index++;
24425789943SMilanka Ringwald     } else {
24525789943SMilanka Ringwald         log_info("Ignoring additional indicator");
24625789943SMilanka Ringwald     }
24725789943SMilanka Ringwald }
24825789943SMilanka Ringwald 
24925789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
25025789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
25125789943SMilanka Ringwald         hfp_connection->parser_item_index++;
25225789943SMilanka Ringwald     } else {
25325789943SMilanka Ringwald         log_info("Ignoring additional codec index");
25425789943SMilanka Ringwald     }
25525789943SMilanka Ringwald }
25625789943SMilanka Ringwald 
257eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
258eed67a37SMilanka Ringwald     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
259eed67a37SMilanka Ringwald         hfp_connection->remote_call_services_index++;
260eed67a37SMilanka Ringwald     } else {
261eed67a37SMilanka Ringwald         log_info("Ignoring additional remote_call_services");
262eed67a37SMilanka Ringwald     }
263eed67a37SMilanka Ringwald }
26425789943SMilanka Ringwald 
2653deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
2663deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
2673deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
2683deb3ec6SMatthias Ringwald     }
2693deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
2703deb3ec6SMatthias Ringwald }
2713deb3ec6SMatthias Ringwald 
2723deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
2733deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
2743deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
2753deb3ec6SMatthias Ringwald     }
2763deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
2773deb3ec6SMatthias Ringwald }
2783deb3ec6SMatthias Ringwald 
2795ebff802SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, const char * command){
2803deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
28174386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
282ab2445a0SMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, (uint16_t) strlen(command));
2833deb3ec6SMatthias Ringwald     if (err){
28428190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
2853deb3ec6SMatthias Ringwald     }
286e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
287e43d1938SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
288e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
289e43d1938SMatthias Ringwald #endif
2903deb3ec6SMatthias Ringwald     return 1;
2913deb3ec6SMatthias Ringwald }
2923deb3ec6SMatthias Ringwald 
2936a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
294e8d1dc0dSMatthias Ringwald 
295e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
296c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
297e8d1dc0dSMatthias Ringwald 
298df327ff3SMilanka Ringwald     int i;
299df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
300e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
301e8d1dc0dSMatthias Ringwald         return 1;
302df327ff3SMilanka Ringwald     }
303df327ff3SMilanka Ringwald     return 0;
304df327ff3SMilanka Ringwald }
305df327ff3SMilanka Ringwald 
306d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
307d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
308806089a7SMatthias Ringwald     uint8_t i;
309806089a7SMatthias Ringwald     int filtered_codec_count = 0;
310d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
311806089a7SMatthias Ringwald         if (codecs[i] != HFP_CODEC_MSBC) {
312806089a7SMatthias Ringwald             codecs[filtered_codec_count++] = codecs[i];
313d715cf51SMatthias Ringwald         }
314806089a7SMatthias Ringwald     }
315806089a7SMatthias Ringwald     *codecs_nr = filtered_codec_count;
316d715cf51SMatthias Ringwald }
317d715cf51SMatthias Ringwald 
3183deb3ec6SMatthias Ringwald // UTILS
3193deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
3203deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
3213deb3ec6SMatthias Ringwald }
3223deb3ec6SMatthias Ringwald 
3233deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
3243deb3ec6SMatthias Ringwald     if (value){
3253deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
3263deb3ec6SMatthias Ringwald     } else {
3273deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
3283deb3ec6SMatthias Ringwald     }
3293deb3ec6SMatthias Ringwald     return bitmap;
3303deb3ec6SMatthias Ringwald }
3313deb3ec6SMatthias Ringwald 
3323deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
333c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3343deb3ec6SMatthias Ringwald     int i;
3353deb3ec6SMatthias Ringwald     int offset = 0;
336c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3373deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
3383deb3ec6SMatthias Ringwald     }
3393deb3ec6SMatthias Ringwald     if (i<values_nr){
3403deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
3413deb3ec6SMatthias Ringwald     }
3423deb3ec6SMatthias Ringwald     return offset;
3433deb3ec6SMatthias Ringwald }
3443deb3ec6SMatthias Ringwald 
3453deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
346c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3473deb3ec6SMatthias Ringwald 
3483deb3ec6SMatthias Ringwald     int i;
3493deb3ec6SMatthias Ringwald     int offset = 0;
350c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3513deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
3523deb3ec6SMatthias Ringwald     }
3533deb3ec6SMatthias Ringwald 
3543deb3ec6SMatthias Ringwald     if (i<values_nr){
3553deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3563deb3ec6SMatthias Ringwald     }
3573deb3ec6SMatthias Ringwald     return offset;
3583deb3ec6SMatthias Ringwald }
359293fae36SMatthias Ringwald static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){
360293fae36SMatthias Ringwald     switch (local_role){
361293fae36SMatthias Ringwald         case HFP_ROLE_HF:
362293fae36SMatthias Ringwald             (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size);
363293fae36SMatthias Ringwald             break;
364293fae36SMatthias Ringwald         case HFP_ROLE_AG:
365293fae36SMatthias Ringwald             (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size);
366293fae36SMatthias Ringwald             break;
367293fae36SMatthias Ringwald         default:
368293fae36SMatthias Ringwald             btstack_unreachable();
369293fae36SMatthias Ringwald             break;
370293fae36SMatthias Ringwald     }
371293fae36SMatthias Ringwald }
3723deb3ec6SMatthias Ringwald 
373ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
374ca59be51SMatthias Ringwald     if (!hfp_connection) return;
375293fae36SMatthias Ringwald     hfp_emit_event_for_role(hfp_connection->local_role, packet, size);
376ca59be51SMatthias Ringwald }
377ca59be51SMatthias Ringwald 
378ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
3797d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
380d703d377SMatthias Ringwald     uint8_t event[5];
381a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
382a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
383a0ffb263SMatthias Ringwald     event[2] = event_subtype;
3847d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
385ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
386a0ffb263SMatthias Ringwald }
387a0ffb263SMatthias Ringwald 
388ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3897d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
390d703d377SMatthias Ringwald     uint8_t event[6];
3913deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3923deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3933deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3947d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
395d703d377SMatthias Ringwald     event[5] = value; // status 0 == OK
396ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3973deb3ec6SMatthias Ringwald }
3983deb3ec6SMatthias Ringwald 
399553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){
400553a4a56SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
401553a4a56SMilanka Ringwald 
402a95ec82fSMilanka Ringwald     uint8_t event[7];
403a95ec82fSMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
404a95ec82fSMilanka Ringwald     event[1] = sizeof(event) - 2;
405a9e632e9SMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED;
406de9e0ea7SMilanka Ringwald 
407553a4a56SMilanka Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
408a95ec82fSMilanka Ringwald     event[5] = status; // 0:success
409553a4a56SMilanka Ringwald     event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0;
410553a4a56SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
4111a26de69SMilanka Ringwald }
412553a4a56SMilanka Ringwald 
413553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){
414553a4a56SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
415553a4a56SMilanka Ringwald 
416553a4a56SMilanka Ringwald     uint8_t event[6];
417553a4a56SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
418553a4a56SMilanka Ringwald     event[1] = sizeof(event) - 2;
419a9e632e9SMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED;
420553a4a56SMilanka Ringwald 
421553a4a56SMilanka Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
422553a4a56SMilanka Ringwald     event[5] = status; // 0:success
423013cc750SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
424013cc750SMilanka Ringwald }
425013cc750SMilanka Ringwald 
426de9e0ea7SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){
427de9e0ea7SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
428de9e0ea7SMilanka Ringwald 
429de9e0ea7SMilanka Ringwald     uint8_t event[6];
430de9e0ea7SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
431de9e0ea7SMilanka Ringwald     event[1] = sizeof(event) - 2;
432de9e0ea7SMilanka Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO;
433de9e0ea7SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
434de9e0ea7SMilanka Ringwald     event[5] = status;
435de9e0ea7SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
436de9e0ea7SMilanka Ringwald }
437de9e0ea7SMilanka Ringwald 
438cf75be85SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){
439cf75be85SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
440cf75be85SMilanka Ringwald 
441cf75be85SMilanka Ringwald     uint8_t event[6];
442cf75be85SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
443cf75be85SMilanka Ringwald     event[1] = sizeof(event) - 2;
444cf75be85SMilanka Ringwald     switch (hfp_connection->ag_vra_state){
445cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT:
446cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT;
447cf75be85SMilanka Ringwald             break;
448cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND:
449cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND;
450cf75be85SMilanka Ringwald             break;
451cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT:
452cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT;
453cf75be85SMilanka Ringwald             break;
454cf75be85SMilanka Ringwald         default:
455cf75be85SMilanka Ringwald             btstack_unreachable();
456cf75be85SMilanka Ringwald             break;
457cf75be85SMilanka Ringwald     }
458cf75be85SMilanka Ringwald 
459cf75be85SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
460cf75be85SMilanka Ringwald     event[5] = status;
461cf75be85SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
462cf75be85SMilanka Ringwald }
463cf75be85SMilanka Ringwald 
4647095467fSMatthias Ringwald void hfp_emit_slc_connection_event(hfp_role_t local_role, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){
4658901a7c4SMatthias Ringwald     uint8_t event[12];
4666a7f44bdSMilanka Ringwald     int pos = 0;
4676a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
4686a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
469d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
4706a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
4716a7f44bdSMilanka Ringwald     pos += 2;
47203870092SMilanka Ringwald     event[pos++] = status; // status 0 == OK
4736a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
4746a7f44bdSMilanka Ringwald     pos += 6;
4757095467fSMatthias Ringwald     hfp_emit_event_for_role(local_role, event, sizeof(event));
476a0653c3bSMilanka Ringwald }
477a0653c3bSMilanka Ringwald 
478d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){
4797d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
480d703d377SMatthias Ringwald     uint8_t event[7];
4815441d52fSMatthias Ringwald     int pos = 0;
4825441d52fSMatthias Ringwald     event[pos++] = HCI_EVENT_HFP_META;
4835441d52fSMatthias Ringwald     event[pos++] = sizeof(event) - 2;
4845441d52fSMatthias Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED;
485d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
486d703d377SMatthias Ringwald     pos += 2;
487d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
488d703d377SMatthias Ringwald     pos += 2;
4895441d52fSMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
4905441d52fSMatthias Ringwald }
4915441d52fSMatthias Ringwald 
492f14c5dafSMatthias Ringwald void hfp_emit_sco_connection_established(hfp_connection_t *hfp_connection, uint8_t status, uint8_t negotiated_codec,
4935e8e3664SMatthias Ringwald                                          uint16_t rx_packet_length, uint16_t tx_packet_length) {
4947d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
495c8149263SMatthias Ringwald     uint8_t event[21];
496d0c4aea6SMilanka Ringwald     int pos = 0;
497d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
498d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
499d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
500d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
501d703d377SMatthias Ringwald     pos += 2;
502d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
503f14c5dafSMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->sco_handle);
504d0c4aea6SMilanka Ringwald     pos += 2;
505f14c5dafSMatthias Ringwald     reverse_bd_addr(hfp_connection->remote_addr,&event[pos]);
506d0c4aea6SMilanka Ringwald     pos += 6;
507d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
5085e8e3664SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->packet_types);
5091a9e9d0bSMatthias Ringwald     pos += 2;
510c8149263SMatthias Ringwald     little_endian_store_16(event, pos, rx_packet_length);
511c8149263SMatthias Ringwald     pos += 2;
512c8149263SMatthias Ringwald     little_endian_store_16(event, pos, tx_packet_length);
513c8149263SMatthias Ringwald     pos += 2;
514ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
515d0c4aea6SMilanka Ringwald }
516d0c4aea6SMilanka Ringwald 
517ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
5187d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
5192c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
5202c50df93SBjoern Hartmann     uint8_t event[256];
5212c50df93SBjoern Hartmann #else
522c1797c7dSMatthias Ringwald     uint8_t event[40];
5232c50df93SBjoern Hartmann #endif
524ab2445a0SMatthias Ringwald     uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6);
525aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
526c10fde09SMatthias Ringwald     event[1] = 4 + string_len;
527aa4dd815SMatthias Ringwald     event[2] = event_subtype;
528d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
529c10fde09SMatthias Ringwald     memcpy((char*)&event[5], value, string_len);
530c10fde09SMatthias Ringwald     event[5 + string_len] = 0;
531c10fde09SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, 6 + string_len);
532aa4dd815SMatthias Ringwald }
533aa4dd815SMatthias Ringwald 
5340cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
5358f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
5363deb3ec6SMatthias Ringwald }
5373deb3ec6SMatthias Ringwald 
5383deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
539665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
540665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
541665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
542a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
543a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
544a0ffb263SMatthias Ringwald             return hfp_connection;
5453deb3ec6SMatthias Ringwald         }
5463deb3ec6SMatthias Ringwald     }
5473deb3ec6SMatthias Ringwald     return NULL;
5483deb3ec6SMatthias Ringwald }
5493deb3ec6SMatthias Ringwald 
550405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
551665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
552665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
553665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
554a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
5555df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
556a0ffb263SMatthias Ringwald             return hfp_connection;
5573deb3ec6SMatthias Ringwald         }
5583deb3ec6SMatthias Ringwald     }
5593deb3ec6SMatthias Ringwald     return NULL;
5603deb3ec6SMatthias Ringwald }
5613deb3ec6SMatthias Ringwald 
562405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
563665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
564665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
565665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
566a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
567c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
568a0ffb263SMatthias Ringwald             return hfp_connection;
5693deb3ec6SMatthias Ringwald         }
5703deb3ec6SMatthias Ringwald     }
5713deb3ec6SMatthias Ringwald     return NULL;
5723deb3ec6SMatthias Ringwald }
5733deb3ec6SMatthias Ringwald 
574405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
575d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
576d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
577d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
578d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
579c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
580d97d752dSMilanka Ringwald             return hfp_connection;
581d97d752dSMilanka Ringwald         }
582d97d752dSMilanka Ringwald     }
583d97d752dSMilanka Ringwald     return NULL;
584d97d752dSMilanka Ringwald }
585d97d752dSMilanka Ringwald 
586c95b5b3cSMilanka Ringwald 
587c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){
588c95b5b3cSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
589c95b5b3cSMilanka Ringwald     hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state;
590c95b5b3cSMilanka Ringwald     hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
591c95b5b3cSMilanka Ringwald 
592c95b5b3cSMilanka Ringwald     if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
593553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
594c95b5b3cSMilanka Ringwald     } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){
595553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
596c95b5b3cSMilanka Ringwald     }
597c95b5b3cSMilanka Ringwald 
598c95b5b3cSMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF;
599c95b5b3cSMilanka Ringwald     hfp_connection->activate_voice_recognition = false;
600c95b5b3cSMilanka Ringwald     hfp_connection->deactivate_voice_recognition = false;
601c95b5b3cSMilanka Ringwald     hfp_connection->enhanced_voice_recognition_enabled = false;
602c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_status = 0;
603c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
604c95b5b3cSMilanka Ringwald }
605c95b5b3cSMilanka Ringwald 
606a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
607a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
608a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
609a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
6103deb3ec6SMatthias Ringwald 
611dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
6123deb3ec6SMatthias Ringwald 
613a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
614a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
6153deb3ec6SMatthias Ringwald 
616a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
617a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
6183deb3ec6SMatthias Ringwald 
619a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
620a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
6217522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
622a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
6233deb3ec6SMatthias Ringwald 
624a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
625a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
626a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
627a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
628125560b8SMatthias Ringwald     hfp_connection->clip_have_alpha = false;
629c95b5b3cSMilanka Ringwald     hfp_reset_voice_recognition(hfp_connection);
6303deb3ec6SMatthias Ringwald }
6313deb3ec6SMatthias Ringwald 
632fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
633a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
634a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
6353deb3ec6SMatthias Ringwald 
636a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
637a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
638a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
639aa4dd815SMatthias Ringwald 
640a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
6413deb3ec6SMatthias Ringwald 
642d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
643d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
644d751f069SMatthias Ringwald 
645a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
6463deb3ec6SMatthias Ringwald 
647a54f7443SMatthias Ringwald     btstack_linked_list_add_tail(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
648a0ffb263SMatthias Ringwald     return hfp_connection;
6493deb3ec6SMatthias Ringwald }
6503deb3ec6SMatthias Ringwald 
65148e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
652a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
65309a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
6543deb3ec6SMatthias Ringwald }
6553deb3ec6SMatthias Ringwald 
6564eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){
657405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
658a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
659a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
660ea7d4f28SPeter Harper     if (!hfp_connection) {
661ea7d4f28SPeter Harper         return NULL;
662ea7d4f28SPeter Harper     }
6636535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
664323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
665bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
666bdf572f4SMatthias Ringwald 
6675fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS
6685fd6f360SMatthias Ringwald     hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID;
6695fd6f360SMatthias Ringwald     hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID;
6705fd6f360SMatthias Ringwald #endif
6715fd6f360SMatthias Ringwald 
672a0ffb263SMatthias Ringwald     return hfp_connection;
6733deb3ec6SMatthias Ringwald }
6743deb3ec6SMatthias Ringwald 
675aa4dd815SMatthias Ringwald /* @param network.
676aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
677aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
678aa4dd815SMatthias Ringwald  */
6793deb3ec6SMatthias Ringwald 
6803deb3ec6SMatthias Ringwald /* @param suported_features
6813deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6823deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
6833deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
6843deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
6853deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
6863deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6873deb3ec6SMatthias Ringwald  */
6883deb3ec6SMatthias Ringwald  /* Bit position:
6893deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
6903deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6913deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
6923deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
6933deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
6943deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6953deb3ec6SMatthias Ringwald  */
6963deb3ec6SMatthias Ringwald 
6979b1c3b4dSMatthias Ringwald void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){
6983deb3ec6SMatthias Ringwald     uint8_t* attribute;
6993deb3ec6SMatthias Ringwald     de_create_sequence(service);
7003deb3ec6SMatthias Ringwald 
7013deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
702235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
7039b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
7043deb3ec6SMatthias Ringwald 
7053deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
706235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
7073deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7083deb3ec6SMatthias Ringwald     {
7093deb3ec6SMatthias Ringwald         //  "UUID for Service"
7103deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
711235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
7123deb3ec6SMatthias Ringwald     }
7133deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7143deb3ec6SMatthias Ringwald 
7153deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
716235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
7173deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7183deb3ec6SMatthias Ringwald     {
7193deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
7203deb3ec6SMatthias Ringwald         {
721235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
7223deb3ec6SMatthias Ringwald         }
7233deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
7243deb3ec6SMatthias Ringwald 
7253deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
7263deb3ec6SMatthias Ringwald         {
727235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
7283deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
7293deb3ec6SMatthias Ringwald         }
7303deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
7313deb3ec6SMatthias Ringwald     }
7323deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7333deb3ec6SMatthias Ringwald 
7343deb3ec6SMatthias Ringwald 
7353deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
736235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
7373deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7383deb3ec6SMatthias Ringwald     {
739235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
7403deb3ec6SMatthias Ringwald     }
7413deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7423deb3ec6SMatthias Ringwald 
7433deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
744235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
7453deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7463deb3ec6SMatthias Ringwald     {
7473deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
7483deb3ec6SMatthias Ringwald         {
749235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
7502eb5c290SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0109); // Version 1.9
7513deb3ec6SMatthias Ringwald         }
7523deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
7533deb3ec6SMatthias Ringwald     }
7543deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7553deb3ec6SMatthias Ringwald 
7563deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
757ada240cdSMatthias Ringwald     if (strlen(name) > 0){
7583deb3ec6SMatthias Ringwald         de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
759ab2445a0SMatthias Ringwald         de_add_data(service,  DE_STRING, (uint16_t) strlen(name), (uint8_t *) name);
7603deb3ec6SMatthias Ringwald     }
761ada240cdSMatthias Ringwald }
7623deb3ec6SMatthias Ringwald 
7637dceaecaSMatthias Ringwald static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){
7647dceaecaSMatthias Ringwald     // cache fields for event
7657dceaecaSMatthias Ringwald     hfp_role_t local_role = hfp_connection->local_role;
7667dceaecaSMatthias Ringwald     bd_addr_t remote_addr;
767d5529700SMatthias Ringwald     // cppcheck-suppress uninitvar ; remote_addr is reported as uninitialized although it's the destination of the memcpy
768481c0cbcSMatthias Ringwald     (void)memcpy(remote_addr, hfp_connection->remote_addr, 6);
7697dceaecaSMatthias Ringwald     // finalize connection struct
7707dceaecaSMatthias Ringwald     hfp_finalize_connection_context(hfp_connection);
7717dceaecaSMatthias Ringwald     // emit event
7727dceaecaSMatthias Ringwald     hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr);
7737dceaecaSMatthias Ringwald }
7747dceaecaSMatthias Ringwald 
7756c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
7768a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
7778a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
7788a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
7791d9c9c90SMilanka Ringwald 
780aeb0f0feSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
7811d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
782aeb0f0feSMatthias Ringwald         log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
78384904e95SMilanka Ringwald         return;
78484904e95SMilanka Ringwald     }
7853deb3ec6SMatthias Ringwald 
7860e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
7875611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
788a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
7893deb3ec6SMatthias Ringwald             break;
7905611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
791a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
792a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
793520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
794520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
795520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
796520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
797520c92d5SMatthias Ringwald                         break;
798520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
799520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
800520c92d5SMatthias Ringwald                         break;
801520c92d5SMatthias Ringwald                     default:
8021d9c9c90SMilanka Ringwald                         btstack_assert(false);
803520c92d5SMatthias Ringwald                         return;
804520c92d5SMatthias Ringwald                 }
8051d9c9c90SMilanka Ringwald 
806ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
8071d9c9c90SMilanka Ringwald 
8081d9c9c90SMilanka Ringwald             } else {
809cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
810cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
811cc92f22bSMatthias Ringwald                     // report service not found
812cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
813cc92f22bSMatthias Ringwald                 }
8147dceaecaSMatthias Ringwald                 hfp_handle_slc_setup_error(hfp_connection, status);
815cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
8161d9c9c90SMilanka Ringwald             }
8171d9c9c90SMilanka Ringwald 
8181d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
8191d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
820aeb0f0feSMatthias Ringwald             (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
8213deb3ec6SMatthias Ringwald             break;
8223deb3ec6SMatthias Ringwald         default:
8233deb3ec6SMatthias Ringwald             break;
8243deb3ec6SMatthias Ringwald     }
8253deb3ec6SMatthias Ringwald }
8263deb3ec6SMatthias Ringwald 
82738200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
82838200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
829eddcd308SMatthias Ringwald 
83030e5ec75SMatthias Ringwald     if (hfp_sco_establishment_active->accept_sco != 0){
8313427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
83238200c1dSMilanka Ringwald         return 0;
833eddcd308SMatthias Ringwald     }
834eddcd308SMatthias Ringwald 
8353427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
8363427dd75SMatthias Ringwald     switch (status){
83733c74df1SMatthias Ringwald         case ERROR_CODE_INVALID_LMP_PARAMETERS_INVALID_LL_PARAMETERS:
8381579b82fSMatthias Ringwald         case ERROR_CODE_SCO_AIR_MODE_REJECTED:
8391579b82fSMatthias Ringwald         case ERROR_CODE_SCO_INTERVAL_REJECTED:
8401579b82fSMatthias Ringwald         case ERROR_CODE_SCO_OFFSET_REJECTED:
8413427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
8421579b82fSMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
843aac28b12SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE:
84469a0db57SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE:
845eddcd308SMatthias Ringwald             break;
8463427dd75SMatthias Ringwald         default:
8473427dd75SMatthias Ringwald             return 0;
8483427dd75SMatthias Ringwald     }
8493427dd75SMatthias Ringwald 
8503427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
851aeb0f0feSMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false);
8523427dd75SMatthias Ringwald 
8533427dd75SMatthias Ringwald     // handle no valid setting found
8543427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
855aeb0f0feSMatthias Ringwald         if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
8563427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
857aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
858aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->sco_for_msbc_failed = 1;
859a814da6aSMatthias Ringwald             hfp_sco_establishment_active->ag_send_common_codec = true;
860aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
8613427dd75SMatthias Ringwald         } else {
8623427dd75SMatthias Ringwald             // no other options
8633427dd75SMatthias Ringwald             return 0;
864eddcd308SMatthias Ringwald         }
8653427dd75SMatthias Ringwald     }
8663427dd75SMatthias Ringwald 
8673427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
868aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->establish_audio_connection = 1;
869aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->link_setting = next_setting;
870677003cbSMatthias Ringwald     hfp_sco_establishment_active->accept_sco = 0;
871aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
87238200c1dSMilanka Ringwald     return 1;
873eddcd308SMatthias Ringwald }
874eddcd308SMatthias Ringwald 
875405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8765575558eSMilanka Ringwald     UNUSED(packet_type);
8778a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8785575558eSMilanka Ringwald     UNUSED(size);
8799ec2630cSMatthias Ringwald 
8803deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
88127950165SMatthias Ringwald     hci_con_handle_t handle;
882a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
883674515e8SMatthias Ringwald     uint8_t status;
8843deb3ec6SMatthias Ringwald 
88527950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
886aa4dd815SMatthias Ringwald 
8870e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
8881b005648SMatthias Ringwald 
889011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
8907522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
8917522e673SMatthias Ringwald                 case 0: //  SCO
8927522e673SMatthias Ringwald                 case 2: // eSCO
893011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
894405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
895011577abSMilanka Ringwald                     if (!hfp_connection) break;
896c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
897cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 2;
898c169df2fSMatthias Ringwald                     } else {
899cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 1;
900c169df2fSMatthias Ringwald                     }
9010327df7dSMatthias Ringwald                     // configure SBC coded if needed
9020327df7dSMatthias Ringwald                     hfp_prepare_for_sco(hfp_connection);
903cb81d35dSMatthias Ringwald                     log_info("accept sco %u\n", hfp_connection->accept_sco);
904202c8a4cSMatthias Ringwald                     break;
9057522e673SMatthias Ringwald                 default:
9067522e673SMatthias Ringwald                     break;
9077522e673SMatthias Ringwald             }
908011577abSMilanka Ringwald             break;
9093deb3ec6SMatthias Ringwald 
910eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
911eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
912aeb0f0feSMatthias Ringwald                 if (hfp_sco_establishment_active == NULL) break;
913be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
91438200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
91538200c1dSMilanka Ringwald 
916aeb0f0feSMatthias Ringwald                 hfp_connection = hfp_sco_establishment_active;
91738200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
918677003cbSMatthias Ringwald 
919677003cbSMatthias Ringwald                 hfp_connection->accept_sco = 0;
92038200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
92138200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
922447743f7SMatthias Ringwald                 hfp_sco_establishment_active = NULL;
923f14c5dafSMatthias Ringwald                 hfp_emit_sco_connection_established(hfp_connection, status,
9245e8e3664SMatthias Ringwald                                                     hfp_connection->negotiated_codec, 0, 0);
9256c5b2002SMatthias Ringwald             }
926eddcd308SMatthias Ringwald             break;
927eddcd308SMatthias Ringwald 
9283deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
929aeb0f0feSMatthias Ringwald             if (hfp_sco_establishment_active == NULL) break;
930447743f7SMatthias Ringwald 
931447743f7SMatthias Ringwald             hfp_connection = hfp_sco_establishment_active;
932ce263fc8SMatthias Ringwald 
933011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
93438200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
93538200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
93638200c1dSMilanka Ringwald 
937677003cbSMatthias Ringwald                 hfp_connection->accept_sco = 0;
93838200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
93938200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
940447743f7SMatthias Ringwald                 hfp_sco_establishment_active = NULL;
941f14c5dafSMatthias Ringwald                 hfp_emit_sco_connection_established(hfp_connection, status,
9425e8e3664SMatthias Ringwald                                                     hfp_connection->negotiated_codec, 0, 0);
943aa4dd815SMatthias Ringwald                 break;
944aa4dd815SMatthias Ringwald             }
945aa4dd815SMatthias Ringwald 
946011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
947011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
948011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
949011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
950011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
951011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald             switch (link_type){
9543deb3ec6SMatthias Ringwald                 case 0x00:
955aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
9563deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
9573deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
9583deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
9593deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
9603deb3ec6SMatthias Ringwald                     break;
9613deb3ec6SMatthias Ringwald                 case 0x02:
962aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
9633deb3ec6SMatthias Ringwald                     break;
9643deb3ec6SMatthias Ringwald                 default:
9653deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
9663deb3ec6SMatthias Ringwald                     break;
9673deb3ec6SMatthias Ringwald             }
968e0d13a19SMilanka Ringwald 
9693deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
970aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
971e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
972e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
9733deb3ec6SMatthias Ringwald 
974d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
975aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
976d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
977aa4dd815SMatthias Ringwald                 break;
978aa4dd815SMatthias Ringwald             }
979d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
980677003cbSMatthias Ringwald             hfp_connection->accept_sco = 0;
981d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
982c95b5b3cSMilanka Ringwald 
983d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
9841a26de69SMilanka Ringwald 
9850b4debbfSMilanka Ringwald             switch (hfp_connection->vra_state){
9860b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
9871a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = false;
9880b4debbfSMilanka Ringwald                     break;
9890b4debbfSMilanka Ringwald                 default:
9901a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = true;
9910b4debbfSMilanka Ringwald                     break;
9920b4debbfSMilanka Ringwald             }
993447743f7SMatthias Ringwald 
994447743f7SMatthias Ringwald             hfp_sco_establishment_active = NULL;
995447743f7SMatthias Ringwald 
996f14c5dafSMatthias Ringwald             hfp_emit_sco_connection_established(hfp_connection, status,
9975e8e3664SMatthias Ringwald                                                 hfp_connection->negotiated_codec, rx_packet_length, tx_packet_length);
9985fd6f360SMatthias Ringwald 
9995fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS
10005fd6f360SMatthias Ringwald             hfp_connection->nxp_start_audio_handle = hfp_connection->sco_handle;
10015fd6f360SMatthias Ringwald #endif
10023deb3ec6SMatthias Ringwald             break;
10033deb3ec6SMatthias Ringwald         }
10043deb3ec6SMatthias Ringwald 
10053deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1006f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
1007405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
1008aa4dd815SMatthias Ringwald 
1009d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
1010aa4dd815SMatthias Ringwald 
10113721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
10123721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
10133721a235SMatthias Ringwald #endif
1014689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1015689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
1016689d4323SMatthias Ringwald #endif
10175fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS
10185fd6f360SMatthias Ringwald             hfp_connection->nxp_stop_audio_handle = hfp_connection->sco_handle;
10195fd6f360SMatthias Ringwald #endif
10205fd6f360SMatthias Ringwald 
102152cbdd6dSMilanka Ringwald             if (hfp_connection->sco_handle == handle){
1022d751f069SMatthias Ringwald                 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
1023d63c37a1SMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
102452cbdd6dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
102552cbdd6dSMilanka Ringwald                 hfp_emit_audio_connection_released(hfp_connection, handle);
102652cbdd6dSMilanka Ringwald 
10271a26de69SMilanka Ringwald                 hfp_connection->ag_audio_connection_opened_before_vra = false;
102852cbdd6dSMilanka Ringwald 
102952cbdd6dSMilanka Ringwald                 if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){
103011b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
103152cbdd6dSMilanka Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
103252cbdd6dSMilanka Ringwald                     hfp_finalize_connection_context(hfp_connection);
103319976d6bSMilanka Ringwald                     break;
103452cbdd6dSMilanka Ringwald                 } else if (hfp_connection->release_slc_connection == 1){
103552cbdd6dSMilanka Ringwald                     hfp_connection->release_slc_connection = 0;
103652cbdd6dSMilanka Ringwald                     hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
103752cbdd6dSMilanka Ringwald                     rfcomm_disconnect(hfp_connection->acl_handle);
103852cbdd6dSMilanka Ringwald                 }
103952cbdd6dSMilanka Ringwald             }
104048e6eeeeSMatthias Ringwald 
104148e6eeeeSMatthias Ringwald             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
104248e6eeeeSMatthias Ringwald                 // RFCOMM already closed -> remote power off
104348e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
104448e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
104548e6eeeeSMatthias Ringwald #else
104648e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
104748e6eeeeSMatthias Ringwald #endif
104848e6eeeeSMatthias Ringwald                 break;
104948e6eeeeSMatthias Ringwald             }
10503deb3ec6SMatthias Ringwald             break;
10513deb3ec6SMatthias Ringwald 
1052fc64f94aSMatthias Ringwald         default:
10533deb3ec6SMatthias Ringwald             break;
10543deb3ec6SMatthias Ringwald     }
10553deb3ec6SMatthias Ringwald }
10563deb3ec6SMatthias Ringwald 
10573721a235SMatthias Ringwald 
105827950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
10595575558eSMilanka Ringwald     UNUSED(packet_type);
106027950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
10615575558eSMilanka Ringwald     UNUSED(size);
106227950165SMatthias Ringwald 
106327950165SMatthias Ringwald     bd_addr_t event_addr;
106427950165SMatthias Ringwald     uint16_t rfcomm_cid;
106527950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
106627950165SMatthias Ringwald     uint8_t status;
106727950165SMatthias Ringwald 
106827950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
106927950165SMatthias Ringwald 
107027950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
107127950165SMatthias Ringwald 
107227950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
107327950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
107427950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
10754eb3f1d8SMilanka Ringwald             hfp_connection = hfp_create_connection(event_addr, local_role);
107627950165SMatthias Ringwald             if (!hfp_connection){
107727950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
107827950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
107927950165SMatthias Ringwald                 return;
108027950165SMatthias Ringwald             }
108127950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
10824960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
10834960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
108427950165SMatthias Ringwald                 return;
108527950165SMatthias Ringwald             }
108627950165SMatthias Ringwald 
108727950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
108827950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
108927950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
109027950165SMatthias Ringwald             break;
109127950165SMatthias Ringwald 
109227950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
109327950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
109427950165SMatthias Ringwald 
1095405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
10966504a03aSMilanka Ringwald             btstack_assert(hfp_connection != NULL);
109727950165SMatthias Ringwald 
10986504a03aSMilanka Ringwald             if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){
10996504a03aSMilanka Ringwald                 break;
11006504a03aSMilanka Ringwald             }
11016504a03aSMilanka Ringwald 
11026504a03aSMilanka Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
11036504a03aSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS) {
11047dceaecaSMatthias Ringwald                 hfp_handle_slc_setup_error(hfp_connection, status);
11056504a03aSMilanka Ringwald                 break;
11066504a03aSMilanka Ringwald             }
11076504a03aSMilanka Ringwald 
110827950165SMatthias Ringwald             hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
110927950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
11106504a03aSMilanka Ringwald             hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
111127950165SMatthias Ringwald             bd_addr_copy(hfp_connection->remote_addr, event_addr);
111227950165SMatthias Ringwald             hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
11136504a03aSMilanka Ringwald 
111427950165SMatthias Ringwald             rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
111527950165SMatthias Ringwald             break;
111627950165SMatthias Ringwald 
111727950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
111827950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
111927950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
112027950165SMatthias Ringwald             if (!hfp_connection) break;
112152cbdd6dSMilanka Ringwald             switch (hfp_connection->state){
112252cbdd6dSMilanka Ringwald                 case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART:
112352cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
112427950165SMatthias Ringwald                     hfp_connection->state = HFP_IDLE;
112527950165SMatthias Ringwald                     hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
112627950165SMatthias Ringwald                     break;
112752cbdd6dSMilanka Ringwald 
112852cbdd6dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
112948e6eeeeSMatthias Ringwald                     // service connection was released, this implicitly releases audio connection as well
113048e6eeeeSMatthias Ringwald                     hfp_connection->release_audio_connection = 0;
113152cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
113248e6eeeeSMatthias Ringwald                     hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
113352cbdd6dSMilanka Ringwald                     gap_disconnect(hfp_connection->sco_handle);
113452cbdd6dSMilanka Ringwald                     break;
113552cbdd6dSMilanka Ringwald 
113652cbdd6dSMilanka Ringwald                 default:
113748e6eeeeSMatthias Ringwald                     // regular case
113811b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
113948e6eeeeSMatthias Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
114048e6eeeeSMatthias Ringwald                     hfp_finalize_connection_context(hfp_connection);
114152cbdd6dSMilanka Ringwald                     break;
114248e6eeeeSMatthias Ringwald             }
114327950165SMatthias Ringwald             break;
114427950165SMatthias Ringwald 
114527950165SMatthias Ringwald         default:
114627950165SMatthias Ringwald             break;
114727950165SMatthias Ringwald     }
114827950165SMatthias Ringwald }
1149aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
115094a27792SMatthias Ringwald 
115194a27792SMatthias Ringwald typedef struct {
115294a27792SMatthias Ringwald     const char * command;
115394a27792SMatthias Ringwald     hfp_command_t command_id;
115494a27792SMatthias Ringwald } hfp_command_entry_t;
115594a27792SMatthias Ringwald 
1156f703e170SMatthias Ringwald static hfp_command_entry_t hfp_ag_command_table[] = {
1157cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
1158cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
1159cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
1160cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
1161cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
116294a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
116394a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
116494a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
1165051f797dSMilanka Ringwald     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
1166cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
1167cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
116894a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
116994a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
11709ed286f3SMilanka Ringwald     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
1171cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
117294a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
117394a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1174cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
117594a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
117694a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
1177cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
1178cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
1179cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
1180cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
1181cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
118294a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
118394a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1184cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
1185cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
1186cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
1187d8656ee6SMilanka Ringwald     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
118894a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
118994a27792SMatthias Ringwald };
119094a27792SMatthias Ringwald 
1191f703e170SMatthias Ringwald static hfp_command_entry_t hfp_hf_command_table[] = {
1192cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
119394a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1194b3615b9aSMilanka Ringwald     { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER },
1195cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1196cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
119794a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1198cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1199cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
120094a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1201cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
120284a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1203cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1204cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1205cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1206cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1207cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1208cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1209ce284ffbSMatthias Ringwald     { "+VGM=",  HFP_CMD_SET_MICROPHONE_GAIN },
12104c30d2d1SMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1211ce284ffbSMatthias Ringwald     { "+VGS=",  HFP_CMD_SET_SPEAKER_GAIN},
1212cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
1213cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1214cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
1215cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
121694a27792SMatthias Ringwald };
121794a27792SMatthias Ringwald 
12186e64d9feSMatthias Ringwald static const hfp_custom_at_command_t *
12196e64d9feSMatthias Ringwald hfp_custom_command_lookup(bool isHandsFree, const char *text) {
12206e64d9feSMatthias Ringwald     btstack_linked_list_t * custom_commands = isHandsFree ? &hfp_custom_commands_hf : &hfp_custom_commands_ag;
122104cf3dd4SMatthias Ringwald     btstack_linked_list_iterator_t it;
12226e64d9feSMatthias Ringwald     btstack_linked_list_iterator_init(&it, custom_commands);
1223471dea41SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)) {
1224471dea41SMatthias Ringwald         hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it);
1225471dea41SMatthias Ringwald         int match = strcmp(text, at_command->command);
1226471dea41SMatthias Ringwald         if (match == 0) {
1227471dea41SMatthias Ringwald             return at_command;
1228471dea41SMatthias Ringwald         }
1229471dea41SMatthias Ringwald     }
1230471dea41SMatthias Ringwald     return NULL;
1231471dea41SMatthias Ringwald }
1232471dea41SMatthias Ringwald 
1233aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
12343deb3ec6SMatthias Ringwald 
1235ff158b9bSMatthias Ringwald     // check for custom commands, AG only
12366e64d9feSMatthias Ringwald     const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(isHandsFree, line_buffer);
1237ff158b9bSMatthias Ringwald     if (custom_at_command != NULL){
1238ff158b9bSMatthias Ringwald         return HFP_CMD_CUSTOM_MESSAGE;
1239ff158b9bSMatthias Ringwald     }
1240ff158b9bSMatthias Ringwald 
1241cb33905eSMatthias Ringwald     // table lookup based on role
124294a27792SMatthias Ringwald     uint16_t num_entries;
124394a27792SMatthias Ringwald     hfp_command_entry_t * table;
124494a27792SMatthias Ringwald     if (isHandsFree == 0){
1245f703e170SMatthias Ringwald         table = hfp_ag_command_table;
1246f703e170SMatthias Ringwald         num_entries = sizeof(hfp_ag_command_table) / sizeof(hfp_command_entry_t);
12473deb3ec6SMatthias Ringwald     } else {
1248f703e170SMatthias Ringwald         table = hfp_hf_command_table;
1249f703e170SMatthias Ringwald         num_entries = sizeof(hfp_hf_command_table) / sizeof(hfp_command_entry_t);
125094a27792SMatthias Ringwald     }
1251791f0d0aSMatthias Ringwald     // binary search
1252791f0d0aSMatthias Ringwald     uint16_t left = 0;
1253791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
1254791f0d0aSMatthias Ringwald     while (left <= right){
1255791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
1256791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
125794a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
1258791f0d0aSMatthias Ringwald         if (match < 0){
1259791f0d0aSMatthias Ringwald             // search term is lower than middle element
1260*ff38a59eSMatthias Ringwald             if (middle == 0) break;
1261791f0d0aSMatthias Ringwald             right = middle - 1;
1262791f0d0aSMatthias Ringwald         } else if (match == 0){
126394a27792SMatthias Ringwald             return entry->command_id;
1264791f0d0aSMatthias Ringwald         } else {
1265791f0d0aSMatthias Ringwald             // search term is higher than middle element
1266791f0d0aSMatthias Ringwald             left = middle + 1;
126794a27792SMatthias Ringwald         }
126894a27792SMatthias Ringwald     }
126994a27792SMatthias Ringwald 
1270cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
12710222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1272cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1273cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1274cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
12753deb3ec6SMatthias Ringwald     }
12763deb3ec6SMatthias Ringwald 
1277cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1278cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1279aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
12803deb3ec6SMatthias Ringwald     }
12813deb3ec6SMatthias Ringwald 
1282cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1283aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1284aa4dd815SMatthias Ringwald     }
12853deb3ec6SMatthias Ringwald 
1286aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
12873deb3ec6SMatthias Ringwald }
12883deb3ec6SMatthias Ringwald 
1289a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
129051aa5d5aSMilanka Ringwald     if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return;
1291a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1292a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
12933deb3ec6SMatthias Ringwald }
1294a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1295a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
12963deb3ec6SMatthias Ringwald }
12973deb3ec6SMatthias Ringwald 
12983deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1299c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
13003deb3ec6SMatthias Ringwald }
13013deb3ec6SMatthias Ringwald 
1302471dea41SMatthias Ringwald void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1303a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
1304125560b8SMatthias Ringwald     // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand
13053deb3ec6SMatthias Ringwald }
13061d81c7feSMatthias Ringwald 
13071d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
13081d81c7feSMatthias Ringwald     switch (byte){
13091d81c7feSMatthias Ringwald         case ',':
1310f8301d46SMatthias Ringwald 		case '-':
13111d81c7feSMatthias Ringwald         case ';':
13121d81c7feSMatthias Ringwald         case '(':
13131d81c7feSMatthias Ringwald         case ')':
13141d81c7feSMatthias Ringwald         case '\n':
13151d81c7feSMatthias Ringwald         case '\r':
13163deb3ec6SMatthias Ringwald             break;
13173deb3ec6SMatthias Ringwald         default:
13181d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
13193deb3ec6SMatthias Ringwald             break;
13203deb3ec6SMatthias Ringwald     }
13213deb3ec6SMatthias Ringwald }
13221d81c7feSMatthias Ringwald 
13231d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
13241d81c7feSMatthias Ringwald     switch (byte){
13251d81c7feSMatthias Ringwald         case ',':
1326f8301d46SMatthias Ringwald 		case '-':
13271d81c7feSMatthias Ringwald         case ';':
13281d81c7feSMatthias Ringwald         case '\n':
13291d81c7feSMatthias Ringwald         case '\r':
13301d81c7feSMatthias Ringwald             return true;
1331dd533208SMatthias Ringwald         default:
13321d81c7feSMatthias Ringwald             return false;
13333deb3ec6SMatthias Ringwald     }
13343deb3ec6SMatthias Ringwald }
13353deb3ec6SMatthias Ringwald 
1336f79a7821SMatthias Ringwald // returns true if received bytes was processed. Otherwise, functions will be called with same byte again
1337f79a7821SMatthias Ringwald // this is used to for a one byte lookahead, where an unexpected byte is pushed back by returning false
1338d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1339aa4dd815SMatthias Ringwald 
1340f79a7821SMatthias Ringwald #ifdef HFP_DEBUG
1341f79a7821SMatthias Ringwald     if (byte >= ' '){
1342f79a7821SMatthias Ringwald         printf("Parse  '%c' - state %u, buffer %s\n", byte, hfp_connection->parser_state, hfp_connection->line_buffer);
1343f79a7821SMatthias Ringwald     } else {
1344f79a7821SMatthias Ringwald         printf("Parse 0x%02x - state %u, buffer %s\n", byte, hfp_connection->parser_state, hfp_connection->line_buffer);
1345f79a7821SMatthias Ringwald     }
1346f79a7821SMatthias Ringwald #endif
1347f79a7821SMatthias Ringwald 
13481dddc4f4SMatthias Ringwald     // handle doubles quotes
13491dddc4f4SMatthias Ringwald     if (byte == '"'){
13501dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1351d6b237acSMatthias Ringwald         return true;
13521dddc4f4SMatthias Ringwald     }
13531dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
13541dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1355d6b237acSMatthias Ringwald         return true;
13561dddc4f4SMatthias Ringwald     }
13573deb3ec6SMatthias Ringwald 
13581dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1359d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
13601dddc4f4SMatthias Ringwald 
1361bfeaf344SMatthias Ringwald     bool processed = true;
1362bfeaf344SMatthias Ringwald 
1363a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1364dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1365d28b2d5dSMilanka Ringwald             switch (byte) {
1366dd533208SMatthias Ringwald                 case '\n':
1367dd533208SMatthias Ringwald                 case '\r':
1368dd533208SMatthias Ringwald                 case ';':
1369bfeaf344SMatthias Ringwald                     // ignore separator
1370bfeaf344SMatthias Ringwald                     break;
1371bfeaf344SMatthias Ringwald                 case ':':
1372bfeaf344SMatthias Ringwald                 case '?':
1373bfeaf344SMatthias Ringwald                     // store separator
1374bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1375dd533208SMatthias Ringwald                     break;
1376d28b2d5dSMilanka Ringwald                 case '=':
1377bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1378dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1379a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1380d6b237acSMatthias Ringwald                     return true;
1381d28b2d5dSMilanka Ringwald                 default:
1382bfeaf344SMatthias Ringwald                     // store if not lookahead
1383d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1384dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1385d6b237acSMatthias Ringwald                         return true;
1386dd533208SMatthias Ringwald                     }
1387bfeaf344SMatthias Ringwald                     // mark as lookahead
1388bfeaf344SMatthias Ringwald                     processed = false;
1389d7f16ff1SMatthias Ringwald                     break;
1390d7f16ff1SMatthias Ringwald             }
1391ce263fc8SMatthias Ringwald 
13921d81c7feSMatthias Ringwald             // ignore empty tokens
1393d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1394dd533208SMatthias Ringwald 
1395bfeaf344SMatthias Ringwald             // parse
1396dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1397aa4dd815SMatthias Ringwald 
139884a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
139984a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1400a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1401aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1402a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1403aa4dd815SMatthias Ringwald                         break;
1404aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1405a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1406aa4dd815SMatthias Ringwald                         break;
1407aa4dd815SMatthias Ringwald                     default:
140884a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1409aa4dd815SMatthias Ringwald                         break;
1410aa4dd815SMatthias Ringwald                 }
1411aa4dd815SMatthias Ringwald             }
14123deb3ec6SMatthias Ringwald 
1413c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1414c0c0437aSMatthias Ringwald 
1415471dea41SMatthias Ringwald             // store command id for custom command and just store rest of line
1416471dea41SMatthias Ringwald             if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){
14176e64d9feSMatthias Ringwald                 const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup(isHandsFree, (const char *) hfp_connection->line_buffer);
14186d9a41f9SMatthias Ringwald                 hfp_connection->custom_at_command_id = at_command->command_id;
1419471dea41SMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND;
1420f79a7821SMatthias Ringwald                 return processed;
1421471dea41SMatthias Ringwald             }
1422471dea41SMatthias Ringwald 
1423bfeaf344SMatthias Ringwald             // next state
14241d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1425dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1426dd533208SMatthias Ringwald 
1427bfeaf344SMatthias Ringwald             return processed;
1428dd533208SMatthias Ringwald 
1429bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
14301d81c7feSMatthias Ringwald             // handle empty fields
14311d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1432dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1433dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1434dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
14353d051a53SMatthias Ringwald                 return true;
14363d051a53SMatthias Ringwald             }
14373d051a53SMatthias Ringwald 
14381d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14391d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
14401d81c7feSMatthias Ringwald 
14411d81c7feSMatthias Ringwald             // ignore empty tokens
144289e7c136SMilanka Ringwald             switch (hfp_connection->command){
144389e7c136SMilanka Ringwald                 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
144489e7c136SMilanka Ringwald                     // don't ignore empty string
144589e7c136SMilanka Ringwald                     break;
144689e7c136SMilanka Ringwald                 default:
144789e7c136SMilanka Ringwald                     if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) {
144889e7c136SMilanka Ringwald                         return true;
144989e7c136SMilanka Ringwald                     }
145089e7c136SMilanka Ringwald                     break;
145189e7c136SMilanka Ringwald             }
14523d051a53SMatthias Ringwald 
14533d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
14541d81c7feSMatthias Ringwald 
14551d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14561d81c7feSMatthias Ringwald 
14571d81c7feSMatthias Ringwald             switch (hfp_connection->command){
14581d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
14591d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
14601d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
14611d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14621d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
14631d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
14641d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14651d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14661d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
14671d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
14681d81c7feSMatthias Ringwald                     break;
14691d81c7feSMatthias Ringwald                 default:
14701d81c7feSMatthias Ringwald                     break;
14711d81c7feSMatthias Ringwald             }
14723d051a53SMatthias Ringwald             return true;
14733d051a53SMatthias Ringwald 
1474ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
14751d81c7feSMatthias Ringwald 
14761d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14771d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1478dd533208SMatthias Ringwald 
1479a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1480ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1481a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
14822308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1483ce263fc8SMatthias Ringwald                     break;
1484ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1485a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
14862308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1487ce263fc8SMatthias Ringwald                     break;
1488ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1489ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1490ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14912308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1492ce263fc8SMatthias Ringwald                     break;
1493ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14942308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1495a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1496a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1497ce263fc8SMatthias Ringwald                     break;
1498ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14992308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1500a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1501ce263fc8SMatthias Ringwald                     break;
1502ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1503a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1504a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
15052308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1506ce263fc8SMatthias Ringwald                     break;
15070222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
15080222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15090222a807SMatthias Ringwald                     break;
1510ce263fc8SMatthias Ringwald                 default:
1511ce263fc8SMatthias Ringwald                     break;
1512ce263fc8SMatthias Ringwald             }
15131d81c7feSMatthias Ringwald 
15141d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
15151d81c7feSMatthias Ringwald 
15161d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
15171d81c7feSMatthias Ringwald 
1518ba0de059SMatthias Ringwald             return true;
1519ce263fc8SMatthias Ringwald 
152074c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
15211d81c7feSMatthias Ringwald 
15221d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
15231d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
15241d81c7feSMatthias Ringwald 
1525a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1526ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1527c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE,  (char *)hfp_connection->line_buffer);
1528ce263fc8SMatthias Ringwald                     break;
1529ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
15302308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
153125789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
153225789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1533ce263fc8SMatthias Ringwald                     break;
1534125560b8SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1535f6e6c52dSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1536125560b8SMatthias Ringwald                     // track if last argument exists
1537125560b8SMatthias Ringwald                     hfp_connection->clip_have_alpha = hfp_connection->line_size != 0;
1538125560b8SMatthias Ringwald                     break;
1539ce263fc8SMatthias Ringwald                 default:
1540ce263fc8SMatthias Ringwald                     break;
1541ce263fc8SMatthias Ringwald             }
15421d81c7feSMatthias Ringwald 
15431d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
15441d81c7feSMatthias Ringwald 
15451d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
15461d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
15471d81c7feSMatthias Ringwald             }
1548d6b237acSMatthias Ringwald             return true;
154974c7548aSMatthias Ringwald 
1550471dea41SMatthias Ringwald         case HFP_PARSER_CUSTOM_COMMAND:
15518deec660SMatthias Ringwald             if (hfp_parser_is_end_of_line(byte) == false){
1552471dea41SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
15538deec660SMatthias Ringwald             }
1554471dea41SMatthias Ringwald             return true;
1555471dea41SMatthias Ringwald 
155674c7548aSMatthias Ringwald         default:
155774c7548aSMatthias Ringwald             btstack_assert(false);
155874c7548aSMatthias Ringwald             return true;
155974c7548aSMatthias Ringwald     }
1560d6b237acSMatthias Ringwald }
1561d6b237acSMatthias Ringwald 
1562d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1563d6b237acSMatthias Ringwald     bool processed = false;
1564d6b237acSMatthias Ringwald     while (!processed){
1565d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1566d6b237acSMatthias Ringwald     }
15671d81c7feSMatthias Ringwald     // reset parser state on end-of-line
15681d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
1569a34e380cSMatthias Ringwald         hfp_connection->found_equal_sign = false;
15701d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
15711d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
15721d81c7feSMatthias Ringwald     }
1573ce263fc8SMatthias Ringwald }
1574ce263fc8SMatthias Ringwald 
1575a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1576ce263fc8SMatthias Ringwald     int value;
1577a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1578667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
15792308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1580667ec068SMatthias Ringwald             int i;
1581a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1582667ec068SMatthias Ringwald                 case 0:
1583a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1584a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1585a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1586667ec068SMatthias Ringwald                             break;
1587667ec068SMatthias Ringwald                         }
1588667ec068SMatthias Ringwald                     }
1589667ec068SMatthias Ringwald                     break;
1590667ec068SMatthias Ringwald                 case 1:
1591a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1592a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1593667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1594a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1595667ec068SMatthias Ringwald                     break;
1596667ec068SMatthias Ringwald                 default:
1597667ec068SMatthias Ringwald                     break;
1598667ec068SMatthias Ringwald             }
159925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1600667ec068SMatthias Ringwald             break;
1601667ec068SMatthias Ringwald 
1602667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1603a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1604667ec068SMatthias Ringwald                 case 0:
160528a4aea3SMilanka Ringwald                     // <alpha>: This optional field is not supported, and shall be left blank.
160628a4aea3SMilanka Ringwald                     break;
160728a4aea3SMilanka Ringwald                 case 1:
160828a4aea3SMilanka Ringwald                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1609c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1610667ec068SMatthias Ringwald                     break;
161128a4aea3SMilanka Ringwald                 case 2:
161228a4aea3SMilanka Ringwald                     /*
161328a4aea3SMilanka Ringwald                       <type> field specifies the format of the phone number provided, and can be one of the following values:
161428a4aea3SMilanka Ringwald                       - values 128-143: The phone number format may be a national or international format, and may contain prefix and/or escape digits. No changes on the number presentation are required.
161528a4aea3SMilanka Ringwald                      - values 144-159: The phone number format is an international number, including the country code prefix. If the plus sign ("+") is not included as part of the number and shall be added by the AG as needed.
161628a4aea3SMilanka Ringwald                      - values 160-175: National number. No prefix nor escape digits included.
161728a4aea3SMilanka Ringwald                      */
16182308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1619a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1620667ec068SMatthias Ringwald                     break;
162128a4aea3SMilanka Ringwald                 case 3:
162228a4aea3SMilanka Ringwald                     // <speed>: This optional field is not supported, and shall be left blank.
162328a4aea3SMilanka Ringwald                     break;
162428a4aea3SMilanka Ringwald                 case 4:
162528a4aea3SMilanka Ringwald                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1626667ec068SMatthias Ringwald                 default:
1627667ec068SMatthias Ringwald                     break;
1628667ec068SMatthias Ringwald             }
1629eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1630a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1631667ec068SMatthias Ringwald             break;
1632667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1633a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1634667ec068SMatthias Ringwald                 case 0:
16352308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1636a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1637667ec068SMatthias Ringwald                     break;
1638667ec068SMatthias Ringwald                 case 1:
16392308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1640a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1641667ec068SMatthias Ringwald                     break;
1642667ec068SMatthias Ringwald                 case 2:
16432308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1644a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1645667ec068SMatthias Ringwald                     break;
1646667ec068SMatthias Ringwald                 case 3:
16472308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
16480aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1649667ec068SMatthias Ringwald                     break;
1650667ec068SMatthias Ringwald                 case 4:
16510aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
16520aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
16530aee97efSMilanka Ringwald                     break;
16540aee97efSMilanka Ringwald                 case 5:
1655c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1656667ec068SMatthias Ringwald                     break;
16570aee97efSMilanka Ringwald                 case 6:
16582308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1659a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1660667ec068SMatthias Ringwald                     break;
1661667ec068SMatthias Ringwald                 default:
1662667ec068SMatthias Ringwald                     break;
1663667ec068SMatthias Ringwald             }
1664eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1665a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1666667ec068SMatthias Ringwald             break;
1667aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
16682308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1669a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1670aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1671aa4dd815SMatthias Ringwald             break;
1672aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
16732308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1674a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1675aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1676aa4dd815SMatthias Ringwald             break;
1677aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
16782308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1679a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1680aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1681aa4dd815SMatthias Ringwald             break;
1682aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
16832308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1684a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1685aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1686aa4dd815SMatthias Ringwald             break;
16873deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
16882308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1689a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
16903deb3ec6SMatthias Ringwald             break;
16913deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
16922308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1693a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
16943deb3ec6SMatthias Ringwald             break;
16953deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
16962308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1697bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
16983deb3ec6SMatthias Ringwald             break;
16993deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1700a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1701ab2445a0SMatthias Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
170225789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1703a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
17043deb3ec6SMatthias Ringwald             break;
1705aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1706c10fde09SMatthias Ringwald             btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer);
1707a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1708a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1709aa4dd815SMatthias Ringwald             break;
1710aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1711a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
17122308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
171325789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
17143deb3ec6SMatthias Ringwald             break;
17153deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
171625789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1717a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1718a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
17192308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1720a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
17213deb3ec6SMatthias Ringwald             break;
17223deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1723a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1724a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
172597d2cfbcSMatthias Ringwald             memcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE-1);
172625789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1727eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
17283deb3ec6SMatthias Ringwald             break;
1729aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1730aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1731a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
17322308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
173325789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1734a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
17353deb3ec6SMatthias Ringwald             break;
1736aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
17373deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1738a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
173925789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
17403deb3ec6SMatthias Ringwald             break;
1741ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
174225789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1743a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1744ce263fc8SMatthias Ringwald             break;
17453deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
17463deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1747a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1748a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1749a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1750a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1751ce263fc8SMatthias Ringwald             }
1752a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1753ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1754a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1755ce263fc8SMatthias Ringwald             } else {
17562308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1757a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1758a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1759a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
17603deb3ec6SMatthias Ringwald             }
176125789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
17623deb3ec6SMatthias Ringwald             break;
17633deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
17643deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
176525789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1766a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
17673deb3ec6SMatthias Ringwald             break;
1768aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
17692308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1770a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1771aa4dd815SMatthias Ringwald             break;
1772aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1773a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1774a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
17753deb3ec6SMatthias Ringwald                 break;
17763deb3ec6SMatthias Ringwald             }
17773deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1778a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
17793deb3ec6SMatthias Ringwald             break;
17803deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
17813deb3ec6SMatthias Ringwald             break;
17823deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1783a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
17842308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
17853deb3ec6SMatthias Ringwald             break;
17863deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
17872308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1788a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
17893deb3ec6SMatthias Ringwald             break;
1790ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1791a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1792a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1793c10fde09SMatthias Ringwald             btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
17943deb3ec6SMatthias Ringwald             break;
17950222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
17960222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
17970222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
17980222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
17990222a807SMatthias Ringwald             }
18000222a807SMatthias Ringwald             break;
18010222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18020222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
18030222a807SMatthias Ringwald             break;
18040222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
18050222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
18060222a807SMatthias Ringwald             break;
18070222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
18080222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
18090222a807SMatthias Ringwald             break;
18100222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
18110222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
18120222a807SMatthias Ringwald             break;
18130b4debbfSMilanka Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
18140b4debbfSMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
18150b4debbfSMilanka Ringwald             hfp_connection->ag_activate_voice_recognition_value = value;
18160b4debbfSMilanka Ringwald             break;
1817db3cdbd4SMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1818db3cdbd4SMilanka Ringwald             switch(hfp_connection->parser_item_index){
1819db3cdbd4SMilanka Ringwald                 case 0:
18200b4debbfSMilanka Ringwald                     hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1821db3cdbd4SMilanka Ringwald                     break;
1822db3cdbd4SMilanka Ringwald                 case 1:
1823c78e7b73SMatthias Ringwald                     hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1824db3cdbd4SMilanka Ringwald                     break;
1825db3cdbd4SMilanka Ringwald                 case 2:
182645796ff1SMilanka Ringwald                     hfp_connection->ag_msg.text_id = 0;
1827db3cdbd4SMilanka Ringwald                     for (i = 0 ; i < 4; i++){
182845796ff1SMilanka Ringwald                         hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]);
1829db3cdbd4SMilanka Ringwald                     }
1830db3cdbd4SMilanka Ringwald                     break;
1831db3cdbd4SMilanka Ringwald                 case 3:
18320d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1833db3cdbd4SMilanka Ringwald                     break;
1834db3cdbd4SMilanka Ringwald                 case 4:
18350d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1836db3cdbd4SMilanka Ringwald                     break;
1837db3cdbd4SMilanka Ringwald                 case 5:
183851aa5d5aSMilanka Ringwald                     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
183951aa5d5aSMilanka Ringwald                     hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1;
1840db3cdbd4SMilanka Ringwald                     break;
1841db3cdbd4SMilanka Ringwald                 default:
1842db3cdbd4SMilanka Ringwald                     break;
1843db3cdbd4SMilanka Ringwald             }
1844ddbf29d2SMilanka Ringwald             hfp_connection->parser_item_index++;
1845db3cdbd4SMilanka Ringwald             break;
18463deb3ec6SMatthias Ringwald         default:
18473deb3ec6SMatthias Ringwald             break;
18483deb3ec6SMatthias Ringwald     }
18493deb3ec6SMatthias Ringwald }
18503deb3ec6SMatthias Ringwald 
18511d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
18521d9c9c90SMilanka Ringwald     UNUSED(context);
18531d9c9c90SMilanka Ringwald 
18541d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
18551d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
18561d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
18571d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
18581d9c9c90SMilanka Ringwald 
18591d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
18601d9c9c90SMilanka Ringwald 
18611d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1862aeb0f0feSMatthias Ringwald         hfp_sdp_query_context.local_role = connection->local_role;
1863aeb0f0feSMatthias Ringwald         (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6);
1864e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
18651d9c9c90SMilanka Ringwald         return;
18661d9c9c90SMilanka Ringwald     }
18671d9c9c90SMilanka Ringwald }
18681d9c9c90SMilanka Ringwald 
18694eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
18704eb3f1d8SMilanka Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1871bb7f748cSMatthias Ringwald     if (connection != NULL){
1872bb7f748cSMatthias Ringwald         // allow to call hfp_establish_service_level_connection after SLC was triggered remotely
1873bb7f748cSMatthias Ringwald         // @note this also allows to call hfp_establish_service_level_connection again before SLC is complete
1874bb7f748cSMatthias Ringwald         if (connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1875bb7f748cSMatthias Ringwald             return ERROR_CODE_SUCCESS;
1876bb7f748cSMatthias Ringwald         } else {
18774eb3f1d8SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
18783deb3ec6SMatthias Ringwald         }
1879bb7f748cSMatthias Ringwald     }
18804eb3f1d8SMilanka Ringwald 
18814eb3f1d8SMilanka Ringwald     connection = hfp_create_connection(bd_addr, local_role);
18824eb3f1d8SMilanka Ringwald     if (!connection){
18834eb3f1d8SMilanka Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
18844eb3f1d8SMilanka Ringwald     }
18854eb3f1d8SMilanka Ringwald 
18864eb3f1d8SMilanka Ringwald     connection->state = HFP_W2_SEND_SDP_QUERY;
18874eb3f1d8SMilanka Ringwald 
18884eb3f1d8SMilanka Ringwald     bd_addr_copy(connection->remote_addr, bd_addr);
18894eb3f1d8SMilanka Ringwald     connection->service_uuid = service_uuid;
18901d9c9c90SMilanka Ringwald 
189194af8b0eSMatthias Ringwald     if (local_role == HFP_ROLE_HF) {
189294af8b0eSMatthias Ringwald         // setup HF Indicators
189394af8b0eSMatthias Ringwald         uint8_t i;
189494af8b0eSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
189594af8b0eSMatthias Ringwald             connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
189694af8b0eSMatthias Ringwald             connection->generic_status_indicators[i].state = 0;
189794af8b0eSMatthias Ringwald         }
189894af8b0eSMatthias Ringwald     }
189994af8b0eSMatthias Ringwald 
1900aeb0f0feSMatthias Ringwald     hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query;
19011d9c9c90SMilanka Ringwald     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
1902aeb0f0feSMatthias Ringwald     (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
19034eb3f1d8SMilanka Ringwald     return ERROR_CODE_SUCCESS;
19043deb3ec6SMatthias Ringwald }
19053deb3ec6SMatthias Ringwald 
1906657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){
19071ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1908657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
19093deb3ec6SMatthias Ringwald 
1910657bc59fSMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
1911657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1912657bc59fSMilanka Ringwald         hfp_connection->state = HFP_IDLE;
19133deb3ec6SMatthias Ringwald         return;
19143deb3ec6SMatthias Ringwald     }
19153deb3ec6SMatthias Ringwald 
1916657bc59fSMilanka Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1917657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
19183deb3ec6SMatthias Ringwald         return;
19193deb3ec6SMatthias Ringwald     }
192052cbdd6dSMilanka Ringwald     hfp_connection->release_slc_connection = 1;
1921657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1922657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
19230c3047fbSMatthias Ringwald         return;
19240c3047fbSMatthias Ringwald     }
19250c3047fbSMatthias Ringwald 
1926657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1927657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
192852cbdd6dSMilanka Ringwald         hfp_connection->release_audio_connection = 1;
19290c3047fbSMatthias Ringwald         return;
19300c3047fbSMatthias Ringwald     }
19313deb3ec6SMatthias Ringwald }
19323deb3ec6SMatthias Ringwald 
19330b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){
19341ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1935657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
19360b4debbfSMilanka Ringwald     if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
19370b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
19381ffa0dd9SMilanka Ringwald     }
19390b4debbfSMilanka Ringwald     switch (hfp_connection->state) {
19400b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
19410b4debbfSMilanka Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
19420b4debbfSMilanka Ringwald             break;
19430b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
19440b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
19450b4debbfSMilanka Ringwald             hfp_connection->release_audio_connection = 1;
19460b4debbfSMilanka Ringwald             break;
19470b4debbfSMilanka Ringwald         default:
19480b4debbfSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
19490b4debbfSMilanka Ringwald     }
19500b4debbfSMilanka Ringwald     return ERROR_CODE_SUCCESS;
19513deb3ec6SMatthias Ringwald }
19523deb3ec6SMatthias Ringwald 
19536b3901f2SMatthias Ringwald bool hfp_sco_setup_active(void){
19546b3901f2SMatthias Ringwald     return hfp_sco_establishment_active != NULL;
19556b3901f2SMatthias Ringwald }
19566b3901f2SMatthias Ringwald 
1957eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1958447743f7SMatthias Ringwald 
1959447743f7SMatthias Ringwald     hfp_sco_establishment_active = hfp_connection;
1960447743f7SMatthias Ringwald 
1961ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1962eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1963ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1964d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1965d393ed5aSMatthias Ringwald     if (hfp_connection->negotiated_codec != HFP_CODEC_CVSD){
1966689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1967689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1968689d4323SMatthias Ringwald #else
1969689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1970689d4323SMatthias Ringwald #endif
1971d2c1d59aSMatthias Ringwald     }
1972d9290e95SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types;
1973d9290e95SMatthias Ringwald     hfp_connection->packet_types = packet_types;
1974d9290e95SMatthias Ringwald 
1975352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1976d9290e95SMatthias Ringwald     uint16_t packet_types_flipped = packet_types ^ 0x03c0;
197777d63549SMatthias Ringwald #if defined(ENABLE_SCO_OVER_PCM) && defined(ENABLE_NXP_PCM_WBS)
197877d63549SMatthias Ringwald     uint8_t  radio_coding_format = 3;
197977d63549SMatthias Ringwald     uint32_t host_bandwidth      = 0;
198077d63549SMatthias Ringwald     uint8_t  coded_data_size     = 0;
198177d63549SMatthias Ringwald     switch (hfp_connection->negotiated_codec){
198277d63549SMatthias Ringwald         case HFP_CODEC_CVSD:
198377d63549SMatthias Ringwald             radio_coding_format = 0x02;
198477d63549SMatthias Ringwald             host_bandwidth = 16000;
198577d63549SMatthias Ringwald             coded_data_size = 0x10;
198677d63549SMatthias Ringwald             break;
198777d63549SMatthias Ringwald         case HFP_CODEC_MSBC:
198877d63549SMatthias Ringwald             radio_coding_format = 0x05;
198977d63549SMatthias Ringwald             host_bandwidth = 32000;
199077d63549SMatthias Ringwald             coded_data_size = 0x08;
199177d63549SMatthias Ringwald             break;
199277d63549SMatthias Ringwald         default:
199377d63549SMatthias Ringwald             log_error("Coding format %u not supported by Controller", hfp_connection->negotiated_codec);
199477d63549SMatthias Ringwald             btstack_assert(false);
199577d63549SMatthias Ringwald             break;
199677d63549SMatthias Ringwald     }
199777d63549SMatthias Ringwald     hci_send_cmd(&hci_enhanced_setup_synchronous_connection,
199877d63549SMatthias Ringwald         // ACL Handle
199977d63549SMatthias Ringwald         hfp_connection->acl_handle,
200077d63549SMatthias Ringwald         // Transmit_Bandwidth
200177d63549SMatthias Ringwald         8000,
200277d63549SMatthias Ringwald         // Receive_Bandwidth
200377d63549SMatthias Ringwald         8000,
200477d63549SMatthias Ringwald         // Transmit_Coding_Format: radio_config_format, company, codec
200577d63549SMatthias Ringwald         radio_coding_format, 0x00, 0x00,
200677d63549SMatthias Ringwald         // Receive_Coding_Format: radio_config_format, company, codec
200777d63549SMatthias Ringwald         radio_coding_format, 0x00, 0x00,
200877d63549SMatthias Ringwald         // Transmit_Codec_Frame_Size
200977d63549SMatthias Ringwald         0x3c,
201077d63549SMatthias Ringwald         // Receive_Codec_Frame_Size
201177d63549SMatthias Ringwald         0x3c,
201277d63549SMatthias Ringwald         // Input_Bandwidth
201377d63549SMatthias Ringwald         host_bandwidth,
201477d63549SMatthias Ringwald         // Output_Bandwidth
201577d63549SMatthias Ringwald         host_bandwidth,
201677d63549SMatthias Ringwald         // Input_Coding_Format, 0x04 = Linear PCM, company, codec
201777d63549SMatthias Ringwald         0x04, 0x00, 0x00,
201877d63549SMatthias Ringwald         // Output_Coding_Format, 0x04 = Linear PCM, company, codec
201977d63549SMatthias Ringwald         0x04, 0x00, 0x00,
202077d63549SMatthias Ringwald         // Input_Coded_Data_Size
202177d63549SMatthias Ringwald         coded_data_size,
202277d63549SMatthias Ringwald         // Output_Coded_Data_Size
202377d63549SMatthias Ringwald         coded_data_size,
202477d63549SMatthias Ringwald         // Input_PCM_Data_Format, 0x02 = 2’s complement
202577d63549SMatthias Ringwald         0x02,
202677d63549SMatthias Ringwald         // Output_PCM_Data_Format, 0x02 = 2’s complement
202777d63549SMatthias Ringwald         0x02,
202877d63549SMatthias Ringwald         // Input_PCM_Sample_Payload_MSB_Position
202977d63549SMatthias Ringwald         0x00,
203077d63549SMatthias Ringwald         // Output_PCM_Sample_Payload_MSB_Position
203177d63549SMatthias Ringwald         0x00,
203277d63549SMatthias Ringwald         // Input_Data_Path - vendor specific: NXP - I2S/PCM
203377d63549SMatthias Ringwald         0x01,
203477d63549SMatthias Ringwald         // Output_Data_Path - vendor specific: NXP - I2S/PCM
203577d63549SMatthias Ringwald         0x01,
203677d63549SMatthias Ringwald         // Input_Transport_Unit_Size
203777d63549SMatthias Ringwald         0x10,
203877d63549SMatthias Ringwald         // Output_Transport_Unit_Size
203977d63549SMatthias Ringwald         0x10,
204077d63549SMatthias Ringwald         //
204177d63549SMatthias Ringwald         hfp_link_settings[setting].max_latency,
204277d63549SMatthias Ringwald         packet_types_flipped,
204377d63549SMatthias Ringwald         hfp_link_settings[setting].retransmission_effort);
204477d63549SMatthias Ringwald #else
2045eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
2046d9290e95SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types_flipped);
204777d63549SMatthias Ringwald #endif
2048ce263fc8SMatthias Ringwald }
2049d68dcce1SMatthias Ringwald 
2050c169b70dSMatthias Ringwald #ifdef ENABLE_HFP_HF_SAFE_SETTINGS
205132459254SMatthias Ringwald hfp_link_settings_t hfp_safe_settings_for_context(bool use_eSCO, uint8_t negotiated_codec, bool secure_connection_in_use){
205232459254SMatthias Ringwald     uint8_t i;
205332459254SMatthias Ringwald     hfp_link_settings_t link_setting = HFP_LINK_SETTINGS_NONE;
205432459254SMatthias Ringwald     for (i=0 ; i < (sizeof(hfp_mandatory_safe_settings) / sizeof(struct hfp_mandatory_safe_setting)) ; i++){
205532459254SMatthias Ringwald         uint16_t packet_types = hfp_link_settings[(uint8_t)(hfp_mandatory_safe_settings[i].link_setting)].packet_types;
205632459254SMatthias Ringwald         bool is_eSCO_setting = (packet_types & SCO_PACKET_TYPES_ESCO) != 0;
205732459254SMatthias Ringwald         if (is_eSCO_setting != use_eSCO) continue;
205832459254SMatthias Ringwald         if ((hfp_mandatory_safe_settings[i].codec_mask & (1 << negotiated_codec)) == 0) continue;
205932459254SMatthias Ringwald         if (hfp_mandatory_safe_settings[i].secure_connection_in_use != secure_connection_in_use) continue;
206032459254SMatthias Ringwald         link_setting = hfp_mandatory_safe_settings[i].link_setting;
206132459254SMatthias Ringwald         break;
206232459254SMatthias Ringwald     }
206332459254SMatthias Ringwald     return link_setting;
206432459254SMatthias Ringwald }
2065c169b70dSMatthias Ringwald #endif
206632459254SMatthias Ringwald 
20673ad75286SMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool use_eSCO){
20683ad75286SMatthias Ringwald 
2069447743f7SMatthias Ringwald     hfp_sco_establishment_active = hfp_connection;
2070447743f7SMatthias Ringwald 
207132459254SMatthias Ringwald     // lookup safe settings based on SCO type, SC use and Codec type
2072715d9d6aSMatthias Ringwald     uint16_t max_latency;
2073715d9d6aSMatthias Ringwald     uint16_t packet_types;
2074715d9d6aSMatthias Ringwald     uint16_t retransmission_effort;
207532459254SMatthias Ringwald 
2076c169b70dSMatthias Ringwald #ifdef ENABLE_HFP_HF_SAFE_SETTINGS
2077577ad333SMatthias Ringwald     hfp_link_settings_t link_setting = HFP_LINK_SETTINGS_NONE;
2078715d9d6aSMatthias Ringwald     // fallback for non-CVSD codec and SCO connection
2079715d9d6aSMatthias Ringwald     if ((hfp_connection->negotiated_codec != HFP_CODEC_CVSD) && (use_eSCO == false)){
2080715d9d6aSMatthias Ringwald         max_latency           = 0xffff;
2081715d9d6aSMatthias Ringwald         retransmission_effort = 0xff;
2082715d9d6aSMatthias Ringwald         packet_types          = SCO_PACKET_TYPES_HV3 | SCO_PACKET_TYPES_HV1;
2083715d9d6aSMatthias Ringwald     } else {
2084c169b70dSMatthias Ringwald         // use safe settings from HFP v1.9, table 6.10
2085c169b70dSMatthias Ringwald     bool secure_connection_in_use = gap_secure_connection(hfp_connection->acl_handle);
2086715d9d6aSMatthias Ringwald         link_setting = hfp_safe_settings_for_context(use_eSCO, hfp_connection->negotiated_codec, secure_connection_in_use);
2087715d9d6aSMatthias Ringwald         max_latency             = hfp_link_settings[(uint8_t) link_setting].max_latency;
2088715d9d6aSMatthias Ringwald         retransmission_effort   = hfp_link_settings[(uint8_t) link_setting].retransmission_effort;
2089715d9d6aSMatthias Ringwald         packet_types            = hfp_link_settings[(uint8_t) link_setting].packet_types;
2090715d9d6aSMatthias Ringwald     }
2091c169b70dSMatthias Ringwald #else
2092c169b70dSMatthias Ringwald     max_latency           = 0xffff;
2093c169b70dSMatthias Ringwald     retransmission_effort = 0xff;
2094c169b70dSMatthias Ringwald     if (use_eSCO) {
2095c169b70dSMatthias Ringwald         packet_types      = SCO_PACKET_TYPES_EV3 | SCO_PACKET_TYPES_2EV3;
2096c169b70dSMatthias Ringwald     } else {
2097c169b70dSMatthias Ringwald         packet_types      = SCO_PACKET_TYPES_HV3 | SCO_PACKET_TYPES_HV1;
2098c169b70dSMatthias Ringwald     }
2099c169b70dSMatthias Ringwald #endif
21003ad75286SMatthias Ringwald 
21013ad75286SMatthias Ringwald     // transparent data for non-CVSD connections or if codec provided by Controller
2102cb81d35dSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
2103d393ed5aSMatthias Ringwald     if (hfp_connection->negotiated_codec != HFP_CODEC_CVSD){
2104cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
2105cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
2106cb81d35dSMatthias Ringwald #else
2107cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
2108cb81d35dSMatthias Ringwald #endif
2109cb81d35dSMatthias Ringwald     }
2110cb81d35dSMatthias Ringwald 
2111cb81d35dSMatthias Ringwald     // filter packet types
211232459254SMatthias Ringwald     packet_types &= hfp_get_sco_packet_types();
211332459254SMatthias Ringwald 
2114d9290e95SMatthias Ringwald     hfp_connection->packet_types = packet_types;
2115d9290e95SMatthias Ringwald 
2116cb81d35dSMatthias Ringwald     // bits 6-9 are 'don't allow'
2117d9290e95SMatthias Ringwald     uint16_t packet_types_flipped = packet_types ^ 0x3c0;
2118cb81d35dSMatthias Ringwald 
2119577ad333SMatthias Ringwald     log_info("Sending hci_accept_connection_request: packet types 0x%04x, sco_voice_setting 0x%02x",
2120577ad333SMatthias Ringwald             packet_types, sco_voice_setting);
212132459254SMatthias Ringwald 
212277d63549SMatthias Ringwald #if defined(ENABLE_SCO_OVER_PCM) && defined(ENABLE_NXP_PCM_WBS)
212377d63549SMatthias Ringwald     uint8_t radio_coding_format = 3;
212477d63549SMatthias Ringwald     uint32_t host_bandwidth = 0;
212577d63549SMatthias Ringwald     uint8_t  coded_data_size = 0;
212677d63549SMatthias Ringwald     switch (hfp_connection->negotiated_codec){
212777d63549SMatthias Ringwald         case HFP_CODEC_CVSD:
212877d63549SMatthias Ringwald             radio_coding_format = 0x02;
212977d63549SMatthias Ringwald             host_bandwidth = 16000;
213077d63549SMatthias Ringwald             coded_data_size = 0x10;
213177d63549SMatthias Ringwald             break;
213277d63549SMatthias Ringwald         case HFP_CODEC_MSBC:
213377d63549SMatthias Ringwald             radio_coding_format = 0x05;
213477d63549SMatthias Ringwald             host_bandwidth = 32000;
213577d63549SMatthias Ringwald             coded_data_size = 0x08;
213677d63549SMatthias Ringwald             break;
213777d63549SMatthias Ringwald         default:
213877d63549SMatthias Ringwald             log_error("Coding format %u not supported by Controller", hfp_connection->negotiated_codec);
213977d63549SMatthias Ringwald             btstack_assert(false);
214077d63549SMatthias Ringwald             break;
214177d63549SMatthias Ringwald     }
214277d63549SMatthias Ringwald     hci_send_cmd(&hci_enhanced_accept_synchronous_connection,
214377d63549SMatthias Ringwald         // BD_ADDR
214477d63549SMatthias Ringwald         hfp_connection->remote_addr,
214577d63549SMatthias Ringwald         // Transmit_Bandwidth
214677d63549SMatthias Ringwald         8000,
214777d63549SMatthias Ringwald         // Receive_Bandwidth
214877d63549SMatthias Ringwald         8000,
214977d63549SMatthias Ringwald         // Transmit_Coding_Format: radio_config_format, company, codec
215077d63549SMatthias Ringwald         radio_coding_format, 0x00, 0x00,
215177d63549SMatthias Ringwald         // Receive_Coding_Format: radio_config_format, company, codec
215277d63549SMatthias Ringwald         radio_coding_format, 0x00, 0x00,
215377d63549SMatthias Ringwald         // Transmit_Codec_Frame_Size
215477d63549SMatthias Ringwald         0x3c,
215577d63549SMatthias Ringwald         // Receive_Codec_Frame_Size
215677d63549SMatthias Ringwald         0x3c,
215777d63549SMatthias Ringwald         // Input_Bandwidth
215877d63549SMatthias Ringwald         host_bandwidth,
215977d63549SMatthias Ringwald         // Output_Bandwidth
216077d63549SMatthias Ringwald         host_bandwidth,
216177d63549SMatthias Ringwald         // Input_Coding_Format, 0x04 = Linear PCM, company, codec
216277d63549SMatthias Ringwald         0x04, 0x00, 0x00,
216377d63549SMatthias Ringwald         // Output_Coding_Format, 0x04 = Linear PCM, company, codec
216477d63549SMatthias Ringwald         0x04, 0x00, 0x00,
216577d63549SMatthias Ringwald         // Input_Coded_Data_Size
216677d63549SMatthias Ringwald         coded_data_size,
216777d63549SMatthias Ringwald         // Output_Coded_Data_Size
216877d63549SMatthias Ringwald         coded_data_size,
216977d63549SMatthias Ringwald         // Input_PCM_Data_Format, 0x02 = 2’s complement
217077d63549SMatthias Ringwald         0x02,
217177d63549SMatthias Ringwald         // Output_PCM_Data_Format, 0x02 = 2’s complement
217277d63549SMatthias Ringwald         0x02,
217377d63549SMatthias Ringwald         // Input_PCM_Sample_Payload_MSB_Position
217477d63549SMatthias Ringwald         0x00,
217577d63549SMatthias Ringwald         // Output_PCM_Sample_Payload_MSB_Position
217677d63549SMatthias Ringwald         0x00,
217777d63549SMatthias Ringwald         // Input_Data_Path - vendor specific: NXP - I2S/PCM
217877d63549SMatthias Ringwald         0x01,
217977d63549SMatthias Ringwald         // Output_Data_Path - vendor specific: NXP - I2S/PCM
218077d63549SMatthias Ringwald         0x01,
218177d63549SMatthias Ringwald         // Input_Transport_Unit_Size
218277d63549SMatthias Ringwald         0x10,
218377d63549SMatthias Ringwald         // Output_Transport_Unit_Size
218477d63549SMatthias Ringwald         0x10,
218577d63549SMatthias Ringwald         //
218677d63549SMatthias Ringwald         max_latency,
218777d63549SMatthias Ringwald         packet_types_flipped,
218877d63549SMatthias Ringwald         retransmission_effort);
218977d63549SMatthias Ringwald #else
2190cb81d35dSMatthias Ringwald     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
2191d9290e95SMatthias Ringwald                  sco_voice_setting, retransmission_effort, packet_types_flipped);
219277d63549SMatthias Ringwald #endif
2193cb81d35dSMatthias Ringwald }
2194cb81d35dSMatthias Ringwald 
21953721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
21963721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
21973721a235SMatthias Ringwald     uint32_t sample_rate_hz;
21983721a235SMatthias Ringwald     uint16_t clock_rate_khz;
21993721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
22003721a235SMatthias Ringwald         clock_rate_khz = 512;
22013721a235SMatthias Ringwald         sample_rate_hz = 16000;
22023721a235SMatthias Ringwald     } else {
22033721a235SMatthias Ringwald         clock_rate_khz = 256;
22043721a235SMatthias Ringwald         sample_rate_hz = 8000;
22053721a235SMatthias Ringwald     }
22063721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
22073721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
22083721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
22093721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
22103721a235SMatthias Ringwald     uint8_t  reserved = 0;
22113721a235SMatthias Ringwald     uint16_t size = 16;
22123721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
22133721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
22143721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
22153721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
22163721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
22173721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
22183721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
22193721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
22203721a235SMatthias Ringwald }
22213721a235SMatthias Ringwald #endif
22223721a235SMatthias Ringwald 
2223689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
2224689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
2225689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
22261d2bbd54SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 512 kHz
2227d12046f0SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
2228689d4323SMatthias Ringwald }
2229689d4323SMatthias Ringwald #endif
2230689d4323SMatthias Ringwald 
22310327df7dSMatthias Ringwald void hfp_prepare_for_sco(hfp_connection_t * hfp_connection){
22323548b7cbSDirk Helbig     UNUSED(hfp_connection);
22330327df7dSMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
22340327df7dSMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
22350327df7dSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
22360327df7dSMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
22370327df7dSMatthias Ringwald     }
22383a8950c4SMatthias Ringwald #endif
22393a8950c4SMatthias Ringwald 
22400327df7dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
224108a78038SMatthias Ringwald #ifndef HAVE_BCM_PCM_NBS_16KHZ
22420327df7dSMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
224308a78038SMatthias Ringwald #endif
22440327df7dSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
22450327df7dSMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
22460327df7dSMatthias Ringwald     }
22470327df7dSMatthias Ringwald #endif
22480327df7dSMatthias Ringwald 
22490327df7dSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
22500327df7dSMatthias Ringwald     hfp_connection->rtk_send_sco_config = true;
22510327df7dSMatthias Ringwald #endif
22520327df7dSMatthias Ringwald }
22530327df7dSMatthias Ringwald 
22541b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
22551b005648SMatthias Ringwald     hfp_hf_callback = callback;
22561b005648SMatthias Ringwald }
22571b005648SMatthias Ringwald 
22581b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
22591b005648SMatthias Ringwald     hfp_ag_callback = callback;
22601b005648SMatthias Ringwald }
22611b005648SMatthias Ringwald 
2262ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
2263ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
2264ca59be51SMatthias Ringwald }
22651b005648SMatthias Ringwald 
2266ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
2267ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
2268d68dcce1SMatthias Ringwald }
2269d68dcce1SMatthias Ringwald 
227021df969bSMatthias Ringwald void hfp_set_hf_indicators(uint8_t indicators_nr, const uint8_t * indicators) {
227121df969bSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
227221df969bSMatthias Ringwald     hfp_hf_indicators = indicators;
227321df969bSMatthias Ringwald }
227421df969bSMatthias Ringwald 
2275520c92d5SMatthias Ringwald void hfp_init(void){
2276352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
2277991c26beSMatthias Ringwald }
2278991c26beSMatthias Ringwald 
227920b2edb6SMatthias Ringwald void hfp_deinit(void){
2280aeb0f0feSMatthias Ringwald     hfp_allowed_sco_packet_types = 0;
228120b2edb6SMatthias Ringwald     hfp_connections = NULL;
228220b2edb6SMatthias Ringwald     hfp_hf_callback = NULL;
228320b2edb6SMatthias Ringwald     hfp_ag_callback = NULL;
228420b2edb6SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = NULL;
228520b2edb6SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = NULL;
2286aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
2287471dea41SMatthias Ringwald     hfp_custom_commands_ag = NULL;
22887404dce3SMatthias Ringwald     hfp_custom_commands_hf = NULL;
2289aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
2290aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t));
229120b2edb6SMatthias Ringwald }
229220b2edb6SMatthias Ringwald 
2293991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
229401e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
2295991c26beSMatthias Ringwald }
2296991c26beSMatthias Ringwald 
2297991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
229801e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
2299520c92d5SMatthias Ringwald }
2300186dd3d2SMatthias Ringwald 
2301b4320e7fSMatthias Ringwald hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, uint16_t local_sco_packet_types,
2302b4320e7fSMatthias Ringwald                                           uint16_t remote_sco_packet_types, bool eSCO_S4_supported,
2303b4320e7fSMatthias Ringwald                                           uint8_t negotiated_codec) {
2304e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
2305e453c1d9SMatthias Ringwald     while (setting > 0){
2306e453c1d9SMatthias Ringwald         setting--;
2307b4320e7fSMatthias Ringwald         // skip S4 if not supported
2308e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
2309e453c1d9SMatthias Ringwald         // skip wrong codec
231064126f36SMatthias Ringwald         if ((hfp_link_settings[setting].codec_mask & (1 << negotiated_codec)) == 0) continue;
23111e70f50aSMatthias Ringwald         // skip disabled or not supported packet types
2312352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
2313b4320e7fSMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types & local_sco_packet_types & remote_sco_packet_types;
2314352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
231501e5b727SMatthias Ringwald 
2316e453c1d9SMatthias Ringwald         // found matching setting
2317e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
2318afac2a04SMilanka Ringwald     }
2319e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
2320afac2a04SMilanka Ringwald }
2321e453c1d9SMatthias Ringwald 
23222b7abbfbSMatthias Ringwald static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2323e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
2324b4320e7fSMatthias Ringwald     uint16_t local_sco_packet_types = hci_usable_sco_packet_types();
2325b4320e7fSMatthias Ringwald     uint16_t remote_sco_packet_types = hci_remote_sco_packet_types(hfp_connection->acl_handle);
2326b4320e7fSMatthias Ringwald     return hfp_next_link_setting(current_setting, local_sco_packet_types, remote_sco_packet_types, eSCO_S4_supported,
2327b4320e7fSMatthias Ringwald                                  negotiated_codec);
23282b7abbfbSMatthias Ringwald }
23292b7abbfbSMatthias Ringwald 
23302b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2331e453c1d9SMatthias Ringwald     // get highest possible link setting
23322b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
2333afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
2334afac2a04SMilanka Ringwald }
2335afac2a04SMilanka Ringwald 
2336186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
2337186dd3d2SMatthias Ringwald 
2338186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
2339186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
2340186dd3d2SMatthias Ringwald     // encode \n\r
2341186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
234215a27967SMatthias Ringwald     int i = 0;
2343186dd3d2SMatthias Ringwald     int pos;
234415a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
2345186dd3d2SMatthias Ringwald         switch (packet[pos]){
2346186dd3d2SMatthias Ringwald             case '\n':
2347186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2348186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
2349186dd3d2SMatthias Ringwald                 break;
2350186dd3d2SMatthias Ringwald             case '\r':
2351186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2352186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
2353186dd3d2SMatthias Ringwald                 break;
2354186dd3d2SMatthias Ringwald             default:
2355186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
2356186dd3d2SMatthias Ringwald                 break;
2357186dd3d2SMatthias Ringwald         }
2358186dd3d2SMatthias Ringwald     }
2359186dd3d2SMatthias Ringwald     printable[i] = 0;
2360186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
2361186dd3d2SMatthias Ringwald #endif
2362186dd3d2SMatthias Ringwald }
2363471dea41SMatthias Ringwald 
2364471dea41SMatthias Ringwald void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){
2365471dea41SMatthias Ringwald     btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command);
2366471dea41SMatthias Ringwald }
236725ffee39SMatthias Ringwald 
23687404dce3SMatthias Ringwald void hfp_register_custom_hf_command(hfp_custom_at_command_t * custom_at_command){
23697404dce3SMatthias Ringwald     btstack_linked_list_add(&hfp_custom_commands_hf, (btstack_linked_item_t *) custom_at_command);
23707404dce3SMatthias Ringwald }
23717404dce3SMatthias Ringwald 
237225ffee39SMatthias Ringwald // HFP H2 Synchronization - might get moved into a hfp_h2.c
237325ffee39SMatthias Ringwald 
237425ffee39SMatthias Ringwald // find position of h2 sync header, returns -1 if not found, or h2 sync position
237525ffee39SMatthias Ringwald static int16_t hfp_h2_sync_find(const uint8_t * frame_data, uint16_t frame_len){
237625ffee39SMatthias Ringwald     uint16_t i;
237725ffee39SMatthias Ringwald     for (i=0;i<(frame_len - 1);i++){
237825ffee39SMatthias Ringwald         // check: first byte == 1
237925ffee39SMatthias Ringwald         uint8_t h2_first_byte = frame_data[i];
238025ffee39SMatthias Ringwald         if (h2_first_byte == 0x01) {
238125ffee39SMatthias Ringwald             uint8_t h2_second_byte = frame_data[i + 1];
238225ffee39SMatthias Ringwald             // check lower nibble of second byte == 0x08
238325ffee39SMatthias Ringwald             if ((h2_second_byte & 0x0F) == 8) {
238425ffee39SMatthias Ringwald                 // check if bits 0+2 == bits 1+3
238525ffee39SMatthias Ringwald                 uint8_t hn = h2_second_byte >> 4;
238625ffee39SMatthias Ringwald                 if (((hn >> 1) & 0x05) == (hn & 0x05)) {
238725ffee39SMatthias Ringwald                     return (int16_t) i;
238825ffee39SMatthias Ringwald                 }
238925ffee39SMatthias Ringwald             }
239025ffee39SMatthias Ringwald         }
239125ffee39SMatthias Ringwald     }
239225ffee39SMatthias Ringwald     return -1;
239325ffee39SMatthias Ringwald }
239425ffee39SMatthias Ringwald 
239525ffee39SMatthias Ringwald static void hfp_h2_sync_reset(hfp_h2_sync_t * hfp_h2_sync){
239625ffee39SMatthias Ringwald     hfp_h2_sync->frame_len = 0;
239725ffee39SMatthias Ringwald }
239825ffee39SMatthias Ringwald 
239925ffee39SMatthias Ringwald void hfp_h2_sync_init(hfp_h2_sync_t * hfp_h2_sync,
240025ffee39SMatthias Ringwald                       bool (*callback)(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len)){
240125ffee39SMatthias Ringwald     hfp_h2_sync->callback = callback;
240225ffee39SMatthias Ringwald     hfp_h2_sync->dropped_bytes = 0;
240325ffee39SMatthias Ringwald     hfp_h2_sync_reset(hfp_h2_sync);
240425ffee39SMatthias Ringwald }
240525ffee39SMatthias Ringwald 
240625ffee39SMatthias Ringwald static void hfp_h2_report_bad_frames(hfp_h2_sync_t *hfp_h2_sync){
240725ffee39SMatthias Ringwald     // report bad frames
240825ffee39SMatthias Ringwald     while (hfp_h2_sync->dropped_bytes >= HFP_H2_SYNC_FRAME_SIZE){
240925ffee39SMatthias Ringwald         hfp_h2_sync->dropped_bytes -= HFP_H2_SYNC_FRAME_SIZE;
241025ffee39SMatthias Ringwald         (void)(*hfp_h2_sync->callback)(true,NULL, HFP_H2_SYNC_FRAME_SIZE);
241125ffee39SMatthias Ringwald     }
241225ffee39SMatthias Ringwald }
241325ffee39SMatthias Ringwald 
241425ffee39SMatthias Ringwald static void hfp_h2_sync_drop_bytes(hfp_h2_sync_t * hfp_h2_sync, uint16_t bytes_to_drop){
241525ffee39SMatthias Ringwald     btstack_assert(bytes_to_drop <= hfp_h2_sync->frame_len);
241625ffee39SMatthias Ringwald     memmove(hfp_h2_sync->frame_data, &hfp_h2_sync->frame_data[bytes_to_drop], hfp_h2_sync->frame_len - bytes_to_drop);
241725ffee39SMatthias Ringwald     hfp_h2_sync->dropped_bytes += bytes_to_drop;
241825ffee39SMatthias Ringwald     hfp_h2_sync->frame_len     -= bytes_to_drop;
241925ffee39SMatthias Ringwald     hfp_h2_report_bad_frames(hfp_h2_sync);
242025ffee39SMatthias Ringwald }
242125ffee39SMatthias Ringwald 
242225ffee39SMatthias Ringwald void hfp_h2_sync_process(hfp_h2_sync_t *hfp_h2_sync, bool bad_frame, const uint8_t *frame_data, uint16_t frame_len) {
242325ffee39SMatthias Ringwald 
242425ffee39SMatthias Ringwald     if (bad_frame){
242525ffee39SMatthias Ringwald         // drop all data
242625ffee39SMatthias Ringwald         hfp_h2_sync->dropped_bytes += hfp_h2_sync->frame_len;
242725ffee39SMatthias Ringwald         hfp_h2_sync->frame_len = 0;
242825ffee39SMatthias Ringwald         // all new data is bad, too
242925ffee39SMatthias Ringwald         hfp_h2_sync->dropped_bytes += frame_len;
243025ffee39SMatthias Ringwald         // report frames
243125ffee39SMatthias Ringwald         hfp_h2_report_bad_frames(hfp_h2_sync);
243225ffee39SMatthias Ringwald         return;
243325ffee39SMatthias Ringwald     }
243425ffee39SMatthias Ringwald 
243525ffee39SMatthias Ringwald     while (frame_len > 0){
243625ffee39SMatthias Ringwald         // Fill hfp_h2_sync->frame_buffer
243725ffee39SMatthias Ringwald         uint16_t bytes_free_in_frame_buffer = HFP_H2_SYNC_FRAME_SIZE - hfp_h2_sync->frame_len;
243825ffee39SMatthias Ringwald         uint16_t bytes_to_append = btstack_min(frame_len, bytes_free_in_frame_buffer);
243925ffee39SMatthias Ringwald         memcpy(&hfp_h2_sync->frame_data[hfp_h2_sync->frame_len], frame_data, bytes_to_append);
244025ffee39SMatthias Ringwald         frame_data             += bytes_to_append;
244125ffee39SMatthias Ringwald         frame_len              -= bytes_to_append;
244225ffee39SMatthias Ringwald         hfp_h2_sync->frame_len += bytes_to_append;
244325ffee39SMatthias Ringwald         // check complete frame for h2 sync
244425ffee39SMatthias Ringwald         if (hfp_h2_sync->frame_len == HFP_H2_SYNC_FRAME_SIZE){
244525ffee39SMatthias Ringwald             bool valid_frame = true;
244625ffee39SMatthias Ringwald             int16_t h2_pos = hfp_h2_sync_find(hfp_h2_sync->frame_data, hfp_h2_sync->frame_len);
244725ffee39SMatthias Ringwald             if (h2_pos < 0){
244825ffee39SMatthias Ringwald                 // no h2 sync, no valid frame, keep last byte if it is 0x01
244925ffee39SMatthias Ringwald                 if (hfp_h2_sync->frame_data[HFP_H2_SYNC_FRAME_SIZE-1] == 0x01){
245025ffee39SMatthias Ringwald                     hfp_h2_sync_drop_bytes(hfp_h2_sync, HFP_H2_SYNC_FRAME_SIZE - 1);
245125ffee39SMatthias Ringwald                 } else {
245225ffee39SMatthias Ringwald                     hfp_h2_sync_drop_bytes(hfp_h2_sync, HFP_H2_SYNC_FRAME_SIZE);
245325ffee39SMatthias Ringwald                 }
245425ffee39SMatthias Ringwald                 valid_frame = false;
245525ffee39SMatthias Ringwald             }
245625ffee39SMatthias Ringwald             else if (h2_pos > 0){
245725ffee39SMatthias Ringwald                 // drop data before h2 sync
245825ffee39SMatthias Ringwald                 hfp_h2_sync_drop_bytes(hfp_h2_sync, h2_pos);
245925ffee39SMatthias Ringwald                 valid_frame = false;
246025ffee39SMatthias Ringwald             }
246125ffee39SMatthias Ringwald             if (valid_frame) {
246225ffee39SMatthias Ringwald                 // h2 sync at pos 0 and complete frame
246325ffee39SMatthias Ringwald                 bool codec_ok = (*hfp_h2_sync->callback)(false, hfp_h2_sync->frame_data, hfp_h2_sync->frame_len);
246425ffee39SMatthias Ringwald                 if (codec_ok){
246525ffee39SMatthias Ringwald                     hfp_h2_sync_reset(hfp_h2_sync);
246625ffee39SMatthias Ringwald                 } else {
246725ffee39SMatthias Ringwald                     // drop first two bytes
246825ffee39SMatthias Ringwald                     hfp_h2_sync_drop_bytes(hfp_h2_sync, 2);
246925ffee39SMatthias Ringwald                 }
247025ffee39SMatthias Ringwald             }
247125ffee39SMatthias Ringwald         }
247225ffee39SMatthias Ringwald     }
247325ffee39SMatthias Ringwald }
2474