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