HaaS100 low energy Bluetooth experience

1 Overview

1.1 Introduction to Bluetooth Low Energy

1.1.1, terminology

BLE: Bluetooth Low Energy, Bluetooth low energy.

L2CAP: Logical link control and adaptation protocol, Bluetooth transport layer protocol.

ATT: Attribute transmission protocol, BLE exclusive transmission protocol.

GATT: Basic attribute specification, BLE exclusive specification, users can define services based on GATT.

BAS: Battery service, a service for batteries defined by the official Bluetooth organization, based on GATT.

HIDS: HID service, a service for human-computer interaction (mouse and keyboard, etc.) defined by the official Bluetooth organization, based on GATT.

 

1.1.2 Introduction to the agreement

BLE Bluetooth low energy is a Bluetooth protocol added in Bluetooth 4.0. It defines its own physical layer, transport layer and application layer, which can exist independently of classic Bluetooth. Due to its low power consumption and low cost characteristics, it is currently widely used. The following is the architecture of the BLE Bluetooth protocol:

image.png

The physical layer of the BLE protocol includes Link Layer and Radio2. Radio defines the physical channel, modulation mode, transmission rate, etc. The standard BLE transmission rate is 1Mbit/S, and Bluetooth 5.0 adds 2Mbit/S high-rate mode and 500Kbit/s and 125Kbit/S long-distance transmission mode; the Link Layer layer defines how to discover, connect, and negotiate connection parameters for low-power Bluetooth devices.

The transport layer of the BLE protocol includes the L2CAP and ATT2 parts. The L2CAP of BLE directly borrows the definition of classic Bluetooth and uses a fixed CID to simplify the difficulty of use. ATT is the transport layer of BLE low energy Bluetooth, which defines how application data interacts.

The application layer protocol of BLE is GATT. GATT defines the concept of services and attributes, and uses the ATT protocol to map service discovery and attribute reading and writing. And based on GATT, Bluetooth official organization defines such as BAS (battery service), HIDS (wireless keyboard and mouse service). Compliance with these services makes it easy to communicate between Bluetooth-based applications.

The BLE protocol also has an SMP security part, which defines key negotiation and data encryption between BLE devices.

 

1.2, BLE mobile phone application

Although the official Bluetooth organization defines a large number of GATT services, except for a few services such as HIDS which are widely used, other services are not very commonly used. The most common application of BLE is the communication between the device and the mobile phone. Through custom services and attributes, data can be exchanged between the mobile application and the device.

 

1.3, HaaS Bluetooth protocol stack

The HaaS100 main chip is a high-performance SoC with a built-in Bluetooth 4.2 dual-mode chip that supports classic Bluetooth and BLE low energy Bluetooth. HaaS100's software SDK also provides a dual-mode Bluetooth protocol stack that can operate BLE and classic Bluetooth. The protocol stack is located in the following path:

components/wireless/bluetooth/bt_stack

 

The dual-mode Bluetooth protocol stack is more complicated and occupies a lot of resources. If users only want to use BLE low energy Bluetooth, they can turn off some of the classic Bluetooth functions in the configuration items, but the resource occupation of the dual-mode protocol stack is difficult to reduce.

For this situation, we can use another BLE low energy Bluetooth protocol stack component, the protocol stack is located in the following path:

components/wireless/bluetooth/ble_host

 

2, good at BLE

2.1 、 BLE Demo

For the convenience of users, we have added a BLE demo to the HaaS project. The code path is as follows

application/example/BLE_demo

 

BLE Demo uses BLE low-power Bluetooth components to reduce resource occupation and realizes BLE broadcast and BLE GATT data interaction services.

Users can execute the following commands to generate firmware

aos make BLE_demo @ haas100 -c config

to make

 

In the next chapter, we will introduce the functions of the BLE protocol stack in the BLE Demo.

2.2, BLE protocol stack usage

To use the BLE protocol stack, you need to modify the application aos.mk as follows:

1: COMPONENTS adds the following components ble_host_comp

image.png

2: GLOBAL_DEFINES adds the following definition CONFIG_BT

image.png

 

After that, you can initialize the Bluetooth protocol stack in the application

    int ret;
    dev_addr_t addr = {DEV_ADDR_LE_RANDOM, DEVICE_ADDR};
    init_param_t init = {
        .dev_name = EXAMPLE_BLE_DEV_NAME,
        .dev_addr = &addr,   //&addr,
        .conn_num_max = 1,
    };

    aos_msleep(1*1000);
    /* we need first init hci driver */
    hci_h4_driver_init();

    /* bt stack init */
    ret = ble_stack_init(&init);
    if (ret) {
        EXAMPLE_TRACE_ERROR("ble_stack_init!, ret = %x\r\n", ret);
        return -1;
    }

