Local Management

Last Updated on : 2023-09-06 10:40:14download

Local access allows for access to the device list and device-reported data as well as local control.

Data type

Device information struct

/**
 * @brief Sub-device information struct.
 */
typedef struct {
    CHAR_T id[DEV_ID_LEN+1]; ///< The unique identifier of the device.
    CHAR_T sw_ver[SW_VER_LEN+1]; ///< The firmware version of the device.
    CHAR_T schema_id[SCHEMA_ID_LEN+1]; ///< The ID of the things data model for the device.
    CHAR_T product_key[PRODUCT_KEY_LEN+1]; ///< The product ID of the device.
    CHAR_T firmware_key[PRODUCT_KEY_LEN+1]; ///< The firmware key of the device.
    BOOL_T is_oem; ///< Specifies whether this is an OEM device.
    #if defined(ENABLE_SIGMESH) && (ENABLE_SIGMESH==1)
    CHAR_T sigmesh_dev_key[SIGMESH_DEV_KEY_LEN+1]; ///< The login key of the Bluetooth mesh sub-device.
    CHAR_T sigmesh_mac[SIGMESH_DEV_MAC_LEN+1]; ///< The MAC address of the Bluetooth mesh sub-device.
    #endif
    CHAR_T virt_id[DEV_ID_LEN+1]; ///< The virtual ID of the device.
    USER_DEV_DTL_DEF_T uddd; ///< Device description 1
    USER_DEV_DTL_DEF_T uddd2; ///< Device description 2
    DEV_TYPE_T tp; ///< The type of the device. 1: Bluetooth device. 2: Zigbee device.
    DEV_SUB_TYPE_T sub_tp; ///< The subtype of the device. Currently, this parameter applies to Bluetooth devices only. 0: Bluetooth mesh. 1: Bluetooth Low Energy. 2: Bluetooth beacon.
    CHAR_T uuid[DEV_UUID_LEN+1]; ///< UUID
    INT_T abi; ///< The capability value of the device.
    BOOL_T bind; ///< Specifies whether the device has been bound. TRUE: Bound. FALSE: Not bound.
    BOOL_T sync; ///< Specifies whether device binding or unbinding has been synchronized with the cloud. TRUE: Synchronized. FALSE: Not synchronized.
    #if defined(ENABLE_SIGMESH) && (ENABLE_SIGMESH==1)
    BOOL_T sigmesh_sync; ///< Specifies whether Bluetooth mesh sub-device binding or unbinding has been synchronized with the cloud. TRUE: Synchronized. FALSE: Not synchronized.
    BOOL_T ble_mesh_bind_rept_sync; ///< Ignore this field.
    #endif
    BOOL_T bind_status; ///< Ignore this field.
    BYTE_T attr_num; ///< The length of the array of device properties.
    GW_ATTACH_ATTR_T attr[GW_ATTACH_ATTR_LMT]; ///< The array of device properties.
    BOOL_T reset_flag; ///< The flag of factory reset.
    #if defined(TUYA_OPERATOR_TYPE) && (TUYA_OPERATOR_TYPE != 0)
    CH_CODE_ST ch_dminfo; ///< Ignore this field.
    #endif
	CHAR_T subList_flag; ///< Ignore this field.
    #if ((!defined(DISABLE_ZIGBEE_LQI)) || (defined(DISABLE_ZIGBEE_LQI) && (DISABLE_ZIGBEE_LQI == 0)))
    DEV_QOS_ST dev_qos; ///< Ignore this field.
    #endif
    CHAR_T schema_flag; ///< Ignore this field.
    CHAR_T schema_sync; ///< Ignore this field.
    CHAR_T schema_ver[SCHEMA_VER_LEN+1]; ///< Ignore this field.
    UINT_T dev_attr; ///< Ignore this field.
    #if defined(ENABLE_SIGMESH) && (ENABLE_SIGMESH==1)
    TY_SUBDEV_BIND_OPTIONS options; ///< Ignore this field.
    #endif
} DEV_DESC_IF_S;

Data point (DP) data struct

/**
 * @brief DP data struct.
 */
typedef struct {
    BYTE_T dpid;             ///< DP ID
    DP_PROP_TP_E type;       ///< DP type
    TY_OBJ_DP_VALUE_U value; ///< DP value
    UINT_T time_stamp;       ///< Timestamp
} TY_OBJ_DP_S;

/**
 * @brief DP value union
 */
