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