Local Control and Control via Remotes

Last Updated on : 2024-10-09 03:23:13download

This topic describes how to control Bluetooth mesh devices locally or using a physical remote. The SDK enables devices to communicate with each other locally, enabling various methods of device control.

Concepts

Local control allows Bluetooth mesh devices to talk to each other after they are paired, without the need for a mobile app or gateway. Based on group subscription, when the controller device sends a message to the group address, any subscribed devices will receive it. This allows for one-to-many and many-to-many control.

publish_addr

The publish_addr is the group address assigned to the controller device for sending control commands after pairing. Devices subscribed to the publish_addr can receive control commands from the controller device.

After the controller device receives the publish_addr, there are eight addresses available from publish_addr to the next seven. Generally, only one address is used, except for complex cases that require multiple publish_addr. For more information, see Multi-Group Control.

Communication process

Local Control and Control via Remotes

Protocols

Send publish address

During pairing, the mobile app or gateway sends a group address to the device through the vendor model.

The mobile app sends:

opcode: 0xC9D007 (TAL_MESH_OPCODE_WRITE)

Payload data:

Field Length (byte) Description
Command 1 0x86
Data length 1 0x02
Data Group address (2 bytes) group_addr: The publish address assigned to the device.

The device returns:

opcode: 0xCDD007 (TAL_MESH_OPCODE_DATA)

Payload data:

Field Length (byte) Description
Command 1 0x86
Data length 1 0x01
Data 1
  • 0x00: Success
  • 0x01: Failure

After the device receives the publish_addr, it must return a success message. Otherwise, pairing will fail.

Bind device locally

The controller device sends:

opcode: 0xC9D007 (TAL_MESH_OPCODE_WRITE)

opcode: 0xCAD007 (TAL_MESH_OPCODE_WRITE_UNACK)

Field Length (byte) Description
Command 1 0x81
Data length 1 0x03
Data Command (1 byte) + Sub_addr (2 bytes) Command:
  • 0x00: Unsubscribe from a group address. The device can decide whether to execute this command.
  • 0x01: Subscribe to a group address. The device can decide whether to execute this command.
  • 0x02: Unsubscribe from a group address. The device must execute this command.
  • 0x03: Subscribe to a group address. The device must execute this command.
  • Sub_addr: The address to subscribe to or unsubscribe from.

The controlled device returns:

opcode: 0xCDD007 (TAL_MESH_OPCODE_DATA)

Field Length (byte) Description
Command 1 0x81
Data length 1 0x01
Data Status (1 byte) + Sub_addr (2 bytes) Status:
  • 0: Success.
  • 1: Hit the maximum number of groups.
  • 2: Reserved.
  • 3: The specified value is out of range.
  • 4: Reserved.
  • 5: Other errors.
  • Sub_addr: The address to subscribe to or unsubscribe from.

Query the binding status of a specific local group:

opcode: 0xC9D007 (TAL_MESH_OPCODE_WRITE)

Field Length (byte) Description
Command 1 0x82
Data length 1 0x00

Return the binding status of the group:

opcode: 0xCDD007 (TAL_MESH_OPCODE_DATA)

Field Length (byte) Description
Command 1 0x82
Data length 1 0x02
Data group_addr (2 bytes) Group_addr: The destination address for receiving query commands, i.e. the group that the app wants to query. This address is returned here.
To prevent network storms caused by a large number of replies, the device replies are delayed or randomized based on their different mesh addresses.

API description

Configure publish request

During initialization, specify whether to request the publish address.

API description

VOID tal_firmware_infor_set(UINT8_T is_key, UINT8_T *product_id, UINT8_T *product_key, UINT16_T version, UINT16_T mesh_category, UINT8_T need_publish_addr);

Parameters

Parameter Description
need_publish_addr Specifies whether to request the publish address during pairing.

Send data

This API is used to respond to the publish address set command as well as send the local binding command.

OPERATE_RET tal_mesh_data_send(USHORT_T src_addr, USHORT_T dst_addr, UINT_T opcode, UCHAR_T *data, USHORT_T data_len);

How-to

Get publish address

The following code shows how to get the publish address and save it in the flash memory.

  • The publish address can serve as the content sent to the controlled device through the local binding command.

  • The publish address can serve as the destination address for controlling the connected device.

VOID_T app_publish_addr_store(UINT16_T publish_addr)
{
    // todo, store publish addr into flash
}

UINT16_T app_publish_addr_get(VOID_T)
{
    UINT16_T publish_addr;
    // todo, get publish addr from flash
    return publish_addr;
}

OPERATE_RET app_mesh_data_recv(TAL_MESH_ACCESS_MSG_T *msg_raw, TAL_MESH_NET_PARAM_T *net_param)
{
    switch (msg_raw->opcode) {
        case TAL_MESH_OPCODE_WRITE:
            if (msg_raw->data_len < 2) {
            } else{
                if (0x86 == msg_raw->data[0]) {
                    UINT8_T rsp_data[3] = {0x86, 0x01, 0x00};
                    UINT16_T publish_addr = msg_raw->data[3] + (msg_raw->data[2] << 8);
                    tal_main_debug("PUBLISH_ADDR_RECV_SUCCESS");
                    app_publish_addr_store(publish_addr);
                    tal_mesh_data_send(net_param->dst_addr, net_param->src_addr, TAL_MESH_OPCODE_DATA, rsp_data, 3);
                }
            }
        break;

        default:
        break;
    }
    return OPRT_OK;
}