typedef union {
    INT_T dp_value;   ///< DP type is PROP_VALUE. This field is valid.
    UINT_T dp_enum;   ///< DP type is PROP_ENUM. This field is valid.
    CHAR_T *dp_str;   ///< DP type is PROP_STR. This field is valid.
    BOOL_T dp_bool;   ///< DP type is PROP_BOOL. This field is valid.
    UINT_T dp_bitmap; ///< DP type is PROP_BITMAP. This field is valid.
} TY_OBJ_DP_VALUE_U;

Control command struct

/**
 * @brief The struct of object-type control commands.
 * @note An object-type control command can contain multiple DPs.
 */
typedef struct {
    DP_CMD_TYPE_E cmd_tp;   ///< The source of the message. 0: LAN. 1: Cloud. 2: Schedule. 3: Linkage. 4: Retransmission. Populate this field with 0 for local control.
    DP_TRANS_TYPE_T dtt_tp; ///< The type of the message. 0: Unicast. 1: Broadcast. 2: Multicast. 3: Linkage.
    CHAR_T *cid;            ///< The unique identifier of the device, corresponding to DEV_DESC_IF_S->id.
    CHAR_T *mb_id;          ///< When dtt_tp is multicast, this field is valid, indicating the group ID.
    UINT_T dps_cnt;         ///< The length of DP data.
    TY_OBJ_DP_S dps[0];     ///< DP data.
} TY_RECV_OBJ_DP_S;

/**
 * @brief The struct of raw-type control commands.
 * @note A raw-type control command can contain only one DP.
 */
typedef struct {
    DP_CMD_TYPE_E cmd_tp;   ///< The source of the message. 0: LAN. 1: Cloud. 2: Schedule. 3: Linkage. 4: Retransmission. Populate this field with 0 for local control.
    DP_TRANS_TYPE_T dtt_tp; ///< The type of the message. 0: Unicast. 1: Broadcast. 2: Multicast. 3: Linkage.
    CHAR_T *cid;            ///< The unique identifier of the device, corresponding to DEV_DESC_IF_S->id.
    BYTE_T dpid;            ///< DP ID.
    CHAR_T *mb_id;          ///< When dtt_tp is multicast, this field is valid, indicating the group ID.
    UINT_T len;             ///< The length of raw data.
    BYTE_T data[0];         ///< Raw data.
}TY_RECV_RAW_DP_S;

API description

Allow sub-devices to join network

/**
 * @brief Allow a sub-device to join the network.
 * @note Turn on or off the pairing mode of the gateway's module.
 *
 * @param[in] tp The type of the sub-device. If you do not distinguish between sub-devices, set it to 255.
 * @param[in] permit Specifies whether to turn on pairing mode. TRUE: Turn on. FALSE: Turn off.
 * @param[in] timeout The timeout period for pairing. This parameter is valid only when permit is TRUE.
 */
OPERATE_RET tuya_iot_dev_join_permit(GW_PERMIT_DEV_TP_T tp, BOOL_T permit, UINT_T timeout);

Get sub-device information

/**
 * @brief Get the device information about a specified sub-device.
 *
 * @param[in] dev_id The unique identifier of the sub-device, which can be obtained through the sub-device list traversal interface.
 *
 * @return (DEV_DESC_IF_S *): The sub-device information. NULL: The sub-device is not found.
 */
DEV_DESC_IF_S *tuya_iot_get_dev_if(IN CONST CHAR_T *dev_id);

Traverse the list of sub-devices

/**
 * @brief Traverse the sub-device list.
 * @note Get the device information about all the sub-devices connected to the gateway.
 *
 * @param[out] iterator The iterator.
 *
 * @return (DEV_DESC_IF_S *): The sub-device information. NULL: Traversal completed.
 */
DEV_DESC_IF_S *tuya_iot_dev_traversal(INOUT VOID **iterator);

Get the data to report

/**
 * @brief The struct of data reporting callbacks.
 */
typedef struct {
    VOID (*obj_dp_rept_cb)(CONST CHAR_T *dev_id, CONST TY_OBJ_DP_S *dps, CONST UINT_T dp_cnt); ///< Object-type DP
    VOID (*raw_dp_rept_cb)(CONST CHAR_T *dev_id, BYTE_T dpid, CONST BYTE_T *data, UINT_T len); ///< Raw-type DP
} TY_DP_REPORT_CBS_S;

