xref: /btstack/example/hfp_hf_demo.c (revision cf7250aa3d6090314187d303f46421dd69a5eef4)
1 
2 /*
3  * Copyright (C) 2014 BlueKitchen GmbH
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  * 4. Any redistribution, use, or modification is done solely for
18  *    personal benefit and not for any commercial purpose or for
19  *    monetary gain.
20  *
21  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
25  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Please inquire about commercial licensing options at
35  * [email protected]
36  *
37  */
38 
39 #define BTSTACK_FILE__ "hfp_hf_demo.c"
40 
41 /*
42  * hfp_hs_demo.c
43  */
44 
45 // *****************************************************************************
46 /* EXAMPLE_START(hfp_hs_demo): HFP HF - Hands-Free
47  *
48  * @text This  HFP Hands-Free example demonstrates how to receive
49  * an output from a remote HFP audio gateway (AG), and,
50  * if HAVE_BTSTACK_STDIN is defined, how to control the HFP AG.
51  */
52 // *****************************************************************************
53 
54 
55 #include "btstack_config.h"
56 
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include "btstack.h"
62 
63 #include "sco_demo_util.h"
64 
65 uint8_t hfp_service_buffer[150];
66 const uint8_t   rfcomm_channel_nr = 1;
67 const char hfp_hf_service_name[] = "HFP HF Demo";
68 
69 
70 #ifdef HAVE_BTSTACK_STDIN
71 // static const char * device_addr_string = "6C:72:E7:10:22:EE";
72 static const char * device_addr_string = "00:02:72:DC:31:C1";
73 #endif
74 
75 static bd_addr_t device_addr;
76 
77 #ifdef HAVE_BTSTACK_STDIN
78 // 80:BE:05:D5:28:48
79 // prototypes
80 static void show_usage(void);
81 #endif
82 static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID;
83 static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID;
84 
85 static uint8_t codecs[] = {
86         HFP_CODEC_CVSD,
87 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
88         HFP_CODEC_MSBC,
89 #endif
90 #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
91         HFP_CODEC_LC3_SWB,
92 #endif
93 };
94 
95 static uint16_t indicators[1] = {0x01};
96 static uint8_t  negotiated_codec = HFP_CODEC_CVSD;
97 static btstack_packet_callback_registration_t hci_event_callback_registration;
98 static char cmd;
99 
100 static void dump_supported_codecs(void){
101     printf("Supported codecs: CVSD");
102     if (hci_extended_sco_link_supported()) {
103 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
104         printf(", mSBC");
105 #endif
106 #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
107         printf(", LC3-SWB");
108 #endif
109         printf("\n");
110     } else {
111         printf("\nmSBC and/or LC3-SWB disabled as eSCO not supported by local controller.\n");
112     }
113 }
114 
115 static void report_status(uint8_t status, const char * message){
116     if (status != ERROR_CODE_SUCCESS){
117         printf("%s command failed, status 0x%02x\n", message, status);
118     } else {
119         printf("%s command successful\n", message);
120     }
121 }
122 
123 #ifdef HAVE_BTSTACK_STDIN
124 
125 // Testig User Interface
126 static void show_usage(void){
127     bd_addr_t iut_address;
128     gap_local_bd_addr(iut_address);
129 
130     printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
131     printf("\n");
132     printf("a - establish SLC to %s     | ", bd_addr_to_str(device_addr));
133     printf("A - release SLC connection to device\n");
134     printf("b - establish Audio connection             | B - release Audio connection\n");
135     printf("d - query network operator                 | D - Enable HFP AG registration status update via bitmap(IIA)\n");
136     printf("f - answer incoming call                   | F - Hangup call\n");
137     printf("g - query network operator name            | G - reject incoming call\n");
138     printf("i - dial 1234567                           | I - dial 7654321\n");
139     printf("j - dial #1                                | J - dial #99\n");
140     printf("o - Set speaker volume to 0  (minimum)     | O - Set speaker volume to 9  (default)\n");
141     printf("p - Set speaker volume to 12 (higher)      | P - Set speaker volume to 15 (maximum)\n");
142     printf("q - Set microphone gain to 0  (minimum)    | Q - Set microphone gain to 9  (default)\n");
143     printf("s - Set microphone gain to 12 (higher)     | S - Set microphone gain to 15 (maximum)\n");
144     printf("t - terminate connection\n");
145     printf("u - send 'user busy' (TWC 0)\n");
146     printf("U - end active call and accept other call' (TWC 1)\n");
147     printf("v - Swap active call call (TWC 2)          | V - Join held call (TWC 3)\n");
148     printf("w - Connect calls (TWC 4)                  | W - redial\n");
149     printf("m - deactivate echo canceling and noise reduction\n");
150     printf("c/C - disable/enable registration status update for all AG indicators\n");
151     printf("e/E - disable/enable reporting of the extended AG error result code\n");
152     printf("k/K - deactivate/activate call waiting notification\n");
153     printf("l/L - deactivate/activate calling line notification\n");
154     printf("n/N - deactivate/activate voice recognition\n");
155 
156     printf("0123456789#*-+ - send DTMF dial tones\n");
157     printf("x - request phone number for voice tag     | X - current call status (ECS)\n");
158     printf("y - release call with index 2 (ECC)        | Y - private consultation with call 2(ECC)\n");
159     printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n");
160     printf("{ - Accept held call(RHH 1)                | } - Reject held call(RHH 2)\n");
161     printf("? - Query Subscriber Number (NUM)\n");
162     printf("! - Update HF indicator with assigned number 1 (HFI)\n");
163     printf("\n");
164 }
165 
166 static void stdin_process(char c){
167     uint8_t status = ERROR_CODE_SUCCESS;
168 
169     cmd = c;    // used in packet handler
170 
171     if (cmd >= '0' && cmd <= '9'){
172         printf("DTMF Code: %c\n", cmd);
173         status = hfp_hf_send_dtmf_code(acl_handle, cmd);
174         return;
175     }
176 
177     switch (cmd){
178         case '#':
179         case '-':
180         case '+':
181         case '*':
182             log_info("USER:\'%c\'", cmd);
183             printf("DTMF Code: %c\n", cmd);
184             status = hfp_hf_send_dtmf_code(acl_handle, cmd);
185             break;
186         case 'a':
187             log_info("USER:\'%c\'", cmd);
188             printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
189             status = hfp_hf_establish_service_level_connection(device_addr);
190             break;
191         case 'A':
192             log_info("USER:\'%c\'", cmd);
193             printf("Release Service level connection.\n");
194             status = hfp_hf_release_service_level_connection(acl_handle);
195             break;
196         case 'b':
197             log_info("USER:\'%c\'", cmd);
198             printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
199             status = hfp_hf_establish_audio_connection(acl_handle);
200             break;
201         case 'B':
202             log_info("USER:\'%c\'", cmd);
203             printf("Release Audio service level connection.\n");
204             status = hfp_hf_release_audio_connection(acl_handle);
205             break;
206         case 'C':
207             log_info("USER:\'%c\'", cmd);
208             printf("Enable registration status update for all AG indicators.\n");
209             status = hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle);
210             break;
211         case 'c':
212             log_info("USER:\'%c\'", cmd);
213             printf("Disable registration status update for all AG indicators.\n");
214             status = hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle);
215             break;
216         case 'D':
217             log_info("USER:\'%c\'", cmd);
218             printf("Set HFP AG registration status update for individual indicators (0111111).\n");
219             status = hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63);
220             break;
221         case 'd':
222             log_info("USER:\'%c\'", cmd);
223             printf("Query network operator.\n");
224             status = hfp_hf_query_operator_selection(acl_handle);
225             break;
226         case 'E':
227             log_info("USER:\'%c\'", cmd);
228             printf("Enable reporting of the extended AG error result code.\n");
229             status = hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle);
230             break;
231         case 'e':
232             log_info("USER:\'%c\'", cmd);
233             printf("Disable reporting of the extended AG error result code.\n");
234             status = hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle);
235             break;
236         case 'f':
237             log_info("USER:\'%c\'", cmd);
238             printf("Answer incoming call.\n");
239             status = hfp_hf_answer_incoming_call(acl_handle);
240             break;
241         case 'F':
242             log_info("USER:\'%c\'", cmd);
243             printf("Hangup call.\n");
244             status = hfp_hf_terminate_call(acl_handle);
245             break;
246         case 'G':
247             log_info("USER:\'%c\'", cmd);
248             printf("Reject incoming call.\n");
249             status = hfp_hf_reject_incoming_call(acl_handle);
250             break;
251         case 'g':
252             log_info("USER:\'%c\'", cmd);
253             printf("Query operator.\n");
254             status = hfp_hf_query_operator_selection(acl_handle);
255             break;
256         case 't':
257             log_info("USER:\'%c\'", cmd);
258             printf("Terminate HCI connection.\n");
259             gap_disconnect(acl_handle);
260             break;
261         case 'i':
262             log_info("USER:\'%c\'", cmd);
263             printf("Dial 1234567\n");
264             status = hfp_hf_dial_number(acl_handle, "1234567");
265             break;
266         case 'I':
267             log_info("USER:\'%c\'", cmd);
268             printf("Dial 7654321\n");
269             status = hfp_hf_dial_number(acl_handle, "7654321");
270             break;
271         case 'j':
272             log_info("USER:\'%c\'", cmd);
273             printf("Dial #1\n");
274             status = hfp_hf_dial_memory(acl_handle,1);
275             break;
276         case 'J':
277             log_info("USER:\'%c\'", cmd);
278             printf("Dial #99\n");
279             status = hfp_hf_dial_memory(acl_handle,99);
280             break;
281         case 'k':
282             log_info("USER:\'%c\'", cmd);
283             printf("Deactivate call waiting notification\n");
284             status = hfp_hf_deactivate_call_waiting_notification(acl_handle);
285             break;
286         case 'K':
287             log_info("USER:\'%c\'", cmd);
288             printf("Activate call waiting notification\n");
289             status = hfp_hf_activate_call_waiting_notification(acl_handle);
290             break;
291         case 'l':
292             log_info("USER:\'%c\'", cmd);
293             printf("Deactivate calling line notification\n");
294             status = hfp_hf_deactivate_calling_line_notification(acl_handle);
295             break;
296         case 'L':
297             log_info("USER:\'%c\'", cmd);
298             printf("Activate calling line notification\n");
299             status = hfp_hf_activate_calling_line_notification(acl_handle);
300             break;
301         case 'm':
302             log_info("USER:\'%c\'", cmd);
303             printf("Deactivate echo canceling and noise reduction\n");
304             status = hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle);
305             break;
306         case 'n':
307             log_info("USER:\'%c\'", cmd);
308             printf("Deactivate voice recognition\n");
309             status = hfp_hf_deactivate_voice_recognition(acl_handle);
310             break;
311         case 'N':
312             log_info("USER:\'%c\'", cmd);
313             printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr));
314             status = hfp_hf_activate_voice_recognition(acl_handle);
315             break;
316         case 'o':
317             log_info("USER:\'%c\'", cmd);
318             printf("Set speaker gain to 0 (minimum)\n");
319             status = hfp_hf_set_speaker_gain(acl_handle, 0);
320             break;
321         case 'O':
322             log_info("USER:\'%c\'", cmd);
323             printf("Set speaker gain to 9 (default)\n");
324             status = hfp_hf_set_speaker_gain(acl_handle, 9);
325             break;
326         case 'p':
327             log_info("USER:\'%c\'", cmd);
328             printf("Set speaker gain to 12 (higher)\n");
329             status = hfp_hf_set_speaker_gain(acl_handle, 12);
330             break;
331         case 'P':
332             log_info("USER:\'%c\'", cmd);
333             printf("Set speaker gain to 15 (maximum)\n");
334             status = hfp_hf_set_speaker_gain(acl_handle, 15);
335             break;
336         case 'q':
337             log_info("USER:\'%c\'", cmd);
338             printf("Set microphone gain to 0\n");
339             status = hfp_hf_set_microphone_gain(acl_handle, 0);
340             break;
341         case 'Q':
342             log_info("USER:\'%c\'", cmd);
343             printf("Set microphone gain to 9\n");
344             status = hfp_hf_set_microphone_gain(acl_handle, 9);
345             break;
346         case 's':
347             log_info("USER:\'%c\'", cmd);
348             printf("Set microphone gain to 12\n");
349             status = hfp_hf_set_microphone_gain(acl_handle, 12);
350             break;
351         case 'S':
352             log_info("USER:\'%c\'", cmd);
353             printf("Set microphone gain to 15\n");
354             status = hfp_hf_set_microphone_gain(acl_handle, 15);
355             break;
356         case 'u':
357             log_info("USER:\'%c\'", cmd);
358             printf("Send 'user busy' (Three-Way Call 0)\n");
359             status = hfp_hf_user_busy(acl_handle);
360             break;
361         case 'U':
362             log_info("USER:\'%c\'", cmd);
363             printf("End active call and accept waiting/held call (Three-Way Call 1)\n");
364             status = hfp_hf_end_active_and_accept_other(acl_handle);
365             break;
366         case 'v':
367             log_info("USER:\'%c\'", cmd);
368             printf("Swap active call and hold/waiting call (Three-Way Call 2)\n");
369             status = hfp_hf_swap_calls(acl_handle);
370             break;
371         case 'V':
372             log_info("USER:\'%c\'", cmd);
373             printf("Join hold call (Three-Way Call 3)\n");
374             status = hfp_hf_join_held_call(acl_handle);
375             break;
376         case 'w':
377             log_info("USER:\'%c\'", cmd);
378             printf("Connect calls (Three-Way Call 4)\n");
379             status = hfp_hf_connect_calls(acl_handle);
380             break;
381         case 'W':
382             log_info("USER:\'%c\'", cmd);
383             printf("Redial\n");
384             status = hfp_hf_redial_last_number(acl_handle);
385             break;
386         case 'x':
387             log_info("USER:\'%c\'", cmd);
388             printf("Request phone number for voice tag\n");
389             status = hfp_hf_request_phone_number_for_voice_tag(acl_handle);
390             break;
391         case 'X':
392             log_info("USER:\'%c\'", cmd);
393             printf("Query current call status\n");
394             status = hfp_hf_query_current_call_status(acl_handle);
395             break;
396         case 'y':
397             log_info("USER:\'%c\'", cmd);
398             printf("Release call with index 2\n");
399             status = hfp_hf_release_call_with_index(acl_handle, 2);
400             break;
401         case 'Y':
402             log_info("USER:\'%c\'", cmd);
403             printf("Private consultation with call 2\n");
404             status = hfp_hf_private_consultation_with_call(acl_handle, 2);
405             break;
406         case '[':
407             log_info("USER:\'%c\'", cmd);
408             printf("Query Response and Hold status (RHH ?)\n");
409             status = hfp_hf_rrh_query_status(acl_handle);
410             break;
411         case ']':
412             log_info("USER:\'%c\'", cmd);
413             printf("Place call in a response and held state (RHH 0)\n");
414             status = hfp_hf_rrh_hold_call(acl_handle);
415            break;
416         case '{':
417             log_info("USER:\'%c\'", cmd);
418             printf("Accept held call (RHH 1)\n");
419             status = hfp_hf_rrh_accept_held_call(acl_handle);
420             break;
421         case '}':
422             log_info("USER:\'%c\'", cmd);
423             printf("Reject held call (RHH 2)\n");
424             status = hfp_hf_rrh_reject_held_call(acl_handle);
425             break;
426         case '?':
427             log_info("USER:\'%c\'", cmd);
428             printf("Query Subscriber Number\n");
429             status = hfp_hf_query_subscriber_number(acl_handle);
430             break;
431         case '!':
432             log_info("USER:\'%c\'", cmd);
433             printf("Update HF indicator with assigned number 1 (HFI)\n");
434             status = hfp_hf_set_hf_indicator(acl_handle, 1, 1);
435             break;
436         default:
437             show_usage();
438             break;
439     }
440 
441     if (status != ERROR_CODE_SUCCESS){
442         printf("Could not perform command, status 0x%02x\n", status);
443     }
444 }
445 #endif
446 
447 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
448     UNUSED(channel);
449     bd_addr_t event_addr;
450 
451     switch (packet_type){
452 
453         case HCI_SCO_DATA_PACKET:
454             // forward received SCO / audio packets to SCO component
455             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
456             sco_demo_receive(event, event_size);
457             break;
458 
459         case HCI_EVENT_PACKET:
460             switch (hci_event_packet_get_type(event)){
461                 case BTSTACK_EVENT_STATE:
462                     // list supported codecs after stack has started up
463                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
464                     dump_supported_codecs();
465                     break;
466 
467                 case HCI_EVENT_PIN_CODE_REQUEST:
468                     // inform about pin code request and respond with '0000'
469                     printf("Pin code request - using '0000'\n");
470                     hci_event_pin_code_request_get_bd_addr(event, event_addr);
471                     gap_pin_code_response(event_addr, "0000");
472                     break;
473 
474                 case HCI_EVENT_SCO_CAN_SEND_NOW:
475                     sco_demo_send(sco_handle);
476                     break;
477 
478                 default:
479                     break;
480             }
481             break;
482 
483         default:
484             break;
485     }
486 
487 }
488 
489 static void hfp_hf_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
490     UNUSED(channel);
491     uint8_t status;
492     bd_addr_t event_addr;
493 
494     switch (packet_type){
495 
496         case HCI_SCO_DATA_PACKET:
497             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
498             sco_demo_receive(event, event_size);
499             break;
500 
501         case HCI_EVENT_PACKET:
502             switch (hci_event_packet_get_type(event)){
503                 case BTSTACK_EVENT_STATE:
504                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
505                     dump_supported_codecs();
506                     break;
507 
508                 case HCI_EVENT_PIN_CODE_REQUEST:
509                     // inform about pin code request
510                     printf("Pin code request - using '0000'\n");
511                     hci_event_pin_code_request_get_bd_addr(event, event_addr);
512                     gap_pin_code_response(event_addr, "0000");
513                     break;
514 
515                 case HCI_EVENT_SCO_CAN_SEND_NOW:
516                     sco_demo_send(sco_handle);
517                     break;
518 
519                 case HCI_EVENT_HFP_META:
520                     switch (hci_event_hfp_meta_get_subevent_code(event)) {
521                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
522                             status = hfp_subevent_service_level_connection_established_get_status(event);
523                             if (status != ERROR_CODE_SUCCESS){
524                                 printf("Connection failed, status 0x%02x\n", status);
525                                 break;
526                             }
527                             acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
528                             hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
529                             printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr));
530                             break;
531                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
532                             acl_handle = HCI_CON_HANDLE_INVALID;
533                             printf("Service level connection released.\n\n");
534                             break;
535                         case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
536                             status = hfp_subevent_audio_connection_established_get_status(event);
537                             if (status != ERROR_CODE_SUCCESS){
538                                 printf("Audio connection establishment failed with status 0x%02x\n", status);
539                                 break;
540                             }
541                             sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
542                             printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
543                             negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
544                             switch (negotiated_codec){
545                                 case HFP_CODEC_CVSD:
546                                     printf("Using CVSD codec.\n");
547                                     break;
548                                 case HFP_CODEC_MSBC:
549                                     printf("Using mSBC codec.\n");
550                                     break;
551                                 case HFP_CODEC_LC3_SWB:
552                                     printf("Using LC3-SWB codec.\n");
553                                     break;
554                                 default:
555                                     printf("Using unknown codec 0x%02x.\n", negotiated_codec);
556                                     break;
557                             }
558                             sco_demo_set_codec(negotiated_codec);
559                             hci_request_sco_can_send_now_event();
560                             break;
561 
562                         case HFP_SUBEVENT_CALL_ANSWERED:
563                             printf("Call answered\n");
564                             break;
565 
566                         case HFP_SUBEVENT_CALL_TERMINATED:
567                             printf("Call terminated\n");
568                             break;
569 
570                         case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
571                             sco_handle = HCI_CON_HANDLE_INVALID;
572                             printf("Audio connection released\n");
573                             sco_demo_close();
574                             break;
575                         case  HFP_SUBEVENT_COMPLETE:
576                             status = hfp_subevent_complete_get_status(event);
577                             if (status == ERROR_CODE_SUCCESS){
578                                 printf("Cmd \'%c\' succeeded\n", cmd);
579                             } else {
580                                 printf("Cmd \'%c\' failed with status 0x%02x\n", cmd, status);
581                             }
582                             break;
583 
584                         case HFP_SUBEVENT_AG_INDICATOR_MAPPING:
585                             printf("AG Indicator Mapping | INDEX %d: range [%d, %d], name '%s'\n",
586                                 hfp_subevent_ag_indicator_mapping_get_indicator_index(event),
587                                 hfp_subevent_ag_indicator_mapping_get_indicator_min_range(event),
588                                 hfp_subevent_ag_indicator_mapping_get_indicator_max_range(event),
589                                 (const char*) hfp_subevent_ag_indicator_mapping_get_indicator_name(event));
590                             break;
591 
592                         case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED:
593                             printf("AG Indicator Status  | INDEX %d: status 0x%02x, '%s'\n",
594                                 hfp_subevent_ag_indicator_status_changed_get_indicator_index(event),
595                                 hfp_subevent_ag_indicator_status_changed_get_indicator_status(event),
596                                 (const char*) hfp_subevent_ag_indicator_status_changed_get_indicator_name(event));
597                             break;
598                         case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED:
599                             printf("NETWORK_OPERATOR_CHANGED, operator mode %d, format %d, name %s\n",
600                                 hfp_subevent_network_operator_changed_get_network_operator_mode(event),
601                                 hfp_subevent_network_operator_changed_get_network_operator_format(event),
602                                 (char *) hfp_subevent_network_operator_changed_get_network_operator_name(event));
603                             break;
604                         case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR:
605                             printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status 0x%02x\n",
606                                 hfp_subevent_extended_audio_gateway_error_get_error(event));
607                             break;
608                         case HFP_SUBEVENT_START_RINGING:
609                             printf("** START Ringing **\n");
610                             break;
611                         case HFP_SUBEVENT_RING:
612                             printf("** Ring **\n");
613                             break;
614                         case HFP_SUBEVENT_STOP_RINGING:
615                             printf("** STOP Ringing **\n");
616                             break;
617                         case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG:
618                             printf("Phone number for voice tag: %s\n",
619                                 (const char *) hfp_subevent_number_for_voice_tag_get_number(event));
620                             break;
621                         case HFP_SUBEVENT_SPEAKER_VOLUME:
622                             printf("Speaker volume: gain %u\n",
623                                 hfp_subevent_speaker_volume_get_gain(event));
624                             break;
625                         case HFP_SUBEVENT_MICROPHONE_VOLUME:
626                             printf("Microphone volume: gain %u\n",
627                             hfp_subevent_microphone_volume_get_gain(event));
628                             break;
629                         case HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION:
630                             printf("Caller ID, number '%s', alpha '%s'\n", (const char *) hfp_subevent_calling_line_identification_notification_get_number(event),
631                                    (const char *) hfp_subevent_calling_line_identification_notification_get_alpha(event));
632                             break;
633                         case HFP_SUBEVENT_ENHANCED_CALL_STATUS:
634                             printf("Enhanced call status:\n");
635                             printf("  - call index: %d \n", hfp_subevent_enhanced_call_status_get_clcc_idx(event));
636                             printf("  - direction : %s \n", hfp_enhanced_call_dir2str(hfp_subevent_enhanced_call_status_get_clcc_dir(event)));
637                             printf("  - status    : %s \n", hfp_enhanced_call_status2str(hfp_subevent_enhanced_call_status_get_clcc_status(event)));
638                             printf("  - mode      : %s \n", hfp_enhanced_call_mode2str(hfp_subevent_enhanced_call_status_get_clcc_mode(event)));
639                             printf("  - multipart : %s \n", hfp_enhanced_call_mpty2str(hfp_subevent_enhanced_call_status_get_clcc_mpty(event)));
640                             printf("  - type      : %d \n", hfp_subevent_enhanced_call_status_get_bnip_type(event));
641                             printf("  - number    : %s \n", hfp_subevent_enhanced_call_status_get_bnip_number(event));
642                             break;
643 
644                         case HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED:
645                             status = hfp_subevent_voice_recognition_activated_get_status(event);
646                             if (status != ERROR_CODE_SUCCESS){
647                                 printf("Voice Recognition Activate command failed, status 0x%02x\n", status);
648                                 break;
649                             }
650 
651                             switch (hfp_subevent_voice_recognition_activated_get_enhanced(event)){
652                                 case 0:
653                                     printf("\nVoice recognition ACTIVATED\n\n");
654                                     break;
655                                 default:
656                                     printf("\nEnhanced voice recognition ACTIVATED.\n");
657                                     printf("Start new audio enhanced voice recognition session %s\n\n", bd_addr_to_str(device_addr));
658                                     status = hfp_hf_enhanced_voice_recognition_report_ready_for_audio(acl_handle);
659                                     break;
660                             }
661                             break;
662 
663                         case HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED:
664                             status = hfp_subevent_voice_recognition_deactivated_get_status(event);
665                             if (status != ERROR_CODE_SUCCESS){
666                                 printf("Voice Recognition Deactivate command failed, status 0x%02x\n", status);
667                                 break;
668                             }
669                             printf("\nVoice Recognition DEACTIVATED\n\n");
670                             break;
671 
672                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO:
673                             status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
674                             report_status(status, "Enhanced Voice recognition: READY FOR AUDIO");
675                             break;
676 
677                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT:
678                             printf("\nEnhanced Voice recognition AG status: AG READY TO ACCEPT AUDIO INPUT\n\n");
679                             break;
680                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND:
681                             printf("\nEnhanced Voice recognition AG status: AG IS STARTING SOUND\n\n");
682                             break;
683                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT:
684                             printf("\nEnhanced Voice recognition AG status: AG IS PROCESSING AUDIO INPUT\n\n");
685                             break;
686 
687                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE:
688                             printf("\nEnhanced Voice recognition AG message: \'%s\'\n", hfp_subevent_enhanced_voice_recognition_ag_message_get_text(event));
689                             break;
690 
691                         case HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE:
692                             status = hfp_subevent_echo_canceling_and_noise_reduction_deactivate_get_status(event);
693                             report_status(status, "Echo Canceling and Noise Reduction Deactivate");
694                             break;
695                         default:
696                             break;
697                     }
698                     break;
699 
700                 default:
701                     break;
702             }
703             break;
704 
705         default:
706             break;
707     }
708 
709 }
710 
711 /* @section Main Application Setup
712  *
713  * @text Listing MainConfiguration shows main application code.
714  * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it.
715  * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers.
716  * The stdin_process callback allows for sending commands to the HFP AG.
717  * At the end the Bluetooth stack is started.
718  */
719 
720 /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */
721 int btstack_main(int argc, const char * argv[]);
722 int btstack_main(int argc, const char * argv[]){
723     (void)argc;
724     (void)argv;
725 
726     // Init protocols
727     // init L2CAP
728     l2cap_init();
729     rfcomm_init();
730     sdp_init();
731 #ifdef ENABLE_BLE
732     // Initialize LE Security Manager. Needed for cross-transport key derivation
733     sm_init();
734 #endif
735 
736     // Init profiles
737     uint16_t hf_supported_features          =
738         (1<<HFP_HFSF_ESCO_S4)               |
739         (1<<HFP_HFSF_CLI_PRESENTATION_CAPABILITY) |
740         (1<<HFP_HFSF_HF_INDICATORS)         |
741         (1<<HFP_HFSF_CODEC_NEGOTIATION)     |
742         (1<<HFP_HFSF_ENHANCED_CALL_STATUS)  |
743         (1<<HFP_HFSF_VOICE_RECOGNITION_FUNCTION)  |
744         (1<<HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
745         (1<<HFP_HFSF_VOICE_RECOGNITION_TEXT) |
746         (1<<HFP_HFSF_EC_NR_FUNCTION) |
747         (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL);
748     int wide_band_speech = 1;
749     hfp_hf_init(rfcomm_channel_nr);
750     hfp_hf_init_supported_features(hf_supported_features);
751     hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
752     hfp_hf_init_codecs(sizeof(codecs), codecs);
753     hfp_hf_register_packet_handler(hfp_hf_packet_handler);
754 
755 
756     // Configure SDP
757 
758     // - Create and register HFP HF service record
759     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
760     hfp_hf_create_sdp_record(hfp_service_buffer, sdp_create_service_record_handle(),
761                              rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech);
762     printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer));
763     sdp_register_service(hfp_service_buffer);
764 
765     // Configure GAP - discovery / connection
766 
767     // - Set local name with a template Bluetooth address, that will be automatically
768     //   replaced with an actual address once it is available, i.e. when BTstack boots
769     //   up and starts talking to a Bluetooth module.
770     gap_set_local_name("HFP HF Demo 00:00:00:00:00:00");
771 
772     // - Allow to show up in Bluetooth inquiry
773     gap_discoverable_control(1);
774 
775     // - Set Class of Device - Service Class: Audio, Major Device Class: Audio, Minor: Hands-Free device
776     gap_set_class_of_device(0x200408);
777 
778     // - Allow for role switch in general and sniff mode
779     gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
780 
781     // - Allow for role switch on outgoing connections - this allows HFP AG, e.g. smartphone, to become master when we re-connect to it
782     gap_set_allow_role_switch(true);
783 
784     // Register for HCI events and SCO packets
785     hci_event_callback_registration.callback = &hci_packet_handler;
786     hci_add_event_handler(&hci_event_callback_registration);
787     hci_register_sco_packet_handler(&hci_packet_handler);
788 
789 
790     // Init SCO / HFP audio processing
791     sco_demo_init();
792 
793 #ifdef HAVE_BTSTACK_STDIN
794     // parse human readable Bluetooth address
795     sscanf_bd_addr(device_addr_string, device_addr);
796     btstack_stdin_setup(stdin_process);
797 #endif
798 
799     // turn on!
800     hci_power_control(HCI_POWER_ON);
801     return 0;
802 }
803 /* LISTING_END */
804 /* EXAMPLE_END */
805