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