Classic Bluetooth Service

Overview

Currently, Bluetooth is mainly divided into Classic Bluetooth BR (Basic Rate)/EDR (Enhanced Data Rate) and Low Energy (BLE) Bluetooth. Sifli Bluetooth is based on Bluetooth 5.3 and provides a comprehensive dual-mode Bluetooth solution. Sifli Bluetooth focuses on supporting Bluetooth audio and Bluetooth voice functions, providing convenient related development methods. Sifli Bluetooth has good readability, scalability, and portability. Sifli Bluetooth is module-oriented, providing rich interfaces with clear hierarchy and logic between modules, simple and easy to understand, providing a good foundation for users to learn and develop. The overall design goal of Sifli Bluetooth is to enable users to form high-quality products in a shorter time through configuration or secondary development of Sifli Bluetooth. This section mainly introduces Sifli’s architecture and related specification configuration information for classic Bluetooth services.

To transmit data via Bluetooth on Bluetooth-enabled devices, Sifli provides basic Bluetooth functionality APIs and common Bluetooth protocol specifications (A2DP/AVRCP/HFP/PBAP/HID/SPP, etc.). Users can perform the following operations through APIs:

  • Scan other Bluetooth devices

  • Manage connected devices

  • Implement call and audio functions wirelessly with other Bluetooth devices (such as phones, headphones, etc.)

  • Transfer custom data with other devices

Note

For more information about APIs, please refer to: bluetooth_service

Classic Bluetooth Architecture

The main purpose of Sifli Classic Bluetooth architecture is to build a convenient, stable, comfortable, and efficient solution for users, structurally encapsulating functions and logic that users care about, and minimizing users’ secondary development workload as much as possible. Therefore, the design of Sifli Classic Bluetooth architecture always revolves around the hierarchical structure and functional modularization.

  • From the overall Bluetooth architecture perspective, Bluetooth is divided into two major parts: controller and host.

    • The controller includes PHY, Baseband, Link Controller, Link Manager, Device Manager, HCI and other modules, used for hardware interface management, link management, etc.;

    • The host includes L2CAP, RFCOMM, SDP, GAP and various specifications, building the foundation for providing interfaces to the application layer, facilitating user access to the Bluetooth system.

  • Sifli’s controller runs on LCPU, while the host part and APIs that provide users access to Bluetooth-related functions run on HCPU.

  • Sifli’s host part is divided into three categories: protocol stack, service, and interface.

    • The protocol stack mainly implements state machines and message processing related to Bluetooth protocols and specifications, which is the core of Bluetooth-related specifications, providing standardized interfaces for Bluetooth communication establishment and data interaction. Provided to users in lib format;

    • Service mainly provides modules for processing specific Bluetooth function service-related information, designed for user-specific functions, released to users in source code format;

    • Interface mainly provides specific functions for users to access the Bluetooth system, which are simple and efficient general-purpose interfaces, released to users in source code format. Users can use or customize related functions according to their needs. Figure 1: Sifli Bluetooth Architecture

Bluetooth Specification Configuration Information

Currently, the classic Bluetooth specifications and protocols supported by the host protocol stack are as follows:

  • Specifications: A2DP, AVRCP, GATT over BR, HFP, HID, PAN, PBAP, SPP

  • Protocols: L2CAP, SDP, AVDTP, AVCTP, RFCOMM

Figure 1: Bluetooth Specifications Supported by Sifli Bluetooth

Bluetooth Service Configuration and Development Instructions

Sifli SDK Project Introduction

Sifli Project Files

  • customer: Mainly contains board-level related files and peripheral drivers

  • drivers: Mainly contains HAL drivers for chip-related peripherals

  • dvt: Mainly contains chip ATE-related test projects

  • example: Mainly contains project examples

  • external: Mainly contains third-party open source project code

  • middleware: Mainly contains middleware-related code

  • rtos: Mainly contains rtthread OS-related code

Sifli Bluetooth Project Compilation Basic Process

  1. Project environment: Run set_env.bat to build the compilation environment

  2. Enter directory: siflisdk\example\bt\test_example\project\common

  3. Compile the corresponding BT project in the above path, and the generated files are also in the common path

    • Compilation result build_<board_name>_hcpu directory description:

      • board: Mainly contains board-level related executable files

      • bootloader: Mainly contains bootloader-related executable files

      • ftab: Mainly contains flash table-related executable files

      • lcpu: Mainly contains lcpu-related executable files

      • peripheral: Mainly contains peripheral driver-related executable files

      • sifli_sdk: Mainly contains SDK middleware-related executable files

  4. Flash files: Run script uart_download.bat Sifli Classic Bluetooth Example Sifli Classic Bluetooth Compilation

