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