xref: /btstack/example/hfp_ag_demo.c (revision de9e0ea7a74966417a0fd17ac6486a0d1ada55c5)
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 Ehnaced Voice Recognition\n");
170     printf("o - Set speaker volume to 0  (minimum)  | O - Set speaker volume to 9  (default)\n");
171     printf("p - Set speaker volume to 12 (higher)   | P - Set speaker volume to 15 (maximum)\n");
172     printf("q - Set microphone gain to 0  (minimum) | Q - Set microphone gain to 9  (default)\n");
173     printf("s - Set microphone gain to 12 (higher)  | S - Set microphone gain to 15 (maximum)\n");
174     printf("t - terminate connection\n");
175     printf("u - join held call\n");
176     printf("v - discover nearby HF units\n");
177     printf("w - put incoming call on hold (Response and Hold)\n");
178     printf("x - accept held incoming call (Response and Hold)\n");
179     printf("X - reject held incoming call (Response and Hold)\n");
180     printf("---\n");
181 }
182 
183 static void stdin_process(char cmd){
184     uint8_t status = ERROR_CODE_SUCCESS;
185 
186     switch (cmd){
187         case 'a':
188             log_info("USER:\'%c\'", cmd);
189             printf("Establish HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
190             status = hfp_ag_establish_service_level_connection(device_addr);
191             break;
192         case 'A':
193             log_info("USER:\'%c\'", cmd);
194             printf("Release HFP service level connection.\n");
195             status = hfp_ag_release_service_level_connection(acl_handle);
196             break;
197 
198         case 'b':
199             log_info("USER:\'%c\'", cmd);
200             printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
201             status = hfp_ag_establish_audio_connection(acl_handle);
202             break;
203         case 'B':
204             log_info("USER:\'%c\'", cmd);
205             printf("Release Audio connection.\n");
206             status = hfp_ag_release_audio_connection(acl_handle);
207             break;
208         case 'c':
209             log_info("USER:\'%c\'", cmd);
210             printf("Simulate incoming call from 1234567\n");
211             hfp_ag_set_clip(129, "1234567");
212             hfp_ag_incoming_call();
213             break;
214         case 'D':
215             printf("Deleting all link keys\n");
216             gap_delete_all_link_keys();
217             break;
218         case 'm':
219             log_info("USER:\'%c\'", cmd);
220             printf("Simulate incoming call from 7654321\n");
221             hfp_ag_set_clip(129, "7654321");
222             hfp_ag_incoming_call();
223             break;
224         case 'C':
225             log_info("USER:\'%c\'", cmd);
226             printf("Simulate terminate call\n");
227             hfp_ag_call_dropped();
228             break;
229         case 'd':
230             log_info("USER:\'%c\'", cmd);
231             printf("Report AG failure\n");
232             status = hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
233             break;
234         case 'e':
235             log_info("USER:\'%c\'", cmd);
236             printf("Answer call on AG\n");
237             hfp_ag_answer_incoming_call();
238             break;
239         case 'E':
240             log_info("USER:\'%c\'", cmd);
241             printf("Reject call on AG\n");
242             hfp_ag_terminate_call();
243             break;
244         case 'f':
245             log_info("USER:\'%c\'", cmd);
246             printf("Disable cellular network\n");
247             status = hfp_ag_set_registration_status(0);
248             break;
249         case 'F':
250             log_info("USER:\'%c\'", cmd);
251             printf("Enable cellular network\n");
252             status = hfp_ag_set_registration_status(1);
253             break;
254         case 'g':
255             log_info("USER:\'%c\'", cmd);
256             printf("Set signal strength to 0\n");
257             status = hfp_ag_set_signal_strength(0);
258             break;
259         case 'G':
260             log_info("USER:\'%c\'", cmd);
261             printf("Set signal strength to 5\n");
262             status = hfp_ag_set_signal_strength(5);
263             break;
264         case 'h':
265             log_info("USER:\'%c\'", cmd);
266             printf("Disable roaming\n");
267             status = hfp_ag_set_roaming_status(0);
268             break;
269         case 'H':
270             log_info("USER:\'%c\'", cmd);
271             printf("Enable roaming\n");
272             status = hfp_ag_set_roaming_status(1);
273             break;
274         case 'i':
275             log_info("USER:\'%c\'", cmd);
276             printf("Set battery level to 3\n");
277             status = hfp_ag_set_battery_level(3);
278             break;
279         case 'I':
280             log_info("USER:\'%c\'", cmd);
281             printf("Set battery level to 5\n");
282             status = hfp_ag_set_battery_level(5);
283             break;
284         case 'j':
285             log_info("USER:\'%c\'", cmd);
286             printf("Answering call on remote side\n");
287             hfp_ag_outgoing_call_established();
288             break;
289         case 'r':
290             log_info("USER:\'%c\'", cmd);
291             printf("Disable in-band ring tone\n");
292             hfp_ag_set_use_in_band_ring_tone(0);
293             break;
294         case 'k':
295             log_info("USER:\'%c\'", cmd);
296             printf("Memory 1 cleared\n");
297             memory_1_enabled = 0;
298             break;
299         case 'K':
300             log_info("USER:\'%c\'", cmd);
301             printf("Memory 1 set\n");
302             memory_1_enabled = 1;
303             break;
304         case 'l':
305             log_info("USER:\'%c\'", cmd);
306             printf("Last dialed number cleared\n");
307             hfp_ag_clear_last_dialed_number();
308             break;
309         case 'L':
310             log_info("USER:\'%c\'", cmd);
311             printf("Outgoing call connected, ringing\n");
312             hfp_ag_outgoing_call_ringing();
313             break;
314         case 'n':
315             log_info("USER:\'%c\'", cmd);
316             printf("Disable Voice Recognition\n");
317             status = hfp_ag_deactivate_voice_recognition(acl_handle);
318             break;
319         case 'N':
320             log_info("USER:\'%c\'", cmd);
321             printf("Enable Voice Recognition\n");
322             status = hfp_ag_activate_voice_recognition(acl_handle);
323             break;
324 
325         case 'z':
326             log_info("USER:\'%c\'", cmd);
327             printf("Disable Enhanced Voice Recognition\n");
328             status = hfp_ag_deactivate_enhanced_voice_recognition(acl_handle);
329             break;
330         case 'Z':
331             log_info("USER:\'%c\'", cmd);
332             printf("Enable Enhanced_Voice Recognition\n");
333             status = hfp_ag_activate_enhanced_voice_recognition(acl_handle);
334             break;
335 
336         case 'o':
337             log_info("USER:\'%c\'", cmd);
338             printf("Set speaker gain to 0 (minimum)\n");
339             status = hfp_ag_set_speaker_gain(acl_handle, 0);
340             break;
341         case 'O':
342             log_info("USER:\'%c\'", cmd);
343             printf("Set speaker gain to 9 (default)\n");
344             status = hfp_ag_set_speaker_gain(acl_handle, 9);
345             break;
346         case 'p':
347             log_info("USER:\'%c\'", cmd);
348             printf("Set speaker gain to 12 (higher)\n");
349             status = hfp_ag_set_speaker_gain(acl_handle, 12);
350             break;
351         case 'P':
352             log_info("USER:\'%c\'", cmd);
353             printf("Set speaker gain to 15 (maximum)\n");
354             status = hfp_ag_set_speaker_gain(acl_handle, 15);
355             break;
356         case 'q':
357             log_info("USER:\'%c\'", cmd);
358             printf("Set microphone gain to 0\n");
359             status = hfp_ag_set_microphone_gain(acl_handle, 0);
360             break;
361         case 'Q':
362             log_info("USER:\'%c\'", cmd);
363             printf("Set microphone gain to 9\n");
364             status = hfp_ag_set_microphone_gain(acl_handle, 9);
365             break;
366         case 's':
367             log_info("USER:\'%c\'", cmd);
368             printf("Set microphone gain to 12\n");
369             status = hfp_ag_set_microphone_gain(acl_handle, 12);
370             break;
371         case 'S':
372             log_info("USER:\'%c\'", cmd);
373             printf("Set microphone gain to 15\n");
374             status = hfp_ag_set_microphone_gain(acl_handle, 15);
375             break;
376         case 'R':
377             log_info("USER:\'%c\'", cmd);
378             printf("Enable in-band ring tone\n");
379             hfp_ag_set_use_in_band_ring_tone(1);
380             break;
381         case 't':
382             log_info("USER:\'%c\'", cmd);
383             printf("Terminate HCI connection. 0x%2x\n", acl_handle);
384             gap_disconnect(acl_handle);
385             break;
386         case 'u':
387             log_info("USER:\'%c\'", cmd);
388             printf("Join held call\n");
389             hfp_ag_join_held_call();
390             break;
391         case 'v':
392             printf("Start scanning...\n");
393             gap_inquiry_start(INQUIRY_INTERVAL);
394             break;
395         case 'w':
396             log_info("USER:\'%c\'", cmd);
397             printf("AG: Put incoming call on hold (Response and Hold)\n");
398             hfp_ag_hold_incoming_call();
399             break;
400         case 'x':
401             log_info("USER:\'%c\'", cmd);
402             printf("AG: Accept held incoming call (Response and Hold)\n");
403             hfp_ag_accept_held_incoming_call();
404             break;
405         case 'X':
406             log_info("USER:\'%c\'", cmd);
407             printf("AG: Reject held incoming call (Response and Hold)\n");
408             hfp_ag_reject_held_incoming_call();
409             break;
410         default:
411             show_usage();
412             break;
413     }
414 
415     if (status != ERROR_CODE_SUCCESS){
416         printf("Could not perform command, status 0x%02x\n", status);
417     }
418 }
419 #endif
420 
421 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
422     UNUSED(channel);
423     bd_addr_t addr;
424     uint8_t status;
425     switch (packet_type){
426         case HCI_EVENT_PACKET:
427             switch(hci_event_packet_get_type(event)){
428 #ifndef HAVE_BTSTACK_STDIN
429                 case BTSTACK_EVENT_STATE:
430                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
431                     printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr));
432                     hfp_ag_establish_service_level_connection(device_addr);
433                     break;
434 #endif
435                 case GAP_EVENT_INQUIRY_RESULT:
436                     gap_event_inquiry_result_get_bd_addr(event, addr);
437                     // print info
438                     printf("Device found: %s ",  bd_addr_to_str(addr));
439                     printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
440                     if (gap_event_inquiry_result_get_rssi_available(event)){
441                         printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
442                     }
443                     if (gap_event_inquiry_result_get_name_available(event)){
444                         char name_buffer[240];
445                         int name_len = gap_event_inquiry_result_get_name_len(event);
446                         memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
447                         name_buffer[name_len] = 0;
448                         printf(", name '%s'", name_buffer);
449                     }
450                     printf("\n");
451                     break;
452                 case GAP_EVENT_INQUIRY_COMPLETE:
453                     printf("Inquiry scan complete.\n");
454                     break;
455                 case HCI_EVENT_SCO_CAN_SEND_NOW:
456                     sco_demo_send(sco_handle);
457                     break;
458                 case HCI_EVENT_COMMAND_COMPLETE:
459                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
460                         dump_supported_codecs();
461                     }
462                     break;
463                 default:
464                     break;
465             }
466 
467             if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return;
468 
469             switch (hci_event_hfp_meta_get_subevent_code(event)) {
470                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
471                     status = hfp_subevent_service_level_connection_established_get_status(event);
472                     if (status != ERROR_CODE_SUCCESS){
473                         printf("Connection failed, status 0x%02x\n", status);
474                         break;
475                     }
476                     acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
477                     hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
478                     printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr));
479                     dump_supported_codecs();
480 #ifndef HAVE_BTSTACK_STDIN
481                     log_info("Establish Audio connection %s", bd_addr_to_str(device_addr));
482                     printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
483                     hfp_ag_establish_audio_connection(acl_handle);
484 #endif
485                     break;
486                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
487                     printf("Service level connection released.\n");
488                     acl_handle = HCI_CON_HANDLE_INVALID;
489                     break;
490                 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
491                     if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){
492                         printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
493                     } else {
494                         sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
495                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
496                         negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
497                         switch (negotiated_codec){
498                             case 0x01:
499                                 printf("Using CVSD codec.\n");
500                                 break;
501                             case 0x02:
502                                 printf("Using mSBC codec.\n");
503                                 break;
504                             default:
505                                 printf("Using unknown codec 0x%02x.\n", negotiated_codec);
506                                 break;
507                         }
508                         sco_demo_set_codec(negotiated_codec);
509                         hci_request_sco_can_send_now_event();
510                     }
511                     break;
512                 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
513                     printf("Audio connection released\n");
514                     sco_handle = HCI_CON_HANDLE_INVALID;
515                     sco_demo_close();
516                     break;
517                 case HFP_SUBEVENT_START_RINGINIG:
518                     printf("Start Ringing\n");
519                     break;
520                 case HFP_SUBEVENT_STOP_RINGINIG:
521                     printf("Stop Ringing\n");
522                     break;
523                 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
524                     printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
525                     // validate number
526                     if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
527                       || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
528                       || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
529                         printf("Dial string valid: accept call\n");
530                         hfp_ag_outgoing_call_accepted();
531                     } else {
532                         printf("Dial string invalid: reject call\n");
533                         hfp_ag_outgoing_call_rejected();
534                     }
535                     break;
536 
537                 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
538                     printf("Attach number to voice tag. Sending '1234567\n");
539                     hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
540                     break;
541                 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
542                     printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
543                     hfp_ag_send_dtmf_code_done(acl_handle);
544                     break;
545                 case HFP_SUBEVENT_CALL_ANSWERED:
546                     printf("Call answered by HF\n");
547                     break;
548 
549                 case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS:
550                     status = hfp_subevent_voice_recognition_status_get_status(event);
551                     if (status != ERROR_CODE_SUCCESS){
552                         printf("Voice Recognition command failed\n");
553                         break;
554                     }
555                     switch(hfp_subevent_voice_recognition_status_get_state(event)){
556                         case 0:
557                             printf("\nVoice recognition status DEACTIVATED\n\n");
558                             break;
559                         case 1:
560                             printf("\nVoice recognition status ACTIVATED\n\n");
561                             break;
562                         default:
563                             btstack_assert(false);
564                             break;
565                     }
566                     break;
567 
568                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO:
569                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
570                     if (status != ERROR_CODE_SUCCESS){
571                         printf("Enhanced Voice recognition READY FOR AUDIO cmd failed\n");
572                         break;
573                     }
574                     printf("\nEnhanced Voice recognition status READY FOR AUDIO\n\n");
575                     break;
576                 default:
577                     break;
578             }
579             break;
580         case HCI_SCO_DATA_PACKET:
581             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
582             sco_demo_receive(event, event_size);
583             break;
584         default:
585             break;
586     }
587 }
588 
589 static hfp_phone_number_t subscriber_number = {
590     129, "225577"
591 };
592 
593 /* @section Main Application Setup
594  *
595  * @text Listing MainConfiguration shows main application code.
596  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
597  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
598  * The stdin_process callback allows for sending commands to the HFP HF.
599  * At the end the Bluetooth stack is started.
600  */
601 
602 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
603 
604 int btstack_main(int argc, const char * argv[]);
605 int btstack_main(int argc, const char * argv[]){
606     (void)argc;
607     (void)argv;
608 
609     sco_demo_init();
610 
611     gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
612     gap_discoverable_control(1);
613 
614     // L2CAP
615     l2cap_init();
616 
617     uint16_t supported_features                   =
618         (1<<HFP_AGSF_ESCO_S4)                     |
619         (1<<HFP_AGSF_HF_INDICATORS)               |
620         (1<<HFP_AGSF_CODEC_NEGOTIATION)           |
621         (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
622         (1<<HFP_AGSF_ENHANCED_CALL_CONTROL)       |
623         (1<<HFP_AGSF_ENHANCED_CALL_STATUS)        |
624         (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL)    |
625         (1<<HFP_AGSF_IN_BAND_RING_TONE)           |
626         (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
627         (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
628         (1<<HFP_AGSF_THREE_WAY_CALLING);
629     int wide_band_speech = 1;
630 
631     // HFP
632     rfcomm_init();
633     hfp_ag_init(rfcomm_channel_nr);
634     hfp_ag_init_supported_features(supported_features);
635     hfp_ag_init_codecs(sizeof(codecs), codecs);
636     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
637     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
638     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
639     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
640 
641     // SDP Server
642     sdp_init();
643     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
644     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
645     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
646     sdp_register_service(hfp_service_buffer);
647 
648     // register for HCI events and SCO packets
649     hci_event_callback_registration.callback = &packet_handler;
650     hci_add_event_handler(&hci_event_callback_registration);
651     hci_register_sco_packet_handler(&packet_handler);
652 
653     // register for HFP events
654     hfp_ag_register_packet_handler(&packet_handler);
655 
656     // parse human readable Bluetooth address
657     sscanf_bd_addr(device_addr_string, device_addr);
658 
659 #ifdef HAVE_BTSTACK_STDIN
660     btstack_stdin_setup(stdin_process);
661 #endif
662     // turn on!
663     hci_power_control(HCI_POWER_ON);
664     return 0;
665 }
666 /* LISTING_END */
667 /* EXAMPLE_END */
668