设备初始化

更新时间:2024-06-25 06:15:50下载pdf

本文主要介绍 TuyaOS 综合 SDK 的初始化流程,先阐述各阶段初始化的概念,然后介绍初始化的使用,最后介绍初始化相关的 API。

背景信息

初始化不是一个独立的接口,而是由一组接口组成,它们共同构成了网关的最小软件模型。按初始化阶段划分,可以把初始化分为以下三类:

  • TuyaOS 初始化:初始化底层业务,为上层应用提供基础服务,如加载存储数据库、创建工作队列和定时器、初始化日志管理等。

  • 网关初始化:初始化网关能力,为了能够在网关初始化的过程中插入其他业务的初始化,网关初始化被拆分成以下三个接口:

    1. 预初始化:该阶段主要初始化网关的基础业务、订阅事件和配置网关默认能力值等。
    2. 初始化:该阶段主要启动 TuyaOS 核心业务,并通知各业务模块 TuyaOS 已就绪,可以开始执行各自的任务。
    3. 启动:该阶段主要运行网关子设备接入业务。
  • 业务初始化(可选):业务既包含涂鸦的业务(如果有使用的话),也包含您实现的业务。其中,涂鸦业务有 Zigbee 网关能力、蓝牙网关能力、施工部署、故障替换等,具体业务可以参考对应的开发指南。

    设备初始化

开发智能网关必须使用 TuyaOS 初始化网关初始化,初始化完成就可以把硬件设备作为智能网关加入到 涂鸦开发者平台

初始化接口要求遵循的接口调用顺序:TuyaOS 初始化 > 网关预初始化 -> 网关初始化 -> 网关启动

您可以根据实际产品需求在初始化的各阶段插入自定义的业务模块,但是不能改变接口调用顺序。

使用方法

设备绑定到涂鸦开发者平台要用到授权信息,因此,需要在初始化时设置设备的授权信息。依次调用以下接口:

  • TuyaOS 初始化:tuya_iot_init
  • 设置授权信息:tuya_iot_set_gw_prod_info
  • 网关预初始化:tuya_iot_sdk_pre_init
  • 网关初始化:tuya_iot_wr_wf_sdk_init
  • 网关启动:tuya_iot_sdk_start

使用示例

#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" /* 修改成在开发者平台申请的 UUID  */
#define M_AUTHKEY      "my-key"  /* 修改成在开发者平台申请的 KEY */
#define M_PID          "my-pid"  /* 修改成在开发者平台创建的产品 ID */
#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 初始化 */
    TUYA_CALL_ERR_RETURN(tuya_iot_init(M_STORAGE_PATH));

    /* 设置授权信息 */
    TUYA_CALL_ERR_RETURN(tuya_iot_set_gw_prod_info(&prod_info));

    /* 网关预初始化 */
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_pre_init(TRUE));

    /* 网关初始化 */
    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));

    /* 网关启动 */
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_start());

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

    return OPRT_OK;
}

数据类型

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 wifi */
#define IOT_GW_NET_WIRED_WIFI  2   /**< support wired and wifi */

GW_WF_CFG_MTHD_SEL

/**
 * @brief WiFi work mode
 */
typedef BYTE_T GW_WF_CFG_MTHD_SEL;  // wifi 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 WiFi 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 说明

TuyaOS 初始化

/**
 * @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)

设置授权信息

/**
 * @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);

网关预初始化

/**
 * @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);

网关初始化

/**
 * @brief Initializes the SDK, both of wired and WiFi commissioning is supported
 *
 * @param[in] net_mode    commissioning mode, see IOT_GW_NET_TYPE_T
 * @param[in] cfg         WiFi work mode, see GW_WF_CFG_MTHD_SEL
 * @param[in] start_mode  WiFi 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);

网关启动

/**
 * @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);