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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald *
333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald * [email protected]
353deb3ec6SMatthias Ringwald *
363deb3ec6SMatthias Ringwald */
37ab2c6ae4SMatthias Ringwald
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald
483deb3ec6SMatthias Ringwald #include <stdint.h>
493cfa4086SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <string.h>
513deb3ec6SMatthias Ringwald
5256042629SMatthias Ringwald #include "hci_cmd.h"
5382636622SMatthias Ringwald #include "btstack_run_loop.h"
543deb3ec6SMatthias Ringwald
55235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
563deb3ec6SMatthias Ringwald #include "hci.h"
573deb3ec6SMatthias Ringwald #include "btstack_memory.h"
583deb3ec6SMatthias Ringwald #include "hci_dump.h"
593deb3ec6SMatthias Ringwald #include "l2cap.h"
6016ece135SMatthias Ringwald #include "btstack_debug.h"
61e30a6a47SMatthias Ringwald #include "btstack_event.h"
6259c6af15SMatthias Ringwald #include "classic/core.h"
633edc84c5SMatthias Ringwald #include "classic/hfp.h"
643edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
65023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
66efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
67023f2764SMatthias Ringwald #include "classic/sdp_server.h"
68023f2764SMatthias Ringwald #include "classic/sdp_util.h"
693deb3ec6SMatthias Ringwald
70c5b64319SMatthias Ringwald // private prototypes
71f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
722ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection);
73fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection);
74c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
75c5b64319SMatthias Ringwald
76c5b64319SMatthias Ringwald // public prototypes
7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
83c5b64319SMatthias Ringwald
8499af1e28SMilanka Ringwald #define HFP_SUBEVENT_INVALID 0xFFFF
8527950165SMatthias Ringwald
8620b2edb6SMatthias Ringwald // const
87aeb0f0feSMatthias Ringwald static const char hfp_ag_default_service_name[] = "Voice gateway";
88aa4dd815SMatthias Ringwald
8920b2edb6SMatthias Ringwald // globals
90aeb0f0feSMatthias Ringwald
91aeb0f0feSMatthias Ringwald // higher layer callbacks
92aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
93aeb0f0feSMatthias Ringwald
9481e25d0aSMatthias Ringwald static bool (*hfp_ag_custom_call_sm_handler)(hfp_ag_call_event_t event);
9581e25d0aSMatthias Ringwald
9620b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration;
97aa4dd815SMatthias Ringwald
98aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features;
9920b2edb6SMatthias Ringwald
10040a8ee13SMatthias Ringwald // in-band ring tone is active on SLC if supported
10140a8ee13SMatthias Ringwald static bool hfp_ag_in_band_ring_tone_active;
10240a8ee13SMatthias Ringwald
103aeb0f0feSMatthias Ringwald // codecs
104aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr;
105aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS];
1063deb3ec6SMatthias Ringwald
107aeb0f0feSMatthias Ringwald // AG indicators
108e58f738aSMatthias Ringwald static uint16_t hfp_ag_indicators_nr;
10925789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
1103deb3ec6SMatthias Ringwald
111aeb0f0feSMatthias Ringwald // generic status indicators
112e58f738aSMatthias Ringwald static uint16_t hfp_ag_generic_status_indicators_nr;
113aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
114a0ffb263SMatthias Ringwald
115e58f738aSMatthias Ringwald static uint16_t hfp_ag_call_hold_services_nr;
1163deb3ec6SMatthias Ringwald static char * hfp_ag_call_hold_services[6];
1173deb3ec6SMatthias Ringwald
118ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
11927bb1817SMatthias Ringwald static bool hfp_ag_response_and_hold_active = false;
120aa4dd815SMatthias Ringwald
12120b2edb6SMatthias Ringwald // Subscriber information entries
122aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers;
123aeb0f0feSMatthias Ringwald static int hfp_ag_subscriber_numbers_count;
124ce263fc8SMatthias Ringwald
125a4f85bd2SMilanka Ringwald // call state
126a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout;
127a4f85bd2SMilanka Ringwald
128edc46354SMatthias Ringwald // Apple extension
129edc46354SMatthias Ringwald static const char * hfp_ag_apple_device;
130edc46354SMatthias Ringwald static uint8_t hfp_ag_apple_features;
131edc46354SMatthias Ringwald
132edc46354SMatthias Ringwald
13320b2edb6SMatthias Ringwald // code
hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection)134a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
135a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
136a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1376535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1386535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1393deb3ec6SMatthias Ringwald }
140a0ffb263SMatthias Ringwald return hfp_connection->ag_indicators_nr;
141a0ffb263SMatthias Ringwald }
142a0ffb263SMatthias Ringwald
hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection)143a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
144a0ffb263SMatthias Ringwald // TODO: save only value, and value changed in the hfp_connection?
145a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
146a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1476535961aSMatthias Ringwald (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1486535961aSMatthias Ringwald hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
149a0ffb263SMatthias Ringwald }
150a0ffb263SMatthias Ringwald return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1513deb3ec6SMatthias Ringwald }
1523deb3ec6SMatthias Ringwald
get_ag_indicator_for_name(const char * name)153aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
154aa4dd815SMatthias Ringwald int i;
155aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){
156aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){
157aa4dd815SMatthias Ringwald return &hfp_ag_indicators[i];
158aa4dd815SMatthias Ringwald }
159aa4dd815SMatthias Ringwald }
160aa4dd815SMatthias Ringwald return NULL;
161aa4dd815SMatthias Ringwald }
162aa4dd815SMatthias Ringwald
get_ag_indicator_index_for_name(const char * name)163aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
164aa4dd815SMatthias Ringwald int i;
165aa4dd815SMatthias Ringwald for (i = 0; i < hfp_ag_indicators_nr; i++){
166aa4dd815SMatthias Ringwald if (strcmp(hfp_ag_indicators[i].name, name) == 0){
167aa4dd815SMatthias Ringwald return i;
168aa4dd815SMatthias Ringwald }
169aa4dd815SMatthias Ringwald }
170aa4dd815SMatthias Ringwald return -1;
171aa4dd815SMatthias Ringwald }
172aa4dd815SMatthias Ringwald
get_hfp_ag_connection_context_for_acl_handle(uint16_t handle)1739c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1749c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it;
1759c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1769c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1779c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1789c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue;
1799c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1809c9c64c1SMatthias Ringwald return hfp_connection;
1819c9c64c1SMatthias Ringwald }
1829c9c64c1SMatthias Ringwald return NULL;
1839c9c64c1SMatthias Ringwald }
1843deb3ec6SMatthias Ringwald
has_in_band_ring_tone(void)18540a8ee13SMatthias Ringwald static bool has_in_band_ring_tone(void){
18640a8ee13SMatthias Ringwald return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) != 0;
18740a8ee13SMatthias Ringwald }
18840a8ee13SMatthias Ringwald
use_in_band_tone(hfp_connection_t * hfp_connection)189f197e761SMatthias Ringwald static int use_in_band_tone(hfp_connection_t *hfp_connection) {
19040a8ee13SMatthias Ringwald return hfp_connection->ag_in_band_ring_tone_active ? 1 : 0;
1913deb3ec6SMatthias Ringwald }
1923deb3ec6SMatthias Ringwald
has_codec_negotiation_feature(hfp_connection_t * hfp_connection)193a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
194a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
195aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1963deb3ec6SMatthias Ringwald return hf && ag;
1973deb3ec6SMatthias Ringwald }
1983deb3ec6SMatthias Ringwald
has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection)199a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
200a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
201aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING);
2023deb3ec6SMatthias Ringwald return hf && ag;
2033deb3ec6SMatthias Ringwald }
2043deb3ec6SMatthias Ringwald
has_hf_indicators_feature(hfp_connection_t * hfp_connection)205a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
206a0ffb263SMatthias Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
207aeb0f0feSMatthias Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS);
2083deb3ec6SMatthias Ringwald return hf && ag;
2093deb3ec6SMatthias Ringwald }
2103deb3ec6SMatthias Ringwald
21176cc1527SMatthias Ringwald /* unsolicited responses */
212aa4dd815SMatthias Ringwald
hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection_t * hfp_connection)213f197e761SMatthias Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection_t * hfp_connection){
2148d860c8fSMatthias Ringwald char buffer[40];
215865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: %d\r\n",
216f197e761SMatthias Ringwald HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone(hfp_connection));
217f197e761SMatthias Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
2183deb3ec6SMatthias Ringwald }
2193deb3ec6SMatthias Ringwald
hfp_ag_exchange_supported_features_cmd(uint16_t cid)2203deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2213deb3ec6SMatthias Ringwald char buffer[40];
222865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n",
223aeb0f0feSMatthias Ringwald HFP_SUPPORTED_FEATURES, hfp_ag_supported_features);
2243deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald
hfp_ag_send_ok(uint16_t cid)227485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2283deb3ec6SMatthias Ringwald char buffer[10];
229865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\nOK\r\n");
2303deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
2313deb3ec6SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald
hfp_ag_send_ring(uint16_t cid)233485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
234aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
235aa4dd815SMatthias Ringwald }
236aa4dd815SMatthias Ringwald
hfp_ag_send_no_carrier(uint16_t cid)237245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){
238865e2b54SMatthias Ringwald return send_str_over_rfcomm(cid, "\r\nNO CARRIER\r\n");
239245852b7SMilanka Ringwald }
240245852b7SMilanka Ringwald
hfp_ag_send_clip(uint16_t cid)241aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
242aa4dd815SMatthias Ringwald char buffer[50];
243865e2b54SMatthias Ringwald btstack_snprintf_best_effort(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP,
244ff7d6aeaSMatthias Ringwald hfp_gsm_clip_number(), hfp_gsm_clip_type());
245aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
246aa4dd815SMatthias Ringwald }
247aa4dd815SMatthias Ringwald
hfp_send_subscriber_number_cmd(uint16_t cid,uint8_t type,const char * number)248ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
249ce263fc8SMatthias Ringwald char buffer[50];
250865e2b54SMatthias Ringwald btstack_snprintf_best_effort(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n",
251ff7d6aeaSMatthias Ringwald HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
252ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
253ce263fc8SMatthias Ringwald }
254ce263fc8SMatthias Ringwald
hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid)255c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
256aa4dd815SMatthias Ringwald char buffer[50];
257865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: %s\r\n",
258ff7d6aeaSMatthias Ringwald HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
259aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
260aa4dd815SMatthias Ringwald }
261aa4dd815SMatthias Ringwald
hfp_ag_send_call_waiting_notification(uint16_t cid)262aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
263aa4dd815SMatthias Ringwald char buffer[50];
264865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n",
265ff7d6aeaSMatthias Ringwald HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(),
266ff7d6aeaSMatthias Ringwald hfp_gsm_clip_type());
267aa4dd815SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
268aa4dd815SMatthias Ringwald }
269aa4dd815SMatthias Ringwald
hfp_ag_send_error(uint16_t cid)270485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2713deb3ec6SMatthias Ringwald char buffer[10];
272865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\nERROR\r\n");
2733deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald
hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid,uint8_t error)276485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2773deb3ec6SMatthias Ringwald char buffer[20];
278865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s=%d\r\n",
279ff7d6aeaSMatthias Ringwald HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2803deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
2813deb3ec6SMatthias Ringwald }
2823deb3ec6SMatthias Ringwald
hfp_ag_send_apple_information(uint16_t cid)283edc46354SMatthias Ringwald static int hfp_ag_send_apple_information(uint16_t cid){
284edc46354SMatthias Ringwald char buffer[50];
285865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s:%s,%d\r\n",
286edc46354SMatthias Ringwald HFP_APPLE_ACCESSORY_INFORMATION, hfp_ag_apple_device, hfp_ag_apple_features);
287edc46354SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
288edc46354SMatthias Ringwald }
289edc46354SMatthias Ringwald
290ad902e3dSMatthias Ringwald // get size for indicator string
hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection,int i)291a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
292ad902e3dSMatthias Ringwald // template: ("$NAME",($MIN,$MAX))
29345718b6fSMatthias Ringwald return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
294a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
295a0ffb263SMatthias Ringwald + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
296ad902e3dSMatthias Ringwald }
297ad902e3dSMatthias Ringwald
298ad902e3dSMatthias Ringwald // store indicator
hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection,int i,uint8_t * buffer,uint16_t buffer_size)2991167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){
300865e2b54SMatthias Ringwald btstack_snprintf_assert_complete((char *)buffer, buffer_size, "(\"%s\",(%d,%d))",
301a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].name,
302a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
303a0ffb263SMatthias Ringwald hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
3043deb3ec6SMatthias Ringwald }
305ad902e3dSMatthias Ringwald
306ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection)307a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
308a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
309ad902e3dSMatthias Ringwald if (!num_indicators) return 2;
310c1ab6cc1SMatthias Ringwald return 3 + ((num_indicators-1) * 2);
3113deb3ec6SMatthias Ringwald }
312ad902e3dSMatthias Ringwald
313ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection,int index)314a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
315ad902e3dSMatthias Ringwald if (index == 0) {
316ab2445a0SMatthias Ringwald return (uint16_t) strlen(HFP_INDICATOR) + 3; // "\n\r%s:""
317ad902e3dSMatthias Ringwald }
318ad902e3dSMatthias Ringwald index--;
319a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
320ad902e3dSMatthias Ringwald int indicator_index = index >> 1;
321ad902e3dSMatthias Ringwald if ((index & 1) == 0){
322a0ffb263SMatthias Ringwald return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
323ad902e3dSMatthias Ringwald }
324c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators - 1)){
325ad902e3dSMatthias Ringwald return 8; // "\r\n\r\nOK\r\n"
326ad902e3dSMatthias Ringwald }
327ad902e3dSMatthias Ringwald return 1; // comma
328ad902e3dSMatthias Ringwald }
329ad902e3dSMatthias Ringwald
hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection,int index,uint8_t * buffer,uint16_t buffer_size)3301167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){
331ad902e3dSMatthias Ringwald if (index == 0){
332ad902e3dSMatthias Ringwald *buffer++ = '\r';
33374386ee0SMatthias Ringwald *buffer++ = '\n';
334ab2445a0SMatthias Ringwald int len = (uint16_t) strlen(HFP_INDICATOR);
3356535961aSMatthias Ringwald (void)memcpy(buffer, HFP_INDICATOR, len);
336ad902e3dSMatthias Ringwald buffer += len;
337ad902e3dSMatthias Ringwald *buffer++ = ':';
338ad902e3dSMatthias Ringwald return;
339ad902e3dSMatthias Ringwald }
340ad902e3dSMatthias Ringwald index--;
341a0ffb263SMatthias Ringwald int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
342ad902e3dSMatthias Ringwald int indicator_index = index >> 1;
343ad902e3dSMatthias Ringwald if ((index & 1) == 0){
3441167ff83SMatthias Ringwald hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size);
345ad902e3dSMatthias Ringwald return;
346ad902e3dSMatthias Ringwald }
347c1ab6cc1SMatthias Ringwald if (indicator_index == (num_indicators-1)){
3486535961aSMatthias Ringwald (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
349ad902e3dSMatthias Ringwald return;
350ad902e3dSMatthias Ringwald }
351ad902e3dSMatthias Ringwald *buffer = ',';
3523deb3ec6SMatthias Ringwald }
3533deb3ec6SMatthias Ringwald
hfp_ag_generic_indicators_join(char * buffer,int buffer_size)354aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){
355c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3563deb3ec6SMatthias Ringwald int i;
3573deb3ec6SMatthias Ringwald int offset = 0;
358aeb0f0feSMatthias Ringwald for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) {
359865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid);
3603deb3ec6SMatthias Ringwald }
361aeb0f0feSMatthias Ringwald if (i < hfp_ag_generic_status_indicators_nr){
362865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid);
3633deb3ec6SMatthias Ringwald }
3643deb3ec6SMatthias Ringwald return offset;
3653deb3ec6SMatthias Ringwald }
3663deb3ec6SMatthias Ringwald
hfp_ag_generic_indicators_initial_status_join(char * buffer,int buffer_size)367aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){
368aeb0f0feSMatthias Ringwald if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0;
3693deb3ec6SMatthias Ringwald int i;
3703deb3ec6SMatthias Ringwald int offset = 0;
371aeb0f0feSMatthias Ringwald for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) {
372865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_ag_generic_status_indicators[i].uuid, hfp_ag_generic_status_indicators[i].state);
3733deb3ec6SMatthias Ringwald }
3743deb3ec6SMatthias Ringwald return offset;
3753deb3ec6SMatthias Ringwald }
3763deb3ec6SMatthias Ringwald
hfp_ag_indicators_status_join(char * buffer,int buffer_size)3773deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
378c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3793deb3ec6SMatthias Ringwald int i;
3803deb3ec6SMatthias Ringwald int offset = 0;
381c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
382865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3833deb3ec6SMatthias Ringwald }
3843deb3ec6SMatthias Ringwald if (i<hfp_ag_indicators_nr){
385865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3863deb3ec6SMatthias Ringwald }
3873deb3ec6SMatthias Ringwald return offset;
3883deb3ec6SMatthias Ringwald }
3893deb3ec6SMatthias Ringwald
hfp_ag_call_services_join(char * buffer,int buffer_size)3903deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
391c1ab6cc1SMatthias Ringwald if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3923deb3ec6SMatthias Ringwald int i;
393865e2b54SMatthias Ringwald int offset = btstack_snprintf_assert_complete(buffer, buffer_size, "(");
394c1ab6cc1SMatthias Ringwald for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
395865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3963deb3ec6SMatthias Ringwald }
3973deb3ec6SMatthias Ringwald if (i<hfp_ag_call_hold_services_nr){
398865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3993deb3ec6SMatthias Ringwald }
4003deb3ec6SMatthias Ringwald return offset;
4013deb3ec6SMatthias Ringwald }
4023deb3ec6SMatthias Ringwald
hfp_ag_send_cmd_via_generator(uint16_t cid,hfp_connection_t * hfp_connection,int start_segment,int num_segments,int (* get_segment_len)(hfp_connection_t * hfp_connection,int segment),void (* store_segment)(hfp_connection_t * hfp_connection,int segment,uint8_t * buffer,uint16_t buffer_size))403485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
404ad902e3dSMatthias Ringwald int start_segment, int num_segments,
405a0ffb263SMatthias Ringwald int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
4061167ff83SMatthias Ringwald void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){
4073deb3ec6SMatthias Ringwald
408ad902e3dSMatthias Ringwald // assumes: can send now == true
409ad902e3dSMatthias Ringwald // assumes: num segments > 0
410aba39421SMatthias Ringwald // assumes: individual segments are smaller than MTU
411ad902e3dSMatthias Ringwald rfcomm_reserve_packet_buffer();
412ad902e3dSMatthias Ringwald int mtu = rfcomm_get_max_frame_size(cid);
413ad902e3dSMatthias Ringwald uint8_t * data = rfcomm_get_outgoing_buffer();
414ad902e3dSMatthias Ringwald int offset = 0;
415ad902e3dSMatthias Ringwald int segment = start_segment;
416ad902e3dSMatthias Ringwald while (segment < num_segments){
417a0ffb263SMatthias Ringwald int segment_len = get_segment_len(hfp_connection, segment);
4181167ff83SMatthias Ringwald if ((offset + segment_len + 1) > mtu) break;
4191167ff83SMatthias Ringwald // append segment. As it appends a '\0', we provide a buffer one byte larger
4201167ff83SMatthias Ringwald store_segment(hfp_connection, segment, data+offset, segment_len + 1);
421ad902e3dSMatthias Ringwald offset += segment_len;
422ad902e3dSMatthias Ringwald segment++;
423ad902e3dSMatthias Ringwald }
424ad902e3dSMatthias Ringwald rfcomm_send_prepared(cid, offset);
425f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
426f20dd2aeSBjoern Hartmann hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data);
427f20dd2aeSBjoern Hartmann #endif
428ad902e3dSMatthias Ringwald return segment;
429ad902e3dSMatthias Ringwald }
4303deb3ec6SMatthias Ringwald
431ad902e3dSMatthias Ringwald // returns next segment to store
hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid,hfp_connection_t * hfp_connection,int start_segment)432485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
433a0ffb263SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
434485ac19eSMilanka Ringwald return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
435894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_get_segment_len,
436894d499cSMatthias Ringwald hfp_ag_indicators_cmd_generator_store_segment);
4373deb3ec6SMatthias Ringwald }
4383deb3ec6SMatthias Ringwald
hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid)439485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4403deb3ec6SMatthias Ringwald char buffer[40];
44189425bfcSMilanka Ringwald const int size = sizeof(buffer);
442865e2b54SMatthias Ringwald int offset = btstack_snprintf_assert_complete(buffer, size, "\r\n%s:", HFP_INDICATOR);
44389425bfcSMilanka Ringwald offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
444865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4453deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
4463deb3ec6SMatthias Ringwald }
4473deb3ec6SMatthias Ringwald
hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid)448485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
449ad902e3dSMatthias Ringwald char buffer[40];
45089425bfcSMilanka Ringwald const int size = sizeof(buffer);
451865e2b54SMatthias Ringwald int offset = btstack_snprintf_assert_complete(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
45289425bfcSMilanka Ringwald offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
453865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4543deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
4553deb3ec6SMatthias Ringwald }
4563deb3ec6SMatthias Ringwald
4573deb3ec6SMatthias Ringwald
hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid)458485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
459485ac19eSMilanka Ringwald return hfp_ag_send_ok(cid);
4603deb3ec6SMatthias Ringwald }
4613deb3ec6SMatthias Ringwald
hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid)462485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4633deb3ec6SMatthias Ringwald char buffer[40];
46489425bfcSMilanka Ringwald const int size = sizeof(buffer);
465865e2b54SMatthias Ringwald int offset = btstack_snprintf_assert_complete(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
466aeb0f0feSMatthias Ringwald offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10);
467865e2b54SMatthias Ringwald offset += btstack_snprintf_assert_complete(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4683deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
4693deb3ec6SMatthias Ringwald }
4703deb3ec6SMatthias Ringwald
hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid)471485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4723deb3ec6SMatthias Ringwald char buffer[40];
473aeb0f0feSMatthias Ringwald int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
474865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4753deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
4763deb3ec6SMatthias Ringwald }
4773deb3ec6SMatthias Ringwald
hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid,hfp_ag_indicator_t * indicator)478485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4793deb3ec6SMatthias Ringwald char buffer[20];
480865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n",
481ff7d6aeaSMatthias Ringwald HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index,
482ff7d6aeaSMatthias Ringwald indicator->status);
4833deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
4843deb3ec6SMatthias Ringwald }
4853deb3ec6SMatthias Ringwald
hfp_ag_send_report_network_operator_name_cmd(uint16_t cid,hfp_network_opearator_t op)486485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4875e71456cSMilanka Ringwald char buffer[41];
4883deb3ec6SMatthias Ringwald if (strlen(op.name) == 0){
489865e2b54SMatthias Ringwald btstack_snprintf_best_effort(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4903deb3ec6SMatthias Ringwald } else {
491865e2b54SMatthias Ringwald int offset = btstack_snprintf_best_effort(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
492865e2b54SMatthias Ringwald offset += btstack_snprintf_best_effort(buffer+offset, 16, "%s", op.name);
493865e2b54SMatthias Ringwald btstack_snprintf_best_effort(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4943deb3ec6SMatthias Ringwald }
4953deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer);
4963deb3ec6SMatthias Ringwald }
4973deb3ec6SMatthias Ringwald
hfp_ag_send_cmd_with_space_and_int(uint16_t cid,const char * cmd,uint8_t value)498a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){
499a04aecb1SMilanka Ringwald char buffer[30];
500865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value);
501a04aecb1SMilanka Ringwald return send_str_over_rfcomm(cid, buffer);
502a04aecb1SMilanka Ringwald }
503a04aecb1SMilanka Ringwald
504a04aecb1SMilanka Ringwald
hfp_ag_send_cmd_with_int(uint16_t cid,const char * cmd,uint8_t value)5051cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
5061cc1d9e9SMilanka Ringwald char buffer[30];
507865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
5081cc1d9e9SMilanka Ringwald return send_str_over_rfcomm(cid, buffer);
5091cc1d9e9SMilanka Ringwald }
5103deb3ec6SMatthias Ringwald
hfp_ag_send_suggest_codec_cmd(uint16_t cid,uint8_t codec)511485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
5121cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
5133deb3ec6SMatthias Ringwald }
5143deb3ec6SMatthias Ringwald
hfp_ag_send_set_speaker_gain_cmd(uint16_t cid,uint8_t gain)515485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
5161cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
517aa4dd815SMatthias Ringwald }
518aa4dd815SMatthias Ringwald
hfp_ag_send_set_microphone_gain_cmd(uint16_t cid,uint8_t gain)519485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5201cc1d9e9SMilanka Ringwald return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
521aa4dd815SMatthias Ringwald }
522aa4dd815SMatthias Ringwald
hfp_ag_send_set_response_and_hold(uint16_t cid,int state)523485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
52466191aa0SMilanka Ringwald return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state);
525ce263fc8SMatthias Ringwald }
526ce263fc8SMatthias Ringwald
hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection)527cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){
528cf75be85SMilanka Ringwald char buffer[30];
529fcf4ede6SMilanka Ringwald uint8_t evra_enabled = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0;
530865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, evra_enabled, hfp_connection->ag_vra_state);
531cf75be85SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
532cf75be85SMilanka Ringwald }
533cf75be85SMilanka Ringwald
hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection,uint8_t activate_voice_recognition)534d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){
53545796ff1SMilanka Ringwald char buffer[30];
536d1b50446SMilanka Ringwald if (hfp_connection->enhanced_voice_recognition_enabled){
537865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state);
538d1b50446SMilanka Ringwald } else {
539865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
540d1b50446SMilanka Ringwald }
541d1b50446SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
54245796ff1SMilanka Ringwald }
54345796ff1SMilanka Ringwald
hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection)54445796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){
545a2e701c0SMilanka Ringwald char buffer[HFP_VR_TEXT_HEADER_SIZE + HFP_MAX_VR_TEXT_SIZE];
546865e2b54SMatthias Ringwald btstack_snprintf_assert_complete(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION,
54745796ff1SMilanka Ringwald hfp_connection->ag_vra_state,
54845796ff1SMilanka Ringwald hfp_connection->ag_msg.text_id,
54945796ff1SMilanka Ringwald hfp_connection->ag_msg.text_type,
55045796ff1SMilanka Ringwald hfp_connection->ag_msg.text_operation,
55145796ff1SMilanka Ringwald hfp_connection->ag_msg.text);
55245796ff1SMilanka Ringwald return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
55345796ff1SMilanka Ringwald }
55445796ff1SMilanka Ringwald
hfp_ag_suggest_codec(hfp_connection_t * hfp_connection)555a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
556bc1b1537SMilanka Ringwald if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
557bc1b1537SMilanka Ringwald
5582085619dSMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
5592085619dSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_ag_codecs_nr, hfp_ag_codecs)){
5602085619dSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
5612085619dSMatthias Ringwald return HFP_CODEC_LC3_SWB;
5622085619dSMatthias Ringwald }
5632085619dSMatthias Ringwald }
5642085619dSMatthias Ringwald #endif
5652085619dSMatthias Ringwald
5662085619dSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
567aeb0f0feSMatthias Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){
5686a7f44bdSMilanka Ringwald if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
569df327ff3SMilanka Ringwald return HFP_CODEC_MSBC;
5703deb3ec6SMatthias Ringwald }
5713deb3ec6SMatthias Ringwald }
5722085619dSMatthias Ringwald #endif
5732085619dSMatthias Ringwald
574df327ff3SMilanka Ringwald return HFP_CODEC_CVSD;
5753deb3ec6SMatthias Ringwald }
5763deb3ec6SMatthias Ringwald
57776cc1527SMatthias Ringwald /* state machines */
57876cc1527SMatthias Ringwald
hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection)579afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
580aeb0f0feSMatthias Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4));
5817522e673SMatthias Ringwald }
5827522e673SMatthias Ringwald
codecs_exchange_state_machine(hfp_connection_t * hfp_connection)583a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
584aa4dd815SMatthias Ringwald /* events ( == commands):
585aa4dd815SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
586aa4dd815SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
587aa4dd815SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC
588aa4dd815SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS
589aa4dd815SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
590aa4dd815SMatthias Ringwald */
591a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){
5923e3b9207SMilanka Ringwald case HFP_CODECS_EXCHANGED:
5933e3b9207SMilanka Ringwald if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){
5943e3b9207SMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5953e3b9207SMilanka Ringwald return 1;
5963e3b9207SMilanka Ringwald }
5973e3b9207SMilanka Ringwald break;
5983e3b9207SMilanka Ringwald
599aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
600aa4dd815SMatthias Ringwald case HFP_CODECS_AG_RESEND_COMMON_CODEC:
601a814da6aSMatthias Ringwald hfp_connection->ag_send_common_codec = true;
602aa4dd815SMatthias Ringwald break;
603aa4dd815SMatthias Ringwald default:
604aa4dd815SMatthias Ringwald break;
605aa4dd815SMatthias Ringwald }
606aa4dd815SMatthias Ringwald
607a814da6aSMatthias Ringwald if (hfp_connection->ag_send_common_codec){
608a814da6aSMatthias Ringwald hfp_connection->ag_send_common_codec = false;
609a814da6aSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
610a814da6aSMatthias Ringwald hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
611a814da6aSMatthias Ringwald hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
612a814da6aSMatthias Ringwald return 1;
613a814da6aSMatthias Ringwald }
614a814da6aSMatthias Ringwald
615a0ffb263SMatthias Ringwald switch (hfp_connection->command){
616aa4dd815SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS:
617a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
618a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
619485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
620aa4dd815SMatthias Ringwald return 1;
621aa4dd815SMatthias Ringwald }
622aa4dd815SMatthias Ringwald
623a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){
624aa4dd815SMatthias Ringwald case HFP_CODECS_AG_SENT_COMMON_CODEC:
625a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
626aa4dd815SMatthias Ringwald break;
627aa4dd815SMatthias Ringwald default:
628aa4dd815SMatthias Ringwald break;
629aa4dd815SMatthias Ringwald }
630485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
631aa4dd815SMatthias Ringwald return 1;
632aa4dd815SMatthias Ringwald
633aa4dd815SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
634a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
63540e8d6c5SMatthias Ringwald hfp_connection->establish_audio_connection = 1;
6362561704cSMatthias Ringwald hfp_connection->sco_for_msbc_failed = 0;
637485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
638aa4dd815SMatthias Ringwald return 1;
639aa4dd815SMatthias Ringwald
640aa4dd815SMatthias Ringwald case HFP_CMD_HF_CONFIRMED_CODEC:
641a0ffb263SMatthias Ringwald if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
642a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_ERROR;
643485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid);
644aa4dd815SMatthias Ringwald return 1;
645aa4dd815SMatthias Ringwald }
646a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
647a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
648c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
649485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
6507522e673SMatthias Ringwald // now, pick link settings
651afac2a04SMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
6520327df7dSMatthias Ringwald // configure SBC coded if needed
6530327df7dSMatthias Ringwald hfp_prepare_for_sco(hfp_connection);
654aa4dd815SMatthias Ringwald return 1;
655aa4dd815SMatthias Ringwald default:
656aa4dd815SMatthias Ringwald break;
657aa4dd815SMatthias Ringwald }
658aa4dd815SMatthias Ringwald return 0;
659aa4dd815SMatthias Ringwald }
660aa4dd815SMatthias Ringwald
hfp_ag_slc_established(hfp_connection_t * hfp_connection)661a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
662a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
6637095467fSMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
664aa4dd815SMatthias Ringwald
66540a8ee13SMatthias Ringwald // in-band ring tone is active if supported
66640a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = has_in_band_ring_tone();
66740a8ee13SMatthias Ringwald
66880702f97SMatthias Ringwald // HFP 4.35: "When [...] a new Service Level Connection is established all indicators are activated by default."
66980702f97SMatthias Ringwald uint16_t i;
67080702f97SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){
67180702f97SMatthias Ringwald hfp_connection->ag_indicators[i].enabled = 1;
67280702f97SMatthias Ringwald }
67380702f97SMatthias Ringwald
674a0ffb263SMatthias Ringwald // if active call exist, set per-hfp_connection state active, too (when audio is on)
675d0c20769SMatthias Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
676a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
677aa4dd815SMatthias Ringwald }
678ce263fc8SMatthias Ringwald // if AG is ringing, also start ringing on the HF
679c4f8fc1cSMilanka Ringwald if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
680c4f8fc1cSMilanka Ringwald switch (hfp_gsm_callsetup_status()){
681c4f8fc1cSMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
6822ff1d7d7SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection);
683c4f8fc1cSMilanka Ringwald break;
684fcd59412SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
685fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection);
686fcd59412SMilanka Ringwald break;
687c4f8fc1cSMilanka Ringwald default:
688c4f8fc1cSMilanka Ringwald break;
689c4f8fc1cSMilanka Ringwald }
690ce263fc8SMatthias Ringwald }
691aa4dd815SMatthias Ringwald }
6923deb3ec6SMatthias Ringwald
hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection)693a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
694c79b4cabSMatthias Ringwald // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
695a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
696c04cf7ffSMilanka Ringwald int sent = 0;
697a0ffb263SMatthias Ringwald switch(hfp_connection->command){
6983deb3ec6SMatthias Ringwald case HFP_CMD_SUPPORTED_FEATURES:
699a0ffb263SMatthias Ringwald switch(hfp_connection->state){
7003deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
701aa4dd815SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES:
702aeb0f0feSMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr);
703a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){
704a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
705aa4dd815SMatthias Ringwald } else {
706a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
707aa4dd815SMatthias Ringwald }
708a0ffb263SMatthias Ringwald hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
709aa4dd815SMatthias Ringwald return 1;
7103deb3ec6SMatthias Ringwald default:
7113deb3ec6SMatthias Ringwald break;
7123deb3ec6SMatthias Ringwald }
7133deb3ec6SMatthias Ringwald break;
7143deb3ec6SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS:
715c04cf7ffSMilanka Ringwald sent = codecs_exchange_state_machine(hfp_connection);
716a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
717a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
7183deb3ec6SMatthias Ringwald }
719c04cf7ffSMilanka Ringwald return sent;
720aa4dd815SMatthias Ringwald
721aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS:
722a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
723473ac565SMatthias Ringwald // HF requested AG Indicators and we did expect it
72484a0c24eSMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0;
725473ac565SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS;
726473ac565SMatthias Ringwald // continue below in state switch
727ad902e3dSMatthias Ringwald }
728ad902e3dSMatthias Ringwald break;
729aa4dd815SMatthias Ringwald
730aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
731a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
732a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
733485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
734aa4dd815SMatthias Ringwald return 1;
735aa4dd815SMatthias Ringwald
7363deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
737a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
738a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
739a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
740880e8fc6SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
741a0ffb263SMatthias Ringwald } else if (has_hf_indicators_feature(hfp_connection)){
742a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
743880e8fc6SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
744aa4dd815SMatthias Ringwald } else {
745880e8fc6SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
746a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection);
7473deb3ec6SMatthias Ringwald }
748aa4dd815SMatthias Ringwald return 1;
7493deb3ec6SMatthias Ringwald
750aa4dd815SMatthias Ringwald case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
751a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
752a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){
753a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
754aa4dd815SMatthias Ringwald }
755485ac19eSMilanka Ringwald hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
756a0ffb263SMatthias Ringwald if (!has_hf_indicators_feature(hfp_connection)){
757a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection);
758ce263fc8SMatthias Ringwald }
759aa4dd815SMatthias Ringwald return 1;
760aa4dd815SMatthias Ringwald
761aa4dd815SMatthias Ringwald case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
762a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
763a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
764485ac19eSMilanka Ringwald hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
765aa4dd815SMatthias Ringwald return 1;
766aa4dd815SMatthias Ringwald
767aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
768a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
769a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
770485ac19eSMilanka Ringwald hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
771aa4dd815SMatthias Ringwald return 1;
772aa4dd815SMatthias Ringwald
773aa4dd815SMatthias Ringwald case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
774a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
775485ac19eSMilanka Ringwald hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
776a7666b02SMatthias Ringwald hfp_ag_slc_established(hfp_connection);
777aa4dd815SMatthias Ringwald return 1;
7783deb3ec6SMatthias Ringwald default:
7793deb3ec6SMatthias Ringwald break;
7803deb3ec6SMatthias Ringwald }
781473ac565SMatthias Ringwald
782473ac565SMatthias Ringwald switch (hfp_connection->state){
783473ac565SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: {
784485ac19eSMilanka Ringwald int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
785473ac565SMatthias Ringwald int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
786473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
787473ac565SMatthias Ringwald if (next_segment < num_segments){
788473ac565SMatthias Ringwald // prepare sending of next segment
789473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = next_segment;
790473ac565SMatthias Ringwald log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
791473ac565SMatthias Ringwald } else {
792473ac565SMatthias Ringwald // done, go to next state
793473ac565SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0;
794473ac565SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
795473ac565SMatthias Ringwald }
796473ac565SMatthias Ringwald return 1;
797473ac565SMatthias Ringwald }
798473ac565SMatthias Ringwald default:
799473ac565SMatthias Ringwald break;
800473ac565SMatthias Ringwald }
801c04cf7ffSMilanka Ringwald return 0;
8023deb3ec6SMatthias Ringwald }
8033deb3ec6SMatthias Ringwald
hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection)8049cd26862SMilanka Ringwald static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){
80514c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
80614c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
80714c148caSMilanka Ringwald return hf && ag;
80814c148caSMilanka Ringwald }
80914c148caSMilanka Ringwald
hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection)8109cd26862SMilanka Ringwald static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
81114c148caSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
81214c148caSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
81314c148caSMilanka Ringwald return hf && ag;
81414c148caSMilanka Ringwald }
81514c148caSMilanka Ringwald
hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection)8169cd26862SMilanka Ringwald static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){
817f71d2aacSMilanka Ringwald int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT);
818f71d2aacSMilanka Ringwald int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT);
819f71d2aacSMilanka Ringwald return hf && ag;
82008a0b01cSMilanka Ringwald }
821f71d2aacSMilanka Ringwald
hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection)822135d9718SMilanka Ringwald static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){
823f71d2aacSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
824135d9718SMilanka Ringwald return false;
825d84719b1SMilanka Ringwald }
8260c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
827135d9718SMilanka Ringwald return false;
828d84719b1SMilanka Ringwald }
82908dc7cc6SMatthias Ringwald if (hfp_connection->ag_vra_send_command){
83008dc7cc6SMatthias Ringwald return false;
83108dc7cc6SMatthias Ringwald }
832135d9718SMilanka Ringwald return true;
833d84719b1SMilanka Ringwald }
834d84719b1SMilanka Ringwald
hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection)835135d9718SMilanka Ringwald static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){
83608a0b01cSMilanka Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
837135d9718SMilanka Ringwald return false;
83808a0b01cSMilanka Ringwald }
8390c738fd4SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
840de9e0ea7SMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
841135d9718SMilanka Ringwald return true;
842de9e0ea7SMilanka Ringwald }
843135d9718SMilanka Ringwald return false;
844d84719b1SMilanka Ringwald }
845135d9718SMilanka Ringwald return true;
846d84719b1SMilanka Ringwald }
847754910caSMilanka Ringwald
hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection)8480b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){
8490b4debbfSMilanka Ringwald switch (hfp_connection->state){
8500b4debbfSMilanka Ringwald case HFP_W2_CONNECT_SCO:
8510b4debbfSMilanka Ringwald case HFP_W4_SCO_CONNECTED:
8520b4debbfSMilanka Ringwald case HFP_AUDIO_CONNECTION_ESTABLISHED:
8530b4debbfSMilanka Ringwald return true;
854754910caSMilanka Ringwald default:
8550b4debbfSMilanka Ringwald return false;
856754910caSMilanka Ringwald }
857754910caSMilanka Ringwald }
858754910caSMilanka Ringwald
hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection,uint8_t status)859b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){
860b95cac54SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
861b95cac54SMilanka Ringwald
862b95cac54SMilanka Ringwald uint8_t event[6];
863b95cac54SMilanka Ringwald event[0] = HCI_EVENT_HFP_META;
864b95cac54SMilanka Ringwald event[1] = sizeof(event) - 2;
865c95b5b3cSMilanka Ringwald event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT;
866b95cac54SMilanka Ringwald little_endian_store_16(event, 3, acl_handle);
867b95cac54SMilanka Ringwald event[5] = status;
868b95cac54SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
869b95cac54SMilanka Ringwald }
870b95cac54SMilanka Ringwald
871347d46c8SMilanka Ringwald
hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection,uint16_t uuid,uint8_t value)872585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){
873347d46c8SMilanka Ringwald hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
874347d46c8SMilanka Ringwald
875347d46c8SMilanka Ringwald uint8_t event[8];
876347d46c8SMilanka Ringwald event[0] = HCI_EVENT_HFP_META;
877347d46c8SMilanka Ringwald event[1] = sizeof(event) - 2;
878347d46c8SMilanka Ringwald event[2] = HFP_SUBEVENT_HF_INDICATOR;
879347d46c8SMilanka Ringwald little_endian_store_16(event, 3, acl_handle);
880347d46c8SMilanka Ringwald little_endian_store_16(event, 5, uuid);
881347d46c8SMilanka Ringwald event[7] = value;
882347d46c8SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
883347d46c8SMilanka Ringwald }
884347d46c8SMilanka Ringwald
hfp_ag_emit_general_simple_event(uint8_t event_subtype)885a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){
886a4f85bd2SMilanka Ringwald if (!hfp_ag_callback) return;
887a4f85bd2SMilanka Ringwald
888a4f85bd2SMilanka Ringwald uint8_t event[5];
889a4f85bd2SMilanka Ringwald event[0] = HCI_EVENT_HFP_META;
890a4f85bd2SMilanka Ringwald event[1] = sizeof(event) - 2;
891a4f85bd2SMilanka Ringwald event[2] = event_subtype;
892a4f85bd2SMilanka Ringwald little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID);
893a4f85bd2SMilanka Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
894a4f85bd2SMilanka Ringwald }
895a4f85bd2SMilanka Ringwald
hfp_ag_emit_apple_accessory_information(hfp_connection_t * hfp_connection)896408b5fdbSMatthias Ringwald static void hfp_ag_emit_apple_accessory_information(hfp_connection_t *hfp_connection){
897959f2ebcSMatthias Ringwald uint8_t event[11 + HFP_APPLE_ACCESSORY_VERSION_LEN];
898959f2ebcSMatthias Ringwald uint8_t version_len = (uint8_t) strlen(hfp_connection->apple_accessory_version);
899edc46354SMatthias Ringwald event[0] = HCI_EVENT_HFP_META;
900edc46354SMatthias Ringwald event[1] = 9 + version_len;
901edc46354SMatthias Ringwald event[2] = HFP_SUBEVENT_APPLE_ACCESSORY_INFORMATION;
902edc46354SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle);
903edc46354SMatthias Ringwald little_endian_store_16(event, 5, hfp_connection->apple_accessory_vendor_id);
904edc46354SMatthias Ringwald little_endian_store_16(event, 7, hfp_connection->apple_accessory_product_id);
905edc46354SMatthias Ringwald event[9] = hfp_connection->apple_accessory_features;
906edc46354SMatthias Ringwald event[10] = version_len;
907edc46354SMatthias Ringwald memcpy(&event[11], hfp_connection->apple_accessory_version, version_len);
908edc46354SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 11 + version_len);
909edc46354SMatthias Ringwald }
910edc46354SMatthias Ringwald
hfp_ag_emit_apple_accessory_state(hfp_connection_t * hfp_connection)911408b5fdbSMatthias Ringwald static void hfp_ag_emit_apple_accessory_state(hfp_connection_t *hfp_connection){
912edc46354SMatthias Ringwald uint8_t event[40];
913edc46354SMatthias Ringwald event[0] = HCI_EVENT_HFP_META;
914edc46354SMatthias Ringwald event[1] = 2;
915edc46354SMatthias Ringwald if (hfp_connection->apple_accessory_battery_level >= 0){
916edc46354SMatthias Ringwald event[2] = HFP_SUBEVENT_APPLE_BATTERY_LEVEL;
917edc46354SMatthias Ringwald event[3] = hfp_connection->apple_accessory_battery_level;
918edc46354SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 4);
919edc46354SMatthias Ringwald }
920edc46354SMatthias Ringwald if (hfp_connection->apple_accessory_docked >= 0){
921edc46354SMatthias Ringwald event[2] = HFP_SUBEVENT_APPLE_DOCKED_STATE;
922edc46354SMatthias Ringwald event[3] = hfp_connection->apple_accessory_docked;
923edc46354SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 4);
924edc46354SMatthias Ringwald }
925edc46354SMatthias Ringwald }
926edc46354SMatthias Ringwald
hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection)927471dea41SMatthias Ringwald static void hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection){
928471dea41SMatthias Ringwald btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5));
929471dea41SMatthias Ringwald
9301a3bc516SMatthias Ringwald uint16_t line_len = (uint16_t) strlen((const char*)hfp_connection->line_buffer) + 1;
931471dea41SMatthias Ringwald uint8_t event[7 + sizeof(hfp_connection->line_buffer)];
932471dea41SMatthias Ringwald event[0] = HCI_EVENT_HFP_META;
933471dea41SMatthias Ringwald event[1] = 5 + line_len;
934471dea41SMatthias Ringwald event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND;
935471dea41SMatthias Ringwald little_endian_store_16(event, 3, hfp_connection->acl_handle);
9366d9a41f9SMatthias Ringwald little_endian_store_16(event, 5, hfp_connection->custom_at_command_id);
937471dea41SMatthias Ringwald memcpy(&event[7], hfp_connection->line_buffer, line_len);
938471dea41SMatthias Ringwald (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len);
939471dea41SMatthias Ringwald }
940471dea41SMatthias Ringwald
941774e3016SMatthias Ringwald // @return status
hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection)942774e3016SMatthias Ringwald static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){
943774e3016SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS;
944774e3016SMatthias Ringwald switch (hfp_connection->vra_state_requested){
945774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
946774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){
947774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection);
948774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){
949774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
950774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status);
951774e3016SMatthias Ringwald break;
952774e3016SMatthias Ringwald }
953774e3016SMatthias Ringwald }
954774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = true;
955774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
956774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
957774e3016SMatthias Ringwald hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
958774e3016SMatthias Ringwald break;
959774e3016SMatthias Ringwald
960774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
961774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
962774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
963774e3016SMatthias Ringwald
964774e3016SMatthias Ringwald // release audio connection only if it was opened after audio VR activated
965774e3016SMatthias Ringwald if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){
966774e3016SMatthias Ringwald hfp_trigger_release_audio_connection(hfp_connection);
967774e3016SMatthias Ringwald }
968774e3016SMatthias Ringwald
969774e3016SMatthias Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
970774e3016SMatthias Ringwald break;
971774e3016SMatthias Ringwald
972774e3016SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
973774e3016SMatthias Ringwald // open only if audio connection does not already exist
974774e3016SMatthias Ringwald if (!hfp_ag_is_audio_connection_active(hfp_connection)){
975774e3016SMatthias Ringwald status = hfp_ag_setup_audio_connection(hfp_connection);
976774e3016SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){
977774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
978774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, status);
979774e3016SMatthias Ringwald break;
980774e3016SMatthias Ringwald }
981774e3016SMatthias Ringwald }
982774e3016SMatthias Ringwald
983774e3016SMatthias Ringwald hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
984774e3016SMatthias Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
985774e3016SMatthias Ringwald hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
98684fb9ac1SMilanka Ringwald
98784fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
988774e3016SMatthias Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
98984fb9ac1SMilanka Ringwald } else {
99084fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = true;
99184fb9ac1SMilanka Ringwald }
992774e3016SMatthias Ringwald break;
993774e3016SMatthias Ringwald
994774e3016SMatthias Ringwald default:
995774e3016SMatthias Ringwald break;
996774e3016SMatthias Ringwald }
997774e3016SMatthias Ringwald return status;
998774e3016SMatthias Ringwald }
999774e3016SMatthias Ringwald
hfp_ag_vra_send_command(hfp_connection_t * hfp_connection)100008dc7cc6SMatthias Ringwald static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){
100108dc7cc6SMatthias Ringwald uint8_t done = 0;
1002754910caSMilanka Ringwald uint8_t status;
1003754910caSMilanka Ringwald
1004cf75be85SMilanka Ringwald switch (hfp_connection->vra_state_requested){
1005b95cac54SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG:
1006b95cac54SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection);
1007b95cac54SMilanka Ringwald if (done == 0){
1008b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1009b95cac54SMilanka Ringwald } else {
1010b95cac54SMilanka Ringwald hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS);
1011b95cac54SMilanka Ringwald }
1012b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
1013b95cac54SMilanka Ringwald return done;
1014b95cac54SMilanka Ringwald
1015cf75be85SMilanka Ringwald case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS:
1016cf75be85SMilanka Ringwald done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection);
1017cf75be85SMilanka Ringwald if (done == 0){
1018cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1019cf75be85SMilanka Ringwald } else {
1020cf75be85SMilanka Ringwald hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
1021cf75be85SMilanka Ringwald }
1022cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
1023b95cac54SMilanka Ringwald return done;
1024b95cac54SMilanka Ringwald
10256e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
10266e13e408SMatthias Ringwald case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1027d1b50446SMilanka Ringwald done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value);
10280c738fd4SMilanka Ringwald if (done == 0){
10290c738fd4SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
1030553a4a56SMilanka Ringwald
1031553a4a56SMilanka Ringwald if (hfp_connection->ag_activate_voice_recognition_value == 1){
1032553a4a56SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, done);
1033553a4a56SMilanka Ringwald } else {
1034553a4a56SMilanka Ringwald hfp_emit_voice_recognition_disabled(hfp_connection, done);
1035553a4a56SMilanka Ringwald }
1036fd4151d1SMilanka Ringwald return 0;
1037fd4151d1SMilanka Ringwald }
103808dc7cc6SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection);
103908dc7cc6SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0;
104008dc7cc6SMatthias Ringwald
10416e13e408SMatthias Ringwald default:
10426e13e408SMatthias Ringwald log_error("state %u", hfp_connection->vra_state_requested);
10436e13e408SMatthias Ringwald btstack_assert(false);
104408dc7cc6SMatthias Ringwald return 0;
1045cf75be85SMilanka Ringwald }
104608dc7cc6SMatthias Ringwald }
104708dc7cc6SMatthias Ringwald
hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection)104808dc7cc6SMatthias Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
104908dc7cc6SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
105008dc7cc6SMatthias Ringwald return 0;
105108dc7cc6SMatthias Ringwald }
105208dc7cc6SMatthias Ringwald int done = 0;
105308dc7cc6SMatthias Ringwald uint8_t status;
105408dc7cc6SMatthias Ringwald
10558f8818a4SMatthias Ringwald // VRA action initiated by AG
10568f8818a4SMatthias Ringwald if (hfp_connection->ag_vra_send_command){
10578f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = false;
10588f8818a4SMatthias Ringwald return hfp_ag_vra_send_command(hfp_connection);
10598f8818a4SMatthias Ringwald }
10608f8818a4SMatthias Ringwald
1061e7c46708SMatthias Ringwald if (hfp_connection->ag_vra_requested_by_hf){
1062e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = false;
1063e7c46708SMatthias Ringwald
1064734ef2bdSMilanka Ringwald // HF initiatied voice recognition, parser extracted activation value
1065de9e0ea7SMilanka Ringwald switch (hfp_connection->ag_activate_voice_recognition_value){
1066de9e0ea7SMilanka Ringwald case 0:
1067135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){
1068fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1069fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1070fd4151d1SMilanka Ringwald }
1071de9e0ea7SMilanka Ringwald break;
1072de9e0ea7SMilanka Ringwald
1073de9e0ea7SMilanka Ringwald case 1:
1074135d9718SMilanka Ringwald if (hfp_ag_can_activate_voice_recognition(hfp_connection)){
1075fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1076fd4151d1SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1077fd4151d1SMilanka Ringwald }
1078de9e0ea7SMilanka Ringwald break;
1079de9e0ea7SMilanka Ringwald
1080de9e0ea7SMilanka Ringwald case 2:
1081135d9718SMilanka Ringwald if (hfp_ag_voice_recognition_session_active(hfp_connection)){
1082de9e0ea7SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1083de9e0ea7SMilanka Ringwald done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1084de9e0ea7SMilanka Ringwald }
1085de9e0ea7SMilanka Ringwald break;
1086de9e0ea7SMilanka Ringwald default:
1087de9e0ea7SMilanka Ringwald break;
1088fd4151d1SMilanka Ringwald }
1089fd4151d1SMilanka Ringwald if (done == 0){
1090fd4151d1SMilanka Ringwald hfp_connection->vra_state_requested = hfp_connection->vra_state;
1091fd4151d1SMilanka Ringwald done = hfp_ag_send_error(hfp_connection->rfcomm_cid);
1092fd4151d1SMilanka Ringwald return 1;
1093fd4151d1SMilanka Ringwald }
1094754910caSMilanka Ringwald
1095774e3016SMatthias Ringwald status = hfp_ag_vra_state_machine_two(hfp_connection);
10962cc52945SMatthias Ringwald return (status == ERROR_CODE_SUCCESS) ? 1 : 0;
1097de9e0ea7SMilanka Ringwald }
10982cc52945SMatthias Ringwald
10992cc52945SMatthias Ringwald return 0;
1100754910caSMilanka Ringwald }
1101754910caSMilanka Ringwald
hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection)1102a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
1103dc6af379SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
1104dc6af379SMatthias Ringwald return 0;
1105dc6af379SMatthias Ringwald }
1106dc6af379SMatthias Ringwald
1107c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection);
1108c04cf7ffSMilanka Ringwald if (sent) return 1;
1109aa4dd815SMatthias Ringwald
111040a8ee13SMatthias Ringwald if (has_in_band_ring_tone()){
111140a8ee13SMatthias Ringwald if (hfp_connection->ag_in_band_ring_tone_active != hfp_ag_in_band_ring_tone_active) {
111240a8ee13SMatthias Ringwald hfp_connection->ag_in_band_ring_tone_active = hfp_ag_in_band_ring_tone_active;
1113f197e761SMatthias Ringwald hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection);
1114aa4dd815SMatthias Ringwald return 1;
11159430c71eSMatthias Ringwald }
111640a8ee13SMatthias Ringwald }
11179430c71eSMatthias Ringwald
11189430c71eSMatthias Ringwald switch(hfp_connection->command){
1119aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1120485ac19eSMilanka Ringwald hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
1121aa4dd815SMatthias Ringwald return 1;
1122aa4dd815SMatthias Ringwald case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1123a0ffb263SMatthias Ringwald if (hfp_connection->network_operator.format != 0){
1124485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid);
1125aa4dd815SMatthias Ringwald } else {
1126485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
11273deb3ec6SMatthias Ringwald }
1128aa4dd815SMatthias Ringwald return 1;
1129aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1130485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1131aa4dd815SMatthias Ringwald return 1;
11323deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1133df9853c5SMatthias Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1134df9853c5SMatthias Ringwald return 1;
11353deb3ec6SMatthias Ringwald case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1136485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1137ce263fc8SMatthias Ringwald return 1;
11383deb3ec6SMatthias Ringwald default:
11393deb3ec6SMatthias Ringwald break;
11403deb3ec6SMatthias Ringwald }
1141aa4dd815SMatthias Ringwald return 0;
11423deb3ec6SMatthias Ringwald }
11433deb3ec6SMatthias Ringwald
hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection)1144a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
1145505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
1146505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
11473deb3ec6SMatthias Ringwald
1148a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
114952cbdd6dSMilanka Ringwald
115052cbdd6dSMilanka Ringwald if (hfp_connection->release_audio_connection){
115152cbdd6dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
115252cbdd6dSMilanka Ringwald hfp_connection->release_audio_connection = 0;
115352cbdd6dSMilanka Ringwald gap_disconnect(hfp_connection->sco_handle);
115452cbdd6dSMilanka Ringwald return 1;
115552cbdd6dSMilanka Ringwald }
1156aa4dd815SMatthias Ringwald
115729cddf58SMatthias Ringwald // accept incoming audio connection (codec negotiation is not used)
1158447743f7SMatthias Ringwald if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){
115929cddf58SMatthias Ringwald // notify about codec selection if not done already
116029cddf58SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){
116129cddf58SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
116229cddf58SMatthias Ringwald }
116329cddf58SMatthias Ringwald //
116429cddf58SMatthias Ringwald bool incoming_eSCO = hfp_connection->accept_sco == 2;
116529cddf58SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED;
116629cddf58SMatthias Ringwald hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
116729cddf58SMatthias Ringwald return 1;
116829cddf58SMatthias Ringwald }
116929cddf58SMatthias Ringwald
1170aa4dd815SMatthias Ringwald // run codecs exchange
1171c04cf7ffSMilanka Ringwald int sent = codecs_exchange_state_machine(hfp_connection);
1172c04cf7ffSMilanka Ringwald if (sent) return 1;
1173aa4dd815SMatthias Ringwald
1174c04cf7ffSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
1175447743f7SMatthias Ringwald if (hfp_sco_setup_active()) return 0;
1176d2e34ffbSMatthias Ringwald if (hci_can_send_command_packet_now() == false) return 0;
1177a0ffb263SMatthias Ringwald if (hfp_connection->establish_audio_connection){
1178a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED;
1179a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0;
1180eddcd308SMatthias Ringwald hfp_setup_synchronous_connection(hfp_connection);
1181aa4dd815SMatthias Ringwald return 1;
1182aa4dd815SMatthias Ringwald }
1183aa4dd815SMatthias Ringwald return 0;
1184aa4dd815SMatthias Ringwald }
1185aa4dd815SMatthias Ringwald
hfp_ag_set_callsetup_indicator(void)1186056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1187056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1188056efd45SMatthias Ringwald if (!indicator){
1189056efd45SMatthias Ringwald log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1190056efd45SMatthias Ringwald return;
1191056efd45SMatthias Ringwald };
1192056efd45SMatthias Ringwald indicator->status = hfp_gsm_callsetup_status();
1193056efd45SMatthias Ringwald }
1194056efd45SMatthias Ringwald
hfp_ag_set_callheld_indicator(void)1195056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1196056efd45SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1197056efd45SMatthias Ringwald if (!indicator){
1198056efd45SMatthias Ringwald log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1199056efd45SMatthias Ringwald return;
1200056efd45SMatthias Ringwald };
1201056efd45SMatthias Ringwald indicator->status = hfp_gsm_callheld_status();
1202056efd45SMatthias Ringwald }
1203056efd45SMatthias Ringwald
1204aa4dd815SMatthias Ringwald //
1205fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine
1206aa4dd815SMatthias Ringwald //
1207aa4dd815SMatthias Ringwald
hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection)12082ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){
1209f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){
1210a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
1211d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1212aa4dd815SMatthias Ringwald } else {
12139493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING;
1214aa4dd815SMatthias Ringwald }
1215aa4dd815SMatthias Ringwald }
1216aa4dd815SMatthias Ringwald
hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection)1217fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){
1218fcd59412SMilanka Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1219fcd59412SMilanka Ringwald }
1220fcd59412SMilanka Ringwald
hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection)1221a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
1222a0ffb263SMatthias Ringwald hfp_connection->ag_ring = 0;
1223a0ffb263SMatthias Ringwald hfp_connection->ag_send_clip = 0;
1224aa4dd815SMatthias Ringwald }
1225aa4dd815SMatthias Ringwald
1226a4f85bd2SMilanka Ringwald
hfp_ag_trigger_incoming_call_during_active_one(void)122731f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){
122831f55b91SMilanka Ringwald btstack_linked_list_iterator_t it;
122931f55b91SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
123031f55b91SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){
123131f55b91SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
123231f55b91SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
123331f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue;
123431f55b91SMilanka Ringwald
123531f55b91SMilanka Ringwald hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
123631f55b91SMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
123731f55b91SMilanka Ringwald }
123831f55b91SMilanka Ringwald }
123931f55b91SMilanka Ringwald
hfp_ag_trigger_ring_and_clip(void)1240d3709f3eSMatthias Ringwald static void hfp_ag_trigger_ring_and_clip(void) {
1241d3709f3eSMatthias Ringwald btstack_linked_list_iterator_t it;
1242d3709f3eSMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1243d3709f3eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1244d3709f3eSMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1245d3709f3eSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1246d3709f3eSMatthias Ringwald switch (hfp_connection->call_state){
1247d3709f3eSMatthias Ringwald case HFP_CALL_INCOMING_RINGING:
1248d3709f3eSMatthias Ringwald case HFP_CALL_OUTGOING_RINGING:
1249d3709f3eSMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
12503561686eSMatthias Ringwald // Queue RING on this connection
1251a4f85bd2SMilanka Ringwald hfp_connection->ag_ring = 1;
12523561686eSMatthias Ringwald
12533561686eSMatthias Ringwald // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification
12543561686eSMatthias Ringwald // "If the calling subscriber number information is available from the network, the AG shall issue the
12553561686eSMatthias Ringwald // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call."
12563561686eSMatthias Ringwald hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
12573561686eSMatthias Ringwald
12583561686eSMatthias Ringwald // trigger next message
1259a4f85bd2SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1260b7e27351SMilanka Ringwald break;
1261b7e27351SMilanka Ringwald default:
1262b7e27351SMilanka Ringwald break;
1263b7e27351SMilanka Ringwald }
1264a4f85bd2SMilanka Ringwald }
1265a4f85bd2SMilanka Ringwald }
1266a4f85bd2SMilanka Ringwald
126750b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing
hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer)126850b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){
126950b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip();
127050b9afa4SMilanka Ringwald
12716db648bcSMilanka Ringwald btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout
127250b9afa4SMilanka Ringwald btstack_run_loop_add_timer(timer);
1273a4f85bd2SMilanka Ringwald }
1274a4f85bd2SMilanka Ringwald
hfp_ag_ring_timeout_stop(void)1275a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){
1276a4f85bd2SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout);
1277a4f85bd2SMilanka Ringwald }
1278a4f85bd2SMilanka Ringwald
hfp_ag_start_ringing(void)1279a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){
128050b9afa4SMilanka Ringwald // setup ring timer
128150b9afa4SMilanka Ringwald btstack_run_loop_remove_timer(&hfp_ag_ring_timeout);
128250b9afa4SMilanka Ringwald btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler);
12836db648bcSMilanka Ringwald btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout
128450b9afa4SMilanka Ringwald btstack_run_loop_add_timer(&hfp_ag_ring_timeout);
128550b9afa4SMilanka Ringwald
128650b9afa4SMilanka Ringwald // emit start ringing
1287a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING);
128850b9afa4SMilanka Ringwald
128950b9afa4SMilanka Ringwald // send initial RING + CLIP
129050b9afa4SMilanka Ringwald hfp_ag_trigger_ring_and_clip();
1291a4f85bd2SMilanka Ringwald }
1292a4f85bd2SMilanka Ringwald
hfp_ag_stop_ringing(void)1293a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){
1294a4f85bd2SMilanka Ringwald hfp_ag_ring_timeout_stop();
1295a4f85bd2SMilanka Ringwald hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING);
1296a4f85bd2SMilanka Ringwald
1297a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_t it;
1298a4f85bd2SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1299a4f85bd2SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1300a4f85bd2SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1301a4f85bd2SMilanka Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1302a4f85bd2SMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1303a4f85bd2SMilanka Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1304a4f85bd2SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection);
1305a4f85bd2SMilanka Ringwald }
1306a4f85bd2SMilanka Ringwald }
1307a4f85bd2SMilanka Ringwald
hfp_ag_trigger_incoming_call_idle(void)130831f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){
1309aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name("callsetup");
1310aa4dd815SMatthias Ringwald if (indicator_index < 0) return;
1311aa4dd815SMatthias Ringwald
13123561686eSMatthias Ringwald // update callsetup state in connections
1313665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1314665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1315665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1316a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
131766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
131831f55b91SMilanka Ringwald if (hfp_connection->call_state != HFP_CALL_IDLE) continue;
131931f55b91SMilanka Ringwald
1320a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1321a4f85bd2SMilanka Ringwald
1322a4f85bd2SMilanka Ringwald hfp_ag_hf_start_ringing_incoming(hfp_connection);
1323a4f85bd2SMilanka Ringwald
1324f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1325aa4dd815SMatthias Ringwald }
13263561686eSMatthias Ringwald
13273561686eSMatthias Ringwald // start local ringing - started after connection state has been updated
13283561686eSMatthias Ringwald hfp_ag_start_ringing();
1329aa4dd815SMatthias Ringwald }
1330aa4dd815SMatthias Ringwald
hfp_ag_trigger_outgoing_call(void)1331fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){
1332fe899794SMatthias Ringwald btstack_linked_list_iterator_t it;
1333fe899794SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1334fe899794SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1335fe899794SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1336fe899794SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1337fe899794SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1338fe899794SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE){
1339fe899794SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
1340fe899794SMatthias Ringwald }
1341fe899794SMatthias Ringwald }
1342fe899794SMatthias Ringwald }
13435a7033e2SMatthias Ringwald
hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection)13445a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){
13455a7033e2SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
13465a7033e2SMatthias Ringwald
13475a7033e2SMatthias Ringwald // trigger callsetup to be
13485a7033e2SMatthias Ringwald int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
13495a7033e2SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL);
13505a7033e2SMatthias Ringwald
13515a7033e2SMatthias Ringwald int indicator_index ;
13525a7033e2SMatthias Ringwald
13535a7033e2SMatthias Ringwald hfp_ag_set_callsetup_indicator();
13545a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup");
13555a7033e2SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
13565a7033e2SMatthias Ringwald
13575a7033e2SMatthias Ringwald // put current call on hold if active
13585a7033e2SMatthias Ringwald if (put_call_on_hold){
13595a7033e2SMatthias Ringwald log_info("AG putting current call on hold for new outgoing call");
13605a7033e2SMatthias Ringwald hfp_ag_set_callheld_indicator();
13615a7033e2SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callheld");
13625a7033e2SMatthias Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
13635a7033e2SMatthias Ringwald }
13645a7033e2SMatthias Ringwald
13655a7033e2SMatthias Ringwald // start audio if needed
13665a7033e2SMatthias Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
13675a7033e2SMatthias Ringwald }
1368fe899794SMatthias Ringwald
hfp_ag_transfer_indicator(const char * name)13694a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){
13704a2e058fSMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name);
1371aa4dd815SMatthias Ringwald if (indicator_index < 0) return;
1372aa4dd815SMatthias Ringwald
1373665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1374665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1375665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1376a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
137766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1378a0ffb263SMatthias Ringwald hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1379a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1380f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1381aa4dd815SMatthias Ringwald }
1382aa4dd815SMatthias Ringwald }
1383aa4dd815SMatthias Ringwald
hfp_ag_transfer_callsetup_state(void)13844a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
13854a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callsetup");
13864a2e058fSMatthias Ringwald }
13874a2e058fSMatthias Ringwald
hfp_ag_transfer_call_state(void)1388aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
13894a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("call");
1390aa4dd815SMatthias Ringwald }
1391aa4dd815SMatthias Ringwald
hfp_ag_transfer_callheld_state(void)1392aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
13934a2e058fSMatthias Ringwald hfp_ag_transfer_indicator("callheld");
1394aa4dd815SMatthias Ringwald }
1395aa4dd815SMatthias Ringwald
hfp_ag_hf_accept_call(hfp_connection_t * source)1396aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
1397aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call");
1398aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1399aa4dd815SMatthias Ringwald
1400a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing();
1401a4f85bd2SMilanka Ringwald
1402665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1403665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1404665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1405a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
140666c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
14079493406bSMilanka Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1408505f1c30SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1409aa4dd815SMatthias Ringwald
1410a0ffb263SMatthias Ringwald if (hfp_connection == source){
1411aa4dd815SMatthias Ringwald
1412f197e761SMatthias Ringwald if (use_in_band_tone(hfp_connection)){
1413a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
1414aa4dd815SMatthias Ringwald } else {
1415a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
1416d97d752dSMilanka Ringwald hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1417aa4dd815SMatthias Ringwald }
1418aa4dd815SMatthias Ringwald
1419a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1420a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1421aa4dd815SMatthias Ringwald
1422aa4dd815SMatthias Ringwald } else {
1423a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE;
1424aa4dd815SMatthias Ringwald }
1425f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1426aa4dd815SMatthias Ringwald }
1427aa4dd815SMatthias Ringwald }
1428aa4dd815SMatthias Ringwald
hfp_ag_ag_accept_call(void)1429aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
1430aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call");
1431aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1432aa4dd815SMatthias Ringwald
1433a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing();
1434a4f85bd2SMilanka Ringwald
1435665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1436665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1437665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1438a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
143966c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1440a5764cd2SMatthias Ringwald if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1441a5764cd2SMatthias Ringwald (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1442aa4dd815SMatthias Ringwald
1443a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
1444aa4dd815SMatthias Ringwald
1445a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1446a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1447aa4dd815SMatthias Ringwald
1448f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1449aa4dd815SMatthias Ringwald break; // only single
1450aa4dd815SMatthias Ringwald }
1451aa4dd815SMatthias Ringwald }
1452aa4dd815SMatthias Ringwald
hfp_ag_trigger_reject_call(void)1453aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
1454aa4dd815SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1455a4f85bd2SMilanka Ringwald
1456a4f85bd2SMilanka Ringwald hfp_ag_stop_ringing();
1457a4f85bd2SMilanka Ringwald
1458665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1459665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1460665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1461665d90f2SMatthias Ringwald hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
146266c5995fSMatthias Ringwald if (connection->local_role != HFP_ROLE_AG) continue;
146307a44c5eSMilanka Ringwald
146407a44c5eSMilanka Ringwald switch (connection->call_state){
146507a44c5eSMilanka Ringwald case HFP_CALL_INCOMING_RINGING:
146607a44c5eSMilanka Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
146707a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_RINGING:
146807a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_INITIATED:
146907a44c5eSMilanka Ringwald case HFP_CALL_OUTGOING_DIALING:
147007a44c5eSMilanka Ringwald break;
147107a44c5eSMilanka Ringwald default:
147207a44c5eSMilanka Ringwald continue;
147307a44c5eSMilanka Ringwald }
1474095a9f2bSMilanka Ringwald
1475aa4dd815SMatthias Ringwald connection->call_state = HFP_CALL_IDLE;
1476095a9f2bSMilanka Ringwald connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1477095a9f2bSMilanka Ringwald
1478095a9f2bSMilanka Ringwald if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1479095a9f2bSMilanka Ringwald connection->release_audio_connection = 1;
1480095a9f2bSMilanka Ringwald }
1481095a9f2bSMilanka Ringwald hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED);
1482f0aeb307SMatthias Ringwald hfp_ag_run_for_context(connection);
1483095a9f2bSMilanka Ringwald rfcomm_request_can_send_now_event(connection->rfcomm_cid);
1484aa4dd815SMatthias Ringwald }
1485aa4dd815SMatthias Ringwald }
1486aa4dd815SMatthias Ringwald
hfp_ag_trigger_terminate_call(void)1487aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1488aa4dd815SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call");
1489aa4dd815SMatthias Ringwald
1490a4f85bd2SMilanka Ringwald // no ringing during call
1491a4f85bd2SMilanka Ringwald
1492665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1493665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1494665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1495a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
149666c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1497a0ffb263SMatthias Ringwald if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1498fb75b483SMilanka Ringwald
1499a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE;
1500a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1501095a9f2bSMilanka Ringwald
15026d78145cSMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1503a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 1;
15046d78145cSMatthias Ringwald }
1505095a9f2bSMilanka Ringwald
1506fb75b483SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1507f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1508a13ff7d1SMilanka Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1509aa4dd815SMatthias Ringwald }
1510aa4dd815SMatthias Ringwald }
1511aa4dd815SMatthias Ringwald
hfp_ag_set_call_indicator(void)15120cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1513aa4dd815SMatthias Ringwald hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1514aa4dd815SMatthias Ringwald if (!indicator){
1515aa4dd815SMatthias Ringwald log_error("hfp_ag_set_call_state: call indicator is missing");
151645718b6fSMatthias Ringwald return;
1517aa4dd815SMatthias Ringwald };
1518d210d9c4SMatthias Ringwald indicator->status = hfp_gsm_call_status();
1519aa4dd815SMatthias Ringwald }
1520aa4dd815SMatthias Ringwald
hfp_ag_connection_for_call_state(hfp_call_state_t call_state)1521aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1522665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1523665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1524665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1525a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
152666c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1527a0ffb263SMatthias Ringwald if (hfp_connection->call_state == call_state) return hfp_connection;
1528aa4dd815SMatthias Ringwald }
1529aa4dd815SMatthias Ringwald return NULL;
1530aa4dd815SMatthias Ringwald }
1531aa4dd815SMatthias Ringwald
hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state)1532a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1533665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
1534665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
1535665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
1536a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
153766c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1538a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = state + 1;
1539ce263fc8SMatthias Ringwald }
1540ce263fc8SMatthias Ringwald }
1541ce263fc8SMatthias Ringwald
call_setup_state_machine(hfp_connection_t * hfp_connection)1542a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1543aa4dd815SMatthias Ringwald int indicator_index;
1544a0ffb263SMatthias Ringwald switch (hfp_connection->call_state){
1545aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1546a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1547a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established
15489493406bSMilanka Ringwald hfp_connection->call_state = HFP_CALL_INCOMING_RINGING;
1549aa4dd815SMatthias Ringwald break;
1550aa4dd815SMatthias Ringwald case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1551a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1552a0ffb263SMatthias Ringwald // we got event: audio hfp_connection established
1553a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
1554aa4dd815SMatthias Ringwald break;
1555aa4dd815SMatthias Ringwald case HFP_CALL_W2_SEND_CALL_WAITING:
1556a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_W4_CHLD;
1557a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1558aa4dd815SMatthias Ringwald indicator_index = get_ag_indicator_index_for_name("callsetup");
1559a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1560aa4dd815SMatthias Ringwald break;
1561aa4dd815SMatthias Ringwald default:
1562aa4dd815SMatthias Ringwald break;
1563aa4dd815SMatthias Ringwald }
1564aa4dd815SMatthias Ringwald return 0;
1565aa4dd815SMatthias Ringwald }
156639426febSMatthias Ringwald
1567e84fa067SMatthias Ringwald // queue 'error' to HF
hfp_ag_queue_error(hfp_connection_t * hfp_connection)1568e84fa067SMatthias Ringwald static void hfp_ag_queue_error(hfp_connection_t * hfp_connection){
15695e87d1ceSMatthias Ringwald hfp_connection->send_error++;
1570e84fa067SMatthias Ringwald }
1571e84fa067SMatthias Ringwald
1572e84fa067SMatthias Ringwald // queue 'ok' to HF
hfp_ag_queue_ok(hfp_connection_t * hfp_connection)1573e84fa067SMatthias Ringwald static void hfp_ag_queue_ok(hfp_connection_t * hfp_connection){
15745e87d1ceSMatthias Ringwald hfp_connection->ok_pending++;
1575e84fa067SMatthias Ringwald }
1576e84fa067SMatthias Ringwald
157739426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
hfp_ag_handle_reject_outgoing_call(void)15789f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){
157939426febSMatthias Ringwald hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
158039426febSMatthias Ringwald if (!hfp_connection){
158139426febSMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
158239426febSMatthias Ringwald return;
158339426febSMatthias Ringwald }
158439426febSMatthias Ringwald
1585f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL);
158639426febSMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE;
1587e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
158839426febSMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
158939426febSMatthias Ringwald }
159039426febSMatthias Ringwald
1591a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
hfp_ag_call_sm(hfp_ag_call_event_t event,hfp_connection_t * hfp_connection)1592a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
159381e25d0aSMatthias Ringwald
159481e25d0aSMatthias Ringwald // allow to intercept call statemachine events
159581e25d0aSMatthias Ringwald if (hfp_ag_custom_call_sm_handler != NULL){
159681e25d0aSMatthias Ringwald bool handle_event = (*hfp_ag_custom_call_sm_handler)(event);
159781e25d0aSMatthias Ringwald if (!handle_event) return;
159881e25d0aSMatthias Ringwald }
159981e25d0aSMatthias Ringwald
1600d210d9c4SMatthias Ringwald int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1601d210d9c4SMatthias Ringwald int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1602d210d9c4SMatthias Ringwald int call_indicator_index = get_ag_indicator_index_for_name("call");
160374386ee0SMatthias Ringwald
1604aa4dd815SMatthias Ringwald switch (event){
1605aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL:
1606d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1607aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1608d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
160912cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1610f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1611d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
161231f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_idle();
161360ebb071SMilanka Ringwald log_info("AG rings");
1614aa4dd815SMatthias Ringwald break;
1615aa4dd815SMatthias Ringwald default:
16163deb3ec6SMatthias Ringwald break;
16173deb3ec6SMatthias Ringwald }
16183deb3ec6SMatthias Ringwald break;
1619aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1620d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
162112cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1622f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1623d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
162431f55b91SMilanka Ringwald hfp_ag_trigger_incoming_call_during_active_one();
162560ebb071SMilanka Ringwald log_info("AG call waiting");
1626aa4dd815SMatthias Ringwald break;
1627aa4dd815SMatthias Ringwald default:
16283deb3ec6SMatthias Ringwald break;
16293deb3ec6SMatthias Ringwald }
16303deb3ec6SMatthias Ringwald break;
16317bbeb3adSMilanka Ringwald default:
16327bbeb3adSMilanka Ringwald break;
1633aa4dd815SMatthias Ringwald }
1634aa4dd815SMatthias Ringwald break;
1635aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1636d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1637aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1638d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
163912cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1640f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1641d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1642d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1643aa4dd815SMatthias Ringwald hfp_ag_ag_accept_call();
164460ebb071SMilanka Ringwald log_info("AG answers call, accept call by GSM");
1645aa4dd815SMatthias Ringwald break;
1646aa4dd815SMatthias Ringwald default:
16473deb3ec6SMatthias Ringwald break;
16483deb3ec6SMatthias Ringwald }
1649aa4dd815SMatthias Ringwald break;
1650aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1651d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
1652aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
165360ebb071SMilanka Ringwald log_info("AG: current call is placed on hold, incoming call gets active");
1654f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1655d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1656d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
1657ce263fc8SMatthias Ringwald hfp_ag_transfer_callsetup_state();
1658ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state();
1659aa4dd815SMatthias Ringwald break;
1660aa4dd815SMatthias Ringwald default:
1661aa4dd815SMatthias Ringwald break;
1662aa4dd815SMatthias Ringwald }
1663aa4dd815SMatthias Ringwald break;
16647bbeb3adSMilanka Ringwald default:
16657bbeb3adSMilanka Ringwald break;
1666aa4dd815SMatthias Ringwald }
1667aa4dd815SMatthias Ringwald break;
1668ce263fc8SMatthias Ringwald
1669ce263fc8SMatthias Ringwald case HFP_AG_HELD_CALL_JOINED_BY_AG:
1670d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1671ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1672d0c20769SMatthias Ringwald switch (hfp_gsm_callheld_status()){
1673ce263fc8SMatthias Ringwald case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
167460ebb071SMilanka Ringwald log_info("AG: joining held call with active call");
1675f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL);
1676d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
1677ce263fc8SMatthias Ringwald hfp_ag_transfer_callheld_state();
1678ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1679ce263fc8SMatthias Ringwald break;
1680ce263fc8SMatthias Ringwald default:
1681ce263fc8SMatthias Ringwald break;
1682ce263fc8SMatthias Ringwald }
1683ce263fc8SMatthias Ringwald break;
1684ce263fc8SMatthias Ringwald default:
1685ce263fc8SMatthias Ringwald break;
1686ce263fc8SMatthias Ringwald }
1687ce263fc8SMatthias Ringwald break;
1688ce263fc8SMatthias Ringwald
1689aa4dd815SMatthias Ringwald case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1690d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1691aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1692d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
169312cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1694f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL);
1695d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1696d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1697a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection);
1698e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
169960ebb071SMilanka Ringwald log_info("HF answers call, accept call by GSM");
1700ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1701aa4dd815SMatthias Ringwald break;
1702aa4dd815SMatthias Ringwald default:
1703aa4dd815SMatthias Ringwald break;
1704aa4dd815SMatthias Ringwald }
17053deb3ec6SMatthias Ringwald break;
17063deb3ec6SMatthias Ringwald default:
17073deb3ec6SMatthias Ringwald break;
17083deb3ec6SMatthias Ringwald }
17093deb3ec6SMatthias Ringwald break;
17103deb3ec6SMatthias Ringwald
1711ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1712d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1713ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1714d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
171512cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1716f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL);
171727bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true;
1718ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1719a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1720ac6f828eSMilanka Ringwald // as with regular call
1721d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1722d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1723ce263fc8SMatthias Ringwald hfp_ag_ag_accept_call();
172460ebb071SMilanka Ringwald log_info("AG response and hold - hold by AG");
1725ce263fc8SMatthias Ringwald break;
1726ce263fc8SMatthias Ringwald default:
1727ce263fc8SMatthias Ringwald break;
1728ce263fc8SMatthias Ringwald }
1729ce263fc8SMatthias Ringwald break;
1730ce263fc8SMatthias Ringwald default:
1731ce263fc8SMatthias Ringwald break;
1732ce263fc8SMatthias Ringwald }
1733ce263fc8SMatthias Ringwald break;
1734ce263fc8SMatthias Ringwald
1735ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1736d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1737ce263fc8SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1738d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
173912cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1740f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL);
174127bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = true;
1742ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1743a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1744ac6f828eSMilanka Ringwald // as with regular call
1745d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1746d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1747a0ffb263SMatthias Ringwald hfp_ag_hf_accept_call(hfp_connection);
174860ebb071SMilanka Ringwald log_info("AG response and hold - hold by HF");
1749ce263fc8SMatthias Ringwald break;
1750ce263fc8SMatthias Ringwald default:
1751ce263fc8SMatthias Ringwald break;
1752ce263fc8SMatthias Ringwald }
1753ce263fc8SMatthias Ringwald break;
1754ce263fc8SMatthias Ringwald default:
1755ce263fc8SMatthias Ringwald break;
1756ce263fc8SMatthias Ringwald }
1757ce263fc8SMatthias Ringwald break;
1758ce263fc8SMatthias Ringwald
1759ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1760ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1761ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break;
1762ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1763f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL);
176427bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false;
1765ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1766a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
176760ebb071SMilanka Ringwald log_info("Held Call accepted and active");
1768ce263fc8SMatthias Ringwald break;
1769ce263fc8SMatthias Ringwald
1770ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1771ce263fc8SMatthias Ringwald case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1772ce263fc8SMatthias Ringwald if (!hfp_ag_response_and_hold_active) break;
1773ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1774f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL);
177527bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false;
1776ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1777a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1778ce263fc8SMatthias Ringwald // from terminate by ag
1779d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1780ce263fc8SMatthias Ringwald hfp_ag_trigger_terminate_call();
1781ce263fc8SMatthias Ringwald break;
1782ce263fc8SMatthias Ringwald
1783aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_HF:
1784d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1785aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1786d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
178712cbbeeeSMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
178807a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
178907a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1790f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1791d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1792aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state();
1793aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call();
179460ebb071SMilanka Ringwald log_info("HF Rejected Incoming call, AG terminate call");
1795aa4dd815SMatthias Ringwald break;
1796aa4dd815SMatthias Ringwald default:
1797aa4dd815SMatthias Ringwald break;
1798aa4dd815SMatthias Ringwald }
1799aa4dd815SMatthias Ringwald break;
180012cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1801f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
18021ec112deSMatthias Ringwald hfp_ag_set_callsetup_indicator();
1803d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
18041ec112deSMatthias Ringwald hfp_ag_trigger_terminate_call();
180560ebb071SMilanka Ringwald log_info("AG terminate call");
1806aa4dd815SMatthias Ringwald break;
18077bbeb3adSMilanka Ringwald default:
18087bbeb3adSMilanka Ringwald break;
1809aa4dd815SMatthias Ringwald }
1810aa4dd815SMatthias Ringwald break;
18113deb3ec6SMatthias Ringwald
1812aa4dd815SMatthias Ringwald case HFP_AG_TERMINATE_CALL_BY_AG:
1813d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1814aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1815d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
181607a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
181707a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
181807a44c5eSMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1819f2aa99a9SMilanka Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1820f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1821d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1822aa4dd815SMatthias Ringwald hfp_ag_trigger_reject_call();
182360ebb071SMilanka Ringwald log_info("AG Rejected Incoming call, AG terminate call");
1824aa4dd815SMatthias Ringwald break;
1825aa4dd815SMatthias Ringwald default:
1826aa4dd815SMatthias Ringwald break;
18273deb3ec6SMatthias Ringwald }
1828202c8a4cSMatthias Ringwald break;
182912cbbeeeSMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1830f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1831d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1832d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1833aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call();
183460ebb071SMilanka Ringwald log_info("AG terminate call");
1835aa4dd815SMatthias Ringwald break;
1836aa4dd815SMatthias Ringwald default:
18373deb3ec6SMatthias Ringwald break;
18383deb3ec6SMatthias Ringwald }
18393deb3ec6SMatthias Ringwald break;
1840aa4dd815SMatthias Ringwald case HFP_AG_CALL_DROPPED:
1841d0c20769SMatthias Ringwald switch (hfp_gsm_call_status()){
1842aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1843d0c20769SMatthias Ringwald switch (hfp_gsm_callsetup_status()){
1844aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1845aa4dd815SMatthias Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1846fb75b483SMilanka Ringwald hfp_ag_stop_ringing();
1847fb75b483SMilanka Ringwald break;
1848fb75b483SMilanka Ringwald case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1849fb75b483SMilanka Ringwald log_info("Outgoing call interrupted\n");
1850aa4dd815SMatthias Ringwald break;
1851aa4dd815SMatthias Ringwald default:
18523deb3ec6SMatthias Ringwald break;
18533deb3ec6SMatthias Ringwald }
1854f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1855d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1856aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state();
185710c236bfSMatthias Ringwald hfp_ag_trigger_terminate_call();
1858aa4dd815SMatthias Ringwald break;
1859aa4dd815SMatthias Ringwald case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1860ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active) {
1861f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1862ce263fc8SMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1863a5bdcda8SMatthias Ringwald hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1864ce263fc8SMatthias Ringwald }
1865f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1866d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1867d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1868aa4dd815SMatthias Ringwald hfp_ag_trigger_terminate_call();
186960ebb071SMilanka Ringwald log_info("AG notify call dropped");
1870aa4dd815SMatthias Ringwald break;
1871aa4dd815SMatthias Ringwald default:
1872aa4dd815SMatthias Ringwald break;
1873aa4dd815SMatthias Ringwald }
1874aa4dd815SMatthias Ringwald break;
1875aa4dd815SMatthias Ringwald
18769ff73f41SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF:
1877d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded
1878d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){
1879e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
1880f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1881d210d9c4SMatthias Ringwald break;
1882d210d9c4SMatthias Ringwald }
1883fe899794SMatthias Ringwald
1884fe899794SMatthias Ringwald // note: number not used currently
18859ff73f41SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]);
18869cae807eSMatthias Ringwald
1887a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
188874386ee0SMatthias Ringwald
1889ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1890aa4dd815SMatthias Ringwald break;
1891aa4dd815SMatthias Ringwald
1892fe899794SMatthias Ringwald case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG:
1893fe899794SMatthias Ringwald // directly reject call if number of free slots is exceeded
1894fe899794SMatthias Ringwald if (!hfp_gsm_call_possible()) {
1895fe899794SMatthias Ringwald // TODO: no error for user
1896fe899794SMatthias Ringwald break;
1897fe899794SMatthias Ringwald }
1898fe899794SMatthias Ringwald switch (hfp_gsm_call_status()) {
1899fe899794SMatthias Ringwald case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1900fe899794SMatthias Ringwald switch (hfp_gsm_callsetup_status()) {
1901fe899794SMatthias Ringwald case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1902fe899794SMatthias Ringwald // note: number not used currently
1903fe899794SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567");
1904fe899794SMatthias Ringwald // init call state for all connections
1905fe899794SMatthias Ringwald hfp_ag_trigger_outgoing_call();
1906fe899794SMatthias Ringwald // get first one and accept call
1907fe899794SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1908fe899794SMatthias Ringwald if (hfp_connection) {
1909fe899794SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1910fe899794SMatthias Ringwald }
1911fe899794SMatthias Ringwald break;
1912fe899794SMatthias Ringwald default:
1913fe899794SMatthias Ringwald // TODO: no error for user
1914fe899794SMatthias Ringwald break;
1915fe899794SMatthias Ringwald }
1916fe899794SMatthias Ringwald break;
1917fe899794SMatthias Ringwald default:
1918fe899794SMatthias Ringwald // TODO: no error for user
1919fe899794SMatthias Ringwald break;
1920fe899794SMatthias Ringwald }
1921fe899794SMatthias Ringwald break;
19229cae807eSMatthias Ringwald case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1923d210d9c4SMatthias Ringwald // directly reject call if number of free slots is exceeded
1924d210d9c4SMatthias Ringwald if (!hfp_gsm_call_possible()){
1925e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
1926f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
1927d210d9c4SMatthias Ringwald break;
1928d210d9c4SMatthias Ringwald }
1929d210d9c4SMatthias Ringwald
1930f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL);
1931a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
193274386ee0SMatthias Ringwald
193360ebb071SMilanka Ringwald log_info("Redial last number");
19349cae807eSMatthias Ringwald char * last_dialed_number = hfp_gsm_last_dialed_number();
1935aa4dd815SMatthias Ringwald
19369cae807eSMatthias Ringwald if (strlen(last_dialed_number) > 0){
193760ebb071SMilanka Ringwald log_info("Last number exists: accept call");
1938ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
19399cae807eSMatthias Ringwald } else {
194060ebb071SMilanka Ringwald log_info("log_infoLast number missing: reject call");
19419f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call();
19429cae807eSMatthias Ringwald }
19439cae807eSMatthias Ringwald break;
19449cae807eSMatthias Ringwald }
1945aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_REJECTED:
19469f9621cfSMatthias Ringwald hfp_ag_handle_reject_outgoing_call();
1947aa4dd815SMatthias Ringwald break;
1948aa4dd815SMatthias Ringwald
1949d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1950a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1951a0ffb263SMatthias Ringwald if (!hfp_connection){
1952a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1953aa4dd815SMatthias Ringwald break;
1954aa4dd815SMatthias Ringwald }
1955aa4dd815SMatthias Ringwald
1956e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
19575a7033e2SMatthias Ringwald hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1958aa4dd815SMatthias Ringwald break;
1959d210d9c4SMatthias Ringwald }
1960aa4dd815SMatthias Ringwald case HFP_AG_OUTGOING_CALL_RINGING:
1961a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1962a0ffb263SMatthias Ringwald if (!hfp_connection){
1963a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1964aa4dd815SMatthias Ringwald break;
1965aa4dd815SMatthias Ringwald }
1966d210d9c4SMatthias Ringwald
1967f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL);
1968a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1969d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1970aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state();
1971fcd59412SMilanka Ringwald hfp_ag_hf_start_ringing_outgoing(hfp_connection);
1972aa4dd815SMatthias Ringwald break;
1973aa4dd815SMatthias Ringwald
1974d210d9c4SMatthias Ringwald case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1975aa4dd815SMatthias Ringwald // get outgoing call
1976a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1977a0ffb263SMatthias Ringwald if (!hfp_connection){
1978a0ffb263SMatthias Ringwald hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1979aa4dd815SMatthias Ringwald }
1980a0ffb263SMatthias Ringwald if (!hfp_connection){
1981a0ffb263SMatthias Ringwald log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1982aa4dd815SMatthias Ringwald break;
1983aa4dd815SMatthias Ringwald }
1984d210d9c4SMatthias Ringwald
1985addc49c4SMilanka Ringwald bool callheld_status_call_on_hold_and_no_active_calls = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
1986f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL);
1987a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
1988d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
1989d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
1990aa4dd815SMatthias Ringwald hfp_ag_transfer_call_state();
1991aa4dd815SMatthias Ringwald hfp_ag_transfer_callsetup_state();
1992addc49c4SMilanka Ringwald if (callheld_status_call_on_hold_and_no_active_calls){
1993d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
1994aa4dd815SMatthias Ringwald hfp_ag_transfer_callheld_state();
19953deb3ec6SMatthias Ringwald }
1996addc49c4SMilanka Ringwald hfp_ag_hf_stop_ringing(hfp_connection);
1997674ebed5SMilanka Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
19983deb3ec6SMatthias Ringwald break;
1999d210d9c4SMatthias Ringwald }
2000d210d9c4SMatthias Ringwald
200112cbbeeeSMatthias Ringwald case HFP_AG_CALL_HOLD_USER_BUSY:
2002f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL);
2003d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
2004a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
2005a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
200660ebb071SMilanka Ringwald log_info("AG: Call Waiting, User Busy");
2007d210d9c4SMatthias Ringwald break;
2008d210d9c4SMatthias Ringwald
2009d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
2010d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
2011d0c20769SMatthias Ringwald int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
2012d210d9c4SMatthias Ringwald
2013d210d9c4SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
2014d0c20769SMatthias Ringwald if (call_held || call_setup_in_progress){
2015f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index,
2016f8737b81SMatthias Ringwald 0, NULL);
2017d0c20769SMatthias Ringwald
2018d0c20769SMatthias Ringwald }
2019d0c20769SMatthias Ringwald
2020d210d9c4SMatthias Ringwald if (call_setup_in_progress){
202160ebb071SMilanka Ringwald log_info("AG: Call Dropped, Accept new call");
2022d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
2023a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
2024d210d9c4SMatthias Ringwald } else {
202560ebb071SMilanka Ringwald log_info("AG: Call Dropped, Resume held call");
2026d210d9c4SMatthias Ringwald }
2027d210d9c4SMatthias Ringwald if (call_held){
2028d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
2029a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2030d210d9c4SMatthias Ringwald }
2031d0c20769SMatthias Ringwald
2032a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
2033d210d9c4SMatthias Ringwald break;
2034d210d9c4SMatthias Ringwald }
2035d0c20769SMatthias Ringwald
2036d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
2037d210d9c4SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
2038d210d9c4SMatthias Ringwald // only update if callsetup changed
2039d0c20769SMatthias Ringwald int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
2040f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0,
2041f8737b81SMatthias Ringwald NULL);
2042d0c20769SMatthias Ringwald
2043d210d9c4SMatthias Ringwald if (call_setup_in_progress){
204460ebb071SMilanka Ringwald log_info("AG: Call on Hold, Accept new call");
2045d0c20769SMatthias Ringwald hfp_ag_set_callsetup_indicator();
2046a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
2047d210d9c4SMatthias Ringwald } else {
204860ebb071SMilanka Ringwald log_info("AG: Swap calls");
2049d210d9c4SMatthias Ringwald }
2050d0c20769SMatthias Ringwald
2051d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
2052d0c20769SMatthias Ringwald // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
2053a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2054a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
2055d210d9c4SMatthias Ringwald break;
2056d210d9c4SMatthias Ringwald }
2057d0c20769SMatthias Ringwald
2058d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
2059d210d9c4SMatthias Ringwald // Adds a held call to the conversation.
2060d0c20769SMatthias Ringwald if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
206160ebb071SMilanka Ringwald log_info("AG: Join 3-way-call");
2062f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL);
2063d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
2064a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2065ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
2066d210d9c4SMatthias Ringwald }
2067a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_ACTIVE;
2068d210d9c4SMatthias Ringwald break;
2069d210d9c4SMatthias Ringwald case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
2070d210d9c4SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
2071f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL);
207260ebb071SMilanka Ringwald log_info("AG: Transfer call -> Connect two calls and disconnect");
2073d210d9c4SMatthias Ringwald hfp_ag_set_call_indicator();
2074d0c20769SMatthias Ringwald hfp_ag_set_callheld_indicator();
2075a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
2076a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2077a0ffb263SMatthias Ringwald hfp_connection->call_state = HFP_CALL_IDLE;
2078d210d9c4SMatthias Ringwald break;
2079ce263fc8SMatthias Ringwald
20803deb3ec6SMatthias Ringwald default:
20813deb3ec6SMatthias Ringwald break;
20823deb3ec6SMatthias Ringwald }
2083d210d9c4SMatthias Ringwald
2084d0c20769SMatthias Ringwald
20853deb3ec6SMatthias Ringwald }
20863deb3ec6SMatthias Ringwald
20879cae807eSMatthias Ringwald
hfp_ag_send_call_status(hfp_connection_t * hfp_connection,int call_index)2088a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
20899cae807eSMatthias Ringwald hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
20909cae807eSMatthias Ringwald if (!active_call) return;
20919cae807eSMatthias Ringwald
20929cae807eSMatthias Ringwald int idx = active_call->index;
20939cae807eSMatthias Ringwald hfp_enhanced_call_dir_t dir = active_call->direction;
20949cae807eSMatthias Ringwald hfp_enhanced_call_status_t status = active_call->enhanced_status;
20959cae807eSMatthias Ringwald hfp_enhanced_call_mode_t mode = active_call->mode;
20969cae807eSMatthias Ringwald hfp_enhanced_call_mpty_t mpty = active_call->mpty;
20979cae807eSMatthias Ringwald uint8_t type = active_call->clip_type;
20989cae807eSMatthias Ringwald char * number = active_call->clip_number;
20999cae807eSMatthias Ringwald
21009cae807eSMatthias Ringwald char buffer[100];
21019cae807eSMatthias Ringwald // TODO: check length of a buffer, to fit the MTU
2102865e2b54SMatthias Ringwald int offset = btstack_snprintf_best_effort(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
21039cae807eSMatthias Ringwald if (number){
2104865e2b54SMatthias Ringwald offset += btstack_snprintf_best_effort(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
21059cae807eSMatthias Ringwald }
2106865e2b54SMatthias Ringwald btstack_snprintf_best_effort(buffer+offset, sizeof(buffer)-offset, "\r\n");
210760ebb071SMilanka Ringwald log_info("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s", idx, dir, status,
21089cae807eSMatthias Ringwald mode, mpty, type, number);
2109a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
21109cae807eSMatthias Ringwald }
21119cae807eSMatthias Ringwald
2112febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
hfp_ag_send_commands(hfp_connection_t * hfp_connection)2113febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
2114a0ffb263SMatthias Ringwald if (hfp_connection->send_status_of_current_calls){
2115a0ffb263SMatthias Ringwald if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
2116a0ffb263SMatthias Ringwald hfp_connection->next_call_index++;
2117a0ffb263SMatthias Ringwald hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
2118febc14f5SMatthias Ringwald return 1;
21199cae807eSMatthias Ringwald } else {
2120febc14f5SMatthias Ringwald // TODO: this code should be removed. check a) before setting send_status_of_current_calls, or b) right before hfp_ag_send_call_status above
2121a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0;
2122e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2123a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 0;
21249cae807eSMatthias Ringwald }
2125ce263fc8SMatthias Ringwald }
2126ce263fc8SMatthias Ringwald
2127a0ffb263SMatthias Ringwald if (hfp_connection->ag_notify_incoming_call_waiting){
2128a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 0;
2129a0ffb263SMatthias Ringwald hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
2130febc14f5SMatthias Ringwald return 1;
2131aa4dd815SMatthias Ringwald }
2132aa4dd815SMatthias Ringwald
2133edc46354SMatthias Ringwald // note: before ok_pending and send_error
2134edc46354SMatthias Ringwald if (hfp_connection->send_apple_information){
2135edc46354SMatthias Ringwald hfp_connection->send_apple_information = false;
2136edc46354SMatthias Ringwald hfp_ag_send_apple_information(hfp_connection->rfcomm_cid);
2137edc46354SMatthias Ringwald return 1;
2138edc46354SMatthias Ringwald }
2139edc46354SMatthias Ringwald
21405e87d1ceSMatthias Ringwald if (hfp_connection->send_error > 0){
21415e87d1ceSMatthias Ringwald hfp_connection->send_error--;
2142a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2143485ac19eSMilanka Ringwald hfp_ag_send_error(hfp_connection->rfcomm_cid);
2144febc14f5SMatthias Ringwald return 1;
2145aa4dd815SMatthias Ringwald }
2146aa4dd815SMatthias Ringwald
2147ce263fc8SMatthias Ringwald // note: before update AG indicators and ok_pending
2148a0ffb263SMatthias Ringwald if (hfp_connection->send_response_and_hold_status){
2149a0ffb263SMatthias Ringwald int status = hfp_connection->send_response_and_hold_status - 1;
2150a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = 0;
2151485ac19eSMilanka Ringwald hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
2152febc14f5SMatthias Ringwald return 1;
2153ce263fc8SMatthias Ringwald }
2154ce263fc8SMatthias Ringwald
21555be6b47aSMatthias Ringwald // note: before ok_pending and send_error to allow for unsolicited result on custom command
21565be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){
21575be6b47aSMatthias Ringwald const char * message = hfp_connection->send_custom_message;
21585be6b47aSMatthias Ringwald hfp_connection->send_custom_message = NULL;
21595be6b47aSMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, message);
21607f8f1191SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS);
21615be6b47aSMatthias Ringwald return 1;
21625be6b47aSMatthias Ringwald }
21635be6b47aSMatthias Ringwald
21645e87d1ceSMatthias Ringwald if (hfp_connection->ok_pending > 0){
21655e87d1ceSMatthias Ringwald hfp_connection->ok_pending--;
2166a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2167485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2168febc14f5SMatthias Ringwald return 1;
2169ce263fc8SMatthias Ringwald }
2170ce263fc8SMatthias Ringwald
217169640b04SMatthias Ringwald // update AG indicators only if enabled by AT+CMER=3,0,0,1
217269640b04SMatthias Ringwald if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){
217369640b04SMatthias Ringwald uint16_t i;
2174a0ffb263SMatthias Ringwald for (i=0;i<hfp_connection->ag_indicators_nr;i++){
2175a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
2176a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
2177485ac19eSMilanka Ringwald hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
2178febc14f5SMatthias Ringwald return 1;
2179aa4dd815SMatthias Ringwald }
2180aa4dd815SMatthias Ringwald }
2181aa4dd815SMatthias Ringwald }
2182aa4dd815SMatthias Ringwald
2183245852b7SMilanka Ringwald if (hfp_connection->ag_send_no_carrier){
2184245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = false;
2185245852b7SMilanka Ringwald hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid);
2186245852b7SMilanka Ringwald return 1;
2187245852b7SMilanka Ringwald }
2188245852b7SMilanka Ringwald
2189a0ffb263SMatthias Ringwald if (hfp_connection->send_phone_number_for_voice_tag){
2190a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 0;
2191a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2192e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2193a0ffb263SMatthias Ringwald hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
2194febc14f5SMatthias Ringwald return 1;
2195aa4dd815SMatthias Ringwald }
2196aa4dd815SMatthias Ringwald
2197a0ffb263SMatthias Ringwald if (hfp_connection->send_subscriber_number){
2198aeb0f0feSMatthias Ringwald if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){
2199aeb0f0feSMatthias Ringwald hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
2200a0ffb263SMatthias Ringwald hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
2201ce263fc8SMatthias Ringwald } else {
2202a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 0;
2203a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0;
2204485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2205ce263fc8SMatthias Ringwald }
2206a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2207febc14f5SMatthias Ringwald return 1;
2208ce263fc8SMatthias Ringwald }
2209ce263fc8SMatthias Ringwald
2210a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){
2211a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0;
2212a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2213485ac19eSMilanka Ringwald hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
2214febc14f5SMatthias Ringwald return 1;
2215aa4dd815SMatthias Ringwald }
2216aa4dd815SMatthias Ringwald
2217a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){
2218a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0;
2219a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2220485ac19eSMilanka Ringwald hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
2221febc14f5SMatthias Ringwald return 1;
22223deb3ec6SMatthias Ringwald }
22233deb3ec6SMatthias Ringwald
2224a0ffb263SMatthias Ringwald if (hfp_connection->send_ag_status_indicators){
2225a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 0;
2226485ac19eSMilanka Ringwald hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
2227febc14f5SMatthias Ringwald return 1;
2228febc14f5SMatthias Ringwald }
2229febc14f5SMatthias Ringwald
223010ae9dfcSMatthias Ringwald if (hfp_connection->extended_audio_gateway_error){
223110ae9dfcSMatthias Ringwald uint8_t extended_audio_gateway_error = hfp_connection->extended_audio_gateway_error;
223210ae9dfcSMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0;
223310ae9dfcSMatthias Ringwald hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, extended_audio_gateway_error);
223410ae9dfcSMatthias Ringwald
223510ae9dfcSMatthias Ringwald }
2236febc14f5SMatthias Ringwald return 0;
2237febc14f5SMatthias Ringwald }
2238febc14f5SMatthias Ringwald
2239c1eef992SMatthias Ringwald // sends pending command, returns if command was sent
hfp_ag_run_ring_and_clip(hfp_connection_t * hfp_connection)2240c1eef992SMatthias Ringwald static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){
2241c1eef992SMatthias Ringwald // delay RING/CLIP until audio connection has been established for incoming calls
2242c1eef992SMatthias Ringwald // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine
2243c1eef992SMatthias Ringwald if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){
2244c1eef992SMatthias Ringwald if (hfp_connection->ag_ring){
2245c1eef992SMatthias Ringwald hfp_connection->ag_ring = 0;
2246c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2247c1eef992SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
2248c1eef992SMatthias Ringwald hfp_ag_send_ring(hfp_connection->rfcomm_cid);
2249c1eef992SMatthias Ringwald return 1;
2250c1eef992SMatthias Ringwald }
2251c1eef992SMatthias Ringwald
2252c1eef992SMatthias Ringwald if (hfp_connection->ag_send_clip){
2253c1eef992SMatthias Ringwald hfp_connection->ag_send_clip = 0;
2254c1eef992SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2255c1eef992SMatthias Ringwald hfp_ag_send_clip(hfp_connection->rfcomm_cid);
2256c1eef992SMatthias Ringwald return 1;
2257c1eef992SMatthias Ringwald }
2258c1eef992SMatthias Ringwald }
2259c1eef992SMatthias Ringwald return 0;
2260c1eef992SMatthias Ringwald }
2261c1eef992SMatthias Ringwald
hfp_ag_run_for_context(hfp_connection_t * hfp_connection)2262febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
2263febc14f5SMatthias Ringwald
226476cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL);
226576cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
2266febc14f5SMatthias Ringwald
226776cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set
226876cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return;
2269ce263fc8SMatthias Ringwald
227084fb9ac1SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){
227184fb9ac1SMilanka Ringwald hfp_connection->emit_vra_enabled_after_audio_established = false;
227284fb9ac1SMilanka Ringwald hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
227384fb9ac1SMilanka Ringwald }
227484fb9ac1SMilanka Ringwald
2275f9f3ba28SMilanka Ringwald if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
2276f9f3ba28SMilanka Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
2277f9f3ba28SMilanka Ringwald hfp_connection->release_audio_connection = 0;
2278f9f3ba28SMilanka Ringwald gap_disconnect(hfp_connection->sco_handle);
2279f9f3ba28SMilanka Ringwald return;
2280f9f3ba28SMilanka Ringwald }
2281f9f3ba28SMilanka Ringwald
2282b7802cc8SMatthias Ringwald // configure NBS/WBS if needed using vendor-specific HCI commands
2283b7802cc8SMatthias Ringwald if (hci_can_send_command_packet_now()) {
22843721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
22853721a235SMatthias Ringwald // WBS Disassociate
22863721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_disassociate){
22873721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_disassociate = false;
22883721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_disassociate);
22893721a235SMatthias Ringwald return;
22903721a235SMatthias Ringwald }
22913721a235SMatthias Ringwald // Write Codec Config
22923721a235SMatthias Ringwald if (hfp_connection->cc256x_send_write_codec_config){
22933721a235SMatthias Ringwald hfp_connection->cc256x_send_write_codec_config = false;
22943721a235SMatthias Ringwald hfp_cc256x_write_codec_config(hfp_connection);
22953721a235SMatthias Ringwald return;
22963721a235SMatthias Ringwald }
22973721a235SMatthias Ringwald // WBS Associate
22983721a235SMatthias Ringwald if (hfp_connection->cc256x_send_wbs_associate){
22993721a235SMatthias Ringwald hfp_connection->cc256x_send_wbs_associate = false;
23003721a235SMatthias Ringwald hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
23013721a235SMatthias Ringwald return;
23023721a235SMatthias Ringwald }
23033721a235SMatthias Ringwald #endif
2304689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
2305689d4323SMatthias Ringwald // Enable WBS
2306689d4323SMatthias Ringwald if (hfp_connection->bcm_send_enable_wbs){
2307689d4323SMatthias Ringwald hfp_connection->bcm_send_enable_wbs = false;
2308689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
2309689d4323SMatthias Ringwald return;
2310689d4323SMatthias Ringwald }
2311689d4323SMatthias Ringwald // Write I2S/PCM params
2312689d4323SMatthias Ringwald if (hfp_connection->bcm_send_write_i2spcm_interface_param){
2313689d4323SMatthias Ringwald hfp_connection->bcm_send_write_i2spcm_interface_param = false;
2314689d4323SMatthias Ringwald hfp_bcm_write_i2spcm_interface_param(hfp_connection);
2315689d4323SMatthias Ringwald return;
2316689d4323SMatthias Ringwald }
2317689d4323SMatthias Ringwald // Disable WBS
2318689d4323SMatthias Ringwald if (hfp_connection->bcm_send_disable_wbs){
2319689d4323SMatthias Ringwald hfp_connection->bcm_send_disable_wbs = false;
2320689d4323SMatthias Ringwald hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
2321689d4323SMatthias Ringwald return;
2322689d4323SMatthias Ringwald }
2323689d4323SMatthias Ringwald #endif
23242b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
23252b5f92fdSMatthias Ringwald // Configure CVSD vs. mSBC
23262b5f92fdSMatthias Ringwald if (hfp_connection->rtk_send_sco_config){
23272b5f92fdSMatthias Ringwald hfp_connection->rtk_send_sco_config = false;
23282b5f92fdSMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
23292b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + mSBC");
23302b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41);
23312b5f92fdSMatthias Ringwald } else {
23322b5f92fdSMatthias Ringwald log_info("RTK SCO: 16k + CVSD");
23332b5f92fdSMatthias Ringwald hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01);
23342b5f92fdSMatthias Ringwald }
23352b5f92fdSMatthias Ringwald return;
23362b5f92fdSMatthias Ringwald }
23372b5f92fdSMatthias Ringwald #endif
23385fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS
23395fd6f360SMatthias Ringwald if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){
23405fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle;
23415fd6f360SMatthias Ringwald hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID;
23425fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0);
23435fd6f360SMatthias Ringwald return;
23445fd6f360SMatthias Ringwald }
23455fd6f360SMatthias Ringwald if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){
23465fd6f360SMatthias Ringwald hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle;
23475fd6f360SMatthias Ringwald hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID;
23485fd6f360SMatthias Ringwald hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0);
23495fd6f360SMatthias Ringwald return;
23505fd6f360SMatthias Ringwald }
23515fd6f360SMatthias Ringwald #endif
2352b7802cc8SMatthias Ringwald }
2353b7802cc8SMatthias Ringwald
235448e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
235548e6eeeeSMatthias Ringwald if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
235648e6eeeeSMatthias Ringwald hfp_finalize_connection_context(hfp_connection);
235748e6eeeeSMatthias Ringwald return;
235848e6eeeeSMatthias Ringwald }
235948e6eeeeSMatthias Ringwald #endif
23603721a235SMatthias Ringwald
2361febc14f5SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
2362febc14f5SMatthias Ringwald log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
2363febc14f5SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
2364febc14f5SMatthias Ringwald return;
2365febc14f5SMatthias Ringwald }
2366febc14f5SMatthias Ringwald
2367febc14f5SMatthias Ringwald int cmd_sent = hfp_ag_send_commands(hfp_connection);
2368febc14f5SMatthias Ringwald
2369febc14f5SMatthias Ringwald if (!cmd_sent){
2370febc14f5SMatthias Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
2371febc14f5SMatthias Ringwald }
2372febc14f5SMatthias Ringwald
2373c04cf7ffSMilanka Ringwald if (!cmd_sent){
2374c04cf7ffSMilanka Ringwald cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
23753deb3ec6SMatthias Ringwald }
23763deb3ec6SMatthias Ringwald
2377c04cf7ffSMilanka Ringwald if (!cmd_sent){
2378c04cf7ffSMilanka Ringwald cmd_sent = call_setup_state_machine(hfp_connection);
2379aa4dd815SMatthias Ringwald }
2380aa4dd815SMatthias Ringwald
2381b956fff3SMatthias Ringwald if (!cmd_sent){
2382b956fff3SMatthias Ringwald cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
2383b956fff3SMatthias Ringwald }
2384b956fff3SMatthias Ringwald
2385c95b5b3cSMilanka Ringwald if (!cmd_sent){
2386c1eef992SMatthias Ringwald cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection);
2387c1eef992SMatthias Ringwald }
2388c1eef992SMatthias Ringwald
2389c1eef992SMatthias Ringwald if (!cmd_sent){
2390c95b5b3cSMilanka Ringwald cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection);
2391c95b5b3cSMilanka Ringwald }
2392b956fff3SMatthias Ringwald
2393d0a0eceeSMatthias Ringwald // disconnect
2394d0a0eceeSMatthias Ringwald if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){
2395d0a0eceeSMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
2396d0a0eceeSMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid);
2397d0a0eceeSMatthias Ringwald }
2398d0a0eceeSMatthias Ringwald
2399c04cf7ffSMilanka Ringwald if (cmd_sent){
2400a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2401473ac565SMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
2402473ac565SMatthias Ringwald }
24033deb3ec6SMatthias Ringwald }
2404d68dcce1SMatthias Ringwald
hfp_parser_is_end_of_line(uint8_t byte)2405e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
2406c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r');
2407e0d09929SMatthias Ringwald }
2408e0d09929SMatthias Ringwald
hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection,uint8_t * packet,uint16_t size)2409fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
24108a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets
2411fdf18f86SMilanka Ringwald if (size < 1) return;
2412347d46c8SMilanka Ringwald uint8_t status = ERROR_CODE_SUCCESS;
24131e35c04dSMatthias Ringwald
2414186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
2415e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
2416e43d1938SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
2417e43d1938SMatthias Ringwald #endif
24181e35c04dSMatthias Ringwald
24198a46ec40SMatthias Ringwald // process messages byte-wise
24203deb3ec6SMatthias Ringwald int pos;
24213deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){
2422a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 0);
2423186dd3d2SMatthias Ringwald
2424e0d09929SMatthias Ringwald // parse until end of line
2425e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue;
2426e0d09929SMatthias Ringwald
2427186dd3d2SMatthias Ringwald hfp_generic_status_indicator_t * indicator;
2428a0ffb263SMatthias Ringwald switch(hfp_connection->command){
242945796ff1SMilanka Ringwald case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
2430e7c46708SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2431e7c46708SMatthias Ringwald hfp_connection->ag_vra_requested_by_hf = true;
243245796ff1SMilanka Ringwald break;
2433ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
2434fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2435ce263fc8SMatthias Ringwald if (hfp_ag_response_and_hold_active){
2436a0ffb263SMatthias Ringwald hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
2437ce263fc8SMatthias Ringwald }
2438e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2439ce263fc8SMatthias Ringwald break;
2440ce263fc8SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
2441fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
24420222a807SMatthias Ringwald switch(hfp_connection->ag_response_and_hold_action){
2443ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
2444a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
2445ce263fc8SMatthias Ringwald break;
2446ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
2447a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
2448ce263fc8SMatthias Ringwald break;
2449ce263fc8SMatthias Ringwald case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
2450a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
2451ce263fc8SMatthias Ringwald break;
2452ce263fc8SMatthias Ringwald default:
2453ce263fc8SMatthias Ringwald break;
2454ce263fc8SMatthias Ringwald }
2455e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2456ce263fc8SMatthias Ringwald break;
2457ce263fc8SMatthias Ringwald case HFP_CMD_HF_INDICATOR_STATUS:
2458a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2459347d46c8SMilanka Ringwald
2460aeb0f0feSMatthias Ringwald if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){
2461aeb0f0feSMatthias Ringwald indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index];
2462ce263fc8SMatthias Ringwald switch (indicator->uuid){
2463347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY:
24640222a807SMatthias Ringwald if (hfp_connection->parser_indicator_value > 1) {
2465e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2466ce263fc8SMatthias Ringwald break;
2467ce263fc8SMatthias Ringwald }
2468e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2469585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value);
2470347d46c8SMilanka Ringwald break;
2471347d46c8SMilanka Ringwald case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL:
2472347d46c8SMilanka Ringwald if (hfp_connection->parser_indicator_value > 100){
2473e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2474347d46c8SMilanka Ringwald break;
2475347d46c8SMilanka Ringwald }
2476e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2477585db78dSMilanka Ringwald hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value);
2478347d46c8SMilanka Ringwald break;
2479347d46c8SMilanka Ringwald default:
2480e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2481347d46c8SMilanka Ringwald break;
2482347d46c8SMilanka Ringwald }
2483347d46c8SMilanka Ringwald } else {
2484e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2485347d46c8SMilanka Ringwald }
2486ce263fc8SMatthias Ringwald break;
2487ce263fc8SMatthias Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
2488ce263fc8SMatthias Ringwald // expected by SLC state machine
2489a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
2490a0ffb263SMatthias Ringwald hfp_connection->send_ag_indicators_segment = 0;
2491a0ffb263SMatthias Ringwald hfp_connection->send_ag_status_indicators = 1;
2492ce263fc8SMatthias Ringwald break;
2493ce263fc8SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS:
2494a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2495a0ffb263SMatthias Ringwald hfp_connection->next_call_index = 0;
2496a0ffb263SMatthias Ringwald hfp_connection->send_status_of_current_calls = 1;
2497ce263fc8SMatthias Ringwald break;
2498ce263fc8SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
2499fd66594dSMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2500aeb0f0feSMatthias Ringwald if (hfp_ag_subscriber_numbers_count == 0){
2501485ac19eSMilanka Ringwald hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2502ce263fc8SMatthias Ringwald break;
2503ce263fc8SMatthias Ringwald }
2504a0ffb263SMatthias Ringwald hfp_connection->next_subscriber_number_to_send = 0;
2505a0ffb263SMatthias Ringwald hfp_connection->send_subscriber_number = 1;
2506ce263fc8SMatthias Ringwald break;
2507c1797c7dSMatthias Ringwald case HFP_CMD_TRANSMIT_DTMF_CODES:
25080222a807SMatthias Ringwald {
2509a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
25100222a807SMatthias Ringwald char buffer[2];
25110222a807SMatthias Ringwald buffer[0] = (char) hfp_connection->ag_dtmf_code;
25120222a807SMatthias Ringwald buffer[1] = 0;
25130222a807SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
2514c1797c7dSMatthias Ringwald break;
25150222a807SMatthias Ringwald }
2516aa4dd815SMatthias Ringwald case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
2517a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2518ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
2519aa4dd815SMatthias Ringwald break;
2520aa4dd815SMatthias Ringwald case HFP_CMD_TURN_OFF_EC_AND_NR:
2521a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2522347d46c8SMilanka Ringwald
2523aeb0f0feSMatthias Ringwald if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
2524e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2525347d46c8SMilanka Ringwald status = ERROR_CODE_SUCCESS;
2526aa4dd815SMatthias Ringwald } else {
2527e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2528347d46c8SMilanka Ringwald status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
2529aa4dd815SMatthias Ringwald }
2530347d46c8SMilanka Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
2531aa4dd815SMatthias Ringwald break;
2532aa4dd815SMatthias Ringwald case HFP_CMD_CALL_ANSWERED:
2533a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
253460ebb071SMilanka Ringwald log_info("HFP: ATA");
2535a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
2536aa4dd815SMatthias Ringwald break;
2537aa4dd815SMatthias Ringwald case HFP_CMD_HANG_UP_CALL:
2538a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2539e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2540a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
2541aa4dd815SMatthias Ringwald break;
2542aa4dd815SMatthias Ringwald case HFP_CMD_CALL_HOLD: {
2543a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2544e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2545d0c20769SMatthias Ringwald
25460222a807SMatthias Ringwald switch (hfp_connection->ag_call_hold_action){
25470222a807SMatthias Ringwald case 0:
2548d0c20769SMatthias Ringwald // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
2549a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
2550aa4dd815SMatthias Ringwald break;
25510222a807SMatthias Ringwald case 1:
2552d0c20769SMatthias Ringwald // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
2553d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the
2554d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation.
2555a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2556aa4dd815SMatthias Ringwald break;
25570222a807SMatthias Ringwald case 2:
2558d0c20769SMatthias Ringwald // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
2559d0c20769SMatthias Ringwald // Where both a held and a waiting call exist, the above procedures shall apply to the
2560d0c20769SMatthias Ringwald // waiting call (i.e., not to the held call) in conflicting situation.
2561a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2562aa4dd815SMatthias Ringwald break;
25630222a807SMatthias Ringwald case 3:
2564d0c20769SMatthias Ringwald // Adds a held call to the conversation.
2565a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
2566aa4dd815SMatthias Ringwald break;
25670222a807SMatthias Ringwald case 4:
2568d0c20769SMatthias Ringwald // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
2569a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
2570aa4dd815SMatthias Ringwald break;
2571aa4dd815SMatthias Ringwald default:
2572aa4dd815SMatthias Ringwald break;
2573aa4dd815SMatthias Ringwald }
25740222a807SMatthias Ringwald hfp_connection->call_index = 0;
257535e92150SMatthias Ringwald break;
2576aa4dd815SMatthias Ringwald }
2577aa4dd815SMatthias Ringwald case HFP_CMD_CALL_PHONE_NUMBER:
2578a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
25799ff73f41SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection);
2580aa4dd815SMatthias Ringwald break;
2581aa4dd815SMatthias Ringwald case HFP_CMD_REDIAL_LAST_NUMBER:
2582a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2583a0ffb263SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
2584aa4dd815SMatthias Ringwald break;
2585aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CLIP:
2586a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2587a0ffb263SMatthias Ringwald log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2588e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2589aa4dd815SMatthias Ringwald break;
2590aa4dd815SMatthias Ringwald case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2591a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2592a0ffb263SMatthias Ringwald log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2593e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2594aa4dd815SMatthias Ringwald break;
2595aa4dd815SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN:
2596a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2597e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
259860ebb071SMilanka Ringwald log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
25993db60f78SBjoern Hartmann hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain);
2600aa4dd815SMatthias Ringwald break;
2601aa4dd815SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN:
2602a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2603e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
260460ebb071SMilanka Ringwald log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
26052abbd98dSMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain);
2606aa4dd815SMatthias Ringwald break;
2607471dea41SMatthias Ringwald case HFP_CMD_CUSTOM_MESSAGE:
2608471dea41SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2609471dea41SMatthias Ringwald hfp_parser_reset_line_buffer(hfp_connection);
26106d9a41f9SMatthias Ringwald log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id);
2611471dea41SMatthias Ringwald hfp_ag_emit_custom_command_event(hfp_connection);
2612471dea41SMatthias Ringwald break;
2613edc46354SMatthias Ringwald case HFP_CMD_APPLE_ACCESSORY_INFORMATION:
2614edc46354SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2615edc46354SMatthias Ringwald if (hfp_ag_apple_device != NULL){
2616edc46354SMatthias Ringwald hfp_connection->send_apple_information = true;
2617edc46354SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2618edc46354SMatthias Ringwald hfp_ag_emit_apple_accessory_information(hfp_connection);
2619edc46354SMatthias Ringwald } else {
2620edc46354SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2621edc46354SMatthias Ringwald };
2622edc46354SMatthias Ringwald break;
2623edc46354SMatthias Ringwald case HFP_CMD_APPLE_ACCESSORY_STATE:
2624edc46354SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2625edc46354SMatthias Ringwald if (hfp_ag_apple_device != NULL) {
2626edc46354SMatthias Ringwald hfp_ag_emit_apple_accessory_state(hfp_connection);
2627edc46354SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
2628edc46354SMatthias Ringwald } else {
2629edc46354SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
2630edc46354SMatthias Ringwald }
2631edc46354SMatthias Ringwald break;
26323f38f554SMatthias Ringwald case HFP_CMD_UNKNOWN:
26333f38f554SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE;
2634e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
26353f38f554SMatthias Ringwald break;
2636aa4dd815SMatthias Ringwald default:
2637aa4dd815SMatthias Ringwald break;
26383deb3ec6SMatthias Ringwald }
26393deb3ec6SMatthias Ringwald }
26400cef86faSMatthias Ringwald }
26413deb3ec6SMatthias Ringwald
hfp_ag_run(void)26421c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){
2643d63c37a1SMatthias Ringwald btstack_linked_list_iterator_t it;
2644d63c37a1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
2645d63c37a1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
2646d63c37a1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
264722387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2648f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
26493deb3ec6SMatthias Ringwald }
26503deb3ec6SMatthias Ringwald }
26513deb3ec6SMatthias Ringwald
hfp_ag_rfcomm_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)26521c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2653fdf18f86SMilanka Ringwald hfp_connection_t * hfp_connection = NULL;
26543deb3ec6SMatthias Ringwald switch (packet_type){
26553deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET:
2656fdf18f86SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
2657fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL);
2658fdf18f86SMilanka Ringwald
2659fdf18f86SMilanka Ringwald hfp_ag_handle_rfcomm_data(hfp_connection, packet, size);
2660fdf18f86SMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
2661fdf18f86SMilanka Ringwald return;
26623deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET:
2663e30a6a47SMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2664e30a6a47SMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2665fd075f20SMilanka Ringwald hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
2666fd075f20SMilanka Ringwald btstack_assert(hfp_connection != NULL);
2667fd075f20SMilanka Ringwald
2668fd075f20SMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
2669e30a6a47SMatthias Ringwald return;
2670e30a6a47SMatthias Ringwald }
267127950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2672aa4dd815SMatthias Ringwald break;
26733deb3ec6SMatthias Ringwald default:
26743deb3ec6SMatthias Ringwald break;
26753deb3ec6SMatthias Ringwald }
26763deb3ec6SMatthias Ringwald
26771c6a0fc0SMatthias Ringwald hfp_ag_run();
26783deb3ec6SMatthias Ringwald }
26793deb3ec6SMatthias Ringwald
hfp_ag_hci_event_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)26801c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2681405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
26821c6a0fc0SMatthias Ringwald hfp_ag_run();
2683405014fbSMatthias Ringwald }
2684405014fbSMatthias Ringwald
hfp_ag_init_codecs(uint8_t codecs_nr,const uint8_t * codecs)2685aa10b9cbSMatthias Ringwald void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){
2686aa10b9cbSMatthias Ringwald btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS);
26878cd06fb6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS) return;
2688aa10b9cbSMatthias Ringwald
2689aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = codecs_nr;
2690aa10b9cbSMatthias Ringwald uint8_t i;
26913deb3ec6SMatthias Ringwald for (i=0; i < codecs_nr; i++){
2692aeb0f0feSMatthias Ringwald hfp_ag_codecs[i] = codecs[i];
26933deb3ec6SMatthias Ringwald }
2694a0ffb263SMatthias Ringwald }
26953deb3ec6SMatthias Ringwald
hfp_ag_init_supported_features(uint32_t supported_features)2696a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2697aeb0f0feSMatthias Ringwald hfp_ag_supported_features = supported_features;
269840a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone();
2699a0ffb263SMatthias Ringwald }
27003deb3ec6SMatthias Ringwald
hfp_ag_init_ag_indicators(int ag_indicators_nr,const hfp_ag_indicator_t * ag_indicators)27017ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){
27028cd06fb6SMatthias Ringwald btstack_assert(ag_indicators_nr <= HFP_MAX_NUM_INDICATORS);
27038cd06fb6SMatthias Ringwald if (ag_indicators_nr > HFP_MAX_NUM_CODECS) return;
27048cd06fb6SMatthias Ringwald
2705a0ffb263SMatthias Ringwald hfp_ag_indicators_nr = ag_indicators_nr;
27066535961aSMatthias Ringwald (void)memcpy(hfp_ag_indicators, ag_indicators,
27076535961aSMatthias Ringwald ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2708a0ffb263SMatthias Ringwald }
27093deb3ec6SMatthias Ringwald
hfp_ag_init_hf_indicators(int hf_indicators_nr,const hfp_generic_status_indicator_t * hf_indicators)27107ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){
27118cd06fb6SMatthias Ringwald btstack_assert(hf_indicators_nr <= HFP_MAX_NUM_INDICATORS);
27128cd06fb6SMatthias Ringwald if (hf_indicators_nr > HFP_MAX_NUM_CODECS) return;
27138cd06fb6SMatthias Ringwald
2714aeb0f0feSMatthias Ringwald hfp_ag_generic_status_indicators_nr = hf_indicators_nr;
2715aeb0f0feSMatthias Ringwald (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators,
27166535961aSMatthias Ringwald hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2717a0ffb263SMatthias Ringwald }
2718a0ffb263SMatthias Ringwald
hfp_ag_init_call_hold_services(int call_hold_services_nr,const char * call_hold_services[])2719a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
27203deb3ec6SMatthias Ringwald hfp_ag_call_hold_services_nr = call_hold_services_nr;
27216535961aSMatthias Ringwald (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
27226535961aSMatthias Ringwald call_hold_services_nr * sizeof(char *));
2723a0ffb263SMatthias Ringwald }
2724a0ffb263SMatthias Ringwald
hfp_ag_init_apple_identification(const char * device,uint8_t features)2725edc46354SMatthias Ringwald void hfp_ag_init_apple_identification(const char * device, uint8_t features){
2726edc46354SMatthias Ringwald hfp_ag_apple_device = device;
2727edc46354SMatthias Ringwald hfp_ag_apple_features = features;
2728edc46354SMatthias Ringwald }
2729a0ffb263SMatthias Ringwald
hfp_ag_init(uint8_t rfcomm_channel_nr)2730ab2445a0SMatthias Ringwald void hfp_ag_init(uint8_t rfcomm_channel_nr){
273127950165SMatthias Ringwald
2732520c92d5SMatthias Ringwald hfp_init();
273320b2edb6SMatthias Ringwald hfp_ag_call_hold_services_nr = 0;
273427bb1817SMatthias Ringwald hfp_ag_response_and_hold_active = false;
273520b2edb6SMatthias Ringwald hfp_ag_indicators_nr = 0;
2736aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0;
2737aeb0f0feSMatthias Ringwald hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
273840a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone();
2739aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL;
2740aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = 0;
2741d63c37a1SMatthias Ringwald
27421c6a0fc0SMatthias Ringwald hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler;
27431c6a0fc0SMatthias Ringwald hci_add_event_handler(&hfp_ag_hci_event_callback_registration);
274427950165SMatthias Ringwald
27451c6a0fc0SMatthias Ringwald rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
274627950165SMatthias Ringwald
274727950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
27481c6a0fc0SMatthias Ringwald hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler);
2749aa4dd815SMatthias Ringwald
2750d210d9c4SMatthias Ringwald hfp_gsm_init();
27513deb3ec6SMatthias Ringwald }
27523deb3ec6SMatthias Ringwald
hfp_ag_deinit(void)275320b2edb6SMatthias Ringwald void hfp_ag_deinit(void){
275420b2edb6SMatthias Ringwald hfp_deinit();
275520b2edb6SMatthias Ringwald hfp_gsm_deinit();
2756aeb0f0feSMatthias Ringwald
2757aeb0f0feSMatthias Ringwald hfp_ag_callback = NULL;
2758aeb0f0feSMatthias Ringwald hfp_ag_supported_features = 0;
2759aeb0f0feSMatthias Ringwald hfp_ag_codecs_nr = 0;
2760aeb0f0feSMatthias Ringwald hfp_ag_indicators_nr = 0;
2761aeb0f0feSMatthias Ringwald hfp_ag_call_hold_services_nr = 0;
2762edc46354SMatthias Ringwald hfp_ag_apple_device = NULL;
276320b2edb6SMatthias Ringwald (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services));
2764aeb0f0feSMatthias Ringwald hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
276520b2edb6SMatthias Ringwald (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t));
2766aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = NULL;
2767aeb0f0feSMatthias Ringwald (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
276881e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = NULL;
276920b2edb6SMatthias Ringwald }
277020b2edb6SMatthias Ringwald
hfp_ag_establish_service_level_connection(bd_addr_t bd_addr)27714eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
27724eb3f1d8SMilanka Ringwald return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
27733deb3ec6SMatthias Ringwald }
27743deb3ec6SMatthias Ringwald
hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle)2775657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
27769c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2777a33eb0c4SMilanka Ringwald if (!hfp_connection){
2778657bc59fSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2779a33eb0c4SMilanka Ringwald }
2780c5fa3c94SMilanka Ringwald
27811ffa0dd9SMilanka Ringwald hfp_trigger_release_service_level_connection(hfp_connection);
2782f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
2783657bc59fSMilanka Ringwald return ERROR_CODE_SUCCESS;
27843deb3ec6SMatthias Ringwald }
27853deb3ec6SMatthias Ringwald
hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle,hfp_cme_error_t error)2786c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
27879c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2788a0ffb263SMatthias Ringwald if (!hfp_connection){
2789c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
27903deb3ec6SMatthias Ringwald }
27913deb3ec6SMatthias Ringwald
2792c5fa3c94SMilanka Ringwald if (!hfp_connection->enable_extended_audio_gateway_error_report){
2793c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
2794c5fa3c94SMilanka Ringwald }
2795c5fa3c94SMilanka Ringwald
2796c5fa3c94SMilanka Ringwald hfp_connection->extended_audio_gateway_error = error;
2797c5fa3c94SMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
2798c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
2799c5fa3c94SMilanka Ringwald }
2800c5fa3c94SMilanka Ringwald
hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection)2801c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2802c5fa3c94SMilanka Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
2803c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
2804c5fa3c94SMilanka Ringwald }
2805c5fa3c94SMilanka Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
2806c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
2807c5fa3c94SMilanka Ringwald }
28083deb3ec6SMatthias Ringwald
2809a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1;
28102561704cSMatthias Ringwald hfp_connection->sco_for_msbc_failed = 0;
2811ced0de4dSMatthias Ringwald
2812*cbceaafaSMatthias Ringwald // Codec Negotiation is not supported by both sides, or HF forgot to exchange codecs
2813*cbceaafaSMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_IDLE){
2814*cbceaafaSMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2815*cbceaafaSMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
2816*cbceaafaSMatthias Ringwald }
2817ced0de4dSMatthias Ringwald
2818ced0de4dSMatthias Ringwald // Codecs list is available, but no codec negotiation happened yet
2819ced0de4dSMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
2820ced0de4dSMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)) {
2821ced0de4dSMatthias Ringwald // Start Codec Negotiation
2822ced0de4dSMatthias Ringwald hfp_connection->ag_send_common_codec = true;
2823ced0de4dSMatthias Ringwald return ERROR_CODE_SUCCESS;
2824ced0de4dSMatthias Ringwald } else {
2825ced0de4dSMatthias Ringwald // No Codec Negotiation -> done, ready to connect
2826d6ff09e1SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2827d6ff09e1SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2828a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
2829ced0de4dSMatthias Ringwald }
28303deb3ec6SMatthias Ringwald }
2831aa4dd815SMatthias Ringwald
2832ced0de4dSMatthias Ringwald // Codec has been negotiated before, start SCO setup
2833ced0de4dSMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_EXCHANGED){
2834ced0de4dSMatthias Ringwald // now, pick link settings
2835ced0de4dSMatthias Ringwald hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
2836ced0de4dSMatthias Ringwald // configure SBC coded if needed
2837ced0de4dSMatthias Ringwald hfp_prepare_for_sco(hfp_connection);
2838ced0de4dSMatthias Ringwald }
2839ced0de4dSMatthias Ringwald
2840c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
2841aa4dd815SMatthias Ringwald }
2842aa4dd815SMatthias Ringwald
hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle)2843c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
28449c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2845a33eb0c4SMilanka Ringwald if (!hfp_connection){
2846c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2847a33eb0c4SMilanka Ringwald }
2848c5fa3c94SMilanka Ringwald uint8_t status = hfp_ag_setup_audio_connection(hfp_connection);
2849c5fa3c94SMilanka Ringwald if (status != ERROR_CODE_SUCCESS){
2850c5fa3c94SMilanka Ringwald return status;
2851c5fa3c94SMilanka Ringwald }
2852f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
2853c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
28543deb3ec6SMatthias Ringwald }
28553deb3ec6SMatthias Ringwald
hfp_ag_release_audio_connection(hci_con_handle_t acl_handle)2856c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
28579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2858a33eb0c4SMilanka Ringwald if (!hfp_connection){
2859c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2860a33eb0c4SMilanka Ringwald }
2861c5fa3c94SMilanka Ringwald
28620b4debbfSMilanka Ringwald if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
28630b4debbfSMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
28640b4debbfSMilanka Ringwald }
28651a26de69SMilanka Ringwald
28660b4debbfSMilanka Ringwald uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
28670b4debbfSMilanka Ringwald if (status == ERROR_CODE_SUCCESS){
2868f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
28690b4debbfSMilanka Ringwald }
28700b4debbfSMilanka Ringwald return status;
28713deb3ec6SMatthias Ringwald }
2872aa4dd815SMatthias Ringwald
2873aa4dd815SMatthias Ringwald /**
2874aa4dd815SMatthias Ringwald * @brief Enable in-band ring tone
2875aa4dd815SMatthias Ringwald */
hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone)2876aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
287740a8ee13SMatthias Ringwald if (has_in_band_ring_tone() == false){
287840a8ee13SMatthias Ringwald return;
287940a8ee13SMatthias Ringwald }
288040a8ee13SMatthias Ringwald if (hfp_ag_in_band_ring_tone_active == use_in_band_ring_tone){
2881aa4dd815SMatthias Ringwald return;
2882aa4dd815SMatthias Ringwald }
2883c5fa3c94SMilanka Ringwald
288440a8ee13SMatthias Ringwald hfp_ag_in_band_ring_tone_active = use_in_band_ring_tone;
2885aa4dd815SMatthias Ringwald
2886665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
2887665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
2888665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
2889a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
289066c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
28919430c71eSMatthias Ringwald hfp_connection->ag_send_in_band_ring_tone_setting = true;
2892f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
2893aa4dd815SMatthias Ringwald }
2894aa4dd815SMatthias Ringwald }
2895aa4dd815SMatthias Ringwald
2896aa4dd815SMatthias Ringwald /**
2897aa4dd815SMatthias Ringwald * @brief Called from GSM
2898aa4dd815SMatthias Ringwald */
hfp_ag_incoming_call(void)2899aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2900aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2901aa4dd815SMatthias Ringwald }
2902aa4dd815SMatthias Ringwald
hfp_ag_outgoing_call_initiated(void)29030f1da533SMatthias Ringwald void hfp_ag_outgoing_call_initiated(void) {
2904fe899794SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL);
2905fe899794SMatthias Ringwald }
2906fe899794SMatthias Ringwald
2907aa4dd815SMatthias Ringwald /**
2908aa4dd815SMatthias Ringwald * @brief number is stored.
2909aa4dd815SMatthias Ringwald */
hfp_ag_set_clip(uint8_t type,const char * number)2910aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2911f8737b81SMatthias Ringwald hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number);
2912aa4dd815SMatthias Ringwald }
2913aa4dd815SMatthias Ringwald
hfp_ag_call_dropped(void)2914aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2915aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2916aa4dd815SMatthias Ringwald }
2917aa4dd815SMatthias Ringwald
2918aa4dd815SMatthias Ringwald // call from AG UI
hfp_ag_answer_incoming_call(void)2919aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2920aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2921aa4dd815SMatthias Ringwald }
2922aa4dd815SMatthias Ringwald
hfp_ag_join_held_call(void)2923ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2924ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2925ce263fc8SMatthias Ringwald }
2926ce263fc8SMatthias Ringwald
hfp_ag_terminate_call(void)2927aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2928aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2929aa4dd815SMatthias Ringwald }
2930aa4dd815SMatthias Ringwald
hfp_ag_outgoing_call_ringing(void)2931aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2932aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2933aa4dd815SMatthias Ringwald }
2934aa4dd815SMatthias Ringwald
hfp_ag_outgoing_call_established(void)2935aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2936aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2937aa4dd815SMatthias Ringwald }
2938aa4dd815SMatthias Ringwald
hfp_ag_outgoing_call_rejected(void)2939aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2940aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2941aa4dd815SMatthias Ringwald }
2942aa4dd815SMatthias Ringwald
hfp_ag_outgoing_call_accepted(void)2943aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2944aa4dd815SMatthias Ringwald hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2945aa4dd815SMatthias Ringwald }
2946ce263fc8SMatthias Ringwald
hfp_ag_hold_incoming_call(void)2947ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2948ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2949ce263fc8SMatthias Ringwald }
2950ce263fc8SMatthias Ringwald
hfp_ag_accept_held_incoming_call(void)2951ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2952ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2953ce263fc8SMatthias Ringwald }
2954ce263fc8SMatthias Ringwald
hfp_ag_reject_held_incoming_call(void)2955ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2956ce263fc8SMatthias Ringwald hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2957ce263fc8SMatthias Ringwald }
2958ce263fc8SMatthias Ringwald
hfp_ag_set_ag_indicator(const char * name,int value)2959aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2960aa4dd815SMatthias Ringwald int indicator_index = get_ag_indicator_index_for_name(name);
2961aa4dd815SMatthias Ringwald if (indicator_index < 0) return;
2962aa4dd815SMatthias Ringwald hfp_ag_indicators[indicator_index].status = value;
2963aa4dd815SMatthias Ringwald
2964665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it;
2965665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
2966665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){
2967a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
296866c5995fSMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2969a0ffb263SMatthias Ringwald if (!hfp_connection->ag_indicators[indicator_index].enabled) {
2970245852b7SMilanka Ringwald log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value);
2971ce263fc8SMatthias Ringwald continue;
2972ce263fc8SMatthias Ringwald }
2973245852b7SMilanka Ringwald log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value);
2974a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2975f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
2976aa4dd815SMatthias Ringwald }
2977aa4dd815SMatthias Ringwald }
2978aa4dd815SMatthias Ringwald
hfp_ag_set_registration_status(int registration_status)2979c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){
2980c5fa3c94SMilanka Ringwald if ((registration_status < 0) || (registration_status > 1)){
2981c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
2982c5fa3c94SMilanka Ringwald }
2983c5fa3c94SMilanka Ringwald
2984c5fa3c94SMilanka Ringwald if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
2985245852b7SMilanka Ringwald
2986245852b7SMilanka Ringwald // if network goes away wihle a call is active:
2987245852b7SMilanka Ringwald // - the call gets dropped
2988245852b7SMilanka Ringwald // - we send NO CARRIER
2989245852b7SMilanka Ringwald // NOTE: the CALL=0 has to be sent before NO CARRIER
2990245852b7SMilanka Ringwald
2991245852b7SMilanka Ringwald hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2992245852b7SMilanka Ringwald
2993245852b7SMilanka Ringwald btstack_linked_list_iterator_t it;
2994245852b7SMilanka Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections());
2995245852b7SMilanka Ringwald while (btstack_linked_list_iterator_has_next(&it)){
2996245852b7SMilanka Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2997245852b7SMilanka Ringwald hfp_connection->ag_send_no_carrier = true;
2998245852b7SMilanka Ringwald }
2999245852b7SMilanka Ringwald }
3000c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("service", registration_status);
3001c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3002aa4dd815SMatthias Ringwald }
3003aa4dd815SMatthias Ringwald
hfp_ag_set_signal_strength(int signal_strength)3004c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){
3005c5fa3c94SMilanka Ringwald if ((signal_strength < 0) || (signal_strength > 5)){
3006c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3007aa4dd815SMatthias Ringwald }
3008aa4dd815SMatthias Ringwald
3009c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("signal", signal_strength);
3010c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3011aa4dd815SMatthias Ringwald }
3012aa4dd815SMatthias Ringwald
hfp_ag_set_roaming_status(int roaming_status)3013c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){
3014c5fa3c94SMilanka Ringwald if ((roaming_status < 0) || (roaming_status > 1)){
3015c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3016aa4dd815SMatthias Ringwald }
3017aa4dd815SMatthias Ringwald
3018c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("roam", roaming_status);
3019c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3020c5fa3c94SMilanka Ringwald }
3021c5fa3c94SMilanka Ringwald
hfp_ag_set_battery_level(int battery_level)3022c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){
3023c5fa3c94SMilanka Ringwald if ((battery_level < 0) || (battery_level > 5)){
3024c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3025c5fa3c94SMilanka Ringwald }
3026c5fa3c94SMilanka Ringwald hfp_ag_set_ag_indicator("battchg", battery_level);
3027c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3028c5fa3c94SMilanka Ringwald }
3029c5fa3c94SMilanka Ringwald
hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle)30306b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){
303114c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
303214c148caSMilanka Ringwald if (!hfp_connection){
303314c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
30346b8275b0SMilanka Ringwald }
3035f71d2aacSMilanka Ringwald
303684fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){
303784fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
303884fb9ac1SMilanka Ringwald }
303984fb9ac1SMilanka Ringwald
30409cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
30419cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection);
3042f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){
304314c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
30446b8275b0SMilanka Ringwald }
30456b8275b0SMilanka Ringwald
3046135d9718SMilanka Ringwald if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){
3047135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3048f71d2aacSMilanka Ringwald }
3049f71d2aacSMilanka Ringwald
3050f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 1;
3051f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
3052fcf4ede6SMilanka Ringwald hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
3053f71d2aacSMilanka Ringwald hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection);
3054f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
30558f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true;
3056f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
3057f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS;
3058f71d2aacSMilanka Ringwald }
3059f71d2aacSMilanka Ringwald
hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle)3060d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){
306114c148caSMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
306214c148caSMilanka Ringwald if (!hfp_connection){
306314c148caSMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
306445796ff1SMilanka Ringwald }
3065f71d2aacSMilanka Ringwald
306684fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){
306784fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
306884fb9ac1SMilanka Ringwald }
306984fb9ac1SMilanka Ringwald
30709cd26862SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
30719cd26862SMilanka Ringwald bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection);
30729cd26862SMilanka Ringwald
3073f71d2aacSMilanka Ringwald if (!enhanced_vra_supported && !legacy_vra_supported){
307414c148caSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
307545796ff1SMilanka Ringwald }
307645796ff1SMilanka Ringwald
3077135d9718SMilanka Ringwald if (!hfp_ag_voice_recognition_session_active(hfp_connection)){
3078135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3079f71d2aacSMilanka Ringwald }
3080f71d2aacSMilanka Ringwald
3081f71d2aacSMilanka Ringwald hfp_connection->ag_activate_voice_recognition_value = 0;
3082f71d2aacSMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
3083f71d2aacSMilanka Ringwald hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
30848f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true;
3085f71d2aacSMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
3086f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS;
3087f71d2aacSMilanka Ringwald }
3088f71d2aacSMilanka Ringwald
hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle,hfp_voice_recognition_state_t state)3089cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){
309045796ff1SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
309145796ff1SMilanka Ringwald if (!hfp_connection){
3092c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
309345796ff1SMilanka Ringwald }
3094c5fa3c94SMilanka Ringwald
309584fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){
309684fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3097f71d2aacSMilanka Ringwald }
3098f71d2aacSMilanka Ringwald
3099645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
3100645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3101645bfa78SMilanka Ringwald }
3102645bfa78SMilanka Ringwald
3103645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
3104135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
31059cd26862SMilanka Ringwald }
31069cd26862SMilanka Ringwald
310784fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
310884fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){
310984fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
311084fb9ac1SMilanka Ringwald }
311184fb9ac1SMilanka Ringwald
311284fb9ac1SMilanka Ringwald
311345796ff1SMilanka Ringwald hfp_connection->ag_vra_state = state;
3114cf75be85SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS;
31158f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true;
311645796ff1SMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
31179cd26862SMilanka Ringwald return ERROR_CODE_SUCCESS;
311845796ff1SMilanka Ringwald }
311945796ff1SMilanka Ringwald
hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle)3120f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){
3121cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND);
312245796ff1SMilanka Ringwald }
hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle)3123f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
3124cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT);
312545796ff1SMilanka Ringwald }
hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle)3126cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){
3127cf75be85SMilanka Ringwald return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT);
312845796ff1SMilanka Ringwald }
312945796ff1SMilanka Ringwald
3130b95cac54SMilanka Ringwald
hfp_ag_enhanced_voice_recognition_send_message(hci_con_handle_t acl_handle,hfp_voice_recognition_state_t state,hfp_voice_recognition_message_t msg)3131b95cac54SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_send_message(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state, hfp_voice_recognition_message_t msg){
3132b95cac54SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3133b95cac54SMilanka Ringwald if (!hfp_connection){
3134b95cac54SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3135b95cac54SMilanka Ringwald }
3136b95cac54SMilanka Ringwald
313784fb9ac1SMilanka Ringwald if (hfp_connection->emit_vra_enabled_after_audio_established){
313884fb9ac1SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3139f71d2aacSMilanka Ringwald }
3140f71d2aacSMilanka Ringwald
3141645bfa78SMilanka Ringwald if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
3142645bfa78SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3143645bfa78SMilanka Ringwald }
3144645bfa78SMilanka Ringwald
3145645bfa78SMilanka Ringwald if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
3146135d9718SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3147f71d2aacSMilanka Ringwald }
3148f71d2aacSMilanka Ringwald
314984fb9ac1SMilanka Ringwald bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
315084fb9ac1SMilanka Ringwald if (!enhanced_vra_supported ){
315184fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
315284fb9ac1SMilanka Ringwald }
315384fb9ac1SMilanka Ringwald
315484fb9ac1SMilanka Ringwald bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection);
315584fb9ac1SMilanka Ringwald if (!enhanced_vra_msg_supported ){
315684fb9ac1SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
315784fb9ac1SMilanka Ringwald }
315884fb9ac1SMilanka Ringwald
3159ab2445a0SMatthias Ringwald uint16_t message_len = (uint16_t) strlen(msg.text);
316051aa5d5aSMilanka Ringwald
316189e7c136SMilanka Ringwald if (message_len > HFP_MAX_VR_TEXT_SIZE){
31626504a03aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
31636504a03aSMilanka Ringwald }
31646504a03aSMilanka Ringwald
31656504a03aSMilanka Ringwald if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){
316651aa5d5aSMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
316751aa5d5aSMilanka Ringwald }
316851aa5d5aSMilanka Ringwald
3169b95cac54SMilanka Ringwald hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG;
3170b95cac54SMilanka Ringwald hfp_connection->ag_msg = msg;
3171b95cac54SMilanka Ringwald hfp_connection->ag_vra_state = state;
31728f8818a4SMatthias Ringwald hfp_connection->ag_vra_send_command = true;
3173b95cac54SMilanka Ringwald hfp_ag_run_for_context(hfp_connection);
3174f71d2aacSMilanka Ringwald
3175f71d2aacSMilanka Ringwald return ERROR_CODE_SUCCESS;
3176b95cac54SMilanka Ringwald }
3177b95cac54SMilanka Ringwald
3178b95cac54SMilanka Ringwald
hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle,int gain)3179c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
31809c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3181a33eb0c4SMilanka Ringwald if (!hfp_connection){
3182c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3183a33eb0c4SMilanka Ringwald }
3184c5fa3c94SMilanka Ringwald
3185c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){
3186c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
3187c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3188c5fa3c94SMilanka Ringwald }
3189c5fa3c94SMilanka Ringwald
3190a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain != gain){
3191a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain;
3192a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1;
3193aa4dd815SMatthias Ringwald }
3194f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
3195c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3196aa4dd815SMatthias Ringwald }
3197aa4dd815SMatthias Ringwald
hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle,int gain)3198c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
31999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3200a33eb0c4SMilanka Ringwald if (!hfp_connection){
3201c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3202a33eb0c4SMilanka Ringwald }
3203c5fa3c94SMilanka Ringwald
3204c5fa3c94SMilanka Ringwald if ((gain < 0) || (gain > 15)){
3205c5fa3c94SMilanka Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
3206c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3207c5fa3c94SMilanka Ringwald }
3208c5fa3c94SMilanka Ringwald
3209a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain != gain){
3210a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain;
3211a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1;
3212aa4dd815SMatthias Ringwald }
3213f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
3214c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3215aa4dd815SMatthias Ringwald }
3216aa4dd815SMatthias Ringwald
hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle,const char * number)3217c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
32189c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3219a33eb0c4SMilanka Ringwald if (!hfp_connection){
3220c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3221a33eb0c4SMilanka Ringwald }
3222c5fa3c94SMilanka Ringwald
3223aa4dd815SMatthias Ringwald hfp_ag_set_clip(0, number);
3224a0ffb263SMatthias Ringwald hfp_connection->send_phone_number_for_voice_tag = 1;
3225c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3226aa4dd815SMatthias Ringwald }
3227aa4dd815SMatthias Ringwald
hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle)3228c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
32299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3230a33eb0c4SMilanka Ringwald if (!hfp_connection){
3231c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3232a33eb0c4SMilanka Ringwald }
3233c5fa3c94SMilanka Ringwald
3234e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
3235c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3236aa4dd815SMatthias Ringwald }
3237aa4dd815SMatthias Ringwald
hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle)3238c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
32399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3240a33eb0c4SMilanka Ringwald if (!hfp_connection){
3241c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3242a33eb0c4SMilanka Ringwald }
3243c5fa3c94SMilanka Ringwald
3244e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
3245c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3246c1797c7dSMatthias Ringwald }
3247aa4dd815SMatthias Ringwald
hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle,const char * unsolicited_result_code)32485be6b47aSMatthias Ringwald uint8_t hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle, const char * unsolicited_result_code){
32495be6b47aSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
32505be6b47aSMatthias Ringwald if (!hfp_connection){
32515be6b47aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
32525be6b47aSMatthias Ringwald }
32535be6b47aSMatthias Ringwald if (hfp_connection->send_custom_message != NULL){
32545be6b47aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
32555be6b47aSMatthias Ringwald }
32565be6b47aSMatthias Ringwald hfp_connection->send_custom_message = unsolicited_result_code;
32575be6b47aSMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
32585be6b47aSMatthias Ringwald return ERROR_CODE_SUCCESS;
32595be6b47aSMatthias Ringwald }
32605be6b47aSMatthias Ringwald
hfp_ag_send_command_result_code(hci_con_handle_t acl_handle,bool ok)3261471dea41SMatthias Ringwald uint8_t hfp_ag_send_command_result_code(hci_con_handle_t acl_handle, bool ok){
3262471dea41SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3263471dea41SMatthias Ringwald if (!hfp_connection){
3264471dea41SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3265471dea41SMatthias Ringwald }
3266471dea41SMatthias Ringwald if (ok){
3267e84fa067SMatthias Ringwald hfp_ag_queue_ok(hfp_connection);
3268471dea41SMatthias Ringwald } else {
3269e84fa067SMatthias Ringwald hfp_ag_queue_error(hfp_connection);
3270471dea41SMatthias Ringwald }
3271471dea41SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
3272471dea41SMatthias Ringwald return ERROR_CODE_SUCCESS;
3273471dea41SMatthias Ringwald }
3274471dea41SMatthias Ringwald
hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers,int numbers_count)3275ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
3276aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers = numbers;
3277aeb0f0feSMatthias Ringwald hfp_ag_subscriber_numbers_count = numbers_count;
3278ce263fc8SMatthias Ringwald }
3279ce263fc8SMatthias Ringwald
hfp_ag_clear_last_dialed_number(void)32809cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
32819cae807eSMatthias Ringwald hfp_gsm_clear_last_dialed_number();
3282ce263fc8SMatthias Ringwald }
3283ce263fc8SMatthias Ringwald
hfp_ag_set_last_dialed_number(const char * number)32849de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){
32859de679b7SMilanka Ringwald hfp_gsm_set_last_dialed_number(number);
32869de679b7SMilanka Ringwald }
32879de679b7SMilanka Ringwald
hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle)3288c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
32899c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3290a33eb0c4SMilanka Ringwald if (!hfp_connection){
3291c5fa3c94SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3292a33eb0c4SMilanka Ringwald }
3293c5fa3c94SMilanka Ringwald
3294c5fa3c94SMilanka Ringwald if (!hfp_connection->call_waiting_notification_enabled){
3295c5fa3c94SMilanka Ringwald return ERROR_CODE_COMMAND_DISALLOWED;
3296c5fa3c94SMilanka Ringwald }
3297a0ffb263SMatthias Ringwald
3298a0ffb263SMatthias Ringwald hfp_connection->ag_notify_incoming_call_waiting = 1;
3299f0aeb307SMatthias Ringwald hfp_ag_run_for_context(hfp_connection);
3300c5fa3c94SMilanka Ringwald return ERROR_CODE_SUCCESS;
3301a0ffb263SMatthias Ringwald }
hfp_ag_create_sdp_record_with_codecs(uint8_t * service,uint32_t service_record_handle,int rfcomm_channel_nr,const char * name,uint8_t ability_to_reject_call,uint16_t supported_features,uint8_t codecs_nr,const uint8_t * codecs)3302aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr,
3303aa10b9cbSMatthias Ringwald const char * name, uint8_t ability_to_reject_call, uint16_t supported_features,
3304aa10b9cbSMatthias Ringwald uint8_t codecs_nr, const uint8_t * codecs){
330576cc1527SMatthias Ringwald if (!name){
3306aeb0f0feSMatthias Ringwald name = hfp_ag_default_service_name;
330776cc1527SMatthias Ringwald }
330876cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
330976cc1527SMatthias Ringwald
331076cc1527SMatthias Ringwald /*
331176cc1527SMatthias Ringwald * 0x01 – Ability to reject a call
331276cc1527SMatthias Ringwald * 0x00 – No ability to reject a call
331376cc1527SMatthias Ringwald */
331476cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network
331576cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
331676cc1527SMatthias Ringwald
331776cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap:
331876cc1527SMatthias Ringwald //
331976cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
332076cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF"
332176cc1527SMatthias Ringwald //
3322aa10b9cbSMatthias Ringwald // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation
332376cc1527SMatthias Ringwald //
332476cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f;
3325ef3ae4ebSMilanka Ringwald
3326ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
3327ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 6;
3328ef3ae4ebSMilanka Ringwald }
3329ef3ae4ebSMilanka Ringwald
3330ef3ae4ebSMilanka Ringwald if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){
3331ef3ae4ebSMilanka Ringwald sdp_features |= 1 << 7;
3332ef3ae4ebSMilanka Ringwald }
3333ef3ae4ebSMilanka Ringwald
3334aa10b9cbSMatthias Ringwald // codecs
3335aa10b9cbSMatthias Ringwald if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){
3336aa10b9cbSMatthias Ringwald uint8_t i;
3337aa10b9cbSMatthias Ringwald for (i=0;i<codecs_nr;i++){
3338aa10b9cbSMatthias Ringwald switch (codecs[i]){
3339aa10b9cbSMatthias Ringwald case HFP_CODEC_MSBC:
3340aa10b9cbSMatthias Ringwald sdp_features |= 1 << 5;
3341aa10b9cbSMatthias Ringwald break;
3342aa10b9cbSMatthias Ringwald case HFP_CODEC_LC3_SWB:
3343aa10b9cbSMatthias Ringwald sdp_features |= 1 << 8;
3344aa10b9cbSMatthias Ringwald break;
334575389f80SMatthias Ringwald default:
334675389f80SMatthias Ringwald break;
3347aa10b9cbSMatthias Ringwald }
3348aa10b9cbSMatthias Ringwald }
3349aa10b9cbSMatthias Ringwald }
3350aa10b9cbSMatthias Ringwald
335176cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures
335276cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
335376cc1527SMatthias Ringwald }
335476cc1527SMatthias Ringwald
hfp_ag_create_sdp_record(uint8_t * service,uint32_t service_record_handle,int rfcomm_channel_nr,const char * name,uint8_t ability_to_reject_call,uint16_t supported_features,int wide_band_speech)3355aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name,
3356aa10b9cbSMatthias Ringwald uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
3357aa10b9cbSMatthias Ringwald uint8_t codecs_nr;
3358aa10b9cbSMatthias Ringwald const uint8_t * codecs;
3359aa10b9cbSMatthias Ringwald const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC };
3360aa10b9cbSMatthias Ringwald if (wide_band_speech == 0){
3361aa10b9cbSMatthias Ringwald codecs_nr = 0;
3362aa10b9cbSMatthias Ringwald codecs = NULL;
3363aa10b9cbSMatthias Ringwald } else {
3364aa10b9cbSMatthias Ringwald codecs_nr = 1;
3365aa10b9cbSMatthias Ringwald codecs = wide_band_codecs;
3366aa10b9cbSMatthias Ringwald }
3367aa10b9cbSMatthias Ringwald hfp_ag_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name,
3368aa10b9cbSMatthias Ringwald ability_to_reject_call, supported_features, codecs_nr, codecs);
3369aa10b9cbSMatthias Ringwald }
3370aa10b9cbSMatthias Ringwald
hfp_ag_register_packet_handler(btstack_packet_handler_t callback)337176cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
337268466199SMilanka Ringwald btstack_assert(callback != NULL);
337368466199SMilanka Ringwald
337476cc1527SMatthias Ringwald hfp_ag_callback = callback;
337576cc1527SMatthias Ringwald hfp_set_ag_callback(callback);
337676cc1527SMatthias Ringwald }
337781e25d0aSMatthias Ringwald
hfp_ag_register_custom_call_sm_handler(bool (* handler)(hfp_ag_call_event_t event))337881e25d0aSMatthias Ringwald void hfp_ag_register_custom_call_sm_handler(bool (*handler)(hfp_ag_call_event_t event)){
337981e25d0aSMatthias Ringwald hfp_ag_custom_call_sm_handler = handler;
338081e25d0aSMatthias Ringwald }
3381471dea41SMatthias Ringwald
hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command)3382471dea41SMatthias Ringwald void hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){
3383471dea41SMatthias Ringwald hfp_register_custom_ag_command(custom_at_command);
3384471dea41SMatthias Ringwald }
3385