From 1ffbbc914daa47cc094c05eba5b623aa61edcd06 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 21 Jan 2024 22:07:49 +0100 Subject: [PATCH] Refactor backlog from LinkedList to TasmotaLList --- lib/default/TasmotaLList/src/LList.h | 5 ++-- tasmota/tasmota.ino | 29 ++++++++++++++++--- tasmota/tasmota_support/support_command.ino | 8 +++-- tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino | 3 +- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/default/TasmotaLList/src/LList.h b/lib/default/TasmotaLList/src/LList.h index 4af512b93..fc3a7b559 100644 --- a/lib/default/TasmotaLList/src/LList.h +++ b/lib/default/TasmotaLList/src/LList.h @@ -145,14 +145,13 @@ void LList::reset(void) { template T * LList::removeHead(void) { if (_head) { - T & orginal_head = _head.val; + T * orginal_head = &_head->_val; LList_elt * next = _head->next(); delete _head; _head = next; return orginal_head; - } else { - return nullptr; } + return nullptr; } template diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 16f6c3a1e..d7e01c0c8 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -434,9 +434,8 @@ struct TasmotaGlobal_t { TSettings* Settings = nullptr; -#include -LinkedList backlog; // Command backlog implemented with LinkedList -#define BACKLOG_EMPTY (backlog.size() == 0) +LList backlog; // Command backlog implemented with TasmotaLList +#define BACKLOG_EMPTY (backlog.isEmpty()) /*********************************************************************************************\ * Main @@ -782,7 +781,12 @@ void BacklogLoop(void) { TasmotaGlobal.backlog_mutex = true; bool nodelay = false; do { - char* cmd = backlog.shift(); + char* cmd = *backlog.head(); + backlog.removeHead(); +/* + // This adds 32 bytes + char* cmd = *backlog.removeHead(); +*/ if (!strncasecmp_P(cmd, PSTR(D_CMND_NODELAY), strlen(D_CMND_NODELAY))) { free(cmd); nodelay = true; @@ -795,6 +799,23 @@ void BacklogLoop(void) { break; } } while (!BACKLOG_EMPTY); +/* + // This adds 96 bytes + for (auto &cmd : backlog) { + backlog.remove(&cmd); + if (!strncasecmp_P(cmd, PSTR(D_CMND_NODELAY), strlen(D_CMND_NODELAY))) { + free(cmd); + nodelay = true; + } else { + ExecuteCommand(cmd, SRC_BACKLOG); + free(cmd); + if (nodelay || TasmotaGlobal.backlog_nodelay) { + TasmotaGlobal.backlog_timer = millis(); // Reset backlog_timer which has been set by ExecuteCommand (CommandHandler) + } + break; + } + } +*/ TasmotaGlobal.backlog_mutex = false; } if (BACKLOG_EMPTY) { diff --git a/tasmota/tasmota_support/support_command.ino b/tasmota/tasmota_support/support_command.ino index d763a4130..03dcd294a 100644 --- a/tasmota/tasmota_support/support_command.ino +++ b/tasmota/tasmota_support/support_command.ino @@ -523,7 +523,8 @@ void CmndBacklog(void) { char* temp = (char*)malloc(strlen(blcommand)+1); if (temp != nullptr) { strcpy(temp, blcommand); - backlog.add(temp); + char* &elem = backlog.addToLast(); + elem = temp; } } blcommand = strtok(nullptr, ";"); @@ -533,8 +534,9 @@ void CmndBacklog(void) { TasmotaGlobal.backlog_timer = millis(); } else { bool blflag = BACKLOG_EMPTY; - while (backlog.size()) { - free(backlog.pop()); + for (auto &elem : backlog) { + free(elem); + backlog.remove(&elem); } ResponseCmndChar(blflag ? PSTR(D_JSON_EMPTY) : PSTR(D_JSON_ABORTED)); } diff --git a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino index 1066951be..e1cec3a67 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino @@ -2069,7 +2069,8 @@ void ExecuteCommandBlock(const char * commands, int len) char* temp = (char*)malloc(strlen(blcommand)+1); if (temp != nullptr) { strcpy(temp, blcommand); - backlog.add(insertPosition, temp); + char* &elem = backlog.insertAt(insertPosition); + elem = temp; } insertPosition++; }