Skip to content

Commit

Permalink
Introduce several const statements
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Dec 7, 2023
1 parent e0c07b9 commit 8b5d406
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion include/WebApi_ws_live.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WebApiWsLiveClass {
private:
void generateJsonResponse(JsonVariant& root);
void addField(JsonObject& root, uint8_t idx, std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, String topic = "");
void addTotalField(JsonObject& root, String name, float value, String unit, uint8_t digits);
void addTotalField(JsonObject& root, const String& name, float value, const String& unit, uint8_t digits);
void onLivedataStatus(AsyncWebServerRequest* request);
void onWebsocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);

Expand Down
6 changes: 3 additions & 3 deletions lib/Hoymiles/src/Hoymiles.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
* Copyright (C) 2022-2023 Thomas Basler and others
*/
#include "Hoymiles.h"
#include "Utils.h"
Expand Down Expand Up @@ -126,7 +126,7 @@ void HoymilesClass::loop()
}

// Perform housekeeping of all inverters on day change
int8_t currentWeekDay = Utils::getWeekDay();
const int8_t currentWeekDay = Utils::getWeekDay();
static int8_t lastWeekDay = -1;
if (lastWeekDay == -1) {
lastWeekDay = currentWeekDay;
Expand Down Expand Up @@ -198,7 +198,7 @@ std::shared_ptr<InverterAbstract> HoymilesClass::getInverterBySerial(uint64_t se
return nullptr;
}

std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByFragment(fragment_t* fragment)
std::shared_ptr<InverterAbstract> HoymilesClass::getInverterByFragment(const fragment_t* fragment)
{
if (fragment->len <= 4) {
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/Hoymiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HoymilesClass {
std::shared_ptr<InverterAbstract> addInverter(const char* name, uint64_t serial);
std::shared_ptr<InverterAbstract> getInverterByPos(uint8_t pos);
std::shared_ptr<InverterAbstract> getInverterBySerial(uint64_t serial);
std::shared_ptr<InverterAbstract> getInverterByFragment(fragment_t* fragment);
std::shared_ptr<InverterAbstract> getInverterByFragment(const fragment_t* fragment);
void removeInverterBySerial(uint64_t serial);
size_t getNumInverters();

Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/HoymilesRadio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ serial_u HoymilesRadio::convertSerialToRadioId(serial_u serial)
return radioId;
}

bool HoymilesRadio::checkFragmentCrc(fragment_t* fragment)
bool HoymilesRadio::checkFragmentCrc(const fragment_t* fragment)
{
uint8_t crc = crc8(fragment->fragment, fragment->len - 1);
return (crc == fragment->fragment[fragment->len - 1]);
Expand Down
2 changes: 1 addition & 1 deletion lib/Hoymiles/src/HoymilesRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HoymilesRadio {
static serial_u convertSerialToRadioId(serial_u serial);
void dumpBuf(const uint8_t buf[], uint8_t len, bool appendNewline = true);

bool checkFragmentCrc(fragment_t* fragment);
bool checkFragmentCrc(const fragment_t* fragment);
virtual void sendEsbPacket(CommandAbstract* cmd) = 0;
void sendRetransmitPacket(uint8_t fragment_id);
void sendLastPacketAgain();
Expand Down
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/commands/MultiDataCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
* Copyright (C) 2022-2023 Thomas Basler and others
*/
#include "MultiDataCommand.h"
#include "crc.h"
Expand Down Expand Up @@ -88,7 +88,7 @@ bool MultiDataCommand::handleResponse(InverterAbstract* inverter, fragment_t fra

void MultiDataCommand::udpateCRC()
{
uint16_t crc = crc16(&_payload[10], 14); // From data_type till password
const uint16_t crc = crc16(&_payload[10], 14); // From data_type till password
_payload[24] = (uint8_t)(crc >> 8);
_payload[25] = (uint8_t)(crc);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Hoymiles/src/commands/RealTimeRunDataCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
* Copyright (C) 2022-2023 Thomas Basler and others
*/
#include "RealTimeRunDataCommand.h"
#include "Hoymiles.h"
Expand Down Expand Up @@ -29,8 +29,8 @@ bool RealTimeRunDataCommand::handleResponse(InverterAbstract* inverter, fragment
// Check if at least all required bytes are received
// In case of low power in the inverter it occours that some incomplete fragments
// with a valid CRC are received.
uint8_t fragmentsSize = getTotalFragmentSize(fragment, max_fragment_id);
uint8_t expectedSize = inverter->Statistics()->getExpectedByteCount();
const uint8_t fragmentsSize = getTotalFragmentSize(fragment, max_fragment_id);
const uint8_t expectedSize = inverter->Statistics()->getExpectedByteCount();
if (fragmentsSize < expectedSize) {
Hoymiles.getMessageOutput()->printf("ERROR in %s: Received fragment size: %d, min expected size: %d\r\n",
getCommandName().c_str(), fragmentsSize, expectedSize);
Expand Down
6 changes: 3 additions & 3 deletions lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
* Copyright (C) 2022-2023 Thomas Basler and others
*/
#include "SystemConfigParaCommand.h"
#include "Hoymiles.h"
Expand Down Expand Up @@ -29,8 +29,8 @@ bool SystemConfigParaCommand::handleResponse(InverterAbstract* inverter, fragmen
// Check if at least all required bytes are received
// In case of low power in the inverter it occours that some incomplete fragments
// with a valid CRC are received.
uint8_t fragmentsSize = getTotalFragmentSize(fragment, max_fragment_id);
uint8_t expectedSize = inverter->SystemConfigPara()->getExpectedByteCount();
const uint8_t fragmentsSize = getTotalFragmentSize(fragment, max_fragment_id);
const uint8_t expectedSize = inverter->SystemConfigPara()->getExpectedByteCount();
if (fragmentsSize < expectedSize) {
Hoymiles.getMessageOutput()->printf("ERROR in %s: Received fragment size: %d, min expected size: %d\r\n",
getCommandName().c_str(), fragmentsSize, expectedSize);
Expand Down
6 changes: 3 additions & 3 deletions lib/Hoymiles/src/parser/AlarmLogParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ void AlarmLogParser::setMessageType(AlarmMessageType_t type)

void AlarmLogParser::getLogEntry(uint8_t entryId, AlarmLogEntry_t* entry, AlarmMessageLocale_t locale)
{
uint8_t entryStartOffset = 2 + entryId * ALARM_LOG_ENTRY_SIZE;
const uint8_t entryStartOffset = 2 + entryId * ALARM_LOG_ENTRY_SIZE;

int timezoneOffset = getTimezoneOffset();
const int timezoneOffset = getTimezoneOffset();

HOY_SEMAPHORE_TAKE();

uint32_t wcode = (uint16_t)_payloadAlarmLog[entryStartOffset] << 8 | _payloadAlarmLog[entryStartOffset + 1];
const uint32_t wcode = (uint16_t)_payloadAlarmLog[entryStartOffset] << 8 | _payloadAlarmLog[entryStartOffset + 1];
uint32_t startTimeOffset = 0;
if (((wcode >> 13) & 0x01) == 1) {
startTimeOffset = 12 * 60 * 60;
Expand Down
17 changes: 7 additions & 10 deletions lib/Hoymiles/src/parser/DevInfoParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void DevInfoParser::setLastUpdateSimple(uint32_t lastUpdate)
uint16_t DevInfoParser::getFwBuildVersion()
{
HOY_SEMAPHORE_TAKE();
uint16_t ret = (((uint16_t)_payloadDevInfoAll[0]) << 8) | _payloadDevInfoAll[1];
const uint16_t ret = (((uint16_t)_payloadDevInfoAll[0]) << 8) | _payloadDevInfoAll[1];
HOY_SEMAPHORE_GIVE();
return ret;
}
Expand All @@ -140,19 +140,16 @@ time_t DevInfoParser::getFwBuildDateTime()
uint16_t DevInfoParser::getFwBootloaderVersion()
{
HOY_SEMAPHORE_TAKE();
uint16_t ret = (((uint16_t)_payloadDevInfoAll[8]) << 8) | _payloadDevInfoAll[9];
const uint16_t ret = (((uint16_t)_payloadDevInfoAll[8]) << 8) | _payloadDevInfoAll[9];
HOY_SEMAPHORE_GIVE();
return ret;
}

uint32_t DevInfoParser::getHwPartNumber()
{
uint16_t hwpn_h;
uint16_t hwpn_l;

HOY_SEMAPHORE_TAKE();
hwpn_h = (((uint16_t)_payloadDevInfoSimple[2]) << 8) | _payloadDevInfoSimple[3];
hwpn_l = (((uint16_t)_payloadDevInfoSimple[4]) << 8) | _payloadDevInfoSimple[5];
const uint16_t hwpn_h = (((uint16_t)_payloadDevInfoSimple[2]) << 8) | _payloadDevInfoSimple[3];
const uint16_t hwpn_l = (((uint16_t)_payloadDevInfoSimple[4]) << 8) | _payloadDevInfoSimple[5];
HOY_SEMAPHORE_GIVE();

return ((uint32_t)hwpn_h << 16) | ((uint32_t)hwpn_l);
Expand All @@ -169,7 +166,7 @@ String DevInfoParser::getHwVersion()

uint16_t DevInfoParser::getMaxPower()
{
uint8_t idx = getDevIdx();
const uint8_t idx = getDevIdx();
if (idx == 0xff) {
return 0;
}
Expand All @@ -178,7 +175,7 @@ uint16_t DevInfoParser::getMaxPower()

String DevInfoParser::getHwModelName()
{
uint8_t idx = getDevIdx();
const uint8_t idx = getDevIdx();
if (idx == 0xff) {
return "";
}
Expand All @@ -187,7 +184,7 @@ String DevInfoParser::getHwModelName()

bool DevInfoParser::containsValidData()
{
time_t t = getFwBuildDateTime();
const time_t t = getFwBuildDateTime();

struct tm info;
localtime_r(&t, &info);
Expand Down
17 changes: 8 additions & 9 deletions lib/Hoymiles/src/parser/StatisticsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ fieldSettings_t* StatisticsParser::getSettingByChannelField(ChannelType_t type,
float StatisticsParser::getChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
{
const byteAssign_t* pos = getAssignmentByChannelField(type, channel, fieldId);
fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId);

if (pos == NULL) {
return 0;
}

uint8_t ptr = pos->start;
uint8_t end = ptr + pos->num;
uint16_t div = pos->div;
const uint8_t end = ptr + pos->num;
const uint16_t div = pos->div;

if (CMD_CALC != div) {
// Value is a static value
Expand All @@ -174,6 +172,8 @@ float StatisticsParser::getChannelFieldValue(ChannelType_t type, ChannelNum_t ch
}

result /= static_cast<float>(div);

const fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId);
if (setting != NULL && _statisticLength > 0) {
result += setting->offset;
}
Expand All @@ -189,20 +189,19 @@ float StatisticsParser::getChannelFieldValue(ChannelType_t type, ChannelNum_t ch
bool StatisticsParser::setChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId, float value)
{
const byteAssign_t* pos = getAssignmentByChannelField(type, channel, fieldId);
fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId);

if (pos == NULL) {
return false;
}

uint8_t ptr = pos->start + pos->num - 1;
uint8_t end = pos->start;
uint16_t div = pos->div;
const uint8_t end = pos->start;
const uint16_t div = pos->div;

if (CMD_CALC == div) {
return false;
}

const fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId);
if (setting != NULL) {
value -= setting->offset;
}
Expand Down Expand Up @@ -260,7 +259,7 @@ uint8_t StatisticsParser::getChannelFieldDigits(ChannelType_t type, ChannelNum_t

float StatisticsParser::getChannelFieldOffset(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
{
fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId);
const fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId);
if (setting != NULL) {
return setting->offset;
}
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_ws_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void WebApiWsLiveClass::addField(JsonObject& root, uint8_t idx, std::shared_ptr<
}
}

void WebApiWsLiveClass::addTotalField(JsonObject& root, String name, float value, String unit, uint8_t digits)
void WebApiWsLiveClass::addTotalField(JsonObject& root, const String& name, float value, const String& unit, uint8_t digits)
{
root[name]["v"] = value;
root[name]["u"] = unit;
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void setup()
MessageOutput.print("migrated... ");
Configuration.migrate();
}
CONFIG_T& config = Configuration.get();
auto& config = Configuration.get();
MessageOutput.println("done");

// Load PinMapping
Expand All @@ -78,7 +78,7 @@ void setup()
} else {
MessageOutput.print("using default config ");
}
const PinMapping_t& pin = PinMapping.get();
const auto& pin = PinMapping.get();
MessageOutput.println("done");

// Initialize WiFi
Expand Down Expand Up @@ -137,7 +137,7 @@ void setup()
MessageOutput.print("Check for default DTU serial... ");
if (config.Dtu.Serial == DTU_SERIAL) {
MessageOutput.print("generate serial based on ESP chip id: ");
uint64_t dtuId = Utils::generateDtuSerial();
const uint64_t dtuId = Utils::generateDtuSerial();
MessageOutput.printf("%0x%08x... ",
((uint32_t)((dtuId >> 32) & 0xFFFFFFFF)),
((uint32_t)(dtuId & 0xFFFFFFFF)));
Expand Down

0 comments on commit 8b5d406

Please sign in to comment.