xref: /btstack/example/hfp_hf_demo.c (revision af97579e443f2c6b71882d1189eb88c19629f90f)
1 
2 /*
3  * Copyright (C) 2014 BlueKitchen GmbH
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  * 4. Any redistribution, use, or modification is done solely for
18  *    personal benefit and not for any commercial purpose or for
19  *    monetary gain.
20  *
21  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
25  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Please inquire about commercial licensing options at
35  * [email protected]
36  *
37  */
38 
39 #define BTSTACK_FILE__ "hfp_hf_demo.c"
40 
41 /*
42  * hfp_hs_demo.c
43  */
44 
45 // *****************************************************************************
46 /* EXAMPLE_START(hfp_hs_demo): HFP HF - Hands-Free
47  *
48  * @text This  HFP Hands-Free example demonstrates how to receive
49  * an output from a remote HFP audio gateway (AG), and,
50  * if HAVE_BTSTACK_STDIN is defined, how to control the HFP AG.
51  */
52 // *****************************************************************************
53 
54 
55 #include "btstack_config.h"
56 
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include "btstack.h"
62 
63 #include "sco_demo_util.h"
64 
65 uint8_t hfp_service_buffer[150];
66 const uint8_t   rfcomm_channel_nr = 1;
67 const char hfp_hf_service_name[] = "HFP HF Demo";
68 
69 #ifdef HAVE_BTSTACK_STDIN
70 // static const char * device_addr_string = "6C:72:E7:10:22:EE";
71 static const char * device_addr_string = "00:1A:7D:DA:71:13";
72 #endif
73 
74 static bd_addr_t device_addr;
75 
76 #ifdef HAVE_BTSTACK_STDIN
77 // 80:BE:05:D5:28:48
78 // prototypes
79 static void show_usage(void);
80 #endif
81 static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID;
82 static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID;
83 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
84 static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
85 #else
86 static uint8_t codecs[] = {HFP_CODEC_CVSD};
87 #endif
88 static uint16_t indicators[1] = {0x01};
89 static uint8_t  negotiated_codec = HFP_CODEC_CVSD;
90 static btstack_packet_callback_registration_t hci_event_callback_registration;
91 char cmd;
92 
93 static void dump_supported_codecs(void){
94     unsigned int i;
95     int mSBC_skipped = 0;
96     printf("Supported codecs: ");
97     for (i = 0; i < sizeof(codecs); i++){
98         switch(codecs[i]){
99             case HFP_CODEC_CVSD:
100                 printf("CVSD");
101                 break;
102             case HFP_CODEC_MSBC:
103                 if (hci_extended_sco_link_supported()){
104                     printf(", mSBC");
105                 } else {
106                     mSBC_skipped = 1;
107                 }
108                 break;
109             default:
110                 btstack_assert(false);
111                 break;
112         }
113     }
114     printf("\n");
115     if (mSBC_skipped){
116         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
117     }
118 }
119 
120 #ifdef HAVE_BTSTACK_STDIN
121 
122 // Testig User Interface
123 static void show_usage(void){
124     bd_addr_t iut_address;
125     gap_local_bd_addr(iut_address);
126 
127     printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
128     printf("\n");
129     printf("a - establish SLC to %s     | ", bd_addr_to_str(device_addr));
130     printf("A - release SLC connection to device\n");
131     printf("b - establish Audio connection             | B - release Audio connection\n");
132     printf("d - query network operator                 | D - Enable HFP AG registration status update via bitmap(IIA)\n");
133     printf("f - answer incoming call                   | F - Hangup call\n");
134     printf("g - query network operator name            | G - reject incoming call\n");
135     printf("i - dial 1234567                           | I - dial 7654321\n");
136     printf("j - dial #1                                | J - dial #99\n");
137     printf("o - Set speaker volume to 0  (minimum)     | O - Set speaker volume to 9  (default)\n");
138     printf("p - Set speaker volume to 12 (higher)      | P - Set speaker volume to 15 (maximum)\n");
139     printf("q - Set microphone gain to 0  (minimum)    | Q - Set microphone gain to 9  (default)\n");
140     printf("s - Set microphone gain to 12 (higher)     | S - Set microphone gain to 15 (maximum)\n");
141     printf("t - terminate connection\n");
142     printf("u - send 'user busy' (TWC 0)\n");
143     printf("U - end active call and accept other call' (TWC 1)\n");
144     printf("v - Swap active call call (TWC 2)          | V - Join held call (TWC 3)\n");
145     printf("w - Connect calls (TWC 4)                  | W - redial\n");
146     printf("c/C - disable/enable registration status update for all AG indicators\n");
147     printf("e/E - disable/enable reporting of the extended AG error result code\n");
148     printf("k/K - deactivate/activate call waiting notification\n");
149     printf("l/L - deactivate/activate calling line notification\n");
150     printf("m/M - deactivate/activate echo canceling and noise reduction\n");
151     printf("n/N - deactivate/activate voice recognition\n");
152     printf("0123456789#*-+ - send DTMF dial tones\n");
153     printf("x - request phone number for voice tag     | X - current call status (ECS)\n");
154     printf("y - release call with index 2 (ECC)        | Y - private consultation with call 2(ECC)\n");
155     printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n");
156     printf("{ - Accept held call(RHH 1)                | } - Reject held call(RHH 2)\n");
157     printf("? - Query Subscriber Number (NUM)\n");
158     printf("! - Update HF indicator with assigned number 1 (HFI)\n");
159     printf("\n");
160 }
161 
162 static void stdin_process(char c){
163     uint8_t status = ERROR_CODE_SUCCESS;
164 
165     cmd = c;    // used in packet handler
166 
167     if (cmd >= '0' && cmd <= '9'){
168         printf("DTMF Code: %c\n", cmd);
169         status = hfp_hf_send_dtmf_code(acl_handle, cmd);
170         return;
171     }
172 
173     switch (cmd){
174         case '#':
175         case '-':
176         case '+':
177         case '*':
178             log_info("USER:\'%c\'", cmd);
179             printf("DTMF Code: %c\n", cmd);
180             status = hfp_hf_send_dtmf_code(acl_handle, cmd);
181             break;
182         case 'a':
183             log_info("USER:\'%c\'", cmd);
184             printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
185             status = hfp_hf_establish_service_level_connection(device_addr);
186             break;
187         case 'A':
188             log_info("USER:\'%c\'", cmd);
189             printf("Release Service level connection.\n");
190             status = hfp_hf_release_service_level_connection(acl_handle);
191             break;
192         case 'b':
193             log_info("USER:\'%c\'", cmd);
194             printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
195             status = hfp_hf_establish_audio_connection(acl_handle);
196             break;
197         case 'B':
198             log_info("USER:\'%c\'", cmd);
199             printf("Release Audio service level connection.\n");
200             status = hfp_hf_release_audio_connection(acl_handle);
201             break;
202         case 'C':
203             log_info("USER:\'%c\'", cmd);
204             printf("Enable registration status update for all AG indicators.\n");
205             status = hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle);
206             break;
207         case 'c':
208             log_info("USER:\'%c\'", cmd);
209             printf("Disable registration status update for all AG indicators.\n");
210             status = hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle);
211             break;
212         case 'D':
213             log_info("USER:\'%c\'", cmd);
214             printf("Set HFP AG registration status update for individual indicators (0111111).\n");
215             status = hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63);
216             break;
217         case 'd':
218             log_info("USER:\'%c\'", cmd);
219             printf("Query network operator.\n");
220             status = hfp_hf_query_operator_selection(acl_handle);
221             break;
222         case 'E':
223             log_info("USER:\'%c\'", cmd);
224             printf("Enable reporting of the extended AG error result code.\n");
225             status = hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle);
226             break;
227         case 'e':
228             log_info("USER:\'%c\'", cmd);
229             printf("Disable reporting of the extended AG error result code.\n");
230             status = hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle);
231             break;
232         case 'f':
233             log_info("USER:\'%c\'", cmd);
234             printf("Answer incoming call.\n");
235             status = hfp_hf_answer_incoming_call(acl_handle);
236             break;
237         case 'F':
238             log_info("USER:\'%c\'", cmd);
239             printf("Hangup call.\n");
240             status = hfp_hf_terminate_call(acl_handle);
241             break;
242         case 'G':
243             log_info("USER:\'%c\'", cmd);
244             printf("Reject incoming call.\n");
245             status = hfp_hf_reject_incoming_call(acl_handle);
246             break;
247         case 'g':
248             log_info("USER:\'%c\'", cmd);
249             printf("Query operator.\n");
250             status = hfp_hf_query_operator_selection(acl_handle);
251             break;
252         case 't':
253             log_info("USER:\'%c\'", cmd);
254             printf("Terminate HCI connection.\n");
255             gap_disconnect(acl_handle);
256             break;
257         case 'i':
258             log_info("USER:\'%c\'", cmd);
259             printf("Dial 1234567\n");
260             status = hfp_hf_dial_number(acl_handle, "1234567");
261             break;
262         case 'I':
263             log_info("USER:\'%c\'", cmd);
264             printf("Dial 7654321\n");
265             status = hfp_hf_dial_number(acl_handle, "7654321");
266             break;
267         case 'j':
268             log_info("USER:\'%c\'", cmd);
269             printf("Dial #1\n");
270             status = hfp_hf_dial_memory(acl_handle,1);
271             break;
272         case 'J':
273             log_info("USER:\'%c\'", cmd);
274             printf("Dial #99\n");
275             status = hfp_hf_dial_memory(acl_handle,99);
276             break;
277         case 'k':
278             log_info("USER:\'%c\'", cmd);
279             printf("Deactivate call waiting notification\n");
280             status = hfp_hf_deactivate_call_waiting_notification(acl_handle);
281             break;
282         case 'K':
283             log_info("USER:\'%c\'", cmd);
284             printf("Activate call waiting notification\n");
285             status = hfp_hf_activate_call_waiting_notification(acl_handle);
286             break;
287         case 'l':
288             log_info("USER:\'%c\'", cmd);
289             printf("Deactivate calling line notification\n");
290             status = hfp_hf_deactivate_calling_line_notification(acl_handle);
291             break;
292         case 'L':
293             log_info("USER:\'%c\'", cmd);
294             printf("Activate calling line notification\n");
295             status = hfp_hf_activate_calling_line_notification(acl_handle);
296             break;
297         case 'm':
298             log_info("USER:\'%c\'", cmd);
299             printf("Deactivate echo canceling and noise reduction\n");
300             status = hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle);
301             break;
302         case 'M':
303             log_info("USER:\'%c\'", cmd);
304             printf("Activate echo canceling and noise reduction\n");
305             status = hfp_hf_activate_echo_canceling_and_noise_reduction(acl_handle);
306             break;
307         case 'n':
308             log_info("USER:\'%c\'", cmd);
309             printf("Deactivate voice recognition\n");
310             status = hfp_hf_deactivate_voice_recognition(acl_handle);
311             break;
312         case 'N':
313             log_info("USER:\'%c\'", cmd);
314             printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr));
315             status = hfp_hf_activate_voice_recognition(acl_handle);
316             break;
317         case 'o':
318             log_info("USER:\'%c\'", cmd);
319             printf("Set speaker gain to 0 (minimum)\n");
320             status = hfp_hf_set_speaker_gain(acl_handle, 0);
321             break;
322         case 'O':
323             log_info("USER:\'%c\'", cmd);
324             printf("Set speaker gain to 9 (default)\n");
325             status = hfp_hf_set_speaker_gain(acl_handle, 9);
326             break;
327         case 'p':
328             log_info("USER:\'%c\'", cmd);
329             printf("Set speaker gain to 12 (higher)\n");
330             status = hfp_hf_set_speaker_gain(acl_handle, 12);
331             break;
332         case 'P':
333             log_info("USER:\'%c\'", cmd);
334             printf("Set speaker gain to 15 (maximum)\n");
335             status = hfp_hf_set_speaker_gain(acl_handle, 15);
336             break;
337         case 'q':
338             log_info("USER:\'%c\'", cmd);
339             printf("Set microphone gain to 0\n");
340             status = hfp_hf_set_microphone_gain(acl_handle, 0);
341             break;
342         case 'Q':
343             log_info("USER:\'%c\'", cmd);
344             printf("Set microphone gain to 9\n");
345             status = hfp_hf_set_microphone_gain(acl_handle, 9);
346             break;
347         case 's':
348             log_info("USER:\'%c\'", cmd);
349             printf("Set microphone gain to 12\n");
350             status = hfp_hf_set_microphone_gain(acl_handle, 12);
351             break;
352         case 'S':
353             log_info("USER:\'%c\'", cmd);
354             printf("Set microphone gain to 15\n");
355             status = hfp_hf_set_microphone_gain(acl_handle, 15);
356             break;
357         case 'u':
358             log_info("USER:\'%c\'", cmd);
359             printf("Send 'user busy' (Three-Way Call 0)\n");
360             status = hfp_hf_user_busy(acl_handle);
361             break;
362         case 'U':
363             log_info("USER:\'%c\'", cmd);
364             printf("End active call and accept waiting/held call (Three-Way Call 1)\n");
365             status = hfp_hf_end_active_and_accept_other(acl_handle);
366             break;
367         case 'v':
368             log_info("USER:\'%c\'", cmd);
369             printf("Swap active call and hold/waiting call (Three-Way Call 2)\n");
370             status = hfp_hf_swap_calls(acl_handle);
371             break;
372         case 'V':
373             log_info("USER:\'%c\'", cmd);
374             printf("Join hold call (Three-Way Call 3)\n");
375             status = hfp_hf_join_held_call(acl_handle);
376             break;
377         case 'w':
378             log_info("USER:\'%c\'", cmd);
379             printf("Connect calls (Three-Way Call 4)\n");
380             status = hfp_hf_connect_calls(acl_handle);
381             break;
382         case 'W':
383             log_info("USER:\'%c\'", cmd);
384             printf("Redial\n");
385             status = hfp_hf_redial_last_number(acl_handle);
386             break;
387         case 'x':
388             log_info("USER:\'%c\'", cmd);
389             printf("Request phone number for voice tag\n");
390             status = hfp_hf_request_phone_number_for_voice_tag(acl_handle);
391             break;
392         case 'X':
393             log_info("USER:\'%c\'", cmd);
394             printf("Query current call status\n");
395             status = hfp_hf_query_current_call_status(acl_handle);
396             break;
397         case 'y':
398             log_info("USER:\'%c\'", cmd);
399             printf("Release call with index 2\n");
400             status = hfp_hf_release_call_with_index(acl_handle, 2);
401             break;
402         case 'Y':
403             log_info("USER:\'%c\'", cmd);
404             printf("Private consultation with call 2\n");
405             status = hfp_hf_private_consultation_with_call(acl_handle, 2);
406             break;
407         case '[':
408             log_info("USER:\'%c\'", cmd);
409             printf("Query Response and Hold status (RHH ?)\n");
410             status = hfp_hf_rrh_query_status(acl_handle);
411             break;
412         case ']':
413             log_info("USER:\'%c\'", cmd);
414             printf("Place call in a response and held state (RHH 0)\n");
415             status = hfp_hf_rrh_hold_call(acl_handle);
416            break;
417         case '{':
418             log_info("USER:\'%c\'", cmd);
419             printf("Accept held call (RHH 1)\n");
420             status = hfp_hf_rrh_accept_held_call(acl_handle);
421             break;
422         case '}':
423             log_info("USER:\'%c\'", cmd);
424             printf("Reject held call (RHH 2)\n");
425             status = hfp_hf_rrh_reject_held_call(acl_handle);
426             break;
427         case '?':
428             log_info("USER:\'%c\'", cmd);
429             printf("Query Subscriber Number\n");
430             status = hfp_hf_query_subscriber_number(acl_handle);
431             break;
432         case '!':
433             log_info("USER:\'%c\'", cmd);
434             printf("Update HF indicator with assigned number 1 (HFI)\n");
435             status = hfp_hf_set_hf_indicator(acl_handle, 1, 1);
436             break;
437         default:
438             show_usage();
439             break;
440     }
441 
442     if (status != ERROR_CODE_SUCCESS){
443         printf("Could not perform command, status 0x%02x\n", status);
444     }
445 }
446 #endif
447 
448 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
449     UNUSED(channel);
450     uint8_t status;
451 
452     switch (packet_type){
453 
454         case HCI_SCO_DATA_PACKET:
455             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
456             sco_demo_receive(event, event_size);
457             break;
458 
459         case HCI_EVENT_PACKET:
460             switch (hci_event_packet_get_type(event)){
461                 case HCI_EVENT_SCO_CAN_SEND_NOW:
462                     sco_demo_send(sco_handle);
463                     break;
464 
465                 case HCI_EVENT_COMMAND_COMPLETE:
466                     if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){
467                         dump_supported_codecs();
468                     }
469                     break;
470 
471                 case HCI_EVENT_HFP_META:
472                     switch (hci_event_hfp_meta_get_subevent_code(event)) {
473                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
474                             acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
475                             hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
476                             printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr));
477                             break;
478                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
479                             acl_handle = HCI_CON_HANDLE_INVALID;
480                             printf("Service level connection released.\n\n");
481                             break;
482                         case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
483                             status = hfp_subevent_audio_connection_established_get_status(event);
484                             if (status != ERROR_CODE_SUCCESS){
485                                 printf("Audio connection establishment failed with status 0x%02x\n", status);
486                             } else {
487                                 sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
488                                 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
489                                 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
490                                 switch (negotiated_codec){
491                                     case 0x01:
492                                         printf("Using CVSD codec.\n");
493                                         break;
494                                     case 0x02:
495                                         printf("Using mSBC codec.\n");
496                                         break;
497                                     default:
498                                         printf("Using unknown codec 0x%02x.\n", negotiated_codec);
499                                         break;
500                                 }
501                                 sco_demo_set_codec(negotiated_codec);
502                                 hci_request_sco_can_send_now_event();
503                             }
504                             break;
505                         case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
506                             sco_handle = HCI_CON_HANDLE_INVALID;
507                             printf("Audio connection released\n");
508                             sco_demo_close();
509                             break;
510                         case HFP_SUBEVENT_COMPLETE:
511                             status = hfp_subevent_complete_get_status(event);
512                             if (status == ERROR_CODE_SUCCESS){
513                                 printf("Cmd \'%c\' succeeded\n", cmd);
514                             } else {
515                                 printf("Cmd \'%c\' failed with status 0x%02x\n", cmd, status);
516                             }
517                             break;
518                         case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED:
519                             printf("AG_INDICATOR_STATUS_CHANGED, AG indicator (index: %d) to: %d of range [%d, %d], name '%s'\n",
520                                 hfp_subevent_ag_indicator_status_changed_get_indicator_index(event),
521                                 hfp_subevent_ag_indicator_status_changed_get_indicator_status(event),
522                                 hfp_subevent_ag_indicator_status_changed_get_indicator_min_range(event),
523                                 hfp_subevent_ag_indicator_status_changed_get_indicator_max_range(event),
524                                 (const char*) hfp_subevent_ag_indicator_status_changed_get_indicator_name(event));
525                             break;
526                         case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED:
527                             printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n",
528                                 hfp_subevent_network_operator_changed_get_network_operator_mode(event),
529                                 hfp_subevent_network_operator_changed_get_network_operator_format(event),
530                                 (char *) hfp_subevent_network_operator_changed_get_network_operator_name(event));
531                             break;
532                         case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR:
533                             printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : 0x%02x\n",
534                                 hfp_subevent_extended_audio_gateway_error_get_error(event));
535                             break;
536                         case HFP_SUBEVENT_RING:
537                             printf("** Ring **\n");
538                             break;
539                         case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG:
540                             printf("Phone number for voice tag: %s\n",
541                                 (const char *) hfp_subevent_number_for_voice_tag_get_number(event));
542                             break;
543                         case HFP_SUBEVENT_SPEAKER_VOLUME:
544                             printf("Speaker volume: gain %u\n",
545                                 hfp_subevent_speaker_volume_get_gain(event));
546                             break;
547                         case HFP_SUBEVENT_MICROPHONE_VOLUME:
548                             printf("Microphone volume: gain %u\n",
549                                 hfp_subevent_microphone_volume_get_gain(event));
550                             break;
551                         case HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION:
552                             printf("Caller ID, number %s\n", hfp_subevent_calling_line_identification_notification_get_number(event));
553                             break;
554                         case HFP_SUBEVENT_ENHANCED_CALL_STATUS:
555                             printf("Enhanced call status:\n");
556                             printf("  - call index: %d \n", hfp_subevent_enhanced_call_status_get_clcc_idx(event));
557                             printf("  - direction : %s \n", hfp_enhanced_call_dir2str(hfp_subevent_enhanced_call_status_get_clcc_dir(event)));
558                             printf("  - status    : %s \n", hfp_enhanced_call_status2str(hfp_subevent_enhanced_call_status_get_clcc_status(event)));
559                             printf("  - mode      : %s \n", hfp_enhanced_call_mode2str(hfp_subevent_enhanced_call_status_get_clcc_mode(event)));
560                             printf("  - multipart : %s \n", hfp_enhanced_call_mpty2str(hfp_subevent_enhanced_call_status_get_clcc_mpty(event)));
561                             printf("  - type      : %d \n", hfp_subevent_enhanced_call_status_get_bnip_type(event));
562                             printf("  - number    : %s \n", hfp_subevent_enhanced_call_status_get_bnip_number(event));
563                             break;
564                         case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS:
565                             status = hfp_subevent_voice_recognition_status_get_status(event);
566                             if (status != ERROR_CODE_SUCCESS){
567                                 printf("Voice Recognition command failed with status 0x%02x\n", status);
568                                 break;
569                             }
570                             switch(hfp_subevent_voice_recognition_status_get_state(event)){
571                                 case 0:
572                                     printf("Voice recognition status deactivated\n");
573                                     break;
574                                 case 1:
575                                     printf("Voice recognition status activated\n");
576                                     break;
577                                 default:
578                                     btstack_assert(false);
579                                     break;
580                             }
581                             break;
582                         default:
583                             break;
584                     }
585                     break;
586 
587                 default:
588                     break;
589             }
590             break;
591 
592         default:
593             break;
594     }
595 
596 }
597 
598 /* @section Main Application Setup
599  *
600  * @text Listing MainConfiguration shows main application code.
601  * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it.
602  * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers.
603  * The stdin_process callback allows for sending commands to the HFP AG.
604  * At the end the Bluetooth stack is started.
605  */
606 
607 /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */
608 int btstack_main(int argc, const char * argv[]);
609 int btstack_main(int argc, const char * argv[]){
610     (void)argc;
611     (void)argv;
612 
613     sco_demo_init();
614 
615     gap_discoverable_control(1);
616     gap_set_class_of_device(0x200408);
617     gap_set_local_name("HFP HF Demo 00:00:00:00:00:00");
618 
619     // init L2CAP
620     l2cap_init();
621 
622     uint16_t hf_supported_features          =
623         (1<<HFP_HFSF_ESCO_S4)               |
624         (1<<HFP_HFSF_CLI_PRESENTATION_CAPABILITY) |
625         (1<<HFP_HFSF_HF_INDICATORS)         |
626         (1<<HFP_HFSF_CODEC_NEGOTIATION)     |
627         (1<<HFP_HFSF_ENHANCED_CALL_STATUS)  |
628         (1<<HFP_HFSF_VOICE_RECOGNITION_FUNCTION)  |
629         (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL);
630     int wide_band_speech = 1;
631 
632     rfcomm_init();
633     hfp_hf_init(rfcomm_channel_nr);
634     hfp_hf_init_supported_features(hf_supported_features);
635     hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
636     hfp_hf_init_codecs(sizeof(codecs), codecs);
637 
638     sdp_init();
639     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
640     hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech);
641     printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer));
642     sdp_register_service(hfp_service_buffer);
643 
644     // register for HCI events and SCO packets
645     hci_event_callback_registration.callback = &packet_handler;
646     hci_add_event_handler(&hci_event_callback_registration);
647     hci_register_sco_packet_handler(&packet_handler);
648     hci_register_sco_packet_handler(&packet_handler);
649 
650     // register for HFP events
651     hfp_hf_register_packet_handler(packet_handler);
652 
653 #ifdef HAVE_BTSTACK_STDIN
654     // parse human readable Bluetooth address
655     sscanf_bd_addr(device_addr_string, device_addr);
656     btstack_stdin_setup(stdin_process);
657 #endif
658     // turn on!
659     hci_power_control(HCI_POWER_ON);
660     return 0;
661 }
662 /* LISTING_END */
663 /* EXAMPLE_END */
664