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