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