From 8b5d406a4fc6dab91da54eccaed8bd0b14cd7957 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Thu, 7 Dec 2023 15:19:04 +0100 Subject: [PATCH] Introduce several const statements --- include/WebApi_ws_live.h | 2 +- lib/Hoymiles/src/Hoymiles.cpp | 6 +++--- lib/Hoymiles/src/Hoymiles.h | 2 +- lib/Hoymiles/src/HoymilesRadio.cpp | 2 +- lib/Hoymiles/src/HoymilesRadio.h | 2 +- lib/Hoymiles/src/commands/MultiDataCommand.cpp | 4 ++-- .../src/commands/RealTimeRunDataCommand.cpp | 6 +++--- .../src/commands/SystemConfigParaCommand.cpp | 6 +++--- lib/Hoymiles/src/parser/AlarmLogParser.cpp | 6 +++--- lib/Hoymiles/src/parser/DevInfoParser.cpp | 17 +++++++---------- lib/Hoymiles/src/parser/StatisticsParser.cpp | 17 ++++++++--------- src/WebApi_ws_live.cpp | 2 +- src/main.cpp | 6 +++--- 13 files changed, 37 insertions(+), 41 deletions(-) diff --git a/include/WebApi_ws_live.h b/include/WebApi_ws_live.h index 1e6649200..ab7d1a43f 100644 --- a/include/WebApi_ws_live.h +++ b/include/WebApi_ws_live.h @@ -14,7 +14,7 @@ class WebApiWsLiveClass { private: void generateJsonResponse(JsonVariant& root); void addField(JsonObject& root, uint8_t idx, std::shared_ptr 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); diff --git a/lib/Hoymiles/src/Hoymiles.cpp b/lib/Hoymiles/src/Hoymiles.cpp index 937be53cf..05a12ef3b 100644 --- a/lib/Hoymiles/src/Hoymiles.cpp +++ b/lib/Hoymiles/src/Hoymiles.cpp @@ -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" @@ -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; @@ -198,7 +198,7 @@ std::shared_ptr HoymilesClass::getInverterBySerial(uint64_t se return nullptr; } -std::shared_ptr HoymilesClass::getInverterByFragment(fragment_t* fragment) +std::shared_ptr HoymilesClass::getInverterByFragment(const fragment_t* fragment) { if (fragment->len <= 4) { return nullptr; diff --git a/lib/Hoymiles/src/Hoymiles.h b/lib/Hoymiles/src/Hoymiles.h index 11b847632..7b37efc3e 100644 --- a/lib/Hoymiles/src/Hoymiles.h +++ b/lib/Hoymiles/src/Hoymiles.h @@ -26,7 +26,7 @@ class HoymilesClass { std::shared_ptr addInverter(const char* name, uint64_t serial); std::shared_ptr getInverterByPos(uint8_t pos); std::shared_ptr getInverterBySerial(uint64_t serial); - std::shared_ptr getInverterByFragment(fragment_t* fragment); + std::shared_ptr getInverterByFragment(const fragment_t* fragment); void removeInverterBySerial(uint64_t serial); size_t getNumInverters(); diff --git a/lib/Hoymiles/src/HoymilesRadio.cpp b/lib/Hoymiles/src/HoymilesRadio.cpp index 4afc447a7..e1e101db2 100644 --- a/lib/Hoymiles/src/HoymilesRadio.cpp +++ b/lib/Hoymiles/src/HoymilesRadio.cpp @@ -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]); diff --git a/lib/Hoymiles/src/HoymilesRadio.h b/lib/Hoymiles/src/HoymilesRadio.h index fa2f6945d..69ef1e91e 100644 --- a/lib/Hoymiles/src/HoymilesRadio.h +++ b/lib/Hoymiles/src/HoymilesRadio.h @@ -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(); diff --git a/lib/Hoymiles/src/commands/MultiDataCommand.cpp b/lib/Hoymiles/src/commands/MultiDataCommand.cpp index 39a0d4c64..396406092 100644 --- a/lib/Hoymiles/src/commands/MultiDataCommand.cpp +++ b/lib/Hoymiles/src/commands/MultiDataCommand.cpp @@ -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" @@ -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); } diff --git a/lib/Hoymiles/src/commands/RealTimeRunDataCommand.cpp b/lib/Hoymiles/src/commands/RealTimeRunDataCommand.cpp index 3f0aed36b..1be41f9fe 100644 --- a/lib/Hoymiles/src/commands/RealTimeRunDataCommand.cpp +++ b/lib/Hoymiles/src/commands/RealTimeRunDataCommand.cpp @@ -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" @@ -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); diff --git a/lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp b/lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp index 5e238a59b..ca42f48f1 100644 --- a/lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp +++ b/lib/Hoymiles/src/commands/SystemConfigParaCommand.cpp @@ -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" @@ -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); diff --git a/lib/Hoymiles/src/parser/AlarmLogParser.cpp b/lib/Hoymiles/src/parser/AlarmLogParser.cpp index fe2d2bab4..41ce4cd7d 100644 --- a/lib/Hoymiles/src/parser/AlarmLogParser.cpp +++ b/lib/Hoymiles/src/parser/AlarmLogParser.cpp @@ -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; diff --git a/lib/Hoymiles/src/parser/DevInfoParser.cpp b/lib/Hoymiles/src/parser/DevInfoParser.cpp index 1de00092d..52edd8c1a 100644 --- a/lib/Hoymiles/src/parser/DevInfoParser.cpp +++ b/lib/Hoymiles/src/parser/DevInfoParser.cpp @@ -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; } @@ -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); @@ -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; } @@ -178,7 +175,7 @@ uint16_t DevInfoParser::getMaxPower() String DevInfoParser::getHwModelName() { - uint8_t idx = getDevIdx(); + const uint8_t idx = getDevIdx(); if (idx == 0xff) { return ""; } @@ -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); diff --git a/lib/Hoymiles/src/parser/StatisticsParser.cpp b/lib/Hoymiles/src/parser/StatisticsParser.cpp index 84f19299c..b436f430d 100644 --- a/lib/Hoymiles/src/parser/StatisticsParser.cpp +++ b/lib/Hoymiles/src/parser/StatisticsParser.cpp @@ -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 @@ -174,6 +172,8 @@ float StatisticsParser::getChannelFieldValue(ChannelType_t type, ChannelNum_t ch } result /= static_cast(div); + + const fieldSettings_t* setting = getSettingByChannelField(type, channel, fieldId); if (setting != NULL && _statisticLength > 0) { result += setting->offset; } @@ -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; } @@ -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; } diff --git a/src/WebApi_ws_live.cpp b/src/WebApi_ws_live.cpp index d0a88b733..41ebb84be 100644 --- a/src/WebApi_ws_live.cpp +++ b/src/WebApi_ws_live.cpp @@ -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; diff --git a/src/main.cpp b/src/main.cpp index e7dd14d2a..cf9f4fa84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 @@ -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 @@ -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)));