Bluetooth Mesh Networking

Last Updated on : 2024-02-23 07:37:09download

This topic describes the Bluetooth mesh networking mechanisms and basic concepts.

Concepts

Mesh communication channel

Bluetooth mesh networking is based on Bluetooth Low Energy (LE) 4.x and communicates on advertising channels 37, 38, and 39.

Due to the maximum length constraint of Bluetooth advertising packets, the packet at the bottom layer is small. For longer packets, the transport layer will perform segmentation, but this can increase the transmission time.

Mesh packet

There are two types of mesh packets: access messages and control messages. Most services use the access message.

The access message at the lower transport layer can be formatted as unsegmented or segmented.

  • An unsegmented access message can be up to 11 bytes. This is calculated by subtracting the 4-byte message integrity check (MIC) from the maximum length of 15 bytes. The 11 bytes include the opcode. When you send data with the vendor model, the opcode is 3 bytes, limiting the maximum data length to 8 bytes.

  • A segmented access message has a maximum length of 12 bytes per packet. The total length is calculated as 12 × n, where n represents the number of packets. The actual length of the user data is calculated by subtracting the 4-byte MIC and opcode length from the total length (12 × n), which is 12 × n − 4 − opcode_len. When using the vendor model, calculate the number of packets as n = (data_len + 7) ÷ 12 and round off n.

When the control command packet is small, the success rate and delivery of the command are excellent. For example, turn a device on/off, name a dimmer, and report sensor data. However, if the command packet is large, the Bluetooth mesh solution is not recommended as this will compromise its advantages and result in packet loss and latency.

Flooding

Mesh networks that use the flooding technique broadcast data to all nodes within direct range. These nodes then relay the received data by broadcasting it again to another set of in-range nodes.

Pros and cons of flooding:

  • Pros: Quick response, high delivery success rate, and strong network reliability.
  • Cons: Prone to network storm. The occurrence of increased collisions in the network due to simultaneous message transmission can lead to reduced delivery rates and slower response speeds.

Time to live (TTL)

TTL is a 7-bit field that all Bluetooth mesh advertising packets include. It controls the maximum number of hops, over which a message is relayed. The following values are defined:

  • 0: has not been relayed and will not be relayed.
  • 1: may have been relayed, but will not be relayed.
  • 2 to 126: may have been relayed and can be relayed.
  • 127: has not been relayed and can be relayed.

The communication between nodes is not dependent on a specific route. A node can relay an advertising packet with a TTL greater than 0, while decrementing the TTL in the packet by 1.

Sequence number (SEQ)

Each packet sent on a mesh network has a unique 3-byte SEQ. The sequence number is incremented every time a device sends a message.

Each Bluetooth mesh device has a SEQ cache table in volatile RAM to store the source address (src_addr) and the latest SEQ of the received mesh packet. Every time the device receives a mesh packet, it verifies if the received SEQ is higher than the SEQ of the corresponding source address in the cache table. If the SEQ is less than or equal to the one in the cache table, the device will discard the received packet as a retransmission or invalid.

Mesh roles

  • Mesh provisioner: A device that is able to add another device to the mesh network, for example, the Tuya Smart app and Bluetooth gateway.
  • Relay node: An always-powered device that is able to receive and retransmit mesh messages over the advertising bearer to enable larger networks.
  • Proxy node: A device that is able to receive and retransmit mesh messages between GATT and advertising bearers. The mobile phone connects to a proxy node to communicate with the mesh network.
  • Low power node (LPN): A device that is able to operate within a mesh network at significantly reduced receiver duty cycles.
  • Friend node: A device that helps a LPN to operate efficiently, by storing messages destined for that node and only delivering them when the LPN polls for messages.

Tuya-enabled devices support the relay and proxy features by default, while do not adopt the LPN and friend features.

Mesh keys

The Bluetooth mesh specification defines three types of security keys.

  • Network key (NetKey): A NetKey secures communication at the network layer and is shared across all nodes in the network or all nodes in a particular subnet. Possession of a given NetKey is what defines membership of a given mesh network or subnet.

  • Application key (AppKey): An AppKey secures communication at the access layer and is shared across all nodes which participate in a given mesh application. An AppKey is bound to a specific NetKey, meaning that a device must possess both the AppKey and the specific NetKey it is mathematically related to in order to be used in securing Bluetooth mesh messages.

  • Device key (DevKey): Each device possesses a unique DevKey. Only the node which the DevKey belongs to and the provisioner know the DevKey, which is used to secure communication between the node and the provisioner.

Mesh network transmit parameters

Mesh networks use the flooding technique for communication, without routing and acknowledgment (ACK) of messages. ACK applies when sending segmented messages to a unicast address.

To enhance the delivery success rate in the absence of ACK, increase the number of packets. The network layer will transmit the messages from the application layer multiple times at specific intervals. Similarly, the message will be relayed multiple times at specific intervals. The network transmit parameters control the number and timing of message transmissions, as shown below.

Parameter Description Calculated value Default value
Network Transmit Count The number of transmissions from the network layer The number of transmissions = Transmit Count + 1 7
Network Transmit Interval Steps The interval between transmissions from the network layer Transmission interval = (Steps + 1) × 10 ms 0
Relay Retransmit Count The number of retransmissions from the network layer The number of transmissions = Transmit Count + 1 4
Relay Retransmit Interval Steps The interval between retransmissions from the network layer Transmission interval = (Steps + 1) × 10 ms 0

API description

Set device’s role

/**@brief mesh node feature. */
typedef enum {
    MESH_FEATURE_RELAY = 0x00,  /**< Relay. */
    MESH_FEATURE_PROXY,         /**< Proxy. */
    MESH_FEATURE_FRIEND,        /**< Friend. */
    MESH_FEATURE_LPN,           /**< LPN. */
} MESH_FEATURE_T;
OPERATE_RET tkl_mesh_node_features_set(MESH_FEATURE_T featrue, UCHAR_T enable);

This API is used to set the role of the mesh device and disable relay and proxy features. Depending on the return value, some chip platforms support the friend feature. The generic SDK does not support LPN.

Disabling the relay feature on certain devices can reduce network storms, improving both network stability and data transmission speed. You can use this API to test the network performance when the relay feature is on or off.

Set TTL

OPERATE_RET tkl_mesh_node_default_ttl_set(UCHAR_T ttl);

The TTL defaults to 8 in the SDK. You can call this API in the initialization function OPERATE_RET tuya_init_last(VOID_T) to modify the TTL.

The default TTL for both the mobile app and gateway is 8. To modify their TTL, make changes in the app SDK or gateway SDK.

Set mesh network parameters

VOID tkl_mesh_network_transmit_set(UCHAR_T cnt, UCHAR_T step);
VOID tkl_mesh_network_relay_retransmit_set(UCHAR_T cnt, UCHAR_T step);

This API is used to set the network transmit and relay retransmit parameters.

How-to

Set mesh network parameters

VOID tal_mesh_state_callback(TAL_MESH_NET_STATE_T state){
    tal_main_debug("mesh_state:%d", state);
    switch(state){
        case TAL_MESH_PROVISION_SUCCESS:
            tkl_mesh_network_transmit_set(7, 0);
            tkl_mesh_network_relay_retransmit_set(4, 0);
        break;
        default:
        break;
    }
}

Update this parameter when receiving a successful pairing in the mesh status callback. If you are not well-versed with the Bluetooth mesh protocol, it is recommended not to modify this parameter.