xref: /btstack/example/hfp_ag_demo.c (revision e64e0086b6e940726b732214f11d6320c5a5b42c)
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 /*
39  * hfp_ag_demo.c
40  */
41 
42 // *****************************************************************************
43 /* EXAMPLE_START(hfp_ag_demo): HFP Audio Gateway (AG) Demo
44  *
45  * @text This HFP Audio Gateway example demonstrates how to receive
46  * an output from a remote HFP Hands-Free (HF) unit, and,
47  * if HAVE_POSIX_STDIN is defined, how to control the HFP HF.
48  */
49 // *****************************************************************************
50 
51 
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "btstack.h"
59 #ifdef HAVE_POSIX_STDIN
60 #include "stdin_support.h"
61 #endif
62 
63 
64 uint8_t hfp_service_buffer[150];
65 const uint8_t    rfcomm_channel_nr = 1;
66 const char hfp_ag_service_name[] = "BTstack HFP AG Test";
67 
68 // PTS
69 // static bd_addr_t device_addr = {0x00,0x15,0x83,0x5F,0x9D,0x46};
70 // BT-201
71 static bd_addr_t device_addr = {0x00, 0x07, 0xB0, 0x83, 0x02, 0x5E};
72 
73 static uint8_t codecs[1] = {HFP_CODEC_CVSD};
74 static uint16_t handle = -1;
75 static int memory_1_enabled = 1;
76 
77 static int ag_indicators_nr = 7;
78 static hfp_ag_indicator_t ag_indicators[] = {
79     // index, name, min range, max range, status, mandatory, enabled, status changed
80     {1, "service",   0, 1, 1, 0, 0, 0},
81     {2, "call",      0, 1, 0, 1, 1, 0},
82     {3, "callsetup", 0, 3, 0, 1, 1, 0},
83     {4, "battchg",   0, 5, 3, 0, 0, 0},
84     {5, "signal",    0, 5, 5, 0, 1, 0},
85     {6, "roam",      0, 1, 0, 0, 1, 0},
86     {7, "callheld",  0, 2, 0, 1, 1, 0}
87 };
88 
89 static int call_hold_services_nr = 5;
90 static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"};
91 
92 static int hf_indicators_nr = 2;
93 static hfp_generic_status_indicator_t hf_indicators[] = {
94     {1, 1},
95     {2, 1},
96 };
97 
98 char cmd;
99 
100 // GAP INQUIRY
101 
102 #define MAX_DEVICES 10
103 enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED };
104 struct device {
105     bd_addr_t  address;
106     uint16_t   clockOffset;
107     uint32_t   classOfDevice;
108     uint8_t    pageScanRepetitionMode;
109     uint8_t    rssi;
110     enum DEVICE_STATE  state;
111 };
112 
113 #define INQUIRY_INTERVAL 5
114 struct device devices[MAX_DEVICES];
115 int deviceCount = 0;
116 
117 
118 enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ;
119 enum STATE state = INIT;
120 
121 
122 static int getDeviceIndexForAddress( bd_addr_t addr){
123     int j;
124     for (j=0; j< deviceCount; j++){
125         if (bd_addr_cmp(addr, devices[j].address) == 0){
126             return j;
127         }
128     }
129     return -1;
130 }
131 
132 #ifdef HAVE_POSIX_STDIN
133 static void start_scan(void){
134     printf("Starting inquiry scan..\n");
135     hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
136 }
137 #endif
138 
139 static int has_more_remote_name_requests(void){
140     int i;
141     for (i=0;i<deviceCount;i++) {
142         if (devices[i].state == REMOTE_NAME_REQUEST) return 1;
143     }
144     return 0;
145 }
146 
147 static void do_next_remote_name_request(void){
148     int i;
149     for (i=0;i<deviceCount;i++) {
150         // remote name request
151         if (devices[i].state == REMOTE_NAME_REQUEST){
152             devices[i].state = REMOTE_NAME_INQUIRED;
153             printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address));
154             hci_send_cmd(&hci_remote_name_request, devices[i].address,
155                         devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000);
156             return;
157         }
158     }
159 }
160 
161 static void continue_remote_names(void){
162     // don't get remote names for testing
163     if (has_more_remote_name_requests()){
164         do_next_remote_name_request();
165         return;
166     }
167     // try to find PTS
168     int i;
169     for (i=0;i<deviceCount;i++){
170         if (memcmp(devices[i].address, device_addr, 6) == 0){
171             printf("Inquiry scan over, successfully found PTS at index %u\nReady to connect to it.\n", i);
172             return;
173         }
174     }
175     printf("Inquiry scan over but PTS not found :(\n");
176 }
177 
178 static void inquiry_packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){
179     bd_addr_t addr;
180     int i;
181     int numResponses;
182     int index;
183 
184     // printf("packet_handler: pt: 0x%02x, packet[0]: 0x%02x\n", packet_type, packet[0]);
185     if (packet_type != HCI_EVENT_PACKET) return;
186 
187     uint8_t event = packet[0];
188 
189     switch(event){
190         case HCI_EVENT_INQUIRY_RESULT:
191         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{
192             numResponses = hci_event_inquiry_result_get_num_responses(packet);
193             int offset = 3;
194             for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){
195                 reverse_bd_addr(&packet[offset], addr);
196                 offset += 6;
197                 index = getDeviceIndexForAddress(addr);
198                 if (index >= 0) continue;   // already in our list
199                 memcpy(devices[deviceCount].address, addr, 6);
200 
201                 devices[deviceCount].pageScanRepetitionMode = packet[offset];
202                 offset += 1;
203 
204                 if (event == HCI_EVENT_INQUIRY_RESULT){
205                     offset += 2; // Reserved + Reserved
206                     devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset);
207                     offset += 3;
208                     devices[deviceCount].clockOffset =   little_endian_read_16(packet, offset) & 0x7fff;
209                     offset += 2;
210                     devices[deviceCount].rssi  = 0;
211                 } else {
212                     offset += 1; // Reserved
213                     devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset);
214                     offset += 3;
215                     devices[deviceCount].clockOffset =   little_endian_read_16(packet, offset) & 0x7fff;
216                     offset += 2;
217                     devices[deviceCount].rssi  = packet[offset];
218                     offset += 1;
219                 }
220                 devices[deviceCount].state = REMOTE_NAME_REQUEST;
221                 printf("Device #%u found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n",
222                     deviceCount, bd_addr_to_str(addr),
223                     devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode,
224                     devices[deviceCount].clockOffset, devices[deviceCount].rssi);
225                 deviceCount++;
226             }
227 
228             break;
229         }
230         case HCI_EVENT_INQUIRY_COMPLETE:
231             for (i=0;i<deviceCount;i++) {
232                 // retry remote name request
233                 if (devices[i].state == REMOTE_NAME_INQUIRED)
234                     devices[i].state = REMOTE_NAME_REQUEST;
235             }
236             continue_remote_names();
237             break;
238 
239         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
240             reverse_bd_addr(&packet[3], addr);
241             index = getDeviceIndexForAddress(addr);
242             if (index >= 0) {
243                 if (packet[2] == 0) {
244                     printf("Name: '%s'\n", &packet[9]);
245                     devices[index].state = REMOTE_NAME_FETCHED;
246                 } else {
247                     printf("Failed to get name: page timeout\n");
248                 }
249             }
250             continue_remote_names();
251             break;
252 
253         default:
254             break;
255     }
256 }
257 // GAP INQUIRY END
258 #ifdef HAVE_POSIX_STDIN
259 
260 // prototypes
261 static void show_usage(void);
262 
263 // Testig User Interface
264 static void show_usage(void){
265     bd_addr_t iut_address;
266     gap_local_bd_addr(iut_address);
267 
268     printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
269     printf("---\n");
270 
271     printf("a - establish HFP connection to PTS module\n");
272     // printf("A - release HFP connection to PTS module\n");
273 
274     printf("b - establish AUDIO connection\n");
275     printf("B - release AUDIO connection\n");
276 
277     printf("c - simulate incoming call from 1234567\n");
278     printf("C - simulate call from 1234567 dropped\n");
279 
280     printf("d - report AG failure\n");
281 
282     printf("e - answer call on AG\n");
283     printf("E - reject call on AG\n");
284 
285     printf("r - disable in-band ring tone\n");
286     printf("R - enable in-band ring tone\n");
287 
288     printf("f - Disable cellular network\n");
289     printf("F - Enable cellular network\n");
290 
291     printf("g - Set signal strength to 0\n");
292     printf("G - Set signal strength to 5\n");
293 
294     printf("h - Disable roaming\n");
295     printf("H - Enable roaming\n");
296 
297     printf("i - Set battery level to 3\n");
298     printf("I - Set battery level to 5\n");
299 
300     printf("j - Answering call on remote side\n");
301 
302     printf("k - Clear memory #1\n");
303     printf("K - Set memory #1\n");
304 
305     printf("l - Clear last number\n");
306     printf("L - Set last number\n");
307 
308     printf("m - simulate incoming call from 7654321\n");
309     // printf("M - simulate call from 7654321 dropped\n");
310 
311     printf("n - Disable Voice Regocnition\n");
312     printf("N - Enable Voice Recognition\n");
313 
314     printf("o - Set speaker volume to 0  (minimum)\n");
315     printf("O - Set speaker volume to 9  (default)\n");
316     printf("p - Set speaker volume to 12 (higher)\n");
317     printf("P - Set speaker volume to 15 (maximum)\n");
318 
319     printf("q - Set microphone gain to 0  (minimum)\n");
320     printf("Q - Set microphone gain to 9  (default)\n");
321     printf("s - Set microphone gain to 12 (higher)\n");
322     printf("S - Set microphone gain to 15 (maximum)\n");
323 
324     printf("t - terminate connection\n");
325     printf("u - join held call\n");
326     printf("v - discover nearby HF units\n");
327     printf("w - put incoming call on hold (Response and Hold)\n");
328     printf("x - accept held incoming call (Response and Hold)\n");
329     printf("X - reject held incoming call (Response and Hold)\n");
330 
331     printf("---\n");
332     printf("Ctrl-c - exit\n");
333     printf("---\n");
334 }
335 
336 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
337     read(ds->fd, &cmd, 1);
338     switch (cmd){
339         case 'a':
340             log_info("USER:\'%c\'", cmd);
341             printf("Establish HFP service level connection to PTS module %s...\n", bd_addr_to_str(device_addr));
342             hfp_ag_establish_service_level_connection(device_addr);
343             break;
344         case 'A':
345             log_info("USER:\'%c\'", cmd);
346             printf("Release HFP service level connection.\n");
347             hfp_ag_release_service_level_connection(device_addr);
348             break;
349         case 'Z':
350             log_info("USER:\'%c\'", cmd);
351             printf("Release HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
352             hfp_ag_release_service_level_connection(device_addr);
353             break;
354         case 'b':
355             log_info("USER:\'%c\'", cmd);
356             printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
357             hfp_ag_establish_audio_connection(device_addr);
358             break;
359         case 'B':
360             log_info("USER:\'%c\'", cmd);
361             printf("Release Audio connection.\n");
362             hfp_ag_release_audio_connection(device_addr);
363             break;
364         case 'c':
365             log_info("USER:\'%c\'", cmd);
366             printf("Simulate incoming call from 1234567\n");
367             hfp_ag_set_clip(129, "1234567");
368             hfp_ag_incoming_call();
369             break;
370         case 'm':
371             log_info("USER:\'%c\'", cmd);
372             printf("Simulate incoming call from 7654321\n");
373             hfp_ag_set_clip(129, "7654321");
374             hfp_ag_incoming_call();
375             break;
376         case 'C':
377             log_info("USER:\'%c\'", cmd);
378             printf("Simulate terminate call\n");
379             hfp_ag_call_dropped();
380             break;
381         case 'd':
382             log_info("USER:\'%c\'", cmd);
383             printf("Report AG failure\n");
384             hfp_ag_report_extended_audio_gateway_error_result_code(device_addr, HFP_CME_ERROR_AG_FAILURE);
385             break;
386         case 'e':
387             log_info("USER:\'%c\'", cmd);
388             printf("Answer call on AG\n");
389             hfp_ag_answer_incoming_call();
390             break;
391         case 'E':
392             log_info("USER:\'%c\'", cmd);
393             printf("Reject call on AG\n");
394             hfp_ag_terminate_call();
395             break;
396         case 'f':
397             log_info("USER:\'%c\'", cmd);
398             printf("Disable cellular network\n");
399             hfp_ag_set_registration_status(0);
400             break;
401         case 'F':
402             log_info("USER:\'%c\'", cmd);
403             printf("Enable cellular network\n");
404             hfp_ag_set_registration_status(1);
405             break;
406         case 'g':
407             log_info("USER:\'%c\'", cmd);
408             printf("Set signal strength to 0\n");
409             hfp_ag_set_signal_strength(0);
410             break;
411         case 'G':
412             log_info("USER:\'%c\'", cmd);
413             printf("Set signal strength to 5\n");
414             hfp_ag_set_signal_strength(5);
415             break;
416         case 'h':
417             log_info("USER:\'%c\'", cmd);
418             printf("Disable roaming\n");
419             hfp_ag_set_roaming_status(0);
420             break;
421         case 'H':
422             log_info("USER:\'%c\'", cmd);
423             printf("Enable roaming\n");
424             hfp_ag_set_roaming_status(1);
425             break;
426         case 'i':
427             log_info("USER:\'%c\'", cmd);
428             printf("Set battery level to 3\n");
429             hfp_ag_set_battery_level(3);
430             break;
431         case 'I':
432             log_info("USER:\'%c\'", cmd);
433             printf("Set battery level to 5\n");
434             hfp_ag_set_battery_level(5);
435             break;
436         case 'j':
437             log_info("USER:\'%c\'", cmd);
438             printf("Answering call on remote side\n");
439             hfp_ag_outgoing_call_established();
440             break;
441         case 'r':
442             log_info("USER:\'%c\'", cmd);
443             printf("Disable in-band ring tone\n");
444             hfp_ag_set_use_in_band_ring_tone(0);
445             break;
446         case 'k':
447             log_info("USER:\'%c\'", cmd);
448             printf("Memory 1 cleared\n");
449             memory_1_enabled = 0;
450             break;
451         case 'K':
452             log_info("USER:\'%c\'", cmd);
453             printf("Memory 1 set\n");
454             memory_1_enabled = 1;
455             break;
456         case 'l':
457             log_info("USER:\'%c\'", cmd);
458             printf("Last dialed number cleared\n");
459             hfp_ag_clear_last_dialed_number();
460             break;
461         case 'L':
462             log_info("USER:\'%c\'", cmd);
463             printf("Outgoing call connected, ringing\n");
464             hfp_ag_outgoing_call_ringing();
465             break;
466         case 'n':
467             log_info("USER:\'%c\'", cmd);
468             printf("Disable Voice Recognition\n");
469             hfp_ag_activate_voice_recognition(device_addr, 0);
470             break;
471         case 'N':
472             log_info("USER:\'%c\'", cmd);
473             printf("Enable Voice Recognition\n");
474             hfp_ag_activate_voice_recognition(device_addr, 1);
475             break;
476         case 'o':
477             log_info("USER:\'%c\'", cmd);
478             printf("Set speaker gain to 0 (minimum)\n");
479             hfp_ag_set_speaker_gain(device_addr, 0);
480             break;
481         case 'O':
482             log_info("USER:\'%c\'", cmd);
483             printf("Set speaker gain to 9 (default)\n");
484             hfp_ag_set_speaker_gain(device_addr, 9);
485             break;
486         case 'p':
487             log_info("USER:\'%c\'", cmd);
488             printf("Set speaker gain to 12 (higher)\n");
489             hfp_ag_set_speaker_gain(device_addr, 12);
490             break;
491         case 'P':
492             log_info("USER:\'%c\'", cmd);
493             printf("Set speaker gain to 15 (maximum)\n");
494             hfp_ag_set_speaker_gain(device_addr, 15);
495             break;
496         case 'q':
497             log_info("USER:\'%c\'", cmd);
498             printf("Set microphone gain to 0\n");
499             hfp_ag_set_microphone_gain(device_addr, 0);
500             break;
501         case 'Q':
502             log_info("USER:\'%c\'", cmd);
503             printf("Set microphone gain to 9\n");
504             hfp_ag_set_microphone_gain(device_addr, 9);
505             break;
506         case 's':
507             log_info("USER:\'%c\'", cmd);
508             printf("Set microphone gain to 12\n");
509             hfp_ag_set_microphone_gain(device_addr, 12);
510             break;
511         case 'S':
512             log_info("USER:\'%c\'", cmd);
513             printf("Set microphone gain to 15\n");
514             hfp_ag_set_microphone_gain(device_addr, 15);
515             break;
516         case 'R':
517             log_info("USER:\'%c\'", cmd);
518             printf("Enable in-band ring tone\n");
519             hfp_ag_set_use_in_band_ring_tone(1);
520             break;
521         case 't':
522             log_info("USER:\'%c\'", cmd);
523             printf("Terminate HCI connection. 0x%2x\n", handle);
524             gap_disconnect(handle);
525             break;
526         case 'u':
527             log_info("USER:\'%c\'", cmd);
528             printf("Join held call\n");
529             hfp_ag_join_held_call();
530             break;
531         case 'v':
532             start_scan();
533             break;
534         case 'w':
535             log_info("USER:\'%c\'", cmd);
536             printf("AG: Put incoming call on hold (Response and Hold)\n");
537             hfp_ag_hold_incoming_call();
538             break;
539         case 'x':
540             log_info("USER:\'%c\'", cmd);
541             printf("AG: Accept held incoming call (Response and Hold)\n");
542             hfp_ag_accept_held_incoming_call();
543             break;
544         case 'X':
545             log_info("USER:\'%c\'", cmd);
546             printf("AG: Reject held incoming call (Response and Hold)\n");
547             hfp_ag_reject_held_incoming_call();
548             break;
549         default:
550             show_usage();
551             break;
552     }
553 }
554 #endif
555 
556 static void packet_handler(uint8_t * event, uint16_t event_size){
557      switch (event[0]){
558         case HCI_EVENT_INQUIRY_RESULT:
559         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
560         case HCI_EVENT_INQUIRY_COMPLETE:
561         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
562             inquiry_packet_handler(HCI_EVENT_PACKET, event, event_size);
563             break;
564 
565         default:
566             break;
567     }
568 
569     if (event[0] != HCI_EVENT_HFP_META) return;
570 
571     if (event[3]
572         && event[2] != HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER
573         && event[2] != HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG
574         && event[2] != HFP_SUBEVENT_TRANSMIT_DTMF_CODES){
575         printf("ERROR, status: %u\n", event[3]);
576         return;
577     }
578 
579     switch (event[2]) {
580         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
581             handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
582             printf("Service level connection established.\n");
583             break;
584         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
585             printf("Service level connection released.\n");
586             break;
587         case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
588             printf("\n** Audio connection established **\n");
589             break;
590         case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
591             printf("\n** Audio connection released **\n");
592             break;
593         case HFP_SUBEVENT_START_RINGINIG:
594             printf("\n** Start Ringing **\n");
595             break;
596         case HFP_SUBEVENT_STOP_RINGINIG:
597             printf("\n** Stop Ringing **\n");
598             break;
599         case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
600             printf("\n** Outgoing call '%s' **\n", hfp_subevent_place_call_with_number_get_number(event));
601             // validate number
602             if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
603               || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
604               || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
605                 printf("Dialstring valid: accept call\n");
606                 hfp_ag_outgoing_call_accepted();
607             } else {
608                 printf("Dialstring invalid: reject call\n");
609                 hfp_ag_outgoing_call_rejected();
610             }
611             break;
612 
613         case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
614             printf("\n** Attach number to voice tag. Sending '1234567\n");
615             hfp_ag_send_phone_number_for_voice_tag(device_addr, "1234567");
616             break;
617         case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
618             printf("\n** Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
619             hfp_ag_send_dtmf_code_done(device_addr);
620             break;
621         case HFP_SUBEVENT_CALL_ANSWERED:
622             printf("Call answered by HF\n");
623             break;
624         default:
625             printf("Event not handled %u\n", event[2]);
626             break;
627     }
628 }
629 
630 static hfp_phone_number_t subscriber_number = {
631     129, "225577"
632 };
633 
634 /* @section Main Application Setup
635  *
636  * @text Listing MainConfiguration shows main application code.
637  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
638  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
639  * The stdin_process callback allows for sending commands to the HFP HF.
640  * At the end the Bluetooth stack is started.
641  */
642 
643 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
644 
645 int btstack_main(int argc, const char * argv[]);
646 int btstack_main(int argc, const char * argv[]){
647 
648     gap_discoverable_control(1);
649 
650     // L2CAP
651     l2cap_init();
652 
653     // HFP
654     rfcomm_init();
655     hfp_ag_init(rfcomm_channel_nr);
656     hfp_ag_init_supported_features(0x3ef | (1<<HFP_AGSF_HF_INDICATORS) | (1<<HFP_AGSF_ESCO_S4));
657     hfp_ag_init_codecs(sizeof(codecs), codecs);
658     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
659     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
660     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
661     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
662     hfp_ag_register_packet_handler(packet_handler);
663 
664     // SDP Server
665     sdp_init();
666     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
667     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, 0);
668     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
669     sdp_register_service(hfp_service_buffer);
670 
671 #ifdef HAVE_POSIX_STDIN
672     btstack_stdin_setup(stdin_process);
673 #endif
674     // turn on!
675     hci_power_control(HCI_POWER_ON);
676     return 0;
677 }
678 /* LISTING_END */
679 /* EXAMPLE_END */
680