Bind device using app panel

Users can add a device to a group using the app panel of the controller device. The target device will receive a group subscription command to bind with the controller. This feature requires a custom app panel. Contact your project manager for details.

The figure below shows the operation on the app panel.

Local Control and Control via Remotes

Bind device locally

Perform manual operations on the device to bind with a controller. Here is how it works:

  • Put the controlled device into binding mode by powering it on or pressing a button. It is recommended to set a time window for the device to remain in pairing mode.

  • Once triggered by a button press or other methods, the controller device will advertise its presence to all devices using a packet with the destination address 0xFFFF. Devices in binding mode can subscribe to the specified group address.

The following code shows the controller device broadcasts a binding command, and the controlled device receives it and decides whether to subscribe to the publish address based on its binding mode.

Controller device

VOID_T app_device_bind(VOID_T)
{
    UINT8_T data[5] = {0};
    data[0] = 0x81;
    data[1] = 0x03;
    data[2] = 0x01;
    data[3] = app_publish_addr_get() >> 8;
    data[4] = app_publish_addr_get() & 0xFF;
    tal_mesh_data_send(tkl_mesh_primary_ele_addr_get(), 0xFFFF, TAL_MESH_OPCODE_WRITE_UNACK, data, 5);
}

Controlled device

OPERATE_RET app_mesh_data_recv(TAL_MESH_ACCESS_MSG_T *msg_raw, TAL_MESH_NET_PARAM_T *net_param)
{
    UINT8_T data[5] = {0};
    UINT8_T data_len;
    switch (msg_raw->opcode) {
        case TAL_MESH_OPCODE_WRITE:
            if (msg_raw->data_len < 2) {
            } else {
                if (0x81 == msg_raw->data[0]) {
                    UINT8_T cmd = msg_raw->data[2];
                    UINT16_T publish_addr = msg_raw->data[4] + (msg_raw->data[3] << 8);
                    if (0x00 == cmd) {
                        tal_group_addr_sub_set(TAL_MESH_OPCODE_CFG_MODEL_SUB_DELETE, 0, publish_addr);
                    } else if (0x01 == cmd) {
                        tal_group_addr_sub_set(TAL_MESH_OPCODE_CFG_MODEL_SUB_ADD, 0, publish_addr);
                    }
                    data[0] = 0x81;
                    data[1] = 0x03;
                    data[2] = 0x01;
                    data[3] = (publish_addr >> 8)  & 0xFF;
                    data[4] = publish_addr & 0xFF;
                    tal_mesh_data_send(tkl_mesh_primary_ele_addr_get(), net_param->src_addr, TAL_MESH_OPCODE_DATA, data, 5);
                } else if (0x82 == msg_raw->data[0]) {
                    data[0] = 0x82;
                    data[1] = 0x02;
                    data[2] = (net_param->dst_addr >> 8)  & 0xFF;
                    data[3] = net_param->dst_addr & 0xFF;
                    tal_mesh_data_send(tkl_mesh_primary_ele_addr_get(), net_param->src_addr, TAL_MESH_OPCODE_DATA, data, 4);
                }
            }
        break;

        default:
        break;
    }
    return OPRT_OK;
}

Control device

When the controlled device subscribes to the controller device’s publish address, they become associated. The controller can then send messages to this address, which will be received by the subscribed device.

STATIC UINT8_T global_tid = 0;
VOID_T app_onoff_send(UINT8_T on_off)
{
    TAL_MESH_GENERIC_ONOFF_SET_T onoff_data;
    onoff_data.onoff = on_off;
    onoff_data.tid = global_tid++;
    tal_mesh_data_send(tkl_mesh_primary_ele_addr_get(), app_publish_addr_get(), TAL_MESH_OPCODE_ON_OFF_SET_UNACK, (UINT8_T*)&onoff_data, 2);
}

The controller device sends unacknowledged control commands to prevent a network storm caused by simultaneous responses from multiple devices.

Control multiple groups

This section describes the scenarios where a controller device uses more than one of the eight assigned publish addresses. Here is an example:

The control panel features six buttons: button 1 for light on/off, and buttons 2 and 3 for brightness up and down. The other three buttons, A, B, and C, are used to bind with three devices.
Press and hold button A to bind lights in rooms 1 and 2. Press and hold button B to bind lights in room 1. Press and hold button C to bind lights in room 2. Press button A to switch the target to all lights in rooms 1 and 2. The commands emitted by buttons 1, 2, and 3 will be sent to them. Similarly, press button B or C to control the lights in room 1 or 2.

Multiple publish addresses are used to control several groups separately.