Skip to content

Commit

Permalink
Fix: Prevent wrong values of systemconfigpara data because of non-ato…
Browse files Browse the repository at this point in the history
…mic transaction
  • Loading branch information
tbnobody committed Jul 31, 2023
1 parent 698ecbc commit 74169c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ bool SystemConfigParaCommand::handleResponse(InverterAbstract* inverter, fragmen

// Move all fragments into target buffer
uint8_t offs = 0;
inverter->SystemConfigPara()->beginAppendFragment();
inverter->SystemConfigPara()->clearBuffer();
for (uint8_t i = 0; i < max_fragment_id; i++) {
inverter->SystemConfigPara()->appendFragment(offs, fragment[i].fragment, fragment[i].len);
offs += (fragment[i].len);
}
inverter->SystemConfigPara()->endAppendFragment();
inverter->SystemConfigPara()->setLastUpdateRequest(millis());
inverter->SystemConfigPara()->setLastLimitRequestSuccess(CMD_OK);
return true;
Expand Down
29 changes: 28 additions & 1 deletion lib/Hoymiles/src/parser/SystemConfigParaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
#include "../Hoymiles.h"
#include <cstring>

#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)

SystemConfigParaParser::SystemConfigParaParser()
: Parser()
{
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
}

void SystemConfigParaParser::clearBuffer()
{
memset(_payload, 0, SYSTEM_CONFIG_PARA_SIZE);
Expand All @@ -22,15 +34,30 @@ void SystemConfigParaParser::appendFragment(uint8_t offset, uint8_t* payload, ui
_payloadLength += len;
}

void SystemConfigParaParser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}

void SystemConfigParaParser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}

float SystemConfigParaParser::getLimitPercent()
{
return ((((uint16_t)_payload[2]) << 8) | _payload[3]) / 10.0;
HOY_SEMAPHORE_TAKE();
float ret = ((((uint16_t)_payload[2]) << 8) | _payload[3]) / 10.0;
HOY_SEMAPHORE_GIVE();
return ret;
}

void SystemConfigParaParser::setLimitPercent(float value)
{
HOY_SEMAPHORE_TAKE();
_payload[2] = ((uint16_t)(value * 10)) >> 8;
_payload[3] = ((uint16_t)(value * 10));
HOY_SEMAPHORE_GIVE();
}

void SystemConfigParaParser::setLastLimitCommandSuccess(LastCommandSuccess status)
Expand Down
5 changes: 5 additions & 0 deletions lib/Hoymiles/src/parser/SystemConfigParaParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

class SystemConfigParaParser : public Parser {
public:
SystemConfigParaParser();
void clearBuffer();
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
void beginAppendFragment();
void endAppendFragment();

float getLimitPercent();
void setLimitPercent(float value);
Expand All @@ -32,4 +35,6 @@ class SystemConfigParaParser : public Parser {

uint32_t _lastUpdateCommand = 0;
uint32_t _lastUpdateRequest = 0;

SemaphoreHandle_t _xSemaphore;
};

0 comments on commit 74169c3

Please sign in to comment.