TUYA Zigbee SDK Document

SDK Document version

SDK summarize

Tuya Zigbee EFR32 SDK is used to develop Zigbee 3.0 standard products. This SDK is abstracted again based on the Silicon Labs SDK ,The complex Zigbee technical details are shielded, which is convenient for developers to get started quickly. The SDK mainly includes three parts: hardware interface, network interface and tool interface,And provide some types of sample code to demonstrate the use of various interfaces and the basic development specifications of Zigbee device access to the Tuya system.

SDK download

https://github.com/TuyaInc/tuya_zigbee_sdk

SDK Directory Structure

    ├── app
    │   ├── build-all.py
    │   ├── light
    │   ├── sensor
    │   ├── smart_plug
    │   └── switch
    ├── doc
    │   ├── datasheet
    │   ├── SPEC
    │   ├── docking standard
    │   └── development manual
    ├── include
    │      ├── hal_adc.h
    │      ├── hal_battery.h
    │      ├── hal_flash.h
    │      ├── hal_gpio.h
    │      ├── hal_i2c.h
    │      ├── hal_pwm.h
    │      ├── hal_spi.h
    │      ├── hal_systick_timer.h
    │      ├── hal_timer.h
    │      ├── hal_uart.h
    │      ├── tuya_app_timer.h
    │      ├── tuya_mcu_os.h
    │      ├── tuya_mf_test.h
    │      ├── tuya_oem_kit.h
    │      ├── tuya_tools.h
    │      ├── tuya_zigbee_modules.h
    │      ├── tuya_zigbee_sdk.h
    │      ├── tuya_zigbee_stack.h
    │      ├── type_def.h
    │      ├── zigbee_attr.h
    │      ├── zigbee_cmd.h
    │      ├── zigbee_dev_template.h
    │      ├── zigbee_modules.h
    │      └── zigbee_raw_cmd_api.h
    ├── lib
    │   ├── gcc_lib
    │   └── iar_lib
    ├── sdk
    │   ├── platform
    │   └── tool
    └── tools
        ├── gcc-arm-none-eabi-9-2019-q4-major
        ├── gcc-arm-none-eabi-9-2019-q4-major.tar.bz2
        ├── gcc_boot
        └── iar_tools

Directory Introduction

SDK Overall process

User-implemented function interface

dev_power_on_init()

/**
 * @note (MUST) This is the first function after the hardware starts.
 * The CPU and base clock are initialized before calling this function.
 * You need to implement the creation of Zigbee devices and 
 * determine the parameters of Zigbee device behavior.
 * Include device roles(router, end device), device networking(join), 
 * rejoin parameters, and more. Refer to the TUYA Zigbee SDK demo for details.
 * @param none
 * @return none
 */
void dev_power_on_init(void);

Instructions

Zigbee SDK will call this function according to the system flowchart,Users need to at least create Zigbee devices,network parameter setting。

Sample code snippet

void dev_power_on_init(void)
{
    join_config_t cfg;
    zg_dev_config_t zg_dev_config;
    
    /**
    Register Zigbee Basic,Including information such as endpoint, cluster, attributes, etc.
    */
    dev_register_zg_ep_infor((dev_description_t *)g_dev_des, EP_SUMS);

    memset(&zg_dev_config, 0, sizeof(zg_dev_config_t));
    zg_dev_config.dev_type = ZG_ROUTER;
    dev_register_zg_dev_config(&zg_dev_config);

    memset(&cfg, 0, sizeof(cfg));
    cfg.auto_join_power_on_flag = TRUE;
    cfg.auto_join_remote_leave_flag = TRUE;
    cfg.join_timeout = ZIGBEE_JOIN_MAX_TIMEOUT;
    dev_zg_join_config(&cfg);

    //TODO: others task
    return;
}

dev_system_on_init()

