1fffdd288SMatthias Ringwald 2fffdd288SMatthias Ringwald /* 3fffdd288SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 4fffdd288SMatthias Ringwald * 5fffdd288SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 6fffdd288SMatthias Ringwald * modification, are permitted provided that the following conditions 7fffdd288SMatthias Ringwald * are met: 8fffdd288SMatthias Ringwald * 9fffdd288SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 10fffdd288SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 11fffdd288SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 12fffdd288SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 13fffdd288SMatthias Ringwald * documentation and/or other materials provided with the distribution. 14fffdd288SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 15fffdd288SMatthias Ringwald * contributors may be used to endorse or promote products derived 16fffdd288SMatthias Ringwald * from this software without specific prior written permission. 17fffdd288SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 18fffdd288SMatthias Ringwald * personal benefit and not for any commercial purpose or for 19fffdd288SMatthias Ringwald * monetary gain. 20fffdd288SMatthias Ringwald * 21fffdd288SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 22fffdd288SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23fffdd288SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24fffdd288SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 25fffdd288SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26fffdd288SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27fffdd288SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28fffdd288SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29fffdd288SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30fffdd288SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 31fffdd288SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32fffdd288SMatthias Ringwald * SUCH DAMAGE. 33fffdd288SMatthias Ringwald * 34fffdd288SMatthias Ringwald * Please inquire about commercial licensing options at 35fffdd288SMatthias Ringwald * [email protected] 36fffdd288SMatthias Ringwald * 37fffdd288SMatthias Ringwald */ 38fffdd288SMatthias Ringwald 39fffdd288SMatthias Ringwald /* 40fffdd288SMatthias Ringwald * hfp_hs_demo.c 41fffdd288SMatthias Ringwald */ 42fffdd288SMatthias Ringwald 43fffdd288SMatthias Ringwald // ***************************************************************************** 44fffdd288SMatthias Ringwald /* EXAMPLE_START(hfp_hs_demo): HFP Hands-Free (HF) Demo 45fffdd288SMatthias Ringwald * 46fffdd288SMatthias Ringwald * @text This HFP Hands-Free example demonstrates how to receive 47fffdd288SMatthias Ringwald * an output from a remote HFP audio gateway (AG), and, 48d0755cd6SMatthias Ringwald * if HAVE_POSIX_STDIN is defined, how to control the HFP AG. 49fffdd288SMatthias Ringwald */ 50fffdd288SMatthias Ringwald // ***************************************************************************** 51fffdd288SMatthias Ringwald 52fffdd288SMatthias Ringwald 53fffdd288SMatthias Ringwald #include "btstack_config.h" 54fffdd288SMatthias Ringwald 55fffdd288SMatthias Ringwald #include <stdint.h> 56fffdd288SMatthias Ringwald #include <stdio.h> 57fffdd288SMatthias Ringwald #include <stdlib.h> 58fffdd288SMatthias Ringwald #include <string.h> 59fffdd288SMatthias Ringwald #include "btstack.h" 60fcb08cdbSMilanka Ringwald 61185c8cd4SMatthias Ringwald #include "sco_demo_util.h" 629491a7bfSMatthias Ringwald 63d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN 649491a7bfSMatthias Ringwald #include <unistd.h> 65fffdd288SMatthias Ringwald #include "stdin_support.h" 664af4141bSMatthias Ringwald #endif 67fffdd288SMatthias Ringwald 68fffdd288SMatthias Ringwald uint8_t hfp_service_buffer[150]; 69fffdd288SMatthias Ringwald const uint8_t rfcomm_channel_nr = 1; 70fffdd288SMatthias Ringwald const char hfp_hf_service_name[] = "BTstack HFP HF Demo"; 719491a7bfSMatthias Ringwald static bd_addr_t device_addr = {0x80,0xbe,0x05,0xd5,0x28,0x48}; 72fffdd288SMatthias Ringwald 73d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN 74185c8cd4SMatthias Ringwald // 80:BE:05:D5:28:48 75fffdd288SMatthias Ringwald // prototypes 7635833313SMatthias Ringwald static void show_usage(void); 77fffdd288SMatthias Ringwald #endif 78c8626498SMilanka Ringwald static hci_con_handle_t acl_handle = -1; 79185c8cd4SMatthias Ringwald static hci_con_handle_t sco_handle; 80fffdd288SMatthias Ringwald static uint8_t codecs[] = {HFP_CODEC_CVSD, HFP_CODEC_MSBC}; 81fffdd288SMatthias Ringwald static uint16_t indicators[1] = {0x01}; 8208d1a604SMilanka Ringwald static uint8_t negotiated_codec = HFP_CODEC_CVSD; 83186bfbecSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 84fffdd288SMatthias Ringwald char cmd; 85fffdd288SMatthias Ringwald 86f9f7adeeSMatthias Ringwald static void dump_supported_codecs(void){ 87f9f7adeeSMatthias Ringwald int i; 88f9f7adeeSMatthias Ringwald int mSBC_skipped = 0; 89f9f7adeeSMatthias Ringwald printf("Supported codecs: "); 90f9f7adeeSMatthias Ringwald for (i = 0; i < sizeof(codecs); i++){ 91f9f7adeeSMatthias Ringwald switch(codecs[i]){ 92f9f7adeeSMatthias Ringwald case HFP_CODEC_CVSD: 93f9f7adeeSMatthias Ringwald printf("CVSD"); 94f9f7adeeSMatthias Ringwald break; 95f9f7adeeSMatthias Ringwald case HFP_CODEC_MSBC: 96f9f7adeeSMatthias Ringwald if (hci_extended_sco_link_supported()){ 972308e108SMilanka Ringwald printf(", mSBC"); 98f9f7adeeSMatthias Ringwald } else { 99f9f7adeeSMatthias Ringwald mSBC_skipped = 1; 100f9f7adeeSMatthias Ringwald } 101f9f7adeeSMatthias Ringwald break; 102f9f7adeeSMatthias Ringwald } 103f9f7adeeSMatthias Ringwald } 104f9f7adeeSMatthias Ringwald printf("\n"); 105f9f7adeeSMatthias Ringwald if (mSBC_skipped){ 106f9f7adeeSMatthias Ringwald printf("mSBC codec disabled because eSCO not supported by local controller.\n"); 107f9f7adeeSMatthias Ringwald } 108f9f7adeeSMatthias Ringwald } 10908d1a604SMilanka Ringwald 110d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN 111fffdd288SMatthias Ringwald 112fffdd288SMatthias Ringwald // Testig User Interface 113fffdd288SMatthias Ringwald static void show_usage(void){ 114fcb08cdbSMilanka Ringwald bd_addr_t iut_address; 115fcb08cdbSMilanka Ringwald gap_local_bd_addr(iut_address); 116fcb08cdbSMilanka Ringwald 117fcb08cdbSMilanka Ringwald printf("\n--- Bluetooth HFP Hands-Free (HF) unit Test Console %s ---\n", bd_addr_to_str(iut_address)); 1188a7d93dcSMatthias Ringwald printf("\n"); 1198a7d93dcSMatthias Ringwald printf("a - establish SLC to %s | ", bd_addr_to_str(device_addr)); 120fffdd288SMatthias Ringwald printf("A - release SLC connection to device\n"); 1218a7d93dcSMatthias Ringwald printf("b - establish Audio connection | B - release Audio connection\n"); 1228a7d93dcSMatthias Ringwald printf("d - query network operator | D - Enable HFP AG registration status update via bitmap(IIA)\n"); 1238a7d93dcSMatthias Ringwald printf("f - answer incoming call | F - Hangup call\n"); 1248a7d93dcSMatthias Ringwald printf("g - query network operator name | G - reject incoming call\n"); 1258a7d93dcSMatthias Ringwald printf("i - dial 1234567 | I - dial 7654321\n"); 1268a7d93dcSMatthias Ringwald printf("j - dial #1 | J - dial #99\n"); 1278a7d93dcSMatthias Ringwald printf("o - Set speaker volume to 0 (minimum) | O - Set speaker volume to 9 (default)\n"); 1288a7d93dcSMatthias Ringwald printf("p - Set speaker volume to 12 (higher) | P - Set speaker volume to 15 (maximum)\n"); 1298a7d93dcSMatthias Ringwald printf("q - Set microphone gain to 0 (minimum) | Q - Set microphone gain to 9 (default)\n"); 1308a7d93dcSMatthias Ringwald printf("s - Set microphone gain to 12 (higher) | S - Set microphone gain to 15 (maximum)\n"); 131fffdd288SMatthias Ringwald printf("t - terminate connection\n"); 132fffdd288SMatthias Ringwald printf("u - send 'user busy' (TWC 0)\n"); 133fffdd288SMatthias Ringwald printf("U - end active call and accept other call' (TWC 1)\n"); 1348a7d93dcSMatthias Ringwald printf("v - Swap active call call (TWC 2) | V - Join held call (TWC 3)\n"); 1358a7d93dcSMatthias Ringwald printf("w - Connect calls (TWC 4) | W - redial\n"); 1368a7d93dcSMatthias Ringwald printf("c/C - disable/enable registration status update for all AG indicators\n"); 1378a7d93dcSMatthias Ringwald printf("e/E - disable/enable reporting of the extended AG error result code\n"); 1388a7d93dcSMatthias Ringwald printf("k/K - deactivate/activate call waiting notification\n"); 1398a7d93dcSMatthias Ringwald printf("l/L - deactivate/activate calling line notification\n"); 1408a7d93dcSMatthias Ringwald printf("m/M - deactivate/activate echo canceling and noise reduction\n"); 1418a7d93dcSMatthias Ringwald printf("n/N - deactivate/activate voice recognition\n"); 142fffdd288SMatthias Ringwald printf("0123456789#*-+ - send DTMF dial tones\n"); 1438a7d93dcSMatthias Ringwald printf("x - request phone number for voice tag | X - current call status (ECS)\n"); 1448a7d93dcSMatthias Ringwald printf("y - release call with index 2 (ECC) | Y - private consulation with call 2(ECC)\n"); 1458a7d93dcSMatthias Ringwald printf("[ - Query Response and Hold status (RHH ?) | ] - Place call in a response and held state(RHH 0)\n"); 1468a7d93dcSMatthias Ringwald printf("{ - Accept held call(RHH 1) | } - Reject held call(RHH 2)\n"); 147fffdd288SMatthias Ringwald printf("? - Query Subscriber Number (NUM)\n"); 148fffdd288SMatthias Ringwald printf("! - Update HF indicator with assigned number 1 (HFI)\n"); 149fffdd288SMatthias Ringwald printf("---\n"); 150fffdd288SMatthias Ringwald printf("Ctrl-c - exit\n"); 151fffdd288SMatthias Ringwald printf("---\n"); 152fffdd288SMatthias Ringwald } 153fffdd288SMatthias Ringwald 1544af4141bSMatthias Ringwald static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ 155*a1dc4edbSMatthias Ringwald 156*a1dc4edbSMatthias Ringwald cmd = btstack_stdin_read(); 157fffdd288SMatthias Ringwald 158fffdd288SMatthias Ringwald if (cmd >= '0' && cmd <= '9'){ 159fffdd288SMatthias Ringwald printf("DTMF Code: %c\n", cmd); 160c8626498SMilanka Ringwald hfp_hf_send_dtmf_code(acl_handle, cmd); 1614af4141bSMatthias Ringwald return; 162fffdd288SMatthias Ringwald } 163fffdd288SMatthias Ringwald 164fffdd288SMatthias Ringwald switch (cmd){ 165fffdd288SMatthias Ringwald case '#': 166fffdd288SMatthias Ringwald case '-': 167fffdd288SMatthias Ringwald case '+': 168fffdd288SMatthias Ringwald case '*': 169fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 170fffdd288SMatthias Ringwald printf("DTMF Code: %c\n", cmd); 171c8626498SMilanka Ringwald hfp_hf_send_dtmf_code(acl_handle, cmd); 172fffdd288SMatthias Ringwald break; 173fffdd288SMatthias Ringwald case 'a': 174fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 175fffdd288SMatthias Ringwald printf("Establish Service level connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 176fffdd288SMatthias Ringwald hfp_hf_establish_service_level_connection(device_addr); 177fffdd288SMatthias Ringwald break; 178fffdd288SMatthias Ringwald case 'A': 179fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 180fffdd288SMatthias Ringwald printf("Release Service level connection.\n"); 181c8626498SMilanka Ringwald hfp_hf_release_service_level_connection(acl_handle); 182fffdd288SMatthias Ringwald break; 183fffdd288SMatthias Ringwald case 'b': 184fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 185fffdd288SMatthias Ringwald printf("Establish Audio connection to device with Bluetooth address %s...\n", bd_addr_to_str(device_addr)); 186c8626498SMilanka Ringwald hfp_hf_establish_audio_connection(acl_handle); 187fffdd288SMatthias Ringwald break; 188fffdd288SMatthias Ringwald case 'B': 189fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 190fffdd288SMatthias Ringwald printf("Release Audio service level connection.\n"); 191c8626498SMilanka Ringwald hfp_hf_release_audio_connection(acl_handle); 192fffdd288SMatthias Ringwald break; 193fffdd288SMatthias Ringwald case 'C': 194fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 195fffdd288SMatthias Ringwald printf("Enable registration status update for all AG indicators.\n"); 196c8626498SMilanka Ringwald hfp_hf_enable_status_update_for_all_ag_indicators(acl_handle); 197fffdd288SMatthias Ringwald case 'c': 198fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 199fffdd288SMatthias Ringwald printf("Disable registration status update for all AG indicators.\n"); 200c8626498SMilanka Ringwald hfp_hf_disable_status_update_for_all_ag_indicators(acl_handle); 201fffdd288SMatthias Ringwald break; 202fffdd288SMatthias Ringwald case 'D': 203fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 204fffdd288SMatthias Ringwald printf("Set HFP AG registration status update for individual indicators (0111111).\n"); 205c8626498SMilanka Ringwald hfp_hf_set_status_update_for_individual_ag_indicators(acl_handle, 63); 206fffdd288SMatthias Ringwald break; 207fffdd288SMatthias Ringwald case 'd': 208fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 209fffdd288SMatthias Ringwald printf("Query network operator.\n"); 210c8626498SMilanka Ringwald hfp_hf_query_operator_selection(acl_handle); 211fffdd288SMatthias Ringwald break; 212fffdd288SMatthias Ringwald case 'E': 213fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 214fffdd288SMatthias Ringwald printf("Enable reporting of the extended AG error result code.\n"); 215c8626498SMilanka Ringwald hfp_hf_enable_report_extended_audio_gateway_error_result_code(acl_handle); 216fffdd288SMatthias Ringwald break; 217fffdd288SMatthias Ringwald case 'e': 218fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 219fffdd288SMatthias Ringwald printf("Disable reporting of the extended AG error result code.\n"); 220c8626498SMilanka Ringwald hfp_hf_disable_report_extended_audio_gateway_error_result_code(acl_handle); 221fffdd288SMatthias Ringwald break; 222fffdd288SMatthias Ringwald case 'f': 223fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 224fffdd288SMatthias Ringwald printf("Answer incoming call.\n"); 225c8626498SMilanka Ringwald hfp_hf_answer_incoming_call(acl_handle); 226fffdd288SMatthias Ringwald break; 227fffdd288SMatthias Ringwald case 'F': 228fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 229fffdd288SMatthias Ringwald printf("Hangup call.\n"); 230c8626498SMilanka Ringwald hfp_hf_terminate_call(acl_handle); 231fffdd288SMatthias Ringwald break; 232fffdd288SMatthias Ringwald case 'G': 233fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 234fffdd288SMatthias Ringwald printf("Reject incoming call.\n"); 235c8626498SMilanka Ringwald hfp_hf_reject_incoming_call(acl_handle); 236fffdd288SMatthias Ringwald break; 237fffdd288SMatthias Ringwald case 'g': 238fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 239fffdd288SMatthias Ringwald printf("Query operator.\n"); 240c8626498SMilanka Ringwald hfp_hf_query_operator_selection(acl_handle); 241fffdd288SMatthias Ringwald break; 242fffdd288SMatthias Ringwald case 't': 243fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 244fffdd288SMatthias Ringwald printf("Terminate HCI connection.\n"); 245c8626498SMilanka Ringwald gap_disconnect(acl_handle); 246fffdd288SMatthias Ringwald break; 247fffdd288SMatthias Ringwald case 'i': 248fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 249fffdd288SMatthias Ringwald printf("Dial 1234567\n"); 250c8626498SMilanka Ringwald hfp_hf_dial_number(acl_handle, "1234567"); 251fffdd288SMatthias Ringwald break; 252fffdd288SMatthias Ringwald case 'I': 253fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 254fffdd288SMatthias Ringwald printf("Dial 7654321\n"); 255c8626498SMilanka Ringwald hfp_hf_dial_number(acl_handle, "7654321"); 256fffdd288SMatthias Ringwald break; 257fffdd288SMatthias Ringwald case 'j': 258fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 259fffdd288SMatthias Ringwald printf("Dial #1\n"); 260c8626498SMilanka Ringwald hfp_hf_dial_memory(acl_handle,1); 261fffdd288SMatthias Ringwald break; 262fffdd288SMatthias Ringwald case 'J': 263fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 264fffdd288SMatthias Ringwald printf("Dial #99\n"); 265c8626498SMilanka Ringwald hfp_hf_dial_memory(acl_handle,99); 266fffdd288SMatthias Ringwald break; 267fffdd288SMatthias Ringwald case 'k': 268fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 269fffdd288SMatthias Ringwald printf("Deactivate call waiting notification\n"); 270c8626498SMilanka Ringwald hfp_hf_deactivate_call_waiting_notification(acl_handle); 271fffdd288SMatthias Ringwald break; 272fffdd288SMatthias Ringwald case 'K': 273fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 274fffdd288SMatthias Ringwald printf("Activate call waiting notification\n"); 275c8626498SMilanka Ringwald hfp_hf_activate_call_waiting_notification(acl_handle); 276fffdd288SMatthias Ringwald break; 277fffdd288SMatthias Ringwald case 'l': 278fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 279fffdd288SMatthias Ringwald printf("Deactivate calling line notification\n"); 280c8626498SMilanka Ringwald hfp_hf_deactivate_calling_line_notification(acl_handle); 281fffdd288SMatthias Ringwald break; 282fffdd288SMatthias Ringwald case 'L': 283fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 284fffdd288SMatthias Ringwald printf("Activate calling line notification\n"); 285c8626498SMilanka Ringwald hfp_hf_activate_calling_line_notification(acl_handle); 286fffdd288SMatthias Ringwald break; 287fffdd288SMatthias Ringwald case 'm': 288fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 289fffdd288SMatthias Ringwald printf("Deactivate echo canceling and noise reduction\n"); 290c8626498SMilanka Ringwald hfp_hf_deactivate_echo_canceling_and_noise_reduction(acl_handle); 291fffdd288SMatthias Ringwald break; 292fffdd288SMatthias Ringwald case 'M': 293fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 294fffdd288SMatthias Ringwald printf("Activate echo canceling and noise reduction\n"); 295c8626498SMilanka Ringwald hfp_hf_activate_echo_canceling_and_noise_reduction(acl_handle); 296fffdd288SMatthias Ringwald break; 297fffdd288SMatthias Ringwald case 'n': 298fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 299fffdd288SMatthias Ringwald printf("Deactivate voice recognition\n"); 300c8626498SMilanka Ringwald hfp_hf_deactivate_voice_recognition_notification(acl_handle); 301fffdd288SMatthias Ringwald break; 302fffdd288SMatthias Ringwald case 'N': 303fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 30408d1a604SMilanka Ringwald printf("Activate voice recognition %s\n", bd_addr_to_str(device_addr)); 305c8626498SMilanka Ringwald hfp_hf_activate_voice_recognition_notification(acl_handle); 306fffdd288SMatthias Ringwald break; 307fffdd288SMatthias Ringwald case 'o': 308fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 309fffdd288SMatthias Ringwald printf("Set speaker gain to 0 (minimum)\n"); 310c8626498SMilanka Ringwald hfp_hf_set_speaker_gain(acl_handle, 0); 311fffdd288SMatthias Ringwald break; 312fffdd288SMatthias Ringwald case 'O': 313fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 314fffdd288SMatthias Ringwald printf("Set speaker gain to 9 (default)\n"); 315c8626498SMilanka Ringwald hfp_hf_set_speaker_gain(acl_handle, 9); 316fffdd288SMatthias Ringwald break; 317fffdd288SMatthias Ringwald case 'p': 318fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 319fffdd288SMatthias Ringwald printf("Set speaker gain to 12 (higher)\n"); 320c8626498SMilanka Ringwald hfp_hf_set_speaker_gain(acl_handle, 12); 321fffdd288SMatthias Ringwald break; 322fffdd288SMatthias Ringwald case 'P': 323fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 324fffdd288SMatthias Ringwald printf("Set speaker gain to 15 (maximum)\n"); 325c8626498SMilanka Ringwald hfp_hf_set_speaker_gain(acl_handle, 15); 326fffdd288SMatthias Ringwald break; 327fffdd288SMatthias Ringwald case 'q': 328fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 329fffdd288SMatthias Ringwald printf("Set microphone gain to 0\n"); 330c8626498SMilanka Ringwald hfp_hf_set_microphone_gain(acl_handle, 0); 331fffdd288SMatthias Ringwald break; 332fffdd288SMatthias Ringwald case 'Q': 333fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 334fffdd288SMatthias Ringwald printf("Set microphone gain to 9\n"); 335c8626498SMilanka Ringwald hfp_hf_set_microphone_gain(acl_handle, 9); 336fffdd288SMatthias Ringwald break; 337fffdd288SMatthias Ringwald case 's': 338fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 339fffdd288SMatthias Ringwald printf("Set microphone gain to 12\n"); 340c8626498SMilanka Ringwald hfp_hf_set_microphone_gain(acl_handle, 12); 341fffdd288SMatthias Ringwald break; 342fffdd288SMatthias Ringwald case 'S': 343fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 344fffdd288SMatthias Ringwald printf("Set microphone gain to 15\n"); 345c8626498SMilanka Ringwald hfp_hf_set_microphone_gain(acl_handle, 15); 346fffdd288SMatthias Ringwald break; 347fffdd288SMatthias Ringwald case 'u': 348fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 349fffdd288SMatthias Ringwald printf("Send 'user busy' (Three-Way Call 0)\n"); 350c8626498SMilanka Ringwald hfp_hf_user_busy(acl_handle); 351fffdd288SMatthias Ringwald break; 352fffdd288SMatthias Ringwald case 'U': 353fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 354fffdd288SMatthias Ringwald printf("End active call and accept waiting/held call (Three-Way Call 1)\n"); 355c8626498SMilanka Ringwald hfp_hf_end_active_and_accept_other(acl_handle); 356fffdd288SMatthias Ringwald break; 357fffdd288SMatthias Ringwald case 'v': 358fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 359fffdd288SMatthias Ringwald printf("Swap active call and hold/waiting call (Three-Way Call 2)\n"); 360c8626498SMilanka Ringwald hfp_hf_swap_calls(acl_handle); 361fffdd288SMatthias Ringwald break; 362fffdd288SMatthias Ringwald case 'V': 363fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 364fffdd288SMatthias Ringwald printf("Join hold call (Three-Way Call 3)\n"); 365c8626498SMilanka Ringwald hfp_hf_join_held_call(acl_handle); 366fffdd288SMatthias Ringwald break; 367fffdd288SMatthias Ringwald case 'w': 368fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 369fffdd288SMatthias Ringwald printf("Connect calls (Three-Way Call 4)\n"); 370c8626498SMilanka Ringwald hfp_hf_connect_calls(acl_handle); 371fffdd288SMatthias Ringwald break; 372fffdd288SMatthias Ringwald case 'W': 373fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 374fffdd288SMatthias Ringwald printf("Redial\n"); 375c8626498SMilanka Ringwald hfp_hf_redial_last_number(acl_handle); 376fffdd288SMatthias Ringwald break; 377fffdd288SMatthias Ringwald case 'x': 378fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 379fffdd288SMatthias Ringwald printf("Request phone number for voice tag\n"); 380c8626498SMilanka Ringwald hfp_hf_request_phone_number_for_voice_tag(acl_handle); 381fffdd288SMatthias Ringwald break; 382fffdd288SMatthias Ringwald case 'X': 383fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 384fffdd288SMatthias Ringwald printf("Query current call status\n"); 385c8626498SMilanka Ringwald hfp_hf_query_current_call_status(acl_handle); 386fffdd288SMatthias Ringwald break; 387fffdd288SMatthias Ringwald case 'y': 388fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 389fffdd288SMatthias Ringwald printf("Release call with index 2\n"); 390c8626498SMilanka Ringwald hfp_hf_release_call_with_index(acl_handle, 2); 391fffdd288SMatthias Ringwald break; 392fffdd288SMatthias Ringwald case 'Y': 393fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 394fffdd288SMatthias Ringwald printf("Private consulation with call 2\n"); 395c8626498SMilanka Ringwald hfp_hf_private_consultation_with_call(acl_handle, 2); 396fffdd288SMatthias Ringwald break; 397fffdd288SMatthias Ringwald case '[': 398fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 399fffdd288SMatthias Ringwald printf("Query Response and Hold status (RHH ?)\n"); 400c8626498SMilanka Ringwald hfp_hf_rrh_query_status(acl_handle); 401fffdd288SMatthias Ringwald break; 402fffdd288SMatthias Ringwald case ']': 403fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 404fffdd288SMatthias Ringwald printf("Place call in a response and held state (RHH 0)\n"); 405c8626498SMilanka Ringwald hfp_hf_rrh_hold_call(acl_handle); 406fffdd288SMatthias Ringwald break; 407fffdd288SMatthias Ringwald case '{': 408fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 409fffdd288SMatthias Ringwald printf("Accept held call (RHH 1)\n"); 410c8626498SMilanka Ringwald hfp_hf_rrh_accept_held_call(acl_handle); 411fffdd288SMatthias Ringwald break; 412fffdd288SMatthias Ringwald case '}': 413fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 414fffdd288SMatthias Ringwald printf("Reject held call (RHH 2)\n"); 415c8626498SMilanka Ringwald hfp_hf_rrh_reject_held_call(acl_handle); 416fffdd288SMatthias Ringwald break; 417fffdd288SMatthias Ringwald case '?': 418fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 419fffdd288SMatthias Ringwald printf("Query Subscriber Number\n"); 420c8626498SMilanka Ringwald hfp_hf_query_subscriber_number(acl_handle); 421fffdd288SMatthias Ringwald break; 422fffdd288SMatthias Ringwald case '!': 423fffdd288SMatthias Ringwald log_info("USER:\'%c\'", cmd); 424fffdd288SMatthias Ringwald printf("Update HF indicator with assigned number 1 (HFI)\n"); 425c8626498SMilanka Ringwald hfp_hf_set_hf_indicator(acl_handle, 1, 1); 426fffdd288SMatthias Ringwald break; 427fffdd288SMatthias Ringwald 428fffdd288SMatthias Ringwald default: 429fffdd288SMatthias Ringwald show_usage(); 430fffdd288SMatthias Ringwald break; 431fffdd288SMatthias Ringwald } 432fffdd288SMatthias Ringwald } 433fffdd288SMatthias Ringwald #endif 434fffdd288SMatthias Ringwald 43513839019SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 436185c8cd4SMatthias Ringwald 43782aca432SMatthias Ringwald switch (packet_type){ 43882aca432SMatthias Ringwald 43982aca432SMatthias Ringwald case HCI_SCO_DATA_PACKET: 440fcb08cdbSMilanka Ringwald sco_demo_receive(event, event_size); 44182aca432SMatthias Ringwald break; 44282aca432SMatthias Ringwald 44382aca432SMatthias Ringwald case HCI_EVENT_PACKET: 44482aca432SMatthias Ringwald switch (event[0]){ 44582aca432SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 446185c8cd4SMatthias Ringwald sco_demo_send(sco_handle); 44782aca432SMatthias Ringwald break; 44882aca432SMatthias Ringwald 449f9f7adeeSMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 450f9f7adeeSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(event, hci_read_local_supported_features)){ 451f9f7adeeSMatthias Ringwald dump_supported_codecs(); 452f9f7adeeSMatthias Ringwald } 453f9f7adeeSMatthias Ringwald break; 454f9f7adeeSMatthias Ringwald 45582aca432SMatthias Ringwald case HCI_EVENT_HFP_META: 45682aca432SMatthias Ringwald switch (event[2]) { 45782aca432SMatthias Ringwald case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 458c8626498SMilanka Ringwald acl_handle = hfp_subevent_service_level_connection_established_get_con_handle(event); 45908d1a604SMilanka Ringwald hfp_subevent_service_level_connection_established_get_bd_addr(event, device_addr); 46008d1a604SMilanka Ringwald printf("Service level connection established %s.\n\n", bd_addr_to_str(device_addr)); 46182aca432SMatthias Ringwald break; 46282aca432SMatthias Ringwald case HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED: 46382aca432SMatthias Ringwald printf("Service level connection released.\n\n"); 46482aca432SMatthias Ringwald break; 46582aca432SMatthias Ringwald case HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED: 46682aca432SMatthias Ringwald if (hfp_subevent_audio_connection_established_get_status(event)){ 46782aca432SMatthias Ringwald sco_handle = 0; 46882aca432SMatthias Ringwald printf("Audio connection establishment failed with status %u\n", hfp_subevent_audio_connection_established_get_status(event)); 46982aca432SMatthias Ringwald } else { 47082aca432SMatthias Ringwald sco_handle = hfp_subevent_audio_connection_established_get_handle(event); 4718901a7c4SMatthias Ringwald printf("Audio connection established with SCO handle 0x%04x.\n", sco_handle); 472d0c4aea6SMilanka Ringwald negotiated_codec = hfp_subevent_audio_connection_established_get_negotiated_codec(event); 473f9f7adeeSMatthias Ringwald switch (negotiated_codec){ 474f9f7adeeSMatthias Ringwald case 0x01: 475f9f7adeeSMatthias Ringwald printf("Using CVSD codec.\n"); 476f9f7adeeSMatthias Ringwald break; 477f9f7adeeSMatthias Ringwald case 0x02: 478f9f7adeeSMatthias Ringwald printf("Using mSBC codec.\n"); 479f9f7adeeSMatthias Ringwald break; 480f9f7adeeSMatthias Ringwald default: 481f9f7adeeSMatthias Ringwald printf("Using unknown codec 0x%02x.\n", negotiated_codec); 482f9f7adeeSMatthias Ringwald break; 483f9f7adeeSMatthias Ringwald } 484d0c4aea6SMilanka Ringwald sco_demo_set_codec(negotiated_codec); 48582aca432SMatthias Ringwald hci_request_sco_can_send_now_event(); 486185c8cd4SMatthias Ringwald } 48782aca432SMatthias Ringwald break; 48882aca432SMatthias Ringwald case HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED: 48982aca432SMatthias Ringwald sco_handle = 0; 49082aca432SMatthias Ringwald printf("Audio connection released\n"); 491fcb08cdbSMilanka Ringwald sco_demo_close(); 49282aca432SMatthias Ringwald break; 49382aca432SMatthias Ringwald case HFP_SUBEVENT_COMPLETE: 49482aca432SMatthias Ringwald switch (cmd){ 49582aca432SMatthias Ringwald case 'd': 49682aca432SMatthias Ringwald printf("HFP AG registration status update enabled.\n"); 49782aca432SMatthias Ringwald break; 49882aca432SMatthias Ringwald case 'e': 49982aca432SMatthias Ringwald printf("HFP AG registration status update for individual indicators set.\n"); 50082aca432SMatthias Ringwald default: 50182aca432SMatthias Ringwald break; 50282aca432SMatthias Ringwald } 50382aca432SMatthias Ringwald break; 50482aca432SMatthias Ringwald case HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED: 50582aca432SMatthias Ringwald printf("AG_INDICATOR_STATUS_CHANGED, AG indicator '%s' (index: %d) to: %d\n", (const char*) &event[5], event[3], event[4]); 50682aca432SMatthias Ringwald break; 50782aca432SMatthias Ringwald case HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED: 50882aca432SMatthias Ringwald printf("NETWORK_OPERATOR_CHANGED, operator mode: %d, format: %d, name: %s\n", event[3], event[4], (char *) &event[5]); 50982aca432SMatthias Ringwald break; 51082aca432SMatthias Ringwald case HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR: 51182aca432SMatthias Ringwald printf("EXTENDED_AUDIO_GATEWAY_ERROR_REPORT, status : %d\n", event[3]); 51282aca432SMatthias Ringwald break; 51382aca432SMatthias Ringwald case HFP_SUBEVENT_RING: 51482aca432SMatthias Ringwald printf("** Ring **\n"); 51582aca432SMatthias Ringwald break; 51682aca432SMatthias Ringwald case HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG: 51782aca432SMatthias Ringwald printf("Phone number for voice tag: %s\n", (const char *) &event[3]); 51882aca432SMatthias Ringwald break; 51982aca432SMatthias Ringwald case HFP_SUBEVENT_SPEAKER_VOLUME: 52082aca432SMatthias Ringwald printf("Speaker volume: %u\n", event[3]); 52182aca432SMatthias Ringwald break; 52282aca432SMatthias Ringwald case HFP_SUBEVENT_MICROPHONE_VOLUME: 52382aca432SMatthias Ringwald printf("Microphone volume: %u\n", event[3]); 52482aca432SMatthias Ringwald break; 52582aca432SMatthias Ringwald default: 52682aca432SMatthias Ringwald printf("event not handled %u\n", event[2]); 52782aca432SMatthias Ringwald break; 52882aca432SMatthias Ringwald } 52982aca432SMatthias Ringwald break; 53082aca432SMatthias Ringwald 53182aca432SMatthias Ringwald default: 53282aca432SMatthias Ringwald break; 53382aca432SMatthias Ringwald } 53482aca432SMatthias Ringwald break; 53582aca432SMatthias Ringwald 53682aca432SMatthias Ringwald default: 53782aca432SMatthias Ringwald break; 53882aca432SMatthias Ringwald } 53982aca432SMatthias Ringwald 540fffdd288SMatthias Ringwald if (event[0] != HCI_EVENT_HFP_META) return; 541fffdd288SMatthias Ringwald } 542fffdd288SMatthias Ringwald 543fffdd288SMatthias Ringwald /* @section Main Application Setup 544fffdd288SMatthias Ringwald * 545fffdd288SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 546fffdd288SMatthias Ringwald * To run a HFP HF service you need to initialize the SDP, and to create and register HFP HF record with it. 547fffdd288SMatthias Ringwald * The packet_handler is used for sending commands to the HFP AG. It also receives the HFP AG's answers. 548fffdd288SMatthias Ringwald * The stdin_process callback allows for sending commands to the HFP AG. 549fffdd288SMatthias Ringwald * At the end the Bluetooth stack is started. 550fffdd288SMatthias Ringwald */ 551fffdd288SMatthias Ringwald 552fffdd288SMatthias Ringwald /* LISTING_START(MainConfiguration): Setup HFP Hands-Free unit */ 553fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 554fffdd288SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 555185c8cd4SMatthias Ringwald 556a895b9f0SMatthias Ringwald sco_demo_init(); 557a895b9f0SMatthias Ringwald 558a895b9f0SMatthias Ringwald // register for HCI events 559a895b9f0SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 560a895b9f0SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 561a895b9f0SMatthias Ringwald hci_register_sco_packet_handler(&packet_handler); 562a895b9f0SMatthias Ringwald 563a895b9f0SMatthias Ringwald gap_discoverable_control(1); 564a895b9f0SMatthias Ringwald gap_set_class_of_device(0x200408); 565a895b9f0SMatthias Ringwald 566fffdd288SMatthias Ringwald // init L2CAP 567fffdd288SMatthias Ringwald l2cap_init(); 568fffdd288SMatthias Ringwald 5694f84bf36SMatthias Ringwald uint16_t hf_supported_features = 5704f84bf36SMatthias Ringwald (1<<HFP_HFSF_ESCO_S4) | 5714f84bf36SMatthias Ringwald (1<<HFP_HFSF_HF_INDICATORS) | 5724f84bf36SMatthias Ringwald (1<<HFP_HFSF_CODEC_NEGOTIATION) | 5734f84bf36SMatthias Ringwald (1<<HFP_HFSF_ENHANCED_CALL_STATUS) | 5744f84bf36SMatthias Ringwald (1<<HFP_HFSF_REMOTE_VOLUME_CONTROL); 5754f84bf36SMatthias Ringwald int wide_band_speech = 1; 5764f84bf36SMatthias Ringwald 577a895b9f0SMatthias Ringwald rfcomm_init(); 578fffdd288SMatthias Ringwald hfp_hf_init(rfcomm_channel_nr); 5794f84bf36SMatthias Ringwald hfp_hf_init_supported_features(hf_supported_features); 580fffdd288SMatthias Ringwald hfp_hf_init_hf_indicators(sizeof(indicators)/sizeof(uint16_t), indicators); 581fffdd288SMatthias Ringwald hfp_hf_init_codecs(sizeof(codecs), codecs); 582fffdd288SMatthias Ringwald 583fffdd288SMatthias Ringwald hfp_hf_register_packet_handler(packet_handler); 584185c8cd4SMatthias Ringwald hci_register_sco_packet_handler(&packet_handler); 585fffdd288SMatthias Ringwald 586a895b9f0SMatthias Ringwald sdp_init(); 587fffdd288SMatthias Ringwald memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer)); 5884f84bf36SMatthias Ringwald hfp_hf_create_sdp_record(hfp_service_buffer, 0x10001, rfcomm_channel_nr, hfp_hf_service_name, hf_supported_features, wide_band_speech); 589fffdd288SMatthias Ringwald printf("SDP service record size: %u\n", de_get_len(hfp_service_buffer)); 590fffdd288SMatthias Ringwald sdp_register_service(hfp_service_buffer); 591a895b9f0SMatthias Ringwald 592fffdd288SMatthias Ringwald 593d0755cd6SMatthias Ringwald #ifdef HAVE_POSIX_STDIN 594fffdd288SMatthias Ringwald btstack_stdin_setup(stdin_process); 595fffdd288SMatthias Ringwald #endif 596fffdd288SMatthias Ringwald // turn on! 597fffdd288SMatthias Ringwald hci_power_control(HCI_POWER_ON); 598fffdd288SMatthias Ringwald return 0; 599fffdd288SMatthias Ringwald } 600fffdd288SMatthias Ringwald /* LISTING_END */ 6012b6b8c15SMilanka Ringwald /* EXAMPLE_END */ 602