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