xref: /btstack/example/hfp_ag_demo.c (revision d4c7baedf72072fcac065435bb85e31ac82de319)
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 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, "test message"
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("d - report AG failure\n");
162     printf("D - delete all link keys\n");
163     printf("e - answer call on AG                   | E - reject call on AG\n");
164     printf("r - disable in-band ring tone           | R - enable in-band ring tone\n");
165     printf("f - Disable cellular network            | F - Enable cellular network\n");
166     printf("g - Set signal strength to 0            | G - Set signal strength to 5\n");
167     printf("h - Disable roaming                     | H - Enable roaming\n");
168     printf("i - Set battery level to 3              | I - Set battery level to 5\n");
169     printf("j - Answering call on remote side\n");
170     printf("k - Clear memory #1                     | K - Set memory #1\n");
171     printf("l - Clear last number                   | L - Set last number\n");
172     printf("m - simulate incoming call from 7654321\n");
173     // printf("M - simulate call from 7654321 dropped\n");
174     printf("n - Disable Voice Recognition           | N - Enable Voice Recognition\n");
175     printf("z - Disable Enhanced Voice Recognition  | Z - Enable Enhanced 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 'D':
224             printf("Deleting all link keys\n");
225             gap_delete_all_link_keys();
226             break;
227         case 'm':
228             log_info("USER:\'%c\'", cmd);
229             printf("Simulate incoming call from 7654321\n");
230             hfp_ag_set_clip(129, "7654321");
231             hfp_ag_incoming_call();
232             break;
233         case 'C':
234             log_info("USER:\'%c\'", cmd);
235             printf("Simulate terminate call\n");
236             hfp_ag_call_dropped();
237             break;
238         case 'd':
239             log_info("USER:\'%c\'", cmd);
240             printf("Report AG failure\n");
241             status = hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
242             break;
243         case 'e':
244             log_info("USER:\'%c\'", cmd);
245             printf("Answer call on AG\n");
246             hfp_ag_answer_incoming_call();
247             break;
248         case 'E':
249             log_info("USER:\'%c\'", cmd);
250             printf("Reject call on AG\n");
251             hfp_ag_terminate_call();
252             break;
253         case 'f':
254             log_info("USER:\'%c\'", cmd);
255             printf("Disable cellular network\n");
256             status = hfp_ag_set_registration_status(0);
257             break;
258         case 'F':
259             log_info("USER:\'%c\'", cmd);
260             printf("Enable cellular network\n");
261             status = hfp_ag_set_registration_status(1);
262             break;
263         case 'g':
264             log_info("USER:\'%c\'", cmd);
265             printf("Set signal strength to 0\n");
266             status = hfp_ag_set_signal_strength(0);
267             break;
268         case 'G':
269             log_info("USER:\'%c\'", cmd);
270             printf("Set signal strength to 5\n");
271             status = hfp_ag_set_signal_strength(5);
272             break;
273         case 'h':
274             log_info("USER:\'%c\'", cmd);
275             printf("Disable roaming\n");
276             status = hfp_ag_set_roaming_status(0);
277             break;
278         case 'H':
279             log_info("USER:\'%c\'", cmd);
280             printf("Enable roaming\n");
281             status = hfp_ag_set_roaming_status(1);
282             break;
283         case 'i':
284             log_info("USER:\'%c\'", cmd);
285             printf("Set battery level to 3\n");
286             status = hfp_ag_set_battery_level(3);
287             break;
288         case 'I':
289             log_info("USER:\'%c\'", cmd);
290             printf("Set battery level to 5\n");
291             status = hfp_ag_set_battery_level(5);
292             break;
293         case 'j':
294             log_info("USER:\'%c\'", cmd);
295             printf("Answering call on remote side\n");
296             hfp_ag_outgoing_call_established();
297             break;
298         case 'r':
299             log_info("USER:\'%c\'", cmd);
300             printf("Disable in-band ring tone\n");
301             hfp_ag_set_use_in_band_ring_tone(0);
302             break;
303         case 'k':
304             log_info("USER:\'%c\'", cmd);
305             printf("Memory 1 cleared\n");
306             memory_1_enabled = 0;
307             break;
308         case 'K':
309             log_info("USER:\'%c\'", cmd);
310             printf("Memory 1 set\n");
311             memory_1_enabled = 1;
312             break;
313         case 'l':
314             log_info("USER:\'%c\'", cmd);
315             printf("Last dialed number cleared\n");
316             hfp_ag_clear_last_dialed_number();
317             break;
318         case 'L':
319             log_info("USER:\'%c\'", cmd);
320             printf("Outgoing call connected, ringing\n");
321             hfp_ag_outgoing_call_ringing();
322             break;
323         case 'n':
324             log_info("USER:\'%c\'", cmd);
325             printf("Disable Voice Recognition\n");
326             status = hfp_ag_deactivate_voice_recognition(acl_handle);
327             break;
328         case 'N':
329             log_info("USER:\'%c\'", cmd);
330             printf("Enable Voice Recognition\n");
331             status = hfp_ag_activate_voice_recognition(acl_handle);
332             break;
333 
334         case 'z':
335             log_info("USER:\'%c\'", cmd);
336             printf("Disable Enhanced Voice Recognition\n");
337             status = hfp_ag_deactivate_enhanced_voice_recognition(acl_handle);
338             break;
339         case 'Z':
340             log_info("USER:\'%c\'", cmd);
341             printf("Enable Enhanced_Voice Recognition\n");
342             status = hfp_ag_activate_enhanced_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 #ifndef HAVE_BTSTACK_STDIN
479                 case BTSTACK_EVENT_STATE:
480                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
481                     printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr));
482                     hfp_ag_establish_service_level_connection(device_addr);
483                     break;
484 #endif
485                 case GAP_EVENT_INQUIRY_RESULT:
486                     gap_event_inquiry_result_get_bd_addr(event, addr);
487                     // print info
488                     printf("Device found: %s ",  bd_addr_to_str(addr));
489                     printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event));
490                     if (gap_event_inquiry_result_get_rssi_available(event)){
491                         printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event));
492                     }
493                     if (gap_event_inquiry_result_get_name_available(event)){
494                         char name_buffer[240];
495                         int name_len = gap_event_inquiry_result_get_name_len(event);
496                         memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len);
497                         name_buffer[name_len] = 0;
498                         printf(", name '%s'", name_buffer);
499                     }
500                     printf("\n");
501                     break;
502                 case GAP_EVENT_INQUIRY_COMPLETE:
503                     printf("Inquiry scan complete.\n");
504                     break;
505                 case HCI_EVENT_SCO_CAN_SEND_NOW:
506                     sco_demo_send(sco_handle);
507                     break;
508                 case HCI_EVENT_COMMAND_COMPLETE:
509                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
510                         dump_supported_codecs();
511                     }
512                     break;
513                 default:
514                     break;
515             }
516 
517             if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return;
518 
519             switch (hci_event_hfp_meta_get_subevent_code(event)) {
520                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
521                     status = hfp_subevent_service_level_connection_established_get_status(event);
522                     if (status != ERROR_CODE_SUCCESS){
523                         printf("Connection failed, status 0x%02x\n", status);
524                         break;
525                     }
526                     acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
527                     hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
528                     printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr));
529                     dump_supported_codecs();
530 #ifndef HAVE_BTSTACK_STDIN
531                     log_info("Establish Audio connection %s", bd_addr_to_str(device_addr));
532                     printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
533                     hfp_ag_establish_audio_connection(acl_handle);
534 #endif
535                     break;
536                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
537                     printf("Service level connection released.\n");
538                     acl_handle = HCI_CON_HANDLE_INVALID;
539                     break;
540                 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
541                     if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){
542                         printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
543                     } else {
544                         sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
545                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
546                         negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
547                         switch (negotiated_codec){
548                             case 0x01:
549                                 printf("Using CVSD codec.\n");
550                                 break;
551                             case 0x02:
552                                 printf("Using mSBC codec.\n");
553                                 break;
554                             default:
555                                 printf("Using unknown codec 0x%02x.\n", negotiated_codec);
556                                 break;
557                         }
558                         sco_demo_set_codec(negotiated_codec);
559                         hci_request_sco_can_send_now_event();
560                     }
561                     break;
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                 case HFP_SUBEVENT_START_RINGING:
568                     printf("** START Ringing **\n");
569                     break;
570                 case HFP_SUBEVENT_RING:
571                     printf("** Ring **\n");
572                     break;
573                 case HFP_SUBEVENT_STOP_RINGING:
574                     printf("** STOP Ringing **\n");
575                     break;
576                 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
577                     printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event));
578                     // validate number
579                     if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
580                       || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
581                       || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
582                         printf("Dial string valid: accept call\n");
583                         hfp_ag_outgoing_call_accepted();
584                         hfp_outgoing_call_ringing_start();
585                     } else {
586                         printf("Dial string invalid: reject call\n");
587                         hfp_ag_outgoing_call_rejected();
588                     }
589                     break;
590 
591                 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
592                     printf("Attach number to voice tag. Sending '1234567\n");
593                     hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
594                     break;
595                 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
596                     printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
597                     hfp_ag_send_dtmf_code_done(acl_handle);
598                     break;
599                 case HFP_SUBEVENT_CALL_ANSWERED:
600                     printf("Call answered by HF\n");
601                     break;
602 
603                 case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS:
604                     status = hfp_subevent_voice_recognition_status_get_status(event);
605                     if (status != ERROR_CODE_SUCCESS){
606                         printf("Voice Recognition command failed\n");
607                         break;
608                     }
609 
610                     if (hfp_subevent_voice_recognition_status_get_state(event) > 0){
611                         printf("\nVoice recognition status ACTIVATED\n\n");
612                     } else {
613                         printf("\nVoice recognition status DEACTIVATED\n\n");
614                     }
615                     break;
616 
617                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO:
618                     status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
619                     report_status(status, "Enhanced Voice recognition: READY FOR AUDIO");
620                     break;
621 
622                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT:
623                     status = hfp_subevent_enhanced_voice_recognition_ag_ready_to_accept_audio_input_get_status(event);
624                     report_status(status, "Enhanced Voice recognition: AG READY TO ACCEPT AUDIO INPUT");
625                     break;
626 
627                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND:
628                     status = hfp_subevent_enhanced_voice_recognition_ag_is_starting_sound_get_status(event);
629                     report_status(status, "Enhanced Voice recognition: AG IS STARTING SOUND");
630                     break;
631 
632                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT:
633                     status = hfp_subevent_enhanced_voice_recognition_ag_is_processing_audio_input_get_status(event);
634                     report_status(status, "Enhanced Voice recognition: AG IS PROCESSING AUDIO INPUT");
635                     break;
636 
637                 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT:
638                     status = hfp_subevent_enhanced_voice_recognition_ag_message_sent_get_status(event);
639                     report_status(status, "Enhanced Voice recognition: AG MESSAGE SENT");
640                     printf("Enhanced Voice recognition: \'%s\'\n\n", msg.text);
641                     break;
642 
643                 case HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE:
644                     status = hfp_subevent_echo_canceling_and_noise_reduction_deactivate_get_status(event);
645                     report_status(status, "Echo Canceling and Noise Reduction Deactivate");
646                     break;
647 
648                 case HFP_SUBEVENT_HF_INDICATOR:
649                     switch (hfp_subevent_hf_indicator_get_uuid(event)){
650                         case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY:
651                             printf("Received ENHANCED SAFETY indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event));
652                             break;
653                         case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL:
654                             printf("Received BATTERY LEVEL indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event));
655                             break;
656                         default:
657                             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));
658                             break;
659                     }
660                     break;
661                 default:
662                     break;
663             }
664             break;
665         case HCI_SCO_DATA_PACKET:
666             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
667             sco_demo_receive(event, event_size);
668             break;
669         default:
670             break;
671     }
672 }
673 
674 static hfp_phone_number_t subscriber_number = {
675     129, "225577"
676 };
677 
678 /* @section Main Application Setup
679  *
680  * @text Listing MainConfiguration shows main application code.
681  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
682  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
683  * The stdin_process callback allows for sending commands to the HFP HF.
684  * At the end the Bluetooth stack is started.
685  */
686 
687 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
688 
689 int btstack_main(int argc, const char * argv[]);
690 int btstack_main(int argc, const char * argv[]){
691     (void)argc;
692     (void)argv;
693 
694     sco_demo_init();
695 
696     gap_set_local_name("HFP AG Demo 00:00:00:00:00:00");
697     gap_discoverable_control(1);
698 
699     // L2CAP
700     l2cap_init();
701 
702     uint16_t supported_features                   =
703         (1<<HFP_AGSF_ESCO_S4)                     |
704         (1<<HFP_AGSF_HF_INDICATORS)               |
705         (1<<HFP_AGSF_CODEC_NEGOTIATION)           |
706         (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
707         (1<<HFP_AGSF_ENHANCED_CALL_CONTROL)       |
708         (1<<HFP_AGSF_ENHANCED_CALL_STATUS)        |
709         (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL)    |
710         (1<<HFP_AGSF_IN_BAND_RING_TONE)           |
711         (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
712         (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
713         (1<<HFP_AGSF_EC_NR_FUNCTION) |
714         (1<<HFP_AGSF_THREE_WAY_CALLING);
715     int wide_band_speech = 1;
716 
717     // HFP
718     rfcomm_init();
719     hfp_ag_init(rfcomm_channel_nr);
720     hfp_ag_init_supported_features(supported_features);
721     hfp_ag_init_codecs(sizeof(codecs), codecs);
722     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
723     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
724     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
725     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
726 
727     // SDP Server
728     sdp_init();
729     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
730     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
731     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
732     sdp_register_service(hfp_service_buffer);
733 
734     // register for HCI events and SCO packets
735     hci_event_callback_registration.callback = &packet_handler;
736     hci_add_event_handler(&hci_event_callback_registration);
737     hci_register_sco_packet_handler(&packet_handler);
738 
739     // register for HFP events
740     hfp_ag_register_packet_handler(&packet_handler);
741 
742     // parse human readable Bluetooth address
743     sscanf_bd_addr(device_addr_string, device_addr);
744 
745 #ifdef HAVE_BTSTACK_STDIN
746     btstack_stdin_setup(stdin_process);
747 #endif
748     // turn on!
749     hci_power_control(HCI_POWER_ON);
750     return 0;
751 }
752 /* LISTING_END */
753 /* EXAMPLE_END */
754