Last Updated on : 2024-07-24 07:06:00download
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.
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.
tuya_iot_wf_gw_unactive
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.
#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
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
break;
case GW_LOCAL_UNACTIVE: //Local reset
//Delete the DB file
//You implement business logic yourself, such as lighting effects and sounds
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
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());
return rt;
}
//Local factory reset
OPERATE_RET test_local_reset(void)
{
OPERATE_RET rt = OPRT_OK;
TUYA_CALL_ERR_LOG(tuya_iot_wf_gw_reset());
return rt;
}
/* 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;
/**
* @brief tuya_iot_wf_gw_unactive
*
* @return OPERATE_RET
*/
OPERATE_RET tuya_iot_wf_gw_unactive(VOID);
/**
* @brief tuya_iot_wf_gw_reset
*
* @return OPERATE_RET
*/
OPERATE_RET tuya_iot_wf_gw_reset(VOID);
You can handle this issue in the following two ways and pair the device again. You will get a new device ID.
tuya_user.db
and tuya_user.db_bak
files.tuya_user.db
and tuya_user.db_bak
files locally.Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback