xref: /btstack/src/classic/hfp.c (revision f14c5daf9a65a6d247dbd12e217750915dcbfa04)
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 
8720b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types;
88aeb0f0feSMatthias Ringwald static hfp_connection_t * hfp_sco_establishment_active;
89aeb0f0feSMatthias Ringwald 
90aeb0f0feSMatthias Ringwald static hfp_sdp_query_context_t                 hfp_sdp_query_context;
91aeb0f0feSMatthias Ringwald static btstack_context_callback_registration_t hfp_sdp_query_request;
92aeb0f0feSMatthias Ringwald 
93aeb0f0feSMatthias Ringwald 
94aeb0f0feSMatthias Ringwald 
95aeb0f0feSMatthias Ringwald 
96aeb0f0feSMatthias Ringwald 
97471dea41SMatthias Ringwald // custom commands
98471dea41SMatthias Ringwald static btstack_linked_list_t hfp_custom_commands_ag;
9920b2edb6SMatthias Ringwald 
10020b2edb6SMatthias Ringwald // prototypes
10120b2edb6SMatthias 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);
10220b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
10320b2edb6SMatthias Ringwald 
1044e01e802SMatthias Ringwald 
1054e01e802SMatthias Ringwald static const struct link_settings {
1064e01e802SMatthias Ringwald     const uint16_t max_latency;
1074e01e802SMatthias Ringwald     const uint8_t  retransmission_effort;
1084e01e802SMatthias Ringwald     const uint16_t packet_types;
1094e01e802SMatthias Ringwald     const bool     eSCO;
1104e01e802SMatthias Ringwald     const uint8_t  codec;
1114e01e802SMatthias Ringwald } hfp_link_settings [] = {
1124e01e802SMatthias Ringwald         { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1134e01e802SMatthias Ringwald         { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1144e01e802SMatthias Ringwald         { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1154e01e802SMatthias Ringwald         { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1164e01e802SMatthias Ringwald         { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1174e01e802SMatthias Ringwald         { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1184e01e802SMatthias Ringwald         { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1194e01e802SMatthias Ringwald         { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1204e01e802SMatthias Ringwald };
1214e01e802SMatthias Ringwald 
1223deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
1233deb3ec6SMatthias Ringwald         "EC and/or NR function",
1243deb3ec6SMatthias Ringwald         "Three-way calling",
1253deb3ec6SMatthias Ringwald         "CLI presentation capability",
1263deb3ec6SMatthias Ringwald         "Voice recognition activation",
1273deb3ec6SMatthias Ringwald         "Remote volume control",
1283deb3ec6SMatthias Ringwald 
1293deb3ec6SMatthias Ringwald         "Enhanced call status",
1303deb3ec6SMatthias Ringwald         "Enhanced call control",
1313deb3ec6SMatthias Ringwald 
1323deb3ec6SMatthias Ringwald         "Codec negotiation",
1333deb3ec6SMatthias Ringwald 
1343deb3ec6SMatthias Ringwald         "HF Indicators",
1353deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1363deb3ec6SMatthias Ringwald         "Reserved for future definition"
1373deb3ec6SMatthias Ringwald };
1383deb3ec6SMatthias Ringwald 
1393deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
1403deb3ec6SMatthias Ringwald         "Three-way calling",
1413deb3ec6SMatthias Ringwald         "EC and/or NR function",
1423deb3ec6SMatthias Ringwald         "Voice recognition function",
1433deb3ec6SMatthias Ringwald         "In-band ring tone capability",
1443deb3ec6SMatthias Ringwald         "Attach a number to a voice tag",
1453deb3ec6SMatthias Ringwald         "Ability to reject a call",
1463deb3ec6SMatthias Ringwald         "Enhanced call status",
1473deb3ec6SMatthias Ringwald         "Enhanced call control",
1483deb3ec6SMatthias Ringwald         "Extended Error Result Codes",
1493deb3ec6SMatthias Ringwald         "Codec negotiation",
1503deb3ec6SMatthias Ringwald         "HF Indicators",
1513deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1523deb3ec6SMatthias Ringwald         "Reserved for future definition"
1533deb3ec6SMatthias Ringwald };
1543deb3ec6SMatthias Ringwald 
1550aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1560aee97efSMilanka Ringwald         "outgoing",
1570aee97efSMilanka Ringwald         "incoming"
1580aee97efSMilanka Ringwald };
1590aee97efSMilanka Ringwald 
1600aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1610aee97efSMilanka Ringwald         "active",
1620aee97efSMilanka Ringwald         "held",
1630aee97efSMilanka Ringwald         "outgoing dialing",
1640aee97efSMilanka Ringwald         "outgoing alerting",
1650aee97efSMilanka Ringwald         "incoming",
1660aee97efSMilanka Ringwald         "incoming waiting",
1670aee97efSMilanka Ringwald         "call held by response and hold"
1680aee97efSMilanka Ringwald };
1690aee97efSMilanka Ringwald 
1700aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1710aee97efSMilanka Ringwald         "voice",
1720aee97efSMilanka Ringwald         "data",
1730aee97efSMilanka Ringwald         "fax"
1740aee97efSMilanka Ringwald };
1750aee97efSMilanka Ringwald 
1760aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1770aee97efSMilanka Ringwald         "not a conference call",
1780aee97efSMilanka Ringwald         "conference call"
1790aee97efSMilanka Ringwald };
1800aee97efSMilanka Ringwald 
1810aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1820aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1830aee97efSMilanka Ringwald     return "not defined";
1840aee97efSMilanka Ringwald }
1850aee97efSMilanka Ringwald 
1860aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1870aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1880aee97efSMilanka Ringwald     return "not defined";
1890aee97efSMilanka Ringwald }
1900aee97efSMilanka Ringwald 
1910aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1920aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1930aee97efSMilanka Ringwald     return "not defined";
1940aee97efSMilanka Ringwald }
1950aee97efSMilanka Ringwald 
1960aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1970aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1980aee97efSMilanka Ringwald     return "not defined";
1990aee97efSMilanka Ringwald }
2000aee97efSMilanka Ringwald 
20125789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
20225789943SMilanka Ringwald     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
20325789943SMilanka Ringwald 
20425789943SMilanka Ringwald     if (index > HFP_MAX_NUM_INDICATORS){
20525789943SMilanka Ringwald         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
20625789943SMilanka Ringwald         return HFP_MAX_NUM_INDICATORS - 1;
20725789943SMilanka Ringwald     }
20825789943SMilanka Ringwald 
20925789943SMilanka Ringwald     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
21025789943SMilanka Ringwald     if (index > 0){
21125789943SMilanka Ringwald         index -= 1;
21225789943SMilanka Ringwald     } else {
21325789943SMilanka Ringwald         log_info("ignoring invalid indicator index 0");
21425789943SMilanka Ringwald         return 0;
21525789943SMilanka Ringwald     }
21625789943SMilanka Ringwald     return index;
21725789943SMilanka Ringwald }
21825789943SMilanka Ringwald 
21925789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
22025789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
22125789943SMilanka Ringwald         hfp_connection->parser_item_index++;
22225789943SMilanka Ringwald     } else {
22325789943SMilanka Ringwald         log_info("Ignoring additional indicator");
22425789943SMilanka Ringwald     }
22525789943SMilanka Ringwald }
22625789943SMilanka Ringwald 
22725789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
22825789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
22925789943SMilanka Ringwald         hfp_connection->parser_item_index++;
23025789943SMilanka Ringwald     } else {
23125789943SMilanka Ringwald         log_info("Ignoring additional codec index");
23225789943SMilanka Ringwald     }
23325789943SMilanka Ringwald }
23425789943SMilanka Ringwald 
235eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
236eed67a37SMilanka Ringwald     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
237eed67a37SMilanka Ringwald         hfp_connection->remote_call_services_index++;
238eed67a37SMilanka Ringwald     } else {
239eed67a37SMilanka Ringwald         log_info("Ignoring additional remote_call_services");
240eed67a37SMilanka Ringwald     }
241eed67a37SMilanka Ringwald }
24225789943SMilanka Ringwald 
2433deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
2443deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
2453deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
2463deb3ec6SMatthias Ringwald     }
2473deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
2483deb3ec6SMatthias Ringwald }
2493deb3ec6SMatthias Ringwald 
2503deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
2513deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
2523deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
2533deb3ec6SMatthias Ringwald     }
2543deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
2553deb3ec6SMatthias Ringwald }
2563deb3ec6SMatthias Ringwald 
2575ebff802SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, const char * command){
2583deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
25974386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
260ab2445a0SMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, (uint16_t) strlen(command));
2613deb3ec6SMatthias Ringwald     if (err){
26228190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
2633deb3ec6SMatthias Ringwald     }
264e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
265e43d1938SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
266e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
267e43d1938SMatthias Ringwald #endif
2683deb3ec6SMatthias Ringwald     return 1;
2693deb3ec6SMatthias Ringwald }
2703deb3ec6SMatthias Ringwald 
2716a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
272e8d1dc0dSMatthias Ringwald 
273e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
274c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
275e8d1dc0dSMatthias Ringwald 
276df327ff3SMilanka Ringwald     int i;
277df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
278e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
279e8d1dc0dSMatthias Ringwald         return 1;
280df327ff3SMilanka Ringwald     }
281df327ff3SMilanka Ringwald     return 0;
282df327ff3SMilanka Ringwald }
283df327ff3SMilanka Ringwald 
284d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
285d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
286d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
287d715cf51SMatthias Ringwald     int i;
288d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
289d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
290d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
291d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
292d715cf51SMatthias Ringwald     }
293d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2946535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
295d715cf51SMatthias Ringwald }
296d715cf51SMatthias Ringwald 
2973deb3ec6SMatthias Ringwald // UTILS
2983deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2993deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
3003deb3ec6SMatthias Ringwald }
3013deb3ec6SMatthias Ringwald 
3023deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
3033deb3ec6SMatthias Ringwald     if (value){
3043deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
3053deb3ec6SMatthias Ringwald     } else {
3063deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
3073deb3ec6SMatthias Ringwald     }
3083deb3ec6SMatthias Ringwald     return bitmap;
3093deb3ec6SMatthias Ringwald }
3103deb3ec6SMatthias Ringwald 
3113deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
312c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3133deb3ec6SMatthias Ringwald     int i;
3143deb3ec6SMatthias Ringwald     int offset = 0;
315c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3163deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
3173deb3ec6SMatthias Ringwald     }
3183deb3ec6SMatthias Ringwald     if (i<values_nr){
3193deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
3203deb3ec6SMatthias Ringwald     }
3213deb3ec6SMatthias Ringwald     return offset;
3223deb3ec6SMatthias Ringwald }
3233deb3ec6SMatthias Ringwald 
3243deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
325c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3263deb3ec6SMatthias Ringwald 
3273deb3ec6SMatthias Ringwald     int i;
3283deb3ec6SMatthias Ringwald     int offset = 0;
329c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3303deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
3313deb3ec6SMatthias Ringwald     }
3323deb3ec6SMatthias Ringwald 
3333deb3ec6SMatthias Ringwald     if (i<values_nr){
3343deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3353deb3ec6SMatthias Ringwald     }
3363deb3ec6SMatthias Ringwald     return offset;
3373deb3ec6SMatthias Ringwald }
338293fae36SMatthias Ringwald static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){
339293fae36SMatthias Ringwald     switch (local_role){
340293fae36SMatthias Ringwald         case HFP_ROLE_HF:
341293fae36SMatthias Ringwald             (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size);
342293fae36SMatthias Ringwald             break;
343293fae36SMatthias Ringwald         case HFP_ROLE_AG:
344293fae36SMatthias Ringwald             (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size);
345293fae36SMatthias Ringwald             break;
346293fae36SMatthias Ringwald         default:
347293fae36SMatthias Ringwald             btstack_unreachable();
348293fae36SMatthias Ringwald             break;
349293fae36SMatthias Ringwald     }
350293fae36SMatthias Ringwald }
3513deb3ec6SMatthias Ringwald 
352ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
353ca59be51SMatthias Ringwald     if (!hfp_connection) return;
354293fae36SMatthias Ringwald     hfp_emit_event_for_role(hfp_connection->local_role, packet, size);
355ca59be51SMatthias Ringwald }
356ca59be51SMatthias Ringwald 
357ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
3587d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
359d703d377SMatthias Ringwald     uint8_t event[5];
360a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
361a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
362a0ffb263SMatthias Ringwald     event[2] = event_subtype;
3637d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
364ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
365a0ffb263SMatthias Ringwald }
366a0ffb263SMatthias Ringwald 
367ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3687d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
369d703d377SMatthias Ringwald     uint8_t event[6];
3703deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3713deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3723deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3737d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
374d703d377SMatthias Ringwald     event[5] = value; // status 0 == OK
375ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3763deb3ec6SMatthias Ringwald }
3773deb3ec6SMatthias Ringwald 
378553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){
379553a4a56SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
380553a4a56SMilanka Ringwald 
381a95ec82fSMilanka Ringwald     uint8_t event[7];
382a95ec82fSMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
383a95ec82fSMilanka Ringwald     event[1] = sizeof(event) - 2;
384a9e632e9SMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED;
385de9e0ea7SMilanka Ringwald 
386553a4a56SMilanka Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
387a95ec82fSMilanka Ringwald     event[5] = status; // 0:success
388553a4a56SMilanka Ringwald     event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0;
389553a4a56SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3901a26de69SMilanka Ringwald }
391553a4a56SMilanka Ringwald 
392553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){
393553a4a56SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
394553a4a56SMilanka Ringwald 
395553a4a56SMilanka Ringwald     uint8_t event[6];
396553a4a56SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
397553a4a56SMilanka Ringwald     event[1] = sizeof(event) - 2;
398a9e632e9SMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED;
399553a4a56SMilanka Ringwald 
400553a4a56SMilanka Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
401553a4a56SMilanka Ringwald     event[5] = status; // 0:success
402013cc750SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
403013cc750SMilanka Ringwald }
404013cc750SMilanka Ringwald 
405de9e0ea7SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){
406de9e0ea7SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
407de9e0ea7SMilanka Ringwald 
408de9e0ea7SMilanka Ringwald     uint8_t event[6];
409de9e0ea7SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
410de9e0ea7SMilanka Ringwald     event[1] = sizeof(event) - 2;
411de9e0ea7SMilanka Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO;
412de9e0ea7SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
413de9e0ea7SMilanka Ringwald     event[5] = status;
414de9e0ea7SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
415de9e0ea7SMilanka Ringwald }
416de9e0ea7SMilanka Ringwald 
417cf75be85SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){
418cf75be85SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
419cf75be85SMilanka Ringwald 
420cf75be85SMilanka Ringwald     uint8_t event[6];
421cf75be85SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
422cf75be85SMilanka Ringwald     event[1] = sizeof(event) - 2;
423cf75be85SMilanka Ringwald     switch (hfp_connection->ag_vra_state){
424cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT:
425cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT;
426cf75be85SMilanka Ringwald             break;
427cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND:
428cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND;
429cf75be85SMilanka Ringwald             break;
430cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT:
431cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT;
432cf75be85SMilanka Ringwald             break;
433cf75be85SMilanka Ringwald         default:
434cf75be85SMilanka Ringwald             btstack_unreachable();
435cf75be85SMilanka Ringwald             break;
436cf75be85SMilanka Ringwald     }
437cf75be85SMilanka Ringwald 
438cf75be85SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
439cf75be85SMilanka Ringwald     event[5] = status;
440cf75be85SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
441cf75be85SMilanka Ringwald }
442cf75be85SMilanka Ringwald 
4437095467fSMatthias 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){
4448901a7c4SMatthias Ringwald     uint8_t event[12];
4456a7f44bdSMilanka Ringwald     int pos = 0;
4466a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
4476a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
448d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
4496a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
4506a7f44bdSMilanka Ringwald     pos += 2;
45103870092SMilanka Ringwald     event[pos++] = status; // status 0 == OK
4526a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
4536a7f44bdSMilanka Ringwald     pos += 6;
4547095467fSMatthias Ringwald     hfp_emit_event_for_role(local_role, event, sizeof(event));
455a0653c3bSMilanka Ringwald }
456a0653c3bSMilanka Ringwald 
457d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){
4587d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
459d703d377SMatthias Ringwald     uint8_t event[7];
4605441d52fSMatthias Ringwald     int pos = 0;
4615441d52fSMatthias Ringwald     event[pos++] = HCI_EVENT_HFP_META;
4625441d52fSMatthias Ringwald     event[pos++] = sizeof(event) - 2;
4635441d52fSMatthias Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED;
464d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
465d703d377SMatthias Ringwald     pos += 2;
466d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
467d703d377SMatthias Ringwald     pos += 2;
4685441d52fSMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
4695441d52fSMatthias Ringwald }
4705441d52fSMatthias Ringwald 
471*f14c5dafSMatthias Ringwald void hfp_emit_sco_connection_established(hfp_connection_t *hfp_connection, uint8_t status, uint8_t negotiated_codec,
472*f14c5dafSMatthias Ringwald                                          uint16_t packet_types, uint16_t rx_packet_length, uint16_t tx_packet_length) {
4737d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
474090c81feSMatthias Ringwald     uint8_t event[17];
475d0c4aea6SMilanka Ringwald     int pos = 0;
476d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
477d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
478d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
479d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
480d703d377SMatthias Ringwald     pos += 2;
481d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
482*f14c5dafSMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->sco_handle);
483d0c4aea6SMilanka Ringwald     pos += 2;
484*f14c5dafSMatthias Ringwald     reverse_bd_addr(hfp_connection->remote_addr,&event[pos]);
485d0c4aea6SMilanka Ringwald     pos += 6;
486d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
487090c81feSMatthias Ringwald     little_endian_store_16(event, pos, packet_types);
488ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
489d0c4aea6SMilanka Ringwald }
490d0c4aea6SMilanka Ringwald 
491ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
4927d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
4932c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
4942c50df93SBjoern Hartmann     uint8_t event[256];
4952c50df93SBjoern Hartmann #else
496c1797c7dSMatthias Ringwald     uint8_t event[40];
4972c50df93SBjoern Hartmann #endif
498ab2445a0SMatthias Ringwald     uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6);
499aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
500c10fde09SMatthias Ringwald     event[1] = 4 + string_len;
501aa4dd815SMatthias Ringwald     event[2] = event_subtype;
502d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
503c10fde09SMatthias Ringwald     memcpy((char*)&event[5], value, string_len);
504c10fde09SMatthias Ringwald     event[5 + string_len] = 0;
505c10fde09SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, 6 + string_len);
506aa4dd815SMatthias Ringwald }
507aa4dd815SMatthias Ringwald 
5080cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
5098f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
5103deb3ec6SMatthias Ringwald }
5113deb3ec6SMatthias Ringwald 
5123deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
513665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
514665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
515665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
516a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
517a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
518a0ffb263SMatthias Ringwald             return hfp_connection;
5193deb3ec6SMatthias Ringwald         }
5203deb3ec6SMatthias Ringwald     }
5213deb3ec6SMatthias Ringwald     return NULL;
5223deb3ec6SMatthias Ringwald }
5233deb3ec6SMatthias Ringwald 
524405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
525665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
526665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
527665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
528a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
5295df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
530a0ffb263SMatthias Ringwald             return hfp_connection;
5313deb3ec6SMatthias Ringwald         }
5323deb3ec6SMatthias Ringwald     }
5333deb3ec6SMatthias Ringwald     return NULL;
5343deb3ec6SMatthias Ringwald }
5353deb3ec6SMatthias Ringwald 
536405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
537665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
538665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
539665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
540a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
541c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
542a0ffb263SMatthias Ringwald             return hfp_connection;
5433deb3ec6SMatthias Ringwald         }
5443deb3ec6SMatthias Ringwald     }
5453deb3ec6SMatthias Ringwald     return NULL;
5463deb3ec6SMatthias Ringwald }
5473deb3ec6SMatthias Ringwald 
548405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
549d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
550d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
551d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
552d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
553c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
554d97d752dSMilanka Ringwald             return hfp_connection;
555d97d752dSMilanka Ringwald         }
556d97d752dSMilanka Ringwald     }
557d97d752dSMilanka Ringwald     return NULL;
558d97d752dSMilanka Ringwald }
559d97d752dSMilanka Ringwald 
560c95b5b3cSMilanka Ringwald 
561c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){
562c95b5b3cSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
563c95b5b3cSMilanka Ringwald     hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state;
564c95b5b3cSMilanka Ringwald     hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
565c95b5b3cSMilanka Ringwald 
566c95b5b3cSMilanka Ringwald     if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
567553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
568c95b5b3cSMilanka Ringwald     } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){
569553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
570c95b5b3cSMilanka Ringwald     }
571c95b5b3cSMilanka Ringwald 
572c95b5b3cSMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF;
573c95b5b3cSMilanka Ringwald     hfp_connection->activate_voice_recognition = false;
574c95b5b3cSMilanka Ringwald     hfp_connection->deactivate_voice_recognition = false;
575c95b5b3cSMilanka Ringwald     hfp_connection->enhanced_voice_recognition_enabled = false;
576c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_status = 0;
577c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
578c95b5b3cSMilanka Ringwald }
579c95b5b3cSMilanka Ringwald 
580a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
581a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
582a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
583a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
5843deb3ec6SMatthias Ringwald 
585dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
5863deb3ec6SMatthias Ringwald 
587a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
588a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
5893deb3ec6SMatthias Ringwald 
590a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
591a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
5923deb3ec6SMatthias Ringwald 
593a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
594a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
5957522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
596a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
5973deb3ec6SMatthias Ringwald 
598a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
599a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
600a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
601a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
602125560b8SMatthias Ringwald     hfp_connection->clip_have_alpha = false;
603c95b5b3cSMilanka Ringwald     hfp_reset_voice_recognition(hfp_connection);
6043deb3ec6SMatthias Ringwald }
6053deb3ec6SMatthias Ringwald 
606fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
607a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
608a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
6093deb3ec6SMatthias Ringwald 
610a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
611a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
612a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
613aa4dd815SMatthias Ringwald 
614a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
6153deb3ec6SMatthias Ringwald 
616d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
617d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
618d751f069SMatthias Ringwald 
619a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
6203deb3ec6SMatthias Ringwald 
621d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
622a0ffb263SMatthias Ringwald     return hfp_connection;
6233deb3ec6SMatthias Ringwald }
6243deb3ec6SMatthias Ringwald 
62548e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
626a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
62709a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
6283deb3ec6SMatthias Ringwald }
6293deb3ec6SMatthias Ringwald 
6304eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){
631405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
632a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
633a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
6346535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
635323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
636bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
637bdf572f4SMatthias Ringwald 
638a0ffb263SMatthias Ringwald     return hfp_connection;
6393deb3ec6SMatthias Ringwald }
6403deb3ec6SMatthias Ringwald 
641aa4dd815SMatthias Ringwald /* @param network.
642aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
643aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
644aa4dd815SMatthias Ringwald  */
6453deb3ec6SMatthias Ringwald 
6463deb3ec6SMatthias Ringwald /* @param suported_features
6473deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6483deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
6493deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
6503deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
6513deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
6523deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6533deb3ec6SMatthias Ringwald  */
6543deb3ec6SMatthias Ringwald  /* Bit position:
6553deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
6563deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6573deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
6583deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
6593deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
6603deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6613deb3ec6SMatthias Ringwald  */
6623deb3ec6SMatthias Ringwald 
6639b1c3b4dSMatthias 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){
6643deb3ec6SMatthias Ringwald     uint8_t* attribute;
6653deb3ec6SMatthias Ringwald     de_create_sequence(service);
6663deb3ec6SMatthias Ringwald 
6673deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
668235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
6699b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
6703deb3ec6SMatthias Ringwald 
6713deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
672235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
6733deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6743deb3ec6SMatthias Ringwald     {
6753deb3ec6SMatthias Ringwald         //  "UUID for Service"
6763deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
677235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
6783deb3ec6SMatthias Ringwald     }
6793deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6803deb3ec6SMatthias Ringwald 
6813deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
682235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
6833deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6843deb3ec6SMatthias Ringwald     {
6853deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
6863deb3ec6SMatthias Ringwald         {
687235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
6883deb3ec6SMatthias Ringwald         }
6893deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
6903deb3ec6SMatthias Ringwald 
6913deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
6923deb3ec6SMatthias Ringwald         {
693235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
6943deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
6953deb3ec6SMatthias Ringwald         }
6963deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
6973deb3ec6SMatthias Ringwald     }
6983deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6993deb3ec6SMatthias Ringwald 
7003deb3ec6SMatthias Ringwald 
7013deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
702235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
7033deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7043deb3ec6SMatthias Ringwald     {
705235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
7063deb3ec6SMatthias Ringwald     }
7073deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7083deb3ec6SMatthias Ringwald 
7093deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
710235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
7113deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7123deb3ec6SMatthias Ringwald     {
7133deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
7143deb3ec6SMatthias Ringwald         {
715235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
7163cb2bba2SMilanka Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8
7173deb3ec6SMatthias Ringwald         }
7183deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
7193deb3ec6SMatthias Ringwald     }
7203deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7213deb3ec6SMatthias Ringwald 
7223deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
7233deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
724ab2445a0SMatthias Ringwald     de_add_data(service,  DE_STRING, (uint16_t) strlen(name), (uint8_t *) name);
7253deb3ec6SMatthias Ringwald }
7263deb3ec6SMatthias Ringwald 
7277dceaecaSMatthias Ringwald static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){
7287dceaecaSMatthias Ringwald     // cache fields for event
7297dceaecaSMatthias Ringwald     hfp_role_t local_role = hfp_connection->local_role;
7307dceaecaSMatthias Ringwald     bd_addr_t remote_addr;
731481c0cbcSMatthias Ringwald     (void)memcpy(remote_addr, hfp_connection->remote_addr, 6);
7327dceaecaSMatthias Ringwald     // finalize connection struct
7337dceaecaSMatthias Ringwald     hfp_finalize_connection_context(hfp_connection);
7347dceaecaSMatthias Ringwald     // emit event
7357dceaecaSMatthias Ringwald     hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr);
7367dceaecaSMatthias Ringwald }
7377dceaecaSMatthias Ringwald 
7386c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
7398a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
7408a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
7418a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
7421d9c9c90SMilanka Ringwald 
743aeb0f0feSMatthias 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);
7441d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
745aeb0f0feSMatthias Ringwald         log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
74684904e95SMilanka Ringwald         return;
74784904e95SMilanka Ringwald     }
7483deb3ec6SMatthias Ringwald 
7490e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
7505611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
751a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
7523deb3ec6SMatthias Ringwald             break;
7535611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
754a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
755a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
756520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
757520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
758520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
759520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
760520c92d5SMatthias Ringwald                         break;
761520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
762520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
763520c92d5SMatthias Ringwald                         break;
764520c92d5SMatthias Ringwald                     default:
7651d9c9c90SMilanka Ringwald                         btstack_assert(false);
766520c92d5SMatthias Ringwald                         return;
767520c92d5SMatthias Ringwald                 }
7681d9c9c90SMilanka Ringwald 
769ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
7701d9c9c90SMilanka Ringwald 
7711d9c9c90SMilanka Ringwald             } else {
772cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
773cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
774cc92f22bSMatthias Ringwald                     // report service not found
775cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
776cc92f22bSMatthias Ringwald                 }
7777dceaecaSMatthias Ringwald                 hfp_handle_slc_setup_error(hfp_connection, status);
778cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
7791d9c9c90SMilanka Ringwald             }
7801d9c9c90SMilanka Ringwald 
7811d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
7821d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
783aeb0f0feSMatthias Ringwald             (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
7843deb3ec6SMatthias Ringwald             break;
7853deb3ec6SMatthias Ringwald         default:
7863deb3ec6SMatthias Ringwald             break;
7873deb3ec6SMatthias Ringwald     }
7883deb3ec6SMatthias Ringwald }
7893deb3ec6SMatthias Ringwald 
79038200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
79138200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
792eddcd308SMatthias Ringwald 
793aeb0f0feSMatthias Ringwald     if (!hfp_sco_establishment_active){
7943427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
79538200c1dSMilanka Ringwald         return 0;
796eddcd308SMatthias Ringwald     }
797eddcd308SMatthias Ringwald 
7983427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
7993427dd75SMatthias Ringwald     switch (status){
8001579b82fSMatthias Ringwald         case ERROR_CODE_SCO_AIR_MODE_REJECTED:
8011579b82fSMatthias Ringwald         case ERROR_CODE_SCO_INTERVAL_REJECTED:
8021579b82fSMatthias Ringwald         case ERROR_CODE_SCO_OFFSET_REJECTED:
8033427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
8041579b82fSMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
80569a0db57SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE:
806eddcd308SMatthias Ringwald             break;
8073427dd75SMatthias Ringwald         default:
8083427dd75SMatthias Ringwald             return 0;
8093427dd75SMatthias Ringwald     }
8103427dd75SMatthias Ringwald 
8113427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
812aeb0f0feSMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false);
8133427dd75SMatthias Ringwald 
8143427dd75SMatthias Ringwald     // handle no valid setting found
8153427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
816aeb0f0feSMatthias Ringwald         if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
8173427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
818aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
819aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->sco_for_msbc_failed = 1;
820a814da6aSMatthias Ringwald             hfp_sco_establishment_active->ag_send_common_codec = true;
821aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
8223427dd75SMatthias Ringwald         } else {
8233427dd75SMatthias Ringwald             // no other options
8243427dd75SMatthias Ringwald             return 0;
825eddcd308SMatthias Ringwald         }
8263427dd75SMatthias Ringwald     }
8273427dd75SMatthias Ringwald 
8283427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
829aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->establish_audio_connection = 1;
830aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->link_setting = next_setting;
831aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
83238200c1dSMilanka Ringwald     return 1;
833eddcd308SMatthias Ringwald }
834eddcd308SMatthias Ringwald 
835405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8365575558eSMilanka Ringwald     UNUSED(packet_type);
8378a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8385575558eSMilanka Ringwald     UNUSED(size);
8399ec2630cSMatthias Ringwald 
8403deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
84127950165SMatthias Ringwald     hci_con_handle_t handle;
842a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
843674515e8SMatthias Ringwald     uint8_t status;
8443deb3ec6SMatthias Ringwald 
84527950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
846aa4dd815SMatthias Ringwald 
8470e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
8481b005648SMatthias Ringwald 
849011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
8507522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
8517522e673SMatthias Ringwald                 case 0: //  SCO
8527522e673SMatthias Ringwald                 case 2: // eSCO
853011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
854405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
855011577abSMilanka Ringwald                     if (!hfp_connection) break;
856c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
857cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 2;
858c169df2fSMatthias Ringwald                     } else {
859cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 1;
860c169df2fSMatthias Ringwald                     }
8613721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
8623721a235SMatthias Ringwald                     hfp_cc256x_prepare_for_sco(hfp_connection);
8633721a235SMatthias Ringwald #endif
864689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
865689d4323SMatthias Ringwald                     hfp_bcm_prepare_for_sco(hfp_connection);
866689d4323SMatthias Ringwald #endif
8672b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
8682b5f92fdSMatthias Ringwald                     hfp_connection->rtk_send_sco_config = true;
8692b5f92fdSMatthias Ringwald #endif
870cb81d35dSMatthias Ringwald                     log_info("accept sco %u\n", hfp_connection->accept_sco);
871aeb0f0feSMatthias Ringwald                     hfp_sco_establishment_active = hfp_connection;
872202c8a4cSMatthias Ringwald                     break;
8737522e673SMatthias Ringwald                 default:
8747522e673SMatthias Ringwald                     break;
8757522e673SMatthias Ringwald             }
876011577abSMilanka Ringwald             break;
8773deb3ec6SMatthias Ringwald 
878eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
879eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
880aeb0f0feSMatthias Ringwald                 if (hfp_sco_establishment_active == NULL) break;
881be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
88238200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
88338200c1dSMilanka Ringwald 
884aeb0f0feSMatthias Ringwald                 hfp_connection = hfp_sco_establishment_active;
88538200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
88638200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
88738200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
888*f14c5dafSMatthias Ringwald                 hfp_emit_sco_connection_established(hfp_connection, status,
8892ef72a1cSMatthias Ringwald                                                     hfp_connection->negotiated_codec, 0, 0, 0);
8906c5b2002SMatthias Ringwald             }
891eddcd308SMatthias Ringwald             break;
892eddcd308SMatthias Ringwald 
8933deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
894aeb0f0feSMatthias Ringwald             if (hfp_sco_establishment_active == NULL) break;
895011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
896405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
897011577abSMilanka Ringwald             if (!hfp_connection) {
898011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
899011577abSMilanka Ringwald                 return;
900011577abSMilanka Ringwald             }
901ce263fc8SMatthias Ringwald 
902011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
90338200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
904cb81d35dSMatthias Ringwald                 hfp_connection->accept_sco = 0;
90538200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
90638200c1dSMilanka Ringwald 
90738200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
90838200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
909*f14c5dafSMatthias Ringwald                 hfp_emit_sco_connection_established(hfp_connection, status,
910090c81feSMatthias Ringwald                                                     hfp_connection->negotiated_codec,
9112ef72a1cSMatthias Ringwald                                                     0, 0, 0);
912aa4dd815SMatthias Ringwald                 break;
913aa4dd815SMatthias Ringwald             }
914aa4dd815SMatthias Ringwald 
915011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
916011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
917011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
918011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
919011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
920011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
921090c81feSMatthias Ringwald             uint16_t packet_types = hfp_link_settings[hfp_sco_establishment_active->link_setting].packet_types;
9223deb3ec6SMatthias Ringwald 
9233deb3ec6SMatthias Ringwald             switch (link_type){
9243deb3ec6SMatthias Ringwald                 case 0x00:
925aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
9263deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
9273deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
9283deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
9293deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
9303deb3ec6SMatthias Ringwald                     break;
9313deb3ec6SMatthias Ringwald                 case 0x02:
932aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
9333deb3ec6SMatthias Ringwald                     break;
9343deb3ec6SMatthias Ringwald                 default:
9353deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
9363deb3ec6SMatthias Ringwald                     break;
9373deb3ec6SMatthias Ringwald             }
938e0d13a19SMilanka Ringwald 
9393deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
940aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
941e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
942e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
9433deb3ec6SMatthias Ringwald 
944d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
945aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
946d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
947aa4dd815SMatthias Ringwald                 break;
948aa4dd815SMatthias Ringwald             }
949d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
950d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
951c95b5b3cSMilanka Ringwald 
952d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
9531a26de69SMilanka Ringwald 
9540b4debbfSMilanka Ringwald             switch (hfp_connection->vra_state){
9550b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
9561a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = false;
9570b4debbfSMilanka Ringwald                     break;
9580b4debbfSMilanka Ringwald                 default:
9591a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = true;
9600b4debbfSMilanka Ringwald                     break;
9610b4debbfSMilanka Ringwald             }
962*f14c5dafSMatthias Ringwald             hfp_emit_sco_connection_established(hfp_connection, status,
963090c81feSMatthias Ringwald                                                 hfp_connection->negotiated_codec,
9642ef72a1cSMatthias Ringwald                                                 packet_types, rx_packet_length, tx_packet_length);
9653deb3ec6SMatthias Ringwald             break;
9663deb3ec6SMatthias Ringwald         }
9673deb3ec6SMatthias Ringwald 
9683deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
969f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
970405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
971aa4dd815SMatthias Ringwald 
972d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
973aa4dd815SMatthias Ringwald 
9743721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
9753721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
9763721a235SMatthias Ringwald #endif
977689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
978689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
979689d4323SMatthias Ringwald #endif
98052cbdd6dSMilanka Ringwald             if (hfp_connection->sco_handle == handle){
981d751f069SMatthias Ringwald                 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
982d63c37a1SMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
98352cbdd6dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
98452cbdd6dSMilanka Ringwald                 hfp_emit_audio_connection_released(hfp_connection, handle);
98552cbdd6dSMilanka Ringwald 
9861a26de69SMilanka Ringwald                 hfp_connection->ag_audio_connection_opened_before_vra = false;
98752cbdd6dSMilanka Ringwald 
98852cbdd6dSMilanka Ringwald                 if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){
98911b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
99052cbdd6dSMilanka Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
99152cbdd6dSMilanka Ringwald                     hfp_finalize_connection_context(hfp_connection);
99219976d6bSMilanka Ringwald                     break;
99352cbdd6dSMilanka Ringwald                 } else if (hfp_connection->release_slc_connection == 1){
99452cbdd6dSMilanka Ringwald                     hfp_connection->release_slc_connection = 0;
99552cbdd6dSMilanka Ringwald                     hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
99652cbdd6dSMilanka Ringwald                     rfcomm_disconnect(hfp_connection->acl_handle);
99752cbdd6dSMilanka Ringwald                 }
99852cbdd6dSMilanka Ringwald             }
99948e6eeeeSMatthias Ringwald 
100048e6eeeeSMatthias Ringwald             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
100148e6eeeeSMatthias Ringwald                 // RFCOMM already closed -> remote power off
100248e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
100348e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
100448e6eeeeSMatthias Ringwald #else
100548e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
100648e6eeeeSMatthias Ringwald #endif
100748e6eeeeSMatthias Ringwald                 break;
100848e6eeeeSMatthias Ringwald             }
10093deb3ec6SMatthias Ringwald             break;
10103deb3ec6SMatthias Ringwald 
1011fc64f94aSMatthias Ringwald         default:
10123deb3ec6SMatthias Ringwald             break;
10133deb3ec6SMatthias Ringwald     }
10143deb3ec6SMatthias Ringwald }
10153deb3ec6SMatthias Ringwald 
10163721a235SMatthias Ringwald 
101727950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
10185575558eSMilanka Ringwald     UNUSED(packet_type);
101927950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
10205575558eSMilanka Ringwald     UNUSED(size);
102127950165SMatthias Ringwald 
102227950165SMatthias Ringwald     bd_addr_t event_addr;
102327950165SMatthias Ringwald     uint16_t rfcomm_cid;
102427950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
102527950165SMatthias Ringwald     uint8_t status;
102627950165SMatthias Ringwald 
102727950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
102827950165SMatthias Ringwald 
102927950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
103027950165SMatthias Ringwald 
103127950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
103227950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
103327950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
10344eb3f1d8SMilanka Ringwald             hfp_connection = hfp_create_connection(event_addr, local_role);
103527950165SMatthias Ringwald             if (!hfp_connection){
103627950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
103727950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
103827950165SMatthias Ringwald                 return;
103927950165SMatthias Ringwald             }
104027950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
10414960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
10424960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
104327950165SMatthias Ringwald                 return;
104427950165SMatthias Ringwald             }
104527950165SMatthias Ringwald 
104627950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
104727950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
104827950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
104927950165SMatthias Ringwald             break;
105027950165SMatthias Ringwald 
105127950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
105227950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
105327950165SMatthias Ringwald 
1054405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
10556504a03aSMilanka Ringwald             btstack_assert(hfp_connection != NULL);
105627950165SMatthias Ringwald 
10576504a03aSMilanka Ringwald             if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){
10586504a03aSMilanka Ringwald                 break;
10596504a03aSMilanka Ringwald             }
10606504a03aSMilanka Ringwald 
10616504a03aSMilanka Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
10626504a03aSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS) {
10637dceaecaSMatthias Ringwald                 hfp_handle_slc_setup_error(hfp_connection, status);
10646504a03aSMilanka Ringwald                 break;
10656504a03aSMilanka Ringwald             }
10666504a03aSMilanka Ringwald 
106727950165SMatthias Ringwald             hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
106827950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
10696504a03aSMilanka Ringwald             hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
107027950165SMatthias Ringwald             bd_addr_copy(hfp_connection->remote_addr, event_addr);
107127950165SMatthias Ringwald             hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
10726504a03aSMilanka Ringwald 
107327950165SMatthias Ringwald             rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
107427950165SMatthias Ringwald             break;
107527950165SMatthias Ringwald 
107627950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
107727950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
107827950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
107927950165SMatthias Ringwald             if (!hfp_connection) break;
108052cbdd6dSMilanka Ringwald             switch (hfp_connection->state){
108152cbdd6dSMilanka Ringwald                 case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART:
108252cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
108327950165SMatthias Ringwald                     hfp_connection->state = HFP_IDLE;
108427950165SMatthias Ringwald                     hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
108527950165SMatthias Ringwald                     break;
108652cbdd6dSMilanka Ringwald 
108752cbdd6dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
108848e6eeeeSMatthias Ringwald                     // service connection was released, this implicitly releases audio connection as well
108948e6eeeeSMatthias Ringwald                     hfp_connection->release_audio_connection = 0;
109052cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
109148e6eeeeSMatthias Ringwald                     hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
109252cbdd6dSMilanka Ringwald                     gap_disconnect(hfp_connection->sco_handle);
109352cbdd6dSMilanka Ringwald                     break;
109452cbdd6dSMilanka Ringwald 
109552cbdd6dSMilanka Ringwald                 default:
109648e6eeeeSMatthias Ringwald                     // regular case
109711b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
109848e6eeeeSMatthias Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
109948e6eeeeSMatthias Ringwald                     hfp_finalize_connection_context(hfp_connection);
110052cbdd6dSMilanka Ringwald                     break;
110148e6eeeeSMatthias Ringwald             }
110227950165SMatthias Ringwald             break;
110327950165SMatthias Ringwald 
110427950165SMatthias Ringwald         default:
110527950165SMatthias Ringwald             break;
110627950165SMatthias Ringwald     }
110727950165SMatthias Ringwald }
1108aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
110994a27792SMatthias Ringwald 
111094a27792SMatthias Ringwald typedef struct {
111194a27792SMatthias Ringwald     const char * command;
111294a27792SMatthias Ringwald     hfp_command_t command_id;
111394a27792SMatthias Ringwald } hfp_command_entry_t;
111494a27792SMatthias Ringwald 
111594a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
1116cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
1117cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
1118cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
1119cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
1120cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
112194a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
112294a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
112394a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
1124051f797dSMilanka Ringwald     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
1125cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
1126cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
112794a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
112894a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
11299ed286f3SMilanka Ringwald     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
1130cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
113194a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
113294a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1133cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
113494a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
113594a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
1136cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
1137cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
1138cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
1139cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
1140cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
114194a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
114294a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1143cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
1144cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
1145cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
1146d8656ee6SMilanka Ringwald     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
114794a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
114894a27792SMatthias Ringwald };
114994a27792SMatthias Ringwald 
115094a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
1151cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
115294a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1153b3615b9aSMilanka Ringwald     { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER },
1154cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1155cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
115694a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1157cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1158cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
115994a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1160cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
116184a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1162cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1163cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1164cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1165cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1166cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1167cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1168ce284ffbSMatthias Ringwald     { "+VGM=",  HFP_CMD_SET_MICROPHONE_GAIN },
11694c30d2d1SMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1170ce284ffbSMatthias Ringwald     { "+VGS=",  HFP_CMD_SET_SPEAKER_GAIN},
1171cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
1172cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1173cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
1174cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
117594a27792SMatthias Ringwald };
117694a27792SMatthias Ringwald 
1177471dea41SMatthias Ringwald static const hfp_custom_at_command_t * hfp_custom_command_lookup(const char * text){
1178471dea41SMatthias Ringwald     btstack_linked_list_iterator_t it;
1179471dea41SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hfp_custom_commands_ag);
1180471dea41SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)) {
1181471dea41SMatthias Ringwald         hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it);
1182471dea41SMatthias Ringwald         int match = strcmp(text, at_command->command);
1183471dea41SMatthias Ringwald         if (match == 0) {
1184471dea41SMatthias Ringwald             return at_command;
1185471dea41SMatthias Ringwald         }
1186471dea41SMatthias Ringwald     }
1187471dea41SMatthias Ringwald     return NULL;
1188471dea41SMatthias Ringwald }
1189471dea41SMatthias Ringwald 
1190aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
11913deb3ec6SMatthias Ringwald 
1192ff158b9bSMatthias Ringwald     // check for custom commands, AG only
1193ff158b9bSMatthias Ringwald     if (isHandsFree == 0) {
1194ff158b9bSMatthias Ringwald         const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(line_buffer);
1195ff158b9bSMatthias Ringwald         if (custom_at_command != NULL){
1196ff158b9bSMatthias Ringwald             return HFP_CMD_CUSTOM_MESSAGE;
1197ff158b9bSMatthias Ringwald         }
1198ff158b9bSMatthias Ringwald     }
1199ff158b9bSMatthias Ringwald 
1200cb33905eSMatthias Ringwald     // table lookup based on role
120194a27792SMatthias Ringwald     uint16_t num_entries;
120294a27792SMatthias Ringwald     hfp_command_entry_t * table;
120394a27792SMatthias Ringwald     if (isHandsFree == 0){
120494a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
120594a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
12063deb3ec6SMatthias Ringwald     } else {
120794a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
120894a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
120994a27792SMatthias Ringwald     }
1210791f0d0aSMatthias Ringwald     // binary search
1211791f0d0aSMatthias Ringwald     uint16_t left = 0;
1212791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
1213791f0d0aSMatthias Ringwald     while (left <= right){
1214791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
1215791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
121694a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
1217791f0d0aSMatthias Ringwald         if (match < 0){
1218791f0d0aSMatthias Ringwald             // search term is lower than middle element
121947005182SMilanka Ringwald             if (right == 0) break;
1220791f0d0aSMatthias Ringwald             right = middle - 1;
1221791f0d0aSMatthias Ringwald         } else if (match == 0){
122294a27792SMatthias Ringwald             return entry->command_id;
1223791f0d0aSMatthias Ringwald         } else {
1224791f0d0aSMatthias Ringwald             // search term is higher than middle element
1225791f0d0aSMatthias Ringwald             left = middle + 1;
122694a27792SMatthias Ringwald         }
122794a27792SMatthias Ringwald     }
122894a27792SMatthias Ringwald 
1229cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
12300222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1231cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1232cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1233cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
12343deb3ec6SMatthias Ringwald     }
12353deb3ec6SMatthias Ringwald 
1236cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1237cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1238aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
12393deb3ec6SMatthias Ringwald     }
12403deb3ec6SMatthias Ringwald 
1241cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1242aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1243aa4dd815SMatthias Ringwald     }
12443deb3ec6SMatthias Ringwald 
1245aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
12463deb3ec6SMatthias Ringwald }
12473deb3ec6SMatthias Ringwald 
1248a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
124951aa5d5aSMilanka Ringwald     if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return;
1250a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1251a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
12523deb3ec6SMatthias Ringwald }
1253a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1254a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
12553deb3ec6SMatthias Ringwald }
12563deb3ec6SMatthias Ringwald 
12573deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1258c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
12593deb3ec6SMatthias Ringwald }
12603deb3ec6SMatthias Ringwald 
1261471dea41SMatthias Ringwald void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1262a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
1263125560b8SMatthias Ringwald     // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand
12643deb3ec6SMatthias Ringwald }
12651d81c7feSMatthias Ringwald 
12661d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
12671d81c7feSMatthias Ringwald     switch (byte){
12681d81c7feSMatthias Ringwald         case ',':
1269f8301d46SMatthias Ringwald 		case '-':
12701d81c7feSMatthias Ringwald         case ';':
12711d81c7feSMatthias Ringwald         case '(':
12721d81c7feSMatthias Ringwald         case ')':
12731d81c7feSMatthias Ringwald         case '\n':
12741d81c7feSMatthias Ringwald         case '\r':
12753deb3ec6SMatthias Ringwald             break;
12763deb3ec6SMatthias Ringwald         default:
12771d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
12783deb3ec6SMatthias Ringwald             break;
12793deb3ec6SMatthias Ringwald     }
12803deb3ec6SMatthias Ringwald }
12811d81c7feSMatthias Ringwald 
12821d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
12831d81c7feSMatthias Ringwald     switch (byte){
12841d81c7feSMatthias Ringwald         case ',':
1285f8301d46SMatthias Ringwald 		case '-':
12861d81c7feSMatthias Ringwald         case ';':
12871d81c7feSMatthias Ringwald         case '\n':
12881d81c7feSMatthias Ringwald         case '\r':
12891d81c7feSMatthias Ringwald             return true;
1290dd533208SMatthias Ringwald         default:
12911d81c7feSMatthias Ringwald             return false;
12923deb3ec6SMatthias Ringwald     }
12933deb3ec6SMatthias Ringwald }
12943deb3ec6SMatthias Ringwald 
1295d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1296aa4dd815SMatthias Ringwald 
12971dddc4f4SMatthias Ringwald     // handle doubles quotes
12981dddc4f4SMatthias Ringwald     if (byte == '"'){
12991dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1300d6b237acSMatthias Ringwald         return true;
13011dddc4f4SMatthias Ringwald     }
13021dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
13031dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1304d6b237acSMatthias Ringwald         return true;
13051dddc4f4SMatthias Ringwald     }
13063deb3ec6SMatthias Ringwald 
13071dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1308d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
13091dddc4f4SMatthias Ringwald 
1310bfeaf344SMatthias Ringwald     bool processed = true;
1311bfeaf344SMatthias Ringwald 
1312a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1313dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1314d28b2d5dSMilanka Ringwald             switch (byte) {
1315dd533208SMatthias Ringwald                 case '\n':
1316dd533208SMatthias Ringwald                 case '\r':
1317dd533208SMatthias Ringwald                 case ';':
1318bfeaf344SMatthias Ringwald                     // ignore separator
1319bfeaf344SMatthias Ringwald                     break;
1320bfeaf344SMatthias Ringwald                 case ':':
1321bfeaf344SMatthias Ringwald                 case '?':
1322bfeaf344SMatthias Ringwald                     // store separator
1323bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1324dd533208SMatthias Ringwald                     break;
1325d28b2d5dSMilanka Ringwald                 case '=':
1326bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1327dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1328a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1329d6b237acSMatthias Ringwald                     return true;
1330d28b2d5dSMilanka Ringwald                 default:
1331bfeaf344SMatthias Ringwald                     // store if not lookahead
1332d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1333dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1334d6b237acSMatthias Ringwald                         return true;
1335dd533208SMatthias Ringwald                     }
1336bfeaf344SMatthias Ringwald                     // mark as lookahead
1337bfeaf344SMatthias Ringwald                     processed = false;
1338d7f16ff1SMatthias Ringwald                     break;
1339d7f16ff1SMatthias Ringwald             }
1340ce263fc8SMatthias Ringwald 
13411d81c7feSMatthias Ringwald             // ignore empty tokens
1342d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1343dd533208SMatthias Ringwald 
1344bfeaf344SMatthias Ringwald             // parse
1345dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1346aa4dd815SMatthias Ringwald 
134784a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
134884a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1349a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1350aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1351a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1352aa4dd815SMatthias Ringwald                         break;
1353aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1354a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1355aa4dd815SMatthias Ringwald                         break;
1356aa4dd815SMatthias Ringwald                     default:
135784a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1358aa4dd815SMatthias Ringwald                         break;
1359aa4dd815SMatthias Ringwald                 }
1360aa4dd815SMatthias Ringwald             }
13613deb3ec6SMatthias Ringwald 
1362c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1363c0c0437aSMatthias Ringwald 
1364471dea41SMatthias Ringwald             // store command id for custom command and just store rest of line
1365471dea41SMatthias Ringwald             if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){
1366471dea41SMatthias Ringwald                 const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup((const char *)hfp_connection->line_buffer);
1367471dea41SMatthias Ringwald                 hfp_connection->ag_custom_at_command_id = at_command->command_id;
1368471dea41SMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND;
1369471dea41SMatthias Ringwald                 return true;
1370471dea41SMatthias Ringwald             }
1371471dea41SMatthias Ringwald 
1372bfeaf344SMatthias Ringwald             // next state
13731d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1374dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1375dd533208SMatthias Ringwald 
1376bfeaf344SMatthias Ringwald             return processed;
1377dd533208SMatthias Ringwald 
1378bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
13791d81c7feSMatthias Ringwald             // handle empty fields
13801d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1381dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1382dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1383dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
13843d051a53SMatthias Ringwald                 return true;
13853d051a53SMatthias Ringwald             }
13863d051a53SMatthias Ringwald 
13871d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
13881d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
13891d81c7feSMatthias Ringwald 
13901d81c7feSMatthias Ringwald             // ignore empty tokens
139189e7c136SMilanka Ringwald             switch (hfp_connection->command){
139289e7c136SMilanka Ringwald                 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
139389e7c136SMilanka Ringwald                     // don't ignore empty string
139489e7c136SMilanka Ringwald                     break;
139589e7c136SMilanka Ringwald                 default:
139689e7c136SMilanka Ringwald                     if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) {
139789e7c136SMilanka Ringwald                         return true;
139889e7c136SMilanka Ringwald                     }
139989e7c136SMilanka Ringwald                     break;
140089e7c136SMilanka Ringwald             }
14013d051a53SMatthias Ringwald 
14023d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
14031d81c7feSMatthias Ringwald 
14041d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14051d81c7feSMatthias Ringwald 
14061d81c7feSMatthias Ringwald             switch (hfp_connection->command){
14071d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
14081d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
14091d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
14101d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14111d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
14121d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
14131d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14141d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14151d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
14161d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
14171d81c7feSMatthias Ringwald                     break;
14181d81c7feSMatthias Ringwald                 default:
14191d81c7feSMatthias Ringwald                     break;
14201d81c7feSMatthias Ringwald             }
14213d051a53SMatthias Ringwald             return true;
14223d051a53SMatthias Ringwald 
1423ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
14241d81c7feSMatthias Ringwald 
14251d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14261d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1427dd533208SMatthias Ringwald 
1428a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1429ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1430a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
14312308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1432ce263fc8SMatthias Ringwald                     break;
1433ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1434a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
14352308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1436ce263fc8SMatthias Ringwald                     break;
1437ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1438ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1439ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14402308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1441ce263fc8SMatthias Ringwald                     break;
1442ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14432308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1444a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1445a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1446ce263fc8SMatthias Ringwald                     break;
1447ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14482308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1449a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1450ce263fc8SMatthias Ringwald                     break;
1451ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1452a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1453a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
14542308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1455ce263fc8SMatthias Ringwald                     break;
14560222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
14570222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
14580222a807SMatthias Ringwald                     break;
1459ce263fc8SMatthias Ringwald                 default:
1460ce263fc8SMatthias Ringwald                     break;
1461ce263fc8SMatthias Ringwald             }
14621d81c7feSMatthias Ringwald 
14631d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14641d81c7feSMatthias Ringwald 
14651d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
14661d81c7feSMatthias Ringwald 
1467ba0de059SMatthias Ringwald             return true;
1468ce263fc8SMatthias Ringwald 
146974c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
14701d81c7feSMatthias Ringwald 
14711d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14721d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
14731d81c7feSMatthias Ringwald 
1474a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1475ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1476c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE,  (char *)hfp_connection->line_buffer);
1477ce263fc8SMatthias Ringwald                     break;
1478ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14792308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
148025789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
148125789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1482ce263fc8SMatthias Ringwald                     break;
1483125560b8SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1484f6e6c52dSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1485125560b8SMatthias Ringwald                     // track if last argument exists
1486125560b8SMatthias Ringwald                     hfp_connection->clip_have_alpha = hfp_connection->line_size != 0;
1487125560b8SMatthias Ringwald                     break;
1488ce263fc8SMatthias Ringwald                 default:
1489ce263fc8SMatthias Ringwald                     break;
1490ce263fc8SMatthias Ringwald             }
14911d81c7feSMatthias Ringwald 
14921d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14931d81c7feSMatthias Ringwald 
14941d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
14951d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
14961d81c7feSMatthias Ringwald             }
1497d6b237acSMatthias Ringwald             return true;
149874c7548aSMatthias Ringwald 
1499471dea41SMatthias Ringwald         case HFP_PARSER_CUSTOM_COMMAND:
1500471dea41SMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
1501471dea41SMatthias Ringwald             return true;
1502471dea41SMatthias Ringwald 
150374c7548aSMatthias Ringwald         default:
150474c7548aSMatthias Ringwald             btstack_assert(false);
150574c7548aSMatthias Ringwald             return true;
150674c7548aSMatthias Ringwald     }
1507d6b237acSMatthias Ringwald }
1508d6b237acSMatthias Ringwald 
1509d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1510d6b237acSMatthias Ringwald     bool processed = false;
1511d6b237acSMatthias Ringwald     while (!processed){
1512d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1513d6b237acSMatthias Ringwald     }
15141d81c7feSMatthias Ringwald     // reset parser state on end-of-line
15151d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
1516a34e380cSMatthias Ringwald         hfp_connection->found_equal_sign = false;
15171d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
15181d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
15191d81c7feSMatthias Ringwald     }
1520ce263fc8SMatthias Ringwald }
1521ce263fc8SMatthias Ringwald 
1522a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1523ce263fc8SMatthias Ringwald     int value;
1524a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1525667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
15262308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1527667ec068SMatthias Ringwald             int i;
1528a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1529667ec068SMatthias Ringwald                 case 0:
1530a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1531a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1532a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1533667ec068SMatthias Ringwald                             break;
1534667ec068SMatthias Ringwald                         }
1535667ec068SMatthias Ringwald                     }
1536667ec068SMatthias Ringwald                     break;
1537667ec068SMatthias Ringwald                 case 1:
1538a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1539a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1540667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1541a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1542667ec068SMatthias Ringwald                     break;
1543667ec068SMatthias Ringwald                 default:
1544667ec068SMatthias Ringwald                     break;
1545667ec068SMatthias Ringwald             }
154625789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1547667ec068SMatthias Ringwald             break;
1548667ec068SMatthias Ringwald 
1549667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1550a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1551667ec068SMatthias Ringwald                 case 0:
155228a4aea3SMilanka Ringwald                     // <alpha>: This optional field is not supported, and shall be left blank.
155328a4aea3SMilanka Ringwald                     break;
155428a4aea3SMilanka Ringwald                 case 1:
155528a4aea3SMilanka Ringwald                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1556c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1557667ec068SMatthias Ringwald                     break;
155828a4aea3SMilanka Ringwald                 case 2:
155928a4aea3SMilanka Ringwald                     /*
156028a4aea3SMilanka Ringwald                       <type> field specifies the format of the phone number provided, and can be one of the following values:
156128a4aea3SMilanka 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.
156228a4aea3SMilanka 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.
156328a4aea3SMilanka Ringwald                      - values 160-175: National number. No prefix nor escape digits included.
156428a4aea3SMilanka Ringwald                      */
15652308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1566a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1567667ec068SMatthias Ringwald                     break;
156828a4aea3SMilanka Ringwald                 case 3:
156928a4aea3SMilanka Ringwald                     // <speed>: This optional field is not supported, and shall be left blank.
157028a4aea3SMilanka Ringwald                     break;
157128a4aea3SMilanka Ringwald                 case 4:
157228a4aea3SMilanka Ringwald                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1573667ec068SMatthias Ringwald                 default:
1574667ec068SMatthias Ringwald                     break;
1575667ec068SMatthias Ringwald             }
1576eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1577a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1578667ec068SMatthias Ringwald             break;
1579667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1580a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1581667ec068SMatthias Ringwald                 case 0:
15822308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1583a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1584667ec068SMatthias Ringwald                     break;
1585667ec068SMatthias Ringwald                 case 1:
15862308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1587a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1588667ec068SMatthias Ringwald                     break;
1589667ec068SMatthias Ringwald                 case 2:
15902308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1591a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1592667ec068SMatthias Ringwald                     break;
1593667ec068SMatthias Ringwald                 case 3:
15942308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15950aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1596667ec068SMatthias Ringwald                     break;
1597667ec068SMatthias Ringwald                 case 4:
15980aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15990aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
16000aee97efSMilanka Ringwald                     break;
16010aee97efSMilanka Ringwald                 case 5:
1602c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1603667ec068SMatthias Ringwald                     break;
16040aee97efSMilanka Ringwald                 case 6:
16052308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1606a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1607667ec068SMatthias Ringwald                     break;
1608667ec068SMatthias Ringwald                 default:
1609667ec068SMatthias Ringwald                     break;
1610667ec068SMatthias Ringwald             }
1611eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1612a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1613667ec068SMatthias Ringwald             break;
1614aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
16152308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1616a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1617aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1618aa4dd815SMatthias Ringwald             break;
1619aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
16202308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1621a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1622aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1623aa4dd815SMatthias Ringwald             break;
1624aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
16252308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1626a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1627aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1628aa4dd815SMatthias Ringwald             break;
1629aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
16302308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1631a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1632aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1633aa4dd815SMatthias Ringwald             break;
16343deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
16352308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1636a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
16373deb3ec6SMatthias Ringwald             break;
16383deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
16392308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1640a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
16413deb3ec6SMatthias Ringwald             break;
16423deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
16432308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1644bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
16453deb3ec6SMatthias Ringwald             break;
16463deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1647a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1648ab2445a0SMatthias Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
164925789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1650a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
16513deb3ec6SMatthias Ringwald             break;
1652aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1653c10fde09SMatthias Ringwald             btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer);
1654a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1655a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1656aa4dd815SMatthias Ringwald             break;
1657aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1658a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
16592308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
166025789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
16613deb3ec6SMatthias Ringwald             break;
16623deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
166325789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1664a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1665a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
16662308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1667a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
16683deb3ec6SMatthias Ringwald             break;
16693deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1670a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1671a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
167297d2cfbcSMatthias 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);
167325789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1674eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
16753deb3ec6SMatthias Ringwald             break;
1676aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1677aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1678a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
16792308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
168025789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1681a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
16823deb3ec6SMatthias Ringwald             break;
1683aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
16843deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1685a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
168625789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
16873deb3ec6SMatthias Ringwald             break;
1688ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
168925789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1690a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1691ce263fc8SMatthias Ringwald             break;
16923deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
16933deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1694a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1695a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1696a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1697a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1698ce263fc8SMatthias Ringwald             }
1699a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1700ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1701a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1702ce263fc8SMatthias Ringwald             } else {
17032308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1704a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1705a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1706a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
17073deb3ec6SMatthias Ringwald             }
170825789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
17093deb3ec6SMatthias Ringwald             break;
17103deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
17113deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
171225789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1713a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
17143deb3ec6SMatthias Ringwald             break;
1715aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
17162308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1717a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1718aa4dd815SMatthias Ringwald             break;
1719aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1720a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1721a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
17223deb3ec6SMatthias Ringwald                 break;
17233deb3ec6SMatthias Ringwald             }
17243deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1725a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
17263deb3ec6SMatthias Ringwald             break;
17273deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
17283deb3ec6SMatthias Ringwald             break;
17293deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1730a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
17312308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
17323deb3ec6SMatthias Ringwald             break;
17333deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
17342308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1735a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1736a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
17373deb3ec6SMatthias Ringwald             break;
1738ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1739a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1740a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1741c10fde09SMatthias Ringwald             btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
17423deb3ec6SMatthias Ringwald             break;
17430222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
17440222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
17450222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
17460222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
17470222a807SMatthias Ringwald             }
17480222a807SMatthias Ringwald             break;
17490222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
17500222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
17510222a807SMatthias Ringwald             break;
17520222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
17530222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
17540222a807SMatthias Ringwald             break;
17550222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
17560222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
17570222a807SMatthias Ringwald             break;
17580222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
17590222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
17600222a807SMatthias Ringwald             break;
17610b4debbfSMilanka Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
17620b4debbfSMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
17630b4debbfSMilanka Ringwald             hfp_connection->ag_activate_voice_recognition_value = value;
17640b4debbfSMilanka Ringwald             break;
1765db3cdbd4SMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1766db3cdbd4SMilanka Ringwald             switch(hfp_connection->parser_item_index){
1767db3cdbd4SMilanka Ringwald                 case 0:
17680b4debbfSMilanka Ringwald                     hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1769db3cdbd4SMilanka Ringwald                     break;
1770db3cdbd4SMilanka Ringwald                 case 1:
1771c78e7b73SMatthias Ringwald                     hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1772db3cdbd4SMilanka Ringwald                     break;
1773db3cdbd4SMilanka Ringwald                 case 2:
177445796ff1SMilanka Ringwald                     hfp_connection->ag_msg.text_id = 0;
1775db3cdbd4SMilanka Ringwald                     for (i = 0 ; i < 4; i++){
177645796ff1SMilanka Ringwald                         hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]);
1777db3cdbd4SMilanka Ringwald                     }
1778db3cdbd4SMilanka Ringwald                     break;
1779db3cdbd4SMilanka Ringwald                 case 3:
17800d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1781db3cdbd4SMilanka Ringwald                     break;
1782db3cdbd4SMilanka Ringwald                 case 4:
17830d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1784db3cdbd4SMilanka Ringwald                     break;
1785db3cdbd4SMilanka Ringwald                 case 5:
178651aa5d5aSMilanka Ringwald                     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
178751aa5d5aSMilanka Ringwald                     hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1;
1788db3cdbd4SMilanka Ringwald                     break;
1789db3cdbd4SMilanka Ringwald                 default:
1790db3cdbd4SMilanka Ringwald                     break;
1791db3cdbd4SMilanka Ringwald             }
1792ddbf29d2SMilanka Ringwald             hfp_connection->parser_item_index++;
1793db3cdbd4SMilanka Ringwald             break;
17943deb3ec6SMatthias Ringwald         default:
17953deb3ec6SMatthias Ringwald             break;
17963deb3ec6SMatthias Ringwald     }
17973deb3ec6SMatthias Ringwald }
17983deb3ec6SMatthias Ringwald 
17991d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
18001d9c9c90SMilanka Ringwald     UNUSED(context);
18011d9c9c90SMilanka Ringwald 
18021d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
18031d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
18041d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
18051d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
18061d9c9c90SMilanka Ringwald 
18071d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
18081d9c9c90SMilanka Ringwald 
18091d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1810aeb0f0feSMatthias Ringwald         hfp_sdp_query_context.local_role = connection->local_role;
1811aeb0f0feSMatthias Ringwald         (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6);
1812e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
18131d9c9c90SMilanka Ringwald         return;
18141d9c9c90SMilanka Ringwald     }
18151d9c9c90SMilanka Ringwald }
18161d9c9c90SMilanka Ringwald 
18174eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
18184eb3f1d8SMilanka Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
18194eb3f1d8SMilanka Ringwald     if (connection){
18204eb3f1d8SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18213deb3ec6SMatthias Ringwald     }
18224eb3f1d8SMilanka Ringwald 
18234eb3f1d8SMilanka Ringwald     connection = hfp_create_connection(bd_addr, local_role);
18244eb3f1d8SMilanka Ringwald     if (!connection){
18254eb3f1d8SMilanka Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
18264eb3f1d8SMilanka Ringwald     }
18274eb3f1d8SMilanka Ringwald 
18284eb3f1d8SMilanka Ringwald     connection->state = HFP_W2_SEND_SDP_QUERY;
18294eb3f1d8SMilanka Ringwald 
18304eb3f1d8SMilanka Ringwald     bd_addr_copy(connection->remote_addr, bd_addr);
18314eb3f1d8SMilanka Ringwald     connection->service_uuid = service_uuid;
18321d9c9c90SMilanka Ringwald 
1833aeb0f0feSMatthias Ringwald     hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query;
18341d9c9c90SMilanka Ringwald     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
1835aeb0f0feSMatthias Ringwald     (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
18364eb3f1d8SMilanka Ringwald     return ERROR_CODE_SUCCESS;
18373deb3ec6SMatthias Ringwald }
18383deb3ec6SMatthias Ringwald 
1839657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){
18401ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1841657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
18423deb3ec6SMatthias Ringwald 
1843657bc59fSMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
1844657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1845657bc59fSMilanka Ringwald         hfp_connection->state = HFP_IDLE;
18463deb3ec6SMatthias Ringwald         return;
18473deb3ec6SMatthias Ringwald     }
18483deb3ec6SMatthias Ringwald 
1849657bc59fSMilanka Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1850657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
18513deb3ec6SMatthias Ringwald         return;
18523deb3ec6SMatthias Ringwald     }
185352cbdd6dSMilanka Ringwald     hfp_connection->release_slc_connection = 1;
1854657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1855657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
18560c3047fbSMatthias Ringwald         return;
18570c3047fbSMatthias Ringwald     }
18580c3047fbSMatthias Ringwald 
1859657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1860657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
186152cbdd6dSMilanka Ringwald         hfp_connection->release_audio_connection = 1;
18620c3047fbSMatthias Ringwald         return;
18630c3047fbSMatthias Ringwald     }
18643deb3ec6SMatthias Ringwald }
18653deb3ec6SMatthias Ringwald 
18660b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){
18671ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1868657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
18690b4debbfSMilanka Ringwald     if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
18700b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18711ffa0dd9SMilanka Ringwald     }
18720b4debbfSMilanka Ringwald     switch (hfp_connection->state) {
18730b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
18740b4debbfSMilanka Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
18750b4debbfSMilanka Ringwald             break;
18760b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
18770b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
18780b4debbfSMilanka Ringwald             hfp_connection->release_audio_connection = 1;
18790b4debbfSMilanka Ringwald             break;
18800b4debbfSMilanka Ringwald         default:
18810b4debbfSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
18820b4debbfSMilanka Ringwald     }
18830b4debbfSMilanka Ringwald     return ERROR_CODE_SUCCESS;
18843deb3ec6SMatthias Ringwald }
18853deb3ec6SMatthias Ringwald 
1886eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1887ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1888eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1889ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1890aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = hfp_connection;
1891d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1892d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1893689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1894689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1895689d4323SMatthias Ringwald #else
1896689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1897689d4323SMatthias Ringwald #endif
1898d2c1d59aSMatthias Ringwald     }
1899352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1900352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1901eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1902352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1903ce263fc8SMatthias Ringwald }
1904d68dcce1SMatthias Ringwald 
1905cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){
1906cb81d35dSMatthias Ringwald 
1907cb81d35dSMatthias Ringwald     // remote supported feature eSCO is set if link type is eSCO
1908cb81d35dSMatthias Ringwald     // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
1909cb81d35dSMatthias Ringwald     uint16_t max_latency;
1910cb81d35dSMatthias Ringwald     uint8_t  retransmission_effort;
1911cb81d35dSMatthias Ringwald     uint16_t packet_types;
1912cb81d35dSMatthias Ringwald 
1913cb81d35dSMatthias Ringwald     if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
1914cb81d35dSMatthias Ringwald         max_latency = 0x000c;
1915cb81d35dSMatthias Ringwald         retransmission_effort = 0x02;
1916cb81d35dSMatthias Ringwald         // eSCO: EV3 and 2-EV3
1917cb81d35dSMatthias Ringwald         packet_types = 0x0048;
1918cb81d35dSMatthias Ringwald     } else {
1919cb81d35dSMatthias Ringwald         max_latency = 0xffff;
1920cb81d35dSMatthias Ringwald         retransmission_effort = 0xff;
1921cb81d35dSMatthias Ringwald         // sco: HV1 and HV3
1922cb81d35dSMatthias Ringwald         packet_types = 0x005;
1923cb81d35dSMatthias Ringwald     }
1924cb81d35dSMatthias Ringwald 
1925cb81d35dSMatthias Ringwald     // mSBC only allows for transparent data
1926cb81d35dSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1927cb81d35dSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1928cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1929cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1930cb81d35dSMatthias Ringwald #else
1931cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1932cb81d35dSMatthias Ringwald #endif
1933cb81d35dSMatthias Ringwald     }
1934cb81d35dSMatthias Ringwald 
1935cb81d35dSMatthias Ringwald     // filter packet types
1936cb81d35dSMatthias Ringwald     packet_types &= hfp_get_sco_packet_types();
1937cb81d35dSMatthias Ringwald 
1938cb81d35dSMatthias Ringwald     // bits 6-9 are 'don't allow'
1939cb81d35dSMatthias Ringwald     packet_types ^= 0x3c0;
1940cb81d35dSMatthias Ringwald 
1941cb81d35dSMatthias Ringwald     log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
1942cb81d35dSMatthias Ringwald     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
1943cb81d35dSMatthias Ringwald                  sco_voice_setting, retransmission_effort, packet_types);
1944cb81d35dSMatthias Ringwald }
1945cb81d35dSMatthias Ringwald 
19463721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
19473721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
19483721a235SMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
19493721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
19503721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
19513721a235SMatthias Ringwald     }
19523721a235SMatthias Ringwald }
19533721a235SMatthias Ringwald 
19543721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
19553721a235SMatthias Ringwald     uint32_t sample_rate_hz;
19563721a235SMatthias Ringwald     uint16_t clock_rate_khz;
19573721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
19583721a235SMatthias Ringwald         clock_rate_khz = 512;
19593721a235SMatthias Ringwald         sample_rate_hz = 16000;
19603721a235SMatthias Ringwald     } else {
19613721a235SMatthias Ringwald         clock_rate_khz = 256;
19623721a235SMatthias Ringwald         sample_rate_hz = 8000;
19633721a235SMatthias Ringwald     }
19643721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
19653721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
19663721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
19673721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
19683721a235SMatthias Ringwald     uint8_t  reserved = 0;
19693721a235SMatthias Ringwald     uint16_t size = 16;
19703721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
19713721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
19723721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
19733721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
19743721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
19753721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
19763721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
19773721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
19783721a235SMatthias Ringwald }
19793721a235SMatthias Ringwald #endif
19803721a235SMatthias Ringwald 
1981689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1982689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1983689d4323SMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1984689d4323SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1985689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
1986689d4323SMatthias Ringwald     }
1987689d4323SMatthias Ringwald }
1988689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1989689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
19901d2bbd54SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 512 kHz
19911d2bbd54SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
1992689d4323SMatthias Ringwald }
1993689d4323SMatthias Ringwald #endif
1994689d4323SMatthias Ringwald 
19951b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
19961b005648SMatthias Ringwald     hfp_hf_callback = callback;
19971b005648SMatthias Ringwald }
19981b005648SMatthias Ringwald 
19991b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
20001b005648SMatthias Ringwald     hfp_ag_callback = callback;
20011b005648SMatthias Ringwald }
20021b005648SMatthias Ringwald 
2003ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
2004ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
2005ca59be51SMatthias Ringwald }
20061b005648SMatthias Ringwald 
2007ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
2008ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
2009d68dcce1SMatthias Ringwald }
2010d68dcce1SMatthias Ringwald 
2011520c92d5SMatthias Ringwald void hfp_init(void){
2012352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
2013991c26beSMatthias Ringwald }
2014991c26beSMatthias Ringwald 
201520b2edb6SMatthias Ringwald void hfp_deinit(void){
2016aeb0f0feSMatthias Ringwald     hfp_allowed_sco_packet_types = 0;
201720b2edb6SMatthias Ringwald     hfp_connections = NULL;
201820b2edb6SMatthias Ringwald     hfp_hf_callback = NULL;
201920b2edb6SMatthias Ringwald     hfp_ag_callback = NULL;
202020b2edb6SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = NULL;
202120b2edb6SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = NULL;
2022aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
2023471dea41SMatthias Ringwald     hfp_custom_commands_ag = NULL;
2024aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
2025aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t));
202620b2edb6SMatthias Ringwald }
202720b2edb6SMatthias Ringwald 
2028991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
202901e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
2030991c26beSMatthias Ringwald }
2031991c26beSMatthias Ringwald 
2032991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
203301e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
2034520c92d5SMatthias Ringwald }
2035186dd3d2SMatthias Ringwald 
2036e453c1d9SMatthias Ringwald hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, bool local_eSCO_supported, bool remote_eSCO_supported, bool eSCO_S4_supported, uint8_t negotiated_codec){
2037e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
2038e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
2039e453c1d9SMatthias Ringwald     while (setting > 0){
2040e453c1d9SMatthias Ringwald         setting--;
2041e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
2042e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
2043e453c1d9SMatthias Ringwald         // skip if S4 but not supported
2044e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
2045e453c1d9SMatthias Ringwald         // skip wrong codec
2046e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
204701e5b727SMatthias Ringwald         // skip disabled packet types
2048352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
2049352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
2050352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
205101e5b727SMatthias Ringwald 
2052e453c1d9SMatthias Ringwald         // found matching setting
2053e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
2054afac2a04SMilanka Ringwald     }
2055e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
2056afac2a04SMilanka Ringwald }
2057e453c1d9SMatthias Ringwald 
20582b7abbfbSMatthias 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){
2059e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
2060e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
2061e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
20622b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
20632b7abbfbSMatthias Ringwald }
20642b7abbfbSMatthias Ringwald 
20652b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2066e453c1d9SMatthias Ringwald     // get highest possible link setting
20672b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
2068afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
2069afac2a04SMilanka Ringwald }
2070afac2a04SMilanka Ringwald 
2071186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
2072186dd3d2SMatthias Ringwald 
2073186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
2074186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
2075186dd3d2SMatthias Ringwald     // encode \n\r
2076186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
207715a27967SMatthias Ringwald     int i = 0;
2078186dd3d2SMatthias Ringwald     int pos;
207915a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
2080186dd3d2SMatthias Ringwald         switch (packet[pos]){
2081186dd3d2SMatthias Ringwald             case '\n':
2082186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2083186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
2084186dd3d2SMatthias Ringwald                 break;
2085186dd3d2SMatthias Ringwald             case '\r':
2086186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2087186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
2088186dd3d2SMatthias Ringwald                 break;
2089186dd3d2SMatthias Ringwald             default:
2090186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
2091186dd3d2SMatthias Ringwald                 break;
2092186dd3d2SMatthias Ringwald         }
2093186dd3d2SMatthias Ringwald     }
2094186dd3d2SMatthias Ringwald     printable[i] = 0;
2095186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
2096186dd3d2SMatthias Ringwald #endif
2097186dd3d2SMatthias Ringwald }
2098471dea41SMatthias Ringwald 
2099471dea41SMatthias Ringwald void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){
2100471dea41SMatthias Ringwald     btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command);
2101471dea41SMatthias Ringwald }
2102