xref: /btstack/src/classic/hfp.c (revision f8301d4687442525e7da0541114d006bcde63652)
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 
623deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
633deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald 
663deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
673deb3ec6SMatthias Ringwald     "EC and/or NR function",
683deb3ec6SMatthias Ringwald     "Three-way calling",
693deb3ec6SMatthias Ringwald     "CLI presentation capability",
703deb3ec6SMatthias Ringwald     "Voice recognition activation",
713deb3ec6SMatthias Ringwald     "Remote volume control",
723deb3ec6SMatthias Ringwald 
733deb3ec6SMatthias Ringwald     "Enhanced call status",
743deb3ec6SMatthias Ringwald     "Enhanced call control",
753deb3ec6SMatthias Ringwald 
763deb3ec6SMatthias Ringwald     "Codec negotiation",
773deb3ec6SMatthias Ringwald 
783deb3ec6SMatthias Ringwald     "HF Indicators",
793deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
803deb3ec6SMatthias Ringwald     "Reserved for future definition"
813deb3ec6SMatthias Ringwald };
823deb3ec6SMatthias Ringwald 
833deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
843deb3ec6SMatthias Ringwald     "Three-way calling",
853deb3ec6SMatthias Ringwald     "EC and/or NR function",
863deb3ec6SMatthias Ringwald     "Voice recognition function",
873deb3ec6SMatthias Ringwald     "In-band ring tone capability",
883deb3ec6SMatthias Ringwald     "Attach a number to a voice tag",
893deb3ec6SMatthias Ringwald     "Ability to reject a call",
903deb3ec6SMatthias Ringwald     "Enhanced call status",
913deb3ec6SMatthias Ringwald     "Enhanced call control",
923deb3ec6SMatthias Ringwald     "Extended Error Result Codes",
933deb3ec6SMatthias Ringwald     "Codec negotiation",
943deb3ec6SMatthias Ringwald     "HF Indicators",
953deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
963deb3ec6SMatthias Ringwald     "Reserved for future definition"
973deb3ec6SMatthias Ringwald };
983deb3ec6SMatthias Ringwald 
990aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1000aee97efSMilanka Ringwald     "outgoing",
1010aee97efSMilanka Ringwald     "incoming"
1020aee97efSMilanka Ringwald };
1030aee97efSMilanka Ringwald 
1040aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1050aee97efSMilanka Ringwald     "active",
1060aee97efSMilanka Ringwald     "held",
1070aee97efSMilanka Ringwald     "outgoing dialing",
1080aee97efSMilanka Ringwald     "outgoing alerting",
1090aee97efSMilanka Ringwald     "incoming",
1100aee97efSMilanka Ringwald     "incoming waiting",
1110aee97efSMilanka Ringwald     "call held by response and hold"
1120aee97efSMilanka Ringwald };
1130aee97efSMilanka Ringwald 
1140aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1150aee97efSMilanka Ringwald     "voice",
1160aee97efSMilanka Ringwald     "data",
1170aee97efSMilanka Ringwald     "fax"
1180aee97efSMilanka Ringwald };
1190aee97efSMilanka Ringwald 
1200aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1210aee97efSMilanka Ringwald     "not a conference call",
1220aee97efSMilanka Ringwald     "conference call"
1230aee97efSMilanka Ringwald };
1240aee97efSMilanka Ringwald 
1250aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1260aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1270aee97efSMilanka Ringwald     return "not defined";
1280aee97efSMilanka Ringwald }
1290aee97efSMilanka Ringwald 
1300aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1310aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1320aee97efSMilanka Ringwald     return "not defined";
1330aee97efSMilanka Ringwald }
1340aee97efSMilanka Ringwald 
1350aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1360aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1370aee97efSMilanka Ringwald     return "not defined";
1380aee97efSMilanka Ringwald }
1390aee97efSMilanka Ringwald 
1400aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1410aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1420aee97efSMilanka Ringwald     return "not defined";
1430aee97efSMilanka Ringwald }
1440aee97efSMilanka Ringwald 
1451d9c9c90SMilanka Ringwald typedef struct {
1462867ed41SMatthias Ringwald     hfp_role_t local_role;
1471d9c9c90SMilanka Ringwald     bd_addr_t  remote_address;
1481d9c9c90SMilanka Ringwald } hfp_sdp_query_context_t;
1491d9c9c90SMilanka Ringwald 
1501d9c9c90SMilanka Ringwald static hfp_sdp_query_context_t sdp_query_context;
1511d9c9c90SMilanka Ringwald static btstack_context_callback_registration_t hfp_handle_sdp_client_query_request;
1521d9c9c90SMilanka Ringwald 
153ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
154ca59be51SMatthias Ringwald 
1559c9c64c1SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL;
1569c9c64c1SMatthias Ringwald 
157ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
158ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
159ca59be51SMatthias Ringwald 
160ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
161ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
162f4000eebSMatthias Ringwald 
1631b005648SMatthias Ringwald static void (*hfp_hf_run_for_context)(hfp_connection_t * hfp_connection);
1641b005648SMatthias Ringwald 
165eddcd308SMatthias Ringwald static hfp_connection_t * sco_establishment_active;
166eddcd308SMatthias Ringwald 
167991c26beSMatthias Ringwald // HFP_SCO_PACKET_TYPES_NONE == no choice/override
16801e5b727SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types;
169991c26beSMatthias Ringwald 
1703427dd75SMatthias Ringwald // prototypes
1713427dd75SMatthias 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);
1723427dd75SMatthias Ringwald 
17325789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
17425789943SMilanka Ringwald     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
17525789943SMilanka Ringwald 
17625789943SMilanka Ringwald     if (index > HFP_MAX_NUM_INDICATORS){
17725789943SMilanka Ringwald         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
17825789943SMilanka Ringwald         return HFP_MAX_NUM_INDICATORS - 1;
17925789943SMilanka Ringwald     }
18025789943SMilanka Ringwald 
18125789943SMilanka Ringwald     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
18225789943SMilanka Ringwald     if (index > 0){
18325789943SMilanka Ringwald         index -= 1;
18425789943SMilanka Ringwald     } else {
18525789943SMilanka Ringwald         log_info("ignoring invalid indicator index 0");
18625789943SMilanka Ringwald         return 0;
18725789943SMilanka Ringwald     }
18825789943SMilanka Ringwald     return index;
18925789943SMilanka Ringwald }
19025789943SMilanka Ringwald 
19125789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
19225789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
19325789943SMilanka Ringwald         hfp_connection->parser_item_index++;
19425789943SMilanka Ringwald     } else {
19525789943SMilanka Ringwald         log_info("Ignoring additional indicator");
19625789943SMilanka Ringwald     }
19725789943SMilanka Ringwald }
19825789943SMilanka Ringwald 
19925789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
20025789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
20125789943SMilanka Ringwald         hfp_connection->parser_item_index++;
20225789943SMilanka Ringwald     } else {
20325789943SMilanka Ringwald         log_info("Ignoring additional codec index");
20425789943SMilanka Ringwald     }
20525789943SMilanka Ringwald }
20625789943SMilanka Ringwald 
207eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
208eed67a37SMilanka Ringwald     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
209eed67a37SMilanka Ringwald         hfp_connection->remote_call_services_index++;
210eed67a37SMilanka Ringwald     } else {
211eed67a37SMilanka Ringwald         log_info("Ignoring additional remote_call_services");
212eed67a37SMilanka Ringwald     }
213eed67a37SMilanka Ringwald }
21425789943SMilanka Ringwald 
2153deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
2163deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
2173deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
2183deb3ec6SMatthias Ringwald     }
2193deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
2203deb3ec6SMatthias Ringwald }
2213deb3ec6SMatthias Ringwald 
2223deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
2233deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
2243deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
2253deb3ec6SMatthias Ringwald     }
2263deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
2273deb3ec6SMatthias Ringwald }
2283deb3ec6SMatthias Ringwald 
2293deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
2303deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
23174386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
23228190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
2333deb3ec6SMatthias Ringwald     if (err){
23428190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
2353deb3ec6SMatthias Ringwald     }
2363deb3ec6SMatthias Ringwald     return 1;
2373deb3ec6SMatthias Ringwald }
2383deb3ec6SMatthias Ringwald 
2396a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
240e8d1dc0dSMatthias Ringwald 
241e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
242c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
243e8d1dc0dSMatthias Ringwald 
244df327ff3SMilanka Ringwald     int i;
245df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
246e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
247e8d1dc0dSMatthias Ringwald         return 1;
248df327ff3SMilanka Ringwald     }
249df327ff3SMilanka Ringwald     return 0;
250df327ff3SMilanka Ringwald }
251df327ff3SMilanka Ringwald 
252d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
253d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
254d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
255d715cf51SMatthias Ringwald     int i;
256d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
257d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
258d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
259d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
260d715cf51SMatthias Ringwald     }
261d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2626535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
263d715cf51SMatthias Ringwald }
264d715cf51SMatthias Ringwald 
2653deb3ec6SMatthias Ringwald // UTILS
2663deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2673deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
2683deb3ec6SMatthias Ringwald }
2693deb3ec6SMatthias Ringwald 
2703deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
2713deb3ec6SMatthias Ringwald     if (value){
2723deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
2733deb3ec6SMatthias Ringwald     } else {
2743deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
2753deb3ec6SMatthias Ringwald     }
2763deb3ec6SMatthias Ringwald     return bitmap;
2773deb3ec6SMatthias Ringwald }
2783deb3ec6SMatthias Ringwald 
2793deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
280c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2813deb3ec6SMatthias Ringwald     int i;
2823deb3ec6SMatthias Ringwald     int offset = 0;
283c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2843deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
2853deb3ec6SMatthias Ringwald     }
2863deb3ec6SMatthias Ringwald     if (i<values_nr){
2873deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
2883deb3ec6SMatthias Ringwald     }
2893deb3ec6SMatthias Ringwald     return offset;
2903deb3ec6SMatthias Ringwald }
2913deb3ec6SMatthias Ringwald 
2923deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
293c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2943deb3ec6SMatthias Ringwald 
2953deb3ec6SMatthias Ringwald     int i;
2963deb3ec6SMatthias Ringwald     int offset = 0;
297c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2983deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
2993deb3ec6SMatthias Ringwald     }
3003deb3ec6SMatthias Ringwald 
3013deb3ec6SMatthias Ringwald     if (i<values_nr){
3023deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3033deb3ec6SMatthias Ringwald     }
3043deb3ec6SMatthias Ringwald     return offset;
3053deb3ec6SMatthias Ringwald }
3063deb3ec6SMatthias Ringwald 
307ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
308ca59be51SMatthias Ringwald     if (!hfp_connection) return;
309ca59be51SMatthias Ringwald     btstack_packet_handler_t callback = NULL;
310ca59be51SMatthias Ringwald     switch (hfp_connection->local_role){
311ca59be51SMatthias Ringwald         case HFP_ROLE_HF:
312ca59be51SMatthias Ringwald             callback = hfp_hf_callback;
313671f1aa2SMatthias Ringwald             break;
314ca59be51SMatthias Ringwald         case HFP_ROLE_AG:
315ca59be51SMatthias Ringwald             callback = hfp_ag_callback;
316671f1aa2SMatthias Ringwald             break;
317ca59be51SMatthias Ringwald         default:
318ca59be51SMatthias Ringwald             return;
319ca59be51SMatthias Ringwald     }
320ca59be51SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
321ca59be51SMatthias Ringwald }
322ca59be51SMatthias Ringwald 
323ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
324a0ffb263SMatthias Ringwald     uint8_t event[3];
325a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
326a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
327a0ffb263SMatthias Ringwald     event[2] = event_subtype;
328ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
329a0ffb263SMatthias Ringwald }
330a0ffb263SMatthias Ringwald 
331ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3323deb3ec6SMatthias Ringwald     uint8_t event[4];
3333deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3343deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3353deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3363deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
337ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3383deb3ec6SMatthias Ringwald }
3393deb3ec6SMatthias Ringwald 
340ca59be51SMatthias 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){
3418901a7c4SMatthias Ringwald     uint8_t event[12];
3426a7f44bdSMilanka Ringwald     int pos = 0;
3436a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
3446a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
345d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
3466a7f44bdSMilanka Ringwald     event[pos++] = status; // status 0 == OK
3476a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
3486a7f44bdSMilanka Ringwald     pos += 2;
3496a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
3506a7f44bdSMilanka Ringwald     pos += 6;
351ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
352a0653c3bSMilanka Ringwald }
353a0653c3bSMilanka Ringwald 
354f2a3f984SMilanka 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){
355d0c4aea6SMilanka Ringwald     uint8_t event[13];
356d0c4aea6SMilanka Ringwald     int pos = 0;
357d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
358d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
359d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
360d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
361d0c4aea6SMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
362d0c4aea6SMilanka Ringwald     pos += 2;
363d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
364d0c4aea6SMilanka Ringwald     pos += 6;
365d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
366ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
367d0c4aea6SMilanka Ringwald }
368d0c4aea6SMilanka Ringwald 
369ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
370c1797c7dSMatthias Ringwald     uint8_t event[40];
371aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
372aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
373aa4dd815SMatthias Ringwald     event[2] = event_subtype;
37409ca35c4SMilanka Ringwald     uint16_t size = btstack_min(strlen(value), sizeof(event) - 4);
375aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
376aa4dd815SMatthias Ringwald     event[3 + size] = 0;
377ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
378aa4dd815SMatthias Ringwald }
379aa4dd815SMatthias Ringwald 
3800cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
3818f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
3823deb3ec6SMatthias Ringwald }
3833deb3ec6SMatthias Ringwald 
3843deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
385665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
386665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
387665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
388a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
389a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
390a0ffb263SMatthias Ringwald             return hfp_connection;
3913deb3ec6SMatthias Ringwald         }
3923deb3ec6SMatthias Ringwald     }
3933deb3ec6SMatthias Ringwald     return NULL;
3943deb3ec6SMatthias Ringwald }
3953deb3ec6SMatthias Ringwald 
396405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
397665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
398665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
399665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
400a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
4015df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
402a0ffb263SMatthias Ringwald             return hfp_connection;
4033deb3ec6SMatthias Ringwald         }
4043deb3ec6SMatthias Ringwald     }
4053deb3ec6SMatthias Ringwald     return NULL;
4063deb3ec6SMatthias Ringwald }
4073deb3ec6SMatthias Ringwald 
408405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
409665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
410665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
411665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
412a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
413c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
414a0ffb263SMatthias Ringwald             return hfp_connection;
4153deb3ec6SMatthias Ringwald         }
4163deb3ec6SMatthias Ringwald     }
4173deb3ec6SMatthias Ringwald     return NULL;
4183deb3ec6SMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
420405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
421d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
422d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
423d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
424d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
425c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
426d97d752dSMilanka Ringwald             return hfp_connection;
427d97d752dSMilanka Ringwald         }
428d97d752dSMilanka Ringwald     }
429d97d752dSMilanka Ringwald     return NULL;
430d97d752dSMilanka Ringwald }
431d97d752dSMilanka Ringwald 
432a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
433a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
434a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
435a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
4363deb3ec6SMatthias Ringwald 
437dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
4383deb3ec6SMatthias Ringwald 
439a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
440a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
4413deb3ec6SMatthias Ringwald 
442a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
443a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
4443deb3ec6SMatthias Ringwald 
445a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
446a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
4477522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
448a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
4493deb3ec6SMatthias Ringwald 
450a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
451a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
452a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
453a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
4543deb3ec6SMatthias Ringwald }
4553deb3ec6SMatthias Ringwald 
456fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
457a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
458a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
4593deb3ec6SMatthias Ringwald 
460a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
461a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
462a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
463aa4dd815SMatthias Ringwald 
464a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
465a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
4663deb3ec6SMatthias Ringwald 
467d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
468d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
469d751f069SMatthias Ringwald 
470a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
4713deb3ec6SMatthias Ringwald 
472d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
473a0ffb263SMatthias Ringwald     return hfp_connection;
4743deb3ec6SMatthias Ringwald }
4753deb3ec6SMatthias Ringwald 
476a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){
477a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
47809a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
4793deb3ec6SMatthias Ringwald }
4803deb3ec6SMatthias Ringwald 
481323d3000SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){
482405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
483a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
484a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
4856535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
486323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
487bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
488bdf572f4SMatthias Ringwald 
489a0ffb263SMatthias Ringwald     return hfp_connection;
4903deb3ec6SMatthias Ringwald }
4913deb3ec6SMatthias Ringwald 
492aa4dd815SMatthias Ringwald /* @param network.
493aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
494aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
495aa4dd815SMatthias Ringwald  */
4963deb3ec6SMatthias Ringwald 
4973deb3ec6SMatthias Ringwald /* @param suported_features
4983deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
4993deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
5003deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
5013deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
5023deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
5033deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5043deb3ec6SMatthias Ringwald  */
5053deb3ec6SMatthias Ringwald  /* Bit position:
5063deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
5073deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5083deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
5093deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
5103deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
5113deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5123deb3ec6SMatthias Ringwald  */
5133deb3ec6SMatthias Ringwald 
5149b1c3b4dSMatthias 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){
5153deb3ec6SMatthias Ringwald     uint8_t* attribute;
5163deb3ec6SMatthias Ringwald     de_create_sequence(service);
5173deb3ec6SMatthias Ringwald 
5183deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
519235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
5209b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
5213deb3ec6SMatthias Ringwald 
5223deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
523235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
5243deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5253deb3ec6SMatthias Ringwald     {
5263deb3ec6SMatthias Ringwald         //  "UUID for Service"
5273deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
528235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
5293deb3ec6SMatthias Ringwald     }
5303deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5313deb3ec6SMatthias Ringwald 
5323deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
533235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
5343deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5353deb3ec6SMatthias Ringwald     {
5363deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
5373deb3ec6SMatthias Ringwald         {
538235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
5393deb3ec6SMatthias Ringwald         }
5403deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
5413deb3ec6SMatthias Ringwald 
5423deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
5433deb3ec6SMatthias Ringwald         {
544235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
5453deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
5463deb3ec6SMatthias Ringwald         }
5473deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
5483deb3ec6SMatthias Ringwald     }
5493deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5503deb3ec6SMatthias Ringwald 
5513deb3ec6SMatthias Ringwald 
5523deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
553235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
5543deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5553deb3ec6SMatthias Ringwald     {
556235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
5573deb3ec6SMatthias Ringwald     }
5583deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5593deb3ec6SMatthias Ringwald 
5603deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
561235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
5623deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5633deb3ec6SMatthias Ringwald     {
5643deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
5653deb3ec6SMatthias Ringwald         {
566235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
5673deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
5683deb3ec6SMatthias Ringwald         }
5693deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
5703deb3ec6SMatthias Ringwald     }
5713deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5723deb3ec6SMatthias Ringwald 
5733deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
5743deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
5753deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
5763deb3ec6SMatthias Ringwald }
5773deb3ec6SMatthias Ringwald 
5786c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
5798a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
5808a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
5818a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
5821d9c9c90SMilanka Ringwald 
5831d9c9c90SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(sdp_query_context.remote_address, sdp_query_context.local_role);
5841d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
5851d9c9c90SMilanka Ringwald         log_info("connection with %s and local role %d not found", sdp_query_context.remote_address, sdp_query_context.local_role);
58684904e95SMilanka Ringwald         return;
58784904e95SMilanka Ringwald     }
5883deb3ec6SMatthias Ringwald 
5890e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
5905611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
591a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
5923deb3ec6SMatthias Ringwald             break;
5935611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
594a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
595a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
596520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
597520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
598520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
599520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
600520c92d5SMatthias Ringwald                         break;
601520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
602520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
603520c92d5SMatthias Ringwald                         break;
604520c92d5SMatthias Ringwald                     default:
6051d9c9c90SMilanka Ringwald                         btstack_assert(false);
606520c92d5SMatthias Ringwald                         return;
607520c92d5SMatthias Ringwald                 }
6081d9c9c90SMilanka Ringwald 
609ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
6101d9c9c90SMilanka Ringwald 
6111d9c9c90SMilanka Ringwald             } else {
61284904e95SMilanka Ringwald                 hfp_connection->state = HFP_IDLE;
613ca59be51SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, sdp_event_query_complete_get_status(packet), HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
6141d9c9c90SMilanka Ringwald                 log_info("rfcomm service not found, status 0x%02x", sdp_event_query_complete_get_status(packet));
6151d9c9c90SMilanka Ringwald             }
6161d9c9c90SMilanka Ringwald 
6171d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
6181d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
6191d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
6203deb3ec6SMatthias Ringwald             break;
6213deb3ec6SMatthias Ringwald         default:
6223deb3ec6SMatthias Ringwald             break;
6233deb3ec6SMatthias Ringwald     }
6243deb3ec6SMatthias Ringwald }
6253deb3ec6SMatthias Ringwald 
62638200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
62738200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
628eddcd308SMatthias Ringwald 
629eddcd308SMatthias Ringwald     if (!sco_establishment_active){
6303427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
63138200c1dSMilanka Ringwald         return 0;
632eddcd308SMatthias Ringwald     }
633eddcd308SMatthias Ringwald 
6343427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
6353427dd75SMatthias Ringwald     switch (status){
6363427dd75SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
6373427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
6383427dd75SMatthias Ringwald         case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES:
639eddcd308SMatthias Ringwald             break;
6403427dd75SMatthias Ringwald         default:
6413427dd75SMatthias Ringwald             return 0;
6423427dd75SMatthias Ringwald     }
6433427dd75SMatthias Ringwald 
6443427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
6453427dd75SMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(sco_establishment_active->link_setting, sco_establishment_active, false);
6463427dd75SMatthias Ringwald 
6473427dd75SMatthias Ringwald     // handle no valid setting found
6483427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
6493427dd75SMatthias Ringwald         if (sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
6503427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
6517522e673SMatthias Ringwald             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
652bc1b1537SMilanka Ringwald             sco_establishment_active->sco_for_msbc_failed = 1;
653bc1b1537SMilanka Ringwald             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
6547522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
6553427dd75SMatthias Ringwald         } else {
6563427dd75SMatthias Ringwald             // no other options
6573427dd75SMatthias Ringwald             return 0;
658eddcd308SMatthias Ringwald         }
6593427dd75SMatthias Ringwald     }
6603427dd75SMatthias Ringwald 
6613427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
662eddcd308SMatthias Ringwald     sco_establishment_active->establish_audio_connection = 1;
6633427dd75SMatthias Ringwald     sco_establishment_active->link_setting = next_setting;
66438200c1dSMilanka Ringwald     sco_establishment_active = NULL;
66538200c1dSMilanka Ringwald     return 1;
666eddcd308SMatthias Ringwald }
667eddcd308SMatthias Ringwald 
668eddcd308SMatthias Ringwald 
669405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
6705575558eSMilanka Ringwald     UNUSED(packet_type);
6718a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
6725575558eSMilanka Ringwald     UNUSED(size);
6739ec2630cSMatthias Ringwald 
6743deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
67527950165SMatthias Ringwald     hci_con_handle_t handle;
676a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
677674515e8SMatthias Ringwald     uint8_t status;
6783deb3ec6SMatthias Ringwald 
67927950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
680aa4dd815SMatthias Ringwald 
6810e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
6821b005648SMatthias Ringwald 
683011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
6847522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
6857522e673SMatthias Ringwald                 case 0: //  SCO
6867522e673SMatthias Ringwald                 case 2: // eSCO
687011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
688405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
689011577abSMilanka Ringwald                     if (!hfp_connection) break;
690c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
691c169df2fSMatthias Ringwald                         hfp_connection->hf_accept_sco = 2;
692c169df2fSMatthias Ringwald                     } else {
693b72c4a9eSMatthias Ringwald                         hfp_connection->hf_accept_sco = 1;
694c169df2fSMatthias Ringwald                     }
695c169df2fSMatthias Ringwald                     log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco);
696719aedb8SMatthias Ringwald                     sco_establishment_active = hfp_connection;
6971b005648SMatthias Ringwald                     if (!hfp_hf_run_for_context) break;
6981b005648SMatthias Ringwald                     (*hfp_hf_run_for_context)(hfp_connection);
699202c8a4cSMatthias Ringwald                     break;
7007522e673SMatthias Ringwald                 default:
7017522e673SMatthias Ringwald                     break;
7027522e673SMatthias Ringwald             }
703011577abSMilanka Ringwald             break;
7043deb3ec6SMatthias Ringwald 
705eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
706eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
707b87963a5SMatthias Ringwald                 if (sco_establishment_active == NULL) break;
708be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
70938200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
71038200c1dSMilanka Ringwald 
71138200c1dSMilanka Ringwald                 hfp_connection = sco_establishment_active;
71238200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
71338200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
71438200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
71538200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
7166c5b2002SMatthias Ringwald             }
717eddcd308SMatthias Ringwald             break;
718eddcd308SMatthias Ringwald 
7193deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
720b87963a5SMatthias Ringwald             if (sco_establishment_active == NULL) break;
721011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
722405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
723011577abSMilanka Ringwald             if (!hfp_connection) {
724011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
725011577abSMilanka Ringwald                 return;
726011577abSMilanka Ringwald             }
727ce263fc8SMatthias Ringwald 
728011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
72938200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
730b72c4a9eSMatthias Ringwald                 hfp_connection->hf_accept_sco = 0;
73138200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
73238200c1dSMilanka Ringwald 
73338200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
73438200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
73538200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
736aa4dd815SMatthias Ringwald                 break;
737aa4dd815SMatthias Ringwald             }
738aa4dd815SMatthias Ringwald 
739011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
740011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
741011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
742011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
743011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
744011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
745011577abSMilanka Ringwald             uint8_t  air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet);
7463deb3ec6SMatthias Ringwald 
7473deb3ec6SMatthias Ringwald             switch (link_type){
7483deb3ec6SMatthias Ringwald                 case 0x00:
749aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
7503deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
7513deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
7523deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
7533deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
7543deb3ec6SMatthias Ringwald                     break;
7553deb3ec6SMatthias Ringwald                 case 0x02:
756aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
7573deb3ec6SMatthias Ringwald                     break;
7583deb3ec6SMatthias Ringwald                 default:
7593deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
7603deb3ec6SMatthias Ringwald                     break;
7613deb3ec6SMatthias Ringwald             }
7623deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
763aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
764aa4dd815SMatthias Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
7653deb3ec6SMatthias Ringwald 
76638200c1dSMilanka Ringwald             // hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
7673deb3ec6SMatthias Ringwald 
76838200c1dSMilanka Ringwald             // if (!hfp_connection) {
76938200c1dSMilanka Ringwald             //     log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr));
77038200c1dSMilanka Ringwald             //     break;
77138200c1dSMilanka Ringwald             // }
7723deb3ec6SMatthias Ringwald 
773d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
774aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
775d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
776aa4dd815SMatthias Ringwald                 break;
777aa4dd815SMatthias Ringwald             }
778d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
779d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
780d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
78138200c1dSMilanka Ringwald             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
7823deb3ec6SMatthias Ringwald             break;
7833deb3ec6SMatthias Ringwald         }
7843deb3ec6SMatthias Ringwald 
7853deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
786f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
787405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
788aa4dd815SMatthias Ringwald 
789d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
790aa4dd815SMatthias Ringwald 
791d751f069SMatthias Ringwald             hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
792d63c37a1SMatthias Ringwald             hfp_connection->release_audio_connection = 0;
793d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
794ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
7950c3047fbSMatthias Ringwald 
7965f053052SMilanka Ringwald             if (hfp_connection->release_slc_connection){
7975f053052SMilanka Ringwald                 hfp_connection->release_slc_connection = 0;
7980c3047fbSMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
7990c3047fbSMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
8005f053052SMilanka Ringwald             }
8013deb3ec6SMatthias Ringwald             break;
8023deb3ec6SMatthias Ringwald 
803fc64f94aSMatthias Ringwald         default:
8043deb3ec6SMatthias Ringwald             break;
8053deb3ec6SMatthias Ringwald     }
8063deb3ec6SMatthias Ringwald }
8073deb3ec6SMatthias Ringwald 
80827950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
8095575558eSMilanka Ringwald     UNUSED(packet_type);
81027950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
8115575558eSMilanka Ringwald     UNUSED(size);
81227950165SMatthias Ringwald 
81327950165SMatthias Ringwald     bd_addr_t event_addr;
81427950165SMatthias Ringwald     uint16_t rfcomm_cid;
81527950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
81627950165SMatthias Ringwald     uint8_t status;
81727950165SMatthias Ringwald 
81827950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
81927950165SMatthias Ringwald 
82027950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
82127950165SMatthias Ringwald 
82227950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
82327950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
82427950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
82527950165SMatthias Ringwald             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role);
82627950165SMatthias Ringwald             if (!hfp_connection){
82727950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
82827950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
82927950165SMatthias Ringwald                 return;
83027950165SMatthias Ringwald             }
83127950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
8324960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
8334960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
83427950165SMatthias Ringwald                 return;
83527950165SMatthias Ringwald             }
83627950165SMatthias Ringwald 
83727950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
83827950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
83927950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
84027950165SMatthias Ringwald             break;
84127950165SMatthias Ringwald 
84227950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
84327950165SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
84427950165SMatthias Ringwald 
84527950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
84627950165SMatthias Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
84727950165SMatthias Ringwald 
848405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
849505f1c30SMatthias Ringwald             if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return;
85027950165SMatthias Ringwald 
85127950165SMatthias Ringwald             if (status) {
85227950165SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
85327950165SMatthias Ringwald                 remove_hfp_connection_context(hfp_connection);
85427950165SMatthias Ringwald             } else {
85527950165SMatthias Ringwald                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
85627950165SMatthias Ringwald                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
85727950165SMatthias Ringwald                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
85827950165SMatthias Ringwald 
85927950165SMatthias Ringwald                 switch (hfp_connection->state){
86027950165SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
86127950165SMatthias Ringwald                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
86227950165SMatthias Ringwald                         break;
86327950165SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
86427950165SMatthias Ringwald                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
86527950165SMatthias Ringwald                         break;
86627950165SMatthias Ringwald                     default:
86727950165SMatthias Ringwald                         break;
86827950165SMatthias Ringwald                 }
86927950165SMatthias Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
87027950165SMatthias Ringwald             }
87127950165SMatthias Ringwald             break;
87227950165SMatthias Ringwald 
87327950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
87427950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
87527950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
87627950165SMatthias Ringwald             if (!hfp_connection) break;
87727950165SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
87827950165SMatthias Ringwald                 hfp_connection->state = HFP_IDLE;
87927950165SMatthias Ringwald                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
88027950165SMatthias Ringwald                 break;
88127950165SMatthias Ringwald             }
88227950165SMatthias Ringwald 
88327950165SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
88427950165SMatthias Ringwald             remove_hfp_connection_context(hfp_connection);
88527950165SMatthias Ringwald             break;
88627950165SMatthias Ringwald 
88727950165SMatthias Ringwald         default:
88827950165SMatthias Ringwald             break;
88927950165SMatthias Ringwald     }
89027950165SMatthias Ringwald }
891aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
89294a27792SMatthias Ringwald 
89394a27792SMatthias Ringwald typedef struct {
89494a27792SMatthias Ringwald     const char * command;
89594a27792SMatthias Ringwald     hfp_command_t command_id;
89694a27792SMatthias Ringwald } hfp_command_entry_t;
89794a27792SMatthias Ringwald 
89894a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
899cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
900cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
901cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
902cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
903cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
90494a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
90594a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
90694a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
907cb33905eSMatthias Ringwald     { "AT+BINP",   HFP_CMD_HF_REQUEST_PHONE_NUMBER },
908cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
909cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
91094a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
91194a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
912cb33905eSMatthias Ringwald     { "AT+BVRA=",  HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
913cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
91494a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
91594a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
916cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
91794a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
91894a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
919cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
920cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
921cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
922cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
923cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
92494a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
92594a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
926cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
927cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
928cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
929cb33905eSMatthias Ringwald     { "AT+VTS:",   HFP_CMD_TRANSMIT_DTMF_CODES },
93094a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
93194a27792SMatthias Ringwald };
93294a27792SMatthias Ringwald 
93394a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
934cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
93594a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
936cb33905eSMatthias Ringwald     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
937cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
938cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
93994a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
940cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
941cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
94294a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
943cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
94484a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
945cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
946cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
947cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
948cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
949cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
950cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
951cb33905eSMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
952cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
953cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
954cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
955cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
95694a27792SMatthias Ringwald };
95794a27792SMatthias Ringwald 
958aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
9593deb3ec6SMatthias Ringwald 
960cb33905eSMatthias Ringwald     // table lookup based on role
96194a27792SMatthias Ringwald     uint16_t num_entries;
96294a27792SMatthias Ringwald     hfp_command_entry_t * table;
96394a27792SMatthias Ringwald     if (isHandsFree == 0){
96494a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
96594a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
9663deb3ec6SMatthias Ringwald     } else {
96794a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
96894a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
96994a27792SMatthias Ringwald     }
970791f0d0aSMatthias Ringwald     // binary search
971791f0d0aSMatthias Ringwald     uint16_t left = 0;
972791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
973791f0d0aSMatthias Ringwald     while (left <= right){
974791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
975791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
97694a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
977791f0d0aSMatthias Ringwald         if (match < 0){
978791f0d0aSMatthias Ringwald             // search term is lower than middle element
97947005182SMilanka Ringwald             if (right == 0) break;
980791f0d0aSMatthias Ringwald             right = middle - 1;
981791f0d0aSMatthias Ringwald         } else if (match == 0){
98294a27792SMatthias Ringwald             return entry->command_id;
983791f0d0aSMatthias Ringwald         } else {
984791f0d0aSMatthias Ringwald             // search term is higher than middle element
985791f0d0aSMatthias Ringwald             left = middle + 1;
98694a27792SMatthias Ringwald         }
98794a27792SMatthias Ringwald     }
98894a27792SMatthias Ringwald 
989cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
9900222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
991cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
992cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
993cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
9943deb3ec6SMatthias Ringwald     }
9953deb3ec6SMatthias Ringwald 
996cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
997cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
998aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
9993deb3ec6SMatthias Ringwald     }
10003deb3ec6SMatthias Ringwald 
1001cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1002aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1003aa4dd815SMatthias Ringwald     }
10043deb3ec6SMatthias Ringwald 
1005aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
10063deb3ec6SMatthias Ringwald }
10073deb3ec6SMatthias Ringwald 
1008a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
100962c7768eSMatthias Ringwald     if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return;
1010a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1011a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
10123deb3ec6SMatthias Ringwald }
1013a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1014a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
10153deb3ec6SMatthias Ringwald }
10163deb3ec6SMatthias Ringwald 
10173deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1018c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
10193deb3ec6SMatthias Ringwald }
10203deb3ec6SMatthias Ringwald 
102194a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1022a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
10233deb3ec6SMatthias Ringwald }
10241d81c7feSMatthias Ringwald 
10251d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
10261d81c7feSMatthias Ringwald     switch (byte){
10271d81c7feSMatthias Ringwald         case ',':
1028*f8301d46SMatthias Ringwald 		case '-':
10291d81c7feSMatthias Ringwald         case ';':
10301d81c7feSMatthias Ringwald         case '(':
10311d81c7feSMatthias Ringwald         case ')':
10321d81c7feSMatthias Ringwald         case '\n':
10331d81c7feSMatthias Ringwald         case '\r':
10343deb3ec6SMatthias Ringwald             break;
10353deb3ec6SMatthias Ringwald         default:
10361d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
10373deb3ec6SMatthias Ringwald             break;
10383deb3ec6SMatthias Ringwald     }
10393deb3ec6SMatthias Ringwald }
10401d81c7feSMatthias Ringwald 
10411d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
10421d81c7feSMatthias Ringwald     switch (byte){
10431d81c7feSMatthias Ringwald         case ',':
1044*f8301d46SMatthias Ringwald 		case '-':
10451d81c7feSMatthias Ringwald         case ';':
10461d81c7feSMatthias Ringwald         case '\n':
10471d81c7feSMatthias Ringwald         case '\r':
10481d81c7feSMatthias Ringwald             return true;
1049dd533208SMatthias Ringwald         default:
10501d81c7feSMatthias Ringwald             return false;
10513deb3ec6SMatthias Ringwald     }
10523deb3ec6SMatthias Ringwald }
10533deb3ec6SMatthias Ringwald 
1054d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1055aa4dd815SMatthias Ringwald 
10561dddc4f4SMatthias Ringwald     // handle doubles quotes
10571dddc4f4SMatthias Ringwald     if (byte == '"'){
10581dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1059d6b237acSMatthias Ringwald         return true;
10601dddc4f4SMatthias Ringwald     }
10611dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
10621dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1063d6b237acSMatthias Ringwald         return true;
10641dddc4f4SMatthias Ringwald     }
10653deb3ec6SMatthias Ringwald 
10661dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1067d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
10681dddc4f4SMatthias Ringwald 
1069bfeaf344SMatthias Ringwald     bool processed = true;
1070bfeaf344SMatthias Ringwald 
1071a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1072dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1073d28b2d5dSMilanka Ringwald             switch (byte) {
1074dd533208SMatthias Ringwald                 case '\n':
1075dd533208SMatthias Ringwald                 case '\r':
1076dd533208SMatthias Ringwald                 case ';':
1077bfeaf344SMatthias Ringwald                     // ignore separator
1078bfeaf344SMatthias Ringwald                     break;
1079bfeaf344SMatthias Ringwald                 case ':':
1080bfeaf344SMatthias Ringwald                 case '?':
1081bfeaf344SMatthias Ringwald                     // store separator
1082bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1083dd533208SMatthias Ringwald                     break;
1084d28b2d5dSMilanka Ringwald                 case '=':
1085bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1086dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1087a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1088d6b237acSMatthias Ringwald                     return true;
1089d28b2d5dSMilanka Ringwald                 default:
1090bfeaf344SMatthias Ringwald                     // store if not lookahead
1091d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1092dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1093d6b237acSMatthias Ringwald                         return true;
1094dd533208SMatthias Ringwald                     }
1095bfeaf344SMatthias Ringwald                     // mark as lookahead
1096bfeaf344SMatthias Ringwald                     processed = false;
1097d7f16ff1SMatthias Ringwald                     break;
1098d7f16ff1SMatthias Ringwald             }
1099ce263fc8SMatthias Ringwald 
11001d81c7feSMatthias Ringwald             // ignore empty tokens
1101d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1102dd533208SMatthias Ringwald 
1103bfeaf344SMatthias Ringwald             // parse
1104dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1105aa4dd815SMatthias Ringwald 
110684a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
110784a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1108a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1109aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1110a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1111aa4dd815SMatthias Ringwald                         break;
1112aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1113a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1114aa4dd815SMatthias Ringwald                         break;
1115aa4dd815SMatthias Ringwald                     default:
111684a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1117aa4dd815SMatthias Ringwald                         break;
1118aa4dd815SMatthias Ringwald                 }
1119aa4dd815SMatthias Ringwald             }
11203deb3ec6SMatthias Ringwald 
1121c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1122c0c0437aSMatthias Ringwald 
1123bfeaf344SMatthias Ringwald             // next state
1124bfeaf344SMatthias Ringwald             hfp_connection->found_equal_sign = false;
11251d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1126dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1127dd533208SMatthias Ringwald 
1128bfeaf344SMatthias Ringwald             return processed;
1129dd533208SMatthias Ringwald 
1130bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
11311d81c7feSMatthias Ringwald             // handle empty fields
11321d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1133dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1134dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1135dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
11363d051a53SMatthias Ringwald                 return true;
11373d051a53SMatthias Ringwald             }
11383d051a53SMatthias Ringwald 
11391d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11401d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
11411d81c7feSMatthias Ringwald 
11421d81c7feSMatthias Ringwald             // ignore empty tokens
11433d051a53SMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true;
11443d051a53SMatthias Ringwald 
11453d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
11461d81c7feSMatthias Ringwald 
11471d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
11481d81c7feSMatthias Ringwald 
11491d81c7feSMatthias Ringwald             switch (hfp_connection->command){
11501d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
11511d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
11521d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
11531d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11541d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
11551d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
11561d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
11571d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
11581d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
11591d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
11601d81c7feSMatthias Ringwald                     break;
11611d81c7feSMatthias Ringwald                 default:
11621d81c7feSMatthias Ringwald                     break;
11631d81c7feSMatthias Ringwald             }
11643d051a53SMatthias Ringwald             return true;
11653d051a53SMatthias Ringwald 
1166ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
11671d81c7feSMatthias Ringwald 
11681d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11691d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1170dd533208SMatthias Ringwald 
1171a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1172ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1173a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
11742308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1175ce263fc8SMatthias Ringwald                     break;
1176ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1177a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
11782308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1179ce263fc8SMatthias Ringwald                     break;
1180ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1181ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1182ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
11832308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1184ce263fc8SMatthias Ringwald                     break;
1185ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11862308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1187a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1188a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1189ce263fc8SMatthias Ringwald                     break;
1190ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
11912308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1192a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1193ce263fc8SMatthias Ringwald                     break;
1194ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1195a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1196a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
11972308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1198ce263fc8SMatthias Ringwald                     break;
11990222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
12000222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
12010222a807SMatthias Ringwald                     break;
1202ce263fc8SMatthias Ringwald                 default:
1203ce263fc8SMatthias Ringwald                     break;
1204ce263fc8SMatthias Ringwald             }
12051d81c7feSMatthias Ringwald 
12061d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12071d81c7feSMatthias Ringwald 
12081d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
12091d81c7feSMatthias Ringwald 
1210ba0de059SMatthias Ringwald             return true;
1211ce263fc8SMatthias Ringwald 
121274c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
12131d81c7feSMatthias Ringwald 
12141d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
12151d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
12161d81c7feSMatthias Ringwald 
1217a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1218ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
121989425bfcSMilanka Ringwald                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
122089425bfcSMilanka Ringwald                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1221a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1222ce263fc8SMatthias Ringwald                     break;
1223ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12242308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
122525789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
122625789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1227a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1228ce263fc8SMatthias Ringwald                     break;
1229ce263fc8SMatthias Ringwald                 default:
1230ce263fc8SMatthias Ringwald                     break;
1231ce263fc8SMatthias Ringwald             }
12321d81c7feSMatthias Ringwald 
12331d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12341d81c7feSMatthias Ringwald 
12351d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
12361d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
12371d81c7feSMatthias Ringwald             } else {
12381d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
12391d81c7feSMatthias Ringwald             }
1240d6b237acSMatthias Ringwald             return true;
124174c7548aSMatthias Ringwald 
124274c7548aSMatthias Ringwald         default:
124374c7548aSMatthias Ringwald             btstack_assert(false);
124474c7548aSMatthias Ringwald             return true;
124574c7548aSMatthias Ringwald     }
1246d6b237acSMatthias Ringwald }
1247d6b237acSMatthias Ringwald 
1248d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1249d6b237acSMatthias Ringwald     bool processed = false;
1250d6b237acSMatthias Ringwald     while (!processed){
1251d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1252d6b237acSMatthias Ringwald     }
12531d81c7feSMatthias Ringwald     // reset parser state on end-of-line
12541d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
12551d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
12561d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
12571d81c7feSMatthias Ringwald     }
1258ce263fc8SMatthias Ringwald }
1259ce263fc8SMatthias Ringwald 
1260a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1261ce263fc8SMatthias Ringwald     int value;
1262a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1263667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
12642308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1265667ec068SMatthias Ringwald             int i;
1266a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1267667ec068SMatthias Ringwald                 case 0:
1268a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1269a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1270a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1271667ec068SMatthias Ringwald                             break;
1272667ec068SMatthias Ringwald                         }
1273667ec068SMatthias Ringwald                     }
1274667ec068SMatthias Ringwald                     break;
1275667ec068SMatthias Ringwald                 case 1:
1276a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1277a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1278667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1279a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1280667ec068SMatthias Ringwald                     break;
1281667ec068SMatthias Ringwald                 default:
1282667ec068SMatthias Ringwald                     break;
1283667ec068SMatthias Ringwald             }
128425789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1285667ec068SMatthias Ringwald             break;
1286667ec068SMatthias Ringwald 
1287667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1288a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1289667ec068SMatthias Ringwald                 case 0:
1290a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1291a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1292667ec068SMatthias Ringwald                     break;
1293667ec068SMatthias Ringwald                 case 1:
12942308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1295a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1296667ec068SMatthias Ringwald                     break;
1297667ec068SMatthias Ringwald                 default:
1298667ec068SMatthias Ringwald                     break;
1299667ec068SMatthias Ringwald             }
1300eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1301a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1302667ec068SMatthias Ringwald             break;
1303667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1304a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1305667ec068SMatthias Ringwald                 case 0:
13062308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1307a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1308667ec068SMatthias Ringwald                     break;
1309667ec068SMatthias Ringwald                 case 1:
13102308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1311a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1312667ec068SMatthias Ringwald                     break;
1313667ec068SMatthias Ringwald                 case 2:
13142308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1315a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1316667ec068SMatthias Ringwald                     break;
1317667ec068SMatthias Ringwald                 case 3:
13182308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13190aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1320667ec068SMatthias Ringwald                     break;
1321667ec068SMatthias Ringwald                 case 4:
13220aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13230aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
13240aee97efSMilanka Ringwald                     break;
13250aee97efSMilanka Ringwald                 case 5:
1326a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1327a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1328667ec068SMatthias Ringwald                     break;
13290aee97efSMilanka Ringwald                 case 6:
13302308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1331a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1332667ec068SMatthias Ringwald                     break;
1333667ec068SMatthias Ringwald                 default:
1334667ec068SMatthias Ringwald                     break;
1335667ec068SMatthias Ringwald             }
1336eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1337a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1338667ec068SMatthias Ringwald             break;
1339aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
13402308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1341a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1342aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1343aa4dd815SMatthias Ringwald             break;
1344aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
13452308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1346a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1347aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1348aa4dd815SMatthias Ringwald             break;
1349aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
13502308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1351a0ffb263SMatthias Ringwald             hfp_connection->ag_activate_voice_recognition = value;
1352aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1353aa4dd815SMatthias Ringwald             break;
1354aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
13552308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1356a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1357aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1358aa4dd815SMatthias Ringwald             break;
1359aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
13602308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1361a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1362aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1363aa4dd815SMatthias Ringwald             break;
13643deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
13652308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1366a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
13673deb3ec6SMatthias Ringwald             break;
13683deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
13692308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1370a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
13713deb3ec6SMatthias Ringwald             break;
13723deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
13732308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1374bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
13753deb3ec6SMatthias Ringwald             break;
13763deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1377a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
13782308e108SMilanka Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
137925789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1380a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
13813deb3ec6SMatthias Ringwald             break;
1382aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
138389425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
138489425bfcSMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1385a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1386a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1387aa4dd815SMatthias Ringwald             break;
1388aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1389a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
13902308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
139125789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
13923deb3ec6SMatthias Ringwald             break;
13933deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
139425789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1395a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1396a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
13972308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1398a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
13993deb3ec6SMatthias Ringwald             break;
14003deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1401a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1402a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
140325789943SMilanka Ringwald             strncpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE);
140425789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1405eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
14063deb3ec6SMatthias Ringwald             break;
1407aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1408aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1409a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
14102308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
141125789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1412a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
14133deb3ec6SMatthias Ringwald             break;
1414aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
14153deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1416a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
141725789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
14183deb3ec6SMatthias Ringwald             break;
1419ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
142025789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1421a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1422ce263fc8SMatthias Ringwald             break;
14233deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
14243deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1425a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1426a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1427a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1428a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1429ce263fc8SMatthias Ringwald             }
1430a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1431ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1432a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1433ce263fc8SMatthias Ringwald             } else {
14342308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1435a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1436a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1437a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
14383deb3ec6SMatthias Ringwald             }
143925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
14403deb3ec6SMatthias Ringwald             break;
14413deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14423deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
144325789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1444a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
14453deb3ec6SMatthias Ringwald             break;
1446aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
14472308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1448a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1449aa4dd815SMatthias Ringwald             break;
1450aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1451a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1452a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
14533deb3ec6SMatthias Ringwald                 break;
14543deb3ec6SMatthias Ringwald             }
14553deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1456a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
14573deb3ec6SMatthias Ringwald             break;
14583deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
14593deb3ec6SMatthias Ringwald             break;
14603deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1461a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
14622308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
14633deb3ec6SMatthias Ringwald             break;
14643deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
14652308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1466a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1467a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
14683deb3ec6SMatthias Ringwald             break;
1469ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1470a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1471a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1472a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1473a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
14743deb3ec6SMatthias Ringwald             break;
14750222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
14760222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
14770222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
14780222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
14790222a807SMatthias Ringwald             }
14800222a807SMatthias Ringwald             break;
14810222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
14820222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
14830222a807SMatthias Ringwald             break;
14840222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
14850222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
14860222a807SMatthias Ringwald             break;
14870222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
14880222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
14890222a807SMatthias Ringwald             break;
14900222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
14910222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
14920222a807SMatthias Ringwald             break;
14933deb3ec6SMatthias Ringwald         default:
14943deb3ec6SMatthias Ringwald             break;
14953deb3ec6SMatthias Ringwald     }
14963deb3ec6SMatthias Ringwald }
14973deb3ec6SMatthias Ringwald 
14981d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
14991d9c9c90SMilanka Ringwald     UNUSED(context);
15001d9c9c90SMilanka Ringwald 
15011d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
15021d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
15031d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
15041d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
15051d9c9c90SMilanka Ringwald 
15061d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
15071d9c9c90SMilanka Ringwald 
15081d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
15091d9c9c90SMilanka Ringwald         sdp_query_context.local_role = connection->local_role;
15101d9c9c90SMilanka Ringwald         (void)memcpy(sdp_query_context.remote_address, connection->remote_addr, 6);
15111d9c9c90SMilanka Ringwald         sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
15121d9c9c90SMilanka Ringwald         return;
15131d9c9c90SMilanka Ringwald     }
15141d9c9c90SMilanka Ringwald }
15151d9c9c90SMilanka Ringwald 
1516323d3000SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
1517323d3000SMatthias Ringwald     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1518a0ffb263SMatthias Ringwald     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
15193deb3ec6SMatthias Ringwald 
1520a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15213deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
15223deb3ec6SMatthias Ringwald         return;
15233deb3ec6SMatthias Ringwald     }
1524a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
15253deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1526a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
15273deb3ec6SMatthias Ringwald             return;
15283deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
1529a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
15303deb3ec6SMatthias Ringwald             return;
15313deb3ec6SMatthias Ringwald         case HFP_IDLE:
15326535961aSMatthias Ringwald             (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
15331d9c9c90SMilanka Ringwald             hfp_connection->state = HFP_W2_SEND_SDP_QUERY;
1534a0ffb263SMatthias Ringwald             hfp_connection->service_uuid = service_uuid;
15351d9c9c90SMilanka Ringwald 
15361d9c9c90SMilanka Ringwald             hfp_handle_sdp_client_query_request.callback = &hfp_handle_start_sdp_client_query;
15371d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
15381d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
15393deb3ec6SMatthias Ringwald             break;
15403deb3ec6SMatthias Ringwald         default:
15413deb3ec6SMatthias Ringwald             break;
15423deb3ec6SMatthias Ringwald     }
15433deb3ec6SMatthias Ringwald }
15443deb3ec6SMatthias Ringwald 
1545a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1546a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1547a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
15483deb3ec6SMatthias Ringwald 
1549a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1550a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_IDLE;
15513deb3ec6SMatthias Ringwald         return;
15523deb3ec6SMatthias Ringwald     }
15533deb3ec6SMatthias Ringwald 
1554a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1555a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
15563deb3ec6SMatthias Ringwald         return;
15573deb3ec6SMatthias Ringwald     }
15583deb3ec6SMatthias Ringwald 
15590c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
15600c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
15610c3047fbSMatthias Ringwald         return;
15620c3047fbSMatthias Ringwald     }
15630c3047fbSMatthias Ringwald 
15640c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
15650c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
15660c3047fbSMatthias Ringwald         return;
15670c3047fbSMatthias Ringwald     }
15680c3047fbSMatthias Ringwald 
15690c3047fbSMatthias Ringwald     // HFP_W4_SCO_DISCONNECTED or later
15705f053052SMilanka Ringwald     hfp_connection->release_slc_connection = 1;
15713deb3ec6SMatthias Ringwald }
15723deb3ec6SMatthias Ringwald 
1573a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1574a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1575a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1576a0ffb263SMatthias Ringwald     hfp_connection->release_audio_connection = 1;
15773deb3ec6SMatthias Ringwald }
15783deb3ec6SMatthias Ringwald 
1579ce263fc8SMatthias Ringwald static const struct link_settings {
1580ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1581ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1582ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1583ebc6f15bSMatthias Ringwald     const bool     eSCO;
1584ebc6f15bSMatthias Ringwald     const uint8_t  codec;
1585ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1586352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1587352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1588352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1589352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1590352a0504SMatthias Ringwald     { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1591352a0504SMatthias Ringwald     { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1592352a0504SMatthias Ringwald     { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1593352a0504SMatthias Ringwald     { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1594ce263fc8SMatthias Ringwald };
15953deb3ec6SMatthias Ringwald 
1596eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1597ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1598eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1599ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1600eddcd308SMatthias Ringwald     sco_establishment_active = hfp_connection;
1601d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1602d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
16037522e673SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data
1604d2c1d59aSMatthias Ringwald     }
1605352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1606352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1607eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1608352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1609ce263fc8SMatthias Ringwald }
1610d68dcce1SMatthias Ringwald 
16111b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
16121b005648SMatthias Ringwald     hfp_hf_callback = callback;
16131b005648SMatthias Ringwald }
16141b005648SMatthias Ringwald 
16151b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
16161b005648SMatthias Ringwald     hfp_ag_callback = callback;
16171b005648SMatthias Ringwald }
16181b005648SMatthias Ringwald 
1619ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1620ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
1621ca59be51SMatthias Ringwald }
16221b005648SMatthias Ringwald 
1623ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1624ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
1625d68dcce1SMatthias Ringwald }
1626d68dcce1SMatthias Ringwald 
16271b005648SMatthias Ringwald void hfp_set_hf_run_for_context(void (*callback)(hfp_connection_t * hfp_connection)){
16281b005648SMatthias Ringwald     hfp_hf_run_for_context = callback;
16291b005648SMatthias Ringwald }
16301b005648SMatthias Ringwald 
1631520c92d5SMatthias Ringwald void hfp_init(void){
1632352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
1633991c26beSMatthias Ringwald }
1634991c26beSMatthias Ringwald 
1635991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
163601e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
1637991c26beSMatthias Ringwald }
1638991c26beSMatthias Ringwald 
1639991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
164001e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
1641520c92d5SMatthias Ringwald }
1642186dd3d2SMatthias Ringwald 
1643e453c1d9SMatthias 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){
1644e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
1645e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
1646e453c1d9SMatthias Ringwald     while (setting > 0){
1647e453c1d9SMatthias Ringwald         setting--;
1648e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
1649e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
1650e453c1d9SMatthias Ringwald         // skip if S4 but not supported
1651e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
1652e453c1d9SMatthias Ringwald         // skip wrong codec
1653e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
165401e5b727SMatthias Ringwald         // skip disabled packet types
1655352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
1656352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
1657352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
165801e5b727SMatthias Ringwald 
1659e453c1d9SMatthias Ringwald         // found matching setting
1660e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
1661afac2a04SMilanka Ringwald     }
1662e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
1663afac2a04SMilanka Ringwald }
1664e453c1d9SMatthias Ringwald 
16652b7abbfbSMatthias 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){
1666e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
1667e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
1668e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
16692b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
16702b7abbfbSMatthias Ringwald }
16712b7abbfbSMatthias Ringwald 
16722b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
1673e453c1d9SMatthias Ringwald     // get highest possible link setting
16742b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
1675afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
1676afac2a04SMilanka Ringwald }
1677afac2a04SMilanka Ringwald 
1678186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
1679186dd3d2SMatthias Ringwald 
1680186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
1681186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1682186dd3d2SMatthias Ringwald     // encode \n\r
1683186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
168415a27967SMatthias Ringwald     int i = 0;
1685186dd3d2SMatthias Ringwald     int pos;
168615a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
1687186dd3d2SMatthias Ringwald         switch (packet[pos]){
1688186dd3d2SMatthias Ringwald             case '\n':
1689186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1690186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
1691186dd3d2SMatthias Ringwald                 break;
1692186dd3d2SMatthias Ringwald             case '\r':
1693186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1694186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
1695186dd3d2SMatthias Ringwald                 break;
1696186dd3d2SMatthias Ringwald             default:
1697186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
1698186dd3d2SMatthias Ringwald                 break;
1699186dd3d2SMatthias Ringwald         }
1700186dd3d2SMatthias Ringwald     }
1701186dd3d2SMatthias Ringwald     printable[i] = 0;
1702186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
1703186dd3d2SMatthias Ringwald #endif
1704186dd3d2SMatthias Ringwald }
1705