1 2 /* 3 * Copyright (C) 2014 BlueKitchen GmbH 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the copyright holders nor the names of 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 4. Any redistribution, use, or modification is done solely for 18 * personal benefit and not for any commercial purpose or for 19 * monetary gain. 20 * 21 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 25 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * Please inquire about commercial licensing options at 35 * [email protected] 36 * 37 */ 38 39 /* 40 * hfp_hs_demo.c 41 */ 42 43 // ***************************************************************************** 44 /* EXAMPLE_START(hfp_hs_demo): HFP Hands-Free (HF) Demo 45 * 46 * @text This HFP Hands-Free example demonstrates how to receive 47 * an output from a remote HFP audio gateway (AG), and, 48 * if HAVE_POSIX_STDIN is defined, how to control the HFP AG. 49 */ 50 // ***************************************************************************** 51 52 53 #include "btstack_config.h" 54 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include "btstack.h" 62 #ifdef HAVE_POSIX_STDIN 63 #include "stdin_support.h" 64 #endif 65 66 uint8_t hfp_service_buffer[150]; 67 const uint8_t rfcomm_channel_nr = 1; 68 const char hfp_hf_service_name[] = "BTstack HFP HF Demo"; 69 70 #ifdef HAVE_POSIX_STDIN 71 static bd_addr_t device_addr = {0xD8,0xBb,0x2C,0xDf,0xF1,0x08}; 72 // prototypes 73 static void show_usage(void); 74 #endif 75 static uint16_t handle = -1; 76 static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC}; 77 static uint16_t indicators[1] = {0x01}; 78 79 char cmd; 80 81 #ifdef HAVE_POSIX_STDIN 82 83 // Testig User Interface 84 static void show_usage(void){ 85 printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console ---\n"); 86 printf("---\n"); 87 88 printf("a - establish SLC connection to device\n"); 89 printf("A - release SLC connection to device\n"); 90 91 printf("b - establish Audio connection\n"); 92 printf("B - release Audio connection\n"); 93 94 printf("c - disable registration status update for all AG indicators\n"); 95 printf("C - enable registration status update for all AG indicators\n"); 96 97 printf("d - query network operator.\n"); 98 printf("D - set HFP AG registration status update for individual indicators (IIA)\n"); 99 100 printf("e - disable reporting of the extended AG error result code\n"); 101 printf("E - enable reporting of the extended AG error result code\n"); 102 103 printf("f - answer incoming call\n"); 104 printf("F - Hangup call\n"); 105 106 printf("g - query network operator name\n"); 107 printf("G - reject incoming call\n"); 108 109 printf("i - dial 1234567\n"); 110 printf("I - dial 7654321\n"); 111 112 printf("j - dial #1\n"); 113 printf("J - dial #99\n"); 114 115 printf("k - deactivate call waiting notification\n"); 116 printf("K - activate call waiting notification\n"); 117 118 printf("l - deactivate calling line notification\n"); 119 printf("L - activate calling line notification\n"); 120 121 printf("m - deactivate echo canceling and noise reduction\n"); 122 printf("M - activate echo canceling and noise reduction\n"); 123 124 printf("n - deactivate voice recognition\n"); 125 printf("N - activate voice recognition\n"); 126 127 printf("o - Set speaker volume to 0 (minimum)\n"); 128 printf("O - Set speaker volume to 9 (default)\n"); 129 printf("p - Set speaker volume to 12 (higher)\n"); 130 printf("P - Set speaker volume to 15 (maximum)\n"); 131 132 printf("q - Set microphone gain to 0 (minimum)\n"); 133 printf("Q - Set microphone gain to 9 (default)\n"); 134 printf("s - Set microphone gain to 12 (higher)\n"); 135 printf("S - Set microphone gain to 15 (maximum)\n"); 136 137 printf("t - terminate connection\n"); 138 139 printf("u - send 'user busy' (TWC 0)\n"); 140 printf("U - end active call and accept other call' (TWC 1)\n"); 141 printf("v - Swap active call and hold/waiting call (TWC 2)\n"); 142 printf("V - Join held call (TWC 3)\n"); 143 printf("w - Connect calls (TWC 4)\n"); 144 printf("W - redial\n"); 145 146 printf("0123456789#*-+ - send DTMF dial tones\n"); 147 148 printf("x - request phone number for voice tag\n"); 149 printf("X - current call status (ECS)\n"); 150 printf("y - release call with index 2 (ECC)\n"); 151 printf("Y - private consulation with call 2(ECC)\n"); 152 153 printf("[ - Query Response and Hold status (RHH ?)\n"); 154 printf("] - Place call in a response and held state(RHH 0)\n"); 155 printf("{ - Accept held call(RHH 1)\n"); 156 printf("} - Reject held call(RHH 2)\n"); 157 158 printf("? - Query Subscriber Number (NUM)\n"); 159 160 printf("! - Update HF indicator with assigned number 1 (HFI)\n"); 161 162 printf("---\n"); 163 printf("Ctrl-c - exit\n"); 164 printf("---\n"); 165 } 166 167 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ 168 read(ds->fd, &cmd, 1); 169 170 if (cmd >= '0' && cmd <= '9'){ 171 printf("DTMF Code: %c\n", cmd); 172 hfp_hf_send_dtmf_code(device_addr, cmd); 173 return; 174 } 175 176 switch (cmd){ 177 case '#': 178 case '-': 179 case '+': 180 case '*': 181 log_info("USER:\'%c\'", cmd); 182 printf("DTMF Code: %c\n", cmd); 183 hfp_hf_send_dtmf_code(device_addr, cmd); 184 break; 185 case 'a': 186 log_info("USER:\'%c\'", cmd); 187 printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 188 hfp_hf_establish_service_level_connection(device_addr); 189 break; 190 case 'A': 191 log_info("USER:\'%c\'", cmd); 192 printf("Release Service level connection.\n"); 193 hfp_hf_release_service_level_connection(device_addr); 194 break; 195 case 'b': 196 log_info("USER:\'%c\'", cmd); 197 printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 198 hfp_hf_establish_audio_connection(device_addr); 199 break; 200 case 'B': 201 log_info("USER:\'%c\'", cmd); 202 printf("Release Audio service level connection.\n"); 203 hfp_hf_release_audio_connection(device_addr); 204 break; 205 case 'C': 206 log_info("USER:\'%c\'", cmd); 207 printf("Enable registration status update for all AG indicators.\n"); 208 hfp_hf_enable_status_update_for_all_ag_indicators(device_addr); 209 case 'c': 210 log_info("USER:\'%c\'", cmd); 211 printf("Disable registration status update for all AG indicators.\n"); 212 hfp_hf_disable_status_update_for_all_ag_indicators(device_addr); 213 break; 214 case 'D': 215 log_info("USER:\'%c\'", cmd); 216 printf("Set HFP AG registration status update for individual indicators (0111111).\n"); 217 hfp_hf_set_status_update_for_individual_ag_indicators(device_addr, 63); 218 break; 219 case 'd': 220 log_info("USER:\'%c\'", cmd); 221 printf("Query network operator.\n"); 222 hfp_hf_query_operator_selection(device_addr); 223 break; 224 case 'E': 225 log_info("USER:\'%c\'", cmd); 226 printf("Enable reporting of the extended AG error result code.\n"); 227 hfp_hf_enable_report_extended_audio_gateway_error_result_code(device_addr); 228 break; 229 case 'e': 230 log_info("USER:\'%c\'", cmd); 231 printf("Disable reporting of the extended AG error result code.\n"); 232 hfp_hf_disable_report_extended_audio_gateway_error_result_code(device_addr); 233 break; 234 case 'f': 235 log_info("USER:\'%c\'", cmd); 236 printf("Answer incoming call.\n"); 237 hfp_hf_answer_incoming_call(device_addr); 238 break; 239 case 'F': 240 log_info("USER:\'%c\'", cmd); 241 printf("Hangup call.\n"); 242 hfp_hf_terminate_call(device_addr); 243 break; 244 case 'G': 245 log_info("USER:\'%c\'", cmd); 246 printf("Reject incoming call.\n"); 247 hfp_hf_reject_incoming_call(device_addr); 248 break; 249 case 'g': 250 log_info("USER:\'%c\'", cmd); 251 printf("Query operator.\n"); 252 hfp_hf_query_operator_selection(device_addr); 253 break; 254 case 't': 255 log_info("USER:\'%c\'", cmd); 256 printf("Terminate HCI connection.\n"); 257 gap_disconnect(handle); 258 break; 259 case 'i': 260 log_info("USER:\'%c\'", cmd); 261 printf("Dial 1234567\n"); 262 hfp_hf_dial_number(device_addr, "1234567"); 263 break; 264 case 'I': 265 log_info("USER:\'%c\'", cmd); 266 printf("Dial 7654321\n"); 267 hfp_hf_dial_number(device_addr, "7654321"); 268 break; 269 case 'j': 270 log_info("USER:\'%c\'", cmd); 271 printf("Dial #1\n"); 272 hfp_hf_dial_memory(device_addr,1); 273 break; 274 case 'J': 275 log_info("USER:\'%c\'", cmd); 276 printf("Dial #99\n"); 277 hfp_hf_dial_memory(device_addr,99); 278 break; 279 case 'k': 280 log_info("USER:\'%c\'", cmd); 281 printf("Deactivate call waiting notification\n"); 282 hfp_hf_deactivate_call_waiting_notification(device_addr); 283 break; 284 case 'K': 285 log_info("USER:\'%c\'", cmd); 286 printf("Activate call waiting notification\n"); 287 hfp_hf_activate_call_waiting_notification(device_addr); 288 break; 289 case 'l': 290 log_info("USER:\'%c\'", cmd); 291 printf("Deactivate calling line notification\n"); 292 hfp_hf_deactivate_calling_line_notification(device_addr); 293 break; 294 case 'L': 295 log_info("USER:\'%c\'", cmd); 296 printf("Activate calling line notification\n"); 297 hfp_hf_activate_calling_line_notification(device_addr); 298 break; 299 case 'm': 300 log_info("USER:\'%c\'", cmd); 301 printf("Deactivate echo canceling and noise reduction\n"); 302 hfp_hf_deactivate_echo_canceling_and_noise_reduction(device_addr); 303 break; 304 case 'M': 305 log_info("USER:\'%c\'", cmd); 306 printf("Activate echo canceling and noise reduction\n"); 307 hfp_hf_activate_echo_canceling_and_noise_reduction(device_addr); 308 break; 309 case 'n': 310 log_info("USER:\'%c\'", cmd); 311 printf("Deactivate voice recognition\n"); 312 hfp_hf_deactivate_voice_recognition_notification(device_addr); 313 break; 314 case 'N': 315 log_info("USER:\'%c\'", cmd); 316 printf("Activate voice recognition\n"); 317 hfp_hf_activate_voice_recognition_notification(device_addr); 318 break; 319 case 'o': 320 log_info("USER:\'%c\'", cmd); 321 printf("Set speaker gain to 0 (minimum)\n"); 322 hfp_hf_set_speaker_gain(device_addr, 0); 323 break; 324 case 'O': 325 log_info("USER:\'%c\'", cmd); 326 printf("Set speaker gain to 9 (default)\n"); 327 hfp_hf_set_speaker_gain(device_addr, 9); 328 break; 329 case 'p': 330 log_info("USER:\'%c\'", cmd); 331 printf("Set speaker gain to 12 (higher)\n"); 332 hfp_hf_set_speaker_gain(device_addr, 12); 333 break; 334 case 'P': 335 log_info("USER:\'%c\'", cmd); 336 printf("Set speaker gain to 15 (maximum)\n"); 337 hfp_hf_set_speaker_gain(device_addr, 15); 338 break; 339 case 'q': 340 log_info("USER:\'%c\'", cmd); 341 printf("Set microphone gain to 0\n"); 342 hfp_hf_set_microphone_gain(device_addr, 0); 343 break; 344 case 'Q': 345 log_info("USER:\'%c\'", cmd); 346 printf("Set microphone gain to 9\n"); 347 hfp_hf_set_microphone_gain(device_addr, 9); 348 break; 349 case 's': 350 log_info("USER:\'%c\'", cmd); 351 printf("Set microphone gain to 12\n"); 352 hfp_hf_set_microphone_gain(device_addr, 12); 353 break; 354 case 'S': 355 log_info("USER:\'%c\'", cmd); 356 printf("Set microphone gain to 15\n"); 357 hfp_hf_set_microphone_gain(device_addr, 15); 358 break; 359 case 'u': 360 log_info("USER:\'%c\'", cmd); 361 printf("Send 'user busy' (Three-Way Call 0)\n"); 362 hfp_hf_user_busy(device_addr); 363 break; 364 case 'U': 365 log_info("USER:\'%c\'", cmd); 366 printf("End active call and accept waiting/held call (Three-Way Call 1)\n"); 367 hfp_hf_end_active_and_accept_other(device_addr); 368 break; 369 case 'v': 370 log_info("USER:\'%c\'", cmd); 371 printf("Swap active call and hold/waiting call (Three-Way Call 2)\n"); 372 hfp_hf_swap_calls(device_addr); 373 break; 374 case 'V': 375 log_info("USER:\'%c\'", cmd); 376 printf("Join hold call (Three-Way Call 3)\n"); 377 hfp_hf_join_held_call(device_addr); 378 break; 379 case 'w': 380 log_info("USER:\'%c\'", cmd); 381 printf("Connect calls (Three-Way Call 4)\n"); 382 hfp_hf_connect_calls(device_addr); 383 break; 384 case 'W': 385 log_info("USER:\'%c\'", cmd); 386 printf("Redial\n"); 387 hfp_hf_redial_last_number(device_addr); 388 break; 389 case 'x': 390 log_info("USER:\'%c\'", cmd); 391 printf("Request phone number for voice tag\n"); 392 hfp_hf_request_phone_number_for_voice_tag(device_addr); 393 break; 394 case 'X': 395 log_info("USER:\'%c\'", cmd); 396 printf("Query current call status\n"); 397 hfp_hf_query_current_call_status(device_addr); 398 break; 399 case 'y': 400 log_info("USER:\'%c\'", cmd); 401 printf("Release call with index 2\n"); 402 hfp_hf_release_call_with_index(device_addr, 2); 403 break; 404 case 'Y': 405 log_info("USER:\'%c\'", cmd); 406 printf("Private consulation with call 2\n"); 407 hfp_hf_private_consultation_with_call(device_addr, 2); 408 break; 409 case '[': 410 log_info("USER:\'%c\'", cmd); 411 printf("Query Response and Hold status (RHH ?)\n"); 412 hfp_hf_rrh_query_status(device_addr); 413 break; 414 case ']': 415 log_info("USER:\'%c\'", cmd); 416 printf("Place call in a response and held state (RHH 0)\n"); 417 hfp_hf_rrh_hold_call(device_addr); 418 break; 419 case '{': 420 log_info("USER:\'%c\'", cmd); 421 printf("Accept held call (RHH 1)\n"); 422 hfp_hf_rrh_accept_held_call(device_addr); 423 break; 424 case '}': 425 log_info("USER:\'%c\'", cmd); 426 printf("Reject held call (RHH 2)\n"); 427 hfp_hf_rrh_reject_held_call(device_addr); 428 break; 429 case '?': 430 log_info("USER:\'%c\'", cmd); 431 printf("Query Subscriber Number\n"); 432 hfp_hf_query_subscriber_number(device_addr); 433 break; 434 case '!': 435 log_info("USER:\'%c\'", cmd); 436 printf("Update HF indicator with assigned number 1 (HFI)\n"); 437 hfp_hf_set_hf_indicator(device_addr, 1, 1); 438 break; 439 440 default: 441 show_usage(); 442 break; 443 } 444 } 445 #endif 446 447 static void packet_handler(uint8_t * event, uint16_t event_size){ 448 if (event[0] != HCI_EVENT_HFP_META) return; 449 450 switch (event[2]) { 451 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 452 handle = hfp_subevent_service_level_connection_established_get_con_handle(event); 453 printf("Service level connection established.\n\n"); 454 break; 455 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 456 printf("Service level connection released.\n\n"); 457 break; 458 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 459 printf("\n** Audio connection established **\n"); 460 break; 461 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 462 printf("\n** Audio connection released **\n"); 463 break; 464 case HFP_SUBEVENT_COMPLETE: 465 switch (cmd){ 466 case 'd': 467 printf("HFP AG registration status update enabled.\n"); 468 break; 469 case 'e': 470 printf("HFP AG registration status update for individual indicators set.\n"); 471 default: 472 break; 473 } 474 break; 475 case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED: 476 printf("AG_INDICATOR_STATUS_CHANGED, AG indicator '%s' (index: %d) to: %d\n", (const char*) &event[5], event[3], event[4]); 477 break; 478 case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED: 479 printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n", event[3], event[4], (char *) &event[5]); 480 break; 481 case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR: 482 if (event[4]) 483 printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : %d\n", event[3]); 484 break; 485 case HFP_SUBEVENT_RING: 486 printf("** Ring **\n"); 487 break; 488 case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG: 489 printf("Phone number for voice tag: %s\n", (const char *) &event[3]); 490 break; 491 case HFP_SUBEVENT_SPEAKER_VOLUME: 492 printf("Speaker volume: %u\n", event[3]); 493 break; 494 case HFP_SUBEVENT_MICROPHONE_VOLUME: 495 printf("Microphone volume: %u\n", event[3]); 496 break; 497 default: 498 printf("event not handled %u\n", event[2]); 499 break; 500 } 501 } 502 503 /* @section Main Application Setup 504 * 505 * @text Listing MainConfiguration shows main application code. 506 * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it. 507 * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers. 508 * The stdin_process callback allows for sending commands to the HFP AG. 509 * At the end the Bluetooth stack is started. 510 */ 511 512 /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */ 513 int btstack_main(int argc, const char * argv[]); 514 int btstack_main(int argc, const char * argv[]){ 515 // HFP AG address is hardcoded, please change it 516 // init L2CAP 517 l2cap_init(); 518 rfcomm_init(); 519 sdp_init(); 520 521 hfp_hf_init(rfcomm_channel_nr); 522 hfp_hf_init_supported_features(438 | (1<<HFP_HFSF_ESCO_S4) | (1<<HFP_HFSF_EC_NR_FUNCTION)); 523 hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators); 524 hfp_hf_init_codecs(sizeof(codecs), codecs); 525 526 hfp_hf_register_packet_handler(packet_handler); 527 528 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 529 hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, 0); 530 printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer)); 531 sdp_register_service(hfp_service_buffer); 532 533 #ifdef HAVE_POSIX_STDIN 534 btstack_stdin_setup(stdin_process); 535 #endif 536 // turn on! 537 hci_power_control(HCI_POWER_ON); 538 return 0; 539 } 540 /* LISTING_END */ 541 /* EXAMPLE_END */ 542