xref: /btstack/src/classic/hfp.c (revision 0b4debbf1d439732a156a3799e682514fbb75688)
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>
443cfa4086SMatthias Ringwald #include <stdio.h>
453deb3ec6SMatthias Ringwald #include <string.h>
463deb3ec6SMatthias Ringwald #include <inttypes.h>
473deb3ec6SMatthias Ringwald 
48235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
49023f2764SMatthias Ringwald #include "btstack_debug.h"
5059c6af15SMatthias Ringwald #include "btstack_event.h"
5159c6af15SMatthias Ringwald #include "btstack_memory.h"
5259c6af15SMatthias Ringwald #include "btstack_run_loop.h"
53efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
54746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
55023f2764SMatthias Ringwald #include "classic/sdp_util.h"
561d9c9c90SMilanka Ringwald #include "classic/sdp_client.h"
5759c6af15SMatthias Ringwald #include "hci.h"
5859c6af15SMatthias Ringwald #include "hci_cmd.h"
5959c6af15SMatthias Ringwald #include "hci_dump.h"
603deb3ec6SMatthias Ringwald 
613721a235SMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM)
623721a235SMatthias Ringwald #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
633721a235SMatthias Ringwald #endif
643721a235SMatthias Ringwald 
65689d4323SMatthias Ringwald #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM)
66689d4323SMatthias Ringwald #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM"
67689d4323SMatthias Ringwald #endif
68689d4323SMatthias Ringwald 
693deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
703deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
713deb3ec6SMatthias Ringwald 
7220b2edb6SMatthias Ringwald typedef struct {
7320b2edb6SMatthias Ringwald     hfp_role_t local_role;
7420b2edb6SMatthias Ringwald     bd_addr_t  remote_address;
7520b2edb6SMatthias Ringwald } hfp_sdp_query_context_t;
7620b2edb6SMatthias Ringwald 
7720b2edb6SMatthias Ringwald // globals
7820b2edb6SMatthias Ringwald 
7920b2edb6SMatthias Ringwald static hfp_sdp_query_context_t sdp_query_context;
8020b2edb6SMatthias Ringwald static btstack_context_callback_registration_t hfp_handle_sdp_client_query_request;
8120b2edb6SMatthias Ringwald 
8220b2edb6SMatthias Ringwald static btstack_linked_list_t hfp_connections ;
8320b2edb6SMatthias Ringwald 
8420b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
8520b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
8620b2edb6SMatthias Ringwald 
8720b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
8820b2edb6SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
8920b2edb6SMatthias Ringwald 
9020b2edb6SMatthias Ringwald static hfp_connection_t * sco_establishment_active;
9120b2edb6SMatthias Ringwald 
9220b2edb6SMatthias Ringwald static uint16_t hfp_allowed_sco_packet_types;
9320b2edb6SMatthias Ringwald 
9420b2edb6SMatthias Ringwald // prototypes
9520b2edb6SMatthias 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);
9620b2edb6SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
9720b2edb6SMatthias Ringwald 
983deb3ec6SMatthias Ringwald 
993deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
1003deb3ec6SMatthias Ringwald         "EC and/or NR function",
1013deb3ec6SMatthias Ringwald         "Three-way calling",
1023deb3ec6SMatthias Ringwald         "CLI presentation capability",
1033deb3ec6SMatthias Ringwald         "Voice recognition activation",
1043deb3ec6SMatthias Ringwald         "Remote volume control",
1053deb3ec6SMatthias Ringwald 
1063deb3ec6SMatthias Ringwald         "Enhanced call status",
1073deb3ec6SMatthias Ringwald         "Enhanced call control",
1083deb3ec6SMatthias Ringwald 
1093deb3ec6SMatthias Ringwald         "Codec negotiation",
1103deb3ec6SMatthias Ringwald 
1113deb3ec6SMatthias Ringwald         "HF Indicators",
1123deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1133deb3ec6SMatthias Ringwald         "Reserved for future definition"
1143deb3ec6SMatthias Ringwald };
1153deb3ec6SMatthias Ringwald 
1163deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
1173deb3ec6SMatthias Ringwald         "Three-way calling",
1183deb3ec6SMatthias Ringwald         "EC and/or NR function",
1193deb3ec6SMatthias Ringwald         "Voice recognition function",
1203deb3ec6SMatthias Ringwald         "In-band ring tone capability",
1213deb3ec6SMatthias Ringwald         "Attach a number to a voice tag",
1223deb3ec6SMatthias Ringwald         "Ability to reject a call",
1233deb3ec6SMatthias Ringwald         "Enhanced call status",
1243deb3ec6SMatthias Ringwald         "Enhanced call control",
1253deb3ec6SMatthias Ringwald         "Extended Error Result Codes",
1263deb3ec6SMatthias Ringwald         "Codec negotiation",
1273deb3ec6SMatthias Ringwald         "HF Indicators",
1283deb3ec6SMatthias Ringwald         "eSCO S4 (and T2) Settings Supported",
1293deb3ec6SMatthias Ringwald         "Reserved for future definition"
1303deb3ec6SMatthias Ringwald };
1313deb3ec6SMatthias Ringwald 
1320aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1330aee97efSMilanka Ringwald         "outgoing",
1340aee97efSMilanka Ringwald         "incoming"
1350aee97efSMilanka Ringwald };
1360aee97efSMilanka Ringwald 
1370aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1380aee97efSMilanka Ringwald         "active",
1390aee97efSMilanka Ringwald         "held",
1400aee97efSMilanka Ringwald         "outgoing dialing",
1410aee97efSMilanka Ringwald         "outgoing alerting",
1420aee97efSMilanka Ringwald         "incoming",
1430aee97efSMilanka Ringwald         "incoming waiting",
1440aee97efSMilanka Ringwald         "call held by response and hold"
1450aee97efSMilanka Ringwald };
1460aee97efSMilanka Ringwald 
1470aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1480aee97efSMilanka Ringwald         "voice",
1490aee97efSMilanka Ringwald         "data",
1500aee97efSMilanka Ringwald         "fax"
1510aee97efSMilanka Ringwald };
1520aee97efSMilanka Ringwald 
1530aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1540aee97efSMilanka Ringwald         "not a conference call",
1550aee97efSMilanka Ringwald         "conference call"
1560aee97efSMilanka Ringwald };
1570aee97efSMilanka Ringwald 
1580aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1590aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1600aee97efSMilanka Ringwald     return "not defined";
1610aee97efSMilanka Ringwald }
1620aee97efSMilanka Ringwald 
1630aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1640aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1650aee97efSMilanka Ringwald     return "not defined";
1660aee97efSMilanka Ringwald }
1670aee97efSMilanka Ringwald 
1680aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1690aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1700aee97efSMilanka Ringwald     return "not defined";
1710aee97efSMilanka Ringwald }
1720aee97efSMilanka Ringwald 
1730aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1740aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1750aee97efSMilanka Ringwald     return "not defined";
1760aee97efSMilanka Ringwald }
1770aee97efSMilanka Ringwald 
17825789943SMilanka Ringwald static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){
17925789943SMilanka Ringwald     uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
18025789943SMilanka Ringwald 
18125789943SMilanka Ringwald     if (index > HFP_MAX_NUM_INDICATORS){
18225789943SMilanka Ringwald         log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS");
18325789943SMilanka Ringwald         return HFP_MAX_NUM_INDICATORS - 1;
18425789943SMilanka Ringwald     }
18525789943SMilanka Ringwald 
18625789943SMilanka Ringwald     // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0
18725789943SMilanka Ringwald     if (index > 0){
18825789943SMilanka Ringwald         index -= 1;
18925789943SMilanka Ringwald     } else {
19025789943SMilanka Ringwald         log_info("ignoring invalid indicator index 0");
19125789943SMilanka Ringwald         return 0;
19225789943SMilanka Ringwald     }
19325789943SMilanka Ringwald     return index;
19425789943SMilanka Ringwald }
19525789943SMilanka Ringwald 
19625789943SMilanka Ringwald static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){
19725789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){
19825789943SMilanka Ringwald         hfp_connection->parser_item_index++;
19925789943SMilanka Ringwald     } else {
20025789943SMilanka Ringwald         log_info("Ignoring additional indicator");
20125789943SMilanka Ringwald     }
20225789943SMilanka Ringwald }
20325789943SMilanka Ringwald 
20425789943SMilanka Ringwald static void hfp_next_codec_index(hfp_connection_t * hfp_connection){
20525789943SMilanka Ringwald     if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){
20625789943SMilanka Ringwald         hfp_connection->parser_item_index++;
20725789943SMilanka Ringwald     } else {
20825789943SMilanka Ringwald         log_info("Ignoring additional codec index");
20925789943SMilanka Ringwald     }
21025789943SMilanka Ringwald }
21125789943SMilanka Ringwald 
212eed67a37SMilanka Ringwald static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){
213eed67a37SMilanka Ringwald     if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){
214eed67a37SMilanka Ringwald         hfp_connection->remote_call_services_index++;
215eed67a37SMilanka Ringwald     } else {
216eed67a37SMilanka Ringwald         log_info("Ignoring additional remote_call_services");
217eed67a37SMilanka Ringwald     }
218eed67a37SMilanka Ringwald }
21925789943SMilanka Ringwald 
2203deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
2213deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
2223deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
2233deb3ec6SMatthias Ringwald     }
2243deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
2273deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
2283deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
2293deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
2303deb3ec6SMatthias Ringwald     }
2313deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
2323deb3ec6SMatthias Ringwald }
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
2353deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
23674386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
23728190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
2383deb3ec6SMatthias Ringwald     if (err){
23928190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
2403deb3ec6SMatthias Ringwald     }
241e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
242e43d1938SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid);
243e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command);
244e43d1938SMatthias Ringwald #endif
2453deb3ec6SMatthias Ringwald     return 1;
2463deb3ec6SMatthias Ringwald }
2473deb3ec6SMatthias Ringwald 
2486a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
249e8d1dc0dSMatthias Ringwald 
250e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
251c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
252e8d1dc0dSMatthias Ringwald 
253df327ff3SMilanka Ringwald     int i;
254df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
255e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
256e8d1dc0dSMatthias Ringwald         return 1;
257df327ff3SMilanka Ringwald     }
258df327ff3SMilanka Ringwald     return 0;
259df327ff3SMilanka Ringwald }
260df327ff3SMilanka Ringwald 
261d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
262d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
263d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
264d715cf51SMatthias Ringwald     int i;
265d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
266d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
267d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
268d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
269d715cf51SMatthias Ringwald     }
270d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2716535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
272d715cf51SMatthias Ringwald }
273d715cf51SMatthias Ringwald 
2743deb3ec6SMatthias Ringwald // UTILS
2753deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2763deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
2773deb3ec6SMatthias Ringwald }
2783deb3ec6SMatthias Ringwald 
2793deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
2803deb3ec6SMatthias Ringwald     if (value){
2813deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
2823deb3ec6SMatthias Ringwald     } else {
2833deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
2843deb3ec6SMatthias Ringwald     }
2853deb3ec6SMatthias Ringwald     return bitmap;
2863deb3ec6SMatthias Ringwald }
2873deb3ec6SMatthias Ringwald 
2883deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
289c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2903deb3ec6SMatthias Ringwald     int i;
2913deb3ec6SMatthias Ringwald     int offset = 0;
292c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2933deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
2943deb3ec6SMatthias Ringwald     }
2953deb3ec6SMatthias Ringwald     if (i<values_nr){
2963deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
2973deb3ec6SMatthias Ringwald     }
2983deb3ec6SMatthias Ringwald     return offset;
2993deb3ec6SMatthias Ringwald }
3003deb3ec6SMatthias Ringwald 
3013deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
302c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
3033deb3ec6SMatthias Ringwald 
3043deb3ec6SMatthias Ringwald     int i;
3053deb3ec6SMatthias Ringwald     int offset = 0;
306c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
3073deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
3083deb3ec6SMatthias Ringwald     }
3093deb3ec6SMatthias Ringwald 
3103deb3ec6SMatthias Ringwald     if (i<values_nr){
3113deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
3123deb3ec6SMatthias Ringwald     }
3133deb3ec6SMatthias Ringwald     return offset;
3143deb3ec6SMatthias Ringwald }
3153deb3ec6SMatthias Ringwald 
316ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
317ca59be51SMatthias Ringwald     if (!hfp_connection) return;
318ca59be51SMatthias Ringwald     btstack_packet_handler_t callback = NULL;
319ca59be51SMatthias Ringwald     switch (hfp_connection->local_role){
320ca59be51SMatthias Ringwald         case HFP_ROLE_HF:
321ca59be51SMatthias Ringwald             callback = hfp_hf_callback;
322671f1aa2SMatthias Ringwald             break;
323ca59be51SMatthias Ringwald         case HFP_ROLE_AG:
324ca59be51SMatthias Ringwald             callback = hfp_ag_callback;
325671f1aa2SMatthias Ringwald             break;
326ca59be51SMatthias Ringwald         default:
327ca59be51SMatthias Ringwald             return;
328ca59be51SMatthias Ringwald     }
329ca59be51SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
330ca59be51SMatthias Ringwald }
331ca59be51SMatthias Ringwald 
332ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
3337d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
334d703d377SMatthias Ringwald     uint8_t event[5];
335a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
336a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
337a0ffb263SMatthias Ringwald     event[2] = event_subtype;
3387d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
339ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
340a0ffb263SMatthias Ringwald }
341a0ffb263SMatthias Ringwald 
342ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
3437d81706fSMatthias Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
344d703d377SMatthias Ringwald     uint8_t event[6];
3453deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3463deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3473deb3ec6SMatthias Ringwald     event[2] = event_subtype;
3487d81706fSMatthias Ringwald     little_endian_store_16(event, 3, acl_handle);
349d703d377SMatthias Ringwald     event[5] = value; // status 0 == OK
350ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3513deb3ec6SMatthias Ringwald }
3523deb3ec6SMatthias Ringwald 
353a95ec82fSMilanka Ringwald void hfp_emit_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status, uint8_t state){
354a95ec82fSMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
355a95ec82fSMilanka Ringwald     uint8_t event[7];
356a95ec82fSMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
357a95ec82fSMilanka Ringwald     event[1] = sizeof(event) - 2;
358a95ec82fSMilanka Ringwald     event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_STATUS;
359a95ec82fSMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
360a95ec82fSMilanka Ringwald     event[5] = status; // 0:success
361a95ec82fSMilanka Ringwald     event[6] = state;  // 0:deactivated, 1:activate
362a95ec82fSMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
363a95ec82fSMilanka Ringwald }
364a95ec82fSMilanka Ringwald 
365ca59be51SMatthias 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){
3667d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
3678901a7c4SMatthias Ringwald     uint8_t event[12];
3686a7f44bdSMilanka Ringwald     int pos = 0;
3696a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
3706a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
371d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
3726a7f44bdSMilanka Ringwald     event[pos++] = status; // status 0 == OK
3736a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
3746a7f44bdSMilanka Ringwald     pos += 2;
3756a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
3766a7f44bdSMilanka Ringwald     pos += 6;
377ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
378a0653c3bSMilanka Ringwald }
379a0653c3bSMilanka Ringwald 
380d703d377SMatthias Ringwald static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){
3817d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
382d703d377SMatthias Ringwald     uint8_t event[7];
3835441d52fSMatthias Ringwald     int pos = 0;
3845441d52fSMatthias Ringwald     event[pos++] = HCI_EVENT_HFP_META;
3855441d52fSMatthias Ringwald     event[pos++] = sizeof(event) - 2;
3865441d52fSMatthias Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED;
387d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
388d703d377SMatthias Ringwald     pos += 2;
389d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
390d703d377SMatthias Ringwald     pos += 2;
3915441d52fSMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
3925441d52fSMatthias Ringwald }
3935441d52fSMatthias Ringwald 
394d703d377SMatthias Ringwald void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t sco_handle, bd_addr_t addr, uint8_t  negotiated_codec){
3957d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
396d703d377SMatthias Ringwald     uint8_t event[15];
397d0c4aea6SMilanka Ringwald     int pos = 0;
398d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
399d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
400d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
401d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
402d703d377SMatthias Ringwald     pos += 2;
403d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
404d703d377SMatthias Ringwald     little_endian_store_16(event, pos, sco_handle);
405d0c4aea6SMilanka Ringwald     pos += 2;
406d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
407d0c4aea6SMilanka Ringwald     pos += 6;
408d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
409ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
410d0c4aea6SMilanka Ringwald }
411d0c4aea6SMilanka Ringwald 
412ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
4137d81706fSMatthias Ringwald     btstack_assert(hfp_connection != NULL);
4142c50df93SBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
4152c50df93SBjoern Hartmann     uint8_t event[256];
4162c50df93SBjoern Hartmann #else
417c1797c7dSMatthias Ringwald     uint8_t event[40];
4182c50df93SBjoern Hartmann #endif
419aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
420aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
421aa4dd815SMatthias Ringwald     event[2] = event_subtype;
422d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
423d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(value), sizeof(event) - 6);
42413ee9fd7SMatthias Ringwald     strncpy((char*)&event[5], value, size);
425d703d377SMatthias Ringwald     event[5 + size] = 0;
426ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
427aa4dd815SMatthias Ringwald }
428aa4dd815SMatthias Ringwald 
429db3cdbd4SMilanka Ringwald static void hfp_emit_enhanced_voice_recognition_state(hfp_connection_t * hfp_connection){
430db3cdbd4SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
431db3cdbd4SMilanka Ringwald     uint8_t event[7];
432db3cdbd4SMilanka Ringwald     int pos = 0;
433db3cdbd4SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
434db3cdbd4SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
435db3cdbd4SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_STATUS;
436db3cdbd4SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
437db3cdbd4SMilanka Ringwald     pos += 2;
438db3cdbd4SMilanka Ringwald     event[pos++] = hfp_connection->ag_vra_status;
439db3cdbd4SMilanka Ringwald     event[pos++] = hfp_connection->ag_vra_state;
440db3cdbd4SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
441db3cdbd4SMilanka Ringwald }
442db3cdbd4SMilanka Ringwald 
443db3cdbd4SMilanka Ringwald static void hfp_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection, uint16_t value_length, uint8_t * value){
444db3cdbd4SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
445db3cdbd4SMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE];
446db3cdbd4SMilanka Ringwald     int pos = 0;
447db3cdbd4SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
448db3cdbd4SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
449db3cdbd4SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_TEXT;
450db3cdbd4SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
451db3cdbd4SMilanka Ringwald     pos += 2;
45245796ff1SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
453db3cdbd4SMilanka Ringwald     pos += 2;
45445796ff1SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
45545796ff1SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
456db3cdbd4SMilanka Ringwald 
457db3cdbd4SMilanka Ringwald     // length, zero ending
458db3cdbd4SMilanka Ringwald     uint16_t size = btstack_min(value_length, sizeof(event) - pos - 2 - 1);
459db3cdbd4SMilanka Ringwald     little_endian_store_16(event, pos, size+1);
460db3cdbd4SMilanka Ringwald     pos += 2;
461db3cdbd4SMilanka Ringwald     memcpy(&event[pos], value, size);
462db3cdbd4SMilanka Ringwald     event[pos + size] = 0;
463db3cdbd4SMilanka Ringwald     pos += size + 1;
464db3cdbd4SMilanka Ringwald     hfp_emit_event_for_context(hfp_connection, event, pos);
465db3cdbd4SMilanka Ringwald }
466db3cdbd4SMilanka Ringwald 
4670cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
4688f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
4693deb3ec6SMatthias Ringwald }
4703deb3ec6SMatthias Ringwald 
4713deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
472665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
473665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
474665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
475a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
476a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
477a0ffb263SMatthias Ringwald             return hfp_connection;
4783deb3ec6SMatthias Ringwald         }
4793deb3ec6SMatthias Ringwald     }
4803deb3ec6SMatthias Ringwald     return NULL;
4813deb3ec6SMatthias Ringwald }
4823deb3ec6SMatthias Ringwald 
483405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
484665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
485665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
486665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
487a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
4885df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
489a0ffb263SMatthias Ringwald             return hfp_connection;
4903deb3ec6SMatthias Ringwald         }
4913deb3ec6SMatthias Ringwald     }
4923deb3ec6SMatthias Ringwald     return NULL;
4933deb3ec6SMatthias Ringwald }
4943deb3ec6SMatthias Ringwald 
495405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
496665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
497665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
498665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
499a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
500c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
501a0ffb263SMatthias Ringwald             return hfp_connection;
5023deb3ec6SMatthias Ringwald         }
5033deb3ec6SMatthias Ringwald     }
5043deb3ec6SMatthias Ringwald     return NULL;
5053deb3ec6SMatthias Ringwald }
5063deb3ec6SMatthias Ringwald 
507405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
508d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
509d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
510d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
511d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
512c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
513d97d752dSMilanka Ringwald             return hfp_connection;
514d97d752dSMilanka Ringwald         }
515d97d752dSMilanka Ringwald     }
516d97d752dSMilanka Ringwald     return NULL;
517d97d752dSMilanka Ringwald }
518d97d752dSMilanka Ringwald 
519a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
520a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
521a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
522a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
5233deb3ec6SMatthias Ringwald 
524dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
5253deb3ec6SMatthias Ringwald 
526a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
527a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
5283deb3ec6SMatthias Ringwald 
529a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
530a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
5313deb3ec6SMatthias Ringwald 
532a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
533a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
5347522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
535a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
5363deb3ec6SMatthias Ringwald 
537a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
538a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
539a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
540a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
5413deb3ec6SMatthias Ringwald }
5423deb3ec6SMatthias Ringwald 
543fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
544a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
545a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
5463deb3ec6SMatthias Ringwald 
547a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
548a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
549a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
550aa4dd815SMatthias Ringwald 
551a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
552a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
5533deb3ec6SMatthias Ringwald 
554d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
555d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
556d751f069SMatthias Ringwald 
557a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
5583deb3ec6SMatthias Ringwald 
559d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
560a0ffb263SMatthias Ringwald     return hfp_connection;
5613deb3ec6SMatthias Ringwald }
5623deb3ec6SMatthias Ringwald 
56348e6eeeeSMatthias Ringwald void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){
56478a72d5eSMilanka Ringwald     btstack_run_loop_remove_timer(&hfp_connection->hfp_timeout);
565a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
56609a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
5673deb3ec6SMatthias Ringwald }
5683deb3ec6SMatthias Ringwald 
5694eb3f1d8SMilanka Ringwald static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){
570405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
571a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
572a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
5736535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
574323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
575bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
576bdf572f4SMatthias Ringwald 
577a0ffb263SMatthias Ringwald     return hfp_connection;
5783deb3ec6SMatthias Ringwald }
5793deb3ec6SMatthias Ringwald 
580aa4dd815SMatthias Ringwald /* @param network.
581aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
582aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
583aa4dd815SMatthias Ringwald  */
5843deb3ec6SMatthias Ringwald 
5853deb3ec6SMatthias Ringwald /* @param suported_features
5863deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5873deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
5883deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
5893deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
5903deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
5913deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
5923deb3ec6SMatthias Ringwald  */
5933deb3ec6SMatthias Ringwald  /* Bit position:
5943deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
5953deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
5963deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
5973deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
5983deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
5993deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
6003deb3ec6SMatthias Ringwald  */
6013deb3ec6SMatthias Ringwald 
6029b1c3b4dSMatthias 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){
6033deb3ec6SMatthias Ringwald     uint8_t* attribute;
6043deb3ec6SMatthias Ringwald     de_create_sequence(service);
6053deb3ec6SMatthias Ringwald 
6063deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
607235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
6089b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
6093deb3ec6SMatthias Ringwald 
6103deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
611235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
6123deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6133deb3ec6SMatthias Ringwald     {
6143deb3ec6SMatthias Ringwald         //  "UUID for Service"
6153deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
616235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
6173deb3ec6SMatthias Ringwald     }
6183deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6193deb3ec6SMatthias Ringwald 
6203deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
621235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
6223deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6233deb3ec6SMatthias Ringwald     {
6243deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
6253deb3ec6SMatthias Ringwald         {
626235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
6273deb3ec6SMatthias Ringwald         }
6283deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
6293deb3ec6SMatthias Ringwald 
6303deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
6313deb3ec6SMatthias Ringwald         {
632235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
6333deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
6343deb3ec6SMatthias Ringwald         }
6353deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
6363deb3ec6SMatthias Ringwald     }
6373deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6383deb3ec6SMatthias Ringwald 
6393deb3ec6SMatthias Ringwald 
6403deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
641235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
6423deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6433deb3ec6SMatthias Ringwald     {
644235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
6453deb3ec6SMatthias Ringwald     }
6463deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6473deb3ec6SMatthias Ringwald 
6483deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
649235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
6503deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
6513deb3ec6SMatthias Ringwald     {
6523deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
6533deb3ec6SMatthias Ringwald         {
654235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
6553cb2bba2SMilanka Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8
6563deb3ec6SMatthias Ringwald         }
6573deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
6583deb3ec6SMatthias Ringwald     }
6593deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
6603deb3ec6SMatthias Ringwald 
6613deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
6623deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
6633deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
6643deb3ec6SMatthias Ringwald }
6653deb3ec6SMatthias Ringwald 
6666c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
6678a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
6688a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
6698a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
6701d9c9c90SMilanka Ringwald 
6711d9c9c90SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(sdp_query_context.remote_address, sdp_query_context.local_role);
6721d9c9c90SMilanka Ringwald     if (hfp_connection == NULL) {
6731d9c9c90SMilanka Ringwald         log_info("connection with %s and local role %d not found", sdp_query_context.remote_address, sdp_query_context.local_role);
67484904e95SMilanka Ringwald         return;
67584904e95SMilanka Ringwald     }
6763deb3ec6SMatthias Ringwald 
6770e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
6785611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
679a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
6803deb3ec6SMatthias Ringwald             break;
6815611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
682a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
683a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
684520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
685520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
686520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
687520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
688520c92d5SMatthias Ringwald                         break;
689520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
690520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
691520c92d5SMatthias Ringwald                         break;
692520c92d5SMatthias Ringwald                     default:
6931d9c9c90SMilanka Ringwald                         btstack_assert(false);
694520c92d5SMatthias Ringwald                         return;
695520c92d5SMatthias Ringwald                 }
6961d9c9c90SMilanka Ringwald 
697ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
6981d9c9c90SMilanka Ringwald 
6991d9c9c90SMilanka Ringwald             } else {
70084904e95SMilanka Ringwald                 hfp_connection->state = HFP_IDLE;
701cc92f22bSMatthias Ringwald                 uint8_t status = sdp_event_query_complete_get_status(packet);
702cc92f22bSMatthias Ringwald                 if (status == ERROR_CODE_SUCCESS){
703cc92f22bSMatthias Ringwald                     // report service not found
704cc92f22bSMatthias Ringwald                     status = SDP_SERVICE_NOT_FOUND;
705cc92f22bSMatthias Ringwald                 }
706cc92f22bSMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
707cc92f22bSMatthias Ringwald                 log_info("rfcomm service not found, status 0x%02x", status);
7081d9c9c90SMilanka Ringwald             }
7091d9c9c90SMilanka Ringwald 
7101d9c9c90SMilanka Ringwald             // register the SDP Query request to check if there is another connection waiting for the query
7111d9c9c90SMilanka Ringwald             // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
7121d9c9c90SMilanka Ringwald             (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
7133deb3ec6SMatthias Ringwald             break;
7143deb3ec6SMatthias Ringwald         default:
7153deb3ec6SMatthias Ringwald             break;
7163deb3ec6SMatthias Ringwald     }
7173deb3ec6SMatthias Ringwald }
7183deb3ec6SMatthias Ringwald 
71938200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
72038200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
721eddcd308SMatthias Ringwald 
722eddcd308SMatthias Ringwald     if (!sco_establishment_active){
7233427dd75SMatthias Ringwald         log_info("(e)SCO Connection failed but not started by us");
72438200c1dSMilanka Ringwald         return 0;
725eddcd308SMatthias Ringwald     }
726eddcd308SMatthias Ringwald 
7273427dd75SMatthias Ringwald     log_info("(e)SCO Connection failed 0x%02x", status);
7283427dd75SMatthias Ringwald     switch (status){
7293427dd75SMatthias Ringwald         case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE:
7303427dd75SMatthias Ringwald         case ERROR_CODE_UNSPECIFIED_ERROR:
7313427dd75SMatthias Ringwald         case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES:
732eddcd308SMatthias Ringwald             break;
7333427dd75SMatthias Ringwald         default:
7343427dd75SMatthias Ringwald             return 0;
7353427dd75SMatthias Ringwald     }
7363427dd75SMatthias Ringwald 
7373427dd75SMatthias Ringwald     // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed)
7383427dd75SMatthias Ringwald     hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(sco_establishment_active->link_setting, sco_establishment_active, false);
7393427dd75SMatthias Ringwald 
7403427dd75SMatthias Ringwald     // handle no valid setting found
7413427dd75SMatthias Ringwald     if (next_setting == HFP_LINK_SETTINGS_NONE) {
7423427dd75SMatthias Ringwald         if (sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){
7433427dd75SMatthias Ringwald             log_info("T2/T1 failed, fallback to CVSD - D1");
7447522e673SMatthias Ringwald             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
745bc1b1537SMilanka Ringwald             sco_establishment_active->sco_for_msbc_failed = 1;
746bc1b1537SMilanka Ringwald             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
7477522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
7483427dd75SMatthias Ringwald         } else {
7493427dd75SMatthias Ringwald             // no other options
7503427dd75SMatthias Ringwald             return 0;
751eddcd308SMatthias Ringwald         }
7523427dd75SMatthias Ringwald     }
7533427dd75SMatthias Ringwald 
7543427dd75SMatthias Ringwald     log_info("e)SCO Connection: try new link_setting %d", next_setting);
755eddcd308SMatthias Ringwald     sco_establishment_active->establish_audio_connection = 1;
7563427dd75SMatthias Ringwald     sco_establishment_active->link_setting = next_setting;
75738200c1dSMilanka Ringwald     sco_establishment_active = NULL;
75838200c1dSMilanka Ringwald     return 1;
759eddcd308SMatthias Ringwald }
760eddcd308SMatthias Ringwald 
761eddcd308SMatthias Ringwald 
762405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
7635575558eSMilanka Ringwald     UNUSED(packet_type);
7648a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
7655575558eSMilanka Ringwald     UNUSED(size);
7669ec2630cSMatthias Ringwald 
7673deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
76827950165SMatthias Ringwald     hci_con_handle_t handle;
769a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
770674515e8SMatthias Ringwald     uint8_t status;
7713deb3ec6SMatthias Ringwald 
77227950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
773aa4dd815SMatthias Ringwald 
7740e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
7751b005648SMatthias Ringwald 
776011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
7777522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
7787522e673SMatthias Ringwald                 case 0: //  SCO
7797522e673SMatthias Ringwald                 case 2: // eSCO
780011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
781405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
782011577abSMilanka Ringwald                     if (!hfp_connection) break;
783c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
784cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 2;
785c169df2fSMatthias Ringwald                     } else {
786cb81d35dSMatthias Ringwald                         hfp_connection->accept_sco = 1;
787c169df2fSMatthias Ringwald                     }
7883721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7893721a235SMatthias Ringwald                     hfp_cc256x_prepare_for_sco(hfp_connection);
7903721a235SMatthias Ringwald #endif
791689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
792689d4323SMatthias Ringwald                     hfp_bcm_prepare_for_sco(hfp_connection);
793689d4323SMatthias Ringwald #endif
794cb81d35dSMatthias Ringwald                     log_info("accept sco %u\n", hfp_connection->accept_sco);
795719aedb8SMatthias Ringwald                     sco_establishment_active = hfp_connection;
796202c8a4cSMatthias Ringwald                     break;
7977522e673SMatthias Ringwald                 default:
7987522e673SMatthias Ringwald                     break;
7997522e673SMatthias Ringwald             }
800011577abSMilanka Ringwald             break;
8013deb3ec6SMatthias Ringwald 
802eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
803eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
804b87963a5SMatthias Ringwald                 if (sco_establishment_active == NULL) break;
805be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
80638200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
80738200c1dSMilanka Ringwald 
80838200c1dSMilanka Ringwald                 hfp_connection = sco_establishment_active;
80938200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
81038200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
81138200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
81238200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
8136c5b2002SMatthias Ringwald             }
814eddcd308SMatthias Ringwald             break;
815eddcd308SMatthias Ringwald 
8163deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
817b87963a5SMatthias Ringwald             if (sco_establishment_active == NULL) break;
818011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
819405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
820011577abSMilanka Ringwald             if (!hfp_connection) {
821011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
822011577abSMilanka Ringwald                 return;
823011577abSMilanka Ringwald             }
824ce263fc8SMatthias Ringwald 
825011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
82638200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
827cb81d35dSMatthias Ringwald                 hfp_connection->accept_sco = 0;
82838200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
82938200c1dSMilanka Ringwald 
83038200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
83138200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
83238200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
833aa4dd815SMatthias Ringwald                 break;
834aa4dd815SMatthias Ringwald             }
835aa4dd815SMatthias Ringwald 
836011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
837011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
838011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
839011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
840011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
841011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
8423deb3ec6SMatthias Ringwald 
8433deb3ec6SMatthias Ringwald             switch (link_type){
8443deb3ec6SMatthias Ringwald                 case 0x00:
845aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
8463deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
8473deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
8483deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
8493deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
8503deb3ec6SMatthias Ringwald                     break;
8513deb3ec6SMatthias Ringwald                 case 0x02:
852aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
8533deb3ec6SMatthias Ringwald                     break;
8543deb3ec6SMatthias Ringwald                 default:
8553deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
8563deb3ec6SMatthias Ringwald                     break;
8573deb3ec6SMatthias Ringwald             }
858e0d13a19SMilanka Ringwald 
8593deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
860aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
861e0d13a19SMilanka Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length,
862e0d13a19SMilanka Ringwald                  hci_event_synchronous_connection_complete_get_air_mode(packet));
8633deb3ec6SMatthias Ringwald 
864d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
865aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
866d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
867aa4dd815SMatthias Ringwald                 break;
868aa4dd815SMatthias Ringwald             }
869d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
870d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
871d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
872*0b4debbfSMilanka Ringwald             switch (hfp_connection->vra_state){
873*0b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
874*0b4debbfSMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_after_vra = false;
875*0b4debbfSMilanka Ringwald                     break;
876*0b4debbfSMilanka Ringwald                 default:
877*0b4debbfSMilanka Ringwald                     hfp_connection->ag_audio_connection_opened_after_vra = true;
878*0b4debbfSMilanka Ringwald                     break;
879*0b4debbfSMilanka Ringwald             }
88038200c1dSMilanka Ringwald             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
8813deb3ec6SMatthias Ringwald             break;
8823deb3ec6SMatthias Ringwald         }
8833deb3ec6SMatthias Ringwald 
8843deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
885f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
886405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
887aa4dd815SMatthias Ringwald 
888d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
889aa4dd815SMatthias Ringwald 
8903721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
8913721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = true;
8923721a235SMatthias Ringwald #endif
893689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
894689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = true;
895689d4323SMatthias Ringwald #endif
896d751f069SMatthias Ringwald             hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
897d63c37a1SMatthias Ringwald             hfp_connection->release_audio_connection = 0;
89848e6eeeeSMatthias Ringwald 
89948e6eeeeSMatthias Ringwald             if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){
90048e6eeeeSMatthias Ringwald                 // RFCOMM already closed -> remote power off
90148e6eeeeSMatthias Ringwald #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
90248e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_WBS_SHUTDOWN;
90348e6eeeeSMatthias Ringwald #else
90448e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
90548e6eeeeSMatthias Ringwald #endif
90648e6eeeeSMatthias Ringwald                 break;
90748e6eeeeSMatthias Ringwald             }
90848e6eeeeSMatthias Ringwald 
909d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
910*0b4debbfSMilanka Ringwald             hfp_connection->ag_audio_connection_opened_after_vra = false;
911d703d377SMatthias Ringwald             hfp_emit_audio_connection_released(hfp_connection, handle);
9120c3047fbSMatthias Ringwald 
9135f053052SMilanka Ringwald             if (hfp_connection->release_slc_connection){
9145f053052SMilanka Ringwald                 hfp_connection->release_slc_connection = 0;
9150c3047fbSMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
9160c3047fbSMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
9175f053052SMilanka Ringwald             }
9183deb3ec6SMatthias Ringwald             break;
9193deb3ec6SMatthias Ringwald 
920fc64f94aSMatthias Ringwald         default:
9213deb3ec6SMatthias Ringwald             break;
9223deb3ec6SMatthias Ringwald     }
9233deb3ec6SMatthias Ringwald }
9243deb3ec6SMatthias Ringwald 
9253721a235SMatthias Ringwald 
92627950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
9275575558eSMilanka Ringwald     UNUSED(packet_type);
92827950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
9295575558eSMilanka Ringwald     UNUSED(size);
93027950165SMatthias Ringwald 
93127950165SMatthias Ringwald     bd_addr_t event_addr;
93227950165SMatthias Ringwald     uint16_t rfcomm_cid;
93327950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
93427950165SMatthias Ringwald     uint8_t status;
93527950165SMatthias Ringwald 
93627950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
93727950165SMatthias Ringwald 
93827950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
93927950165SMatthias Ringwald 
94027950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
94127950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
94227950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
9434eb3f1d8SMilanka Ringwald             hfp_connection = hfp_create_connection(event_addr, local_role);
94427950165SMatthias Ringwald             if (!hfp_connection){
94527950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
94627950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
94727950165SMatthias Ringwald                 return;
94827950165SMatthias Ringwald             }
94927950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
9504960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
9514960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
95227950165SMatthias Ringwald                 return;
95327950165SMatthias Ringwald             }
95427950165SMatthias Ringwald 
95527950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
95627950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
95727950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
95827950165SMatthias Ringwald             break;
95927950165SMatthias Ringwald 
96027950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
96127950165SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
96227950165SMatthias Ringwald 
96327950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
96427950165SMatthias Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
96527950165SMatthias Ringwald 
966405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
967505f1c30SMatthias Ringwald             if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return;
96827950165SMatthias Ringwald 
96927950165SMatthias Ringwald             if (status) {
97027950165SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
97148e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
97227950165SMatthias Ringwald             } else {
97327950165SMatthias Ringwald                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
97427950165SMatthias Ringwald                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
97527950165SMatthias Ringwald                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
97627950165SMatthias Ringwald 
97727950165SMatthias Ringwald                 switch (hfp_connection->state){
97827950165SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
97927950165SMatthias Ringwald                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
98027950165SMatthias Ringwald                         break;
98127950165SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
98227950165SMatthias Ringwald                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
98327950165SMatthias Ringwald                         break;
98427950165SMatthias Ringwald                     default:
98527950165SMatthias Ringwald                         break;
98627950165SMatthias Ringwald                 }
98727950165SMatthias Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
98827950165SMatthias Ringwald             }
98927950165SMatthias Ringwald             break;
99027950165SMatthias Ringwald 
99127950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
99227950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
99327950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
99427950165SMatthias Ringwald             if (!hfp_connection) break;
99527950165SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
99627950165SMatthias Ringwald                 hfp_connection->state = HFP_IDLE;
99727950165SMatthias Ringwald                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
99827950165SMatthias Ringwald                 break;
99927950165SMatthias Ringwald             }
100048e6eeeeSMatthias Ringwald             if ( hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
100148e6eeeeSMatthias Ringwald                 // service connection was released, this implicitly releases audio connection as well
100248e6eeeeSMatthias Ringwald                 hfp_connection->release_audio_connection = 0;
1003d703d377SMatthias Ringwald                 hci_con_handle_t sco_handle = hfp_connection->sco_handle;
100448e6eeeeSMatthias Ringwald                 gap_disconnect(hfp_connection->sco_handle);
100548e6eeeeSMatthias Ringwald                 hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN;
1006d703d377SMatthias Ringwald                 hfp_emit_audio_connection_released(hfp_connection, sco_handle);
100727950165SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
100848e6eeeeSMatthias Ringwald             } else {
100948e6eeeeSMatthias Ringwald                 // regular case
101048e6eeeeSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
101148e6eeeeSMatthias Ringwald                 hfp_finalize_connection_context(hfp_connection);
101248e6eeeeSMatthias Ringwald             }
101327950165SMatthias Ringwald             break;
101427950165SMatthias Ringwald 
101527950165SMatthias Ringwald         default:
101627950165SMatthias Ringwald             break;
101727950165SMatthias Ringwald     }
101827950165SMatthias Ringwald }
1019aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
102094a27792SMatthias Ringwald 
102194a27792SMatthias Ringwald typedef struct {
102294a27792SMatthias Ringwald     const char * command;
102394a27792SMatthias Ringwald     hfp_command_t command_id;
102494a27792SMatthias Ringwald } hfp_command_entry_t;
102594a27792SMatthias Ringwald 
102694a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
1027cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
1028cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
1029cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
1030cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
1031cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
103294a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
103394a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
103494a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
1035051f797dSMilanka Ringwald     { "AT+BINP=",  HFP_CMD_HF_REQUEST_PHONE_NUMBER },
1036cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
1037cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
103894a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
103994a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
10409ed286f3SMilanka Ringwald     { "AT+BVRA=",  HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION },
1041cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
104294a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
104394a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1044cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
104594a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
104694a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
1047cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
1048cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
1049cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
1050cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
1051cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
105294a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
105394a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1054cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
1055cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
1056cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
1057d8656ee6SMilanka Ringwald     { "AT+VTS=",   HFP_CMD_TRANSMIT_DTMF_CODES },
105894a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
105994a27792SMatthias Ringwald };
106094a27792SMatthias Ringwald 
106194a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
1062cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
106394a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
1064cb33905eSMatthias Ringwald     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
1065cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
1066cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
106794a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
1068cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
1069cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
107094a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
1071cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
107284a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
1073cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
1074cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
1075cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
1076cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
1077cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
1078cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
1079cb33905eSMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
1080cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
1081cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
1082cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
1083cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
108494a27792SMatthias Ringwald };
108594a27792SMatthias Ringwald 
1086aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
10873deb3ec6SMatthias Ringwald 
1088cb33905eSMatthias Ringwald     // table lookup based on role
108994a27792SMatthias Ringwald     uint16_t num_entries;
109094a27792SMatthias Ringwald     hfp_command_entry_t * table;
109194a27792SMatthias Ringwald     if (isHandsFree == 0){
109294a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
109394a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
10943deb3ec6SMatthias Ringwald     } else {
109594a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
109694a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
109794a27792SMatthias Ringwald     }
1098791f0d0aSMatthias Ringwald     // binary search
1099791f0d0aSMatthias Ringwald     uint16_t left = 0;
1100791f0d0aSMatthias Ringwald     uint16_t right = num_entries - 1;
1101791f0d0aSMatthias Ringwald     while (left <= right){
1102791f0d0aSMatthias Ringwald         uint16_t middle = left + (right - left) / 2;
1103791f0d0aSMatthias Ringwald         hfp_command_entry_t *entry = &table[middle];
110494a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
1105791f0d0aSMatthias Ringwald         if (match < 0){
1106791f0d0aSMatthias Ringwald             // search term is lower than middle element
110747005182SMilanka Ringwald             if (right == 0) break;
1108791f0d0aSMatthias Ringwald             right = middle - 1;
1109791f0d0aSMatthias Ringwald         } else if (match == 0){
111094a27792SMatthias Ringwald             return entry->command_id;
1111791f0d0aSMatthias Ringwald         } else {
1112791f0d0aSMatthias Ringwald             // search term is higher than middle element
1113791f0d0aSMatthias Ringwald             left = middle + 1;
111494a27792SMatthias Ringwald         }
111594a27792SMatthias Ringwald     }
111694a27792SMatthias Ringwald 
1117cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
11180222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
1119cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
1120cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
1121cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
11223deb3ec6SMatthias Ringwald     }
11233deb3ec6SMatthias Ringwald 
1124cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
1125cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
1126aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
11273deb3ec6SMatthias Ringwald     }
11283deb3ec6SMatthias Ringwald 
1129cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
1130aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
1131aa4dd815SMatthias Ringwald     }
11323deb3ec6SMatthias Ringwald 
1133aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
11343deb3ec6SMatthias Ringwald }
11353deb3ec6SMatthias Ringwald 
1136a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
113762c7768eSMatthias Ringwald     if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return;
1138a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
1139a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
11403deb3ec6SMatthias Ringwald }
1141a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
1142a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
11433deb3ec6SMatthias Ringwald }
11443deb3ec6SMatthias Ringwald 
11453deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1146c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
11473deb3ec6SMatthias Ringwald }
11483deb3ec6SMatthias Ringwald 
114994a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
1150a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
11513deb3ec6SMatthias Ringwald }
11521d81c7feSMatthias Ringwald 
11531d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
11541d81c7feSMatthias Ringwald     switch (byte){
11551d81c7feSMatthias Ringwald         case ',':
1156f8301d46SMatthias Ringwald 		case '-':
11571d81c7feSMatthias Ringwald         case ';':
11581d81c7feSMatthias Ringwald         case '(':
11591d81c7feSMatthias Ringwald         case ')':
11601d81c7feSMatthias Ringwald         case '\n':
11611d81c7feSMatthias Ringwald         case '\r':
11623deb3ec6SMatthias Ringwald             break;
11633deb3ec6SMatthias Ringwald         default:
11641d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
11653deb3ec6SMatthias Ringwald             break;
11663deb3ec6SMatthias Ringwald     }
11673deb3ec6SMatthias Ringwald }
11681d81c7feSMatthias Ringwald 
11691d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
11701d81c7feSMatthias Ringwald     switch (byte){
11711d81c7feSMatthias Ringwald         case ',':
1172f8301d46SMatthias Ringwald 		case '-':
11731d81c7feSMatthias Ringwald         case ';':
11741d81c7feSMatthias Ringwald         case '\n':
11751d81c7feSMatthias Ringwald         case '\r':
11761d81c7feSMatthias Ringwald             return true;
1177dd533208SMatthias Ringwald         default:
11781d81c7feSMatthias Ringwald             return false;
11793deb3ec6SMatthias Ringwald     }
11803deb3ec6SMatthias Ringwald }
11813deb3ec6SMatthias Ringwald 
1182d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1183aa4dd815SMatthias Ringwald 
11841dddc4f4SMatthias Ringwald     // handle doubles quotes
11851dddc4f4SMatthias Ringwald     if (byte == '"'){
11861dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
1187d6b237acSMatthias Ringwald         return true;
11881dddc4f4SMatthias Ringwald     }
11891dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
11901dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
1191d6b237acSMatthias Ringwald         return true;
11921dddc4f4SMatthias Ringwald     }
11933deb3ec6SMatthias Ringwald 
11941dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1195d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
11961dddc4f4SMatthias Ringwald 
1197bfeaf344SMatthias Ringwald     bool processed = true;
1198bfeaf344SMatthias Ringwald 
1199a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1200dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1201d28b2d5dSMilanka Ringwald             switch (byte) {
1202dd533208SMatthias Ringwald                 case '\n':
1203dd533208SMatthias Ringwald                 case '\r':
1204dd533208SMatthias Ringwald                 case ';':
1205bfeaf344SMatthias Ringwald                     // ignore separator
1206bfeaf344SMatthias Ringwald                     break;
1207bfeaf344SMatthias Ringwald                 case ':':
1208bfeaf344SMatthias Ringwald                 case '?':
1209bfeaf344SMatthias Ringwald                     // store separator
1210bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1211dd533208SMatthias Ringwald                     break;
1212d28b2d5dSMilanka Ringwald                 case '=':
1213bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1214dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1215a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1216d6b237acSMatthias Ringwald                     return true;
1217d28b2d5dSMilanka Ringwald                 default:
1218bfeaf344SMatthias Ringwald                     // store if not lookahead
1219d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1220dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1221d6b237acSMatthias Ringwald                         return true;
1222dd533208SMatthias Ringwald                     }
1223bfeaf344SMatthias Ringwald                     // mark as lookahead
1224bfeaf344SMatthias Ringwald                     processed = false;
1225d7f16ff1SMatthias Ringwald                     break;
1226d7f16ff1SMatthias Ringwald             }
1227ce263fc8SMatthias Ringwald 
12281d81c7feSMatthias Ringwald             // ignore empty tokens
1229d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1230dd533208SMatthias Ringwald 
1231bfeaf344SMatthias Ringwald             // parse
1232dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1233aa4dd815SMatthias Ringwald 
123484a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
123584a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1236a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1237aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1238a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1239aa4dd815SMatthias Ringwald                         break;
1240aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1241a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1242aa4dd815SMatthias Ringwald                         break;
1243aa4dd815SMatthias Ringwald                     default:
124484a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1245aa4dd815SMatthias Ringwald                         break;
1246aa4dd815SMatthias Ringwald                 }
1247aa4dd815SMatthias Ringwald             }
12483deb3ec6SMatthias Ringwald 
1249c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1250c0c0437aSMatthias Ringwald 
1251bfeaf344SMatthias Ringwald             // next state
1252bfeaf344SMatthias Ringwald             hfp_connection->found_equal_sign = false;
12531d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1254dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1255dd533208SMatthias Ringwald 
1256bfeaf344SMatthias Ringwald             return processed;
1257dd533208SMatthias Ringwald 
1258bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
12591d81c7feSMatthias Ringwald             // handle empty fields
12601d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1261dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1262dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1263dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
12643d051a53SMatthias Ringwald                 return true;
12653d051a53SMatthias Ringwald             }
12663d051a53SMatthias Ringwald 
12671d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
12681d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
12691d81c7feSMatthias Ringwald 
12701d81c7feSMatthias Ringwald             // ignore empty tokens
12713d051a53SMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true;
12723d051a53SMatthias Ringwald 
12733d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
12741d81c7feSMatthias Ringwald 
12751d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
12761d81c7feSMatthias Ringwald 
12771d81c7feSMatthias Ringwald             switch (hfp_connection->command){
12781d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
12791d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
12801d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
12811d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12821d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
12831d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
12841d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
12851d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
12861d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
12871d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
12881d81c7feSMatthias Ringwald                     break;
12891d81c7feSMatthias Ringwald                 default:
12901d81c7feSMatthias Ringwald                     break;
12911d81c7feSMatthias Ringwald             }
12923d051a53SMatthias Ringwald             return true;
12933d051a53SMatthias Ringwald 
1294ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
12951d81c7feSMatthias Ringwald 
12961d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
12971d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1298dd533208SMatthias Ringwald 
1299a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1300ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1301a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
13022308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1303ce263fc8SMatthias Ringwald                     break;
1304ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1305a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
13062308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1307ce263fc8SMatthias Ringwald                     break;
1308ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1309ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1310ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
13112308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1312ce263fc8SMatthias Ringwald                     break;
1313ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13142308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1315a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1316a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1317ce263fc8SMatthias Ringwald                     break;
1318ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
13192308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1320a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1321ce263fc8SMatthias Ringwald                     break;
1322ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1323a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1324a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
13252308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1326ce263fc8SMatthias Ringwald                     break;
13270222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
13280222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
13290222a807SMatthias Ringwald                     break;
1330ce263fc8SMatthias Ringwald                 default:
1331ce263fc8SMatthias Ringwald                     break;
1332ce263fc8SMatthias Ringwald             }
13331d81c7feSMatthias Ringwald 
13341d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
13351d81c7feSMatthias Ringwald 
13361d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
13371d81c7feSMatthias Ringwald 
1338ba0de059SMatthias Ringwald             return true;
1339ce263fc8SMatthias Ringwald 
134074c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
13411d81c7feSMatthias Ringwald 
13421d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
13431d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
13441d81c7feSMatthias Ringwald 
1345a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1346ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
134789425bfcSMilanka Ringwald                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
134889425bfcSMilanka Ringwald                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1349a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1350ce263fc8SMatthias Ringwald                     break;
1351ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
13522308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
135325789943SMilanka Ringwald                     hfp_next_indicators_index(hfp_connection);
135425789943SMilanka Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1355a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1356ce263fc8SMatthias Ringwald                     break;
1357ce263fc8SMatthias Ringwald                 default:
1358ce263fc8SMatthias Ringwald                     break;
1359ce263fc8SMatthias Ringwald             }
13601d81c7feSMatthias Ringwald 
13611d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
13621d81c7feSMatthias Ringwald 
13631d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
13641d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
13651d81c7feSMatthias Ringwald             } else {
13661d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
13671d81c7feSMatthias Ringwald             }
1368d6b237acSMatthias Ringwald             return true;
136974c7548aSMatthias Ringwald 
137074c7548aSMatthias Ringwald         default:
137174c7548aSMatthias Ringwald             btstack_assert(false);
137274c7548aSMatthias Ringwald             return true;
137374c7548aSMatthias Ringwald     }
1374d6b237acSMatthias Ringwald }
1375d6b237acSMatthias Ringwald 
1376d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1377d6b237acSMatthias Ringwald     bool processed = false;
1378d6b237acSMatthias Ringwald     while (!processed){
1379d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1380d6b237acSMatthias Ringwald     }
13811d81c7feSMatthias Ringwald     // reset parser state on end-of-line
13821d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
13831d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
13841d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
13851d81c7feSMatthias Ringwald     }
1386ce263fc8SMatthias Ringwald }
1387ce263fc8SMatthias Ringwald 
1388a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1389ce263fc8SMatthias Ringwald     int value;
1390a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1391667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
13922308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1393667ec068SMatthias Ringwald             int i;
1394a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1395667ec068SMatthias Ringwald                 case 0:
1396a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1397a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1398a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1399667ec068SMatthias Ringwald                             break;
1400667ec068SMatthias Ringwald                         }
1401667ec068SMatthias Ringwald                     }
1402667ec068SMatthias Ringwald                     break;
1403667ec068SMatthias Ringwald                 case 1:
1404a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1405a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1406667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1407a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1408667ec068SMatthias Ringwald                     break;
1409667ec068SMatthias Ringwald                 default:
1410667ec068SMatthias Ringwald                     break;
1411667ec068SMatthias Ringwald             }
141225789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1413667ec068SMatthias Ringwald             break;
1414667ec068SMatthias Ringwald 
1415667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1416a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1417667ec068SMatthias Ringwald                 case 0:
141828a4aea3SMilanka Ringwald                     // <alpha>: This optional field is not supported, and shall be left blank.
141928a4aea3SMilanka Ringwald                     break;
142028a4aea3SMilanka Ringwald                 case 1:
142128a4aea3SMilanka Ringwald                     // <number>: Quoted string containing the phone number in the format specified by <type>.
1422a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1423a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1424667ec068SMatthias Ringwald                     break;
142528a4aea3SMilanka Ringwald                 case 2:
142628a4aea3SMilanka Ringwald                     /*
142728a4aea3SMilanka Ringwald                       <type> field specifies the format of the phone number provided, and can be one of the following values:
142828a4aea3SMilanka Ringwald                       - values 128-143: The phone number format may be a national or international format, and may contain prefix and/or escape digits. No changes on the number presentation are required.
142928a4aea3SMilanka Ringwald                      - values 144-159: The phone number format is an international number, including the country code prefix. If the plus sign ("+") is not included as part of the number and shall be added by the AG as needed.
143028a4aea3SMilanka Ringwald                      - values 160-175: National number. No prefix nor escape digits included.
143128a4aea3SMilanka Ringwald                      */
14322308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1433a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1434667ec068SMatthias Ringwald                     break;
143528a4aea3SMilanka Ringwald                 case 3:
143628a4aea3SMilanka Ringwald                     // <speed>: This optional field is not supported, and shall be left blank.
143728a4aea3SMilanka Ringwald                     break;
143828a4aea3SMilanka Ringwald                 case 4:
143928a4aea3SMilanka Ringwald                     // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax).
1440667ec068SMatthias Ringwald                 default:
1441667ec068SMatthias Ringwald                     break;
1442667ec068SMatthias Ringwald             }
1443eed67a37SMilanka Ringwald             // index > 2 are ignored in switch above
1444a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1445667ec068SMatthias Ringwald             break;
1446667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1447a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1448667ec068SMatthias Ringwald                 case 0:
14492308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1450a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1451667ec068SMatthias Ringwald                     break;
1452667ec068SMatthias Ringwald                 case 1:
14532308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1454a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1455667ec068SMatthias Ringwald                     break;
1456667ec068SMatthias Ringwald                 case 2:
14572308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1458a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1459667ec068SMatthias Ringwald                     break;
1460667ec068SMatthias Ringwald                 case 3:
14612308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
14620aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1463667ec068SMatthias Ringwald                     break;
1464667ec068SMatthias Ringwald                 case 4:
14650aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
14660aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
14670aee97efSMilanka Ringwald                     break;
14680aee97efSMilanka Ringwald                 case 5:
1469a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1470a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1471667ec068SMatthias Ringwald                     break;
14720aee97efSMilanka Ringwald                 case 6:
14732308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1474a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1475667ec068SMatthias Ringwald                     break;
1476667ec068SMatthias Ringwald                 default:
1477667ec068SMatthias Ringwald                     break;
1478667ec068SMatthias Ringwald             }
1479eed67a37SMilanka Ringwald             // index > 6 are ignored in switch above
1480a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1481667ec068SMatthias Ringwald             break;
1482aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
14832308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1484a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1485aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1486aa4dd815SMatthias Ringwald             break;
1487aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
14882308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1489a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1490aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1491aa4dd815SMatthias Ringwald             break;
1492aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
14932308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1494a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1495aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1496aa4dd815SMatthias Ringwald             break;
1497aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
14982308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1499a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1500aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1501aa4dd815SMatthias Ringwald             break;
15023deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
15032308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1504a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
15053deb3ec6SMatthias Ringwald             break;
15063deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
15072308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1508a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
15093deb3ec6SMatthias Ringwald             break;
15103deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
15112308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1512bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
15133deb3ec6SMatthias Ringwald             break;
15143deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1515a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
15162308e108SMilanka Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
151725789943SMilanka Ringwald             hfp_next_codec_index(hfp_connection);
1518a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
15193deb3ec6SMatthias Ringwald             break;
1520aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
152189425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
152289425bfcSMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1523a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1524a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1525aa4dd815SMatthias Ringwald             break;
1526aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1527a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
15282308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
152925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
15303deb3ec6SMatthias Ringwald             break;
15313deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
153225789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1533a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1534a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
15352308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1536a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
15373deb3ec6SMatthias Ringwald             break;
15383deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1539a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1540a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
154197d2cfbcSMatthias Ringwald             memcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE-1);
154225789943SMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1543eed67a37SMilanka Ringwald             hfp_next_remote_call_services_index(hfp_connection);
15443deb3ec6SMatthias Ringwald             break;
1545aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1546aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1547a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
15482308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
154925789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
1550a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
15513deb3ec6SMatthias Ringwald             break;
1552aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
15533deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1554a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
155525789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
15563deb3ec6SMatthias Ringwald             break;
1557ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
155825789943SMilanka Ringwald             hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection);
1559a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1560ce263fc8SMatthias Ringwald             break;
15613deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
15623deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1563a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1564a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1565a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1566a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1567ce263fc8SMatthias Ringwald             }
1568a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1569ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1570a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1571ce263fc8SMatthias Ringwald             } else {
15722308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1573a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1574a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1575a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
15763deb3ec6SMatthias Ringwald             }
157725789943SMilanka Ringwald             hfp_next_indicators_index(hfp_connection);
15783deb3ec6SMatthias Ringwald             break;
15793deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
15803deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
158125789943SMilanka Ringwald             hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection);
1582a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
15833deb3ec6SMatthias Ringwald             break;
1584aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
15852308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1586a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1587aa4dd815SMatthias Ringwald             break;
1588aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1589a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1590a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
15913deb3ec6SMatthias Ringwald                 break;
15923deb3ec6SMatthias Ringwald             }
15933deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1594a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
15953deb3ec6SMatthias Ringwald             break;
15963deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
15973deb3ec6SMatthias Ringwald             break;
15983deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1599a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
16002308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
16013deb3ec6SMatthias Ringwald             break;
16023deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
16032308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1604a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1605a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
16063deb3ec6SMatthias Ringwald             break;
1607ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1608a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1609a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1610a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1611a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
16123deb3ec6SMatthias Ringwald             break;
16130222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
16140222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
16150222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
16160222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
16170222a807SMatthias Ringwald             }
16180222a807SMatthias Ringwald             break;
16190222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
16200222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
16210222a807SMatthias Ringwald             break;
16220222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
16230222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
16240222a807SMatthias Ringwald             break;
16250222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
16260222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
16270222a807SMatthias Ringwald             break;
16280222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
16290222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
16300222a807SMatthias Ringwald             break;
1631*0b4debbfSMilanka Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1632*0b4debbfSMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1633*0b4debbfSMilanka Ringwald             hfp_connection->ag_activate_voice_recognition_value = value;
1634*0b4debbfSMilanka Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1635*0b4debbfSMilanka Ringwald             break;
1636db3cdbd4SMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1637db3cdbd4SMilanka Ringwald             switch(hfp_connection->parser_item_index){
1638db3cdbd4SMilanka Ringwald                 case 0:
1639*0b4debbfSMilanka Ringwald                     hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1640db3cdbd4SMilanka Ringwald                     break;
1641db3cdbd4SMilanka Ringwald                 case 1:
1642c78e7b73SMatthias Ringwald                     hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1643db3cdbd4SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_state(hfp_connection);
1644db3cdbd4SMilanka Ringwald                     break;
1645db3cdbd4SMilanka Ringwald                 case 2:
164645796ff1SMilanka Ringwald                     hfp_connection->ag_msg.text_id = 0;
1647db3cdbd4SMilanka Ringwald                     for (i = 0 ; i < 4; i++){
164845796ff1SMilanka Ringwald                         hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]);
1649db3cdbd4SMilanka Ringwald                     }
1650db3cdbd4SMilanka Ringwald                     break;
1651db3cdbd4SMilanka Ringwald                 case 3:
1652c78e7b73SMatthias Ringwald                     hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1653db3cdbd4SMilanka Ringwald                     break;
1654db3cdbd4SMilanka Ringwald                 case 4:
1655c78e7b73SMatthias Ringwald                     hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1656db3cdbd4SMilanka Ringwald                     break;
1657db3cdbd4SMilanka Ringwald                 case 5:
1658db3cdbd4SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_text(hfp_connection, hfp_connection->line_size, &hfp_connection->line_buffer[0]);
1659db3cdbd4SMilanka Ringwald                     break;
1660db3cdbd4SMilanka Ringwald                 default:
1661db3cdbd4SMilanka Ringwald                     break;
1662db3cdbd4SMilanka Ringwald             }
1663db3cdbd4SMilanka Ringwald             break;
16643deb3ec6SMatthias Ringwald         default:
16653deb3ec6SMatthias Ringwald             break;
16663deb3ec6SMatthias Ringwald     }
16673deb3ec6SMatthias Ringwald }
16683deb3ec6SMatthias Ringwald 
16691d9c9c90SMilanka Ringwald static void hfp_handle_start_sdp_client_query(void * context){
16701d9c9c90SMilanka Ringwald     UNUSED(context);
16711d9c9c90SMilanka Ringwald 
16721d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_t it;
16731d9c9c90SMilanka Ringwald     btstack_linked_list_iterator_init(&it, &hfp_connections);
16741d9c9c90SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
16751d9c9c90SMilanka Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
16761d9c9c90SMilanka Ringwald 
16771d9c9c90SMilanka Ringwald         if (connection->state != HFP_W2_SEND_SDP_QUERY) continue;
16781d9c9c90SMilanka Ringwald 
16791d9c9c90SMilanka Ringwald         connection->state = HFP_W4_SDP_QUERY_COMPLETE;
16801d9c9c90SMilanka Ringwald         sdp_query_context.local_role = connection->local_role;
16811d9c9c90SMilanka Ringwald         (void)memcpy(sdp_query_context.remote_address, connection->remote_addr, 6);
1682e55c05e3SMatthias Ringwald         sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid);
16831d9c9c90SMilanka Ringwald         return;
16841d9c9c90SMilanka Ringwald     }
16851d9c9c90SMilanka Ringwald }
16861d9c9c90SMilanka Ringwald 
16874eb3f1d8SMilanka Ringwald uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
16884eb3f1d8SMilanka Ringwald     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
16894eb3f1d8SMilanka Ringwald     if (connection){
16904eb3f1d8SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16913deb3ec6SMatthias Ringwald     }
16924eb3f1d8SMilanka Ringwald 
16934eb3f1d8SMilanka Ringwald     connection = hfp_create_connection(bd_addr, local_role);
16944eb3f1d8SMilanka Ringwald     if (!connection){
16954eb3f1d8SMilanka Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
16964eb3f1d8SMilanka Ringwald     }
16974eb3f1d8SMilanka Ringwald 
16984eb3f1d8SMilanka Ringwald     connection->state = HFP_W2_SEND_SDP_QUERY;
16994eb3f1d8SMilanka Ringwald 
17004eb3f1d8SMilanka Ringwald     bd_addr_copy(connection->remote_addr, bd_addr);
17014eb3f1d8SMilanka Ringwald     connection->service_uuid = service_uuid;
17021d9c9c90SMilanka Ringwald 
17031d9c9c90SMilanka Ringwald     hfp_handle_sdp_client_query_request.callback = &hfp_handle_start_sdp_client_query;
17041d9c9c90SMilanka Ringwald     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
17051d9c9c90SMilanka Ringwald     (void) sdp_client_register_query_callback(&hfp_handle_sdp_client_query_request);
17064eb3f1d8SMilanka Ringwald     return ERROR_CODE_SUCCESS;
17073deb3ec6SMatthias Ringwald }
17083deb3ec6SMatthias Ringwald 
1709657bc59fSMilanka Ringwald void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){
17101ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1711657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
17123deb3ec6SMatthias Ringwald 
1713657bc59fSMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
17141ffa0dd9SMilanka Ringwald 
1715657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1716657bc59fSMilanka Ringwald         hfp_connection->state = HFP_IDLE;
17173deb3ec6SMatthias Ringwald         return;
17183deb3ec6SMatthias Ringwald     }
17193deb3ec6SMatthias Ringwald 
1720657bc59fSMilanka Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1721657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
17223deb3ec6SMatthias Ringwald         return;
17233deb3ec6SMatthias Ringwald     }
17243deb3ec6SMatthias Ringwald 
1725657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1726657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
17270c3047fbSMatthias Ringwald         return;
17280c3047fbSMatthias Ringwald     }
17290c3047fbSMatthias Ringwald 
1730657bc59fSMilanka Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1731657bc59fSMilanka Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
17320c3047fbSMatthias Ringwald         return;
17330c3047fbSMatthias Ringwald     }
17340c3047fbSMatthias Ringwald 
17350c3047fbSMatthias Ringwald     // HFP_W4_SCO_DISCONNECTED or later
1736657bc59fSMilanka Ringwald     hfp_connection->release_slc_connection = 1;
17373deb3ec6SMatthias Ringwald }
17383deb3ec6SMatthias Ringwald 
1739*0b4debbfSMilanka Ringwald uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){
17401ffa0dd9SMilanka Ringwald     // called internally, NULL check already performed
1741657bc59fSMilanka Ringwald     btstack_assert(hfp_connection != NULL);
1742*0b4debbfSMilanka Ringwald     if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1743*0b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
17441ffa0dd9SMilanka Ringwald     }
1745*0b4debbfSMilanka Ringwald     switch (hfp_connection->state) {
1746*0b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
1747*0b4debbfSMilanka Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1748*0b4debbfSMilanka Ringwald             break;
1749*0b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
1750*0b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1751*0b4debbfSMilanka Ringwald             hfp_connection->release_audio_connection = 1;
1752*0b4debbfSMilanka Ringwald             break;
1753*0b4debbfSMilanka Ringwald         default:
1754*0b4debbfSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1755*0b4debbfSMilanka Ringwald     }
1756*0b4debbfSMilanka Ringwald     return ERROR_CODE_SUCCESS;
17573deb3ec6SMatthias Ringwald }
17583deb3ec6SMatthias Ringwald 
1759ce263fc8SMatthias Ringwald static const struct link_settings {
1760ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1761ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1762ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1763ebc6f15bSMatthias Ringwald     const bool     eSCO;
1764ebc6f15bSMatthias Ringwald     const uint8_t  codec;
1765ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1766352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV1,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0
1767352a0504SMatthias Ringwald     { 0xffff, 0xff, SCO_PACKET_TYPES_HV3,  false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1
1768352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1
1769352a0504SMatthias Ringwald     { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2
1770352a0504SMatthias Ringwald     { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3
1771352a0504SMatthias Ringwald     { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4
1772352a0504SMatthias Ringwald     { 0x0008, 0x02, SCO_PACKET_TYPES_EV3,  true,  HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1
1773352a0504SMatthias Ringwald     { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true,  HFP_CODEC_MSBC }  // HFP_LINK_SETTINGS_T2
1774ce263fc8SMatthias Ringwald };
17753deb3ec6SMatthias Ringwald 
1776eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1777ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1778eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1779ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1780eddcd308SMatthias Ringwald     sco_establishment_active = hfp_connection;
1781d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1782d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1783689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1784689d4323SMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1785689d4323SMatthias Ringwald #else
1786689d4323SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1787689d4323SMatthias Ringwald #endif
1788d2c1d59aSMatthias Ringwald     }
1789352a0504SMatthias Ringwald     // get packet types - bits 6-9 are 'don't allow'
1790352a0504SMatthias Ringwald     uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0;
1791eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1792352a0504SMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types);
1793ce263fc8SMatthias Ringwald }
1794d68dcce1SMatthias Ringwald 
1795cb81d35dSMatthias Ringwald void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){
1796cb81d35dSMatthias Ringwald 
1797cb81d35dSMatthias Ringwald     // remote supported feature eSCO is set if link type is eSCO
1798cb81d35dSMatthias Ringwald     // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
1799cb81d35dSMatthias Ringwald     uint16_t max_latency;
1800cb81d35dSMatthias Ringwald     uint8_t  retransmission_effort;
1801cb81d35dSMatthias Ringwald     uint16_t packet_types;
1802cb81d35dSMatthias Ringwald 
1803cb81d35dSMatthias Ringwald     if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
1804cb81d35dSMatthias Ringwald         max_latency = 0x000c;
1805cb81d35dSMatthias Ringwald         retransmission_effort = 0x02;
1806cb81d35dSMatthias Ringwald         // eSCO: EV3 and 2-EV3
1807cb81d35dSMatthias Ringwald         packet_types = 0x0048;
1808cb81d35dSMatthias Ringwald     } else {
1809cb81d35dSMatthias Ringwald         max_latency = 0xffff;
1810cb81d35dSMatthias Ringwald         retransmission_effort = 0xff;
1811cb81d35dSMatthias Ringwald         // sco: HV1 and HV3
1812cb81d35dSMatthias Ringwald         packet_types = 0x005;
1813cb81d35dSMatthias Ringwald     }
1814cb81d35dSMatthias Ringwald 
1815cb81d35dSMatthias Ringwald     // mSBC only allows for transparent data
1816cb81d35dSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1817cb81d35dSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1818cb81d35dSMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1819cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
1820cb81d35dSMatthias Ringwald #else
1821cb81d35dSMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
1822cb81d35dSMatthias Ringwald #endif
1823cb81d35dSMatthias Ringwald     }
1824cb81d35dSMatthias Ringwald 
1825cb81d35dSMatthias Ringwald     // filter packet types
1826cb81d35dSMatthias Ringwald     packet_types &= hfp_get_sco_packet_types();
1827cb81d35dSMatthias Ringwald 
1828cb81d35dSMatthias Ringwald     // bits 6-9 are 'don't allow'
1829cb81d35dSMatthias Ringwald     packet_types ^= 0x3c0;
1830cb81d35dSMatthias Ringwald 
1831cb81d35dSMatthias Ringwald     log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
1832cb81d35dSMatthias Ringwald     hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
1833cb81d35dSMatthias Ringwald                  sco_voice_setting, retransmission_effort, packet_types);
1834cb81d35dSMatthias Ringwald }
1835cb81d35dSMatthias Ringwald 
18363721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
18373721a235SMatthias Ringwald void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){
18383721a235SMatthias Ringwald     hfp_connection->cc256x_send_write_codec_config = true;
18393721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
18403721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = true;
18413721a235SMatthias Ringwald     }
18423721a235SMatthias Ringwald }
18433721a235SMatthias Ringwald 
18443721a235SMatthias Ringwald void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){
18453721a235SMatthias Ringwald     uint32_t sample_rate_hz;
18463721a235SMatthias Ringwald     uint16_t clock_rate_khz;
18473721a235SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
18483721a235SMatthias Ringwald         clock_rate_khz = 512;
18493721a235SMatthias Ringwald         sample_rate_hz = 16000;
18503721a235SMatthias Ringwald     } else {
18513721a235SMatthias Ringwald         clock_rate_khz = 256;
18523721a235SMatthias Ringwald         sample_rate_hz = 8000;
18533721a235SMatthias Ringwald     }
18543721a235SMatthias Ringwald     uint8_t clock_direction = 0;        // master
18553721a235SMatthias Ringwald     uint16_t frame_sync_duty_cycle = 0; // i2s with 50%
18563721a235SMatthias Ringwald     uint8_t  frame_sync_edge = 1;       // rising edge
18573721a235SMatthias Ringwald     uint8_t  frame_sync_polarity = 0;   // active high
18583721a235SMatthias Ringwald     uint8_t  reserved = 0;
18593721a235SMatthias Ringwald     uint16_t size = 16;
18603721a235SMatthias Ringwald     uint16_t chan_1_offset = 1;
18613721a235SMatthias Ringwald     uint16_t chan_2_offset = chan_1_offset + size;
18623721a235SMatthias Ringwald     uint8_t  out_edge = 1;              // rising
18633721a235SMatthias Ringwald     uint8_t  in_edge = 0;               // falling
18643721a235SMatthias Ringwald     hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle,
18653721a235SMatthias Ringwald                  frame_sync_edge, frame_sync_polarity, reserved,
18663721a235SMatthias Ringwald                  size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved,
18673721a235SMatthias Ringwald                  size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved);
18683721a235SMatthias Ringwald }
18693721a235SMatthias Ringwald #endif
18703721a235SMatthias Ringwald 
1871689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1872689d4323SMatthias Ringwald void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){
1873689d4323SMatthias Ringwald     hfp_connection->bcm_send_write_i2spcm_interface_param = true;
1874689d4323SMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
1875689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = true;
1876689d4323SMatthias Ringwald     }
1877689d4323SMatthias Ringwald }
1878689d4323SMatthias Ringwald void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){
1879689d4323SMatthias Ringwald     uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0;
18801d2bbd54SMatthias Ringwald     // i2s enable, master, 8/16 kHz, 512 kHz
18811d2bbd54SMatthias Ringwald     hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2);
1882689d4323SMatthias Ringwald }
1883689d4323SMatthias Ringwald #endif
1884689d4323SMatthias Ringwald 
18851b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
18861b005648SMatthias Ringwald     hfp_hf_callback = callback;
18871b005648SMatthias Ringwald }
18881b005648SMatthias Ringwald 
18891b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
18901b005648SMatthias Ringwald     hfp_ag_callback = callback;
18911b005648SMatthias Ringwald }
18921b005648SMatthias Ringwald 
1893ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1894ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
1895ca59be51SMatthias Ringwald }
18961b005648SMatthias Ringwald 
1897ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1898ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
1899d68dcce1SMatthias Ringwald }
1900d68dcce1SMatthias Ringwald 
1901520c92d5SMatthias Ringwald void hfp_init(void){
1902352a0504SMatthias Ringwald     hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL;
1903991c26beSMatthias Ringwald }
1904991c26beSMatthias Ringwald 
190520b2edb6SMatthias Ringwald void hfp_deinit(void){
190620b2edb6SMatthias Ringwald     hfp_connections = NULL;
190720b2edb6SMatthias Ringwald     hfp_hf_callback = NULL;
190820b2edb6SMatthias Ringwald     hfp_ag_callback = NULL;
190920b2edb6SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = NULL;
191020b2edb6SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = NULL;
191120b2edb6SMatthias Ringwald     sco_establishment_active = NULL;
191220b2edb6SMatthias Ringwald     (void) memset(&sdp_query_context, 0, sizeof(hfp_sdp_query_context_t));
191320b2edb6SMatthias Ringwald     (void) memset(&hfp_handle_sdp_client_query_request, 0, sizeof(btstack_context_callback_registration_t));
191420b2edb6SMatthias Ringwald }
191520b2edb6SMatthias Ringwald 
1916991c26beSMatthias Ringwald void hfp_set_sco_packet_types(uint16_t packet_types){
191701e5b727SMatthias Ringwald     hfp_allowed_sco_packet_types = packet_types;
1918991c26beSMatthias Ringwald }
1919991c26beSMatthias Ringwald 
1920991c26beSMatthias Ringwald uint16_t hfp_get_sco_packet_types(void){
192101e5b727SMatthias Ringwald     return hfp_allowed_sco_packet_types;
1922520c92d5SMatthias Ringwald }
1923186dd3d2SMatthias Ringwald 
1924e453c1d9SMatthias 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){
1925e453c1d9SMatthias Ringwald     int8_t setting = (int8_t) current_setting;
1926e453c1d9SMatthias Ringwald     bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported;
1927e453c1d9SMatthias Ringwald     while (setting > 0){
1928e453c1d9SMatthias Ringwald         setting--;
1929e453c1d9SMatthias Ringwald         // skip if eSCO required but not available
1930e453c1d9SMatthias Ringwald         if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue;
1931e453c1d9SMatthias Ringwald         // skip if S4 but not supported
1932e453c1d9SMatthias Ringwald         if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue;
1933e453c1d9SMatthias Ringwald         // skip wrong codec
1934e453c1d9SMatthias Ringwald         if ( hfp_link_settings[setting].codec != negotiated_codec) continue;
193501e5b727SMatthias Ringwald         // skip disabled packet types
1936352a0504SMatthias Ringwald         uint16_t required_packet_types = hfp_link_settings[setting].packet_types;
1937352a0504SMatthias Ringwald         uint16_t allowed_packet_types  = hfp_allowed_sco_packet_types;
1938352a0504SMatthias Ringwald         if ((required_packet_types & allowed_packet_types) == 0) continue;
193901e5b727SMatthias Ringwald 
1940e453c1d9SMatthias Ringwald         // found matching setting
1941e453c1d9SMatthias Ringwald         return (hfp_link_settings_t) setting;
1942afac2a04SMilanka Ringwald     }
1943e453c1d9SMatthias Ringwald     return HFP_LINK_SETTINGS_NONE;
1944afac2a04SMilanka Ringwald }
1945e453c1d9SMatthias Ringwald 
19462b7abbfbSMatthias 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){
1947e453c1d9SMatthias Ringwald     bool local_eSCO_supported  = hci_extended_sco_link_supported();
1948e453c1d9SMatthias Ringwald     bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle);
1949e453c1d9SMatthias Ringwald     uint8_t negotiated_codec   = hfp_connection->negotiated_codec;
19502b7abbfbSMatthias Ringwald     return  hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec);
19512b7abbfbSMatthias Ringwald }
19522b7abbfbSMatthias Ringwald 
19532b7abbfbSMatthias Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){
1954e453c1d9SMatthias Ringwald     // get highest possible link setting
19552b7abbfbSMatthias Ringwald     hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported);
1956afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
1957afac2a04SMilanka Ringwald }
1958afac2a04SMilanka Ringwald 
1959186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
1960186dd3d2SMatthias Ringwald 
1961186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
1962186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1963186dd3d2SMatthias Ringwald     // encode \n\r
1964186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
196515a27967SMatthias Ringwald     int i = 0;
1966186dd3d2SMatthias Ringwald     int pos;
196715a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
1968186dd3d2SMatthias Ringwald         switch (packet[pos]){
1969186dd3d2SMatthias Ringwald             case '\n':
1970186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1971186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
1972186dd3d2SMatthias Ringwald                 break;
1973186dd3d2SMatthias Ringwald             case '\r':
1974186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1975186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
1976186dd3d2SMatthias Ringwald                 break;
1977186dd3d2SMatthias Ringwald             default:
1978186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
1979186dd3d2SMatthias Ringwald                 break;
1980186dd3d2SMatthias Ringwald         }
1981186dd3d2SMatthias Ringwald     }
1982186dd3d2SMatthias Ringwald     printable[i] = 0;
1983186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
1984186dd3d2SMatthias Ringwald #endif
1985186dd3d2SMatthias Ringwald }
1986