Hoymiles Lib: Move semaphore handing into parser base class

This commit is contained in:
Thomas Basler 2023-09-07 22:07:10 +02:00
parent 9ac6dd6e8d
commit 0260af9ada
14 changed files with 35 additions and 119 deletions

View File

@ -86,16 +86,9 @@ const std::array<const AlarmMessage_t, ALARM_MSG_COUNT> AlarmLogParser::_alarmMe
{ AlarmMessageType_t::ALL, 9000, "Microinverter is suspected of being stolen" }, { AlarmMessageType_t::ALL, 9000, "Microinverter is suspected of being stolen" },
} }; } };
#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
AlarmLogParser::AlarmLogParser() AlarmLogParser::AlarmLogParser()
: Parser() : Parser()
{ {
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
clearBuffer(); clearBuffer();
} }
@ -115,16 +108,6 @@ void AlarmLogParser::appendFragment(uint8_t offset, uint8_t* payload, uint8_t le
_alarmLogLength += len; _alarmLogLength += len;
} }
void AlarmLogParser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}
void AlarmLogParser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}
uint8_t AlarmLogParser::getEntryCount() uint8_t AlarmLogParser::getEntryCount()
{ {
if (_alarmLogLength < 2) { if (_alarmLogLength < 2) {

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include "Parser.h" #include "Parser.h"
#include <Arduino.h>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
@ -34,8 +33,6 @@ public:
AlarmLogParser(); AlarmLogParser();
void clearBuffer(); void clearBuffer();
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
void beginAppendFragment();
void endAppendFragment();
uint8_t getEntryCount(); uint8_t getEntryCount();
void getLogEntry(uint8_t entryId, AlarmLogEntry_t* entry); void getLogEntry(uint8_t entryId, AlarmLogEntry_t* entry);
@ -56,6 +53,4 @@ private:
AlarmMessageType_t _messageType = AlarmMessageType_t::ALL; AlarmMessageType_t _messageType = AlarmMessageType_t::ALL;
static const std::array<const AlarmMessage_t, ALARM_MSG_COUNT> _alarmMessages; static const std::array<const AlarmMessage_t, ALARM_MSG_COUNT> _alarmMessages;
SemaphoreHandle_t _xSemaphore;
}; };

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Copyright (C) 2022 Thomas Basler and others * Copyright (C) 2022 - 2023 Thomas Basler and others
*/ */
#include "DevInfoParser.h" #include "DevInfoParser.h"
#include "../Hoymiles.h" #include "../Hoymiles.h"
@ -46,16 +46,9 @@ const devInfo_t devInfo[] = {
{ { 0x10, 0x33, 0x31, ALL }, 2250, "HMT-2250" } // 01 { { 0x10, 0x33, 0x31, ALL }, 2250, "HMT-2250" } // 01
}; };
#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
DevInfoParser::DevInfoParser() DevInfoParser::DevInfoParser()
: Parser() : Parser()
{ {
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
clearBufferSimple(); clearBufferSimple();
clearBufferAll(); clearBufferAll();
} }
@ -92,16 +85,6 @@ void DevInfoParser::appendFragmentSimple(uint8_t offset, uint8_t* payload, uint8
_devInfoSimpleLength += len; _devInfoSimpleLength += len;
} }
void DevInfoParser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}
void DevInfoParser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}
uint32_t DevInfoParser::getLastUpdateAll() uint32_t DevInfoParser::getLastUpdateAll()
{ {
return _lastUpdateAll; return _lastUpdateAll;

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include "Parser.h" #include "Parser.h"
#include <Arduino.h>
#define DEV_INFO_SIZE 20 #define DEV_INFO_SIZE 20
@ -14,9 +13,6 @@ public:
void clearBufferSimple(); void clearBufferSimple();
void appendFragmentSimple(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragmentSimple(uint8_t offset, uint8_t* payload, uint8_t len);
void beginAppendFragment();
void endAppendFragment();
uint32_t getLastUpdateAll(); uint32_t getLastUpdateAll();
void setLastUpdateAll(uint32_t lastUpdate); void setLastUpdateAll(uint32_t lastUpdate);
@ -47,6 +43,4 @@ private:
uint8_t _payloadDevInfoSimple[DEV_INFO_SIZE] = {}; uint8_t _payloadDevInfoSimple[DEV_INFO_SIZE] = {};
uint8_t _devInfoSimpleLength = 0; uint8_t _devInfoSimpleLength = 0;
SemaphoreHandle_t _xSemaphore;
}; };

View File

@ -1,21 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Copyright (C) 2022 Thomas Basler and others * Copyright (C) 2023 Thomas Basler and others
*/ */
#include "GridProfileParser.h" #include "GridProfileParser.h"
#include "../Hoymiles.h" #include "../Hoymiles.h"
#include <cstring> #include <cstring>
#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
GridProfileParser::GridProfileParser() GridProfileParser::GridProfileParser()
: Parser() : Parser()
{ {
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
clearBuffer(); clearBuffer();
} }
@ -35,16 +28,6 @@ void GridProfileParser::appendFragment(uint8_t offset, uint8_t* payload, uint8_t
_gridProfileLength += len; _gridProfileLength += len;
} }
void GridProfileParser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}
void GridProfileParser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}
std::vector<uint8_t> GridProfileParser::getRawData() std::vector<uint8_t> GridProfileParser::getRawData()
{ {
std::vector<uint8_t> ret; std::vector<uint8_t> ret;

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include "Parser.h" #include "Parser.h"
#include <Arduino.h>
#define GRID_PROFILE_SIZE 141 #define GRID_PROFILE_SIZE 141
@ -11,14 +10,9 @@ public:
void clearBuffer(); void clearBuffer();
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
void beginAppendFragment();
void endAppendFragment();
std::vector<uint8_t> getRawData(); std::vector<uint8_t> getRawData();
private: private:
uint8_t _payloadGridProfile[GRID_PROFILE_SIZE] = {}; uint8_t _payloadGridProfile[GRID_PROFILE_SIZE] = {};
uint8_t _gridProfileLength = 0; uint8_t _gridProfileLength = 0;
SemaphoreHandle_t _xSemaphore;
}; };

