From ce6ea68f2263b31f53e0bc1423e003020b461668 Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 17 Sep 2022 23:46:25 +0200 Subject: [PATCH] formatting, remove unused --- src/ebusd/bushandler.cpp | 4 +- src/ebusd/knxhandler.cpp | 97 ++++++++++++----------- src/ebusd/knxhandler.h | 14 ++-- src/ebusd/main.cpp | 6 +- src/ebusd/mqtthandler.cpp | 8 +- src/ebusd/mqtthandler.h | 6 +- src/lib/ebus/datatype.cpp | 2 +- src/lib/ebus/device.cpp | 17 ++-- src/lib/ebus/device.h | 3 +- src/lib/ebus/stringhelper.cpp | 7 +- src/lib/ebus/stringhelper.h | 5 +- src/lib/knx/knx.cpp | 4 +- src/lib/knx/knx.h | 9 ++- src/lib/knx/knxd.h | 4 +- src/lib/knx/knxnet.h | 143 +++++++++++++++++----------------- src/lib/utils/clock.cpp | 2 +- src/lib/utils/clock.h | 3 +- src/lib/utils/httpclient.cpp | 2 +- src/lib/utils/thread.cpp | 4 +- src/tools/ebuspicloader.cpp | 38 ++++----- 20 files changed, 199 insertions(+), 179 deletions(-) diff --git a/src/ebusd/bushandler.cpp b/src/ebusd/bushandler.cpp index edebc5c8..0e41d377 100644 --- a/src/ebusd/bushandler.cpp +++ b/src/ebusd/bushandler.cpp @@ -784,7 +784,7 @@ result_t BusHandler::handleSymbol() { // check arbitration if (recvSymbol == sendSymbol) { // arbitration successful // measure arbitration delay - long long latencyLong = (sentTime.tv_sec*1000000000 + sentTime.tv_nsec + int64_t latencyLong = (sentTime.tv_sec*1000000000 + sentTime.tv_nsec - m_lastSynReceiveTime.tv_sec*1000000000 - m_lastSynReceiveTime.tv_nsec)/1000; if (latencyLong >= 0 && latencyLong <= 10000) { // skip clock skew or out of reasonable range auto latency = static_cast(latencyLong); @@ -1113,7 +1113,7 @@ result_t BusHandler::setState(BusState state, result_t result, bool firstRepetit } void BusHandler::measureLatency(struct timespec* sentTime, struct timespec* recvTime) { - long long latencyLong = (recvTime->tv_sec*1000000000 + recvTime->tv_nsec + int64_t latencyLong = (recvTime->tv_sec*1000000000 + recvTime->tv_nsec - sentTime->tv_sec*1000000000 - sentTime->tv_nsec)/1000000; if (latencyLong < 0 || latencyLong > 1000) { return; // clock skew or out of reasonable range diff --git a/src/ebusd/knxhandler.cpp b/src/ebusd/knxhandler.cpp index b4bdf4f8..c9a892d0 100644 --- a/src/ebusd/knxhandler.cpp +++ b/src/ebusd/knxhandler.cpp @@ -43,7 +43,7 @@ using std::dec; // 5 bits magic (to be incremented with incompatible changes, not shown) // 5 bits major, using major directly // 6 bits minor, using minor multiplied by 10 to have space for micro versioning in future -#define VERSION_INT ((PACKAGE_VERSION_MAJOR<<6)|(PACKAGE_VERSION_MINOR*10)) +#define VERSION_INT ((PACKAGE_VERSION_MAJOR << 6) |(PACKAGE_VERSION_MINOR*10)) #define O_URL -2 #define O_AGR (O_URL-1) @@ -59,8 +59,10 @@ static const struct argp_option g_knx_argp_options[] = { " or \"ip:host[:port]\" / \"local:/socketpath\" for knxd" #endif ") []", 0 }, - {"knxrage", O_AGR, "SEC", 0, "Maximum age in seconds for using the last value of read messages (0=disable) [5]", 0 }, - {"knxwage", O_AGW, "SEC", 0, "Maximum age in seconds for using the last value for reads on write messages (0=disable), [99999999]", 0 }, + {"knxrage", O_AGR, "SEC", 0, "Maximum age in seconds for using the last value of read messages (0=disable)" + " [5]", 0 }, + {"knxwage", O_AGW, "SEC", 0, "Maximum age in seconds for using the last value for reads on write messages" + " (0=disable), [99999999]", 0 }, {"knxint", O_INT, "FILE", 0, "Read KNX integration settings from FILE [/etc/ebusd/knx.cfg]", 0 }, {"knxvar", O_VAR, "NAME=VALUE", 0, "Add a variable to the read KNX integration settings", 0 }, @@ -69,7 +71,8 @@ static const struct argp_option g_knx_argp_options[] = { static const char* g_url = nullptr; //!< URL of KNX daemon static unsigned int g_maxReadAge = 5; //!< max age in seconds for using the last value of read messages -static unsigned int g_maxWriteAge = 99999999; //!< max age in seconds for using the last value for reads on write messages +// max age in seconds for using the last value for reads on write messages +static unsigned int g_maxWriteAge = 99999999; static const char* g_integrationFile = nullptr; //!< the integration settings file static vector* g_integrationVars = nullptr; //!< the integration settings variables @@ -259,7 +262,7 @@ void KnxHandler::startHandler() { void KnxHandler::notifyUpdateCheckResult(const string& checkResult) { if (checkResult != m_lastUpdateCheckResult) { m_lastUpdateCheckResult = checkResult; - sendGlobalValue(GLOBAL_UPDATECHECK, checkResult.empty() || checkResult=="OK" ? 0 : 1); + sendGlobalValue(GLOBAL_UPDATECHECK, checkResult.empty() || checkResult == "OK" ? 0 : 1); } } @@ -269,7 +272,7 @@ void KnxHandler::notifyScanStatus(scanStatus_t scanStatus) { } if (scanStatus != m_lastScanStatus) { m_lastScanStatus = scanStatus; - sendGlobalValue(GLOBAL_SCAN, m_lastScanStatus==SCAN_STATUS_RUNNING ? 1 : 0); + sendGlobalValue(GLOBAL_SCAN, m_lastScanStatus == SCAN_STATUS_RUNNING ? 1 : 0); } } @@ -288,20 +291,20 @@ result_t getFieldLength(const SingleDataField *field, dtlf_t *length) { return RESULT_OK; } const auto nt = dynamic_cast(dt); - if (nt->getDivisor()!=1) { + if (nt->getDivisor() != 1) { // adjust bit count to 2 octet or 4 octet float DPT - if (bitCnt>=24 && bitCnt<31) { + if (bitCnt >= 24 && bitCnt < 31) { bitCnt = 32; - } else if (bitCnt<16) { + } else if (bitCnt < 16) { bitCnt = 16; } // TODO uncommon divisor (e.g. >100) may not fit into KNX 2-octet float or truncates precision - } else if (bitCnt>=24 && bitCnt<31) { + } else if (bitCnt >= 24 && bitCnt < 31) { // adjust bit count for non-existent 24 bit KNX type bitCnt = 32; } *length = {{ - .hasDivisor = nt->getDivisor()!=1, + .hasDivisor = nt->getDivisor() != 1, .isFloat = dt->hasFlag(EXP), .isSigned = dt->hasFlag(SIG), .lastValueSent = false, @@ -339,7 +342,7 @@ float int16ToFloat(uint16_t val) { return 0; } if (val == 0x7fff) { - return static_cast(0xffffffff); // NaN + return static_cast(0xffffffff); // NaN } bool negative = val&0x8000; int exp = (val>>11)&0xf; @@ -347,7 +350,8 @@ float int16ToFloat(uint16_t val) { return static_cast(sig * exp2(exp) * (negative ? -0.01 : 0.01)); } -result_t KnxHandler::sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& lengthFlag, unsigned int value, const SingleDataField *field) const { +result_t KnxHandler::sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& lengthFlag, unsigned int value, +const SingleDataField *field) const { if (!m_con || !m_con->isConnected() || !m_con->getAddress()) { return RESULT_EMPTY; } @@ -365,7 +369,7 @@ result_t KnxHandler::sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& length result_t ret = nt->getFloatFromRawValue(value, &fval); if (ret == RESULT_EMPTY) { // replacement value: - if (lengthFlag.length==2) { + if (lengthFlag.length == 2) { // shall have 0x7fff for DPT 9 value = 0x7fff; } else { @@ -384,7 +388,7 @@ result_t KnxHandler::sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& length } } // else signed values: fine as long as length is identical - if (apci==APCI_GROUPVALUE_WRITE && lengthFlag.lastValueSent && lengthFlag.lastValue==value) { + if (apci == APCI_GROUPVALUE_WRITE && lengthFlag.lastValueSent && lengthFlag.lastValue == value) { return RESULT_EMPTY; // no need to send the same group value again } lengthFlag.lastValue = value; @@ -413,12 +417,12 @@ result_t KnxHandler::sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& length const char* err = m_con->sendGroup(dest, len, data); if (err) { logOtherError("knx", "unable to send %s, dest %4.4x, len %d", - apci==APCI_GROUPVALUE_WRITE ? "write" : apci==APCI_GROUPVALUE_READ ? "read" : "response", + apci == APCI_GROUPVALUE_WRITE ? "write" : apci == APCI_GROUPVALUE_READ ? "read" : "response", dest, len); return RESULT_ERR_SEND; } logOtherDebug("knx", "sent %s, dest %4.4x, len %d", - apci==APCI_GROUPVALUE_WRITE ? "write" : apci==APCI_GROUPVALUE_READ ? "read" : "response", + apci == APCI_GROUPVALUE_WRITE ? "write" : apci == APCI_GROUPVALUE_READ ? "read" : "response", dest, len); return RESULT_OK; } @@ -503,24 +507,25 @@ void printResponse(knx_addr_t src, knx_addr_t dest, int len, const uint8_t *data if ((apci & APCI_GROUPVALUE_READ_MASK) == 0) { apci &= ~APCI_GROUPVALUE_READ_MASK; } - int value = len==2 ? data[1]&0x3f : data[2]; // 6 bits or full octet + int value = len == 2 ? data[1]&0x3f : data[2]; // 6 bits or full octet if (len>3) { - value = (value<<8) | data[3]; // up to 16 bits + value = (value<<8) | data[3]; // up to 16 bits } if (len>4) { - value = (value<<8) | data[4]; // up to 24 bits + value = (value<<8) | data[4]; // up to 24 bits } if (len>5) { - value = (value<<8) | data[5]; // up to 32 bits + value = (value<<8) | data[5]; // up to 32 bits } logOtherDebug("knx", "recv from %4.4x to %4.4x, %s (0x%3.3x, tctrl 0x%2.2x), len %d", src, dest, - apci==APCI_GROUPVALUE_WRITE ? "write" : apci==APCI_GROUPVALUE_READ ? "read" - : apci==APCI_GROUPVALUE_RESPONSE ? "response" : "other", + apci == APCI_GROUPVALUE_WRITE ? "write" : apci == APCI_GROUPVALUE_READ ? "read" + : apci == APCI_GROUPVALUE_RESPONSE ? "response" : "other", apci, tctrl, len); } */ -void KnxHandler::handleReceivedTelegram(knx_transfer_t typ, knx_addr_t src, knx_addr_t dest, int len, const uint8_t *data) { +void KnxHandler::handleReceivedTelegram(knx_transfer_t typ, knx_addr_t src, knx_addr_t dest, int len, +const uint8_t *data) { if (typ == KNX_TRANSFER_GROUP) { handleGroupTelegram(src, dest, len, data); return; @@ -542,7 +547,8 @@ void KnxHandler::sendNonGroupDisconnect(knx_addr_t dest) { // the connection timeout in millis (6 seconds) #define CONNECTION_TIMEOUT 6000 -void KnxHandler::handleNonGroupTelegram(knx_transfer_t typ, knx_addr_t src, knx_addr_t dest, int len, const uint8_t *data) { +void KnxHandler::handleNonGroupTelegram(knx_transfer_t typ, knx_addr_t src, knx_addr_t dest, int len, +const uint8_t *data) { if (typ == KNX_TRANSFER_NONE) { return; } @@ -552,24 +558,24 @@ void KnxHandler::handleNonGroupTelegram(knx_transfer_t typ, knx_addr_t src, knx_ void KnxHandler::handleGroupTelegram(knx_addr_t src, knx_addr_t dest, int len, const uint8_t *data) { time_t now; time(&now); - int apci = ((data[0]&0x03)<<8) | data[1]; + int apci = ((data[0]&0x03) << 8) | data[1]; int groupReadWriteApci = apci & APCI_GROUPVALUE_READ_WRITE_MASK; if (groupReadWriteApci == APCI_GROUPVALUE_WRITE || groupReadWriteApci == APCI_GROUPVALUE_READ) { apci = groupReadWriteApci; } - bool isWrite = apci==APCI_GROUPVALUE_WRITE; - if (apci!=APCI_GROUPVALUE_READ && !isWrite) { + bool isWrite = apci == APCI_GROUPVALUE_WRITE; + if (apci != APCI_GROUPVALUE_READ && !isWrite) { if (m_con->isProgrammingMode()) { - if (apci == APCI_INDIVIDUALADDRESS_READ && m_lastIndividualAddressResponseTime>8, APCI_INDIVIDUALADDRESS_RESPONSE&0xff}; + if (apci == APCI_INDIVIDUALADDRESS_READ && m_lastIndividualAddressResponseTime < now-3) { // timeout 3 seconds + uint8_t buf[] = {APCI_INDIVIDUALADDRESS_RESPONSE >> 8, APCI_INDIVIDUALADDRESS_RESPONSE&0xff}; logOtherNotice("knx", "answering to A_IndividualAddress_Read"); if (m_con->sendGroup(0, 2, buf)) { logOtherDebug("knx", "cannot send"); } else { m_lastIndividualAddressResponseTime = now; } - } else if (apci==APCI_INDIVIDUALADDRESS_WRITE && len==4 && !m_con->getAddress() && (data[2]|data[3])) { - m_con->setAddress((data[2]<<8)|data[3]); + } else if (apci == APCI_INDIVIDUALADDRESS_WRITE && len == 4 && !m_con->getAddress() && (data[2]|data[3])) { + m_con->setAddress(static_cast((data[2] << 8)|data[3])); m_lastIndividualAddressResponseTime = 0; logOtherNotice("knx", "received new address %x", m_con->getAddress()); } @@ -581,7 +587,7 @@ void KnxHandler::handleGroupTelegram(knx_addr_t src, knx_addr_t dest, int len, c if (needsLog(lf_other, ll_debug)) { logOtherDebug("knx", "received %ssubscribed %s from %4.4x to %4.4x, len %d", sit == m_subscribedGroups.end() ? "un" : "", - apci==APCI_GROUPVALUE_WRITE ? "write" : apci==APCI_GROUPVALUE_READ ? "read" : "response", + apci == APCI_GROUPVALUE_WRITE ? "write" : apci == APCI_GROUPVALUE_READ ? "read" : "response", src, dest, len); } if (sit == m_subscribedGroups.end()) { @@ -603,10 +609,11 @@ void KnxHandler::handleGroupTelegram(knx_addr_t src, knx_addr_t dest, int len, c sendGlobalValue(GLOBAL_SIGNAL, m_busHandler->hasSignal() ? 1 : 0, true); break; case GLOBAL_SCAN: - sendGlobalValue(GLOBAL_SCAN, m_lastScanStatus==SCAN_STATUS_RUNNING ? 1 : 0, true); + sendGlobalValue(GLOBAL_SCAN, m_lastScanStatus == SCAN_STATUS_RUNNING ? 1 : 0, true); break; case GLOBAL_UPDATECHECK: - sendGlobalValue(GLOBAL_UPDATECHECK, m_lastUpdateCheckResult.empty() || m_lastUpdateCheckResult=="OK" || m_lastUpdateCheckResult=="." ? 0 : 1, true); + sendGlobalValue(GLOBAL_UPDATECHECK, m_lastUpdateCheckResult.empty() || m_lastUpdateCheckResult == "OK" + || m_lastUpdateCheckResult == "." ? 0 : 1, true); break; default: return; // ignore @@ -655,15 +662,15 @@ void KnxHandler::handleGroupTelegram(knx_addr_t src, knx_addr_t dest, int len, c result_t res; const string circuit = msg->getCircuit(), name = msg->getName(), fieldName = msg->getFieldName(fieldIndex); if (isWrite) { - unsigned int value = len==2 ? data[1]&0x3f : data[2]; // <=6 bits or full octet - if (len>3) { - value = (value<<8) | data[3]; // up to 16 bits + unsigned int value = len == 2 ? data[1]&0x3f : data[2]; // <=6 bits or full octet + if (len > 3) { + value = (value << 8) | data[3]; // up to 16 bits } - if (len>4) { - value = (value<<8) | data[4]; // up to 24 bits + if (len > 4) { + value = (value << 8) | data[4]; // up to 24 bits } - if (len>5) { - value = (value<<8) | data[5]; // up to 32 bits + if (len > 5) { + value = (value << 8) | data[5]; // up to 32 bits } // note: a write from KNX updates the message and thus re-sends the write later on again during update check logOtherNotice("knx", "received write request from %4.4x to %4.4x for %s/%s/%s, value %d", @@ -691,7 +698,7 @@ void KnxHandler::handleGroupTelegram(knx_addr_t src, knx_addr_t dest, int len, c } else { if (lengthFlag.isSigned) { // signed values: determine sign - uint32_t bit = 1<<(lengthFlag.length*8-1); + uint32_t bit = 1 << (lengthFlag.length*8-1); if (value & bit) { value = -(value&~bit); } @@ -801,7 +808,7 @@ void KnxHandler::run() { continue; } ssize_t fieldCount = static_cast(message->getFieldCount()); - if (isWrite && fieldCount>1) { + if (isWrite && fieldCount > 1) { // impossible with more than one field continue; } @@ -878,7 +885,7 @@ void KnxHandler::run() { } } } - if (addCnt>0) { + if (addCnt > 0) { logOtherInfo("knx", "added %d associations, %d active now", addCnt, m_subscribedGroups.size()); } definitionsSince = now; diff --git a/src/ebusd/knxhandler.h b/src/ebusd/knxhandler.h index 375d2799..092627b0 100644 --- a/src/ebusd/knxhandler.h +++ b/src/ebusd/knxhandler.h @@ -88,7 +88,7 @@ typedef union { bool isFloat: 1; bool isSigned: 1; bool lastValueSent: 1; - uint8_t length; // 0 for 1-6 bits, number of bytes otherwise + uint8_t length; // 0 for 1-6 bits, number of bytes otherwise uint32_t lastValue; }; uint64_t value; @@ -155,7 +155,8 @@ class KnxHandler : public DataSink, public DataSource, public WaitThread { * @param field the message field or nullptr for non field related. * @return the result code. */ - result_t sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& lengthFlag, unsigned int value, const SingleDataField *field = nullptr) const; + result_t sendGroupValue(knx_addr_t dest, apci_t apci, dtlf_t& lengthFlag, unsigned int value, + const SingleDataField *field = nullptr) const; /** * Send a global value to the registered group address. @@ -175,7 +176,8 @@ class KnxHandler : public DataSink, public DataSource, public WaitThread { * @return the result code, either RESULT_OK on success, RESULT_ERR_GENERIC_IO on I/O error (e.g. socket closed), * or RESULT_ERR_TIMEOUT if no data is available. */ - result_t receiveTelegram(int maxlen, knx_transfer_t* typ, uint8_t *buf, int *recvlen, knx_addr_t *src, knx_addr_t *dest); + result_t receiveTelegram(int maxlen, knx_transfer_t* typ, uint8_t *buf, int *recvlen, knx_addr_t *src, + knx_addr_t *dest); /** * Handle a received KNX telegram. @@ -237,7 +239,7 @@ class KnxHandler : public DataSink, public DataSource, public WaitThread { mapm_subscribedGroups; /** the group address and flags (key of m_subscribedGroups) by subscribed message key. */ - map>m_subscribedMessages; + map >m_subscribedMessages; /** the group address and flags (key of m_subscribedGroups) by subscribed global values. */ mapm_subscribedGlobals; @@ -252,7 +254,7 @@ class KnxHandler : public DataSink, public DataSource, public WaitThread { time_t m_lastIndividualAddressResponseTime = 0; /** the time of the last connection, or 0 if not connected. */ - long long m_lastConnectTime = 0; + uint64_t m_lastConnectTime = 0; /** the source address of the last connection, or 0. */ knx_addr_t m_lastConnectSource = 0; @@ -276,7 +278,7 @@ class KnxHandler : public DataSink, public DataSource, public WaitThread { bool m_scanFinishReceived; /** the last system time when a communication error was logged. */ - long long m_lastErrorLogTime; + time_t m_lastErrorLogTime; }; } // namespace ebusd diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index 856e357d..032ee679 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -222,7 +222,8 @@ static const struct argp_option argpoptions[] = { {"inject", 'i', "stop", OPTION_ARG_OPTIONAL, "Inject remaining arguments as already seen messages (e.g. " "\"FF08070400/0AB5454850303003277201\"), optionally stop afterwards", 0 }, #ifdef HAVE_SSL - {"cafile", O_CAFILE, "FILE", 0, "Use CA FILE for checking certificates (uses defaults, \"#\" for insecure)", 0 }, + {"cafile", O_CAFILE, "FILE", 0, "Use CA FILE for checking certificates (uses defaults," + " \"#\" for insecure)", 0 }, {"capath", O_CAPATH, "PATH", 0, "Use CA PATH for checking certificates (uses defaults)", 0 }, #endif // HAVE_SSL @@ -1371,7 +1372,8 @@ int main(int argc, char* argv[]) { } size_t pos = s_configPath.find(PREVIOUS_CONFIG_PATH_SUFFIX); if (pos != string::npos) { - string newPath = s_configPath.substr(0, pos) + CONFIG_PATH_SUFFIX + s_configPath.substr(pos+strlen(PREVIOUS_CONFIG_PATH_SUFFIX)); + string newPath = s_configPath.substr(0, pos) + CONFIG_PATH_SUFFIX + + s_configPath.substr(pos+strlen(PREVIOUS_CONFIG_PATH_SUFFIX)); logNotice(lf_main, "replaced old configPath %s with new one: %s", s_configPath.c_str(), newPath.c_str()); s_configPath = newPath; } diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index 66dd99e8..53969a3a 100755 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -374,7 +374,8 @@ bool mqtthandler_register(UserInfo* userInfo, BusHandler* busHandler, MessageMap int revision = -1; mosquitto_lib_version(&major, &minor, &revision); if (major < LIBMOSQUITTO_MAJOR) { - logOtherError("mqtt", "invalid mosquitto version %d instead of %d, will try connecting anyway", major, LIBMOSQUITTO_MAJOR); + logOtherError("mqtt", "invalid mosquitto version %d instead of %d, will try connecting anyway", major, + LIBMOSQUITTO_MAJOR); } logOtherInfo("mqtt", "mosquitto version %d.%d.%d (compiled with %d.%d.%d)", major, minor, revision, LIBMOSQUITTO_MAJOR, LIBMOSQUITTO_MINOR, LIBMOSQUITTO_REVISION); @@ -482,7 +483,8 @@ string removeTrailingNonTopicPart(const string& str) { MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap* messages) : DataSink(userInfo, "mqtt"), DataSource(busHandler), WaitThread(), m_messages(messages), m_connected(false), - m_initialConnectFailed(false), m_lastUpdateCheckResult("."), m_lastScanStatus(SCAN_STATUS_NONE), m_lastErrorLogTime(0) { + m_initialConnectFailed(false), m_lastUpdateCheckResult("."), m_lastScanStatus(SCAN_STATUS_NONE), + m_lastErrorLogTime(0) { m_definitionsSince = 0; m_mosquitto = nullptr; bool hasIntegration = false; @@ -540,7 +542,7 @@ MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap* m_replacers.set("version", PACKAGE_VERSION); if (m_replacers["prefix"].empty()) { string line = m_replacers.get("topic", true); - if (line.empty() || line=="/") { + if (line.empty() || line == "/") { line = string(PACKAGE); // ensure prefix if cmdline topic is absent } m_replacers.set("prefix", line); diff --git a/src/ebusd/mqtthandler.h b/src/ebusd/mqtthandler.h index ba0f887f..d4f4ad78 100644 --- a/src/ebusd/mqtthandler.h +++ b/src/ebusd/mqtthandler.h @@ -20,9 +20,10 @@ #define EBUSD_MQTTHANDLER_H_ #include +#include #include #include -#include +#include #include #include "ebusd/datahandler.h" #include "ebusd/bushandler.h" @@ -36,6 +37,7 @@ namespace ebusd { */ using std::map; +using std::pair; using std::string; using std::vector; @@ -185,7 +187,7 @@ class MqttHandler : public DataSink, public DataSource, public WaitThread { bool m_hasDefinitionFieldsPayload; /** map of type name to a list of pairs of wildcard string and mapped value. */ - map>> m_typeSwitches; + map>> m_typeSwitches; /** the subscribed configuration restart topic, or empty. */ string m_subscribeConfigRestartTopic; diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index d321bd4d..3fbba6a0 100755 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -1011,7 +1011,7 @@ result_t NumberDataType::getRawValueFromFloat(float val, unsigned int* output) c } else { if (m_divisor == 1) { if (hasFlag(SIG)) { - long signedValue = static_cast(val); // TODO static_c? + long signedValue = static_cast(val); // TODO static_c? if (signedValue < 0 && m_bitCount != 32) { value = (unsigned int)(signedValue + (1 << m_bitCount)); } else { diff --git a/src/lib/ebus/device.cpp b/src/lib/ebus/device.cpp index 993976a1..f40cdda5 100755 --- a/src/lib/ebus/device.cpp +++ b/src/lib/ebus/device.cpp @@ -465,7 +465,8 @@ bool Device::available() { ch = m_buffer[(pos+m_bufPos+1)%m_bufSize]; if (!(ch&ENH_BYTE_FLAG) || (ch&ENH_BYTE_MASK) != ENH_BYTE2) { #ifdef DEBUG_RAW_TRAFFIC - fprintf(stdout, "raw avail enhanced following bad @%d+%d %2.2x %2.2x\n", m_bufPos, pos, m_buffer[(pos+m_bufPos)%m_bufSize], ch); + fprintf(stdout, "raw avail enhanced following bad @%d+%d %2.2x %2.2x\n", m_bufPos, pos, + m_buffer[(pos+m_bufPos)%m_bufSize], ch); fflush(stdout); #endif if (m_listener != nullptr) { @@ -654,17 +655,17 @@ bool Device::read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrati ostringstream stream; switch ((m_infoLen << 8) | m_infoId) { case 0x0200: - case 0x0500: // with firmware version and jumper info - case 0x0800: // with firmware version, jumper info, and bootloader version - stream << "firmware " << static_cast(m_infoBuf[0]) << "." // version minor - << std::hex << static_cast(m_infoBuf[1]); // features mask - if (m_infoLen>=5) { + case 0x0500: // with firmware version and jumper info + case 0x0800: // with firmware version, jumper info, and bootloader version + stream << "firmware " << static_cast(m_infoBuf[0]) << "." // version minor + << std::hex << static_cast(m_infoBuf[1]); // features mask + if (m_infoLen >= 5) { stream << " [" << std::setfill('0') << std::setw(2) << std::hex << static_cast(m_infoBuf[2]) << std::setw(2) << static_cast(m_infoBuf[3]) << "]"; stream << ", jumpers 0x" << std::setw(2) << static_cast(m_infoBuf[4]); stream << std::setfill(' '); // reset } - if (m_infoLen>=8) { + if (m_infoLen >= 8) { stream << ", bootloader " << std::dec << static_cast(m_infoBuf[5]); stream << " [" << std::setfill('0') << std::setw(2) << std::hex << static_cast(m_infoBuf[6]) << std::setw(2) << static_cast(m_infoBuf[7]) << "]"; @@ -677,7 +678,7 @@ bool Device::read(symbol_t* value, bool isAvailable, ArbitrationState* arbitrati for (uint8_t pos = 0; pos < m_infoPos; pos++) { stream << " " << std::setw(2) << static_cast(m_infoBuf[pos]); } - if (m_infoId == 2 && (m_infoBuf[2]&0x3f)!=0x3f) { + if (m_infoId == 2 && (m_infoBuf[2]&0x3f) != 0x3f) { // non-default arbitration delay val = (m_infoBuf[2]&0x3f)*10; // steps of 10us stream << ", arbitration delay " << std::dec << static_cast(val) << " us"; diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index 1cf944aa..33ca04e3 100755 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -352,7 +352,8 @@ class SerialDevice : public Device { */ SerialDevice(const char* name, bool checkDevice, unsigned int extraLatency, bool readOnly, bool initialSend, bool enhancedProto = false, bool enhancedHighSpeed = false) - : Device(name, checkDevice, extraLatency, readOnly, initialSend, enhancedProto), m_enhancedHighSpeed(enhancedHighSpeed) { + : Device(name, checkDevice, extraLatency, readOnly, initialSend, enhancedProto), + m_enhancedHighSpeed(enhancedHighSpeed) { } // @copydoc diff --git a/src/lib/ebus/stringhelper.cpp b/src/lib/ebus/stringhelper.cpp index 62b244e4..2ff6e6ee 100644 --- a/src/lib/ebus/stringhelper.cpp +++ b/src/lib/ebus/stringhelper.cpp @@ -22,6 +22,7 @@ #include "lib/ebus/stringhelper.h" #include +#include namespace ebusd { @@ -296,7 +297,8 @@ bool StringReplacer::checkMatchability() const { return true; } -ssize_t StringReplacer::match(const string& str, string* circuit, string* name, string* field, const string& separator) const { +ssize_t StringReplacer::match(const string& str, string* circuit, string* name, string* field, + const string& separator) const { size_t last = 0; size_t count = m_parts.size(); size_t idx; @@ -442,7 +444,8 @@ StringReplacer StringReplacers::get(const string& key) const { return StringReplacer(); } -string StringReplacers::get(const string& key, bool untilFirstEmpty, bool onlyAlphanum, const string& fallbackKey) const { +string StringReplacers::get(const string& key, bool untilFirstEmpty, bool onlyAlphanum, + const string& fallbackKey) const { auto itc = m_constants.find(key); if (itc != m_constants.end()) { return itc->second; diff --git a/src/lib/ebus/stringhelper.h b/src/lib/ebus/stringhelper.h index 57938967..b927a3a1 100644 --- a/src/lib/ebus/stringhelper.h +++ b/src/lib/ebus/stringhelper.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "lib/ebus/message.h" @@ -34,6 +35,7 @@ namespace ebusd { */ using std::map; +using std::pair; using std::ostringstream; using std::string; using std::vector; @@ -172,7 +174,7 @@ class StringReplacer { * @param isField true when it is a field. * @return the created pair. */ - static std::pair makeField(const string& name, bool isField); + static pair makeField(const string& name, bool isField); /** * Add a part to the list of parts. @@ -180,7 +182,6 @@ class StringReplacer { * @param inField 1 after '%', 2 after '%{', 0 otherwise. */ void addPart(ostringstream& stack, int inField); - }; diff --git a/src/lib/knx/knx.cpp b/src/lib/knx/knx.cpp index 8988caed..c71178fd 100644 --- a/src/lib/knx/knx.cpp +++ b/src/lib/knx/knx.cpp @@ -20,6 +20,8 @@ # include #endif +#include + #include "lib/knx/knx.h" #ifdef HAVE_KNXD @@ -27,8 +29,6 @@ #endif #include "lib/knx/knxnet.h" -#include - namespace ebusd { unsigned int parseInt(const char* str, int base, unsigned int minValue, unsigned int maxValue, diff --git a/src/lib/knx/knx.h b/src/lib/knx/knx.h index 8b44a48b..6dacb0f7 100644 --- a/src/lib/knx/knx.h +++ b/src/lib/knx/knx.h @@ -150,12 +150,12 @@ class KnxConnection { /** * @return true if connection allows programming via ETS. */ - virtual bool isProgrammable() const { return false; }; + virtual bool isProgrammable() const { return false; } /** * @return the individual address, or 0 if not programmed yet, or any non-zero value if not programmable. */ - virtual knx_addr_t getAddress() { return DEFAULT_ADDRESS; }; + virtual knx_addr_t getAddress() const { return DEFAULT_ADDRESS; } /** * @param address the individual address to set. @@ -168,7 +168,7 @@ class KnxConnection { * Get the programming mode. * @return true when in programming mode, false if not. */ - virtual bool isProgrammingMode() { + virtual bool isProgrammingMode() const { return false; } @@ -183,4 +183,5 @@ class KnxConnection { } // namespace ebusd -#endif // LIB_KNX_KNX_H_ \ No newline at end of file +#endif // LIB_KNX_KNX_H_ + diff --git a/src/lib/knx/knxd.h b/src/lib/knx/knxd.h index 547d3212..363bb370 100644 --- a/src/lib/knx/knxd.h +++ b/src/lib/knx/knxd.h @@ -34,7 +34,7 @@ class KnxdConnection : public KnxConnection { /** * Construct a new instance. */ - KnxdConnection(const char *url) + explicit KnxdConnection(const char *url) : KnxConnection(), m_url(url), m_con(nullptr) {} /** @@ -121,4 +121,4 @@ class KnxdConnection : public KnxConnection { } // namespace ebusd -#endif // LIB_KNX_KNXD_H_ \ No newline at end of file +#endif // LIB_KNX_KNXD_H_ diff --git a/src/lib/knx/knxnet.h b/src/lib/knx/knxnet.h index 7bcab6e2..13295ea4 100644 --- a/src/lib/knx/knxnet.h +++ b/src/lib/knx/knxnet.h @@ -19,7 +19,6 @@ #ifndef LIB_KNX_KNXNET_H_ #define LIB_KNX_KNXNET_H_ -#include #include #include #include @@ -33,6 +32,7 @@ #include #include #include +#include #include #include "lib/knx/knx.h" @@ -62,10 +62,10 @@ typedef union __attribute__ ((packed)) { // KNXnet/IP header typedef struct __attribute__ ((packed)) { - uint8_t headerLength; // =6 - uint8_t protocolVersion; // =0x10 + uint8_t headerLength; // =6 + uint8_t protocolVersion; // =0x10 uint16be_t serviceTypeIdentifier; - uint16be_t totalLength; // complete length including header + uint16be_t totalLength; // complete length including header } knxnet_header_t; /** service types. */ @@ -92,7 +92,8 @@ typedef enum { // cEMI frame header (external message interface) typedef struct __attribute__ ((packed)) { uint8_t messageCode; - uint8_t additionalInfoLength; // optional immediately following additional bytes, usually =0. fixed to 0 in cEMI management messages + // optional immediately following additional bytes, usually =0. fixed to 0 in cEMI management messages + uint8_t additionalInfoLength; } knxnet_cemi_header_t; /* cEMI message codes. */ @@ -128,21 +129,21 @@ typedef struct __attribute__ ((packed)) { uint8_t raw; struct { #if __BYTE_ORDER == __BIG_ENDIAN - bool frameType: 1; // 0=extended, 1=standard + bool frameType: 1; // 0=extended, 1=standard bool reserved: 1; - bool repeat: 1; // 0=repeat, 1=do not repeat - bool systemBroadcast: 1; // 0=system broadcast, 1=broadcast - uint8_t priority: 2; // 0=system, 1=normal, 2=urgent, 3=low - bool acknowledgeRequest: 1; // 1=ack requested - bool confirm: 1; // 0=no error, 1=error + bool repeat: 1; // 0=repeat, 1=do not repeat + bool systemBroadcast: 1; // 0=system broadcast, 1=broadcast + uint8_t priority: 2; // 0=system, 1=normal, 2=urgent, 3=low + bool acknowledgeRequest: 1; // 1=ack requested + bool confirm: 1; // 0=no error, 1=error #else - bool confirm: 1; // 0=no error, 1=error - bool acknowledgeRequest: 1; // 1=ack requested - uint8_t priority: 2; // 0=system, 1=normal, 2=urgent, 3=low - bool systemBroadcast: 1; // 0=system broadcast, 1=broadcast - bool repeat: 1; // 0=repeat, 1=do not repeat + bool confirm: 1; // 0=no error, 1=error + bool acknowledgeRequest: 1; // 1=ack requested + uint8_t priority: 2; // 0=system, 1=normal, 2=urgent, 3=low + bool systemBroadcast: 1; // 0=system broadcast, 1=broadcast + bool repeat: 1; // 0=repeat, 1=do not repeat bool reserved: 1; - bool frameType: 1; // 0=extended, 1=standard + bool frameType: 1; // 0=extended, 1=standard #endif }; } controlField1; @@ -150,19 +151,19 @@ typedef struct __attribute__ ((packed)) { uint8_t raw; struct { #if __BYTE_ORDER == __BIG_ENDIAN - bool addressType: 1; // 0=individual, 1=group + bool addressType: 1; // 0=individual, 1=group uint8_t hopCount: 3; - uint8_t extendedFrameFormat: 4; // 0=standard frame, 0xf=escape + uint8_t extendedFrameFormat: 4; // 0=standard frame, 0xf=escape #else - uint8_t extendedFrameFormat: 4; // 0=standard frame, 0xf=escape + uint8_t extendedFrameFormat: 4; // 0=standard frame, 0xf=escape uint8_t hopCount: 3; - bool addressType: 1; // 0=individual, 1=group + bool addressType: 1; // 0=individual, 1=group #endif }; } controlField2; uint16be_t sourceAddress; uint16be_t destinationAddress; - uint8_t informationLength; // number of NPDU octets (not including the TPCI/APCI octet) + uint8_t informationLength; // number of NPDU octets (not including the TPCI/APCI octet) } knxnet_l_data_header_t; @@ -170,22 +171,22 @@ typedef union __attribute__ ((packed)) { uint8_t raw; struct { #if __BYTE_ORDER == __BIG_ENDIAN - bool controlFlag: 1; // 0=data, 1=control - bool numbered: 1; // 1=has sequence, 0=no sequence - uint8_t sequence: 4; // optional sequence number - uint8_t apci: 2; // highest 2 bits of APCI + bool controlFlag: 1; // 0=data, 1=control + bool numbered: 1; // 1=has sequence, 0=no sequence + uint8_t sequence: 4; // optional sequence number + uint8_t apci: 2; // highest 2 bits of APCI #else - uint8_t apci: 2; // highest 2 bits of APCI - uint8_t sequence: 4; // optional sequence number - bool numbered: 1; // 1=has sequence, 0=no sequence - bool controlFlag: 1; // 0=data, 1=control + uint8_t apci: 2; // highest 2 bits of APCI + uint8_t sequence: 4; // optional sequence number + bool numbered: 1; // 1=has sequence, 0=no sequence + bool controlFlag: 1; // 0=data, 1=control #endif }; } knxnet_tpci_apci_t; typedef struct __attribute__ ((packed)) { uint8_t length; - uint8_t protocolCode; // 0x01=UDP over IPv4 + uint8_t protocolCode; // 0x01=UDP over IPv4 uint32be_t ipAddressV4; uint16be_t port; } knxnet_hpai_t; @@ -194,9 +195,9 @@ typedef struct __attribute__ ((packed)) { typedef struct __attribute__ ((packed)) { uint8_t length; - uint8_t descriptionCode; // 0x01=device info - uint8_t medium; // 0x20=IP - uint8_t status; // bit 0=programming mode + uint8_t descriptionCode; // 0x01=device info + uint8_t medium; // 0x20=IP + uint8_t status; // bit 0=programming mode uint16be_t individualAddress; uint16be_t projInstId; uint8_t serial[6]; @@ -207,11 +208,11 @@ typedef struct __attribute__ ((packed)) { typedef struct __attribute__ ((packed)) { uint8_t length; - uint8_t descriptionCode; // 0x02=services + uint8_t descriptionCode; // 0x02=services struct { uint8_t familyId; uint8_t familyVersion; - }; // just one for now + }; // just one for now } knxnet_dib_services_t; // the default system port @@ -227,7 +228,7 @@ class LastFrame { public: void set(uint8_t* data, size_t len, size_t lOffset, time_t now) { - if (len>=sizeof(m_data)) { + if (len >= sizeof(m_data)) { return; } memcpy(m_data, data, len); @@ -237,7 +238,7 @@ class LastFrame { } bool isValid(time_t now) { - return m_len && m_time>=now-LAST_FRAME_TIMEOUT; + return m_len && m_time >= now-LAST_FRAME_TIMEOUT; } bool isSameAs(uint8_t* data, size_t len, size_t lOffset, time_t now, bool isSend = false) { @@ -250,10 +251,10 @@ class LastFrame { } int oldHopCount = (m_data[lOffset+1]&0x70)>>4; int newHopCount = (data[lOffset+1]&0x70)>>4; - if (newHopCount < 6 // top hop count is always tolerated TODO bad idea? - && memcmp(data, m_data, lOffset+1) == 0 // including first byte of l_data header - && (data[lOffset+1]&~0x70)==(m_data[lOffset+1]&~0x70) // ignore hop count - && (isSend ? newHopCount<=oldHopCount : newHopCountcontrolField2.addressType; - PRINTF("%s msgcode=%2.2x, %d.%d.%d > %d%c%d%c%d, repeat=%s, ack=%s, hopcnt=%d, prio=%s, frame=%s, %sbroad, confirm=%s, tpci/apci=%2.2x", + PRINTF("%s msgcode=%2.2x, %d.%d.%d > %d%c%d%c%d, repeat=%s, ack=%s, hopcnt=%d, prio=%s, frame=%s, %sbroad, " + "confirm=%s, tpci/apci=%2.2x", sent ? "send" : "recv", c->messageCode, l->sourceAddress.high>>4, @@ -342,15 +344,15 @@ void logTelegram(bool sent, knxnet_cemi_header_t* c, knxnet_l_data_header_t* l, l->controlField1.repeat ? "yes" : "no", l->controlField1.acknowledgeRequest ? "yes" : "no", l->controlField2.hopCount, - l->controlField1.priority==1 ? "normal" : l->controlField1.priority==2 ? "urgent" : l->controlField1.priority==3 ? "low" : "system", + l->controlField1.priority == 1 ? "normal" : l->controlField1.priority == 2 ? "urgent" : + l->controlField1.priority == 3 ? "low" : "system", l->controlField1.frameType ? "std" : "ext", l->controlField1.systemBroadcast ? "" : "sys ", l->controlField1.confirm ? "error" : "no err", - d[0] - ); + d[0]); if (d) { PRINTF(", data="); - for (int i=0; iinformationLength; i++) { + for (int i=0; i < l->informationLength; i++) { PRINTF("%2.2x ", d[1+i]); } } @@ -371,7 +373,7 @@ class KnxNetConnection : public KnxConnection { /** * Construct a new instance. */ - KnxNetConnection(const char* url) + explicit KnxNetConnection(const char* url) : KnxConnection(), m_url(url), m_sock(0), m_programmingMode(false), m_addr(0) {} /** @@ -395,7 +397,7 @@ class KnxNetConnection : public KnxConnection { m_interface.s_addr = INADDR_ANY; m_port = SYSTEM_MULTICAST_PORT; if (m_url && m_url[0]) { // non-empty - string urlStr = m_url; // "[mcast][@intf]" for non-default 224.0.23.12:3671) + string urlStr = m_url; // "[mcast][@intf]" for non-default 224.0.23.12:3671) if (!urlStr.empty()) { auto pos = urlStr.find('@'); if (pos != string::npos) { @@ -504,11 +506,12 @@ class KnxNetConnection : public KnxConnection { // @copydoc knx_transfer_t getPollData(int size, uint8_t* data, int* recvlen, knx_addr_t* src, knx_addr_t* dst) override { uint8_t buf[128]; - ssize_t len = recv(m_sock, buf, sizeof(buf), 0); - if (len < sizeof(knxnet_header_t)) { + ssize_t slen = recv(m_sock, buf, sizeof(buf), 0); + if (slen < 0 || static_cast(slen) < sizeof(knxnet_header_t)) { PRINTF("#skip recv short hdr len=%d\n", len); return KNX_TRANSFER_NONE; } + size_t len = static_cast(slen); auto h = (knxnet_header_t*)buf; if (h->headerLength != sizeof(knxnet_header_t) || h->protocolVersion != 0x10) { PRINTF("#skip recv short/proto len=%d\n", len); @@ -583,16 +586,16 @@ class KnxNetConnection : public KnxConnection { // all fine int ret = d[0]; if (l->controlField2.addressType) { - ret |= 0x100; // address type group + ret |= 0x100; // address type group } if (!(ret&0x80)) { - ret &= ~0x03; // remove two apci bits + ret &= ~0x03; // remove two apci bits } if (ret&0x40) { - ret &= ~0x3c; // remove sequence number + ret &= ~0x3c; // remove sequence number } *recvlen = size > dataLen ? dataLen : size; - memcpy(data, d, *recvlen); // including the TPCI/APCI octet 6 + memcpy(data, d, *recvlen); // including the TPCI/APCI octet 6 if (src) { *src = htons(l->sourceAddress.raw); } @@ -638,27 +641,27 @@ class KnxNetConnection : public KnxConnection { c->additionalInfoLength = 0; auto lOffset = sizeof(knxnet_header_t)+sizeof(knxnet_cemi_header_t)+c->additionalInfoLength; auto l = (knxnet_l_data_header_t*)(((uint8_t*)h)+lOffset); - l->controlField1.raw = 0xbc; // standard frame, no repeat, broadcast, low prio, no ack, no err - l->controlField2.raw = 0xe0; // group address, hop count 6, standard frame - l->controlField2.addressType = (typ&0x100)!=0; + l->controlField1.raw = 0xbc; // standard frame, no repeat, broadcast, low prio, no ack, no err + l->controlField2.raw = 0xe0; // group address, hop count 6, standard frame + l->controlField2.addressType = (typ&0x100) != 0; l->sourceAddress.raw = htons(m_addr); l->destinationAddress.raw = htons(dst); if (typ&0x100) { // ensure at least default individual address if (!m_addr) { - l->sourceAddress.raw = 0xffff; // for "unregistered device" in S-Mode + l->sourceAddress.raw = 0xffff; // for "unregistered device" in S-Mode } } - l->informationLength = len-1; // subtracting the TPCI/APCI + l->informationLength = len-1; // subtracting the TPCI/APCI uint8_t* d = buf+sizeof(knxnet_header_t)+sizeof(knxnet_cemi_header_t)+sizeof(knxnet_l_data_header_t); // first byte of data is expected to hold the APCI upper byte, copy remainder: memcpy(d, data, len); - int tpci = typ&0xff; // TPCI/APCI - if ((typ&0x080)==0) { - tpci |= (d[0]&0x03); // highest 2 bits of APCI + int tpci = typ&0xff; // TPCI/APCI + if ((typ&0x080) == 0) { + tpci |= (d[0]&0x03); // highest 2 bits of APCI } if (typ&0x040) { - tpci |= d[0]&((0x0f)<<2); // SeqNo + tpci |= d[0]&((0x0f) << 2); // SeqNo } d[0] = tpci; logTelegram(true, c, l, d); @@ -673,7 +676,7 @@ class KnxNetConnection : public KnxConnection { } // copydoc - knx_addr_t getAddress() override { + knx_addr_t getAddress() const override { return m_addr; } @@ -686,7 +689,7 @@ class KnxNetConnection : public KnxConnection { } // copydoc - bool isProgrammingMode() override { + bool isProgrammingMode() const override { return m_programmingMode; } diff --git a/src/lib/utils/clock.cpp b/src/lib/utils/clock.cpp index 1befe810..827cd0a9 100755 --- a/src/lib/utils/clock.cpp +++ b/src/lib/utils/clock.cpp @@ -44,7 +44,7 @@ void clockGettime(struct timespec* t) { #endif } -long long clockGetMillis() { +uint64_t clockGetMillis() { struct timespec t; clockGettime(&t); return t.tv_sec*1000LL + t.tv_nsec / 1000000; diff --git a/src/lib/utils/clock.h b/src/lib/utils/clock.h index 7f046433..a4eeafac 100755 --- a/src/lib/utils/clock.h +++ b/src/lib/utils/clock.h @@ -19,6 +19,7 @@ #ifndef LIB_UTILS_CLOCK_H_ #define LIB_UTILS_CLOCK_H_ +#include #include namespace ebusd { @@ -34,7 +35,7 @@ void clockGettime(struct timespec* t); /** * Get the current system time in milliseconds since the Epoch. */ -long long clockGetMillis(); +uint64_t clockGetMillis(); } // namespace ebusd diff --git a/src/lib/utils/httpclient.cpp b/src/lib/utils/httpclient.cpp index 28699183..d3fbdb5e 100755 --- a/src/lib/utils/httpclient.cpp +++ b/src/lib/utils/httpclient.cpp @@ -170,7 +170,7 @@ SSLSocket* SSLSocket::connect(const string& host, const uint16_t& port, bool htt if (isError("ctx_new", ctx)) { break; } - bool verifyPeer = !caFile || strcmp(caFile, "#")!=0; + bool verifyPeer = !caFile || strcmp(caFile, "#") != 0; SSL_CTX_set_verify(ctx, verifyPeer ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, nullptr); if (verifyPeer) { #if OPENSSL_VERSION_NUMBER >= 0x10101000L diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index d34d40d6..4a5d5600 100755 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -98,10 +98,10 @@ bool WaitThread::Wait(int seconds, int millis) { struct timespec t; clockGettime(&t); t.tv_sec += seconds; - long newMillis = t.tv_nsec/1000000 + millis; + long int newMillis = t.tv_nsec/1000000 + millis; if (newMillis >= 1000) { t.tv_sec += newMillis / 1000; - t.tv_nsec = (newMillis%1000) * 1000000; // rounds down to whole millis + t.tv_nsec = (newMillis%1000) * 1000000; // rounds down to whole millis } else { t.tv_nsec += millis * 1000000; } diff --git a/src/tools/ebuspicloader.cpp b/src/tools/ebuspicloader.cpp index 77dcfdc3..7f95f919 100644 --- a/src/tools/ebuspicloader.cpp +++ b/src/tools/ebuspicloader.cpp @@ -45,8 +45,8 @@ const char *argp_program_version = "eBUS adapter PIC firmware loader"; /** the documentation of the program. */ static const char argpdoc[] = "A tool for loading firmware to the eBUS adapter PIC." - "\vPORT is either the serial port to use (e.g./dev/ttyUSB0) that also supports a trailing wildcard '*' for testing multiple ports," - "or a network port as \"ip:port\" for use with e.g. socat."; + "\vPORT is either the serial port to use (e.g./dev/ttyUSB0) that also supports a trailing wildcard '*' for testing" + " multiple ports, or a network port as \"ip:port\" for use with e.g. socat."; static const char argpargsdoc[] = "PORT"; @@ -149,7 +149,7 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { } setIp = true; break; - case 'm': // --mask=24 + case 'm': // --mask=24 if (arg == nullptr || arg[0] == 0) { argp_error(state, "invalid IP mask"); return EINVAL; @@ -164,10 +164,10 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { } setMask = true; break; - case 'M': // --macip + case 'M': // --macip setMacFromIp = true; break; - case 'a': // --arbdel=1000 + case 'a': // --arbdel=1000 if (arg == nullptr || arg[0] == 0) { argp_error(state, "invalid arbitration delay"); return EINVAL; @@ -178,17 +178,17 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) { } setArbitrationDelay = true; break; - case 'f': // --flash=firmware.hex + case 'f': // --flash=firmware.hex if (arg == nullptr || arg[0] == 0 || stat(arg, &st) != 0 || !S_ISREG(st.st_mode)) { argp_error(state, "invalid flash file"); return EINVAL; } flashFile = arg; break; - case 'r': // --reset + case 'r': // --reset reset = true; break; - case 's': // --slow + case 's': // --slow lowSpeed = true; break; default: @@ -265,12 +265,6 @@ static bool isSerial = true; static int timeoutFactor = 1; static int timeoutAddend = 0; -long long getTime() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec*1000+ts.tv_nsec/1000000; -} - ssize_t waitWrite(int fd, uint8_t *data, size_t len, int timeoutMillis) { int ret; struct pollfd pfd; @@ -770,7 +764,7 @@ void printFileChecksum() { } std::cout << "New firmware version: " << static_cast(newFirmwareVersion) - << " [" << std::hex << std::setw (4) << std::setfill('0') << static_cast(checkSum) << "]" << std::endl; + << " [" << std::hex << std::setw(4) << std::setfill('0') << static_cast(checkSum) << "]" << std::endl; } bool flashPic(int fd) { @@ -938,10 +932,10 @@ void readSettings(int fd) { } uint16_t arbitrationDelay = configData[3]&0x3f; std::cout << "Arbitration delay: "; - if (arbitrationDelay==0x3f) { + if (arbitrationDelay == 0x3f) { std::cout << "200 us (default)" << std::endl; } else { - arbitrationDelay *= 10; // steps of 10us + arbitrationDelay *= 10; // steps of 10us std::cout << std::dec << static_cast(arbitrationDelay) << " us" << std::endl; } } @@ -996,7 +990,7 @@ int main(int argc, char* argv[]) { } std::string port = argv[arg_index]; std::string::size_type pos = port.find('*'); - if (pos==std::string::npos || pos != port.length()-1) { + if (pos == std::string::npos || pos != port.length()-1) { int fd; pos = port.find(':'); if (pos != std::string::npos) { @@ -1019,14 +1013,14 @@ int main(int argc, char* argv[]) { } std::string::size_type sep = port.find_last_of('/'); - std::string base = sep==std::string::npos ? "" : port.substr(0, sep); + std::string base = sep == std::string::npos ? "" : port.substr(0, sep); DIR* dir = opendir(base.c_str()); if (!dir) { std::cerr << "Unable to open directory " << base << std::endl; exit(EXIT_FAILURE); } - std::string prefix = sep==std::string::npos ? port.substr(0, pos) : port.substr(sep + 1, pos - 1 - sep); + std::string prefix = sep == std::string::npos ? port.substr(0, pos) : port.substr(sep + 1, pos - 1 - sep); struct dirent* ent; while ((ent = readdir(dir))) { if (std::string(ent->d_name).substr(0, prefix.length()) != prefix) { @@ -1081,7 +1075,7 @@ int run(int fd) { int picSum = calcChecksum(fd, 0x0000, END_BOOT_BYTES); std::cout << "Bootloader version: " << static_cast(bootloaderVersion) - << " [" << std::hex << std::setw (4) << std::setfill('0') << static_cast(picSum) << "]" << std::endl; + << " [" << std::hex << std::setw(4) << std::setfill('0') << static_cast(picSum) << "]" << std::endl; } else { std::cerr << "Bootloader version not found" << std::endl; } @@ -1092,7 +1086,7 @@ int run(int fd) { int picSum = calcChecksum(fd, END_BOOT, END_FLASH_BYTES-END_BOOT_BYTES); std::cout << "Firmware version: " << static_cast(firmwareVersion) - << " [" << std::hex << std::setw (4) << std::setfill('0') << static_cast(picSum) << "]" << std::endl; + << " [" << std::hex << std::setw(4) << std::setfill('0') << static_cast(picSum) << "]" << std::endl; } else { std::cout << "Firmware version not found" << std::endl; }