Skip to content

Commit 95a714d

Browse files
committed
add tuya_lan_disable
1 parent 69618bc commit 95a714d

6 files changed

Lines changed: 154 additions & 1 deletion

File tree

src/tuya_cloud_service/ble/ble_mgr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ typedef void (*ble_session_fn_t)(ble_packet_t *packet, void *priv_data);
6161
*/
6262
int tuya_ble_init(tuya_ble_cfg_t *cfg);
6363

64+
/**
65+
* @brief Deinitializes the Tuya BLE module.
66+
*
67+
* This function deinitializes the Tuya BLE module and releases resources.
68+
*
69+
* @return 0 if successful, otherwise an error code.
70+
*/
71+
int tuya_ble_deinit(void);
72+
6473
/**
6574
* @brief Checks if the device is connected to the Tuya BLE service.
6675
*

src/tuya_cloud_service/cloud/tuya_iot.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@
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
3441
typedef 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

6170
static int iot_dispatch_event(tuya_iot_client_t *client)
6271
{
@@ -722,6 +731,60 @@ int tuya_iot_reset(tuya_iot_client_t *client)
722731
*/
723732
int 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;

src/tuya_cloud_service/lan/lan_sock.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ BOOL_T tuya_get_sock_loop_terminate(void)
422422
return g_sloop->terminate;
423423
}
424424

425+
BOOL_T tuya_sock_loop_is_inited(void)
426+
{
427+
return (g_sloop != NULL);
428+
}
429+
425430
/**
426431
* @brief Function to dump the LAN socket reader.
427432
*

src/tuya_cloud_service/lan/lan_sock.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ int tuya_get_sock_loop_terminate();
111111
*/
112112
void tuya_dump_lan_sock_reader();
113113

114+
/**
115+
* @brief check sock loop init
116+
*
117+
* @return TRUE if sock loop inited, otherwise FALSE
118+
*/
119+
BOOL_T tuya_sock_loop_is_inited(void);
120+
114121
/**
115122
* @brief sock loop init
116123
*

src/tuya_cloud_service/lan/tuya_lan.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,65 @@ int tuya_lan_exit(void)
13911391
return OPRT_OK;
13921392
}
13931393

1394+
/**
1395+
* @brief Disable LAN service and release sockets
1396+
*
1397+
* @return OPRT_OK on success. Others on error, please refer to
1398+
* tuya_error_code.h
1399+
*/
1400+
int tuya_lan_disable(void)
1401+
{
1402+
if (s_lan_mgr == NULL) {
1403+
return OPRT_OK;
1404+
}
1405+
1406+
lan_session_close_all();
1407+
1408+
if (s_lan_mgr->tcp_serv_fd >= 0) {
1409+
tuya_unreg_lan_sock(s_lan_mgr->tcp_serv_fd);
1410+
s_lan_mgr->tcp_serv_fd = -1;
1411+
}
1412+
1413+
if (s_lan_mgr->udp_serv_fd >= 0) {
1414+
tuya_unreg_lan_sock(s_lan_mgr->udp_serv_fd);
1415+
s_lan_mgr->udp_serv_fd = -1;
1416+
}
1417+
1418+
if (s_lan_mgr->udp_client_fd >= 0) {
1419+
tal_net_close(s_lan_mgr->udp_client_fd);
1420+
s_lan_mgr->udp_client_fd = -1;
1421+
}
1422+
1423+
tuya_sock_loop_disable();
1424+
uint32_t wait_ms = 0;
1425+
while (tuya_sock_loop_is_inited() && wait_ms < 3000) {
1426+
tal_system_sleep(50);
1427+
wait_ms += 50;
1428+
}
1429+
1430+
return OPRT_OK;
1431+
}
1432+
1433+
/**
1434+
* @brief Enable LAN service
1435+
*
1436+
* @return OPRT_OK on success. Others on error, please refer to
1437+
* tuya_error_code.h
1438+
*/
1439+
int tuya_lan_enable(void)
1440+
{
1441+
if (s_lan_mgr != NULL) {
1442+
return OPRT_OK;
1443+
}
1444+
1445+
tuya_iot_client_t *client = tuya_iot_client_get();
1446+
if (client == NULL || client->is_activated == false) {
1447+
return OPRT_COM_ERROR;
1448+
}
1449+
1450+
return tuya_lan_init(client);
1451+
}
1452+
13941453
/**
13951454
* @brief distribute data to all connections
13961455
*

src/tuya_cloud_service/netcfg/ap_netcfg.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "tal_wifi.h"
2525
#include "tal_network.h"
2626
#include "tuya_iot.h"
27+
#include "tuya_lan.h"
2728
#include "tuya_register_center.h"
2829

2930
#define AP_BROADCAST_PORT 6667
@@ -861,6 +862,8 @@ static int ap_netcfg_start(int type, netcfg_finish_cb_t cb, void *args)
861862
return OPRT_MALLOC_FAILED;
862863
}
863864

865+
tuya_lan_disable();
866+
864867
op_ret = ap_mode_start(ap);
865868
if (OPRT_OK != op_ret) {
866869
PR_ERR("ap mode start fail:%d", op_ret);

0 commit comments

Comments
 (0)