From 7b4720a39a83d3afdb01313c061d8a030455e817 Mon Sep 17 00:00:00 2001 From: 0x1abin <0x1abin@gmail.com> Date: Fri, 27 Aug 2021 17:04:50 +0800 Subject: [PATCH 1/2] 1. MQTT_PINGRESP_TIMEOUT_MS 0.5s ==> 2s; 2. MATOP_TIMEOUT_MS_DEFAULT 5s ==> 8s; --- include/tuya_config_defaults.h | 7 +++++++ middleware/core_mqtt_config.h | 2 +- middleware/mqtt_client_wrapper.c | 1 - src/matop_service.c | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/tuya_config_defaults.h b/include/tuya_config_defaults.h index 66758bc..2757411 100644 --- a/include/tuya_config_defaults.h +++ b/include/tuya_config_defaults.h @@ -77,4 +77,11 @@ #define HTTP_TIMEOUT_MS_DEFAULT (5000U) #endif +/** + * @brief HTTP TLS timeout config. + */ +#ifndef MATOP_TIMEOUT_MS_DEFAULT + #define MATOP_TIMEOUT_MS_DEFAULT (8000U) +#endif + #endif /* ifndef TUYA_CONFIG_DEFAULTS_H_ */ diff --git a/middleware/core_mqtt_config.h b/middleware/core_mqtt_config.h index 163bb28..301741e 100644 --- a/middleware/core_mqtt_config.h +++ b/middleware/core_mqtt_config.h @@ -33,7 +33,7 @@ * If a ping response is not received before this timeout, then * #MQTT_ProcessLoop will return #MQTTKeepAliveTimeout. */ -#define MQTT_PINGRESP_TIMEOUT_MS ( 500U ) +#define MQTT_PINGRESP_TIMEOUT_MS ( 2000U ) /** * @brief CORE_MQTT_BUFFER_SIZE diff --git a/middleware/mqtt_client_wrapper.c b/middleware/mqtt_client_wrapper.c index bf2e6e2..bfb0f01 100644 --- a/middleware/mqtt_client_wrapper.c +++ b/middleware/mqtt_client_wrapper.c @@ -300,7 +300,6 @@ mqtt_client_status_t mqtt_client_yield(void* client) if( mqtt_status != MQTTSuccess ) { log_error("MQTT_ProcessLoop returned with status = %s.", MQTT_Status_strerror( mqtt_status )); mqtt_client_disconnect(context); - system_sleep(context->config.timeout_ms); return MQTT_STATUS_NETWORK_TIMEOUT; } return MQTT_STATUS_SUCCESS; diff --git a/src/matop_service.c b/src/matop_service.c index b4f5c8a..d1f73a7 100644 --- a/src/matop_service.c +++ b/src/matop_service.c @@ -4,6 +4,7 @@ #include #include "tuya_log.h" +#include "tuya_config_defaults.h" #include "tuya_error_code.h" #include "tuya_cloud_types.h" #include "system_interface.h" @@ -245,7 +246,7 @@ int matop_service_request_async(matop_context_t* context, } message_handle->next = NULL; message_handle->id = ++matop->id_cnt; - message_handle->timeout = system_ticks() + (request->timeout == 0 ? 5000:request->timeout); + message_handle->timeout = system_ticks() + (request->timeout == 0 ? MATOP_TIMEOUT_MS_DEFAULT:request->timeout); message_handle->notify_cb = notify_cb; message_handle->user_data = user_data; From 9904a199871bb3db3d66143719fc5125af3ec225 Mon Sep 17 00:00:00 2001 From: 0x1abin <0x1abin@gmail.com> Date: Fri, 27 Aug 2021 17:05:51 +0800 Subject: [PATCH 2/2] Software timer update. --- utils/MultiTimer.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/utils/MultiTimer.c b/utils/MultiTimer.c index 11ddafe..ff3c91f 100644 --- a/utils/MultiTimer.c +++ b/utils/MultiTimer.c @@ -1,10 +1,15 @@ #include "MultiTimer.h" #include -// Timer handle list head. +#define MULTIMER_MAX_TIMEOUT 0x7fffffff + +/* Check if timer's expiry time is greater than time and care about uint32_t wraparounds */ +#define CHECK_TIME_LESS_THAN(t, compare_to) ( (((uint32_t)((t)-(compare_to))) > MULTIMER_MAX_TIMEOUT) ? 1 : 0 ) + +/* Timer handle list head. */ static MultiTimer* timerList = NULL; -// Timer tick +/* Timer tick */ static PlatformTicksFunction_t platformTicksFunction = NULL; /** @@ -101,17 +106,25 @@ int MultiTimerStop(MultiTimer* timer) */ void MultiTimerYield(void) { - MultiTimer* target = timerList; - for (; target; target = target->next) { - if (target->deadline > platformTicksFunction()) { + MultiTimer** nextTimer = &timerList; + + for (; *nextTimer; nextTimer = &(*nextTimer)->next) { + MultiTimer* entry = *nextTimer; + /* Sorted list, just process with the front part. */ + if (CHECK_TIME_LESS_THAN(platformTicksFunction(), entry->deadline)) { return; } - MultiTimerStop(target); - if (target->period) { - MultiTimerStart(target, target->period); + /* remove expired timer from list */ + *nextTimer = entry->next; + + if (entry->period) { + MultiTimerStart(entry, entry->period); + } + if (entry->callback) { + entry->callback(entry, entry->userData); } - if (target->callback) { - target->callback(target, target->userData); + if (entry->next == NULL) { + return; } } }