Refactor backlog by removing some str copies

This commit is contained in:
Theo Arends 2024-01-20 22:45:24 +01:00
parent 8223777e4f
commit 9608b7513a
3 changed files with 20 additions and 58 deletions

View File

@ -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)
}

View File

@ -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]}
//

View File

@ -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;
}