xref: /btstack/example/hfp_hf_demo.c (revision ab2c6ae4b737d5e801d3defe4117331eb244ebb7)
1fffdd288SMatthias Ringwald 
2fffdd288SMatthias Ringwald /*
3fffdd288SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
4fffdd288SMatthias Ringwald  *
5fffdd288SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
6fffdd288SMatthias Ringwald  * modification, are permitted provided that the following conditions
7fffdd288SMatthias Ringwald  * are met:
8fffdd288SMatthias Ringwald  *
9fffdd288SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
10fffdd288SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
11fffdd288SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
12fffdd288SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
13fffdd288SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
14fffdd288SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
15fffdd288SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
16fffdd288SMatthias Ringwald  *    from this software without specific prior written permission.
17fffdd288SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
18fffdd288SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
19fffdd288SMatthias Ringwald  *    monetary gain.
20fffdd288SMatthias Ringwald  *
21fffdd288SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
22fffdd288SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23fffdd288SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24fffdd288SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
25fffdd288SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26fffdd288SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27fffdd288SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28fffdd288SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29fffdd288SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30fffdd288SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31fffdd288SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32fffdd288SMatthias Ringwald  * SUCH DAMAGE.
33fffdd288SMatthias Ringwald  *
34fffdd288SMatthias Ringwald  * Please inquire about commercial licensing options at
35fffdd288SMatthias Ringwald  * [email protected]
36fffdd288SMatthias Ringwald  *
37fffdd288SMatthias Ringwald  */
38fffdd288SMatthias Ringwald 
39*ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf_demo.c"
40*ab2c6ae4SMatthias Ringwald 
41fffdd288SMatthias Ringwald /*
42fffdd288SMatthias Ringwald  * hfp_hs_demo.c
43fffdd288SMatthias Ringwald  */
44fffdd288SMatthias Ringwald 
45fffdd288SMatthias Ringwald // *****************************************************************************
46fffdd288SMatthias Ringwald /* EXAMPLE_START(hfp_hs_demo): HFP Hands-Free (HF) Demo
47fffdd288SMatthias Ringwald  *
48fffdd288SMatthias Ringwald  * @text This  HFP Hands-Free example demonstrates how to receive
49fffdd288SMatthias Ringwald  * an output from a remote HFP audio gateway (AG), and,
50d0755cd6SMatthias Ringwald  * if HAVE_POSIX_STDIN is defined, how to control the HFP AG.
51fffdd288SMatthias Ringwald  */
52fffdd288SMatthias Ringwald // *****************************************************************************
53fffdd288SMatthias Ringwald 
54fffdd288SMatthias Ringwald 
55fffdd288SMatthias Ringwald #include "btstack_config.h"
56fffdd288SMatthias Ringwald 
57fffdd288SMatthias Ringwald #include <stdint.h>
58fffdd288SMatthias Ringwald #include <stdio.h>
59fffdd288SMatthias Ringwald #include <stdlib.h>
60fffdd288SMatthias Ringwald #include <string.h>
61fffdd288SMatthias Ringwald #include "btstack.h"
62fcb08cdbSMilanka Ringwald 
63185c8cd4SMatthias Ringwald #include "sco_demo_util.h"
649491a7bfSMatthias Ringwald 
65d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
669491a7bfSMatthias Ringwald #include <unistd.h>
67fffdd288SMatthias Ringwald #include "stdin_support.h"
684af4141bSMatthias Ringwald #endif
69fffdd288SMatthias Ringwald 
70fffdd288SMatthias Ringwald uint8_t hfp_service_buffer[150];
71fffdd288SMatthias Ringwald const uint8_t   rfcomm_channel_nr = 1;
72fffdd288SMatthias Ringwald const char hfp_hf_service_name[] = "BTstack HFP HF Demo";
739491a7bfSMatthias Ringwald static bd_addr_t device_addr = {0x80,0xbe,0x05,0xd5,0x28,0x48};
74fffdd288SMatthias Ringwald 
75d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
76185c8cd4SMatthias Ringwald // 80:BE:05:D5:28:48
77fffdd288SMatthias Ringwald // prototypes
7835833313SMatthias Ringwald static void show_usage(void);
79fffdd288SMatthias Ringwald #endif
80c8626498SMilanka Ringwald static hci_con_handle_t acl_handle = -1;
81185c8cd4SMatthias Ringwald static hci_con_handle_t sco_handle;
821bbecc2bSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
83fffdd288SMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
841bbecc2bSMatthias Ringwald #else
851bbecc2bSMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD};
861bbecc2bSMatthias Ringwald #endif
87fffdd288SMatthias Ringwald static uint16_t indicators[1] = {0x01};
8808d1a604SMilanka Ringwald static uint8_t  negotiated_codec = HFP_CODEC_CVSD;
89186bfbecSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
90fffdd288SMatthias Ringwald char cmd;
91fffdd288SMatthias Ringwald 
92f9f7adeeSMatthias Ringwald static void dump_supported_codecs(void){
93f9f7adeeSMatthias Ringwald     int i;
94f9f7adeeSMatthias Ringwald     int mSBC_skipped = 0;
95f9f7adeeSMatthias Ringwald     printf("Supported codecs: ");
96f9f7adeeSMatthias Ringwald     for (i = 0; i < sizeof(codecs); i++){
97f9f7adeeSMatthias Ringwald         switch(codecs[i]){
98f9f7adeeSMatthias Ringwald             case HFP_CODEC_CVSD:
99f9f7adeeSMatthias Ringwald                 printf("CVSD");
100f9f7adeeSMatthias Ringwald                 break;
101f9f7adeeSMatthias Ringwald             case HFP_CODEC_MSBC:
102f9f7adeeSMatthias Ringwald                 if (hci_extended_sco_link_supported()){
1032308e108SMilanka Ringwald                     printf(", mSBC");
104f9f7adeeSMatthias Ringwald                 } else {
105f9f7adeeSMatthias Ringwald                     mSBC_skipped = 1;
106f9f7adeeSMatthias Ringwald                 }
107f9f7adeeSMatthias Ringwald                 break;
108f9f7adeeSMatthias Ringwald         }
109f9f7adeeSMatthias Ringwald     }
110f9f7adeeSMatthias Ringwald     printf("\n");
111f9f7adeeSMatthias Ringwald     if (mSBC_skipped){
112f9f7adeeSMatthias Ringwald         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
113f9f7adeeSMatthias Ringwald     }
114f9f7adeeSMatthias Ringwald }
11508d1a604SMilanka Ringwald 
116d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
117fffdd288SMatthias Ringwald 
118fffdd288SMatthias Ringwald // Testig User Interface
119fffdd288SMatthias Ringwald static void show_usage(void){
120fcb08cdbSMilanka Ringwald     bd_addr_t iut_address;
121fcb08cdbSMilanka Ringwald     gap_local_bd_addr(iut_address);
122fcb08cdbSMilanka Ringwald 
123fcb08cdbSMilanka Ringwald     printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
1248a7d93dcSMatthias Ringwald     printf("\n");
1258a7d93dcSMatthias Ringwald     printf("a - establish SLC to %s     | ", bd_addr_to_str(device_addr));
126fffdd288SMatthias Ringwald     printf("A - release SLC connection to device\n");
1278a7d93dcSMatthias Ringwald     printf("b - establish Audio connection             | B - release Audio connection\n");
1288a7d93dcSMatthias Ringwald     printf("d - query network operator                 | D - Enable HFP AG registration status update via bitmap(IIA)\n");
1298a7d93dcSMatthias Ringwald     printf("f - answer incoming call                   | F - Hangup call\n");
1308a7d93dcSMatthias Ringwald     printf("g - query network operator name            | G - reject incoming call\n");
1318a7d93dcSMatthias Ringwald     printf("i - dial 1234567                           | I - dial 7654321\n");
1328a7d93dcSMatthias Ringwald     printf("j - dial #1                                | J - dial #99\n");
1338a7d93dcSMatthias Ringwald     printf("o - Set speaker volume to 0  (minimum)     | O - Set speaker volume to 9  (default)\n");
1348a7d93dcSMatthias Ringwald     printf("p - Set speaker volume to 12 (higher)      | P - Set speaker volume to 15 (maximum)\n");
1358a7d93dcSMatthias Ringwald     printf("q - Set microphone gain to 0  (minimum)    | Q - Set microphone gain to 9  (default)\n");
1368a7d93dcSMatthias Ringwald     printf("s - Set microphone gain to 12 (higher)     | S - Set microphone gain to 15 (maximum)\n");
137fffdd288SMatthias Ringwald     printf("t - terminate connection\n");
138fffdd288SMatthias Ringwald     printf("u - send 'user busy' (TWC 0)\n");
139fffdd288SMatthias Ringwald     printf("U - end active call and accept other call' (TWC 1)\n");
1408a7d93dcSMatthias Ringwald     printf("v - Swap active call call (TWC 2)          | V - Join held call (TWC 3)\n");
1418a7d93dcSMatthias Ringwald     printf("w - Connect calls (TWC 4)                  | W - redial\n");
1428a7d93dcSMatthias Ringwald     printf("c/C - disable/enable registration status update for all AG indicators\n");
1438a7d93dcSMatthias Ringwald     printf("e/E - disable/enable reporting of the extended AG error result code\n");
1448a7d93dcSMatthias Ringwald     printf("k/K - deactivate/activate call waiting notification\n");
1458a7d93dcSMatthias Ringwald     printf("l/L - deactivate/activate calling line notification\n");
1468a7d93dcSMatthias Ringwald     printf("m/M - deactivate/activate echo canceling and noise reduction\n");
1478a7d93dcSMatthias Ringwald     printf("n/N - deactivate/activate voice recognition\n");
148fffdd288SMatthias Ringwald     printf("0123456789#*-+ - send DTMF dial tones\n");
1498a7d93dcSMatthias Ringwald     printf("x - request phone number for voice tag     | X - current call status (ECS)\n");
1508a7d93dcSMatthias Ringwald     printf("y - release call with index 2 (ECC)        | Y - private consulation with call 2(ECC)\n");
1518a7d93dcSMatthias Ringwald     printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n");
1528a7d93dcSMatthias Ringwald     printf("{ - Accept held call(RHH 1)                | } - Reject held call(RHH 2)\n");
153fffdd288SMatthias Ringwald     printf("? - Query Subscriber Number (NUM)\n");
154fffdd288SMatthias Ringwald     printf("! - Update HF indicator with assigned number 1 (HFI)\n");
155fffdd288SMatthias Ringwald     printf("---\n");
156fffdd288SMatthias Ringwald     printf("Ctrl-c - exit\n");
157fffdd288SMatthias Ringwald     printf("---\n");
158fffdd288SMatthias Ringwald }
159fffdd288SMatthias Ringwald 
1604af4141bSMatthias Ringwald static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
1619ec2630cSMatthias Ringwald     UNUSED(ds);
1629ec2630cSMatthias Ringwald     UNUSED(callback_type);
163a1dc4edbSMatthias Ringwald 
164a1dc4edbSMatthias Ringwald     cmd = btstack_stdin_read();
165fffdd288SMatthias Ringwald 
166fffdd288SMatthias Ringwald     if (cmd >= '0' && cmd <= '9'){
167fffdd288SMatthias Ringwald         printf("DTMF Code: %c\n", cmd);
168c8626498SMilanka Ringwald         hfp_hf_send_dtmf_code(acl_handle, cmd);
1694af4141bSMatthias Ringwald         return;
170fffdd288SMatthias Ringwald     }
171fffdd288SMatthias Ringwald 
172fffdd288SMatthias Ringwald     switch (cmd){
173fffdd288SMatthias Ringwald         case '#':
174fffdd288SMatthias Ringwald         case '-':
175fffdd288SMatthias Ringwald         case '+':
176fffdd288SMatthias Ringwald         case '*':
177fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
178fffdd288SMatthias Ringwald             printf("DTMF Code: %c\n", cmd);
179c8626498SMilanka Ringwald             hfp_hf_send_dtmf_code(acl_handle, cmd);
180fffdd288SMatthias Ringwald             break;
181fffdd288SMatthias Ringwald         case 'a':
182fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
183fffdd288SMatthias Ringwald             printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
184fffdd288SMatthias Ringwald             hfp_hf_establish_service_level_connection(device_addr);
185fffdd288SMatthias Ringwald             break;
186fffdd288SMatthias Ringwald         case 'A':
187fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
188fffdd288SMatthias Ringwald             printf("Release Service level connection.\n");
189c8626498SMilanka Ringwald             hfp_hf_release_service_level_connection(acl_handle);
190fffdd288SMatthias Ringwald             break;
191fffdd288SMatthias Ringwald         case 'b':
192fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
193fffdd288SMatthias Ringwald             printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
194c8626498SMilanka Ringwald             hfp_hf_establish_audio_connection(acl_handle);
195fffdd288SMatthias Ringwald             break;
196fffdd288SMatthias Ringwald         case 'B':
197fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
198fffdd288SMatthias Ringwald             printf("Release Audio service level connection.\n");
199c8626498SMilanka Ringwald             hfp_hf_release_audio_connection(acl_handle);
200fffdd288SMatthias Ringwald             break;
201fffdd288SMatthias Ringwald         case 'C':
202fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
203fffdd288SMatthias Ringwald             printf("Enable registration status update for all AG indicators.\n");
204c8626498SMilanka Ringwald             hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle);
205fffdd288SMatthias Ringwald         case 'c':
206fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
207fffdd288SMatthias Ringwald             printf("Disable registration status update for all AG indicators.\n");
208c8626498SMilanka Ringwald             hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle);
209fffdd288SMatthias Ringwald             break;
210fffdd288SMatthias Ringwald         case 'D':
211fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
212fffdd288SMatthias Ringwald             printf("Set HFP AG registration status update for individual indicators (0111111).\n");
213c8626498SMilanka Ringwald             hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63);
214fffdd288SMatthias Ringwald             break;
215fffdd288SMatthias Ringwald         case 'd':
216fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
217fffdd288SMatthias Ringwald             printf("Query network operator.\n");
218c8626498SMilanka Ringwald             hfp_hf_query_operator_selection(acl_handle);
219fffdd288SMatthias Ringwald             break;
220fffdd288SMatthias Ringwald         case 'E':
221fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
222fffdd288SMatthias Ringwald             printf("Enable reporting of the extended AG error result code.\n");
223c8626498SMilanka Ringwald             hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle);
224fffdd288SMatthias Ringwald             break;
225fffdd288SMatthias Ringwald         case 'e':
226fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
227fffdd288SMatthias Ringwald             printf("Disable reporting of the extended AG error result code.\n");
228c8626498SMilanka Ringwald             hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle);
229fffdd288SMatthias Ringwald             break;
230fffdd288SMatthias Ringwald         case 'f':
231fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
232fffdd288SMatthias Ringwald             printf("Answer incoming call.\n");
233c8626498SMilanka Ringwald             hfp_hf_answer_incoming_call(acl_handle);
234fffdd288SMatthias Ringwald             break;
235fffdd288SMatthias Ringwald         case 'F':
236fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
237fffdd288SMatthias Ringwald             printf("Hangup call.\n");
238c8626498SMilanka Ringwald             hfp_hf_terminate_call(acl_handle);
239fffdd288SMatthias Ringwald             break;
240fffdd288SMatthias Ringwald         case 'G':
241fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
242fffdd288SMatthias Ringwald             printf("Reject incoming call.\n");
243c8626498SMilanka Ringwald             hfp_hf_reject_incoming_call(acl_handle);
244fffdd288SMatthias Ringwald             break;
245fffdd288SMatthias Ringwald         case 'g':
246fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
247fffdd288SMatthias Ringwald             printf("Query operator.\n");
248c8626498SMilanka Ringwald             hfp_hf_query_operator_selection(acl_handle);
249fffdd288SMatthias Ringwald             break;
250fffdd288SMatthias Ringwald         case 't':
251fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
252fffdd288SMatthias Ringwald             printf("Terminate HCI connection.\n");
253c8626498SMilanka Ringwald             gap_disconnect(acl_handle);
254fffdd288SMatthias Ringwald             break;
255fffdd288SMatthias Ringwald         case 'i':
256fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
257fffdd288SMatthias Ringwald             printf("Dial 1234567\n");
258c8626498SMilanka Ringwald             hfp_hf_dial_number(acl_handle, "1234567");
259fffdd288SMatthias Ringwald             break;
260fffdd288SMatthias Ringwald         case 'I':
261fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
262fffdd288SMatthias Ringwald             printf("Dial 7654321\n");
263c8626498SMilanka Ringwald             hfp_hf_dial_number(acl_handle, "7654321");
264fffdd288SMatthias Ringwald             break;
265fffdd288SMatthias Ringwald         case 'j':
266fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
267fffdd288SMatthias Ringwald             printf("Dial #1\n");
268c8626498SMilanka Ringwald             hfp_hf_dial_memory(acl_handle,1);
269fffdd288SMatthias Ringwald             break;
270fffdd288SMatthias Ringwald         case 'J':
271fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
272fffdd288SMatthias Ringwald             printf("Dial #99\n");
273c8626498SMilanka Ringwald             hfp_hf_dial_memory(acl_handle,99);
274fffdd288SMatthias Ringwald             break;
275fffdd288SMatthias Ringwald         case 'k':
276fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
277fffdd288SMatthias Ringwald             printf("Deactivate call waiting notification\n");
278c8626498SMilanka Ringwald             hfp_hf_deactivate_call_waiting_notification(acl_handle);
279fffdd288SMatthias Ringwald             break;
280fffdd288SMatthias Ringwald         case 'K':
281fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
282fffdd288SMatthias Ringwald             printf("Activate call waiting notification\n");
283c8626498SMilanka Ringwald             hfp_hf_activate_call_waiting_notification(acl_handle);
284fffdd288SMatthias Ringwald             break;
285fffdd288SMatthias Ringwald         case 'l':
286fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
287fffdd288SMatthias Ringwald             printf("Deactivate calling line notification\n");
288c8626498SMilanka Ringwald             hfp_hf_deactivate_calling_line_notification(acl_handle);
289fffdd288SMatthias Ringwald             break;
290fffdd288SMatthias Ringwald         case 'L':
291fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
292fffdd288SMatthias Ringwald             printf("Activate calling line notification\n");
293c8626498SMilanka Ringwald             hfp_hf_activate_calling_line_notification(acl_handle);
294fffdd288SMatthias Ringwald             break;
295fffdd288SMatthias Ringwald         case 'm':
296fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
297fffdd288SMatthias Ringwald             printf("Deactivate echo canceling and noise reduction\n");
298c8626498SMilanka Ringwald             hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle);
299fffdd288SMatthias Ringwald             break;
300fffdd288SMatthias Ringwald         case 'M':
301fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
302fffdd288SMatthias Ringwald             printf("Activate echo canceling and noise reduction\n");
303c8626498SMilanka Ringwald             hfp_hf_activate_echo_canceling_and_noise_reduction(acl_handle);
304fffdd288SMatthias Ringwald             break;
305fffdd288SMatthias Ringwald         case 'n':
306fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
307fffdd288SMatthias Ringwald             printf("Deactivate voice recognition\n");
308c8626498SMilanka Ringwald             hfp_hf_deactivate_voice_recognition_notification(acl_handle);
309fffdd288SMatthias Ringwald             break;
310fffdd288SMatthias Ringwald         case 'N':
311fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
31208d1a604SMilanka Ringwald             printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr));
313c8626498SMilanka Ringwald             hfp_hf_activate_voice_recognition_notification(acl_handle);
314fffdd288SMatthias Ringwald             break;
315fffdd288SMatthias Ringwald         case 'o':
316fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
317fffdd288SMatthias Ringwald             printf("Set speaker gain to 0 (minimum)\n");
318c8626498SMilanka Ringwald             hfp_hf_set_speaker_gain(acl_handle, 0);
319fffdd288SMatthias Ringwald             break;
320fffdd288SMatthias Ringwald         case 'O':
321fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
322fffdd288SMatthias Ringwald             printf("Set speaker gain to 9 (default)\n");
323c8626498SMilanka Ringwald             hfp_hf_set_speaker_gain(acl_handle, 9);
324fffdd288SMatthias Ringwald             break;
325fffdd288SMatthias Ringwald         case 'p':
326fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
327fffdd288SMatthias Ringwald             printf("Set speaker gain to 12 (higher)\n");
328c8626498SMilanka Ringwald             hfp_hf_set_speaker_gain(acl_handle, 12);
329fffdd288SMatthias Ringwald             break;
330fffdd288SMatthias Ringwald         case 'P':
331fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
332fffdd288SMatthias Ringwald             printf("Set speaker gain to 15 (maximum)\n");
333c8626498SMilanka Ringwald             hfp_hf_set_speaker_gain(acl_handle, 15);
334fffdd288SMatthias Ringwald             break;
335fffdd288SMatthias Ringwald         case 'q':
336fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
337fffdd288SMatthias Ringwald             printf("Set microphone gain to 0\n");
338c8626498SMilanka Ringwald             hfp_hf_set_microphone_gain(acl_handle, 0);
339fffdd288SMatthias Ringwald             break;
340fffdd288SMatthias Ringwald         case 'Q':
341fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
342fffdd288SMatthias Ringwald             printf("Set microphone gain to 9\n");
343c8626498SMilanka Ringwald             hfp_hf_set_microphone_gain(acl_handle, 9);
344fffdd288SMatthias Ringwald             break;
345fffdd288SMatthias Ringwald         case 's':
346fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
347fffdd288SMatthias Ringwald             printf("Set microphone gain to 12\n");
348c8626498SMilanka Ringwald             hfp_hf_set_microphone_gain(acl_handle, 12);
349fffdd288SMatthias Ringwald             break;
350fffdd288SMatthias Ringwald         case 'S':
351fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
352fffdd288SMatthias Ringwald             printf("Set microphone gain to 15\n");
353c8626498SMilanka Ringwald             hfp_hf_set_microphone_gain(acl_handle, 15);
354fffdd288SMatthias Ringwald             break;
355fffdd288SMatthias Ringwald         case 'u':
356fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
357fffdd288SMatthias Ringwald             printf("Send 'user busy' (Three-Way Call 0)\n");
358c8626498SMilanka Ringwald             hfp_hf_user_busy(acl_handle);
359fffdd288SMatthias Ringwald             break;
360fffdd288SMatthias Ringwald         case 'U':
361fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
362fffdd288SMatthias Ringwald             printf("End active call and accept waiting/held call (Three-Way Call 1)\n");
363c8626498SMilanka Ringwald             hfp_hf_end_active_and_accept_other(acl_handle);
364fffdd288SMatthias Ringwald             break;
365fffdd288SMatthias Ringwald         case 'v':
366fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
367fffdd288SMatthias Ringwald             printf("Swap active call and hold/waiting call (Three-Way Call 2)\n");
368c8626498SMilanka Ringwald             hfp_hf_swap_calls(acl_handle);
369fffdd288SMatthias Ringwald             break;
370fffdd288SMatthias Ringwald         case 'V':
371fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
372fffdd288SMatthias Ringwald             printf("Join hold call (Three-Way Call 3)\n");
373c8626498SMilanka Ringwald             hfp_hf_join_held_call(acl_handle);
374fffdd288SMatthias Ringwald             break;
375fffdd288SMatthias Ringwald         case 'w':
376fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
377fffdd288SMatthias Ringwald             printf("Connect calls (Three-Way Call 4)\n");
378c8626498SMilanka Ringwald             hfp_hf_connect_calls(acl_handle);
379fffdd288SMatthias Ringwald             break;
380fffdd288SMatthias Ringwald         case 'W':
381fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
382fffdd288SMatthias Ringwald             printf("Redial\n");
383c8626498SMilanka Ringwald             hfp_hf_redial_last_number(acl_handle);
384fffdd288SMatthias Ringwald             break;
385fffdd288SMatthias Ringwald         case 'x':
386fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
387fffdd288SMatthias Ringwald             printf("Request phone number for voice tag\n");
388c8626498SMilanka Ringwald             hfp_hf_request_phone_number_for_voice_tag(acl_handle);
389fffdd288SMatthias Ringwald             break;
390fffdd288SMatthias Ringwald         case 'X':
391fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
392fffdd288SMatthias Ringwald             printf("Query current call status\n");
393c8626498SMilanka Ringwald             hfp_hf_query_current_call_status(acl_handle);
394fffdd288SMatthias Ringwald             break;
395fffdd288SMatthias Ringwald         case 'y':
396fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
397fffdd288SMatthias Ringwald             printf("Release call with index 2\n");
398c8626498SMilanka Ringwald             hfp_hf_release_call_with_index(acl_handle, 2);
399fffdd288SMatthias Ringwald             break;
400fffdd288SMatthias Ringwald         case 'Y':
401fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
402fffdd288SMatthias Ringwald             printf("Private consulation with call 2\n");
403c8626498SMilanka Ringwald             hfp_hf_private_consultation_with_call(acl_handle, 2);
404fffdd288SMatthias Ringwald             break;
405fffdd288SMatthias Ringwald         case '[':
406fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
407fffdd288SMatthias Ringwald             printf("Query Response and Hold status (RHH ?)\n");
408c8626498SMilanka Ringwald             hfp_hf_rrh_query_status(acl_handle);
409fffdd288SMatthias Ringwald             break;
410fffdd288SMatthias Ringwald         case ']':
411fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
412fffdd288SMatthias Ringwald             printf("Place call in a response and held state (RHH 0)\n");
413c8626498SMilanka Ringwald             hfp_hf_rrh_hold_call(acl_handle);
414fffdd288SMatthias Ringwald            break;
415fffdd288SMatthias Ringwald         case '{':
416fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
417fffdd288SMatthias Ringwald             printf("Accept held call (RHH 1)\n");
418c8626498SMilanka Ringwald             hfp_hf_rrh_accept_held_call(acl_handle);
419fffdd288SMatthias Ringwald             break;
420fffdd288SMatthias Ringwald         case '}':
421fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
422fffdd288SMatthias Ringwald             printf("Reject held call (RHH 2)\n");
423c8626498SMilanka Ringwald             hfp_hf_rrh_reject_held_call(acl_handle);
424fffdd288SMatthias Ringwald             break;
425fffdd288SMatthias Ringwald         case '?':
426fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
427fffdd288SMatthias Ringwald             printf("Query Subscriber Number\n");
428c8626498SMilanka Ringwald             hfp_hf_query_subscriber_number(acl_handle);
429fffdd288SMatthias Ringwald             break;
430fffdd288SMatthias Ringwald         case '!':
431fffdd288SMatthias Ringwald             log_info("USER:\'%c\'", cmd);
432fffdd288SMatthias Ringwald             printf("Update HF indicator with assigned number 1 (HFI)\n");
433c8626498SMilanka Ringwald             hfp_hf_set_hf_indicator(acl_handle, 1, 1);
434fffdd288SMatthias Ringwald             break;
435fffdd288SMatthias Ringwald 
436fffdd288SMatthias Ringwald         default:
437fffdd288SMatthias Ringwald             show_usage();
438fffdd288SMatthias Ringwald             break;
439fffdd288SMatthias Ringwald     }
440fffdd288SMatthias Ringwald }
441fffdd288SMatthias Ringwald #endif
442fffdd288SMatthias Ringwald 
44313839019SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
4449ec2630cSMatthias Ringwald     UNUSED(channel);
445185c8cd4SMatthias Ringwald 
44682aca432SMatthias Ringwald     switch (packet_type){
44782aca432SMatthias Ringwald 
44882aca432SMatthias Ringwald         case HCI_SCO_DATA_PACKET:
449fcb08cdbSMilanka Ringwald             sco_demo_receive(event, event_size);
45082aca432SMatthias Ringwald             break;
45182aca432SMatthias Ringwald 
45282aca432SMatthias Ringwald         case HCI_EVENT_PACKET:
45382aca432SMatthias Ringwald             switch (event[0]){
45482aca432SMatthias Ringwald                 case HCI_EVENT_SCO_CAN_SEND_NOW:
455185c8cd4SMatthias Ringwald                     sco_demo_send(sco_handle);
45682aca432SMatthias Ringwald                     break;
45782aca432SMatthias Ringwald 
458f9f7adeeSMatthias Ringwald                 case HCI_EVENT_COMMAND_COMPLETE:
459f9f7adeeSMatthias Ringwald                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
460f9f7adeeSMatthias Ringwald                         dump_supported_codecs();
461f9f7adeeSMatthias Ringwald                     }
462f9f7adeeSMatthias Ringwald                     break;
463f9f7adeeSMatthias Ringwald 
46482aca432SMatthias Ringwald                 case HCI_EVENT_HFP_META:
46582aca432SMatthias Ringwald                     switch (event[2]) {
46682aca432SMatthias Ringwald                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
467c8626498SMilanka Ringwald                             acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
46808d1a604SMilanka Ringwald                             hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
46908d1a604SMilanka Ringwald                             printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr));
47082aca432SMatthias Ringwald                             break;
47182aca432SMatthias Ringwald                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
47282aca432SMatthias Ringwald                             printf("Service level connection released.\n\n");
47382aca432SMatthias Ringwald                             break;
47482aca432SMatthias Ringwald                         case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
47582aca432SMatthias Ringwald                             if (hfp_subevent_audio_connection_established_get_status(event)){
47682aca432SMatthias Ringwald                                 sco_handle = 0;
47782aca432SMatthias Ringwald                                 printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
47882aca432SMatthias Ringwald                             } else {
47982aca432SMatthias Ringwald                                 sco_handle = hfp_subevent_audio_connection_established_get_handle(event);
4808901a7c4SMatthias Ringwald                                 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
481d0c4aea6SMilanka Ringwald                                 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
482f9f7adeeSMatthias Ringwald                                 switch (negotiated_codec){
483f9f7adeeSMatthias Ringwald                                     case 0x01:
484f9f7adeeSMatthias Ringwald                                         printf("Using CVSD codec.\n");
485f9f7adeeSMatthias Ringwald                                         break;
486f9f7adeeSMatthias Ringwald                                     case 0x02:
487f9f7adeeSMatthias Ringwald                                         printf("Using mSBC codec.\n");
488f9f7adeeSMatthias Ringwald                                         break;
489f9f7adeeSMatthias Ringwald                                     default:
490f9f7adeeSMatthias Ringwald                                         printf("Using unknown codec 0x%02x.\n", negotiated_codec);
491f9f7adeeSMatthias Ringwald                                         break;
492f9f7adeeSMatthias Ringwald                                 }
493d0c4aea6SMilanka Ringwald                                 sco_demo_set_codec(negotiated_codec);
49482aca432SMatthias Ringwald                                 hci_request_sco_can_send_now_event();
495185c8cd4SMatthias Ringwald                             }
49682aca432SMatthias Ringwald                             break;
49782aca432SMatthias Ringwald                         case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
49882aca432SMatthias Ringwald                             sco_handle = 0;
49982aca432SMatthias Ringwald                             printf("Audio connection released\n");
500fcb08cdbSMilanka Ringwald                             sco_demo_close();
50182aca432SMatthias Ringwald                             break;
50282aca432SMatthias Ringwald                         case HFP_SUBEVENT_COMPLETE:
50382aca432SMatthias Ringwald                             switch (cmd){
50482aca432SMatthias Ringwald                                 case 'd':
50582aca432SMatthias Ringwald                                     printf("HFP AG registration status update enabled.\n");
50682aca432SMatthias Ringwald                                     break;
50782aca432SMatthias Ringwald                                 case 'e':
50882aca432SMatthias Ringwald                                     printf("HFP AG registration status update for individual indicators set.\n");
50982aca432SMatthias Ringwald                                 default:
51082aca432SMatthias Ringwald                                     break;
51182aca432SMatthias Ringwald                             }
51282aca432SMatthias Ringwald                             break;
51382aca432SMatthias Ringwald                         case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED:
51482aca432SMatthias Ringwald                             printf("AG_INDICATOR_STATUS_CHANGED, AG indicator '%s' (index: %d) to: %d\n", (const char*) &event[5], event[3], event[4]);
51582aca432SMatthias Ringwald                             break;
51682aca432SMatthias Ringwald                         case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED:
51782aca432SMatthias Ringwald                             printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n", event[3], event[4], (char *) &event[5]);
51882aca432SMatthias Ringwald                             break;
51982aca432SMatthias Ringwald                         case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR:
52082aca432SMatthias Ringwald                             printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : %d\n", event[3]);
52182aca432SMatthias Ringwald                             break;
52282aca432SMatthias Ringwald                         case HFP_SUBEVENT_RING:
52382aca432SMatthias Ringwald                             printf("** Ring **\n");
52482aca432SMatthias Ringwald                             break;
52582aca432SMatthias Ringwald                         case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG:
52682aca432SMatthias Ringwald                             printf("Phone number for voice tag: %s\n", (const char *) &event[3]);
52782aca432SMatthias Ringwald                             break;
52882aca432SMatthias Ringwald                         case HFP_SUBEVENT_SPEAKER_VOLUME:
52982aca432SMatthias Ringwald                             printf("Speaker volume: %u\n", event[3]);
53082aca432SMatthias Ringwald                             break;
53182aca432SMatthias Ringwald                         case HFP_SUBEVENT_MICROPHONE_VOLUME:
53282aca432SMatthias Ringwald                             printf("Microphone volume: %u\n", event[3]);
53382aca432SMatthias Ringwald                             break;
53482aca432SMatthias Ringwald                         default:
53582aca432SMatthias Ringwald                             printf("event not handled %u\n", event[2]);
53682aca432SMatthias Ringwald                             break;
53782aca432SMatthias Ringwald                     }
53882aca432SMatthias Ringwald                     break;
53982aca432SMatthias Ringwald 
54082aca432SMatthias Ringwald                 default:
54182aca432SMatthias Ringwald                     break;
54282aca432SMatthias Ringwald             }
54382aca432SMatthias Ringwald             break;
54482aca432SMatthias Ringwald 
54582aca432SMatthias Ringwald         default:
54682aca432SMatthias Ringwald             break;
54782aca432SMatthias Ringwald     }
54882aca432SMatthias Ringwald 
549fffdd288SMatthias Ringwald     if (event[0] != HCI_EVENT_HFP_META) return;
550fffdd288SMatthias Ringwald }
551fffdd288SMatthias Ringwald 
552fffdd288SMatthias Ringwald /* @section Main Application Setup
553fffdd288SMatthias Ringwald  *
554fffdd288SMatthias Ringwald  * @text Listing MainConfiguration shows main application code.
555fffdd288SMatthias Ringwald  * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it.
556fffdd288SMatthias Ringwald  * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers.
557fffdd288SMatthias Ringwald  * The stdin_process callback allows for sending commands to the HFP AG.
558fffdd288SMatthias Ringwald  * At the end the Bluetooth stack is started.
559fffdd288SMatthias Ringwald  */
560fffdd288SMatthias Ringwald 
561fffdd288SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */
562fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
563fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
5649ec2630cSMatthias Ringwald     (void)argc;
5659ec2630cSMatthias Ringwald     (void)argv;
566185c8cd4SMatthias Ringwald 
567a895b9f0SMatthias Ringwald     sco_demo_init();
568a895b9f0SMatthias Ringwald 
569a895b9f0SMatthias Ringwald     // register for HCI events
570a895b9f0SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
571a895b9f0SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
572a895b9f0SMatthias Ringwald     hci_register_sco_packet_handler(&packet_handler);
573a895b9f0SMatthias Ringwald 
574a895b9f0SMatthias Ringwald     gap_discoverable_control(1);
575a895b9f0SMatthias Ringwald     gap_set_class_of_device(0x200408);
576a895b9f0SMatthias Ringwald 
577fffdd288SMatthias Ringwald     // init L2CAP
578fffdd288SMatthias Ringwald     l2cap_init();
579fffdd288SMatthias Ringwald 
5804f84bf36SMatthias Ringwald     uint16_t hf_supported_features          =
5814f84bf36SMatthias Ringwald         (1<<HFP_HFSF_ESCO_S4)               |
5824f84bf36SMatthias Ringwald         (1<<HFP_HFSF_HF_INDICATORS)         |
5834f84bf36SMatthias Ringwald         (1<<HFP_HFSF_CODEC_NEGOTIATION)     |
5844f84bf36SMatthias Ringwald         (1<<HFP_HFSF_ENHANCED_CALL_STATUS)  |
5854f84bf36SMatthias Ringwald         (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL);
5864f84bf36SMatthias Ringwald     int wide_band_speech = 1;
5874f84bf36SMatthias Ringwald 
588a895b9f0SMatthias Ringwald     rfcomm_init();
589fffdd288SMatthias Ringwald     hfp_hf_init(rfcomm_channel_nr);
5904f84bf36SMatthias Ringwald     hfp_hf_init_supported_features(hf_supported_features);
591fffdd288SMatthias Ringwald     hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
592fffdd288SMatthias Ringwald     hfp_hf_init_codecs(sizeof(codecs), codecs);
593fffdd288SMatthias Ringwald 
594fffdd288SMatthias Ringwald     hfp_hf_register_packet_handler(packet_handler);
595185c8cd4SMatthias Ringwald     hci_register_sco_packet_handler(&packet_handler);
596fffdd288SMatthias Ringwald 
597a895b9f0SMatthias Ringwald     sdp_init();
598fffdd288SMatthias Ringwald     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
5994f84bf36SMatthias Ringwald     hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech);
600fffdd288SMatthias Ringwald     printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer));
601fffdd288SMatthias Ringwald     sdp_register_service(hfp_service_buffer);
602a895b9f0SMatthias Ringwald 
603fffdd288SMatthias Ringwald 
604d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN
605fffdd288SMatthias Ringwald     btstack_stdin_setup(stdin_process);
606fffdd288SMatthias Ringwald #endif
607fffdd288SMatthias Ringwald     // turn on!
608fffdd288SMatthias Ringwald     hci_power_control(HCI_POWER_ON);
609fffdd288SMatthias Ringwald     return 0;
610fffdd288SMatthias Ringwald }
611fffdd288SMatthias Ringwald /* LISTING_END */
6122b6b8c15SMilanka Ringwald /* EXAMPLE_END */
613