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