xref: /btstack/src/classic/hfp.c (revision 19976d6bd98e8cf68abcc2f738a03f6c8bc4c552)
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 
9720b2edb6SMatthias Ringwald 
9820b2edb6SMatthias Ringwald // prototypes
9920b2edb6SMatthias 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);
10020b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
10120b2edb6SMatthias Ringwald 
1023deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
1033deb3ec6SMatthias Ringwald         "EC and/or NR function",
1043deb3ec6SMatthias Ringwald         "Three-way calling",
1053deb3ec6SMatthias Ringwald         "CLI presentation capability",
1063deb3ec6SMatthias Ringwald         "Voice recognition activation",
1073deb3ec6SMatthias Ringwald         "Remote volume control",
1083deb3ec6SMatthias Ringwald 
1093deb3ec6SMatthias Ringwald         "Enhanced call status",
1103deb3ec6SMatthias Ringwald         "Enhanced call control",
1113deb3ec6SMatthias Ringwald 
1123deb3ec6SMatthias Ringwald         "Codec negotiation",
1133deb3ec6SMatthias Ringwald 
1143deb3ec6SMatthias Ringwald         "HF Indicators",
1153deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1163deb3ec6SMatthias Ringwald         "Reserved for future definition"
1173deb3ec6SMatthias Ringwald };
1183deb3ec6SMatthias Ringwald 
1193deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
1203deb3ec6SMatthias Ringwald         "Three-way calling",
1213deb3ec6SMatthias Ringwald         "EC and/or NR function",
1223deb3ec6SMatthias Ringwald         "Voice recognition function",
1233deb3ec6SMatthias Ringwald         "In-band ring tone capability",
1243deb3ec6SMatthias Ringwald         "Attach a number to a voice tag",
1253deb3ec6SMatthias Ringwald         "Ability to reject a call",
1263deb3ec6SMatthias Ringwald         "Enhanced call status",
1273deb3ec6SMatthias Ringwald         "Enhanced call control",
1283deb3ec6SMatthias Ringwald         "Extended Error Result Codes",
1293deb3ec6SMatthias Ringwald         "Codec negotiation",
1303deb3ec6SMatthias Ringwald         "HF Indicators",
1313deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1323deb3ec6SMatthias Ringwald         "Reserved for future definition"
1333deb3ec6SMatthias Ringwald };
1343deb3ec6SMatthias Ringwald 
1350aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1360aee97efSMilanka Ringwald         "outgoing",
1370aee97efSMilanka Ringwald         "incoming"
1380aee97efSMilanka Ringwald };
1390aee97efSMilanka Ringwald 
1400aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1410aee97efSMilanka Ringwald         "active",
1420aee97efSMilanka Ringwald         "held",
1430aee97efSMilanka Ringwald         "outgoing dialing",
1440aee97efSMilanka Ringwald         "outgoing alerting",
1450aee97efSMilanka Ringwald         "incoming",
1460aee97efSMilanka Ringwald         "incoming waiting",
1470aee97efSMilanka Ringwald         "call held by response and hold"
1480aee97efSMilanka Ringwald };
1490aee97efSMilanka Ringwald 
1500aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1510aee97efSMilanka Ringwald         "voice",
1520aee97efSMilanka Ringwald         "data",
1530aee97efSMilanka Ringwald         "fax"
1540aee97efSMilanka Ringwald };
1550aee97efSMilanka Ringwald 
1560aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1570aee97efSMilanka Ringwald         "not a conference call",
1580aee97efSMilanka Ringwald         "conference call"
1590aee97efSMilanka Ringwald };
1600aee97efSMilanka Ringwald 
1610aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1620aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1630aee97efSMilanka Ringwald     return "not defined";
1640aee97efSMilanka Ringwald }
1650aee97efSMilanka Ringwald 
1660aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1670aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1680aee97efSMilanka Ringwald     return "not defined";
1690aee97efSMilanka Ringwald }
1700aee97efSMilanka Ringwald 
1710aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1720aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1730aee97efSMilanka Ringwald     return "not defined";
1740aee97efSMilanka Ringwald }
1750aee97efSMilanka Ringwald 
1760aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1770aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1780aee97efSMilanka Ringwald     return "not defined";
1790aee97efSMilanka Ringwald }
1800aee97efSMilanka Ringwald 
18125789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
18225789943SMilanka Ringwald     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
18325789943SMilanka Ringwald 
18425789943SMilanka Ringwald     if (index > HFP_MAX_NUM_INDICATORS){
18525789943SMilanka Ringwald         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
18625789943SMilanka Ringwald         return HFP_MAX_NUM_INDICATORS - 1;
18725789943SMilanka Ringwald     }
18825789943SMilanka Ringwald 
18925789943SMilanka Ringwald     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
19025789943SMilanka Ringwald     if (index > 0){
19125789943SMilanka Ringwald         index -= 1;
19225789943SMilanka Ringwald     } else {
19325789943SMilanka Ringwald         log_info("ignoring invalid indicator index 0");
19425789943SMilanka Ringwald         return 0;
19525789943SMilanka Ringwald     }
19625789943SMilanka Ringwald     return index;
19725789943SMilanka Ringwald }
19825789943SMilanka Ringwald 
19925789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
20025789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
20125789943SMilanka Ringwald         hfp_connection->parser_item_index++;
20225789943SMilanka Ringwald     } else {
20325789943SMilanka Ringwald         log_info("Ignoring additional indicator");
20425789943SMilanka Ringwald     }
20525789943SMilanka Ringwald }
20625789943SMilanka Ringwald 
20725789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
20825789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
20925789943SMilanka Ringwald         hfp_connection->parser_item_index++;
21025789943SMilanka Ringwald     } else {
21125789943SMilanka Ringwald         log_info("Ignoring additional codec index");
21225789943SMilanka Ringwald     }
21325789943SMilanka Ringwald }
21425789943SMilanka Ringwald 
215eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
216eed67a37SMilanka Ringwald     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
217eed67a37SMilanka Ringwald         hfp_connection->remote_call_services_index++;
218eed67a37SMilanka Ringwald     } else {
219eed67a37SMilanka Ringwald         log_info("Ignoring additional remote_call_services");
220eed67a37SMilanka Ringwald     }
221eed67a37SMilanka Ringwald }
22225789943SMilanka Ringwald 
2233deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
2243deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
2253deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
2263deb3ec6SMatthias Ringwald     }
2273deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
2283deb3ec6SMatthias Ringwald }
2293deb3ec6SMatthias Ringwald 
2303deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
2313deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
2323deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
2333deb3ec6SMatthias Ringwald     }
2343deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
2353deb3ec6SMatthias Ringwald }
2363deb3ec6SMatthias Ringwald 
2373deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
2383deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
23974386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
24028190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
2413deb3ec6SMatthias Ringwald     if (err){
24228190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
2433deb3ec6SMatthias Ringwald     }
244e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
245e43d1938SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
246e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
247e43d1938SMatthias Ringwald #endif
2483deb3ec6SMatthias Ringwald     return 1;
2493deb3ec6SMatthias Ringwald }
2503deb3ec6SMatthias Ringwald 
2516a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
252e8d1dc0dSMatthias Ringwald 
253e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
254c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
255e8d1dc0dSMatthias Ringwald 
256df327ff3SMilanka Ringwald     int i;
257df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
258e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
259e8d1dc0dSMatthias Ringwald         return 1;
260df327ff3SMilanka Ringwald     }
261df327ff3SMilanka Ringwald     return 0;
262df327ff3SMilanka Ringwald }
263df327ff3SMilanka Ringwald 
264d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
265d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
266d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
267d715cf51SMatthias Ringwald     int i;
268d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
269d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
270d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
271d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
272d715cf51SMatthias Ringwald     }
273d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2746535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
275d715cf51SMatthias Ringwald }
276d715cf51SMatthias Ringwald 
2773deb3ec6SMatthias Ringwald // UTILS
2783deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2793deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
2803deb3ec6SMatthias Ringwald }
2813deb3ec6SMatthias Ringwald 
2823deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
2833deb3ec6SMatthias Ringwald     if (value){
2843deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
2853deb3ec6SMatthias Ringwald     } else {
2863deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
2873deb3ec6SMatthias Ringwald     }
2883deb3ec6SMatthias Ringwald     return bitmap;
2893deb3ec6SMatthias Ringwald }
2903deb3ec6SMatthias Ringwald 
2913deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
292c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2933deb3ec6SMatthias Ringwald     int i;
2943deb3ec6SMatthias Ringwald     int offset = 0;
295c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2963deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
2973deb3ec6SMatthias Ringwald     }
2983deb3ec6SMatthias Ringwald     if (i<values_nr){
2993deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
3003deb3ec6SMatthias Ringwald     }
3013deb3ec6SMatthias Ringwald     return offset;
3023deb3ec6SMatthias Ringwald }
3033deb3ec6SMatthias Ringwald 
3043deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
305c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3063deb3ec6SMatthias Ringwald 
3073deb3ec6SMatthias Ringwald     int i;
3083deb3ec6SMatthias Ringwald     int offset = 0;
309c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3103deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
3113deb3ec6SMatthias Ringwald     }
3123deb3ec6SMatthias Ringwald 
3133deb3ec6SMatthias Ringwald     if (i<values_nr){
3143deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3153deb3ec6SMatthias Ringwald     }
3163deb3ec6SMatthias Ringwald     return offset;
3173deb3ec6SMatthias Ringwald }
3183deb3ec6SMatthias Ringwald 
319ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
320ca59be51SMatthias Ringwald     if (!hfp_connection) return;
321ca59be51SMatthias Ringwald     btstack_packet_handler_t callback = NULL;
322ca59be51SMatthias Ringwald     switch (hfp_connection->local_role){
323ca59be51SMatthias Ringwald         case HFP_ROLE_HF:
324ca59be51SMatthias Ringwald             callback = hfp_hf_callback;
325671f1aa2SMatthias Ringwald             break;
326ca59be51SMatthias Ringwald         case HFP_ROLE_AG:
327ca59be51SMatthias Ringwald             callback = hfp_ag_callback;
328671f1aa2SMatthias Ringwald             break;
329ca59be51SMatthias Ringwald         default:
330ca59be51SMatthias Ringwald             return;
331ca59be51SMatthias Ringwald     }
332ca59be51SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
333ca59be51SMatthias Ringwald }
334ca59be51SMatthias Ringwald 
335ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
3367d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
337d703d377SMatthias Ringwald     uint8_t event[5];
338a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
339a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
340a0ffb263SMatthias Ringwald     event[2] = event_subtype;
3417d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
342ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
343a0ffb263SMatthias Ringwald }
344a0ffb263SMatthias Ringwald 
345ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3467d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
347d703d377SMatthias Ringwald     uint8_t event[6];
3483deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3493deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3503deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3517d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
352d703d377SMatthias Ringwald     event[5] = value; // status 0 == OK
353ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3543deb3ec6SMatthias Ringwald }
3553deb3ec6SMatthias Ringwald 
356553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){
357553a4a56SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
358553a4a56SMilanka Ringwald 
359a95ec82fSMilanka Ringwald     uint8_t event[7];
360a95ec82fSMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
361a95ec82fSMilanka Ringwald     event[1] = sizeof(event) - 2;
362a9e632e9SMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED;
363de9e0ea7SMilanka Ringwald 
364553a4a56SMilanka Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
365a95ec82fSMilanka Ringwald     event[5] = status; // 0:success
366553a4a56SMilanka Ringwald     event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0;
367553a4a56SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3681a26de69SMilanka Ringwald }
369553a4a56SMilanka Ringwald 
370553a4a56SMilanka Ringwald void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){
371553a4a56SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
372553a4a56SMilanka Ringwald 
373553a4a56SMilanka Ringwald     uint8_t event[6];
374553a4a56SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
375553a4a56SMilanka Ringwald     event[1] = sizeof(event) - 2;
376a9e632e9SMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED;
377553a4a56SMilanka Ringwald 
378553a4a56SMilanka Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
379553a4a56SMilanka Ringwald     event[5] = status; // 0:success
380013cc750SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
381013cc750SMilanka Ringwald }
382013cc750SMilanka Ringwald 
383de9e0ea7SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){
384de9e0ea7SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
385de9e0ea7SMilanka Ringwald 
386de9e0ea7SMilanka Ringwald     uint8_t event[6];
387de9e0ea7SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
388de9e0ea7SMilanka Ringwald     event[1] = sizeof(event) - 2;
389de9e0ea7SMilanka Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO;
390de9e0ea7SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
391de9e0ea7SMilanka Ringwald     event[5] = status;
392de9e0ea7SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
393de9e0ea7SMilanka Ringwald }
394de9e0ea7SMilanka Ringwald 
395cf75be85SMilanka Ringwald void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){
396cf75be85SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
397cf75be85SMilanka Ringwald 
398cf75be85SMilanka Ringwald     uint8_t event[6];
399cf75be85SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
400cf75be85SMilanka Ringwald     event[1] = sizeof(event) - 2;
401cf75be85SMilanka Ringwald     switch (hfp_connection->ag_vra_state){
402cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT:
403cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT;
404cf75be85SMilanka Ringwald             break;
405cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND:
406cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND;
407cf75be85SMilanka Ringwald             break;
408cf75be85SMilanka Ringwald         case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT:
409cf75be85SMilanka Ringwald             event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT;
410cf75be85SMilanka Ringwald             break;
411cf75be85SMilanka Ringwald         default:
412cf75be85SMilanka Ringwald             btstack_unreachable();
413cf75be85SMilanka Ringwald             break;
414cf75be85SMilanka Ringwald     }
415cf75be85SMilanka Ringwald 
416cf75be85SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
417cf75be85SMilanka Ringwald     event[5] = status;
418cf75be85SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
419cf75be85SMilanka Ringwald }
420cf75be85SMilanka Ringwald 
421ca59be51SMatthias Ringwald void hfp_emit_slc_connection_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){
4227d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
4238901a7c4SMatthias Ringwald     uint8_t event[12];
4246a7f44bdSMilanka Ringwald     int pos = 0;
4256a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
4266a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
427d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
4286a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
4296a7f44bdSMilanka Ringwald     pos += 2;
43003870092SMilanka Ringwald     event[pos++] = status; // status 0 == OK
4316a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
4326a7f44bdSMilanka Ringwald     pos += 6;
433ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
434a0653c3bSMilanka Ringwald }
435a0653c3bSMilanka Ringwald 
436d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){
4377d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
438d703d377SMatthias Ringwald     uint8_t event[7];
4395441d52fSMatthias Ringwald     int pos = 0;
4405441d52fSMatthias Ringwald     event[pos++] = HCI_EVENT_HFP_META;
4415441d52fSMatthias Ringwald     event[pos++] = sizeof(event) - 2;
4425441d52fSMatthias Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED;
443d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
444d703d377SMatthias Ringwald     pos += 2;
445d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
446d703d377SMatthias Ringwald     pos += 2;
4475441d52fSMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
4485441d52fSMatthias Ringwald }
4495441d52fSMatthias Ringwald 
450d703d377SMatthias Ringwald void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t sco_handle, bd_addr_t addr, uint8_t  negotiated_codec){
4517d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
452d703d377SMatthias Ringwald     uint8_t event[15];
453d0c4aea6SMilanka Ringwald     int pos = 0;
454d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
455d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
456d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
457d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
458d703d377SMatthias Ringwald     pos += 2;
459d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
460d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
461d0c4aea6SMilanka Ringwald     pos += 2;
462d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
463d0c4aea6SMilanka Ringwald     pos += 6;
464d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
465ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
466d0c4aea6SMilanka Ringwald }
467d0c4aea6SMilanka Ringwald 
468ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
4697d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
4702c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
4712c50df93SBjoern Hartmann     uint8_t event[256];
4722c50df93SBjoern Hartmann #else
473c1797c7dSMatthias Ringwald     uint8_t event[40];
4742c50df93SBjoern Hartmann #endif
475aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
476aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
477aa4dd815SMatthias Ringwald     event[2] = event_subtype;
478d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
479d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(value), sizeof(event) - 6);
48013ee9fd7SMatthias Ringwald     strncpy((char*)&event[5], value, size);
481d703d377SMatthias Ringwald     event[5 + size] = 0;
482ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
483aa4dd815SMatthias Ringwald }
484aa4dd815SMatthias Ringwald 
4850cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
4868f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
4873deb3ec6SMatthias Ringwald }
4883deb3ec6SMatthias Ringwald 
4893deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
490665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
491665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
492665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
493a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
494a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
495a0ffb263SMatthias Ringwald             return hfp_connection;
4963deb3ec6SMatthias Ringwald         }
4973deb3ec6SMatthias Ringwald     }
4983deb3ec6SMatthias Ringwald     return NULL;
4993deb3ec6SMatthias Ringwald }
5003deb3ec6SMatthias Ringwald 
501405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
502665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
503665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
504665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
505a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
5065df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
507a0ffb263SMatthias Ringwald             return hfp_connection;
5083deb3ec6SMatthias Ringwald         }
5093deb3ec6SMatthias Ringwald     }
5103deb3ec6SMatthias Ringwald     return NULL;
5113deb3ec6SMatthias Ringwald }
5123deb3ec6SMatthias Ringwald 
513405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
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);
518c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
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_acl_handle(uint16_t handle, hfp_role_t hfp_role){
526d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
527d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
528d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
529d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
530c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
531d97d752dSMilanka Ringwald             return hfp_connection;
532d97d752dSMilanka Ringwald         }
533d97d752dSMilanka Ringwald     }
534d97d752dSMilanka Ringwald     return NULL;
535d97d752dSMilanka Ringwald }
536d97d752dSMilanka Ringwald 
537c95b5b3cSMilanka Ringwald 
538c95b5b3cSMilanka Ringwald static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){
539c95b5b3cSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
540c95b5b3cSMilanka Ringwald     hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state;
541c95b5b3cSMilanka Ringwald     hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
542c95b5b3cSMilanka Ringwald 
543c95b5b3cSMilanka Ringwald     if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
544553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
545c95b5b3cSMilanka Ringwald     } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){
546553a4a56SMilanka Ringwald         hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
547c95b5b3cSMilanka Ringwald     }
548c95b5b3cSMilanka Ringwald 
549c95b5b3cSMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF;
550c95b5b3cSMilanka Ringwald     hfp_connection->activate_voice_recognition = false;
551c95b5b3cSMilanka Ringwald     hfp_connection->deactivate_voice_recognition = false;
552c95b5b3cSMilanka Ringwald     hfp_connection->enhanced_voice_recognition_enabled = false;
553c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_status = 0;
554c95b5b3cSMilanka Ringwald     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
555c95b5b3cSMilanka Ringwald }
556c95b5b3cSMilanka Ringwald 
557a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
558a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
559a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
560a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
5613deb3ec6SMatthias Ringwald 
562dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
5633deb3ec6SMatthias Ringwald 
564a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
565a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
5663deb3ec6SMatthias Ringwald 
567a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
568a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
5693deb3ec6SMatthias Ringwald 
570a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
571a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
5727522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
573a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
5743deb3ec6SMatthias Ringwald 
575a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
576a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
577a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
578a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
579c95b5b3cSMilanka Ringwald     hfp_reset_voice_recognition(hfp_connection);
5803deb3ec6SMatthias Ringwald }
5813deb3ec6SMatthias Ringwald 
582fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
583a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
584a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
5853deb3ec6SMatthias Ringwald 
586a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
587a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
588a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
589aa4dd815SMatthias Ringwald 
590a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
591a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
5923deb3ec6SMatthias Ringwald 
593d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
594d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
595d751f069SMatthias Ringwald 
596a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
5973deb3ec6SMatthias Ringwald 
598d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
599a0ffb263SMatthias Ringwald     return hfp_connection;
6003deb3ec6SMatthias Ringwald }
6013deb3ec6SMatthias Ringwald 
60248e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
603a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
60409a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
6053deb3ec6SMatthias Ringwald }
6063deb3ec6SMatthias Ringwald 
6074eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){
608405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
609a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
610a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
6116535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
612323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
613bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
614bdf572f4SMatthias Ringwald 
615a0ffb263SMatthias Ringwald     return hfp_connection;
6163deb3ec6SMatthias Ringwald }
6173deb3ec6SMatthias Ringwald 
618aa4dd815SMatthias Ringwald /* @param network.
619aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
620aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
621aa4dd815SMatthias Ringwald  */
6223deb3ec6SMatthias Ringwald 
6233deb3ec6SMatthias Ringwald /* @param suported_features
6243deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6253deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
6263deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
6273deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
6283deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
6293deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6303deb3ec6SMatthias Ringwald  */
6313deb3ec6SMatthias Ringwald  /* Bit position:
6323deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
6333deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
6343deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
6353deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
6363deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
6373deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6383deb3ec6SMatthias Ringwald  */
6393deb3ec6SMatthias Ringwald 
6409b1c3b4dSMatthias 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){
6413deb3ec6SMatthias Ringwald     uint8_t* attribute;
6423deb3ec6SMatthias Ringwald     de_create_sequence(service);
6433deb3ec6SMatthias Ringwald 
6443deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
645235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
6469b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
6473deb3ec6SMatthias Ringwald 
6483deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
649235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
6503deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6513deb3ec6SMatthias Ringwald     {
6523deb3ec6SMatthias Ringwald         //  "UUID for Service"
6533deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
654235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
6553deb3ec6SMatthias Ringwald     }
6563deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6573deb3ec6SMatthias Ringwald 
6583deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
659235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
6603deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6613deb3ec6SMatthias Ringwald     {
6623deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
6633deb3ec6SMatthias Ringwald         {
664235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
6653deb3ec6SMatthias Ringwald         }
6663deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
6673deb3ec6SMatthias Ringwald 
6683deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
6693deb3ec6SMatthias Ringwald         {
670235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
6713deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
6723deb3ec6SMatthias Ringwald         }
6733deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
6743deb3ec6SMatthias Ringwald     }
6753deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6763deb3ec6SMatthias Ringwald 
6773deb3ec6SMatthias Ringwald 
6783deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
679235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
6803deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6813deb3ec6SMatthias Ringwald     {
682235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
6833deb3ec6SMatthias Ringwald     }
6843deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6853deb3ec6SMatthias Ringwald 
6863deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
687235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
6883deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6893deb3ec6SMatthias Ringwald     {
6903deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
6913deb3ec6SMatthias Ringwald         {
692235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
6933cb2bba2SMilanka Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8
6943deb3ec6SMatthias Ringwald         }
6953deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
6963deb3ec6SMatthias Ringwald     }
6973deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6983deb3ec6SMatthias Ringwald 
6993deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
7003deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
7013deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
7023deb3ec6SMatthias Ringwald }
7033deb3ec6SMatthias Ringwald 
7046c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
7058a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
7068a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
7078a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
7081d9c9c90SMilanka Ringwald 
709aeb0f0feSMatthias 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);
7101d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
711aeb0f0feSMatthias Ringwald         log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role);
71284904e95SMilanka Ringwald         return;
71384904e95SMilanka Ringwald     }
7143deb3ec6SMatthias Ringwald 
7150e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
7165611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
717a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
7183deb3ec6SMatthias Ringwald             break;
7195611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
720a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
721a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
722520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
723520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
724520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
725520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
726520c92d5SMatthias Ringwald                         break;
727520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
728520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
729520c92d5SMatthias Ringwald                         break;
730520c92d5SMatthias Ringwald                     default:
7311d9c9c90SMilanka Ringwald                         btstack_assert(false);
732520c92d5SMatthias Ringwald                         return;
733520c92d5SMatthias Ringwald                 }
7341d9c9c90SMilanka Ringwald 
735ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
7361d9c9c90SMilanka Ringwald 
7371d9c9c90SMilanka Ringwald             } else {
73884904e95SMilanka Ringwald                 hfp_connection->state = HFP_IDLE;
739cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
740cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
741cc92f22bSMatthias Ringwald                     // report service not found
742cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
743cc92f22bSMatthias Ringwald                 }
744cc92f22bSMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
745cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
7461d9c9c90SMilanka Ringwald             }
7471d9c9c90SMilanka Ringwald 
7481d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
7491d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
750aeb0f0feSMatthias Ringwald             (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
7513deb3ec6SMatthias Ringwald             break;
7523deb3ec6SMatthias Ringwald         default:
7533deb3ec6SMatthias Ringwald             break;
7543deb3ec6SMatthias Ringwald     }
7553deb3ec6SMatthias Ringwald }
7563deb3ec6SMatthias Ringwald 
75738200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
75838200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
759eddcd308SMatthias Ringwald 
760aeb0f0feSMatthias Ringwald     if (!hfp_sco_establishment_active){
7613427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
76238200c1dSMilanka Ringwald         return 0;
763eddcd308SMatthias Ringwald     }
764eddcd308SMatthias Ringwald 
7653427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
7663427dd75SMatthias Ringwald     switch (status){
7673427dd75SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
7683427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
7693427dd75SMatthias Ringwald         case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES:
770eddcd308SMatthias Ringwald             break;
7713427dd75SMatthias Ringwald         default:
7723427dd75SMatthias Ringwald             return 0;
7733427dd75SMatthias Ringwald     }
7743427dd75SMatthias Ringwald 
7753427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
776aeb0f0feSMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false);
7773427dd75SMatthias Ringwald 
7783427dd75SMatthias Ringwald     // handle no valid setting found
7793427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
780aeb0f0feSMatthias Ringwald         if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
7813427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
782aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
783aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->sco_for_msbc_failed = 1;
784a814da6aSMatthias Ringwald             hfp_sco_establishment_active->ag_send_common_codec = true;
785aeb0f0feSMatthias Ringwald             hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
7863427dd75SMatthias Ringwald         } else {
7873427dd75SMatthias Ringwald             // no other options
7883427dd75SMatthias Ringwald             return 0;
789eddcd308SMatthias Ringwald         }
7903427dd75SMatthias Ringwald     }
7913427dd75SMatthias Ringwald 
7923427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
793aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->establish_audio_connection = 1;
794aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active->link_setting = next_setting;
795aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
79638200c1dSMilanka Ringwald     return 1;
797eddcd308SMatthias Ringwald }
798eddcd308SMatthias Ringwald 
799405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8005575558eSMilanka Ringwald     UNUSED(packet_type);
8018a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8025575558eSMilanka Ringwald     UNUSED(size);
8039ec2630cSMatthias Ringwald 
8043deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
80527950165SMatthias Ringwald     hci_con_handle_t handle;
806a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
807674515e8SMatthias Ringwald     uint8_t status;
8083deb3ec6SMatthias Ringwald 
80927950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
810aa4dd815SMatthias Ringwald 
8110e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
8121b005648SMatthias Ringwald 
813011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
8147522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
8157522e673SMatthias Ringwald                 case 0: //  SCO
8167522e673SMatthias Ringwald                 case 2: // eSCO
817011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
818405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
819011577abSMilanka Ringwald                     if (!hfp_connection) break;
820c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
821cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 2;
822c169df2fSMatthias Ringwald                     } else {
823cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 1;
824c169df2fSMatthias Ringwald                     }
8253721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
8263721a235SMatthias Ringwald                     hfp_cc256x_prepare_for_sco(hfp_connection);
8273721a235SMatthias Ringwald #endif
828689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
829689d4323SMatthias Ringwald                     hfp_bcm_prepare_for_sco(hfp_connection);
830689d4323SMatthias Ringwald #endif
8312b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
8322b5f92fdSMatthias Ringwald                     hfp_connection->rtk_send_sco_config = true;
8332b5f92fdSMatthias Ringwald #endif
834cb81d35dSMatthias Ringwald                     log_info("accept sco %u\n", hfp_connection->accept_sco);
835aeb0f0feSMatthias Ringwald                     hfp_sco_establishment_active = hfp_connection;
836202c8a4cSMatthias Ringwald                     break;
8377522e673SMatthias Ringwald                 default:
8387522e673SMatthias Ringwald                     break;
8397522e673SMatthias Ringwald             }
840011577abSMilanka Ringwald             break;
8413deb3ec6SMatthias Ringwald 
842eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
843eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
844aeb0f0feSMatthias Ringwald                 if (hfp_sco_establishment_active == NULL) break;
845be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
84638200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
84738200c1dSMilanka Ringwald 
848aeb0f0feSMatthias Ringwald                 hfp_connection = hfp_sco_establishment_active;
84938200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
85038200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
85138200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
85238200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
8536c5b2002SMatthias Ringwald             }
854eddcd308SMatthias Ringwald             break;
855eddcd308SMatthias Ringwald 
8563deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
857aeb0f0feSMatthias Ringwald             if (hfp_sco_establishment_active == NULL) break;
858011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
859405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
860011577abSMilanka Ringwald             if (!hfp_connection) {
861011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
862011577abSMilanka Ringwald                 return;
863011577abSMilanka Ringwald             }
864ce263fc8SMatthias Ringwald 
865011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
86638200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
867cb81d35dSMatthias Ringwald                 hfp_connection->accept_sco = 0;
86838200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
86938200c1dSMilanka Ringwald 
87038200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
87138200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
87238200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
873aa4dd815SMatthias Ringwald                 break;
874aa4dd815SMatthias Ringwald             }
875aa4dd815SMatthias Ringwald 
876011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
877011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
878011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
879011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
880011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
881011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
8823deb3ec6SMatthias Ringwald 
8833deb3ec6SMatthias Ringwald             switch (link_type){
8843deb3ec6SMatthias Ringwald                 case 0x00:
885aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
8863deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
8873deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
8883deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
8893deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
8903deb3ec6SMatthias Ringwald                     break;
8913deb3ec6SMatthias Ringwald                 case 0x02:
892aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
8933deb3ec6SMatthias Ringwald                     break;
8943deb3ec6SMatthias Ringwald                 default:
8953deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
8963deb3ec6SMatthias Ringwald                     break;
8973deb3ec6SMatthias Ringwald             }
898e0d13a19SMilanka Ringwald 
8993deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
900aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
901e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
902e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
9033deb3ec6SMatthias Ringwald 
904d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
905aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
906d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
907aa4dd815SMatthias Ringwald                 break;
908aa4dd815SMatthias Ringwald             }
909d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
910d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
911c95b5b3cSMilanka Ringwald 
912d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
9131a26de69SMilanka Ringwald 
9140b4debbfSMilanka Ringwald             switch (hfp_connection->vra_state){
9150b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
9161a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = false;
9170b4debbfSMilanka Ringwald                     break;
9180b4debbfSMilanka Ringwald                 default:
9191a26de69SMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_before_vra = true;
9200b4debbfSMilanka Ringwald                     break;
9210b4debbfSMilanka Ringwald             }
92238200c1dSMilanka Ringwald             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
9233deb3ec6SMatthias Ringwald             break;
9243deb3ec6SMatthias Ringwald         }
9253deb3ec6SMatthias Ringwald 
9263deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
927f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
928405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
929aa4dd815SMatthias Ringwald 
930d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
931aa4dd815SMatthias Ringwald 
9323721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
9333721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
9343721a235SMatthias Ringwald #endif
935689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
936689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
937689d4323SMatthias Ringwald #endif
93852cbdd6dSMilanka Ringwald             if (hfp_connection->sco_handle == handle){
939d751f069SMatthias Ringwald                 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
940d63c37a1SMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
94152cbdd6dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
94252cbdd6dSMilanka Ringwald                 hfp_emit_audio_connection_released(hfp_connection, handle);
94352cbdd6dSMilanka Ringwald 
9441a26de69SMilanka Ringwald                 hfp_connection->ag_audio_connection_opened_before_vra = false;
94552cbdd6dSMilanka Ringwald 
94652cbdd6dSMilanka Ringwald                 if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){
94711b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
94852cbdd6dSMilanka Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
94952cbdd6dSMilanka Ringwald                     hfp_finalize_connection_context(hfp_connection);
950*19976d6bSMilanka Ringwald                     break;
95152cbdd6dSMilanka Ringwald                 } else if (hfp_connection->release_slc_connection == 1){
95252cbdd6dSMilanka Ringwald                     hfp_connection->release_slc_connection = 0;
95352cbdd6dSMilanka Ringwald                     hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
95452cbdd6dSMilanka Ringwald                     rfcomm_disconnect(hfp_connection->acl_handle);
95552cbdd6dSMilanka Ringwald                 }
95652cbdd6dSMilanka Ringwald             }
95748e6eeeeSMatthias Ringwald 
95848e6eeeeSMatthias Ringwald             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
95948e6eeeeSMatthias Ringwald                 // RFCOMM already closed -> remote power off
96048e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
96148e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
96248e6eeeeSMatthias Ringwald #else
96348e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
96448e6eeeeSMatthias Ringwald #endif
96548e6eeeeSMatthias Ringwald                 break;
96648e6eeeeSMatthias Ringwald             }
9673deb3ec6SMatthias Ringwald             break;
9683deb3ec6SMatthias Ringwald 
969fc64f94aSMatthias Ringwald         default:
9703deb3ec6SMatthias Ringwald             break;
9713deb3ec6SMatthias Ringwald     }
9723deb3ec6SMatthias Ringwald }
9733deb3ec6SMatthias Ringwald 
9743721a235SMatthias Ringwald 
97527950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
9765575558eSMilanka Ringwald     UNUSED(packet_type);
97727950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
9785575558eSMilanka Ringwald     UNUSED(size);
97927950165SMatthias Ringwald 
98027950165SMatthias Ringwald     bd_addr_t event_addr;
98127950165SMatthias Ringwald     uint16_t rfcomm_cid;
98227950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
98327950165SMatthias Ringwald     uint8_t status;
98427950165SMatthias Ringwald 
98527950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
98627950165SMatthias Ringwald 
98727950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
98827950165SMatthias Ringwald 
98927950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
99027950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
99127950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
9924eb3f1d8SMilanka Ringwald             hfp_connection = hfp_create_connection(event_addr, local_role);
99327950165SMatthias Ringwald             if (!hfp_connection){
99427950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
99527950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
99627950165SMatthias Ringwald                 return;
99727950165SMatthias Ringwald             }
99827950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
9994960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
10004960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
100127950165SMatthias Ringwald                 return;
100227950165SMatthias Ringwald             }
100327950165SMatthias Ringwald 
100427950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
100527950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
100627950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
100727950165SMatthias Ringwald             break;
100827950165SMatthias Ringwald 
100927950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
101027950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
101127950165SMatthias Ringwald 
1012405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
10136504a03aSMilanka Ringwald             btstack_assert(hfp_connection != NULL);
101427950165SMatthias Ringwald 
10156504a03aSMilanka Ringwald             if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){
10166504a03aSMilanka Ringwald                 break;
10176504a03aSMilanka Ringwald             }
10186504a03aSMilanka Ringwald 
10196504a03aSMilanka Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
10206504a03aSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS) {
102127950165SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
102248e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
10236504a03aSMilanka Ringwald                 break;
10246504a03aSMilanka Ringwald             }
10256504a03aSMilanka Ringwald 
102627950165SMatthias Ringwald             hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
102727950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
10286504a03aSMilanka Ringwald             hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
102927950165SMatthias Ringwald             bd_addr_copy(hfp_connection->remote_addr, event_addr);
103027950165SMatthias Ringwald             hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
10316504a03aSMilanka Ringwald 
103227950165SMatthias Ringwald             rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
103327950165SMatthias Ringwald             break;
103427950165SMatthias Ringwald 
103527950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
103627950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
103727950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
103827950165SMatthias Ringwald             if (!hfp_connection) break;
103952cbdd6dSMilanka Ringwald             switch (hfp_connection->state){
104052cbdd6dSMilanka Ringwald                 case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART:
104152cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
104227950165SMatthias Ringwald                     hfp_connection->state = HFP_IDLE;
104327950165SMatthias Ringwald                     hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
104427950165SMatthias Ringwald                     break;
104552cbdd6dSMilanka Ringwald 
104652cbdd6dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
104748e6eeeeSMatthias Ringwald                     // service connection was released, this implicitly releases audio connection as well
104848e6eeeeSMatthias Ringwald                     hfp_connection->release_audio_connection = 0;
104952cbdd6dSMilanka Ringwald                     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
105048e6eeeeSMatthias Ringwald                     hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
105152cbdd6dSMilanka Ringwald                     gap_disconnect(hfp_connection->sco_handle);
105252cbdd6dSMilanka Ringwald                     break;
105352cbdd6dSMilanka Ringwald 
105452cbdd6dSMilanka Ringwald                 default:
105548e6eeeeSMatthias Ringwald                     // regular case
105611b091bfSMilanka Ringwald                     hfp_reset_voice_recognition(hfp_connection);
105748e6eeeeSMatthias Ringwald                     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
105848e6eeeeSMatthias Ringwald                     hfp_finalize_connection_context(hfp_connection);
105952cbdd6dSMilanka Ringwald                     break;
106048e6eeeeSMatthias Ringwald             }
106127950165SMatthias Ringwald             break;
106227950165SMatthias Ringwald 
106327950165SMatthias Ringwald         default:
106427950165SMatthias Ringwald             break;
106527950165SMatthias Ringwald     }
106627950165SMatthias Ringwald }
1067aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
106894a27792SMatthias Ringwald 
106994a27792SMatthias Ringwald typedef struct {
107094a27792SMatthias Ringwald     const char * command;
107194a27792SMatthias Ringwald     hfp_command_t command_id;
107294a27792SMatthias Ringwald } hfp_command_entry_t;
107394a27792SMatthias Ringwald 
107494a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
1075cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
1076cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
1077cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
1078cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
1079cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
108094a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
108194a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
108294a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
1083051f797dSMilanka Ringwald     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
1084cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
1085cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
108694a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
108794a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
10889ed286f3SMilanka Ringwald     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
1089cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
109094a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
109194a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1092cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
109394a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
109494a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
1095cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
1096cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
1097cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
1098cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
1099cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
110094a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
110194a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1102cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
1103cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
1104cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
1105d8656ee6SMilanka Ringwald     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
110694a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
110794a27792SMatthias Ringwald };
110894a27792SMatthias Ringwald 
110994a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
1110cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
111194a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1112cb33905eSMatthias Ringwald     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
1113cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1114cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
111594a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1116cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1117cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
111894a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1119cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
112084a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1121cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1122cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1123cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1124cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1125cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1126cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1127ce284ffbSMatthias Ringwald     { "+VGM=",  HFP_CMD_SET_MICROPHONE_GAIN },
11284c30d2d1SMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1129ce284ffbSMatthias Ringwald     { "+VGS=",  HFP_CMD_SET_SPEAKER_GAIN},
1130cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
1131cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1132cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
1133cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
113494a27792SMatthias Ringwald };
113594a27792SMatthias Ringwald 
1136aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
11373deb3ec6SMatthias Ringwald 
1138cb33905eSMatthias Ringwald     // table lookup based on role
113994a27792SMatthias Ringwald     uint16_t num_entries;
114094a27792SMatthias Ringwald     hfp_command_entry_t * table;
114194a27792SMatthias Ringwald     if (isHandsFree == 0){
114294a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
114394a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
11443deb3ec6SMatthias Ringwald     } else {
114594a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
114694a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
114794a27792SMatthias Ringwald     }
1148791f0d0aSMatthias Ringwald     // binary search
1149791f0d0aSMatthias Ringwald     uint16_t left = 0;
1150791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
1151791f0d0aSMatthias Ringwald     while (left <= right){
1152791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
1153791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
115494a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
1155791f0d0aSMatthias Ringwald         if (match < 0){
1156791f0d0aSMatthias Ringwald             // search term is lower than middle element
115747005182SMilanka Ringwald             if (right == 0) break;
1158791f0d0aSMatthias Ringwald             right = middle - 1;
1159791f0d0aSMatthias Ringwald         } else if (match == 0){
116094a27792SMatthias Ringwald             return entry->command_id;
1161791f0d0aSMatthias Ringwald         } else {
1162791f0d0aSMatthias Ringwald             // search term is higher than middle element
1163791f0d0aSMatthias Ringwald             left = middle + 1;
116494a27792SMatthias Ringwald         }
116594a27792SMatthias Ringwald     }
116694a27792SMatthias Ringwald 
1167cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
11680222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1169cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1170cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1171cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
11723deb3ec6SMatthias Ringwald     }
11733deb3ec6SMatthias Ringwald 
1174cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1175cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1176aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
11773deb3ec6SMatthias Ringwald     }
11783deb3ec6SMatthias Ringwald 
1179cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1180aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1181aa4dd815SMatthias Ringwald     }
11823deb3ec6SMatthias Ringwald 
1183aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
11843deb3ec6SMatthias Ringwald }
11853deb3ec6SMatthias Ringwald 
1186a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
118751aa5d5aSMilanka Ringwald     if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return;
1188a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1189a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
11903deb3ec6SMatthias Ringwald }
1191a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1192a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
11933deb3ec6SMatthias Ringwald }
11943deb3ec6SMatthias Ringwald 
11953deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1196c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
11973deb3ec6SMatthias Ringwald }
11983deb3ec6SMatthias Ringwald 
119994a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1200a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
120151aa5d5aSMilanka Ringwald     // hfp_connection->line_buffer[0] = 0;
12023deb3ec6SMatthias Ringwald }
12031d81c7feSMatthias Ringwald 
12041d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
12051d81c7feSMatthias Ringwald     switch (byte){
12061d81c7feSMatthias Ringwald         case ',':
1207f8301d46SMatthias Ringwald 		case '-':
12081d81c7feSMatthias Ringwald         case ';':
12091d81c7feSMatthias Ringwald         case '(':
12101d81c7feSMatthias Ringwald         case ')':
12111d81c7feSMatthias Ringwald         case '\n':
12121d81c7feSMatthias Ringwald         case '\r':
12133deb3ec6SMatthias Ringwald             break;
12143deb3ec6SMatthias Ringwald         default:
12151d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
12163deb3ec6SMatthias Ringwald             break;
12173deb3ec6SMatthias Ringwald     }
12183deb3ec6SMatthias Ringwald }
12191d81c7feSMatthias Ringwald 
12201d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
12211d81c7feSMatthias Ringwald     switch (byte){
12221d81c7feSMatthias Ringwald         case ',':
1223f8301d46SMatthias Ringwald 		case '-':
12241d81c7feSMatthias Ringwald         case ';':
12251d81c7feSMatthias Ringwald         case '\n':
12261d81c7feSMatthias Ringwald         case '\r':
12271d81c7feSMatthias Ringwald             return true;
1228dd533208SMatthias Ringwald         default:
12291d81c7feSMatthias Ringwald             return false;
12303deb3ec6SMatthias Ringwald     }
12313deb3ec6SMatthias Ringwald }
12323deb3ec6SMatthias Ringwald 
1233d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1234aa4dd815SMatthias Ringwald 
12351dddc4f4SMatthias Ringwald     // handle doubles quotes
12361dddc4f4SMatthias Ringwald     if (byte == '"'){
12371dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1238d6b237acSMatthias Ringwald         return true;
12391dddc4f4SMatthias Ringwald     }
12401dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
12411dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1242d6b237acSMatthias Ringwald         return true;
12431dddc4f4SMatthias Ringwald     }
12443deb3ec6SMatthias Ringwald 
12451dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1246d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
12471dddc4f4SMatthias Ringwald 
1248bfeaf344SMatthias Ringwald     bool processed = true;
1249bfeaf344SMatthias Ringwald 
1250a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1251dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1252d28b2d5dSMilanka Ringwald             switch (byte) {
1253dd533208SMatthias Ringwald                 case '\n':
1254dd533208SMatthias Ringwald                 case '\r':
1255dd533208SMatthias Ringwald                 case ';':
1256bfeaf344SMatthias Ringwald                     // ignore separator
1257bfeaf344SMatthias Ringwald                     break;
1258bfeaf344SMatthias Ringwald                 case ':':
1259bfeaf344SMatthias Ringwald                 case '?':
1260bfeaf344SMatthias Ringwald                     // store separator
1261bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1262dd533208SMatthias Ringwald                     break;
1263d28b2d5dSMilanka Ringwald                 case '=':
1264bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1265dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1266a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1267d6b237acSMatthias Ringwald                     return true;
1268d28b2d5dSMilanka Ringwald                 default:
1269bfeaf344SMatthias Ringwald                     // store if not lookahead
1270d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1271dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1272d6b237acSMatthias Ringwald                         return true;
1273dd533208SMatthias Ringwald                     }
1274bfeaf344SMatthias Ringwald                     // mark as lookahead
1275bfeaf344SMatthias Ringwald                     processed = false;
1276d7f16ff1SMatthias Ringwald                     break;
1277d7f16ff1SMatthias Ringwald             }
1278ce263fc8SMatthias Ringwald 
12791d81c7feSMatthias Ringwald             // ignore empty tokens
1280d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1281dd533208SMatthias Ringwald 
1282bfeaf344SMatthias Ringwald             // parse
1283dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1284aa4dd815SMatthias Ringwald 
128584a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
128684a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1287a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1288aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1289a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1290aa4dd815SMatthias Ringwald                         break;
1291aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1292a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1293aa4dd815SMatthias Ringwald                         break;
1294aa4dd815SMatthias Ringwald                     default:
129584a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1296aa4dd815SMatthias Ringwald                         break;
1297aa4dd815SMatthias Ringwald                 }
1298aa4dd815SMatthias Ringwald             }
12993deb3ec6SMatthias Ringwald 
1300c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1301c0c0437aSMatthias Ringwald 
1302bfeaf344SMatthias Ringwald             // next state
1303bfeaf344SMatthias Ringwald             hfp_connection->found_equal_sign = false;
13041d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1305dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1306dd533208SMatthias Ringwald 
1307bfeaf344SMatthias Ringwald             return processed;
1308dd533208SMatthias Ringwald 
1309bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
13101d81c7feSMatthias Ringwald             // handle empty fields
13111d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1312dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1313dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1314dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
13153d051a53SMatthias Ringwald                 return true;
13163d051a53SMatthias Ringwald             }
13173d051a53SMatthias Ringwald 
13181d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
13191d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
13201d81c7feSMatthias Ringwald 
13211d81c7feSMatthias Ringwald             // ignore empty tokens
132289e7c136SMilanka Ringwald             switch (hfp_connection->command){
132389e7c136SMilanka Ringwald                 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
132489e7c136SMilanka Ringwald                     // don't ignore empty string
132589e7c136SMilanka Ringwald                     break;
132689e7c136SMilanka Ringwald                 default:
132789e7c136SMilanka Ringwald                     if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) {
132889e7c136SMilanka Ringwald                         return true;
132989e7c136SMilanka Ringwald                     }
133089e7c136SMilanka Ringwald                     break;
133189e7c136SMilanka Ringwald             }
13323d051a53SMatthias Ringwald 
13333d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
13341d81c7feSMatthias Ringwald 
13351d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
13361d81c7feSMatthias Ringwald 
13371d81c7feSMatthias Ringwald             switch (hfp_connection->command){
13381d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
13391d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
13401d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
13411d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13421d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
13431d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
13441d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
13451d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
13461d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
13471d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
13481d81c7feSMatthias Ringwald                     break;
13491d81c7feSMatthias Ringwald                 default:
13501d81c7feSMatthias Ringwald                     break;
13511d81c7feSMatthias Ringwald             }
13523d051a53SMatthias Ringwald             return true;
13533d051a53SMatthias Ringwald 
1354ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
13551d81c7feSMatthias Ringwald 
13561d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
13571d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1358dd533208SMatthias Ringwald 
1359a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1360ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1361a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
13622308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1363ce263fc8SMatthias Ringwald                     break;
1364ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1365a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
13662308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1367ce263fc8SMatthias Ringwald                     break;
1368ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1369ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1370ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
13712308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1372ce263fc8SMatthias Ringwald                     break;
1373ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13742308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1375a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1376a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1377ce263fc8SMatthias Ringwald                     break;
1378ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
13792308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1380a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1381ce263fc8SMatthias Ringwald                     break;
1382ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1383a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1384a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
13852308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1386ce263fc8SMatthias Ringwald                     break;
13870222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
13880222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13890222a807SMatthias Ringwald                     break;
1390ce263fc8SMatthias Ringwald                 default:
1391ce263fc8SMatthias Ringwald                     break;
1392ce263fc8SMatthias Ringwald             }
13931d81c7feSMatthias Ringwald 
13941d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
13951d81c7feSMatthias Ringwald 
13961d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
13971d81c7feSMatthias Ringwald 
1398ba0de059SMatthias Ringwald             return true;
1399ce263fc8SMatthias Ringwald 
140074c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
14011d81c7feSMatthias Ringwald 
14021d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
14031d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
14041d81c7feSMatthias Ringwald 
1405a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1406ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
140789425bfcSMilanka Ringwald                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
140889425bfcSMilanka Ringwald                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1409a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1410ce263fc8SMatthias Ringwald                     break;
1411ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
14122308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
141325789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
141425789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1415a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1416ce263fc8SMatthias Ringwald                     break;
1417ce263fc8SMatthias Ringwald                 default:
1418ce263fc8SMatthias Ringwald                     break;
1419ce263fc8SMatthias Ringwald             }
14201d81c7feSMatthias Ringwald 
14211d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
14221d81c7feSMatthias Ringwald 
14231d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
14241d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
14251d81c7feSMatthias Ringwald             } else {
14261d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
14271d81c7feSMatthias Ringwald             }
1428d6b237acSMatthias Ringwald             return true;
142974c7548aSMatthias Ringwald 
143074c7548aSMatthias Ringwald         default:
143174c7548aSMatthias Ringwald             btstack_assert(false);
143274c7548aSMatthias Ringwald             return true;
143374c7548aSMatthias Ringwald     }
1434d6b237acSMatthias Ringwald }
1435d6b237acSMatthias Ringwald 
1436d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1437d6b237acSMatthias Ringwald     bool processed = false;
1438d6b237acSMatthias Ringwald     while (!processed){
1439d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1440d6b237acSMatthias Ringwald     }
14411d81c7feSMatthias Ringwald     // reset parser state on end-of-line
14421d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
14431d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
14441d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
14451d81c7feSMatthias Ringwald     }
1446ce263fc8SMatthias Ringwald }
1447ce263fc8SMatthias Ringwald 
1448a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1449ce263fc8SMatthias Ringwald     int value;
1450a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1451667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
14522308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1453667ec068SMatthias Ringwald             int i;
1454a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1455667ec068SMatthias Ringwald                 case 0:
1456a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1457a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1458a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1459667ec068SMatthias Ringwald                             break;
1460667ec068SMatthias Ringwald                         }
1461667ec068SMatthias Ringwald                     }
1462667ec068SMatthias Ringwald                     break;
1463667ec068SMatthias Ringwald                 case 1:
1464a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1465a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1466667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1467a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1468667ec068SMatthias Ringwald                     break;
1469667ec068SMatthias Ringwald                 default:
1470667ec068SMatthias Ringwald                     break;
1471667ec068SMatthias Ringwald             }
147225789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1473667ec068SMatthias Ringwald             break;
1474667ec068SMatthias Ringwald 
1475667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1476a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1477667ec068SMatthias Ringwald                 case 0:
147828a4aea3SMilanka Ringwald                     // <alpha>: This optional field is not supported, and shall be left blank.
147928a4aea3SMilanka Ringwald                     break;
148028a4aea3SMilanka Ringwald                 case 1:
148128a4aea3SMilanka Ringwald                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1482a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1483a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1484667ec068SMatthias Ringwald                     break;
148528a4aea3SMilanka Ringwald                 case 2:
148628a4aea3SMilanka Ringwald                     /*
148728a4aea3SMilanka Ringwald                       <type> field specifies the format of the phone number provided, and can be one of the following values:
148828a4aea3SMilanka 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.
148928a4aea3SMilanka 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.
149028a4aea3SMilanka Ringwald                      - values 160-175: National number. No prefix nor escape digits included.
149128a4aea3SMilanka Ringwald                      */
14922308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1493a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1494667ec068SMatthias Ringwald                     break;
149528a4aea3SMilanka Ringwald                 case 3:
149628a4aea3SMilanka Ringwald                     // <speed>: This optional field is not supported, and shall be left blank.
149728a4aea3SMilanka Ringwald                     break;
149828a4aea3SMilanka Ringwald                 case 4:
149928a4aea3SMilanka Ringwald                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1500667ec068SMatthias Ringwald                 default:
1501667ec068SMatthias Ringwald                     break;
1502667ec068SMatthias Ringwald             }
1503eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1504a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1505667ec068SMatthias Ringwald             break;
1506667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1507a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1508667ec068SMatthias Ringwald                 case 0:
15092308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1510a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1511667ec068SMatthias Ringwald                     break;
1512667ec068SMatthias Ringwald                 case 1:
15132308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1514a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1515667ec068SMatthias Ringwald                     break;
1516667ec068SMatthias Ringwald                 case 2:
15172308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1518a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1519667ec068SMatthias Ringwald                     break;
1520667ec068SMatthias Ringwald                 case 3:
15212308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15220aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1523667ec068SMatthias Ringwald                     break;
1524667ec068SMatthias Ringwald                 case 4:
15250aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15260aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
15270aee97efSMilanka Ringwald                     break;
15280aee97efSMilanka Ringwald                 case 5:
1529a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1530a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1531667ec068SMatthias Ringwald                     break;
15320aee97efSMilanka Ringwald                 case 6:
15332308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1534a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1535667ec068SMatthias Ringwald                     break;
1536667ec068SMatthias Ringwald                 default:
1537667ec068SMatthias Ringwald                     break;
1538667ec068SMatthias Ringwald             }
1539eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1540a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1541667ec068SMatthias Ringwald             break;
1542aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
15432308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1544a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1545aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1546aa4dd815SMatthias Ringwald             break;
1547aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
15482308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1549a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1550aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1551aa4dd815SMatthias Ringwald             break;
1552aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
15532308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1554a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1555aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1556aa4dd815SMatthias Ringwald             break;
1557aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
15582308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1559a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1560aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1561aa4dd815SMatthias Ringwald             break;
15623deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
15632308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1564a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
15653deb3ec6SMatthias Ringwald             break;
15663deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
15672308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1568a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
15693deb3ec6SMatthias Ringwald             break;
15703deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
15712308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1572bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
15733deb3ec6SMatthias Ringwald             break;
15743deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1575a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
15762308e108SMilanka Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
157725789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1578a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
15793deb3ec6SMatthias Ringwald             break;
1580aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
158189425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
158289425bfcSMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1583a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1584a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1585aa4dd815SMatthias Ringwald             break;
1586aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1587a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
15882308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
158925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
15903deb3ec6SMatthias Ringwald             break;
15913deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
159225789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1593a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1594a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
15952308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1596a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
15973deb3ec6SMatthias Ringwald             break;
15983deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1599a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1600a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
160197d2cfbcSMatthias 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);
160225789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1603eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
16043deb3ec6SMatthias Ringwald             break;
1605aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1606aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1607a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
16082308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
160925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1610a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
16113deb3ec6SMatthias Ringwald             break;
1612aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
16133deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1614a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
161525789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
16163deb3ec6SMatthias Ringwald             break;
1617ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
161825789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1619a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1620ce263fc8SMatthias Ringwald             break;
16213deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
16223deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1623a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1624a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1625a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1626a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1627ce263fc8SMatthias Ringwald             }
1628a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1629ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1630a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1631ce263fc8SMatthias Ringwald             } else {
16322308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1633a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1634a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1635a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
16363deb3ec6SMatthias Ringwald             }
163725789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
16383deb3ec6SMatthias Ringwald             break;
16393deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
16403deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
164125789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1642a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
16433deb3ec6SMatthias Ringwald             break;
1644aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
16452308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1646a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1647aa4dd815SMatthias Ringwald             break;
1648aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1649a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1650a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
16513deb3ec6SMatthias Ringwald                 break;
16523deb3ec6SMatthias Ringwald             }
16533deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1654a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
16553deb3ec6SMatthias Ringwald             break;
16563deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
16573deb3ec6SMatthias Ringwald             break;
16583deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1659a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
16602308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
16613deb3ec6SMatthias Ringwald             break;
16623deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
16632308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1664a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1665a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
16663deb3ec6SMatthias Ringwald             break;
1667ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1668a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1669a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1670a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1671a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
16723deb3ec6SMatthias Ringwald             break;
16730222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
16740222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
16750222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
16760222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
16770222a807SMatthias Ringwald             }
16780222a807SMatthias Ringwald             break;
16790222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
16800222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
16810222a807SMatthias Ringwald             break;
16820222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
16830222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
16840222a807SMatthias Ringwald             break;
16850222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
16860222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
16870222a807SMatthias Ringwald             break;
16880222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
16890222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
16900222a807SMatthias Ringwald             break;
16910b4debbfSMilanka Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
16920b4debbfSMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
16930b4debbfSMilanka Ringwald             hfp_connection->ag_activate_voice_recognition_value = value;
16940b4debbfSMilanka Ringwald             break;
1695db3cdbd4SMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1696db3cdbd4SMilanka Ringwald             switch(hfp_connection->parser_item_index){
1697db3cdbd4SMilanka Ringwald                 case 0:
16980b4debbfSMilanka Ringwald                     hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1699db3cdbd4SMilanka Ringwald                     break;
1700db3cdbd4SMilanka Ringwald                 case 1:
1701c78e7b73SMatthias Ringwald                     hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1702db3cdbd4SMilanka Ringwald                     break;
1703db3cdbd4SMilanka Ringwald                 case 2:
170445796ff1SMilanka Ringwald                     hfp_connection->ag_msg.text_id = 0;
1705db3cdbd4SMilanka Ringwald                     for (i = 0 ; i < 4; i++){
170645796ff1SMilanka Ringwald                         hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]);
1707db3cdbd4SMilanka Ringwald                     }
1708db3cdbd4SMilanka Ringwald                     break;
1709db3cdbd4SMilanka Ringwald                 case 3:
17100d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1711db3cdbd4SMilanka Ringwald                     break;
1712db3cdbd4SMilanka Ringwald                 case 4:
17130d854c7cSMilanka Ringwald                     hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1714db3cdbd4SMilanka Ringwald                     break;
1715db3cdbd4SMilanka Ringwald                 case 5:
171651aa5d5aSMilanka Ringwald                     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
171751aa5d5aSMilanka Ringwald                     hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1;
1718db3cdbd4SMilanka Ringwald                     break;
1719db3cdbd4SMilanka Ringwald                 default:
1720db3cdbd4SMilanka Ringwald                     break;
1721db3cdbd4SMilanka Ringwald             }
1722ddbf29d2SMilanka Ringwald             hfp_connection->parser_item_index++;
1723db3cdbd4SMilanka Ringwald             break;
17243deb3ec6SMatthias Ringwald         default:
17253deb3ec6SMatthias Ringwald             break;
17263deb3ec6SMatthias Ringwald     }
17273deb3ec6SMatthias Ringwald }
17283deb3ec6SMatthias Ringwald 
17291d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
17301d9c9c90SMilanka Ringwald     UNUSED(context);
17311d9c9c90SMilanka Ringwald 
17321d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
17331d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
17341d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
17351d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
17361d9c9c90SMilanka Ringwald 
17371d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
17381d9c9c90SMilanka Ringwald 
17391d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1740aeb0f0feSMatthias Ringwald         hfp_sdp_query_context.local_role = connection->local_role;
1741aeb0f0feSMatthias Ringwald         (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6);
1742e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
17431d9c9c90SMilanka Ringwald         return;
17441d9c9c90SMilanka Ringwald     }
17451d9c9c90SMilanka Ringwald }
17461d9c9c90SMilanka Ringwald 
17474eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
17484eb3f1d8SMilanka Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
17494eb3f1d8SMilanka Ringwald     if (connection){
17504eb3f1d8SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
17513deb3ec6SMatthias Ringwald     }
17524eb3f1d8SMilanka Ringwald 
17534eb3f1d8SMilanka Ringwald     connection = hfp_create_connection(bd_addr, local_role);
17544eb3f1d8SMilanka Ringwald     if (!connection){
17554eb3f1d8SMilanka Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
17564eb3f1d8SMilanka Ringwald     }
17574eb3f1d8SMilanka Ringwald 
17584eb3f1d8SMilanka Ringwald     connection->state = HFP_W2_SEND_SDP_QUERY;
17594eb3f1d8SMilanka Ringwald 
17604eb3f1d8SMilanka Ringwald     bd_addr_copy(connection->remote_addr, bd_addr);
17614eb3f1d8SMilanka Ringwald     connection->service_uuid = service_uuid;
17621d9c9c90SMilanka Ringwald 
1763aeb0f0feSMatthias Ringwald     hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query;
17641d9c9c90SMilanka Ringwald     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
1765aeb0f0feSMatthias Ringwald     (void) sdp_client_register_query_callback(&hfp_sdp_query_request);
17664eb3f1d8SMilanka Ringwald     return ERROR_CODE_SUCCESS;
17673deb3ec6SMatthias Ringwald }
17683deb3ec6SMatthias Ringwald 
1769657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){
17701ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1771657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
17723deb3ec6SMatthias Ringwald 
1773657bc59fSMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
1774657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1775657bc59fSMilanka Ringwald         hfp_connection->state = HFP_IDLE;
17763deb3ec6SMatthias Ringwald         return;
17773deb3ec6SMatthias Ringwald     }
17783deb3ec6SMatthias Ringwald 
1779657bc59fSMilanka Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1780657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
17813deb3ec6SMatthias Ringwald         return;
17823deb3ec6SMatthias Ringwald     }
178352cbdd6dSMilanka Ringwald     hfp_connection->release_slc_connection = 1;
1784657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1785657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
17860c3047fbSMatthias Ringwald         return;
17870c3047fbSMatthias Ringwald     }
17880c3047fbSMatthias Ringwald 
1789657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1790657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
179152cbdd6dSMilanka Ringwald         hfp_connection->release_audio_connection = 1;
17920c3047fbSMatthias Ringwald         return;
17930c3047fbSMatthias Ringwald     }
17943deb3ec6SMatthias Ringwald }
17953deb3ec6SMatthias Ringwald 
17960b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){
17971ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1798657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
17990b4debbfSMilanka Ringwald     if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
18000b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18011ffa0dd9SMilanka Ringwald     }
18020b4debbfSMilanka Ringwald     switch (hfp_connection->state) {
18030b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
18040b4debbfSMilanka Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
18050b4debbfSMilanka Ringwald             break;
18060b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
18070b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
18080b4debbfSMilanka Ringwald             hfp_connection->release_audio_connection = 1;
18090b4debbfSMilanka Ringwald             break;
18100b4debbfSMilanka Ringwald         default:
18110b4debbfSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
18120b4debbfSMilanka Ringwald     }
18130b4debbfSMilanka Ringwald     return ERROR_CODE_SUCCESS;
18143deb3ec6SMatthias Ringwald }
18153deb3ec6SMatthias Ringwald 
1816ce263fc8SMatthias Ringwald static const struct link_settings {
1817ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1818ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1819ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1820ebc6f15bSMatthias Ringwald     const bool     eSCO;
1821ebc6f15bSMatthias Ringwald     const uint8_t  codec;
1822ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1823352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1824352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1825352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1826352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1827352a0504SMatthias Ringwald     { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1828352a0504SMatthias Ringwald     { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1829352a0504SMatthias Ringwald     { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1830352a0504SMatthias Ringwald     { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1831ce263fc8SMatthias Ringwald };
18323deb3ec6SMatthias Ringwald 
1833eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1834ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1835eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1836ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1837aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = hfp_connection;
1838d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1839d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1840689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1841689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1842689d4323SMatthias Ringwald #else
1843689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1844689d4323SMatthias Ringwald #endif
1845d2c1d59aSMatthias Ringwald     }
1846352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1847352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1848eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1849352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1850ce263fc8SMatthias Ringwald }
1851d68dcce1SMatthias Ringwald 
1852cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){
1853cb81d35dSMatthias Ringwald 
1854cb81d35dSMatthias Ringwald     // remote supported feature eSCO is set if link type is eSCO
1855cb81d35dSMatthias Ringwald     // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
1856cb81d35dSMatthias Ringwald     uint16_t max_latency;
1857cb81d35dSMatthias Ringwald     uint8_t  retransmission_effort;
1858cb81d35dSMatthias Ringwald     uint16_t packet_types;
1859cb81d35dSMatthias Ringwald 
1860cb81d35dSMatthias Ringwald     if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
1861cb81d35dSMatthias Ringwald         max_latency = 0x000c;
1862cb81d35dSMatthias Ringwald         retransmission_effort = 0x02;
1863cb81d35dSMatthias Ringwald         // eSCO: EV3 and 2-EV3
1864cb81d35dSMatthias Ringwald         packet_types = 0x0048;
1865cb81d35dSMatthias Ringwald     } else {
1866cb81d35dSMatthias Ringwald         max_latency = 0xffff;
1867cb81d35dSMatthias Ringwald         retransmission_effort = 0xff;
1868cb81d35dSMatthias Ringwald         // sco: HV1 and HV3
1869cb81d35dSMatthias Ringwald         packet_types = 0x005;
1870cb81d35dSMatthias Ringwald     }
1871cb81d35dSMatthias Ringwald 
1872cb81d35dSMatthias Ringwald     // mSBC only allows for transparent data
1873cb81d35dSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1874cb81d35dSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1875cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1876cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1877cb81d35dSMatthias Ringwald #else
1878cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1879cb81d35dSMatthias Ringwald #endif
1880cb81d35dSMatthias Ringwald     }
1881cb81d35dSMatthias Ringwald 
1882cb81d35dSMatthias Ringwald     // filter packet types
1883cb81d35dSMatthias Ringwald     packet_types &= hfp_get_sco_packet_types();
1884cb81d35dSMatthias Ringwald 
1885cb81d35dSMatthias Ringwald     // bits 6-9 are 'don't allow'
1886cb81d35dSMatthias Ringwald     packet_types ^= 0x3c0;
1887cb81d35dSMatthias Ringwald 
1888cb81d35dSMatthias Ringwald     log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
1889cb81d35dSMatthias Ringwald     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
1890cb81d35dSMatthias Ringwald                  sco_voice_setting, retransmission_effort, packet_types);
1891cb81d35dSMatthias Ringwald }
1892cb81d35dSMatthias Ringwald 
18933721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
18943721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
18953721a235SMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
18963721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
18973721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
18983721a235SMatthias Ringwald     }
18993721a235SMatthias Ringwald }
19003721a235SMatthias Ringwald 
19013721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
19023721a235SMatthias Ringwald     uint32_t sample_rate_hz;
19033721a235SMatthias Ringwald     uint16_t clock_rate_khz;
19043721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
19053721a235SMatthias Ringwald         clock_rate_khz = 512;
19063721a235SMatthias Ringwald         sample_rate_hz = 16000;
19073721a235SMatthias Ringwald     } else {
19083721a235SMatthias Ringwald         clock_rate_khz = 256;
19093721a235SMatthias Ringwald         sample_rate_hz = 8000;
19103721a235SMatthias Ringwald     }
19113721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
19123721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
19133721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
19143721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
19153721a235SMatthias Ringwald     uint8_t  reserved = 0;
19163721a235SMatthias Ringwald     uint16_t size = 16;
19173721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
19183721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
19193721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
19203721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
19213721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
19223721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
19233721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
19243721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
19253721a235SMatthias Ringwald }
19263721a235SMatthias Ringwald #endif
19273721a235SMatthias Ringwald 
1928689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1929689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1930689d4323SMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1931689d4323SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1932689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
1933689d4323SMatthias Ringwald     }
1934689d4323SMatthias Ringwald }
1935689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1936689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
19371d2bbd54SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 512 kHz
19381d2bbd54SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
1939689d4323SMatthias Ringwald }
1940689d4323SMatthias Ringwald #endif
1941689d4323SMatthias Ringwald 
19421b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
19431b005648SMatthias Ringwald     hfp_hf_callback = callback;
19441b005648SMatthias Ringwald }
19451b005648SMatthias Ringwald 
19461b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
19471b005648SMatthias Ringwald     hfp_ag_callback = callback;
19481b005648SMatthias Ringwald }
19491b005648SMatthias Ringwald 
1950ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1951ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
1952ca59be51SMatthias Ringwald }
19531b005648SMatthias Ringwald 
1954ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1955ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
1956d68dcce1SMatthias Ringwald }
1957d68dcce1SMatthias Ringwald 
1958520c92d5SMatthias Ringwald void hfp_init(void){
1959352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
1960991c26beSMatthias Ringwald }
1961991c26beSMatthias Ringwald 
196220b2edb6SMatthias Ringwald void hfp_deinit(void){
1963aeb0f0feSMatthias Ringwald     hfp_allowed_sco_packet_types = 0;
196420b2edb6SMatthias Ringwald     hfp_connections = NULL;
196520b2edb6SMatthias Ringwald     hfp_hf_callback = NULL;
196620b2edb6SMatthias Ringwald     hfp_ag_callback = NULL;
196720b2edb6SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = NULL;
196820b2edb6SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = NULL;
1969aeb0f0feSMatthias Ringwald     hfp_sco_establishment_active = NULL;
1970aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
1971aeb0f0feSMatthias Ringwald     (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t));
197220b2edb6SMatthias Ringwald }
197320b2edb6SMatthias Ringwald 
1974991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
197501e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
1976991c26beSMatthias Ringwald }
1977991c26beSMatthias Ringwald 
1978991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
197901e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
1980520c92d5SMatthias Ringwald }
1981186dd3d2SMatthias Ringwald 
1982e453c1d9SMatthias 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){
1983e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
1984e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
1985e453c1d9SMatthias Ringwald     while (setting > 0){
1986e453c1d9SMatthias Ringwald         setting--;
1987e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
1988e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
1989e453c1d9SMatthias Ringwald         // skip if S4 but not supported
1990e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
1991e453c1d9SMatthias Ringwald         // skip wrong codec
1992e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
199301e5b727SMatthias Ringwald         // skip disabled packet types
1994352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
1995352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
1996352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
199701e5b727SMatthias Ringwald 
1998e453c1d9SMatthias Ringwald         // found matching setting
1999e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
2000afac2a04SMilanka Ringwald     }
2001e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
2002afac2a04SMilanka Ringwald }
2003e453c1d9SMatthias Ringwald 
20042b7abbfbSMatthias 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){
2005e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
2006e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
2007e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
20082b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
20092b7abbfbSMatthias Ringwald }
20102b7abbfbSMatthias Ringwald 
20112b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
2012e453c1d9SMatthias Ringwald     // get highest possible link setting
20132b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
2014afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
2015afac2a04SMilanka Ringwald }
2016afac2a04SMilanka Ringwald 
2017186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
2018186dd3d2SMatthias Ringwald 
2019186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
2020186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
2021186dd3d2SMatthias Ringwald     // encode \n\r
2022186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
202315a27967SMatthias Ringwald     int i = 0;
2024186dd3d2SMatthias Ringwald     int pos;
202515a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
2026186dd3d2SMatthias Ringwald         switch (packet[pos]){
2027186dd3d2SMatthias Ringwald             case '\n':
2028186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2029186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
2030186dd3d2SMatthias Ringwald                 break;
2031186dd3d2SMatthias Ringwald             case '\r':
2032186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
2033186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
2034186dd3d2SMatthias Ringwald                 break;
2035186dd3d2SMatthias Ringwald             default:
2036186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
2037186dd3d2SMatthias Ringwald                 break;
2038186dd3d2SMatthias Ringwald         }
2039186dd3d2SMatthias Ringwald     }
2040186dd3d2SMatthias Ringwald     printable[i] = 0;
2041186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
2042186dd3d2SMatthias Ringwald #endif
2043186dd3d2SMatthias Ringwald }
2044