xref: /btstack/example/hfp_ag_demo.c (revision cf75be853470858fd5258f077fae9b5179159329)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hfp_ag_demo.c"
39 
40 /*
41  * hfp_ag_demo.c
42  */
43 
44 // *****************************************************************************
45 /* EXAMPLE_START(hfp_ag_demo): HFP AG - Audio Gateway
46  *
47  * @text This HFP Audio Gateway example demonstrates how to receive
48  * an output from a remote HFP Hands-Free (HF) unit, and,
49  * if HAVE_BTSTACK_STDIN is defined, how to control the HFP HF.
50  */
51 // *****************************************************************************
52 
53 
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 
59 #include "btstack.h"
60 #include "sco_demo_util.h"
61 #ifdef HAVE_BTSTACK_STDIN
62 #include "btstack_stdin.h"
63 #endif
64 
65 // uncomment to temp disable mSBC codec
66 // #undef ENABLE_HFP_WIDE_BAND_SPEECH
67 
68 uint8_t hfp_service_buffer[150];
69 const uint8_t    rfcomm_channel_nr = 1;
70 const char hfp_ag_service_name[] = "HFP AG Demo";
71 
72 static bd_addr_t device_addr;
73 static const char * device_addr_string = "00:1A:7D:DA:71:13";
74 
75 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
76 static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
77 #else
78 static uint8_t codecs[] = {HFP_CODEC_CVSD};
79 #endif
80 
81 static uint8_t negotiated_codec = HFP_CODEC_CVSD;
82 
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 static int memory_1_enabled = 1;
86 static btstack_packet_callback_registration_t hci_event_callback_registration;
87 
88 static int ag_indicators_nr = 7;
89 static hfp_ag_indicator_t ag_indicators[] = {
90     // index, name, min range, max range, status, mandatory, enabled, status changed
91     {1, "service",   0, 1, 1, 0, 0, 0},
92     {2, "call",      0, 1, 0, 1, 1, 0},
93     {3, "callsetup", 0, 3, 0, 1, 1, 0},
94     {4, "battchg",   0, 5, 3, 0, 0, 0},
95     {5, "signal",    0, 5, 5, 0, 1, 0},
96     {6, "roam",      0, 1, 0, 0, 1, 0},
97     {7, "callheld",  0, 2, 0, 1, 1, 0}
98 };
99 
100 static int call_hold_services_nr = 5;
101 static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"};
102 
103 static int hf_indicators_nr = 2;
104 static hfp_generic_status_indicator_t hf_indicators[] = {
105     {1, 1},
106     {2, 1},
107 };
108 
109 #define INQUIRY_INTERVAL 5
110 
111 enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
112 enum STATE state = INIT;
113 
114 static void dump_supported_codecs(void){
115     unsigned int i;
116     int mSBC_skipped = 0;
117     printf("Supported codecs: ");
118     for (i = 0; i < sizeof(codecs); i++){
119         switch(codecs[i]){
120             case HFP_CODEC_CVSD:
121                 printf("CVSD");
122                 break;
123             case HFP_CODEC_MSBC:
124                 if (hci_extended_sco_link_supported()){
125                     printf(", mSBC");
126                 } else {
127                     mSBC_skipped = 1;
128                 }
129                 break;
130             default:
131                 btstack_assert(false);
132                 break;
133         }
134     }
135     printf("\n");
136     if (mSBC_skipped){
137         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
138     }
139 }
140 
141 #ifdef HAVE_BTSTACK_STDIN
142 // Testig User Interface
143 static void show_usage(void){
144     bd_addr_t iut_address;
145     gap_local_bd_addr(iut_address);
146 
147     printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
148     printf("\n");
149 
150     printf("a - establish HFP connection to %s\n", bd_addr_to_str(device_addr));
151     // printf("A - release HFP connection\n");
152 
153     printf("b - establish AUDIO connection          | B - release AUDIO connection\n");
154     printf("c - simulate incoming call from 1234567 | C - simulate call from 1234567 dropped\n");
155     printf("d - report AG failure\n");
156     printf("D - delete all link keys\n");
157     printf("e - answer call on AG                   | E - reject call on AG\n");
158     printf("r - disable in-band ring tone           | R - enable in-band ring tone\n");
159     printf("f - Disable cellular network            | F - Enable cellular network\n");
160     printf("g - Set signal strength to 0            | G - Set signal strength to 5\n");
161     printf("h - Disable roaming                     | H - Enable roaming\n");
162     printf("i - Set battery level to 3              | I - Set battery level to 5\n");
163     printf("j - Answering call on remote side\n");
164     printf("k - Clear memory #1                     | K - Set memory #1\n");
165     printf("l - Clear last number                   | L - Set last number\n");
166     printf("m - simulate incoming call from 7654321\n");
167     // printf("M - simulate call from 7654321 dropped\n");
168     printf("n - Disable Voice Recognition           | N - Enable Voice Recognition\n");
169     printf("z - Disable Enhanced Voice Recognition  | Z - Enable Enhanced Voice Recognition\n");
170     printf("1 - EVR report starting sound           | 2 - EVR report ready for input\n");
171     printf("3 - EVR report processing input         | 4 - EVR report message\n");
172 
173     printf("o - Set speaker volume to 0  (minimum)  | O - Set speaker volume to 9  (default)\n");
174     printf("p - Set speaker volume to 12 (higher)   | P - Set speaker volume to 15 (maximum)\n");
175     printf("q - Set microphone gain to 0  (minimum) | Q - Set microphone gain to 9  (default)\n");
176     printf("s - Set microphone gain to 12 (higher)  | S - Set microphone gain to 15 (maximum)\n");
177     printf("t - terminate connection\n");
178     printf("u - join held call\n");
179     printf("v - discover nearby HF units\n");
180     printf("w - put incoming call on hold (Response and Hold)\n");
181     printf("x - accept held incoming call (Response and Hold)\n");
182     printf("X - reject held incoming call (Response and Hold)\n");
183     printf("---\n");
184 }
185 
186 static void stdin_process(char cmd){
187     uint8_t status = ERROR_CODE_SUCCESS;
188 
189     switch (cmd){
190         case 'a':
191             log_info("USER:\'%c\'", cmd);
192             printf("Establish HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
193             status = hfp_ag_establish_service_level_connection(device_addr);
194             break;
195         case 'A':
196             log_info("USER:\'%c\'", cmd);
197             printf("Release HFP service level connection.\n");
198             status = hfp_ag_release_service_level_connection(acl_handle);
199             break;
200 
201         case 'b':
202             log_info("USER:\'%c\'", cmd);
203             printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
204             status = hfp_ag_establish_audio_connection(acl_handle);
205             break;
206         case 'B':
207             log_info("USER:\'%c\'", cmd);
208             printf("Release Audio connection.\n");
209             status = hfp_ag_release_audio_connection(acl_handle);
210             break;
211         case 'c':
212             log_info("USER:\'%c\'", cmd);
213             printf("Simulate incoming call from 1234567\n");
214             hfp_ag_set_clip(129, "1234567");
215             hfp_ag_incoming_call();
216             break;
217         case 'D':
218             printf("Deleting all link keys\n");
219             gap_delete_all_link_keys();
220             break;
221         case 'm':
222             log_info("USER:\'%c\'", cmd);
223             printf("Simulate incoming call from 7654321\n");
224             hfp_ag_set_clip(129, "7654321");
225             hfp_ag_incoming_call();
226             break;
227         case 'C':
228             log_info("USER:\'%c\'", cmd);
229             printf("Simulate terminate call\n");
230             hfp_ag_call_dropped();
231             break;
232         case 'd':
233             log_info("USER:\'%c\'", cmd);
234             printf("Report AG failure\n");
235             status = hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
236             break;
237         case 'e':
238             log_info("USER:\'%c\'", cmd);
239             printf("Answer call on AG\n");
240             hfp_ag_answer_incoming_call();
241             break;
242         case 'E':
243             log_info("USER:\'%c\'", cmd);
244             printf("Reject call on AG\n");
245             hfp_ag_terminate_call();
246             break;
247         case 'f':
248             log_info("USER:\'%c\'", cmd);
249             printf("Disable cellular network\n");
250             status = hfp_ag_set_registration_status(0);
251             break;
252         case 'F':
253             log_info("USER:\'%c\'", cmd);
254             printf("Enable cellular network\n");
255             status = hfp_ag_set_registration_status(1);
256             break;
257         case 'g':
258             log_info("USER:\'%c\'", cmd);
259             printf("Set signal strength to 0\n");
260             status = hfp_ag_set_signal_strength(0);
261             break;
262         case 'G':
263             log_info("USER:\'%c\'", cmd);
264             printf("Set signal strength to 5\n");
265             status = hfp_ag_set_signal_strength(5);
266             break;
267         case 'h':
268             log_info("USER:\'%c\'", cmd);
269             printf("Disable roaming\n");
270             status = hfp_ag_set_roaming_status(0);
271             break;
272         case 'H':
273             log_info("USER:\'%c\'", cmd);
274             printf("Enable roaming\n");
275             status = hfp_ag_set_roaming_status(1);
276             break;
277         case 'i':
278             log_info("USER:\'%c\'", cmd);
279             printf("Set battery level to 3\n");
280             status = hfp_ag_set_battery_level(3);
281             break;
282         case 'I':
283             log_info("USER:\'%c\'", cmd);
284             printf("Set battery level to 5\n");
285             status = hfp_ag_set_battery_level(5);
286             break;
287         case 'j':
288             log_info("USER:\'%c\'", cmd);
289             printf("Answering call on remote side\n");
290             hfp_ag_outgoing_call_established();
291             break;
292         case 'r':
293             log_info("USER:\'%c\'", cmd);
294             printf("Disable in-band ring tone\n");
295             hfp_ag_set_use_in_band_ring_tone(0);
296             break;
297         case 'k':
298             log_info("USER:\'%c\'", cmd);
299             printf("Memory 1 cleared\n");
300             memory_1_enabled = 0;
301             break;
302         case 'K':
303             log_info("USER:\'%c\'", cmd);
304             printf("Memory 1 set\n");
305             memory_1_enabled = 1;
306             break;
307         case 'l':
308             log_info("USER:\'%c\'", cmd);
309             printf("Last dialed number cleared\n");
310             hfp_ag_clear_last_dialed_number();
311             break;
312         case 'L':
313             log_info("USER:\'%c\'", cmd);
314             printf("Outgoing call connected, ringing\n");
315             hfp_ag_outgoing_call_ringing();
316             break;
317         case 'n':
318             log_info("USER:\'%c\'", cmd);
319             printf("Disable Voice Recognition\n");
320             status = hfp_ag_deactivate_voice_recognition(acl_handle);
321             break;
322         case 'N':
323             log_info("USER:\'%c\'", cmd);
324             printf("Enable Voice Recognition\n");
325             status = hfp_ag_activate_voice_recognition(acl_handle);
326             break;
327 
328         case 'z':
329             log_info("USER:\'%c\'", cmd);
330             printf("Disable Enhanced Voice Recognition\n");
331             status = hfp_ag_deactivate_enhanced_voice_recognition(acl_handle);
332             break;
333         case 'Z':
334             log_info("USER:\'%c\'", cmd);
335             printf("Enable Enhanced_Voice Recognition\n");
336             status = hfp_ag_activate_enhanced_voice_recognition(acl_handle);
337             break;
338 
339         case '1':
340             log_info("USER:\'%c\'", cmd);
341             printf("Enhanced_Voice Recognition: report starting sound\n");
342             status = hfp_ag_enhanced_voice_recognition_report_starting_sound(acl_handle);
343             break;
344         case '2':
345             log_info("USER:\'%c\'", cmd);
346             printf("Enhanced_Voice Recognition: report ready for input\n");
347             status = hfp_ag_enhanced_voice_recognition_report_ready_for_input(acl_handle);
348             break;
349         case '3':
350             log_info("USER:\'%c\'", cmd);
351             printf("Enhanced_Voice Recognition: report processing input\n");
352             status = hfp_ag_enhanced_voice_recognition_report_processing_input(acl_handle);
353             break;
354         case '4':
355             log_info("USER:\'%c\'", cmd);
356             printf("Enhanced_Voice Recognition: report message\n");
357             // status = hfp_ag_enhanced_voice_recognition_report_message(acl_handle);
358             break;
359 
360         case 'o':
361             log_info("USER:\'%c\'", cmd);
362             printf("Set speaker gain to 0 (minimum)\n");
363             status = hfp_ag_set_speaker_gain(acl_handle, 0);
364             break;
365         case 'O':
366             log_info("USER:\'%c\'", cmd);
367             printf("Set speaker gain to 9 (default)\n");
368             status = hfp_ag_set_speaker_gain(acl_handle, 9);
369             break;
370         case 'p':
371             log_info("USER:\'%c\'", cmd);
372             printf("Set speaker gain to 12 (higher)\n");
373             status = hfp_ag_set_speaker_gain(acl_handle, 12);
374             break;
375         case 'P':
376             log_info("USER:\'%c\'", cmd);
377             printf("Set speaker gain to 15 (maximum)\n");
378             status = hfp_ag_set_speaker_gain(acl_handle, 15);
379             break;
380         case 'q':
381             log_info("USER:\'%c\'", cmd);
382             printf("Set microphone gain to 0\n");
383             status = hfp_ag_set_microphone_gain(acl_handle, 0);
384             break;
385         case 'Q':
386             log_info("USER:\'%c\'", cmd);
387             printf("Set microphone gain to 9\n");
388             status = hfp_ag_set_microphone_gain(acl_handle, 9);
389             break;
390         case 's':
391             log_info("USER:\'%c\'", cmd);
392             printf("Set microphone gain to 12\n");
393             status = hfp_ag_set_microphone_gain(acl_handle, 12);
394             break;
395         case 'S':
396             log_info("USER:\'%c\'", cmd);
397             printf("Set microphone gain to 15\n");
398             status = hfp_ag_set_microphone_gain(acl_handle, 15);
399             break;
400         case 'R':
401             log_info("USER:\'%c\'", cmd);
402             printf("Enable in-band ring tone\n");
403             hfp_ag_set_use_in_band_ring_tone(1);
404             break;
405         case 't':
406             log_info("USER:\'%c\'", cmd);
407             printf("Terminate HCI connection. 0x%2x\n", acl_handle);
408             gap_disconnect(acl_handle);
409             break;
410         case 'u':
411             log_info("USER:\'%c\'", cmd);
412             printf("Join held call\n");
413             hfp_ag_join_held_call();
414             break;
415         case 'v':
416             printf("Start scanning...\n");
417             gap_inquiry_start(INQUIRY_INTERVAL);
418             break;
419         case 'w':
420             log_info("USER:\'%c\'", cmd);
421             printf("AG: Put incoming call on hold (Response and Hold)\n");
422             hfp_ag_hold_incoming_call();
423             break;
424         case 'x':
425             log_info("USER:\'%c\'", cmd);
426             printf("AG: Accept held incoming call (Response and Hold)\n");
427             hfp_ag_accept_held_incoming_call();
428             break;
429         case 'X':
430             log_info("USER:\'%c\'", cmd);
431             printf("AG: Reject held incoming call (Response and Hold)\n");
432             hfp_ag_reject_held_incoming_call();
433             break;
434         default:
435             show_usage();
436             break;
437     }
438 
439     if (status != ERROR_CODE_SUCCESS){
440         printf("Could not perform command, status 0x%02x\n", status);
441     }
442 }
443 #endif
444 
445 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
446     UNUSED(channel);
447     bd_addr_t addr;
448     uint8_t status;
449     switch (packet_type){
450         case HCI_EVENT_PACKET:
451             switch(hci_event_packet_get_type(event)){
452 #ifndef HAVE_BTSTACK_STDIN
453                 case BTSTACK_EVENT_STATE:
454                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
455                     printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr));
456                     hfp_ag_establish_service_level_connection(device_addr);
457                     break;
458 #endif
459                 case GAP_EVENT_INQUIRY_RESULT:
460                     gap_event_inquiry_result_get_bd_addr(event, addr);
461                     // print info
462                     printf("Device found: %s ",  bd_addr_to_str(addr));
463                     printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
464                     if (gap_event_inquiry_result_get_rssi_available(event)){
465                         printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
466                     }
467                     if (gap_event_inquiry_result_get_name_available(event)){
468                         char name_buffer[240];
469                         int name_len = gap_event_inquiry_result_get_name_len(event);
470                         memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
471                         name_buffer[name_len] = 0;
472                         printf(", name '%s'", name_buffer);
473                     }
474                     printf("\n");
475                     break;
476                 case GAP_EVENT_INQUIRY_COMPLETE:
477                     printf("Inquiry scan complete.\n");
478                     break;
479                 case HCI_EVENT_SCO_CAN_SEND_NOW:
480                     sco_demo_send(sco_handle);
481                     break;
482                 case HCI_EVENT_COMMAND_COMPLETE:
483                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
484                         dump_supported_codecs();
485                     }
486                     break;
487                 default:
488                     break;
489             }
490 
491             if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return;
492 
493             switch (hci_event_hfp_meta_get_subevent_code(event)) {
494                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
495                     status = hfp_subevent_service_level_connection_established_get_status(event);
496                     if (status != ERROR_CODE_SUCCESS){
497                         printf("Connection failed, status 0x%02x\n", status);
498                         break;
499                     }
500                     acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
501                     hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
502                     printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr));
503                     dump_supported_codecs();
504 #ifndef HAVE_BTSTACK_STDIN
505                     log_info("Establish Audio connection %s", bd_addr_to_str(device_addr));
506                     printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
507                     hfp_ag_establish_audio_connection(acl_handle);
508 #endif
509                     break;
510                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
511                     printf("Service level connection released.\n");
512                     acl_handle = HCI_CON_HANDLE_INVALID;
513                     break;
514                 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
515                     if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){
516                         printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
517                     } else {
518                         sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
519                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
520                         negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
521                         switch (negotiated_codec){
522                             case 0x01:
523                                 printf("Using CVSD codec.\n");
524                                 break;
525                             case 0x02:
526                                 printf("Using mSBC codec.\n");
527                                 break;
528                             default:
529                                 printf("Using unknown codec 0x%02x.\n", negotiated_codec);
530                                 break;
531                         }
532                         sco_demo_set_codec(negotiated_codec);
533                         hci_request_sco_can_send_now_event();
534                     }
535                     break;
536                 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
537                     printf("Audio connection released\n");
538                     sco_handle = HCI_CON_HANDLE_INVALID;
539                     sco_demo_close();
540                     break;
541                 case HFP_SUBEVENT_START_RINGINIG:
542                     printf("Start Ringing\n");
543                     break;
544                 case HFP_SUBEVENT_STOP_RINGINIG:
545                     printf("Stop Ringing\n");
546                     break;
547                 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
548                     printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
549                     // validate number
550                     if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
551                       || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
552                       || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
553                         printf("Dial string valid: accept call\n");
554                         hfp_ag_outgoing_call_accepted();
555                     } else {
556                         printf("Dial string invalid: reject call\n");
557                         hfp_ag_outgoing_call_rejected();
558                     }
559                     break;
560 
561                 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
562                     printf("Attach number to voice tag. Sending '1234567\n");
563                     hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
564                     break;
565                 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
566                     printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
567                     hfp_ag_send_dtmf_code_done(acl_handle);
568                     break;
569                 case HFP_SUBEVENT_CALL_ANSWERED:
570                     printf("Call answered by HF\n");
571                     break;
572 
573                 case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS:
574                     status = hfp_subevent_voice_recognition_status_get_status(event);
575                     if (status != ERROR_CODE_SUCCESS){
576                         printf("Voice Recognition command failed\n");
577                         break;
578                     }
579                     switch(hfp_subevent_voice_recognition_status_get_state(event)){
580                         case 0:
581                             printf("\nVoice recognition status DEACTIVATED\n\n");
582                             break;
583                         case 1:
584                             printf("\nVoice recognition status ACTIVATED\n\n");
585                             break;
586                         default:
587                             btstack_assert(false);
588                             break;
589                     }
590                     break;
591 
592                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO:
593                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
594                     if (status != ERROR_CODE_SUCCESS){
595                         printf("Enhanced Voice recognition READY FOR AUDIO cmd failed\n");
596                         break;
597                     }
598                     printf("\nEnhanced Voice recognition status READY FOR AUDIO\n\n");
599                     break;
600 
601                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT:
602                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
603                     if (status != ERROR_CODE_SUCCESS){
604                         printf("Enhanced Voice recognition AG READY TO ACCEPT AUDIO INPU cmd failed\n");
605                         break;
606                     }
607                     printf("\nEnhanced Voice recognition AG status: AG READY TO ACCEPT AUDIO INPUT\n\n");
608                     break;
609 
610                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND:
611                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
612                     if (status != ERROR_CODE_SUCCESS){
613                         printf("Enhanced Voice recognition AG IS STARTING SOUND cmd failed\n");
614                         break;
615                     }
616                     printf("\nEnhanced Voice recognition AG status: AG IS STARTING SOUND\n\n");
617                     break;
618 
619                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT:
620                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
621                     if (status != ERROR_CODE_SUCCESS){
622                         printf("Enhanced Voice recognition AG IS PROCESSING AUDIO INPUT cmd failed\n");
623                         break;
624                     }
625                     printf("\nEnhanced Voice recognition AG status: AG IS PROCESSING AUDIO INPUT\n\n");
626                     break;
627 
628                 default:
629                     break;
630             }
631             break;
632         case HCI_SCO_DATA_PACKET:
633             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
634             sco_demo_receive(event, event_size);
635             break;
636         default:
637             break;
638     }
639 }
640 
641 static hfp_phone_number_t subscriber_number = {
642     129, "225577"
643 };
644 
645 /* @section Main Application Setup
646  *
647  * @text Listing MainConfiguration shows main application code.
648  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
649  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
650  * The stdin_process callback allows for sending commands to the HFP HF.
651  * At the end the Bluetooth stack is started.
652  */
653 
654 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
655 
656 int btstack_main(int argc, const char * argv[]);
657 int btstack_main(int argc, const char * argv[]){
658     (void)argc;
659     (void)argv;
660 
661     sco_demo_init();
662 
663     gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
664     gap_discoverable_control(1);
665 
666     // L2CAP
667     l2cap_init();
668 
669     uint16_t supported_features                   =
670         (1<<HFP_AGSF_ESCO_S4)                     |
671         (1<<HFP_AGSF_HF_INDICATORS)               |
672         (1<<HFP_AGSF_CODEC_NEGOTIATION)           |
673         (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
674         (1<<HFP_AGSF_ENHANCED_CALL_CONTROL)       |
675         (1<<HFP_AGSF_ENHANCED_CALL_STATUS)        |
676         (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL)    |
677         (1<<HFP_AGSF_IN_BAND_RING_TONE)           |
678         (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
679         (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
680         (1<<HFP_AGSF_THREE_WAY_CALLING);
681     int wide_band_speech = 1;
682 
683     // HFP
684     rfcomm_init();
685     hfp_ag_init(rfcomm_channel_nr);
686     hfp_ag_init_supported_features(supported_features);
687     hfp_ag_init_codecs(sizeof(codecs), codecs);
688     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
689     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
690     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
691     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
692 
693     // SDP Server
694     sdp_init();
695     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
696     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
697     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
698     sdp_register_service(hfp_service_buffer);
699 
700     // register for HCI events and SCO packets
701     hci_event_callback_registration.callback = &packet_handler;
702     hci_add_event_handler(&hci_event_callback_registration);
703     hci_register_sco_packet_handler(&packet_handler);
704 
705     // register for HFP events
706     hfp_ag_register_packet_handler(&packet_handler);
707 
708     // parse human readable Bluetooth address
709     sscanf_bd_addr(device_addr_string, device_addr);
710 
711 #ifdef HAVE_BTSTACK_STDIN
712     btstack_stdin_setup(stdin_process);
713 #endif
714     // turn on!
715     hci_power_control(HCI_POWER_ON);
716     return 0;
717 }
718 /* LISTING_END */
719 /* EXAMPLE_END */
720