xref: /btstack/example/hfp_ag_demo.c (revision 2308e10812a99217dbf6b81db828f24e082ffed8)
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 // Mini Jambox
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     int mSBC_skipped = 0;
133     printf("Supported codecs: ");
134     for (i = 0; i < sizeof(codecs); i++){
135         switch(codecs[i]){
136             case HFP_CODEC_CVSD:
137                 printf("CVSD");
138                 break;
139             case HFP_CODEC_MSBC:
140                 if (hci_extended_sco_link_supported()){
141                     printf(", mSBC");
142                 } else {
143                     mSBC_skipped = 1;
144                 }
145                 break;
146         }
147     }
148     printf("\n");
149     if (mSBC_skipped){
150         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
151     }
152 }
153 
154 static int getDeviceIndexForAddress( bd_addr_t addr){
155     int j;
156     for (j=0; j< deviceCount; j++){
157         if (bd_addr_cmp(addr, devices[j].address) == 0){
158             return j;
159         }
160     }
161     return -1;
162 }
163 
164 #ifdef HAVE_POSIX_STDIN
165 static void start_scan(void){
166     printf("Starting inquiry scan..\n");
167     hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
168 }
169 #endif
170 
171 static int has_more_remote_name_requests(void){
172     int i;
173     for (i=0;i<deviceCount;i++) {
174         if (devices[i].state == REMOTE_NAME_REQUEST) return 1;
175     }
176     return 0;
177 }
178 
179 static void do_next_remote_name_request(void){
180     int i;
181     for (i=0;i<deviceCount;i++) {
182         // remote name request
183         if (devices[i].state == REMOTE_NAME_REQUEST){
184             devices[i].state = REMOTE_NAME_INQUIRED;
185             printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address));
186             hci_send_cmd(&hci_remote_name_request, devices[i].address,
187                         devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000);
188             return;
189         }
190     }
191 }
192 
193 static void continue_remote_names(void){
194     // don't get remote names for testing
195     if (has_more_remote_name_requests()){
196         do_next_remote_name_request();
197         return;
198     }
199     // try to find PTS
200     int i;
201     for (i=0;i<deviceCount;i++){
202         if (memcmp(devices[i].address, device_addr, 6) == 0){
203             printf("Inquiry scan over, successfully found PTS at index %u\nReady to connect to it.\n", i);
204             return;
205         }
206     }
207     printf("Inquiry scan over but PTS not found :(\n");
208 }
209 
210 static void inquiry_packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){
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     read(ds->fd, &cmd, 1);
339     switch (cmd){
340         case 'a':
341             log_info("USER:\'%c\'", cmd);
342             printf("Establish HFP service level connection to PTS module %s...\n", bd_addr_to_str(device_addr));
343             hfp_ag_establish_service_level_connection(device_addr);
344             break;
345         case 'A':
346             log_info("USER:\'%c\'", cmd);
347             printf("Release HFP service level connection.\n");
348             hfp_ag_release_service_level_connection(acl_handle);
349             break;
350         case 'Z':
351             log_info("USER:\'%c\'", cmd);
352             printf("Release HFP service level connection to %s...\n", bd_addr_to_str(device_addr));
353             hfp_ag_release_service_level_connection(acl_handle);
354             break;
355         case 'b':
356             log_info("USER:\'%c\'", cmd);
357             printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr));
358             hfp_ag_establish_audio_connection(acl_handle);
359             break;
360         case 'B':
361             log_info("USER:\'%c\'", cmd);
362             printf("Release Audio connection.\n");
363             hfp_ag_release_audio_connection(acl_handle);
364             break;
365         case 'c':
366             log_info("USER:\'%c\'", cmd);
367             printf("Simulate incoming call from 1234567\n");
368             hfp_ag_set_clip(129, "1234567");
369             hfp_ag_incoming_call();
370             break;
371         case 'm':
372             log_info("USER:\'%c\'", cmd);
373             printf("Simulate incoming call from 7654321\n");
374             hfp_ag_set_clip(129, "7654321");
375             hfp_ag_incoming_call();
376             break;
377         case 'C':
378             log_info("USER:\'%c\'", cmd);
379             printf("Simulate terminate call\n");
380             hfp_ag_call_dropped();
381             break;
382         case 'd':
383             log_info("USER:\'%c\'", cmd);
384             printf("Report AG failure\n");
385             hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE);
386             break;
387         case 'e':
388             log_info("USER:\'%c\'", cmd);
389             printf("Answer call on AG\n");
390             hfp_ag_answer_incoming_call();
391             break;
392         case 'E':
393             log_info("USER:\'%c\'", cmd);
394             printf("Reject call on AG\n");
395             hfp_ag_terminate_call();
396             break;
397         case 'f':
398             log_info("USER:\'%c\'", cmd);
399             printf("Disable cellular network\n");
400             hfp_ag_set_registration_status(0);
401             break;
402         case 'F':
403             log_info("USER:\'%c\'", cmd);
404             printf("Enable cellular network\n");
405             hfp_ag_set_registration_status(1);
406             break;
407         case 'g':
408             log_info("USER:\'%c\'", cmd);
409             printf("Set signal strength to 0\n");
410             hfp_ag_set_signal_strength(0);
411             break;
412         case 'G':
413             log_info("USER:\'%c\'", cmd);
414             printf("Set signal strength to 5\n");
415             hfp_ag_set_signal_strength(5);
416             break;
417         case 'h':
418             log_info("USER:\'%c\'", cmd);
419             printf("Disable roaming\n");
420             hfp_ag_set_roaming_status(0);
421             break;
422         case 'H':
423             log_info("USER:\'%c\'", cmd);
424             printf("Enable roaming\n");
425             hfp_ag_set_roaming_status(1);
426             break;
427         case 'i':
428             log_info("USER:\'%c\'", cmd);
429             printf("Set battery level to 3\n");
430             hfp_ag_set_battery_level(3);
431             break;
432         case 'I':
433             log_info("USER:\'%c\'", cmd);
434             printf("Set battery level to 5\n");
435             hfp_ag_set_battery_level(5);
436             break;
437         case 'j':
438             log_info("USER:\'%c\'", cmd);
439             printf("Answering call on remote side\n");
440             hfp_ag_outgoing_call_established();
441             break;
442         case 'r':
443             log_info("USER:\'%c\'", cmd);
444             printf("Disable in-band ring tone\n");
445             hfp_ag_set_use_in_band_ring_tone(0);
446             break;
447         case 'k':
448             log_info("USER:\'%c\'", cmd);
449             printf("Memory 1 cleared\n");
450             memory_1_enabled = 0;
451             break;
452         case 'K':
453             log_info("USER:\'%c\'", cmd);
454             printf("Memory 1 set\n");
455             memory_1_enabled = 1;
456             break;
457         case 'l':
458             log_info("USER:\'%c\'", cmd);
459             printf("Last dialed number cleared\n");
460             hfp_ag_clear_last_dialed_number();
461             break;
462         case 'L':
463             log_info("USER:\'%c\'", cmd);
464             printf("Outgoing call connected, ringing\n");
465             hfp_ag_outgoing_call_ringing();
466             break;
467         case 'n':
468             log_info("USER:\'%c\'", cmd);
469             printf("Disable Voice Recognition\n");
470             hfp_ag_activate_voice_recognition(acl_handle, 0);
471             break;
472         case 'N':
473             log_info("USER:\'%c\'", cmd);
474             printf("Enable Voice Recognition\n");
475             hfp_ag_activate_voice_recognition(acl_handle, 1);
476             break;
477         case 'o':
478             log_info("USER:\'%c\'", cmd);
479             printf("Set speaker gain to 0 (minimum)\n");
480             hfp_ag_set_speaker_gain(acl_handle, 0);
481             break;
482         case 'O':
483             log_info("USER:\'%c\'", cmd);
484             printf("Set speaker gain to 9 (default)\n");
485             hfp_ag_set_speaker_gain(acl_handle, 9);
486             break;
487         case 'p':
488             log_info("USER:\'%c\'", cmd);
489             printf("Set speaker gain to 12 (higher)\n");
490             hfp_ag_set_speaker_gain(acl_handle, 12);
491             break;
492         case 'P':
493             log_info("USER:\'%c\'", cmd);
494             printf("Set speaker gain to 15 (maximum)\n");
495             hfp_ag_set_speaker_gain(acl_handle, 15);
496             break;
497         case 'q':
498             log_info("USER:\'%c\'", cmd);
499             printf("Set microphone gain to 0\n");
500             hfp_ag_set_microphone_gain(acl_handle, 0);
501             break;
502         case 'Q':
503             log_info("USER:\'%c\'", cmd);
504             printf("Set microphone gain to 9\n");
505             hfp_ag_set_microphone_gain(acl_handle, 9);
506             break;
507         case 's':
508             log_info("USER:\'%c\'", cmd);
509             printf("Set microphone gain to 12\n");
510             hfp_ag_set_microphone_gain(acl_handle, 12);
511             break;
512         case 'S':
513             log_info("USER:\'%c\'", cmd);
514             printf("Set microphone gain to 15\n");
515             hfp_ag_set_microphone_gain(acl_handle, 15);
516             break;
517         case 'R':
518             log_info("USER:\'%c\'", cmd);
519             printf("Enable in-band ring tone\n");
520             hfp_ag_set_use_in_band_ring_tone(1);
521             break;
522         case 't':
523             log_info("USER:\'%c\'", cmd);
524             printf("Terminate HCI connection. 0x%2x\n", acl_handle);
525             gap_disconnect(acl_handle);
526             break;
527         case 'u':
528             log_info("USER:\'%c\'", cmd);
529             printf("Join held call\n");
530             hfp_ag_join_held_call();
531             break;
532         case 'v':
533             start_scan();
534             break;
535         case 'w':
536             log_info("USER:\'%c\'", cmd);
537             printf("AG: Put incoming call on hold (Response and Hold)\n");
538             hfp_ag_hold_incoming_call();
539             break;
540         case 'x':
541             log_info("USER:\'%c\'", cmd);
542             printf("AG: Accept held incoming call (Response and Hold)\n");
543             hfp_ag_accept_held_incoming_call();
544             break;
545         case 'X':
546             log_info("USER:\'%c\'", cmd);
547             printf("AG: Reject held incoming call (Response and Hold)\n");
548             hfp_ag_reject_held_incoming_call();
549             break;
550         default:
551             show_usage();
552             break;
553     }
554 }
555 #endif
556 
557 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
558     switch (packet_type){
559         case HCI_EVENT_PACKET:
560             switch (event[0]){
561                 case HCI_EVENT_INQUIRY_RESULT:
562                 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
563                 case HCI_EVENT_INQUIRY_COMPLETE:
564                 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
565                     inquiry_packet_handler(HCI_EVENT_PACKET, event, event_size);
566                     break;
567                 case HCI_EVENT_SCO_CAN_SEND_NOW:
568                     sco_demo_send(sco_handle);
569                     break;
570                 case HCI_EVENT_COMMAND_COMPLETE:
571                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
572                         dump_supported_codecs();
573                     }
574                     break;
575                 default:
576                     break;
577             }
578 
579             if (event[0] != HCI_EVENT_HFP_META) return;
580 
581             if (event[3]
582                 && event[2] != HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER
583                 && event[2] != HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG
584                 && event[2] != HFP_SUBEVENT_TRANSMIT_DTMF_CODES){
585                 printf("ERROR, status: %u\n", event[3]);
586                 return;
587             }
588 
589             switch (event[2]) {
590                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
591                     acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event);
592                     hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
593                     printf("Service level connection established from %s.\n", bd_addr_to_str(device_addr));
594                     dump_supported_codecs();
595                    break;
596                 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
597                     printf("Service level connection released.\n");
598                     sco_handle = 0;
599                     break;
600                 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
601                     if (hfp_subevent_audio_connection_established_get_status(event)){
602                         printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event));
603                         sco_handle = 0;
604                     } else {
605                         sco_handle = hfp_subevent_audio_connection_established_get_handle(event);
606                         printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
607                         negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
608                         switch (negotiated_codec){
609                             case 0x01:
610                                 printf("Using CVSD codec.\n");
611                                 break;
612                             case 0x02:
613                                 printf("Using mSBC codec.\n");
614                                 break;
615                             default:
616                                 printf("Using unknown codec 0x%02x.\n", negotiated_codec);
617                                 break;
618                         }
619                         sco_demo_set_codec(negotiated_codec);
620                         hci_request_sco_can_send_now_event();
621                     }
622                     break;
623                 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
624                     printf("\n** Audio connection released **\n");
625                     sco_handle = 0;
626                     break;
627                 case HFP_SUBEVENT_START_RINGINIG:
628                     printf("\n** Start Ringing **\n");
629                     break;
630                 case HFP_SUBEVENT_STOP_RINGINIG:
631                     printf("\n** Stop Ringing **\n");
632                     break;
633                 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER:
634                     printf("\n** Outgoing call '%s' **\n", hfp_subevent_place_call_with_number_get_number(event));
635                     // validate number
636                     if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0
637                       || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0
638                       || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){
639                         printf("Dialstring valid: accept call\n");
640                         hfp_ag_outgoing_call_accepted();
641                     } else {
642                         printf("Dialstring invalid: reject call\n");
643                         hfp_ag_outgoing_call_rejected();
644                     }
645                     break;
646 
647                 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG:
648                     printf("\n** Attach number to voice tag. Sending '1234567\n");
649                     hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567");
650                     break;
651                 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES:
652                     printf("\n** Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event));
653                     hfp_ag_send_dtmf_code_done(acl_handle);
654                     break;
655                 case HFP_SUBEVENT_CALL_ANSWERED:
656                     printf("Call answered by HF\n");
657                     break;
658                 default:
659                     printf("Event not handled %u\n", event[2]);
660                     break;
661             }
662         case HCI_SCO_DATA_PACKET:
663             sco_demo_receive(event, event_size);
664             break;
665         default:
666             break;
667     }
668 }
669 
670 static hfp_phone_number_t subscriber_number = {
671     129, "225577"
672 };
673 
674 /* @section Main Application Setup
675  *
676  * @text Listing MainConfiguration shows main application code.
677  * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it.
678  * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers.
679  * The stdin_process callback allows for sending commands to the HFP HF.
680  * At the end the Bluetooth stack is started.
681  */
682 
683 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */
684 
685 int btstack_main(int argc, const char * argv[]);
686 int btstack_main(int argc, const char * argv[]){
687 
688     sco_demo_init();
689 
690     // register for HCI events
691     hci_event_callback_registration.callback = &packet_handler;
692     hci_add_event_handler(&hci_event_callback_registration);
693     hci_register_sco_packet_handler(&packet_handler);
694 
695     gap_discoverable_control(1);
696 
697     // L2CAP
698     l2cap_init();
699 
700     uint16_t supported_features                   =
701         (1<<HFP_AGSF_ESCO_S4)                     |
702         (1<<HFP_AGSF_HF_INDICATORS)               |
703         (1<<HFP_AGSF_CODEC_NEGOTIATION)           |
704         (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
705         (1<<HFP_AGSF_ENHANCED_CALL_CONTROL)       |
706         (1<<HFP_AGSF_ENHANCED_CALL_STATUS)        |
707         (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL)    |
708         (1<<HFP_AGSF_IN_BAND_RING_TONE)           |
709         (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
710         (1<<HFP_AGSF_THREE_WAY_CALLING);
711     int wide_band_speech = 1;
712 
713     // HFP
714     rfcomm_init();
715     hfp_ag_init(rfcomm_channel_nr);
716     hfp_ag_init_supported_features(supported_features);
717     hfp_ag_init_codecs(sizeof(codecs), codecs);
718     hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators);
719     hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators);
720     hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services);
721     hfp_ag_set_subcriber_number_information(&subscriber_number, 1);
722     hfp_ag_register_packet_handler(&packet_handler);
723     hci_register_sco_packet_handler(&packet_handler);
724 
725     // SDP Server
726     sdp_init();
727     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
728     hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech);
729     printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer));
730     sdp_register_service(hfp_service_buffer);
731 
732 #ifdef HAVE_POSIX_STDIN
733     btstack_stdin_setup(stdin_process);
734 #endif
735     // turn on!
736     hci_power_control(HCI_POWER_ON);
737     return 0;
738 }
739 /* LISTING_END */
740 /* EXAMPLE_END */
741