xref: /btstack/src/classic/hfp.c (revision 48e6eeee0f7cdb5532f162e9529f2bd0affc0dd3)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
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>
443deb3ec6SMatthias Ringwald #include <string.h>
453deb3ec6SMatthias Ringwald #include <inttypes.h>
463deb3ec6SMatthias Ringwald 
47235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
48023f2764SMatthias Ringwald #include "btstack_debug.h"
4959c6af15SMatthias Ringwald #include "btstack_event.h"
5059c6af15SMatthias Ringwald #include "btstack_memory.h"
5159c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5259c6af15SMatthias Ringwald #include "classic/core.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"
6059c6af15SMatthias Ringwald #include "l2cap.h"
613deb3ec6SMatthias Ringwald 
623721a235SMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM)
633721a235SMatthias Ringwald #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
643721a235SMatthias Ringwald #endif
653721a235SMatthias Ringwald 
66689d4323SMatthias Ringwald #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM)
67689d4323SMatthias Ringwald #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
68689d4323SMatthias Ringwald #endif
69689d4323SMatthias Ringwald 
703deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
713deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
723deb3ec6SMatthias Ringwald 
7320b2edb6SMatthias Ringwald typedef struct {
7420b2edb6SMatthias Ringwald     hfp_role_t local_role;
7520b2edb6SMatthias Ringwald     bd_addr_t  remote_address;
7620b2edb6SMatthias Ringwald } hfp_sdp_query_context_t;
7720b2edb6SMatthias Ringwald 
7820b2edb6SMatthias Ringwald // globals
7920b2edb6SMatthias Ringwald 
8020b2edb6SMatthias Ringwald static hfp_sdp_query_context_t sdp_query_context;
8120b2edb6SMatthias Ringwald static btstack_context_callback_registration_t hfp_handle_sdp_client_query_request;
8220b2edb6SMatthias Ringwald 
8320b2edb6SMatthias Ringwald static btstack_linked_list_t hfp_connections ;
8420b2edb6SMatthias Ringwald 
8520b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
8620b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
8720b2edb6SMatthias Ringwald 
8820b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
8920b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
9020b2edb6SMatthias Ringwald 
9120b2edb6SMatthias Ringwald static hfp_connection_t * sco_establishment_active;
9220b2edb6SMatthias Ringwald 
9320b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types;
9420b2edb6SMatthias Ringwald 
9520b2edb6SMatthias Ringwald // prototypes
9620b2edb6SMatthias 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);
9720b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
9820b2edb6SMatthias Ringwald 
993deb3ec6SMatthias Ringwald 
1003deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
1013deb3ec6SMatthias Ringwald         "EC and/or NR function",
1023deb3ec6SMatthias Ringwald         "Three-way calling",
1033deb3ec6SMatthias Ringwald         "CLI presentation capability",
1043deb3ec6SMatthias Ringwald         "Voice recognition activation",
1053deb3ec6SMatthias Ringwald         "Remote volume control",
1063deb3ec6SMatthias Ringwald 
1073deb3ec6SMatthias Ringwald         "Enhanced call status",
1083deb3ec6SMatthias Ringwald         "Enhanced call control",
1093deb3ec6SMatthias Ringwald 
1103deb3ec6SMatthias Ringwald         "Codec negotiation",
1113deb3ec6SMatthias Ringwald 
1123deb3ec6SMatthias Ringwald         "HF Indicators",
1133deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1143deb3ec6SMatthias Ringwald         "Reserved for future definition"
1153deb3ec6SMatthias Ringwald };
1163deb3ec6SMatthias Ringwald 
1173deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
1183deb3ec6SMatthias Ringwald         "Three-way calling",
1193deb3ec6SMatthias Ringwald         "EC and/or NR function",
1203deb3ec6SMatthias Ringwald         "Voice recognition function",
1213deb3ec6SMatthias Ringwald         "In-band ring tone capability",
1223deb3ec6SMatthias Ringwald         "Attach a number to a voice tag",
1233deb3ec6SMatthias Ringwald         "Ability to reject a call",
1243deb3ec6SMatthias Ringwald         "Enhanced call status",
1253deb3ec6SMatthias Ringwald         "Enhanced call control",
1263deb3ec6SMatthias Ringwald         "Extended Error Result Codes",
1273deb3ec6SMatthias Ringwald         "Codec negotiation",
1283deb3ec6SMatthias Ringwald         "HF Indicators",
1293deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1303deb3ec6SMatthias Ringwald         "Reserved for future definition"
1313deb3ec6SMatthias Ringwald };
1323deb3ec6SMatthias Ringwald 
1330aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1340aee97efSMilanka Ringwald         "outgoing",
1350aee97efSMilanka Ringwald         "incoming"
1360aee97efSMilanka Ringwald };
1370aee97efSMilanka Ringwald 
1380aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1390aee97efSMilanka Ringwald         "active",
1400aee97efSMilanka Ringwald         "held",
1410aee97efSMilanka Ringwald         "outgoing dialing",
1420aee97efSMilanka Ringwald         "outgoing alerting",
1430aee97efSMilanka Ringwald         "incoming",
1440aee97efSMilanka Ringwald         "incoming waiting",
1450aee97efSMilanka Ringwald         "call held by response and hold"
1460aee97efSMilanka Ringwald };
1470aee97efSMilanka Ringwald 
1480aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1490aee97efSMilanka Ringwald         "voice",
1500aee97efSMilanka Ringwald         "data",
1510aee97efSMilanka Ringwald         "fax"
1520aee97efSMilanka Ringwald };
1530aee97efSMilanka Ringwald 
1540aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1550aee97efSMilanka Ringwald         "not a conference call",
1560aee97efSMilanka Ringwald         "conference call"
1570aee97efSMilanka Ringwald };
1580aee97efSMilanka Ringwald 
1590aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1600aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1610aee97efSMilanka Ringwald     return "not defined";
1620aee97efSMilanka Ringwald }
1630aee97efSMilanka Ringwald 
1640aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1650aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1660aee97efSMilanka Ringwald     return "not defined";
1670aee97efSMilanka Ringwald }
1680aee97efSMilanka Ringwald 
1690aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1700aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1710aee97efSMilanka Ringwald     return "not defined";
1720aee97efSMilanka Ringwald }
1730aee97efSMilanka Ringwald 
1740aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1750aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1760aee97efSMilanka Ringwald     return "not defined";
1770aee97efSMilanka Ringwald }
1780aee97efSMilanka Ringwald 
17925789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
18025789943SMilanka Ringwald     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
18125789943SMilanka Ringwald 
18225789943SMilanka Ringwald     if (index > HFP_MAX_NUM_INDICATORS){
18325789943SMilanka Ringwald         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
18425789943SMilanka Ringwald         return HFP_MAX_NUM_INDICATORS - 1;
18525789943SMilanka Ringwald     }
18625789943SMilanka Ringwald 
18725789943SMilanka Ringwald     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
18825789943SMilanka Ringwald     if (index > 0){
18925789943SMilanka Ringwald         index -= 1;
19025789943SMilanka Ringwald     } else {
19125789943SMilanka Ringwald         log_info("ignoring invalid indicator index 0");
19225789943SMilanka Ringwald         return 0;
19325789943SMilanka Ringwald     }
19425789943SMilanka Ringwald     return index;
19525789943SMilanka Ringwald }
19625789943SMilanka Ringwald 
19725789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
19825789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
19925789943SMilanka Ringwald         hfp_connection->parser_item_index++;
20025789943SMilanka Ringwald     } else {
20125789943SMilanka Ringwald         log_info("Ignoring additional indicator");
20225789943SMilanka Ringwald     }
20325789943SMilanka Ringwald }
20425789943SMilanka Ringwald 
20525789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
20625789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
20725789943SMilanka Ringwald         hfp_connection->parser_item_index++;
20825789943SMilanka Ringwald     } else {
20925789943SMilanka Ringwald         log_info("Ignoring additional codec index");
21025789943SMilanka Ringwald     }
21125789943SMilanka Ringwald }
21225789943SMilanka Ringwald 
213eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
214eed67a37SMilanka Ringwald     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
215eed67a37SMilanka Ringwald         hfp_connection->remote_call_services_index++;
216eed67a37SMilanka Ringwald     } else {
217eed67a37SMilanka Ringwald         log_info("Ignoring additional remote_call_services");
218eed67a37SMilanka Ringwald     }
219eed67a37SMilanka Ringwald }
22025789943SMilanka Ringwald 
2213deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
2223deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
2233deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
2243deb3ec6SMatthias Ringwald     }
2253deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
2263deb3ec6SMatthias Ringwald }
2273deb3ec6SMatthias Ringwald 
2283deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
2293deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
2303deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
2313deb3ec6SMatthias Ringwald     }
2323deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
2333deb3ec6SMatthias Ringwald }
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
2363deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
23774386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
23828190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
2393deb3ec6SMatthias Ringwald     if (err){
24028190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
2413deb3ec6SMatthias Ringwald     }
242e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
243e43d1938SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
244e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
245e43d1938SMatthias Ringwald #endif
2463deb3ec6SMatthias Ringwald     return 1;
2473deb3ec6SMatthias Ringwald }
2483deb3ec6SMatthias Ringwald 
2496a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
250e8d1dc0dSMatthias Ringwald 
251e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
252c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
253e8d1dc0dSMatthias Ringwald 
254df327ff3SMilanka Ringwald     int i;
255df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
256e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
257e8d1dc0dSMatthias Ringwald         return 1;
258df327ff3SMilanka Ringwald     }
259df327ff3SMilanka Ringwald     return 0;
260df327ff3SMilanka Ringwald }
261df327ff3SMilanka Ringwald 
262d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
263d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
264d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
265d715cf51SMatthias Ringwald     int i;
266d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
267d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
268d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
269d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
270d715cf51SMatthias Ringwald     }
271d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2726535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
273d715cf51SMatthias Ringwald }
274d715cf51SMatthias Ringwald 
2753deb3ec6SMatthias Ringwald // UTILS
2763deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2773deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
2783deb3ec6SMatthias Ringwald }
2793deb3ec6SMatthias Ringwald 
2803deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
2813deb3ec6SMatthias Ringwald     if (value){
2823deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
2833deb3ec6SMatthias Ringwald     } else {
2843deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
2853deb3ec6SMatthias Ringwald     }
2863deb3ec6SMatthias Ringwald     return bitmap;
2873deb3ec6SMatthias Ringwald }
2883deb3ec6SMatthias Ringwald 
2893deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
290c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2913deb3ec6SMatthias Ringwald     int i;
2923deb3ec6SMatthias Ringwald     int offset = 0;
293c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2943deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
2953deb3ec6SMatthias Ringwald     }
2963deb3ec6SMatthias Ringwald     if (i<values_nr){
2973deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
2983deb3ec6SMatthias Ringwald     }
2993deb3ec6SMatthias Ringwald     return offset;
3003deb3ec6SMatthias Ringwald }
3013deb3ec6SMatthias Ringwald 
3023deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
303c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3043deb3ec6SMatthias Ringwald 
3053deb3ec6SMatthias Ringwald     int i;
3063deb3ec6SMatthias Ringwald     int offset = 0;
307c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3083deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
3093deb3ec6SMatthias Ringwald     }
3103deb3ec6SMatthias Ringwald 
3113deb3ec6SMatthias Ringwald     if (i<values_nr){
3123deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3133deb3ec6SMatthias Ringwald     }
3143deb3ec6SMatthias Ringwald     return offset;
3153deb3ec6SMatthias Ringwald }
3163deb3ec6SMatthias Ringwald 
317ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
318ca59be51SMatthias Ringwald     if (!hfp_connection) return;
319ca59be51SMatthias Ringwald     btstack_packet_handler_t callback = NULL;
320ca59be51SMatthias Ringwald     switch (hfp_connection->local_role){
321ca59be51SMatthias Ringwald         case HFP_ROLE_HF:
322ca59be51SMatthias Ringwald             callback = hfp_hf_callback;
323671f1aa2SMatthias Ringwald             break;
324ca59be51SMatthias Ringwald         case HFP_ROLE_AG:
325ca59be51SMatthias Ringwald             callback = hfp_ag_callback;
326671f1aa2SMatthias Ringwald             break;
327ca59be51SMatthias Ringwald         default:
328ca59be51SMatthias Ringwald             return;
329ca59be51SMatthias Ringwald     }
330ca59be51SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
331ca59be51SMatthias Ringwald }
332ca59be51SMatthias Ringwald 
333ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
334a0ffb263SMatthias Ringwald     uint8_t event[3];
335a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
336a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
337a0ffb263SMatthias Ringwald     event[2] = event_subtype;
338ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
339a0ffb263SMatthias Ringwald }
340a0ffb263SMatthias Ringwald 
341ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3423deb3ec6SMatthias Ringwald     uint8_t event[4];
3433deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3443deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3453deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3463deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
347ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3483deb3ec6SMatthias Ringwald }
3493deb3ec6SMatthias Ringwald 
350ca59be51SMatthias 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){
3518901a7c4SMatthias Ringwald     uint8_t event[12];
3526a7f44bdSMilanka Ringwald     int pos = 0;
3536a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
3546a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
355d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
3566a7f44bdSMilanka Ringwald     event[pos++] = status; // status 0 == OK
3576a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
3586a7f44bdSMilanka Ringwald     pos += 2;
3596a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
3606a7f44bdSMilanka Ringwald     pos += 6;
361ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
362a0653c3bSMilanka Ringwald }
363a0653c3bSMilanka Ringwald 
364f2a3f984SMilanka Ringwald void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t  negotiated_codec){
365d0c4aea6SMilanka Ringwald     uint8_t event[13];
366d0c4aea6SMilanka Ringwald     int pos = 0;
367d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
368d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
369d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
370d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
371d0c4aea6SMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
372d0c4aea6SMilanka Ringwald     pos += 2;
373d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
374d0c4aea6SMilanka Ringwald     pos += 6;
375d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
376ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
377d0c4aea6SMilanka Ringwald }
378d0c4aea6SMilanka Ringwald 
379ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
3802c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
3812c50df93SBjoern Hartmann     uint8_t event[256];
3822c50df93SBjoern Hartmann #else
383c1797c7dSMatthias Ringwald     uint8_t event[40];
3842c50df93SBjoern Hartmann #endif
385aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
386aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
387aa4dd815SMatthias Ringwald     event[2] = event_subtype;
38809ca35c4SMilanka Ringwald     uint16_t size = btstack_min(strlen(value), sizeof(event) - 4);
389aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
390aa4dd815SMatthias Ringwald     event[3 + size] = 0;
391ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
392aa4dd815SMatthias Ringwald }
393aa4dd815SMatthias Ringwald 
3940cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
3958f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
3963deb3ec6SMatthias Ringwald }
3973deb3ec6SMatthias Ringwald 
3983deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
399665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
400665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
401665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
402a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
403a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
404a0ffb263SMatthias Ringwald             return hfp_connection;
4053deb3ec6SMatthias Ringwald         }
4063deb3ec6SMatthias Ringwald     }
4073deb3ec6SMatthias Ringwald     return NULL;
4083deb3ec6SMatthias Ringwald }
4093deb3ec6SMatthias Ringwald 
410405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
411665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
412665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
413665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
414a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
4155df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
416a0ffb263SMatthias Ringwald             return hfp_connection;
4173deb3ec6SMatthias Ringwald         }
4183deb3ec6SMatthias Ringwald     }
4193deb3ec6SMatthias Ringwald     return NULL;
4203deb3ec6SMatthias Ringwald }
4213deb3ec6SMatthias Ringwald 
422405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
423665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
424665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
425665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
426a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
427c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
428a0ffb263SMatthias Ringwald             return hfp_connection;
4293deb3ec6SMatthias Ringwald         }
4303deb3ec6SMatthias Ringwald     }
4313deb3ec6SMatthias Ringwald     return NULL;
4323deb3ec6SMatthias Ringwald }
4333deb3ec6SMatthias Ringwald 
434405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
435d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
436d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
437d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
438d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
439c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
440d97d752dSMilanka Ringwald             return hfp_connection;
441d97d752dSMilanka Ringwald         }
442d97d752dSMilanka Ringwald     }
443d97d752dSMilanka Ringwald     return NULL;
444d97d752dSMilanka Ringwald }
445d97d752dSMilanka Ringwald 
446a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
447a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
448a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
449a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
4503deb3ec6SMatthias Ringwald 
451dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
4523deb3ec6SMatthias Ringwald 
453a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
454a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
4553deb3ec6SMatthias Ringwald 
456a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
457a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
4583deb3ec6SMatthias Ringwald 
459a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
460a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
4617522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
462a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
4633deb3ec6SMatthias Ringwald 
464a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
465a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
466a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
467a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
4683deb3ec6SMatthias Ringwald }
4693deb3ec6SMatthias Ringwald 
470fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
471a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
472a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
4733deb3ec6SMatthias Ringwald 
474a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
475a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
476a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
477aa4dd815SMatthias Ringwald 
478a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
479a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
4803deb3ec6SMatthias Ringwald 
481d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
482d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
483d751f069SMatthias Ringwald 
484a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
4853deb3ec6SMatthias Ringwald 
486d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
487a0ffb263SMatthias Ringwald     return hfp_connection;
4883deb3ec6SMatthias Ringwald }
4893deb3ec6SMatthias Ringwald 
490*48e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
491a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
49209a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
4933deb3ec6SMatthias Ringwald }
4943deb3ec6SMatthias Ringwald 
495323d3000SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){
496405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
497a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
498a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
4996535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
500323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
501bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
502bdf572f4SMatthias Ringwald 
503a0ffb263SMatthias Ringwald     return hfp_connection;
5043deb3ec6SMatthias Ringwald }
5053deb3ec6SMatthias Ringwald 
506aa4dd815SMatthias Ringwald /* @param network.
507aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
508aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
509aa4dd815SMatthias Ringwald  */
5103deb3ec6SMatthias Ringwald 
5113deb3ec6SMatthias Ringwald /* @param suported_features
5123deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5133deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
5143deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
5153deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
5163deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
5173deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5183deb3ec6SMatthias Ringwald  */
5193deb3ec6SMatthias Ringwald  /* Bit position:
5203deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
5213deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5223deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
5233deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
5243deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
5253deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5263deb3ec6SMatthias Ringwald  */
5273deb3ec6SMatthias Ringwald 
5289b1c3b4dSMatthias 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){
5293deb3ec6SMatthias Ringwald     uint8_t* attribute;
5303deb3ec6SMatthias Ringwald     de_create_sequence(service);
5313deb3ec6SMatthias Ringwald 
5323deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
533235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
5349b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
5353deb3ec6SMatthias Ringwald 
5363deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
537235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
5383deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5393deb3ec6SMatthias Ringwald     {
5403deb3ec6SMatthias Ringwald         //  "UUID for Service"
5413deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
542235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
5433deb3ec6SMatthias Ringwald     }
5443deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5453deb3ec6SMatthias Ringwald 
5463deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
547235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
5483deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5493deb3ec6SMatthias Ringwald     {
5503deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
5513deb3ec6SMatthias Ringwald         {
552235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
5533deb3ec6SMatthias Ringwald         }
5543deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
5553deb3ec6SMatthias Ringwald 
5563deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
5573deb3ec6SMatthias Ringwald         {
558235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
5593deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
5603deb3ec6SMatthias Ringwald         }
5613deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
5623deb3ec6SMatthias Ringwald     }
5633deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5643deb3ec6SMatthias Ringwald 
5653deb3ec6SMatthias Ringwald 
5663deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
567235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
5683deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5693deb3ec6SMatthias Ringwald     {
570235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
5713deb3ec6SMatthias Ringwald     }
5723deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5733deb3ec6SMatthias Ringwald 
5743deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
575235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
5763deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5773deb3ec6SMatthias Ringwald     {
5783deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
5793deb3ec6SMatthias Ringwald         {
580235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
5813deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
5823deb3ec6SMatthias Ringwald         }
5833deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
5843deb3ec6SMatthias Ringwald     }
5853deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5863deb3ec6SMatthias Ringwald 
5873deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
5883deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
5893deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
5903deb3ec6SMatthias Ringwald }
5913deb3ec6SMatthias Ringwald 
5926c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
5938a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
5948a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
5958a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
5961d9c9c90SMilanka Ringwald 
5971d9c9c90SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(sdp_query_context.remote_address, sdp_query_context.local_role);
5981d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
5991d9c9c90SMilanka Ringwald         log_info("connection with %s and local role %d not found", sdp_query_context.remote_address, sdp_query_context.local_role);
60084904e95SMilanka Ringwald         return;
60184904e95SMilanka Ringwald     }
6023deb3ec6SMatthias Ringwald 
6030e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
6045611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
605a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
6063deb3ec6SMatthias Ringwald             break;
6075611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
608a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
609a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
610520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
611520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
612520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
613520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
614520c92d5SMatthias Ringwald                         break;
615520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
616520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
617520c92d5SMatthias Ringwald                         break;
618520c92d5SMatthias Ringwald                     default:
6191d9c9c90SMilanka Ringwald                         btstack_assert(false);
620520c92d5SMatthias Ringwald                         return;
621520c92d5SMatthias Ringwald                 }
6221d9c9c90SMilanka Ringwald 
623ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
6241d9c9c90SMilanka Ringwald 
6251d9c9c90SMilanka Ringwald             } else {
62684904e95SMilanka Ringwald                 hfp_connection->state = HFP_IDLE;
627cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
628cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
629cc92f22bSMatthias Ringwald                     // report service not found
630cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
631cc92f22bSMatthias Ringwald                 }
632cc92f22bSMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
633cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
6341d9c9c90SMilanka Ringwald             }
6351d9c9c90SMilanka Ringwald 
6361d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
6371d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
6381d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
6393deb3ec6SMatthias Ringwald             break;
6403deb3ec6SMatthias Ringwald         default:
6413deb3ec6SMatthias Ringwald             break;
6423deb3ec6SMatthias Ringwald     }
6433deb3ec6SMatthias Ringwald }
6443deb3ec6SMatthias Ringwald 
64538200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
64638200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
647eddcd308SMatthias Ringwald 
648eddcd308SMatthias Ringwald     if (!sco_establishment_active){
6493427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
65038200c1dSMilanka Ringwald         return 0;
651eddcd308SMatthias Ringwald     }
652eddcd308SMatthias Ringwald 
6533427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
6543427dd75SMatthias Ringwald     switch (status){
6553427dd75SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
6563427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
6573427dd75SMatthias Ringwald         case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES:
658eddcd308SMatthias Ringwald             break;
6593427dd75SMatthias Ringwald         default:
6603427dd75SMatthias Ringwald             return 0;
6613427dd75SMatthias Ringwald     }
6623427dd75SMatthias Ringwald 
6633427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
6643427dd75SMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(sco_establishment_active->link_setting, sco_establishment_active, false);
6653427dd75SMatthias Ringwald 
6663427dd75SMatthias Ringwald     // handle no valid setting found
6673427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
6683427dd75SMatthias Ringwald         if (sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
6693427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
6707522e673SMatthias Ringwald             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
671bc1b1537SMilanka Ringwald             sco_establishment_active->sco_for_msbc_failed = 1;
672bc1b1537SMilanka Ringwald             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
6737522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
6743427dd75SMatthias Ringwald         } else {
6753427dd75SMatthias Ringwald             // no other options
6763427dd75SMatthias Ringwald             return 0;
677eddcd308SMatthias Ringwald         }
6783427dd75SMatthias Ringwald     }
6793427dd75SMatthias Ringwald 
6803427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
681eddcd308SMatthias Ringwald     sco_establishment_active->establish_audio_connection = 1;
6823427dd75SMatthias Ringwald     sco_establishment_active->link_setting = next_setting;
68338200c1dSMilanka Ringwald     sco_establishment_active = NULL;
68438200c1dSMilanka Ringwald     return 1;
685eddcd308SMatthias Ringwald }
686eddcd308SMatthias Ringwald 
687eddcd308SMatthias Ringwald 
688405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
6895575558eSMilanka Ringwald     UNUSED(packet_type);
6908a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
6915575558eSMilanka Ringwald     UNUSED(size);
6929ec2630cSMatthias Ringwald 
6933deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
69427950165SMatthias Ringwald     hci_con_handle_t handle;
695a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
696674515e8SMatthias Ringwald     uint8_t status;
6973deb3ec6SMatthias Ringwald 
69827950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
699aa4dd815SMatthias Ringwald 
7000e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
7011b005648SMatthias Ringwald 
702011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
7037522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
7047522e673SMatthias Ringwald                 case 0: //  SCO
7057522e673SMatthias Ringwald                 case 2: // eSCO
706011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
707405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
708011577abSMilanka Ringwald                     if (!hfp_connection) break;
709c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
710c169df2fSMatthias Ringwald                         hfp_connection->hf_accept_sco = 2;
711c169df2fSMatthias Ringwald                     } else {
712b72c4a9eSMatthias Ringwald                         hfp_connection->hf_accept_sco = 1;
713c169df2fSMatthias Ringwald                     }
7143721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7153721a235SMatthias Ringwald                     hfp_cc256x_prepare_for_sco(hfp_connection);
7163721a235SMatthias Ringwald #endif
717689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
718689d4323SMatthias Ringwald                     hfp_bcm_prepare_for_sco(hfp_connection);
719689d4323SMatthias Ringwald #endif
720c169df2fSMatthias Ringwald                     log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco);
721719aedb8SMatthias Ringwald                     sco_establishment_active = hfp_connection;
722202c8a4cSMatthias Ringwald                     break;
7237522e673SMatthias Ringwald                 default:
7247522e673SMatthias Ringwald                     break;
7257522e673SMatthias Ringwald             }
726011577abSMilanka Ringwald             break;
7273deb3ec6SMatthias Ringwald 
728eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
729eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
730b87963a5SMatthias Ringwald                 if (sco_establishment_active == NULL) break;
731be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
73238200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
73338200c1dSMilanka Ringwald 
73438200c1dSMilanka Ringwald                 hfp_connection = sco_establishment_active;
73538200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
73638200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
73738200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
73838200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
7396c5b2002SMatthias Ringwald             }
740eddcd308SMatthias Ringwald             break;
741eddcd308SMatthias Ringwald 
7423deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
743b87963a5SMatthias Ringwald             if (sco_establishment_active == NULL) break;
744011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
745405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
746011577abSMilanka Ringwald             if (!hfp_connection) {
747011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
748011577abSMilanka Ringwald                 return;
749011577abSMilanka Ringwald             }
750ce263fc8SMatthias Ringwald 
751011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
75238200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
753b72c4a9eSMatthias Ringwald                 hfp_connection->hf_accept_sco = 0;
75438200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
75538200c1dSMilanka Ringwald 
75638200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
75738200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
75838200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
759aa4dd815SMatthias Ringwald                 break;
760aa4dd815SMatthias Ringwald             }
761aa4dd815SMatthias Ringwald 
762011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
763011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
764011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
765011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
766011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
767011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
7683deb3ec6SMatthias Ringwald 
7693deb3ec6SMatthias Ringwald             switch (link_type){
7703deb3ec6SMatthias Ringwald                 case 0x00:
771aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
7723deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
7733deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
7743deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
7753deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
7763deb3ec6SMatthias Ringwald                     break;
7773deb3ec6SMatthias Ringwald                 case 0x02:
778aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
7793deb3ec6SMatthias Ringwald                     break;
7803deb3ec6SMatthias Ringwald                 default:
7813deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
7823deb3ec6SMatthias Ringwald                     break;
7833deb3ec6SMatthias Ringwald             }
784e0d13a19SMilanka Ringwald 
7853deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
786aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
787e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
788e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
7893deb3ec6SMatthias Ringwald 
790d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
791aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
792d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
793aa4dd815SMatthias Ringwald                 break;
794aa4dd815SMatthias Ringwald             }
795d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
796d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
797d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
79838200c1dSMilanka Ringwald             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
7993deb3ec6SMatthias Ringwald             break;
8003deb3ec6SMatthias Ringwald         }
8013deb3ec6SMatthias Ringwald 
8023deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
803f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
804405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
805aa4dd815SMatthias Ringwald 
806d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
807aa4dd815SMatthias Ringwald 
8083721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
8093721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
8103721a235SMatthias Ringwald #endif
811689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
812689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
813689d4323SMatthias Ringwald #endif
814d751f069SMatthias Ringwald             hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
815d63c37a1SMatthias Ringwald             hfp_connection->release_audio_connection = 0;
816*48e6eeeeSMatthias Ringwald 
817*48e6eeeeSMatthias Ringwald             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
818*48e6eeeeSMatthias Ringwald                 // RFCOMM already closed -> remote power off
819*48e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
820*48e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
821*48e6eeeeSMatthias Ringwald #else
822*48e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
823*48e6eeeeSMatthias Ringwald #endif
824*48e6eeeeSMatthias Ringwald                 break;
825*48e6eeeeSMatthias Ringwald             }
826*48e6eeeeSMatthias Ringwald 
827d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
828ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
8290c3047fbSMatthias Ringwald 
8305f053052SMilanka Ringwald             if (hfp_connection->release_slc_connection){
8315f053052SMilanka Ringwald                 hfp_connection->release_slc_connection = 0;
8320c3047fbSMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
8330c3047fbSMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
8345f053052SMilanka Ringwald             }
8353deb3ec6SMatthias Ringwald             break;
8363deb3ec6SMatthias Ringwald 
837fc64f94aSMatthias Ringwald         default:
8383deb3ec6SMatthias Ringwald             break;
8393deb3ec6SMatthias Ringwald     }
8403deb3ec6SMatthias Ringwald }
8413deb3ec6SMatthias Ringwald 
8423721a235SMatthias Ringwald 
84327950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8445575558eSMilanka Ringwald     UNUSED(packet_type);
84527950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8465575558eSMilanka Ringwald     UNUSED(size);
84727950165SMatthias Ringwald 
84827950165SMatthias Ringwald     bd_addr_t event_addr;
84927950165SMatthias Ringwald     uint16_t rfcomm_cid;
85027950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
85127950165SMatthias Ringwald     uint8_t status;
85227950165SMatthias Ringwald 
85327950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
85427950165SMatthias Ringwald 
85527950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
85627950165SMatthias Ringwald 
85727950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
85827950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
85927950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
86027950165SMatthias Ringwald             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role);
86127950165SMatthias Ringwald             if (!hfp_connection){
86227950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
86327950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
86427950165SMatthias Ringwald                 return;
86527950165SMatthias Ringwald             }
86627950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
8674960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
8684960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
86927950165SMatthias Ringwald                 return;
87027950165SMatthias Ringwald             }
87127950165SMatthias Ringwald 
87227950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
87327950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
87427950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
87527950165SMatthias Ringwald             break;
87627950165SMatthias Ringwald 
87727950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
87827950165SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
87927950165SMatthias Ringwald 
88027950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
88127950165SMatthias Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
88227950165SMatthias Ringwald 
883405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
884505f1c30SMatthias Ringwald             if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return;
88527950165SMatthias Ringwald 
88627950165SMatthias Ringwald             if (status) {
88727950165SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
888*48e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
88927950165SMatthias Ringwald             } else {
89027950165SMatthias Ringwald                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
89127950165SMatthias Ringwald                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
89227950165SMatthias Ringwald                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
89327950165SMatthias Ringwald 
89427950165SMatthias Ringwald                 switch (hfp_connection->state){
89527950165SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
89627950165SMatthias Ringwald                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
89727950165SMatthias Ringwald                         break;
89827950165SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
89927950165SMatthias Ringwald                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
90027950165SMatthias Ringwald                         break;
90127950165SMatthias Ringwald                     default:
90227950165SMatthias Ringwald                         break;
90327950165SMatthias Ringwald                 }
90427950165SMatthias Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
90527950165SMatthias Ringwald             }
90627950165SMatthias Ringwald             break;
90727950165SMatthias Ringwald 
90827950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
90927950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
91027950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
91127950165SMatthias Ringwald             if (!hfp_connection) break;
91227950165SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
91327950165SMatthias Ringwald                 hfp_connection->state = HFP_IDLE;
91427950165SMatthias Ringwald                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
91527950165SMatthias Ringwald                 break;
91627950165SMatthias Ringwald             }
917*48e6eeeeSMatthias Ringwald             if ( hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
918*48e6eeeeSMatthias Ringwald                 // service connection was released, this implicitly releases audio connection as well
919*48e6eeeeSMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
920*48e6eeeeSMatthias Ringwald                 gap_disconnect(hfp_connection->sco_handle);
921*48e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
922*48e6eeeeSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
92327950165SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
924*48e6eeeeSMatthias Ringwald             } else {
925*48e6eeeeSMatthias Ringwald                 // regular case
926*48e6eeeeSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
927*48e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
928*48e6eeeeSMatthias Ringwald             }
92927950165SMatthias Ringwald             break;
93027950165SMatthias Ringwald 
93127950165SMatthias Ringwald         default:
93227950165SMatthias Ringwald             break;
93327950165SMatthias Ringwald     }
93427950165SMatthias Ringwald }
935aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
93694a27792SMatthias Ringwald 
93794a27792SMatthias Ringwald typedef struct {
93894a27792SMatthias Ringwald     const char * command;
93994a27792SMatthias Ringwald     hfp_command_t command_id;
94094a27792SMatthias Ringwald } hfp_command_entry_t;
94194a27792SMatthias Ringwald 
94294a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
943cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
944cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
945cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
946cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
947cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
94894a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
94994a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
95094a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
951cb33905eSMatthias Ringwald     { "AT+BINP",   HFP_CMD_HF_REQUEST_PHONE_NUMBER },
952cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
953cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
95494a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
95594a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
956cb33905eSMatthias Ringwald     { "AT+BVRA=",  HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
957cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
95894a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
95994a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
960cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
96194a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
96294a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
963cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
964cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
965cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
966cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
967cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
96894a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
96994a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
970cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
971cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
972cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
973cb33905eSMatthias Ringwald     { "AT+VTS:",   HFP_CMD_TRANSMIT_DTMF_CODES },
97494a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
97594a27792SMatthias Ringwald };
97694a27792SMatthias Ringwald 
97794a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
978cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
97994a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
980cb33905eSMatthias Ringwald     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
981cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
982cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
98394a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
984cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
985cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
98694a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
987cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
98884a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
989cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
990cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
991cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
992cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
993cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
994cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
995cb33905eSMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
996cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
997cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
998cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
999cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
100094a27792SMatthias Ringwald };
100194a27792SMatthias Ringwald 
1002aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
10033deb3ec6SMatthias Ringwald 
1004cb33905eSMatthias Ringwald     // table lookup based on role
100594a27792SMatthias Ringwald     uint16_t num_entries;
100694a27792SMatthias Ringwald     hfp_command_entry_t * table;
100794a27792SMatthias Ringwald     if (isHandsFree == 0){
100894a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
100994a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
10103deb3ec6SMatthias Ringwald     } else {
101194a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
101294a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
101394a27792SMatthias Ringwald     }
1014791f0d0aSMatthias Ringwald     // binary search
1015791f0d0aSMatthias Ringwald     uint16_t left = 0;
1016791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
1017791f0d0aSMatthias Ringwald     while (left <= right){
1018791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
1019791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
102094a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
1021791f0d0aSMatthias Ringwald         if (match < 0){
1022791f0d0aSMatthias Ringwald             // search term is lower than middle element
102347005182SMilanka Ringwald             if (right == 0) break;
1024791f0d0aSMatthias Ringwald             right = middle - 1;
1025791f0d0aSMatthias Ringwald         } else if (match == 0){
102694a27792SMatthias Ringwald             return entry->command_id;
1027791f0d0aSMatthias Ringwald         } else {
1028791f0d0aSMatthias Ringwald             // search term is higher than middle element
1029791f0d0aSMatthias Ringwald             left = middle + 1;
103094a27792SMatthias Ringwald         }
103194a27792SMatthias Ringwald     }
103294a27792SMatthias Ringwald 
1033cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
10340222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1035cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1036cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1037cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
10383deb3ec6SMatthias Ringwald     }
10393deb3ec6SMatthias Ringwald 
1040cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1041cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1042aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
10433deb3ec6SMatthias Ringwald     }
10443deb3ec6SMatthias Ringwald 
1045cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1046aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1047aa4dd815SMatthias Ringwald     }
10483deb3ec6SMatthias Ringwald 
1049aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
10503deb3ec6SMatthias Ringwald }
10513deb3ec6SMatthias Ringwald 
1052a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
105362c7768eSMatthias Ringwald     if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return;
1054a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1055a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
10563deb3ec6SMatthias Ringwald }
1057a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1058a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
10593deb3ec6SMatthias Ringwald }
10603deb3ec6SMatthias Ringwald 
10613deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1062c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
10633deb3ec6SMatthias Ringwald }
10643deb3ec6SMatthias Ringwald 
106594a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1066a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
10673deb3ec6SMatthias Ringwald }
10681d81c7feSMatthias Ringwald 
10691d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
10701d81c7feSMatthias Ringwald     switch (byte){
10711d81c7feSMatthias Ringwald         case ',':
1072f8301d46SMatthias Ringwald 		case '-':
10731d81c7feSMatthias Ringwald         case ';':
10741d81c7feSMatthias Ringwald         case '(':
10751d81c7feSMatthias Ringwald         case ')':
10761d81c7feSMatthias Ringwald         case '\n':
10771d81c7feSMatthias Ringwald         case '\r':
10783deb3ec6SMatthias Ringwald             break;
10793deb3ec6SMatthias Ringwald         default:
10801d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
10813deb3ec6SMatthias Ringwald             break;
10823deb3ec6SMatthias Ringwald     }
10833deb3ec6SMatthias Ringwald }
10841d81c7feSMatthias Ringwald 
10851d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
10861d81c7feSMatthias Ringwald     switch (byte){
10871d81c7feSMatthias Ringwald         case ',':
1088f8301d46SMatthias Ringwald 		case '-':
10891d81c7feSMatthias Ringwald         case ';':
10901d81c7feSMatthias Ringwald         case '\n':
10911d81c7feSMatthias Ringwald         case '\r':
10921d81c7feSMatthias Ringwald             return true;
1093dd533208SMatthias Ringwald         default:
10941d81c7feSMatthias Ringwald             return false;
10953deb3ec6SMatthias Ringwald     }
10963deb3ec6SMatthias Ringwald }
10973deb3ec6SMatthias Ringwald 
1098d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1099aa4dd815SMatthias Ringwald 
11001dddc4f4SMatthias Ringwald     // handle doubles quotes
11011dddc4f4SMatthias Ringwald     if (byte == '"'){
11021dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1103d6b237acSMatthias Ringwald         return true;
11041dddc4f4SMatthias Ringwald     }
11051dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
11061dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1107d6b237acSMatthias Ringwald         return true;
11081dddc4f4SMatthias Ringwald     }
11093deb3ec6SMatthias Ringwald 
11101dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1111d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
11121dddc4f4SMatthias Ringwald 
1113bfeaf344SMatthias Ringwald     bool processed = true;
1114bfeaf344SMatthias Ringwald 
1115a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1116dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1117d28b2d5dSMilanka Ringwald             switch (byte) {
1118dd533208SMatthias Ringwald                 case '\n':
1119dd533208SMatthias Ringwald                 case '\r':
1120dd533208SMatthias Ringwald                 case ';':
1121bfeaf344SMatthias Ringwald                     // ignore separator
1122bfeaf344SMatthias Ringwald                     break;
1123bfeaf344SMatthias Ringwald                 case ':':
1124bfeaf344SMatthias Ringwald                 case '?':
1125bfeaf344SMatthias Ringwald                     // store separator
1126bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1127dd533208SMatthias Ringwald                     break;
1128d28b2d5dSMilanka Ringwald                 case '=':
1129bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1130dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1131a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1132d6b237acSMatthias Ringwald                     return true;
1133d28b2d5dSMilanka Ringwald                 default:
1134bfeaf344SMatthias Ringwald                     // store if not lookahead
1135d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1136dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1137d6b237acSMatthias Ringwald                         return true;
1138dd533208SMatthias Ringwald                     }
1139bfeaf344SMatthias Ringwald                     // mark as lookahead
1140bfeaf344SMatthias Ringwald                     processed = false;
1141d7f16ff1SMatthias Ringwald                     break;
1142d7f16ff1SMatthias Ringwald             }
1143ce263fc8SMatthias Ringwald 
11441d81c7feSMatthias Ringwald             // ignore empty tokens
1145d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1146dd533208SMatthias Ringwald 
1147bfeaf344SMatthias Ringwald             // parse
1148dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1149aa4dd815SMatthias Ringwald 
115084a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
115184a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1152a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1153aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1154a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1155aa4dd815SMatthias Ringwald                         break;
1156aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1157a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1158aa4dd815SMatthias Ringwald                         break;
1159aa4dd815SMatthias Ringwald                     default:
116084a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1161aa4dd815SMatthias Ringwald                         break;
1162aa4dd815SMatthias Ringwald                 }
1163aa4dd815SMatthias Ringwald             }
11643deb3ec6SMatthias Ringwald 
1165c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1166c0c0437aSMatthias Ringwald 
1167bfeaf344SMatthias Ringwald             // next state
1168bfeaf344SMatthias Ringwald             hfp_connection->found_equal_sign = false;
11691d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1170dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1171dd533208SMatthias Ringwald 
1172bfeaf344SMatthias Ringwald             return processed;
1173dd533208SMatthias Ringwald 
1174bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
11751d81c7feSMatthias Ringwald             // handle empty fields
11761d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1177dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1178dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1179dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
11803d051a53SMatthias Ringwald                 return true;
11813d051a53SMatthias Ringwald             }
11823d051a53SMatthias Ringwald 
11831d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11841d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
11851d81c7feSMatthias Ringwald 
11861d81c7feSMatthias Ringwald             // ignore empty tokens
11873d051a53SMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true;
11883d051a53SMatthias Ringwald 
11893d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
11901d81c7feSMatthias Ringwald 
11911d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
11921d81c7feSMatthias Ringwald 
11931d81c7feSMatthias Ringwald             switch (hfp_connection->command){
11941d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
11951d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
11961d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
11971d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11981d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
11991d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
12001d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12011d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
12021d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
12031d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
12041d81c7feSMatthias Ringwald                     break;
12051d81c7feSMatthias Ringwald                 default:
12061d81c7feSMatthias Ringwald                     break;
12071d81c7feSMatthias Ringwald             }
12083d051a53SMatthias Ringwald             return true;
12093d051a53SMatthias Ringwald 
1210ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
12111d81c7feSMatthias Ringwald 
12121d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
12131d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1214dd533208SMatthias Ringwald 
1215a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1216ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1217a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
12182308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1219ce263fc8SMatthias Ringwald                     break;
1220ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1221a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
12222308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1223ce263fc8SMatthias Ringwald                     break;
1224ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1225ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1226ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
12272308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1228ce263fc8SMatthias Ringwald                     break;
1229ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12302308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1231a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1232a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1233ce263fc8SMatthias Ringwald                     break;
1234ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12352308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1236a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1237ce263fc8SMatthias Ringwald                     break;
1238ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1239a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1240a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
12412308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1242ce263fc8SMatthias Ringwald                     break;
12430222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
12440222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
12450222a807SMatthias Ringwald                     break;
1246ce263fc8SMatthias Ringwald                 default:
1247ce263fc8SMatthias Ringwald                     break;
1248ce263fc8SMatthias Ringwald             }
12491d81c7feSMatthias Ringwald 
12501d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12511d81c7feSMatthias Ringwald 
12521d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
12531d81c7feSMatthias Ringwald 
1254ba0de059SMatthias Ringwald             return true;
1255ce263fc8SMatthias Ringwald 
125674c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
12571d81c7feSMatthias Ringwald 
12581d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
12591d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
12601d81c7feSMatthias Ringwald 
1261a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1262ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
126389425bfcSMilanka Ringwald                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
126489425bfcSMilanka Ringwald                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1265a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1266ce263fc8SMatthias Ringwald                     break;
1267ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12682308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
126925789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
127025789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1271a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1272ce263fc8SMatthias Ringwald                     break;
1273ce263fc8SMatthias Ringwald                 default:
1274ce263fc8SMatthias Ringwald                     break;
1275ce263fc8SMatthias Ringwald             }
12761d81c7feSMatthias Ringwald 
12771d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12781d81c7feSMatthias Ringwald 
12791d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
12801d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
12811d81c7feSMatthias Ringwald             } else {
12821d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
12831d81c7feSMatthias Ringwald             }
1284d6b237acSMatthias Ringwald             return true;
128574c7548aSMatthias Ringwald 
128674c7548aSMatthias Ringwald         default:
128774c7548aSMatthias Ringwald             btstack_assert(false);
128874c7548aSMatthias Ringwald             return true;
128974c7548aSMatthias Ringwald     }
1290d6b237acSMatthias Ringwald }
1291d6b237acSMatthias Ringwald 
1292d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1293d6b237acSMatthias Ringwald     bool processed = false;
1294d6b237acSMatthias Ringwald     while (!processed){
1295d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1296d6b237acSMatthias Ringwald     }
12971d81c7feSMatthias Ringwald     // reset parser state on end-of-line
12981d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
12991d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
13001d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
13011d81c7feSMatthias Ringwald     }
1302ce263fc8SMatthias Ringwald }
1303ce263fc8SMatthias Ringwald 
1304a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1305ce263fc8SMatthias Ringwald     int value;
1306a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1307667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
13082308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1309667ec068SMatthias Ringwald             int i;
1310a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1311667ec068SMatthias Ringwald                 case 0:
1312a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1313a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1314a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1315667ec068SMatthias Ringwald                             break;
1316667ec068SMatthias Ringwald                         }
1317667ec068SMatthias Ringwald                     }
1318667ec068SMatthias Ringwald                     break;
1319667ec068SMatthias Ringwald                 case 1:
1320a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1321a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1322667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1323a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1324667ec068SMatthias Ringwald                     break;
1325667ec068SMatthias Ringwald                 default:
1326667ec068SMatthias Ringwald                     break;
1327667ec068SMatthias Ringwald             }
132825789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1329667ec068SMatthias Ringwald             break;
1330667ec068SMatthias Ringwald 
1331667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1332a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1333667ec068SMatthias Ringwald                 case 0:
1334a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1335a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1336667ec068SMatthias Ringwald                     break;
1337667ec068SMatthias Ringwald                 case 1:
13382308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1339a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1340667ec068SMatthias Ringwald                     break;
1341667ec068SMatthias Ringwald                 default:
1342667ec068SMatthias Ringwald                     break;
1343667ec068SMatthias Ringwald             }
1344eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1345a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1346667ec068SMatthias Ringwald             break;
1347667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1348a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1349667ec068SMatthias Ringwald                 case 0:
13502308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1351a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1352667ec068SMatthias Ringwald                     break;
1353667ec068SMatthias Ringwald                 case 1:
13542308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1355a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1356667ec068SMatthias Ringwald                     break;
1357667ec068SMatthias Ringwald                 case 2:
13582308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1359a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1360667ec068SMatthias Ringwald                     break;
1361667ec068SMatthias Ringwald                 case 3:
13622308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13630aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1364667ec068SMatthias Ringwald                     break;
1365667ec068SMatthias Ringwald                 case 4:
13660aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13670aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
13680aee97efSMilanka Ringwald                     break;
13690aee97efSMilanka Ringwald                 case 5:
1370a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1371a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1372667ec068SMatthias Ringwald                     break;
13730aee97efSMilanka Ringwald                 case 6:
13742308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1375a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1376667ec068SMatthias Ringwald                     break;
1377667ec068SMatthias Ringwald                 default:
1378667ec068SMatthias Ringwald                     break;
1379667ec068SMatthias Ringwald             }
1380eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1381a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1382667ec068SMatthias Ringwald             break;
1383aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
13842308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1385a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1386aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1387aa4dd815SMatthias Ringwald             break;
1388aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
13892308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1390a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1391aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1392aa4dd815SMatthias Ringwald             break;
1393aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
13942308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1395a0ffb263SMatthias Ringwald             hfp_connection->ag_activate_voice_recognition = value;
1396aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1397aa4dd815SMatthias Ringwald             break;
1398aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
13992308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1400a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1401aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1402aa4dd815SMatthias Ringwald             break;
1403aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
14042308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1405a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1406aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1407aa4dd815SMatthias Ringwald             break;
14083deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
14092308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1410a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
14113deb3ec6SMatthias Ringwald             break;
14123deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
14132308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1414a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
14153deb3ec6SMatthias Ringwald             break;
14163deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
14172308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1418bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
14193deb3ec6SMatthias Ringwald             break;
14203deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1421a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
14222308e108SMilanka Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
142325789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1424a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
14253deb3ec6SMatthias Ringwald             break;
1426aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
142789425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
142889425bfcSMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1429a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1430a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1431aa4dd815SMatthias Ringwald             break;
1432aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1433a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
14342308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
143525789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
14363deb3ec6SMatthias Ringwald             break;
14373deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
143825789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1439a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1440a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
14412308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1442a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
14433deb3ec6SMatthias Ringwald             break;
14443deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1445a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1446a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
144797d2cfbcSMatthias 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);
144825789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1449eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
14503deb3ec6SMatthias Ringwald             break;
1451aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1452aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1453a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
14542308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
145525789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1456a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
14573deb3ec6SMatthias Ringwald             break;
1458aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14593deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1460a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
146125789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
14623deb3ec6SMatthias Ringwald             break;
1463ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
146425789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1465a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1466ce263fc8SMatthias Ringwald             break;
14673deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
14683deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1469a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1470a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1471a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1472a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1473ce263fc8SMatthias Ringwald             }
1474a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1475ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1476a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1477ce263fc8SMatthias Ringwald             } else {
14782308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1479a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1480a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1481a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
14823deb3ec6SMatthias Ringwald             }
148325789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
14843deb3ec6SMatthias Ringwald             break;
14853deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14863deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
148725789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1488a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
14893deb3ec6SMatthias Ringwald             break;
1490aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
14912308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1492a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1493aa4dd815SMatthias Ringwald             break;
1494aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1495a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1496a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
14973deb3ec6SMatthias Ringwald                 break;
14983deb3ec6SMatthias Ringwald             }
14993deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1500a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
15013deb3ec6SMatthias Ringwald             break;
15023deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
15033deb3ec6SMatthias Ringwald             break;
15043deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1505a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
15062308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
15073deb3ec6SMatthias Ringwald             break;
15083deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
15092308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1510a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1511a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
15123deb3ec6SMatthias Ringwald             break;
1513ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1514a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1515a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1516a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1517a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
15183deb3ec6SMatthias Ringwald             break;
15190222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
15200222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
15210222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
15220222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
15230222a807SMatthias Ringwald             }
15240222a807SMatthias Ringwald             break;
15250222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
15260222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
15270222a807SMatthias Ringwald             break;
15280222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
15290222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
15300222a807SMatthias Ringwald             break;
15310222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
15320222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
15330222a807SMatthias Ringwald             break;
15340222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
15350222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
15360222a807SMatthias Ringwald             break;
15373deb3ec6SMatthias Ringwald         default:
15383deb3ec6SMatthias Ringwald             break;
15393deb3ec6SMatthias Ringwald     }
15403deb3ec6SMatthias Ringwald }
15413deb3ec6SMatthias Ringwald 
15421d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
15431d9c9c90SMilanka Ringwald     UNUSED(context);
15441d9c9c90SMilanka Ringwald 
15451d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
15461d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
15471d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
15481d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
15491d9c9c90SMilanka Ringwald 
15501d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
15511d9c9c90SMilanka Ringwald 
15521d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
15531d9c9c90SMilanka Ringwald         sdp_query_context.local_role = connection->local_role;
15541d9c9c90SMilanka Ringwald         (void)memcpy(sdp_query_context.remote_address, connection->remote_addr, 6);
1555e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
15561d9c9c90SMilanka Ringwald         return;
15571d9c9c90SMilanka Ringwald     }
15581d9c9c90SMilanka Ringwald }
15591d9c9c90SMilanka Ringwald 
1560323d3000SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
1561323d3000SMatthias Ringwald     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1562a0ffb263SMatthias Ringwald     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
15633deb3ec6SMatthias Ringwald 
1564a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15653deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
15663deb3ec6SMatthias Ringwald         return;
15673deb3ec6SMatthias Ringwald     }
1568a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
15693deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1570a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
15713deb3ec6SMatthias Ringwald             return;
15723deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
1573a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
15743deb3ec6SMatthias Ringwald             return;
15753deb3ec6SMatthias Ringwald         case HFP_IDLE:
15766535961aSMatthias Ringwald             (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
15771d9c9c90SMilanka Ringwald             hfp_connection->state = HFP_W2_SEND_SDP_QUERY;
1578a0ffb263SMatthias Ringwald             hfp_connection->service_uuid = service_uuid;
15791d9c9c90SMilanka Ringwald 
15801d9c9c90SMilanka Ringwald             hfp_handle_sdp_client_query_request.callback = &hfp_handle_start_sdp_client_query;
15811d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
15821d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
15833deb3ec6SMatthias Ringwald             break;
15843deb3ec6SMatthias Ringwald         default:
15853deb3ec6SMatthias Ringwald             break;
15863deb3ec6SMatthias Ringwald     }
15873deb3ec6SMatthias Ringwald }
15883deb3ec6SMatthias Ringwald 
1589a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1590a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1591a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
15923deb3ec6SMatthias Ringwald 
1593a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1594a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_IDLE;
15953deb3ec6SMatthias Ringwald         return;
15963deb3ec6SMatthias Ringwald     }
15973deb3ec6SMatthias Ringwald 
1598a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1599a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
16003deb3ec6SMatthias Ringwald         return;
16013deb3ec6SMatthias Ringwald     }
16023deb3ec6SMatthias Ringwald 
16030c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
16040c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
16050c3047fbSMatthias Ringwald         return;
16060c3047fbSMatthias Ringwald     }
16070c3047fbSMatthias Ringwald 
16080c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
16090c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
16100c3047fbSMatthias Ringwald         return;
16110c3047fbSMatthias Ringwald     }
16120c3047fbSMatthias Ringwald 
16130c3047fbSMatthias Ringwald     // HFP_W4_SCO_DISCONNECTED or later
16145f053052SMilanka Ringwald     hfp_connection->release_slc_connection = 1;
16153deb3ec6SMatthias Ringwald }
16163deb3ec6SMatthias Ringwald 
1617a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1618a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1619a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1620a0ffb263SMatthias Ringwald     hfp_connection->release_audio_connection = 1;
16213deb3ec6SMatthias Ringwald }
16223deb3ec6SMatthias Ringwald 
1623ce263fc8SMatthias Ringwald static const struct link_settings {
1624ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1625ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1626ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1627ebc6f15bSMatthias Ringwald     const bool     eSCO;
1628ebc6f15bSMatthias Ringwald     const uint8_t  codec;
1629ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1630352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1631352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1632352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1633352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1634352a0504SMatthias Ringwald     { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1635352a0504SMatthias Ringwald     { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1636352a0504SMatthias Ringwald     { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1637352a0504SMatthias Ringwald     { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1638ce263fc8SMatthias Ringwald };
16393deb3ec6SMatthias Ringwald 
1640eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1641ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1642eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1643ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1644eddcd308SMatthias Ringwald     sco_establishment_active = hfp_connection;
1645d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1646d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1647689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1648689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1649689d4323SMatthias Ringwald #else
1650689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1651689d4323SMatthias Ringwald #endif
1652d2c1d59aSMatthias Ringwald     }
1653352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1654352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1655eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1656352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1657ce263fc8SMatthias Ringwald }
1658d68dcce1SMatthias Ringwald 
16593721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
16603721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
16613721a235SMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
16623721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
16633721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
16643721a235SMatthias Ringwald     }
16653721a235SMatthias Ringwald }
16663721a235SMatthias Ringwald 
16673721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
16683721a235SMatthias Ringwald     uint32_t sample_rate_hz;
16693721a235SMatthias Ringwald     uint16_t clock_rate_khz;
16703721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
16713721a235SMatthias Ringwald         clock_rate_khz = 512;
16723721a235SMatthias Ringwald         sample_rate_hz = 16000;
16733721a235SMatthias Ringwald     } else {
16743721a235SMatthias Ringwald         clock_rate_khz = 256;
16753721a235SMatthias Ringwald         sample_rate_hz = 8000;
16763721a235SMatthias Ringwald     }
16773721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
16783721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
16793721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
16803721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
16813721a235SMatthias Ringwald     uint8_t  reserved = 0;
16823721a235SMatthias Ringwald     uint16_t size = 16;
16833721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
16843721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
16853721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
16863721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
16873721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
16883721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
16893721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
16903721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
16913721a235SMatthias Ringwald }
16923721a235SMatthias Ringwald #endif
16933721a235SMatthias Ringwald 
1694689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1695689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1696689d4323SMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1697689d4323SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1698689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
1699689d4323SMatthias Ringwald     }
1700689d4323SMatthias Ringwald }
1701689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1702689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
17031d2bbd54SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 512 kHz
17041d2bbd54SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
1705689d4323SMatthias Ringwald }
1706689d4323SMatthias Ringwald #endif
1707689d4323SMatthias Ringwald 
17081b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
17091b005648SMatthias Ringwald     hfp_hf_callback = callback;
17101b005648SMatthias Ringwald }
17111b005648SMatthias Ringwald 
17121b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
17131b005648SMatthias Ringwald     hfp_ag_callback = callback;
17141b005648SMatthias Ringwald }
17151b005648SMatthias Ringwald 
1716ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1717ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
1718ca59be51SMatthias Ringwald }
17191b005648SMatthias Ringwald 
1720ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1721ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
1722d68dcce1SMatthias Ringwald }
1723d68dcce1SMatthias Ringwald 
1724520c92d5SMatthias Ringwald void hfp_init(void){
1725352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
1726991c26beSMatthias Ringwald }
1727991c26beSMatthias Ringwald 
172820b2edb6SMatthias Ringwald void hfp_deinit(void){
172920b2edb6SMatthias Ringwald     hfp_connections = NULL;
173020b2edb6SMatthias Ringwald     hfp_hf_callback = NULL;
173120b2edb6SMatthias Ringwald     hfp_ag_callback = NULL;
173220b2edb6SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = NULL;
173320b2edb6SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = NULL;
173420b2edb6SMatthias Ringwald     sco_establishment_active = NULL;
173520b2edb6SMatthias Ringwald     (void) memset(&sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
173620b2edb6SMatthias Ringwald     (void) memset(&hfp_handle_sdp_client_query_request, 0, sizeof(btstack_context_callback_registration_t));
173720b2edb6SMatthias Ringwald }
173820b2edb6SMatthias Ringwald 
1739991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
174001e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
1741991c26beSMatthias Ringwald }
1742991c26beSMatthias Ringwald 
1743991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
174401e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
1745520c92d5SMatthias Ringwald }
1746186dd3d2SMatthias Ringwald 
1747e453c1d9SMatthias 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){
1748e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
1749e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
1750e453c1d9SMatthias Ringwald     while (setting > 0){
1751e453c1d9SMatthias Ringwald         setting--;
1752e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
1753e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
1754e453c1d9SMatthias Ringwald         // skip if S4 but not supported
1755e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
1756e453c1d9SMatthias Ringwald         // skip wrong codec
1757e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
175801e5b727SMatthias Ringwald         // skip disabled packet types
1759352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
1760352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
1761352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
176201e5b727SMatthias Ringwald 
1763e453c1d9SMatthias Ringwald         // found matching setting
1764e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
1765afac2a04SMilanka Ringwald     }
1766e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
1767afac2a04SMilanka Ringwald }
1768e453c1d9SMatthias Ringwald 
17692b7abbfbSMatthias 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){
1770e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
1771e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
1772e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
17732b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
17742b7abbfbSMatthias Ringwald }
17752b7abbfbSMatthias Ringwald 
17762b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
1777e453c1d9SMatthias Ringwald     // get highest possible link setting
17782b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
1779afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
1780afac2a04SMilanka Ringwald }
1781afac2a04SMilanka Ringwald 
1782186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
1783186dd3d2SMatthias Ringwald 
1784186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
1785186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1786186dd3d2SMatthias Ringwald     // encode \n\r
1787186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
178815a27967SMatthias Ringwald     int i = 0;
1789186dd3d2SMatthias Ringwald     int pos;
179015a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
1791186dd3d2SMatthias Ringwald         switch (packet[pos]){
1792186dd3d2SMatthias Ringwald             case '\n':
1793186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1794186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
1795186dd3d2SMatthias Ringwald                 break;
1796186dd3d2SMatthias Ringwald             case '\r':
1797186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1798186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
1799186dd3d2SMatthias Ringwald                 break;
1800186dd3d2SMatthias Ringwald             default:
1801186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
1802186dd3d2SMatthias Ringwald                 break;
1803186dd3d2SMatthias Ringwald         }
1804186dd3d2SMatthias Ringwald     }
1805186dd3d2SMatthias Ringwald     printable[i] = 0;
1806186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
1807186dd3d2SMatthias Ringwald #endif
1808186dd3d2SMatthias Ringwald }
1809