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 MATTHIAS 24 * RINGWALD 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 int ag_indicators_nr = 7; 89 static hfp_ag_indicator_t ag_indicators[] = { 90 // index, name, min range, max range, status, mandatory, enabled, status changed 91 {1, "service", 0, 1, 1, 0, 0, 0}, 92 {2, "call", 0, 1, 0, 1, 1, 0}, 93 {3, "callsetup", 0, 3, 0, 1, 1, 0}, 94 {4, "battchg", 0, 5, 3, 0, 0, 0}, 95 {5, "signal", 0, 5, 5, 0, 1, 0}, 96 {6, "roam", 0, 1, 0, 0, 1, 0}, 97 {7, "callheld", 0, 2, 0, 1, 1, 0} 98 }; 99 100 static int call_hold_services_nr = 5; 101 static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"}; 102 103 static int hf_indicators_nr = 2; 104 static hfp_generic_status_indicator_t hf_indicators[] = { 105 {1, 1}, 106 {2, 1}, 107 }; 108 109 static hfp_voice_recognition_message_t msg = { 110 0xABCD, HFP_TEXT_TYPE_MESSAGE_FROM_AG, HFP_TEXT_OPERATION_REPLACE, "test message" 111 }; 112 113 #define INQUIRY_INTERVAL 5 114 115 enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ; 116 enum STATE state = INIT; 117 118 static void dump_supported_codecs(void){ 119 unsigned int i; 120 int mSBC_skipped = 0; 121 printf("Supported codecs: "); 122 for (i = 0; i < sizeof(codecs); i++){ 123 switch(codecs[i]){ 124 case HFP_CODEC_CVSD: 125 printf("CVSD"); 126 break; 127 case HFP_CODEC_MSBC: 128 if (hci_extended_sco_link_supported()){ 129 printf(", mSBC"); 130 } else { 131 mSBC_skipped = 1; 132 } 133 break; 134 default: 135 btstack_assert(false); 136 break; 137 } 138 } 139 printf("\n"); 140 if (mSBC_skipped){ 141 printf("mSBC codec disabled because eSCO not supported by local controller.\n"); 142 } 143 } 144 145 #ifdef HAVE_BTSTACK_STDIN 146 // Testig User Interface 147 static void show_usage(void){ 148 bd_addr_t iut_address; 149 gap_local_bd_addr(iut_address); 150 151 printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address)); 152 printf("\n"); 153 154 printf("a - establish HFP connection to %s\n", bd_addr_to_str(device_addr)); 155 // printf("A - release HFP connection\n"); 156 157 printf("b - establish AUDIO connection | B - release AUDIO connection\n"); 158 printf("c - simulate incoming call from 1234567 | C - simulate call from 1234567 dropped\n"); 159 printf("d - report AG failure\n"); 160 printf("D - delete all link keys\n"); 161 printf("e - answer call on AG | E - reject call on AG\n"); 162 printf("r - disable in-band ring tone | R - enable in-band ring tone\n"); 163 printf("f - Disable cellular network | F - Enable cellular network\n"); 164 printf("g - Set signal strength to 0 | G - Set signal strength to 5\n"); 165 printf("h - Disable roaming | H - Enable roaming\n"); 166 printf("i - Set battery level to 3 | I - Set battery level to 5\n"); 167 printf("j - Answering call on remote side\n"); 168 printf("k - Clear memory #1 | K - Set memory #1\n"); 169 printf("l - Clear last number | L - Set last number\n"); 170 printf("m - simulate incoming call from 7654321\n"); 171 // printf("M - simulate call from 7654321 dropped\n"); 172 printf("n - Disable Voice Recognition | N - Enable Voice Recognition\n"); 173 printf("z - Disable Enhanced Voice Recognition | Z - Enable Enhanced Voice Recognition\n"); 174 printf("1 - EVR play sound | 2 - EVR report ready for audio input\n"); 175 printf("3 - EVR report processing input | 4 - EVR send message\n"); 176 177 printf("o - Set speaker volume to 0 (minimum) | O - Set speaker volume to 9 (default)\n"); 178 printf("p - Set speaker volume to 12 (higher) | P - Set speaker volume to 15 (maximum)\n"); 179 printf("q - Set microphone gain to 0 (minimum) | Q - Set microphone gain to 9 (default)\n"); 180 printf("s - Set microphone gain to 12 (higher) | S - Set microphone gain to 15 (maximum)\n"); 181 printf("t - terminate connection\n"); 182 printf("u - join held call\n"); 183 printf("v - discover nearby HF units\n"); 184 printf("w - put incoming call on hold (Response and Hold)\n"); 185 printf("x - accept held incoming call (Response and Hold)\n"); 186 printf("X - reject held incoming call (Response and Hold)\n"); 187 printf("---\n"); 188 } 189 190 static void stdin_process(char cmd){ 191 uint8_t status = ERROR_CODE_SUCCESS; 192 193 switch (cmd){ 194 case 'a': 195 log_info("USER:\'%c\'", cmd); 196 printf("Establish HFP service level connection to %s...\n", bd_addr_to_str(device_addr)); 197 status = hfp_ag_establish_service_level_connection(device_addr); 198 break; 199 case 'A': 200 log_info("USER:\'%c\'", cmd); 201 printf("Release HFP service level connection.\n"); 202 status = hfp_ag_release_service_level_connection(acl_handle); 203 break; 204 205 case 'b': 206 log_info("USER:\'%c\'", cmd); 207 printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr)); 208 status = hfp_ag_establish_audio_connection(acl_handle); 209 break; 210 case 'B': 211 log_info("USER:\'%c\'", cmd); 212 printf("Release Audio connection.\n"); 213 status = hfp_ag_release_audio_connection(acl_handle); 214 break; 215 case 'c': 216 log_info("USER:\'%c\'", cmd); 217 printf("Simulate incoming call from 1234567\n"); 218 hfp_ag_set_clip(129, "1234567"); 219 hfp_ag_incoming_call(); 220 break; 221 case 'D': 222 printf("Deleting all link keys\n"); 223 gap_delete_all_link_keys(); 224 break; 225 case 'm': 226 log_info("USER:\'%c\'", cmd); 227 printf("Simulate incoming call from 7654321\n"); 228 hfp_ag_set_clip(129, "7654321"); 229 hfp_ag_incoming_call(); 230 break; 231 case 'C': 232 log_info("USER:\'%c\'", cmd); 233 printf("Simulate terminate call\n"); 234 hfp_ag_call_dropped(); 235 break; 236 case 'd': 237 log_info("USER:\'%c\'", cmd); 238 printf("Report AG failure\n"); 239 status = hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE); 240 break; 241 case 'e': 242 log_info("USER:\'%c\'", cmd); 243 printf("Answer call on AG\n"); 244 hfp_ag_answer_incoming_call(); 245 break; 246 case 'E': 247 log_info("USER:\'%c\'", cmd); 248 printf("Reject call on AG\n"); 249 hfp_ag_terminate_call(); 250 break; 251 case 'f': 252 log_info("USER:\'%c\'", cmd); 253 printf("Disable cellular network\n"); 254 status = hfp_ag_set_registration_status(0); 255 break; 256 case 'F': 257 log_info("USER:\'%c\'", cmd); 258 printf("Enable cellular network\n"); 259 status = hfp_ag_set_registration_status(1); 260 break; 261 case 'g': 262 log_info("USER:\'%c\'", cmd); 263 printf("Set signal strength to 0\n"); 264 status = hfp_ag_set_signal_strength(0); 265 break; 266 case 'G': 267 log_info("USER:\'%c\'", cmd); 268 printf("Set signal strength to 5\n"); 269 status = hfp_ag_set_signal_strength(5); 270 break; 271 case 'h': 272 log_info("USER:\'%c\'", cmd); 273 printf("Disable roaming\n"); 274 status = hfp_ag_set_roaming_status(0); 275 break; 276 case 'H': 277 log_info("USER:\'%c\'", cmd); 278 printf("Enable roaming\n"); 279 status = hfp_ag_set_roaming_status(1); 280 break; 281 case 'i': 282 log_info("USER:\'%c\'", cmd); 283 printf("Set battery level to 3\n"); 284 status = hfp_ag_set_battery_level(3); 285 break; 286 case 'I': 287 log_info("USER:\'%c\'", cmd); 288 printf("Set battery level to 5\n"); 289 status = hfp_ag_set_battery_level(5); 290 break; 291 case 'j': 292 log_info("USER:\'%c\'", cmd); 293 printf("Answering call on remote side\n"); 294 hfp_ag_outgoing_call_established(); 295 break; 296 case 'r': 297 log_info("USER:\'%c\'", cmd); 298 printf("Disable in-band ring tone\n"); 299 hfp_ag_set_use_in_band_ring_tone(0); 300 break; 301 case 'k': 302 log_info("USER:\'%c\'", cmd); 303 printf("Memory 1 cleared\n"); 304 memory_1_enabled = 0; 305 break; 306 case 'K': 307 log_info("USER:\'%c\'", cmd); 308 printf("Memory 1 set\n"); 309 memory_1_enabled = 1; 310 break; 311 case 'l': 312 log_info("USER:\'%c\'", cmd); 313 printf("Last dialed number cleared\n"); 314 hfp_ag_clear_last_dialed_number(); 315 break; 316 case 'L': 317 log_info("USER:\'%c\'", cmd); 318 printf("Outgoing call connected, ringing\n"); 319 hfp_ag_outgoing_call_ringing(); 320 break; 321 case 'n': 322 log_info("USER:\'%c\'", cmd); 323 printf("Disable Voice Recognition\n"); 324 status = hfp_ag_deactivate_voice_recognition(acl_handle); 325 break; 326 case 'N': 327 log_info("USER:\'%c\'", cmd); 328 printf("Enable Voice Recognition\n"); 329 status = hfp_ag_activate_voice_recognition(acl_handle); 330 break; 331 332 case 'z': 333 log_info("USER:\'%c\'", cmd); 334 printf("Disable Enhanced Voice Recognition\n"); 335 status = hfp_ag_deactivate_enhanced_voice_recognition(acl_handle); 336 break; 337 case 'Z': 338 log_info("USER:\'%c\'", cmd); 339 printf("Enable Enhanced_Voice Recognition\n"); 340 status = hfp_ag_activate_enhanced_voice_recognition(acl_handle); 341 break; 342 343 case '1': 344 log_info("USER:\'%c\'", cmd); 345 printf("Enhanced_Voice Recognition: play sound\n"); 346 status = hfp_ag_enhanced_voice_recognition_report_sending_audio(acl_handle); 347 break; 348 case '2': 349 log_info("USER:\'%c\'", cmd); 350 printf("Enhanced_Voice Recognition: report ready for audio input\n"); 351 status = hfp_ag_enhanced_voice_recognition_report_ready_for_audio(acl_handle); 352 break; 353 case '3': 354 log_info("USER:\'%c\'", cmd); 355 printf("Enhanced_Voice Recognition: report processing input\n"); 356 status = hfp_ag_enhanced_voice_recognition_report_processing_input(acl_handle); 357 break; 358 case '4': 359 log_info("USER:\'%c\'", cmd); 360 printf("Enhanced_Voice Recognition: send message\n"); 361 status = hfp_ag_enhanced_voice_recognition_send_message(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT, msg); 362 break; 363 364 case 'o': 365 log_info("USER:\'%c\'", cmd); 366 printf("Set speaker gain to 0 (minimum)\n"); 367 status = hfp_ag_set_speaker_gain(acl_handle, 0); 368 break; 369 case 'O': 370 log_info("USER:\'%c\'", cmd); 371 printf("Set speaker gain to 9 (default)\n"); 372 status = hfp_ag_set_speaker_gain(acl_handle, 9); 373 break; 374 case 'p': 375 log_info("USER:\'%c\'", cmd); 376 printf("Set speaker gain to 12 (higher)\n"); 377 status = hfp_ag_set_speaker_gain(acl_handle, 12); 378 break; 379 case 'P': 380 log_info("USER:\'%c\'", cmd); 381 printf("Set speaker gain to 15 (maximum)\n"); 382 status = hfp_ag_set_speaker_gain(acl_handle, 15); 383 break; 384 case 'q': 385 log_info("USER:\'%c\'", cmd); 386 printf("Set microphone gain to 0\n"); 387 status = hfp_ag_set_microphone_gain(acl_handle, 0); 388 break; 389 case 'Q': 390 log_info("USER:\'%c\'", cmd); 391 printf("Set microphone gain to 9\n"); 392 status = hfp_ag_set_microphone_gain(acl_handle, 9); 393 break; 394 case 's': 395 log_info("USER:\'%c\'", cmd); 396 printf("Set microphone gain to 12\n"); 397 status = hfp_ag_set_microphone_gain(acl_handle, 12); 398 break; 399 case 'S': 400 log_info("USER:\'%c\'", cmd); 401 printf("Set microphone gain to 15\n"); 402 status = hfp_ag_set_microphone_gain(acl_handle, 15); 403 break; 404 case 'R': 405 log_info("USER:\'%c\'", cmd); 406 printf("Enable in-band ring tone\n"); 407 hfp_ag_set_use_in_band_ring_tone(1); 408 break; 409 case 't': 410 log_info("USER:\'%c\'", cmd); 411 printf("Terminate HCI connection. 0x%2x\n", acl_handle); 412 gap_disconnect(acl_handle); 413 break; 414 case 'u': 415 log_info("USER:\'%c\'", cmd); 416 printf("Join held call\n"); 417 hfp_ag_join_held_call(); 418 break; 419 case 'v': 420 printf("Start scanning...\n"); 421 gap_inquiry_start(INQUIRY_INTERVAL); 422 break; 423 case 'w': 424 log_info("USER:\'%c\'", cmd); 425 printf("AG: Put incoming call on hold (Response and Hold)\n"); 426 hfp_ag_hold_incoming_call(); 427 break; 428 case 'x': 429 log_info("USER:\'%c\'", cmd); 430 printf("AG: Accept held incoming call (Response and Hold)\n"); 431 hfp_ag_accept_held_incoming_call(); 432 break; 433 case 'X': 434 log_info("USER:\'%c\'", cmd); 435 printf("AG: Reject held incoming call (Response and Hold)\n"); 436 hfp_ag_reject_held_incoming_call(); 437 break; 438 default: 439 show_usage(); 440 break; 441 } 442 443 if (status != ERROR_CODE_SUCCESS){ 444 printf("Could not perform command, status 0x%02x\n", status); 445 } 446 } 447 #endif 448 449 static void report_status(uint8_t status, const char * message){ 450 if (status != ERROR_CODE_SUCCESS){ 451 printf("%s command failed, status 0x%02x\n", message, status); 452 } else { 453 printf("%s command successful\n", message); 454 } 455 } 456 457 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 458 UNUSED(channel); 459 bd_addr_t addr; 460 uint8_t status; 461 switch (packet_type){ 462 case HCI_EVENT_PACKET: 463 switch(hci_event_packet_get_type(event)){ 464 #ifndef HAVE_BTSTACK_STDIN 465 case BTSTACK_EVENT_STATE: 466 if (btstack_event_state_get_state(event) != HCI_STATE_WORKING) break; 467 printf("Establish HFP AG service level connection to %s...\n", bd_addr_to_str(device_addr)); 468 hfp_ag_establish_service_level_connection(device_addr); 469 break; 470 #endif 471 case GAP_EVENT_INQUIRY_RESULT: 472 gap_event_inquiry_result_get_bd_addr(event, addr); 473 // print info 474 printf("Device found: %s ", bd_addr_to_str(addr)); 475 printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(event)); 476 if (gap_event_inquiry_result_get_rssi_available(event)){ 477 printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(event)); 478 } 479 if (gap_event_inquiry_result_get_name_available(event)){ 480 char name_buffer[240]; 481 int name_len = gap_event_inquiry_result_get_name_len(event); 482 memcpy(name_buffer, gap_event_inquiry_result_get_name(event), name_len); 483 name_buffer[name_len] = 0; 484 printf(", name '%s'", name_buffer); 485 } 486 printf("\n"); 487 break; 488 case GAP_EVENT_INQUIRY_COMPLETE: 489 printf("Inquiry scan complete.\n"); 490 break; 491 case HCI_EVENT_SCO_CAN_SEND_NOW: 492 sco_demo_send(sco_handle); 493 break; 494 case HCI_EVENT_COMMAND_COMPLETE: 495 if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){ 496 dump_supported_codecs(); 497 } 498 break; 499 default: 500 break; 501 } 502 503 if (hci_event_packet_get_type(event) != HCI_EVENT_HFP_META) return; 504 505 switch (hci_event_hfp_meta_get_subevent_code(event)) { 506 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 507 status = hfp_subevent_service_level_connection_established_get_status(event); 508 if (status != ERROR_CODE_SUCCESS){ 509 printf("Connection failed, status 0x%02x\n", status); 510 break; 511 } 512 acl_handle = hfp_subevent_service_level_connection_established_get_acl_handle(event); 513 hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr); 514 printf("Service level connection established to %s.\n", bd_addr_to_str(device_addr)); 515 dump_supported_codecs(); 516 #ifndef HAVE_BTSTACK_STDIN 517 log_info("Establish Audio connection %s", bd_addr_to_str(device_addr)); 518 printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr)); 519 hfp_ag_establish_audio_connection(acl_handle); 520 #endif 521 break; 522 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 523 printf("Service level connection released.\n"); 524 acl_handle = HCI_CON_HANDLE_INVALID; 525 break; 526 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 527 if (hfp_subevent_audio_connection_established_get_status(event) != ERROR_CODE_SUCCESS){ 528 printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event)); 529 } else { 530 sco_handle = hfp_subevent_audio_connection_established_get_sco_handle(event); 531 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 532 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event); 533 switch (negotiated_codec){ 534 case 0x01: 535 printf("Using CVSD codec.\n"); 536 break; 537 case 0x02: 538 printf("Using mSBC codec.\n"); 539 break; 540 default: 541 printf("Using unknown codec 0x%02x.\n", negotiated_codec); 542 break; 543 } 544 sco_demo_set_codec(negotiated_codec); 545 hci_request_sco_can_send_now_event(); 546 } 547 break; 548 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 549 printf("Audio connection released\n"); 550 sco_handle = HCI_CON_HANDLE_INVALID; 551 sco_demo_close(); 552 break; 553 case HFP_SUBEVENT_START_RINGINIG: 554 printf("** START Ringing **\n"); 555 break; 556 case HFP_SUBEVENT_RING: 557 printf("** Ring **\n"); 558 break; 559 case HFP_SUBEVENT_STOP_RINGINIG: 560 printf("** STOP Ringing **\n"); 561 break; 562 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER: 563 printf("Outgoing call '%s'\n", hfp_subevent_place_call_with_number_get_number(event)); 564 // validate number 565 if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0 566 || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0 567 || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){ 568 printf("Dial string valid: accept call\n"); 569 hfp_ag_outgoing_call_accepted(); 570 } else { 571 printf("Dial string invalid: reject call\n"); 572 hfp_ag_outgoing_call_rejected(); 573 } 574 break; 575 576 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG: 577 printf("Attach number to voice tag. Sending '1234567\n"); 578 hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567"); 579 break; 580 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES: 581 printf("Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event)); 582 hfp_ag_send_dtmf_code_done(acl_handle); 583 break; 584 case HFP_SUBEVENT_CALL_ANSWERED: 585 printf("Call answered by HF\n"); 586 break; 587 588 case HFP_SUBEVENT_VOICE_RECOGNITION_STATUS: 589 status = hfp_subevent_voice_recognition_status_get_status(event); 590 if (status != ERROR_CODE_SUCCESS){ 591 printf("Voice Recognition command failed\n"); 592 break; 593 } 594 595 if (hfp_subevent_voice_recognition_status_get_state(event) > 0){ 596 printf("\nVoice recognition status ACTIVATED\n\n"); 597 } else { 598 printf("\nVoice recognition status DEACTIVATED\n\n"); 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 report_status(status, "Enhanced Voice recognition: READY FOR AUDIO"); 605 break; 606 607 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT: 608 status = hfp_subevent_enhanced_voice_recognition_ag_ready_to_accept_audio_input_get_status(event); 609 report_status(status, "Enhanced Voice recognition: AG READY TO ACCEPT AUDIO INPUT"); 610 break; 611 612 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND: 613 status = hfp_subevent_enhanced_voice_recognition_ag_is_starting_sound_get_status(event); 614 report_status(status, "Enhanced Voice recognition: AG IS STARTING SOUND"); 615 break; 616 617 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT: 618 status = hfp_subevent_enhanced_voice_recognition_ag_is_processing_audio_input_get_status(event); 619 report_status(status, "Enhanced Voice recognition: AG IS PROCESSING AUDIO INPUT"); 620 break; 621 622 case HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT: 623 status = hfp_subevent_enhanced_voice_recognition_ag_message_sent_get_status(event); 624 report_status(status, "Enhanced Voice recognition: AG MESSAGE SENT"); 625 printf("Enhanced Voice recognition: \'%s\'\n\n", msg.text); 626 break; 627 628 case HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE: 629 status = hfp_subevent_echo_canceling_and_noise_reduction_deactivate_get_status(event); 630 report_status(status, "Echo Canceling and Noise Reduction Deactivate"); 631 break; 632 633 case HFP_SUBEVENT_HF_INDICATOR: 634 switch (hfp_subevent_hf_indicator_get_uuid(event)){ 635 case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 636 printf("Received ENHANCED SAFETY indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event)); 637 break; 638 case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 639 printf("Received BATTERY LEVEL indicator, value %d\n", hfp_subevent_hf_indicator_get_value(event)); 640 break; 641 default: 642 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)); 643 break; 644 } 645 break; 646 default: 647 break; 648 } 649 break; 650 case HCI_SCO_DATA_PACKET: 651 if (READ_SCO_CONNECTION_HANDLE(event) != sco_handle) break; 652 sco_demo_receive(event, event_size); 653 break; 654 default: 655 break; 656 } 657 } 658 659 static hfp_phone_number_t subscriber_number = { 660 129, "225577" 661 }; 662 663 /* @section Main Application Setup 664 * 665 * @text Listing MainConfiguration shows main application code. 666 * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it. 667 * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers. 668 * The stdin_process callback allows for sending commands to the HFP HF. 669 * At the end the Bluetooth stack is started. 670 */ 671 672 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */ 673 674 int btstack_main(int argc, const char * argv[]); 675 int btstack_main(int argc, const char * argv[]){ 676 (void)argc; 677 (void)argv; 678 679 sco_demo_init(); 680 681 gap_set_local_name("HFP AG Demo 00:00:00:00:00:00"); 682 gap_discoverable_control(1); 683 684 // L2CAP 685 l2cap_init(); 686 687 uint16_t supported_features = 688 (1<<HFP_AGSF_ESCO_S4) | 689 (1<<HFP_AGSF_HF_INDICATORS) | 690 (1<<HFP_AGSF_CODEC_NEGOTIATION) | 691 (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) | 692 (1<<HFP_AGSF_ENHANCED_CALL_CONTROL) | 693 (1<<HFP_AGSF_ENHANCED_CALL_STATUS) | 694 (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL) | 695 (1<<HFP_AGSF_IN_BAND_RING_TONE) | 696 (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION) | 697 (1<<HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) | 698 (1<<HFP_AGSF_EC_NR_FUNCTION) | 699 (1<<HFP_AGSF_THREE_WAY_CALLING); 700 int wide_band_speech = 1; 701 702 // HFP 703 rfcomm_init(); 704 hfp_ag_init(rfcomm_channel_nr); 705 hfp_ag_init_supported_features(supported_features); 706 hfp_ag_init_codecs(sizeof(codecs), codecs); 707 hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators); 708 hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators); 709 hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services); 710 hfp_ag_set_subcriber_number_information(&subscriber_number, 1); 711 712 // SDP Server 713 sdp_init(); 714 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 715 hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech); 716 printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer)); 717 sdp_register_service(hfp_service_buffer); 718 719 // register for HCI events and SCO packets 720 hci_event_callback_registration.callback = &packet_handler; 721 hci_add_event_handler(&hci_event_callback_registration); 722 hci_register_sco_packet_handler(&packet_handler); 723 724 // register for HFP events 725 hfp_ag_register_packet_handler(&packet_handler); 726 727 // parse human readable Bluetooth address 728 sscanf_bd_addr(device_addr_string, device_addr); 729 730 #ifdef HAVE_BTSTACK_STDIN 731 btstack_stdin_setup(stdin_process); 732 #endif 733 // turn on! 734 hci_power_control(HCI_POWER_ON); 735 return 0; 736 } 737 /* LISTING_END */ 738 /* EXAMPLE_END */ 739