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_STDIO 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 60 #include "btstack.h" 61 #include "stdin_support.h" 62 63 uint8_t hfp_service_buffer[150]; 64 const uint8_t rfcomm_channel_nr = 1; 65 const char hfp_hf_service_name[] = "BTstack HFP HF Demo"; 66 67 #ifdef HAVE_STDIO 68 static bd_addr_t device_addr = {0xD8,0xBb,0x2C,0xDf,0xF1,0x08}; 69 // prototypes 70 static void show_usage(); 71 #endif 72 static uint16_t handle = -1; 73 static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC}; 74 static uint16_t indicators[1] = {0x01}; 75 76 char cmd; 77 78 #ifdef HAVE_STDIO 79 80 // Testig User Interface 81 static void show_usage(void){ 82 printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console ---\n"); 83 printf("---\n"); 84 85 printf("a - establish SLC connection to device\n"); 86 printf("A - release SLC connection to device\n"); 87 88 printf("b - establish Audio connection\n"); 89 printf("B - release Audio connection\n"); 90 91 printf("c - disable registration status update for all AG indicators\n"); 92 printf("C - enable registration status update for all AG indicators\n"); 93 94 printf("d - query network operator.\n"); 95 printf("D - set HFP AG registration status update for individual indicators (IIA)\n"); 96 97 printf("e - disable reporting of the extended AG error result code\n"); 98 printf("E - enable reporting of the extended AG error result code\n"); 99 100 printf("f - answer incoming call\n"); 101 printf("F - Hangup call\n"); 102 103 printf("g - query network operator name\n"); 104 printf("G - reject incoming call\n"); 105 106 printf("i - dial 1234567\n"); 107 printf("I - dial 7654321\n"); 108 109 printf("j - dial #1\n"); 110 printf("J - dial #99\n"); 111 112 printf("k - deactivate call waiting notification\n"); 113 printf("K - activate call waiting notification\n"); 114 115 printf("l - deactivate calling line notification\n"); 116 printf("L - activate calling line notification\n"); 117 118 printf("m - deactivate echo canceling and noise reduction\n"); 119 printf("M - activate echo canceling and noise reduction\n"); 120 121 printf("n - deactivate voice recognition\n"); 122 printf("N - activate voice recognition\n"); 123 124 printf("o - Set speaker volume to 0 (minimum)\n"); 125 printf("O - Set speaker volume to 9 (default)\n"); 126 printf("p - Set speaker volume to 12 (higher)\n"); 127 printf("P - Set speaker volume to 15 (maximum)\n"); 128 129 printf("q - Set microphone gain to 0 (minimum)\n"); 130 printf("Q - Set microphone gain to 9 (default)\n"); 131 printf("s - Set microphone gain to 12 (higher)\n"); 132 printf("S - Set microphone gain to 15 (maximum)\n"); 133 134 printf("t - terminate connection\n"); 135 136 printf("u - send 'user busy' (TWC 0)\n"); 137 printf("U - end active call and accept other call' (TWC 1)\n"); 138 printf("v - Swap active call and hold/waiting call (TWC 2)\n"); 139 printf("V - Join held call (TWC 3)\n"); 140 printf("w - Connect calls (TWC 4)\n"); 141 printf("W - redial\n"); 142 143 printf("0123456789#*-+ - send DTMF dial tones\n"); 144 145 printf("x - request phone number for voice tag\n"); 146 printf("X - current call status (ECS)\n"); 147 printf("y - release call with index 2 (ECC)\n"); 148 printf("Y - private consulation with call 2(ECC)\n"); 149 150 printf("[ - Query Response and Hold status (RHH ?)\n"); 151 printf("] - Place call in a response and held state(RHH 0)\n"); 152 printf("{ - Accept held call(RHH 1)\n"); 153 printf("} - Reject held call(RHH 2)\n"); 154 155 printf("? - Query Subscriber Number (NUM)\n"); 156 157 printf("! - Update HF indicator with assigned number 1 (HFI)\n"); 158 159 printf("---\n"); 160 printf("Ctrl-c - exit\n"); 161 printf("---\n"); 162 } 163 164 static int stdin_process(struct data_source *ds){ 165 read(ds->fd, &cmd, 1); 166 167 if (cmd >= '0' && cmd <= '9'){ 168 printf("DTMF Code: %c\n", cmd); 169 hfp_hf_send_dtmf_code(device_addr, cmd); 170 return 0; 171 } 172 173 switch (cmd){ 174 case '#': 175 case '-': 176 case '+': 177 case '*': 178 log_info("USER:\'%c\'", cmd); 179 printf("DTMF Code: %c\n", cmd); 180 hfp_hf_send_dtmf_code(device_addr, cmd); 181 break; 182 case 'a': 183 log_info("USER:\'%c\'", cmd); 184 printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 185 hfp_hf_establish_service_level_connection(device_addr); 186 break; 187 case 'A': 188 log_info("USER:\'%c\'", cmd); 189 printf("Release Service level connection.\n"); 190 hfp_hf_release_service_level_connection(device_addr); 191 break; 192 case 'b': 193 log_info("USER:\'%c\'", cmd); 194 printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 195 hfp_hf_establish_audio_connection(device_addr); 196 break; 197 case 'B': 198 log_info("USER:\'%c\'", cmd); 199 printf("Release Audio service level connection.\n"); 200 hfp_hf_release_audio_connection(device_addr); 201 break; 202 case 'C': 203 log_info("USER:\'%c\'", cmd); 204 printf("Enable registration status update for all AG indicators.\n"); 205 hfp_hf_enable_status_update_for_all_ag_indicators(device_addr); 206 case 'c': 207 log_info("USER:\'%c\'", cmd); 208 printf("Disable registration status update for all AG indicators.\n"); 209 hfp_hf_disable_status_update_for_all_ag_indicators(device_addr); 210 break; 211 case 'D': 212 log_info("USER:\'%c\'", cmd); 213 printf("Set HFP AG registration status update for individual indicators (0111111).\n"); 214 hfp_hf_set_status_update_for_individual_ag_indicators(device_addr, 63); 215 break; 216 case 'd': 217 log_info("USER:\'%c\'", cmd); 218 printf("Query network operator.\n"); 219 hfp_hf_query_operator_selection(device_addr); 220 break; 221 case 'E': 222 log_info("USER:\'%c\'", cmd); 223 printf("Enable reporting of the extended AG error result code.\n"); 224 hfp_hf_enable_report_extended_audio_gateway_error_result_code(device_addr); 225 break; 226 case 'e': 227 log_info("USER:\'%c\'", cmd); 228 printf("Disable reporting of the extended AG error result code.\n"); 229 hfp_hf_disable_report_extended_audio_gateway_error_result_code(device_addr); 230 break; 231 case 'f': 232 log_info("USER:\'%c\'", cmd); 233 printf("Answer incoming call.\n"); 234 hfp_hf_answer_incoming_call(device_addr); 235 break; 236 case 'F': 237 log_info("USER:\'%c\'", cmd); 238 printf("Hangup call.\n"); 239 hfp_hf_terminate_call(device_addr); 240 break; 241 case 'G': 242 log_info("USER:\'%c\'", cmd); 243 printf("Reject incoming call.\n"); 244 hfp_hf_reject_incoming_call(device_addr); 245 break; 246 case 'g': 247 log_info("USER:\'%c\'", cmd); 248 printf("Query operator.\n"); 249 hfp_hf_query_operator_selection(device_addr); 250 break; 251 case 't': 252 log_info("USER:\'%c\'", cmd); 253 printf("Terminate HCI connection.\n"); 254 gap_disconnect(handle); 255 break; 256 case 'i': 257 log_info("USER:\'%c\'", cmd); 258 printf("Dial 1234567\n"); 259 hfp_hf_dial_number(device_addr, "1234567"); 260 break; 261 case 'I': 262 log_info("USER:\'%c\'", cmd); 263 printf("Dial 7654321\n"); 264 hfp_hf_dial_number(device_addr, "7654321"); 265 break; 266 case 'j': 267 log_info("USER:\'%c\'", cmd); 268 printf("Dial #1\n"); 269 hfp_hf_dial_memory(device_addr,1); 270 break; 271 case 'J': 272 log_info("USER:\'%c\'", cmd); 273 printf("Dial #99\n"); 274 hfp_hf_dial_memory(device_addr,99); 275 break; 276 case 'k': 277 log_info("USER:\'%c\'", cmd); 278 printf("Deactivate call waiting notification\n"); 279 hfp_hf_deactivate_call_waiting_notification(device_addr); 280 break; 281 case 'K': 282 log_info("USER:\'%c\'", cmd); 283 printf("Activate call waiting notification\n"); 284 hfp_hf_activate_call_waiting_notification(device_addr); 285 break; 286 case 'l': 287 log_info("USER:\'%c\'", cmd); 288 printf("Deactivate calling line notification\n"); 289 hfp_hf_deactivate_calling_line_notification(device_addr); 290 break; 291 case 'L': 292 log_info("USER:\'%c\'", cmd); 293 printf("Activate calling line notification\n"); 294 hfp_hf_activate_calling_line_notification(device_addr); 295 break; 296 case 'm': 297 log_info("USER:\'%c\'", cmd); 298 printf("Deactivate echo canceling and noise reduction\n"); 299 hfp_hf_deactivate_echo_canceling_and_noise_reduction(device_addr); 300 break; 301 case 'M': 302 log_info("USER:\'%c\'", cmd); 303 printf("Activate echo canceling and noise reduction\n"); 304 hfp_hf_activate_echo_canceling_and_noise_reduction(device_addr); 305 break; 306 case 'n': 307 log_info("USER:\'%c\'", cmd); 308 printf("Deactivate voice recognition\n"); 309 hfp_hf_deactivate_voice_recognition_notification(device_addr); 310 break; 311 case 'N': 312 log_info("USER:\'%c\'", cmd); 313 printf("Activate voice recognition\n"); 314 hfp_hf_activate_voice_recognition_notification(device_addr); 315 break; 316 case 'o': 317 log_info("USER:\'%c\'", cmd); 318 printf("Set speaker gain to 0 (minimum)\n"); 319 hfp_hf_set_speaker_gain(device_addr, 0); 320 break; 321 case 'O': 322 log_info("USER:\'%c\'", cmd); 323 printf("Set speaker gain to 9 (default)\n"); 324 hfp_hf_set_speaker_gain(device_addr, 9); 325 break; 326 case 'p': 327 log_info("USER:\'%c\'", cmd); 328 printf("Set speaker gain to 12 (higher)\n"); 329 hfp_hf_set_speaker_gain(device_addr, 12); 330 break; 331 case 'P': 332 log_info("USER:\'%c\'", cmd); 333 printf("Set speaker gain to 15 (maximum)\n"); 334 hfp_hf_set_speaker_gain(device_addr, 15); 335 break; 336 case 'q': 337 log_info("USER:\'%c\'", cmd); 338 printf("Set microphone gain to 0\n"); 339 hfp_hf_set_microphone_gain(device_addr, 0); 340 break; 341 case 'Q': 342 log_info("USER:\'%c\'", cmd); 343 printf("Set microphone gain to 9\n"); 344 hfp_hf_set_microphone_gain(device_addr, 9); 345 break; 346 case 's': 347 log_info("USER:\'%c\'", cmd); 348 printf("Set microphone gain to 12\n"); 349 hfp_hf_set_microphone_gain(device_addr, 12); 350 break; 351 case 'S': 352 log_info("USER:\'%c\'", cmd); 353 printf("Set microphone gain to 15\n"); 354 hfp_hf_set_microphone_gain(device_addr, 15); 355 break; 356 case 'u': 357 log_info("USER:\'%c\'", cmd); 358 printf("Send 'user busy' (Three-Way Call 0)\n"); 359 hfp_hf_user_busy(device_addr); 360 break; 361 case 'U': 362 log_info("USER:\'%c\'", cmd); 363 printf("End active call and accept waiting/held call (Three-Way Call 1)\n"); 364 hfp_hf_end_active_and_accept_other(device_addr); 365 break; 366 case 'v': 367 log_info("USER:\'%c\'", cmd); 368 printf("Swap active call and hold/waiting call (Three-Way Call 2)\n"); 369 hfp_hf_swap_calls(device_addr); 370 break; 371 case 'V': 372 log_info("USER:\'%c\'", cmd); 373 printf("Join hold call (Three-Way Call 3)\n"); 374 hfp_hf_join_held_call(device_addr); 375 break; 376 case 'w': 377 log_info("USER:\'%c\'", cmd); 378 printf("Connect calls (Three-Way Call 4)\n"); 379 hfp_hf_connect_calls(device_addr); 380 break; 381 case 'W': 382 log_info("USER:\'%c\'", cmd); 383 printf("Redial\n"); 384 hfp_hf_redial_last_number(device_addr); 385 break; 386 case 'x': 387 log_info("USER:\'%c\'", cmd); 388 printf("Request phone number for voice tag\n"); 389 hfp_hf_request_phone_number_for_voice_tag(device_addr); 390 break; 391 case 'X': 392 log_info("USER:\'%c\'", cmd); 393 printf("Query current call status\n"); 394 hfp_hf_query_current_call_status(device_addr); 395 break; 396 case 'y': 397 log_info("USER:\'%c\'", cmd); 398 printf("Release call with index 2\n"); 399 hfp_hf_release_call_with_index(device_addr, 2); 400 break; 401 case 'Y': 402 log_info("USER:\'%c\'", cmd); 403 printf("Private consulation with call 2\n"); 404 hfp_hf_private_consultation_with_call(device_addr, 2); 405 break; 406 case '[': 407 log_info("USER:\'%c\'", cmd); 408 printf("Query Response and Hold status (RHH ?)\n"); 409 hfp_hf_rrh_query_status(device_addr); 410 break; 411 case ']': 412 log_info("USER:\'%c\'", cmd); 413 printf("Place call in a response and held state (RHH 0)\n"); 414 hfp_hf_rrh_hold_call(device_addr); 415 break; 416 case '{': 417 log_info("USER:\'%c\'", cmd); 418 printf("Accept held call (RHH 1)\n"); 419 hfp_hf_rrh_accept_held_call(device_addr); 420 break; 421 case '}': 422 log_info("USER:\'%c\'", cmd); 423 printf("Reject held call (RHH 2)\n"); 424 hfp_hf_rrh_reject_held_call(device_addr); 425 break; 426 case '?': 427 log_info("USER:\'%c\'", cmd); 428 printf("Query Subscriber Number\n"); 429 hfp_hf_query_subscriber_number(device_addr); 430 break; 431 case '!': 432 log_info("USER:\'%c\'", cmd); 433 printf("Update HF indicator with assigned number 1 (HFI)\n"); 434 hfp_hf_set_hf_indicator(device_addr, 1, 1); 435 break; 436 437 default: 438 show_usage(); 439 break; 440 } 441 442 return 0; 443 } 444 #endif 445 446 static void packet_handler(uint8_t * event, uint16_t event_size){ 447 if (event[0] == RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE){ 448 handle = little_endian_read_16(event, 9); 449 printf("RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE received for handle 0x%04x\n", handle); 450 return; 451 } 452 if (event[0] != HCI_EVENT_HFP_META) return; 453 454 switch (event[2]) { 455 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 456 printf("Service level connection established.\n\n"); 457 break; 458 case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 459 printf("Service level connection released.\n\n"); 460 break; 461 case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 462 printf("\n** Audio connection established **\n"); 463 break; 464 case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 465 printf("\n** Audio connection released **\n"); 466 break; 467 case HFP_SUBEVENT_COMPLETE: 468 switch (cmd){ 469 case 'd': 470 printf("HFP AG registration status update enabled.\n"); 471 break; 472 case 'e': 473 printf("HFP AG registration status update for individual indicators set.\n"); 474 default: 475 break; 476 } 477 break; 478 case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED: 479 printf("AG_INDICATOR_STATUS_CHANGED, AG indicator '%s' (index: %d) to: %d\n", (const char*) &event[5], event[3], event[4]); 480 break; 481 case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED: 482 printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n", event[3], event[4], (char *) &event[5]); 483 break; 484 case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR: 485 if (event[4]) 486 printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : %d\n", event[3]); 487 break; 488 case HFP_SUBEVENT_RING: 489 printf("** Ring **\n"); 490 break; 491 case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG: 492 printf("Phone number for voice tag: %s\n", (const char *) &event[3]); 493 break; 494 case HFP_SUBEVENT_SPEAKER_VOLUME: 495 printf("Speaker volume: %u\n", event[3]); 496 break; 497 case HFP_SUBEVENT_MICROPHONE_VOLUME: 498 printf("Microphone volume: %u\n", event[3]); 499 break; 500 default: 501 printf("event not handled %u\n", event[2]); 502 break; 503 } 504 } 505 506 /* @section Main Application Setup 507 * 508 * @text Listing MainConfiguration shows main application code. 509 * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it. 510 * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers. 511 * The stdin_process callback allows for sending commands to the HFP AG. 512 * At the end the Bluetooth stack is started. 513 */ 514 515 /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */ 516 int btstack_main(int argc, const char * argv[]); 517 int btstack_main(int argc, const char * argv[]){ 518 // HFP AG address is hardcoded, please change it 519 // init L2CAP 520 l2cap_init(); 521 rfcomm_init(); 522 sdp_init(); 523 524 hfp_hf_init(rfcomm_channel_nr); 525 hfp_hf_init_supported_features(438 | (1<<HFP_HFSF_ESCO_S4) | (1<<HFP_HFSF_EC_NR_FUNCTION)); 526 hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators); 527 hfp_hf_init_codecs(sizeof(codecs), codecs); 528 529 hfp_hf_register_packet_handler(packet_handler); 530 531 memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 532 hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, 0); 533 printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer)); 534 sdp_register_service(hfp_service_buffer); 535 536 #ifdef HAVE_STDIO 537 btstack_stdin_setup(stdin_process); 538 #endif 539 // turn on! 540 hci_power_control(HCI_POWER_ON); 541 return 0; 542 } 543 /* LISTING_END */ 544