xref: /btstack/example/hfp_hf_demo.c (revision 360d44f609bbebbf954ea85f461752a6b07644cd)
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 BLUEKITCHEN
25  * GMBH 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 
70 #ifdef HAVE_BTSTACK_STDIN
71 // static const char * device_addr_string = "6C:72:E7:10:22:EE";
72 static const char * device_addr_string = "00:02:72:DC:31:C1";
73 #endif
74 
75 static bd_addr_t device_addr;
76 
77 #ifdef HAVE_BTSTACK_STDIN
78 // 80:BE:05:D5:28:48
79 // prototypes
80 static void show_usage(void);
81 #endif
82 static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID;
83 static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID;
84 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
85 static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC};
86 #else
87 static uint8_t codecs[] = {HFP_CODEC_CVSD};
88 #endif
89 static uint16_t indicators[1] = {0x01};
90 static uint8_t  negotiated_codec = HFP_CODEC_CVSD;
91 static btstack_packet_callback_registration_t hci_event_callback_registration;
92 static char cmd;
93 
94 static void dump_supported_codecs(void){
95     unsigned int i;
96     int mSBC_skipped = 0;
97     printf("Supported codecs: ");
98     for (i = 0; i < sizeof(codecs); i++){
99         switch(codecs[i]){
100             case HFP_CODEC_CVSD:
101                 printf("CVSD");
102                 break;
103             case HFP_CODEC_MSBC:
104                 if (hci_extended_sco_link_supported()){
105                     printf(", mSBC");
106                 } else {
107                     mSBC_skipped = 1;
108                 }
109                 break;
110             default:
111                 btstack_assert(false);
112                 break;
113         }
114     }
115     printf("\n");
116     if (mSBC_skipped){
117         printf("mSBC codec disabled because eSCO not supported by local controller.\n");
118     }
119 }
120 
121 static void report_status(uint8_t status, const char * message){
122     if (status != ERROR_CODE_SUCCESS){
123         printf("%s command failed, status 0x%02x\n", message, status);
124     } else {
125         printf("%s command successful\n", message);
126     }
127 }
128 
129 #ifdef HAVE_BTSTACK_STDIN
130 
131 // Testig User Interface
132 static void show_usage(void){
133     bd_addr_t iut_address;
134     gap_local_bd_addr(iut_address);
135 
136     printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console %s ---\n", bd_addr_to_str(iut_address));
137     printf("\n");
138     printf("a - establish SLC to %s     | ", bd_addr_to_str(device_addr));
139     printf("A - release SLC connection to device\n");
140     printf("b - establish Audio connection             | B - release Audio connection\n");
141     printf("d - query network operator                 | D - Enable HFP AG registration status update via bitmap(IIA)\n");
142     printf("f - answer incoming call                   | F - Hangup call\n");
143     printf("g - query network operator name            | G - reject incoming call\n");
144     printf("i - dial 1234567                           | I - dial 7654321\n");
145     printf("j - dial #1                                | J - dial #99\n");
146     printf("o - Set speaker volume to 0  (minimum)     | O - Set speaker volume to 9  (default)\n");
147     printf("p - Set speaker volume to 12 (higher)      | P - Set speaker volume to 15 (maximum)\n");
148     printf("q - Set microphone gain to 0  (minimum)    | Q - Set microphone gain to 9  (default)\n");
149     printf("s - Set microphone gain to 12 (higher)     | S - Set microphone gain to 15 (maximum)\n");
150     printf("t - terminate connection\n");
151     printf("u - send 'user busy' (TWC 0)\n");
152     printf("U - end active call and accept other call' (TWC 1)\n");
153     printf("v - Swap active call call (TWC 2)          | V - Join held call (TWC 3)\n");
154     printf("w - Connect calls (TWC 4)                  | W - redial\n");
155     printf("m - deactivate echo canceling and noise reduction\n");
156     printf("c/C - disable/enable registration status update for all AG indicators\n");
157     printf("e/E - disable/enable reporting of the extended AG error result code\n");
158     printf("k/K - deactivate/activate call waiting notification\n");
159     printf("l/L - deactivate/activate calling line notification\n");
160     printf("n/N - deactivate/activate voice recognition\n");
161 
162     printf("0123456789#*-+ - send DTMF dial tones\n");
163     printf("x - request phone number for voice tag     | X - current call status (ECS)\n");
164     printf("y - release call with index 2 (ECC)        | Y - private consultation with call 2(ECC)\n");
165     printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n");
166     printf("{ - Accept held call(RHH 1)                | } - Reject held call(RHH 2)\n");
167     printf("? - Query Subscriber Number (NUM)\n");
168     printf("! - Update HF indicator with assigned number 1 (HFI)\n");
169     printf("\n");
170 }
171 
172 static void stdin_process(char c){
173     uint8_t status = ERROR_CODE_SUCCESS;
174 
175     cmd = c;    // used in packet handler
176 
177     if (cmd >= '0' && cmd <= '9'){
178         printf("DTMF Code: %c\n", cmd);
179         status = hfp_hf_send_dtmf_code(acl_handle, cmd);
180         return;
181     }
182 
183     switch (cmd){
184         case '#':
185         case '-':
186         case '+':
187         case '*':
188             log_info("USER:\'%c\'", cmd);
189             printf("DTMF Code: %c\n", cmd);
190             status = hfp_hf_send_dtmf_code(acl_handle, cmd);
191             break;
192         case 'a':
193             log_info("USER:\'%c\'", cmd);
194             printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
195             status = hfp_hf_establish_service_level_connection(device_addr);
196             break;
197         case 'A':
198             log_info("USER:\'%c\'", cmd);
199             printf("Release Service level connection.\n");
200             status = hfp_hf_release_service_level_connection(acl_handle);
201             break;
202         case 'b':
203             log_info("USER:\'%c\'", cmd);
204             printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr));
205             status = hfp_hf_establish_audio_connection(acl_handle);
206             break;
207         case 'B':
208             log_info("USER:\'%c\'", cmd);
209             printf("Release Audio service level connection.\n");
210             status = hfp_hf_release_audio_connection(acl_handle);
211             break;
212         case 'C':
213             log_info("USER:\'%c\'", cmd);
214             printf("Enable registration status update for all AG indicators.\n");
215             status = hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle);
216             break;
217         case 'c':
218             log_info("USER:\'%c\'", cmd);
219             printf("Disable registration status update for all AG indicators.\n");
220             status = hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle);
221             break;
222         case 'D':
223             log_info("USER:\'%c\'", cmd);
224             printf("Set HFP AG registration status update for individual indicators (0111111).\n");
225             status = hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63);
226             break;
227         case 'd':
228             log_info("USER:\'%c\'", cmd);
229             printf("Query network operator.\n");
230             status = hfp_hf_query_operator_selection(acl_handle);
231             break;
232         case 'E':
233             log_info("USER:\'%c\'", cmd);
234             printf("Enable reporting of the extended AG error result code.\n");
235             status = hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle);
236             break;
237         case 'e':
238             log_info("USER:\'%c\'", cmd);
239             printf("Disable reporting of the extended AG error result code.\n");
240             status = hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle);
241             break;
242         case 'f':
243             log_info("USER:\'%c\'", cmd);
244             printf("Answer incoming call.\n");
245             status = hfp_hf_answer_incoming_call(acl_handle);
246             break;
247         case 'F':
248             log_info("USER:\'%c\'", cmd);
249             printf("Hangup call.\n");
250             status = hfp_hf_terminate_call(acl_handle);
251             break;
252         case 'G':
253             log_info("USER:\'%c\'", cmd);
254             printf("Reject incoming call.\n");
255             status = hfp_hf_reject_incoming_call(acl_handle);
256             break;
257         case 'g':
258             log_info("USER:\'%c\'", cmd);
259             printf("Query operator.\n");
260             status = hfp_hf_query_operator_selection(acl_handle);
261             break;
262         case 't':
263             log_info("USER:\'%c\'", cmd);
264             printf("Terminate HCI connection.\n");
265             gap_disconnect(acl_handle);
266             break;
267         case 'i':
268             log_info("USER:\'%c\'", cmd);
269             printf("Dial 1234567\n");
270             status = hfp_hf_dial_number(acl_handle, "1234567");
271             break;
272         case 'I':
273             log_info("USER:\'%c\'", cmd);
274             printf("Dial 7654321\n");
275             status = hfp_hf_dial_number(acl_handle, "7654321");
276             break;
277         case 'j':
278             log_info("USER:\'%c\'", cmd);
279             printf("Dial #1\n");
280             status = hfp_hf_dial_memory(acl_handle,1);
281             break;
282         case 'J':
283             log_info("USER:\'%c\'", cmd);
284             printf("Dial #99\n");
285             status = hfp_hf_dial_memory(acl_handle,99);
286             break;
287         case 'k':
288             log_info("USER:\'%c\'", cmd);
289             printf("Deactivate call waiting notification\n");
290             status = hfp_hf_deactivate_call_waiting_notification(acl_handle);
291             break;
292         case 'K':
293             log_info("USER:\'%c\'", cmd);
294             printf("Activate call waiting notification\n");
295             status = hfp_hf_activate_call_waiting_notification(acl_handle);
296             break;
297         case 'l':
298             log_info("USER:\'%c\'", cmd);
299             printf("Deactivate calling line notification\n");
300             status = hfp_hf_deactivate_calling_line_notification(acl_handle);
301             break;
302         case 'L':
303             log_info("USER:\'%c\'", cmd);
304             printf("Activate calling line notification\n");
305             status = hfp_hf_activate_calling_line_notification(acl_handle);
306             break;
307         case 'm':
308             log_info("USER:\'%c\'", cmd);
309             printf("Deactivate echo canceling and noise reduction\n");
310             status = hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle);
311             break;
312         case 'n':
313             log_info("USER:\'%c\'", cmd);
314             printf("Deactivate voice recognition\n");
315             status = hfp_hf_deactivate_voice_recognition(acl_handle);
316             break;
317         case 'N':
318             log_info("USER:\'%c\'", cmd);
319             printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr));
320             status = hfp_hf_activate_voice_recognition(acl_handle);
321             break;
322         case 'o':
323             log_info("USER:\'%c\'", cmd);
324             printf("Set speaker gain to 0 (minimum)\n");
325             status = hfp_hf_set_speaker_gain(acl_handle, 0);
326             break;
327         case 'O':
328             log_info("USER:\'%c\'", cmd);
329             printf("Set speaker gain to 9 (default)\n");
330             status = hfp_hf_set_speaker_gain(acl_handle, 9);
331             break;
332         case 'p':
333             log_info("USER:\'%c\'", cmd);
334             printf("Set speaker gain to 12 (higher)\n");
335             status = hfp_hf_set_speaker_gain(acl_handle, 12);
336             break;
337         case 'P':
338             log_info("USER:\'%c\'", cmd);
339             printf("Set speaker gain to 15 (maximum)\n");
340             status = hfp_hf_set_speaker_gain(acl_handle, 15);
341             break;
342         case 'q':
343             log_info("USER:\'%c\'", cmd);
344             printf("Set microphone gain to 0\n");
345             status = hfp_hf_set_microphone_gain(acl_handle, 0);
346             break;
347         case 'Q':
348             log_info("USER:\'%c\'", cmd);
349             printf("Set microphone gain to 9\n");
350             status = hfp_hf_set_microphone_gain(acl_handle, 9);
351             break;
352         case 's':
353             log_info("USER:\'%c\'", cmd);
354             printf("Set microphone gain to 12\n");
355             status = hfp_hf_set_microphone_gain(acl_handle, 12);
356             break;
357         case 'S':
358             log_info("USER:\'%c\'", cmd);
359             printf("Set microphone gain to 15\n");
360             status = hfp_hf_set_microphone_gain(acl_handle, 15);
361             break;
362         case 'u':
363             log_info("USER:\'%c\'", cmd);
364             printf("Send 'user busy' (Three-Way Call 0)\n");
365             status = hfp_hf_user_busy(acl_handle);
366             break;
367         case 'U':
368             log_info("USER:\'%c\'", cmd);
369             printf("End active call and accept waiting/held call (Three-Way Call 1)\n");
370             status = hfp_hf_end_active_and_accept_other(acl_handle);
371             break;
372         case 'v':
373             log_info("USER:\'%c\'", cmd);
374             printf("Swap active call and hold/waiting call (Three-Way Call 2)\n");
375             status = hfp_hf_swap_calls(acl_handle);
376             break;
377         case 'V':
378             log_info("USER:\'%c\'", cmd);
379             printf("Join hold call (Three-Way Call 3)\n");
380             status = hfp_hf_join_held_call(acl_handle);
381             break;
382         case 'w':
383             log_info("USER:\'%c\'", cmd);
384             printf("Connect calls (Three-Way Call 4)\n");
385             status = hfp_hf_connect_calls(acl_handle);
386             break;
387         case 'W':
388             log_info("USER:\'%c\'", cmd);
389             printf("Redial\n");
390             status = hfp_hf_redial_last_number(acl_handle);
391             break;
392         case 'x':
393             log_info("USER:\'%c\'", cmd);
394             printf("Request phone number for voice tag\n");
395             status = hfp_hf_request_phone_number_for_voice_tag(acl_handle);
396             break;
397         case 'X':
398             log_info("USER:\'%c\'", cmd);
399             printf("Query current call status\n");
400             status = hfp_hf_query_current_call_status(acl_handle);
401             break;
402         case 'y':
403             log_info("USER:\'%c\'", cmd);
404             printf("Release call with index 2\n");
405             status = hfp_hf_release_call_with_index(acl_handle, 2);
406             break;
407         case 'Y':
408             log_info("USER:\'%c\'", cmd);
409             printf("Private consultation with call 2\n");
410             status = hfp_hf_private_consultation_with_call(acl_handle, 2);
411             break;
412         case '[':
413             log_info("USER:\'%c\'", cmd);
414             printf("Query Response and Hold status (RHH ?)\n");
415             status = hfp_hf_rrh_query_status(acl_handle);
416             break;
417         case ']':
418             log_info("USER:\'%c\'", cmd);
419             printf("Place call in a response and held state (RHH 0)\n");
420             status = hfp_hf_rrh_hold_call(acl_handle);
421            break;
422         case '{':
423             log_info("USER:\'%c\'", cmd);
424             printf("Accept held call (RHH 1)\n");
425             status = hfp_hf_rrh_accept_held_call(acl_handle);
426             break;
427         case '}':
428             log_info("USER:\'%c\'", cmd);
429             printf("Reject held call (RHH 2)\n");
430             status = hfp_hf_rrh_reject_held_call(acl_handle);
431             break;
432         case '?':
433             log_info("USER:\'%c\'", cmd);
434             printf("Query Subscriber Number\n");
435             status = hfp_hf_query_subscriber_number(acl_handle);
436             break;
437         case '!':
438             log_info("USER:\'%c\'", cmd);
439             printf("Update HF indicator with assigned number 1 (HFI)\n");
440             status = hfp_hf_set_hf_indicator(acl_handle, 1, 1);
441             break;
442         default:
443             show_usage();
444             break;
445     }
446 
447     if (status != ERROR_CODE_SUCCESS){
448         printf("Could not perform command, status 0x%02x\n", status);
449     }
450 }
451 #endif
452 
453 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
454     UNUSED(channel);
455     bd_addr_t event_addr;
456 
457     switch (packet_type){
458 
459         case HCI_SCO_DATA_PACKET:
460             // forward received SCO / audio packets to SCO component
461             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
462             sco_demo_receive(event, event_size);
463             break;
464 
465         case HCI_EVENT_PACKET:
466             switch (hci_event_packet_get_type(event)){
467                 case BTSTACK_EVENT_STATE:
468                     // list supported codecs after stack has started up
469                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
470                     dump_supported_codecs();
471                     break;
472 
473                 case HCI_EVENT_PIN_CODE_REQUEST:
474                     // inform about pin code request and respond with '0000'
475                     printf("Pin code request - using '0000'\n");
476                     hci_event_pin_code_request_get_bd_addr(event, event_addr);
477                     gap_pin_code_response(event_addr, "0000");
478                     break;
479 
480                 case HCI_EVENT_SCO_CAN_SEND_NOW:
481                     sco_demo_send(sco_handle);
482                     break;
483 
484                 default:
485                     break;
486             }
487             break;
488 
489         default:
490             break;
491     }
492 
493 }
494 
495 static void hfp_hf_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
496     UNUSED(channel);
497     uint8_t status;
498     bd_addr_t event_addr;
499 
500     switch (packet_type){
501 
502         case HCI_SCO_DATA_PACKET:
503             if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break;
504             sco_demo_receive(event, event_size);
505             break;
506 
507         case HCI_EVENT_PACKET:
508             switch (hci_event_packet_get_type(event)){
509                 case BTSTACK_EVENT_STATE:
510                     if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break;
511                     dump_supported_codecs();
512                     break;
513 
514                 case HCI_EVENT_PIN_CODE_REQUEST:
515                     // inform about pin code request
516                     printf("Pin code request - using '0000'\n");
517                     hci_event_pin_code_request_get_bd_addr(event, event_addr);
518                     gap_pin_code_response(event_addr, "0000");
519                     break;
520 
521                 case HCI_EVENT_SCO_CAN_SEND_NOW:
522                     sco_demo_send(sco_handle);
523                     break;
524 
525                 case HCI_EVENT_HFP_META:
526                     switch (hci_event_hfp_meta_get_subevent_code(event)) {
527                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
528                             status = hfp_subevent_service_level_connection_established_get_status(event);
529                             if (status != ERROR_CODE_SUCCESS){
530                                 printf("Connection failed, status 0x%02x\n", status);
531                                 break;
532                             }
533                             acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event);
534                             hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr);
535                             printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr));
536                             break;
537                         case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED:
538                             acl_handle = HCI_CON_HANDLE_INVALID;
539                             printf("Service level connection released.\n\n");
540                             break;
541                         case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED:
542                             status = hfp_subevent_audio_connection_established_get_status(event);
543                             if (status != ERROR_CODE_SUCCESS){
544                                 printf("Audio connection establishment failed with status 0x%02x\n", status);
545                                 break;
546                             }
547                             sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event);
548                             printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle);
549                             negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event);
550                             switch (negotiated_codec){
551                                 case 0x01:
552                                     printf("Using CVSD codec.\n");
553                                     break;
554                                 case 0x02:
555                                     printf("Using mSBC codec.\n");
556                                     break;
557                                 default:
558                                     printf("Using unknown codec 0x%02x.\n", negotiated_codec);
559                                     break;
560                             }
561                             sco_demo_set_codec(negotiated_codec);
562                             hci_request_sco_can_send_now_event();
563                             break;
564 
565                         case HFP_SUBEVENT_CALL_ANSWERED:
566                             printf("Call answered\n");
567                             break;
568 
569                         case HFP_SUBEVENT_CALL_TERMINATED:
570                             printf("Call terminated\n");
571                             break;
572 
573                         case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED:
574                             sco_handle = HCI_CON_HANDLE_INVALID;
575                             printf("Audio connection released\n");
576                             sco_demo_close();
577                             break;
578                         case  HFP_SUBEVENT_COMPLETE:
579                             status = hfp_subevent_complete_get_status(event);
580                             if (status == ERROR_CODE_SUCCESS){
581                                 printf("Cmd \'%c\' succeeded\n", cmd);
582                             } else {
583                                 printf("Cmd \'%c\' failed with status 0x%02x\n", cmd, status);
584                             }
585                             break;
586 
587                         case HFP_SUBEVENT_AG_INDICATOR_MAPPING:
588                             printf("AG Indicator Mapping | INDEX %d: range [%d, %d], name '%s'\n",
589                                 hfp_subevent_ag_indicator_mapping_get_indicator_index(event),
590                                 hfp_subevent_ag_indicator_mapping_get_indicator_min_range(event),
591                                 hfp_subevent_ag_indicator_mapping_get_indicator_max_range(event),
592                                 (const char*) hfp_subevent_ag_indicator_mapping_get_indicator_name(event));
593                             break;
594 
595                         case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED:
596                             printf("AG Indicator Status  | INDEX %d: status 0x%02x, '%s'\n",
597                                 hfp_subevent_ag_indicator_status_changed_get_indicator_index(event),
598                                 hfp_subevent_ag_indicator_status_changed_get_indicator_status(event),
599                                 (const char*) hfp_subevent_ag_indicator_status_changed_get_indicator_name(event));
600                             break;
601                         case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED:
602                             printf("NETWORK_OPERATOR_CHANGED, operator mode %d, format %d, name %s\n",
603                                 hfp_subevent_network_operator_changed_get_network_operator_mode(event),
604                                 hfp_subevent_network_operator_changed_get_network_operator_format(event),
605                                 (char *) hfp_subevent_network_operator_changed_get_network_operator_name(event));
606                             break;
607                         case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR:
608                             printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status 0x%02x\n",
609                                 hfp_subevent_extended_audio_gateway_error_get_error(event));
610                             break;
611                         case HFP_SUBEVENT_START_RINGING:
612                             printf("** START Ringing **\n");
613                             break;
614                         case HFP_SUBEVENT_RING:
615                             printf("** Ring **\n");
616                             break;
617                         case HFP_SUBEVENT_STOP_RINGING:
618                             printf("** STOP Ringing **\n");
619                             break;
620                         case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG:
621                             printf("Phone number for voice tag: %s\n",
622                                 (const char *) hfp_subevent_number_for_voice_tag_get_number(event));
623                             break;
624                         case HFP_SUBEVENT_SPEAKER_VOLUME:
625                             printf("Speaker volume: gain %u\n",
626                                 hfp_subevent_speaker_volume_get_gain(event));
627                             break;
628                         case HFP_SUBEVENT_MICROPHONE_VOLUME:
629                             printf("Microphone volume: gain %u\n",
630                             hfp_subevent_microphone_volume_get_gain(event));
631                             break;
632                         case HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION:
633                             printf("Caller ID, number '%s', alpha '%s'\n", (const char *) hfp_subevent_calling_line_identification_notification_get_number(event),
634                                    (const char *) hfp_subevent_calling_line_identification_notification_get_alpha(event));
635                             break;
636                         case HFP_SUBEVENT_ENHANCED_CALL_STATUS:
637                             printf("Enhanced call status:\n");
638                             printf("  - call index: %d \n", hfp_subevent_enhanced_call_status_get_clcc_idx(event));
639                             printf("  - direction : %s \n", hfp_enhanced_call_dir2str(hfp_subevent_enhanced_call_status_get_clcc_dir(event)));
640                             printf("  - status    : %s \n", hfp_enhanced_call_status2str(hfp_subevent_enhanced_call_status_get_clcc_status(event)));
641                             printf("  - mode      : %s \n", hfp_enhanced_call_mode2str(hfp_subevent_enhanced_call_status_get_clcc_mode(event)));
642                             printf("  - multipart : %s \n", hfp_enhanced_call_mpty2str(hfp_subevent_enhanced_call_status_get_clcc_mpty(event)));
643                             printf("  - type      : %d \n", hfp_subevent_enhanced_call_status_get_bnip_type(event));
644                             printf("  - number    : %s \n", hfp_subevent_enhanced_call_status_get_bnip_number(event));
645                             break;
646 
647                         case HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED:
648                             status = hfp_subevent_voice_recognition_activated_get_status(event);
649                             if (status != ERROR_CODE_SUCCESS){
650                                 printf("Voice Recognition Activate command failed, status 0x%02x\n", status);
651                                 break;
652                             }
653 
654                             switch (hfp_subevent_voice_recognition_activated_get_enhanced(event)){
655                                 case 0:
656                                     printf("\nVoice recognition ACTIVATED\n\n");
657                                     break;
658                                 default:
659                                     printf("\nEnhanced voice recognition ACTIVATED.\n");
660                                     printf("Start new audio enhanced voice recognition session %s\n\n", bd_addr_to_str(device_addr));
661                                     status = hfp_hf_enhanced_voice_recognition_report_ready_for_audio(acl_handle);
662                                     break;
663                             }
664                             break;
665 
666                         case HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED:
667                             status = hfp_subevent_voice_recognition_deactivated_get_status(event);
668                             if (status != ERROR_CODE_SUCCESS){
669                                 printf("Voice Recognition Deactivate command failed, status 0x%02x\n", status);
670                                 break;
671                             }
672                             printf("\nVoice Recognition DEACTIVATED\n\n");
673                             break;
674 
675                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO:
676                             status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event);
677                             report_status(status, "Enhanced Voice recognition: READY FOR AUDIO");
678                             break;
679 
680                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT:
681                             printf("\nEnhanced Voice recognition AG status: AG READY TO ACCEPT AUDIO INPUT\n\n");
682                             break;
683                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND:
684                             printf("\nEnhanced Voice recognition AG status: AG IS STARTING SOUND\n\n");
685                             break;
686                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT:
687                             printf("\nEnhanced Voice recognition AG status: AG IS PROCESSING AUDIO INPUT\n\n");
688                             break;
689 
690                         case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE:
691                             printf("\nEnhanced Voice recognition AG message: \'%s\'\n", hfp_subevent_enhanced_voice_recognition_ag_message_get_text(event));
692                             break;
693 
694                         case HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE:
695                             status = hfp_subevent_echo_canceling_and_noise_reduction_deactivate_get_status(event);
696                             report_status(status, "Echo Canceling and Noise Reduction Deactivate");
697                             break;
698                         default:
699                             break;
700                     }
701                     break;
702 
703                 default:
704                     break;
705             }
706             break;
707 
708         default:
709             break;
710     }
711 
712 }
713 
714 /* @section Main Application Setup
715  *
716  * @text Listing MainConfiguration shows main application code.
717  * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it.
718  * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers.
719  * The stdin_process callback allows for sending commands to the HFP AG.
720  * At the end the Bluetooth stack is started.
721  */
722 
723 /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */
724 int btstack_main(int argc, const char * argv[]);
725 int btstack_main(int argc, const char * argv[]){
726     (void)argc;
727     (void)argv;
728 
729     // Init protocols
730     // init L2CAP
731     l2cap_init();
732     rfcomm_init();
733     sdp_init();
734 #ifdef ENABLE_BLE
735     // Initialize LE Security Manager. Needed for cross-transport key derivation
736     sm_init();
737 #endif
738 
739     // Init profiles
740     uint16_t hf_supported_features          =
741         (1<<HFP_HFSF_ESCO_S4)               |
742         (1<<HFP_HFSF_CLI_PRESENTATION_CAPABILITY) |
743         (1<<HFP_HFSF_HF_INDICATORS)         |
744         (1<<HFP_HFSF_CODEC_NEGOTIATION)     |
745         (1<<HFP_HFSF_ENHANCED_CALL_STATUS)  |
746         (1<<HFP_HFSF_VOICE_RECOGNITION_FUNCTION)  |
747         (1<<HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
748         (1<<HFP_HFSF_VOICE_RECOGNITION_TEXT) |
749         (1<<HFP_HFSF_EC_NR_FUNCTION) |
750         (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL);
751     int wide_band_speech = 1;
752     hfp_hf_init(rfcomm_channel_nr);
753     hfp_hf_init_supported_features(hf_supported_features);
754     hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators);
755     hfp_hf_init_codecs(sizeof(codecs), codecs);
756     hfp_hf_register_packet_handler(hfp_hf_packet_handler);
757 
758 
759     // Configure SDP
760 
761     // - Create and register HFP HF service record
762     memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
763     hfp_hf_create_sdp_record(hfp_service_buffer, sdp_create_service_record_handle(),
764                              rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech);
765     printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer));
766     sdp_register_service(hfp_service_buffer);
767 
768     // Configure GAP - discovery / connection
769 
770     // - Set local name with a template Bluetooth address, that will be automatically
771     //   replaced with an actual address once it is available, i.e. when BTstack boots
772     //   up and starts talking to a Bluetooth module.
773     gap_set_local_name("HFP HF Demo 00:00:00:00:00:00");
774 
775     // - Allow to show up in Bluetooth inquiry
776     gap_discoverable_control(1);
777 
778     // - Set Class of Device - Service Class: Audio, Major Device Class: Audio, Minor: Hands-Free device
779     gap_set_class_of_device(0x200408);
780 
781     // - Allow for role switch in general and sniff mode
782     gap_set_default_link_policy_settings( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
783 
784     // - Allow for role switch on outgoing connections - this allows HFP AG, e.g. smartphone, to become master when we re-connect to it
785     gap_set_allow_role_switch(true);
786 
787     // Register for HCI events and SCO packets
788     hci_event_callback_registration.callback = &hci_packet_handler;
789     hci_add_event_handler(&hci_event_callback_registration);
790     hci_register_sco_packet_handler(&hci_packet_handler);
791 
792 
793     // Init SCO / HFP audio processing
794     sco_demo_init();
795 
796 #ifdef HAVE_BTSTACK_STDIN
797     // parse human readable Bluetooth address
798     sscanf_bd_addr(device_addr_string, device_addr);
799     btstack_stdin_setup(stdin_process);
800 #endif
801 
802     // turn on!
803     hci_power_control(HCI_POWER_ON);
804     return 0;
805 }
806 /* LISTING_END */
807 /* EXAMPLE_END */
808