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