After the initialization is successful, a callback of the protocol stack can be registered to receive events of the Bluetooth protocol stack.

    ret = ble_stack_event_register(&ble_cb);
    if(ret) {
        return -1;
    }

2.2.1, BLE broadcast

BLE broadcast is a common function. After the device broadcasts, it can be searched and connected by mobile phones. To enable BLE broadcast, you can call the broadcast interface of the protocol stack after Bluetooth is initialized. The reference code in BLE Demo is as follows.

    ad_data_t ad[2] = {0};
    ad[0].type = AD_DATA_TYPE_FLAGS;
    ad[0].data = (uint8_t *)&g_adv_flag;
    ad[0].len = 1;

    ad[1].type = AD_DATA_TYPE_UUID16_ALL;
    ad[1].data = (uint8_t *)g_uuid16_list;
    ad[1].len = sizeof(g_uuid16_list);

//    ad[2].type = AD_DATA_TYPE_NAME_COMPLETE;
//    ad[2].data = g_adv_name;
//    ad[2].len = sizeof(g_adv_name);

    adv_param_t param = {
        .type = ADV_IND,
        .ad = ad,
        .sd = NULL,
        .ad_num = BLE_ARRAY_NUM(ad),
        .sd_num = 0,
        .interval_min = ADV_FAST_INT_MIN_1,
        .interval_max = ADV_FAST_INT_MAX_1,
        .filter_policy = 0,
        .channel_map = 7,
        .direct_peer_addr = NULL,
    };

    int ret = ble_stack_adv_start(&param);
    if (ret) {
        EXAMPLE_TRACE_ERROR("adv start fail %d!", ret);
    } else {
        EXAMPLE_TRACE_INFO("adv start!");
    }

2.2.2, BLE interaction

The interaction of BLE is based on GATT service. We define the following GATT service in BLE Demo for data interaction

Service UUID

FFE0

Char Read

FFF1

Char Write

FFF2

Char Notify

FFF3

The reference code is as follows

gatt_attr_t  g_example_BLE_gatt_attrs[] = {
    BT_GATT_PRIMARY_SERVICE(UUID_VENDOR_SERVICE),
    /* READ CHAR */
    BT_GATT_CHARACTERISTIC(UUID_VENDOR_CHAR_READ, BT_GATT_CHRC_READ,
                            BT_GATT_PERM_READ, 
                            example_char_read_cb, NULL, 
                            &example_gatt_read_char),

    /* WRITE CHAR */
    BT_GATT_CHARACTERISTIC(UUID_VENDOR_CHAR_WRITE, BT_GATT_CHRC_WRITE,
                            BT_GATT_PERM_WRITE,
                            NULL, example_char_write_cb,
                            &example_gatt_write_char),

    /* NOTIFY CHAR */
    BT_GATT_CHARACTERISTIC(UUID_VENDOR_CHAR_NOTIFY, BT_GATT_CHRC_READ|BT_GATT_CHRC_NOTIFY,
                            BT_GATT_PERM_READ,
                            example_char_notify_cb, NULL,
                            &example_gatt_notify_char),
    BT_GATT_CCC_MANAGED(&example_gatt_notify_ccc, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
};

static struct bt_gatt_service g_example_BLE_gatt_service = BT_GATT_SERVICE(g_example_BLE_gatt_attrs);

When the device is connected, you can find this own service in the GATT service list, and you can access the attribute items in this service.

 

3. Actual measurement

3.1, software preparation

To facilitate testing, we can download BLE debugging tools on the mobile phone, the more common ones are NRF Connect.

 

3.2. Test process

3.2.1, service connection

After the firmware is burned to the HaaS100 development board and restart, you can see the following prints on the HaaS development board

[INFO]adv start!

 

Now open NRF Connect, click Scan, you can find the HaaS BLE device, as follows

image.png

Then click the connection button of the HaaS BLE device, you can see the device's GATT service list, where the service of ffe0 is a custom service, as shown in the following figure

image.png

Click this service to expand, you can see the three attribute items fff1, fff2 and fff3.

3.2.2, attribute item read

fff1 is the read attribute defined by us. The value of this attribute is set in the code

uint8_t example_gatt_read_char[16] = "HaaS Read";

Click the read arrow to get the attribute value of this attribute item, as shown in the figure below, it is the ASCII code of the attribute value

image.png

3.2.3, attribute item write

fff2 is the write attribute defined by us. Click the write arrow to fill in the value to be written, as shown in the figure below

image.png

After clicking Send, you can see that the printout of the HaaS development board has a corresponding reception.

image.png

4. Summary

If you need more technical support, you can join the Dingding Developer Group

For more technology and solution introduction, please visit the Aliyun AIoT homepage https://iot.aliyun.com/

Guess you like

Origin blog.csdn.net/HaaSTech/article/details/111939387