From 885a7a3692937e893199ef77a52dfdeb1498206f Mon Sep 17 00:00:00 2001 From: nhkefus Date: Wed, 8 Apr 2026 23:16:13 +0800 Subject: [PATCH] refactor: improve memory safety in web logging and add connection check for MQTT power reporting --- .idea/.gitignore | 3 + .idea/caches/deviceStreaming.xml | 679 +++++++++++++++++++ .idea/inspectionProfiles/Project_Default.xml | 21 + .idea/libraries/FlashPlus_v1_0_10_msi.xml | 9 + .idea/libraries/mkfatimg.xml | 9 + .idea/misc.xml | 5 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + TC1/.DS_Store | Bin 0 -> 6148 bytes TC1/http_server/web_log.h | 19 +- TC1/main.c | 5 +- TC1/mqtt_server/user_mqtt_client.c | 85 ++- TC1/user_wifi.c | 1 + 13 files changed, 813 insertions(+), 37 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/caches/deviceStreaming.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/libraries/FlashPlus_v1_0_10_msi.xml create mode 100644 .idea/libraries/mkfatimg.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 TC1/.DS_Store diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/caches/deviceStreaming.xml b/.idea/caches/deviceStreaming.xml new file mode 100644 index 0000000..61a0a4a --- /dev/null +++ b/.idea/caches/deviceStreaming.xml @@ -0,0 +1,679 @@ + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..65b62af --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,21 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/FlashPlus_v1_0_10_msi.xml b/.idea/libraries/FlashPlus_v1_0_10_msi.xml new file mode 100644 index 0000000..6094479 --- /dev/null +++ b/.idea/libraries/FlashPlus_v1_0_10_msi.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/mkfatimg.xml b/.idea/libraries/mkfatimg.xml new file mode 100644 index 0000000..ec09897 --- /dev/null +++ b/.idea/libraries/mkfatimg.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..104fe35 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..515acdf --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TC1/.DS_Store b/TC1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..296c9f46961a4cdb7188945f845b8ab5e7d87316 GIT binary patch literal 6148 zcmeHKyH3ME5S)b+k5h2!Fg1VfKCzR^I9lv+i5J{pz>e`a^V$XO@XG56`}Mi4 zx3{@FSr2#)-cPLKP8CZENC7Dz1*E|LRe(L)ZN5lUlmb#f3VbQx--kwb?1f`ud^#9n z1R&0s4&!~y62#^SVlNyMnW0&h60_85#IP)9z7?+*j)_?gi<7xeooqFsSe(v$3p%VP zDoOznH2a91wH_NHXV@w literal 0 HcmV?d00001 diff --git a/TC1/http_server/web_log.h b/TC1/http_server/web_log.h index 46a3b2c..eb03711 100644 --- a/TC1/http_server/web_log.h +++ b/TC1/http_server/web_log.h @@ -20,14 +20,17 @@ void WebLog(const char *M, ...); extern LogRecord log_record; extern char* LOG_TMP; extern time_t LOG_NOW; -#define web_log(N, M, ...) \ - LOG_TMP = (char*)malloc(sizeof(char)*LOG_LEN); \ - LOG_NOW = time(NULL) + 28800; \ - strftime(LOG_TMP, TIME_LEN, "[%Y-%m-%d %H:%M:%S]", localtime(&LOG_NOW)); \ - LOG_TMP[TIME_LEN - 1] = ' '; \ - snprintf(LOG_TMP + TIME_LEN, LOG_LEN - TIME_LEN, "["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__); \ - SetLogRecord(&log_record, LOG_TMP); \ +#define web_log(N, M, ...) do { \ + char* _wl_tmp = (char*)malloc(sizeof(char)*LOG_LEN); \ + if (_wl_tmp) { \ + time_t _wl_now = time(NULL) + 28800; \ + strftime(_wl_tmp, TIME_LEN, "[%Y-%m-%d %H:%M:%S]", localtime(&_wl_now)); \ + _wl_tmp[TIME_LEN - 1] = ' '; \ + snprintf(_wl_tmp + TIME_LEN, LOG_LEN - TIME_LEN, "[" N " %s:%d] " M, SHORT_FILE, __LINE__, ##__VA_ARGS__); \ + SetLogRecord(&log_record, _wl_tmp); \ + } \ +} while(0) #define web_log0(N, M, ...) WebLog("["N" %s:%d] "M, SHORT_FILE, __LINE__, ##__VA_ARGS__) -#endif // !WEB_LOG_H +#endif // !WEB_LOG_H diff --git a/TC1/main.c b/TC1/main.c index 00a047b..e053277 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -10,6 +10,7 @@ #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 "udp_server/udp_server.h" #include "timed_task/timed_task.h" @@ -113,7 +114,9 @@ void schedule_p_count_task(mico_thread_arg_t arg) { void reportMqttPowerInfoThread() { while (1) { - UserMqttHassPower(); + if (UserMqttIsConnect()) { + UserMqttHassPower(); + } int freq = user_config->mqtt_report_freq; if (freq == 0) { diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index 20198ee..fee5233 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -62,6 +62,7 @@ mico_queue_t mqtt_msg_send_queue = NULL; mico_queue_t mqtt_msg_send_queue2 = NULL; volatile bool mqtt_thread_should_exit = false; +static bool mqtt_initialized = false; static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */ @@ -150,6 +151,7 @@ OSStatus UserMqttDeInit(void) { // 1. 请求线程退出 mqtt_thread_should_exit = true; + mqtt_initialized = false; return err; } @@ -158,6 +160,11 @@ OSStatus UserMqttDeInit(void) { OSStatus UserMqttInit(void) { OSStatus err = kNoErr; + if (mqtt_initialized) { + mqtt_log("MQTT already initialized, skip re-init."); + return kNoErr; + } + sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1); sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac); //TODO size:0x800 @@ -173,6 +180,12 @@ OSStatus UserMqttInit(void) { MAX_MQTT_SEND_QUEUE_SIZE); require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue err=%d.", err)); + /* create mqtt msg send queue 2 */ + err = mico_rtos_init_queue(&mqtt_msg_send_queue2, "mqtt_msg_send_queue2", + sizeof(p_mqtt_send_msg_t), + MAX_MQTT_SEND_QUEUE_SIZE); + require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue2 err=%d.", err)); + /* start mqtt client */ mqtt_context_t *ctx1 = malloc(sizeof(mqtt_context_t)); memset(ctx1, 0, sizeof(mqtt_context_t)); @@ -198,6 +211,8 @@ OSStatus UserMqttInit(void) { 0x1000, 5); require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread."); + mqtt_initialized = true; + exit: if (kNoErr != err)mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr); return err; @@ -344,6 +359,8 @@ void MqttClientThread(mico_thread_arg_t arg) { UserMqttSendTotalSocketState(); UserMqttSendChildLockState(); + mico_stop_timer(&timer_handle); + mico_deinit_timer(&timer_handle); mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg); registerMqttEvents(); /* 5. client loop for recv msg && keepalive */ @@ -503,6 +520,8 @@ void MqttClientThread2(mico_thread_arg_t arg) { UserMqttSendTotalSocketState(); UserMqttSendChildLockState(); + mico_stop_timer(&timer_handle2); + mico_deinit_timer(&timer_handle2); mico_init_timer(&timer_handle2, 150, UserMqttTimerFunc2, &arg); registerMqttEvents2(); /* 5. client loop for recv msg && keepalive */ @@ -665,47 +684,57 @@ void ProcessHaCmd(char *cmd) { } OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) { - OSStatus err = kUnknownErr; + OSStatus err = kNoErr; p_mqtt_send_msg_t p_send_msg = NULL; + p_mqtt_send_msg_t p_old_msg = NULL; // mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory); + /* Push to queue 1 */ if(mqtt_msg_send_queue != NULL && isconnect) { /* Send queue is full, pop the oldest */ if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true) { - mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0); - free(p_send_msg); - p_send_msg = NULL; + mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_old_msg, 0); + free(p_old_msg); + p_old_msg = NULL; + } + p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t)); + if (p_send_msg) { + p_send_msg->qos = 0; + p_send_msg->retained = retained; + p_send_msg->datalen = strlen(arg); + memcpy(p_send_msg->data, arg, p_send_msg->datalen); + strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE); + err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0); + if (err != kNoErr) { + free(p_send_msg); + p_send_msg = NULL; + } } } + + /* Push to queue 2 (separate allocation to avoid double-free) */ if(mqtt_msg_send_queue2 != NULL && isconnect2) { if (mico_rtos_is_queue_full(&mqtt_msg_send_queue2) == true) { - mico_rtos_pop_from_queue(&mqtt_msg_send_queue2, &p_send_msg, 0); - free(p_send_msg); - p_send_msg = NULL; + mico_rtos_pop_from_queue(&mqtt_msg_send_queue2, &p_old_msg, 0); + free(p_old_msg); + p_old_msg = NULL; + } + p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t)); + if (p_send_msg) { + p_send_msg->qos = 0; + p_send_msg->retained = retained; + p_send_msg->datalen = strlen(arg); + memcpy(p_send_msg->data, arg, p_send_msg->datalen); + strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE); + err = mico_rtos_push_to_queue(&mqtt_msg_send_queue2, &p_send_msg, 0); + if (err != kNoErr) { + free(p_send_msg); + p_send_msg = NULL; + } } } - /* Push the latest data into send queue*/ - p_send_msg = (p_mqtt_send_msg_t) calloc(1, sizeof(mqtt_send_msg_t)); - require_action(p_send_msg, exit, err = kNoMemoryErr); - - p_send_msg->qos = 0; - p_send_msg->retained = retained; - p_send_msg->datalen = strlen(arg); - memcpy(p_send_msg->data, arg, p_send_msg->datalen); - strncpy(p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE); - if(mqtt_msg_send_queue != NULL && isconnect) { - err = mico_rtos_push_to_queue(&mqtt_msg_send_queue, &p_send_msg, 0); - } - if(mqtt_msg_send_queue2 != NULL && isconnect2) { - err = mico_rtos_push_to_queue(&mqtt_msg_send_queue2, &p_send_msg, 0); - } - require_noerr(err, exit); - //mqtt_log("Push user msg into send queue success!"); - - exit: - if (err != kNoErr && p_send_msg) free(p_send_msg); return err; } @@ -1065,5 +1094,5 @@ extern void UserMqttHassPower(void) { } bool UserMqttIsConnect() { - return isconnect; + return isconnect || isconnect2; } diff --git a/TC1/user_wifi.c b/TC1/user_wifi.c index f26f415..33dd652 100644 --- a/TC1/user_wifi.c +++ b/TC1/user_wifi.c @@ -70,6 +70,7 @@ void WifiScanCallback(ScanResult_adv* scan_ret, void* arg) wifi_log("wifi_scan_callback ApNum[%d] ApList[0](%s)", count, scan_ret->ApList[0].ssid); int i = 0; + if (wifi_ret) { free(wifi_ret); wifi_ret = NULL; } wifi_ret = malloc(sizeof(char)*count * (32 + 2) + 50); char* ssids = malloc(sizeof(char)*count * 32); char* secs = malloc(sizeof(char)*count * 2 + 1);