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