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