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