1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "hfp_ag_demo.c" 39 40 /* 41 * hfp_ag_demo.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(hfp_ag_demo): HFP AG - Audio Gateway 46 * 47 * @text This HFP Audio Gateway example demonstrates how to receive 48 * an output from a remote HFP Hands-Free (HF) unit, and, 49 * if HAVE_BTSTACK_STDIN is defined, how to control the HFP HF. 50 */ 51 // ***************************************************************************** 52 53 54 #include <stdint.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 59 #include "btstack.h" 60 #include "sco_demo_util.h" 61 #ifdef HAVE_BTSTACK_STDIN 62 #include "btstack_stdin.h" 63 #endif 64 65 // uncomment to temp disable mSBC codec 66 // #undef ENABLE_HFP_WIDE_BAND_SPEECH 67 68 uint8_t hfp_service_buffer[150]; 69 const uint8_t rfcomm_channel_nr = 1; 70 const char hfp_ag_service_name[] = "HFP AG Demo"; 71 72 static bd_addr_t device_addr; 73 static const char * device_addr_string = "00:1A:7D:DA:71:13"; 74 75 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 76 static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC}; 77 #else 78 static uint8_t codecs[] = {HFP_CODEC_CVSD}; 79 #endif 80 81 static uint8_t negotiated_codec = HFP_CODEC_CVSD; 82 83 static hci_con_handle_t acl_handle = HCI_CON_HANDLE_INVALID; 84 static hci_con_handle_t sco_handle = HCI_CON_HANDLE_INVALID; 85 static int memory_1_enabled = 1; 86 static btstack_packet_callback_registration_t hci_event_callback_registration; 87 88 static btstack_timer_source_t hfp_outgoing_call_ringing_timer; 89 90 static int ag_indicators_nr = 7; 91 static hfp_ag_indicator_t ag_indicators[] = { 92 // index, name, min range, max range, status, mandatory, enabled, status changed 93 {1, "service", 0, 1, 1, 0, 0, 0}, 94 {2, "call", 0, 1, 0, 1, 1, 0}, 95 {3, "callsetup", 0, 3, 0, 1, 1, 0}, 96 {4, "battchg", 0, 5, 3, 0, 0, 0}, 97 {5, "signal", 0, 5, 5, 0, 1, 0}, 98 {6, "roam", 0, 1, 0, 0, 1, 0}, 99 {7, "callheld", 0, 2, 0, 1, 1, 0} 100 }; 101 102 static int call_hold_services_nr = 5; 103 static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"}; 104 105 static int hf_indicators_nr = 2; 106 static hfp_generic_status_indicator_t hf_indicators[] = { 107 {1, 1}, 108 {2, 1}, 109 }; 110 111 static hfp_voice_recognition_message_t msg = { 112 0xABCD, HFP_TEXT_TYPE_MESSAGE_FROM_AG, HFP_TEXT_OPERATION_REPLACE, "The temperature in Munich is 30 degrees." 113 }; 114 115 #define INQUIRY_INTERVAL 5 116 117 enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ; 118 enum STATE state = INIT; 119 120 static void dump_supported_codecs(void){ 121 unsigned int i; 122 int mSBC_skipped = 0; 123 printf("Supported codecs: "); 124 for (i = 0; i < sizeof(codecs); i++){ 125 switch(codecs[i]){ 126 case HFP_CODEC_CVSD: 127 printf("CVSD"); 128 break; 129 case HFP_CODEC_MSBC: 130 if (hci_extended_sco_link_supported()){ 131 printf(", mSBC"); 132 } else { 133 mSBC_skipped = 1; 134 } 135 break; 136 default: 137 btstack_assert(false); 138 break; 139 } 140 } 141 printf("\n"); 142 if (mSBC_skipped){ 143 printf("mSBC codec disabled because eSCO not supported by local controller.\n"); 144 } 145 } 146 147 #ifdef HAVE_BTSTACK_STDIN 148 // Testig User Interface 149 static void show_usage(void){ 150 bd_addr_t iut_address; 151 gap_local_bd_addr(iut_address); 152 153 printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address)); 154 printf("\n"); 155 156 printf("a - establish HFP connection to %s\n", bd_addr_to_str(device_addr)); 157 // printf("A - release HFP connection\n"); 158 159 printf("b - establish AUDIO connection | B - release AUDIO connection\n"); 160 printf("c - simulate incoming call from 1234567 | C - simulate call from 1234567 dropped\n"); 161 printf("d - report AG failure\n"); 162 printf("D - delete all link keys\n"); 163 printf("e - answer call on AG | E - terminate call on AG\n"); 164 printf("r - disable in-band ring tone | R - enable in-band ring tone\n"); 165 printf("f - Disable cellular network | F - Enable cellular network\n"); 166 printf("g - Set signal strength to 0 | G - Set signal strength to 5\n"); 167 printf("h - Disable roaming | H - Enable roaming\n"); 168 printf("i - Set battery level to 3 | I - Set battery level to 5\n"); 169 printf("j - Answering call on remote side\n"); 170 printf("k - Clear memory #1 | K - Set memory #1\n"); 171 printf("l - Clear last number | L - Set last number\n"); 172 printf("m - simulate incoming call from 7654321\n"); 173 // printf("M - simulate call from 7654321 dropped\n"); 174 printf("n - Disable Voice Recognition | N - Enable Voice Recognition\n"); 175 printf("1 - EVR play sound | 2 - EVR report ready for audio input\n"); 176 printf("3 - EVR report processing input | 4 - EVR send message\n"); 177 178 printf("o - Set speaker volume to 0 (minimum) | O - Set speaker volume to 9 (default)\n"); 179 printf("p - Set speaker volume to 12 (higher) | P - Set speaker volume to 15 (maximum)\n"); 180 printf("q - Set microphone gain to 0 (minimum) | Q - Set microphone gain to 9 (default)\n"); 181 printf("s - Set microphone gain to 12 (higher) | S - Set microphone gain to 15 (maximum)\n"); 182 printf("t - terminate connection\n"); 183 printf("u - join held call\n"); 184 printf("v - discover nearby HF units\n"); 185 printf("w - put incoming call on hold (Response and Hold)\n"); 186 printf("x - accept held incoming call (Response and Hold)\n"); 187 printf("X - reject held incoming call (Response and Hold)\n"); 188 printf("---\n"); 189 } 190 191 static void stdin_process(char cmd){ 192 uint8_t status = ERROR_CODE_SUCCESS; 193 194 switch (cmd){ 195 case 'a': 196 log_info("USER:\'%c\'", cmd); 197 printf("Establish HFP service level connection to %s...\n", bd_addr_to_str(device_addr)); 198 status = hfp_ag_establish_service_level_connection(device_addr); 199 break; 200 case 'A': 201 log_info("USER:\'%c\'", cmd); 202 printf("Release HFP service level connection.\n"); 203 status = hfp_ag_release_service_level_connection(acl_handle); 204 break; 205 206 case 'b': 207 log_info("USER:\'%c\'", cmd); 208 printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr)); 209 status = hfp_ag_establish_audio_connection(acl_handle); 210 break; 211 case 'B': 212 log_info("USER:\'%c\'", cmd); 213 printf("Release Audio connection.\n"); 214 status = hfp_ag_release_audio_connection(acl_handle); 215 break; 216 case 'c': 217 log_info("USER:\'%c\'", cmd); 218 printf("Simulate incoming call from 1234567\n"); 219 hfp_ag_set_clip(129, "1234567"); 220 hfp_ag_incoming_call(); 221 break; 222 case 'D': 223 printf("Deleting all link keys\n"); 224 gap_delete_all_link_keys(); 225 break; 226 case 'm': 227 log_info("USER:\'%c\'", cmd); 228 printf("Simulate incoming call from 7654321\n"); 229 hfp_ag_set_clip(129, "7654321"); 230 hfp_ag_incoming_call(); 231 break; 232 case 'C': 233 log_info("USER:\'%c\'", cmd); 234 printf("Simulate terminate call\n"); 235 hfp_ag_call_dropped(); 236 break; 237 case 'd': 238 log_info("USER:\'%c\'", cmd); 239 printf("Report AG failure\n"); 240 status = hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE); 241 break; 242 case 'e': 243 log_info("USER:\'%c\'", cmd); 244 printf("Answer call on AG\n"); 245 hfp_ag_answer_incoming_call(); 246 break; 247 case 'E': 248 log_info("USER:\'%c\'", cmd); 249 printf("Terminate call on AG\n"); 250 hfp_ag_terminate_call(); 251 break; 252 case 'f': 253 log_info("USER:\'%c\'", cmd); 254 printf("Disable cellular network\n"); 255 status = hfp_ag_set_registration_status(0); 256 break; 257 case 'F': 258 log_info("USER:\'%c\'", cmd); 259 printf("Enable cellular network\n"); 260 status = hfp_ag_set_registration_status(1); 261 break; 262 case 'g': 263 log_info("USER:\'%c\'", cmd); 264 printf("Set signal strength to 0\n"); 265 status = hfp_ag_set_signal_strength(0); 266 break; 267 case 'G': 268 log_info("USER:\'%c\'", cmd); 269 printf("Set signal strength to 5\n"); 270 status = hfp_ag_set_signal_strength(5); 271 break; 272 case 'h': 273 log_info("USER:\'%c\'", cmd); 274 printf("Disable roaming\n"); 275 status = hfp_ag_set_roaming_status(0); 276 break; 277 case 'H': 278 log_info("USER:\'%c\'", cmd); 279 printf("Enable roaming\n"); 280 status = hfp_ag_set_roaming_status(1); 281 break; 282 case 'i': 283 log_info("USER:\'%c\'", cmd); 284 printf("Set battery level to 3\n"); 285 status = hfp_ag_set_battery_level(3); 286 break; 287 case 'I': 288 log_info("USER:\'%c\'", cmd); 289 printf("Set battery level to 5\n"); 290 status = hfp_ag_set_battery_level(5); 291 break; 292 case 'j': 293 log_info("USER:\'%c\'", cmd); 294 printf("Answering call on remote side\n"); 295 hfp_ag_outgoing_call_established(); 296 break; 297 case 'r': 298 log_info("USER:\'%c\'", cmd); 299 printf("Disable in-band ring tone\n"); 300 hfp_ag_set_use_in_band_ring_tone(0); 301 break; 302 case 'k': 303 log_info("USER:\'%c\'", cmd); 304 printf("Memory 1 cleared\n"); 305 memory_1_enabled = 0; 306 break; 307 case 'K': 308 log_info("USER:\'%c\'", cmd); 309 printf("Memory 1 set\n"); 310 memory_1_enabled = 1; 311 break; 312 case 'l': 313 log_info("USER:\'%c\'", cmd); 314 printf("Last dialed number cleared\n"); 315 hfp_ag_clear_last_dialed_number(); 316 break; 317 case 'L': 318 log_info("USER:\'%c\'", cmd); 319 printf("Outgoing call connected, ringing\n"); 320 hfp_ag_outgoing_call_ringing(); 321 break; 322 case 'n': 323 log_info("USER:\'%c\'", cmd); 324 printf("Disable Voice Recognition\n"); 325 status = hfp_ag_deactivate_voice_recognition(acl_handle); 326 break; 327 case 'N': 328 log_info("USER:\'%c\'", cmd); 329 printf("Enable Voice Recognition\n"); 330 status = hfp_ag_activate_voice_recognition(acl_handle); 331 break; 332 333 case '1': 334 log_info("USER:\'%c\'", cmd); 335 printf("Enhanced_Voice Recognition: play sound\n"); 336 status = hfp_ag_enhanced_voice_recognition_report_sending_audio(acl_handle); 337 break; 338 case '2': 339 log_info("USER:\'%c\'", cmd); 340 printf("Enhanced_Voice Recognition: report ready for audio input\n"); 341 status = hfp_ag_enhanced_voice_recognition_report_ready_for_audio(acl_handle); 342 break; 343 case '3': 344 log_info("USER:\'%c\'", cmd); 345 printf("Enhanced_Voice Recognition: report processing input\n"); 346 status = hfp_ag_enhanced_voice_recognition_report_processing_input(acl_handle); 347 break; 348 case '4': 349 log_info("USER:\'%c\'", cmd); 350 printf("Enhanced_Voice Recognition: send message\n"); 351 status = hfp_ag_enhanced_voice_recognition_send_message(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT, msg); 352 break; 353 354 case 'o': 355 log_info("USER:\'%c\'", cmd); 356 printf("Set speaker gain to 0 (minimum)\n"); 357 status = hfp_ag_set_speaker_gain(acl_handle, 0); 358 break; 359 case 'O': 360 log_info("USER:\'%c\'", cmd); 361 printf("Set speaker gain to 9 (default)\n"); 362 status = hfp_ag_set_speaker_gain(acl_handle, 9); 363 break; 364 case 'p': 365 log_info("USER:\'%c\'", cmd); 366 printf("Set speaker gain to 12 (higher)\n"); 367 status = hfp_ag_set_speaker_gain(acl_handle, 12); 368 break; 369 case 'P': 370 log_info("USER:\'%c\'", cmd); 371 printf("Set speaker gain to 15 (maximum)\n"); 372 status = hfp_ag_set_speaker_gain(acl_handle, 15); 373 break; 374 case 'q': 375 log_info("USER:\'%c\'", cmd); 376 printf("Set microphone gain to 0\n"); 377 status = hfp_ag_set_microphone_gain(acl_handle, 0); 378 break; 379 case 'Q': 380 log_info("USER:\'%c\'", cmd); 381 printf("Set microphone gain to 9\n"); 382 status = hfp_ag_set_microphone_gain(acl_handle, 9); 383 break; 384 case 's': 385 log_info("USER:\'%c\'", cmd); 386 printf("Set microphone gain to 12\n"); 387 status = hfp_ag_set_microphone_gain(acl_handle, 12); 388 break; 389 case 'S': 390 log_info("USER:\'%c\'", cmd); 391 printf("Set microphone gain to 15\n"); 392 status = hfp_ag_set_microphone_gain(acl_handle, 15); 393 break; 394 case 'R': 395 log_info("USER:\'%c\'", cmd); 396 printf("Enable in-band ring tone\n"); 397 hfp_ag_set_use_in_band_ring_tone(1); 398 break; 399 case 't': 400 log_info("USER:\'%c\'", cmd); 401 printf("Terminate HCI connection. 0x%2x\n", acl_handle); 402 gap_disconnect(acl_handle); 403 break; 404 case 'u': 405 log_info("USER:\'%c\'", cmd); 406 printf("Join held call\n"); 407 hfp_ag_join_held_call(); 408 break; 409 case 'v': 410 printf("Start scanning...\n"); 411 gap_inquiry_start(INQUIRY_INTERVAL); 412 break; 413 case 'w': 414 log_info("USER:\'%c\'", cmd); 415 printf("AG: Put incoming call on hold (Response and Hold)\n"); 416 hfp_ag_hold_incoming_call(); 417 break; 418 case 'x': 419 log_info("USER:\'%c\'", cmd); 420 printf("AG: Accept held incoming call (Response and Hold)\n"); 421 hfp_ag_accept_held_incoming_call(); 422 break; 423 case 'X': 424 log_info("USER:\'%c\'", cmd); 425 printf("AG: Reject held incoming call (Response and Hold)\n"); 426 hfp_ag_reject_held_incoming_call(); 427 break; 428 default: 429 show_usage(); 430 break; 431 } 432 433 if (status != ERROR_CODE_SUCCESS){ 434 printf("Could not perform command, status 0x%02x\n", status); 435 } 436 } 437 #endif 438 439 static void report_status(uint8_t status, const char * message){ 440 if (status != ERROR_CODE_SUCCESS){ 441 printf("%s command failed, status 0x%02x\n", message, status); 442 } else { 443 printf("%s command successful\n", message); 444 } 445 } 446 447 static void hfp_outgoing_call_ringing_handler(btstack_timer_source_t * timer){ 448 UNUSED(timer); 449 printf("Simulate call connected -> start ringing\n"); 450 hfp_ag_outgoing_call_ringing(); 451 } 452 453 static void hfp_outgoing_call_ringing_start(void){ 454 btstack_run_loop_set_timer_handler(&hfp_outgoing_call_ringing_timer, hfp_outgoing_call_ringing_handler); 455 btstack_run_loop_set_timer(&hfp_outgoing_call_ringing_timer, 1000); // 1 second timeout 456 btstack_run_loop_add_timer(&hfp_outgoing_call_ringing_timer); 457 } 458 459 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 460 UNUSED(channel); 461 bd_addr_t addr; 462 uint8_t status; 463 switch (packet_type){ 464 case HCI_EVENT_PACKET: 465 switch(hci_event_packet_get_type(event)){ 466 case BTSTACK_EVENT_STATE: 467 if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break; 468 dump_supported_codecs(); 469 #ifndef HAVE_BTSTACK_STDIN 470 printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr)); 471 hfp_ag_establish_service_level_connection(device_addr); 472 #endif 473 break; 474 case GAP_EVENT_INQUIRY_RESULT: 475 gap_event_inquiry_result_get_bd_addr(event, addr); 476 // print info 477 printf("Device found: %s ", bd_addr_to_str(addr)); 478 printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event)); 479 if (gap_event_inquiry_result_get_rssi_available(event)){ 480 printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event)); 481 } 482 if (gap_event_inquiry_result_get_name_available(event)){ 483 char name_buffer[240]; 484 int name_len = gap_event_inquiry_result_get_name_len(event); 485 memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len); 486 name_buffer[name_len] = 0; 487 printf(", name '%s'", name_buffer); 488 } 489 printf("\n"); 490 break; 491 case GAP_EVENT_INQUIRY_COMPLETE: 492 printf("Inquiry scan complete.\n"); 493 break; 494 case HCI_EVENT_SCO_CAN_SEND_NOW: 495 sco_demo_send(sco_handle); 496 break; 497 default: 498 break; 499 } 500 501 if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return; 502 503 switch (hci_event_hfp_meta_get_subevent_code(event)) { 504 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 505 status = hfp_subevent_service_level_connection_established_get_status(event); 506 if (status != ERROR_CODE_SUCCESS){ 507 printf("Connection failed, status 0x%02x\n", status); 508 break; 509 } 510 acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event); 511 hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr); 512 printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr)); 513 dump_supported_codecs(); 514 #ifndef HAVE_BTSTACK_STDIN 515 log_info("Establish Audio connection %s", bd_addr_to_str(device_addr)); 516 printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr)); 517 hfp_ag_establish_audio_connection(acl_handle); 518 #endif 519 break; 520 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 521 printf("Service level connection released.\n"); 522 acl_handle = HCI_CON_HANDLE_INVALID; 523 break; 524 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 525 if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){ 526 printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event)); 527 } else { 528 sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event); 529 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 530 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event); 531 switch (negotiated_codec){ 532 case 0x01: 533 printf("Using CVSD codec.\n"); 534 break; 535 case 0x02: 536 printf("Using mSBC codec.\n"); 537 break; 538 default: 539 printf("Using unknown codec 0x%02x.\n", negotiated_codec); 540 break; 541 } 542 sco_demo_set_codec(negotiated_codec); 543 hci_request_sco_can_send_now_event(); 544 } 545 break; 546 547 case HFP_SUBEVENT_CALL_ANSWERED: 548 printf("Call answered\n"); 549 break; 550 551 case HFP_SUBEVENT_CALL_TERMINATED: 552 printf("Call terminated\n"); 553 break; 554 555 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 556 printf("Audio connection released\n"); 557 sco_handle = HCI_CON_HANDLE_INVALID; 558 sco_demo_close(); 559 break; 560 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 570 case HFP_SUBEVENT_START_RINGING: 571 printf("** START Ringing **\n"); 572 break; 573 case HFP_SUBEVENT_RING: 574 printf("** Ring **\n"); 575 break; 576 case HFP_SUBEVENT_STOP_RINGING: 577 printf("** STOP Ringing **\n"); 578 break; 579 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER: 580 printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event)); 581 // validate number 582 if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0 583 || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0 584 || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){ 585 printf("Dial string valid: accept call\n"); 586 hfp_ag_outgoing_call_accepted(); 587 hfp_outgoing_call_ringing_start(); 588 } else { 589 printf("Dial string invalid: reject call\n"); 590 hfp_ag_outgoing_call_rejected(); 591 } 592 break; 593 594 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG: 595 printf("Attach number to voice tag. Sending '1234567\n"); 596 hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567"); 597 break; 598 599 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES: 600 printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event)); 601 hfp_ag_send_dtmf_code_done(acl_handle); 602 break; 603 604 case HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED: 605 status = hfp_subevent_voice_recognition_activated_get_status(event); 606 if (status != ERROR_CODE_SUCCESS){ 607 printf("Voice Recognition Activate command failed\n"); 608 break; 609 } 610 611 switch (hfp_subevent_voice_recognition_activated_get_enhanced(event)){ 612 case 0: 613 printf("\nVoice recognition ACTVATED\n\n"); 614 break; 615 default: 616 printf("\nEnhanced voice recognition ACTVATED\n\n"); 617 break; 618 } 619 break; 620 621 case HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED: 622 status = hfp_subevent_voice_recognition_deactivated_get_status(event); 623 if (status != ERROR_CODE_SUCCESS){ 624 printf("Voice Recognition Deactivate command failed\n"); 625 break; 626 } 627 printf("\nVoice Recognition DEACTIVATED\n\n"); 628 break; 629 630 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO: 631 status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event); 632 report_status(status, "Enhanced Voice recognition: READY FOR AUDIO"); 633 break; 634 635 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT: 636 status = hfp_subevent_enhanced_voice_recognition_ag_ready_to_accept_audio_input_get_status(event); 637 report_status(status, "Enhanced Voice recognition: AG READY TO ACCEPT AUDIO INPUT"); 638 break; 639 640 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND: 641 status = hfp_subevent_enhanced_voice_recognition_ag_is_starting_sound_get_status(event); 642 report_status(status, "Enhanced Voice recognition: AG IS STARTING SOUND"); 643 break; 644 645 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT: 646 status = hfp_subevent_enhanced_voice_recognition_ag_is_processing_audio_input_get_status(event); 647 report_status(status, "Enhanced Voice recognition: AG IS PROCESSING AUDIO INPUT"); 648 break; 649 650 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT: 651 status = hfp_subevent_enhanced_voice_recognition_ag_message_sent_get_status(event); 652 report_status(status, "Enhanced Voice recognition: AG MESSAGE SENT"); 653 printf("Enhanced Voice recognition: \'%s\'\n\n", msg.text); 654 break; 655 656 case HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE: 657 status = hfp_subevent_echo_canceling_and_noise_reduction_deactivate_get_status(event); 658 report_status(status, "Echo Canceling and Noise Reduction Deactivate"); 659 break; 660 661 case HFP_SUBEVENT_HF_INDICATOR: 662 switch (hfp_subevent_hf_indicator_get_uuid(event)){ 663 case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 664 printf("Received ENHANCED SAFETY indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event)); 665 break; 666 case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 667 printf("Received BATTERY LEVEL indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event)); 668 break; 669 default: 670 printf("Received HF INDICATOR indicator, UUID 0x%4X, value %d\n", hfp_subevent_hf_indicator_get_uuid(event), hfp_subevent_hf_indicator_get_value(event)); 671 break; 672 } 673 break; 674 default: 675 break; 676 } 677 break; 678 case HCI_SCO_DATA_PACKET: 679 if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break; 680 sco_demo_receive(event, event_size); 681 break; 682 default: 683 break; 684 } 685 } 686 687 static hfp_phone_number_t subscriber_number = { 688 129, "225577" 689 }; 690 691 /* @section Main Application Setup 692 * 693 * @text Listing MainConfiguration shows main application code. 694 * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it. 695 * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers. 696 * The stdin_process callback allows for sending commands to the HFP HF. 697 * At the end the Bluetooth stack is started. 698 */ 699 700 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */ 701 702 int btstack_main(int argc, const char * argv[]); 703 int btstack_main(int argc, const char * argv[]){ 704 (void)argc; 705 (void)argv; 706 707 sco_demo_init(); 708 709 gap_set_local_name("HFP AG Demo 00:00:00:00:00:00"); 710 gap_discoverable_control(1); 711 712 // L2CAP 713 l2cap_init(); 714 715 #ifdef ENABLE_BLE 716 // Initialize LE Security Manager. Needed for cross-transport key derivation 717 sm_init(); 718 #endif 719 720 uint16_t supported_features = 721 (1<<HFP_AGSF_ESCO_S4) | 722 (1<<HFP_AGSF_HF_INDICATORS) | 723 (1<<HFP_AGSF_CODEC_NEGOTIATION) | 724 (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) | 725 (1<<HFP_AGSF_ENHANCED_CALL_CONTROL) | 726 (1<<HFP_AGSF_ENHANCED_CALL_STATUS) | 727 (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL) | 728 (1<<HFP_AGSF_IN_BAND_RING_TONE) | 729 (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION) | 730 (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) | 731 (1<<HFP_AGSF_VOICE_RECOGNITION_TEXT) | 732 (1<<HFP_AGSF_EC_NR_FUNCTION) | 733 (1<<HFP_AGSF_THREE_WAY_CALLING); 734 int wide_band_speech = 1; 735 736 // HFP 737 rfcomm_init(); 738 hfp_ag_init(rfcomm_channel_nr); 739 hfp_ag_init_supported_features(supported_features); 740 hfp_ag_init_codecs(sizeof(codecs), codecs); 741 hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators); 742 hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators); 743 hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services); 744 hfp_ag_set_subcriber_number_information(&subscriber_number, 1); 745 746 // SDP Server 747 sdp_init(); 748 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 749 hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech); 750 printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer)); 751 sdp_register_service(hfp_service_buffer); 752 753 // register for HCI events and SCO packets 754 hci_event_callback_registration.callback = &packet_handler; 755 hci_add_event_handler(&hci_event_callback_registration); 756 hci_register_sco_packet_handler(&packet_handler); 757 758 // register for HFP events 759 hfp_ag_register_packet_handler(&packet_handler); 760 761 // parse human readable Bluetooth address 762 sscanf_bd_addr(device_addr_string, device_addr); 763 764 #ifdef HAVE_BTSTACK_STDIN 765 btstack_stdin_setup(stdin_process); 766 #endif 767 // turn on! 768 hci_power_control(HCI_POWER_ON); 769 return 0; 770 } 771 /* LISTING_END */ 772 /* EXAMPLE_END */ 773