xref: /btstack/src/classic/hfp.c (revision 2ef72a1c4c1c4e3d6bb6c7ff84446767c3a1e72b)
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*2ef72a1cSMatthias Ringwald void hfp_emit_sco_connection_established(hfp_connection_t *hfp_connection, uint8_t status, hci_con_handle_t sco_handle,
472*2ef72a1cSMatthias Ringwald                                          bd_addr_t addr, uint8_t negotiated_codec, uint16_t packet_types,
473*2ef72a1cSMatthias Ringwald                                          uint16_t rx_packet_length, uint16_t tx_packet_length) {
4747d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
475090c81feSMatthias Ringwald     uint8_t event[17];
476d0c4aea6SMilanka Ringwald     int pos = 0;
477d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
478d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
479d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
480d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
481d703d377SMatthias Ringwald     pos += 2;
482d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
483d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
484d0c4aea6SMilanka Ringwald     pos += 2;
485d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
486d0c4aea6SMilanka Ringwald     pos += 6;
487d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
488090c81feSMatthias Ringwald     little_endian_store_16(event, pos, packet_types);
489ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
490d0c4aea6SMilanka Ringwald }
491d0c4aea6SMilanka Ringwald 
492ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
4937d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
4942c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
4952c50df93SBjoern Hartmann     uint8_t event[256];
4962c50df93SBjoern Hartmann #else
497c1797c7dSMatthias Ringwald     uint8_t event[40];
4982c50df93SBjoern Hartmann #endif
499ab2445a0SMatthias Ringwald     uint16_t string_len = btstack_min((uint16_t) strlen(value), sizeof(event) - 6);
500aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
501c10fde09SMatthias Ringwald     event[1] = 4 + string_len;
502aa4dd815SMatthias Ringwald     event[2] = event_subtype;
503d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
504c10fde09SMatthias Ringwald     memcpy((char*)&event[5], value, string_len);
505c10fde09SMatthias Ringwald     event[5 + string_len] = 0;
506c10fde09SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, 6 + string_len);
507aa4dd815SMatthias Ringwald }
508aa4dd815SMatthias Ringwald 
5090cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
5108f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
5113deb3ec6SMatthias Ringwald }
5123deb3ec6SMatthias Ringwald 
5133deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
514665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
515665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
516665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
517a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
518a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
519a0ffb263SMatthias Ringwald             return hfp_connection;
5203deb3ec6SMatthias Ringwald         }
5213deb3ec6SMatthias Ringwald     }
5223deb3ec6SMatthias Ringwald     return NULL;
5233deb3ec6SMatthias Ringwald }
5243deb3ec6SMatthias Ringwald 
525405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
526665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
527665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
528665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
529a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
5305df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
531a0ffb263SMatthias Ringwald             return hfp_connection;
5323deb3ec6SMatthias Ringwald         }
5333deb3ec6SMatthias Ringwald     }
5343deb3ec6SMatthias Ringwald     return NULL;
5353deb3ec6SMatthias Ringwald }
5363deb3ec6SMatthias Ringwald 
537405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
538665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
539665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
540665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
541a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
542c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
543a0ffb263SMatthias Ringwald             return hfp_connection;
5443deb3ec6SMatthias Ringwald         }
5453deb3ec6SMatthias Ringwald     }
5463deb3ec6SMatthias Ringwald     return NULL;
5473deb3ec6SMatthias Ringwald }
5483deb3ec6SMatthias Ringwald 
549405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
550d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
551d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
552d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
553d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
554c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
555d97d752dSMilanka Ringwald             return hfp_connection;
556d97d752dSMilanka Ringwald         }
557d97d752dSMilanka Ringwald     }
558d97d752dSMilanka Ringwald     return NULL;
559d97d752dSMilanka Ringwald }
560d97d752dSMilanka Ringwald 
561c95b5b3cSMilanka Ringwald 
562c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){
563c95b5b3cSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
564c95b5b3cSMilanka Ringwald     hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state;
565c95b5b3cSMilanka Ringwald     hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
566c95b5b3cSMilanka Ringwald 
567c95b5b3cSMilanka Ringwald     if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
568553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
569c95b5b3cSMilanka Ringwald     } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){
570553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
571c95b5b3cSMilanka Ringwald     }
572c95b5b3cSMilanka Ringwald 
573c95b5b3cSMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF;
574c95b5b3cSMilanka Ringwald     hfp_connection->activate_voice_recognition = false;
575c95b5b3cSMilanka Ringwald     hfp_connection->deactivate_voice_recognition = false;
576c95b5b3cSMilanka Ringwald     hfp_connection->enhanced_voice_recognition_enabled = false;
577c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_status = 0;
578c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
579c95b5b3cSMilanka Ringwald }
580c95b5b3cSMilanka Ringwald 
581a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
582a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
583a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
584a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
5853deb3ec6SMatthias Ringwald 
586dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
5873deb3ec6SMatthias Ringwald 
588a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
589a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
5903deb3ec6SMatthias Ringwald 
591a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
592a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
5933deb3ec6SMatthias Ringwald 
594a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
595a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
5967522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
597a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
5983deb3ec6SMatthias Ringwald 
599a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
600a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
601a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
602a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
603125560b8SMatthias Ringwald     hfp_connection->clip_have_alpha = false;
604c95b5b3cSMilanka Ringwald     hfp_reset_voice_recognition(hfp_connection);
6053deb3ec6SMatthias Ringwald }
6063deb3ec6SMatthias Ringwald 
607fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
608a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
609a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
6103deb3ec6SMatthias Ringwald 
611a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
612a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
613a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
614aa4dd815SMatthias Ringwald 
615a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
6163deb3ec6SMatthias Ringwald 
617d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
618d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
619d751f069SMatthias Ringwald 
620a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
6213deb3ec6SMatthias Ringwald 
622d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
623a0ffb263SMatthias Ringwald     return hfp_connection;
6243deb3ec6SMatthias Ringwald }
6253deb3ec6SMatthias Ringwald 
62648e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
627a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
62809a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
6293deb3ec6SMatthias Ringwald }
6303deb3ec6SMatthias Ringwald 
6314eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){
632405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
633a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
634a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
6356535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
636323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
637bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
638bdf572f4SMatthias Ringwald 
639a0ffb263SMatthias Ringwald     return hfp_connection;
6403deb3ec6SMatthias Ringwald }
6413deb3ec6SMatthias Ringwald 
642aa4dd815SMatthias Ringwald /* @param network.
643aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
644aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
645aa4dd815SMatthias Ringwald  */
6463deb3ec6SMatthias Ringwald 
6473deb3ec6SMatthias Ringwald /* @param suported_features
6483deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6493deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
6503deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
6513deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
6523deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
6533deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6543deb3ec6SMatthias Ringwald  */
6553deb3ec6SMatthias Ringwald  /* Bit position:
6563deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
6573deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6583deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
6593deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
6603deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
6613deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6623deb3ec6SMatthias Ringwald  */
6633deb3ec6SMatthias Ringwald 
6649b1c3b4dSMatthias 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){
6653deb3ec6SMatthias Ringwald     uint8_t* attribute;
6663deb3ec6SMatthias Ringwald     de_create_sequence(service);
6673deb3ec6SMatthias Ringwald 
6683deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
669235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
6709b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
6713deb3ec6SMatthias Ringwald 
6723deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
673235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
6743deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6753deb3ec6SMatthias Ringwald     {
6763deb3ec6SMatthias Ringwald         //  "UUID for Service"
6773deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
678235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
6793deb3ec6SMatthias Ringwald     }
6803deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6813deb3ec6SMatthias Ringwald 
6823deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
683235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
6843deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6853deb3ec6SMatthias Ringwald     {
6863deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
6873deb3ec6SMatthias Ringwald         {
688235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
6893deb3ec6SMatthias Ringwald         }
6903deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
6913deb3ec6SMatthias Ringwald 
6923deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
6933deb3ec6SMatthias Ringwald         {
694235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
6953deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
6963deb3ec6SMatthias Ringwald         }
6973deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
6983deb3ec6SMatthias Ringwald     }
6993deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7003deb3ec6SMatthias Ringwald 
7013deb3ec6SMatthias Ringwald 
7023deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
703235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
7043deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7053deb3ec6SMatthias Ringwald     {
706235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
7073deb3ec6SMatthias Ringwald     }
7083deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7093deb3ec6SMatthias Ringwald 
7103deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
711235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
7123deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
7133deb3ec6SMatthias Ringwald     {
7143deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
7153deb3ec6SMatthias Ringwald         {
716235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
7173cb2bba2SMilanka Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8
7183deb3ec6SMatthias Ringwald         }
7193deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
7203deb3ec6SMatthias Ringwald     }
7213deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
7223deb3ec6SMatthias Ringwald 
7233deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
7243deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
725ab2445a0SMatthias Ringwald     de_add_data(service,  DE_STRING, (uint16_t) strlen(name), (uint8_t *) name);
7263deb3ec6SMatthias Ringwald }
7273deb3ec6SMatthias Ringwald 
7287dceaecaSMatthias Ringwald static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){
7297dceaecaSMatthias Ringwald     // cache fields for event
7307dceaecaSMatthias Ringwald     hfp_role_t local_role = hfp_connection->local_role;
7317dceaecaSMatthias Ringwald     bd_addr_t remote_addr;
732481c0cbcSMatthias Ringwald     (void)memcpy(remote_addr, hfp_connection->remote_addr, 6);
7337dceaecaSMatthias Ringwald     // finalize connection struct
7347dceaecaSMatthias Ringwald     hfp_finalize_connection_context(hfp_connection);
7357dceaecaSMatthias Ringwald     // emit event
7367dceaecaSMatthias Ringwald     hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr);
7377dceaecaSMatthias Ringwald }
7387dceaecaSMatthias Ringwald 
7396c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
7408a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
7418a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
7428a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
7431d9c9c90SMilanka Ringwald 
744aeb0f0feSMatthias 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);
7451d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
746aeb0f0feSMatthias Ringwald         log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
74784904e95SMilanka Ringwald         return;
74884904e95SMilanka Ringwald     }
7493deb3ec6SMatthias Ringwald 
7500e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
7515611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
752a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
7533deb3ec6SMatthias Ringwald             break;
7545611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
755a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
756a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
757520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
758520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
759520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
760520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
761520c92d5SMatthias Ringwald                         break;
762520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
763520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
764520c92d5SMatthias Ringwald                         break;
765520c92d5SMatthias Ringwald                     default:
7661d9c9c90SMilanka Ringwald                         btstack_assert(false);
767520c92d5SMatthias Ringwald                         return;
768520c92d5SMatthias Ringwald                 }
7691d9c9c90SMilanka Ringwald 
770ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
7711d9c9c90SMilanka Ringwald 
7721d9c9c90SMilanka Ringwald             } else {
773cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
774cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
775cc92f22bSMatthias Ringwald                     // report service not found
776cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
777cc92f22bSMatthias Ringwald                 }
7787dceaecaSMatthias Ringwald                 hfp_handle_slc_setup_error(hfp_connection, status);
779cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
7801d9c9c90SMilanka Ringwald             }
7811d9c9c90SMilanka Ringwald 
7821d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
7831d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
784aeb0f0feSMatthias Ringwald             (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
7853deb3ec6SMatthias Ringwald             break;
7863deb3ec6SMatthias Ringwald         default:
7873deb3ec6SMatthias Ringwald             break;
7883deb3ec6SMatthias Ringwald     }
7893deb3ec6SMatthias Ringwald }
7903deb3ec6SMatthias Ringwald 
79138200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
79238200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
793eddcd308SMatthias Ringwald 
794aeb0f0feSMatthias Ringwald     if (!hfp_sco_establishment_active){
7953427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
79638200c1dSMilanka Ringwald         return 0;
797eddcd308SMatthias Ringwald     }
798eddcd308SMatthias Ringwald 
7993427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
8003427dd75SMatthias Ringwald     switch (status){
8011579b82fSMatthias Ringwald         case ERROR_CODE_SCO_AIR_MODE_REJECTED:
8021579b82fSMatthias Ringwald         case ERROR_CODE_SCO_INTERVAL_REJECTED:
8031579b82fSMatthias Ringwald         case ERROR_CODE_SCO_OFFSET_REJECTED:
8043427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
8051579b82fSMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
80669a0db57SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_REMOTE_FEATURE_UNSUPPORTED_LMP_FEATURE:
807eddcd308SMatthias Ringwald             break;
8083427dd75SMatthias Ringwald         default:
8093427dd75SMatthias Ringwald             return 0;
8103427dd75SMatthias Ringwald     }
8113427dd75SMatthias Ringwald 
8123427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
813aeb0f0feSMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false);
8143427dd75SMatthias Ringwald 
8153427dd75SMatthias Ringwald     // handle no valid setting found
8163427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
817aeb0f0feSMatthias Ringwald         if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
8183427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
819aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
820aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->sco_for_msbc_failed = 1;
821a814da6aSMatthias Ringwald             hfp_sco_establishment_active->ag_send_common_codec = true;
822aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
8233427dd75SMatthias Ringwald         } else {
8243427dd75SMatthias Ringwald             // no other options
8253427dd75SMatthias Ringwald             return 0;
826eddcd308SMatthias Ringwald         }
8273427dd75SMatthias Ringwald     }
8283427dd75SMatthias Ringwald 
8293427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
830aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->establish_audio_connection = 1;
831aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->link_setting = next_setting;
832aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
83338200c1dSMilanka Ringwald     return 1;
834eddcd308SMatthias Ringwald }
835eddcd308SMatthias Ringwald 
836405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8375575558eSMilanka Ringwald     UNUSED(packet_type);
8388a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8395575558eSMilanka Ringwald     UNUSED(size);
8409ec2630cSMatthias Ringwald 
8413deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
84227950165SMatthias Ringwald     hci_con_handle_t handle;
843a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
844674515e8SMatthias Ringwald     uint8_t status;
8453deb3ec6SMatthias Ringwald 
84627950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
847aa4dd815SMatthias Ringwald 
8480e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
8491b005648SMatthias Ringwald 
850011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
8517522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
8527522e673SMatthias Ringwald                 case 0: //  SCO
8537522e673SMatthias Ringwald                 case 2: // eSCO
854011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
855405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
856011577abSMilanka Ringwald                     if (!hfp_connection) break;
857c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
858cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 2;
859c169df2fSMatthias Ringwald                     } else {
860cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 1;
861c169df2fSMatthias Ringwald                     }
8623721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
8633721a235SMatthias Ringwald                     hfp_cc256x_prepare_for_sco(hfp_connection);
8643721a235SMatthias Ringwald #endif
865689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
866689d4323SMatthias Ringwald                     hfp_bcm_prepare_for_sco(hfp_connection);
867689d4323SMatthias Ringwald #endif
8682b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
8692b5f92fdSMatthias Ringwald                     hfp_connection->rtk_send_sco_config = true;
8702b5f92fdSMatthias Ringwald #endif
871cb81d35dSMatthias Ringwald                     log_info("accept sco %u\n", hfp_connection->accept_sco);
872aeb0f0feSMatthias Ringwald                     hfp_sco_establishment_active = hfp_connection;
873202c8a4cSMatthias Ringwald                     break;
8747522e673SMatthias Ringwald                 default:
8757522e673SMatthias Ringwald                     break;
8767522e673SMatthias Ringwald             }
877011577abSMilanka Ringwald             break;
8783deb3ec6SMatthias Ringwald 
879eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
880eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
881aeb0f0feSMatthias Ringwald                 if (hfp_sco_establishment_active == NULL) break;
882be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
88338200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
88438200c1dSMilanka Ringwald 
885aeb0f0feSMatthias Ringwald                 hfp_connection = hfp_sco_establishment_active;
88638200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
88738200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
88838200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
889090c81feSMatthias Ringwald                 hfp_emit_sco_connection_established(hfp_connection, status, 0, hfp_connection->remote_addr,
890*2ef72a1cSMatthias Ringwald                                                     hfp_connection->negotiated_codec, 0, 0, 0);
8916c5b2002SMatthias Ringwald             }
892eddcd308SMatthias Ringwald             break;
893eddcd308SMatthias Ringwald 
8943deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
895aeb0f0feSMatthias Ringwald             if (hfp_sco_establishment_active == NULL) break;
896011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
897405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
898011577abSMilanka Ringwald             if (!hfp_connection) {
899011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
900011577abSMilanka Ringwald                 return;
901011577abSMilanka Ringwald             }
902ce263fc8SMatthias Ringwald 
903011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
90438200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
905cb81d35dSMatthias Ringwald                 hfp_connection->accept_sco = 0;
90638200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
90738200c1dSMilanka Ringwald 
90838200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
90938200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
910090c81feSMatthias Ringwald                 hfp_emit_sco_connection_established(hfp_connection, status, 0, event_addr,
911090c81feSMatthias Ringwald                                                     hfp_connection->negotiated_codec,
912*2ef72a1cSMatthias Ringwald                                                     0, 0, 0);
913aa4dd815SMatthias Ringwald                 break;
914aa4dd815SMatthias Ringwald             }
915aa4dd815SMatthias Ringwald 
916011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
917011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
918011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
919011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
920011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
921011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
922090c81feSMatthias Ringwald             uint16_t packet_types = hfp_link_settings[hfp_sco_establishment_active->link_setting].packet_types;
9233deb3ec6SMatthias Ringwald 
9243deb3ec6SMatthias Ringwald             switch (link_type){
9253deb3ec6SMatthias Ringwald                 case 0x00:
926aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
9273deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
9283deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
9293deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
9303deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
9313deb3ec6SMatthias Ringwald                     break;
9323deb3ec6SMatthias Ringwald                 case 0x02:
933aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
9343deb3ec6SMatthias Ringwald                     break;
9353deb3ec6SMatthias Ringwald                 default:
9363deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
9373deb3ec6SMatthias Ringwald                     break;
9383deb3ec6SMatthias Ringwald             }
939e0d13a19SMilanka Ringwald 
9403deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
941aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
942e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
943e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
9443deb3ec6SMatthias Ringwald 
945d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
946aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
947d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
948aa4dd815SMatthias Ringwald                 break;
949aa4dd815SMatthias Ringwald             }
950d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
951d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
952c95b5b3cSMilanka Ringwald 
953d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
9541a26de69SMilanka Ringwald 
9550b4debbfSMilanka Ringwald             switch (hfp_connection->vra_state){
9560b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
9571a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = false;
9580b4debbfSMilanka Ringwald                     break;
9590b4debbfSMilanka Ringwald                 default:
9601a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = true;
9610b4debbfSMilanka Ringwald                     break;
9620b4debbfSMilanka Ringwald             }
963090c81feSMatthias Ringwald             hfp_emit_sco_connection_established(hfp_connection, status, sco_handle, event_addr,
964090c81feSMatthias Ringwald                                                 hfp_connection->negotiated_codec,
965*2ef72a1cSMatthias Ringwald                                                 packet_types, rx_packet_length, tx_packet_length);
9663deb3ec6SMatthias Ringwald             break;
9673deb3ec6SMatthias Ringwald         }
9683deb3ec6SMatthias Ringwald 
9693deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
970f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
971405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
972aa4dd815SMatthias Ringwald 
973d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
974aa4dd815SMatthias Ringwald 
9753721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
9763721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
9773721a235SMatthias Ringwald #endif
978689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
979689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
980689d4323SMatthias Ringwald #endif
98152cbdd6dSMilanka Ringwald             if (hfp_connection->sco_handle == handle){
982d751f069SMatthias Ringwald                 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
983d63c37a1SMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
98452cbdd6dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
98552cbdd6dSMilanka Ringwald                 hfp_emit_audio_connection_released(hfp_connection, handle);
98652cbdd6dSMilanka Ringwald 
9871a26de69SMilanka Ringwald                 hfp_connection->ag_audio_connection_opened_before_vra = false;
98852cbdd6dSMilanka Ringwald 
98952cbdd6dSMilanka Ringwald                 if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){
99011b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
99152cbdd6dSMilanka Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
99252cbdd6dSMilanka Ringwald                     hfp_finalize_connection_context(hfp_connection);
99319976d6bSMilanka Ringwald                     break;
99452cbdd6dSMilanka Ringwald                 } else if (hfp_connection->release_slc_connection == 1){
99552cbdd6dSMilanka Ringwald                     hfp_connection->release_slc_connection = 0;
99652cbdd6dSMilanka Ringwald                     hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
99752cbdd6dSMilanka Ringwald                     rfcomm_disconnect(hfp_connection->acl_handle);
99852cbdd6dSMilanka Ringwald                 }
99952cbdd6dSMilanka Ringwald             }
100048e6eeeeSMatthias Ringwald 
100148e6eeeeSMatthias Ringwald             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
100248e6eeeeSMatthias Ringwald                 // RFCOMM already closed -> remote power off
100348e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
100448e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
100548e6eeeeSMatthias Ringwald #else
100648e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
100748e6eeeeSMatthias Ringwald #endif
100848e6eeeeSMatthias Ringwald                 break;
100948e6eeeeSMatthias Ringwald             }
10103deb3ec6SMatthias Ringwald             break;
10113deb3ec6SMatthias Ringwald 
1012fc64f94aSMatthias Ringwald         default:
10133deb3ec6SMatthias Ringwald             break;
10143deb3ec6SMatthias Ringwald     }
10153deb3ec6SMatthias Ringwald }
10163deb3ec6SMatthias Ringwald 
10173721a235SMatthias Ringwald 
101827950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
10195575558eSMilanka Ringwald     UNUSED(packet_type);
102027950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
10215575558eSMilanka Ringwald     UNUSED(size);
102227950165SMatthias Ringwald 
102327950165SMatthias Ringwald     bd_addr_t event_addr;
102427950165SMatthias Ringwald     uint16_t rfcomm_cid;
102527950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
102627950165SMatthias Ringwald     uint8_t status;
102727950165SMatthias Ringwald 
102827950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
102927950165SMatthias Ringwald 
103027950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
103127950165SMatthias Ringwald 
103227950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
103327950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
103427950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
10354eb3f1d8SMilanka Ringwald             hfp_connection = hfp_create_connection(event_addr, local_role);
103627950165SMatthias Ringwald             if (!hfp_connection){
103727950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
103827950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
103927950165SMatthias Ringwald                 return;
104027950165SMatthias Ringwald             }
104127950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
10424960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
10434960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
104427950165SMatthias Ringwald                 return;
104527950165SMatthias Ringwald             }
104627950165SMatthias Ringwald 
104727950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
104827950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
104927950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
105027950165SMatthias Ringwald             break;
105127950165SMatthias Ringwald 
105227950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
105327950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
105427950165SMatthias Ringwald 
1055405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
10566504a03aSMilanka Ringwald             btstack_assert(hfp_connection != NULL);
105727950165SMatthias Ringwald 
10586504a03aSMilanka Ringwald             if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){
10596504a03aSMilanka Ringwald                 break;
10606504a03aSMilanka Ringwald             }
10616504a03aSMilanka Ringwald 
10626504a03aSMilanka Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
10636504a03aSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS) {
10647dceaecaSMatthias Ringwald                 hfp_handle_slc_setup_error(hfp_connection, status);
10656504a03aSMilanka Ringwald                 break;
10666504a03aSMilanka Ringwald             }
10676504a03aSMilanka Ringwald 
106827950165SMatthias Ringwald             hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
106927950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
10706504a03aSMilanka Ringwald             hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
107127950165SMatthias Ringwald             bd_addr_copy(hfp_connection->remote_addr, event_addr);
107227950165SMatthias Ringwald             hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
10736504a03aSMilanka Ringwald 
107427950165SMatthias Ringwald             rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
107527950165SMatthias Ringwald             break;
107627950165SMatthias Ringwald 
107727950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
107827950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
107927950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
108027950165SMatthias Ringwald             if (!hfp_connection) break;
108152cbdd6dSMilanka Ringwald             switch (hfp_connection->state){
108252cbdd6dSMilanka Ringwald                 case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART:
108352cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
108427950165SMatthias Ringwald                     hfp_connection->state = HFP_IDLE;
108527950165SMatthias Ringwald                     hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
108627950165SMatthias Ringwald                     break;
108752cbdd6dSMilanka Ringwald 
108852cbdd6dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
108948e6eeeeSMatthias Ringwald                     // service connection was released, this implicitly releases audio connection as well
109048e6eeeeSMatthias Ringwald                     hfp_connection->release_audio_connection = 0;
109152cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
109248e6eeeeSMatthias Ringwald                     hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
109352cbdd6dSMilanka Ringwald                     gap_disconnect(hfp_connection->sco_handle);
109452cbdd6dSMilanka Ringwald                     break;
109552cbdd6dSMilanka Ringwald 
109652cbdd6dSMilanka Ringwald                 default:
109748e6eeeeSMatthias Ringwald                     // regular case
109811b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
109948e6eeeeSMatthias Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
110048e6eeeeSMatthias Ringwald                     hfp_finalize_connection_context(hfp_connection);
110152cbdd6dSMilanka Ringwald                     break;
110248e6eeeeSMatthias Ringwald             }
110327950165SMatthias Ringwald             break;
110427950165SMatthias Ringwald 
110527950165SMatthias Ringwald         default:
110627950165SMatthias Ringwald             break;
110727950165SMatthias Ringwald     }
110827950165SMatthias Ringwald }
1109aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
111094a27792SMatthias Ringwald 
111194a27792SMatthias Ringwald typedef struct {
111294a27792SMatthias Ringwald     const char * command;
111394a27792SMatthias Ringwald     hfp_command_t command_id;
111494a27792SMatthias Ringwald } hfp_command_entry_t;
111594a27792SMatthias Ringwald 
111694a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
1117cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
1118cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
1119cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
1120cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
1121cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
112294a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
112394a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
112494a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
1125051f797dSMilanka Ringwald     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
1126cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
1127cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
112894a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
112994a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
11309ed286f3SMilanka Ringwald     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
1131cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
113294a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
113394a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1134cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
113594a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
113694a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
1137cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
1138cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
1139cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
1140cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
1141cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
114294a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
114394a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1144cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
1145cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
1146cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
1147d8656ee6SMilanka Ringwald     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
114894a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
114994a27792SMatthias Ringwald };
115094a27792SMatthias Ringwald 
115194a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
1152cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
115394a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1154b3615b9aSMilanka Ringwald     { "+BINP:", HFP_CMD_AG_SENT_PHONE_NUMBER },
1155cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1156cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
115794a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1158cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1159cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
116094a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1161cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
116284a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1163cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1164cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1165cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1166cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1167cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1168cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1169ce284ffbSMatthias Ringwald     { "+VGM=",  HFP_CMD_SET_MICROPHONE_GAIN },
11704c30d2d1SMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1171ce284ffbSMatthias Ringwald     { "+VGS=",  HFP_CMD_SET_SPEAKER_GAIN},
1172cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
1173cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1174cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
1175cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
117694a27792SMatthias Ringwald };
117794a27792SMatthias Ringwald 
1178471dea41SMatthias Ringwald static const hfp_custom_at_command_t * hfp_custom_command_lookup(const char * text){
1179471dea41SMatthias Ringwald     btstack_linked_list_iterator_t it;
1180471dea41SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &hfp_custom_commands_ag);
1181471dea41SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)) {
1182471dea41SMatthias Ringwald         hfp_custom_at_command_t *at_command = (hfp_custom_at_command_t *) btstack_linked_list_iterator_next(&it);
1183471dea41SMatthias Ringwald         int match = strcmp(text, at_command->command);
1184471dea41SMatthias Ringwald         if (match == 0) {
1185471dea41SMatthias Ringwald             return at_command;
1186471dea41SMatthias Ringwald         }
1187471dea41SMatthias Ringwald     }
1188471dea41SMatthias Ringwald     return NULL;
1189471dea41SMatthias Ringwald }
1190471dea41SMatthias Ringwald 
1191aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
11923deb3ec6SMatthias Ringwald 
1193ff158b9bSMatthias Ringwald     // check for custom commands, AG only
1194ff158b9bSMatthias Ringwald     if (isHandsFree == 0) {
1195ff158b9bSMatthias Ringwald         const hfp_custom_at_command_t * custom_at_command = hfp_custom_command_lookup(line_buffer);
1196ff158b9bSMatthias Ringwald         if (custom_at_command != NULL){
1197ff158b9bSMatthias Ringwald             return HFP_CMD_CUSTOM_MESSAGE;
1198ff158b9bSMatthias Ringwald         }
1199ff158b9bSMatthias Ringwald     }
1200ff158b9bSMatthias Ringwald 
1201cb33905eSMatthias Ringwald     // table lookup based on role
120294a27792SMatthias Ringwald     uint16_t num_entries;
120394a27792SMatthias Ringwald     hfp_command_entry_t * table;
120494a27792SMatthias Ringwald     if (isHandsFree == 0){
120594a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
120694a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
12073deb3ec6SMatthias Ringwald     } else {
120894a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
120994a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
121094a27792SMatthias Ringwald     }
1211791f0d0aSMatthias Ringwald     // binary search
1212791f0d0aSMatthias Ringwald     uint16_t left = 0;
1213791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
1214791f0d0aSMatthias Ringwald     while (left <= right){
1215791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
1216791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
121794a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
1218791f0d0aSMatthias Ringwald         if (match < 0){
1219791f0d0aSMatthias Ringwald             // search term is lower than middle element
122047005182SMilanka Ringwald             if (right == 0) break;
1221791f0d0aSMatthias Ringwald             right = middle - 1;
1222791f0d0aSMatthias Ringwald         } else if (match == 0){
122394a27792SMatthias Ringwald             return entry->command_id;
1224791f0d0aSMatthias Ringwald         } else {
1225791f0d0aSMatthias Ringwald             // search term is higher than middle element
1226791f0d0aSMatthias Ringwald             left = middle + 1;
122794a27792SMatthias Ringwald         }
122894a27792SMatthias Ringwald     }
122994a27792SMatthias Ringwald 
1230cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
12310222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1232cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1233cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1234cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
12353deb3ec6SMatthias Ringwald     }
12363deb3ec6SMatthias Ringwald 
1237cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1238cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1239aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
12403deb3ec6SMatthias Ringwald     }
12413deb3ec6SMatthias Ringwald 
1242cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1243aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1244aa4dd815SMatthias Ringwald     }
12453deb3ec6SMatthias Ringwald 
1246aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
12473deb3ec6SMatthias Ringwald }
12483deb3ec6SMatthias Ringwald 
1249a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
125051aa5d5aSMilanka Ringwald     if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return;
1251a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1252a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
12533deb3ec6SMatthias Ringwald }
1254a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1255a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
12563deb3ec6SMatthias Ringwald }
12573deb3ec6SMatthias Ringwald 
12583deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1259c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
12603deb3ec6SMatthias Ringwald }
12613deb3ec6SMatthias Ringwald 
1262471dea41SMatthias Ringwald void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1263a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
1264125560b8SMatthias Ringwald     // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand
12653deb3ec6SMatthias Ringwald }
12661d81c7feSMatthias Ringwald 
12671d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
12681d81c7feSMatthias Ringwald     switch (byte){
12691d81c7feSMatthias Ringwald         case ',':
1270f8301d46SMatthias Ringwald 		case '-':
12711d81c7feSMatthias Ringwald         case ';':
12721d81c7feSMatthias Ringwald         case '(':
12731d81c7feSMatthias Ringwald         case ')':
12741d81c7feSMatthias Ringwald         case '\n':
12751d81c7feSMatthias Ringwald         case '\r':
12763deb3ec6SMatthias Ringwald             break;
12773deb3ec6SMatthias Ringwald         default:
12781d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
12793deb3ec6SMatthias Ringwald             break;
12803deb3ec6SMatthias Ringwald     }
12813deb3ec6SMatthias Ringwald }
12821d81c7feSMatthias Ringwald 
12831d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
12841d81c7feSMatthias Ringwald     switch (byte){
12851d81c7feSMatthias Ringwald         case ',':
1286f8301d46SMatthias Ringwald 		case '-':
12871d81c7feSMatthias Ringwald         case ';':
12881d81c7feSMatthias Ringwald         case '\n':
12891d81c7feSMatthias Ringwald         case '\r':
12901d81c7feSMatthias Ringwald             return true;
1291dd533208SMatthias Ringwald         default:
12921d81c7feSMatthias Ringwald             return false;
12933deb3ec6SMatthias Ringwald     }
12943deb3ec6SMatthias Ringwald }
12953deb3ec6SMatthias Ringwald 
1296d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1297aa4dd815SMatthias Ringwald 
12981dddc4f4SMatthias Ringwald     // handle doubles quotes
12991dddc4f4SMatthias Ringwald     if (byte == '"'){
13001dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1301d6b237acSMatthias Ringwald         return true;
13021dddc4f4SMatthias Ringwald     }
13031dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
13041dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1305d6b237acSMatthias Ringwald         return true;
13061dddc4f4SMatthias Ringwald     }
13073deb3ec6SMatthias Ringwald 
13081dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1309d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
13101dddc4f4SMatthias Ringwald 
1311bfeaf344SMatthias Ringwald     bool processed = true;
1312bfeaf344SMatthias Ringwald 
1313a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1314dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1315d28b2d5dSMilanka Ringwald             switch (byte) {
1316dd533208SMatthias Ringwald                 case '\n':
1317dd533208SMatthias Ringwald                 case '\r':
1318dd533208SMatthias Ringwald                 case ';':
1319bfeaf344SMatthias Ringwald                     // ignore separator
1320bfeaf344SMatthias Ringwald                     break;
1321bfeaf344SMatthias Ringwald                 case ':':
1322bfeaf344SMatthias Ringwald                 case '?':
1323bfeaf344SMatthias Ringwald                     // store separator
1324bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1325dd533208SMatthias Ringwald                     break;
1326d28b2d5dSMilanka Ringwald                 case '=':
1327bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1328dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1329a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1330d6b237acSMatthias Ringwald                     return true;
1331d28b2d5dSMilanka Ringwald                 default:
1332bfeaf344SMatthias Ringwald                     // store if not lookahead
1333d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1334dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1335d6b237acSMatthias Ringwald                         return true;
1336dd533208SMatthias Ringwald                     }
1337bfeaf344SMatthias Ringwald                     // mark as lookahead
1338bfeaf344SMatthias Ringwald                     processed = false;
1339d7f16ff1SMatthias Ringwald                     break;
1340d7f16ff1SMatthias Ringwald             }
1341ce263fc8SMatthias Ringwald 
13421d81c7feSMatthias Ringwald             // ignore empty tokens
1343d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1344dd533208SMatthias Ringwald 
1345bfeaf344SMatthias Ringwald             // parse
1346dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1347aa4dd815SMatthias Ringwald 
134884a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
134984a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1350a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1351aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1352a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1353aa4dd815SMatthias Ringwald                         break;
1354aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1355a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1356aa4dd815SMatthias Ringwald                         break;
1357aa4dd815SMatthias Ringwald                     default:
135884a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1359aa4dd815SMatthias Ringwald                         break;
1360aa4dd815SMatthias Ringwald                 }
1361aa4dd815SMatthias Ringwald             }
13623deb3ec6SMatthias Ringwald 
1363c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1364c0c0437aSMatthias Ringwald 
1365471dea41SMatthias Ringwald             // store command id for custom command and just store rest of line
1366471dea41SMatthias Ringwald             if (hfp_connection->command == HFP_CMD_CUSTOM_MESSAGE){
1367471dea41SMatthias Ringwald                 const hfp_custom_at_command_t * at_command = hfp_custom_command_lookup((const char *)hfp_connection->line_buffer);
1368471dea41SMatthias Ringwald                 hfp_connection->ag_custom_at_command_id = at_command->command_id;
1369471dea41SMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CUSTOM_COMMAND;
1370471dea41SMatthias Ringwald                 return true;
1371471dea41SMatthias Ringwald             }
1372471dea41SMatthias Ringwald 
1373bfeaf344SMatthias Ringwald             // next state
13741d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1375dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1376dd533208SMatthias Ringwald 
1377bfeaf344SMatthias Ringwald             return processed;
1378dd533208SMatthias Ringwald 
1379bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
13801d81c7feSMatthias Ringwald             // handle empty fields
13811d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1382dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1383dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1384dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
13853d051a53SMatthias Ringwald                 return true;
13863d051a53SMatthias Ringwald             }
13873d051a53SMatthias Ringwald 
13881d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
13891d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
13901d81c7feSMatthias Ringwald 
13911d81c7feSMatthias Ringwald             // ignore empty tokens
139289e7c136SMilanka Ringwald             switch (hfp_connection->command){
139389e7c136SMilanka Ringwald                 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
139489e7c136SMilanka Ringwald                     // don't ignore empty string
139589e7c136SMilanka Ringwald                     break;
139689e7c136SMilanka Ringwald                 default:
139789e7c136SMilanka Ringwald                     if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) {
139889e7c136SMilanka Ringwald                         return true;
139989e7c136SMilanka Ringwald                     }
140089e7c136SMilanka Ringwald                     break;
140189e7c136SMilanka Ringwald             }
14023d051a53SMatthias Ringwald 
14033d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
14041d81c7feSMatthias Ringwald 
14051d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14061d81c7feSMatthias Ringwald 
14071d81c7feSMatthias Ringwald             switch (hfp_connection->command){
14081d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
14091d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
14101d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
14111d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14121d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
14131d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
14141d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14151d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14161d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
14171d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
14181d81c7feSMatthias Ringwald                     break;
14191d81c7feSMatthias Ringwald                 default:
14201d81c7feSMatthias Ringwald                     break;
14211d81c7feSMatthias Ringwald             }
14223d051a53SMatthias Ringwald             return true;
14233d051a53SMatthias Ringwald 
1424ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
14251d81c7feSMatthias Ringwald 
14261d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14271d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1428dd533208SMatthias Ringwald 
1429a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1430ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1431a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
14322308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1433ce263fc8SMatthias Ringwald                     break;
1434ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1435a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
14362308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1437ce263fc8SMatthias Ringwald                     break;
1438ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1439ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1440ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14412308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1442ce263fc8SMatthias Ringwald                     break;
1443ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14442308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1445a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1446a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1447ce263fc8SMatthias Ringwald                     break;
1448ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14492308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1450a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1451ce263fc8SMatthias Ringwald                     break;
1452ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1453a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1454a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
14552308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1456ce263fc8SMatthias Ringwald                     break;
14570222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
14580222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
14590222a807SMatthias Ringwald                     break;
1460ce263fc8SMatthias Ringwald                 default:
1461ce263fc8SMatthias Ringwald                     break;
1462ce263fc8SMatthias Ringwald             }
14631d81c7feSMatthias Ringwald 
14641d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14651d81c7feSMatthias Ringwald 
14661d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
14671d81c7feSMatthias Ringwald 
1468ba0de059SMatthias Ringwald             return true;
1469ce263fc8SMatthias Ringwald 
147074c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
14711d81c7feSMatthias Ringwald 
14721d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14731d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
14741d81c7feSMatthias Ringwald 
1475a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1476ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1477c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE,  (char *)hfp_connection->line_buffer);
1478ce263fc8SMatthias Ringwald                     break;
1479ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14802308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
148125789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
148225789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1483ce263fc8SMatthias Ringwald                     break;
1484125560b8SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1485f6e6c52dSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1486125560b8SMatthias Ringwald                     // track if last argument exists
1487125560b8SMatthias Ringwald                     hfp_connection->clip_have_alpha = hfp_connection->line_size != 0;
1488125560b8SMatthias Ringwald                     break;
1489ce263fc8SMatthias Ringwald                 default:
1490ce263fc8SMatthias Ringwald                     break;
1491ce263fc8SMatthias Ringwald             }
14921d81c7feSMatthias Ringwald 
14931d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14941d81c7feSMatthias Ringwald 
14951d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
14961d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
14971d81c7feSMatthias Ringwald             }
1498d6b237acSMatthias Ringwald             return true;
149974c7548aSMatthias Ringwald 
1500471dea41SMatthias Ringwald         case HFP_PARSER_CUSTOM_COMMAND:
1501471dea41SMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
1502471dea41SMatthias Ringwald             return true;
1503471dea41SMatthias Ringwald 
150474c7548aSMatthias Ringwald         default:
150574c7548aSMatthias Ringwald             btstack_assert(false);
150674c7548aSMatthias Ringwald             return true;
150774c7548aSMatthias Ringwald     }
1508d6b237acSMatthias Ringwald }
1509d6b237acSMatthias Ringwald 
1510d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1511d6b237acSMatthias Ringwald     bool processed = false;
1512d6b237acSMatthias Ringwald     while (!processed){
1513d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1514d6b237acSMatthias Ringwald     }
15151d81c7feSMatthias Ringwald     // reset parser state on end-of-line
15161d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
1517a34e380cSMatthias Ringwald         hfp_connection->found_equal_sign = false;
15181d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
15191d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
15201d81c7feSMatthias Ringwald     }
1521ce263fc8SMatthias Ringwald }
1522ce263fc8SMatthias Ringwald 
1523a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1524ce263fc8SMatthias Ringwald     int value;
1525a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1526667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
15272308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1528667ec068SMatthias Ringwald             int i;
1529a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1530667ec068SMatthias Ringwald                 case 0:
1531a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1532a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1533a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1534667ec068SMatthias Ringwald                             break;
1535667ec068SMatthias Ringwald                         }
1536667ec068SMatthias Ringwald                     }
1537667ec068SMatthias Ringwald                     break;
1538667ec068SMatthias Ringwald                 case 1:
1539a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1540a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1541667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1542a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1543667ec068SMatthias Ringwald                     break;
1544667ec068SMatthias Ringwald                 default:
1545667ec068SMatthias Ringwald                     break;
1546667ec068SMatthias Ringwald             }
154725789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1548667ec068SMatthias Ringwald             break;
1549667ec068SMatthias Ringwald 
1550667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1551a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1552667ec068SMatthias Ringwald                 case 0:
155328a4aea3SMilanka Ringwald                     // <alpha>: This optional field is not supported, and shall be left blank.
155428a4aea3SMilanka Ringwald                     break;
155528a4aea3SMilanka Ringwald                 case 1:
155628a4aea3SMilanka Ringwald                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1557c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1558667ec068SMatthias Ringwald                     break;
155928a4aea3SMilanka Ringwald                 case 2:
156028a4aea3SMilanka Ringwald                     /*
156128a4aea3SMilanka Ringwald                       <type> field specifies the format of the phone number provided, and can be one of the following values:
156228a4aea3SMilanka 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.
156328a4aea3SMilanka 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.
156428a4aea3SMilanka Ringwald                      - values 160-175: National number. No prefix nor escape digits included.
156528a4aea3SMilanka Ringwald                      */
15662308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1567a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1568667ec068SMatthias Ringwald                     break;
156928a4aea3SMilanka Ringwald                 case 3:
157028a4aea3SMilanka Ringwald                     // <speed>: This optional field is not supported, and shall be left blank.
157128a4aea3SMilanka Ringwald                     break;
157228a4aea3SMilanka Ringwald                 case 4:
157328a4aea3SMilanka Ringwald                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1574667ec068SMatthias Ringwald                 default:
1575667ec068SMatthias Ringwald                     break;
1576667ec068SMatthias Ringwald             }
1577eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1578a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1579667ec068SMatthias Ringwald             break;
1580667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1581a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1582667ec068SMatthias Ringwald                 case 0:
15832308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1584a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1585667ec068SMatthias Ringwald                     break;
1586667ec068SMatthias Ringwald                 case 1:
15872308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1588a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1589667ec068SMatthias Ringwald                     break;
1590667ec068SMatthias Ringwald                 case 2:
15912308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1592a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1593667ec068SMatthias Ringwald                     break;
1594667ec068SMatthias Ringwald                 case 3:
15952308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15960aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1597667ec068SMatthias Ringwald                     break;
1598667ec068SMatthias Ringwald                 case 4:
15990aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
16000aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
16010aee97efSMilanka Ringwald                     break;
16020aee97efSMilanka Ringwald                 case 5:
1603c10fde09SMatthias Ringwald                     btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
1604667ec068SMatthias Ringwald                     break;
16050aee97efSMilanka Ringwald                 case 6:
16062308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1607a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1608667ec068SMatthias Ringwald                     break;
1609667ec068SMatthias Ringwald                 default:
1610667ec068SMatthias Ringwald                     break;
1611667ec068SMatthias Ringwald             }
1612eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1613a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1614667ec068SMatthias Ringwald             break;
1615aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
16162308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1617a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1618aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1619aa4dd815SMatthias Ringwald             break;
1620aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
16212308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1622a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1623aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1624aa4dd815SMatthias Ringwald             break;
1625aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
16262308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1627a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1628aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1629aa4dd815SMatthias Ringwald             break;
1630aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
16312308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1632a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1633aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1634aa4dd815SMatthias Ringwald             break;
16353deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
16362308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1637a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
16383deb3ec6SMatthias Ringwald             break;
16393deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
16402308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1641a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
16423deb3ec6SMatthias Ringwald             break;
16433deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
16442308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1645bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
16463deb3ec6SMatthias Ringwald             break;
16473deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1648a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1649ab2445a0SMatthias Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
165025789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1651a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
16523deb3ec6SMatthias Ringwald             break;
1653aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1654c10fde09SMatthias Ringwald             btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer);
1655a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1656a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1657aa4dd815SMatthias Ringwald             break;
1658aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1659a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
16602308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
166125789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
16623deb3ec6SMatthias Ringwald             break;
16633deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
166425789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1665a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1666a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
16672308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1668a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
16693deb3ec6SMatthias Ringwald             break;
16703deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1671a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1672a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
167397d2cfbcSMatthias 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);
167425789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1675eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
16763deb3ec6SMatthias Ringwald             break;
1677aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1678aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1679a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
16802308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
168125789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1682a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
16833deb3ec6SMatthias Ringwald             break;
1684aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
16853deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1686a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
168725789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
16883deb3ec6SMatthias Ringwald             break;
1689ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
169025789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1691a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1692ce263fc8SMatthias Ringwald             break;
16933deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
16943deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1695a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1696a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1697a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1698a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1699ce263fc8SMatthias Ringwald             }
1700a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1701ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1702a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1703ce263fc8SMatthias Ringwald             } else {
17042308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1705a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1706a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1707a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
17083deb3ec6SMatthias Ringwald             }
170925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
17103deb3ec6SMatthias Ringwald             break;
17113deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
17123deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
171325789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1714a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
17153deb3ec6SMatthias Ringwald             break;
1716aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
17172308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1718a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1719aa4dd815SMatthias Ringwald             break;
1720aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1721a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1722a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
17233deb3ec6SMatthias Ringwald                 break;
17243deb3ec6SMatthias Ringwald             }
17253deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1726a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
17273deb3ec6SMatthias Ringwald             break;
17283deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
17293deb3ec6SMatthias Ringwald             break;
17303deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1731a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
17322308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
17333deb3ec6SMatthias Ringwald             break;
17343deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
17352308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1736a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1737a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
17383deb3ec6SMatthias Ringwald             break;
1739ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1740a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1741a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1742c10fde09SMatthias Ringwald             btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer);
17433deb3ec6SMatthias Ringwald             break;
17440222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
17450222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
17460222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
17470222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
17480222a807SMatthias Ringwald             }
17490222a807SMatthias Ringwald             break;
17500222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
17510222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
17520222a807SMatthias Ringwald             break;
17530222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
17540222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
17550222a807SMatthias Ringwald             break;
17560222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
17570222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
17580222a807SMatthias Ringwald             break;
17590222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
17600222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
17610222a807SMatthias Ringwald             break;
17620b4debbfSMilanka Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
17630b4debbfSMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
17640b4debbfSMilanka Ringwald             hfp_connection->ag_activate_voice_recognition_value = value;
17650b4debbfSMilanka Ringwald             break;
1766db3cdbd4SMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1767db3cdbd4SMilanka Ringwald             switch(hfp_connection->parser_item_index){
1768db3cdbd4SMilanka Ringwald                 case 0:
17690b4debbfSMilanka Ringwald                     hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1770db3cdbd4SMilanka Ringwald                     break;
1771db3cdbd4SMilanka Ringwald                 case 1:
1772c78e7b73SMatthias Ringwald                     hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1773db3cdbd4SMilanka Ringwald                     break;
1774db3cdbd4SMilanka Ringwald                 case 2:
177545796ff1SMilanka Ringwald                     hfp_connection->ag_msg.text_id = 0;
1776db3cdbd4SMilanka Ringwald                     for (i = 0 ; i < 4; i++){
177745796ff1SMilanka Ringwald                         hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]);
1778db3cdbd4SMilanka Ringwald                     }
1779db3cdbd4SMilanka Ringwald                     break;
1780db3cdbd4SMilanka Ringwald                 case 3:
17810d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1782db3cdbd4SMilanka Ringwald                     break;
1783db3cdbd4SMilanka Ringwald                 case 4:
17840d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1785db3cdbd4SMilanka Ringwald                     break;
1786db3cdbd4SMilanka Ringwald                 case 5:
178751aa5d5aSMilanka Ringwald                     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
178851aa5d5aSMilanka Ringwald                     hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1;
1789db3cdbd4SMilanka Ringwald                     break;
1790db3cdbd4SMilanka Ringwald                 default:
1791db3cdbd4SMilanka Ringwald                     break;
1792db3cdbd4SMilanka Ringwald             }
1793ddbf29d2SMilanka Ringwald             hfp_connection->parser_item_index++;
1794db3cdbd4SMilanka Ringwald             break;
17953deb3ec6SMatthias Ringwald         default:
17963deb3ec6SMatthias Ringwald             break;
17973deb3ec6SMatthias Ringwald     }
17983deb3ec6SMatthias Ringwald }
17993deb3ec6SMatthias Ringwald 
18001d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
18011d9c9c90SMilanka Ringwald     UNUSED(context);
18021d9c9c90SMilanka Ringwald 
18031d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
18041d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
18051d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
18061d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
18071d9c9c90SMilanka Ringwald 
18081d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
18091d9c9c90SMilanka Ringwald 
18101d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1811aeb0f0feSMatthias Ringwald         hfp_sdp_query_context.local_role = connection->local_role;
1812aeb0f0feSMatthias Ringwald         (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6);
1813e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
18141d9c9c90SMilanka Ringwald         return;
18151d9c9c90SMilanka Ringwald     }
18161d9c9c90SMilanka Ringwald }
18171d9c9c90SMilanka Ringwald 
18184eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
18194eb3f1d8SMilanka Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
18204eb3f1d8SMilanka Ringwald     if (connection){
18214eb3f1d8SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18223deb3ec6SMatthias Ringwald     }
18234eb3f1d8SMilanka Ringwald 
18244eb3f1d8SMilanka Ringwald     connection = hfp_create_connection(bd_addr, local_role);
18254eb3f1d8SMilanka Ringwald     if (!connection){
18264eb3f1d8SMilanka Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
18274eb3f1d8SMilanka Ringwald     }
18284eb3f1d8SMilanka Ringwald 
18294eb3f1d8SMilanka Ringwald     connection->state = HFP_W2_SEND_SDP_QUERY;
18304eb3f1d8SMilanka Ringwald 
18314eb3f1d8SMilanka Ringwald     bd_addr_copy(connection->remote_addr, bd_addr);
18324eb3f1d8SMilanka Ringwald     connection->service_uuid = service_uuid;
18331d9c9c90SMilanka Ringwald 
1834aeb0f0feSMatthias Ringwald     hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query;
18351d9c9c90SMilanka Ringwald     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
1836aeb0f0feSMatthias Ringwald     (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
18374eb3f1d8SMilanka Ringwald     return ERROR_CODE_SUCCESS;
18383deb3ec6SMatthias Ringwald }
18393deb3ec6SMatthias Ringwald 
1840657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){
18411ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1842657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
18433deb3ec6SMatthias Ringwald 
1844657bc59fSMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
1845657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1846657bc59fSMilanka Ringwald         hfp_connection->state = HFP_IDLE;
18473deb3ec6SMatthias Ringwald         return;
18483deb3ec6SMatthias Ringwald     }
18493deb3ec6SMatthias Ringwald 
1850657bc59fSMilanka Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1851657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
18523deb3ec6SMatthias Ringwald         return;
18533deb3ec6SMatthias Ringwald     }
185452cbdd6dSMilanka Ringwald     hfp_connection->release_slc_connection = 1;
1855657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1856657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
18570c3047fbSMatthias Ringwald         return;
18580c3047fbSMatthias Ringwald     }
18590c3047fbSMatthias Ringwald 
1860657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1861657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
186252cbdd6dSMilanka Ringwald         hfp_connection->release_audio_connection = 1;
18630c3047fbSMatthias Ringwald         return;
18640c3047fbSMatthias Ringwald     }
18653deb3ec6SMatthias Ringwald }
18663deb3ec6SMatthias Ringwald 
18670b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){
18681ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1869657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
18700b4debbfSMilanka Ringwald     if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
18710b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18721ffa0dd9SMilanka Ringwald     }
18730b4debbfSMilanka Ringwald     switch (hfp_connection->state) {
18740b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
18750b4debbfSMilanka Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
18760b4debbfSMilanka Ringwald             break;
18770b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
18780b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
18790b4debbfSMilanka Ringwald             hfp_connection->release_audio_connection = 1;
18800b4debbfSMilanka Ringwald             break;
18810b4debbfSMilanka Ringwald         default:
18820b4debbfSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
18830b4debbfSMilanka Ringwald     }
18840b4debbfSMilanka Ringwald     return ERROR_CODE_SUCCESS;
18853deb3ec6SMatthias Ringwald }
18863deb3ec6SMatthias Ringwald 
1887eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1888ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1889eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1890ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1891aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = hfp_connection;
1892d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1893d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1894689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1895689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1896689d4323SMatthias Ringwald #else
1897689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1898689d4323SMatthias Ringwald #endif
1899d2c1d59aSMatthias Ringwald     }
1900352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1901352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1902eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1903352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1904ce263fc8SMatthias Ringwald }
1905d68dcce1SMatthias Ringwald 
1906cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){
1907cb81d35dSMatthias Ringwald 
1908cb81d35dSMatthias Ringwald     // remote supported feature eSCO is set if link type is eSCO
1909cb81d35dSMatthias Ringwald     // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
1910cb81d35dSMatthias Ringwald     uint16_t max_latency;
1911cb81d35dSMatthias Ringwald     uint8_t  retransmission_effort;
1912cb81d35dSMatthias Ringwald     uint16_t packet_types;
1913cb81d35dSMatthias Ringwald 
1914cb81d35dSMatthias Ringwald     if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
1915cb81d35dSMatthias Ringwald         max_latency = 0x000c;
1916cb81d35dSMatthias Ringwald         retransmission_effort = 0x02;
1917cb81d35dSMatthias Ringwald         // eSCO: EV3 and 2-EV3
1918cb81d35dSMatthias Ringwald         packet_types = 0x0048;
1919cb81d35dSMatthias Ringwald     } else {
1920cb81d35dSMatthias Ringwald         max_latency = 0xffff;
1921cb81d35dSMatthias Ringwald         retransmission_effort = 0xff;
1922cb81d35dSMatthias Ringwald         // sco: HV1 and HV3
1923cb81d35dSMatthias Ringwald         packet_types = 0x005;
1924cb81d35dSMatthias Ringwald     }
1925cb81d35dSMatthias Ringwald 
1926cb81d35dSMatthias Ringwald     // mSBC only allows for transparent data
1927cb81d35dSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1928cb81d35dSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1929cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1930cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1931cb81d35dSMatthias Ringwald #else
1932cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1933cb81d35dSMatthias Ringwald #endif
1934cb81d35dSMatthias Ringwald     }
1935cb81d35dSMatthias Ringwald 
1936cb81d35dSMatthias Ringwald     // filter packet types
1937cb81d35dSMatthias Ringwald     packet_types &= hfp_get_sco_packet_types();
1938cb81d35dSMatthias Ringwald 
1939cb81d35dSMatthias Ringwald     // bits 6-9 are 'don't allow'
1940cb81d35dSMatthias Ringwald     packet_types ^= 0x3c0;
1941cb81d35dSMatthias Ringwald 
1942cb81d35dSMatthias Ringwald     log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
1943cb81d35dSMatthias Ringwald     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
1944cb81d35dSMatthias Ringwald                  sco_voice_setting, retransmission_effort, packet_types);
1945cb81d35dSMatthias Ringwald }
1946cb81d35dSMatthias Ringwald 
19473721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
19483721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
19493721a235SMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
19503721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
19513721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
19523721a235SMatthias Ringwald     }
19533721a235SMatthias Ringwald }
19543721a235SMatthias Ringwald 
19553721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
19563721a235SMatthias Ringwald     uint32_t sample_rate_hz;
19573721a235SMatthias Ringwald     uint16_t clock_rate_khz;
19583721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
19593721a235SMatthias Ringwald         clock_rate_khz = 512;
19603721a235SMatthias Ringwald         sample_rate_hz = 16000;
19613721a235SMatthias Ringwald     } else {
19623721a235SMatthias Ringwald         clock_rate_khz = 256;
19633721a235SMatthias Ringwald         sample_rate_hz = 8000;
19643721a235SMatthias Ringwald     }
19653721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
19663721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
19673721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
19683721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
19693721a235SMatthias Ringwald     uint8_t  reserved = 0;
19703721a235SMatthias Ringwald     uint16_t size = 16;
19713721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
19723721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
19733721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
19743721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
19753721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
19763721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
19773721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
19783721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
19793721a235SMatthias Ringwald }
19803721a235SMatthias Ringwald #endif
19813721a235SMatthias Ringwald 
1982689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1983689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1984689d4323SMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1985689d4323SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1986689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
1987689d4323SMatthias Ringwald     }
1988689d4323SMatthias Ringwald }
1989689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1990689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
19911d2bbd54SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 512 kHz
19921d2bbd54SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
1993689d4323SMatthias Ringwald }
1994689d4323SMatthias Ringwald #endif
1995689d4323SMatthias Ringwald 
19961b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
19971b005648SMatthias Ringwald     hfp_hf_callback = callback;
19981b005648SMatthias Ringwald }
19991b005648SMatthias Ringwald 
20001b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
20011b005648SMatthias Ringwald     hfp_ag_callback = callback;
20021b005648SMatthias Ringwald }
20031b005648SMatthias Ringwald 
2004ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
2005ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
2006ca59be51SMatthias Ringwald }
20071b005648SMatthias Ringwald 
2008ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
2009ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
2010d68dcce1SMatthias Ringwald }
2011d68dcce1SMatthias Ringwald 
2012520c92d5SMatthias Ringwald void hfp_init(void){
2013352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
2014991c26beSMatthias Ringwald }
2015991c26beSMatthias Ringwald 
201620b2edb6SMatthias Ringwald void hfp_deinit(void){
2017aeb0f0feSMatthias Ringwald     hfp_allowed_sco_packet_types = 0;
201820b2edb6SMatthias Ringwald     hfp_connections = NULL;
201920b2edb6SMatthias Ringwald     hfp_hf_callback = NULL;
202020b2edb6SMatthias Ringwald     hfp_ag_callback = NULL;
202120b2edb6SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = NULL;
202220b2edb6SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = NULL;
2023aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
2024471dea41SMatthias Ringwald     hfp_custom_commands_ag = NULL;
2025aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
2026aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t));
202720b2edb6SMatthias Ringwald }
202820b2edb6SMatthias Ringwald 
2029991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
203001e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
2031991c26beSMatthias Ringwald }
2032991c26beSMatthias Ringwald 
2033991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
203401e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
2035520c92d5SMatthias Ringwald }
2036186dd3d2SMatthias Ringwald 
2037e453c1d9SMatthias 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){
2038e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
2039e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
2040e453c1d9SMatthias Ringwald     while (setting > 0){
2041e453c1d9SMatthias Ringwald         setting--;
2042e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
2043e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
2044e453c1d9SMatthias Ringwald         // skip if S4 but not supported
2045e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
2046e453c1d9SMatthias Ringwald         // skip wrong codec
2047e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
204801e5b727SMatthias Ringwald         // skip disabled packet types
2049352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
2050352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
2051352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
205201e5b727SMatthias Ringwald 
2053e453c1d9SMatthias Ringwald         // found matching setting
2054e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
2055afac2a04SMilanka Ringwald     }
2056e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
2057afac2a04SMilanka Ringwald }
2058e453c1d9SMatthias Ringwald 
20592b7abbfbSMatthias 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){
2060e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
2061e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
2062e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
20632b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
20642b7abbfbSMatthias Ringwald }
20652b7abbfbSMatthias Ringwald 
20662b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2067e453c1d9SMatthias Ringwald     // get highest possible link setting
20682b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
2069afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
2070afac2a04SMilanka Ringwald }
2071afac2a04SMilanka Ringwald 
2072186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
2073186dd3d2SMatthias Ringwald 
2074186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
2075186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
2076186dd3d2SMatthias Ringwald     // encode \n\r
2077186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
207815a27967SMatthias Ringwald     int i = 0;
2079186dd3d2SMatthias Ringwald     int pos;
208015a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
2081186dd3d2SMatthias Ringwald         switch (packet[pos]){
2082186dd3d2SMatthias Ringwald             case '\n':
2083186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2084186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
2085186dd3d2SMatthias Ringwald                 break;
2086186dd3d2SMatthias Ringwald             case '\r':
2087186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2088186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
2089186dd3d2SMatthias Ringwald                 break;
2090186dd3d2SMatthias Ringwald             default:
2091186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
2092186dd3d2SMatthias Ringwald                 break;
2093186dd3d2SMatthias Ringwald         }
2094186dd3d2SMatthias Ringwald     }
2095186dd3d2SMatthias Ringwald     printable[i] = 0;
2096186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
2097186dd3d2SMatthias Ringwald #endif
2098186dd3d2SMatthias Ringwald }
2099471dea41SMatthias Ringwald 
2100471dea41SMatthias Ringwald void hfp_register_custom_ag_command(hfp_custom_at_command_t * custom_at_command){
2101471dea41SMatthias Ringwald     btstack_linked_list_add(&hfp_custom_commands_ag, (btstack_linked_item_t *) custom_at_command);
2102471dea41SMatthias Ringwald }
2103