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:02:72:DC:31:C1"; 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("z/Z - deactivate/activate enhanced voice recognition\n"); 153 printf("h - start new audio enhanced voice recognition session\n"); 154 155 printf("0123456789#*-+ - send DTMF dial tones\n"); 156 printf("x - request phone number for voice tag | X - current call status (ECS)\n"); 157 printf("y - release call with index 2 (ECC) | Y - private consultation with call 2(ECC)\n"); 158 printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n"); 159 printf("{ - Accept held call(RHH 1) | } - Reject held call(RHH 2)\n"); 160 printf("? - Query Subscriber Number (NUM)\n"); 161 printf("! - Update HF indicator with assigned number 1 (HFI)\n"); 162 printf("\n"); 163 } 164 165 static void stdin_process(char c){ 166 uint8_t status = ERROR_CODE_SUCCESS; 167 168 cmd = c; // used in packet handler 169 170 if (cmd >= '0' && cmd <= '9'){ 171 printf("DTMF Code: %c\n", cmd); 172 status = hfp_hf_send_dtmf_code(acl_handle, cmd); 173 return; 174 } 175 176 switch (cmd){ 177 case '#': 178 case '-': 179 case '+': 180 case '*': 181 log_info("USER:\'%c\'", cmd); 182 printf("DTMF Code: %c\n", cmd); 183 status = hfp_hf_send_dtmf_code(acl_handle, cmd); 184 break; 185 case 'a': 186 log_info("USER:\'%c\'", cmd); 187 printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 188 status = hfp_hf_establish_service_level_connection(device_addr); 189 break; 190 case 'A': 191 log_info("USER:\'%c\'", cmd); 192 printf("Release Service level connection.\n"); 193 status = hfp_hf_release_service_level_connection(acl_handle); 194 break; 195 case 'b': 196 log_info("USER:\'%c\'", cmd); 197 printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 198 status = hfp_hf_establish_audio_connection(acl_handle); 199 break; 200 case 'B': 201 log_info("USER:\'%c\'", cmd); 202 printf("Release Audio service level connection.\n"); 203 status = hfp_hf_release_audio_connection(acl_handle); 204 break; 205 case 'C': 206 log_info("USER:\'%c\'", cmd); 207 printf("Enable registration status update for all AG indicators.\n"); 208 status = hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle); 209 break; 210 case 'c': 211 log_info("USER:\'%c\'", cmd); 212 printf("Disable registration status update for all AG indicators.\n"); 213 status = hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle); 214 break; 215 case 'D': 216 log_info("USER:\'%c\'", cmd); 217 printf("Set HFP AG registration status update for individual indicators (0111111).\n"); 218 status = hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63); 219 break; 220 case 'd': 221 log_info("USER:\'%c\'", cmd); 222 printf("Query network operator.\n"); 223 status = hfp_hf_query_operator_selection(acl_handle); 224 break; 225 case 'E': 226 log_info("USER:\'%c\'", cmd); 227 printf("Enable reporting of the extended AG error result code.\n"); 228 status = hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle); 229 break; 230 case 'e': 231 log_info("USER:\'%c\'", cmd); 232 printf("Disable reporting of the extended AG error result code.\n"); 233 status = hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle); 234 break; 235 case 'f': 236 log_info("USER:\'%c\'", cmd); 237 printf("Answer incoming call.\n"); 238 status = hfp_hf_answer_incoming_call(acl_handle); 239 break; 240 case 'F': 241 log_info("USER:\'%c\'", cmd); 242 printf("Hangup call.\n"); 243 status = hfp_hf_terminate_call(acl_handle); 244 break; 245 case 'G': 246 log_info("USER:\'%c\'", cmd); 247 printf("Reject incoming call.\n"); 248 status = hfp_hf_reject_incoming_call(acl_handle); 249 break; 250 case 'g': 251 log_info("USER:\'%c\'", cmd); 252 printf("Query operator.\n"); 253 status = hfp_hf_query_operator_selection(acl_handle); 254 break; 255 case 't': 256 log_info("USER:\'%c\'", cmd); 257 printf("Terminate HCI connection.\n"); 258 gap_disconnect(acl_handle); 259 break; 260 case 'i': 261 log_info("USER:\'%c\'", cmd); 262 printf("Dial 1234567\n"); 263 status = hfp_hf_dial_number(acl_handle, "1234567"); 264 break; 265 case 'I': 266 log_info("USER:\'%c\'", cmd); 267 printf("Dial 7654321\n"); 268 status = hfp_hf_dial_number(acl_handle, "7654321"); 269 break; 270 case 'j': 271 log_info("USER:\'%c\'", cmd); 272 printf("Dial #1\n"); 273 status = hfp_hf_dial_memory(acl_handle,1); 274 break; 275 case 'J': 276 log_info("USER:\'%c\'", cmd); 277 printf("Dial #99\n"); 278 status = hfp_hf_dial_memory(acl_handle,99); 279 break; 280 case 'k': 281 log_info("USER:\'%c\'", cmd); 282 printf("Deactivate call waiting notification\n"); 283 status = hfp_hf_deactivate_call_waiting_notification(acl_handle); 284 break; 285 case 'K': 286 log_info("USER:\'%c\'", cmd); 287 printf("Activate call waiting notification\n"); 288 status = hfp_hf_activate_call_waiting_notification(acl_handle); 289 break; 290 case 'l': 291 log_info("USER:\'%c\'", cmd); 292 printf("Deactivate calling line notification\n"); 293 status = hfp_hf_deactivate_calling_line_notification(acl_handle); 294 break; 295 case 'L': 296 log_info("USER:\'%c\'", cmd); 297 printf("Activate calling line notification\n"); 298 status = hfp_hf_activate_calling_line_notification(acl_handle); 299 break; 300 case 'm': 301 log_info("USER:\'%c\'", cmd); 302 printf("Deactivate echo canceling and noise reduction\n"); 303 status = hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle); 304 break; 305 case 'M': 306 log_info("USER:\'%c\'", cmd); 307 printf("Activate echo canceling and noise reduction\n"); 308 status = hfp_hf_activate_echo_canceling_and_noise_reduction(acl_handle); 309 break; 310 case 'n': 311 log_info("USER:\'%c\'", cmd); 312 printf("Deactivate voice recognition\n"); 313 status = hfp_hf_deactivate_voice_recognition(acl_handle); 314 break; 315 case 'N': 316 log_info("USER:\'%c\'", cmd); 317 printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr)); 318 status = hfp_hf_activate_voice_recognition(acl_handle); 319 break; 320 case 'z': 321 log_info("USER:\'%c\'", cmd); 322 printf("Deactivate enhanced voice recognition\n"); 323 status = hfp_hf_deactivate_enhanced_voice_recognition(acl_handle); 324 break; 325 case 'Z': 326 log_info("USER:\'%c\'", cmd); 327 printf("Activate enhanced voice recognition %s\n", bd_addr_to_str(device_addr)); 328 status = hfp_hf_activate_enhanced_voice_recognition(acl_handle); 329 break; 330 case 'h': 331 log_info("USER:\'%c\'", cmd); 332 printf("Start new audio enhanced voice recognition session %s\n", bd_addr_to_str(device_addr)); 333 status = hfp_hf_enhanced_voice_recognition_report_ready_for_audio(acl_handle); 334 break; 335 case 'o': 336 log_info("USER:\'%c\'", cmd); 337 printf("Set speaker gain to 0 (minimum)\n"); 338 status = hfp_hf_set_speaker_gain(acl_handle, 0); 339 break; 340 case 'O': 341 log_info("USER:\'%c\'", cmd); 342 printf("Set speaker gain to 9 (default)\n"); 343 status = hfp_hf_set_speaker_gain(acl_handle, 9); 344 break; 345 case 'p': 346 log_info("USER:\'%c\'", cmd); 347 printf("Set speaker gain to 12 (higher)\n"); 348 status = hfp_hf_set_speaker_gain(acl_handle, 12); 349 break; 350 case 'P': 351 log_info("USER:\'%c\'", cmd); 352 printf("Set speaker gain to 15 (maximum)\n"); 353 status = hfp_hf_set_speaker_gain(acl_handle, 15); 354 break; 355 case 'q': 356 log_info("USER:\'%c\'", cmd); 357 printf("Set microphone gain to 0\n"); 358 status = hfp_hf_set_microphone_gain(acl_handle, 0); 359 break; 360 case 'Q': 361 log_info("USER:\'%c\'", cmd); 362 printf("Set microphone gain to 9\n"); 363 status = hfp_hf_set_microphone_gain(acl_handle, 9); 364 break; 365 case 's': 366 log_info("USER:\'%c\'", cmd); 367 printf("Set microphone gain to 12\n"); 368 status = hfp_hf_set_microphone_gain(acl_handle, 12); 369 break; 370 case 'S': 371 log_info("USER:\'%c\'", cmd); 372 printf("Set microphone gain to 15\n"); 373 status = hfp_hf_set_microphone_gain(acl_handle, 15); 374 break; 375 case 'u': 376 log_info("USER:\'%c\'", cmd); 377 printf("Send 'user busy' (Three-Way Call 0)\n"); 378 status = hfp_hf_user_busy(acl_handle); 379 break; 380 case 'U': 381 log_info("USER:\'%c\'", cmd); 382 printf("End active call and accept waiting/held call (Three-Way Call 1)\n"); 383 status = hfp_hf_end_active_and_accept_other(acl_handle); 384 break; 385 case 'v': 386 log_info("USER:\'%c\'", cmd); 387 printf("Swap active call and hold/waiting call (Three-Way Call 2)\n"); 388 status = hfp_hf_swap_calls(acl_handle); 389 break; 390 case 'V': 391 log_info("USER:\'%c\'", cmd); 392 printf("Join hold call (Three-Way Call 3)\n"); 393 status = hfp_hf_join_held_call(acl_handle); 394 break; 395 case 'w': 396 log_info("USER:\'%c\'", cmd); 397 printf("Connect calls (Three-Way Call 4)\n"); 398 status = hfp_hf_connect_calls(acl_handle); 399 break; 400 case 'W': 401 log_info("USER:\'%c\'", cmd); 402 printf("Redial\n"); 403 status = hfp_hf_redial_last_number(acl_handle); 404 break; 405 case 'x': 406 log_info("USER:\'%c\'", cmd); 407 printf("Request phone number for voice tag\n"); 408 status = hfp_hf_request_phone_number_for_voice_tag(acl_handle); 409 break; 410 case 'X': 411 log_info("USER:\'%c\'", cmd); 412 printf("Query current call status\n"); 413 status = hfp_hf_query_current_call_status(acl_handle); 414 break; 415 case 'y': 416 log_info("USER:\'%c\'", cmd); 417 printf("Release call with index 2\n"); 418 status = hfp_hf_release_call_with_index(acl_handle, 2); 419 break; 420 case 'Y': 421 log_info("USER:\'%c\'", cmd); 422 printf("Private consultation with call 2\n"); 423 status = hfp_hf_private_consultation_with_call(acl_handle, 2); 424 break; 425 case '[': 426 log_info("USER:\'%c\'", cmd); 427 printf("Query Response and Hold status (RHH ?)\n"); 428 status = hfp_hf_rrh_query_status(acl_handle); 429 break; 430 case ']': 431 log_info("USER:\'%c\'", cmd); 432 printf("Place call in a response and held state (RHH 0)\n"); 433 status = hfp_hf_rrh_hold_call(acl_handle); 434 break; 435 case '{': 436 log_info("USER:\'%c\'", cmd); 437 printf("Accept held call (RHH 1)\n"); 438 status = hfp_hf_rrh_accept_held_call(acl_handle); 439 break; 440 case '}': 441 log_info("USER:\'%c\'", cmd); 442 printf("Reject held call (RHH 2)\n"); 443 status = hfp_hf_rrh_reject_held_call(acl_handle); 444 break; 445 case '?': 446 log_info("USER:\'%c\'", cmd); 447 printf("Query Subscriber Number\n"); 448 status = hfp_hf_query_subscriber_number(acl_handle); 449 break; 450 case '!': 451 log_info("USER:\'%c\'", cmd); 452 printf("Update HF indicator with assigned number 1 (HFI)\n"); 453 status = hfp_hf_set_hf_indicator(acl_handle, 1, 1); 454 break; 455 default: 456 show_usage(); 457 break; 458 } 459 460 if (status != ERROR_CODE_SUCCESS){ 461 printf("Could not perform command, status 0x%02x\n", status); 462 } 463 } 464 #endif 465 466 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 467 UNUSED(channel); 468 uint8_t status; 469 470 switch (packet_type){ 471 472 case HCI_SCO_DATA_PACKET: 473 if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break; 474 sco_demo_receive(event, event_size); 475 break; 476 477 case HCI_EVENT_PACKET: 478 switch (hci_event_packet_get_type(event)){ 479 case HCI_EVENT_SCO_CAN_SEND_NOW: 480 sco_demo_send(sco_handle); 481 break; 482 483 case HCI_EVENT_COMMAND_COMPLETE: 484 if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){ 485 dump_supported_codecs(); 486 } 487 break; 488 489 case HCI_EVENT_HFP_META: 490 switch (hci_event_hfp_meta_get_subevent_code(event)) { 491 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 492 acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event); 493 hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr); 494 printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr)); 495 break; 496 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 497 acl_handle = HCI_CON_HANDLE_INVALID; 498 printf("Service level connection released.\n\n"); 499 break; 500 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 501 status = hfp_subevent_audio_connection_established_get_status(event); 502 if (status != ERROR_CODE_SUCCESS){ 503 printf("Audio connection establishment failed with status 0x%02x\n", status); 504 } else { 505 sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event); 506 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 507 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event); 508 switch (negotiated_codec){ 509 case 0x01: 510 printf("Using CVSD codec.\n"); 511 break; 512 case 0x02: 513 printf("Using mSBC codec.\n"); 514 break; 515 default: 516 printf("Using unknown codec 0x%02x.\n", negotiated_codec); 517 break; 518 } 519 sco_demo_set_codec(negotiated_codec); 520 hci_request_sco_can_send_now_event(); 521 } 522 break; 523 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 524 sco_handle = HCI_CON_HANDLE_INVALID; 525 printf("Audio connection released\n"); 526 sco_demo_close(); 527 break; 528 case HFP_SUBEVENT_COMPLETE: 529 status = hfp_subevent_complete_get_status(event); 530 if (status == ERROR_CODE_SUCCESS){ 531 printf("Cmd \'%c\' succeeded\n", cmd); 532 } else { 533 printf("Cmd \'%c\' failed with status 0x%02x\n", cmd, status); 534 } 535 break; 536 case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED: 537 printf("AG_INDICATOR_STATUS_CHANGED, AG indicator (index: %d) to: %d of range [%d, %d], name '%s'\n", 538 hfp_subevent_ag_indicator_status_changed_get_indicator_index(event), 539 hfp_subevent_ag_indicator_status_changed_get_indicator_status(event), 540 hfp_subevent_ag_indicator_status_changed_get_indicator_min_range(event), 541 hfp_subevent_ag_indicator_status_changed_get_indicator_max_range(event), 542 (const char*) hfp_subevent_ag_indicator_status_changed_get_indicator_name(event)); 543 break; 544 case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED: 545 printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n", 546 hfp_subevent_network_operator_changed_get_network_operator_mode(event), 547 hfp_subevent_network_operator_changed_get_network_operator_format(event), 548 (char *) hfp_subevent_network_operator_changed_get_network_operator_name(event)); 549 break; 550 case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR: 551 printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : 0x%02x\n", 552 hfp_subevent_extended_audio_gateway_error_get_error(event)); 553 break; 554 case HFP_SUBEVENT_RING: 555 printf("** Ring **\n"); 556 break; 557 case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG: 558 printf("Phone number for voice tag: %s\n", 559 (const char *) hfp_subevent_number_for_voice_tag_get_number(event)); 560 break; 561 case HFP_SUBEVENT_SPEAKER_VOLUME: 562 printf("Speaker volume: gain %u\n", 563 hfp_subevent_speaker_volume_get_gain(event)); 564 break; 565 case HFP_SUBEVENT_MICROPHONE_VOLUME: 566 printf("Microphone volume: gain %u\n", 567 hfp_subevent_microphone_volume_get_gain(event)); 568 break; 569 case HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION: 570 printf("Caller ID, number %s\n", hfp_subevent_calling_line_identification_notification_get_number(event)); 571 break; 572 case HFP_SUBEVENT_ENHANCED_CALL_STATUS: 573 printf("Enhanced call status:\n"); 574 printf(" - call index: %d \n", hfp_subevent_enhanced_call_status_get_clcc_idx(event)); 575 printf(" - direction : %s \n", hfp_enhanced_call_dir2str(hfp_subevent_enhanced_call_status_get_clcc_dir(event))); 576 printf(" - status : %s \n", hfp_enhanced_call_status2str(hfp_subevent_enhanced_call_status_get_clcc_status(event))); 577 printf(" - mode : %s \n", hfp_enhanced_call_mode2str(hfp_subevent_enhanced_call_status_get_clcc_mode(event))); 578 printf(" - multipart : %s \n", hfp_enhanced_call_mpty2str(hfp_subevent_enhanced_call_status_get_clcc_mpty(event))); 579 printf(" - type : %d \n", hfp_subevent_enhanced_call_status_get_bnip_type(event)); 580 printf(" - number : %s \n", hfp_subevent_enhanced_call_status_get_bnip_number(event)); 581 break; 582 583 case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS: 584 status = hfp_subevent_voice_recognition_status_get_status(event); 585 if (status != ERROR_CODE_SUCCESS){ 586 printf("Voice Recognition command failed\n"); 587 break; 588 } 589 switch(hfp_subevent_voice_recognition_status_get_state(event)){ 590 case 0: 591 printf("\nVoice recognition status DEACTIVATED\n\n"); 592 break; 593 case 1: 594 printf("\nVoice recognition status ACTIVATED\n\n"); 595 break; 596 default: 597 btstack_assert(false); 598 break; 599 } 600 break; 601 602 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO: 603 status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event); 604 if (status != ERROR_CODE_SUCCESS){ 605 printf("Enhanced Voice recognition READY FOR AUDIO cmd failed\n"); 606 break; 607 } 608 printf("\nEnhanced Voice recognition status READY FOR AUDIO\n\n"); 609 break; 610 611 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT: 612 printf("\nEnhanced Voice recognition AG status: AG READY TO ACCEPT AUDIO INPUT\n\n"); 613 break; 614 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND: 615 printf("\nEnhanced Voice recognition AG status: AG IS STARTING SOUND\n\n"); 616 break; 617 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT: 618 printf("\nEnhanced Voice recognition AG status: AG IS PROCESSING AUDIO INPUT\n\n"); 619 break; 620 621 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE: 622 printf("\nEnhanced Voice recognition AG message: \'%s\'\n", hfp_subevent_enhanced_voice_recognition_ag_message_get_text(event)); 623 624 break; 625 626 default: 627 break; 628 } 629 break; 630 631 default: 632 break; 633 } 634 break; 635 636 default: 637 break; 638 } 639 640 } 641 642 /* @section Main Application Setup 643 * 644 * @text Listing MainConfiguration shows main application code. 645 * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it. 646 * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers. 647 * The stdin_process callback allows for sending commands to the HFP AG. 648 * At the end the Bluetooth stack is started. 649 */ 650 651 /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */ 652 int btstack_main(int argc, const char * argv[]); 653 int btstack_main(int argc, const char * argv[]){ 654 (void)argc; 655 (void)argv; 656 657 sco_demo_init(); 658 659 gap_discoverable_control(1); 660 gap_set_class_of_device(0x200408); 661 gap_set_local_name("HFP HF Demo 00:00:00:00:00:00"); 662 663 // init L2CAP 664 l2cap_init(); 665 666 uint16_t hf_supported_features = 667 (1<<HFP_HFSF_ESCO_S4) | 668 (1<<HFP_HFSF_CLI_PRESENTATION_CAPABILITY) | 669 (1<<HFP_HFSF_HF_INDICATORS) | 670 (1<<HFP_HFSF_CODEC_NEGOTIATION) | 671 (1<<HFP_HFSF_ENHANCED_CALL_STATUS) | 672 (1<<HFP_HFSF_VOICE_RECOGNITION_FUNCTION) | 673 (1<<HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS) | 674 (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL); 675 int wide_band_speech = 1; 676 677 rfcomm_init(); 678 hfp_hf_init(rfcomm_channel_nr); 679 hfp_hf_init_supported_features(hf_supported_features); 680 hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators); 681 hfp_hf_init_codecs(sizeof(codecs), codecs); 682 683 sdp_init(); 684 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 685 hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech); 686 printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer)); 687 sdp_register_service(hfp_service_buffer); 688 689 // register for HCI events and SCO packets 690 hci_event_callback_registration.callback = &packet_handler; 691 hci_add_event_handler(&hci_event_callback_registration); 692 hci_register_sco_packet_handler(&packet_handler); 693 hci_register_sco_packet_handler(&packet_handler); 694 695 // register for HFP events 696 hfp_hf_register_packet_handler(packet_handler); 697 698 #ifdef HAVE_BTSTACK_STDIN 699 // parse human readable Bluetooth address 700 sscanf_bd_addr(device_addr_string, device_addr); 701 btstack_stdin_setup(stdin_process); 702 #endif 703 // turn on! 704 hci_power_control(HCI_POWER_ON); 705 return 0; 706 } 707 /* LISTING_END */ 708 /* EXAMPLE_END */ 709