xref: /btstack/src/classic/hfp.c (revision 689d4323e4f864e100adb6c55186bad11954d6c9)
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 
66*689d4323SMatthias Ringwald #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM)
67*689d4323SMatthias Ringwald #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
68*689d4323SMatthias Ringwald #endif
69*689d4323SMatthias Ringwald 
703deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
713deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
723deb3ec6SMatthias Ringwald 
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
753deb3ec6SMatthias Ringwald     "EC and/or NR function",
763deb3ec6SMatthias Ringwald     "Three-way calling",
773deb3ec6SMatthias Ringwald     "CLI presentation capability",
783deb3ec6SMatthias Ringwald     "Voice recognition activation",
793deb3ec6SMatthias Ringwald     "Remote volume control",
803deb3ec6SMatthias Ringwald 
813deb3ec6SMatthias Ringwald     "Enhanced call status",
823deb3ec6SMatthias Ringwald     "Enhanced call control",
833deb3ec6SMatthias Ringwald 
843deb3ec6SMatthias Ringwald     "Codec negotiation",
853deb3ec6SMatthias Ringwald 
863deb3ec6SMatthias Ringwald     "HF Indicators",
873deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
883deb3ec6SMatthias Ringwald     "Reserved for future definition"
893deb3ec6SMatthias Ringwald };
903deb3ec6SMatthias Ringwald 
913deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
923deb3ec6SMatthias Ringwald     "Three-way calling",
933deb3ec6SMatthias Ringwald     "EC and/or NR function",
943deb3ec6SMatthias Ringwald     "Voice recognition function",
953deb3ec6SMatthias Ringwald     "In-band ring tone capability",
963deb3ec6SMatthias Ringwald     "Attach a number to a voice tag",
973deb3ec6SMatthias Ringwald     "Ability to reject a call",
983deb3ec6SMatthias Ringwald     "Enhanced call status",
993deb3ec6SMatthias Ringwald     "Enhanced call control",
1003deb3ec6SMatthias Ringwald     "Extended Error Result Codes",
1013deb3ec6SMatthias Ringwald     "Codec negotiation",
1023deb3ec6SMatthias Ringwald     "HF Indicators",
1033deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
1043deb3ec6SMatthias Ringwald     "Reserved for future definition"
1053deb3ec6SMatthias Ringwald };
1063deb3ec6SMatthias Ringwald 
1070aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1080aee97efSMilanka Ringwald     "outgoing",
1090aee97efSMilanka Ringwald     "incoming"
1100aee97efSMilanka Ringwald };
1110aee97efSMilanka Ringwald 
1120aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1130aee97efSMilanka Ringwald     "active",
1140aee97efSMilanka Ringwald     "held",
1150aee97efSMilanka Ringwald     "outgoing dialing",
1160aee97efSMilanka Ringwald     "outgoing alerting",
1170aee97efSMilanka Ringwald     "incoming",
1180aee97efSMilanka Ringwald     "incoming waiting",
1190aee97efSMilanka Ringwald     "call held by response and hold"
1200aee97efSMilanka Ringwald };
1210aee97efSMilanka Ringwald 
1220aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1230aee97efSMilanka Ringwald     "voice",
1240aee97efSMilanka Ringwald     "data",
1250aee97efSMilanka Ringwald     "fax"
1260aee97efSMilanka Ringwald };
1270aee97efSMilanka Ringwald 
1280aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1290aee97efSMilanka Ringwald     "not a conference call",
1300aee97efSMilanka Ringwald     "conference call"
1310aee97efSMilanka Ringwald };
1320aee97efSMilanka Ringwald 
1330aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1340aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1350aee97efSMilanka Ringwald     return "not defined";
1360aee97efSMilanka Ringwald }
1370aee97efSMilanka Ringwald 
1380aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1390aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1400aee97efSMilanka Ringwald     return "not defined";
1410aee97efSMilanka Ringwald }
1420aee97efSMilanka Ringwald 
1430aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1440aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1450aee97efSMilanka Ringwald     return "not defined";
1460aee97efSMilanka Ringwald }
1470aee97efSMilanka Ringwald 
1480aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1490aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1500aee97efSMilanka Ringwald     return "not defined";
1510aee97efSMilanka Ringwald }
1520aee97efSMilanka Ringwald 
1531d9c9c90SMilanka Ringwald typedef struct {
1542867ed41SMatthias Ringwald     hfp_role_t local_role;
1551d9c9c90SMilanka Ringwald     bd_addr_t  remote_address;
1561d9c9c90SMilanka Ringwald } hfp_sdp_query_context_t;
1571d9c9c90SMilanka Ringwald 
1581d9c9c90SMilanka Ringwald static hfp_sdp_query_context_t sdp_query_context;
1591d9c9c90SMilanka Ringwald static btstack_context_callback_registration_t hfp_handle_sdp_client_query_request;
1601d9c9c90SMilanka Ringwald 
161ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
162ca59be51SMatthias Ringwald 
1639c9c64c1SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL;
1649c9c64c1SMatthias Ringwald 
165ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
166ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
167ca59be51SMatthias Ringwald 
168ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
169ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
170f4000eebSMatthias Ringwald 
171eddcd308SMatthias Ringwald static hfp_connection_t * sco_establishment_active;
172eddcd308SMatthias Ringwald 
173991c26beSMatthias Ringwald // HFP_SCO_PACKET_TYPES_NONE == no choice/override
17401e5b727SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types;
175991c26beSMatthias Ringwald 
1763427dd75SMatthias Ringwald // prototypes
1773427dd75SMatthias 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);
1783427dd75SMatthias 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     }
2423deb3ec6SMatthias Ringwald     return 1;
2433deb3ec6SMatthias Ringwald }
2443deb3ec6SMatthias Ringwald 
2456a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
246e8d1dc0dSMatthias Ringwald 
247e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
248c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
249e8d1dc0dSMatthias Ringwald 
250df327ff3SMilanka Ringwald     int i;
251df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
252e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
253e8d1dc0dSMatthias Ringwald         return 1;
254df327ff3SMilanka Ringwald     }
255df327ff3SMilanka Ringwald     return 0;
256df327ff3SMilanka Ringwald }
257df327ff3SMilanka Ringwald 
258d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
259d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
260d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
261d715cf51SMatthias Ringwald     int i;
262d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
263d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
264d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
265d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
266d715cf51SMatthias Ringwald     }
267d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2686535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
269d715cf51SMatthias Ringwald }
270d715cf51SMatthias Ringwald 
2713deb3ec6SMatthias Ringwald // UTILS
2723deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2733deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald 
2763deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
2773deb3ec6SMatthias Ringwald     if (value){
2783deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
2793deb3ec6SMatthias Ringwald     } else {
2803deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
2813deb3ec6SMatthias Ringwald     }
2823deb3ec6SMatthias Ringwald     return bitmap;
2833deb3ec6SMatthias Ringwald }
2843deb3ec6SMatthias Ringwald 
2853deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
286c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2873deb3ec6SMatthias Ringwald     int i;
2883deb3ec6SMatthias Ringwald     int offset = 0;
289c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2903deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
2913deb3ec6SMatthias Ringwald     }
2923deb3ec6SMatthias Ringwald     if (i<values_nr){
2933deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
2943deb3ec6SMatthias Ringwald     }
2953deb3ec6SMatthias Ringwald     return offset;
2963deb3ec6SMatthias Ringwald }
2973deb3ec6SMatthias Ringwald 
2983deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
299c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3003deb3ec6SMatthias Ringwald 
3013deb3ec6SMatthias Ringwald     int i;
3023deb3ec6SMatthias Ringwald     int offset = 0;
303c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3043deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
3053deb3ec6SMatthias Ringwald     }
3063deb3ec6SMatthias Ringwald 
3073deb3ec6SMatthias Ringwald     if (i<values_nr){
3083deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3093deb3ec6SMatthias Ringwald     }
3103deb3ec6SMatthias Ringwald     return offset;
3113deb3ec6SMatthias Ringwald }
3123deb3ec6SMatthias Ringwald 
313ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
314ca59be51SMatthias Ringwald     if (!hfp_connection) return;
315ca59be51SMatthias Ringwald     btstack_packet_handler_t callback = NULL;
316ca59be51SMatthias Ringwald     switch (hfp_connection->local_role){
317ca59be51SMatthias Ringwald         case HFP_ROLE_HF:
318ca59be51SMatthias Ringwald             callback = hfp_hf_callback;
319671f1aa2SMatthias Ringwald             break;
320ca59be51SMatthias Ringwald         case HFP_ROLE_AG:
321ca59be51SMatthias Ringwald             callback = hfp_ag_callback;
322671f1aa2SMatthias Ringwald             break;
323ca59be51SMatthias Ringwald         default:
324ca59be51SMatthias Ringwald             return;
325ca59be51SMatthias Ringwald     }
326ca59be51SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
327ca59be51SMatthias Ringwald }
328ca59be51SMatthias Ringwald 
329ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
330a0ffb263SMatthias Ringwald     uint8_t event[3];
331a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
332a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
333a0ffb263SMatthias Ringwald     event[2] = event_subtype;
334ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
335a0ffb263SMatthias Ringwald }
336a0ffb263SMatthias Ringwald 
337ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3383deb3ec6SMatthias Ringwald     uint8_t event[4];
3393deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3403deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3413deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3423deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
343ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3443deb3ec6SMatthias Ringwald }
3453deb3ec6SMatthias Ringwald 
346ca59be51SMatthias 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){
3478901a7c4SMatthias Ringwald     uint8_t event[12];
3486a7f44bdSMilanka Ringwald     int pos = 0;
3496a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
3506a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
351d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
3526a7f44bdSMilanka Ringwald     event[pos++] = status; // status 0 == OK
3536a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
3546a7f44bdSMilanka Ringwald     pos += 2;
3556a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
3566a7f44bdSMilanka Ringwald     pos += 6;
357ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
358a0653c3bSMilanka Ringwald }
359a0653c3bSMilanka Ringwald 
360f2a3f984SMilanka 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){
361d0c4aea6SMilanka Ringwald     uint8_t event[13];
362d0c4aea6SMilanka Ringwald     int pos = 0;
363d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
364d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
365d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
366d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
367d0c4aea6SMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
368d0c4aea6SMilanka Ringwald     pos += 2;
369d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
370d0c4aea6SMilanka Ringwald     pos += 6;
371d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
372ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
373d0c4aea6SMilanka Ringwald }
374d0c4aea6SMilanka Ringwald 
375ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
376c1797c7dSMatthias Ringwald     uint8_t event[40];
377aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
378aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
379aa4dd815SMatthias Ringwald     event[2] = event_subtype;
38009ca35c4SMilanka Ringwald     uint16_t size = btstack_min(strlen(value), sizeof(event) - 4);
381aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
382aa4dd815SMatthias Ringwald     event[3 + size] = 0;
383ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
384aa4dd815SMatthias Ringwald }
385aa4dd815SMatthias Ringwald 
3860cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
3878f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
3883deb3ec6SMatthias Ringwald }
3893deb3ec6SMatthias Ringwald 
3903deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
391665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
392665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
393665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
394a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
395a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
396a0ffb263SMatthias Ringwald             return hfp_connection;
3973deb3ec6SMatthias Ringwald         }
3983deb3ec6SMatthias Ringwald     }
3993deb3ec6SMatthias Ringwald     return NULL;
4003deb3ec6SMatthias Ringwald }
4013deb3ec6SMatthias Ringwald 
402405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
403665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
404665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
405665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
406a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
4075df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
408a0ffb263SMatthias Ringwald             return hfp_connection;
4093deb3ec6SMatthias Ringwald         }
4103deb3ec6SMatthias Ringwald     }
4113deb3ec6SMatthias Ringwald     return NULL;
4123deb3ec6SMatthias Ringwald }
4133deb3ec6SMatthias Ringwald 
414405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
415665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
416665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
417665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
418a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
419c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
420a0ffb263SMatthias Ringwald             return hfp_connection;
4213deb3ec6SMatthias Ringwald         }
4223deb3ec6SMatthias Ringwald     }
4233deb3ec6SMatthias Ringwald     return NULL;
4243deb3ec6SMatthias Ringwald }
4253deb3ec6SMatthias Ringwald 
426405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
427d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
428d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
429d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
430d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
431c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
432d97d752dSMilanka Ringwald             return hfp_connection;
433d97d752dSMilanka Ringwald         }
434d97d752dSMilanka Ringwald     }
435d97d752dSMilanka Ringwald     return NULL;
436d97d752dSMilanka Ringwald }
437d97d752dSMilanka Ringwald 
438a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
439a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
440a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
441a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
4423deb3ec6SMatthias Ringwald 
443dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
4443deb3ec6SMatthias Ringwald 
445a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
446a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
4473deb3ec6SMatthias Ringwald 
448a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
449a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
4503deb3ec6SMatthias Ringwald 
451a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
452a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
4537522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
454a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
4553deb3ec6SMatthias Ringwald 
456a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
457a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
458a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
459a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
4603deb3ec6SMatthias Ringwald }
4613deb3ec6SMatthias Ringwald 
462fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
463a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
464a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
4653deb3ec6SMatthias Ringwald 
466a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
467a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
468a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
469aa4dd815SMatthias Ringwald 
470a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
471a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
4723deb3ec6SMatthias Ringwald 
473d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
474d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
475d751f069SMatthias Ringwald 
476a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
4773deb3ec6SMatthias Ringwald 
478d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
479a0ffb263SMatthias Ringwald     return hfp_connection;
4803deb3ec6SMatthias Ringwald }
4813deb3ec6SMatthias Ringwald 
482a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){
483a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
48409a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
4853deb3ec6SMatthias Ringwald }
4863deb3ec6SMatthias Ringwald 
487323d3000SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){
488405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
489a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
490a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
4916535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
492323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
493bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
494bdf572f4SMatthias Ringwald 
495a0ffb263SMatthias Ringwald     return hfp_connection;
4963deb3ec6SMatthias Ringwald }
4973deb3ec6SMatthias Ringwald 
498aa4dd815SMatthias Ringwald /* @param network.
499aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
500aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
501aa4dd815SMatthias Ringwald  */
5023deb3ec6SMatthias Ringwald 
5033deb3ec6SMatthias Ringwald /* @param suported_features
5043deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5053deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
5063deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
5073deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
5083deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
5093deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5103deb3ec6SMatthias Ringwald  */
5113deb3ec6SMatthias Ringwald  /* Bit position:
5123deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
5133deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5143deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
5153deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
5163deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
5173deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5183deb3ec6SMatthias Ringwald  */
5193deb3ec6SMatthias Ringwald 
5209b1c3b4dSMatthias 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){
5213deb3ec6SMatthias Ringwald     uint8_t* attribute;
5223deb3ec6SMatthias Ringwald     de_create_sequence(service);
5233deb3ec6SMatthias Ringwald 
5243deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
525235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
5269b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
5273deb3ec6SMatthias Ringwald 
5283deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
529235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
5303deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5313deb3ec6SMatthias Ringwald     {
5323deb3ec6SMatthias Ringwald         //  "UUID for Service"
5333deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
534235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
5353deb3ec6SMatthias Ringwald     }
5363deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5373deb3ec6SMatthias Ringwald 
5383deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
539235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
5403deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5413deb3ec6SMatthias Ringwald     {
5423deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
5433deb3ec6SMatthias Ringwald         {
544235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
5453deb3ec6SMatthias Ringwald         }
5463deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
5473deb3ec6SMatthias Ringwald 
5483deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
5493deb3ec6SMatthias Ringwald         {
550235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
5513deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
5523deb3ec6SMatthias Ringwald         }
5533deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
5543deb3ec6SMatthias Ringwald     }
5553deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5563deb3ec6SMatthias Ringwald 
5573deb3ec6SMatthias Ringwald 
5583deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
559235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
5603deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5613deb3ec6SMatthias Ringwald     {
562235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
5633deb3ec6SMatthias Ringwald     }
5643deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5653deb3ec6SMatthias Ringwald 
5663deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
567235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
5683deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5693deb3ec6SMatthias Ringwald     {
5703deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
5713deb3ec6SMatthias Ringwald         {
572235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
5733deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
5743deb3ec6SMatthias Ringwald         }
5753deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
5763deb3ec6SMatthias Ringwald     }
5773deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5783deb3ec6SMatthias Ringwald 
5793deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
5803deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
5813deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
5823deb3ec6SMatthias Ringwald }
5833deb3ec6SMatthias Ringwald 
5846c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
5858a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
5868a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
5878a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
5881d9c9c90SMilanka Ringwald 
5891d9c9c90SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(sdp_query_context.remote_address, sdp_query_context.local_role);
5901d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
5911d9c9c90SMilanka Ringwald         log_info("connection with %s and local role %d not found", sdp_query_context.remote_address, sdp_query_context.local_role);
59284904e95SMilanka Ringwald         return;
59384904e95SMilanka Ringwald     }
5943deb3ec6SMatthias Ringwald 
5950e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
5965611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
597a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
5983deb3ec6SMatthias Ringwald             break;
5995611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
600a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
601a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
602520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
603520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
604520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
605520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
606520c92d5SMatthias Ringwald                         break;
607520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
608520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
609520c92d5SMatthias Ringwald                         break;
610520c92d5SMatthias Ringwald                     default:
6111d9c9c90SMilanka Ringwald                         btstack_assert(false);
612520c92d5SMatthias Ringwald                         return;
613520c92d5SMatthias Ringwald                 }
6141d9c9c90SMilanka Ringwald 
615ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
6161d9c9c90SMilanka Ringwald 
6171d9c9c90SMilanka Ringwald             } else {
61884904e95SMilanka Ringwald                 hfp_connection->state = HFP_IDLE;
619cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
620cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
621cc92f22bSMatthias Ringwald                     // report service not found
622cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
623cc92f22bSMatthias Ringwald                 }
624cc92f22bSMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
625cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
6261d9c9c90SMilanka Ringwald             }
6271d9c9c90SMilanka Ringwald 
6281d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
6291d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
6301d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
6313deb3ec6SMatthias Ringwald             break;
6323deb3ec6SMatthias Ringwald         default:
6333deb3ec6SMatthias Ringwald             break;
6343deb3ec6SMatthias Ringwald     }
6353deb3ec6SMatthias Ringwald }
6363deb3ec6SMatthias Ringwald 
63738200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
63838200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
639eddcd308SMatthias Ringwald 
640eddcd308SMatthias Ringwald     if (!sco_establishment_active){
6413427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
64238200c1dSMilanka Ringwald         return 0;
643eddcd308SMatthias Ringwald     }
644eddcd308SMatthias Ringwald 
6453427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
6463427dd75SMatthias Ringwald     switch (status){
6473427dd75SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
6483427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
6493427dd75SMatthias Ringwald         case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES:
650eddcd308SMatthias Ringwald             break;
6513427dd75SMatthias Ringwald         default:
6523427dd75SMatthias Ringwald             return 0;
6533427dd75SMatthias Ringwald     }
6543427dd75SMatthias Ringwald 
6553427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
6563427dd75SMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(sco_establishment_active->link_setting, sco_establishment_active, false);
6573427dd75SMatthias Ringwald 
6583427dd75SMatthias Ringwald     // handle no valid setting found
6593427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
6603427dd75SMatthias Ringwald         if (sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
6613427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
6627522e673SMatthias Ringwald             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
663bc1b1537SMilanka Ringwald             sco_establishment_active->sco_for_msbc_failed = 1;
664bc1b1537SMilanka Ringwald             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
6657522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
6663427dd75SMatthias Ringwald         } else {
6673427dd75SMatthias Ringwald             // no other options
6683427dd75SMatthias Ringwald             return 0;
669eddcd308SMatthias Ringwald         }
6703427dd75SMatthias Ringwald     }
6713427dd75SMatthias Ringwald 
6723427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
673eddcd308SMatthias Ringwald     sco_establishment_active->establish_audio_connection = 1;
6743427dd75SMatthias Ringwald     sco_establishment_active->link_setting = next_setting;
67538200c1dSMilanka Ringwald     sco_establishment_active = NULL;
67638200c1dSMilanka Ringwald     return 1;
677eddcd308SMatthias Ringwald }
678eddcd308SMatthias Ringwald 
679eddcd308SMatthias Ringwald 
680405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
6815575558eSMilanka Ringwald     UNUSED(packet_type);
6828a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
6835575558eSMilanka Ringwald     UNUSED(size);
6849ec2630cSMatthias Ringwald 
6853deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
68627950165SMatthias Ringwald     hci_con_handle_t handle;
687a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
688674515e8SMatthias Ringwald     uint8_t status;
6893deb3ec6SMatthias Ringwald 
69027950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
691aa4dd815SMatthias Ringwald 
6920e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
6931b005648SMatthias Ringwald 
694011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
6957522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
6967522e673SMatthias Ringwald                 case 0: //  SCO
6977522e673SMatthias Ringwald                 case 2: // eSCO
698011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
699405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
700011577abSMilanka Ringwald                     if (!hfp_connection) break;
701c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
702c169df2fSMatthias Ringwald                         hfp_connection->hf_accept_sco = 2;
703c169df2fSMatthias Ringwald                     } else {
704b72c4a9eSMatthias Ringwald                         hfp_connection->hf_accept_sco = 1;
705c169df2fSMatthias Ringwald                     }
7063721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7073721a235SMatthias Ringwald                     hfp_cc256x_prepare_for_sco(hfp_connection);
7083721a235SMatthias Ringwald #endif
709*689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
710*689d4323SMatthias Ringwald                     hfp_bcm_prepare_for_sco(hfp_connection);
711*689d4323SMatthias Ringwald #endif
712c169df2fSMatthias Ringwald                     log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco);
713719aedb8SMatthias Ringwald                     sco_establishment_active = hfp_connection;
714202c8a4cSMatthias Ringwald                     break;
7157522e673SMatthias Ringwald                 default:
7167522e673SMatthias Ringwald                     break;
7177522e673SMatthias Ringwald             }
718011577abSMilanka Ringwald             break;
7193deb3ec6SMatthias Ringwald 
720eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
721eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
722b87963a5SMatthias Ringwald                 if (sco_establishment_active == NULL) break;
723be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
72438200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
72538200c1dSMilanka Ringwald 
72638200c1dSMilanka Ringwald                 hfp_connection = sco_establishment_active;
72738200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
72838200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
72938200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
73038200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
7316c5b2002SMatthias Ringwald             }
732eddcd308SMatthias Ringwald             break;
733eddcd308SMatthias Ringwald 
7343deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
735b87963a5SMatthias Ringwald             if (sco_establishment_active == NULL) break;
736011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
737405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
738011577abSMilanka Ringwald             if (!hfp_connection) {
739011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
740011577abSMilanka Ringwald                 return;
741011577abSMilanka Ringwald             }
742ce263fc8SMatthias Ringwald 
743011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
74438200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
745b72c4a9eSMatthias Ringwald                 hfp_connection->hf_accept_sco = 0;
74638200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
74738200c1dSMilanka Ringwald 
74838200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
74938200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
75038200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
751aa4dd815SMatthias Ringwald                 break;
752aa4dd815SMatthias Ringwald             }
753aa4dd815SMatthias Ringwald 
754011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
755011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
756011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
757011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
758011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
759011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
7603deb3ec6SMatthias Ringwald 
7613deb3ec6SMatthias Ringwald             switch (link_type){
7623deb3ec6SMatthias Ringwald                 case 0x00:
763aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
7643deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
7653deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
7663deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
7673deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
7683deb3ec6SMatthias Ringwald                     break;
7693deb3ec6SMatthias Ringwald                 case 0x02:
770aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
7713deb3ec6SMatthias Ringwald                     break;
7723deb3ec6SMatthias Ringwald                 default:
7733deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
7743deb3ec6SMatthias Ringwald                     break;
7753deb3ec6SMatthias Ringwald             }
776e0d13a19SMilanka Ringwald 
7773deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
778aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
779e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
780e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
7813deb3ec6SMatthias Ringwald 
782d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
783aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
784d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
785aa4dd815SMatthias Ringwald                 break;
786aa4dd815SMatthias Ringwald             }
787d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
788d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
789d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
79038200c1dSMilanka Ringwald             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
7913deb3ec6SMatthias Ringwald             break;
7923deb3ec6SMatthias Ringwald         }
7933deb3ec6SMatthias Ringwald 
7943deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
795f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
796405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
797aa4dd815SMatthias Ringwald 
798d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
799aa4dd815SMatthias Ringwald 
8003721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
8013721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
8023721a235SMatthias Ringwald #endif
803*689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
804*689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
805*689d4323SMatthias Ringwald #endif
806d751f069SMatthias Ringwald             hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
807d63c37a1SMatthias Ringwald             hfp_connection->release_audio_connection = 0;
808d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
809ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
8100c3047fbSMatthias Ringwald 
8115f053052SMilanka Ringwald             if (hfp_connection->release_slc_connection){
8125f053052SMilanka Ringwald                 hfp_connection->release_slc_connection = 0;
8130c3047fbSMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
8140c3047fbSMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
8155f053052SMilanka Ringwald             }
8163deb3ec6SMatthias Ringwald             break;
8173deb3ec6SMatthias Ringwald 
818fc64f94aSMatthias Ringwald         default:
8193deb3ec6SMatthias Ringwald             break;
8203deb3ec6SMatthias Ringwald     }
8213deb3ec6SMatthias Ringwald }
8223deb3ec6SMatthias Ringwald 
8233721a235SMatthias Ringwald 
82427950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8255575558eSMilanka Ringwald     UNUSED(packet_type);
82627950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8275575558eSMilanka Ringwald     UNUSED(size);
82827950165SMatthias Ringwald 
82927950165SMatthias Ringwald     bd_addr_t event_addr;
83027950165SMatthias Ringwald     uint16_t rfcomm_cid;
83127950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
83227950165SMatthias Ringwald     uint8_t status;
83327950165SMatthias Ringwald 
83427950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
83527950165SMatthias Ringwald 
83627950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
83727950165SMatthias Ringwald 
83827950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
83927950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
84027950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
84127950165SMatthias Ringwald             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role);
84227950165SMatthias Ringwald             if (!hfp_connection){
84327950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
84427950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
84527950165SMatthias Ringwald                 return;
84627950165SMatthias Ringwald             }
84727950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
8484960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
8494960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
85027950165SMatthias Ringwald                 return;
85127950165SMatthias Ringwald             }
85227950165SMatthias Ringwald 
85327950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
85427950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
85527950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
85627950165SMatthias Ringwald             break;
85727950165SMatthias Ringwald 
85827950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
85927950165SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
86027950165SMatthias Ringwald 
86127950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
86227950165SMatthias Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
86327950165SMatthias Ringwald 
864405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
865505f1c30SMatthias Ringwald             if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return;
86627950165SMatthias Ringwald 
86727950165SMatthias Ringwald             if (status) {
86827950165SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
86927950165SMatthias Ringwald                 remove_hfp_connection_context(hfp_connection);
87027950165SMatthias Ringwald             } else {
87127950165SMatthias Ringwald                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
87227950165SMatthias Ringwald                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
87327950165SMatthias Ringwald                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
87427950165SMatthias Ringwald 
87527950165SMatthias Ringwald                 switch (hfp_connection->state){
87627950165SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
87727950165SMatthias Ringwald                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
87827950165SMatthias Ringwald                         break;
87927950165SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
88027950165SMatthias Ringwald                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
88127950165SMatthias Ringwald                         break;
88227950165SMatthias Ringwald                     default:
88327950165SMatthias Ringwald                         break;
88427950165SMatthias Ringwald                 }
88527950165SMatthias Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
88627950165SMatthias Ringwald             }
88727950165SMatthias Ringwald             break;
88827950165SMatthias Ringwald 
88927950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
89027950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
89127950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
89227950165SMatthias Ringwald             if (!hfp_connection) break;
89327950165SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
89427950165SMatthias Ringwald                 hfp_connection->state = HFP_IDLE;
89527950165SMatthias Ringwald                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
89627950165SMatthias Ringwald                 break;
89727950165SMatthias Ringwald             }
89827950165SMatthias Ringwald 
89927950165SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
90027950165SMatthias Ringwald             remove_hfp_connection_context(hfp_connection);
90127950165SMatthias Ringwald             break;
90227950165SMatthias Ringwald 
90327950165SMatthias Ringwald         default:
90427950165SMatthias Ringwald             break;
90527950165SMatthias Ringwald     }
90627950165SMatthias Ringwald }
907aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
90894a27792SMatthias Ringwald 
90994a27792SMatthias Ringwald typedef struct {
91094a27792SMatthias Ringwald     const char * command;
91194a27792SMatthias Ringwald     hfp_command_t command_id;
91294a27792SMatthias Ringwald } hfp_command_entry_t;
91394a27792SMatthias Ringwald 
91494a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
915cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
916cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
917cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
918cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
919cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
92094a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
92194a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
92294a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
923cb33905eSMatthias Ringwald     { "AT+BINP",   HFP_CMD_HF_REQUEST_PHONE_NUMBER },
924cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
925cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
92694a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
92794a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
928cb33905eSMatthias Ringwald     { "AT+BVRA=",  HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
929cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
93094a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
93194a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
932cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
93394a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
93494a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
935cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
936cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
937cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
938cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
939cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
94094a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
94194a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
942cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
943cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
944cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
945cb33905eSMatthias Ringwald     { "AT+VTS:",   HFP_CMD_TRANSMIT_DTMF_CODES },
94694a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
94794a27792SMatthias Ringwald };
94894a27792SMatthias Ringwald 
94994a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
950cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
95194a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
952cb33905eSMatthias Ringwald     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
953cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
954cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
95594a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
956cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
957cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
95894a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
959cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
96084a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
961cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
962cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
963cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
964cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
965cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
966cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
967cb33905eSMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
968cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
969cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
970cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
971cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
97294a27792SMatthias Ringwald };
97394a27792SMatthias Ringwald 
974aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
9753deb3ec6SMatthias Ringwald 
976cb33905eSMatthias Ringwald     // table lookup based on role
97794a27792SMatthias Ringwald     uint16_t num_entries;
97894a27792SMatthias Ringwald     hfp_command_entry_t * table;
97994a27792SMatthias Ringwald     if (isHandsFree == 0){
98094a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
98194a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
9823deb3ec6SMatthias Ringwald     } else {
98394a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
98494a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
98594a27792SMatthias Ringwald     }
986791f0d0aSMatthias Ringwald     // binary search
987791f0d0aSMatthias Ringwald     uint16_t left = 0;
988791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
989791f0d0aSMatthias Ringwald     while (left <= right){
990791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
991791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
99294a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
993791f0d0aSMatthias Ringwald         if (match < 0){
994791f0d0aSMatthias Ringwald             // search term is lower than middle element
99547005182SMilanka Ringwald             if (right == 0) break;
996791f0d0aSMatthias Ringwald             right = middle - 1;
997791f0d0aSMatthias Ringwald         } else if (match == 0){
99894a27792SMatthias Ringwald             return entry->command_id;
999791f0d0aSMatthias Ringwald         } else {
1000791f0d0aSMatthias Ringwald             // search term is higher than middle element
1001791f0d0aSMatthias Ringwald             left = middle + 1;
100294a27792SMatthias Ringwald         }
100394a27792SMatthias Ringwald     }
100494a27792SMatthias Ringwald 
1005cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
10060222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1007cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1008cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1009cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
10103deb3ec6SMatthias Ringwald     }
10113deb3ec6SMatthias Ringwald 
1012cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1013cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1014aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
10153deb3ec6SMatthias Ringwald     }
10163deb3ec6SMatthias Ringwald 
1017cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1018aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1019aa4dd815SMatthias Ringwald     }
10203deb3ec6SMatthias Ringwald 
1021aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
10223deb3ec6SMatthias Ringwald }
10233deb3ec6SMatthias Ringwald 
1024a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
102562c7768eSMatthias Ringwald     if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return;
1026a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1027a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
10283deb3ec6SMatthias Ringwald }
1029a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1030a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
10313deb3ec6SMatthias Ringwald }
10323deb3ec6SMatthias Ringwald 
10333deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1034c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
10353deb3ec6SMatthias Ringwald }
10363deb3ec6SMatthias Ringwald 
103794a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1038a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
10393deb3ec6SMatthias Ringwald }
10401d81c7feSMatthias Ringwald 
10411d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
10421d81c7feSMatthias Ringwald     switch (byte){
10431d81c7feSMatthias Ringwald         case ',':
1044f8301d46SMatthias Ringwald 		case '-':
10451d81c7feSMatthias Ringwald         case ';':
10461d81c7feSMatthias Ringwald         case '(':
10471d81c7feSMatthias Ringwald         case ')':
10481d81c7feSMatthias Ringwald         case '\n':
10491d81c7feSMatthias Ringwald         case '\r':
10503deb3ec6SMatthias Ringwald             break;
10513deb3ec6SMatthias Ringwald         default:
10521d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
10533deb3ec6SMatthias Ringwald             break;
10543deb3ec6SMatthias Ringwald     }
10553deb3ec6SMatthias Ringwald }
10561d81c7feSMatthias Ringwald 
10571d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
10581d81c7feSMatthias Ringwald     switch (byte){
10591d81c7feSMatthias Ringwald         case ',':
1060f8301d46SMatthias Ringwald 		case '-':
10611d81c7feSMatthias Ringwald         case ';':
10621d81c7feSMatthias Ringwald         case '\n':
10631d81c7feSMatthias Ringwald         case '\r':
10641d81c7feSMatthias Ringwald             return true;
1065dd533208SMatthias Ringwald         default:
10661d81c7feSMatthias Ringwald             return false;
10673deb3ec6SMatthias Ringwald     }
10683deb3ec6SMatthias Ringwald }
10693deb3ec6SMatthias Ringwald 
1070d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1071aa4dd815SMatthias Ringwald 
10721dddc4f4SMatthias Ringwald     // handle doubles quotes
10731dddc4f4SMatthias Ringwald     if (byte == '"'){
10741dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1075d6b237acSMatthias Ringwald         return true;
10761dddc4f4SMatthias Ringwald     }
10771dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
10781dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1079d6b237acSMatthias Ringwald         return true;
10801dddc4f4SMatthias Ringwald     }
10813deb3ec6SMatthias Ringwald 
10821dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1083d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
10841dddc4f4SMatthias Ringwald 
1085bfeaf344SMatthias Ringwald     bool processed = true;
1086bfeaf344SMatthias Ringwald 
1087a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1088dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1089d28b2d5dSMilanka Ringwald             switch (byte) {
1090dd533208SMatthias Ringwald                 case '\n':
1091dd533208SMatthias Ringwald                 case '\r':
1092dd533208SMatthias Ringwald                 case ';':
1093bfeaf344SMatthias Ringwald                     // ignore separator
1094bfeaf344SMatthias Ringwald                     break;
1095bfeaf344SMatthias Ringwald                 case ':':
1096bfeaf344SMatthias Ringwald                 case '?':
1097bfeaf344SMatthias Ringwald                     // store separator
1098bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1099dd533208SMatthias Ringwald                     break;
1100d28b2d5dSMilanka Ringwald                 case '=':
1101bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1102dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1103a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1104d6b237acSMatthias Ringwald                     return true;
1105d28b2d5dSMilanka Ringwald                 default:
1106bfeaf344SMatthias Ringwald                     // store if not lookahead
1107d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1108dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1109d6b237acSMatthias Ringwald                         return true;
1110dd533208SMatthias Ringwald                     }
1111bfeaf344SMatthias Ringwald                     // mark as lookahead
1112bfeaf344SMatthias Ringwald                     processed = false;
1113d7f16ff1SMatthias Ringwald                     break;
1114d7f16ff1SMatthias Ringwald             }
1115ce263fc8SMatthias Ringwald 
11161d81c7feSMatthias Ringwald             // ignore empty tokens
1117d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1118dd533208SMatthias Ringwald 
1119bfeaf344SMatthias Ringwald             // parse
1120dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1121aa4dd815SMatthias Ringwald 
112284a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
112384a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1124a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1125aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1126a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1127aa4dd815SMatthias Ringwald                         break;
1128aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1129a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1130aa4dd815SMatthias Ringwald                         break;
1131aa4dd815SMatthias Ringwald                     default:
113284a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1133aa4dd815SMatthias Ringwald                         break;
1134aa4dd815SMatthias Ringwald                 }
1135aa4dd815SMatthias Ringwald             }
11363deb3ec6SMatthias Ringwald 
1137c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1138c0c0437aSMatthias Ringwald 
1139bfeaf344SMatthias Ringwald             // next state
1140bfeaf344SMatthias Ringwald             hfp_connection->found_equal_sign = false;
11411d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1142dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1143dd533208SMatthias Ringwald 
1144bfeaf344SMatthias Ringwald             return processed;
1145dd533208SMatthias Ringwald 
1146bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
11471d81c7feSMatthias Ringwald             // handle empty fields
11481d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1149dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1150dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1151dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
11523d051a53SMatthias Ringwald                 return true;
11533d051a53SMatthias Ringwald             }
11543d051a53SMatthias Ringwald 
11551d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11561d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
11571d81c7feSMatthias Ringwald 
11581d81c7feSMatthias Ringwald             // ignore empty tokens
11593d051a53SMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true;
11603d051a53SMatthias Ringwald 
11613d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
11621d81c7feSMatthias Ringwald 
11631d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
11641d81c7feSMatthias Ringwald 
11651d81c7feSMatthias Ringwald             switch (hfp_connection->command){
11661d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
11671d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
11681d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
11691d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11701d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
11711d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
11721d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
11731d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
11741d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
11751d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
11761d81c7feSMatthias Ringwald                     break;
11771d81c7feSMatthias Ringwald                 default:
11781d81c7feSMatthias Ringwald                     break;
11791d81c7feSMatthias Ringwald             }
11803d051a53SMatthias Ringwald             return true;
11813d051a53SMatthias Ringwald 
1182ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
11831d81c7feSMatthias Ringwald 
11841d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11851d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1186dd533208SMatthias Ringwald 
1187a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1188ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1189a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
11902308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1191ce263fc8SMatthias Ringwald                     break;
1192ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1193a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
11942308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1195ce263fc8SMatthias Ringwald                     break;
1196ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1197ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1198ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
11992308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1200ce263fc8SMatthias Ringwald                     break;
1201ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12022308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1203a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1204a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1205ce263fc8SMatthias Ringwald                     break;
1206ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12072308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1208a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1209ce263fc8SMatthias Ringwald                     break;
1210ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1211a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1212a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
12132308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1214ce263fc8SMatthias Ringwald                     break;
12150222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
12160222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
12170222a807SMatthias Ringwald                     break;
1218ce263fc8SMatthias Ringwald                 default:
1219ce263fc8SMatthias Ringwald                     break;
1220ce263fc8SMatthias Ringwald             }
12211d81c7feSMatthias Ringwald 
12221d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12231d81c7feSMatthias Ringwald 
12241d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
12251d81c7feSMatthias Ringwald 
1226ba0de059SMatthias Ringwald             return true;
1227ce263fc8SMatthias Ringwald 
122874c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
12291d81c7feSMatthias Ringwald 
12301d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
12311d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
12321d81c7feSMatthias Ringwald 
1233a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1234ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
123589425bfcSMilanka Ringwald                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
123689425bfcSMilanka Ringwald                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1237a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1238ce263fc8SMatthias Ringwald                     break;
1239ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12402308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
124125789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
124225789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1243a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1244ce263fc8SMatthias Ringwald                     break;
1245ce263fc8SMatthias Ringwald                 default:
1246ce263fc8SMatthias Ringwald                     break;
1247ce263fc8SMatthias Ringwald             }
12481d81c7feSMatthias Ringwald 
12491d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12501d81c7feSMatthias Ringwald 
12511d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
12521d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
12531d81c7feSMatthias Ringwald             } else {
12541d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
12551d81c7feSMatthias Ringwald             }
1256d6b237acSMatthias Ringwald             return true;
125774c7548aSMatthias Ringwald 
125874c7548aSMatthias Ringwald         default:
125974c7548aSMatthias Ringwald             btstack_assert(false);
126074c7548aSMatthias Ringwald             return true;
126174c7548aSMatthias Ringwald     }
1262d6b237acSMatthias Ringwald }
1263d6b237acSMatthias Ringwald 
1264d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1265d6b237acSMatthias Ringwald     bool processed = false;
1266d6b237acSMatthias Ringwald     while (!processed){
1267d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1268d6b237acSMatthias Ringwald     }
12691d81c7feSMatthias Ringwald     // reset parser state on end-of-line
12701d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
12711d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
12721d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
12731d81c7feSMatthias Ringwald     }
1274ce263fc8SMatthias Ringwald }
1275ce263fc8SMatthias Ringwald 
1276a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1277ce263fc8SMatthias Ringwald     int value;
1278a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1279667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
12802308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1281667ec068SMatthias Ringwald             int i;
1282a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1283667ec068SMatthias Ringwald                 case 0:
1284a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1285a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1286a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1287667ec068SMatthias Ringwald                             break;
1288667ec068SMatthias Ringwald                         }
1289667ec068SMatthias Ringwald                     }
1290667ec068SMatthias Ringwald                     break;
1291667ec068SMatthias Ringwald                 case 1:
1292a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1293a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1294667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1295a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1296667ec068SMatthias Ringwald                     break;
1297667ec068SMatthias Ringwald                 default:
1298667ec068SMatthias Ringwald                     break;
1299667ec068SMatthias Ringwald             }
130025789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1301667ec068SMatthias Ringwald             break;
1302667ec068SMatthias Ringwald 
1303667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1304a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1305667ec068SMatthias Ringwald                 case 0:
1306a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1307a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1308667ec068SMatthias Ringwald                     break;
1309667ec068SMatthias Ringwald                 case 1:
13102308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1311a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1312667ec068SMatthias Ringwald                     break;
1313667ec068SMatthias Ringwald                 default:
1314667ec068SMatthias Ringwald                     break;
1315667ec068SMatthias Ringwald             }
1316eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1317a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1318667ec068SMatthias Ringwald             break;
1319667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1320a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1321667ec068SMatthias Ringwald                 case 0:
13222308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1323a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1324667ec068SMatthias Ringwald                     break;
1325667ec068SMatthias Ringwald                 case 1:
13262308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1327a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1328667ec068SMatthias Ringwald                     break;
1329667ec068SMatthias Ringwald                 case 2:
13302308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1331a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1332667ec068SMatthias Ringwald                     break;
1333667ec068SMatthias Ringwald                 case 3:
13342308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13350aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1336667ec068SMatthias Ringwald                     break;
1337667ec068SMatthias Ringwald                 case 4:
13380aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13390aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
13400aee97efSMilanka Ringwald                     break;
13410aee97efSMilanka Ringwald                 case 5:
1342a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1343a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1344667ec068SMatthias Ringwald                     break;
13450aee97efSMilanka Ringwald                 case 6:
13462308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1347a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1348667ec068SMatthias Ringwald                     break;
1349667ec068SMatthias Ringwald                 default:
1350667ec068SMatthias Ringwald                     break;
1351667ec068SMatthias Ringwald             }
1352eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1353a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1354667ec068SMatthias Ringwald             break;
1355aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
13562308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1357a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1358aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1359aa4dd815SMatthias Ringwald             break;
1360aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
13612308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1362a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1363aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1364aa4dd815SMatthias Ringwald             break;
1365aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
13662308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1367a0ffb263SMatthias Ringwald             hfp_connection->ag_activate_voice_recognition = value;
1368aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1369aa4dd815SMatthias Ringwald             break;
1370aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
13712308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1372a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1373aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1374aa4dd815SMatthias Ringwald             break;
1375aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
13762308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1377a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1378aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1379aa4dd815SMatthias Ringwald             break;
13803deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
13812308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1382a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
13833deb3ec6SMatthias Ringwald             break;
13843deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
13852308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1386a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
13873deb3ec6SMatthias Ringwald             break;
13883deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
13892308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1390bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
13913deb3ec6SMatthias Ringwald             break;
13923deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1393a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
13942308e108SMilanka Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
139525789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1396a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
13973deb3ec6SMatthias Ringwald             break;
1398aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
139989425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
140089425bfcSMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1401a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1402a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1403aa4dd815SMatthias Ringwald             break;
1404aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1405a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
14062308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
140725789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
14083deb3ec6SMatthias Ringwald             break;
14093deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
141025789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1411a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1412a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
14132308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1414a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
14153deb3ec6SMatthias Ringwald             break;
14163deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1417a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1418a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
141997d2cfbcSMatthias 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);
142025789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1421eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
14223deb3ec6SMatthias Ringwald             break;
1423aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1424aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1425a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
14262308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
142725789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1428a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
14293deb3ec6SMatthias Ringwald             break;
1430aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14313deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1432a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
143325789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
14343deb3ec6SMatthias Ringwald             break;
1435ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
143625789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1437a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1438ce263fc8SMatthias Ringwald             break;
14393deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
14403deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1441a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1442a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1443a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1444a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1445ce263fc8SMatthias Ringwald             }
1446a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1447ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1448a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1449ce263fc8SMatthias Ringwald             } else {
14502308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1451a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1452a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1453a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
14543deb3ec6SMatthias Ringwald             }
145525789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
14563deb3ec6SMatthias Ringwald             break;
14573deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14583deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
145925789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1460a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
14613deb3ec6SMatthias Ringwald             break;
1462aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
14632308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1464a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1465aa4dd815SMatthias Ringwald             break;
1466aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1467a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1468a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
14693deb3ec6SMatthias Ringwald                 break;
14703deb3ec6SMatthias Ringwald             }
14713deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1472a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
14733deb3ec6SMatthias Ringwald             break;
14743deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
14753deb3ec6SMatthias Ringwald             break;
14763deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1477a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
14782308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
14793deb3ec6SMatthias Ringwald             break;
14803deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
14812308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1482a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1483a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
14843deb3ec6SMatthias Ringwald             break;
1485ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1486a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1487a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1488a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1489a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
14903deb3ec6SMatthias Ringwald             break;
14910222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
14920222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
14930222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
14940222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
14950222a807SMatthias Ringwald             }
14960222a807SMatthias Ringwald             break;
14970222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
14980222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
14990222a807SMatthias Ringwald             break;
15000222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
15010222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
15020222a807SMatthias Ringwald             break;
15030222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
15040222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
15050222a807SMatthias Ringwald             break;
15060222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
15070222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
15080222a807SMatthias Ringwald             break;
15093deb3ec6SMatthias Ringwald         default:
15103deb3ec6SMatthias Ringwald             break;
15113deb3ec6SMatthias Ringwald     }
15123deb3ec6SMatthias Ringwald }
15133deb3ec6SMatthias Ringwald 
15141d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
15151d9c9c90SMilanka Ringwald     UNUSED(context);
15161d9c9c90SMilanka Ringwald 
15171d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
15181d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
15191d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
15201d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
15211d9c9c90SMilanka Ringwald 
15221d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
15231d9c9c90SMilanka Ringwald 
15241d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
15251d9c9c90SMilanka Ringwald         sdp_query_context.local_role = connection->local_role;
15261d9c9c90SMilanka Ringwald         (void)memcpy(sdp_query_context.remote_address, connection->remote_addr, 6);
1527e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
15281d9c9c90SMilanka Ringwald         return;
15291d9c9c90SMilanka Ringwald     }
15301d9c9c90SMilanka Ringwald }
15311d9c9c90SMilanka Ringwald 
1532323d3000SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
1533323d3000SMatthias Ringwald     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1534a0ffb263SMatthias Ringwald     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
15353deb3ec6SMatthias Ringwald 
1536a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15373deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
15383deb3ec6SMatthias Ringwald         return;
15393deb3ec6SMatthias Ringwald     }
1540a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
15413deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1542a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
15433deb3ec6SMatthias Ringwald             return;
15443deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
1545a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
15463deb3ec6SMatthias Ringwald             return;
15473deb3ec6SMatthias Ringwald         case HFP_IDLE:
15486535961aSMatthias Ringwald             (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
15491d9c9c90SMilanka Ringwald             hfp_connection->state = HFP_W2_SEND_SDP_QUERY;
1550a0ffb263SMatthias Ringwald             hfp_connection->service_uuid = service_uuid;
15511d9c9c90SMilanka Ringwald 
15521d9c9c90SMilanka Ringwald             hfp_handle_sdp_client_query_request.callback = &hfp_handle_start_sdp_client_query;
15531d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
15541d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
15553deb3ec6SMatthias Ringwald             break;
15563deb3ec6SMatthias Ringwald         default:
15573deb3ec6SMatthias Ringwald             break;
15583deb3ec6SMatthias Ringwald     }
15593deb3ec6SMatthias Ringwald }
15603deb3ec6SMatthias Ringwald 
1561a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1562a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1563a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
15643deb3ec6SMatthias Ringwald 
1565a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1566a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_IDLE;
15673deb3ec6SMatthias Ringwald         return;
15683deb3ec6SMatthias Ringwald     }
15693deb3ec6SMatthias Ringwald 
1570a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1571a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
15723deb3ec6SMatthias Ringwald         return;
15733deb3ec6SMatthias Ringwald     }
15743deb3ec6SMatthias Ringwald 
15750c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
15760c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
15770c3047fbSMatthias Ringwald         return;
15780c3047fbSMatthias Ringwald     }
15790c3047fbSMatthias Ringwald 
15800c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
15810c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
15820c3047fbSMatthias Ringwald         return;
15830c3047fbSMatthias Ringwald     }
15840c3047fbSMatthias Ringwald 
15850c3047fbSMatthias Ringwald     // HFP_W4_SCO_DISCONNECTED or later
15865f053052SMilanka Ringwald     hfp_connection->release_slc_connection = 1;
15873deb3ec6SMatthias Ringwald }
15883deb3ec6SMatthias Ringwald 
1589a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1590a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1591a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1592a0ffb263SMatthias Ringwald     hfp_connection->release_audio_connection = 1;
15933deb3ec6SMatthias Ringwald }
15943deb3ec6SMatthias Ringwald 
1595ce263fc8SMatthias Ringwald static const struct link_settings {
1596ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1597ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1598ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1599ebc6f15bSMatthias Ringwald     const bool     eSCO;
1600ebc6f15bSMatthias Ringwald     const uint8_t  codec;
1601ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1602352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1603352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1604352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1605352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1606352a0504SMatthias Ringwald     { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1607352a0504SMatthias Ringwald     { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1608352a0504SMatthias Ringwald     { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1609352a0504SMatthias Ringwald     { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1610ce263fc8SMatthias Ringwald };
16113deb3ec6SMatthias Ringwald 
1612eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1613ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1614eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1615ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1616eddcd308SMatthias Ringwald     sco_establishment_active = hfp_connection;
1617d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1618d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1619*689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1620*689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1621*689d4323SMatthias Ringwald #else
1622*689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1623*689d4323SMatthias Ringwald #endif
1624d2c1d59aSMatthias Ringwald     }
1625352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1626352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1627eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1628352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1629ce263fc8SMatthias Ringwald }
1630d68dcce1SMatthias Ringwald 
16313721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
16323721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
16333721a235SMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
16343721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
16353721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
16363721a235SMatthias Ringwald     }
16373721a235SMatthias Ringwald }
16383721a235SMatthias Ringwald 
16393721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
16403721a235SMatthias Ringwald     uint32_t sample_rate_hz;
16413721a235SMatthias Ringwald     uint16_t clock_rate_khz;
16423721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
16433721a235SMatthias Ringwald         clock_rate_khz = 512;
16443721a235SMatthias Ringwald         sample_rate_hz = 16000;
16453721a235SMatthias Ringwald     } else {
16463721a235SMatthias Ringwald         clock_rate_khz = 256;
16473721a235SMatthias Ringwald         sample_rate_hz = 8000;
16483721a235SMatthias Ringwald     }
16493721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
16503721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
16513721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
16523721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
16533721a235SMatthias Ringwald     uint8_t  reserved = 0;
16543721a235SMatthias Ringwald     uint16_t size = 16;
16553721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
16563721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
16573721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
16583721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
16593721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
16603721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
16613721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
16623721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
16633721a235SMatthias Ringwald }
16643721a235SMatthias Ringwald #endif
16653721a235SMatthias Ringwald 
1666*689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1667*689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1668*689d4323SMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1669*689d4323SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1670*689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
1671*689d4323SMatthias Ringwald     }
1672*689d4323SMatthias Ringwald }
1673*689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1674*689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
1675*689d4323SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 2048 kHz
1676*689d4323SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 4);
1677*689d4323SMatthias Ringwald }
1678*689d4323SMatthias Ringwald #endif
1679*689d4323SMatthias Ringwald 
16801b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
16811b005648SMatthias Ringwald     hfp_hf_callback = callback;
16821b005648SMatthias Ringwald }
16831b005648SMatthias Ringwald 
16841b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
16851b005648SMatthias Ringwald     hfp_ag_callback = callback;
16861b005648SMatthias Ringwald }
16871b005648SMatthias Ringwald 
1688ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1689ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
1690ca59be51SMatthias Ringwald }
16911b005648SMatthias Ringwald 
1692ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1693ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
1694d68dcce1SMatthias Ringwald }
1695d68dcce1SMatthias Ringwald 
1696520c92d5SMatthias Ringwald void hfp_init(void){
1697352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
1698991c26beSMatthias Ringwald }
1699991c26beSMatthias Ringwald 
1700991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
170101e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
1702991c26beSMatthias Ringwald }
1703991c26beSMatthias Ringwald 
1704991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
170501e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
1706520c92d5SMatthias Ringwald }
1707186dd3d2SMatthias Ringwald 
1708e453c1d9SMatthias 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){
1709e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
1710e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
1711e453c1d9SMatthias Ringwald     while (setting > 0){
1712e453c1d9SMatthias Ringwald         setting--;
1713e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
1714e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
1715e453c1d9SMatthias Ringwald         // skip if S4 but not supported
1716e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
1717e453c1d9SMatthias Ringwald         // skip wrong codec
1718e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
171901e5b727SMatthias Ringwald         // skip disabled packet types
1720352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
1721352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
1722352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
172301e5b727SMatthias Ringwald 
1724e453c1d9SMatthias Ringwald         // found matching setting
1725e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
1726afac2a04SMilanka Ringwald     }
1727e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
1728afac2a04SMilanka Ringwald }
1729e453c1d9SMatthias Ringwald 
17302b7abbfbSMatthias 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){
1731e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
1732e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
1733e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
17342b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
17352b7abbfbSMatthias Ringwald }
17362b7abbfbSMatthias Ringwald 
17372b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
1738e453c1d9SMatthias Ringwald     // get highest possible link setting
17392b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
1740afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
1741afac2a04SMilanka Ringwald }
1742afac2a04SMilanka Ringwald 
1743186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
1744186dd3d2SMatthias Ringwald 
1745186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
1746186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1747186dd3d2SMatthias Ringwald     // encode \n\r
1748186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
174915a27967SMatthias Ringwald     int i = 0;
1750186dd3d2SMatthias Ringwald     int pos;
175115a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
1752186dd3d2SMatthias Ringwald         switch (packet[pos]){
1753186dd3d2SMatthias Ringwald             case '\n':
1754186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1755186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
1756186dd3d2SMatthias Ringwald                 break;
1757186dd3d2SMatthias Ringwald             case '\r':
1758186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1759186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
1760186dd3d2SMatthias Ringwald                 break;
1761186dd3d2SMatthias Ringwald             default:
1762186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
1763186dd3d2SMatthias Ringwald                 break;
1764186dd3d2SMatthias Ringwald         }
1765186dd3d2SMatthias Ringwald     }
1766186dd3d2SMatthias Ringwald     printable[i] = 0;
1767186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
1768186dd3d2SMatthias Ringwald #endif
1769186dd3d2SMatthias Ringwald }
1770