/**
 * @note (MUST) This is the first function after system startup. 
 * Before calling this function, Zigbee stack and some basic 
 * components have been started. You can use all API except individual ones. 
 * API limits refer to the API limits description table
 * @param none
 * @return none
 */
void dev_system_on_init(void);

Instructions Zigbee SDK will call this function according to the system flowchart,Users can realize Zigbee attribute operation, hardware initialization, software timing processing and other initialization. Sample code snippet

static void __uart_rx_callback(uint8_t *data, uint16_t len)
{
    //TODO: Serial port receiving and processing
}

static void __dev_evt_callback(uint8_t evt)
{
    switch(evt) {
        case EVT_LOCK_CANCEL: {
            //TODO: Custom processing
            break;
        }
        
        default: {
            break;
        }
    }
}

void dev_system_on_init(void)
{
    user_uart_config_t *p_default_cfg = mf_test_uart_config();
    user_uart_config_t uart_cfg;

    memcpy(&uart_cfg, p_default_cfg, sizeof(user_uart_config_t));
    uart_cfg.func = __uart_rx_callback;
    user_uart_init(&uart_cfg);


    dev_timer_start_with_callback(EVT_LOCK_CANCEL, 1000, __dev_evt_callback);

    dev_change_power(11, 19);
    return;
}

nwk_state_changed_callback()

/**
 * @note (MUST) This function is invoked when the network state changes.
 * Handling network-related matters at this function is recommended.
 * @param[in] {state} Refer to NET_EVT_T for more detal.
 * @return none
 */
void nwk_state_changed_callback(NET_EVT_T state);

Instructions Zigbee SDK will call this function according to the system flowchart,Users can implement Zigbee network status processing. Sample code snippet

void nwk_state_changed_callback(NET_EVT_T state)
{
    switch(state) {
        case NET_POWER_ON_LEAVE: {
            //TODO: No network status after power on
            break;
        }
        case NET_JOIN_START: {
            //TODO: Start networking
            break;
        }
        case NET_JOIN_TIMEOUT: {
            //TODO: Start networking
            break;
        }
        case NET_POWER_ON_ONLINE: {
            //TODO: Power up has network status
            break;
        }
        case NET_JOIN_OK: {
            //TODO: Successful networking
            break;
        }
        case NET_REJOIN_OK: {
            //TODO: Reconnect successful
            break;
        }
        case NET_LOST: {
            //TODO: Lost with parent
            break;
        }
        case NET_REMOTE_LEAVE: {
            //TODO: Remote off-net notification
            break;
        }
        case NET_LOCAL_LEAVE: {
            //TODO: Local off-net notification            break;
        }
        case NET_MF_TEST_LEAVE: {
            //TODO: Production Test Off-Grid Notification
            break;
        }
        default: {
            break;
        }
    }
}

Quick development guide

The SDK development kit contains documentation and examples, and the use of and attribute sample code is the only rule of quick start. It is recommended to compile, run, and network with the sample code first, and then try to modify the code to achieve personalized functions.

Example description

Document description

API call restrictions

