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