Device Initialization

Last Updated on : 2023-06-26 06:51:06

This topic describes the initialization process of the TuyaOS Integrated SDK in terms of initialization phases, use cases, and related APIs.

Background

Initialization is made up of a set of interfaces needed to build a minimal software model for a gateway. Initialization can be broken down into three parts by phase:

  • TuyaOS initialization: Initializes the basic services to run applications, for example, loads the database, creates job queues and timers, and initializes log management.

  • Gateway initialization: Initializes the gateway capabilities. To initialize other services during gateway initialization, three interfaces make up this initialization process.

    1. Pre-initialization: Initializes the gateway’s basic services, subscribes to events, and configures the default settings.
    2. Initialization: Starts the TuyaOS core services and notifies the service modules that TuyaOS is ready for running tasks.
    3. Startup: Runs the sub-device connection service.
  • (Optional) Services initialization: The services include Tuya services if any and the service you implement. Tuya services include Zigbee gateway capabilities, Bluetooth gateway capabilities, bulk deployment, and failover.

    Device Initialization

To build a smart gateway, TuyaOS initialization and gateway initialization are required. After that, you can connect your hardware to the Tuya IoT Development Platform as a smart gateway.

The initialization interfaces are called in this sequence: TuyaOS initialization > Gateway pre-initialization > gateway initialization > gateway startup

You can add custom service modules in different phases as needed, but you cannot adjust the interface calling sequence.

How to

During initialization, set the authorization information that is required to bind a device with the Tuya IoT Development Platform. Call the following interfaces in sequence:

  • TuyaOS initialization: tuya_iot_init
  • Setting authorization information: tuya_iot_set_gw_prod_info
  • Gateway pre-initialization: tuya_iot_sdk_pre_init
  • Gateway initialization: tuya_iot_wr_wf_sdk_init
  • Gateway startup: tuya_iot_sdk_start

Example

#include "tuya_iot_com_api.h"
#include "tuya_iot_base_api.h"
#include "tuya_cloud_wifi_defs.h"
#include "tuya_gw_com_defs.h"
#include "tuya_iot_sdk_api.h"

#include "uni_log.h"
#include "tuya_hal_system.h"

#define M_STORAGE_PATH "./"
#define M_UUID         "my-uuid" /* Replace the value of UUID with your own UUID. */
#define M_AUTHKEY      "my-key"  /* Replace the value of AUTHKEY with your own AuthKey. */
#define M_PID          "my-pid"  /* Replace the value of PID with the PID of the gateway product created on the Tuya IoT Development Platform. */
#define M_SW_VERSION   "1.0.0"

int main(int argc, char **argv)
{
    OPERATE_RET rt = OPRT_OK;
    GW_PROD_INFO_S prod_info = {
        .uuid     = M_UUID,
        .auth_key = M_AUTHKEY,
    };

    /* TuyaOS initialization */
    TUYA_CALL_ERR_RETURN(tuya_iot_init(M_STORAGE_PATH));

    /* Set authorization information */
    TUYA_CALL_ERR_RETURN(tuya_iot_set_gw_prod_info(&prod_info));

    /* Pre-initialize gateway */
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_pre_init(TRUE));

    /* Initialize gateway */
    TUYA_CALL_ERR_RETURN(tuya_iot_wr_wf_sdk_init(IOT_GW_NET_WIRED_WIFI, GWCM_OLD, WF_START_AP_ONLY, M_PID, M_SW_VERSION, NULL, 0));

    /* Start gateway */
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_start());

    while (1) {
        tuya_hal_system_sleep(10*1000);
    }

    return OPRT_OK;
}

Data type

GW_PROD_INFO_S

/**
 * @brief Definition of product info
 */
typedef struct {
    CHAR_T *uuid;        // strlen(uuid) <= 16, must not be null
    CHAR_T *auth_key;    // strlen(auth_key) <= 32, must not be null
} GW_PROD_INFO_S;

IOT_GW_NET_TYPE_T

/**
 * @brief Gateway network mode
 */
typedef BYTE_T IOT_GW_NET_TYPE_T;
#define IOT_GW_NET_WIRED       0   /**< only support wried */
#define IOT_GW_NET_WIFI        1   /**< only support Wi-Fi */
#define IOT_GW_NET_WIRED_WIFI  2   /**< support wired and Wi-Fi */

