2929#include "tuya_iot_dp.h"
3030#include "tuya_register_center.h"
3131#include "tuya_tls.h"
32+ #include "tuya_lan.h"
33+ #if defined(ENABLE_WIFI ) && (ENABLE_WIFI == 1 )
34+ #include "netcfg.h"
35+ #endif
3236#include "netmgr.h"
3337#include "tuya_health.h"
38+ #if defined(ENABLE_BLUETOOTH ) && (ENABLE_BLUETOOTH == 1 )
39+ #include "ble_mgr.h"
40+ #endif
3441typedef enum {
3542 STATE_IDLE ,
3643 STATE_START ,
@@ -57,6 +64,8 @@ static tuya_iot_client_t *s_iot_client_solo;
5764/* -------------------------------------------------------------------------- */
5865/* Internal utils functions */
5966/* -------------------------------------------------------------------------- */
67+ static int tuya_iot_token_activate_evt (void * data );
68+ static OPERATE_RET __tuya_iot_link_type_change_cb (void * data );
6069
6170static int iot_dispatch_event (tuya_iot_client_t * client )
6271{
@@ -722,6 +731,60 @@ int tuya_iot_reset(tuya_iot_client_t *client)
722731 */
723732int tuya_iot_destroy (tuya_iot_client_t * client )
724733{
734+ if (client == NULL ) {
735+ return OPRT_INVALID_PARM ;
736+ }
737+
738+ tuya_lan_disable ();
739+ #if defined(ENABLE_WIFI ) && (ENABLE_WIFI == 1 )
740+ netcfg_stop (NETCFG_TUYA_WIFI_AP );
741+ #endif
742+ #if defined(ENABLE_BLUETOOTH ) && (ENABLE_BLUETOOTH == 1 )
743+ netcfg_stop (NETCFG_TUYA_BLE );
744+ tuya_ble_deinit ();
745+ #endif
746+
747+ if (tuya_mqtt_connected (& client -> mqctx )) {
748+ tuya_mqtt_stop (& client -> mqctx );
749+ }
750+ tuya_mqtt_destory (& client -> mqctx );
751+
752+ if (client -> matop .config .mqctx && client -> matop .config .devid ) {
753+ matop_serice_destory (& client -> matop );
754+ memset (& client -> matop , 0 , sizeof (client -> matop ));
755+ }
756+
757+ netmgr_conn_set (NETCONN_WIFI , NETCONN_CMD_CLOSE , NULL );
758+ netmgr_conn_set (NETCONN_WIRED , NETCONN_CMD_CLOSE , NULL );
759+ netmgr_conn_set (NETCONN_CELLULAR , NETCONN_CMD_CLOSE , NULL );
760+
761+ if (client -> check_upgrade_timer ) {
762+ tal_sw_timer_delete (client -> check_upgrade_timer );
763+ client -> check_upgrade_timer = NULL ;
764+ }
765+
766+ if (client -> token_get .sem ) {
767+ tal_semaphore_release (client -> token_get .sem );
768+ client -> token_get .sem = NULL ;
769+ }
770+
771+ if (client -> binding ) {
772+ tal_free (client -> binding );
773+ client -> binding = NULL ;
774+ }
775+
776+ if (client -> schema ) {
777+ dp_schema_delete (client -> activate .devid );
778+ client -> schema = NULL ;
779+ }
780+
781+ tal_event_unsubscribe (EVENT_LINK_TYPE_CHG , "iot" , __tuya_iot_link_type_change_cb );
782+ tal_event_unsubscribe (EVENT_LINK_ACTIVATE , "iot" , tuya_iot_token_activate_evt );
783+
784+ client -> state = STATE_IDLE ;
785+ client -> nextstate = STATE_IDLE ;
786+ client -> is_activated = false;
787+
725788 return OPRT_OK ;
726789}
727790
@@ -1042,12 +1105,19 @@ int tuya_iot_activated_data_remove(tuya_iot_client_t *client)
10421105 return OPRT_COM_ERROR ;
10431106 }
10441107
1108+ client -> is_activated = false;
1109+ tuya_lan_disable ();
1110+ #if defined(ENABLE_WIFI ) && (ENABLE_WIFI == 1 )
1111+ netcfg_stop (NETCFG_TUYA_WIFI_AP );
1112+ #endif
1113+ #if defined(ENABLE_BLUETOOTH ) && (ENABLE_BLUETOOTH == 1 )
1114+ netcfg_stop (NETCFG_TUYA_BLE );
1115+ #endif
10451116 /* Clean client local data */
10461117 dp_schema_delete (client -> activate .devid );
10471118 tal_kv_del ((const char * )(client -> activate .schemaId ));
10481119 tal_kv_del ((const char * )(client -> config .storage_namespace ));
10491120 tuya_endpoint_remove ();
1050- client -> is_activated = false;
10511121 PR_INFO ("Activated data remove successed" );
10521122
10531123 client -> event .id = TUYA_EVENT_RESET_COMPLETE ;
0 commit comments