From 3f5078294f1efa373cff836bcba55398626f3e33 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 18 Apr 2025 22:35:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B7=B2=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/http_server/app_httpd.c | 2 -- TC1/main.c | 5 ---- TC1/mqtt_server/user_mqtt_client.c | 27 ++++++++++++++----- TC1/mqtt_server/user_mqtt_client.h | 1 + TC1/user_gpio.c | 1 + TC1/user_wifi.c | 4 +++ mico-os/libraries/daemons/http_server/httpd.h | 2 ++ 7 files changed, 29 insertions(+), 13 deletions(-) diff --git a/TC1/http_server/app_httpd.c b/TC1/http_server/app_httpd.c index a52ffc6..bdde3b7 100644 --- a/TC1/http_server/app_httpd.c +++ b/TC1/http_server/app_httpd.c @@ -326,8 +326,6 @@ static int HttpSetOTAFile(httpd_request_t *req) mico_system_power_perform(mico_system_context_get(), eState_Software_Reset); exit: if (buffer) free(buffer); - tc1_log("[OTA] buffer"); - tc1_log("[OTA] bbbbbbbbbbbbbbbbbbbb"); return err; // ota_file_req = req; diff --git a/TC1/main.c b/TC1/main.c index dc6e87b..7d0971c 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -10,7 +10,6 @@ #include "user_wifi.h" #include "time_server/user_rtc.h" #include "user_power.h" -#include "mqtt_server/user_mqtt_client.h" #include "http_server/app_httpd.h" #include "timed_task/timed_task.h" @@ -186,10 +185,6 @@ int application_start(void) { } } KeyInit(); - if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){ - err = UserMqttInit(); - require_noerr(err, exit); - } err = UserRtcInit(); require_noerr(err, exit); PowerInit(); diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index 12c7364..b1e8325 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -53,6 +53,7 @@ mico_queue_t mqtt_msg_send_queue = NULL; Client c; // mqtt client object Network n; // socket network for mqtt client +volatile bool mqtt_thread_should_exit = false; static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */ //static mico_timed_event_t mqtt_client_send_event; @@ -98,6 +99,18 @@ void UserMqttTimerFunc(void *arg) { } } +OSStatus UserMqttDeInit(void) { + OSStatus err = kNoErr; + + // 1. 请求线程退出 + mqtt_thread_should_exit = true; + +exit: + if (kNoErr != err) + mqtt_log("ERROR: UserMqttDeInit exit err: %d", err); + return err; +} + /* Application entrance */ OSStatus UserMqttInit(void) { OSStatus err = kNoErr; @@ -204,14 +217,14 @@ void MqttClientThread(mico_thread_arg_t arg) { msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue); require_action(msg_send_event_fd >= 0, exit, mqtt_log("ERROR: create msg send queue event fd failed!!!")); - + mqtt_thread_should_exit = false; MQTT_start: isconnect = false; /* 1. create network connection */ ssl_settings.ssl_enable = false; LinkStatusTypeDef LinkStatus; - while (1) { + while (!mqtt_thread_should_exit) { isconnect = false; mico_rtos_thread_sleep(3); if (MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1) @@ -272,7 +285,7 @@ void MqttClientThread(mico_thread_arg_t arg) { mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg); registerMqttEvents(); /* 5. client loop for recv msg && keepalive */ - while (1) { + while (!mqtt_thread_should_exit) { isconnect = true; no_mqtt_msg_exchange = true; FD_ZERO(&readfds); @@ -329,10 +342,12 @@ mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, mico_rtos_thread_sleep(5); goto MQTT_start; - exit: - isconnect = false;mqtt_log("EXIT: MQTT client exit with err = %d.", err); +exit: + isconnect = false; + mqtt_log("EXIT: MQTT client exit with err = %d.", err); UserMqttClientRelease(&c, &n); - mico_rtos_delete_thread(NULL); + mico_rtos_delete_thread(NULL); // 自删 + return; } // callback, msg received from mqtt server diff --git a/TC1/mqtt_server/user_mqtt_client.h b/TC1/mqtt_server/user_mqtt_client.h index d384d07..1e49744 100644 --- a/TC1/mqtt_server/user_mqtt_client.h +++ b/TC1/mqtt_server/user_mqtt_client.h @@ -22,6 +22,7 @@ #define MQTT_LED_ENABLED user_config->power_led_enabled extern OSStatus UserMqttInit(void); +extern OSStatus UserMqttDeInit(void); extern OSStatus UserMqttSend(char *arg); diff --git a/TC1/user_gpio.c b/TC1/user_gpio.c index 863e822..afb16a4 100644 --- a/TC1/user_gpio.c +++ b/TC1/user_gpio.c @@ -239,6 +239,7 @@ static void KeyEventHandler(int num, boolean longPress) { if (childLockEnabled) break; StartLedBlink(3); + UserMqttDeInit(); micoWlanSuspendStation(); ApInit(true); break; diff --git a/TC1/user_wifi.c b/TC1/user_wifi.c index 89525a2..e46b427 100644 --- a/TC1/user_wifi.c +++ b/TC1/user_wifi.c @@ -4,6 +4,7 @@ #include "mico_socket.h" #include "user_gpio.h" #include "http_server/web_log.h" +#include "mqtt_server/user_mqtt_client.h" char wifi_status = WIFI_STATE_NOCONNECT; @@ -122,6 +123,9 @@ static void WifiLedTimerCallback(void* arg) UserLedSet(-1); break; case WIFI_STATE_CONNECTED: + if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){ + UserMqttInit(); + } UserLedSet(0); mico_rtos_stop_timer(&wifi_led_timer); if (RelayOut()&&user_config->power_led_enabled) diff --git a/mico-os/libraries/daemons/http_server/httpd.h b/mico-os/libraries/daemons/http_server/httpd.h index 1643760..33b7500 100644 --- a/mico-os/libraries/daemons/http_server/httpd.h +++ b/mico-os/libraries/daemons/http_server/httpd.h @@ -1125,6 +1125,8 @@ int httpd_parse_hdr_tags(httpd_request_t *req, int sock, */ int httpd_get_data(httpd_request_t *req, char *content, int length); +int httpd_get_data2(httpd_request_t *req, char *content, int length); + /** @brief Get the incoming JSON data in case of HTTP POST request * * @note This function is an extension to \ref httpd_get_data. Additionally this