View File

@ -1,9 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Copyright (C) 2022 Thomas Basler and others * Copyright (C) 2022 - 2023 Thomas Basler and others
*/ */
#include "Parser.h" #include "Parser.h"
Parser::Parser()
{
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
}
uint32_t Parser::getLastUpdate() uint32_t Parser::getLastUpdate()
{ {
return _lastUpdate; return _lastUpdate;
@ -13,3 +19,13 @@ void Parser::setLastUpdate(uint32_t lastUpdate)
{ {
_lastUpdate = lastUpdate; _lastUpdate = lastUpdate;
} }
void Parser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}
void Parser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}

View File

@ -1,7 +1,13 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include <Arduino.h>
#include <cstdint> #include <cstdint>
#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
typedef enum { typedef enum {
CMD_OK, CMD_OK,
CMD_NOK, CMD_NOK,
@ -10,9 +16,16 @@ typedef enum {
class Parser { class Parser {
public: public:
Parser();
uint32_t getLastUpdate(); uint32_t getLastUpdate();
void setLastUpdate(uint32_t lastUpdate); void setLastUpdate(uint32_t lastUpdate);
void beginAppendFragment();
void endAppendFragment();
protected:
SemaphoreHandle_t _xSemaphore;
private: private:
uint32_t _lastUpdate = 0; uint32_t _lastUpdate = 0;
}; };

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Copyright (C) 2022 Thomas Basler and others * Copyright (C) 2022 - 2023 Thomas Basler and others
*/ */
#include "PowerCommandParser.h" #include "PowerCommandParser.h"

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include "Parser.h" #include "Parser.h"
#include <Arduino.h>
class PowerCommandParser : public Parser { class PowerCommandParser : public Parser {
public: public:

View File

@ -1,15 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Copyright (C) 2022 Thomas Basler and others * Copyright (C) 2022 - 2023 Thomas Basler and others
*/ */
#include "StatisticsParser.h" #include "StatisticsParser.h"
#include "../Hoymiles.h" #include "../Hoymiles.h"
#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
static float calcYieldTotalCh0(StatisticsParser* iv, uint8_t arg0); static float calcYieldTotalCh0(StatisticsParser* iv, uint8_t arg0);
static float calcYieldDayCh0(StatisticsParser* iv, uint8_t arg0); static float calcYieldDayCh0(StatisticsParser* iv, uint8_t arg0);
static float calcUdcCh(StatisticsParser* iv, uint8_t arg0); static float calcUdcCh(StatisticsParser* iv, uint8_t arg0);
@ -62,8 +57,6 @@ const FieldId_t dailyProductionFields[] = {
StatisticsParser::StatisticsParser() StatisticsParser::StatisticsParser()
: Parser() : Parser()
{ {
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
clearBuffer(); clearBuffer();
} }
@ -101,16 +94,6 @@ void StatisticsParser::appendFragment(uint8_t offset, uint8_t* payload, uint8_t
_statisticLength += len; _statisticLength += len;
} }
void StatisticsParser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}
void StatisticsParser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}
const byteAssign_t* StatisticsParser::getAssignmentByChannelField(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId) const byteAssign_t* StatisticsParser::getAssignmentByChannelField(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
{ {
for (uint8_t i = 0; i < _byteAssignmentSize; i++) { for (uint8_t i = 0; i < _byteAssignmentSize; i++) {

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include "Parser.h" #include "Parser.h"
#include <Arduino.h>
#include <cstdint> #include <cstdint>
#include <list> #include <list>
@ -107,8 +106,6 @@ public:
StatisticsParser(); StatisticsParser();
void clearBuffer(); void clearBuffer();
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
void beginAppendFragment();
void endAppendFragment();
void setByteAssignment(const byteAssign_t* byteAssignment, uint8_t size); void setByteAssignment(const byteAssign_t* byteAssignment, uint8_t size);
@ -157,6 +154,4 @@ private:
std::list<fieldSettings_t> _fieldSettings; std::list<fieldSettings_t> _fieldSettings;
uint32_t _rxFailureCount = 0; uint32_t _rxFailureCount = 0;
SemaphoreHandle_t _xSemaphore;
}; };

View File

@ -1,21 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
/* /*
* Copyright (C) 2022 Thomas Basler and others * Copyright (C) 2022 - 2023 Thomas Basler and others
*/ */
#include "SystemConfigParaParser.h" #include "SystemConfigParaParser.h"
#include "../Hoymiles.h" #include "../Hoymiles.h"
#include <cstring> #include <cstring>
#define HOY_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define HOY_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
SystemConfigParaParser::SystemConfigParaParser() SystemConfigParaParser::SystemConfigParaParser()
: Parser() : Parser()
{ {
_xSemaphore = xSemaphoreCreateMutex();
HOY_SEMAPHORE_GIVE(); // release before first use
clearBuffer(); clearBuffer();
} }
@ -35,16 +28,6 @@ void SystemConfigParaParser::appendFragment(uint8_t offset, uint8_t* payload, ui
_payloadLength += len; _payloadLength += len;
} }
void SystemConfigParaParser::beginAppendFragment()
{
HOY_SEMAPHORE_TAKE();
}
void SystemConfigParaParser::endAppendFragment()
{
HOY_SEMAPHORE_GIVE();
}
float SystemConfigParaParser::getLimitPercent() float SystemConfigParaParser::getLimitPercent()
{ {
HOY_SEMAPHORE_TAKE(); HOY_SEMAPHORE_TAKE();

View File

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include "Parser.h" #include "Parser.h"
#include <Arduino.h>
#define SYSTEM_CONFIG_PARA_SIZE 16 #define SYSTEM_CONFIG_PARA_SIZE 16
@ -10,8 +9,6 @@ public:
SystemConfigParaParser(); SystemConfigParaParser();
void clearBuffer(); void clearBuffer();
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
void beginAppendFragment();
void endAppendFragment();
float getLimitPercent(); float getLimitPercent();
void setLimitPercent(float value); void setLimitPercent(float value);
@ -35,6 +32,4 @@ private:
uint32_t _lastUpdateCommand = 0; uint32_t _lastUpdateCommand = 0;
uint32_t _lastUpdateRequest = 0; uint32_t _lastUpdateRequest = 0;
SemaphoreHandle_t _xSemaphore;
}; };