HFP AG - Audio Gateway

Source Code: hfp_ag_demo.c

This HFP Audio Gateway example demonstrates how to receive an output from a remote HFP Hands-Free (HF) unit, and, if HAVE_BTSTACK_STDIN is defined, how to control the HFP HF.

Main Application Setup

Listing here shows main application code. To run a HFP AG service you need to initialize the SDP, and to create and register HFP AG record with it. The packet_handler is used for sending commands to the HFP HF. It also receives the HFP HF's answers. The stdin_process callback allows for sending commands to the HFP HF. At the end the Bluetooth stack is started.

int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
  (void)argc;
  (void)argv;

  sco_demo_init();

  // Request role change on reconnecting headset to always use them in slave mode
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1725">hci_set_master_slave_policy</a>(0);

  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("HFP AG Demo 00:00:00:00:00:00");
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);

  // L2CAP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();

#ifdef ENABLE_BLE
  // Initialize LE Security Manager. Needed for cross-transport key derivation
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
#endif

  uint16_t supported_features           =
    (1&lt;&lt;HFP_AGSF_ESCO_S4)           |
    (1&lt;&lt;HFP_AGSF_HF_INDICATORS)         |
    (1&lt;&lt;HFP_AGSF_CODEC_NEGOTIATION)       |
    (1&lt;&lt;HFP_AGSF_EXTENDED_ERROR_RESULT_CODES) |
    (1&lt;&lt;HFP_AGSF_ENHANCED_CALL_CONTROL)     |
    (1&lt;&lt;HFP_AGSF_ENHANCED_CALL_STATUS)    |
    (1&lt;&lt;HFP_AGSF_ABILITY_TO_REJECT_A_CALL)  |
    (1&lt;&lt;HFP_AGSF_IN_BAND_RING_TONE)       |
    (1&lt;&lt;HFP_AGSF_VOICE_RECOGNITION_FUNCTION)  |
    (1&lt;&lt;HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS) |
    (1&lt;&lt;HFP_AGSF_VOICE_RECOGNITION_TEXT) |
    (1&lt;&lt;HFP_AGSF_EC_NR_FUNCTION) |
    (1&lt;&lt;HFP_AGSF_THREE_WAY_CALLING);

  // HFP
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/rfcomm.h#L278">rfcomm_init</a>();
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>(rfcomm_channel_nr);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>_supported_features(supported_features);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>_codecs(sizeof(codecs), codecs);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>_ag_indicators(ag_indicators_nr, ag_indicators);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>_hf_indicators(hf_indicators_nr, hf_indicators); 
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>_call_hold_services(call_hold_services_nr, call_hold_services);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L95">hfp_ag_init</a>_apple_identification("BTstack", 0);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L467">hfp_ag_set_subscriber_number_information</a>(&amp;subscriber_number, 1);

  // SDP Server
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
  memset(hfp_service_buffer, 0, sizeof(hfp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L87"><a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L550">hfp_ag_create_sdp_record</a>_with_codecs</a>( hfp_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(),
                      rfcomm_channel_nr, hfp_ag_service_name, 0, supported_features, sizeof(codecs), codecs);
  btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( hfp_service_buffer) &lt;= sizeof(hfp_service_buffer));
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(hfp_service_buffer);

  // register for HCI events and SCO packets
  hci_event_callback_registration.callback = &amp;packet_handler;
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&amp;hci_event_callback_registration);
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1596">hci_register_sco_packet_handler</a>(&amp;packet_handler);

  // register for HFP events
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hfp_ag.h#L143">hfp_ag_register_packet_handler</a>(&amp;packet_handler);

  // parse human readable Bluetooth address
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/btstack_util.h#L287">sscanf_bd_addr</a>(device_addr_string, device_addr);

#ifdef HAVE_BTSTACK_STDIN
  btstack_stdin_setup(stdin_process);
#endif  
  // turn on!
  <a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1566">hci_power_control</a>(HCI_POWER_ON);
  return 0;
}