/**
 * @brief Register data reporting callbacks.
 * @note The gateway gets the data reported by the sub-device through a callback. The callback is invoked when the sub-device reports data. The data complies with the Tuya-defined DP model.
 *
 * @param[in] cbs The callback interface.
 */
OPERATE_RET tuya_iot_reg_dp_report_cb(TY_DP_REPORT_CBS_S *cbs);

Send control command

/**
 * @brief Send object-type control commands.
 * @note The control command complies with the Tuya-defined DP model.
 *
 * @param[in] cmd The object-type control command.
 */
OPERATE_RET tuya_iot_dev_obj_cmd_send(CONST TY_RECV_OBJ_DP_S *cmd);

/**
 * @brief Send raw-type control command.
 * @note The control command complies with the Tuya-defined DP model.
 *
 * @param[in] cmd Raw-type control command.
 */
OPERATE_RET tuya_iot_dev_raw_cmd_send(CONST TY_RECV_RAW_DP_S *cmd);

Example

Get sub-device information

VOID test_subdev_info(VOID)
{
	DEV_DESC_IF_S *dev_if = NULL;

	dev_if = tuya_iot_get_dev_if("112233aabbcc");
	if (dev_if == NULL) {
		PR_ERR("dev is not found");
		return;
	}
	PR_DEBUG("id: %s, type: %d, pid: %s, version: %s", dev_if->id, dev_if->tp, \
	                                                   dev_if->product_key, dev_if->sw_ver);
}

Traverse the list of sub-devices

VOID test_subdev_list(VOID)
{
    DEV_DESC_IF_S *dev_if = NULL;
    VOID *iterator = NULL;

    do {
        dev_if = tuya_iot_dev_traversal(&iterator);
        if (dev_if) {
            PR_DEBUG("id: %s, type: %d", dev_if->id, dev_if->tp);
        }
    } while(dev_if);
}

Get the data to report

STATIC VOID __obj_dp_rept_cb(CONST CHAR_T *dev_id, CONST TY_OBJ_DP_S *dps, CONST UINT_T dp_cnt)
{
    PR_DEBUG("report data, dev_id: %s", dev_id);

    for (INT_T i = 0; i < dp_cnt; i++) {
        PR_DEBUG("dpid: %d", dps[i].dpid);
        switch (dps[i].type) {
        case PROP_BOOL:
            PR_DEBUG("dp_bool value: %d", dps[i].value.dp_bool);
            break;
        case PROP_VALUE:
            PR_DEBUG("dp_value value: %d", dps[i].value.dp_value);
            break;
        case PROP_ENUM:
            PR_DEBUG("dp_enum value: %d", dps[i].value.dp_enum);
            break;
        case PROP_STR:
            PR_DEBUG("dp_str value: %s", dps[i].value.dp_str);
            break;
        }
    }
}

VOID test_subdev_report(VOID)
{
    OPERATE_RET op_ret = OPRT_OK;

    TY_DP_REPORT_CBS_S __dp_report_cbs = {
        .obj_dp_rept_cb = __obj_dp_rept_cb,
    };

    op_ret = tuya_iot_reg_dp_report_cb(&__dp_report_cbs);
    if (op_ret != OPRT_OK) {
        PR_ERR("tuya_iot_reg_dp_report_cb err: %d", op_ret);
        return;
    }
}

Send control command

VOID test_subdev_cmd(VOID)
{
    UINT_T dp_cnt = 3;
    TY_RECV_OBJ_DP_S *obj_cmd = NULL;

    obj_cmd = Malloc(SIZEOF(TY_RECV_OBJ_DP_S) + dp_cnt * SIZEOF(TY_OBJ_DP_S));
    if (obj_cmd == NULL) {
        PR_ERR("Malloc error");
        return;
    }

    obj_cmd->cid = "112233aabbcc";
    obj_cmd->cmd_tp = DP_CMD_LAN;
    obj_cmd->dtt_tp = DTT_SCT_UNC;
    obj_cmd->dps_cnt = dp_cnt;

    for (INT_T i = 0; i < dp_cnt; i++) {
        obj_cmd->dps[i].dpid = (i + 1);
        obj_cmd->dps[i].type = PROP_BOOL;
        obj_cmd->dps[i].value.dp_bool = TRUE;
    }

    tuya_iot_dev_obj_cmd_send(obj_cmd);

    Free(obj_cmd);
}