Device Reset

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

This topic describes how to implement the device reset feature that restores a device to the settings that the device had before it was paired and activated.

Background

Device reset can be triggered by hardware operation or mobile app operation. In terms of device status after a reset, there are two types of reset:

  • Normal reset (Unbind a device): Unbind a device from the cloud and retain the data stored in the cloud and the device. After the device is activated again, all the sub-devices and smart scenes associated with this device are restored.

  • Factory reset (Unbind a device and clear data): Unbind a device from the cloud and clear the data stored in the cloud and the device.

How to

If the device is reset via a mobile app, no interface needs to be called. If the device is reset locally, the application should proactively call the reset interface:

  • Normal reset (Unbind a device): tuya_iot_wf_gw_unactive
  • Factory reset (Unbind a device and clear data): tuya_iot_wf_gw_reset

When a device is reset, TuyaOS processes the internal data, without the application taking care of it. If you want to run some custom actions, call tuya_iot_reg_gw_app_cb to register the TY_GW_APP_CBS_S > gw_reset_cb callback. In the callback, you can run custom actions, such as clearing application data.

Examples

/**
 * @brief Device reset callback
 * @note You can run custom actions as needed in the callback.
 *
 * @param[in] type The reset type.
 */
VOID __gw_reset_cb(GW_RESET_TYPE_E type)
{
    if (type == GW_LOCAL_UNACTIVE) { // Call the local normal reset (unbind device) interface.
        // TODO
    } else if (type == GW_LOCAL_RESET_FACTORY) { // Call the local factory reset (unbind device and clear data) interface.
        // TODO
    } else if (type == GW_REMOTE_UNACTIVE) { // Mobile app performs normal reset (unbind device).
        // TODO
    } else if (type == GW_REMOTE_RESET_FACTORY) { // Mobile app performs factory reset (unbind device and clear data).
        // TODO
    } else if (type == GW_RESET_DATA_FACTORY) { // Reset data on activation.
        /**
         * If the mobile app performs a factory reset (unbind device and clear data) or the user uses a different app account to pair the device, 
         * this type is received on activation and the device cannot restart.
         */
    	// TODO
    }

    if (type != GW_RESET_DATA_FACTORY) {
    	tal_system_reset();
    }
}

// Register the reset callback on initialization.
int test_init()
{
    TY_GW_APP_CBS_S gw_app_cbs = {
        .gw_reset_cb = __gw_reset_cb,
    };

    tuya_iot_reg_gw_app_cb(&gw_app_cbs);
}

// Local normal reset.
void test_local_unactive()
{
    TUYA_CALL_ERR_LOG(tuya_iot_wf_gw_unactive());
}

// Local factory reset.
void test_local_reset()
{
    TUYA_CALL_ERR_LOG(tuya_iot_wf_gw_reset());
}

Data type

GW_RESET_TYPE_E

typedef enum {
    GW_LOCAL_RESET_FACTORY = 0,    // Local factory reset.
    GW_REMOTE_UNACTIVE,            // Normal reset via mobile app (unbind device)
    GW_LOCAL_UNACTIVE,             // Local normal reset (unbind device).
    GW_REMOTE_RESET_FACTORY,       // Factory reset via mobile app.
    GW_RESET_DATA_FACTORY,         // Reset data on activation.
} GW_RESET_TYPE_E;

Callback

GW_APP_RESET_CB

/**
 * @brief reset callback function
 *
 * @param[in] type reset type
 *
 * @return VOID
 */
typedef VOID (*GW_APP_RESET_CB)(GW_RESET_TYPE_E type);

API description

Normal reset (Unbind a device)

/**
 * @brief local inactive
 *
 * @return OPRT_OK on success. For others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_iot_wf_gw_unactive(VOID);

Factory reset (Unbind a device and clear data)

/**
 * @brief local reset factory
 *
 * @return OPRT_OK on success. For others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_iot_wf_gw_reset(VOID);

Register application callbacks

/**
 * @brief register application callback functions
 *
 * @param[in] cbs callback functions, see TY_GW_APP_CBS_S
 *
 * @return OPRT_OK on success. For others on error, please refer to tuya_error_code.h   
 */
OPERATE_RET tuya_iot_reg_gw_app_cb(CONST TY_GW_APP_CBS_S *cbs);