xref: /btstack/src/classic/hfp.c (revision 0222a807f0850c38f466a77b75db79b644a65ef3)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
417907f069SMatthias Ringwald #include "btstack_config.h"
423deb3ec6SMatthias Ringwald 
433deb3ec6SMatthias Ringwald #include <stdint.h>
443deb3ec6SMatthias Ringwald #include <stdio.h>
453deb3ec6SMatthias Ringwald #include <stdlib.h>
463deb3ec6SMatthias Ringwald #include <string.h>
473deb3ec6SMatthias Ringwald #include <inttypes.h>
483deb3ec6SMatthias Ringwald 
49235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
50023f2764SMatthias Ringwald #include "btstack_debug.h"
5159c6af15SMatthias Ringwald #include "btstack_event.h"
5259c6af15SMatthias Ringwald #include "btstack_memory.h"
5359c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5459c6af15SMatthias Ringwald #include "classic/core.h"
55efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
56746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
57023f2764SMatthias Ringwald #include "classic/sdp_util.h"
5859c6af15SMatthias Ringwald #include "hci.h"
5959c6af15SMatthias Ringwald #include "hci_cmd.h"
6059c6af15SMatthias Ringwald #include "hci_dump.h"
6159c6af15SMatthias Ringwald #include "l2cap.h"
623deb3ec6SMatthias Ringwald 
633deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
643deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
653deb3ec6SMatthias Ringwald 
663deb3ec6SMatthias Ringwald 
673deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
683deb3ec6SMatthias Ringwald     "EC and/or NR function",
693deb3ec6SMatthias Ringwald     "Three-way calling",
703deb3ec6SMatthias Ringwald     "CLI presentation capability",
713deb3ec6SMatthias Ringwald     "Voice recognition activation",
723deb3ec6SMatthias Ringwald     "Remote volume control",
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald     "Enhanced call status",
753deb3ec6SMatthias Ringwald     "Enhanced call control",
763deb3ec6SMatthias Ringwald 
773deb3ec6SMatthias Ringwald     "Codec negotiation",
783deb3ec6SMatthias Ringwald 
793deb3ec6SMatthias Ringwald     "HF Indicators",
803deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
813deb3ec6SMatthias Ringwald     "Reserved for future definition"
823deb3ec6SMatthias Ringwald };
833deb3ec6SMatthias Ringwald 
843deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
853deb3ec6SMatthias Ringwald     "Three-way calling",
863deb3ec6SMatthias Ringwald     "EC and/or NR function",
873deb3ec6SMatthias Ringwald     "Voice recognition function",
883deb3ec6SMatthias Ringwald     "In-band ring tone capability",
893deb3ec6SMatthias Ringwald     "Attach a number to a voice tag",
903deb3ec6SMatthias Ringwald     "Ability to reject a call",
913deb3ec6SMatthias Ringwald     "Enhanced call status",
923deb3ec6SMatthias Ringwald     "Enhanced call control",
933deb3ec6SMatthias Ringwald     "Extended Error Result Codes",
943deb3ec6SMatthias Ringwald     "Codec negotiation",
953deb3ec6SMatthias Ringwald     "HF Indicators",
963deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
973deb3ec6SMatthias Ringwald     "Reserved for future definition"
983deb3ec6SMatthias Ringwald };
993deb3ec6SMatthias Ringwald 
1000aee97efSMilanka Ringwald static const char * hfp_enhanced_call_dir[] = {
1010aee97efSMilanka Ringwald     "outgoing",
1020aee97efSMilanka Ringwald     "incoming"
1030aee97efSMilanka Ringwald };
1040aee97efSMilanka Ringwald 
1050aee97efSMilanka Ringwald static const char * hfp_enhanced_call_status[] = {
1060aee97efSMilanka Ringwald     "active",
1070aee97efSMilanka Ringwald     "held",
1080aee97efSMilanka Ringwald     "outgoing dialing",
1090aee97efSMilanka Ringwald     "outgoing alerting",
1100aee97efSMilanka Ringwald     "incoming",
1110aee97efSMilanka Ringwald     "incoming waiting",
1120aee97efSMilanka Ringwald     "call held by response and hold"
1130aee97efSMilanka Ringwald };
1140aee97efSMilanka Ringwald 
1150aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mode[] = {
1160aee97efSMilanka Ringwald     "voice",
1170aee97efSMilanka Ringwald     "data",
1180aee97efSMilanka Ringwald     "fax"
1190aee97efSMilanka Ringwald };
1200aee97efSMilanka Ringwald 
1210aee97efSMilanka Ringwald static const char * hfp_enhanced_call_mpty[] = {
1220aee97efSMilanka Ringwald     "not a conference call",
1230aee97efSMilanka Ringwald     "conference call"
1240aee97efSMilanka Ringwald };
1250aee97efSMilanka Ringwald 
1260aee97efSMilanka Ringwald const char * hfp_enhanced_call_dir2str(uint16_t index){
1270aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index];
1280aee97efSMilanka Ringwald     return "not defined";
1290aee97efSMilanka Ringwald }
1300aee97efSMilanka Ringwald 
1310aee97efSMilanka Ringwald const char * hfp_enhanced_call_status2str(uint16_t index){
1320aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index];
1330aee97efSMilanka Ringwald     return "not defined";
1340aee97efSMilanka Ringwald }
1350aee97efSMilanka Ringwald 
1360aee97efSMilanka Ringwald const char * hfp_enhanced_call_mode2str(uint16_t index){
1370aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index];
1380aee97efSMilanka Ringwald     return "not defined";
1390aee97efSMilanka Ringwald }
1400aee97efSMilanka Ringwald 
1410aee97efSMilanka Ringwald const char * hfp_enhanced_call_mpty2str(uint16_t index){
1420aee97efSMilanka Ringwald     if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index];
1430aee97efSMilanka Ringwald     return "not defined";
1440aee97efSMilanka Ringwald }
1450aee97efSMilanka Ringwald 
146ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
147ca59be51SMatthias Ringwald 
1489c9c64c1SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL;
1499c9c64c1SMatthias Ringwald 
150ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
151ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
152ca59be51SMatthias Ringwald 
153ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler;
154ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler;
155f4000eebSMatthias Ringwald 
1561b005648SMatthias Ringwald static void (*hfp_hf_run_for_context)(hfp_connection_t * hfp_connection);
1571b005648SMatthias Ringwald 
158eddcd308SMatthias Ringwald static hfp_connection_t * sco_establishment_active;
159eddcd308SMatthias Ringwald 
1603deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
1613deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
1623deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
1633deb3ec6SMatthias Ringwald     }
1643deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
1653deb3ec6SMatthias Ringwald }
1663deb3ec6SMatthias Ringwald 
1673deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
1683deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
1693deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
1703deb3ec6SMatthias Ringwald     }
1713deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
1743deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
1753deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
17674386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
17728190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
1783deb3ec6SMatthias Ringwald     if (err){
17928190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
1803deb3ec6SMatthias Ringwald     }
1813deb3ec6SMatthias Ringwald     return 1;
1823deb3ec6SMatthias Ringwald }
1833deb3ec6SMatthias Ringwald 
1846a7f44bdSMilanka Ringwald int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){
185e8d1dc0dSMatthias Ringwald 
186e8d1dc0dSMatthias Ringwald     // mSBC requires support for eSCO connections
187c1ab6cc1SMatthias Ringwald     if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0;
188e8d1dc0dSMatthias Ringwald 
189df327ff3SMilanka Ringwald     int i;
190df327ff3SMilanka Ringwald     for (i = 0; i < codecs_nr; i++){
191e8d1dc0dSMatthias Ringwald         if (codecs[i] != codec) continue;
192e8d1dc0dSMatthias Ringwald         return 1;
193df327ff3SMilanka Ringwald     }
194df327ff3SMilanka Ringwald     return 0;
195df327ff3SMilanka Ringwald }
196df327ff3SMilanka Ringwald 
197d715cf51SMatthias Ringwald void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){
198d715cf51SMatthias Ringwald     if (hci_extended_sco_link_supported()) return;
199d715cf51SMatthias Ringwald     uint8_t tmp_codecs[HFP_MAX_NUM_CODECS];
200d715cf51SMatthias Ringwald     int i;
201d715cf51SMatthias Ringwald     int tmp_codec_nr = 0;
202d715cf51SMatthias Ringwald     for (i=0; i < *codecs_nr; i++){
203d715cf51SMatthias Ringwald         if (codecs[i] == HFP_CODEC_MSBC) continue;
204d715cf51SMatthias Ringwald         tmp_codecs[tmp_codec_nr++] = codecs[i];
205d715cf51SMatthias Ringwald     }
206d715cf51SMatthias Ringwald     *codecs_nr = tmp_codec_nr;
2076535961aSMatthias Ringwald     (void)memcpy(codecs, tmp_codecs, tmp_codec_nr);
208d715cf51SMatthias Ringwald }
209d715cf51SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald // UTILS
2113deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
2123deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
2153deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
2163deb3ec6SMatthias Ringwald     if (value){
2173deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
2183deb3ec6SMatthias Ringwald     } else {
2193deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
2203deb3ec6SMatthias Ringwald     }
2213deb3ec6SMatthias Ringwald     return bitmap;
2223deb3ec6SMatthias Ringwald }
2233deb3ec6SMatthias Ringwald 
2243deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
225c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2263deb3ec6SMatthias Ringwald     int i;
2273deb3ec6SMatthias Ringwald     int offset = 0;
228c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2293deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
2303deb3ec6SMatthias Ringwald     }
2313deb3ec6SMatthias Ringwald     if (i<values_nr){
2323deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
2333deb3ec6SMatthias Ringwald     }
2343deb3ec6SMatthias Ringwald     return offset;
2353deb3ec6SMatthias Ringwald }
2363deb3ec6SMatthias Ringwald 
2373deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
238c1ab6cc1SMatthias Ringwald     if (buffer_size < (values_nr * 3)) return 0;
2393deb3ec6SMatthias Ringwald 
2403deb3ec6SMatthias Ringwald     int i;
2413deb3ec6SMatthias Ringwald     int offset = 0;
242c1ab6cc1SMatthias Ringwald     for (i = 0; i < (values_nr-1); i++) {
2433deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
2443deb3ec6SMatthias Ringwald     }
2453deb3ec6SMatthias Ringwald 
2463deb3ec6SMatthias Ringwald     if (i<values_nr){
2473deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
2483deb3ec6SMatthias Ringwald     }
2493deb3ec6SMatthias Ringwald     return offset;
2503deb3ec6SMatthias Ringwald }
2513deb3ec6SMatthias Ringwald 
252ca59be51SMatthias Ringwald static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){
253ca59be51SMatthias Ringwald     if (!hfp_connection) return;
254ca59be51SMatthias Ringwald     btstack_packet_handler_t callback = NULL;
255ca59be51SMatthias Ringwald     switch (hfp_connection->local_role){
256ca59be51SMatthias Ringwald         case HFP_ROLE_HF:
257ca59be51SMatthias Ringwald             callback = hfp_hf_callback;
258671f1aa2SMatthias Ringwald             break;
259ca59be51SMatthias Ringwald         case HFP_ROLE_AG:
260ca59be51SMatthias Ringwald             callback = hfp_ag_callback;
261671f1aa2SMatthias Ringwald             break;
262ca59be51SMatthias Ringwald         default:
263ca59be51SMatthias Ringwald             return;
264ca59be51SMatthias Ringwald     }
265ca59be51SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
266ca59be51SMatthias Ringwald }
267ca59be51SMatthias Ringwald 
268ca59be51SMatthias Ringwald void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){
269a0ffb263SMatthias Ringwald     uint8_t event[3];
270a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
271a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
272a0ffb263SMatthias Ringwald     event[2] = event_subtype;
273ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
274a0ffb263SMatthias Ringwald }
275a0ffb263SMatthias Ringwald 
276ca59be51SMatthias Ringwald void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){
2773deb3ec6SMatthias Ringwald     uint8_t event[4];
2783deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
2793deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
2803deb3ec6SMatthias Ringwald     event[2] = event_subtype;
2813deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
282ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
2833deb3ec6SMatthias Ringwald }
2843deb3ec6SMatthias Ringwald 
285ca59be51SMatthias 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){
2868901a7c4SMatthias Ringwald     uint8_t event[12];
2876a7f44bdSMilanka Ringwald     int pos = 0;
2886a7f44bdSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
2896a7f44bdSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
290d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
2916a7f44bdSMilanka Ringwald     event[pos++] = status; // status 0 == OK
2926a7f44bdSMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
2936a7f44bdSMilanka Ringwald     pos += 2;
2946a7f44bdSMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
2956a7f44bdSMilanka Ringwald     pos += 6;
296ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
297a0653c3bSMilanka Ringwald }
298a0653c3bSMilanka Ringwald 
299ca59be51SMatthias Ringwald static void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr, uint8_t  negotiated_codec){
300d0c4aea6SMilanka Ringwald     uint8_t event[13];
301d0c4aea6SMilanka Ringwald     int pos = 0;
302d0c4aea6SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
303d0c4aea6SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
304d0c4aea6SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
305d0c4aea6SMilanka Ringwald     event[pos++] = status; // status 0 == OK
306d0c4aea6SMilanka Ringwald     little_endian_store_16(event, pos, con_handle);
307d0c4aea6SMilanka Ringwald     pos += 2;
308d0c4aea6SMilanka Ringwald     reverse_bd_addr(addr,&event[pos]);
309d0c4aea6SMilanka Ringwald     pos += 6;
310d0c4aea6SMilanka Ringwald     event[pos++] = negotiated_codec;
311ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
312d0c4aea6SMilanka Ringwald }
313d0c4aea6SMilanka Ringwald 
314ca59be51SMatthias Ringwald void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){
315c1797c7dSMatthias Ringwald     uint8_t event[40];
316aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
317aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
318aa4dd815SMatthias Ringwald     event[2] = event_subtype;
31909ca35c4SMilanka Ringwald     uint16_t size = btstack_min(strlen(value), sizeof(event) - 4);
320aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
321aa4dd815SMatthias Ringwald     event[3 + size] = 0;
322ca59be51SMatthias Ringwald     hfp_emit_event_for_context(hfp_connection, event, sizeof(event));
323aa4dd815SMatthias Ringwald }
324aa4dd815SMatthias Ringwald 
3250cb5b971SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(void){
3268f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
3273deb3ec6SMatthias Ringwald }
3283deb3ec6SMatthias Ringwald 
3293deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
330665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
331665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
332665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
333a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
334a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
335a0ffb263SMatthias Ringwald             return hfp_connection;
3363deb3ec6SMatthias Ringwald         }
3373deb3ec6SMatthias Ringwald     }
3383deb3ec6SMatthias Ringwald     return NULL;
3393deb3ec6SMatthias Ringwald }
3403deb3ec6SMatthias Ringwald 
341405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){
342665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
343665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
344665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
345a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
3465df9dc78SMatthias Ringwald         if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) {
347a0ffb263SMatthias Ringwald             return hfp_connection;
3483deb3ec6SMatthias Ringwald         }
3493deb3ec6SMatthias Ringwald     }
3503deb3ec6SMatthias Ringwald     return NULL;
3513deb3ec6SMatthias Ringwald }
3523deb3ec6SMatthias Ringwald 
353405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){
354665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
355665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
356665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
357a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
358c1ab6cc1SMatthias Ringwald         if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){
359a0ffb263SMatthias Ringwald             return hfp_connection;
3603deb3ec6SMatthias Ringwald         }
3613deb3ec6SMatthias Ringwald     }
3623deb3ec6SMatthias Ringwald     return NULL;
3633deb3ec6SMatthias Ringwald }
3643deb3ec6SMatthias Ringwald 
365405014fbSMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){
366d97d752dSMilanka Ringwald     btstack_linked_list_iterator_t it;
367d97d752dSMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
368d97d752dSMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
369d97d752dSMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
370c1ab6cc1SMatthias Ringwald         if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){
371d97d752dSMilanka Ringwald             return hfp_connection;
372d97d752dSMilanka Ringwald         }
373d97d752dSMilanka Ringwald     }
374d97d752dSMilanka Ringwald     return NULL;
375d97d752dSMilanka Ringwald }
376d97d752dSMilanka Ringwald 
377a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
378a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
379a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
380a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
3813deb3ec6SMatthias Ringwald 
382dd533208SMatthias Ringwald     hfp_connection->found_equal_sign = false;
3833deb3ec6SMatthias Ringwald 
384a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
385a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
3863deb3ec6SMatthias Ringwald 
387a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
388a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
3893deb3ec6SMatthias Ringwald 
390a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
391a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
3927522e673SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
393a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
3943deb3ec6SMatthias Ringwald 
395a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
396a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
397a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
398a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
3993deb3ec6SMatthias Ringwald }
4003deb3ec6SMatthias Ringwald 
401fffdd288SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(void){
402a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
403a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
4043deb3ec6SMatthias Ringwald 
405a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
406a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
407a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
408aa4dd815SMatthias Ringwald 
409a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
410a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
4113deb3ec6SMatthias Ringwald 
412d751f069SMatthias Ringwald     hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID;
413d751f069SMatthias Ringwald     hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
414d751f069SMatthias Ringwald 
415a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
4163deb3ec6SMatthias Ringwald 
417d63c37a1SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection);
418a0ffb263SMatthias Ringwald     return hfp_connection;
4193deb3ec6SMatthias Ringwald }
4203deb3ec6SMatthias Ringwald 
421a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){
422a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
42309a233a1SMatthias Ringwald     btstack_memory_hfp_connection_free(hfp_connection);
4243deb3ec6SMatthias Ringwald }
4253deb3ec6SMatthias Ringwald 
426323d3000SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t local_role){
427405014fbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role);
428a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
429a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
4306535961aSMatthias Ringwald     (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
431323d3000SMatthias Ringwald     hfp_connection->local_role = local_role;
432bdf572f4SMatthias Ringwald     log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr));
433bdf572f4SMatthias Ringwald 
434a0ffb263SMatthias Ringwald     return hfp_connection;
4353deb3ec6SMatthias Ringwald }
4363deb3ec6SMatthias Ringwald 
437aa4dd815SMatthias Ringwald /* @param network.
438aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
439aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
440aa4dd815SMatthias Ringwald  */
4413deb3ec6SMatthias Ringwald 
4423deb3ec6SMatthias Ringwald /* @param suported_features
4433deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
4443deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
4453deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
4463deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
4473deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
4483deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
4493deb3ec6SMatthias Ringwald  */
4503deb3ec6SMatthias Ringwald  /* Bit position:
4513deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
4523deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
4533deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
4543deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
4553deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
4563deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
4573deb3ec6SMatthias Ringwald  */
4583deb3ec6SMatthias Ringwald 
4599b1c3b4dSMatthias 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){
4603deb3ec6SMatthias Ringwald     uint8_t* attribute;
4613deb3ec6SMatthias Ringwald     de_create_sequence(service);
4623deb3ec6SMatthias Ringwald 
4633deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
464235946f1SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
4659b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
4663deb3ec6SMatthias Ringwald 
4673deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
468235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
4693deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4703deb3ec6SMatthias Ringwald     {
4713deb3ec6SMatthias Ringwald         //  "UUID for Service"
4723deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
473235946f1SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO);
4743deb3ec6SMatthias Ringwald     }
4753deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4763deb3ec6SMatthias Ringwald 
4773deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
478235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
4793deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4803deb3ec6SMatthias Ringwald     {
4813deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
4823deb3ec6SMatthias Ringwald         {
483235946f1SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
4843deb3ec6SMatthias Ringwald         }
4853deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
4863deb3ec6SMatthias Ringwald 
4873deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
4883deb3ec6SMatthias Ringwald         {
489235946f1SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM);  // rfcomm_service
4903deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
4913deb3ec6SMatthias Ringwald         }
4923deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
4933deb3ec6SMatthias Ringwald     }
4943deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4953deb3ec6SMatthias Ringwald 
4963deb3ec6SMatthias Ringwald 
4973deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
498235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
4993deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5003deb3ec6SMatthias Ringwald     {
501235946f1SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
5023deb3ec6SMatthias Ringwald     }
5033deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5043deb3ec6SMatthias Ringwald 
5053deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
506235946f1SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
5073deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
5083deb3ec6SMatthias Ringwald     {
5093deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
5103deb3ec6SMatthias Ringwald         {
511235946f1SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
5123deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
5133deb3ec6SMatthias Ringwald         }
5143deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
5153deb3ec6SMatthias Ringwald     }
5163deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
5173deb3ec6SMatthias Ringwald 
5183deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
5193deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
5203deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
5213deb3ec6SMatthias Ringwald }
5223deb3ec6SMatthias Ringwald 
5233deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL;
524a8747467SMatthias Ringwald 
5256c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
5268a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: handling own sdp events
5278a46ec40SMatthias Ringwald     UNUSED(channel);        // ok: no channel
5288a46ec40SMatthias Ringwald     UNUSED(size);           // ok: handling own sdp events
529a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = connection_doing_sdp_query;
53084904e95SMilanka Ringwald     if (!hfp_connection) {
53184904e95SMilanka Ringwald         log_error("handle_query_rfcomm_event, no connection");
53284904e95SMilanka Ringwald         return;
53384904e95SMilanka Ringwald     }
5343deb3ec6SMatthias Ringwald 
5350e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
5365611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
537a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
5383deb3ec6SMatthias Ringwald             break;
5395611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
5403deb3ec6SMatthias Ringwald             connection_doing_sdp_query = NULL;
541a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
542a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
543a0ffb263SMatthias Ringwald                 log_info("HFP: SDP_EVENT_QUERY_COMPLETE context %p, addr %s, state %d", hfp_connection, bd_addr_to_str( hfp_connection->remote_addr),  hfp_connection->state);
544520c92d5SMatthias Ringwald                 btstack_packet_handler_t packet_handler;
545520c92d5SMatthias Ringwald                 switch (hfp_connection->local_role){
546520c92d5SMatthias Ringwald                     case HFP_ROLE_AG:
547520c92d5SMatthias Ringwald                         packet_handler = hfp_ag_rfcomm_packet_handler;
548520c92d5SMatthias Ringwald                         break;
549520c92d5SMatthias Ringwald                     case HFP_ROLE_HF:
550520c92d5SMatthias Ringwald                         packet_handler = hfp_hf_rfcomm_packet_handler;
551520c92d5SMatthias Ringwald                         break;
552520c92d5SMatthias Ringwald                     default:
553520c92d5SMatthias Ringwald                         log_error("Role %x", hfp_connection->local_role);
554520c92d5SMatthias Ringwald                         return;
555520c92d5SMatthias Ringwald                 }
556ca59be51SMatthias Ringwald                 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
5573deb3ec6SMatthias Ringwald                 break;
5583deb3ec6SMatthias Ringwald             }
55984904e95SMilanka Ringwald             hfp_connection->state = HFP_IDLE;
560ca59be51SMatthias Ringwald             hfp_emit_slc_connection_event(hfp_connection, sdp_event_query_complete_get_status(packet), HCI_CON_HANDLE_INVALID, hfp_connection->remote_addr);
56172b50801SMatthias Ringwald             log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet));
5623deb3ec6SMatthias Ringwald             break;
5633deb3ec6SMatthias Ringwald         default:
5643deb3ec6SMatthias Ringwald             break;
5653deb3ec6SMatthias Ringwald     }
5663deb3ec6SMatthias Ringwald }
5673deb3ec6SMatthias Ringwald 
56838200c1dSMilanka Ringwald // returns 0 if unexpected error or no other link options remained, otherwise 1
56938200c1dSMilanka Ringwald static int hfp_handle_failed_sco_connection(uint8_t status){
570eddcd308SMatthias Ringwald 
571eddcd308SMatthias Ringwald     if (!sco_establishment_active){
572eddcd308SMatthias Ringwald         log_error("(e)SCO Connection failed but not started by us");
57338200c1dSMilanka Ringwald         return 0;
574eddcd308SMatthias Ringwald     }
57538200c1dSMilanka Ringwald     log_info("(e)SCO Connection failed status 0x%02x", status);
576eddcd308SMatthias Ringwald     // invalid params / unspecified error
577505f1c30SMatthias Ringwald     if ((status != 0x11) && (status != 0x1f) && (status != 0x0D)) return 0;
578eddcd308SMatthias Ringwald 
579eddcd308SMatthias Ringwald     switch (sco_establishment_active->link_setting){
580eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_D0:
58138200c1dSMilanka Ringwald             return 0; // no other option left
582eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_D1:
583eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D0;
584eddcd308SMatthias Ringwald             break;
585eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_S1:
586eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
587eddcd308SMatthias Ringwald             break;
588eddcd308SMatthias Ringwald         case HFP_LINK_SETTINGS_S2:
589eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S1;
590eddcd308SMatthias Ringwald             break;
5917522e673SMatthias Ringwald         case HFP_LINK_SETTINGS_S3:
5927522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S2;
5937522e673SMatthias Ringwald             break;
5947522e673SMatthias Ringwald         case HFP_LINK_SETTINGS_S4:
595eddcd308SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_S3;
596eddcd308SMatthias Ringwald             break;
5977522e673SMatthias Ringwald         case HFP_LINK_SETTINGS_T1:
5987522e673SMatthias Ringwald             log_info("T1 failed, fallback to CVSD - D1");
5997522e673SMatthias Ringwald             sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD;
600bc1b1537SMilanka Ringwald             sco_establishment_active->sco_for_msbc_failed = 1;
601bc1b1537SMilanka Ringwald             sco_establishment_active->command = HFP_CMD_AG_SEND_COMMON_CODEC;
6027522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1;
6037522e673SMatthias Ringwald             break;
6047522e673SMatthias Ringwald         case HFP_LINK_SETTINGS_T2:
6057522e673SMatthias Ringwald             sco_establishment_active->link_setting = HFP_LINK_SETTINGS_T1;
6067522e673SMatthias Ringwald             break;
607eddcd308SMatthias Ringwald     }
60838200c1dSMilanka Ringwald     log_info("e)SCO Connection: try new link_setting %d", sco_establishment_active->link_setting);
609eddcd308SMatthias Ringwald     sco_establishment_active->establish_audio_connection = 1;
61038200c1dSMilanka Ringwald     sco_establishment_active = NULL;
61138200c1dSMilanka Ringwald     return 1;
612eddcd308SMatthias Ringwald }
613eddcd308SMatthias Ringwald 
614eddcd308SMatthias Ringwald 
615405014fbSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
6165575558eSMilanka Ringwald     UNUSED(packet_type);
6178a46ec40SMatthias Ringwald     UNUSED(channel);    // ok: no channel
6185575558eSMilanka Ringwald     UNUSED(size);
6199ec2630cSMatthias Ringwald 
6203deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
62127950165SMatthias Ringwald     hci_con_handle_t handle;
622a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
623674515e8SMatthias Ringwald     uint8_t status;
6243deb3ec6SMatthias Ringwald 
62527950165SMatthias Ringwald     log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
626aa4dd815SMatthias Ringwald 
6270e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
6281b005648SMatthias Ringwald 
629011577abSMilanka Ringwald         case HCI_EVENT_CONNECTION_REQUEST:
6307522e673SMatthias Ringwald             switch(hci_event_connection_request_get_link_type(packet)){
6317522e673SMatthias Ringwald                 case 0: //  SCO
6327522e673SMatthias Ringwald                 case 2: // eSCO
633011577abSMilanka Ringwald                     hci_event_connection_request_get_bd_addr(packet, event_addr);
634405014fbSMatthias Ringwald                     hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
635011577abSMilanka Ringwald                     if (!hfp_connection) break;
636c169df2fSMatthias Ringwald                     if (hci_event_connection_request_get_link_type(packet) == 2){
637c169df2fSMatthias Ringwald                         hfp_connection->hf_accept_sco = 2;
638c169df2fSMatthias Ringwald                     } else {
639b72c4a9eSMatthias Ringwald                         hfp_connection->hf_accept_sco = 1;
640c169df2fSMatthias Ringwald                     }
641c169df2fSMatthias Ringwald                     log_info("hf accept sco %u\n", hfp_connection->hf_accept_sco);
6421b005648SMatthias Ringwald                     if (!hfp_hf_run_for_context) break;
6431b005648SMatthias Ringwald                     (*hfp_hf_run_for_context)(hfp_connection);
644202c8a4cSMatthias Ringwald                     break;
6457522e673SMatthias Ringwald                 default:
6467522e673SMatthias Ringwald                     break;
6477522e673SMatthias Ringwald             }
648011577abSMilanka Ringwald             break;
6493deb3ec6SMatthias Ringwald 
650eddcd308SMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
651eddcd308SMatthias Ringwald             if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) {
652be2a1a78SMatthias Ringwald                 status = hci_event_command_status_get_status(packet);
65338200c1dSMilanka Ringwald                 if (status == ERROR_CODE_SUCCESS) break;
65438200c1dSMilanka Ringwald 
65538200c1dSMilanka Ringwald                 hfp_connection = sco_establishment_active;
65638200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
65738200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
65838200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
65938200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
6606c5b2002SMatthias Ringwald             }
661eddcd308SMatthias Ringwald             break;
662eddcd308SMatthias Ringwald 
6633deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
664011577abSMilanka Ringwald             hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr);
665405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
666011577abSMilanka Ringwald             if (!hfp_connection) {
667011577abSMilanka Ringwald                 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr));
668011577abSMilanka Ringwald                 return;
669011577abSMilanka Ringwald             }
670ce263fc8SMatthias Ringwald 
671011577abSMilanka Ringwald             status = hci_event_synchronous_connection_complete_get_status(packet);
67238200c1dSMilanka Ringwald             if (status != ERROR_CODE_SUCCESS){
673b72c4a9eSMatthias Ringwald                 hfp_connection->hf_accept_sco = 0;
67438200c1dSMilanka Ringwald                 if (hfp_handle_failed_sco_connection(status)) break;
67538200c1dSMilanka Ringwald 
67638200c1dSMilanka Ringwald                 hfp_connection->establish_audio_connection = 0;
67738200c1dSMilanka Ringwald                 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
67838200c1dSMilanka Ringwald                 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec);
679aa4dd815SMatthias Ringwald                 break;
680aa4dd815SMatthias Ringwald             }
681aa4dd815SMatthias Ringwald 
682011577abSMilanka Ringwald             uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet);
683011577abSMilanka Ringwald             uint8_t  link_type = hci_event_synchronous_connection_complete_get_link_type(packet);
684011577abSMilanka Ringwald             uint8_t  transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet);  // measured in slots
685011577abSMilanka Ringwald             uint8_t  retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots
686011577abSMilanka Ringwald             uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes
687011577abSMilanka Ringwald             uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes
688011577abSMilanka Ringwald             uint8_t  air_mode = hci_event_synchronous_connection_complete_get_air_mode(packet);
6893deb3ec6SMatthias Ringwald 
6903deb3ec6SMatthias Ringwald             switch (link_type){
6913deb3ec6SMatthias Ringwald                 case 0x00:
692aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
6933deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
6943deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
6953deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
6963deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
6973deb3ec6SMatthias Ringwald                     break;
6983deb3ec6SMatthias Ringwald                 case 0x02:
699aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
7003deb3ec6SMatthias Ringwald                     break;
7013deb3ec6SMatthias Ringwald                 default:
7023deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
7033deb3ec6SMatthias Ringwald                     break;
7043deb3ec6SMatthias Ringwald             }
7053deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
706aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
707aa4dd815SMatthias Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
7083deb3ec6SMatthias Ringwald 
70938200c1dSMilanka Ringwald             // hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
7103deb3ec6SMatthias Ringwald 
71138200c1dSMilanka Ringwald             // if (!hfp_connection) {
71238200c1dSMilanka Ringwald             //     log_error("SCO link created, hfp_connection for address %s not found.", bd_addr_to_str(event_addr));
71338200c1dSMilanka Ringwald             //     break;
71438200c1dSMilanka Ringwald             // }
7153deb3ec6SMatthias Ringwald 
716d63c37a1SMatthias Ringwald             if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
717aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
718d63c37a1SMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_SCO;
719aa4dd815SMatthias Ringwald                 break;
720aa4dd815SMatthias Ringwald             }
721d63c37a1SMatthias Ringwald             hfp_connection->sco_handle = sco_handle;
722d63c37a1SMatthias Ringwald             hfp_connection->establish_audio_connection = 0;
723d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
72438200c1dSMilanka Ringwald             hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec);
7253deb3ec6SMatthias Ringwald             break;
7263deb3ec6SMatthias Ringwald         }
7273deb3ec6SMatthias Ringwald 
7283deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
729f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
730405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role);
731aa4dd815SMatthias Ringwald 
732d63c37a1SMatthias Ringwald             if (!hfp_connection) break;
733aa4dd815SMatthias Ringwald 
734d751f069SMatthias Ringwald             hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID;
735d63c37a1SMatthias Ringwald             hfp_connection->release_audio_connection = 0;
736d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
737ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
7380c3047fbSMatthias Ringwald 
7395f053052SMilanka Ringwald             if (hfp_connection->release_slc_connection){
7405f053052SMilanka Ringwald                 hfp_connection->release_slc_connection = 0;
7410c3047fbSMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
7420c3047fbSMatthias Ringwald                 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
7435f053052SMilanka Ringwald             }
7443deb3ec6SMatthias Ringwald             break;
7453deb3ec6SMatthias Ringwald 
746fc64f94aSMatthias Ringwald         default:
7473deb3ec6SMatthias Ringwald             break;
7483deb3ec6SMatthias Ringwald     }
7493deb3ec6SMatthias Ringwald }
7503deb3ec6SMatthias Ringwald 
75127950165SMatthias Ringwald void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){
7525575558eSMilanka Ringwald     UNUSED(packet_type);
75327950165SMatthias Ringwald     UNUSED(channel);    // ok: no channel
7545575558eSMilanka Ringwald     UNUSED(size);
75527950165SMatthias Ringwald 
75627950165SMatthias Ringwald     bd_addr_t event_addr;
75727950165SMatthias Ringwald     uint16_t rfcomm_cid;
75827950165SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
75927950165SMatthias Ringwald     uint8_t status;
76027950165SMatthias Ringwald 
76127950165SMatthias Ringwald     log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size);
76227950165SMatthias Ringwald 
76327950165SMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
76427950165SMatthias Ringwald 
76527950165SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
76627950165SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
76727950165SMatthias Ringwald             rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr);
76827950165SMatthias Ringwald             hfp_connection = provide_hfp_connection_context_for_bd_addr(event_addr, local_role);
76927950165SMatthias Ringwald             if (!hfp_connection){
77027950165SMatthias Ringwald                 log_info("hfp: no memory to accept incoming connection - decline");
77127950165SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
77227950165SMatthias Ringwald                 return;
77327950165SMatthias Ringwald             }
77427950165SMatthias Ringwald             if (hfp_connection->state != HFP_IDLE) {
7754960f9e7SMatthias Ringwald                 log_error("hfp: incoming connection but not idle, reject");
7764960f9e7SMatthias Ringwald                 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet));
77727950165SMatthias Ringwald                 return;
77827950165SMatthias Ringwald             }
77927950165SMatthias Ringwald 
78027950165SMatthias Ringwald             hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet);
78127950165SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
78227950165SMatthias Ringwald             // printf("RFCOMM channel %u requested for %s\n", hfp_connection->rfcomm_cid, bd_addr_to_str(hfp_connection->remote_addr));
78327950165SMatthias Ringwald             rfcomm_accept_connection(hfp_connection->rfcomm_cid);
78427950165SMatthias Ringwald             break;
78527950165SMatthias Ringwald 
78627950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_OPENED:
78727950165SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
78827950165SMatthias Ringwald 
78927950165SMatthias Ringwald             rfcomm_event_channel_opened_get_bd_addr(packet, event_addr);
79027950165SMatthias Ringwald             status = rfcomm_event_channel_opened_get_status(packet);
79127950165SMatthias Ringwald 
792405014fbSMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role);
793505f1c30SMatthias Ringwald             if (!hfp_connection || (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED)) return;
79427950165SMatthias Ringwald 
79527950165SMatthias Ringwald             if (status) {
79627950165SMatthias Ringwald                 hfp_emit_slc_connection_event(hfp_connection, status, rfcomm_event_channel_opened_get_con_handle(packet), event_addr);
79727950165SMatthias Ringwald                 remove_hfp_connection_context(hfp_connection);
79827950165SMatthias Ringwald             } else {
79927950165SMatthias Ringwald                 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet);
80027950165SMatthias Ringwald                 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
80127950165SMatthias Ringwald                 bd_addr_copy(hfp_connection->remote_addr, event_addr);
80227950165SMatthias Ringwald                 // uint16_t mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
80327950165SMatthias Ringwald                 // printf("RFCOMM channel open succeeded. hfp_connection %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", hfp_connection, hfp_connection->rfcomm_cid, mtu);
80427950165SMatthias Ringwald 
80527950165SMatthias Ringwald                 switch (hfp_connection->state){
80627950165SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
80727950165SMatthias Ringwald                         hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
80827950165SMatthias Ringwald                         break;
80927950165SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
81027950165SMatthias Ringwald                         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
81127950165SMatthias Ringwald                         // printf("Shutting down RFCOMM.\n");
81227950165SMatthias Ringwald                         break;
81327950165SMatthias Ringwald                     default:
81427950165SMatthias Ringwald                         break;
81527950165SMatthias Ringwald                 }
81627950165SMatthias Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
81727950165SMatthias Ringwald             }
81827950165SMatthias Ringwald             break;
81927950165SMatthias Ringwald 
82027950165SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
82127950165SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
82227950165SMatthias Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
82327950165SMatthias Ringwald             if (!hfp_connection) break;
82427950165SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
82527950165SMatthias Ringwald                 hfp_connection->state = HFP_IDLE;
82627950165SMatthias Ringwald                 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role);
82727950165SMatthias Ringwald                 break;
82827950165SMatthias Ringwald             }
82927950165SMatthias Ringwald 
83027950165SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
83127950165SMatthias Ringwald             remove_hfp_connection_context(hfp_connection);
83227950165SMatthias Ringwald             break;
83327950165SMatthias Ringwald 
83427950165SMatthias Ringwald         default:
83527950165SMatthias Ringwald             break;
83627950165SMatthias Ringwald     }
83727950165SMatthias Ringwald }
838aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
83994a27792SMatthias Ringwald 
84094a27792SMatthias Ringwald typedef struct {
84194a27792SMatthias Ringwald     const char * command;
84294a27792SMatthias Ringwald     hfp_command_t command_id;
84394a27792SMatthias Ringwald } hfp_command_entry_t;
84494a27792SMatthias Ringwald 
84594a27792SMatthias Ringwald static hfp_command_entry_t hfp_ag_commmand_table[] = {
846cb33905eSMatthias Ringwald     { "AT+BAC=",   HFP_CMD_AVAILABLE_CODECS },
847cb33905eSMatthias Ringwald     { "AT+BCC",    HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP },
848cb33905eSMatthias Ringwald     { "AT+BCS=",   HFP_CMD_HF_CONFIRMED_CODEC },
849cb33905eSMatthias Ringwald     { "AT+BIA=",   HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled>
850cb33905eSMatthias Ringwald     { "AT+BIEV=",  HFP_CMD_HF_INDICATOR_STATUS },
85194a27792SMatthias Ringwald     { "AT+BIND=",  HFP_CMD_LIST_GENERIC_STATUS_INDICATORS },
85294a27792SMatthias Ringwald     { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS },
85394a27792SMatthias Ringwald     { "AT+BIND?",  HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE },
854cb33905eSMatthias Ringwald     { "AT+BINP",   HFP_CMD_HF_REQUEST_PHONE_NUMBER },
855cb33905eSMatthias Ringwald     { "AT+BLDN",   HFP_CMD_REDIAL_LAST_NUMBER },
856cb33905eSMatthias Ringwald     { "AT+BRSF=",  HFP_CMD_SUPPORTED_FEATURES },
85794a27792SMatthias Ringwald     { "AT+BTRH=",  HFP_CMD_RESPONSE_AND_HOLD_COMMAND },
85894a27792SMatthias Ringwald     { "AT+BTRH?",  HFP_CMD_RESPONSE_AND_HOLD_QUERY },
859cb33905eSMatthias Ringwald     { "AT+BVRA=",  HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
860cb33905eSMatthias Ringwald     { "AT+CCWA=",  HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION},
86194a27792SMatthias Ringwald     { "AT+CHLD=",  HFP_CMD_CALL_HOLD },
86294a27792SMatthias Ringwald     { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
863cb33905eSMatthias Ringwald     { "AT+CHUP",   HFP_CMD_HANG_UP_CALL },
86494a27792SMatthias Ringwald     { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS },
86594a27792SMatthias Ringwald     { "AT+CIND?",  HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS },
866cb33905eSMatthias Ringwald     { "AT+CLCC",   HFP_CMD_LIST_CURRENT_CALLS },
867cb33905eSMatthias Ringwald     { "AT+CLIP=",  HFP_CMD_ENABLE_CLIP},
868cb33905eSMatthias Ringwald     { "AT+CMEE=",  HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR},
869cb33905eSMatthias Ringwald     { "AT+CMER=",  HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE },
870cb33905eSMatthias Ringwald     { "AT+CNUM",   HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION },
87194a27792SMatthias Ringwald     { "AT+COPS=",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT },
87294a27792SMatthias Ringwald     { "AT+COPS?",  HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
873cb33905eSMatthias Ringwald     { "AT+NREC=",  HFP_CMD_TURN_OFF_EC_AND_NR, },
874cb33905eSMatthias Ringwald     { "AT+VGM=",   HFP_CMD_SET_MICROPHONE_GAIN },
875cb33905eSMatthias Ringwald     { "AT+VGS=",   HFP_CMD_SET_SPEAKER_GAIN },
876cb33905eSMatthias Ringwald     { "AT+VTS:",   HFP_CMD_TRANSMIT_DTMF_CODES },
87794a27792SMatthias Ringwald     { "ATA",       HFP_CMD_CALL_ANSWERED },
87894a27792SMatthias Ringwald };
87994a27792SMatthias Ringwald 
88094a27792SMatthias Ringwald static hfp_command_entry_t hfp_hf_commmand_table[] = {
881cb33905eSMatthias Ringwald     { "+BCS:",  HFP_CMD_AG_SUGGESTED_CODEC },
88294a27792SMatthias Ringwald     { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS },
883cb33905eSMatthias Ringwald     { "+BINP",  HFP_CMD_AG_SENT_PHONE_NUMBER },
884cb33905eSMatthias Ringwald     { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES },
885cb33905eSMatthias Ringwald     { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING },
88694a27792SMatthias Ringwald     { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS },
887cb33905eSMatthias Ringwald     { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION },
888cb33905eSMatthias Ringwald     { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, },
88994a27792SMatthias Ringwald     { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES },
890cb33905eSMatthias Ringwald     { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS},
89184a0c24eSMatthias Ringwald     { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC },
892cb33905eSMatthias Ringwald     { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS },
893cb33905eSMatthias Ringwald     { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION },
894cb33905eSMatthias Ringwald     { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR },
895cb33905eSMatthias Ringwald     { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION},
896cb33905eSMatthias Ringwald     { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME },
897cb33905eSMatthias Ringwald     { "+VGM:",  HFP_CMD_SET_MICROPHONE_GAIN },
898cb33905eSMatthias Ringwald     { "+VGS:",  HFP_CMD_SET_SPEAKER_GAIN},
899cb33905eSMatthias Ringwald     { "ERROR",  HFP_CMD_ERROR},
900cb33905eSMatthias Ringwald     { "NOP",    HFP_CMD_NONE}, // dummy command used by unit tests
901cb33905eSMatthias Ringwald     { "OK",     HFP_CMD_OK },
902cb33905eSMatthias Ringwald     { "RING",   HFP_CMD_RING },
90394a27792SMatthias Ringwald };
90494a27792SMatthias Ringwald 
905aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
9063deb3ec6SMatthias Ringwald 
907cb33905eSMatthias Ringwald     // table lookup based on role
90894a27792SMatthias Ringwald     uint16_t i;
90994a27792SMatthias Ringwald     uint16_t num_entries;
91094a27792SMatthias Ringwald     hfp_command_entry_t * table;
91194a27792SMatthias Ringwald     if (isHandsFree == 0){
91294a27792SMatthias Ringwald         table = hfp_ag_commmand_table;
91394a27792SMatthias Ringwald         num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t);
9143deb3ec6SMatthias Ringwald     } else {
91594a27792SMatthias Ringwald         table = hfp_hf_commmand_table;
91694a27792SMatthias Ringwald         num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t);
91794a27792SMatthias Ringwald     }
91894a27792SMatthias Ringwald     for (i=0;i<num_entries;i++) {
91994a27792SMatthias Ringwald         hfp_command_entry_t *entry = &table[i];
92094a27792SMatthias Ringwald         int match = strcmp(line_buffer, entry->command);
92194a27792SMatthias Ringwald         if (match == 0){
92294a27792SMatthias Ringwald             return entry->command_id;
92394a27792SMatthias Ringwald         }
92494a27792SMatthias Ringwald     }
92594a27792SMatthias Ringwald 
926cb33905eSMatthias Ringwald     // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work.
927*0222a807SMatthias Ringwald     // note: phone number is currently expected in line_buffer[3..]
928cb33905eSMatthias Ringwald     // prefix match on 'ATD', AG only
929cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){
930cb33905eSMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
9313deb3ec6SMatthias Ringwald     }
9323deb3ec6SMatthias Ringwald 
933cb33905eSMatthias Ringwald     // Valid looking, but unknown commands/responses
934cb33905eSMatthias Ringwald     if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){
935aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
9363deb3ec6SMatthias Ringwald     }
9373deb3ec6SMatthias Ringwald 
938cb33905eSMatthias Ringwald     if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){
939aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
940aa4dd815SMatthias Ringwald     }
9413deb3ec6SMatthias Ringwald 
942aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
9433deb3ec6SMatthias Ringwald }
9443deb3ec6SMatthias Ringwald 
945a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
94662c7768eSMatthias Ringwald     if ((hfp_connection->line_size + 1 ) >= HFP_MAX_INDICATOR_DESC_SIZE) return;
947a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
948a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
9493deb3ec6SMatthias Ringwald }
950a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
951a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
9523deb3ec6SMatthias Ringwald }
9533deb3ec6SMatthias Ringwald 
9543deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
955c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
9563deb3ec6SMatthias Ringwald }
9573deb3ec6SMatthias Ringwald 
95894a27792SMatthias Ringwald static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) {
959a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
9603deb3ec6SMatthias Ringwald }
9611d81c7feSMatthias Ringwald 
9621d81c7feSMatthias Ringwald static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){
9631d81c7feSMatthias Ringwald     switch (byte){
9641d81c7feSMatthias Ringwald         case ',':
9651d81c7feSMatthias Ringwald         case ';':
9661d81c7feSMatthias Ringwald         case '(':
9671d81c7feSMatthias Ringwald         case ')':
9681d81c7feSMatthias Ringwald         case '\n':
9691d81c7feSMatthias Ringwald         case '\r':
9703deb3ec6SMatthias Ringwald             break;
9713deb3ec6SMatthias Ringwald         default:
9721d81c7feSMatthias Ringwald             hfp_parser_store_byte(hfp_connection, byte);
9733deb3ec6SMatthias Ringwald             break;
9743deb3ec6SMatthias Ringwald     }
9753deb3ec6SMatthias Ringwald }
9761d81c7feSMatthias Ringwald 
9771d81c7feSMatthias Ringwald static bool hfp_parser_is_separator( uint8_t byte){
9781d81c7feSMatthias Ringwald     switch (byte){
9791d81c7feSMatthias Ringwald         case ',':
9801d81c7feSMatthias Ringwald         case ';':
9811d81c7feSMatthias Ringwald         case '\n':
9821d81c7feSMatthias Ringwald         case '\r':
9831d81c7feSMatthias Ringwald             return true;
984dd533208SMatthias Ringwald         default:
9851d81c7feSMatthias Ringwald             return false;
9863deb3ec6SMatthias Ringwald     }
9873deb3ec6SMatthias Ringwald }
9883deb3ec6SMatthias Ringwald 
989d6b237acSMatthias Ringwald static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
990aa4dd815SMatthias Ringwald 
9911dddc4f4SMatthias Ringwald     // handle doubles quotes
9921dddc4f4SMatthias Ringwald     if (byte == '"'){
9931dddc4f4SMatthias Ringwald         hfp_connection->parser_quoted = !hfp_connection->parser_quoted;
994d6b237acSMatthias Ringwald         return true;
9951dddc4f4SMatthias Ringwald     }
9961dddc4f4SMatthias Ringwald     if (hfp_connection->parser_quoted) {
9971dddc4f4SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
998d6b237acSMatthias Ringwald         return true;
9991dddc4f4SMatthias Ringwald     }
10003deb3ec6SMatthias Ringwald 
10011dddc4f4SMatthias Ringwald     // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command
1002d6b237acSMatthias Ringwald     if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true;
10031dddc4f4SMatthias Ringwald 
1004bfeaf344SMatthias Ringwald     bool processed = true;
1005bfeaf344SMatthias Ringwald 
1006a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state) {
1007dd533208SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
1008d28b2d5dSMilanka Ringwald             switch (byte) {
1009dd533208SMatthias Ringwald                 case '\n':
1010dd533208SMatthias Ringwald                 case '\r':
1011dd533208SMatthias Ringwald                 case ';':
1012bfeaf344SMatthias Ringwald                     // ignore separator
1013bfeaf344SMatthias Ringwald                     break;
1014bfeaf344SMatthias Ringwald                 case ':':
1015bfeaf344SMatthias Ringwald                 case '?':
1016bfeaf344SMatthias Ringwald                     // store separator
1017bfeaf344SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1018dd533208SMatthias Ringwald                     break;
1019d28b2d5dSMilanka Ringwald                 case '=':
1020bfeaf344SMatthias Ringwald                     // equal sign: remember and wait for next char to decided between '=?' and '=\?'
1021dd533208SMatthias Ringwald                     hfp_connection->found_equal_sign = true;
1022a0ffb263SMatthias Ringwald                     hfp_parser_store_byte(hfp_connection, byte);
1023d6b237acSMatthias Ringwald                     return true;
1024d28b2d5dSMilanka Ringwald                 default:
1025bfeaf344SMatthias Ringwald                     // store if not lookahead
1026d7f16ff1SMatthias Ringwald                     if (!hfp_connection->found_equal_sign) {
1027dd533208SMatthias Ringwald                         hfp_parser_store_byte(hfp_connection, byte);
1028d6b237acSMatthias Ringwald                         return true;
1029dd533208SMatthias Ringwald                     }
1030bfeaf344SMatthias Ringwald                     // mark as lookahead
1031bfeaf344SMatthias Ringwald                     processed = false;
1032d7f16ff1SMatthias Ringwald                     break;
1033d7f16ff1SMatthias Ringwald             }
1034ce263fc8SMatthias Ringwald 
10351d81c7feSMatthias Ringwald             // ignore empty tokens
1036d6b237acSMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection)) return true;
1037dd533208SMatthias Ringwald 
1038bfeaf344SMatthias Ringwald             // parse
1039dd533208SMatthias Ringwald             hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree);
1040aa4dd815SMatthias Ringwald 
104184a0c24eSMatthias Ringwald             // pick +CIND version based on connection state: descriptions during SLC vs. states later
104284a0c24eSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){
1043a0ffb263SMatthias Ringwald                 switch(hfp_connection->state){
1044aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1045a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
1046aa4dd815SMatthias Ringwald                         break;
1047aa4dd815SMatthias Ringwald                     case HFP_W4_RETRIEVE_INDICATORS:
1048a0ffb263SMatthias Ringwald                         hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
1049aa4dd815SMatthias Ringwald                         break;
1050aa4dd815SMatthias Ringwald                     default:
105184a0c24eSMatthias Ringwald                         btstack_assert(false);
105284a0c24eSMatthias Ringwald                         hfp_connection->command = HFP_CMD_UNKNOWN;
1053aa4dd815SMatthias Ringwald                         break;
1054aa4dd815SMatthias Ringwald                 }
1055aa4dd815SMatthias Ringwald             }
10563deb3ec6SMatthias Ringwald 
1057c0c0437aSMatthias Ringwald             log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command);
1058c0c0437aSMatthias Ringwald 
1059bfeaf344SMatthias Ringwald             // next state
1060bfeaf344SMatthias Ringwald             hfp_connection->found_equal_sign = false;
10611d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1062dd533208SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
1063dd533208SMatthias Ringwald 
1064bfeaf344SMatthias Ringwald             return processed;
1065dd533208SMatthias Ringwald 
1066bfeaf344SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
10671d81c7feSMatthias Ringwald             // handle empty fields
10681d81c7feSMatthias Ringwald             if ((byte == ',' ) && (hfp_connection->line_size == 0)){
1069dd533208SMatthias Ringwald                 hfp_connection->line_buffer[0] = 0;
1070dd533208SMatthias Ringwald                 hfp_connection->ignore_value = 1;
1071dd533208SMatthias Ringwald                 parse_sequence(hfp_connection);
10723d051a53SMatthias Ringwald                 return true;
10733d051a53SMatthias Ringwald             }
10743d051a53SMatthias Ringwald 
10751d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
10761d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
10771d81c7feSMatthias Ringwald 
10781d81c7feSMatthias Ringwald             // ignore empty tokens
10793d051a53SMatthias Ringwald             if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) return true;
10803d051a53SMatthias Ringwald 
10813d051a53SMatthias Ringwald             parse_sequence(hfp_connection);
10821d81c7feSMatthias Ringwald 
10831d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
10841d81c7feSMatthias Ringwald 
10851d81c7feSMatthias Ringwald             switch (hfp_connection->command){
10861d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
10871d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
10881d81c7feSMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
10891d81c7feSMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
10901d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
10911d81c7feSMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
10921d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
10931d81c7feSMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
10941d81c7feSMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
10951d81c7feSMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
10961d81c7feSMatthias Ringwald                     break;
10971d81c7feSMatthias Ringwald                 default:
10981d81c7feSMatthias Ringwald                     break;
10991d81c7feSMatthias Ringwald             }
11003d051a53SMatthias Ringwald             return true;
11013d051a53SMatthias Ringwald 
1102ba0de059SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
11031d81c7feSMatthias Ringwald 
11041d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11051d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
1106dd533208SMatthias Ringwald 
1107a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1108ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1109a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
11102308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1111ce263fc8SMatthias Ringwald                     break;
1112ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1113a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
11142308e108SMilanka Ringwald                     hfp_connection->network_operator.format =  btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1115ce263fc8SMatthias Ringwald                     break;
1116ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1117ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1118ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
11192308e108SMilanka Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1120ce263fc8SMatthias Ringwald                     break;
1121ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11222308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1123a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1124a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1125ce263fc8SMatthias Ringwald                     break;
1126ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
11272308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer);
1128a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1129ce263fc8SMatthias Ringwald                     break;
1130ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1131a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1132a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
11332308e108SMilanka Ringwald                     hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1134ce263fc8SMatthias Ringwald                     break;
1135*0222a807SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
1136*0222a807SMatthias Ringwald                     hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1137*0222a807SMatthias Ringwald                     break;
1138ce263fc8SMatthias Ringwald                 default:
1139ce263fc8SMatthias Ringwald                     break;
1140ce263fc8SMatthias Ringwald             }
11411d81c7feSMatthias Ringwald 
11421d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
11431d81c7feSMatthias Ringwald 
11441d81c7feSMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
11451d81c7feSMatthias Ringwald 
1146ba0de059SMatthias Ringwald             return true;
1147ce263fc8SMatthias Ringwald 
114874c7548aSMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
11491d81c7feSMatthias Ringwald 
11501d81c7feSMatthias Ringwald             hfp_parser_store_if_token(hfp_connection, byte);
11511d81c7feSMatthias Ringwald             if (!hfp_parser_is_separator(byte)) return true;
11521d81c7feSMatthias Ringwald 
1153a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
1154ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
115589425bfcSMilanka Ringwald                     strncpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
115689425bfcSMilanka Ringwald                     hfp_connection->network_operator.name[HFP_MAX_NETWORK_OPERATOR_NAME_SIZE - 1] = 0;
1157a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1158ce263fc8SMatthias Ringwald                     break;
1159ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
11602308e108SMilanka Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer);
1161a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index++;
1162c741b032SMilanka Ringwald                     hfp_connection->ag_indicators_nr++;
1163a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1164ce263fc8SMatthias Ringwald                     break;
1165ce263fc8SMatthias Ringwald                 default:
1166ce263fc8SMatthias Ringwald                     break;
1167ce263fc8SMatthias Ringwald             }
11681d81c7feSMatthias Ringwald 
11691d81c7feSMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
11701d81c7feSMatthias Ringwald 
11711d81c7feSMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
11721d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
11731d81c7feSMatthias Ringwald             } else {
11741d81c7feSMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
11751d81c7feSMatthias Ringwald             }
1176d6b237acSMatthias Ringwald             return true;
117774c7548aSMatthias Ringwald 
117874c7548aSMatthias Ringwald         default:
117974c7548aSMatthias Ringwald             btstack_assert(false);
118074c7548aSMatthias Ringwald             return true;
118174c7548aSMatthias Ringwald     }
1182d6b237acSMatthias Ringwald }
1183d6b237acSMatthias Ringwald 
1184d6b237acSMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
1185d6b237acSMatthias Ringwald     bool processed = false;
1186d6b237acSMatthias Ringwald     while (!processed){
1187d6b237acSMatthias Ringwald         processed = hfp_parse_byte(hfp_connection, byte, isHandsFree);
1188d6b237acSMatthias Ringwald     }
11891d81c7feSMatthias Ringwald     // reset parser state on end-of-line
11901d81c7feSMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
11911d81c7feSMatthias Ringwald         hfp_connection->parser_item_index = 0;
11921d81c7feSMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
11931d81c7feSMatthias Ringwald     }
1194ce263fc8SMatthias Ringwald }
1195ce263fc8SMatthias Ringwald 
1196a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1197ce263fc8SMatthias Ringwald     int value;
1198a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1199667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
12002308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1201667ec068SMatthias Ringwald             int i;
1202a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1203667ec068SMatthias Ringwald                 case 0:
1204a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1205a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1206a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1207667ec068SMatthias Ringwald                             break;
1208667ec068SMatthias Ringwald                         }
1209667ec068SMatthias Ringwald                     }
1210667ec068SMatthias Ringwald                     break;
1211667ec068SMatthias Ringwald                 case 1:
1212a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1213a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1214667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1215a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1216667ec068SMatthias Ringwald                     break;
1217667ec068SMatthias Ringwald                 default:
1218667ec068SMatthias Ringwald                     break;
1219667ec068SMatthias Ringwald             }
1220a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1221667ec068SMatthias Ringwald             break;
1222667ec068SMatthias Ringwald 
1223667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1224a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1225667ec068SMatthias Ringwald                 case 0:
1226a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1227a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1228667ec068SMatthias Ringwald                     break;
1229667ec068SMatthias Ringwald                 case 1:
12302308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1231a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1232667ec068SMatthias Ringwald                     break;
1233667ec068SMatthias Ringwald                 default:
1234667ec068SMatthias Ringwald                     break;
1235667ec068SMatthias Ringwald             }
1236a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1237667ec068SMatthias Ringwald             break;
1238667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1239a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1240667ec068SMatthias Ringwald                 case 0:
12412308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1242a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1243667ec068SMatthias Ringwald                     break;
1244667ec068SMatthias Ringwald                 case 1:
12452308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1246a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1247667ec068SMatthias Ringwald                     break;
1248667ec068SMatthias Ringwald                 case 2:
12492308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1250a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1251667ec068SMatthias Ringwald                     break;
1252667ec068SMatthias Ringwald                 case 3:
12532308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
12540aee97efSMilanka Ringwald                     hfp_connection->clcc_mode = value;
1255667ec068SMatthias Ringwald                     break;
1256667ec068SMatthias Ringwald                 case 4:
12570aee97efSMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
12580aee97efSMilanka Ringwald                     hfp_connection->clcc_mpty = value;
12590aee97efSMilanka Ringwald                     break;
12600aee97efSMilanka Ringwald                 case 5:
1261a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1262a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1263667ec068SMatthias Ringwald                     break;
12640aee97efSMilanka Ringwald                 case 6:
12652308e108SMilanka Ringwald                     value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1266a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1267667ec068SMatthias Ringwald                     break;
1268667ec068SMatthias Ringwald                 default:
1269667ec068SMatthias Ringwald                     break;
1270667ec068SMatthias Ringwald             }
1271a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1272667ec068SMatthias Ringwald             break;
1273aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
12742308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1275a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1276aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1277aa4dd815SMatthias Ringwald             break;
1278aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
12792308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1280a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1281aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1282aa4dd815SMatthias Ringwald             break;
1283aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
12842308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1285a0ffb263SMatthias Ringwald             hfp_connection->ag_activate_voice_recognition = value;
1286aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1287aa4dd815SMatthias Ringwald             break;
1288aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
12892308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1290a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1291aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1292aa4dd815SMatthias Ringwald             break;
1293aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
12942308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1295a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1296aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1297aa4dd815SMatthias Ringwald             break;
12983deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
12992308e108SMilanka Ringwald             hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer);
1300a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
13013deb3ec6SMatthias Ringwald             break;
13023deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
13032308e108SMilanka Ringwald             hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer);
1304a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
13053deb3ec6SMatthias Ringwald             break;
13063deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
13072308e108SMilanka Ringwald             hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer);
1308bace42efSMatthias Ringwald             log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features);
13093deb3ec6SMatthias Ringwald             break;
13103deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1311a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
13122308e108SMilanka Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
1313a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1314a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
13153deb3ec6SMatthias Ringwald             break;
1316aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
131789425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer, HFP_MAX_INDICATOR_DESC_SIZE);
131889425bfcSMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].name[HFP_MAX_INDICATOR_DESC_SIZE-1] = 0;
1319a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1320a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1321aa4dd815SMatthias Ringwald             break;
1322aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1323a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
13242308e108SMilanka Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer);
1325a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
13263deb3ec6SMatthias Ringwald             break;
13273deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1328a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1329a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1330a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
13312308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1332a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
13333deb3ec6SMatthias Ringwald             break;
13343deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1335a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1336a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
133789425bfcSMilanka Ringwald             strncpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE);
133889425bfcSMilanka Ringwald             hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name[HFP_CALL_SERVICE_SIZE - 1] = 0;
1339a0ffb263SMatthias Ringwald             hfp_connection->remote_call_services_nr++;
13403deb3ec6SMatthias Ringwald             break;
1341aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1342aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1343a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
13442308e108SMilanka Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer);
1345a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1346a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
13473deb3ec6SMatthias Ringwald             break;
1348aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
13493deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1350a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
13512308e108SMilanka Ringwald             hfp_connection->parser_item_index = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
13523deb3ec6SMatthias Ringwald             break;
1353ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
13542308e108SMilanka Ringwald             hfp_connection->parser_indicator_index = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1355a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1356ce263fc8SMatthias Ringwald             break;
13573deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
13583deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1359a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1360a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1361a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1362a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1363ce263fc8SMatthias Ringwald             }
1364a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1365ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1366a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1367ce263fc8SMatthias Ringwald             } else {
13682308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1369a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1370a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1371a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
13723deb3ec6SMatthias Ringwald             }
1373a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
13743deb3ec6SMatthias Ringwald             break;
13753deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13763deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
13772308e108SMilanka Ringwald             hfp_connection->parser_item_index = btstack_atoi((char *)&hfp_connection->line_buffer[0]) - 1;
1378a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
13793deb3ec6SMatthias Ringwald             break;
1380aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
13812308e108SMilanka Ringwald             hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1382a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1383aa4dd815SMatthias Ringwald             break;
1384aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1385a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1386a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
13873deb3ec6SMatthias Ringwald                 break;
13883deb3ec6SMatthias Ringwald             }
13893deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1390a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
13913deb3ec6SMatthias Ringwald             break;
13923deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
13933deb3ec6SMatthias Ringwald             break;
13943deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1395a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
13962308e108SMilanka Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
13973deb3ec6SMatthias Ringwald             break;
13983deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
13992308e108SMilanka Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer);
1400a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1401a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
14023deb3ec6SMatthias Ringwald             break;
1403ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1404a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1405a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1406a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1407a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
14083deb3ec6SMatthias Ringwald             break;
1409*0222a807SMatthias Ringwald         case HFP_CMD_CALL_HOLD:
1410*0222a807SMatthias Ringwald             hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0';
1411*0222a807SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
1412*0222a807SMatthias Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1413*0222a807SMatthias Ringwald             }
1414*0222a807SMatthias Ringwald             break;
1415*0222a807SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1416*0222a807SMatthias Ringwald             hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1417*0222a807SMatthias Ringwald             break;
1418*0222a807SMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1419*0222a807SMatthias Ringwald             hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0];
1420*0222a807SMatthias Ringwald             break;
1421*0222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1422*0222a807SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0';
1423*0222a807SMatthias Ringwald             break;
1424*0222a807SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1425*0222a807SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0';
1426*0222a807SMatthias Ringwald             break;
14273deb3ec6SMatthias Ringwald         default:
14283deb3ec6SMatthias Ringwald             break;
14293deb3ec6SMatthias Ringwald     }
14303deb3ec6SMatthias Ringwald }
14313deb3ec6SMatthias Ringwald 
1432323d3000SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){
1433323d3000SMatthias Ringwald     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr, local_role);
1434a0ffb263SMatthias Ringwald     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
14353deb3ec6SMatthias Ringwald 
1436a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14373deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
14383deb3ec6SMatthias Ringwald         return;
14393deb3ec6SMatthias Ringwald     }
1440a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
14413deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1442a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
14433deb3ec6SMatthias Ringwald             return;
14443deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
1445a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
14463deb3ec6SMatthias Ringwald             return;
14473deb3ec6SMatthias Ringwald         case HFP_IDLE:
14486535961aSMatthias Ringwald             (void)memcpy(hfp_connection->remote_addr, bd_addr, 6);
1449d63c37a1SMatthias Ringwald             hfp_connection->state = HFP_W4_SDP_QUERY_COMPLETE;
1450a0ffb263SMatthias Ringwald             connection_doing_sdp_query = hfp_connection;
1451a0ffb263SMatthias Ringwald             hfp_connection->service_uuid = service_uuid;
14527fbb5f59SMatthias Ringwald             sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid);
14533deb3ec6SMatthias Ringwald             break;
14543deb3ec6SMatthias Ringwald         default:
14553deb3ec6SMatthias Ringwald             break;
14563deb3ec6SMatthias Ringwald     }
14573deb3ec6SMatthias Ringwald }
14583deb3ec6SMatthias Ringwald 
1459a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1460a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1461a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
14623deb3ec6SMatthias Ringwald 
1463a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1464a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_IDLE;
14653deb3ec6SMatthias Ringwald         return;
14663deb3ec6SMatthias Ringwald     }
14673deb3ec6SMatthias Ringwald 
1468a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1469a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
14703deb3ec6SMatthias Ringwald         return;
14713deb3ec6SMatthias Ringwald     }
14723deb3ec6SMatthias Ringwald 
14730c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
14740c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
14750c3047fbSMatthias Ringwald         return;
14760c3047fbSMatthias Ringwald     }
14770c3047fbSMatthias Ringwald 
14780c3047fbSMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
14790c3047fbSMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
14800c3047fbSMatthias Ringwald         return;
14810c3047fbSMatthias Ringwald     }
14820c3047fbSMatthias Ringwald 
14830c3047fbSMatthias Ringwald     // HFP_W4_SCO_DISCONNECTED or later
14845f053052SMilanka Ringwald     hfp_connection->release_slc_connection = 1;
14853deb3ec6SMatthias Ringwald }
14863deb3ec6SMatthias Ringwald 
1487a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1488a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1489a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1490a0ffb263SMatthias Ringwald     hfp_connection->release_audio_connection = 1;
14913deb3ec6SMatthias Ringwald }
14923deb3ec6SMatthias Ringwald 
1493ce263fc8SMatthias Ringwald static const struct link_settings {
1494ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1495ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1496ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1497ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1498ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0,   HV1
1499ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1,   HV3
1500ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1,   EV3
1501ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1502ce263fc8SMatthias Ringwald     { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1503ce263fc8SMatthias Ringwald     { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1504ce263fc8SMatthias Ringwald     { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1,   EV3
1505ce263fc8SMatthias Ringwald     { 0x000d, 0x02, 0x0380 }  // HFP_LINK_SETTINGS_T2, 2-EV3
1506ce263fc8SMatthias Ringwald };
15073deb3ec6SMatthias Ringwald 
1508eddcd308SMatthias Ringwald void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){
1509ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1510eddcd308SMatthias Ringwald     int setting = hfp_connection->link_setting;
1511ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1512eddcd308SMatthias Ringwald     sco_establishment_active = hfp_connection;
1513d2c1d59aSMatthias Ringwald     uint16_t sco_voice_setting = hci_get_sco_voice_setting();
1514d2c1d59aSMatthias Ringwald     if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
15157522e673SMatthias Ringwald         sco_voice_setting = 0x0043; // Transparent data
1516d2c1d59aSMatthias Ringwald     }
1517eddcd308SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1518d2c1d59aSMatthias Ringwald         sco_voice_setting, hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1519ce263fc8SMatthias Ringwald }
1520d68dcce1SMatthias Ringwald 
15211b005648SMatthias Ringwald void hfp_set_hf_callback(btstack_packet_handler_t callback){
15221b005648SMatthias Ringwald     hfp_hf_callback = callback;
15231b005648SMatthias Ringwald }
15241b005648SMatthias Ringwald 
15251b005648SMatthias Ringwald void hfp_set_ag_callback(btstack_packet_handler_t callback){
15261b005648SMatthias Ringwald     hfp_ag_callback = callback;
15271b005648SMatthias Ringwald }
15281b005648SMatthias Ringwald 
1529ca59be51SMatthias Ringwald void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){
1530ca59be51SMatthias Ringwald     hfp_ag_rfcomm_packet_handler = handler;
1531ca59be51SMatthias Ringwald }
15321b005648SMatthias Ringwald 
1533ca59be51SMatthias Ringwald void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){
1534ca59be51SMatthias Ringwald     hfp_hf_rfcomm_packet_handler = handler;
1535d68dcce1SMatthias Ringwald }
1536d68dcce1SMatthias Ringwald 
15371b005648SMatthias Ringwald void hfp_set_hf_run_for_context(void (*callback)(hfp_connection_t * hfp_connection)){
15381b005648SMatthias Ringwald     hfp_hf_run_for_context = callback;
15391b005648SMatthias Ringwald }
15401b005648SMatthias Ringwald 
1541520c92d5SMatthias Ringwald void hfp_init(void){
1542520c92d5SMatthias Ringwald }
1543186dd3d2SMatthias Ringwald 
1544afac2a04SMilanka Ringwald void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t esco_s4_supported){
1545afac2a04SMilanka Ringwald     // determine highest possible link setting
1546afac2a04SMilanka Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
1547afac2a04SMilanka Ringwald     // anything else requires eSCO support on both sides
1548afac2a04SMilanka Ringwald     if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
1549afac2a04SMilanka Ringwald         switch (hfp_connection->negotiated_codec){
1550afac2a04SMilanka Ringwald             case HFP_CODEC_CVSD:
1551afac2a04SMilanka Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
1552afac2a04SMilanka Ringwald                 if (esco_s4_supported){
1553afac2a04SMilanka Ringwald                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
1554afac2a04SMilanka Ringwald                 }
1555afac2a04SMilanka Ringwald                 break;
1556afac2a04SMilanka Ringwald             case HFP_CODEC_MSBC:
1557afac2a04SMilanka Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
1558afac2a04SMilanka Ringwald                 break;
1559afac2a04SMilanka Ringwald             default:
1560afac2a04SMilanka Ringwald                 break;
1561afac2a04SMilanka Ringwald         }
1562afac2a04SMilanka Ringwald     }
1563afac2a04SMilanka Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
1564afac2a04SMilanka Ringwald }
1565afac2a04SMilanka Ringwald 
1566186dd3d2SMatthias Ringwald #define HFP_HF_RX_DEBUG_PRINT_LINE 80
1567186dd3d2SMatthias Ringwald 
1568186dd3d2SMatthias Ringwald void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){
1569186dd3d2SMatthias Ringwald #ifdef ENABLE_LOG_INFO
1570186dd3d2SMatthias Ringwald     // encode \n\r
1571186dd3d2SMatthias Ringwald     char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2];
157215a27967SMatthias Ringwald     int i = 0;
1573186dd3d2SMatthias Ringwald     int pos;
157415a27967SMatthias Ringwald     for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){
1575186dd3d2SMatthias Ringwald         switch (packet[pos]){
1576186dd3d2SMatthias Ringwald             case '\n':
1577186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1578186dd3d2SMatthias Ringwald                 printable[i++] = 'n';
1579186dd3d2SMatthias Ringwald                 break;
1580186dd3d2SMatthias Ringwald             case '\r':
1581186dd3d2SMatthias Ringwald                 printable[i++] = '\\';
1582186dd3d2SMatthias Ringwald                 printable[i++] = 'r';
1583186dd3d2SMatthias Ringwald                 break;
1584186dd3d2SMatthias Ringwald             default:
1585186dd3d2SMatthias Ringwald                 printable[i++] = packet[pos];
1586186dd3d2SMatthias Ringwald                 break;
1587186dd3d2SMatthias Ringwald         }
1588186dd3d2SMatthias Ringwald     }
1589186dd3d2SMatthias Ringwald     printable[i] = 0;
1590186dd3d2SMatthias Ringwald     log_info("%s: '%s'", tag, printable);
1591186dd3d2SMatthias Ringwald #endif
1592186dd3d2SMatthias Ringwald }
1593