Bluetooth Service Interface Invocation and Callback Usage Methods and Precautions

  • Bluetooth interface integrates BT discovery, connection, and reconnection functions. Related function interfaces are mainly in: bts2_app_interface

  • Bluetooth service callbacks include data transmitted by remote Bluetooth devices, results of calling Bluetooth service interface processing, and active updates of Bluetooth status information, etc.

    • Sifli Bluetooth service callbacks require users to implement callback handling functions themselves and transfer data to user’s own task to process related data

    • Registration function: bt_interface_register_bt_event_notify_callback

    • Unregistration function: bt_interface_unregister_bt_event_notify_callback

Note

The data processing task for callback functions is a user-defined implemented task.

int bt_notify_event_hdl(uint16_t type, uint16_t event_id, uint8_t *data, uint16_t data_len)
{
     return 0;
}

Brief Description of Important Bluetooth Events

Messages received by bt_notify_event_hdl include:

  • ACL connection successful message: BT_NOTIFY_COMMON_ACL_CONNECTED

  • ACL disconnection successful message: BT_NOTIFY_COMMON_ACL_DISCONNECTED

  • Profile connection successful message: BT_NOTIFY_(profile_type)_PROFILE_CONNECTED

  • Profile disconnection successful message: BT_NOTIFY_(profile_type)_PROFILE_DISCONNECTED

  • SCO connection successful message: BT_NOTIFY_COMMON_SCO_CONNECTED

  • SCO disconnection successful message: BT_NOTIFY_COMMON_SCO_DISCONNECTED

  • HFP_HF sending AT cmd peer device processing result message: BT_NOTIFY_HF_AT_CMD_CFM

// ACL start
typedef struct
{
    bt_notify_device_mac_t mac;                  /// the remote device mac
    uint8_t res;                                /// core spec's error code
    uint8_t acl_dir;                            ///(0x00):ACL_INIT_LOCAL  (0x01):ACL_INIT_PEER
    uint32_t dev_cls;                            ///remote device class of device
} bt_notify_device_acl_conn_info_t;

typedef struct
{
    bt_notify_device_mac_t mac;                  /// the bt device mac
    uint8_t res;                               /// core spec's error code
} bt_notify_device_base_info_t;
// ACL end

// profile start
typedef struct
{
    bt_notify_device_mac_t mac;                  /// the bt device mac
    uint8_t profile_type;                        /// BT_NOTIFY type id
    uint8_t res;                                /// error code is custom type
} bt_notify_profile_state_info_t;
// profile end

// error code table start
typedef enum
{
    BTS2_SUCC = 0,
    BTS2_FAILED,       /*  1 */
    BTS2_TIMEOUT,
    BTS2_BOND_FAIL,
    BTS2_CONN_FAIL,
    BTS2_SCO_FAIL,     /*  5 */
    BTS2_LINK_LOST,
    BTS2_LOCAL_DISC,
    BTS2_RMT_DISC,
    BTS2_REJ,
    BTS2_PSM_REJ,
    BTS2_SECU_FAIL,    /* 10 */
    BTS2_SDP_CLT_FAIL,
    BTS2_SDP_SRV_FAIL,
    BTS2_CMD_ERR,
    BTS2_DATA_WR_FAIL,
    BTS2_HW_ERR,       /* 15 */
    BTS2_UNSUPP_FEAT,
    BTS2_EXCEED_MAX_CONN_NUM,
    BTS2_IN_W4CONN_ST,
    BTS2_LOCAL_DISC_FAIL,
    BTS2_SCO_DISC_FAIL,
    BTS2_INQURI_INTERUPT,
    BTS2_INQURI_REPEAT_ERR,
    BTS2_INQURI_DEV_BUSY,
    BTS2_DEV_BUSY,
    BTS2_NO_PROFILE_LINK, //ADD for
} BTS2E_RESULT_CODE;
// error code table end

// sco start
typedef struct
{
    uint8_t sco_type;
    uint8_t sco_res;                               /// error code is custom type: BTS2E_RESULT_CODE
} bt_notify_device_sco_info_t;
// sco end

// at cmd cfm start
typedef struct
{
    uint8_t  at_cmd_id;
    uint8_t  res;                              /// error code is custom type (BTS2_SUCC(0)/BTS2_FAILED(1)/BTS2_TIMEOUT(2))
} bt_notify_at_cmd_cfm_t;
// at cmd cfm end
  1. Bluetooth on/off demo:

    • Turn on Bluetooth, corresponding interface: bt_interface_open_bt

    • Turn off Bluetooth, corresponding interface: bt_interface_close_bt

    • Corresponding processing result message event handling as follows:

