From 0217096b6b5e62cc1611b4ca67f2ed229772f46c Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 14 Jan 2017 17:34:02 +0100 Subject: [PATCH] code style --- src/ebusd/bushandler.cpp | 6 +-- src/ebusd/bushandler.h | 5 ++- src/ebusd/datahandler.cpp | 1 + src/ebusd/main.cpp | 2 +- src/ebusd/mainloop.cpp | 4 +- src/ebusd/mqtthandler.cpp | 8 ++-- src/ebusd/network.h | 2 +- src/lib/ebus/data.cpp | 29 +++++++----- src/lib/ebus/datatype.cpp | 48 ++++++++++---------- src/lib/ebus/datatype.h | 25 ++++++----- src/lib/ebus/device.cpp | 8 ++-- src/lib/ebus/device.h | 2 +- src/lib/ebus/message.cpp | 74 +++++++++++++++---------------- src/lib/ebus/message.h | 17 +++---- src/lib/ebus/symbol.cpp | 2 +- src/lib/ebus/test/test_device.cpp | 1 + src/lib/ebus/test/test_symbol.cpp | 1 + src/lib/utils/log.cpp | 4 +- src/lib/utils/rotatefile.cpp | 2 +- src/lib/utils/rotatefile.h | 7 +-- src/lib/utils/tcpsocket.cpp | 2 +- src/lib/utils/thread.cpp | 2 +- 22 files changed, 132 insertions(+), 120 deletions(-) diff --git a/src/ebusd/bushandler.cpp b/src/ebusd/bushandler.cpp index d8881e24..23ab2058 100644 --- a/src/ebusd/bushandler.cpp +++ b/src/ebusd/bushandler.cpp @@ -320,7 +320,7 @@ void BusHandler::run() { } result_t BusHandler::handleSymbol() { - long timeout = SYN_TIMEOUT; + unsigned int timeout = SYN_TIMEOUT; unsigned char sendSymbol = ESC; bool sending = false; BusRequest* startRequest = NULL; @@ -879,7 +879,7 @@ void BusHandler::receiveCompleted() { } Message* message = m_messages->find(m_command); if (m_grabMessages) { - unsigned long long key; + uint64_t key; if (message) { key = message->getKey(); } else { @@ -1110,7 +1110,7 @@ void BusHandler::formatGrabResult(const bool unknown, ostringstream& output) { output << "grab disabled"; } else { bool first = true; - for (map::iterator it = m_grabbedMessages.begin(); it != m_grabbedMessages.end(); it++) { + for (map::iterator it = m_grabbedMessages.begin(); it != m_grabbedMessages.end(); it++) { if (it->second.dump(unknown, m_messages, first, output)) { first = false; } diff --git a/src/ebusd/bushandler.h b/src/ebusd/bushandler.h index 6765b951..be93e07e 100644 --- a/src/ebusd/bushandler.h +++ b/src/ebusd/bushandler.h @@ -20,6 +20,7 @@ #define EBUSD_BUSHANDLER_H_ #include +#include #include #include #include @@ -588,7 +589,7 @@ class BusHandler : public WaitThread { unsigned int m_remainLockCount; /** the interval in microseconds after which to generate an AUTO-SYN symbol, or 0 if disabled. */ - long m_generateSynInterval; + unsigned int m_generateSynInterval; /** the interval in seconds in which poll messages are cycled, or 0 if disabled. */ const unsigned int m_pollInterval; @@ -649,7 +650,7 @@ class BusHandler : public WaitThread { bool m_grabMessages; /** the grabbed messages by key.*/ - map m_grabbedMessages; + map m_grabbedMessages; }; } // namespace ebusd diff --git a/src/ebusd/datahandler.cpp b/src/ebusd/datahandler.cpp index 7d5fa679..f3020182 100644 --- a/src/ebusd/datahandler.cpp +++ b/src/ebusd/datahandler.cpp @@ -22,6 +22,7 @@ #include "datahandler.h" #include +#include #ifdef HAVE_MQTT # include "mqtthandler.h" #endif diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index d203c8d1..9c7e61b1 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -861,7 +861,7 @@ result_t loadScanConfigFile(MessageMap* messages, unsigned char address, SymbolS if (::isspace(*it)) { ident.erase(it--); } else { - *it = (char)::tolower(*it); + *it = static_cast(::tolower(*it)); } } // complete name: cfgpath/MANUFACTURER/ZZ[.C[C[C[C[C]]]]][.circuit][.suffix][.*][.SWxxxx][.HWxxxx][.*].csv diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index 8a023d7a..abfc1150 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -615,14 +615,14 @@ string MainLoop::executeRead(vector &args) { " Dx data byte(s) to send"; } string fieldName; - signed char fieldIndex = -2; + char fieldIndex = -2; if (args.size() == argPos + 2) { fieldName = args[argPos + 1]; fieldIndex = -1; size_t pos = fieldName.find_last_of('.'); if (pos != string::npos) { result_t result = RESULT_OK; - fieldIndex = (char)parseInt(fieldName.substr(pos+1).c_str(), 10, 0, MAX_POS, result); + fieldIndex = static_cast(parseInt(fieldName.substr(pos+1).c_str(), 10, 0, MAX_POS, result)); if (result == RESULT_OK) { fieldName = fieldName.substr(0, pos); } diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index 62dc70e1..1b6e4725 100644 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -215,7 +215,7 @@ MqttHandler::MqttHandler(BusHandler* busHandler, MessageMap* messages) #if (LIBMOSQUITTO_MAJOR < 1) true, #endif - willTopic.c_str(), (uint32_t)len, (uint8_t*)(willData.c_str()), 0, true); + willTopic.c_str(), (uint32_t)len, reinterpret_cast(willData.c_str()), 0, true); if (mosquitto_connect(m_mosquitto, g_host, g_port, 60 #if (LIBMOSQUITTO_MAJOR < 1) , true @@ -250,12 +250,12 @@ void on_message( struct mosquitto *mosq, #endif void *obj, const struct mosquitto_message *message) { - MqttHandler* handler = (MqttHandler*)obj; + MqttHandler* handler = reinterpret_cast(obj); if (!handler || !message || !handler->isRunning()) { return; } string topic(message->topic); - string data(message->payloadlen > 0 ? (char*)message->payload : ""); + string data(message->payloadlen > 0 ? reinterpret_cast(message->payload) : ""); handler->notifyTopic(topic, data); } @@ -455,7 +455,7 @@ void MqttHandler::publishMessage(Message* message, ostringstream& updates) { void MqttHandler::publishTopic(string topic, string data, bool retain) { logOtherDebug("mqtt", "publish %s %s", topic.c_str(), data.c_str()); - mosquitto_publish(m_mosquitto, NULL, topic.c_str(), (uint32_t)data.size(), (uint8_t*)(data.c_str()), 0, retain); + mosquitto_publish(m_mosquitto, NULL, topic.c_str(), (uint32_t)data.size(), reinterpret_cast(data.c_str()), 0, retain); } } // namespace ebusd diff --git a/src/ebusd/network.h b/src/ebusd/network.h index b79654fb..aa0e0029 100644 --- a/src/ebusd/network.h +++ b/src/ebusd/network.h @@ -94,7 +94,7 @@ class NetMessage { if (sscanf("%1x%1x", m_request.c_str()+pos+1, &value1, &value2) < 2) { break; } - m_request[pos] = (char)(((value1&0x0f)<<4) | (value2&0x0f)); + m_request[pos] = static_cast(((value1&0x0f)<<4) | (value2&0x0f)); m_request.erase(pos+1, 2); } } else if (pos+1 == m_request.length()) { diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 44380ebf..acd96ca5 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -128,7 +128,7 @@ result_t DataField::create(vector::iterator& it, FileReader::trim(token); const char* str = token.c_str(); char* strEnd = NULL; - unsigned long int id; + unsigned long id; if (strncasecmp(str, "0x", 2) == 0) { str += 2; id = strtoul(str, &strEnd, 16); // hexadecimal @@ -296,7 +296,7 @@ result_t SingleDataField::create(const string id, const unsigned char length, return RESULT_OK; } if (dataType->isNumeric()) { - NumberDataType* numType = (NumberDataType*)dataType; + NumberDataType* numType = reinterpret_cast(dataType); if (values.empty() && numType->hasFlag(DAY)) { for (unsigned int i = 0; i < sizeof(dayNames) / sizeof(dayNames[0]); i++) values[numType->getMinValue() + i] = dayNames[i]; @@ -318,7 +318,7 @@ result_t SingleDataField::create(const string id, const unsigned char length, if (divisor != 0 || !values.empty()) { return RESULT_ERR_INVALID_ARG; // cannot set divisor or values for string field } - returnField = new SingleDataField(name, comment, unit, (StringDataType*)dataType, partType, byteCount); + returnField = new SingleDataField(name, comment, unit, dataType, partType, byteCount); return RESULT_OK; } @@ -488,7 +488,7 @@ result_t SingleDataField::derive(string name, string comment, } DataType* dataType = m_dataType; if (numeric) { - NumberDataType* numType = (NumberDataType*)m_dataType; + NumberDataType* numType = reinterpret_cast(dataType); result_t result = numType->derive(divisor, 0, numType); if (result != RESULT_OK) { return result; @@ -497,8 +497,10 @@ result_t SingleDataField::derive(string name, string comment, } if (values.empty()) { fields.push_back(new SingleDataField(name, comment, unit, dataType, partType, m_length)); + } else if (numeric) { + fields.push_back(new ValueListDataField(name, comment, unit, reinterpret_cast(dataType), partType, m_length, values)); } else { - fields.push_back(new ValueListDataField(name, comment, unit, (NumberDataType*)dataType, partType, m_length, values)); + return RESULT_ERR_INVALID_ARG; } return RESULT_OK; } @@ -520,7 +522,7 @@ bool SingleDataField::hasFullByteOffset(bool after) { if (m_length > 1 || !m_dataType->isNumeric()) { return true; } - NumberDataType* num = (NumberDataType*)m_dataType; + NumberDataType* num = reinterpret_cast(m_dataType); return (num->getBitCount() % 8) == 0 || (after && num->getFirstBit() + (num->getBitCount() % 8) >= 8); } @@ -549,15 +551,18 @@ result_t ValueListDataField::derive(string name, string comment, if (divisor != 0 && divisor != 1) { return RESULT_ERR_INVALID_ARG; // cannot use divisor != 1 for value list field } + if (!m_dataType->isNumeric()) { + return RESULT_ERR_INVALID_ARG; + } if (!values.empty()) { - NumberDataType* num = (NumberDataType*)m_dataType; + NumberDataType* num = reinterpret_cast(m_dataType); if (values.begin()->first < num->getMinValue() || values.rbegin()->first > num->getMaxValue()) { return RESULT_ERR_INVALID_ARG; // cannot use divisor != 1 for value list field } } else { values = m_values; } - fields.push_back(new ValueListDataField(name, comment, unit, (NumberDataType*)m_dataType, partType, m_length, values)); + fields.push_back(new ValueListDataField(name, comment, unit, reinterpret_cast(m_dataType), partType, m_length, values)); return RESULT_OK; } @@ -617,7 +622,7 @@ result_t ValueListDataField::readSymbols(SymbolString& input, const bool isMaste result_t ValueListDataField::writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output, const bool isMaster, unsigned char* usedLength) { - NumberDataType* numType = (NumberDataType*)m_dataType; + NumberDataType* numType = reinterpret_cast(m_dataType); if (isIgnored()) { return numType->writeRawValue(numType->getReplacement(), offset, m_length, output, usedLength); // replacement value } @@ -721,9 +726,9 @@ DataFieldSet* DataFieldSet::s_identFields = NULL; DataFieldSet* DataFieldSet::getIdentFields() { if (s_identFields == NULL) { - NumberDataType* uchDataType = (NumberDataType*)DataTypeList::getInstance()->get("UCH"); - StringDataType* stringDataType = (StringDataType*)DataTypeList::getInstance()->get("STR"); - NumberDataType* pinDataType = (NumberDataType*)DataTypeList::getInstance()->get("PIN"); + NumberDataType* uchDataType = reinterpret_cast(DataTypeList::getInstance()->get("UCH")); + StringDataType* stringDataType = reinterpret_cast(DataTypeList::getInstance()->get("STR")); + NumberDataType* pinDataType = reinterpret_cast(DataTypeList::getInstance()->get("PIN")); map manufacturers; manufacturers[0x06] = "Dungs"; manufacturers[0x0f] = "FH Ostfalia"; diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index 7f73856a..2828232e 100644 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -43,7 +43,7 @@ using std::setw; unsigned int parseInt(const char* str, int base, const unsigned int minValue, const unsigned int maxValue, result_t& result, unsigned int* length) { char* strEnd = NULL; - unsigned long int ret = strtoul(str, &strEnd, base); + unsigned long ret = strtoul(str, &strEnd, base); if (strEnd == NULL || strEnd == str || *strEnd != 0) { result = RESULT_ERR_INVALID_NUM; // invalid value @@ -64,7 +64,7 @@ unsigned int parseInt(const char* str, int base, const unsigned int minValue, co int parseSignedInt(const char* str, int base, const int minValue, const int maxValue, result_t& result, unsigned int* length) { char* strEnd = NULL; - long int ret = strtol(str, &strEnd, base); + long ret = strtol(str, &strEnd, base); if (strEnd == NULL || *strEnd != 0) { result = RESULT_ERR_INVALID_NUM; // invalid value @@ -79,7 +79,7 @@ int parseSignedInt(const char* str, int base, const int minValue, const int maxV *length = (unsigned int)(strEnd - str); } result = RESULT_OK; - return (int)ret; + return static_cast(ret); } void printErrorPos(ostream& out, vector::iterator begin, const vector::iterator end, vector::iterator pos, string filename, size_t lineNo, result_t result) { @@ -337,9 +337,9 @@ result_t DateTimeDataType::readSymbols(SymbolString& input, const bool isMaster, break; } int mjd = last + ch*256 + 15020; // 01.01.1900 - int y = (int)((mjd-15078.2)/365.25); - int m = (int)((mjd-14956.1-(int)(y*365.25))/30.6001); - int d = mjd-14956-(int)(y*365.25)-(int)(m*30.6001); + int y = static_cast((mjd-15078.2)/365.25); + int m = static_cast((mjd-14956.1-static_cast(y*365.25))/30.6001); + int d = mjd-14956-static_cast(y*365.25)-static_cast(m*30.6001); m--; if (m >= 13) { y++; @@ -470,7 +470,7 @@ result_t DateTimeDataType::writeSymbols(istringstream& input, } else if (i + 1 == count) { int y = (value < 100 ? value + 2000 : value) - 1900; int l = last <= 2 ? 1 : 0; - int mjd = 14956 + lastLast + (int)((y-l)*365.25) + (int)((last+1+l*12)*30.6001); + int mjd = 14956 + lastLast + static_cast((y-l)*365.25) + static_cast((last+1+l*12)*30.6001); value = mjd - 15020; // 01.01.1900 output[baseOffset + offset] = (unsigned char)(value&0xff); value >>= 8; @@ -484,7 +484,7 @@ result_t DateTimeDataType::writeSymbols(istringstream& input, // calculate local week day int y = (value < 100 ? value + 2000 : value) - 1900; int l = last <= 2 ? 1 : 0; - int mjd = 14956 + lastLast + (int)((y-l)*365.25) + (int)((last+1+l*12)*30.6001); + int mjd = 14956 + lastLast + static_cast((y-l)*365.25) + static_cast((last+1+l*12)*30.6001); int daysSinceSunday = (mjd+3) % 7; // Sun=0 if (hasFlag(BCD)) { output[baseOffset + offset - incr] = (unsigned char)((6+daysSinceSunday) % 7); // Sun=0x06 @@ -737,7 +737,7 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster, value = __builtin_bswap32(value); # endif unsigned char* pval = (unsigned char*)&value; - val = *((float*)pval); + val = *reinterpret_cast(pval); #else int exp = (value >> 23) & 0xff; // 8 bits, signed if (exp == 0) { @@ -745,7 +745,7 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster, } else { exp -= 127; unsigned int sig = value & ((1 << 23) - 1); - val = (1.0f + (float)(sig / exp2(23))) * (float)exp2(exp); + val = (1.0f + static_cast(sig / exp2(23))) * static_cast(exp2(exp)); if (negative) { val = -val; } @@ -753,9 +753,9 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster, #endif if (val != 0.0) { if (m_divisor < 0) { - val *= (float)-m_divisor; + val *= static_cast(-m_divisor); } else if (m_divisor > 1) { - val /= (float)m_divisor; + val /= static_cast(m_divisor); } } if (m_precision != 0) { @@ -768,24 +768,24 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster, } if (!negative) { if (m_divisor < 0) { - output << static_cast((float)value * (float)(-m_divisor)); + output << (static_cast(value) * static_cast(-m_divisor)); } else if (m_divisor <= 1) { output << static_cast(value); } else { output << setprecision(m_precision) - << fixed << static_cast((float)value / (float)m_divisor); + << fixed << (static_cast(value) / static_cast(m_divisor)); } return RESULT_OK; } - signedValue = (int)value; // negative signed value + signedValue = static_cast(value); // negative signed value } else if (negative) { // negative signed value - signedValue = (int)value - (1 << m_bitCount); + signedValue = static_cast(value) - (1 << m_bitCount); } else { - signedValue = (int)value; + signedValue = static_cast(value); } if (m_divisor < 0) { output << fixed << setprecision(0) - << static_cast((float)signedValue * (float)(-m_divisor)); + << (static_cast(signedValue) * static_cast(-m_divisor)); } else if (m_divisor <= 1) { if (hasFlag(FIX) && hasFlag(BCD)) { if (outputFormat & OF_JSON) { @@ -800,7 +800,7 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster, output << static_cast(signedValue) << setw(0); } else { output << setprecision(m_precision) - << fixed << static_cast((float)signedValue / (float)m_divisor); + << fixed << (static_cast(signedValue) / static_cast(m_divisor)); } return RESULT_OK; } @@ -873,9 +873,9 @@ result_t NumberDataType::writeSymbols(istringstream& input, dvalue *= m_divisor; } #ifdef HAVE_DIRECT_FLOAT_FORMAT - float val = (float)dvalue; + float val = static_cast(dvalue); unsigned char* pval = (unsigned char*)&val; - value = *((int32_t*)pval); + value = *reinterpret_cast(pval); # if HAVE_DIRECT_FLOAT_FORMAT == 2 value = __builtin_bswap32(value); # endif @@ -903,7 +903,7 @@ result_t NumberDataType::writeSymbols(istringstream& input, char* strEnd = NULL; if (m_divisor == 1) { if (hasFlag(SIG)) { - long int signedValue = strtol(str, &strEnd, 10); + long signedValue = strtol(str, &strEnd, 10); if (signedValue < 0 && m_bitCount != 32) { value = (unsigned int)(signedValue + (1 << m_bitCount)); } else { @@ -931,9 +931,9 @@ result_t NumberDataType::writeSymbols(istringstream& input, return RESULT_ERR_OUT_OF_RANGE; // value out of range } if (dvalue < 0 && m_bitCount != 32) { - value = (int)(dvalue + (1 << m_bitCount)); + value = static_cast(dvalue + (1 << m_bitCount)); } else { - value = (int)dvalue; + value = static_cast(dvalue); } } else { if (dvalue < 0.0 || dvalue >= (1LL << (8 * length))) { diff --git a/src/lib/ebus/datatype.h b/src/lib/ebus/datatype.h index ed083b0f..3d4608e5 100644 --- a/src/lib/ebus/datatype.h +++ b/src/lib/ebus/datatype.h @@ -19,6 +19,7 @@ #ifndef LIB_EBUS_DATATYPE_H_ #define LIB_EBUS_DATATYPE_H_ +#include #include #include #include @@ -158,7 +159,7 @@ class DataType { * @param flags the combination of flags (like #BCD). * @param replacement the replacement value (fill-up value for @a StringDataType, no replacement if equal to @a NumberDataType#minValue). */ - DataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement) + DataType(const string id, const unsigned char bitCount, const uint16_t flags, const unsigned int replacement) : m_id(id), m_bitCount(bitCount), m_flags(flags), m_replacement(replacement) {} /** @@ -261,7 +262,7 @@ class DataType { const unsigned char m_bitCount; /** the combination of flags (like #BCD). */ - const unsigned short m_flags; + const uint16_t m_flags; /** the replacement value (fill-up value for @a StringDataType, no replacement if equal to @a NumberDataType#minValue). */ const unsigned int m_replacement; @@ -281,7 +282,7 @@ class StringDataType : public DataType { * @param replacement the replacement value (fill-up value). * @param isHex true for hex digits instead of characters. */ - StringDataType(const string id, const unsigned char bitCount, const unsigned short flags, + StringDataType(const string id, const unsigned char bitCount, const uint16_t flags, const unsigned int replacement, bool isHex = false) : DataType(id, bitCount, flags, replacement), m_isHex(isHex) {} @@ -327,8 +328,8 @@ class DateTimeDataType : public DataType { * @param hasTime true if time part is present. * @param resolution the the resolution in minutes for time types, or 1. */ - DateTimeDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, - const bool hasDate, const bool hasTime, const short resolution) + DateTimeDataType(const string id, const unsigned char bitCount, const uint16_t flags, const unsigned int replacement, + const bool hasDate, const bool hasTime, const int16_t resolution) : DataType(id, bitCount, flags, replacement), m_hasDate(hasDate), m_hasTime(hasTime), m_resolution(resolution) {} /** @@ -349,7 +350,7 @@ class DateTimeDataType : public DataType { /** * @return the resolution in minutes for time types, or 1. */ - short getResolution() const { return m_resolution; } + int16_t getResolution() const { return m_resolution; } // @copydoc virtual result_t readRawValue(SymbolString& input, @@ -375,7 +376,7 @@ class DateTimeDataType : public DataType { const bool m_hasTime; /** the resolution in minutes for time types, or 1. */ - const short m_resolution; + const int16_t m_resolution; }; @@ -394,7 +395,7 @@ class NumberDataType : public DataType { * @param maxValue the maximum raw value. * @param divisor the divisor (negative for reciprocal). */ - NumberDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, + NumberDataType(const string id, const unsigned char bitCount, const uint16_t flags, const unsigned int replacement, const unsigned int minValue, const unsigned int maxValue, const int divisor) : DataType(id, bitCount, flags|NUM, replacement), m_minValue(minValue), m_maxValue(maxValue), m_divisor(divisor), m_precision(calcPrecision(divisor)), m_firstBit(0), m_baseType(NULL) {} @@ -407,8 +408,8 @@ class NumberDataType : public DataType { * @param firstBit the offset to the first bit. * @param divisor the divisor (negative for reciprocal). */ - NumberDataType(const string id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, - const short firstBit, const int divisor) + NumberDataType(const string id, const unsigned char bitCount, const uint16_t flags, const unsigned int replacement, + const int16_t firstBit, const int divisor) : DataType(id, bitCount, flags|NUM, replacement), m_minValue(0), m_maxValue((1<(&address), 0, sizeof(address)); *portpos = 0; if (inet_aton(addrpos, &address.sin_addr) == 0) { struct hostent* h = gethostbyname(addrpos); @@ -111,7 +111,7 @@ result_t Device::send(const unsigned char value) { return RESULT_OK; } -result_t Device::recv(const long timeout, unsigned char& value) { +result_t Device::recv(const unsigned int timeout, unsigned char& value) { if (!isValid()) { return RESULT_ERR_DEVICE; } @@ -254,9 +254,9 @@ result_t NetworkDevice::open() { ret = bind(m_fd, (struct sockaddr*)&address, sizeof(address)); } else { int value = 1; - ret = setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, (void*)&value, sizeof(value)); + ret = setsockopt(m_fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&value), sizeof(value)); value = 1; - setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&value, sizeof(value)); + setsockopt(m_fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast(&value), sizeof(value)); } if (ret == 0) { ret = connect(m_fd, (struct sockaddr*)&m_address, sizeof(m_address)); diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index 9381f3c1..cb17d7d8 100644 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -119,7 +119,7 @@ class Device { * @param value the reference in which the received byte value is stored. * @return the result_t code. */ - result_t recv(const long timeout, unsigned char& value); + result_t recv(const unsigned int timeout, unsigned char& value); /** * Return the device name. diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index eb4ad0ec..e799bf2b 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -92,13 +92,13 @@ Message::Message(const string circuit, const string name, m_lastUpdateTime(0), m_lastChangeTime(0), m_pollCount(0), m_lastPollTime(0) { m_id.push_back(pb); m_id.push_back(sb); - unsigned long long key = 0; + uint64_t key = 0; if (!isPassive) { key |= (isWrite ? 0x1fLL : 0x1eLL) << (8 * 7); // special values for active } - key |= (unsigned long long)SYN << (8 * 6); - key |= (unsigned long long)pb << (8 * 5); - key |= (unsigned long long)sb << (8 * 4); + key |= (uint64_t)SYN << (8 * 6); + key |= (uint64_t)pb << (8 * 5); + key |= (uint64_t)sb << (8 * 4); m_key = key; setScanMessage(); } @@ -134,19 +134,19 @@ string getDefault(const string value, vector* defaults, size_t pos, bool return defaultStr.substr(0, insertPos)+value+defaultStr.substr(insertPos+1); } -unsigned long long Message::createKey(const vector id, +uint64_t Message::createKey(const vector id, const bool isWrite, const bool isPassive, const unsigned char srcAddress, const unsigned char dstAddress) { - unsigned long long key = (unsigned long long)(id.size()-2) << (8 * 7 + 5); + uint64_t key = (uint64_t)(id.size()-2) << (8 * 7 + 5); if (isPassive) { - key |= (unsigned long long)getMasterNumber(srcAddress) << (8 * 7); // 0..25 + key |= (uint64_t)getMasterNumber(srcAddress) << (8 * 7); // 0..25 } else { key |= (isWrite ? 0x1fLL : 0x1eLL) << (8 * 7); // special values for active } - key |= (unsigned long long)dstAddress << (8 * 6); + key |= (uint64_t)dstAddress << (8 * 6); int exp = 5; for (vector::const_iterator it = id.begin(); it < id.end(); it++) { - key ^= (unsigned long long)*it << (8 * exp--); + key ^= (uint64_t)*it << (8 * exp--); if (exp == 0) { exp = 3; } @@ -154,7 +154,7 @@ unsigned long long Message::createKey(const vector id, return key; } -unsigned long long Message::createKey(SymbolString& master, unsigned char maxIdLength, bool anyDestination) { +uint64_t Message::createKey(SymbolString& master, unsigned char maxIdLength, bool anyDestination) { if (master.size() < 5) { return INVALID_KEY; } @@ -165,14 +165,14 @@ unsigned long long Message::createKey(SymbolString& master, unsigned char maxIdL if (master.size() < 5+idLength) { return INVALID_KEY; } - unsigned long long key = (unsigned long long)idLength << (8 * 7 + 5); - key |= (unsigned long long)getMasterNumber(master[0]) << (8 * 7); // QQ address for passive message - key |= (unsigned long long)(anyDestination ? SYN : master[1]) << (8 * 6); // ZZ address - key |= (unsigned long long)master[2] << (8 * 5); // PB - key |= (unsigned long long)master[3] << (8 * 4); // SB + uint64_t key = (uint64_t)idLength << (8 * 7 + 5); + key |= (uint64_t)getMasterNumber(master[0]) << (8 * 7); // QQ address for passive message + key |= (uint64_t)(anyDestination ? SYN : master[1]) << (8 * 6); // ZZ address + key |= (uint64_t)master[2] << (8 * 5); // PB + key |= (uint64_t)master[3] << (8 * 4); // SB int exp = 3; for (unsigned char i = 0; i < idLength; i++) { - key ^= (unsigned long long)master[5 + i] << (8 * exp--); + key ^= (uint64_t)master[5 + i] << (8 * exp--); if (exp == 0) { exp = 3; } @@ -190,11 +190,11 @@ result_t Message::parseId(string input, vector& id) { break; } input.clear(); - input.push_back((char)in.get()); + input.push_back(static_cast(in.get())); if (in.eof()) { return RESULT_ERR_INVALID_ARG; // too short hex } - input.push_back((char)in.get()); + input.push_back(static_cast(in.get())); result_t result; unsigned char value = (unsigned char)parseInt(input.c_str(), 16, 0, 0xff, result); @@ -522,8 +522,8 @@ bool Message::checkId(Message& other) { return other.checkIdPrefix(m_id); } -unsigned long long Message::getDerivedKey(const unsigned char dstAddress) { - return (m_key & ~(0xffLL << (8*6))) | (unsigned long long)dstAddress << (8*6); +uint64_t Message::getDerivedKey(const unsigned char dstAddress) { + return (m_key & ~(0xffLL << (8*6))) | (uint64_t)dstAddress << (8*6); } bool Message::setPollPriority(unsigned char priority) { @@ -842,10 +842,10 @@ ChainedMessage::ChainedMessage(const string circuit, const string name, m_ids(ids), m_lengths(lengths), m_maxTimeDiff(m_ids.size()*15) { // 15 seconds per message size_t cnt = ids.size(); - m_lastMasterDatas = (SymbolString**)calloc(cnt, sizeof(SymbolString*)); - m_lastSlaveDatas = (SymbolString**)calloc(cnt, sizeof(SymbolString*)); - m_lastMasterUpdateTimes = (time_t*)calloc(cnt, sizeof(time_t)); - m_lastSlaveUpdateTimes = (time_t*)calloc(cnt, sizeof(time_t)); + m_lastMasterDatas = reinterpret_cast(calloc(cnt, sizeof(SymbolString*))); + m_lastSlaveDatas = reinterpret_cast(calloc(cnt, sizeof(SymbolString*))); + m_lastMasterUpdateTimes = reinterpret_cast(calloc(cnt, sizeof(time_t))); + m_lastSlaveUpdateTimes = reinterpret_cast(calloc(cnt, sizeof(time_t))); for (size_t index = 0; index < cnt; index++) { m_lastMasterDatas[index] = new SymbolString(); m_lastSlaveDatas[index] = new SymbolString(); @@ -1331,7 +1331,7 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess return RESULT_ERR_INVALID_ADDR; } // clone the message with dedicated dstAddress if necessary - unsigned long long key = message->getDerivedKey(m_dstAddress); + uint64_t key = message->getDerivedKey(m_dstAddress); vector* derived = messages->getByKey(key); if (derived == NULL) { message = message->derive(m_dstAddress, true); @@ -1524,10 +1524,10 @@ result_t LoadInstruction::execute(MessageMap* messages, ostringstream& log, Cond result_t MessageMap::add(Message* message, bool storeByName) { - unsigned long long key = message->getKey(); + uint64_t key = message->getKey(); bool conditional = message->isConditional(); if (!m_addAll) { - map >::iterator keyIt = m_messagesByKey.find(key); + map >::iterator keyIt = m_messagesByKey.find(key); if (keyIt != m_messagesByKey.end()) { Message* other = getFirstAvailable(keyIt->second, message); if (other != NULL) { @@ -1768,7 +1768,7 @@ Message* MessageMap::getScanMessage(const unsigned char dstAddress) { if (!isValidAddress(dstAddress, false) || isMaster(dstAddress)) { return NULL; } - unsigned long long key = m_scanMessage->getDerivedKey(dstAddress); + uint64_t key = m_scanMessage->getDerivedKey(dstAddress); vector* msgs = getByKey(key); if (msgs != NULL) { return msgs->front(); @@ -1888,8 +1888,8 @@ string MessageMap::getLoadedFiles(unsigned char address) { return m_loadedFiles[address]; } -vector* MessageMap::getByKey(const unsigned long long key) { - map >::iterator it = m_messagesByKey.find(key); +vector* MessageMap::getByKey(const uint64_t key) { + map >::iterator it = m_messagesByKey.find(key); if (it != m_messagesByKey.end()) { return &it->second; } @@ -2002,26 +2002,26 @@ Message* MessageMap::find(SymbolString& master, bool anyDestination, if (master.size() >= 5 && master[4] == 0 && anyDestination && master[2] == 0x07 && master[3] == 0x04) { return m_scanMessage; } - unsigned long long baseKey = Message::createKey(master, m_maxIdLength, anyDestination); + uint64_t baseKey = Message::createKey(master, m_maxIdLength, anyDestination); if (baseKey == INVALID_KEY) { return NULL; } unsigned char maxIdLength = Message::getKeyLength(baseKey); for (unsigned char idLength = maxIdLength; true; idLength--) { - unsigned long long key = baseKey; + uint64_t key = baseKey; if (idLength == maxIdLength) { baseKey &= ~ID_LENGTH_AND_IDS_MASK; } else { - key |= (unsigned long long)idLength << (8 * 7 + 5); + key |= (uint64_t)idLength << (8 * 7 + 5); int exp = 3; for (unsigned char i = 0; i < idLength; i++) { - key ^= (unsigned long long)master[5 + i] << (8 * exp--); + key ^= (uint64_t)master[5 + i] << (8 * exp--); if (exp == 0) { exp = 3; } } } - map >::iterator it; + map >::iterator it; if (withPassive) { it = m_messagesByKey.find(key); if (it != m_messagesByKey.end()) { @@ -2105,7 +2105,7 @@ void MessageMap::clear() { if (it->first[0] != '-') { // avoid double free: instances stored multiple times have a key starting with "-" for (vector::iterator nit = nameMessages.begin(); nit != nameMessages.end(); nit++) { Message* message = *nit; - map >::iterator keyIt = m_messagesByKey.find(message->getKey()); + map >::iterator keyIt = m_messagesByKey.find(message->getKey()); if (keyIt != m_messagesByKey.end()) { vector* keyMessages = &keyIt->second; if (!keyMessages->empty()) { @@ -2122,7 +2122,7 @@ void MessageMap::clear() { nameMessages.clear(); } // free remaining message instances by key - for (map >::iterator it = m_messagesByKey.begin(); it != m_messagesByKey.end(); it++) { + for (map >::iterator it = m_messagesByKey.begin(); it != m_messagesByKey.end(); it++) { vector keyMessages = it->second; for (vector::iterator kit = keyMessages.begin(); kit != keyMessages.end(); kit++) { Message* message = *kit; diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 4f373cc0..4baa25f4 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -19,6 +19,7 @@ #ifndef LIB_EBUS_MESSAGE_H_ #define LIB_EBUS_MESSAGE_H_ +#include #include #include #include @@ -160,7 +161,7 @@ class Message { * @param dstAddress the destination address, or @a SYN for any (set later). * @return the key for the ID. */ - static unsigned long long createKey(const vector id, + static uint64_t createKey(const vector id, const bool isWrite, const bool isPassive, const unsigned char srcAddress, const unsigned char dstAddress); @@ -171,7 +172,7 @@ class Message { * @param anyDestination @p true to use the special @a SYN as destination address in the key. * @return the key for the ID, or -1LL if the data is invalid. */ - static unsigned long long createKey(SymbolString& master, + static uint64_t createKey(SymbolString& master, unsigned char maxIdLength, bool anyDestination = false); /** @@ -179,7 +180,7 @@ class Message { * @param key the key. * @return the length field from the key. */ - static unsigned char getKeyLength(unsigned long long key) { return (unsigned char)(key >> (8 * 7 + 5)); } + static unsigned char getKeyLength(uint64_t key) { return (unsigned char)(key >> (8 * 7 + 5)); } /** * Parse an ID part from the input @a string. @@ -332,14 +333,14 @@ class Message { * Return the key for storing in @a MessageMap. * @return the key for storing in @a MessageMap. */ - unsigned long long getKey() { return m_key; } + uint64_t getKey() { return m_key; } /** * Return the derived key for storing in @a MessageMap. * @param dstAddress the destination address for the derivation. * @return the derived key for storing in @a MessageMap. */ - unsigned long long getDerivedKey(const unsigned char dstAddress); + uint64_t getDerivedKey(const unsigned char dstAddress); /** * Get the polling priority, or 0 for no polling at all. @@ -573,7 +574,7 @@ class Message { *
  • bytes 3-0: ID bytes (with cyclic xor if more than 4)
  • * */ - unsigned long long m_key; + uint64_t m_key; /** the @a DataField for encoding/decoding the message. */ DataField* m_data; @@ -1228,7 +1229,7 @@ class MessageMap : public FileReader { * @return the found @a Message instances, or NULL. * Note: the caller may not free the returned instances. */ - vector* getByKey(const unsigned long long key); + vector* getByKey(const uint64_t key); /** * Find the @a Message instance for the specified circuit and name. @@ -1369,7 +1370,7 @@ class MessageMap : public FileReader { map > m_messagesByName; /** the known @a Message instances by key. */ - map > m_messagesByKey; + map > m_messagesByKey; /** the known @a Message instances to poll, by priority. */ MessagePriorityQueue m_pollMessages; diff --git a/src/lib/ebus/symbol.cpp b/src/lib/ebus/symbol.cpp index f402a9ba..cf76d060 100644 --- a/src/lib/ebus/symbol.cpp +++ b/src/lib/ebus/symbol.cpp @@ -75,7 +75,7 @@ result_t SymbolString::parseHex(const string& str, const bool isEscaped) { for (size_t i = 0; i < str.size(); i += 2) { char* strEnd = NULL; const char* strBegin = str.substr(i, 2).c_str(); - unsigned long int value = strtoul(strBegin, &strEnd, 16); + unsigned long value = strtoul(strBegin, &strEnd, 16); if (strEnd == NULL || strEnd != strBegin+2 || value > 0xff) { return RESULT_ERR_INVALID_NUM; // invalid value diff --git a/src/lib/ebus/test/test_device.cpp b/src/lib/ebus/test/test_device.cpp index 7c79127f..b32e0005 100644 --- a/src/lib/ebus/test/test_device.cpp +++ b/src/lib/ebus/test/test_device.cpp @@ -20,6 +20,7 @@ #include #include "device.h" +using namespace std; using namespace ebusd; int main() { diff --git a/src/lib/ebus/test/test_symbol.cpp b/src/lib/ebus/test/test_symbol.cpp index bb191cd0..d168c62f 100644 --- a/src/lib/ebus/test/test_symbol.cpp +++ b/src/lib/ebus/test/test_symbol.cpp @@ -21,6 +21,7 @@ #include #include "symbol.h" +using namespace std; using namespace ebusd; static bool error = false; diff --git a/src/lib/utils/log.cpp b/src/lib/utils/log.cpp index aecc4f34..49cc25ab 100644 --- a/src/lib/utils/log.cpp +++ b/src/lib/utils/log.cpp @@ -57,7 +57,7 @@ static FILE* s_logFile = stdout; bool setLogFacilities(const char* facilities) { char *input = strdup(facilities); - char *opt = (char*)input, *value = NULL; + char *opt = reinterpret_cast(input), *value = NULL; int newFacilites = 0; while (*opt) { int val = getsubopt(&opt, (char *const *)facilityNames, &value); @@ -97,7 +97,7 @@ bool getLogFacilities(char* buffer) { bool setLogLevel(const char* level) { char *input = strdup(level); - char *opt = (char*)input, *value = NULL; + char *opt = reinterpret_cast(input), *value = NULL; int newLevel = 0; if (*opt) { int val = getsubopt(&opt, (char *const *)levelNames, &value); diff --git a/src/lib/utils/rotatefile.cpp b/src/lib/utils/rotatefile.cpp index 49de8e3f..3eafe6e8 100644 --- a/src/lib/utils/rotatefile.cpp +++ b/src/lib/utils/rotatefile.cpp @@ -77,7 +77,7 @@ void RotateFile::write(unsigned char* value, unsigned int size, bool received) { if ((m_fileSize%1024) == 0) { fflush(m_stream); } - if (m_fileSize >= m_maxSize * 1024) { + if (m_fileSize >= m_maxSize * 1024LL) { string oldfile = string(m_fileName)+".old"; if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) { fclose(m_stream); diff --git a/src/lib/utils/rotatefile.h b/src/lib/utils/rotatefile.h index 5abfc38d..75d75eb2 100644 --- a/src/lib/utils/rotatefile.h +++ b/src/lib/utils/rotatefile.h @@ -20,6 +20,7 @@ #define LIB_UTILS_ROTATEFILE_H_ #include +#include #include #include #include @@ -41,7 +42,7 @@ class RotateFile { * @param maxSize the maximum size of the file to write to. * @param textMode whether to write each byte with prefixed timestamp and direction as text. */ - RotateFile(const string fileName, const unsigned long maxSize, const bool textMode = false) + RotateFile(const string fileName, const unsigned int maxSize, const bool textMode = false) : m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {} /** @@ -79,7 +80,7 @@ class RotateFile { const string m_fileName; /** the maximum size of @a m_file, or 0 for infinite. */ - const unsigned long m_maxSize; + const unsigned int m_maxSize; /** whether to write each byte with prefixed timestamp and direction as text. */ const bool m_textMode; @@ -88,7 +89,7 @@ class RotateFile { FILE* m_stream; /** the number of bytes already written to the @a m_file. */ - unsigned long m_fileSize; + uint64_t m_fileSize; }; #endif // LIB_UTILS_ROTATEFILE_H_ diff --git a/src/lib/utils/tcpsocket.cpp b/src/lib/utils/tcpsocket.cpp index 69a9d045..47b1cf80 100644 --- a/src/lib/utils/tcpsocket.cpp +++ b/src/lib/utils/tcpsocket.cpp @@ -39,7 +39,7 @@ TCPSocket* TCPClient::connect(const string& server, const uint16_t& port) { struct sockaddr_in address; int ret; - memset((char*) &address, 0, sizeof(address)); + memset(reinterpret_cast(&address), 0, sizeof(address)); if (inet_addr(server.c_str()) == INADDR_NONE) { struct hostent* he; diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index 3132c0cc..197d3014 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -24,7 +24,7 @@ #include "clock.h" void* Thread::runThread(void* arg) { - ((Thread*)arg)->enter(); + reinterpret_cast(arg)->enter(); return NULL; }