From 9608b7513a9f66ed695dd9952bb8d623ab56bce5 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 20 Jan 2024 22:45:24 +0100 Subject: [PATCH] Refactor backlog by removing some str copies --- tasmota/tasmota.ino | 5 +- tasmota/tasmota_support/support_command.ino | 49 +++---------------- tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino | 24 ++++----- 3 files changed, 20 insertions(+), 58 deletions(-) diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 9cd999350..4b9643f91 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -789,12 +789,13 @@ void BacklogLoop(void) { TasmotaGlobal.backlog_mutex = true; bool nodelay = false; do { - char cmd[strlen(backlog.get(0))+1]; // Need to accomodate large commands like Template - BacklogHead(cmd); + char* cmd = backlog.shift(); 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) } diff --git a/tasmota/tasmota_support/support_command.ino b/tasmota/tasmota_support/support_command.ino index 7c49944c9..197568e5a 100644 --- a/tasmota/tasmota_support/support_command.ino +++ b/tasmota/tasmota_support/support_command.ino @@ -491,43 +491,6 @@ void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len) { TasmotaGlobal.fallback_topic_flag = false; } -/********************************************************************************************/ - -void BacklogAdd(const char* blcommand) { - // Add object at end of list - char* temp = (char*)malloc(strlen(blcommand)+1); - if (temp != nullptr) { - strcpy(temp, blcommand); - backlog.add(temp); - } -} - -void BacklogInsert(uint32_t position, const char* blcommand) { - // Add object at position in list - char* temp = (char*)malloc(strlen(blcommand)+1); - if (temp != nullptr) { - strcpy(temp, (char*)blcommand); - backlog.add(position, temp); - } -} - -void BacklogClear(void) { - // Clear list - while (backlog.size()) { - free(backlog.pop()); - } -} - -char* BacklogHead(char* blcommand) { - // Remove first object from list - char* temp = backlog.shift(); - strcpy(blcommand, temp); - free(temp); - return blcommand; -} - -/*------------------------------------------------------------------------------------------*/ - void CmndBacklog(void) { // Backlog command1;command2;.. Execute commands in sequence with a delay in between set with SetOption34 // Backlog0 command1;command2;.. Execute commands in sequence with no delay @@ -557,7 +520,11 @@ void CmndBacklog(void) { } // Do not allow command Reset in backlog if ((*blcommand != '\0') && (strncasecmp_P(blcommand, PSTR(D_CMND_RESET), strlen(D_CMND_RESET)) != 0)) { - BacklogAdd(blcommand); + char* temp = (char*)malloc(strlen(blcommand)+1); + if (temp != nullptr) { + strcpy(temp, blcommand); + backlog.add(temp); + } } blcommand = strtok(nullptr, ";"); } @@ -566,13 +533,13 @@ void CmndBacklog(void) { TasmotaGlobal.backlog_timer = millis(); } else { bool blflag = BACKLOG_EMPTY; - BacklogClear(); + while (backlog.size()) { + free(backlog.pop()); + } ResponseCmndChar(blflag ? PSTR(D_JSON_EMPTY) : PSTR(D_JSON_ABORTED)); } } -/********************************************************************************************/ - void CmndJson(void) { // Json {"template":{"NAME":"Dummy","GPIO":[320,0,321],"FLAG":0,"BASE":18},"power":2,"HSBColor":"51,97,100","Channel":[100,85,3]} // diff --git a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino index 550c46463..1066951be 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_10_rules.ino @@ -2062,24 +2062,18 @@ void ExecuteCommandBlock(const char * commands, int len) } //Start to process current command we found //Going to insert the command into backlog - String sCurrentCommand = oneCommand; - sCurrentCommand.trim(); -/* - if (sCurrentCommand.length() > 0 - && !TasmotaGlobal.backlog_mutex) - { + char* blcommand = oneCommand; + Trim(blcommand); + if (strlen(blcommand)) { //Insert into backlog - TasmotaGlobal.backlog_mutex = true; - BacklogInsert(insertPosition, sCurrentCommand.c_str()); - TasmotaGlobal.backlog_mutex = false; - insertPosition++; - } -*/ - if (sCurrentCommand.length() > 0) { - //Insert into backlog - BacklogInsert(insertPosition, sCurrentCommand.c_str()); + char* temp = (char*)malloc(strlen(blcommand)+1); + if (temp != nullptr) { + strcpy(temp, blcommand); + backlog.add(insertPosition, temp); + } insertPosition++; } + } return; }