static int bt_sifli_notify_common_event_hdl(uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    switch (event_id)
    {
    case BT_NOTIFY_COMMON_BT_STACK_READY:
    {
        // handle open bt
        break;
    }
    case BT_NOTIFY_COMMON_CLOSE_COMPLETE:
    {
        //handle close bt
        break;
    }
}

static int bt_notify_handle(uint16_t type, uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    int ret = -1;

    switch (type)
    {
    case BT_NOTIFY_COMMON:
    {
        ret = bt_sifli_notify_common_event_hdl(event_id, data, data_len);
    }
    break;
    default:
        break;
    }

    return 0;
}
int app_bt_notify_init(void)
{
    bt_interface_register_bt_event_notify_callback(bt_notify_handle);
    return BT_EOK;
}

INIT_ENV_EXPORT(app_bt_notify_init);
  1. Bluetooth device scanning demo:

    • Start Bluetooth scanning, corresponding interface: bt_interface_start_inquiry

    • Stop Bluetooth scanning, corresponding interface: bt_interface_stop_inquiry

    • Corresponding processing result message event handling as follows:

static int bt_sifli_notify_common_event_hdl(uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    switch (event_id)
    {
    case BT_NOTIFY_COMMON_DISCOVER_IND:
    {
        //handle inquiry result device info
        break;
    }
    case BT_NOTIFY_COMMON_INQUIRY_CMP:
    {
        //handle inquiry complete result
        break;
    }
}

static int bt_notify_handle(uint16_t type, uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    int ret = -1;

    switch (type)
    {
    case BT_NOTIFY_COMMON:
    {
        ret = bt_sifli_notify_common_event_hdl(event_id, data, data_len);
    }
    break;
    default:
        break;
    }

    return 0;
}
int app_bt_notify_init(void)
{
    bt_interface_register_bt_event_notify_callback(bt_notify_handle);
    return BT_EOK;
}

INIT_ENV_EXPORT(app_bt_notify_init);
  1. Bluetooth device connection demo:

    • Connect Bluetooth device, corresponding interface: bt_interface_conn_ext

    • Disconnect Bluetooth device, corresponding interface: bt_interface_disc_ext

    • Corresponding processing result message event handling as follows:

static int bt_sifli_notify_common_event_hdl(uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    switch (event_id)
    {
    case BT_NOTIFY_COMMON_BT_STACK_READY:
    {
        // handle open bt
        break;
    }
    case BT_NOTIFY_COMMON_CLOSE_COMPLETE:
    {
        //handle close bt
        break;
    }
}

static int bt_notify_handle(uint16_t type, uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    int ret = -1;

    switch (type)
    {
    case BT_NOTIFY_COMMON:
    {
        ret = bt_sifli_notify_common_event_hdl(event_id, data, data_len);
    }
    break;
    default:
        break;
    }

    return 0;
}
int app_bt_notify_init(void)
{
    bt_interface_register_bt_event_notify_callback(bt_notify_handle);
    return BT_EOK;
}

INIT_ENV_EXPORT(app_bt_notify_init);
  1. Bluetooth device scanning demo:

    • Start Bluetooth scanning, corresponding interface: bt_interface_start_inquiry

    • Stop Bluetooth scanning, corresponding interface: bt_interface_stop_inquiry

    • Corresponding processing result message event handling as follows:

static int bt_sifli_notify_(profile)_event_hdl(uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    switch (event_id)
    {
    case BT_NOTIFY_(profile_type)_PROFILE_CONNECTED:
    {
        //handle profile connected event
        break;
    }
    case BT_NOTIFY_(profile_type)_PROFILE_DISCONNECTED:
    {
        //handle profile disconnected event
        break;
    }
}

static int bt_sifli_notify_common_event_hdl(uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    switch (event_id)
    {
    case BT_NOTIFY_COMMON_ACL_CONNECTED:
    {
        //handle acl connected event
        break;
    }
    case BT_NOTIFY_COMMON_ACL_DISCONNECTED:
    {
        //handle acl disconnected event
        break;
    }
}

static int bt_notify_handle(uint16_t type, uint16_t event_id, uint8_t *data, uint16_t data_len)
{
    int ret = -1;

    switch (type)
    {
    case BT_NOTIFY_COMMON:
    {
        ret = bt_sifli_notify_common_event_hdl(event_id, data, data_len);
    }
    break;
    case BT_NOTIFY_(profile_type):
    {
        ret = bt_sifli_notify_(profile)_event_hdl(event_id, data, data_len);
    }
    break;
    default:
        break;
    }

    return 0;
}
int app_bt_notify_init(void)
{
    bt_interface_register_bt_event_notify_callback(bt_notify_handle);
    return BT_EOK;
}

INIT_ENV_EXPORT(app_bt_notify_init);

Bluetooth Protocols and Specifications