Device Reset

Last Updated on : 2024-09-13 07:26:30download

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 information

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 (device unbound): 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 data of this device is restored.

  • Factory reset (device unbound with data erased): Unbind a device from the cloud and clear the data stored in the cloud and the device. You need to delete the local DB file manually.

Implementation

  • 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 with data erased): tuya_iot_wf_gw_reset

The ty_sdk_app_reset_cb callback function is registered in TuyaOS SDK to receive reset types. As long as the callback function is registered, the TuyaOS SDK will trigger the above callback function regardless of which reset method is used. You can perform customized behavior in this callback function. For example, clear the application data and delete tuya_user.db and tuya_user.db_bak files.

Example


#include <string.h>
#include <stdio.h>
#include "tuya_iot_wifi_api.h"

/**
 * @brief  The callback on the adaptation layer to invoke when the device is removed
 * @param [GW_RESET_TYPE_E] type -> Reason for reset
 * @return [*]
 */
VOID ty_sdk_app_reset_cb(GW_RESET_TYPE_E type)
{
    printf(" gw reset success. please restart the %d\n", type);
    switch (type) {
        case GW_LOCAL_RESET_FACTORY:     //Factory reset
             //Delete the DB file and clear necessary data
             //Finally, the device needs to be restarted to enter pairing mode
            break;
        case GW_REMOTE_RESET_FACTORY:   //The app sends instructions to remove the device and clear data
            //Delete the DB file and clear necessary data
            //Finally, the device needs to be restarted to enter pairing mode
            break;
        case GW_LOCAL_UNACTIVE:   //Local reset
            //Delete the DB file
            //You implement business logic yourself, such as lighting effects and sounds
            //Finally, the device needs to be restarted to enter pairing mode
            break;
        case GW_REMOTE_UNACTIVE: //Reset the device using the mobile app
            //Delete the DB file
            //You implement business logic yourself, such as lighting effects and sounds
            //Finally, the device needs to be restarted to enter pairing mode
            break;
        case GW_RESET_DATA_FACTORY://Reset the data during activation
        /**
         * When the app performs unbinding and clears data to reset the device, or the re-paired app account is not the same home account as the original app account, then
         * This type will be received during activation, and the device cannot be restarted
         * You can implement business logic
         */
        break;
        default:
            break;
    }
    return;
    /***Based on the reset type received, you send it out as an event and create an independent task for processing. Do not perform complex tasks in callbacks****/
}
   /**
* @brief main initialization
* @param [*]
* @return [*]
*/
OPERATE_RET main(void)
{
    TY_IOT_CBS_S iot_cbs = {0};
    iot_cbs.gw_reset_cb = ty_sdk_app_reset_cb; // Register the callback for removing the app from pairing to TuyaOS SDK.
    /****For specific implementation, refer to Demo*****/
   /****For specific implementation, refer to Demo*****/
   return 0;
}

//Local normal reset
 OPERATE_RET test_local_unactive(void)
{
    OPERATE_RET rt = OPRT_OK;
    TUYA_CALL_ERR_LOG(tuya_iot_wf_gw_unactive()); //You can use a key combination to remove the device. This interface will not clear the cloud information.
    return rt; //After pairing again, the device ID remains unchanged, and the cleaning records and floor maps still exist.
}

//Local factory reset
 OPERATE_RET test_local_reset(void)
{
    OPERATE_RET rt = OPRT_OK;
    TUYA_CALL_ERR_LOG(tuya_iot_wf_gw_reset()); //You can use a key combination to restore the device to its original state. This interface will clear the device information saved in the cloud.
    return rt; //For example, cleaning records, floor maps, and device IDs. Note that the relevant local information should also be cleared.
}

Data type

GW_RESET_TYPE_E

/* tuya sdk gateway reset type */
typedef enum {
    GW_LOCAL_RESET_FACTORY = 0,
    GW_REMOTE_UNACTIVE,
    GW_LOCAL_UNACTIVE,
    GW_REMOTE_RESET_FACTORY,
    GW_RESET_DATA_FACTORY, //need clear local data when active
} GW_RESET_TYPE_E;

API description

Local normal reset (Unbind a device)

/**
 * @brief tuya_iot_wf_gw_unactive
 *
 * @return OPERATE_RET
 */
OPERATE_RET tuya_iot_wf_gw_unactive(VOID);

Local factory reset (Unbind a device with data erased)

/**
 * @brief tuya_iot_wf_gw_reset
 *
 * @return OPERATE_RET
 */
OPERATE_RET tuya_iot_wf_gw_reset(VOID);

FAQs

How to change the device ID?

You can handle this issue in the following two ways and pair the device again. You will get a new device ID.

  • Through the mobile app, send a command to unbind the device with data erased. After the device receives the command, you need to delete the tuya_user.db and tuya_user.db_bak files.
  • To perform a local factory reset, you also need to delete the tuya_user.db and tuya_user.db_bak files locally.