GW_WF_CFG_MTHD_SEL

/**
 * @brief Wi-Fi work mode
 */
typedef BYTE_T GW_WF_CFG_MTHD_SEL;  // Wi-Fi config method select
#define GWCM_OLD                0   // do not have low power mode
#define GWCM_LOW_POWER          1   // with low power mode
#define GWCM_SPCL_MODE          2   // special with low power mode
#define GWCM_OLD_PROD           3   // GWCM_OLD mode with product
#define GWCM_LOW_POWER_AUTOCFG  4   // with low power mode && auto cfg
#define GWCM_SPCL_AUTOCFG       5   // special with low power mode && auto cfg

GW_WF_START_MODE

/**
 * @brief Wi-Fi pairing mode
 */
typedef BYTE_T GW_WF_START_MODE;
#define WF_START_AP_ONLY        0   // only have ap-cfg mode
#define WF_START_SMART_ONLY     1   // only have smart-cfg mode
#define WF_START_AP_FIRST       2   // have both ap-cfg and smart-cfg. default is ap-cfg mode
#define WF_START_SMART_FIRST    3   // have both ap-cfg and smart-cfg. default is smart-cfg mode
#define WF_START_SMART_AP_CONCURRENT    4   //  ap-cfg and smart-cfg is concurrent

GW_ATTACH_ATTR_T

/**
 * @brief Definition of attach module attribute
 */
typedef struct {
    /** attach OTA channel */
    GW_PERMIT_DEV_TP_T tp;
    /** attach version, format xx.xx.xx */
    CHAR_T ver[SW_VER_LEN + 1];
    CHAR_T md5[SW_MD5_LEN + 1];
} GW_ATTACH_ATTR_T;

API description

TuyaOS initialization

/**
 * @brief tuya_iot_init_params
 * @desc init tuya_iot_sdk
 *
 * @param[in] fs_storge_path: filesystem read/write storage path
 *            (if os have no fs, then fs_storge_path is invalid)
 * @param[in] p_param: custom init params
 *
 * @return OPRT_OK: success  Other: fail
 */
OPERATE_RET tuya_iot_init_params(IN CONST CHAR_T *fs_storge_path, IN CONST TY_INIT_PARAMS_S *p_param);

#define tuya_iot_init(fs_storge_path) tuya_iot_init_params(fs_storge_path, NULL)

Setting authorization information

/**
 * @brief tuya_iot_set_gw_prod_info
 * @desc set tuya-sdk product info (wired version)
 *
 * @param prod_info: tuya-sdk product info
 *
 * @return OPRT_OK: success  Other: fail
 */
OPERATE_RET tuya_iot_set_gw_prod_info(IN CONST GW_PROD_INFO_S *prod_info);

Gateway pre-initialization

/**
 * @brief Preinitializes the SDK
 *
 * @param[in] is_gw TRUE: enable gateway feature, FALSE: disable gateway feature
 *
 * @return OPRT_OK on success. For others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_iot_sdk_pre_init(BOOL_T is_gw);

Gateway initialization

/**
 * @brief Initializes the SDK, both wired and Wi-Fi commissioning is supported
 *
 * @param[in] net_mode    commissioning mode, see IOT_GW_NET_TYPE_T
 * @param[in] cfg         Wi-Fi work mode, see GW_WF_CFG_MTHD_SEL
 * @param[in] start_mode  Wi-Fi start mode, see GW_WF_START_MODE
 * @param[in] product_key product key or product id
 * @param[in] sw_ver      firmware version, string format: xx.xx.xx
 * @param[in] attr        gw attach protocol list
 * @param[in] attr_num    gw attach protocol num
 *
 * @return OPRT_OK on success. For others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_iot_wr_wf_sdk_init(IN CONST IOT_GW_NET_TYPE_T net_mode, \
                                    IN CONST GW_WF_CFG_MTHD_SEL cfg, IN CONST GW_WF_START_MODE start_mode, \
                                    IN CONST CHAR_T *product_key, IN CONST CHAR_T *sw_ver, \
                                    IN CONST GW_ATTACH_ATTR_T *attr, IN CONST UINT_T attr_num);

Gateway startup

/**
 * @brief Start the SDK, which should be called at the last
 *
 * @return OPRT_OK on success. For others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_iot_sdk_start(VOID);