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 #ifndef HAVE_BTSTACK_STDIN 467 case BTSTACK_EVENT_STATE: 468 if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break; 469 printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr)); 470 hfp_ag_establish_service_level_connection(device_addr); 471 break; 472 #endif 473 case GAP_EVENT_INQUIRY_RESULT: 474 gap_event_inquiry_result_get_bd_addr(event, addr); 475 // print info 476 printf("Device found: %s ", bd_addr_to_str(addr)); 477 printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event)); 478 if (gap_event_inquiry_result_get_rssi_available(event)){ 479 printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event)); 480 } 481 if (gap_event_inquiry_result_get_name_available(event)){ 482 char name_buffer[240]; 483 int name_len = gap_event_inquiry_result_get_name_len(event); 484 memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len); 485 name_buffer[name_len] = 0; 486 printf(", name '%s'", name_buffer); 487 } 488 printf("\n"); 489 break; 490 case GAP_EVENT_INQUIRY_COMPLETE: 491 printf("Inquiry scan complete.\n"); 492 break; 493 case HCI_EVENT_SCO_CAN_SEND_NOW: 494 sco_demo_send(sco_handle); 495 break; 496 case HCI_EVENT_COMMAND_COMPLETE: 497 if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){ 498 dump_supported_codecs(); 499 } 500 break; 501 default: 502 break; 503 } 504 505 if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return; 506 507 switch (hci_event_hfp_meta_get_subevent_code(event)) { 508 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 509 status = hfp_subevent_service_level_connection_established_get_status(event); 510 if (status != ERROR_CODE_SUCCESS){ 511 printf("Connection failed, status 0x%02x\n", status); 512 break; 513 } 514 acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event); 515 hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr); 516 printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr)); 517 dump_supported_codecs(); 518 #ifndef HAVE_BTSTACK_STDIN 519 log_info("Establish Audio connection %s", bd_addr_to_str(device_addr)); 520 printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr)); 521 hfp_ag_establish_audio_connection(acl_handle); 522 #endif 523 break; 524 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 525 printf("Service level connection released.\n"); 526 acl_handle = HCI_CON_HANDLE_INVALID; 527 break; 528 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 529 if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){ 530 printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event)); 531 } else { 532 sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event); 533 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 534 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event); 535 switch (negotiated_codec){ 536 case 0x01: 537 printf("Using CVSD codec.\n"); 538 break; 539 case 0x02: 540 printf("Using mSBC codec.\n"); 541 break; 542 default: 543 printf("Using unknown codec 0x%02x.\n", negotiated_codec); 544 break; 545 } 546 sco_demo_set_codec(negotiated_codec); 547 hci_request_sco_can_send_now_event(); 548 } 549 break; 550 551 case HFP_SUBEVENT_CALL_ANSWERED: 552 printf("Call answered\n"); 553 break; 554 555 case HFP_SUBEVENT_CALL_TERMINATED: 556 printf("Call terminated\n"); 557 break; 558 559 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 560 printf("Audio connection released\n"); 561 sco_handle = HCI_CON_HANDLE_INVALID; 562 sco_demo_close(); 563 break; 564 565 case HFP_SUBEVENT_SPEAKER_VOLUME: 566 printf("Speaker volume: gain %u\n", 567 hfp_subevent_speaker_volume_get_gain(event)); 568 break; 569 case HFP_SUBEVENT_MICROPHONE_VOLUME: 570 printf("Microphone volume: gain %u\n", 571 hfp_subevent_microphone_volume_get_gain(event)); 572 break; 573 574 case HFP_SUBEVENT_START_RINGING: 575 printf("** START Ringing **\n"); 576 break; 577 case HFP_SUBEVENT_RING: 578 printf("** Ring **\n"); 579 break; 580 case HFP_SUBEVENT_STOP_RINGING: 581 printf("** STOP Ringing **\n"); 582 break; 583 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER: 584 printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event)); 585 // validate number 586 if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0 587 || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0 588 || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){ 589 printf("Dial string valid: accept call\n"); 590 hfp_ag_outgoing_call_accepted(); 591 hfp_outgoing_call_ringing_start(); 592 } else { 593 printf("Dial string invalid: reject call\n"); 594 hfp_ag_outgoing_call_rejected(); 595 } 596 break; 597 598 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG: 599 printf("Attach number to voice tag. Sending '1234567\n"); 600 hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567"); 601 break; 602 603 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES: 604 printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event)); 605 hfp_ag_send_dtmf_code_done(acl_handle); 606 break; 607 608 case HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED: 609 status = hfp_subevent_voice_recognition_activated_get_status(event); 610 if (status != ERROR_CODE_SUCCESS){ 611 printf("Voice Recognition Activate command failed\n"); 612 break; 613 } 614 615 switch (hfp_subevent_voice_recognition_activated_get_enhanced(event)){ 616 case 0: 617 printf("\nVoice recognition ACTVATED\n\n"); 618 break; 619 default: 620 printf("\nEnhanced voice recognition ACTVATED\n\n"); 621 break; 622 } 623 break; 624 625 case HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED: 626 status = hfp_subevent_voice_recognition_deactivated_get_status(event); 627 if (status != ERROR_CODE_SUCCESS){ 628 printf("Voice Recognition Deactivate command failed\n"); 629 break; 630 } 631 printf("\nVoice Recognition DEACTIVATED\n\n"); 632 break; 633 634 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO: 635 status = hfp_subevent_enhanced_voice_recognition_hf_ready_for_audio_get_status(event); 636 report_status(status, "Enhanced Voice recognition: READY FOR AUDIO"); 637 break; 638 639 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT: 640 status = hfp_subevent_enhanced_voice_recognition_ag_ready_to_accept_audio_input_get_status(event); 641 report_status(status, "Enhanced Voice recognition: AG READY TO ACCEPT AUDIO INPUT"); 642 break; 643 644 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND: 645 status = hfp_subevent_enhanced_voice_recognition_ag_is_starting_sound_get_status(event); 646 report_status(status, "Enhanced Voice recognition: AG IS STARTING SOUND"); 647 break; 648 649 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT: 650 status = hfp_subevent_enhanced_voice_recognition_ag_is_processing_audio_input_get_status(event); 651 report_status(status, "Enhanced Voice recognition: AG IS PROCESSING AUDIO INPUT"); 652 break; 653 654 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT: 655 status = hfp_subevent_enhanced_voice_recognition_ag_message_sent_get_status(event); 656 report_status(status, "Enhanced Voice recognition: AG MESSAGE SENT"); 657 printf("Enhanced Voice recognition: \'%s\'\n\n", msg.text); 658 break; 659 660 case HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE: 661 status = hfp_subevent_echo_canceling_and_noise_reduction_deactivate_get_status(event); 662 report_status(status, "Echo Canceling and Noise Reduction Deactivate"); 663 break; 664 665 case HFP_SUBEVENT_HF_INDICATOR: 666 switch (hfp_subevent_hf_indicator_get_uuid(event)){ 667 case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 668 printf("Received ENHANCED SAFETY indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event)); 669 break; 670 case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 671 printf("Received BATTERY LEVEL indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event)); 672 break; 673 default: 674 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)); 675 break; 676 } 677 break; 678 default: 679 break; 680 } 681 break; 682 case HCI_SCO_DATA_PACKET: 683 if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break; 684 sco_demo_receive(event, event_size); 685 break; 686 default: 687 break; 688 } 689 } 690 691 static hfp_phone_number_t subscriber_number = { 692 129, "225577" 693 }; 694 695 /* @section Main Application Setup 696 * 697 * @text Listing MainConfiguration shows main application code. 698 * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it. 699 * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers. 700 * The stdin_process callback allows for sending commands to the HFP HF. 701 * At the end the Bluetooth stack is started. 702 */ 703 704 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */ 705 706 int btstack_main(int argc, const char * argv[]); 707 int btstack_main(int argc, const char * argv[]){ 708 (void)argc; 709 (void)argv; 710 711 sco_demo_init(); 712 713 gap_set_local_name("HFP AG Demo 00:00:00:00:00:00"); 714 gap_discoverable_control(1); 715 716 // L2CAP 717 l2cap_init(); 718 719 #ifdef ENABLE_BLE 720 // Initialize LE Security Manager. Needed for cross-transport key derivation 721 sm_init(); 722 #endif 723 724 uint16_t supported_features = 725 (1<<HFP_AGSF_ESCO_S4) | 726 (1<<HFP_AGSF_HF_INDICATORS) | 727 (1<<HFP_AGSF_CODEC_NEGOTIATION) | 728 (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) | 729 (1<<HFP_AGSF_ENHANCED_CALL_CONTROL) | 730 (1<<HFP_AGSF_ENHANCED_CALL_STATUS) | 731 (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL) | 732 (1<<HFP_AGSF_IN_BAND_RING_TONE) | 733 (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION) | 734 (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) | 735 (1<<HFP_AGSF_VOICE_RECOGNITION_TEXT) | 736 (1<<HFP_AGSF_EC_NR_FUNCTION) | 737 (1<<HFP_AGSF_THREE_WAY_CALLING); 738 int wide_band_speech = 1; 739 740 // HFP 741 rfcomm_init(); 742 hfp_ag_init(rfcomm_channel_nr); 743 hfp_ag_init_supported_features(supported_features); 744 hfp_ag_init_codecs(sizeof(codecs), codecs); 745 hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators); 746 hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators); 747 hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services); 748 hfp_ag_set_subcriber_number_information(&subscriber_number, 1); 749 750 // SDP Server 751 sdp_init(); 752 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 753 hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech); 754 printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer)); 755 sdp_register_service(hfp_service_buffer); 756 757 // register for HCI events and SCO packets 758 hci_event_callback_registration.callback = &packet_handler; 759 hci_add_event_handler(&hci_event_callback_registration); 760 hci_register_sco_packet_handler(&packet_handler); 761 762 // register for HFP events 763 hfp_ag_register_packet_handler(&packet_handler); 764 765 // parse human readable Bluetooth address 766 sscanf_bd_addr(device_addr_string, device_addr); 767 768 #ifdef HAVE_BTSTACK_STDIN 769 btstack_stdin_setup(stdin_process); 770 #endif 771 // turn on! 772 hci_power_control(HCI_POWER_ON); 773 return 0; 774 } 775 /* LISTING_END */ 776 /* EXAMPLE_END */ 777