xref: /btstack/src/classic/hfp.c (revision f8fbdce0c5067e7e7edd3a29934b1f9b79c8ff2d)
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"
593edc84c5SMatthias Ringwald #include "classic/sdp_query_rfcomm.h"
603edc84c5SMatthias Ringwald #include "classic/sdp.h"
6116ece135SMatthias Ringwald #include "btstack_debug.h"
627bd8e93bSMatthias Ringwald #include "btstack_event.h"
633deb3ec6SMatthias Ringwald 
643deb3ec6SMatthias Ringwald #define HFP_HF_FEATURES_SIZE 10
653deb3ec6SMatthias Ringwald #define HFP_AG_FEATURES_SIZE 12
663deb3ec6SMatthias Ringwald 
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald static const char * hfp_hf_features[] = {
693deb3ec6SMatthias Ringwald     "EC and/or NR function",
703deb3ec6SMatthias Ringwald     "Three-way calling",
713deb3ec6SMatthias Ringwald     "CLI presentation capability",
723deb3ec6SMatthias Ringwald     "Voice recognition activation",
733deb3ec6SMatthias Ringwald     "Remote volume control",
743deb3ec6SMatthias Ringwald 
753deb3ec6SMatthias Ringwald     "Enhanced call status",
763deb3ec6SMatthias Ringwald     "Enhanced call control",
773deb3ec6SMatthias Ringwald 
783deb3ec6SMatthias Ringwald     "Codec negotiation",
793deb3ec6SMatthias Ringwald 
803deb3ec6SMatthias Ringwald     "HF Indicators",
813deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
823deb3ec6SMatthias Ringwald     "Reserved for future definition"
833deb3ec6SMatthias Ringwald };
843deb3ec6SMatthias Ringwald 
853deb3ec6SMatthias Ringwald static const char * hfp_ag_features[] = {
863deb3ec6SMatthias Ringwald     "Three-way calling",
873deb3ec6SMatthias Ringwald     "EC and/or NR function",
883deb3ec6SMatthias Ringwald     "Voice recognition function",
893deb3ec6SMatthias Ringwald     "In-band ring tone capability",
903deb3ec6SMatthias Ringwald     "Attach a number to a voice tag",
913deb3ec6SMatthias Ringwald     "Ability to reject a call",
923deb3ec6SMatthias Ringwald     "Enhanced call status",
933deb3ec6SMatthias Ringwald     "Enhanced call control",
943deb3ec6SMatthias Ringwald     "Extended Error Result Codes",
953deb3ec6SMatthias Ringwald     "Codec negotiation",
963deb3ec6SMatthias Ringwald     "HF Indicators",
973deb3ec6SMatthias Ringwald     "eSCO S4 (and T2) Settings Supported",
983deb3ec6SMatthias Ringwald     "Reserved for future definition"
993deb3ec6SMatthias Ringwald };
1003deb3ec6SMatthias Ringwald 
1013deb3ec6SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
1023deb3ec6SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
1033deb3ec6SMatthias Ringwald 
1048f2a52f4SMatthias Ringwald static btstack_linked_list_t hfp_connections = NULL;
105ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context);
1063deb3ec6SMatthias Ringwald 
1073deb3ec6SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void){
1083deb3ec6SMatthias Ringwald     return (hfp_generic_status_indicator_t *) &hfp_generic_status_indicators;
1093deb3ec6SMatthias Ringwald }
1103deb3ec6SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void){
1113deb3ec6SMatthias Ringwald     return hfp_generic_status_indicators_nr;
1123deb3ec6SMatthias Ringwald }
1133deb3ec6SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr){
1143deb3ec6SMatthias Ringwald     if (indicator_nr > HFP_MAX_NUM_HF_INDICATORS) return;
1153deb3ec6SMatthias Ringwald     hfp_generic_status_indicators_nr = indicator_nr;
1163deb3ec6SMatthias Ringwald     memcpy(hfp_generic_status_indicators, indicators, indicator_nr * sizeof(hfp_generic_status_indicator_t));
1173deb3ec6SMatthias Ringwald }
1183deb3ec6SMatthias Ringwald 
1193deb3ec6SMatthias Ringwald const char * hfp_hf_feature(int index){
1203deb3ec6SMatthias Ringwald     if (index > HFP_HF_FEATURES_SIZE){
1213deb3ec6SMatthias Ringwald         return hfp_hf_features[HFP_HF_FEATURES_SIZE];
1223deb3ec6SMatthias Ringwald     }
1233deb3ec6SMatthias Ringwald     return hfp_hf_features[index];
1243deb3ec6SMatthias Ringwald }
1253deb3ec6SMatthias Ringwald 
1263deb3ec6SMatthias Ringwald const char * hfp_ag_feature(int index){
1273deb3ec6SMatthias Ringwald     if (index > HFP_AG_FEATURES_SIZE){
1283deb3ec6SMatthias Ringwald         return hfp_ag_features[HFP_AG_FEATURES_SIZE];
1293deb3ec6SMatthias Ringwald     }
1303deb3ec6SMatthias Ringwald     return hfp_ag_features[index];
1313deb3ec6SMatthias Ringwald }
1323deb3ec6SMatthias Ringwald 
1333deb3ec6SMatthias Ringwald int send_str_over_rfcomm(uint16_t cid, char * command){
1343deb3ec6SMatthias Ringwald     if (!rfcomm_can_send_packet_now(cid)) return 1;
13574386ee0SMatthias Ringwald     log_info("HFP_TX %s", command);
13628190c0bSMatthias Ringwald     int err = rfcomm_send(cid, (uint8_t*) command, strlen(command));
1373deb3ec6SMatthias Ringwald     if (err){
13828190c0bSMatthias Ringwald         log_error("rfcomm_send -> error 0x%02x \n", err);
1393deb3ec6SMatthias Ringwald     }
1403deb3ec6SMatthias Ringwald     return 1;
1413deb3ec6SMatthias Ringwald }
1423deb3ec6SMatthias Ringwald 
1433deb3ec6SMatthias Ringwald #if 0
1443deb3ec6SMatthias Ringwald void hfp_set_codec(hfp_connection_t * context, uint8_t *packet, uint16_t size){
1453deb3ec6SMatthias Ringwald     // parse available codecs
1463deb3ec6SMatthias Ringwald     int pos = 0;
1473deb3ec6SMatthias Ringwald     int i;
1483deb3ec6SMatthias Ringwald     for (i=0; i<size; i++){
1493deb3ec6SMatthias Ringwald         pos+=8;
1503deb3ec6SMatthias Ringwald         if (packet[pos] > context->negotiated_codec){
1513deb3ec6SMatthias Ringwald             context->negotiated_codec = packet[pos];
1523deb3ec6SMatthias Ringwald         }
1533deb3ec6SMatthias Ringwald     }
1543deb3ec6SMatthias Ringwald     printf("Negotiated Codec 0x%02x\n", context->negotiated_codec);
1553deb3ec6SMatthias Ringwald }
1563deb3ec6SMatthias Ringwald #endif
1573deb3ec6SMatthias Ringwald 
1583deb3ec6SMatthias Ringwald // UTILS
1593deb3ec6SMatthias Ringwald int get_bit(uint16_t bitmap, int position){
1603deb3ec6SMatthias Ringwald     return (bitmap >> position) & 1;
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
1633deb3ec6SMatthias Ringwald int store_bit(uint32_t bitmap, int position, uint8_t value){
1643deb3ec6SMatthias Ringwald     if (value){
1653deb3ec6SMatthias Ringwald         bitmap |= 1 << position;
1663deb3ec6SMatthias Ringwald     } else {
1673deb3ec6SMatthias Ringwald         bitmap &= ~ (1 << position);
1683deb3ec6SMatthias Ringwald     }
1693deb3ec6SMatthias Ringwald     return bitmap;
1703deb3ec6SMatthias Ringwald }
1713deb3ec6SMatthias Ringwald 
1723deb3ec6SMatthias Ringwald int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){
1733deb3ec6SMatthias Ringwald     if (buffer_size < values_nr * 3) return 0;
1743deb3ec6SMatthias Ringwald     int i;
1753deb3ec6SMatthias Ringwald     int offset = 0;
1763deb3ec6SMatthias Ringwald     for (i = 0; i < values_nr-1; i++) {
1773deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer
1783deb3ec6SMatthias Ringwald     }
1793deb3ec6SMatthias Ringwald     if (i<values_nr){
1803deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]);
1813deb3ec6SMatthias Ringwald     }
1823deb3ec6SMatthias Ringwald     return offset;
1833deb3ec6SMatthias Ringwald }
1843deb3ec6SMatthias Ringwald 
1853deb3ec6SMatthias Ringwald int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){
1863deb3ec6SMatthias Ringwald     if (buffer_size < values_nr * 3) return 0;
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald     int i;
1893deb3ec6SMatthias Ringwald     int offset = 0;
1903deb3ec6SMatthias Ringwald     for (i = 0; i < values_nr-1; i++) {
1913deb3ec6SMatthias Ringwald       offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer
1923deb3ec6SMatthias Ringwald     }
1933deb3ec6SMatthias Ringwald 
1943deb3ec6SMatthias Ringwald     if (i<values_nr){
1953deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i));
1963deb3ec6SMatthias Ringwald     }
1973deb3ec6SMatthias Ringwald     return offset;
1983deb3ec6SMatthias Ringwald }
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald void hfp_emit_event(hfp_callback_t callback, uint8_t event_subtype, uint8_t value){
2013deb3ec6SMatthias Ringwald     if (!callback) return;
2023deb3ec6SMatthias Ringwald     uint8_t event[4];
2033deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
2043deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
2053deb3ec6SMatthias Ringwald     event[2] = event_subtype;
2063deb3ec6SMatthias Ringwald     event[3] = value; // status 0 == OK
2073deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
210aa4dd815SMatthias Ringwald void hfp_emit_string_event(hfp_callback_t callback, uint8_t event_subtype, const char * value){
211aa4dd815SMatthias Ringwald     if (!callback) return;
212c1797c7dSMatthias Ringwald     uint8_t event[40];
213aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
214aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
215aa4dd815SMatthias Ringwald     event[2] = event_subtype;
216aa4dd815SMatthias Ringwald     int size = (strlen(value) < sizeof(event) - 4) ? strlen(value) : sizeof(event) - 4;
217aa4dd815SMatthias Ringwald     strncpy((char*)&event[3], value, size);
218aa4dd815SMatthias Ringwald     event[3 + size] = 0;
219aa4dd815SMatthias Ringwald     (*callback)(event, sizeof(event));
220aa4dd815SMatthias Ringwald }
221aa4dd815SMatthias Ringwald 
222aa4dd815SMatthias Ringwald static void hfp_emit_audio_connection_established_event(hfp_callback_t callback, uint8_t value, uint16_t sco_handle){
223aa4dd815SMatthias Ringwald     if (!callback) return;
224aa4dd815SMatthias Ringwald     uint8_t event[6];
225aa4dd815SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
226aa4dd815SMatthias Ringwald     event[1] = sizeof(event) - 2;
227aa4dd815SMatthias Ringwald     event[2] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED;
228aa4dd815SMatthias Ringwald     event[3] = value; // status 0 == OK
229*f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, sco_handle);
230aa4dd815SMatthias Ringwald     (*callback)(event, sizeof(event));
231aa4dd815SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald 
2338f2a52f4SMatthias Ringwald btstack_linked_list_t * hfp_get_connections(){
2348f2a52f4SMatthias Ringwald     return (btstack_linked_list_t *) &hfp_connections;
2353deb3ec6SMatthias Ringwald }
2363deb3ec6SMatthias Ringwald 
2373deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){
238665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
239665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
240665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
241665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2423deb3ec6SMatthias Ringwald         if (connection->rfcomm_cid == cid){
2433deb3ec6SMatthias Ringwald             return connection;
2443deb3ec6SMatthias Ringwald         }
2453deb3ec6SMatthias Ringwald     }
2463deb3ec6SMatthias Ringwald     return NULL;
2473deb3ec6SMatthias Ringwald }
2483deb3ec6SMatthias Ringwald 
2493deb3ec6SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
250665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
251665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
252665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
253665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2543deb3ec6SMatthias Ringwald         if (memcmp(connection->remote_addr, bd_addr, 6) == 0) {
2553deb3ec6SMatthias Ringwald             return connection;
2563deb3ec6SMatthias Ringwald         }
2573deb3ec6SMatthias Ringwald     }
2583deb3ec6SMatthias Ringwald     return NULL;
2593deb3ec6SMatthias Ringwald }
2603deb3ec6SMatthias Ringwald 
261aa4dd815SMatthias Ringwald hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle){
262665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
263665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
264665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
265665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
266aa4dd815SMatthias Ringwald         if (connection->sco_handle == handle){
2673deb3ec6SMatthias Ringwald             return connection;
2683deb3ec6SMatthias Ringwald         }
2693deb3ec6SMatthias Ringwald     }
2703deb3ec6SMatthias Ringwald     return NULL;
2713deb3ec6SMatthias Ringwald }
2723deb3ec6SMatthias Ringwald 
2733deb3ec6SMatthias Ringwald void hfp_reset_context_flags(hfp_connection_t * context){
2743deb3ec6SMatthias Ringwald     if (!context) return;
275aa4dd815SMatthias Ringwald     context->ok_pending = 0;
2763deb3ec6SMatthias Ringwald     context->send_error = 0;
2773deb3ec6SMatthias Ringwald 
278ce263fc8SMatthias Ringwald     context->keep_byte = 0;
2793deb3ec6SMatthias Ringwald 
2803deb3ec6SMatthias Ringwald     context->change_status_update_for_individual_ag_indicators = 0;
2813deb3ec6SMatthias Ringwald     context->operator_name_changed = 0;
2823deb3ec6SMatthias Ringwald 
2833deb3ec6SMatthias Ringwald     context->enable_extended_audio_gateway_error_report = 0;
2843deb3ec6SMatthias Ringwald     context->extended_audio_gateway_error = 0;
2853deb3ec6SMatthias Ringwald 
2863deb3ec6SMatthias Ringwald     // establish codecs connection
2873deb3ec6SMatthias Ringwald     context->suggested_codec = 0;
2883deb3ec6SMatthias Ringwald     context->negotiated_codec = 0;
2893deb3ec6SMatthias Ringwald     context->codec_confirmed = 0;
2903deb3ec6SMatthias Ringwald 
2913deb3ec6SMatthias Ringwald     context->establish_audio_connection = 0;
2923deb3ec6SMatthias Ringwald }
2933deb3ec6SMatthias Ringwald 
2943deb3ec6SMatthias Ringwald static hfp_connection_t * create_hfp_connection_context(){
2953deb3ec6SMatthias Ringwald     hfp_connection_t * context = btstack_memory_hfp_connection_get();
2963deb3ec6SMatthias Ringwald     if (!context) return NULL;
2973deb3ec6SMatthias Ringwald     // init state
2983deb3ec6SMatthias Ringwald     memset(context,0, sizeof(hfp_connection_t));
2993deb3ec6SMatthias Ringwald 
3003deb3ec6SMatthias Ringwald     context->state = HFP_IDLE;
301aa4dd815SMatthias Ringwald     context->call_state = HFP_CALL_IDLE;
302aa4dd815SMatthias Ringwald     context->codecs_state = HFP_CODECS_IDLE;
303aa4dd815SMatthias Ringwald 
3043deb3ec6SMatthias Ringwald     context->parser_state = HFP_PARSER_CMD_HEADER;
3053deb3ec6SMatthias Ringwald     context->command = HFP_CMD_NONE;
3063deb3ec6SMatthias Ringwald     context->negotiated_codec = 0;
3073deb3ec6SMatthias Ringwald 
3083deb3ec6SMatthias Ringwald     context->enable_status_update_for_ag_indicators = 0xFF;
3093deb3ec6SMatthias Ringwald 
3103deb3ec6SMatthias Ringwald     context->generic_status_indicators_nr = hfp_generic_status_indicators_nr;
3113deb3ec6SMatthias Ringwald     memcpy(context->generic_status_indicators, hfp_generic_status_indicators, hfp_generic_status_indicators_nr * sizeof(hfp_generic_status_indicator_t));
3123deb3ec6SMatthias Ringwald 
313665d90f2SMatthias Ringwald     btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)context);
3143deb3ec6SMatthias Ringwald     return context;
3153deb3ec6SMatthias Ringwald }
3163deb3ec6SMatthias Ringwald 
3173deb3ec6SMatthias Ringwald static void remove_hfp_connection_context(hfp_connection_t * context){
318665d90f2SMatthias Ringwald     btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*)context);
3193deb3ec6SMatthias Ringwald }
3203deb3ec6SMatthias Ringwald 
3213deb3ec6SMatthias Ringwald static hfp_connection_t * provide_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr){
3223deb3ec6SMatthias Ringwald     hfp_connection_t * context = get_hfp_connection_context_for_bd_addr(bd_addr);
3233deb3ec6SMatthias Ringwald     if (context) return  context;
3243deb3ec6SMatthias Ringwald     context = create_hfp_connection_context();
325aa4dd815SMatthias Ringwald     printf("created context for address %s\n", bd_addr_to_str(bd_addr));
3263deb3ec6SMatthias Ringwald     memcpy(context->remote_addr, bd_addr, 6);
3273deb3ec6SMatthias Ringwald     return context;
3283deb3ec6SMatthias Ringwald }
3293deb3ec6SMatthias Ringwald 
330aa4dd815SMatthias Ringwald /* @param network.
331aa4dd815SMatthias Ringwald  * 0 == no ability to reject a call.
332aa4dd815SMatthias Ringwald  * 1 == ability to reject a call.
333aa4dd815SMatthias Ringwald  */
3343deb3ec6SMatthias Ringwald 
3353deb3ec6SMatthias Ringwald /* @param suported_features
3363deb3ec6SMatthias Ringwald  * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no)
3373deb3ec6SMatthias Ringwald  * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no)
3383deb3ec6SMatthias Ringwald  * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no)
3393deb3ec6SMatthias Ringwald  * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no)
3403deb3ec6SMatthias Ringwald  * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no)
3413deb3ec6SMatthias Ringwald  * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
3423deb3ec6SMatthias Ringwald  */
3433deb3ec6SMatthias Ringwald  /* Bit position:
3443deb3ec6SMatthias Ringwald  * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no)
3453deb3ec6SMatthias Ringwald  * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no)
3463deb3ec6SMatthias Ringwald  * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no)
3473deb3ec6SMatthias Ringwald  * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no)
3483deb3ec6SMatthias Ringwald  * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no)
3493deb3ec6SMatthias Ringwald  * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no)
3503deb3ec6SMatthias Ringwald  */
3513deb3ec6SMatthias Ringwald 
3529b1c3b4dSMatthias 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){
3533deb3ec6SMatthias Ringwald     uint8_t* attribute;
3543deb3ec6SMatthias Ringwald     de_create_sequence(service);
3553deb3ec6SMatthias Ringwald 
3563deb3ec6SMatthias Ringwald     // 0x0000 "Service Record Handle"
3573deb3ec6SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
3589b1c3b4dSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
3593deb3ec6SMatthias Ringwald 
3603deb3ec6SMatthias Ringwald     // 0x0001 "Service Class ID List"
3613deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
3623deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3633deb3ec6SMatthias Ringwald     {
3643deb3ec6SMatthias Ringwald         //  "UUID for Service"
3653deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid);
3663deb3ec6SMatthias Ringwald         de_add_number(attribute, DE_UUID, DE_SIZE_16, SDP_GenericAudio);
3673deb3ec6SMatthias Ringwald     }
3683deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
3693deb3ec6SMatthias Ringwald 
3703deb3ec6SMatthias Ringwald     // 0x0004 "Protocol Descriptor List"
3713deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_ProtocolDescriptorList);
3723deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3733deb3ec6SMatthias Ringwald     {
3743deb3ec6SMatthias Ringwald         uint8_t* l2cpProtocol = de_push_sequence(attribute);
3753deb3ec6SMatthias Ringwald         {
3763deb3ec6SMatthias Ringwald             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, SDP_L2CAPProtocol);
3773deb3ec6SMatthias Ringwald         }
3783deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, l2cpProtocol);
3793deb3ec6SMatthias Ringwald 
3803deb3ec6SMatthias Ringwald         uint8_t* rfcomm = de_push_sequence(attribute);
3813deb3ec6SMatthias Ringwald         {
3823deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UUID, DE_SIZE_16, SDP_RFCOMMProtocol);  // rfcomm_service
3833deb3ec6SMatthias Ringwald             de_add_number(rfcomm,  DE_UINT, DE_SIZE_8,  rfcomm_channel_nr);  // rfcomm channel
3843deb3ec6SMatthias Ringwald         }
3853deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, rfcomm);
3863deb3ec6SMatthias Ringwald     }
3873deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
3883deb3ec6SMatthias Ringwald 
3893deb3ec6SMatthias Ringwald 
3903deb3ec6SMatthias Ringwald     // 0x0005 "Public Browse Group"
3913deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BrowseGroupList); // public browse group
3923deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
3933deb3ec6SMatthias Ringwald     {
3943deb3ec6SMatthias Ringwald         de_add_number(attribute,  DE_UUID, DE_SIZE_16, SDP_PublicBrowseGroup);
3953deb3ec6SMatthias Ringwald     }
3963deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
3973deb3ec6SMatthias Ringwald 
3983deb3ec6SMatthias Ringwald     // 0x0009 "Bluetooth Profile Descriptor List"
3993deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, SDP_BluetoothProfileDescriptorList);
4003deb3ec6SMatthias Ringwald     attribute = de_push_sequence(service);
4013deb3ec6SMatthias Ringwald     {
4023deb3ec6SMatthias Ringwald         uint8_t *sppProfile = de_push_sequence(attribute);
4033deb3ec6SMatthias Ringwald         {
4043deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UUID, DE_SIZE_16, SDP_Handsfree);
4053deb3ec6SMatthias Ringwald             de_add_number(sppProfile,  DE_UINT, DE_SIZE_16, 0x0107); // Verision 1.7
4063deb3ec6SMatthias Ringwald         }
4073deb3ec6SMatthias Ringwald         de_pop_sequence(attribute, sppProfile);
4083deb3ec6SMatthias Ringwald     }
4093deb3ec6SMatthias Ringwald     de_pop_sequence(service, attribute);
4103deb3ec6SMatthias Ringwald 
4113deb3ec6SMatthias Ringwald     // 0x0100 "Service Name"
4123deb3ec6SMatthias Ringwald     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
4133deb3ec6SMatthias Ringwald     de_add_data(service,  DE_STRING, strlen(name), (uint8_t *) name);
4143deb3ec6SMatthias Ringwald }
4153deb3ec6SMatthias Ringwald 
4163deb3ec6SMatthias Ringwald static hfp_connection_t * connection_doing_sdp_query = NULL;
417a8747467SMatthias Ringwald 
418a8747467SMatthias Ringwald static void handle_query_rfcomm_event(uint8_t packet_type, uint8_t *packet, uint16_t size, void * context){
4193deb3ec6SMatthias Ringwald     hfp_connection_t * connection = connection_doing_sdp_query;
4203deb3ec6SMatthias Ringwald 
4215611a760SMatthias Ringwald     if ( connection->state != HFP_W4_SDP_EVENT_QUERY_COMPLETE) return;
4223deb3ec6SMatthias Ringwald 
423a8747467SMatthias Ringwald     switch (packet[0]){
4245611a760SMatthias Ringwald         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
4253deb3ec6SMatthias Ringwald             if (!connection) {
426a8747467SMatthias Ringwald                 log_error("handle_query_rfcomm_event alloc connection for RFCOMM port %u failed", sdp_query_rfcomm_service_event_get_rfcomm_channel(packet));
4273deb3ec6SMatthias Ringwald                 return;
4283deb3ec6SMatthias Ringwald             }
429a8747467SMatthias Ringwald             connection->rfcomm_channel_nr = sdp_query_rfcomm_service_event_get_rfcomm_channel(packet);
4303deb3ec6SMatthias Ringwald             break;
4315611a760SMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
4323deb3ec6SMatthias Ringwald             connection_doing_sdp_query = NULL;
4333deb3ec6SMatthias Ringwald             if (connection->rfcomm_channel_nr > 0){
4343deb3ec6SMatthias Ringwald                 connection->state = HFP_W4_RFCOMM_CONNECTED;
4355611a760SMatthias Ringwald                 log_info("HFP: SDP_EVENT_QUERY_COMPLETE context %p, addr %s, state %d", connection, bd_addr_to_str( connection->remote_addr),  connection->state);
436ea832403SMatthias Ringwald                 rfcomm_create_channel(connection->remote_addr, connection->rfcomm_channel_nr, NULL);
4373deb3ec6SMatthias Ringwald                 break;
4383deb3ec6SMatthias Ringwald             }
439a8747467SMatthias Ringwald             log_info("rfcomm service not found, status %u.", sdp_query_complete_event_get_status(packet));
4403deb3ec6SMatthias Ringwald             break;
4413deb3ec6SMatthias Ringwald         default:
4423deb3ec6SMatthias Ringwald             break;
4433deb3ec6SMatthias Ringwald     }
4443deb3ec6SMatthias Ringwald }
4453deb3ec6SMatthias Ringwald 
4463deb3ec6SMatthias Ringwald void hfp_handle_hci_event(hfp_callback_t callback, uint8_t packet_type, uint8_t *packet, uint16_t size){
4473deb3ec6SMatthias Ringwald     bd_addr_t event_addr;
4483deb3ec6SMatthias Ringwald     uint16_t rfcomm_cid, handle;
4493deb3ec6SMatthias Ringwald     hfp_connection_t * context = NULL;
4503deb3ec6SMatthias Ringwald 
451aa4dd815SMatthias Ringwald     // printf("AG packet_handler type %u, packet[0] %x, size %u\n", packet_type, packet[0], size);
452aa4dd815SMatthias Ringwald 
4533deb3ec6SMatthias Ringwald     switch (packet[0]) {
4543deb3ec6SMatthias Ringwald         case BTSTACK_EVENT_STATE:
4553deb3ec6SMatthias Ringwald             // bt stack activated, get started
4563deb3ec6SMatthias Ringwald             if (packet[2] == HCI_STATE_WORKING){
4573deb3ec6SMatthias Ringwald                 printf("BTstack activated, get started .\n");
4583deb3ec6SMatthias Ringwald             }
4593deb3ec6SMatthias Ringwald             break;
4603deb3ec6SMatthias Ringwald 
4613deb3ec6SMatthias Ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
4623deb3ec6SMatthias Ringwald             // inform about pin code request
4633deb3ec6SMatthias Ringwald             printf("Pin code request - using '0000'\n\r");
4643deb3ec6SMatthias Ringwald             bt_flip_addr(event_addr, &packet[2]);
4653deb3ec6SMatthias Ringwald             hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000");
4663deb3ec6SMatthias Ringwald             break;
4673deb3ec6SMatthias Ringwald 
4683deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_INCOMING_CONNECTION:
4693deb3ec6SMatthias Ringwald             // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
4703deb3ec6SMatthias Ringwald             bt_flip_addr(event_addr, &packet[2]);
471aa4dd815SMatthias Ringwald             context = provide_hfp_connection_context_for_bd_addr(event_addr);
4723deb3ec6SMatthias Ringwald 
4733deb3ec6SMatthias Ringwald             if (!context || context->state != HFP_IDLE) return;
4743deb3ec6SMatthias Ringwald 
475*f8fbdce0SMatthias Ringwald             context->rfcomm_cid = little_endian_read_16(packet, 9);
4763deb3ec6SMatthias Ringwald             context->state = HFP_W4_RFCOMM_CONNECTED;
4773deb3ec6SMatthias Ringwald             printf("RFCOMM channel %u requested for %s\n", context->rfcomm_cid, bd_addr_to_str(context->remote_addr));
47828190c0bSMatthias Ringwald             rfcomm_accept_connection(context->rfcomm_cid);
4793deb3ec6SMatthias Ringwald             break;
4803deb3ec6SMatthias Ringwald 
4813deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
4823deb3ec6SMatthias Ringwald             // data: event(8), len(8), status (8), address (48), handle(16), server channel(8), rfcomm_cid(16), max frame size(16)
483aa4dd815SMatthias Ringwald             printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE packet_handler type %u, packet[0] %x, size %u\n", packet_type, packet[0], size);
484aa4dd815SMatthias Ringwald 
4853deb3ec6SMatthias Ringwald             bt_flip_addr(event_addr, &packet[3]);
4863deb3ec6SMatthias Ringwald             context = get_hfp_connection_context_for_bd_addr(event_addr);
4873deb3ec6SMatthias Ringwald             if (!context || context->state != HFP_W4_RFCOMM_CONNECTED) return;
4883deb3ec6SMatthias Ringwald 
4893deb3ec6SMatthias Ringwald             if (packet[2]) {
4903deb3ec6SMatthias Ringwald                 hfp_emit_event(callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, packet[2]);
4913deb3ec6SMatthias Ringwald                 remove_hfp_connection_context(context);
4923deb3ec6SMatthias Ringwald             } else {
493*f8fbdce0SMatthias Ringwald                 context->con_handle = little_endian_read_16(packet, 9);
494aa4dd815SMatthias Ringwald                 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE con_handle 0x%02x\n", context->con_handle);
495aa4dd815SMatthias Ringwald 
496*f8fbdce0SMatthias Ringwald                 context->rfcomm_cid = little_endian_read_16(packet, 12);
497*f8fbdce0SMatthias Ringwald                 uint16_t mtu = little_endian_read_16(packet, 14);
4983deb3ec6SMatthias Ringwald                 printf("RFCOMM channel open succeeded. Context %p, RFCOMM Channel ID 0x%02x, max frame size %u\n", context, context->rfcomm_cid, mtu);
4993deb3ec6SMatthias Ringwald 
5003deb3ec6SMatthias Ringwald                 switch (context->state){
5013deb3ec6SMatthias Ringwald                     case HFP_W4_RFCOMM_CONNECTED:
5023deb3ec6SMatthias Ringwald                         context->state = HFP_EXCHANGE_SUPPORTED_FEATURES;
5033deb3ec6SMatthias Ringwald                         break;
5043deb3ec6SMatthias Ringwald                     case HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN:
5053deb3ec6SMatthias Ringwald                         context->state = HFP_W2_DISCONNECT_RFCOMM;
5063deb3ec6SMatthias Ringwald                         printf("Shutting down RFCOMM.\n");
5073deb3ec6SMatthias Ringwald                         break;
5083deb3ec6SMatthias Ringwald                     default:
5093deb3ec6SMatthias Ringwald                         break;
5103deb3ec6SMatthias Ringwald                 }
511aa4dd815SMatthias Ringwald                 // forward event to app, to learn about con_handle
512aa4dd815SMatthias Ringwald                 (*callback)(packet, size);
5133deb3ec6SMatthias Ringwald             }
5143deb3ec6SMatthias Ringwald             break;
5153deb3ec6SMatthias Ringwald 
5163deb3ec6SMatthias Ringwald         case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{
517ce263fc8SMatthias Ringwald 
518aa4dd815SMatthias Ringwald             bt_flip_addr(event_addr, &packet[5]);
5193deb3ec6SMatthias Ringwald             int index = 2;
5203deb3ec6SMatthias Ringwald             uint8_t status = packet[index++];
521aa4dd815SMatthias Ringwald 
522aa4dd815SMatthias Ringwald             if (status != 0){
523ce263fc8SMatthias Ringwald                 log_error("(e)SCO Connection failed status %u", status);
524ce263fc8SMatthias Ringwald                 // if outgoing && link_setting != d0 && appropriate error
525ce263fc8SMatthias Ringwald                 if (status != 0x11 && status != 0x1f) break;  // invalid params / unspecified error
526ce263fc8SMatthias Ringwald                 context = get_hfp_connection_context_for_bd_addr(event_addr);
527ce263fc8SMatthias Ringwald                 if (!context) break;
528ce263fc8SMatthias Ringwald                 switch (context->link_setting){
529ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_D0:
530ce263fc8SMatthias Ringwald                         return; // no other option left
531ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_D1:
532ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_D0;
533ce263fc8SMatthias Ringwald                         // break;
534ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S1:
535ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_D1;
536ce263fc8SMatthias Ringwald                         // break;
537ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S2:
538ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S3:
539ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_S4:
540ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_S1;
541ce263fc8SMatthias Ringwald                         // break;
542ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_T1:
543ce263fc8SMatthias Ringwald                     case HFP_LINK_SETTINGS_T2:
544ce263fc8SMatthias Ringwald                         // context->link_setting = HFP_LINK_SETTINGS_S3;
545ce263fc8SMatthias Ringwald                         context->link_setting = HFP_LINK_SETTINGS_D0;
546ce263fc8SMatthias Ringwald                         break;
547ce263fc8SMatthias Ringwald                 }
548ce263fc8SMatthias Ringwald                 context->establish_audio_connection = 1;
549aa4dd815SMatthias Ringwald                 break;
550aa4dd815SMatthias Ringwald             }
551aa4dd815SMatthias Ringwald 
552*f8fbdce0SMatthias Ringwald             uint16_t sco_handle = little_endian_read_16(packet, index);
5533deb3ec6SMatthias Ringwald             index+=2;
554aa4dd815SMatthias Ringwald 
555aa4dd815SMatthias Ringwald             bt_flip_addr(event_addr, &packet[index]);
5563deb3ec6SMatthias Ringwald             index+=6;
557aa4dd815SMatthias Ringwald 
5583deb3ec6SMatthias Ringwald             uint8_t link_type = packet[index++];
5593deb3ec6SMatthias Ringwald             uint8_t transmission_interval = packet[index++];  // measured in slots
5603deb3ec6SMatthias Ringwald             uint8_t retransmission_interval = packet[index++];// measured in slots
561*f8fbdce0SMatthias Ringwald             uint16_t rx_packet_length = little_endian_read_16(packet, index); // measured in bytes
5623deb3ec6SMatthias Ringwald             index+=2;
563*f8fbdce0SMatthias Ringwald             uint16_t tx_packet_length = little_endian_read_16(packet, index); // measured in bytes
5643deb3ec6SMatthias Ringwald             index+=2;
5653deb3ec6SMatthias Ringwald             uint8_t air_mode = packet[index];
5663deb3ec6SMatthias Ringwald 
5673deb3ec6SMatthias Ringwald             switch (link_type){
5683deb3ec6SMatthias Ringwald                 case 0x00:
569aa4dd815SMatthias Ringwald                     log_info("SCO Connection established.");
5703deb3ec6SMatthias Ringwald                     if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval);
5713deb3ec6SMatthias Ringwald                     if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval);
5723deb3ec6SMatthias Ringwald                     if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length);
5733deb3ec6SMatthias Ringwald                     if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length);
5743deb3ec6SMatthias Ringwald                     break;
5753deb3ec6SMatthias Ringwald                 case 0x02:
576aa4dd815SMatthias Ringwald                     log_info("eSCO Connection established. \n");
5773deb3ec6SMatthias Ringwald                     break;
5783deb3ec6SMatthias Ringwald                 default:
5793deb3ec6SMatthias Ringwald                     log_error("(e)SCO reserved link_type 0x%2x", link_type);
5803deb3ec6SMatthias Ringwald                     break;
5813deb3ec6SMatthias Ringwald             }
5823deb3ec6SMatthias Ringwald             log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, "
583aa4dd815SMatthias Ringwald                  " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle,
584aa4dd815SMatthias Ringwald                  bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, air_mode);
5853deb3ec6SMatthias Ringwald 
586aa4dd815SMatthias Ringwald             context = get_hfp_connection_context_for_bd_addr(event_addr);
5873deb3ec6SMatthias Ringwald 
588aa4dd815SMatthias Ringwald             if (!context) {
589aa4dd815SMatthias Ringwald                 log_error("SCO link created, context for address %s not found.", bd_addr_to_str(event_addr));
5903deb3ec6SMatthias Ringwald                 break;
5913deb3ec6SMatthias Ringwald             }
5923deb3ec6SMatthias Ringwald 
593aa4dd815SMatthias Ringwald             if (context->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){
594aa4dd815SMatthias Ringwald                 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN");
595aa4dd815SMatthias Ringwald                 context->state = HFP_W2_DISCONNECT_SCO;
596aa4dd815SMatthias Ringwald                 break;
597aa4dd815SMatthias Ringwald             }
5983deb3ec6SMatthias Ringwald             context->sco_handle = sco_handle;
599aa4dd815SMatthias Ringwald             context->establish_audio_connection = 0;
6003deb3ec6SMatthias Ringwald             context->state = HFP_AUDIO_CONNECTION_ESTABLISHED;
601aa4dd815SMatthias Ringwald             hfp_emit_audio_connection_established_event(callback, packet[2], sco_handle);
6023deb3ec6SMatthias Ringwald             break;
6033deb3ec6SMatthias Ringwald         }
6043deb3ec6SMatthias Ringwald 
6053deb3ec6SMatthias Ringwald         case RFCOMM_EVENT_CHANNEL_CLOSED:
606*f8fbdce0SMatthias Ringwald             rfcomm_cid = little_endian_read_16(packet,2);
6073deb3ec6SMatthias Ringwald             context = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
6083deb3ec6SMatthias Ringwald             if (!context) break;
6093deb3ec6SMatthias Ringwald             if (context->state == HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART){
6103deb3ec6SMatthias Ringwald                 context->state = HFP_IDLE;
6113deb3ec6SMatthias Ringwald                 hfp_establish_service_level_connection(context->remote_addr, context->service_uuid);
6123deb3ec6SMatthias Ringwald                 break;
6133deb3ec6SMatthias Ringwald             }
6143deb3ec6SMatthias Ringwald 
6153deb3ec6SMatthias Ringwald             hfp_emit_event(callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0);
6163deb3ec6SMatthias Ringwald             remove_hfp_connection_context(context);
6173deb3ec6SMatthias Ringwald             break;
6183deb3ec6SMatthias Ringwald 
6193deb3ec6SMatthias Ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
620*f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet,3);
621aa4dd815SMatthias Ringwald             context = get_hfp_connection_context_for_sco_handle(handle);
622aa4dd815SMatthias Ringwald 
6233deb3ec6SMatthias Ringwald             if (!context) break;
624aa4dd815SMatthias Ringwald 
625aa4dd815SMatthias Ringwald             if (context->state != HFP_W4_SCO_DISCONNECTED){
626aa4dd815SMatthias Ringwald                 log_info("Received gap disconnect in wrong hfp state");
627aa4dd815SMatthias Ringwald             }
628aa4dd815SMatthias Ringwald             log_info("Check SCO handle: incoming 0x%02x, context 0x%02x\n", handle,context->sco_handle);
629aa4dd815SMatthias Ringwald 
630aa4dd815SMatthias Ringwald             if (handle == context->sco_handle){
631aa4dd815SMatthias Ringwald                 log_info("SCO disconnected, w2 disconnect RFCOMM\n");
632aa4dd815SMatthias Ringwald                 context->sco_handle = 0;
633aa4dd815SMatthias Ringwald                 context->release_audio_connection = 0;
634aa4dd815SMatthias Ringwald                 context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
635aa4dd815SMatthias Ringwald                 hfp_emit_event(callback, HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED, 0);
6363deb3ec6SMatthias Ringwald                 break;
6373deb3ec6SMatthias Ringwald             }
6383deb3ec6SMatthias Ringwald             break;
6393deb3ec6SMatthias Ringwald 
640ce263fc8SMatthias Ringwald         case HCI_EVENT_INQUIRY_RESULT:
641ce263fc8SMatthias Ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
642ce263fc8SMatthias Ringwald         case HCI_EVENT_INQUIRY_COMPLETE:
643ce263fc8SMatthias Ringwald         case BTSTACK_EVENT_REMOTE_NAME_CACHED:
644ce263fc8SMatthias Ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
645ce263fc8SMatthias Ringwald             // forward inquiry events to app - TODO: replace with new event handler architecture
646ce263fc8SMatthias Ringwald             (*callback)(packet, size);
6473deb3ec6SMatthias Ringwald             break;
6483deb3ec6SMatthias Ringwald     }
6493deb3ec6SMatthias Ringwald }
6503deb3ec6SMatthias Ringwald 
651aa4dd815SMatthias Ringwald // translates command string into hfp_command_t CMD
652aa4dd815SMatthias Ringwald static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){
653aa4dd815SMatthias Ringwald     int offset = isHandsFree ? 0 : 2;
6543deb3ec6SMatthias Ringwald 
655ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_LIST_CURRENT_CALLS, strlen(HFP_LIST_CURRENT_CALLS)) == 0){
656ce263fc8SMatthias Ringwald         return HFP_CMD_LIST_CURRENT_CALLS;
657ce263fc8SMatthias Ringwald     }
658ce263fc8SMatthias Ringwald 
659ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUBSCRIBER_NUMBER_INFORMATION, strlen(HFP_SUBSCRIBER_NUMBER_INFORMATION)) == 0){
660ce263fc8SMatthias Ringwald         return HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION;
661ce263fc8SMatthias Ringwald     }
662ce263fc8SMatthias Ringwald 
663aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_PHONE_NUMBER_FOR_VOICE_TAG, strlen(HFP_PHONE_NUMBER_FOR_VOICE_TAG)) == 0){
664ce263fc8SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_SENT_PHONE_NUMBER;
665aa4dd815SMatthias Ringwald         return HFP_CMD_HF_REQUEST_PHONE_NUMBER;
6663deb3ec6SMatthias Ringwald     }
6673deb3ec6SMatthias Ringwald 
668aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSMIT_DTMF_CODES, strlen(HFP_TRANSMIT_DTMF_CODES)) == 0){
669aa4dd815SMatthias Ringwald         return HFP_CMD_TRANSMIT_DTMF_CODES;
6703deb3ec6SMatthias Ringwald     }
6713deb3ec6SMatthias Ringwald 
672aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SET_MICROPHONE_GAIN, strlen(HFP_SET_MICROPHONE_GAIN)) == 0){
673aa4dd815SMatthias Ringwald         return HFP_CMD_SET_MICROPHONE_GAIN;
6743deb3ec6SMatthias Ringwald     }
6753deb3ec6SMatthias Ringwald 
676aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SET_SPEAKER_GAIN, strlen(HFP_SET_SPEAKER_GAIN)) == 0){
677aa4dd815SMatthias Ringwald         return HFP_CMD_SET_SPEAKER_GAIN;
6783deb3ec6SMatthias Ringwald     }
6793deb3ec6SMatthias Ringwald 
680aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ACTIVATE_VOICE_RECOGNITION, strlen(HFP_ACTIVATE_VOICE_RECOGNITION)) == 0){
681aa4dd815SMatthias Ringwald         if (isHandsFree) return HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
682aa4dd815SMatthias Ringwald         return HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
6833deb3ec6SMatthias Ringwald     }
6843deb3ec6SMatthias Ringwald 
685aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TURN_OFF_EC_AND_NR, strlen(HFP_TURN_OFF_EC_AND_NR)) == 0){
686aa4dd815SMatthias Ringwald         return HFP_CMD_TURN_OFF_EC_AND_NR;
6873deb3ec6SMatthias Ringwald     }
6883deb3ec6SMatthias Ringwald 
689aa4dd815SMatthias Ringwald     if (strncmp(line_buffer, HFP_CALL_ANSWERED, strlen(HFP_CALL_ANSWERED)) == 0){
690aa4dd815SMatthias Ringwald         return HFP_CMD_CALL_ANSWERED;
6913deb3ec6SMatthias Ringwald     }
6923deb3ec6SMatthias Ringwald 
693aa4dd815SMatthias Ringwald     if (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
694aa4dd815SMatthias Ringwald         return HFP_CMD_CALL_PHONE_NUMBER;
6953deb3ec6SMatthias Ringwald     }
6963deb3ec6SMatthias Ringwald 
697ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_REDIAL_LAST_NUMBER, strlen(HFP_REDIAL_LAST_NUMBER)) == 0){
698aa4dd815SMatthias Ringwald         return HFP_CMD_REDIAL_LAST_NUMBER;
6993deb3ec6SMatthias Ringwald     }
7003deb3ec6SMatthias Ringwald 
701aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_CHANGE_IN_BAND_RING_TONE_SETTING, strlen(HFP_CHANGE_IN_BAND_RING_TONE_SETTING)) == 0){
702aa4dd815SMatthias Ringwald         return HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
703aa4dd815SMatthias Ringwald     }
704aa4dd815SMatthias Ringwald 
705aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_HANG_UP_CALL, strlen(HFP_HANG_UP_CALL)) == 0){
706aa4dd815SMatthias Ringwald         return HFP_CMD_HANG_UP_CALL;
707aa4dd815SMatthias Ringwald     }
708aa4dd815SMatthias Ringwald 
709aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ERROR, strlen(HFP_ERROR)) == 0){
710aa4dd815SMatthias Ringwald         return HFP_CMD_ERROR;
711aa4dd815SMatthias Ringwald     }
712aa4dd815SMatthias Ringwald 
713ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_RING, strlen(HFP_RING)) == 0){
714ce263fc8SMatthias Ringwald         return HFP_CMD_RING;
715ce263fc8SMatthias Ringwald     }
716ce263fc8SMatthias Ringwald 
717aa4dd815SMatthias Ringwald     if (isHandsFree && strncmp(line_buffer+offset, HFP_OK, strlen(HFP_OK)) == 0){
718aa4dd815SMatthias Ringwald         return HFP_CMD_OK;
719aa4dd815SMatthias Ringwald     }
720aa4dd815SMatthias Ringwald 
721aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUPPORTED_FEATURES, strlen(HFP_SUPPORTED_FEATURES)) == 0){
722aa4dd815SMatthias Ringwald         return HFP_CMD_SUPPORTED_FEATURES;
723aa4dd815SMatthias Ringwald     }
724aa4dd815SMatthias Ringwald 
725ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSFER_HF_INDICATOR_STATUS, strlen(HFP_TRANSFER_HF_INDICATOR_STATUS)) == 0){
726ce263fc8SMatthias Ringwald         return HFP_CMD_HF_INDICATOR_STATUS;
727ce263fc8SMatthias Ringwald     }
728ce263fc8SMatthias Ringwald 
729ce263fc8SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_RESPONSE_AND_HOLD, strlen(HFP_RESPONSE_AND_HOLD)) == 0){
730ce263fc8SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "?", 1) == 0){
731ce263fc8SMatthias Ringwald             return HFP_CMD_RESPONSE_AND_HOLD_QUERY;
732ce263fc8SMatthias Ringwald         }
733ce263fc8SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_RESPONSE_AND_HOLD)+offset, "=", 1) == 0){
734ce263fc8SMatthias Ringwald             return HFP_CMD_RESPONSE_AND_HOLD_COMMAND;
735ce263fc8SMatthias Ringwald         }
736667ec068SMatthias Ringwald         return HFP_CMD_RESPONSE_AND_HOLD_STATUS;
737ce263fc8SMatthias Ringwald     }
738ce263fc8SMatthias Ringwald 
739aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_INDICATOR, strlen(HFP_INDICATOR)) == 0){
740aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "?", 1) == 0){
741aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
742aa4dd815SMatthias Ringwald         }
743aa4dd815SMatthias Ringwald 
744aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_INDICATOR)+offset, "=?", 2) == 0){
745aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_AG_INDICATORS;
746aa4dd815SMatthias Ringwald         }
747aa4dd815SMatthias Ringwald     }
748aa4dd815SMatthias Ringwald 
749aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_AVAILABLE_CODECS, strlen(HFP_AVAILABLE_CODECS)) == 0){
750aa4dd815SMatthias Ringwald         return HFP_CMD_AVAILABLE_CODECS;
751aa4dd815SMatthias Ringwald     }
752aa4dd815SMatthias Ringwald 
753aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, strlen(HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS)) == 0){
754aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE;
755aa4dd815SMatthias Ringwald     }
756aa4dd815SMatthias Ringwald 
757aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_CLIP, strlen(HFP_ENABLE_CLIP)) == 0){
758aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_CLIP;
759aa4dd815SMatthias Ringwald     }
760aa4dd815SMatthias Ringwald 
761aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_ENABLE_CALL_WAITING_NOTIFICATION, strlen(HFP_ENABLE_CALL_WAITING_NOTIFICATION)) == 0){
762aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION;
763aa4dd815SMatthias Ringwald     }
764aa4dd815SMatthias Ringwald 
765aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)) == 0){
766aa4dd815SMatthias Ringwald 
767aa4dd815SMatthias Ringwald         if (isHandsFree) return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
768aa4dd815SMatthias Ringwald 
769aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=?", 2) == 0){
770aa4dd815SMatthias Ringwald             return HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES;
771aa4dd815SMatthias Ringwald         }
772667ec068SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES)+offset, "=", 1) == 0){
773aa4dd815SMatthias Ringwald             return HFP_CMD_CALL_HOLD;
774aa4dd815SMatthias Ringwald         }
775aa4dd815SMatthias Ringwald 
776aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
777aa4dd815SMatthias Ringwald     }
778aa4dd815SMatthias Ringwald 
779aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_GENERIC_STATUS_INDICATOR, strlen(HFP_GENERIC_STATUS_INDICATOR)) == 0){
780667ec068SMatthias Ringwald         if (isHandsFree) {
781667ec068SMatthias Ringwald             return HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS;
782667ec068SMatthias Ringwald         }
783aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=?", 2) == 0){
784aa4dd815SMatthias Ringwald             return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
785aa4dd815SMatthias Ringwald         }
786aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_GENERIC_STATUS_INDICATOR)+offset, "=", 1) == 0){
787aa4dd815SMatthias Ringwald             return HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
788aa4dd815SMatthias Ringwald         }
789aa4dd815SMatthias Ringwald         return HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
790aa4dd815SMatthias Ringwald     }
791aa4dd815SMatthias Ringwald 
792aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS, strlen(HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS)) == 0){
793aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE;
7943deb3ec6SMatthias Ringwald     }
7953deb3ec6SMatthias Ringwald 
7963deb3ec6SMatthias Ringwald 
797aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_QUERY_OPERATOR_SELECTION, strlen(HFP_QUERY_OPERATOR_SELECTION)) == 0){
798aa4dd815SMatthias Ringwald         if (strncmp(line_buffer+strlen(HFP_QUERY_OPERATOR_SELECTION)+offset, "=", 1) == 0){
799aa4dd815SMatthias Ringwald             return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT;
8003deb3ec6SMatthias Ringwald         }
801aa4dd815SMatthias Ringwald         return HFP_CMD_QUERY_OPERATOR_SELECTION_NAME;
8023deb3ec6SMatthias Ringwald     }
8033deb3ec6SMatthias Ringwald 
804aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRANSFER_AG_INDICATOR_STATUS, strlen(HFP_TRANSFER_AG_INDICATOR_STATUS)) == 0){
805aa4dd815SMatthias Ringwald         return HFP_CMD_TRANSFER_AG_INDICATOR_STATUS;
8063deb3ec6SMatthias Ringwald     }
8073deb3ec6SMatthias Ringwald 
808aa4dd815SMatthias Ringwald     if (isHandsFree && strncmp(line_buffer+offset, HFP_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
809aa4dd815SMatthias Ringwald         return HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR;
8103deb3ec6SMatthias Ringwald     }
8113deb3ec6SMatthias Ringwald 
812aa4dd815SMatthias Ringwald     if (!isHandsFree && strncmp(line_buffer+offset, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, strlen(HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR)) == 0){
813aa4dd815SMatthias Ringwald         return HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR;
8143deb3ec6SMatthias Ringwald     }
8153deb3ec6SMatthias Ringwald 
816aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_TRIGGER_CODEC_CONNECTION_SETUP, strlen(HFP_TRIGGER_CODEC_CONNECTION_SETUP)) == 0){
817aa4dd815SMatthias Ringwald         return HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
818aa4dd815SMatthias Ringwald     }
819aa4dd815SMatthias Ringwald 
820aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, HFP_CONFIRM_COMMON_CODEC, strlen(HFP_CONFIRM_COMMON_CODEC)) == 0){
8213deb3ec6SMatthias Ringwald         if (isHandsFree){
822aa4dd815SMatthias Ringwald             return HFP_CMD_AG_SUGGESTED_CODEC;
8233deb3ec6SMatthias Ringwald         } else {
824aa4dd815SMatthias Ringwald             return HFP_CMD_HF_CONFIRMED_CODEC;
8253deb3ec6SMatthias Ringwald         }
8263deb3ec6SMatthias Ringwald     }
8273deb3ec6SMatthias Ringwald 
828aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "AT+", 3) == 0){
829667ec068SMatthias Ringwald         log_info("process unknown HF command %s \n", line_buffer);
830aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
8313deb3ec6SMatthias Ringwald     }
8323deb3ec6SMatthias Ringwald 
833aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "+", 1) == 0){
834667ec068SMatthias Ringwald         log_info(" process unknown AG command %s \n", line_buffer);
835aa4dd815SMatthias Ringwald         return HFP_CMD_UNKNOWN;
836aa4dd815SMatthias Ringwald     }
8373deb3ec6SMatthias Ringwald 
838aa4dd815SMatthias Ringwald     if (strncmp(line_buffer+offset, "NOP", 3) == 0){
839aa4dd815SMatthias Ringwald         return HFP_CMD_NONE;
840aa4dd815SMatthias Ringwald     }
841aa4dd815SMatthias Ringwald 
842aa4dd815SMatthias Ringwald     return HFP_CMD_NONE;
8433deb3ec6SMatthias Ringwald }
8443deb3ec6SMatthias Ringwald 
8453deb3ec6SMatthias Ringwald static void hfp_parser_store_byte(hfp_connection_t * context, uint8_t byte){
846ce263fc8SMatthias Ringwald     // printf("hfp_parser_store_byte %c at pos %u\n", (char) byte, context->line_size);
8473deb3ec6SMatthias Ringwald     // TODO: add limit
8483deb3ec6SMatthias Ringwald     context->line_buffer[context->line_size++] = byte;
8493deb3ec6SMatthias Ringwald     context->line_buffer[context->line_size] = 0;
8503deb3ec6SMatthias Ringwald }
8513deb3ec6SMatthias Ringwald static int hfp_parser_is_buffer_empty(hfp_connection_t * context){
8523deb3ec6SMatthias Ringwald     return context->line_size == 0;
8533deb3ec6SMatthias Ringwald }
8543deb3ec6SMatthias Ringwald 
8553deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
8563deb3ec6SMatthias Ringwald     return byte == '\n' || byte == '\r';
8573deb3ec6SMatthias Ringwald }
8583deb3ec6SMatthias Ringwald 
8593deb3ec6SMatthias Ringwald static int hfp_parser_is_end_of_header(uint8_t byte){
8603deb3ec6SMatthias Ringwald     return hfp_parser_is_end_of_line(byte) || byte == ':' || byte == '?';
8613deb3ec6SMatthias Ringwald }
8623deb3ec6SMatthias Ringwald 
8633deb3ec6SMatthias Ringwald static int hfp_parser_found_separator(hfp_connection_t * context, uint8_t byte){
864ce263fc8SMatthias Ringwald     if (context->keep_byte == 1) return 1;
8653deb3ec6SMatthias Ringwald 
8663deb3ec6SMatthias Ringwald     int found_separator =   byte == ',' || byte == '\n'|| byte == '\r'||
8673deb3ec6SMatthias Ringwald                             byte == ')' || byte == '(' || byte == ':' ||
8683deb3ec6SMatthias Ringwald                             byte == '-' || byte == '"' ||  byte == '?'|| byte == '=';
8693deb3ec6SMatthias Ringwald     return found_separator;
8703deb3ec6SMatthias Ringwald }
8713deb3ec6SMatthias Ringwald 
8723deb3ec6SMatthias Ringwald static void hfp_parser_next_state(hfp_connection_t * context, uint8_t byte){
8733deb3ec6SMatthias Ringwald     context->line_size = 0;
8743deb3ec6SMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)){
8753deb3ec6SMatthias Ringwald         context->parser_item_index = 0;
8763deb3ec6SMatthias Ringwald         context->parser_state = HFP_PARSER_CMD_HEADER;
8773deb3ec6SMatthias Ringwald         return;
8783deb3ec6SMatthias Ringwald     }
8793deb3ec6SMatthias Ringwald     switch (context->parser_state){
8803deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_HEADER:
8813deb3ec6SMatthias Ringwald             context->parser_state = HFP_PARSER_CMD_SEQUENCE;
882ce263fc8SMatthias Ringwald             if (context->keep_byte == 1){
8833deb3ec6SMatthias Ringwald                 hfp_parser_store_byte(context, byte);
884ce263fc8SMatthias Ringwald                 context->keep_byte = 0;
8853deb3ec6SMatthias Ringwald             }
8863deb3ec6SMatthias Ringwald             break;
8873deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
8883deb3ec6SMatthias Ringwald             switch (context->command){
889ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
8903deb3ec6SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
891aa4dd815SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
892aa4dd815SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
893aa4dd815SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
894aa4dd815SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
895ce263fc8SMatthias Ringwald                 case HFP_CMD_HF_INDICATOR_STATUS:
8963deb3ec6SMatthias Ringwald                     context->parser_state = HFP_PARSER_SECOND_ITEM;
8973deb3ec6SMatthias Ringwald                     break;
8983deb3ec6SMatthias Ringwald                 default:
8993deb3ec6SMatthias Ringwald                     break;
9003deb3ec6SMatthias Ringwald             }
9013deb3ec6SMatthias Ringwald             break;
9023deb3ec6SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
9033deb3ec6SMatthias Ringwald             context->parser_state = HFP_PARSER_THIRD_ITEM;
9043deb3ec6SMatthias Ringwald             break;
9053deb3ec6SMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
906aa4dd815SMatthias Ringwald             if (context->command == HFP_CMD_RETRIEVE_AG_INDICATORS){
9073deb3ec6SMatthias Ringwald                 context->parser_state = HFP_PARSER_CMD_SEQUENCE;
9083deb3ec6SMatthias Ringwald                 break;
9093deb3ec6SMatthias Ringwald             }
9103deb3ec6SMatthias Ringwald             context->parser_state = HFP_PARSER_CMD_HEADER;
9113deb3ec6SMatthias Ringwald             break;
9123deb3ec6SMatthias Ringwald     }
9133deb3ec6SMatthias Ringwald }
9143deb3ec6SMatthias Ringwald 
915aa4dd815SMatthias Ringwald void hfp_parse(hfp_connection_t * context, uint8_t byte, int isHandsFree){
916aa4dd815SMatthias Ringwald     // handle ATD<dial_string>;
917aa4dd815SMatthias Ringwald     if (strncmp((const char*)context->line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0){
918aa4dd815SMatthias Ringwald         // check for end-of-line or ';'
919aa4dd815SMatthias Ringwald         if (byte == ';' || hfp_parser_is_end_of_line(byte)){
920aa4dd815SMatthias Ringwald             context->line_buffer[context->line_size] = 0;
921aa4dd815SMatthias Ringwald             context->line_size = 0;
922aa4dd815SMatthias Ringwald             context->command = HFP_CMD_CALL_PHONE_NUMBER;
923aa4dd815SMatthias Ringwald         } else {
924aa4dd815SMatthias Ringwald             context->line_buffer[context->line_size++] = byte;
925aa4dd815SMatthias Ringwald         }
926aa4dd815SMatthias Ringwald         return;
927aa4dd815SMatthias Ringwald     }
928aa4dd815SMatthias Ringwald 
9293deb3ec6SMatthias Ringwald     // TODO: handle space inside word
9303deb3ec6SMatthias Ringwald     if (byte == ' ' && context->parser_state > HFP_PARSER_CMD_HEADER) return;
9313deb3ec6SMatthias Ringwald 
932ce263fc8SMatthias Ringwald     if (byte == ',' && context->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
933ce263fc8SMatthias Ringwald         if (context->line_size == 0){
934ce263fc8SMatthias Ringwald             context->line_buffer[0] = 0;
935ce263fc8SMatthias Ringwald             context->ignore_value = 1;
936ce263fc8SMatthias Ringwald             parse_sequence(context);
937ce263fc8SMatthias Ringwald             return;
938ce263fc8SMatthias Ringwald         }
939ce263fc8SMatthias Ringwald     }
940ce263fc8SMatthias Ringwald 
9413deb3ec6SMatthias Ringwald     if (!hfp_parser_found_separator(context, byte)){
9423deb3ec6SMatthias Ringwald         hfp_parser_store_byte(context, byte);
9433deb3ec6SMatthias Ringwald         return;
9443deb3ec6SMatthias Ringwald     }
945ce263fc8SMatthias Ringwald 
9463deb3ec6SMatthias Ringwald     if (hfp_parser_is_end_of_line(byte)) {
9473deb3ec6SMatthias Ringwald         if (hfp_parser_is_buffer_empty(context)){
9483deb3ec6SMatthias Ringwald             context->parser_state = HFP_PARSER_CMD_HEADER;
9493deb3ec6SMatthias Ringwald         }
9503deb3ec6SMatthias Ringwald     }
9513deb3ec6SMatthias Ringwald     if (hfp_parser_is_buffer_empty(context)) return;
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald     switch (context->parser_state){
9543deb3ec6SMatthias Ringwald         case HFP_PARSER_CMD_HEADER: // header
9553deb3ec6SMatthias Ringwald             if (byte == '='){
956ce263fc8SMatthias Ringwald                 context->keep_byte = 1;
9573deb3ec6SMatthias Ringwald                 hfp_parser_store_byte(context, byte);
9583deb3ec6SMatthias Ringwald                 return;
9593deb3ec6SMatthias Ringwald             }
9603deb3ec6SMatthias Ringwald 
9613deb3ec6SMatthias Ringwald             if (byte == '?'){
962ce263fc8SMatthias Ringwald                 context->keep_byte = 0;
9633deb3ec6SMatthias Ringwald                 hfp_parser_store_byte(context, byte);
9643deb3ec6SMatthias Ringwald                 return;
9653deb3ec6SMatthias Ringwald             }
966ce263fc8SMatthias Ringwald 
967ce263fc8SMatthias Ringwald             if (byte == ','){
968ce263fc8SMatthias Ringwald                 context->resolve_byte = 1;
969ce263fc8SMatthias Ringwald             }
970ce263fc8SMatthias Ringwald 
971ce263fc8SMatthias Ringwald             // printf(" parse header 2 %s, keep separator $ %d\n", context->line_buffer, context->keep_byte);
972ce263fc8SMatthias Ringwald             if (hfp_parser_is_end_of_header(byte) || context->keep_byte == 1){
973ce263fc8SMatthias Ringwald                 // printf(" parse header 3 %s, keep separator $ %d\n", context->line_buffer, context->keep_byte);
974aa4dd815SMatthias Ringwald                 char * line_buffer = (char *)context->line_buffer;
975aa4dd815SMatthias Ringwald                 context->command = parse_command(line_buffer, isHandsFree);
976aa4dd815SMatthias Ringwald 
977aa4dd815SMatthias Ringwald                 /* resolve command name according to context */
978aa4dd815SMatthias Ringwald                 if (context->command == HFP_CMD_UNKNOWN){
979aa4dd815SMatthias Ringwald                     switch(context->state){
980aa4dd815SMatthias Ringwald                         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
981aa4dd815SMatthias Ringwald                             context->command = HFP_CMD_LIST_GENERIC_STATUS_INDICATORS;
982aa4dd815SMatthias Ringwald                             break;
983aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
984aa4dd815SMatthias Ringwald                             context->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS;
985aa4dd815SMatthias Ringwald                             break;
986aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
987aa4dd815SMatthias Ringwald                             context->command = HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE;
988aa4dd815SMatthias Ringwald                             break;
989aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
990aa4dd815SMatthias Ringwald                             context->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS;
991aa4dd815SMatthias Ringwald                             break;
992aa4dd815SMatthias Ringwald                         case HFP_W4_RETRIEVE_INDICATORS:
993ad902e3dSMatthias Ringwald                             context->send_ag_indicators_segment = 0;
994aa4dd815SMatthias Ringwald                             context->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
995aa4dd815SMatthias Ringwald                             break;
996aa4dd815SMatthias Ringwald                         default:
997aa4dd815SMatthias Ringwald                             break;
998aa4dd815SMatthias Ringwald                     }
999aa4dd815SMatthias Ringwald                 }
10003deb3ec6SMatthias Ringwald             }
10013deb3ec6SMatthias Ringwald             break;
10023deb3ec6SMatthias Ringwald 
1003ce263fc8SMatthias Ringwald         case HFP_PARSER_CMD_SEQUENCE:
1004ce263fc8SMatthias Ringwald             parse_sequence(context);
1005ce263fc8SMatthias Ringwald             break;
1006ce263fc8SMatthias Ringwald         case HFP_PARSER_SECOND_ITEM:
1007ce263fc8SMatthias Ringwald             switch (context->command){
1008ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1009ce263fc8SMatthias Ringwald                     log_info("format %s, ", context->line_buffer);
1010ce263fc8SMatthias Ringwald                     context->network_operator.format =  atoi((char *)&context->line_buffer[0]);
1011ce263fc8SMatthias Ringwald                     break;
1012ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1013ce263fc8SMatthias Ringwald                     log_info("format %s \n", context->line_buffer);
1014ce263fc8SMatthias Ringwald                     context->network_operator.format =  atoi((char *)&context->line_buffer[0]);
1015ce263fc8SMatthias Ringwald                     break;
1016ce263fc8SMatthias Ringwald                 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1017ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
1018ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
1019ce263fc8SMatthias Ringwald                     context->generic_status_indicators[context->parser_item_index].state = (uint8_t)atoi((char*)context->line_buffer);
1020ce263fc8SMatthias Ringwald                     break;
1021ce263fc8SMatthias Ringwald                 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1022ce263fc8SMatthias Ringwald                     context->ag_indicators[context->parser_item_index].status = (uint8_t)atoi((char*)context->line_buffer);
1023ce263fc8SMatthias Ringwald                     log_info("%d \n", context->ag_indicators[context->parser_item_index].status);
1024ce263fc8SMatthias Ringwald                     context->ag_indicators[context->parser_item_index].status_changed = 1;
1025ce263fc8SMatthias Ringwald                     break;
1026ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1027ce263fc8SMatthias Ringwald                     context->ag_indicators[context->parser_item_index].min_range = atoi((char *)context->line_buffer);
1028ce263fc8SMatthias Ringwald                     log_info("%s, ", context->line_buffer);
1029ce263fc8SMatthias Ringwald                     break;
1030ce263fc8SMatthias Ringwald                 case HFP_CMD_AG_SENT_PHONE_NUMBER:
1031ce263fc8SMatthias Ringwald                     context->bnip_type = (uint8_t)atoi((char*)context->line_buffer);
1032ce263fc8SMatthias Ringwald                     break;
1033ce263fc8SMatthias Ringwald                 default:
1034ce263fc8SMatthias Ringwald                     break;
1035ce263fc8SMatthias Ringwald             }
1036ce263fc8SMatthias Ringwald             break;
1037ce263fc8SMatthias Ringwald 
1038ce263fc8SMatthias Ringwald         case HFP_PARSER_THIRD_ITEM:
1039ce263fc8SMatthias Ringwald              switch (context->command){
1040ce263fc8SMatthias Ringwald                 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1041ce263fc8SMatthias Ringwald                     strcpy(context->network_operator.name, (char *)context->line_buffer);
1042ce263fc8SMatthias Ringwald                     log_info("name %s\n", context->line_buffer);
1043ce263fc8SMatthias Ringwald                     break;
1044ce263fc8SMatthias Ringwald                 case HFP_CMD_RETRIEVE_AG_INDICATORS:
1045ce263fc8SMatthias Ringwald                     context->ag_indicators[context->parser_item_index].max_range = atoi((char *)context->line_buffer);
1046ce263fc8SMatthias Ringwald                     context->parser_item_index++;
1047ce263fc8SMatthias Ringwald                     context->ag_indicators_nr = context->parser_item_index;
1048ce263fc8SMatthias Ringwald                     log_info("%s)\n", context->line_buffer);
1049ce263fc8SMatthias Ringwald                     break;
1050ce263fc8SMatthias Ringwald                 default:
1051ce263fc8SMatthias Ringwald                     break;
1052ce263fc8SMatthias Ringwald             }
1053ce263fc8SMatthias Ringwald             break;
1054ce263fc8SMatthias Ringwald     }
1055ce263fc8SMatthias Ringwald     hfp_parser_next_state(context, byte);
1056ce263fc8SMatthias Ringwald 
1057ce263fc8SMatthias Ringwald     if (context->resolve_byte && context->command == HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE){
1058ce263fc8SMatthias Ringwald         context->resolve_byte = 0;
1059ce263fc8SMatthias Ringwald         context->ignore_value = 1;
1060ce263fc8SMatthias Ringwald         parse_sequence(context);
1061ce263fc8SMatthias Ringwald         context->line_buffer[0] = 0;
1062ce263fc8SMatthias Ringwald         context->line_size = 0;
1063ce263fc8SMatthias Ringwald     }
1064ce263fc8SMatthias Ringwald }
1065ce263fc8SMatthias Ringwald 
1066ce263fc8SMatthias Ringwald static void parse_sequence(hfp_connection_t * context){
1067ce263fc8SMatthias Ringwald     int value;
10683deb3ec6SMatthias Ringwald     switch (context->command){
1069667ec068SMatthias Ringwald         case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS:
1070667ec068SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
1071667ec068SMatthias Ringwald             int i;
1072667ec068SMatthias Ringwald             switch (context->parser_item_index){
1073667ec068SMatthias Ringwald                 case 0:
1074667ec068SMatthias Ringwald                     for (i=0;i<context->generic_status_indicators_nr;i++){
1075667ec068SMatthias Ringwald                         if (context->generic_status_indicators[i].uuid == value){
1076667ec068SMatthias Ringwald                             context->parser_indicator_index = i;
1077667ec068SMatthias Ringwald                             break;
1078667ec068SMatthias Ringwald                         }
1079667ec068SMatthias Ringwald                     }
1080667ec068SMatthias Ringwald                     break;
1081667ec068SMatthias Ringwald                 case 1:
1082667ec068SMatthias Ringwald                     if (context->parser_indicator_index <0) break;
1083667ec068SMatthias Ringwald                     context->generic_status_indicators[context->parser_indicator_index].state = value;
1084667ec068SMatthias Ringwald                     log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n",
1085667ec068SMatthias Ringwald                      context->parser_item_index, value);
1086667ec068SMatthias Ringwald                     break;
1087667ec068SMatthias Ringwald                 default:
1088667ec068SMatthias Ringwald                     break;
1089667ec068SMatthias Ringwald             }
1090667ec068SMatthias Ringwald             context->parser_item_index++;
1091667ec068SMatthias Ringwald             break;
1092667ec068SMatthias Ringwald 
1093667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1094667ec068SMatthias Ringwald             switch(context->parser_item_index){
1095667ec068SMatthias Ringwald                 case 0:
1096667ec068SMatthias Ringwald                     strncpy(context->bnip_number, (char *)context->line_buffer, sizeof(context->bnip_number));
1097667ec068SMatthias Ringwald                     context->bnip_number[sizeof(context->bnip_number)-1] = 0;
1098667ec068SMatthias Ringwald                     break;
1099667ec068SMatthias Ringwald                 case 1:
1100667ec068SMatthias Ringwald                     value = atoi((char *)&context->line_buffer[0]);
1101667ec068SMatthias Ringwald                     context->bnip_type = value;
1102667ec068SMatthias Ringwald                     break;
1103667ec068SMatthias Ringwald                 default:
1104667ec068SMatthias Ringwald                     break;
1105667ec068SMatthias Ringwald             }
1106667ec068SMatthias Ringwald             context->parser_item_index++;
1107667ec068SMatthias Ringwald             break;
1108667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1109667ec068SMatthias Ringwald             switch(context->parser_item_index){
1110667ec068SMatthias Ringwald                 case 0:
1111667ec068SMatthias Ringwald                     value = atoi((char *)&context->line_buffer[0]);
1112667ec068SMatthias Ringwald                     context->clcc_idx = value;
1113667ec068SMatthias Ringwald                     break;
1114667ec068SMatthias Ringwald                 case 1:
1115667ec068SMatthias Ringwald                     value = atoi((char *)&context->line_buffer[0]);
1116667ec068SMatthias Ringwald                     context->clcc_dir = value;
1117667ec068SMatthias Ringwald                     break;
1118667ec068SMatthias Ringwald                 case 2:
1119667ec068SMatthias Ringwald                     value = atoi((char *)&context->line_buffer[0]);
1120667ec068SMatthias Ringwald                     context->clcc_status = value;
1121667ec068SMatthias Ringwald                     break;
1122667ec068SMatthias Ringwald                 case 3:
1123667ec068SMatthias Ringwald                     value = atoi((char *)&context->line_buffer[0]);
1124667ec068SMatthias Ringwald                     context->clcc_mpty = value;
1125667ec068SMatthias Ringwald                     break;
1126667ec068SMatthias Ringwald                 case 4:
1127667ec068SMatthias Ringwald                     strncpy(context->bnip_number, (char *)context->line_buffer, sizeof(context->bnip_number));
1128667ec068SMatthias Ringwald                     context->bnip_number[sizeof(context->bnip_number)-1] = 0;
1129667ec068SMatthias Ringwald                     break;
1130667ec068SMatthias Ringwald                 case 5:
1131667ec068SMatthias Ringwald                     value = atoi((char *)&context->line_buffer[0]);
1132667ec068SMatthias Ringwald                     context->bnip_type = value;
1133667ec068SMatthias Ringwald                     break;
1134667ec068SMatthias Ringwald                 default:
1135667ec068SMatthias Ringwald                     break;
1136667ec068SMatthias Ringwald             }
1137667ec068SMatthias Ringwald             context->parser_item_index++;
1138667ec068SMatthias Ringwald             break;
1139aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1140aa4dd815SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
1141aa4dd815SMatthias Ringwald             context->microphone_gain = value;
1142aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value);
1143aa4dd815SMatthias Ringwald             break;
1144aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1145aa4dd815SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
1146aa4dd815SMatthias Ringwald             context->speaker_gain = value;
1147aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value);
1148aa4dd815SMatthias Ringwald             break;
1149aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
1150aa4dd815SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
1151aa4dd815SMatthias Ringwald             context->ag_activate_voice_recognition = value;
1152aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION %d\n", value);
1153aa4dd815SMatthias Ringwald             break;
1154aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1155aa4dd815SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
1156aa4dd815SMatthias Ringwald             context->ag_echo_and_noise_reduction = value;
1157aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value);
1158aa4dd815SMatthias Ringwald             break;
1159aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1160aa4dd815SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
1161aa4dd815SMatthias Ringwald             context->remote_supported_features = store_bit(context->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value);
1162aa4dd815SMatthias Ringwald             log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value);
1163aa4dd815SMatthias Ringwald             break;
11643deb3ec6SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
11653deb3ec6SMatthias Ringwald             context->codec_confirmed = atoi((char*)context->line_buffer);
11663deb3ec6SMatthias Ringwald             log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", context->codec_confirmed);
11673deb3ec6SMatthias Ringwald             break;
11683deb3ec6SMatthias Ringwald         case HFP_CMD_AG_SUGGESTED_CODEC:
11693deb3ec6SMatthias Ringwald             context->suggested_codec = atoi((char*)context->line_buffer);
11703deb3ec6SMatthias Ringwald             log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", context->suggested_codec);
11713deb3ec6SMatthias Ringwald             break;
11723deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
11733deb3ec6SMatthias Ringwald             context->remote_supported_features = atoi((char*)context->line_buffer);
11743deb3ec6SMatthias Ringwald             log_info("Parsed supported feature %d\n", context->remote_supported_features);
11753deb3ec6SMatthias Ringwald             break;
11763deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
11773deb3ec6SMatthias Ringwald             log_info("Parsed codec %s\n", context->line_buffer);
11783deb3ec6SMatthias Ringwald             context->remote_codecs[context->parser_item_index] = (uint16_t)atoi((char*)context->line_buffer);
11793deb3ec6SMatthias Ringwald             context->parser_item_index++;
11803deb3ec6SMatthias Ringwald             context->remote_codecs_nr = context->parser_item_index;
11813deb3ec6SMatthias Ringwald             break;
1182aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
11833deb3ec6SMatthias Ringwald             strcpy((char *)context->ag_indicators[context->parser_item_index].name,  (char *)context->line_buffer);
11843deb3ec6SMatthias Ringwald             context->ag_indicators[context->parser_item_index].index = context->parser_item_index+1;
11853deb3ec6SMatthias Ringwald             log_info("Indicator %d: %s (", context->ag_indicators_nr+1, context->line_buffer);
1186aa4dd815SMatthias Ringwald             break;
1187aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
11883deb3ec6SMatthias Ringwald             log_info("Parsed Indicator %d with status: %s\n", context->parser_item_index+1, context->line_buffer);
11893deb3ec6SMatthias Ringwald             context->ag_indicators[context->parser_item_index].status = atoi((char *) context->line_buffer);
11903deb3ec6SMatthias Ringwald             context->parser_item_index++;
11913deb3ec6SMatthias Ringwald             break;
11923deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
11933deb3ec6SMatthias Ringwald             context->parser_item_index++;
11943deb3ec6SMatthias Ringwald             if (context->parser_item_index != 4) break;
11953deb3ec6SMatthias Ringwald             log_info("Parsed Enable indicators: %s\n", context->line_buffer);
11963deb3ec6SMatthias Ringwald             value = atoi((char *)&context->line_buffer[0]);
11973deb3ec6SMatthias Ringwald             context->enable_status_update_for_ag_indicators = (uint8_t) value;
11983deb3ec6SMatthias Ringwald             break;
11993deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
12003deb3ec6SMatthias Ringwald             log_info("Parsed Support call hold: %s\n", context->line_buffer);
12013deb3ec6SMatthias Ringwald             if (context->line_size > 2 ) break;
12023deb3ec6SMatthias Ringwald             strcpy((char *)context->remote_call_services[context->remote_call_services_nr].name,  (char *)context->line_buffer);
12033deb3ec6SMatthias Ringwald             context->remote_call_services_nr++;
12043deb3ec6SMatthias Ringwald             break;
1205aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
1206aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
12073deb3ec6SMatthias Ringwald             log_info("Parsed Generic status indicator: %s\n", context->line_buffer);
12083deb3ec6SMatthias Ringwald             context->generic_status_indicators[context->parser_item_index].uuid = (uint16_t)atoi((char*)context->line_buffer);
12093deb3ec6SMatthias Ringwald             context->parser_item_index++;
12103deb3ec6SMatthias Ringwald             context->generic_status_indicators_nr = context->parser_item_index;
12113deb3ec6SMatthias Ringwald             break;
1212aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
12133deb3ec6SMatthias Ringwald             // HF parses inital AG gen. ind. state
12143deb3ec6SMatthias Ringwald             log_info("Parsed List generic status indicator %s state: ", context->line_buffer);
12153deb3ec6SMatthias Ringwald             context->parser_item_index = (uint8_t)atoi((char*)context->line_buffer);
12163deb3ec6SMatthias Ringwald             break;
1217ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1218ce263fc8SMatthias Ringwald             context->parser_indicator_index = (uint8_t)atoi((char*)context->line_buffer);
1219ce263fc8SMatthias Ringwald             log_info("Parsed HF indicator index %u", context->parser_indicator_index);
1220ce263fc8SMatthias Ringwald             break;
12213deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
12223deb3ec6SMatthias Ringwald             // AG parses new gen. ind. state
1223ce263fc8SMatthias Ringwald             if (context->ignore_value){
1224ce263fc8SMatthias Ringwald                 context->ignore_value = 0;
1225ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", context->parser_item_index,
1226ce263fc8SMatthias Ringwald                     context->ag_indicators[context->parser_item_index].name, context->ag_indicators[context->parser_item_index].enabled);
1227ce263fc8SMatthias Ringwald             }
1228ce263fc8SMatthias Ringwald             else if (context->ag_indicators[context->parser_item_index].mandatory){
1229ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n",
1230ce263fc8SMatthias Ringwald                     context->parser_item_index, context->ag_indicators[context->parser_item_index].name);
1231ce263fc8SMatthias Ringwald             } else {
12323deb3ec6SMatthias Ringwald                 value = atoi((char *)&context->line_buffer[0]);
12333deb3ec6SMatthias Ringwald                 context->ag_indicators[context->parser_item_index].enabled = value;
1234ce263fc8SMatthias Ringwald                 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", context->parser_item_index,
1235ce263fc8SMatthias Ringwald                     context->ag_indicators[context->parser_item_index].name, value);
12363deb3ec6SMatthias Ringwald             }
12373deb3ec6SMatthias Ringwald             context->parser_item_index++;
12383deb3ec6SMatthias Ringwald             break;
12393deb3ec6SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12403deb3ec6SMatthias Ringwald             // indicators are indexed starting with 1
12413deb3ec6SMatthias Ringwald             context->parser_item_index = atoi((char *)&context->line_buffer[0]) - 1;
12423deb3ec6SMatthias Ringwald             log_info("Parsed status of the AG indicator %d, status ", context->parser_item_index);
12433deb3ec6SMatthias Ringwald             break;
1244aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1245aa4dd815SMatthias Ringwald             context->network_operator.mode = atoi((char *)&context->line_buffer[0]);
1246aa4dd815SMatthias Ringwald             log_info("Parsed network operator mode: %d, ", context->network_operator.mode);
1247aa4dd815SMatthias Ringwald             break;
1248aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
12493deb3ec6SMatthias Ringwald             if (context->line_buffer[0] == '3'){
12503deb3ec6SMatthias Ringwald                 log_info("Parsed Set network operator format : %s, ", context->line_buffer);
12513deb3ec6SMatthias Ringwald                 break;
12523deb3ec6SMatthias Ringwald             }
12533deb3ec6SMatthias Ringwald             // TODO emit ERROR, wrong format
12543deb3ec6SMatthias Ringwald             log_info("ERROR Set network operator format: index %s not supported\n", context->line_buffer);
12553deb3ec6SMatthias Ringwald             break;
12563deb3ec6SMatthias Ringwald         case HFP_CMD_ERROR:
12573deb3ec6SMatthias Ringwald             break;
12583deb3ec6SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
12593deb3ec6SMatthias Ringwald             context->extended_audio_gateway_error = (uint8_t)atoi((char*)context->line_buffer);
12603deb3ec6SMatthias Ringwald             break;
12613deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
12623deb3ec6SMatthias Ringwald             context->enable_extended_audio_gateway_error_report = (uint8_t)atoi((char*)context->line_buffer);
1263aa4dd815SMatthias Ringwald             context->ok_pending = 1;
12643deb3ec6SMatthias Ringwald             context->extended_audio_gateway_error = 0;
12653deb3ec6SMatthias Ringwald             break;
1266ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1267ce263fc8SMatthias Ringwald             strncpy(context->bnip_number, (char *)context->line_buffer, sizeof(context->bnip_number));
1268ce263fc8SMatthias Ringwald             context->bnip_number[sizeof(context->bnip_number)-1] = 0;
12693deb3ec6SMatthias Ringwald             break;
12703deb3ec6SMatthias Ringwald         default:
12713deb3ec6SMatthias Ringwald             break;
12723deb3ec6SMatthias Ringwald     }
12733deb3ec6SMatthias Ringwald }
12743deb3ec6SMatthias Ringwald 
12753deb3ec6SMatthias Ringwald void hfp_init(uint16_t rfcomm_channel_nr){
1276457b5cb1SMatthias Ringwald     rfcomm_register_service(rfcomm_channel_nr, 0xffff);
12773deb3ec6SMatthias Ringwald     sdp_query_rfcomm_register_callback(handle_query_rfcomm_event, NULL);
12783deb3ec6SMatthias Ringwald }
12793deb3ec6SMatthias Ringwald 
12803deb3ec6SMatthias Ringwald void hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid){
12813deb3ec6SMatthias Ringwald     hfp_connection_t * context = provide_hfp_connection_context_for_bd_addr(bd_addr);
12823deb3ec6SMatthias Ringwald     log_info("hfp_connect %s, context %p", bd_addr_to_str(bd_addr), context);
12833deb3ec6SMatthias Ringwald 
12843deb3ec6SMatthias Ringwald     if (!context) {
12853deb3ec6SMatthias Ringwald         log_error("hfp_establish_service_level_connection for addr %s failed", bd_addr_to_str(bd_addr));
12863deb3ec6SMatthias Ringwald         return;
12873deb3ec6SMatthias Ringwald     }
1288aa4dd815SMatthias Ringwald 
12893deb3ec6SMatthias Ringwald     switch (context->state){
12903deb3ec6SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
12913deb3ec6SMatthias Ringwald             context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
12923deb3ec6SMatthias Ringwald             return;
12933deb3ec6SMatthias Ringwald         case HFP_W4_RFCOMM_DISCONNECTED:
12943deb3ec6SMatthias Ringwald             context->state = HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART;
12953deb3ec6SMatthias Ringwald             return;
12963deb3ec6SMatthias Ringwald         case HFP_IDLE:
12973deb3ec6SMatthias Ringwald             memcpy(context->remote_addr, bd_addr, 6);
12985611a760SMatthias Ringwald             context->state = HFP_W4_SDP_EVENT_QUERY_COMPLETE;
12993deb3ec6SMatthias Ringwald             connection_doing_sdp_query = context;
13003deb3ec6SMatthias Ringwald             context->service_uuid = service_uuid;
13013deb3ec6SMatthias Ringwald             sdp_query_rfcomm_channel_and_name_for_uuid(context->remote_addr, service_uuid);
13023deb3ec6SMatthias Ringwald             break;
13033deb3ec6SMatthias Ringwald         default:
13043deb3ec6SMatthias Ringwald             break;
13053deb3ec6SMatthias Ringwald     }
13063deb3ec6SMatthias Ringwald }
13073deb3ec6SMatthias Ringwald 
13083deb3ec6SMatthias Ringwald void hfp_release_service_level_connection(hfp_connection_t * context){
13093deb3ec6SMatthias Ringwald     if (!context) return;
13103deb3ec6SMatthias Ringwald 
13113deb3ec6SMatthias Ringwald     if (context->state < HFP_W4_RFCOMM_CONNECTED){
13123deb3ec6SMatthias Ringwald         context->state = HFP_IDLE;
13133deb3ec6SMatthias Ringwald         return;
13143deb3ec6SMatthias Ringwald     }
13153deb3ec6SMatthias Ringwald 
13163deb3ec6SMatthias Ringwald     if (context->state == HFP_W4_RFCOMM_CONNECTED){
13173deb3ec6SMatthias Ringwald         context->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN;
13183deb3ec6SMatthias Ringwald         return;
13193deb3ec6SMatthias Ringwald     }
13203deb3ec6SMatthias Ringwald 
1321aa4dd815SMatthias Ringwald     if (context->state < HFP_W4_SCO_CONNECTED){
13223deb3ec6SMatthias Ringwald         context->state = HFP_W2_DISCONNECT_RFCOMM;
13233deb3ec6SMatthias Ringwald         return;
13243deb3ec6SMatthias Ringwald     }
13253deb3ec6SMatthias Ringwald 
13263deb3ec6SMatthias Ringwald     if (context->state < HFP_W4_SCO_DISCONNECTED){
13273deb3ec6SMatthias Ringwald         context->state = HFP_W2_DISCONNECT_SCO;
13283deb3ec6SMatthias Ringwald         return;
13293deb3ec6SMatthias Ringwald     }
13303deb3ec6SMatthias Ringwald 
13313deb3ec6SMatthias Ringwald     return;
13323deb3ec6SMatthias Ringwald }
13333deb3ec6SMatthias Ringwald 
1334aa4dd815SMatthias Ringwald void hfp_release_audio_connection(hfp_connection_t * context){
1335aa4dd815SMatthias Ringwald     if (!context) return;
1336aa4dd815SMatthias Ringwald     if (context->state >= HFP_W2_DISCONNECT_SCO) return;
1337aa4dd815SMatthias Ringwald     context->release_audio_connection = 1;
13383deb3ec6SMatthias Ringwald }
13393deb3ec6SMatthias Ringwald 
1340ce263fc8SMatthias Ringwald static const struct link_settings {
1341ce263fc8SMatthias Ringwald     const uint16_t max_latency;
1342ce263fc8SMatthias Ringwald     const uint8_t  retransmission_effort;
1343ce263fc8SMatthias Ringwald     const uint16_t packet_types;
1344ce263fc8SMatthias Ringwald } hfp_link_settings [] = {
1345ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c1 }, // HFP_LINK_SETTINGS_D0,   HV1
1346ce263fc8SMatthias Ringwald     { 0xffff, 0xff, 0x03c4 }, // HFP_LINK_SETTINGS_D1,   HV3
1347ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x03c8 }, // HFP_LINK_SETTINGS_S1,   EV3
1348ce263fc8SMatthias Ringwald     { 0x0007, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S2, 2-EV3
1349ce263fc8SMatthias Ringwald     { 0x000a, 0x01, 0x0380 }, // HFP_LINK_SETTINGS_S3, 2-EV3
1350ce263fc8SMatthias Ringwald     { 0x000c, 0x02, 0x0380 }, // HFP_LINK_SETTINGS_S4, 2-EV3
1351ce263fc8SMatthias Ringwald     { 0x0008, 0x02, 0x03c8 }, // HFP_LINK_SETTINGS_T1,   EV3
1352ce263fc8SMatthias Ringwald     { 0x000d, 0x02, 0x0380 }  // HFP_LINK_SETTINGS_T2, 2-EV3
1353ce263fc8SMatthias Ringwald };
13543deb3ec6SMatthias Ringwald 
1355ce263fc8SMatthias Ringwald void hfp_setup_synchronous_connection(hci_con_handle_t handle, hfp_link_setttings_t setting){
1356ce263fc8SMatthias Ringwald     // all packet types, fixed bandwidth
1357ce263fc8SMatthias Ringwald     log_info("hfp_setup_synchronous_connection using setting nr %u", setting);
1358ce263fc8SMatthias Ringwald     hci_send_cmd(&hci_setup_synchronous_connection, handle, 8000, 8000, hfp_link_settings[setting].max_latency,
1359ce263fc8SMatthias 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
1360ce263fc8SMatthias Ringwald }
1361