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