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