</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_timer_start</strong></span></td>
 <td class="confluenceTd"><strong>Start a delayed execution event,If the event has set an event handler function,</strong><br /><strong>The subsequent call does not need to set the event handler function every time
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_timer_get_valid_flag</strong></span></td>
 <td class="confluenceTd"><strong>Determine whether an event is valid (that is, the event has not been reached)
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_timer_get_remaining_time</strong></span></td>
 <td class="confluenceTd"><strong>Get how many ms remain before an event is executed
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="3" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>zigbeeDevice creation</strong></span><br /><span style="color: rgb(0,128,0);"><strong> basic configuration</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_register_zg_ep_infor</strong></span></td>
 <td class="confluenceTd"><strong>Register a complete zigbee device, including an endpoint describing the zigbee device,</strong><br /><strong>cluster,attributes  etc. </strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_register_zg_dev_config</strong></span></td>
 <td class="confluenceTd"><strong>Configure the device role of zigbee, whether it is routing or end device and join, rejoin parameters</strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_zg_join_config</strong></span></td>
 <td class="confluenceTd"><strong>Configure networking policies for power-on and remote deletion</strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="2" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>Network operation</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_zigbee_join_start</strong></span></td>
 <td class="confluenceTd"><strong>Exit the network and enter the network</strong></td>
 Call after<td class="confluenceTd"><strong>dev_system_on_init ,It will involve the handling of events and network status</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_zigbee_join_stop</strong></span></td>
 <td class="confluenceTd"><strong>Stop networking early</strong></td>
 Call after<td class="confluenceTd"><strong>dev_system_on_init ,It will involve the handling of events and network status</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="6" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>zigbee 3.0 related</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_report_table_init</strong></span></td>
 <td class="confluenceTd"><strong>Configure the default report table</strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init, Called after dev_register_zg_ep_infor</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>ext_plugin_identify_client_enable</strong></span></td>
 <td class="confluenceTd"><strong>zigbee 3.0 used,Open the identify client service</strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init, Called after dev_register_zg_ep_infor</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>ext_plugin_identify_server_enable</strong></span></td>
 <td class="confluenceTd"><strong>zigbee 3.0 used,Open the identify  service
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init, Called after dev_register_zg_ep_infor</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>ext_plugin_green_power_client_enable</strong></span></td>
 <td class="confluenceTd"><strong>zigbee 3.0 used,Start green power client service</strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init, Called after dev_register_zg_ep_info</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>ext_plugin_reporting_enable</strong></span></td>
 <td class="confluenceTd"><strong>zigbee 3.0 used,Open reporting service
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init, Called after dev_register_zg_ep_infor </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>ext_plugin_register_cmd_handle</strong></span></td>
 <td class="confluenceTd"><strong>Register cluster command processing</strong></td>
 <td class="confluenceTd"><strong>Can only be called in dev_power_on_init</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="3" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>电池采集相关</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>hal_battery_config</strong></span></td>
 <td class="confluenceTd"><strong>Configuration of battery collection function</strong></td>
 <td class="confluenceTd"><strong>Need to be called after dev_system_on_init </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>hal_battery_set_battery_type</strong></span></td>
 <td class="confluenceTd"><strong>Dynamically configure battery type and whether the device wakes up frequently,</strong><br /><strong>SDK According to these parameters, the internal collection strategy will be made internally</strong></td>
 <td class="confluenceTd"><strong>call afer hal_battery_config </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>hal_battery_report_policy_config</strong></span></td>
 <td class="confluenceTd"><strong>Battery special strategy configuration</strong></td>
 <td class="confluenceTd"><strong> call after hal_battery_config </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="7" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>flash 操作</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>flash_block_raw_write</strong></span></td>
 <td class="confluenceTd"><strong>Write flash in blocks,One block is 250 bytes</strong></td>
 <td rowspan="4" class="confluenceTd"><strong>Currently only the big flash version of lib is available该函数,目前只有门锁在用</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>flash_block_raw_read</strong></span></td>
 <td class="confluenceTd"><strong>Read flash by block,One block is 250 bytes</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>flash_addr_raw_write</strong></span></td>
 <td class="confluenceTd"><strong>Write flash according to virtual address</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>flash_addr_raw_read</strong></span></td>
 <td class="confluenceTd"><strong>Write flash according to virtual address</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>user_flash_data_write</strong></span></td>
 <td class="confluenceTd"><strong>Application flash write,Leave 250 bytes of bottom flash area for APP development</strong></td>
 <td rowspan="2" class="confluenceTd"><strong>Call after dev_system_on_init 
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>user_flash_data_read</strong></span></td>
 <td class="confluenceTd"><strong>Application flash write,Leave 250 bytes of bottom flash area for APP development</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="3" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>Off-grid self-recovery network</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>nwk_disable_self_recovery_once</strong></span></td>
 <td class="confluenceTd"><strong>Disable the self-recovery function</strong></td>
 <td rowspan="3" class="confluenceTd"><strong>call after dev_system_on_init </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>nwk_enable_self_recovery_once</strong></span></td>
 <td class="confluenceTd"><strong>Enable self-recovery</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>nwk_self_recovery_manual</strong></span></td>
 <td class="confluenceTd"><strong>Self-recovery immediately</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="4" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>Get all kinds of information</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zigbee_get_net_info</strong></span></td>
 <td class="confluenceTd"><strong>Obtain device networking parameter information</strong></td>
 <td class="confluenceTd"><strong>call after dev_system_on_init </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_get_join_type</strong></span></td>
 <td class="confluenceTd"><strong>Acquisition networking method: centralized, distributed, not networking</strong></td>
 <td class="confluenceTd"><strong>call after dev_system_on_init </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_get_join_gw_type</strong></span></td>
 <td class="confluenceTd"><strong>Obtain networking gateways: Tuya gateway, other gateways, non-gateway (unnetworked / distributed)</strong></td>
 <td class="confluenceTd"><strong>call after dev_system_on_init </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_is_zll_net</strong></span></td>
 <td class="confluenceTd"><strong>Query whether it is ZLL networking</strong></td>
 <td class="confluenceTd"><strong>Call after dev_system_on_init , zll lib dedicated
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="3" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>General configuration</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_heartbeat_set</strong></span></td>
 <td class="confluenceTd"><strong>Heartbeat configuration</strong></td>
 <td class="confluenceTd"><strong>Call after  dev_system_on_init </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_change_power</strong></span></td>
 <td class="confluenceTd"><strong>Configure transmit power</strong></td>
 <td class="confluenceTd"><strong>Call afterdev_system_on_init  </strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>disable_gw_change_power</strong></span></td>
 <td class="confluenceTd"><strong>Disable the gateway to change the transmit power</strong></td>
 <td class="confluenceTd"><strong>Called in dev_power_on_init</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="4" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>poll 相关</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_poll_interval_change</strong></span></td>
 <td class="confluenceTd"><strong>Change poll interval</strong></td>
 <td rowspan="4" class="confluenceTd"><strong>Called in dev_register_zg_dev_config</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_poll_start</strong></span></td>
 <td class="confluenceTd"><strong>The call will always be poll</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_poll_end</strong></span></td>
 <td class="confluenceTd"><strong>Stop generating new polls,Stop after sending the remaining polls</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>zg_poll_clear</strong></span></td>
 <td class="confluenceTd"><strong>Stop poll now ,</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
 <td class="confluenceTd"><br /></td>
</tr>
<tr>
 <td rowspan="3" class="confluenceTd"><span style="color: rgb(0,128,0);"><strong>属性,endpoint</strong></span></td>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_set_endpoint_alias</strong></span></td>
 <td class="confluenceTd"><strong>Append an alias to an endpoint ,Mainly solves the compatibility problem of endpoint processing when docking with other gateways and Tuya gateways</strong><br /><strong></strong></td>
 <td class="confluenceTd"><strong>Called after dev_register_zg_ep_infor</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_endpint_enable_disable</strong></span></td>
 <td class="confluenceTd"><strong>Dynamically disable and enable an endpoint,After disabling an endpoint,</strong><br /><strong>Remote and local cannot access the endpoint</strong></td>
 <td class="confluenceTd"><strong>call after dev_system_on_init ,It will involve the handling of events</strong></td>
</tr>
<tr>
 <td class="confluenceTd"><span style="color: rgb(0,0,255);"><strong>dev_attr_recovery</strong></span></td>
 <td class="confluenceTd"><strong>Do not use it for the time being, the PID will be restored to the default value instead of the PID value of the production test</strong></td>
 <td class="confluenceTd"><br /></td>
</tr>

Call after Call after

appendix

Glossary

Last updated