xref: /btstack/src/classic/hfp.c (revision a0ffb263e02c613f9155d5035f68cb0ad7b80d74)
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  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald // *****************************************************************************
393deb3ec6SMatthias Ringwald //
403deb3ec6SMatthias Ringwald // Minimal setup for HFP Audio Gateway (AG) unit (!! UNDER DEVELOPMENT !!)
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald #include <inttypes.h>
513deb3ec6SMatthias Ringwald 
5256042629SMatthias Ringwald #include "hci_cmd.h"
5382636622SMatthias Ringwald #include "btstack_run_loop.h"
543deb3ec6SMatthias Ringwald 
553deb3ec6SMatthias Ringwald #include "hci.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
573deb3ec6SMatthias Ringwald #include "hci_dump.h"
583deb3ec6SMatthias Ringwald #include "l2cap.h"
59023f2764SMatthias Ringwald #include "btstack_debug.h"
603edc84c5SMatthias Ringwald #include "classic/sdp_query_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
637bd8e93bSMatthias Ringwald #include "btstack_event.h"
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
663deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald 
693deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
703deb3ec6SMatthias Ringwald     "EC and/or NR function",
713deb3ec6SMatthias Ringwald     "Three-way calling",
723deb3ec6SMatthias Ringwald     "CLI presentation capability",
733deb3ec6SMatthias Ringwald     "Voice recognition activation",
743deb3ec6SMatthias Ringwald     "Remote volume control",
753deb3ec6SMatthias Ringwald 
763deb3ec6SMatthias Ringwald     "Enhanced call status",
773deb3ec6SMatthias Ringwald     "Enhanced call control",
783deb3ec6SMatthias Ringwald 
793deb3ec6SMatthias Ringwald     "Codec negotiation",
803deb3ec6SMatthias Ringwald 
813deb3ec6SMatthias Ringwald     "HF Indicators",
823deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
833deb3ec6SMatthias Ringwald     "Reserved for future definition"
843deb3ec6SMatthias Ringwald };
853deb3ec6SMatthias Ringwald 
863deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
873deb3ec6SMatthias Ringwald     "Three-way calling",
883deb3ec6SMatthias Ringwald     "EC and/or NR function",
893deb3ec6SMatthias Ringwald     "Voice recognition function",
903deb3ec6SMatthias Ringwald     "In-band ring tone capability",
913deb3ec6SMatthias Ringwald     "Attach a number to a voice tag",
923deb3ec6SMatthias Ringwald     "Ability to reject a call",
933deb3ec6SMatthias Ringwald     "Enhanced call status",
943deb3ec6SMatthias Ringwald     "Enhanced call control",
953deb3ec6SMatthias Ringwald     "Extended Error Result Codes",
963deb3ec6SMatthias Ringwald     "Codec negotiation",
973deb3ec6SMatthias Ringwald     "HF Indicators",
983deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
993deb3ec6SMatthias Ringwald     "Reserved for future definition"
1003deb3ec6SMatthias Ringwald };
1013deb3ec6SMatthias Ringwald 
1023deb3ec6SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
1033deb3ec6SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
1043deb3ec6SMatthias Ringwald 
1058f2a52f4SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL;
106ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
107f4000eebSMatthias Ringwald static hfp_callback_t hfp_callback;
108f4000eebSMatthias Ringwald 
109f4000eebSMatthias Ringwald void hfp_set_callback(hfp_callback_t callback){
110f4000eebSMatthias Ringwald     hfp_callback = callback;
111f4000eebSMatthias Ringwald }
1123deb3ec6SMatthias Ringwald 
1133deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
1143deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
1153deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
1163deb3ec6SMatthias Ringwald     }
1173deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
1183deb3ec6SMatthias Ringwald }
1193deb3ec6SMatthias Ringwald 
1203deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
1213deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
1223deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
1233deb3ec6SMatthias Ringwald     }
1243deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
1253deb3ec6SMatthias Ringwald }
1263deb3ec6SMatthias Ringwald 
1273deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
1283deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
12974386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
13028190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
1313deb3ec6SMatthias Ringwald     if (err){
13228190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
1333deb3ec6SMatthias Ringwald     }
1343deb3ec6SMatthias Ringwald     return 1;
1353deb3ec6SMatthias Ringwald }
1363deb3ec6SMatthias Ringwald 
1373deb3ec6SMatthias Ringwald #if 0
138*a0ffb263SMatthias Ringwald void hfp_set_codec(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1393deb3ec6SMatthias Ringwald     // parse available codecs
1403deb3ec6SMatthias Ringwald     int pos = 0;
1413deb3ec6SMatthias Ringwald     int i;
1423deb3ec6SMatthias Ringwald     for (i=0; i<size; i++){
1433deb3ec6SMatthias Ringwald         pos+=8;
144*a0ffb263SMatthias Ringwald         if (packet[pos] > hfp_connection->negotiated_codec){
145*a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = packet[pos];
1463deb3ec6SMatthias Ringwald         }
1473deb3ec6SMatthias Ringwald     }
148*a0ffb263SMatthias Ringwald     printf("Negotiated Codec 0x%02x\n", hfp_connection->negotiated_codec);
1493deb3ec6SMatthias Ringwald }
1503deb3ec6SMatthias Ringwald #endif
1513deb3ec6SMatthias Ringwald 
1523deb3ec6SMatthias Ringwald // UTILS
1533deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
1543deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
1553deb3ec6SMatthias Ringwald }
1563deb3ec6SMatthias Ringwald 
1573deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
1583deb3ec6SMatthias Ringwald     if (value){
1593deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
1603deb3ec6SMatthias Ringwald     } else {
1613deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
1623deb3ec6SMatthias Ringwald     }
1633deb3ec6SMatthias Ringwald     return bitmap;
1643deb3ec6SMatthias Ringwald }
1653deb3ec6SMatthias Ringwald 
1663deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
1673deb3ec6SMatthias Ringwald     if (buffer_size < values_nr * 3) return 0;
1683deb3ec6SMatthias Ringwald     int i;
1693deb3ec6SMatthias Ringwald     int offset = 0;
1703deb3ec6SMatthias Ringwald     for (i = 0; i < values_nr-1; i++) {
1713deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
1723deb3ec6SMatthias Ringwald     }
1733deb3ec6SMatthias Ringwald     if (i<values_nr){
1743deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
1753deb3ec6SMatthias Ringwald     }
1763deb3ec6SMatthias Ringwald     return offset;
1773deb3ec6SMatthias Ringwald }
1783deb3ec6SMatthias Ringwald 
1793deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
1803deb3ec6SMatthias Ringwald     if (buffer_size < values_nr * 3) return 0;
1813deb3ec6SMatthias Ringwald 
1823deb3ec6SMatthias Ringwald     int i;
1833deb3ec6SMatthias Ringwald     int offset = 0;
1843deb3ec6SMatthias Ringwald     for (i = 0; i < values_nr-1; i++) {
1853deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
1863deb3ec6SMatthias Ringwald     }
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald     if (i<values_nr){
1893deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
1903deb3ec6SMatthias Ringwald     }
1913deb3ec6SMatthias Ringwald     return offset;
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
194*a0ffb263SMatthias Ringwald void hfp_emit_simple_event(hfp_callback_t callback, uint8_t event_subtype){
195*a0ffb263SMatthias Ringwald     if (!callback) return;
196*a0ffb263SMatthias Ringwald     uint8_t event[3];
197*a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
198*a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
199*a0ffb263SMatthias Ringwald     event[2] = event_subtype;
200*a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
201*a0ffb263SMatthias Ringwald }
202*a0ffb263SMatthias Ringwald 
2033deb3ec6SMatthias Ringwald void hfp_emit_event(hfp_callback_t callback, uint8_t event_subtype, uint8_t value){
2043deb3ec6SMatthias Ringwald     if (!callback) return;
2053deb3ec6SMatthias Ringwald     uint8_t event[4];
2063deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
2073deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
2083deb3ec6SMatthias Ringwald     event[2] = event_subtype;
2093deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
2103deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
2113deb3ec6SMatthias Ringwald }
2123deb3ec6SMatthias Ringwald 
213aa4dd815SMatthias Ringwald void hfp_emit_string_event(hfp_callback_t callback, uint8_t event_subtype, const char * value){
214aa4dd815SMatthias Ringwald     if (!callback) return;
215c1797c7dSMatthias Ringwald     uint8_t event[40];
216aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
217aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
218aa4dd815SMatthias Ringwald     event[2] = event_subtype;
219aa4dd815SMatthias Ringwald     int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4;
220aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
221aa4dd815SMatthias Ringwald     event[3 + size] = 0;
222aa4dd815SMatthias Ringwald     (*callback)(event, sizeof(event));
223aa4dd815SMatthias Ringwald }
224aa4dd815SMatthias Ringwald 
225aa4dd815SMatthias Ringwald static void hfp_emit_audio_connection_established_event(hfp_callback_t callback, uint8_t value, uint16_t sco_handle){
226aa4dd815SMatthias Ringwald     if (!callback) return;
227aa4dd815SMatthias Ringwald     uint8_t event[6];
228aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
229aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
230aa4dd815SMatthias Ringwald     event[2] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
231aa4dd815SMatthias Ringwald     event[3] = value; // status 0 == OK
232f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, sco_handle);
233aa4dd815SMatthias Ringwald     (*callback)(event, sizeof(event));
234aa4dd815SMatthias Ringwald }
2353deb3ec6SMatthias Ringwald 
2368f2a52f4SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(){
2378f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
2383deb3ec6SMatthias Ringwald }
2393deb3ec6SMatthias Ringwald 
2403deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
241665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
242665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
243665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
244*a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
245*a0ffb263SMatthias Ringwald         if (hfp_connection->rfcomm_cid == cid){
246*a0ffb263SMatthias Ringwald             return hfp_connection;
2473deb3ec6SMatthias Ringwald         }
2483deb3ec6SMatthias Ringwald     }
2493deb3ec6SMatthias Ringwald     return NULL;
2503deb3ec6SMatthias Ringwald }
2513deb3ec6SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
253665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
254665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
255665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
256*a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
257*a0ffb263SMatthias Ringwald         if (memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) {
258*a0ffb263SMatthias Ringwald             return hfp_connection;
2593deb3ec6SMatthias Ringwald         }
2603deb3ec6SMatthias Ringwald     }
2613deb3ec6SMatthias Ringwald     return NULL;
2623deb3ec6SMatthias Ringwald }
2633deb3ec6SMatthias Ringwald 
264aa4dd815SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){
265665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
266665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
267665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
268*a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
269*a0ffb263SMatthias Ringwald         if (hfp_connection->sco_handle == handle){
270*a0ffb263SMatthias Ringwald             return hfp_connection;
2713deb3ec6SMatthias Ringwald         }
2723deb3ec6SMatthias Ringwald     }
2733deb3ec6SMatthias Ringwald     return NULL;
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald 
276*a0ffb263SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * hfp_connection){
277*a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
278*a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
279*a0ffb263SMatthias Ringwald     hfp_connection->send_error = 0;
2803deb3ec6SMatthias Ringwald 
281*a0ffb263SMatthias Ringwald     hfp_connection->keep_byte = 0;
2823deb3ec6SMatthias Ringwald 
283*a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 0;
284*a0ffb263SMatthias Ringwald     hfp_connection->operator_name_changed = 0;
2853deb3ec6SMatthias Ringwald 
286*a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = 0;
287*a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2883deb3ec6SMatthias Ringwald 
289*a0ffb263SMatthias Ringwald     // establish codecs hfp_connection
290*a0ffb263SMatthias Ringwald     hfp_connection->suggested_codec = 0;
291*a0ffb263SMatthias Ringwald     hfp_connection->negotiated_codec = 0;
292*a0ffb263SMatthias Ringwald     hfp_connection->codec_confirmed = 0;
2933deb3ec6SMatthias Ringwald 
294*a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
295*a0ffb263SMatthias Ringwald     hfp_connection->call_waiting_notification_enabled = 0;
296*a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
297*a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
2983deb3ec6SMatthias Ringwald }
2993deb3ec6SMatthias Ringwald 
3003deb3ec6SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(){
301*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get();
302*a0ffb263SMatthias Ringwald     if (!hfp_connection) return NULL;
3033deb3ec6SMatthias Ringwald     // init state
304*a0ffb263SMatthias Ringwald     memset(hfp_connection,0, sizeof(hfp_connection_t));
3053deb3ec6SMatthias Ringwald 
306*a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_IDLE;
307*a0ffb263SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
308*a0ffb263SMatthias Ringwald     hfp_connection->codecs_state = HFP_CODECS_IDLE;
309aa4dd815SMatthias Ringwald 
310*a0ffb263SMatthias Ringwald     hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
311*a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
3123deb3ec6SMatthias Ringwald 
313*a0ffb263SMatthias Ringwald     hfp_reset_context_flags(hfp_connection);
3143deb3ec6SMatthias Ringwald 
315*a0ffb263SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (linked_item_t*)hfp_connection);
316*a0ffb263SMatthias Ringwald     return hfp_connection;
3173deb3ec6SMatthias Ringwald }
3183deb3ec6SMatthias Ringwald 
319*a0ffb263SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * hfp_connection){
320*a0ffb263SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection);
3213deb3ec6SMatthias Ringwald }
3223deb3ec6SMatthias Ringwald 
3233deb3ec6SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
324*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
325*a0ffb263SMatthias Ringwald     if (hfp_connection) return  hfp_connection;
326*a0ffb263SMatthias Ringwald     hfp_connection = create_hfp_connection_context();
327*a0ffb263SMatthias Ringwald     printf("created hfp_connection for address %s\n", bd_addr_to_str(bd_addr));
328*a0ffb263SMatthias Ringwald     memcpy(hfp_connection->remote_addr, bd_addr, 6);
329*a0ffb263SMatthias Ringwald     return hfp_connection;
3303deb3ec6SMatthias Ringwald }
3313deb3ec6SMatthias Ringwald 
332aa4dd815SMatthias Ringwald /* @param network.
333aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
334aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
335aa4dd815SMatthias Ringwald  */
3363deb3ec6SMatthias Ringwald 
3373deb3ec6SMatthias Ringwald /* @param suported_features
3383deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
3393deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
3403deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
3413deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
3423deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
3433deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
3443deb3ec6SMatthias Ringwald  */
3453deb3ec6SMatthias Ringwald  /* Bit position:
3463deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
3473deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
3483deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
3493deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
3503deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
3513deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
3523deb3ec6SMatthias Ringwald  */
3533deb3ec6SMatthias Ringwald 
3549b1c3b4dSMatthias 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){
3553deb3ec6SMatthias Ringwald     uint8_t* attribute;
3563deb3ec6SMatthias Ringwald     de_create_sequence(service);
3573deb3ec6SMatthias Ringwald 
3583deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
3593deb3ec6SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
3609b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
3613deb3ec6SMatthias Ringwald 
3623deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
3633deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
3643deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3653deb3ec6SMatthias Ringwald     {
3663deb3ec6SMatthias Ringwald         //  "UUID for Service"
3673deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
3683deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio);
3693deb3ec6SMatthias Ringwald     }
3703deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
3713deb3ec6SMatthias Ringwald 
3723deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
3733deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
3743deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3753deb3ec6SMatthias Ringwald     {
3763deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
3773deb3ec6SMatthias Ringwald         {
3783deb3ec6SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
3793deb3ec6SMatthias Ringwald         }
3803deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
3813deb3ec6SMatthias Ringwald 
3823deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
3833deb3ec6SMatthias Ringwald         {
3843deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol);  // rfcomm_service
3853deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
3863deb3ec6SMatthias Ringwald         }
3873deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
3883deb3ec6SMatthias Ringwald     }
3893deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
3903deb3ec6SMatthias Ringwald 
3913deb3ec6SMatthias Ringwald 
3923deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
3933deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
3943deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3953deb3ec6SMatthias Ringwald     {
3963deb3ec6SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
3973deb3ec6SMatthias Ringwald     }
3983deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
3993deb3ec6SMatthias Ringwald 
4003deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
4013deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
4023deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4033deb3ec6SMatthias Ringwald     {
4043deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
4053deb3ec6SMatthias Ringwald         {
4063deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, SDP_Handsfree);
4073deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
4083deb3ec6SMatthias Ringwald         }
4093deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
4103deb3ec6SMatthias Ringwald     }
4113deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4123deb3ec6SMatthias Ringwald 
4133deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
4143deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
4153deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
4163deb3ec6SMatthias Ringwald }
4173deb3ec6SMatthias Ringwald 
4183deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL;
419a8747467SMatthias Ringwald 
42033885e44SMatthias Ringwald static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
421f4000eebSMatthias Ringwald     hfp_handle_hci_event(packet_type, packet, size);
422f4000eebSMatthias Ringwald }
423f4000eebSMatthias Ringwald 
4246c927b22SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
425*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = connection_doing_sdp_query;
4263deb3ec6SMatthias Ringwald 
427*a0ffb263SMatthias Ringwald     if ( hfp_connection->state != HFP_W4_SDP_EVENT_QUERY_COMPLETE) return;
4283deb3ec6SMatthias Ringwald 
4290e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
4305611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
431*a0ffb263SMatthias Ringwald             if (!hfp_connection) {
43272b50801SMatthias Ringwald                 log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_event_query_rfcomm_service_get_rfcomm_channel(packet));
4333deb3ec6SMatthias Ringwald                 return;
4343deb3ec6SMatthias Ringwald             }
435*a0ffb263SMatthias Ringwald             hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
4363deb3ec6SMatthias Ringwald             break;
4375611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
4383deb3ec6SMatthias Ringwald             connection_doing_sdp_query = NULL;
439*a0ffb263SMatthias Ringwald             if (hfp_connection->rfcomm_channel_nr > 0){
440*a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED;
441*a0ffb263SMatthias 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);
442*a0ffb263SMatthias Ringwald                 rfcomm_create_channel(handle_hci_event, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL);
4433deb3ec6SMatthias Ringwald                 break;
4443deb3ec6SMatthias Ringwald             }
44572b50801SMatthias Ringwald             log_info("rfcomm service not found, status %u.", sdp_event_query_complete_get_status(packet));
4463deb3ec6SMatthias Ringwald             break;
4473deb3ec6SMatthias Ringwald         default:
4483deb3ec6SMatthias Ringwald             break;
4493deb3ec6SMatthias Ringwald     }
4503deb3ec6SMatthias Ringwald }
4513deb3ec6SMatthias Ringwald 
452f4000eebSMatthias Ringwald void hfp_handle_hci_event(uint8_t packet_type, uint8_t *packet, uint16_t size){
4533deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
4543deb3ec6SMatthias Ringwald     uint16_t rfcomm_cid, handle;
455*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = NULL;
4563deb3ec6SMatthias Ringwald 
4570e2df43fSMatthias Ringwald     // printf("AG packet_handler type %u, event type %x, size %u\n", packet_type, hci_event_packet_get_type(packet), size);
458aa4dd815SMatthias Ringwald 
4590e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)) {
4603deb3ec6SMatthias Ringwald 
4613deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
4623deb3ec6SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
463724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[2], event_addr);
464aa4dd815SMatthias Ringwald             context = provide_hfp_connection_context_for_bd_addr(event_addr);
4653deb3ec6SMatthias Ringwald 
4663deb3ec6SMatthias Ringwald             if (!context || context->state != HFP_IDLE) return;
4673deb3ec6SMatthias Ringwald 
468f8fbdce0SMatthias Ringwald             context->rfcomm_cid = little_endian_read_16(packet, 9);
4693deb3ec6SMatthias Ringwald             context->state = HFP_W4_RFCOMM_CONNECTED;
4703deb3ec6SMatthias Ringwald             printf("RFCOMM channel %u requested for %s\n", context->rfcomm_cid, bd_addr_to_str(context->remote_addr));
47128190c0bSMatthias Ringwald             rfcomm_accept_connection(context->rfcomm_cid);
4723deb3ec6SMatthias Ringwald             break;
4733deb3ec6SMatthias Ringwald 
4743deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
4753deb3ec6SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
4760e2df43fSMatthias Ringwald             printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, size %u\n", packet_type, size);
477aa4dd815SMatthias Ringwald 
478724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[3], event_addr);
4793deb3ec6SMatthias Ringwald             context = get_hfp_connection_context_for_bd_addr(event_addr);
4803deb3ec6SMatthias Ringwald             if (!context || context->state != HFP_W4_RFCOMM_CONNECTED) return;
4813deb3ec6SMatthias Ringwald 
4823deb3ec6SMatthias Ringwald             if (packet[2]) {
483f4000eebSMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, packet[2]);
4843deb3ec6SMatthias Ringwald                 remove_hfp_connection_context(context);
4853deb3ec6SMatthias Ringwald             } else {
486fc64f94aSMatthias Ringwald                 context->acl_handle = little_endian_read_16(packet, 9);
487fc64f94aSMatthias Ringwald                 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE con_handle 0x%02x\n", context->acl_handle);
488aa4dd815SMatthias Ringwald 
489f8fbdce0SMatthias Ringwald                 context->rfcomm_cid = little_endian_read_16(packet, 12);
490f8fbdce0SMatthias Ringwald                 uint16_t mtu = little_endian_read_16(packet, 14);
4913deb3ec6SMatthias Ringwald                 printf("RFCOMM channel open succeeded. Context %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", context, context->rfcomm_cid, mtu);
4923deb3ec6SMatthias Ringwald 
4933deb3ec6SMatthias Ringwald                 switch (context->state){
4943deb3ec6SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
4953deb3ec6SMatthias Ringwald                         context->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
4963deb3ec6SMatthias Ringwald                         break;
4973deb3ec6SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
4983deb3ec6SMatthias Ringwald                         context->state = HFP_W2_DISCONNECT_RFCOMM;
4993deb3ec6SMatthias Ringwald                         printf("Shutting down RFCOMM.\n");
5003deb3ec6SMatthias Ringwald                         break;
5013deb3ec6SMatthias Ringwald                     default:
5023deb3ec6SMatthias Ringwald                         break;
5033deb3ec6SMatthias Ringwald                 }
5043deb3ec6SMatthias Ringwald             }
5053deb3ec6SMatthias Ringwald             break;
5063deb3ec6SMatthias Ringwald 
5073deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
508ce263fc8SMatthias Ringwald 
509724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], event_addr);
5103deb3ec6SMatthias Ringwald             int index = 2;
5113deb3ec6SMatthias Ringwald             uint8_t status = packet[index++];
512aa4dd815SMatthias Ringwald 
513aa4dd815SMatthias Ringwald             if (status != 0){
514ce263fc8SMatthias Ringwald                 log_error("(e)SCO Connection failed status %u", status);
515ce263fc8SMatthias Ringwald                 // if outgoing && link_setting != d0 && appropriate error
516ce263fc8SMatthias Ringwald                 if (status != 0x11 && status != 0x1f) break;  // invalid params / unspecified error
517ce263fc8SMatthias Ringwald                 context = get_hfp_connection_context_for_bd_addr(event_addr);
518ce263fc8SMatthias Ringwald                 if (!context) break;
519ce263fc8SMatthias Ringwald                 switch (context->link_setting){
520ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_D0:
521ce263fc8SMatthias Ringwald                         return; // no other option left
522ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_D1:
523ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_D0;
524ce263fc8SMatthias Ringwald                         // break;
525ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S1:
526ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_D1;
527ce263fc8SMatthias Ringwald                         // break;
528ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S2:
529ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S3:
530ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S4:
531ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_S1;
532ce263fc8SMatthias Ringwald                         // break;
533ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_T1:
534ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_T2:
535ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_S3;
536ce263fc8SMatthias Ringwald                         context->link_setting = HFP_LINK_SETTINGS_D0;
537ce263fc8SMatthias Ringwald                         break;
538ce263fc8SMatthias Ringwald                 }
539ce263fc8SMatthias Ringwald                 context->establish_audio_connection = 1;
540aa4dd815SMatthias Ringwald                 break;
541aa4dd815SMatthias Ringwald             }
542aa4dd815SMatthias Ringwald 
543f8fbdce0SMatthias Ringwald             uint16_t sco_handle = little_endian_read_16(packet, index);
5443deb3ec6SMatthias Ringwald             index+=2;
545aa4dd815SMatthias Ringwald 
546724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[index], event_addr);
5473deb3ec6SMatthias Ringwald             index+=6;
548aa4dd815SMatthias Ringwald 
5493deb3ec6SMatthias Ringwald             uint8_t link_type = packet[index++];
5503deb3ec6SMatthias Ringwald             uint8_t transmission_interval = packet[index++];  // measured in slots
5513deb3ec6SMatthias Ringwald             uint8_t retransmission_interval = packet[index++];// measured in slots
552f8fbdce0SMatthias Ringwald             uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes
5533deb3ec6SMatthias Ringwald             index+=2;
554f8fbdce0SMatthias Ringwald             uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes
5553deb3ec6SMatthias Ringwald             index+=2;
5563deb3ec6SMatthias Ringwald             uint8_t air_mode = packet[index];
5573deb3ec6SMatthias Ringwald 
5583deb3ec6SMatthias Ringwald             switch (link_type){
5593deb3ec6SMatthias Ringwald                 case 0x00:
560aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
5613deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
5623deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
5633deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
5643deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
5653deb3ec6SMatthias Ringwald                     break;
5663deb3ec6SMatthias Ringwald                 case 0x02:
567aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
5683deb3ec6SMatthias Ringwald                     break;
5693deb3ec6SMatthias Ringwald                 default:
5703deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
5713deb3ec6SMatthias Ringwald                     break;
5723deb3ec6SMatthias Ringwald             }
5733deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
574aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
575aa4dd815SMatthias Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
5763deb3ec6SMatthias Ringwald 
577aa4dd815SMatthias Ringwald             context = get_hfp_connection_context_for_bd_addr(event_addr);
5783deb3ec6SMatthias Ringwald 
579aa4dd815SMatthias Ringwald             if (!context) {
580aa4dd815SMatthias Ringwald                 log_error("SCO link created, context for address %s not found.", bd_addr_to_str(event_addr));
5813deb3ec6SMatthias Ringwald                 break;
5823deb3ec6SMatthias Ringwald             }
5833deb3ec6SMatthias Ringwald 
584aa4dd815SMatthias Ringwald             if (context->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
585aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
586aa4dd815SMatthias Ringwald                 context->state = HFP_W2_DISCONNECT_SCO;
587aa4dd815SMatthias Ringwald                 break;
588aa4dd815SMatthias Ringwald             }
5893deb3ec6SMatthias Ringwald             context->sco_handle = sco_handle;
590aa4dd815SMatthias Ringwald             context->establish_audio_connection = 0;
5913deb3ec6SMatthias Ringwald             context->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
592f4000eebSMatthias Ringwald             hfp_emit_audio_connection_established_event(hfp_callback, packet[2], sco_handle);
5933deb3ec6SMatthias Ringwald             break;
5943deb3ec6SMatthias Ringwald         }
5953deb3ec6SMatthias Ringwald 
5963deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
597f8fbdce0SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
5983deb3ec6SMatthias Ringwald             context = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
5993deb3ec6SMatthias Ringwald             if (!context) break;
6003deb3ec6SMatthias Ringwald             if (context->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
6013deb3ec6SMatthias Ringwald                 context->state = HFP_IDLE;
6023deb3ec6SMatthias Ringwald                 hfp_establish_service_level_connection(context->remote_addr, context->service_uuid);
6033deb3ec6SMatthias Ringwald                 break;
6043deb3ec6SMatthias Ringwald             }
6053deb3ec6SMatthias Ringwald 
606f4000eebSMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
6073deb3ec6SMatthias Ringwald             remove_hfp_connection_context(context);
6083deb3ec6SMatthias Ringwald             break;
6093deb3ec6SMatthias Ringwald 
6103deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
611f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
612aa4dd815SMatthias Ringwald             context = get_hfp_connection_context_for_sco_handle(handle);
613aa4dd815SMatthias Ringwald 
6143deb3ec6SMatthias Ringwald             if (!context) break;
615aa4dd815SMatthias Ringwald 
616aa4dd815SMatthias Ringwald             if (context->state != HFP_W4_SCO_DISCONNECTED){
617aa4dd815SMatthias Ringwald                 log_info("Received gap disconnect in wrong hfp state");
618aa4dd815SMatthias Ringwald             }
619aa4dd815SMatthias Ringwald             log_info("Check SCO handle: incoming 0x%02x, context 0x%02x\n", handle,context->sco_handle);
620aa4dd815SMatthias Ringwald 
621aa4dd815SMatthias Ringwald             if (handle == context->sco_handle){
622aa4dd815SMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
623aa4dd815SMatthias Ringwald                 context->sco_handle = 0;
624aa4dd815SMatthias Ringwald                 context->release_audio_connection = 0;
625aa4dd815SMatthias Ringwald                 context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
626f4000eebSMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
6273deb3ec6SMatthias Ringwald                 break;
6283deb3ec6SMatthias Ringwald             }
6293deb3ec6SMatthias Ringwald             break;
6303deb3ec6SMatthias Ringwald 
631fc64f94aSMatthias Ringwald         default:
6323deb3ec6SMatthias Ringwald             break;
6333deb3ec6SMatthias Ringwald     }
6343deb3ec6SMatthias Ringwald }
6353deb3ec6SMatthias Ringwald 
636aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
637aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
638aa4dd815SMatthias Ringwald     int offset = isHandsFree ? 0 : 2;
6393deb3ec6SMatthias Ringwald 
640ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){
641ce263fc8SMatthias Ringwald         return HFP_CMD_LIST_CURRENT_CALLS;
642ce263fc8SMatthias Ringwald     }
643ce263fc8SMatthias Ringwald 
644ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){
645ce263fc8SMatthias Ringwald         return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION;
646ce263fc8SMatthias Ringwald     }
647ce263fc8SMatthias Ringwald 
648aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){
649ce263fc8SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER;
650aa4dd815SMatthias Ringwald         return HFP_CMD_HF_REQUEST_PHONE_NUMBER;
6513deb3ec6SMatthias Ringwald     }
6523deb3ec6SMatthias Ringwald 
653aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){
654aa4dd815SMatthias Ringwald         return HFP_CMD_TRANSMIT_DTMF_CODES;
6553deb3ec6SMatthias Ringwald     }
6563deb3ec6SMatthias Ringwald 
657aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){
658aa4dd815SMatthias Ringwald         return HFP_CMD_SET_MICROPHONE_GAIN;
6593deb3ec6SMatthias Ringwald     }
6603deb3ec6SMatthias Ringwald 
661aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){
662aa4dd815SMatthias Ringwald         return HFP_CMD_SET_SPEAKER_GAIN;
6633deb3ec6SMatthias Ringwald     }
6643deb3ec6SMatthias Ringwald 
665aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){
666aa4dd815SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
667aa4dd815SMatthias Ringwald         return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
6683deb3ec6SMatthias Ringwald     }
6693deb3ec6SMatthias Ringwald 
670aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){
671aa4dd815SMatthias Ringwald         return HFP_CMD_TURN_OFF_EC_AND_NR;
6723deb3ec6SMatthias Ringwald     }
6733deb3ec6SMatthias Ringwald 
674aa4dd815SMatthias Ringwald     if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){
675aa4dd815SMatthias Ringwald         return HFP_CMD_CALL_ANSWERED;
6763deb3ec6SMatthias Ringwald     }
6773deb3ec6SMatthias Ringwald 
678aa4dd815SMatthias Ringwald     if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
679aa4dd815SMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
6803deb3ec6SMatthias Ringwald     }
6813deb3ec6SMatthias Ringwald 
682ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){
683aa4dd815SMatthias Ringwald         return HFP_CMD_REDIAL_LAST_NUMBER;
6843deb3ec6SMatthias Ringwald     }
6853deb3ec6SMatthias Ringwald 
686aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){
687aa4dd815SMatthias Ringwald         return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
688aa4dd815SMatthias Ringwald     }
689aa4dd815SMatthias Ringwald 
690aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){
691aa4dd815SMatthias Ringwald         return HFP_CMD_HANG_UP_CALL;
692aa4dd815SMatthias Ringwald     }
693aa4dd815SMatthias Ringwald 
694aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){
695aa4dd815SMatthias Ringwald         return HFP_CMD_ERROR;
696aa4dd815SMatthias Ringwald     }
697aa4dd815SMatthias Ringwald 
698ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){
699ce263fc8SMatthias Ringwald         return HFP_CMD_RING;
700ce263fc8SMatthias Ringwald     }
701ce263fc8SMatthias Ringwald 
702aa4dd815SMatthias Ringwald     if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){
703aa4dd815SMatthias Ringwald         return HFP_CMD_OK;
704aa4dd815SMatthias Ringwald     }
705aa4dd815SMatthias Ringwald 
706aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){
707aa4dd815SMatthias Ringwald         return HFP_CMD_SUPPORTED_FEATURES;
708aa4dd815SMatthias Ringwald     }
709aa4dd815SMatthias Ringwald 
710ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){
711ce263fc8SMatthias Ringwald         return HFP_CMD_HF_INDICATOR_STATUS;
712ce263fc8SMatthias Ringwald     }
713ce263fc8SMatthias Ringwald 
714ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){
715ce263fc8SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){
716ce263fc8SMatthias Ringwald             return HFP_CMD_RESPONSE_AND_HOLD_QUERY;
717ce263fc8SMatthias Ringwald         }
718ce263fc8SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){
719ce263fc8SMatthias Ringwald             return HFP_CMD_RESPONSE_AND_HOLD_COMMAND;
720ce263fc8SMatthias Ringwald         }
721667ec068SMatthias Ringwald         return HFP_CMD_RESPONSE_AND_HOLD_STATUS;
722ce263fc8SMatthias Ringwald     }
723ce263fc8SMatthias Ringwald 
724aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){
725aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){
726aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
727aa4dd815SMatthias Ringwald         }
728aa4dd815SMatthias Ringwald 
729aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){
730aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_AG_INDICATORS;
731aa4dd815SMatthias Ringwald         }
732aa4dd815SMatthias Ringwald     }
733aa4dd815SMatthias Ringwald 
734aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){
735aa4dd815SMatthias Ringwald         return HFP_CMD_AVAILABLE_CODECS;
736aa4dd815SMatthias Ringwald     }
737aa4dd815SMatthias Ringwald 
738aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){
739aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE;
740aa4dd815SMatthias Ringwald     }
741aa4dd815SMatthias Ringwald 
742aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){
743*a0ffb263SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_CLIP_INFORMATION;
744aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_CLIP;
745aa4dd815SMatthias Ringwald     }
746aa4dd815SMatthias Ringwald 
747aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){
748*a0ffb263SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE;
749aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION;
750aa4dd815SMatthias Ringwald     }
751aa4dd815SMatthias Ringwald 
752aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){
753aa4dd815SMatthias Ringwald 
754aa4dd815SMatthias Ringwald         if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
755aa4dd815SMatthias Ringwald 
756aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){
757aa4dd815SMatthias Ringwald             return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
758aa4dd815SMatthias Ringwald         }
759667ec068SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){
760aa4dd815SMatthias Ringwald             return HFP_CMD_CALL_HOLD;
761aa4dd815SMatthias Ringwald         }
762aa4dd815SMatthias Ringwald 
763aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
764aa4dd815SMatthias Ringwald     }
765aa4dd815SMatthias Ringwald 
766aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){
767667ec068SMatthias Ringwald         if (isHandsFree) {
768667ec068SMatthias Ringwald             return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS;
769667ec068SMatthias Ringwald         }
770aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){
771aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
772aa4dd815SMatthias Ringwald         }
773aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){
774aa4dd815SMatthias Ringwald             return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
775aa4dd815SMatthias Ringwald         }
776aa4dd815SMatthias Ringwald         return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
777aa4dd815SMatthias Ringwald     }
778aa4dd815SMatthias Ringwald 
779aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){
780aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE;
7813deb3ec6SMatthias Ringwald     }
7823deb3ec6SMatthias Ringwald 
7833deb3ec6SMatthias Ringwald 
784aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){
785aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){
786aa4dd815SMatthias Ringwald             return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT;
7873deb3ec6SMatthias Ringwald         }
788aa4dd815SMatthias Ringwald         return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME;
7893deb3ec6SMatthias Ringwald     }
7903deb3ec6SMatthias Ringwald 
791aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){
792aa4dd815SMatthias Ringwald         return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS;
7933deb3ec6SMatthias Ringwald     }
7943deb3ec6SMatthias Ringwald 
795aa4dd815SMatthias Ringwald     if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
796aa4dd815SMatthias Ringwald         return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR;
7973deb3ec6SMatthias Ringwald     }
7983deb3ec6SMatthias Ringwald 
799aa4dd815SMatthias Ringwald     if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
800aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR;
8013deb3ec6SMatthias Ringwald     }
8023deb3ec6SMatthias Ringwald 
803aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){
804aa4dd815SMatthias Ringwald         return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
805aa4dd815SMatthias Ringwald     }
806aa4dd815SMatthias Ringwald 
807aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){
8083deb3ec6SMatthias Ringwald         if (isHandsFree){
809aa4dd815SMatthias Ringwald             return HFP_CMD_AG_SUGGESTED_CODEC;
8103deb3ec6SMatthias Ringwald         } else {
811aa4dd815SMatthias Ringwald             return HFP_CMD_HF_CONFIRMED_CODEC;
8123deb3ec6SMatthias Ringwald         }
8133deb3ec6SMatthias Ringwald     }
8143deb3ec6SMatthias Ringwald 
815aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "AT+", 3) == 0){
816667ec068SMatthias Ringwald         log_info("process unknown HF command %s \n", line_buffer);
817aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
8183deb3ec6SMatthias Ringwald     }
8193deb3ec6SMatthias Ringwald 
820aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "+", 1) == 0){
821667ec068SMatthias Ringwald         log_info(" process unknown AG command %s \n", line_buffer);
822aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
823aa4dd815SMatthias Ringwald     }
8243deb3ec6SMatthias Ringwald 
825aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "NOP", 3) == 0){
826aa4dd815SMatthias Ringwald         return HFP_CMD_NONE;
827aa4dd815SMatthias Ringwald     }
828aa4dd815SMatthias Ringwald 
829aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
8303deb3ec6SMatthias Ringwald }
8313deb3ec6SMatthias Ringwald 
832*a0ffb263SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){
833ce263fc8SMatthias Ringwald     // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size);
8343deb3ec6SMatthias Ringwald     // TODO: add limit
835*a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
836*a0ffb263SMatthias Ringwald     hfp_connection->line_buffer[hfp_connection->line_size] = 0;
8373deb3ec6SMatthias Ringwald }
838*a0ffb263SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){
839*a0ffb263SMatthias Ringwald     return hfp_connection->line_size == 0;
8403deb3ec6SMatthias Ringwald }
8413deb3ec6SMatthias Ringwald 
8423deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
8433deb3ec6SMatthias Ringwald     return byte == '\n' || byte == '\r';
8443deb3ec6SMatthias Ringwald }
8453deb3ec6SMatthias Ringwald 
8463deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_header(uint8_t byte){
8473deb3ec6SMatthias Ringwald     return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?';
8483deb3ec6SMatthias Ringwald }
8493deb3ec6SMatthias Ringwald 
850*a0ffb263SMatthias Ringwald static int hfp_parser_found_separator(hfp_connection_t * hfp_connection, uint8_t byte){
851*a0ffb263SMatthias Ringwald     if (hfp_connection->keep_byte == 1) return 1;
8523deb3ec6SMatthias Ringwald 
8533deb3ec6SMatthias Ringwald     int found_separator =   byte == ',' || byte == '\n'|| byte == '\r'||
8543deb3ec6SMatthias Ringwald                             byte == ')' || byte == '(' || byte == ':' ||
8553deb3ec6SMatthias Ringwald                             byte == '-' || byte == '"' ||  byte == '?'|| byte == '=';
8563deb3ec6SMatthias Ringwald     return found_separator;
8573deb3ec6SMatthias Ringwald }
8583deb3ec6SMatthias Ringwald 
859*a0ffb263SMatthias Ringwald static void hfp_parser_next_state(hfp_connection_t * hfp_connection, uint8_t byte){
860*a0ffb263SMatthias Ringwald     hfp_connection->line_size = 0;
8613deb3ec6SMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
862*a0ffb263SMatthias Ringwald         hfp_connection->parser_item_index = 0;
863*a0ffb263SMatthias Ringwald         hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
8643deb3ec6SMatthias Ringwald         return;
8653deb3ec6SMatthias Ringwald     }
866*a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state){
8673deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
868*a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
869*a0ffb263SMatthias Ringwald             if (hfp_connection->keep_byte == 1){
870*a0ffb263SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
871*a0ffb263SMatthias Ringwald                 hfp_connection->keep_byte = 0;
8723deb3ec6SMatthias Ringwald             }
8733deb3ec6SMatthias Ringwald             break;
8743deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
875*a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
876ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
877*a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
878*a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
8793deb3ec6SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
880aa4dd815SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
881aa4dd815SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
882aa4dd815SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
883aa4dd815SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
884ce263fc8SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
885*a0ffb263SMatthias Ringwald                     hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM;
8863deb3ec6SMatthias Ringwald                     break;
8873deb3ec6SMatthias Ringwald                 default:
8883deb3ec6SMatthias Ringwald                     break;
8893deb3ec6SMatthias Ringwald             }
8903deb3ec6SMatthias Ringwald             break;
8913deb3ec6SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
892*a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM;
8933deb3ec6SMatthias Ringwald             break;
8943deb3ec6SMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
895*a0ffb263SMatthias Ringwald             if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
896*a0ffb263SMatthias Ringwald                 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE;
8973deb3ec6SMatthias Ringwald                 break;
8983deb3ec6SMatthias Ringwald             }
899*a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
9003deb3ec6SMatthias Ringwald             break;
9013deb3ec6SMatthias Ringwald     }
9023deb3ec6SMatthias Ringwald }
9033deb3ec6SMatthias Ringwald 
904*a0ffb263SMatthias Ringwald void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){
905aa4dd815SMatthias Ringwald     // handle ATD<dial_string>;
906*a0ffb263SMatthias Ringwald     if (strncmp((const char*)hfp_connection->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
907aa4dd815SMatthias Ringwald         // check for end-of-line or ';'
908aa4dd815SMatthias Ringwald         if (byte == ';' || hfp_parser_is_end_of_line(byte)){
909*a0ffb263SMatthias Ringwald             hfp_connection->line_buffer[hfp_connection->line_size] = 0;
910*a0ffb263SMatthias Ringwald             hfp_connection->line_size = 0;
911*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_CALL_PHONE_NUMBER;
912aa4dd815SMatthias Ringwald         } else {
913*a0ffb263SMatthias Ringwald             hfp_connection->line_buffer[hfp_connection->line_size++] = byte;
914aa4dd815SMatthias Ringwald         }
915aa4dd815SMatthias Ringwald         return;
916aa4dd815SMatthias Ringwald     }
917aa4dd815SMatthias Ringwald 
9183deb3ec6SMatthias Ringwald     // TODO: handle space inside word
919*a0ffb263SMatthias Ringwald     if (byte == ' ' && hfp_connection->parser_state > HFP_PARSER_CMD_HEADER) return;
9203deb3ec6SMatthias Ringwald 
921*a0ffb263SMatthias Ringwald     if (byte == ',' && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
922*a0ffb263SMatthias Ringwald         if (hfp_connection->line_size == 0){
923*a0ffb263SMatthias Ringwald             hfp_connection->line_buffer[0] = 0;
924*a0ffb263SMatthias Ringwald             hfp_connection->ignore_value = 1;
925*a0ffb263SMatthias Ringwald             parse_sequence(hfp_connection);
926ce263fc8SMatthias Ringwald             return;
927ce263fc8SMatthias Ringwald         }
928ce263fc8SMatthias Ringwald     }
929ce263fc8SMatthias Ringwald 
930*a0ffb263SMatthias Ringwald     if (!hfp_parser_found_separator(hfp_connection, byte)){
931*a0ffb263SMatthias Ringwald         hfp_parser_store_byte(hfp_connection, byte);
9323deb3ec6SMatthias Ringwald         return;
9333deb3ec6SMatthias Ringwald     }
934ce263fc8SMatthias Ringwald 
9353deb3ec6SMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)) {
936*a0ffb263SMatthias Ringwald         if (hfp_parser_is_buffer_empty(hfp_connection)){
937*a0ffb263SMatthias Ringwald             hfp_connection->parser_state = HFP_PARSER_CMD_HEADER;
9383deb3ec6SMatthias Ringwald         }
9393deb3ec6SMatthias Ringwald     }
940*a0ffb263SMatthias Ringwald     if (hfp_parser_is_buffer_empty(hfp_connection)) return;
9413deb3ec6SMatthias Ringwald 
942*a0ffb263SMatthias Ringwald     switch (hfp_connection->parser_state){
9433deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_HEADER: // header
9443deb3ec6SMatthias Ringwald             if (byte == '='){
945*a0ffb263SMatthias Ringwald                 hfp_connection->keep_byte = 1;
946*a0ffb263SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
9473deb3ec6SMatthias Ringwald                 return;
9483deb3ec6SMatthias Ringwald             }
9493deb3ec6SMatthias Ringwald 
9503deb3ec6SMatthias Ringwald             if (byte == '?'){
951*a0ffb263SMatthias Ringwald                 hfp_connection->keep_byte = 0;
952*a0ffb263SMatthias Ringwald                 hfp_parser_store_byte(hfp_connection, byte);
9533deb3ec6SMatthias Ringwald                 return;
9543deb3ec6SMatthias Ringwald             }
955ce263fc8SMatthias Ringwald 
956ce263fc8SMatthias Ringwald             if (byte == ','){
957*a0ffb263SMatthias Ringwald                 hfp_connection->resolve_byte = 1;
958ce263fc8SMatthias Ringwald             }
959ce263fc8SMatthias Ringwald 
960*a0ffb263SMatthias Ringwald             // printf(" parse header 2 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
961*a0ffb263SMatthias Ringwald             if (hfp_parser_is_end_of_header(byte) || hfp_connection->keep_byte == 1){
962*a0ffb263SMatthias Ringwald                 // printf(" parse header 3 %s, keep separator $ %d\n", hfp_connection->line_buffer, hfp_connection->keep_byte);
963*a0ffb263SMatthias Ringwald                 char * line_buffer = (char *)hfp_connection->line_buffer;
964*a0ffb263SMatthias Ringwald                 hfp_connection->command = parse_command(line_buffer, isHandsFree);
965aa4dd815SMatthias Ringwald 
966*a0ffb263SMatthias Ringwald                 /* resolve command name according to hfp_connection */
967*a0ffb263SMatthias Ringwald                 if (hfp_connection->command == HFP_CMD_UNKNOWN){
968*a0ffb263SMatthias Ringwald                     switch(hfp_connection->state){
969aa4dd815SMatthias Ringwald                         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
970*a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
971aa4dd815SMatthias Ringwald                             break;
972aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
973*a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
974aa4dd815SMatthias Ringwald                             break;
975aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
976*a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
977aa4dd815SMatthias Ringwald                             break;
978aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
979*a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
980aa4dd815SMatthias Ringwald                             break;
981aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INDICATORS:
982*a0ffb263SMatthias Ringwald                             hfp_connection->send_ag_indicators_segment = 0;
983*a0ffb263SMatthias Ringwald                             hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
984aa4dd815SMatthias Ringwald                             break;
985aa4dd815SMatthias Ringwald                         default:
986aa4dd815SMatthias Ringwald                             break;
987aa4dd815SMatthias Ringwald                     }
988aa4dd815SMatthias Ringwald                 }
9893deb3ec6SMatthias Ringwald             }
9903deb3ec6SMatthias Ringwald             break;
9913deb3ec6SMatthias Ringwald 
992ce263fc8SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
993*a0ffb263SMatthias Ringwald             parse_sequence(hfp_connection);
994ce263fc8SMatthias Ringwald             break;
995ce263fc8SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
996*a0ffb263SMatthias Ringwald             switch (hfp_connection->command){
997ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
998*a0ffb263SMatthias Ringwald                     log_info("format %s, ", hfp_connection->line_buffer);
999*a0ffb263SMatthias Ringwald                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1000ce263fc8SMatthias Ringwald                     break;
1001ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1002*a0ffb263SMatthias Ringwald                     log_info("format %s \n", hfp_connection->line_buffer);
1003*a0ffb263SMatthias Ringwald                     hfp_connection->network_operator.format =  atoi((char *)&hfp_connection->line_buffer[0]);
1004ce263fc8SMatthias Ringwald                     break;
1005ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1006ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1007ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1008*a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1009ce263fc8SMatthias Ringwald                     break;
1010ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1011*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1012*a0ffb263SMatthias Ringwald                     log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status);
1013*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1;
1014ce263fc8SMatthias Ringwald                     break;
1015ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1016*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = atoi((char *)hfp_connection->line_buffer);
1017*a0ffb263SMatthias Ringwald                     log_info("%s, ", hfp_connection->line_buffer);
1018ce263fc8SMatthias Ringwald                     break;
1019ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1020*a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1021*a0ffb263SMatthias Ringwald                 case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1022*a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1023ce263fc8SMatthias Ringwald                     break;
1024ce263fc8SMatthias Ringwald                 default:
1025ce263fc8SMatthias Ringwald                     break;
1026ce263fc8SMatthias Ringwald             }
1027ce263fc8SMatthias Ringwald             break;
1028ce263fc8SMatthias Ringwald 
1029ce263fc8SMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
1030*a0ffb263SMatthias Ringwald              switch (hfp_connection->command){
1031ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1032*a0ffb263SMatthias Ringwald                     strcpy(hfp_connection->network_operator.name, (char *)hfp_connection->line_buffer);
1033*a0ffb263SMatthias Ringwald                     log_info("name %s\n", hfp_connection->line_buffer);
1034ce263fc8SMatthias Ringwald                     break;
1035ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1036*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = atoi((char *)hfp_connection->line_buffer);
1037*a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index++;
1038*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index;
1039*a0ffb263SMatthias Ringwald                     log_info("%s)\n", hfp_connection->line_buffer);
1040ce263fc8SMatthias Ringwald                     break;
1041ce263fc8SMatthias Ringwald                 default:
1042ce263fc8SMatthias Ringwald                     break;
1043ce263fc8SMatthias Ringwald             }
1044ce263fc8SMatthias Ringwald             break;
1045ce263fc8SMatthias Ringwald     }
1046*a0ffb263SMatthias Ringwald     hfp_parser_next_state(hfp_connection, byte);
1047ce263fc8SMatthias Ringwald 
1048*a0ffb263SMatthias Ringwald     if (hfp_connection->resolve_byte && hfp_connection->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
1049*a0ffb263SMatthias Ringwald         hfp_connection->resolve_byte = 0;
1050*a0ffb263SMatthias Ringwald         hfp_connection->ignore_value = 1;
1051*a0ffb263SMatthias Ringwald         parse_sequence(hfp_connection);
1052*a0ffb263SMatthias Ringwald         hfp_connection->line_buffer[0] = 0;
1053*a0ffb263SMatthias Ringwald         hfp_connection->line_size = 0;
1054ce263fc8SMatthias Ringwald     }
1055ce263fc8SMatthias Ringwald }
1056ce263fc8SMatthias Ringwald 
1057*a0ffb263SMatthias Ringwald static void parse_sequence(hfp_connection_t * hfp_connection){
1058ce263fc8SMatthias Ringwald     int value;
1059*a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1060667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1061*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1062667ec068SMatthias Ringwald             int i;
1063*a0ffb263SMatthias Ringwald             switch (hfp_connection->parser_item_index){
1064667ec068SMatthias Ringwald                 case 0:
1065*a0ffb263SMatthias Ringwald                     for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){
1066*a0ffb263SMatthias Ringwald                         if (hfp_connection->generic_status_indicators[i].uuid == value){
1067*a0ffb263SMatthias Ringwald                             hfp_connection->parser_indicator_index = i;
1068667ec068SMatthias Ringwald                             break;
1069667ec068SMatthias Ringwald                         }
1070667ec068SMatthias Ringwald                     }
1071667ec068SMatthias Ringwald                     break;
1072667ec068SMatthias Ringwald                 case 1:
1073*a0ffb263SMatthias Ringwald                     if (hfp_connection->parser_indicator_index <0) break;
1074*a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value;
1075667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1076*a0ffb263SMatthias Ringwald                      hfp_connection->parser_item_index, value);
1077667ec068SMatthias Ringwald                     break;
1078667ec068SMatthias Ringwald                 default:
1079667ec068SMatthias Ringwald                     break;
1080667ec068SMatthias Ringwald             }
1081*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1082667ec068SMatthias Ringwald             break;
1083667ec068SMatthias Ringwald 
1084667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1085*a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1086667ec068SMatthias Ringwald                 case 0:
1087*a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1088*a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1089667ec068SMatthias Ringwald                     break;
1090667ec068SMatthias Ringwald                 case 1:
1091*a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1092*a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1093667ec068SMatthias Ringwald                     break;
1094667ec068SMatthias Ringwald                 default:
1095667ec068SMatthias Ringwald                     break;
1096667ec068SMatthias Ringwald             }
1097*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1098667ec068SMatthias Ringwald             break;
1099667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1100*a0ffb263SMatthias Ringwald             switch(hfp_connection->parser_item_index){
1101667ec068SMatthias Ringwald                 case 0:
1102*a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1103*a0ffb263SMatthias Ringwald                     hfp_connection->clcc_idx = value;
1104667ec068SMatthias Ringwald                     break;
1105667ec068SMatthias Ringwald                 case 1:
1106*a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1107*a0ffb263SMatthias Ringwald                     hfp_connection->clcc_dir = value;
1108667ec068SMatthias Ringwald                     break;
1109667ec068SMatthias Ringwald                 case 2:
1110*a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1111*a0ffb263SMatthias Ringwald                     hfp_connection->clcc_status = value;
1112667ec068SMatthias Ringwald                     break;
1113667ec068SMatthias Ringwald                 case 3:
1114*a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1115*a0ffb263SMatthias Ringwald                     hfp_connection->clcc_mpty = value;
1116667ec068SMatthias Ringwald                     break;
1117667ec068SMatthias Ringwald                 case 4:
1118*a0ffb263SMatthias Ringwald                     strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1119*a0ffb263SMatthias Ringwald                     hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
1120667ec068SMatthias Ringwald                     break;
1121667ec068SMatthias Ringwald                 case 5:
1122*a0ffb263SMatthias Ringwald                     value = atoi((char *)&hfp_connection->line_buffer[0]);
1123*a0ffb263SMatthias Ringwald                     hfp_connection->bnip_type = value;
1124667ec068SMatthias Ringwald                     break;
1125667ec068SMatthias Ringwald                 default:
1126667ec068SMatthias Ringwald                     break;
1127667ec068SMatthias Ringwald             }
1128*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1129667ec068SMatthias Ringwald             break;
1130aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1131*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1132*a0ffb263SMatthias Ringwald             hfp_connection->microphone_gain = value;
1133aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1134aa4dd815SMatthias Ringwald             break;
1135aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1136*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1137*a0ffb263SMatthias Ringwald             hfp_connection->speaker_gain = value;
1138aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1139aa4dd815SMatthias Ringwald             break;
1140aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1141*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1142*a0ffb263SMatthias Ringwald             hfp_connection->ag_activate_voice_recognition = value;
1143aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1144aa4dd815SMatthias Ringwald             break;
1145aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1146*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1147*a0ffb263SMatthias Ringwald             hfp_connection->ag_echo_and_noise_reduction = value;
1148aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1149aa4dd815SMatthias Ringwald             break;
1150aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1151*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1152*a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1153aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1154aa4dd815SMatthias Ringwald             break;
11553deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
1156*a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = atoi((char*)hfp_connection->line_buffer);
1157*a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed);
11583deb3ec6SMatthias Ringwald             break;
11593deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
1160*a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = atoi((char*)hfp_connection->line_buffer);
1161*a0ffb263SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec);
11623deb3ec6SMatthias Ringwald             break;
11633deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
1164*a0ffb263SMatthias Ringwald             hfp_connection->remote_supported_features = atoi((char*)hfp_connection->line_buffer);
1165*a0ffb263SMatthias Ringwald             log_info("Parsed supported feature %d\n", hfp_connection->remote_supported_features);
11663deb3ec6SMatthias Ringwald             break;
11673deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
1168*a0ffb263SMatthias Ringwald             log_info("Parsed codec %s\n", hfp_connection->line_buffer);
1169*a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1170*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1171*a0ffb263SMatthias Ringwald             hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index;
11723deb3ec6SMatthias Ringwald             break;
1173aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
1174*a0ffb263SMatthias Ringwald             strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name,  (char *)hfp_connection->line_buffer);
1175*a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1;
1176*a0ffb263SMatthias Ringwald             log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer);
1177aa4dd815SMatthias Ringwald             break;
1178aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1179*a0ffb263SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer);
1180*a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = atoi((char *) hfp_connection->line_buffer);
1181*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
11823deb3ec6SMatthias Ringwald             break;
11833deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1184*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1185*a0ffb263SMatthias Ringwald             if (hfp_connection->parser_item_index != 4) break;
1186*a0ffb263SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer);
1187*a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1188*a0ffb263SMatthias Ringwald             hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value;
11893deb3ec6SMatthias Ringwald             break;
11903deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
1191*a0ffb263SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer);
1192*a0ffb263SMatthias Ringwald             if (hfp_connection->line_size > 2 ) break;
1193*a0ffb263SMatthias Ringwald             strcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_nr].name,  (char *)hfp_connection->line_buffer);
1194*a0ffb263SMatthias Ringwald             hfp_connection->remote_call_services_nr++;
11953deb3ec6SMatthias Ringwald             break;
1196aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1197aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1198*a0ffb263SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer);
1199*a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)atoi((char*)hfp_connection->line_buffer);
1200*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
1201*a0ffb263SMatthias Ringwald             hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index;
12023deb3ec6SMatthias Ringwald             break;
1203aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
12043deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
1205*a0ffb263SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer);
1206*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
12073deb3ec6SMatthias Ringwald             break;
1208ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1209*a0ffb263SMatthias Ringwald             hfp_connection->parser_indicator_index = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1210*a0ffb263SMatthias Ringwald             log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index);
1211ce263fc8SMatthias Ringwald             break;
12123deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
12133deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1214*a0ffb263SMatthias Ringwald             if (hfp_connection->ignore_value){
1215*a0ffb263SMatthias Ringwald                 hfp_connection->ignore_value = 0;
1216*a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index,
1217*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled);
1218ce263fc8SMatthias Ringwald             }
1219*a0ffb263SMatthias Ringwald             else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){
1220ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1221*a0ffb263SMatthias Ringwald                     hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name);
1222ce263fc8SMatthias Ringwald             } else {
1223*a0ffb263SMatthias Ringwald                 value = atoi((char *)&hfp_connection->line_buffer[0]);
1224*a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value;
1225*a0ffb263SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index,
1226*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value);
12273deb3ec6SMatthias Ringwald             }
1228*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index++;
12293deb3ec6SMatthias Ringwald             break;
12303deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12313deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
1232*a0ffb263SMatthias Ringwald             hfp_connection->parser_item_index = atoi((char *)&hfp_connection->line_buffer[0]) - 1;
1233*a0ffb263SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index);
12343deb3ec6SMatthias Ringwald             break;
1235aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1236*a0ffb263SMatthias Ringwald             hfp_connection->network_operator.mode = atoi((char *)&hfp_connection->line_buffer[0]);
1237*a0ffb263SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode);
1238aa4dd815SMatthias Ringwald             break;
1239aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1240*a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[0] == '3'){
1241*a0ffb263SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer);
12423deb3ec6SMatthias Ringwald                 break;
12433deb3ec6SMatthias Ringwald             }
12443deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
1245*a0ffb263SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer);
12463deb3ec6SMatthias Ringwald             break;
12473deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
12483deb3ec6SMatthias Ringwald             break;
12493deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1250*a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 1;
1251*a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error_value = (uint8_t)atoi((char*)hfp_connection->line_buffer);
12523deb3ec6SMatthias Ringwald             break;
12533deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1254*a0ffb263SMatthias Ringwald             hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)hfp_connection->line_buffer);
1255*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1256*a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
12573deb3ec6SMatthias Ringwald             break;
1258ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1259*a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1260*a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1261*a0ffb263SMatthias Ringwald             strncpy(hfp_connection->bnip_number, (char *)hfp_connection->line_buffer, sizeof(hfp_connection->bnip_number));
1262*a0ffb263SMatthias Ringwald             hfp_connection->bnip_number[sizeof(hfp_connection->bnip_number)-1] = 0;
12633deb3ec6SMatthias Ringwald             break;
12643deb3ec6SMatthias Ringwald         default:
12653deb3ec6SMatthias Ringwald             break;
12663deb3ec6SMatthias Ringwald     }
12673deb3ec6SMatthias Ringwald }
12683deb3ec6SMatthias Ringwald 
12693deb3ec6SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){
1270*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = provide_hfp_connection_context_for_bd_addr(bd_addr);
1271*a0ffb263SMatthias Ringwald     log_info("hfp_connect %s, hfp_connection %p", bd_addr_to_str(bd_addr), hfp_connection);
12723deb3ec6SMatthias Ringwald 
1273*a0ffb263SMatthias Ringwald     if (!hfp_connection) {
12743deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
12753deb3ec6SMatthias Ringwald         return;
12763deb3ec6SMatthias Ringwald     }
1277aa4dd815SMatthias Ringwald 
1278*a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
12793deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1280*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
12813deb3ec6SMatthias Ringwald             return;
12823deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
1283*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
12843deb3ec6SMatthias Ringwald             return;
12853deb3ec6SMatthias Ringwald         case HFP_IDLE:
1286*a0ffb263SMatthias Ringwald             memcpy(hfp_connection->remote_addr, bd_addr, 6);
1287*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_SDP_EVENT_QUERY_COMPLETE;
1288*a0ffb263SMatthias Ringwald             connection_doing_sdp_query = hfp_connection;
1289*a0ffb263SMatthias Ringwald             hfp_connection->service_uuid = service_uuid;
1290*a0ffb263SMatthias Ringwald             sdp_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, hfp_connection->remote_addr, service_uuid);
12913deb3ec6SMatthias Ringwald             break;
12923deb3ec6SMatthias Ringwald         default:
12933deb3ec6SMatthias Ringwald             break;
12943deb3ec6SMatthias Ringwald     }
12953deb3ec6SMatthias Ringwald }
12963deb3ec6SMatthias Ringwald 
1297*a0ffb263SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * hfp_connection){
1298*a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1299*a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
13003deb3ec6SMatthias Ringwald 
1301*a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){
1302*a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_IDLE;
13033deb3ec6SMatthias Ringwald         return;
13043deb3ec6SMatthias Ringwald     }
13053deb3ec6SMatthias Ringwald 
1306*a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){
1307*a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
13083deb3ec6SMatthias Ringwald         return;
13093deb3ec6SMatthias Ringwald     }
13103deb3ec6SMatthias Ringwald 
1311*a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_CONNECTED){
1312*a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM;
13133deb3ec6SMatthias Ringwald         return;
13143deb3ec6SMatthias Ringwald     }
13153deb3ec6SMatthias Ringwald 
1316*a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){
1317*a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W2_DISCONNECT_SCO;
13183deb3ec6SMatthias Ringwald         return;
13193deb3ec6SMatthias Ringwald     }
13203deb3ec6SMatthias Ringwald 
13213deb3ec6SMatthias Ringwald     return;
13223deb3ec6SMatthias Ringwald }
13233deb3ec6SMatthias Ringwald 
1324*a0ffb263SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * hfp_connection){
1325*a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1326*a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1327*a0ffb263SMatthias Ringwald     hfp_connection->release_audio_connection = 1;
13283deb3ec6SMatthias Ringwald }
13293deb3ec6SMatthias Ringwald 
1330ce263fc8SMatthias Ringwald static const struct link_settings {
1331ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1332ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1333ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1334ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1335ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0,   HV1
1336ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1,   HV3
1337ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1,   EV3
1338ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1339ce263fc8SMatthias Ringwald     { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1340ce263fc8SMatthias Ringwald     { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1341ce263fc8SMatthias Ringwald     { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1,   EV3
1342ce263fc8SMatthias Ringwald     { 0x000d, 0x02, 0x0380 }  // HFP_LINK_SETTINGS_T2, 2-EV3
1343ce263fc8SMatthias Ringwald };
13443deb3ec6SMatthias Ringwald 
1345ce263fc8SMatthias Ringwald void hfp_setup_synchronous_connection(hci_con_handle_t handle, hfp_link_setttings_t setting){
1346ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1347ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1348ce263fc8SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1349ce263fc8SMatthias Ringwald         hci_get_sco_voice_setting(), hfp_link_settings[setting].retransmission_effort, hfp_link_settings[setting].packet_types); // all types 0x003f, only 2-ev3 0x380
1350ce263fc8SMatthias Ringwald }
1351