xref: /btstack/example/hfp_ag_demo.c (revision de9e0ea7a74966417a0fd17ac6486a0d1ada55c5)
1fffdd288SMatthias Ringwald /*
2fffdd288SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3fffdd288SMatthias Ringwald  *
4fffdd288SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5fffdd288SMatthias Ringwald  * modification, are permitted provided that the following conditions
6fffdd288SMatthias Ringwald  * are met:
7fffdd288SMatthias Ringwald  *
8fffdd288SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9fffdd288SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10fffdd288SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11fffdd288SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12fffdd288SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13fffdd288SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14fffdd288SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15fffdd288SMatthias Ringwald  *    from this software without specific prior written permission.
16fffdd288SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17fffdd288SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18fffdd288SMatthias Ringwald  *    monetary gain.
19fffdd288SMatthias Ringwald  *
20fffdd288SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21fffdd288SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22fffdd288SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23fffdd288SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24fffdd288SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25fffdd288SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26fffdd288SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27fffdd288SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28fffdd288SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29fffdd288SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30fffdd288SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31fffdd288SMatthias Ringwald  * SUCH DAMAGE.
32fffdd288SMatthias Ringwald  *
33fffdd288SMatthias Ringwald  * Please inquire about commercial licensing options at
34fffdd288SMatthias Ringwald  * [email protected]
35fffdd288SMatthias Ringwald  *
36fffdd288SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag_demo.c"
39fffdd288SMatthias Ringwald 
40fffdd288SMatthias Ringwald /*
41fffdd288SMatthias Ringwald  * hfp_ag_demo.c
42fffdd288SMatthias Ringwald  */
43fffdd288SMatthias Ringwald 
44fffdd288SMatthias Ringwald // *****************************************************************************
45ec8ae085SMilanka Ringwald /* EXAMPLE_START(hfp_ag_demo): HFP AG - Audio Gateway
46fffdd288SMatthias Ringwald  *
47fffdd288SMatthias Ringwald  * @text This HFP Audio Gateway example demonstrates how to receive
48fffdd288SMatthias Ringwald  * an output from a remote HFP Hands-Free (HF) unit, and,
497ea7688aSMatthias Ringwald  * if HAVE_BTSTACK_STDIN is defined, how to control the HFP HF.
50fffdd288SMatthias Ringwald  */
51fffdd288SMatthias Ringwald // *****************************************************************************
52fffdd288SMatthias Ringwald 
53fffdd288SMatthias Ringwald 
54fffdd288SMatthias Ringwald #include <stdint.h>
55fffdd288SMatthias Ringwald #include <stdio.h>
56fffdd288SMatthias Ringwald #include <stdlib.h>
57fffdd288SMatthias Ringwald #include <string.h>
58fffdd288SMatthias Ringwald 
59fffdd288SMatthias Ringwald #include "btstack.h"
60185c8cd4SMatthias Ringwald #include "sco_demo_util.h"
617ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
627ea7688aSMatthias Ringwald #include "btstack_stdin.h"
634af4141bSMatthias Ringwald #endif
64fffdd288SMatthias Ringwald 
6580b0ebb6SMatthias Ringwald // uncomment to temp disable mSBC codec
6680b0ebb6SMatthias Ringwald // #undef ENABLE_HFP_WIDE_BAND_SPEECH
6780b0ebb6SMatthias Ringwald 
68fffdd288SMatthias Ringwald uint8_t hfp_service_buffer[150];
69fffdd288SMatthias Ringwald const uint8_t    rfcomm_channel_nr = 1;
700c2b8870SMatthias Ringwald const char hfp_ag_service_name[] = "HFP AG Demo";
71fffdd288SMatthias Ringwald 
720e62b7a5SMatthias Ringwald static bd_addr_t device_addr;
73*de9e0ea7SMilanka Ringwald static const char * device_addr_string = "00:1A:7D:DA:71:13";
74fffdd288SMatthias Ringwald 
751bbecc2bSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
76fa5e2954SMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
771bbecc2bSMatthias Ringwald #else
781bbecc2bSMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD};
791bbecc2bSMatthias Ringwald #endif
801bbecc2bSMatthias Ringwald 
81220eb563SMilanka Ringwald static uint8_t negotiated_codec = HFP_CODEC_CVSD;
82220eb563SMilanka Ringwald 
8380b0ebb6SMatthias Ringwald static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID;
8480b0ebb6SMatthias Ringwald static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID;
85fffdd288SMatthias Ringwald static int memory_1_enabled = 1;
86186bfbecSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
87fffdd288SMatthias Ringwald 
88fffdd288SMatthias Ringwald static int ag_indicators_nr = 7;
89fffdd288SMatthias Ringwald static hfp_ag_indicator_t ag_indicators[] = {
90fffdd288SMatthias Ringwald     // index, name, min range, max range, status, mandatory, enabled, status changed
91fffdd288SMatthias Ringwald     {1, "service",   0, 1, 1, 0, 0, 0},
92fffdd288SMatthias Ringwald     {2, "call",      0, 1, 0, 1, 1, 0},
93fffdd288SMatthias Ringwald     {3, "callsetup", 0, 3, 0, 1, 1, 0},
94fffdd288SMatthias Ringwald     {4, "battchg",   0, 5, 3, 0, 0, 0},
95fffdd288SMatthias Ringwald     {5, "signal",    0, 5, 5, 0, 1, 0},
96fffdd288SMatthias Ringwald     {6, "roam",      0, 1, 0, 0, 1, 0},
97fffdd288SMatthias Ringwald     {7, "callheld",  0, 2, 0, 1, 1, 0}
98fffdd288SMatthias Ringwald };
99fffdd288SMatthias Ringwald 
100fffdd288SMatthias Ringwald static int call_hold_services_nr = 5;
101fffdd288SMatthias Ringwald static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"};
102fffdd288SMatthias Ringwald 
103fffdd288SMatthias Ringwald static int hf_indicators_nr = 2;
104fffdd288SMatthias Ringwald static hfp_generic_status_indicator_t hf_indicators[] = {
105fffdd288SMatthias Ringwald     {1, 1},
106fffdd288SMatthias Ringwald     {2, 1},
107fffdd288SMatthias Ringwald };
108fffdd288SMatthias Ringwald 
109fffdd288SMatthias Ringwald #define INQUIRY_INTERVAL 5
110fffdd288SMatthias Ringwald 
111fffdd288SMatthias Ringwald enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
112fffdd288SMatthias Ringwald enum STATE state = INIT;
113fffdd288SMatthias Ringwald 
1143b9522a6SMatthias Ringwald static void dump_supported_codecs(void){
115249d94cfSMatthias Ringwald     unsigned int i;
116f9f7adeeSMatthias Ringwald     int mSBC_skipped = 0;
117b3f76298SMilanka Ringwald     printf("Supported codecs: ");
118b3f76298SMilanka Ringwald     for (i = 0; i < sizeof(codecs); i++){
119b3f76298SMilanka Ringwald         switch(codecs[i]){
120b3f76298SMilanka Ringwald             case HFP_CODEC_CVSD:
121b3f76298SMilanka Ringwald                 printf("CVSD");
122b3f76298SMilanka Ringwald                 break;
123b3f76298SMilanka Ringwald             case HFP_CODEC_MSBC:
124f9f7adeeSMatthias Ringwald                 if (hci_extended_sco_link_supported()){
1252308e108SMilanka Ringwald                     printf(", mSBC");
126f9f7adeeSMatthias Ringwald                 } else {
127f9f7adeeSMatthias Ringwald                     mSBC_skipped = 1;
128f9f7adeeSMatthias Ringwald                 }
129b3f76298SMilanka Ringwald                 break;
1307bbeb3adSMilanka Ringwald             default:
1317bbeb3adSMilanka Ringwald                 btstack_assert(false);
1327bbeb3adSMilanka Ringwald                 break;
133b3f76298SMilanka Ringwald         }
134b3f76298SMilanka Ringwald     }
135b3f76298SMilanka Ringwald     printf("\n");
136f9f7adeeSMatthias Ringwald     if (mSBC_skipped){
137f9f7adeeSMatthias Ringwald         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
138f9f7adeeSMatthias Ringwald     }
139b3f76298SMilanka Ringwald }
140fffdd288SMatthias Ringwald 
1417ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
142fffdd288SMatthias Ringwald // Testig User Interface
143fffdd288SMatthias Ringwald static void show_usage(void){
144e64e0086SMatthias Ringwald     bd_addr_t iut_address;
145e64e0086SMatthias Ringwald     gap_local_bd_addr(iut_address);
146e64e0086SMatthias Ringwald 
147e64e0086SMatthias Ringwald     printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
1488a7d93dcSMatthias Ringwald     printf("\n");
149fffdd288SMatthias Ringwald 
15005d93378SMilanka Ringwald     printf("a - establish HFP connection to %s\n", bd_addr_to_str(device_addr));
15105d93378SMilanka Ringwald     // printf("A - release HFP connection\n");
152fffdd288SMatthias Ringwald 
1538a7d93dcSMatthias Ringwald     printf("b - establish AUDIO connection          | B - release AUDIO connection\n");
1548a7d93dcSMatthias Ringwald     printf("c - simulate incoming call from 1234567 | C - simulate call from 1234567 dropped\n");
155fffdd288SMatthias Ringwald     printf("d - report AG failure\n");
156584803f1SMatthias Ringwald     printf("D - delete all link keys\n");
1578a7d93dcSMatthias Ringwald     printf("e - answer call on AG                   | E - reject call on AG\n");
1588a7d93dcSMatthias Ringwald     printf("r - disable in-band ring tone           | R - enable in-band ring tone\n");
1598a7d93dcSMatthias Ringwald     printf("f - Disable cellular network            | F - Enable cellular network\n");
1608a7d93dcSMatthias Ringwald     printf("g - Set signal strength to 0            | G - Set signal strength to 5\n");
1618a7d93dcSMatthias Ringwald     printf("h - Disable roaming                     | H - Enable roaming\n");
1628a7d93dcSMatthias Ringwald     printf("i - Set battery level to 3              | I - Set battery level to 5\n");
163fffdd288SMatthias Ringwald     printf("j - Answering call on remote side\n");
1648a7d93dcSMatthias Ringwald     printf("k - Clear memory #1                     | K - Set memory #1\n");
1658a7d93dcSMatthias Ringwald     printf("l - Clear last number                   | L - Set last number\n");
166fffdd288SMatthias Ringwald     printf("m - simulate incoming call from 7654321\n");
167fffdd288SMatthias Ringwald     // printf("M - simulate call from 7654321 dropped\n");
168ac6f828eSMilanka Ringwald     printf("n - Disable Voice Recognition           | N - Enable Voice Recognition\n");
1696b67407dSMilanka Ringwald     printf("z - Disable Enhanced Voice Recognition  | Z - Enable Ehnaced Voice Recognition\n");
1708a7d93dcSMatthias Ringwald     printf("o - Set speaker volume to 0  (minimum)  | O - Set speaker volume to 9  (default)\n");
1718a7d93dcSMatthias Ringwald     printf("p - Set speaker volume to 12 (higher)   | P - Set speaker volume to 15 (maximum)\n");
1728a7d93dcSMatthias Ringwald     printf("q - Set microphone gain to 0  (minimum) | Q - Set microphone gain to 9  (default)\n");
1738a7d93dcSMatthias Ringwald     printf("s - Set microphone gain to 12 (higher)  | S - Set microphone gain to 15 (maximum)\n");
174fffdd288SMatthias Ringwald     printf("t - terminate connection\n");
175fffdd288SMatthias Ringwald     printf("u - join held call\n");
176fffdd288SMatthias Ringwald     printf("v - discover nearby HF units\n");
177fffdd288SMatthias Ringwald     printf("w - put incoming call on hold (Response and Hold)\n");
178fffdd288SMatthias Ringwald     printf("x - accept held incoming call (Response and Hold)\n");
179fffdd288SMatthias Ringwald     printf("X - reject held incoming call (Response and Hold)\n");
180fffdd288SMatthias Ringwald     printf("---\n");
181fffdd288SMatthias Ringwald }
182fffdd288SMatthias Ringwald 
18395a8ee01SMatthias Ringwald static void stdin_process(char cmd){
184ac6f828eSMilanka Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
185ac6f828eSMilanka Ringwald 
186fffdd288SMatthias Ringwald     switch (cmd){
187fffdd288SMatthias Ringwald         case 'a':
188fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
18905d93378SMilanka Ringwald             printf("Establish HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
190ac6f828eSMilanka Ringwald             status = hfp_ag_establish_service_level_connection(device_addr);
191fffdd288SMatthias Ringwald             break;
192fffdd288SMatthias Ringwald         case 'A':
193fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
194fffdd288SMatthias Ringwald             printf("Release HFP service level connection.\n");
195ac6f828eSMilanka Ringwald             status = hfp_ag_release_service_level_connection(acl_handle);
196fffdd288SMatthias Ringwald             break;
1976b67407dSMilanka Ringwald 
198fffdd288SMatthias Ringwald         case 'b':
199fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
200fffdd288SMatthias Ringwald             printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
201ac6f828eSMilanka Ringwald             status = hfp_ag_establish_audio_connection(acl_handle);
202fffdd288SMatthias Ringwald             break;
203fffdd288SMatthias Ringwald         case 'B':
204fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
205fffdd288SMatthias Ringwald             printf("Release Audio connection.\n");
206ac6f828eSMilanka Ringwald             status = hfp_ag_release_audio_connection(acl_handle);
207fffdd288SMatthias Ringwald             break;
208fffdd288SMatthias Ringwald         case 'c':
209fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
210fffdd288SMatthias Ringwald             printf("Simulate incoming call from 1234567\n");
211fffdd288SMatthias Ringwald             hfp_ag_set_clip(129, "1234567");
212fffdd288SMatthias Ringwald             hfp_ag_incoming_call();
213fffdd288SMatthias Ringwald             break;
214584803f1SMatthias Ringwald         case 'D':
215584803f1SMatthias Ringwald             printf("Deleting all link keys\n");
216584803f1SMatthias Ringwald             gap_delete_all_link_keys();
217584803f1SMatthias Ringwald             break;
218fffdd288SMatthias Ringwald         case 'm':
219fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
220fffdd288SMatthias Ringwald             printf("Simulate incoming call from 7654321\n");
221fffdd288SMatthias Ringwald             hfp_ag_set_clip(129, "7654321");
222fffdd288SMatthias Ringwald             hfp_ag_incoming_call();
223fffdd288SMatthias Ringwald             break;
224fffdd288SMatthias Ringwald         case 'C':
225fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
226fffdd288SMatthias Ringwald             printf("Simulate terminate call\n");
227fffdd288SMatthias Ringwald             hfp_ag_call_dropped();
228fffdd288SMatthias Ringwald             break;
229fffdd288SMatthias Ringwald         case 'd':
230fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
231fffdd288SMatthias Ringwald             printf("Report AG failure\n");
232ac6f828eSMilanka Ringwald             status = hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
233fffdd288SMatthias Ringwald             break;
234fffdd288SMatthias Ringwald         case 'e':
235fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
236fffdd288SMatthias Ringwald             printf("Answer call on AG\n");
237fffdd288SMatthias Ringwald             hfp_ag_answer_incoming_call();
238fffdd288SMatthias Ringwald             break;
239fffdd288SMatthias Ringwald         case 'E':
240fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
241fffdd288SMatthias Ringwald             printf("Reject call on AG\n");
242fffdd288SMatthias Ringwald             hfp_ag_terminate_call();
243fffdd288SMatthias Ringwald             break;
244fffdd288SMatthias Ringwald         case 'f':
245fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
246fffdd288SMatthias Ringwald             printf("Disable cellular network\n");
247ac6f828eSMilanka Ringwald             status = hfp_ag_set_registration_status(0);
248fffdd288SMatthias Ringwald             break;
249fffdd288SMatthias Ringwald         case 'F':
250fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
251fffdd288SMatthias Ringwald             printf("Enable cellular network\n");
252ac6f828eSMilanka Ringwald             status = hfp_ag_set_registration_status(1);
253fffdd288SMatthias Ringwald             break;
254fffdd288SMatthias Ringwald         case 'g':
255fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
256fffdd288SMatthias Ringwald             printf("Set signal strength to 0\n");
257ac6f828eSMilanka Ringwald             status = hfp_ag_set_signal_strength(0);
258fffdd288SMatthias Ringwald             break;
259fffdd288SMatthias Ringwald         case 'G':
260fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
261fffdd288SMatthias Ringwald             printf("Set signal strength to 5\n");
262ac6f828eSMilanka Ringwald             status = hfp_ag_set_signal_strength(5);
263fffdd288SMatthias Ringwald             break;
264fffdd288SMatthias Ringwald         case 'h':
265fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
266fffdd288SMatthias Ringwald             printf("Disable roaming\n");
267ac6f828eSMilanka Ringwald             status = hfp_ag_set_roaming_status(0);
268fffdd288SMatthias Ringwald             break;
269fffdd288SMatthias Ringwald         case 'H':
270fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
271fffdd288SMatthias Ringwald             printf("Enable roaming\n");
272ac6f828eSMilanka Ringwald             status = hfp_ag_set_roaming_status(1);
273fffdd288SMatthias Ringwald             break;
274fffdd288SMatthias Ringwald         case 'i':
275fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
276fffdd288SMatthias Ringwald             printf("Set battery level to 3\n");
277ac6f828eSMilanka Ringwald             status = hfp_ag_set_battery_level(3);
278fffdd288SMatthias Ringwald             break;
279fffdd288SMatthias Ringwald         case 'I':
280fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
281fffdd288SMatthias Ringwald             printf("Set battery level to 5\n");
282ac6f828eSMilanka Ringwald             status = hfp_ag_set_battery_level(5);
283fffdd288SMatthias Ringwald             break;
284fffdd288SMatthias Ringwald         case 'j':
285fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
286fffdd288SMatthias Ringwald             printf("Answering call on remote side\n");
287fffdd288SMatthias Ringwald             hfp_ag_outgoing_call_established();
288fffdd288SMatthias Ringwald             break;
289fffdd288SMatthias Ringwald         case 'r':
290fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
291fffdd288SMatthias Ringwald             printf("Disable in-band ring tone\n");
292fffdd288SMatthias Ringwald             hfp_ag_set_use_in_band_ring_tone(0);
293fffdd288SMatthias Ringwald             break;
294fffdd288SMatthias Ringwald         case 'k':
295fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
296fffdd288SMatthias Ringwald             printf("Memory 1 cleared\n");
297fffdd288SMatthias Ringwald             memory_1_enabled = 0;
298fffdd288SMatthias Ringwald             break;
299fffdd288SMatthias Ringwald         case 'K':
300fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
301fffdd288SMatthias Ringwald             printf("Memory 1 set\n");
302fffdd288SMatthias Ringwald             memory_1_enabled = 1;
303fffdd288SMatthias Ringwald             break;
304fffdd288SMatthias Ringwald         case 'l':
305fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
306fffdd288SMatthias Ringwald             printf("Last dialed number cleared\n");
307fffdd288SMatthias Ringwald             hfp_ag_clear_last_dialed_number();
308fffdd288SMatthias Ringwald             break;
309fffdd288SMatthias Ringwald         case 'L':
310fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
311fffdd288SMatthias Ringwald             printf("Outgoing call connected, ringing\n");
312fffdd288SMatthias Ringwald             hfp_ag_outgoing_call_ringing();
313fffdd288SMatthias Ringwald             break;
314fffdd288SMatthias Ringwald         case 'n':
315fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
316fffdd288SMatthias Ringwald             printf("Disable Voice Recognition\n");
317d84719b1SMilanka Ringwald             status = hfp_ag_deactivate_voice_recognition(acl_handle);
318fffdd288SMatthias Ringwald             break;
319fffdd288SMatthias Ringwald         case 'N':
320fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
321fffdd288SMatthias Ringwald             printf("Enable Voice Recognition\n");
322d84719b1SMilanka Ringwald             status = hfp_ag_activate_voice_recognition(acl_handle);
323fffdd288SMatthias Ringwald             break;
3246b67407dSMilanka Ringwald 
3256b67407dSMilanka Ringwald         case 'z':
3266b67407dSMilanka Ringwald             log_info("USER:\'%c\'", cmd);
3276b67407dSMilanka Ringwald             printf("Disable Enhanced Voice Recognition\n");
3286b67407dSMilanka Ringwald             status = hfp_ag_deactivate_enhanced_voice_recognition(acl_handle);
3296b67407dSMilanka Ringwald             break;
3306b67407dSMilanka Ringwald         case 'Z':
3316b67407dSMilanka Ringwald             log_info("USER:\'%c\'", cmd);
3326b67407dSMilanka Ringwald             printf("Enable Enhanced_Voice Recognition\n");
3336b67407dSMilanka Ringwald             status = hfp_ag_activate_enhanced_voice_recognition(acl_handle);
3346b67407dSMilanka Ringwald             break;
3356b67407dSMilanka Ringwald 
336fffdd288SMatthias Ringwald         case 'o':
337fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
338fffdd288SMatthias Ringwald             printf("Set speaker gain to 0 (minimum)\n");
339ac6f828eSMilanka Ringwald             status = hfp_ag_set_speaker_gain(acl_handle, 0);
340fffdd288SMatthias Ringwald             break;
341fffdd288SMatthias Ringwald         case 'O':
342fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
343fffdd288SMatthias Ringwald             printf("Set speaker gain to 9 (default)\n");
344ac6f828eSMilanka Ringwald             status = hfp_ag_set_speaker_gain(acl_handle, 9);
345fffdd288SMatthias Ringwald             break;
346fffdd288SMatthias Ringwald         case 'p':
347fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
348fffdd288SMatthias Ringwald             printf("Set speaker gain to 12 (higher)\n");
349ac6f828eSMilanka Ringwald             status = hfp_ag_set_speaker_gain(acl_handle, 12);
350fffdd288SMatthias Ringwald             break;
351fffdd288SMatthias Ringwald         case 'P':
352fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
353fffdd288SMatthias Ringwald             printf("Set speaker gain to 15 (maximum)\n");
354ac6f828eSMilanka Ringwald             status = hfp_ag_set_speaker_gain(acl_handle, 15);
355fffdd288SMatthias Ringwald             break;
356fffdd288SMatthias Ringwald         case 'q':
357fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
358fffdd288SMatthias Ringwald             printf("Set microphone gain to 0\n");
359ac6f828eSMilanka Ringwald             status = hfp_ag_set_microphone_gain(acl_handle, 0);
360fffdd288SMatthias Ringwald             break;
361fffdd288SMatthias Ringwald         case 'Q':
362fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
363fffdd288SMatthias Ringwald             printf("Set microphone gain to 9\n");
364ac6f828eSMilanka Ringwald             status = hfp_ag_set_microphone_gain(acl_handle, 9);
365fffdd288SMatthias Ringwald             break;
366fffdd288SMatthias Ringwald         case 's':
367fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
368fffdd288SMatthias Ringwald             printf("Set microphone gain to 12\n");
369ac6f828eSMilanka Ringwald             status = hfp_ag_set_microphone_gain(acl_handle, 12);
370fffdd288SMatthias Ringwald             break;
371fffdd288SMatthias Ringwald         case 'S':
372fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
373fffdd288SMatthias Ringwald             printf("Set microphone gain to 15\n");
374ac6f828eSMilanka Ringwald             status = hfp_ag_set_microphone_gain(acl_handle, 15);
375fffdd288SMatthias Ringwald             break;
376fffdd288SMatthias Ringwald         case 'R':
377fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
378fffdd288SMatthias Ringwald             printf("Enable in-band ring tone\n");
379fffdd288SMatthias Ringwald             hfp_ag_set_use_in_band_ring_tone(1);
380fffdd288SMatthias Ringwald             break;
381fffdd288SMatthias Ringwald         case 't':
382fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
383d97d752dSMilanka Ringwald             printf("Terminate HCI connection. 0x%2x\n", acl_handle);
384d97d752dSMilanka Ringwald             gap_disconnect(acl_handle);
385fffdd288SMatthias Ringwald             break;
386fffdd288SMatthias Ringwald         case 'u':
387fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
388fffdd288SMatthias Ringwald             printf("Join held call\n");
389fffdd288SMatthias Ringwald             hfp_ag_join_held_call();
390fffdd288SMatthias Ringwald             break;
391fffdd288SMatthias Ringwald         case 'v':
3926e87f20cSMatthias Ringwald             printf("Start scanning...\n");
3936e87f20cSMatthias Ringwald             gap_inquiry_start(INQUIRY_INTERVAL);
394fffdd288SMatthias Ringwald             break;
395fffdd288SMatthias Ringwald         case 'w':
396fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
397fffdd288SMatthias Ringwald             printf("AG: Put incoming call on hold (Response and Hold)\n");
398fffdd288SMatthias Ringwald             hfp_ag_hold_incoming_call();
399fffdd288SMatthias Ringwald             break;
400fffdd288SMatthias Ringwald         case 'x':
401fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
402fffdd288SMatthias Ringwald             printf("AG: Accept held incoming call (Response and Hold)\n");
403fffdd288SMatthias Ringwald             hfp_ag_accept_held_incoming_call();
404fffdd288SMatthias Ringwald             break;
405fffdd288SMatthias Ringwald         case 'X':
406fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
407fffdd288SMatthias Ringwald             printf("AG: Reject held incoming call (Response and Hold)\n");
408fffdd288SMatthias Ringwald             hfp_ag_reject_held_incoming_call();
409fffdd288SMatthias Ringwald             break;
410fffdd288SMatthias Ringwald         default:
411fffdd288SMatthias Ringwald             show_usage();
412fffdd288SMatthias Ringwald             break;
413fffdd288SMatthias Ringwald     }
414ac6f828eSMilanka Ringwald 
415ac6f828eSMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
416ac6f828eSMilanka Ringwald         printf("Could not perform command, status 0x%02x\n", status);
417ac6f828eSMilanka Ringwald     }
418fffdd288SMatthias Ringwald }
419fffdd288SMatthias Ringwald #endif
420fffdd288SMatthias Ringwald 
42113839019SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
4229ec2630cSMatthias Ringwald     UNUSED(channel);
4236e87f20cSMatthias Ringwald     bd_addr_t addr;
42405d93378SMilanka Ringwald     uint8_t status;
4253cef842bSMatthias Ringwald     switch (packet_type){
4263cef842bSMatthias Ringwald         case HCI_EVENT_PACKET:
42705d93378SMilanka Ringwald             switch(hci_event_packet_get_type(event)){
42805d93378SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN
42905d93378SMilanka Ringwald                 case BTSTACK_EVENT_STATE:
43005d93378SMilanka Ringwald                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
43105d93378SMilanka Ringwald                     printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr));
43205d93378SMilanka Ringwald                     hfp_ag_establish_service_level_connection(device_addr);
43305d93378SMilanka Ringwald                     break;
43405d93378SMilanka Ringwald #endif
4356e87f20cSMatthias Ringwald                 case GAP_EVENT_INQUIRY_RESULT:
4366e87f20cSMatthias Ringwald                     gap_event_inquiry_result_get_bd_addr(event, addr);
4376e87f20cSMatthias Ringwald                     // print info
4386e87f20cSMatthias Ringwald                     printf("Device found: %s ",  bd_addr_to_str(addr));
4396e87f20cSMatthias Ringwald                     printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
440eaac31e8SMatthias Ringwald                     if (gap_event_inquiry_result_get_rssi_available(event)){
4416e87f20cSMatthias Ringwald                         printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
4426e87f20cSMatthias Ringwald                     }
4436e87f20cSMatthias Ringwald                     if (gap_event_inquiry_result_get_name_available(event)){
4446e87f20cSMatthias Ringwald                         char name_buffer[240];
4456e87f20cSMatthias Ringwald                         int name_len = gap_event_inquiry_result_get_name_len(event);
4466e87f20cSMatthias Ringwald                         memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
4476e87f20cSMatthias Ringwald                         name_buffer[name_len] = 0;
4486e87f20cSMatthias Ringwald                         printf(", name '%s'", name_buffer);
4496e87f20cSMatthias Ringwald                     }
4506e87f20cSMatthias Ringwald                     printf("\n");
4516e87f20cSMatthias Ringwald                     break;
4526e87f20cSMatthias Ringwald                 case GAP_EVENT_INQUIRY_COMPLETE:
4536e87f20cSMatthias Ringwald                     printf("Inquiry scan complete.\n");
454fffdd288SMatthias Ringwald                     break;
45513839019SMatthias Ringwald                 case HCI_EVENT_SCO_CAN_SEND_NOW:
456185c8cd4SMatthias Ringwald                     sco_demo_send(sco_handle);
45713839019SMatthias Ringwald                     break;
45892bac129SMatthias Ringwald                 case HCI_EVENT_COMMAND_COMPLETE:
45992bac129SMatthias Ringwald                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
460f9f7adeeSMatthias Ringwald                         dump_supported_codecs();
46192bac129SMatthias Ringwald                     }
46292bac129SMatthias Ringwald                     break;
463fffdd288SMatthias Ringwald                 default:
464fffdd288SMatthias Ringwald                     break;
465fffdd288SMatthias Ringwald             }
466fffdd288SMatthias Ringwald 
46705d93378SMilanka Ringwald             if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return;
468fffdd288SMatthias Ringwald 
46905d93378SMilanka Ringwald             switch (hci_event_hfp_meta_get_subevent_code(event)) {
470fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
47105d93378SMilanka Ringwald                     status = hfp_subevent_service_level_connection_established_get_status(event);
4726058cb0dSMatthias Ringwald                     if (status != ERROR_CODE_SUCCESS){
473ac6f828eSMilanka Ringwald                         printf("Connection failed, status 0x%02x\n", status);
47405d93378SMilanka Ringwald                         break;
47505d93378SMilanka Ringwald                     }
47611d54508SMatthias Ringwald                     acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
47766c297f9SMilanka Ringwald                     hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
47805d93378SMilanka Ringwald                     printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr));
479b3f76298SMilanka Ringwald                     dump_supported_codecs();
48005d93378SMilanka Ringwald #ifndef HAVE_BTSTACK_STDIN
48105d93378SMilanka Ringwald                     log_info("Establish Audio connection %s", bd_addr_to_str(device_addr));
48205d93378SMilanka Ringwald                     printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
48305d93378SMilanka Ringwald                     hfp_ag_establish_audio_connection(acl_handle);
48405d93378SMilanka Ringwald #endif
485fffdd288SMatthias Ringwald                     break;
486fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
487fffdd288SMatthias Ringwald                     printf("Service level connection released.\n");
48880b0ebb6SMatthias Ringwald                     acl_handle = HCI_CON_HANDLE_INVALID;
489fffdd288SMatthias Ringwald                     break;
490fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
4916058cb0dSMatthias Ringwald                     if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){
4923cef842bSMatthias Ringwald                         printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
4933cef842bSMatthias Ringwald                     } else {
49411d54508SMatthias Ringwald                         sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
4953cef842bSMatthias Ringwald                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
496d0c4aea6SMilanka Ringwald                         negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
497f9f7adeeSMatthias Ringwald                         switch (negotiated_codec){
498f9f7adeeSMatthias Ringwald                             case 0x01:
499f9f7adeeSMatthias Ringwald                                 printf("Using CVSD codec.\n");
500f9f7adeeSMatthias Ringwald                                 break;
501f9f7adeeSMatthias Ringwald                             case 0x02:
502f9f7adeeSMatthias Ringwald                                 printf("Using mSBC codec.\n");
503f9f7adeeSMatthias Ringwald                                 break;
504f9f7adeeSMatthias Ringwald                             default:
505f9f7adeeSMatthias Ringwald                                 printf("Using unknown codec 0x%02x.\n", negotiated_codec);
506f9f7adeeSMatthias Ringwald                                 break;
507f9f7adeeSMatthias Ringwald                         }
508d0c4aea6SMilanka Ringwald                         sco_demo_set_codec(negotiated_codec);
5093cef842bSMatthias Ringwald                         hci_request_sco_can_send_now_event();
5103cef842bSMatthias Ringwald                     }
511fffdd288SMatthias Ringwald                     break;
512fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
513b038503dSMatthias Ringwald                     printf("Audio connection released\n");
51480b0ebb6SMatthias Ringwald                     sco_handle = HCI_CON_HANDLE_INVALID;
515b038503dSMatthias Ringwald                     sco_demo_close();
516fffdd288SMatthias Ringwald                     break;
517fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_START_RINGINIG:
518b038503dSMatthias Ringwald                     printf("Start Ringing\n");
519fffdd288SMatthias Ringwald                     break;
520fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_STOP_RINGINIG:
521b038503dSMatthias Ringwald                     printf("Stop Ringing\n");
522fffdd288SMatthias Ringwald                     break;
523fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
524b038503dSMatthias Ringwald                     printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
525fffdd288SMatthias Ringwald                     // validate number
5260696953aSMilanka Ringwald                     if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
5270696953aSMilanka Ringwald                       || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
5280696953aSMilanka Ringwald                       || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
529fffdd288SMatthias Ringwald                         printf("Dial string valid: accept call\n");
530fffdd288SMatthias Ringwald                         hfp_ag_outgoing_call_accepted();
531fffdd288SMatthias Ringwald                     } else {
532fffdd288SMatthias Ringwald                         printf("Dial string invalid: reject call\n");
533fffdd288SMatthias Ringwald                         hfp_ag_outgoing_call_rejected();
534fffdd288SMatthias Ringwald                     }
535fffdd288SMatthias Ringwald                     break;
536fffdd288SMatthias Ringwald 
537fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
538b038503dSMatthias Ringwald                     printf("Attach number to voice tag. Sending '1234567\n");
539d97d752dSMilanka Ringwald                     hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
540fffdd288SMatthias Ringwald                     break;
541fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
542b038503dSMatthias Ringwald                     printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
543d97d752dSMilanka Ringwald                     hfp_ag_send_dtmf_code_done(acl_handle);
544fffdd288SMatthias Ringwald                     break;
5450696953aSMilanka Ringwald                 case HFP_SUBEVENT_CALL_ANSWERED:
546fffdd288SMatthias Ringwald                     printf("Call answered by HF\n");
547fffdd288SMatthias Ringwald                     break;
5486b67407dSMilanka Ringwald 
549ac6f828eSMilanka Ringwald                 case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS:
5507253db3cSMilanka Ringwald                     status = hfp_subevent_voice_recognition_status_get_status(event);
5517253db3cSMilanka Ringwald                     if (status != ERROR_CODE_SUCCESS){
552*de9e0ea7SMilanka Ringwald                         printf("Voice Recognition command failed\n");
5537253db3cSMilanka Ringwald                         break;
5547253db3cSMilanka Ringwald                     }
5557253db3cSMilanka Ringwald                     switch(hfp_subevent_voice_recognition_status_get_state(event)){
5566b67407dSMilanka Ringwald                         case 0:
557*de9e0ea7SMilanka Ringwald                             printf("\nVoice recognition status DEACTIVATED\n\n");
5587253db3cSMilanka Ringwald                             break;
5596b67407dSMilanka Ringwald                         case 1:
560*de9e0ea7SMilanka Ringwald                             printf("\nVoice recognition status ACTIVATED\n\n");
5617253db3cSMilanka Ringwald                             break;
5627253db3cSMilanka Ringwald                         default:
5637253db3cSMilanka Ringwald                             btstack_assert(false);
5647253db3cSMilanka Ringwald                             break;
565ac6f828eSMilanka Ringwald                     }
566ac6f828eSMilanka Ringwald                     break;
5676b67407dSMilanka Ringwald 
568*de9e0ea7SMilanka Ringwald                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO:
569*de9e0ea7SMilanka Ringwald                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
5706b67407dSMilanka Ringwald                     if (status != ERROR_CODE_SUCCESS){
571*de9e0ea7SMilanka Ringwald                         printf("Enhanced Voice recognition READY FOR AUDIO cmd failed\n");
5726b67407dSMilanka Ringwald                         break;
5736b67407dSMilanka Ringwald                     }
574*de9e0ea7SMilanka Ringwald                     printf("\nEnhanced Voice recognition status READY FOR AUDIO\n\n");
5756b67407dSMilanka Ringwald                     break;
576fffdd288SMatthias Ringwald                 default:
577fffdd288SMatthias Ringwald                     break;
578fffdd288SMatthias Ringwald             }
57996205903SMatthias Ringwald             break;
580185c8cd4SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
58180b0ebb6SMatthias Ringwald             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
582185c8cd4SMatthias Ringwald             sco_demo_receive(event, event_size);
583185c8cd4SMatthias Ringwald             break;
58413839019SMatthias Ringwald         default:
58513839019SMatthias Ringwald             break;
58613839019SMatthias Ringwald     }
587fffdd288SMatthias Ringwald }
588fffdd288SMatthias Ringwald 
589fffdd288SMatthias Ringwald static hfp_phone_number_t subscriber_number = {
590fffdd288SMatthias Ringwald     129, "225577"
591fffdd288SMatthias Ringwald };
592fffdd288SMatthias Ringwald 
593fffdd288SMatthias Ringwald /* @section Main Application Setup
594fffdd288SMatthias Ringwald  *
595fffdd288SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
596fffdd288SMatthias Ringwald  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
597fffdd288SMatthias Ringwald  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
598fffdd288SMatthias Ringwald  * The stdin_process callback allows for sending commands to the HFP HF.
599fffdd288SMatthias Ringwald  * At the end the Bluetooth stack is started.
600fffdd288SMatthias Ringwald  */
601fffdd288SMatthias Ringwald 
602fffdd288SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
603fffdd288SMatthias Ringwald 
604fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
605fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
6069ec2630cSMatthias Ringwald     (void)argc;
6079ec2630cSMatthias Ringwald     (void)argv;
608fffdd288SMatthias Ringwald 
609185c8cd4SMatthias Ringwald     sco_demo_init();
610185c8cd4SMatthias Ringwald 
6110c2b8870SMatthias Ringwald     gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
612e64e0086SMatthias Ringwald     gap_discoverable_control(1);
613e64e0086SMatthias Ringwald 
614e64e0086SMatthias Ringwald     // L2CAP
615e64e0086SMatthias Ringwald     l2cap_init();
616e64e0086SMatthias Ringwald 
6174f84bf36SMatthias Ringwald     uint16_t supported_features                   =
6184f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ESCO_S4)                     |
6194f84bf36SMatthias Ringwald         (1<<HFP_AGSF_HF_INDICATORS)               |
6204f84bf36SMatthias Ringwald         (1<<HFP_AGSF_CODEC_NEGOTIATION)           |
6214f84bf36SMatthias Ringwald         (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
6224f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ENHANCED_CALL_CONTROL)       |
6234f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ENHANCED_CALL_STATUS)        |
6244f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL)    |
6254f84bf36SMatthias Ringwald         (1<<HFP_AGSF_IN_BAND_RING_TONE)           |
6264f84bf36SMatthias Ringwald         (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
6276b67407dSMilanka Ringwald         (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
6284f84bf36SMatthias Ringwald         (1<<HFP_AGSF_THREE_WAY_CALLING);
6294f84bf36SMatthias Ringwald     int wide_band_speech = 1;
6304f84bf36SMatthias Ringwald 
631e64e0086SMatthias Ringwald     // HFP
632e64e0086SMatthias Ringwald     rfcomm_init();
633fffdd288SMatthias Ringwald     hfp_ag_init(rfcomm_channel_nr);
6344f84bf36SMatthias Ringwald     hfp_ag_init_supported_features(supported_features);
635fffdd288SMatthias Ringwald     hfp_ag_init_codecs(sizeof(codecs), codecs);
636fffdd288SMatthias Ringwald     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
637fffdd288SMatthias Ringwald     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
638fffdd288SMatthias Ringwald     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
639fffdd288SMatthias Ringwald     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
640fffdd288SMatthias Ringwald 
641e64e0086SMatthias Ringwald     // SDP Server
642e64e0086SMatthias Ringwald     sdp_init();
643fffdd288SMatthias Ringwald     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
6444f84bf36SMatthias Ringwald     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
645fffdd288SMatthias Ringwald     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
646fffdd288SMatthias Ringwald     sdp_register_service(hfp_service_buffer);
647fffdd288SMatthias Ringwald 
648a4fe6467SMatthias Ringwald     // register for HCI events and SCO packets
649a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
650a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
651a4fe6467SMatthias Ringwald     hci_register_sco_packet_handler(&packet_handler);
652a4fe6467SMatthias Ringwald 
653a4fe6467SMatthias Ringwald     // register for HFP events
654a4fe6467SMatthias Ringwald     hfp_ag_register_packet_handler(&packet_handler);
655a4fe6467SMatthias Ringwald 
656ac6f828eSMilanka Ringwald     // parse human readable Bluetooth address
6570e62b7a5SMatthias Ringwald     sscanf_bd_addr(device_addr_string, device_addr);
6580e62b7a5SMatthias Ringwald 
6597ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN
660fffdd288SMatthias Ringwald     btstack_stdin_setup(stdin_process);
661fffdd288SMatthias Ringwald #endif
662fffdd288SMatthias Ringwald     // turn on!
663fffdd288SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
664fffdd288SMatthias Ringwald     return 0;
665fffdd288SMatthias Ringwald }
666fffdd288SMatthias Ringwald /* LISTING_END */
6672b6b8c15SMilanka Ringwald /* EXAMPLE_END */
668