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