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 /* 39 * hfp_ag_demo.c 40 */ 41 42 // ***************************************************************************** 43 /* EXAMPLE_START(hfp_ag_demo): HFP Audio Gateway (AG) Demo 44 * 45 * @text This HFP Audio Gateway example demonstrates how to receive 46 * an output from a remote HFP Hands-Free (HF) unit, and, 47 * if HAVE_POSIX_STDIN is defined, how to control the HFP HF. 48 */ 49 // ***************************************************************************** 50 51 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 #include "btstack.h" 59 #include "sco_demo_util.h" 60 #ifdef HAVE_POSIX_STDIN 61 #include "stdin_support.h" 62 #endif 63 64 uint8_t hfp_service_buffer[150]; 65 const uint8_t rfcomm_channel_nr = 1; 66 const char hfp_ag_service_name[] = "BTstack HFP AG Test"; 67 68 // PTS 69 // static bd_addr_t device_addr = {0x00,0x15,0x83,0x5F,0x9D,0x46}; 70 // BT-201 71 // static bd_addr_t device_addr = {0x00, 0x07, 0xB0, 0x83, 0x02, 0x5E}; 72 // CC256x 73 // bd_addr_t device_addr = { 0xD0, 0x39, 0x72, 0xCD, 0x83, 0x45}; 74 // Minijamox 75 bd_addr_t device_addr = { 0x00, 0x15, 0x83, 0x5F, 0x9D, 0x46}; 76 77 // static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC}; 78 static uint8_t codecs[] = {HFP_CODEC_CVSD}; 79 static uint8_t negotiated_codec = HFP_CODEC_CVSD; 80 81 static hci_con_handle_t acl_handle = -1; 82 static hci_con_handle_t sco_handle; 83 static int memory_1_enabled = 1; 84 static btstack_packet_callback_registration_t hci_event_callback_registration; 85 86 static int ag_indicators_nr = 7; 87 static hfp_ag_indicator_t ag_indicators[] = { 88 // index, name, min range, max range, status, mandatory, enabled, status changed 89 {1, "service", 0, 1, 1, 0, 0, 0}, 90 {2, "call", 0, 1, 0, 1, 1, 0}, 91 {3, "callsetup", 0, 3, 0, 1, 1, 0}, 92 {4, "battchg", 0, 5, 3, 0, 0, 0}, 93 {5, "signal", 0, 5, 5, 0, 1, 0}, 94 {6, "roam", 0, 1, 0, 0, 1, 0}, 95 {7, "callheld", 0, 2, 0, 1, 1, 0} 96 }; 97 98 static int call_hold_services_nr = 5; 99 static const char* call_hold_services[] = {"1", "1x", "2", "2x", "3"}; 100 101 static int hf_indicators_nr = 2; 102 static hfp_generic_status_indicator_t hf_indicators[] = { 103 {1, 1}, 104 {2, 1}, 105 }; 106 107 char cmd; 108 109 // GAP INQUIRY 110 111 #define MAX_DEVICES 10 112 enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED }; 113 struct device { 114 bd_addr_t address; 115 uint16_t clockOffset; 116 uint32_t classOfDevice; 117 uint8_t pageScanRepetitionMode; 118 uint8_t rssi; 119 enum DEVICE_STATE state; 120 }; 121 122 #define INQUIRY_INTERVAL 5 123 struct device devices[MAX_DEVICES]; 124 int deviceCount = 0; 125 126 127 enum STATE {INIT, W4_INQUIRY_MODE_COMPLETE, ACTIVE} ; 128 enum STATE state = INIT; 129 130 static void dump_supported_codecs(void){ 131 int i; 132 int mSBC_skipped = 0; 133 printf("Supported codecs:"); 134 for (i = 0; i < sizeof(codecs); i++){ 135 switch(codecs[i]){ 136 case HFP_CODEC_CVSD: 137 printf(" CVSD"); 138 break; 139 case HFP_CODEC_MSBC: 140 if (hci_extended_sco_link_supported()){ 141 printf("mSBC"); 142 } else { 143 mSBC_skipped = 1; 144 } 145 break; 146 } 147 } 148 printf("\n"); 149 if (mSBC_skipped){ 150 printf("mSBC codec disabled because eSCO not supported by local controller.\n"); 151 } 152 } 153 154 static int getDeviceIndexForAddress( bd_addr_t addr){ 155 int j; 156 for (j=0; j< deviceCount; j++){ 157 if (bd_addr_cmp(addr, devices[j].address) == 0){ 158 return j; 159 } 160 } 161 return -1; 162 } 163 164 #ifdef HAVE_POSIX_STDIN 165 static void start_scan(void){ 166 printf("Starting inquiry scan..\n"); 167 hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); 168 } 169 #endif 170 171 static int has_more_remote_name_requests(void){ 172 int i; 173 for (i=0;i<deviceCount;i++) { 174 if (devices[i].state == REMOTE_NAME_REQUEST) return 1; 175 } 176 return 0; 177 } 178 179 static void do_next_remote_name_request(void){ 180 int i; 181 for (i=0;i<deviceCount;i++) { 182 // remote name request 183 if (devices[i].state == REMOTE_NAME_REQUEST){ 184 devices[i].state = REMOTE_NAME_INQUIRED; 185 printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address)); 186 hci_send_cmd(&hci_remote_name_request, devices[i].address, 187 devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000); 188 return; 189 } 190 } 191 } 192 193 static void continue_remote_names(void){ 194 // don't get remote names for testing 195 if (has_more_remote_name_requests()){ 196 do_next_remote_name_request(); 197 return; 198 } 199 // try to find PTS 200 int i; 201 for (i=0;i<deviceCount;i++){ 202 if (memcmp(devices[i].address, device_addr, 6) == 0){ 203 printf("Inquiry scan over, successfully found PTS at index %u\nReady to connect to it.\n", i); 204 return; 205 } 206 } 207 printf("Inquiry scan over but PTS not found :(\n"); 208 } 209 210 static void inquiry_packet_handler (uint8_t packet_type, uint8_t *packet, uint16_t size){ 211 bd_addr_t addr; 212 int i; 213 int numResponses; 214 int index; 215 216 // printf("packet_handler: pt: 0x%02x, packet[0]: 0x%02x\n", packet_type, packet[0]); 217 if (packet_type != HCI_EVENT_PACKET) return; 218 219 uint8_t event = packet[0]; 220 221 switch(event){ 222 case HCI_EVENT_INQUIRY_RESULT: 223 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ 224 numResponses = hci_event_inquiry_result_get_num_responses(packet); 225 int offset = 3; 226 for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){ 227 reverse_bd_addr(&packet[offset], addr); 228 offset += 6; 229 index = getDeviceIndexForAddress(addr); 230 if (index >= 0) continue; // already in our list 231 memcpy(devices[deviceCount].address, addr, 6); 232 233 devices[deviceCount].pageScanRepetitionMode = packet[offset]; 234 offset += 1; 235 236 if (event == HCI_EVENT_INQUIRY_RESULT){ 237 offset += 2; // Reserved + Reserved 238 devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset); 239 offset += 3; 240 devices[deviceCount].clockOffset = little_endian_read_16(packet, offset) & 0x7fff; 241 offset += 2; 242 devices[deviceCount].rssi = 0; 243 } else { 244 offset += 1; // Reserved 245 devices[deviceCount].classOfDevice = little_endian_read_24(packet, offset); 246 offset += 3; 247 devices[deviceCount].clockOffset = little_endian_read_16(packet, offset) & 0x7fff; 248 offset += 2; 249 devices[deviceCount].rssi = packet[offset]; 250 offset += 1; 251 } 252 devices[deviceCount].state = REMOTE_NAME_REQUEST; 253 printf("Device #%u found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n", 254 deviceCount, bd_addr_to_str(addr), 255 devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode, 256 devices[deviceCount].clockOffset, devices[deviceCount].rssi); 257 deviceCount++; 258 } 259 260 break; 261 } 262 case HCI_EVENT_INQUIRY_COMPLETE: 263 for (i=0;i<deviceCount;i++) { 264 // retry remote name request 265 if (devices[i].state == REMOTE_NAME_INQUIRED) 266 devices[i].state = REMOTE_NAME_REQUEST; 267 } 268 continue_remote_names(); 269 break; 270 271 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 272 reverse_bd_addr(&packet[3], addr); 273 index = getDeviceIndexForAddress(addr); 274 if (index >= 0) { 275 if (packet[2] == 0) { 276 printf("Name: '%s'\n", &packet[9]); 277 devices[index].state = REMOTE_NAME_FETCHED; 278 } else { 279 printf("Failed to get name: page timeout\n"); 280 } 281 } 282 continue_remote_names(); 283 break; 284 285 default: 286 break; 287 } 288 } 289 // GAP INQUIRY END 290 #ifdef HAVE_POSIX_STDIN 291 292 // prototypes 293 static void show_usage(void); 294 295 // Testig User Interface 296 static void show_usage(void){ 297 bd_addr_t iut_address; 298 gap_local_bd_addr(iut_address); 299 300 printf("\n--- Bluetooth HFP Audiogateway (AG) unit Test Console %s ---\n", bd_addr_to_str(iut_address)); 301 printf("---\n"); 302 303 printf("a - establish HFP connection to PTS module %s\n", bd_addr_to_str(device_addr)); 304 // printf("A - release HFP connection to PTS module\n"); 305 306 printf("b - establish AUDIO connection\n"); 307 printf("B - release AUDIO connection\n"); 308 309 printf("c - simulate incoming call from 1234567\n"); 310 printf("C - simulate call from 1234567 dropped\n"); 311 312 printf("d - report AG failure\n"); 313 314 printf("e - answer call on AG\n"); 315 printf("E - reject call on AG\n"); 316 317 printf("r - disable in-band ring tone\n"); 318 printf("R - enable in-band ring tone\n"); 319 320 printf("f - Disable cellular network\n"); 321 printf("F - Enable cellular network\n"); 322 323 printf("g - Set signal strength to 0\n"); 324 printf("G - Set signal strength to 5\n"); 325 326 printf("h - Disable roaming\n"); 327 printf("H - Enable roaming\n"); 328 329 printf("i - Set battery level to 3\n"); 330 printf("I - Set battery level to 5\n"); 331 332 printf("j - Answering call on remote side\n"); 333 334 printf("k - Clear memory #1\n"); 335 printf("K - Set memory #1\n"); 336 337 printf("l - Clear last number\n"); 338 printf("L - Set last number\n"); 339 340 printf("m - simulate incoming call from 7654321\n"); 341 // printf("M - simulate call from 7654321 dropped\n"); 342 343 printf("n - Disable Voice Regocnition\n"); 344 printf("N - Enable Voice Recognition\n"); 345 346 printf("o - Set speaker volume to 0 (minimum)\n"); 347 printf("O - Set speaker volume to 9 (default)\n"); 348 printf("p - Set speaker volume to 12 (higher)\n"); 349 printf("P - Set speaker volume to 15 (maximum)\n"); 350 351 printf("q - Set microphone gain to 0 (minimum)\n"); 352 printf("Q - Set microphone gain to 9 (default)\n"); 353 printf("s - Set microphone gain to 12 (higher)\n"); 354 printf("S - Set microphone gain to 15 (maximum)\n"); 355 356 printf("t - terminate connection\n"); 357 printf("u - join held call\n"); 358 printf("v - discover nearby HF units\n"); 359 printf("w - put incoming call on hold (Response and Hold)\n"); 360 printf("x - accept held incoming call (Response and Hold)\n"); 361 printf("X - reject held incoming call (Response and Hold)\n"); 362 363 printf("---\n"); 364 printf("Ctrl-c - exit\n"); 365 printf("---\n"); 366 } 367 368 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ 369 read(ds->fd, &cmd, 1); 370 switch (cmd){ 371 case 'a': 372 log_info("USER:\'%c\'", cmd); 373 printf("Establish HFP service level connection to PTS module %s...\n", bd_addr_to_str(device_addr)); 374 hfp_ag_establish_service_level_connection(device_addr); 375 break; 376 case 'A': 377 log_info("USER:\'%c\'", cmd); 378 printf("Release HFP service level connection.\n"); 379 hfp_ag_release_service_level_connection(acl_handle); 380 break; 381 case 'Z': 382 log_info("USER:\'%c\'", cmd); 383 printf("Release HFP service level connection to %s...\n", bd_addr_to_str(device_addr)); 384 hfp_ag_release_service_level_connection(acl_handle); 385 break; 386 case 'b': 387 log_info("USER:\'%c\'", cmd); 388 printf("Establish Audio connection %s...\n", bd_addr_to_str(device_addr)); 389 hfp_ag_establish_audio_connection(acl_handle); 390 break; 391 case 'B': 392 log_info("USER:\'%c\'", cmd); 393 printf("Release Audio connection.\n"); 394 hfp_ag_release_audio_connection(acl_handle); 395 break; 396 case 'c': 397 log_info("USER:\'%c\'", cmd); 398 printf("Simulate incoming call from 1234567\n"); 399 hfp_ag_set_clip(129, "1234567"); 400 hfp_ag_incoming_call(); 401 break; 402 case 'm': 403 log_info("USER:\'%c\'", cmd); 404 printf("Simulate incoming call from 7654321\n"); 405 hfp_ag_set_clip(129, "7654321"); 406 hfp_ag_incoming_call(); 407 break; 408 case 'C': 409 log_info("USER:\'%c\'", cmd); 410 printf("Simulate terminate call\n"); 411 hfp_ag_call_dropped(); 412 break; 413 case 'd': 414 log_info("USER:\'%c\'", cmd); 415 printf("Report AG failure\n"); 416 hfp_ag_report_extended_audio_gateway_error_result_code(acl_handle, HFP_CME_ERROR_AG_FAILURE); 417 break; 418 case 'e': 419 log_info("USER:\'%c\'", cmd); 420 printf("Answer call on AG\n"); 421 hfp_ag_answer_incoming_call(); 422 break; 423 case 'E': 424 log_info("USER:\'%c\'", cmd); 425 printf("Reject call on AG\n"); 426 hfp_ag_terminate_call(); 427 break; 428 case 'f': 429 log_info("USER:\'%c\'", cmd); 430 printf("Disable cellular network\n"); 431 hfp_ag_set_registration_status(0); 432 break; 433 case 'F': 434 log_info("USER:\'%c\'", cmd); 435 printf("Enable cellular network\n"); 436 hfp_ag_set_registration_status(1); 437 break; 438 case 'g': 439 log_info("USER:\'%c\'", cmd); 440 printf("Set signal strength to 0\n"); 441 hfp_ag_set_signal_strength(0); 442 break; 443 case 'G': 444 log_info("USER:\'%c\'", cmd); 445 printf("Set signal strength to 5\n"); 446 hfp_ag_set_signal_strength(5); 447 break; 448 case 'h': 449 log_info("USER:\'%c\'", cmd); 450 printf("Disable roaming\n"); 451 hfp_ag_set_roaming_status(0); 452 break; 453 case 'H': 454 log_info("USER:\'%c\'", cmd); 455 printf("Enable roaming\n"); 456 hfp_ag_set_roaming_status(1); 457 break; 458 case 'i': 459 log_info("USER:\'%c\'", cmd); 460 printf("Set battery level to 3\n"); 461 hfp_ag_set_battery_level(3); 462 break; 463 case 'I': 464 log_info("USER:\'%c\'", cmd); 465 printf("Set battery level to 5\n"); 466 hfp_ag_set_battery_level(5); 467 break; 468 case 'j': 469 log_info("USER:\'%c\'", cmd); 470 printf("Answering call on remote side\n"); 471 hfp_ag_outgoing_call_established(); 472 break; 473 case 'r': 474 log_info("USER:\'%c\'", cmd); 475 printf("Disable in-band ring tone\n"); 476 hfp_ag_set_use_in_band_ring_tone(0); 477 break; 478 case 'k': 479 log_info("USER:\'%c\'", cmd); 480 printf("Memory 1 cleared\n"); 481 memory_1_enabled = 0; 482 break; 483 case 'K': 484 log_info("USER:\'%c\'", cmd); 485 printf("Memory 1 set\n"); 486 memory_1_enabled = 1; 487 break; 488 case 'l': 489 log_info("USER:\'%c\'", cmd); 490 printf("Last dialed number cleared\n"); 491 hfp_ag_clear_last_dialed_number(); 492 break; 493 case 'L': 494 log_info("USER:\'%c\'", cmd); 495 printf("Outgoing call connected, ringing\n"); 496 hfp_ag_outgoing_call_ringing(); 497 break; 498 case 'n': 499 log_info("USER:\'%c\'", cmd); 500 printf("Disable Voice Recognition\n"); 501 hfp_ag_activate_voice_recognition(acl_handle, 0); 502 break; 503 case 'N': 504 log_info("USER:\'%c\'", cmd); 505 printf("Enable Voice Recognition\n"); 506 hfp_ag_activate_voice_recognition(acl_handle, 1); 507 break; 508 case 'o': 509 log_info("USER:\'%c\'", cmd); 510 printf("Set speaker gain to 0 (minimum)\n"); 511 hfp_ag_set_speaker_gain(acl_handle, 0); 512 break; 513 case 'O': 514 log_info("USER:\'%c\'", cmd); 515 printf("Set speaker gain to 9 (default)\n"); 516 hfp_ag_set_speaker_gain(acl_handle, 9); 517 break; 518 case 'p': 519 log_info("USER:\'%c\'", cmd); 520 printf("Set speaker gain to 12 (higher)\n"); 521 hfp_ag_set_speaker_gain(acl_handle, 12); 522 break; 523 case 'P': 524 log_info("USER:\'%c\'", cmd); 525 printf("Set speaker gain to 15 (maximum)\n"); 526 hfp_ag_set_speaker_gain(acl_handle, 15); 527 break; 528 case 'q': 529 log_info("USER:\'%c\'", cmd); 530 printf("Set microphone gain to 0\n"); 531 hfp_ag_set_microphone_gain(acl_handle, 0); 532 break; 533 case 'Q': 534 log_info("USER:\'%c\'", cmd); 535 printf("Set microphone gain to 9\n"); 536 hfp_ag_set_microphone_gain(acl_handle, 9); 537 break; 538 case 's': 539 log_info("USER:\'%c\'", cmd); 540 printf("Set microphone gain to 12\n"); 541 hfp_ag_set_microphone_gain(acl_handle, 12); 542 break; 543 case 'S': 544 log_info("USER:\'%c\'", cmd); 545 printf("Set microphone gain to 15\n"); 546 hfp_ag_set_microphone_gain(acl_handle, 15); 547 break; 548 case 'R': 549 log_info("USER:\'%c\'", cmd); 550 printf("Enable in-band ring tone\n"); 551 hfp_ag_set_use_in_band_ring_tone(1); 552 break; 553 case 't': 554 log_info("USER:\'%c\'", cmd); 555 printf("Terminate HCI connection. 0x%2x\n", acl_handle); 556 gap_disconnect(acl_handle); 557 break; 558 case 'u': 559 log_info("USER:\'%c\'", cmd); 560 printf("Join held call\n"); 561 hfp_ag_join_held_call(); 562 break; 563 case 'v': 564 start_scan(); 565 break; 566 case 'w': 567 log_info("USER:\'%c\'", cmd); 568 printf("AG: Put incoming call on hold (Response and Hold)\n"); 569 hfp_ag_hold_incoming_call(); 570 break; 571 case 'x': 572 log_info("USER:\'%c\'", cmd); 573 printf("AG: Accept held incoming call (Response and Hold)\n"); 574 hfp_ag_accept_held_incoming_call(); 575 break; 576 case 'X': 577 log_info("USER:\'%c\'", cmd); 578 printf("AG: Reject held incoming call (Response and Hold)\n"); 579 hfp_ag_reject_held_incoming_call(); 580 break; 581 default: 582 show_usage(); 583 break; 584 } 585 } 586 #endif 587 588 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 589 switch (packet_type){ 590 case HCI_EVENT_PACKET: 591 switch (event[0]){ 592 case HCI_EVENT_INQUIRY_RESULT: 593 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 594 case HCI_EVENT_INQUIRY_COMPLETE: 595 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 596 inquiry_packet_handler(HCI_EVENT_PACKET, event, event_size); 597 break; 598 case HCI_EVENT_SCO_CAN_SEND_NOW: 599 sco_demo_send(sco_handle); 600 break; 601 case HCI_EVENT_COMMAND_COMPLETE: 602 if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){ 603 dump_supported_codecs(); 604 } 605 break; 606 default: 607 break; 608 } 609 610 if (event[0] != HCI_EVENT_HFP_META) return; 611 612 if (event[3] 613 && event[2] != HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER 614 && event[2] != HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG 615 && event[2] != HFP_SUBEVENT_TRANSMIT_DTMF_CODES){ 616 printf("ERROR, status: %u\n", event[3]); 617 return; 618 } 619 620 switch (event[2]) { 621 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 622 acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event); 623 hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr); 624 printf("Service level connection established from %s.\n", bd_addr_to_str(device_addr)); 625 dump_supported_codecs(); 626 break; 627 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 628 printf("Service level connection released.\n"); 629 sco_handle = 0; 630 break; 631 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 632 if (hfp_subevent_audio_connection_established_get_status(event)){ 633 printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event)); 634 sco_handle = 0; 635 } else { 636 sco_handle = hfp_subevent_audio_connection_established_get_handle(event); 637 printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 638 negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event); 639 switch (negotiated_codec){ 640 case 0x01: 641 printf("Using CVSD codec.\n"); 642 break; 643 case 0x02: 644 printf("Using mSBC codec.\n"); 645 break; 646 default: 647 printf("Using unknown codec 0x%02x.\n", negotiated_codec); 648 break; 649 } 650 sco_demo_set_codec(negotiated_codec); 651 hci_request_sco_can_send_now_event(); 652 } 653 break; 654 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 655 printf("\n** Audio connection released **\n"); 656 sco_handle = 0; 657 break; 658 case HFP_SUBEVENT_START_RINGINIG: 659 printf("\n** Start Ringing **\n"); 660 break; 661 case HFP_SUBEVENT_STOP_RINGINIG: 662 printf("\n** Stop Ringing **\n"); 663 break; 664 case HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER: 665 printf("\n** Outgoing call '%s' **\n", hfp_subevent_place_call_with_number_get_number(event)); 666 // validate number 667 if ( strcmp("1234567", hfp_subevent_place_call_with_number_get_number(event)) == 0 668 || strcmp("7654321", hfp_subevent_place_call_with_number_get_number(event)) == 0 669 || (memory_1_enabled && strcmp(">1", hfp_subevent_place_call_with_number_get_number(event)) == 0)){ 670 printf("Dialstring valid: accept call\n"); 671 hfp_ag_outgoing_call_accepted(); 672 } else { 673 printf("Dialstring invalid: reject call\n"); 674 hfp_ag_outgoing_call_rejected(); 675 } 676 break; 677 678 case HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG: 679 printf("\n** Attach number to voice tag. Sending '1234567\n"); 680 hfp_ag_send_phone_number_for_voice_tag(acl_handle, "1234567"); 681 break; 682 case HFP_SUBEVENT_TRANSMIT_DTMF_CODES: 683 printf("\n** Send DTMF Codes: '%s'\n", hfp_subevent_transmit_dtmf_codes_get_dtmf(event)); 684 hfp_ag_send_dtmf_code_done(acl_handle); 685 break; 686 case HFP_SUBEVENT_CALL_ANSWERED: 687 printf("Call answered by HF\n"); 688 break; 689 default: 690 printf("Event not handled %u\n", event[2]); 691 break; 692 } 693 case HCI_SCO_DATA_PACKET: 694 sco_demo_receive(event, event_size); 695 break; 696 default: 697 break; 698 } 699 } 700 701 static hfp_phone_number_t subscriber_number = { 702 129, "225577" 703 }; 704 705 /* @section Main Application Setup 706 * 707 * @text Listing MainConfiguration shows main application code. 708 * To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it. 709 * The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers. 710 * The stdin_process callback allows for sending commands to the HFP HF. 711 * At the end the Bluetooth stack is started. 712 */ 713 714 /* LISTING_START(MainConfiguration): Setup HFP Audio Gateway */ 715 716 int btstack_main(int argc, const char * argv[]); 717 int btstack_main(int argc, const char * argv[]){ 718 719 sco_demo_init(); 720 721 // register for HCI events 722 hci_event_callback_registration.callback = &packet_handler; 723 hci_add_event_handler(&hci_event_callback_registration); 724 hci_register_sco_packet_handler(&packet_handler); 725 726 gap_discoverable_control(1); 727 728 // L2CAP 729 l2cap_init(); 730 731 uint16_t supported_features = 732 (1<<HFP_AGSF_ESCO_S4) | 733 (1<<HFP_AGSF_HF_INDICATORS) | 734 (1<<HFP_AGSF_CODEC_NEGOTIATION) | 735 (1<<HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) | 736 (1<<HFP_AGSF_ENHANCED_CALL_CONTROL) | 737 (1<<HFP_AGSF_ENHANCED_CALL_STATUS) | 738 (1<<HFP_AGSF_ABILITY_TO_REJECT_A_CALL) | 739 (1<<HFP_AGSF_IN_BAND_RING_TONE) | 740 (1<<HFP_AGSF_VOICE_RECOGNITION_FUNCTION) | 741 (1<<HFP_AGSF_THREE_WAY_CALLING); 742 int wide_band_speech = 1; 743 744 // HFP 745 rfcomm_init(); 746 hfp_ag_init(rfcomm_channel_nr); 747 hfp_ag_init_supported_features(supported_features); 748 hfp_ag_init_codecs(sizeof(codecs), codecs); 749 hfp_ag_init_ag_indicators(ag_indicators_nr, ag_indicators); 750 hfp_ag_init_hf_indicators(hf_indicators_nr, hf_indicators); 751 hfp_ag_init_call_hold_services(call_hold_services_nr, call_hold_services); 752 hfp_ag_set_subcriber_number_information(&subscriber_number, 1); 753 hfp_ag_register_packet_handler(&packet_handler); 754 hci_register_sco_packet_handler(&packet_handler); 755 756 // SDP Server 757 sdp_init(); 758 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 759 hfp_ag_create_sdp_record( hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, wide_band_speech); 760 printf("SDP service record size: %u\n", de_get_len( hfp_service_buffer)); 761 sdp_register_service(hfp_service_buffer); 762 763 #ifdef HAVE_POSIX_STDIN 764 btstack_stdin_setup(stdin_process); 765 #endif 766 // turn on! 767 hci_power_control(HCI_POWER_ON); 768 return 0; 769 } 770 /* LISTING_END */ 771 /* EXAMPLE_END */ 772