xref: /btstack/example/hfp_ag_demo.c (revision 6e87f20c5be4acf294ae811171317a92f9dc136c)
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 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_ag_demo.c"
39fffdd288SMatthias Ringwald 
40fffdd288SMatthias Ringwald /*
41fffdd288SMatthias Ringwald  * hfp_ag_demo.c
42fffdd288SMatthias Ringwald  */
43fffdd288SMatthias Ringwald 
44fffdd288SMatthias Ringwald // *****************************************************************************
45fffdd288SMatthias Ringwald /* EXAMPLE_START(hfp_ag_demo): HFP Audio Gateway (AG) Demo
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,
49d0755cd6SMatthias Ringwald  * if HAVE_POSIX_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 #include <unistd.h>
59fffdd288SMatthias Ringwald 
60fffdd288SMatthias Ringwald #include "btstack.h"
61185c8cd4SMatthias Ringwald #include "sco_demo_util.h"
62d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
63fffdd288SMatthias Ringwald #include "stdin_support.h"
644af4141bSMatthias Ringwald #endif
65fffdd288SMatthias Ringwald 
66fffdd288SMatthias Ringwald uint8_t hfp_service_buffer[150];
67fffdd288SMatthias Ringwald const uint8_t    rfcomm_channel_nr = 1;
68fffdd288SMatthias Ringwald const char hfp_ag_service_name[] = "BTstack HFP AG Test";
69fffdd288SMatthias Ringwald 
700e62b7a5SMatthias Ringwald static bd_addr_t device_addr;
710e62b7a5SMatthias Ringwald static const char * device_addr_string = "00:15:83:5F:9D:46";
72fffdd288SMatthias Ringwald 
731bbecc2bSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
74fa5e2954SMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
751bbecc2bSMatthias Ringwald #else
761bbecc2bSMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD};
771bbecc2bSMatthias Ringwald #endif
781bbecc2bSMatthias Ringwald 
79220eb563SMilanka Ringwald static uint8_t negotiated_codec = HFP_CODEC_CVSD;
80220eb563SMilanka Ringwald 
81d97d752dSMilanka Ringwald static hci_con_handle_t acl_handle = -1;
823cef842bSMatthias Ringwald static hci_con_handle_t sco_handle;
83fffdd288SMatthias Ringwald static int memory_1_enabled = 1;
84186bfbecSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
85fffdd288SMatthias Ringwald 
86fffdd288SMatthias Ringwald static int ag_indicators_nr = 7;
87fffdd288SMatthias Ringwald static hfp_ag_indicator_t ag_indicators[] = {
88fffdd288SMatthias Ringwald     // index, name, min range, max range, status, mandatory, enabled, status changed
89fffdd288SMatthias Ringwald     {1, "service",   0, 1, 1, 0, 0, 0},
90fffdd288SMatthias Ringwald     {2, "call",      0, 1, 0, 1, 1, 0},
91fffdd288SMatthias Ringwald     {3, "callsetup", 0, 3, 0, 1, 1, 0},
92fffdd288SMatthias Ringwald     {4, "battchg",   0, 5, 3, 0, 0, 0},
93fffdd288SMatthias Ringwald     {5, "signal",    0, 5, 5, 0, 1, 0},
94fffdd288SMatthias Ringwald     {6, "roam",      0, 1, 0, 0, 1, 0},
95fffdd288SMatthias Ringwald     {7, "callheld",  0, 2, 0, 1, 1, 0}
96fffdd288SMatthias Ringwald };
97fffdd288SMatthias Ringwald 
98fffdd288SMatthias Ringwald static int call_hold_services_nr = 5;
99fffdd288SMatthias Ringwald static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"};
100fffdd288SMatthias Ringwald 
101fffdd288SMatthias Ringwald static int hf_indicators_nr = 2;
102fffdd288SMatthias Ringwald static hfp_generic_status_indicator_t hf_indicators[] = {
103fffdd288SMatthias Ringwald     {1, 1},
104fffdd288SMatthias Ringwald     {2, 1},
105fffdd288SMatthias Ringwald };
106fffdd288SMatthias Ringwald 
107fffdd288SMatthias Ringwald #define INQUIRY_INTERVAL 5
108fffdd288SMatthias Ringwald 
109fffdd288SMatthias Ringwald enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
110fffdd288SMatthias Ringwald enum STATE state = INIT;
111fffdd288SMatthias Ringwald 
1123b9522a6SMatthias Ringwald static void dump_supported_codecs(void){
113b3f76298SMilanka Ringwald     int i;
114f9f7adeeSMatthias Ringwald     int mSBC_skipped = 0;
115b3f76298SMilanka Ringwald     printf("Supported codecs: ");
116b3f76298SMilanka Ringwald     for (i = 0; i < sizeof(codecs); i++){
117b3f76298SMilanka Ringwald         switch(codecs[i]){
118b3f76298SMilanka Ringwald             case HFP_CODEC_CVSD:
119b3f76298SMilanka Ringwald                 printf("CVSD");
120b3f76298SMilanka Ringwald                 break;
121b3f76298SMilanka Ringwald             case HFP_CODEC_MSBC:
122f9f7adeeSMatthias Ringwald                 if (hci_extended_sco_link_supported()){
1232308e108SMilanka Ringwald                     printf(", mSBC");
124f9f7adeeSMatthias Ringwald                 } else {
125f9f7adeeSMatthias Ringwald                     mSBC_skipped = 1;
126f9f7adeeSMatthias Ringwald                 }
127b3f76298SMilanka Ringwald                 break;
128b3f76298SMilanka Ringwald         }
129b3f76298SMilanka Ringwald     }
130b3f76298SMilanka Ringwald     printf("\n");
131f9f7adeeSMatthias Ringwald     if (mSBC_skipped){
132f9f7adeeSMatthias Ringwald         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
133f9f7adeeSMatthias Ringwald     }
134b3f76298SMilanka Ringwald }
135fffdd288SMatthias Ringwald 
136d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
137fffdd288SMatthias Ringwald // Testig User Interface
138fffdd288SMatthias Ringwald static void show_usage(void){
139e64e0086SMatthias Ringwald     bd_addr_t iut_address;
140e64e0086SMatthias Ringwald     gap_local_bd_addr(iut_address);
141e64e0086SMatthias Ringwald 
142e64e0086SMatthias Ringwald     printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
1438a7d93dcSMatthias Ringwald     printf("\n");
144fffdd288SMatthias Ringwald 
145bdb44bd9SMatthias Ringwald     printf("a - establish HFP connection to PTS module %s\n", bd_addr_to_str(device_addr));
146fffdd288SMatthias Ringwald     // printf("A - release HFP connection to PTS module\n");
147fffdd288SMatthias Ringwald 
1488a7d93dcSMatthias Ringwald     printf("b - establish AUDIO connection          | B - release AUDIO connection\n");
1498a7d93dcSMatthias Ringwald     printf("c - simulate incoming call from 1234567 | C - simulate call from 1234567 dropped\n");
150fffdd288SMatthias Ringwald     printf("d - report AG failure\n");
1518a7d93dcSMatthias Ringwald     printf("e - answer call on AG                   | E - reject call on AG\n");
1528a7d93dcSMatthias Ringwald     printf("r - disable in-band ring tone           | R - enable in-band ring tone\n");
1538a7d93dcSMatthias Ringwald     printf("f - Disable cellular network            | F - Enable cellular network\n");
1548a7d93dcSMatthias Ringwald     printf("g - Set signal strength to 0            | G - Set signal strength to 5\n");
1558a7d93dcSMatthias Ringwald     printf("h - Disable roaming                     | H - Enable roaming\n");
1568a7d93dcSMatthias Ringwald     printf("i - Set battery level to 3              | I - Set battery level to 5\n");
157fffdd288SMatthias Ringwald     printf("j - Answering call on remote side\n");
1588a7d93dcSMatthias Ringwald     printf("k - Clear memory #1                     | K - Set memory #1\n");
1598a7d93dcSMatthias Ringwald     printf("l - Clear last number                   | L - Set last number\n");
160fffdd288SMatthias Ringwald     printf("m - simulate incoming call from 7654321\n");
161fffdd288SMatthias Ringwald     // printf("M - simulate call from 7654321 dropped\n");
1628a7d93dcSMatthias Ringwald     printf("n - Disable Voice Regocnition           | N - Enable Voice Recognition\n");
1638a7d93dcSMatthias Ringwald     printf("o - Set speaker volume to 0  (minimum)  | O - Set speaker volume to 9  (default)\n");
1648a7d93dcSMatthias Ringwald     printf("p - Set speaker volume to 12 (higher)   | P - Set speaker volume to 15 (maximum)\n");
1658a7d93dcSMatthias Ringwald     printf("q - Set microphone gain to 0  (minimum) | Q - Set microphone gain to 9  (default)\n");
1668a7d93dcSMatthias Ringwald     printf("s - Set microphone gain to 12 (higher)  | S - Set microphone gain to 15 (maximum)\n");
167fffdd288SMatthias Ringwald     printf("t - terminate connection\n");
168fffdd288SMatthias Ringwald     printf("u - join held call\n");
169fffdd288SMatthias Ringwald     printf("v - discover nearby HF units\n");
170fffdd288SMatthias Ringwald     printf("w - put incoming call on hold (Response and Hold)\n");
171fffdd288SMatthias Ringwald     printf("x - accept held incoming call (Response and Hold)\n");
172fffdd288SMatthias Ringwald     printf("X - reject held incoming call (Response and Hold)\n");
173fffdd288SMatthias Ringwald 
174fffdd288SMatthias Ringwald     printf("---\n");
175fffdd288SMatthias Ringwald     printf("Ctrl-c - exit\n");
176fffdd288SMatthias Ringwald     printf("---\n");
177fffdd288SMatthias Ringwald }
178fffdd288SMatthias Ringwald 
1794af4141bSMatthias Ringwald static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
1809ec2630cSMatthias Ringwald     UNUSED(ds);
1819ec2630cSMatthias Ringwald     UNUSED(callback_type);
1829ec2630cSMatthias Ringwald 
183*6e87f20cSMatthias Ringwald     char cmd = btstack_stdin_read();
184fffdd288SMatthias Ringwald     switch (cmd){
185fffdd288SMatthias Ringwald         case 'a':
186fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
187fffdd288SMatthias Ringwald             printf("Establish HFP service level connection to PTS module %s...\n", bd_addr_to_str(device_addr));
188fffdd288SMatthias Ringwald             hfp_ag_establish_service_level_connection(device_addr);
189fffdd288SMatthias Ringwald             break;
190fffdd288SMatthias Ringwald         case 'A':
191fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
192fffdd288SMatthias Ringwald             printf("Release HFP service level connection.\n");
193d97d752dSMilanka Ringwald             hfp_ag_release_service_level_connection(acl_handle);
194fffdd288SMatthias Ringwald             break;
195fffdd288SMatthias Ringwald         case 'Z':
196fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
197fffdd288SMatthias Ringwald             printf("Release HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
198d97d752dSMilanka Ringwald             hfp_ag_release_service_level_connection(acl_handle);
199fffdd288SMatthias Ringwald             break;
200fffdd288SMatthias Ringwald         case 'b':
201fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
202fffdd288SMatthias Ringwald             printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
203d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(acl_handle);
204fffdd288SMatthias Ringwald             break;
205fffdd288SMatthias Ringwald         case 'B':
206fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
207fffdd288SMatthias Ringwald             printf("Release Audio connection.\n");
208d97d752dSMilanka Ringwald             hfp_ag_release_audio_connection(acl_handle);
209fffdd288SMatthias Ringwald             break;
210fffdd288SMatthias Ringwald         case 'c':
211fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
212fffdd288SMatthias Ringwald             printf("Simulate incoming call from 1234567\n");
213fffdd288SMatthias Ringwald             hfp_ag_set_clip(129, "1234567");
214fffdd288SMatthias Ringwald             hfp_ag_incoming_call();
215fffdd288SMatthias Ringwald             break;
216fffdd288SMatthias Ringwald         case 'm':
217fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
218fffdd288SMatthias Ringwald             printf("Simulate incoming call from 7654321\n");
219fffdd288SMatthias Ringwald             hfp_ag_set_clip(129, "7654321");
220fffdd288SMatthias Ringwald             hfp_ag_incoming_call();
221fffdd288SMatthias Ringwald             break;
222fffdd288SMatthias Ringwald         case 'C':
223fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
224fffdd288SMatthias Ringwald             printf("Simulate terminate call\n");
225fffdd288SMatthias Ringwald             hfp_ag_call_dropped();
226fffdd288SMatthias Ringwald             break;
227fffdd288SMatthias Ringwald         case 'd':
228fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
229fffdd288SMatthias Ringwald             printf("Report AG failure\n");
230d97d752dSMilanka Ringwald             hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
231fffdd288SMatthias Ringwald             break;
232fffdd288SMatthias Ringwald         case 'e':
233fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
234fffdd288SMatthias Ringwald             printf("Answer call on AG\n");
235fffdd288SMatthias Ringwald             hfp_ag_answer_incoming_call();
236fffdd288SMatthias Ringwald             break;
237fffdd288SMatthias Ringwald         case 'E':
238fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
239fffdd288SMatthias Ringwald             printf("Reject call on AG\n");
240fffdd288SMatthias Ringwald             hfp_ag_terminate_call();
241fffdd288SMatthias Ringwald             break;
242fffdd288SMatthias Ringwald         case 'f':
243fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
244fffdd288SMatthias Ringwald             printf("Disable cellular network\n");
245fffdd288SMatthias Ringwald             hfp_ag_set_registration_status(0);
246fffdd288SMatthias Ringwald             break;
247fffdd288SMatthias Ringwald         case 'F':
248fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
249fffdd288SMatthias Ringwald             printf("Enable cellular network\n");
250fffdd288SMatthias Ringwald             hfp_ag_set_registration_status(1);
251fffdd288SMatthias Ringwald             break;
252fffdd288SMatthias Ringwald         case 'g':
253fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
254fffdd288SMatthias Ringwald             printf("Set signal strength to 0\n");
255fffdd288SMatthias Ringwald             hfp_ag_set_signal_strength(0);
256fffdd288SMatthias Ringwald             break;
257fffdd288SMatthias Ringwald         case 'G':
258fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
259fffdd288SMatthias Ringwald             printf("Set signal strength to 5\n");
260fffdd288SMatthias Ringwald             hfp_ag_set_signal_strength(5);
261fffdd288SMatthias Ringwald             break;
262fffdd288SMatthias Ringwald         case 'h':
263fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
264fffdd288SMatthias Ringwald             printf("Disable roaming\n");
265fffdd288SMatthias Ringwald             hfp_ag_set_roaming_status(0);
266fffdd288SMatthias Ringwald             break;
267fffdd288SMatthias Ringwald         case 'H':
268fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
269fffdd288SMatthias Ringwald             printf("Enable roaming\n");
270fffdd288SMatthias Ringwald             hfp_ag_set_roaming_status(1);
271fffdd288SMatthias Ringwald             break;
272fffdd288SMatthias Ringwald         case 'i':
273fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
274fffdd288SMatthias Ringwald             printf("Set battery level to 3\n");
275fffdd288SMatthias Ringwald             hfp_ag_set_battery_level(3);
276fffdd288SMatthias Ringwald             break;
277fffdd288SMatthias Ringwald         case 'I':
278fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
279fffdd288SMatthias Ringwald             printf("Set battery level to 5\n");
280fffdd288SMatthias Ringwald             hfp_ag_set_battery_level(5);
281fffdd288SMatthias Ringwald             break;
282fffdd288SMatthias Ringwald         case 'j':
283fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
284fffdd288SMatthias Ringwald             printf("Answering call on remote side\n");
285fffdd288SMatthias Ringwald             hfp_ag_outgoing_call_established();
286fffdd288SMatthias Ringwald             break;
287fffdd288SMatthias Ringwald         case 'r':
288fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
289fffdd288SMatthias Ringwald             printf("Disable in-band ring tone\n");
290fffdd288SMatthias Ringwald             hfp_ag_set_use_in_band_ring_tone(0);
291fffdd288SMatthias Ringwald             break;
292fffdd288SMatthias Ringwald         case 'k':
293fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
294fffdd288SMatthias Ringwald             printf("Memory 1 cleared\n");
295fffdd288SMatthias Ringwald             memory_1_enabled = 0;
296fffdd288SMatthias Ringwald             break;
297fffdd288SMatthias Ringwald         case 'K':
298fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
299fffdd288SMatthias Ringwald             printf("Memory 1 set\n");
300fffdd288SMatthias Ringwald             memory_1_enabled = 1;
301fffdd288SMatthias Ringwald             break;
302fffdd288SMatthias Ringwald         case 'l':
303fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
304fffdd288SMatthias Ringwald             printf("Last dialed number cleared\n");
305fffdd288SMatthias Ringwald             hfp_ag_clear_last_dialed_number();
306fffdd288SMatthias Ringwald             break;
307fffdd288SMatthias Ringwald         case 'L':
308fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
309fffdd288SMatthias Ringwald             printf("Outgoing call connected, ringing\n");
310fffdd288SMatthias Ringwald             hfp_ag_outgoing_call_ringing();
311fffdd288SMatthias Ringwald             break;
312fffdd288SMatthias Ringwald         case 'n':
313fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
314fffdd288SMatthias Ringwald             printf("Disable Voice Recognition\n");
315d97d752dSMilanka Ringwald             hfp_ag_activate_voice_recognition(acl_handle, 0);
316fffdd288SMatthias Ringwald             break;
317fffdd288SMatthias Ringwald         case 'N':
318fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
319fffdd288SMatthias Ringwald             printf("Enable Voice Recognition\n");
320d97d752dSMilanka Ringwald             hfp_ag_activate_voice_recognition(acl_handle, 1);
321fffdd288SMatthias Ringwald             break;
322fffdd288SMatthias Ringwald         case 'o':
323fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
324fffdd288SMatthias Ringwald             printf("Set speaker gain to 0 (minimum)\n");
325d97d752dSMilanka Ringwald             hfp_ag_set_speaker_gain(acl_handle, 0);
326fffdd288SMatthias Ringwald             break;
327fffdd288SMatthias Ringwald         case 'O':
328fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
329fffdd288SMatthias Ringwald             printf("Set speaker gain to 9 (default)\n");
330d97d752dSMilanka Ringwald             hfp_ag_set_speaker_gain(acl_handle, 9);
331fffdd288SMatthias Ringwald             break;
332fffdd288SMatthias Ringwald         case 'p':
333fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
334fffdd288SMatthias Ringwald             printf("Set speaker gain to 12 (higher)\n");
335d97d752dSMilanka Ringwald             hfp_ag_set_speaker_gain(acl_handle, 12);
336fffdd288SMatthias Ringwald             break;
337fffdd288SMatthias Ringwald         case 'P':
338fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
339fffdd288SMatthias Ringwald             printf("Set speaker gain to 15 (maximum)\n");
340d97d752dSMilanka Ringwald             hfp_ag_set_speaker_gain(acl_handle, 15);
341fffdd288SMatthias Ringwald             break;
342fffdd288SMatthias Ringwald         case 'q':
343fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
344fffdd288SMatthias Ringwald             printf("Set microphone gain to 0\n");
345d97d752dSMilanka Ringwald             hfp_ag_set_microphone_gain(acl_handle, 0);
346fffdd288SMatthias Ringwald             break;
347fffdd288SMatthias Ringwald         case 'Q':
348fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
349fffdd288SMatthias Ringwald             printf("Set microphone gain to 9\n");
350d97d752dSMilanka Ringwald             hfp_ag_set_microphone_gain(acl_handle, 9);
351fffdd288SMatthias Ringwald             break;
352fffdd288SMatthias Ringwald         case 's':
353fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
354fffdd288SMatthias Ringwald             printf("Set microphone gain to 12\n");
355d97d752dSMilanka Ringwald             hfp_ag_set_microphone_gain(acl_handle, 12);
356fffdd288SMatthias Ringwald             break;
357fffdd288SMatthias Ringwald         case 'S':
358fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
359fffdd288SMatthias Ringwald             printf("Set microphone gain to 15\n");
360d97d752dSMilanka Ringwald             hfp_ag_set_microphone_gain(acl_handle, 15);
361fffdd288SMatthias Ringwald             break;
362fffdd288SMatthias Ringwald         case 'R':
363fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
364fffdd288SMatthias Ringwald             printf("Enable in-band ring tone\n");
365fffdd288SMatthias Ringwald             hfp_ag_set_use_in_band_ring_tone(1);
366fffdd288SMatthias Ringwald             break;
367fffdd288SMatthias Ringwald         case 't':
368fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
369d97d752dSMilanka Ringwald             printf("Terminate HCI connection. 0x%2x\n", acl_handle);
370d97d752dSMilanka Ringwald             gap_disconnect(acl_handle);
371fffdd288SMatthias Ringwald             break;
372fffdd288SMatthias Ringwald         case 'u':
373fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
374fffdd288SMatthias Ringwald             printf("Join held call\n");
375fffdd288SMatthias Ringwald             hfp_ag_join_held_call();
376fffdd288SMatthias Ringwald             break;
377fffdd288SMatthias Ringwald         case 'v':
378*6e87f20cSMatthias Ringwald             printf("Start scanning...\n");
379*6e87f20cSMatthias Ringwald             gap_inquiry_start(INQUIRY_INTERVAL);
380fffdd288SMatthias Ringwald             break;
381fffdd288SMatthias Ringwald         case 'w':
382fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
383fffdd288SMatthias Ringwald             printf("AG: Put incoming call on hold (Response and Hold)\n");
384fffdd288SMatthias Ringwald             hfp_ag_hold_incoming_call();
385fffdd288SMatthias Ringwald             break;
386fffdd288SMatthias Ringwald         case 'x':
387fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
388fffdd288SMatthias Ringwald             printf("AG: Accept held incoming call (Response and Hold)\n");
389fffdd288SMatthias Ringwald             hfp_ag_accept_held_incoming_call();
390fffdd288SMatthias Ringwald             break;
391fffdd288SMatthias Ringwald         case 'X':
392fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
393fffdd288SMatthias Ringwald             printf("AG: Reject held incoming call (Response and Hold)\n");
394fffdd288SMatthias Ringwald             hfp_ag_reject_held_incoming_call();
395fffdd288SMatthias Ringwald             break;
396fffdd288SMatthias Ringwald         default:
397fffdd288SMatthias Ringwald             show_usage();
398fffdd288SMatthias Ringwald             break;
399fffdd288SMatthias Ringwald     }
400fffdd288SMatthias Ringwald }
401fffdd288SMatthias Ringwald #endif
402fffdd288SMatthias Ringwald 
40313839019SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
4049ec2630cSMatthias Ringwald     UNUSED(channel);
405*6e87f20cSMatthias Ringwald     bd_addr_t addr;
4063cef842bSMatthias Ringwald     switch (packet_type){
4073cef842bSMatthias Ringwald         case HCI_EVENT_PACKET:
408fffdd288SMatthias Ringwald             switch (event[0]){
409*6e87f20cSMatthias Ringwald                 case GAP_EVENT_INQUIRY_RESULT:
410*6e87f20cSMatthias Ringwald                     gap_event_inquiry_result_get_bd_addr(event, addr);
411*6e87f20cSMatthias Ringwald                     // print info
412*6e87f20cSMatthias Ringwald                     printf("Device found: %s ",  bd_addr_to_str(addr));
413*6e87f20cSMatthias Ringwald                     printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
414*6e87f20cSMatthias Ringwald                     if (gap_event_inquiry_result_get_rssi_availabe(event)){
415*6e87f20cSMatthias Ringwald                         printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
416*6e87f20cSMatthias Ringwald                     }
417*6e87f20cSMatthias Ringwald                     if (gap_event_inquiry_result_get_name_available(event)){
418*6e87f20cSMatthias Ringwald                         char name_buffer[240];
419*6e87f20cSMatthias Ringwald                         int name_len = gap_event_inquiry_result_get_name_len(event);
420*6e87f20cSMatthias Ringwald                         memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
421*6e87f20cSMatthias Ringwald                         name_buffer[name_len] = 0;
422*6e87f20cSMatthias Ringwald                         printf(", name '%s'", name_buffer);
423*6e87f20cSMatthias Ringwald                     }
424*6e87f20cSMatthias Ringwald                     printf("\n");
425*6e87f20cSMatthias Ringwald                     break;
426*6e87f20cSMatthias Ringwald                 case GAP_EVENT_INQUIRY_COMPLETE:
427*6e87f20cSMatthias Ringwald                     printf("Inquiry scan complete.\n");
428fffdd288SMatthias Ringwald                     break;
42913839019SMatthias Ringwald                 case HCI_EVENT_SCO_CAN_SEND_NOW:
430185c8cd4SMatthias Ringwald                     sco_demo_send(sco_handle);
43113839019SMatthias Ringwald                     break;
43292bac129SMatthias Ringwald                 case HCI_EVENT_COMMAND_COMPLETE:
43392bac129SMatthias Ringwald                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
434f9f7adeeSMatthias Ringwald                         dump_supported_codecs();
43592bac129SMatthias Ringwald                     }
43692bac129SMatthias Ringwald                     break;
437fffdd288SMatthias Ringwald                 default:
438fffdd288SMatthias Ringwald                     break;
439fffdd288SMatthias Ringwald             }
440fffdd288SMatthias Ringwald 
441fffdd288SMatthias Ringwald             if (event[0] != HCI_EVENT_HFP_META) return;
442fffdd288SMatthias Ringwald 
443fffdd288SMatthias Ringwald             if (event[3]
444fffdd288SMatthias Ringwald                 && event[2] != HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER
445fffdd288SMatthias Ringwald                 && event[2] != HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG
446fffdd288SMatthias Ringwald                 && event[2] != HFP_SUBEVENT_TRANSMIT_DTMF_CODES){
447fffdd288SMatthias Ringwald                 printf("ERROR, status: %u\n", event[3]);
448fffdd288SMatthias Ringwald                 return;
449fffdd288SMatthias Ringwald             }
450fffdd288SMatthias Ringwald 
451fffdd288SMatthias Ringwald             switch (event[2]) {
452fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
453d97d752dSMilanka Ringwald                     acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
45466c297f9SMilanka Ringwald                     hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
45566c297f9SMilanka Ringwald                     printf("Service level connection established from %s.\n", bd_addr_to_str(device_addr));
456b3f76298SMilanka Ringwald                     dump_supported_codecs();
457fffdd288SMatthias Ringwald                    break;
458fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
459fffdd288SMatthias Ringwald                     printf("Service level connection released.\n");
4603cef842bSMatthias Ringwald                     sco_handle = 0;
461fffdd288SMatthias Ringwald                     break;
462fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
4633cef842bSMatthias Ringwald                     if (hfp_subevent_audio_connection_established_get_status(event)){
4643cef842bSMatthias Ringwald                         printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
4653cef842bSMatthias Ringwald                         sco_handle = 0;
4663cef842bSMatthias Ringwald                     } else {
4673cef842bSMatthias Ringwald                         sco_handle = hfp_subevent_audio_connection_established_get_handle(event);
4683cef842bSMatthias Ringwald                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
469d0c4aea6SMilanka Ringwald                         negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
470f9f7adeeSMatthias Ringwald                         switch (negotiated_codec){
471f9f7adeeSMatthias Ringwald                             case 0x01:
472f9f7adeeSMatthias Ringwald                                 printf("Using CVSD codec.\n");
473f9f7adeeSMatthias Ringwald                                 break;
474f9f7adeeSMatthias Ringwald                             case 0x02:
475f9f7adeeSMatthias Ringwald                                 printf("Using mSBC codec.\n");
476f9f7adeeSMatthias Ringwald                                 break;
477f9f7adeeSMatthias Ringwald                             default:
478f9f7adeeSMatthias Ringwald                                 printf("Using unknown codec 0x%02x.\n", negotiated_codec);
479f9f7adeeSMatthias Ringwald                                 break;
480f9f7adeeSMatthias Ringwald                         }
481d0c4aea6SMilanka Ringwald                         sco_demo_set_codec(negotiated_codec);
4823cef842bSMatthias Ringwald                         hci_request_sco_can_send_now_event();
4833cef842bSMatthias Ringwald                     }
484fffdd288SMatthias Ringwald                     break;
485fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
486b038503dSMatthias Ringwald                     printf("Audio connection released\n");
4873cef842bSMatthias Ringwald                     sco_handle = 0;
488b038503dSMatthias Ringwald                     sco_demo_close();
489fffdd288SMatthias Ringwald                     break;
490fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_START_RINGINIG:
491b038503dSMatthias Ringwald                     printf("Start Ringing\n");
492fffdd288SMatthias Ringwald                     break;
493fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_STOP_RINGINIG:
494b038503dSMatthias Ringwald                     printf("Stop Ringing\n");
495fffdd288SMatthias Ringwald                     break;
496fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
497b038503dSMatthias Ringwald                     printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
498fffdd288SMatthias Ringwald                     // validate number
4990696953aSMilanka Ringwald                     if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
5000696953aSMilanka Ringwald                       || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
5010696953aSMilanka Ringwald                       || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
502fffdd288SMatthias Ringwald                         printf("Dialstring valid: accept call\n");
503fffdd288SMatthias Ringwald                         hfp_ag_outgoing_call_accepted();
504fffdd288SMatthias Ringwald                     } else {
505fffdd288SMatthias Ringwald                         printf("Dialstring invalid: reject call\n");
506fffdd288SMatthias Ringwald                         hfp_ag_outgoing_call_rejected();
507fffdd288SMatthias Ringwald                     }
508fffdd288SMatthias Ringwald                     break;
509fffdd288SMatthias Ringwald 
510fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
511b038503dSMatthias Ringwald                     printf("Attach number to voice tag. Sending '1234567\n");
512d97d752dSMilanka Ringwald                     hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
513fffdd288SMatthias Ringwald                     break;
514fffdd288SMatthias Ringwald                 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
515b038503dSMatthias Ringwald                     printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
516d97d752dSMilanka Ringwald                     hfp_ag_send_dtmf_code_done(acl_handle);
517fffdd288SMatthias Ringwald                     break;
5180696953aSMilanka Ringwald                 case HFP_SUBEVENT_CALL_ANSWERED:
519fffdd288SMatthias Ringwald                     printf("Call answered by HF\n");
520fffdd288SMatthias Ringwald                     break;
521fffdd288SMatthias Ringwald                 default:
522fffdd288SMatthias Ringwald                     printf("Event not handled %u\n", event[2]);
523fffdd288SMatthias Ringwald                     break;
524fffdd288SMatthias Ringwald             }
525185c8cd4SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
526185c8cd4SMatthias Ringwald             sco_demo_receive(event, event_size);
527185c8cd4SMatthias Ringwald             break;
52813839019SMatthias Ringwald         default:
52913839019SMatthias Ringwald             break;
53013839019SMatthias Ringwald     }
531fffdd288SMatthias Ringwald }
532fffdd288SMatthias Ringwald 
533fffdd288SMatthias Ringwald static hfp_phone_number_t subscriber_number = {
534fffdd288SMatthias Ringwald     129, "225577"
535fffdd288SMatthias Ringwald };
536fffdd288SMatthias Ringwald 
537fffdd288SMatthias Ringwald /* @section Main Application Setup
538fffdd288SMatthias Ringwald  *
539fffdd288SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
540fffdd288SMatthias Ringwald  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
541fffdd288SMatthias Ringwald  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
542fffdd288SMatthias Ringwald  * The stdin_process callback allows for sending commands to the HFP HF.
543fffdd288SMatthias Ringwald  * At the end the Bluetooth stack is started.
544fffdd288SMatthias Ringwald  */
545fffdd288SMatthias Ringwald 
546fffdd288SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
547fffdd288SMatthias Ringwald 
548fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
549fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
5509ec2630cSMatthias Ringwald     (void)argc;
5519ec2630cSMatthias Ringwald     (void)argv;
552fffdd288SMatthias Ringwald 
553185c8cd4SMatthias Ringwald     sco_demo_init();
554185c8cd4SMatthias Ringwald 
555a895b9f0SMatthias Ringwald     // register for HCI events
556a895b9f0SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
557a895b9f0SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
558a895b9f0SMatthias Ringwald     hci_register_sco_packet_handler(&packet_handler);
559a895b9f0SMatthias Ringwald 
560e64e0086SMatthias Ringwald     gap_discoverable_control(1);
561e64e0086SMatthias Ringwald 
562e64e0086SMatthias Ringwald     // L2CAP
563e64e0086SMatthias Ringwald     l2cap_init();
564e64e0086SMatthias Ringwald 
5654f84bf36SMatthias Ringwald     uint16_t supported_features                   =
5664f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ESCO_S4)                     |
5674f84bf36SMatthias Ringwald         (1<<HFP_AGSF_HF_INDICATORS)               |
5684f84bf36SMatthias Ringwald         (1<<HFP_AGSF_CODEC_NEGOTIATION)           |
5694f84bf36SMatthias Ringwald         (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
5704f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ENHANCED_CALL_CONTROL)       |
5714f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ENHANCED_CALL_STATUS)        |
5724f84bf36SMatthias Ringwald         (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL)    |
5734f84bf36SMatthias Ringwald         (1<<HFP_AGSF_IN_BAND_RING_TONE)           |
5744f84bf36SMatthias Ringwald         (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
5754f84bf36SMatthias Ringwald         (1<<HFP_AGSF_THREE_WAY_CALLING);
5764f84bf36SMatthias Ringwald     int wide_band_speech = 1;
5774f84bf36SMatthias Ringwald 
578e64e0086SMatthias Ringwald     // HFP
579e64e0086SMatthias Ringwald     rfcomm_init();
580fffdd288SMatthias Ringwald     hfp_ag_init(rfcomm_channel_nr);
5814f84bf36SMatthias Ringwald     hfp_ag_init_supported_features(supported_features);
582fffdd288SMatthias Ringwald     hfp_ag_init_codecs(sizeof(codecs), codecs);
583fffdd288SMatthias Ringwald     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
584fffdd288SMatthias Ringwald     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
585fffdd288SMatthias Ringwald     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
586fffdd288SMatthias Ringwald     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
5873cef842bSMatthias Ringwald     hfp_ag_register_packet_handler(&packet_handler);
58813839019SMatthias Ringwald     hci_register_sco_packet_handler(&packet_handler);
589fffdd288SMatthias Ringwald 
590e64e0086SMatthias Ringwald     // SDP Server
591e64e0086SMatthias Ringwald     sdp_init();
592fffdd288SMatthias Ringwald     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
5934f84bf36SMatthias Ringwald     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
594fffdd288SMatthias Ringwald     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
595fffdd288SMatthias Ringwald     sdp_register_service(hfp_service_buffer);
596fffdd288SMatthias Ringwald 
5970e62b7a5SMatthias Ringwald     // parse humand readable Bluetooth address
5980e62b7a5SMatthias Ringwald     sscanf_bd_addr(device_addr_string, device_addr);
5990e62b7a5SMatthias Ringwald 
600d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
601fffdd288SMatthias Ringwald     btstack_stdin_setup(stdin_process);
602fffdd288SMatthias Ringwald #endif
603fffdd288SMatthias Ringwald     // turn on!
604fffdd288SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
605fffdd288SMatthias Ringwald     return 0;
606fffdd288SMatthias Ringwald }
607fffdd288SMatthias Ringwald /* LISTING_END */
6082b6b8c15SMilanka Ringwald /* EXAMPLE_END */
609