Merge branch 'master' of github.com:john30/ebusd
This commit is contained in:
@@ -34,7 +34,12 @@
|
||||
#include "symbol.h"
|
||||
#include "log.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
using std::setfill;
|
||||
using std::setw;
|
||||
|
||||
// the string used for answering to a scan request (07h 04h)
|
||||
#define SCAN_ANSWER ("ebusd.eu;" PACKAGE_NAME ";" SCAN_VERSION ";100")
|
||||
@@ -315,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;
|
||||
@@ -874,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 {
|
||||
@@ -1105,7 +1110,7 @@ void BusHandler::formatGrabResult(const bool unknown, ostringstream& output) {
|
||||
output << "grab disabled";
|
||||
} else {
|
||||
bool first = true;
|
||||
for (map<unsigned long long, GrabbedMessage>::iterator it = m_grabbedMessages.begin(); it != m_grabbedMessages.end(); it++) {
|
||||
for (map<uint64_t, GrabbedMessage>::iterator it = m_grabbedMessages.begin(); it != m_grabbedMessages.end(); it++) {
|
||||
if (it->second.dump(unknown, m_messages, first, output)) {
|
||||
first = false;
|
||||
}
|
||||
@@ -1141,3 +1146,5 @@ void BusHandler::setScanConfigLoaded(unsigned char address, string file) {
|
||||
m_messages->addLoadedFile(address, file, "");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#define EBUSD_BUSHANDLER_H_
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
@@ -40,7 +41,9 @@
|
||||
* @image html states.png "ebusd BusHandler states"
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::string;
|
||||
|
||||
/** the default time [us] for retrieving a symbol from an addressed slave. */
|
||||
#define SLAVE_RECV_TIMEOUT 15000
|
||||
@@ -143,7 +146,7 @@ class PollRequest : public BusRequest {
|
||||
* Constructor.
|
||||
* @param message the associated @a Message.
|
||||
*/
|
||||
PollRequest(Message* message)
|
||||
explicit PollRequest(Message* message)
|
||||
: BusRequest(m_master, true), m_message(message), m_index(0) {}
|
||||
|
||||
/**
|
||||
@@ -586,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;
|
||||
@@ -647,7 +650,9 @@ class BusHandler : public WaitThread {
|
||||
bool m_grabMessages;
|
||||
|
||||
/** the grabbed messages by key.*/
|
||||
map<unsigned long long, GrabbedMessage> m_grabbedMessages;
|
||||
map<uint64_t, GrabbedMessage> m_grabbedMessages;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // EBUSD_BUSHANDLER_H_
|
||||
|
||||
@@ -22,11 +22,14 @@
|
||||
|
||||
#include "datahandler.h"
|
||||
#include <list>
|
||||
#include <string>
|
||||
#ifdef HAVE_MQTT
|
||||
# include "mqtthandler.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::string;
|
||||
|
||||
/** the final @a argp_child structure. */
|
||||
static const struct argp_child g_last_argp_child = {NULL, 0, NULL, 0};
|
||||
@@ -67,3 +70,5 @@ bool datahandler_register(BusHandler* busHandler, MessageMap* messages, list<Dat
|
||||
void DataSink::notifyUpdate(Message* message) {
|
||||
m_updatedMessages[message]++;
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -31,7 +31,10 @@
|
||||
* the bus.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::list;
|
||||
using std::map;
|
||||
|
||||
class DataHandler;
|
||||
|
||||
@@ -125,7 +128,7 @@ class DataSource : virtual public DataHandler {
|
||||
* Constructor.
|
||||
* @param busHandler the @a BusHandler instance.
|
||||
*/
|
||||
DataSource(BusHandler* busHandler)
|
||||
explicit DataSource(BusHandler* busHandler)
|
||||
: m_busHandler(busHandler) {}
|
||||
|
||||
/**
|
||||
@@ -142,4 +145,6 @@ class DataSource : virtual public DataHandler {
|
||||
BusHandler* m_busHandler;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // EBUSD_DATAHANDLER_H_
|
||||
|
||||
+13
-1
@@ -38,6 +38,14 @@
|
||||
#include "log.h"
|
||||
#include "rotatefile.h"
|
||||
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
using std::setfill;
|
||||
using std::setw;
|
||||
using std::nouppercase;
|
||||
|
||||
/** the path and name of the PID file. */
|
||||
#ifdef PACKAGE_PIDFILE
|
||||
#define PID_FILE_NAME PACKAGE_PIDFILE
|
||||
@@ -853,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<char>(::tolower(*it));
|
||||
}
|
||||
}
|
||||
// complete name: cfgpath/MANUFACTURER/ZZ[.C[C[C[C[C]]]]][.circuit][.suffix][.*][.SWxxxx][.HWxxxx][.*].csv
|
||||
@@ -935,6 +943,9 @@ result_t loadScanConfigFile(MessageMap* messages, unsigned char address, SymbolS
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
using namespace ebusd;
|
||||
|
||||
/**
|
||||
* Main method.
|
||||
@@ -1033,4 +1044,5 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
// shutdown
|
||||
shutdown();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
|
||||
/** \file main.h */
|
||||
|
||||
namespace ebusd {
|
||||
|
||||
/** A structure holding all program options. */
|
||||
struct options {
|
||||
const char* device; //!< eBUS device (serial device or [udp:]ip:port) [/dev/ttyUSB0]
|
||||
@@ -96,4 +98,6 @@ result_t loadConfigFiles(MessageMap* messages, bool verbose = false, bool denyRe
|
||||
*/
|
||||
result_t loadScanConfigFile(MessageMap* messages, unsigned char address, SymbolString& data, string& relativeFile, bool verbose = false);
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // EBUSD_MAIN_H_
|
||||
|
||||
+20
-11
@@ -27,7 +27,12 @@
|
||||
#include "log.h"
|
||||
#include "data.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
using std::setfill;
|
||||
using std::setw;
|
||||
|
||||
/** the number of seconds of permanent missing signal after which to reconnect the device. */
|
||||
#define RECONNECT_MISSING_SIGNAL 60
|
||||
@@ -610,14 +615,14 @@ string MainLoop::executeRead(vector<string> &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<char>(parseInt(fieldName.substr(pos+1).c_str(), 10, 0, MAX_POS, result));
|
||||
if (result == RESULT_OK) {
|
||||
fieldName = fieldName.substr(0, pos);
|
||||
}
|
||||
@@ -1042,17 +1047,19 @@ string MainLoop::executeFind(vector<string> &args) {
|
||||
if (verbosity == (OF_NAMES|OF_UNITS|OF_COMMENTS)) {
|
||||
unsigned char dstAddress = message->getDstAddress();
|
||||
if (dstAddress != SYN) {
|
||||
sprintf(str, "%02x", dstAddress);
|
||||
snprintf(str, sizeof(str), "%02x", dstAddress);
|
||||
} else if (lastup != 0 && message->getLastMasterData().size() > 1) {
|
||||
sprintf(str, "%02x", message->getLastMasterData()[1]);
|
||||
snprintf(str, sizeof(str), "%02x", message->getLastMasterData()[1]);
|
||||
} else {
|
||||
sprintf(str, "any");
|
||||
snprintf(str, sizeof(str), "any");
|
||||
}
|
||||
if (lastup != 0) {
|
||||
struct tm* td = localtime(&lastup);
|
||||
sprintf(str+strlen(str), ", lastup=%04d-%02d-%02d %02d:%02d:%02d",
|
||||
td->tm_year+1900, td->tm_mon+1, td->tm_mday,
|
||||
td->tm_hour, td->tm_min, td->tm_sec);
|
||||
struct tm td;
|
||||
localtime_r(&lastup, &td);
|
||||
size_t len = strlen(str);
|
||||
snprintf(str+len, sizeof(str)-len, ", lastup=%04d-%02d-%02d %02d:%02d:%02d",
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec);
|
||||
}
|
||||
result << " [ZZ=" << str;
|
||||
if (message->isPassive()) {
|
||||
@@ -1182,7 +1189,7 @@ string MainLoop::executeScan(vector<string> &args) {
|
||||
string MainLoop::executeLog(vector<string> &args) {
|
||||
if (args.size() == 1) {
|
||||
ostringstream ret;
|
||||
char str[32];
|
||||
char str[48];
|
||||
if (getLogFacilities(str)) {
|
||||
ret << str << ' ';
|
||||
}
|
||||
@@ -1534,3 +1541,5 @@ string MainLoop::executeGet(vector<string> &args, bool& connected) {
|
||||
connected = false;
|
||||
return result.str();
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
/** \file mainloop.h */
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
/**
|
||||
* The main loop handling requests from connected clients.
|
||||
@@ -252,4 +252,6 @@ class MainLoop : public Thread, DeviceListener {
|
||||
list<DataHandler*> m_dataHandlers;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // EBUSD_MAINLOOP_H_
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
#include "mqtthandler.h"
|
||||
#include "log.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
|
||||
/** the definition of the MQTT arguments. */
|
||||
static const struct argp_option g_mqtt_argp_options[] = {
|
||||
@@ -213,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<const uint8_t*>(willData.c_str()), 0, true);
|
||||
if (mosquitto_connect(m_mosquitto, g_host, g_port, 60
|
||||
#if (LIBMOSQUITTO_MAJOR < 1)
|
||||
, true
|
||||
@@ -248,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<MqttHandler*>(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<char*>(message->payload) : "");
|
||||
handler->notifyTopic(topic, data);
|
||||
}
|
||||
|
||||
@@ -453,5 +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<const uint8_t*>(data.c_str()), 0, retain);
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
#define EBUSD_MQTTHANDLER_H_
|
||||
|
||||
#include <mosquitto.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "datahandler.h"
|
||||
#include "bushandler.h"
|
||||
#include "message.h"
|
||||
@@ -31,7 +31,11 @@
|
||||
* A data handler enabling MQTT support via mosquitto.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::map;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
/**
|
||||
* Helper function for getting the argp definition for MQTT.
|
||||
@@ -128,4 +132,6 @@ class MqttHandler : public DataSink, public DataSource, public Thread {
|
||||
struct mosquitto* m_mosquitto;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // EBUSD_MQTTHANDLER_H_
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <cstring>
|
||||
#include "log.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
int Connection::m_ids = 0;
|
||||
|
||||
@@ -299,3 +299,4 @@ void Network::cleanConnections() {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
+5
-4
@@ -30,7 +30,7 @@
|
||||
|
||||
/** \file network.h */
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
/** Forward declaration for @a Connection. */
|
||||
class Connection;
|
||||
@@ -44,7 +44,7 @@ class NetMessage {
|
||||
* Constructor.
|
||||
* @param isHttp whether this is a HTTP message.
|
||||
*/
|
||||
NetMessage(const bool isHttp)
|
||||
explicit NetMessage(const bool isHttp)
|
||||
: m_isHttp(isHttp), m_resultSet(false), m_disconnect(false), m_listening(false), m_listenSince(0) {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
@@ -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<char>(((value1&0x0f) << 4) | (value2&0x0f));
|
||||
m_request.erase(pos+1, 2);
|
||||
}
|
||||
} else if (pos+1 == m_request.length()) {
|
||||
@@ -306,5 +306,6 @@ class Network : public Thread {
|
||||
void cleanConnections();
|
||||
};
|
||||
|
||||
#endif // EBUSD_NETWORK_H_
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // EBUSD_NETWORK_H_
|
||||
|
||||
@@ -19,9 +19,11 @@
|
||||
#include "contrib.h"
|
||||
#include "tem.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
bool libebus_contrib_register() {
|
||||
contrib_tem_register();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
* configure switch: --without-contrib
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
/**
|
||||
* Registration function that is called once during initialization.
|
||||
@@ -32,4 +32,6 @@ using namespace std;
|
||||
*/
|
||||
bool libebus_contrib_register();
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_CONTRIB_CONTRIB_H_
|
||||
|
||||
@@ -29,7 +29,11 @@
|
||||
#include <cstring>
|
||||
#include "datatype.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::setfill;
|
||||
using std::setw;
|
||||
using std::dec;
|
||||
|
||||
void contrib_tem_register() {
|
||||
DataTypeList::getInstance()->add(new TemParamDataType("TEM_P"));
|
||||
@@ -125,9 +129,9 @@ result_t TemParamDataType::writeSymbols(istringstream& input,
|
||||
return RESULT_ERR_OUT_OF_RANGE; // value out of range
|
||||
}
|
||||
if (isMaster) {
|
||||
value = grp | (num<<8); // grp in bits 0...5, num in bits 8...13
|
||||
value = grp | (num << 8); // grp in bits 0...5, num in bits 8...13
|
||||
} else {
|
||||
value = (grp<<7) | num; // grp in bits 7...11, num in bits 0...6
|
||||
value = (grp << 7) | num; // grp in bits 7...11, num in bits 0...6
|
||||
}
|
||||
}
|
||||
if (value < getMinValue() || value > getMaxValue()) {
|
||||
@@ -135,3 +139,5 @@ result_t TemParamDataType::writeSymbols(istringstream& input,
|
||||
}
|
||||
return writeRawValue(value, offset, length, output, usedLength);
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
* Contributed data types for TEM devices not part of regular releases.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
/**
|
||||
* A special variant of @a NumberDataType for TEM/Dungs ParamID in master/slave
|
||||
@@ -46,7 +46,7 @@ class TemParamDataType : public NumberDataType {
|
||||
* Constructs a new instance.
|
||||
* @param id the type identifier.
|
||||
*/
|
||||
TemParamDataType(const string id)
|
||||
explicit TemParamDataType(const string id)
|
||||
: NumberDataType(id, 16, 0, 0xffff, 0, 0xffff, 0) {}
|
||||
|
||||
// @copydoc
|
||||
@@ -68,4 +68,6 @@ class TemParamDataType : public NumberDataType {
|
||||
*/
|
||||
void contrib_tem_register();
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_CONTRIB_TEM_H_
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "tem.h"
|
||||
#include "data.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ebusd;
|
||||
|
||||
static bool error = false;
|
||||
|
||||
|
||||
+24
-13
@@ -29,7 +29,11 @@
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
using std::setw;
|
||||
|
||||
/** the week day names. */
|
||||
static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
|
||||
@@ -124,7 +128,7 @@ result_t DataField::create(vector<string>::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
|
||||
@@ -292,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<NumberDataType*>(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];
|
||||
@@ -314,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;
|
||||
}
|
||||
|
||||
@@ -484,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<NumberDataType*>(dataType);
|
||||
result_t result = numType->derive(divisor, 0, numType);
|
||||
if (result != RESULT_OK) {
|
||||
return result;
|
||||
@@ -493,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<NumberDataType*>(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;
|
||||
}
|
||||
@@ -516,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<NumberDataType*>(m_dataType);
|
||||
return (num->getBitCount() % 8) == 0
|
||||
|| (after && num->getFirstBit() + (num->getBitCount() % 8) >= 8);
|
||||
}
|
||||
@@ -545,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<NumberDataType*>(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<NumberDataType*>(m_dataType), partType, m_length, values));
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
@@ -613,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<NumberDataType*>(m_dataType);
|
||||
if (isIgnored()) {
|
||||
return numType->writeRawValue(numType->getReplacement(), offset, m_length, output, usedLength); // replacement value
|
||||
}
|
||||
@@ -717,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<NumberDataType*>(DataTypeList::getInstance()->get("UCH"));
|
||||
StringDataType* stringDataType = reinterpret_cast<StringDataType*>(DataTypeList::getInstance()->get("STR"));
|
||||
NumberDataType* pinDataType = reinterpret_cast<NumberDataType*>(DataTypeList::getInstance()->get("PIN"));
|
||||
map<unsigned int, string> manufacturers;
|
||||
manufacturers[0x06] = "Dungs";
|
||||
manufacturers[0x0f] = "FH Ostfalia";
|
||||
@@ -1057,3 +1066,5 @@ DataField* DataFieldTemplates::get(const string name) {
|
||||
}
|
||||
return ref->second;
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
+4
-1
@@ -45,7 +45,8 @@
|
||||
* instances from configuration files by inheriting the @a FileReader template
|
||||
* class.
|
||||
*/
|
||||
using namespace std;
|
||||
|
||||
namespace ebusd {
|
||||
|
||||
class DataFieldTemplates;
|
||||
class SingleDataField;
|
||||
@@ -656,4 +657,6 @@ class DataFieldTemplates : public FileReader {
|
||||
map<string, DataField*> m_fieldsByName;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_DATA_H_
|
||||
|
||||
+34
-25
@@ -31,12 +31,19 @@
|
||||
# include "contrib/contrib.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
using std::fixed;
|
||||
using std::setfill;
|
||||
using std::setprecision;
|
||||
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
|
||||
@@ -57,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
|
||||
@@ -72,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<int>(ret);
|
||||
}
|
||||
|
||||
void printErrorPos(ostream& out, vector<string>::iterator begin, const vector<string>::iterator end, vector<string>::iterator pos, string filename, size_t lineNo, result_t result) {
|
||||
@@ -330,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<int>((mjd-15078.2)/365.25);
|
||||
int m = static_cast<int>((mjd-14956.1-static_cast<int>(y*365.25))/30.6001);
|
||||
int d = mjd-14956-static_cast<int>(y*365.25)-static_cast<int>(m*30.6001);
|
||||
m--;
|
||||
if (m >= 13) {
|
||||
y++;
|
||||
@@ -463,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<int>((y-l)*365.25) + static_cast<int>((last+1+l*12)*30.6001);
|
||||
value = mjd - 15020; // 01.01.1900
|
||||
output[baseOffset + offset] = (unsigned char)(value&0xff);
|
||||
value >>= 8;
|
||||
@@ -477,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<int>((y-l)*365.25) + static_cast<int>((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
|
||||
@@ -730,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<float*>(pval);
|
||||
#else
|
||||
int exp = (value >> 23) & 0xff; // 8 bits, signed
|
||||
if (exp == 0) {
|
||||
@@ -738,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<float>(sig / exp2(23))) * static_cast<float>(exp2(exp));
|
||||
if (negative) {
|
||||
val = -val;
|
||||
}
|
||||
@@ -746,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<float>(-m_divisor);
|
||||
} else if (m_divisor > 1) {
|
||||
val /= (float)m_divisor;
|
||||
val /= static_cast<float>(m_divisor);
|
||||
}
|
||||
}
|
||||
if (m_precision != 0) {
|
||||
@@ -761,24 +768,24 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster,
|
||||
}
|
||||
if (!negative) {
|
||||
if (m_divisor < 0) {
|
||||
output << static_cast<float>((float)value * (float)(-m_divisor));
|
||||
output << (static_cast<float>(value) * static_cast<float>(-m_divisor));
|
||||
} else if (m_divisor <= 1) {
|
||||
output << static_cast<unsigned>(value);
|
||||
} else {
|
||||
output << setprecision(m_precision)
|
||||
<< fixed << static_cast<float>((float)value / (float)m_divisor);
|
||||
<< fixed << (static_cast<float>(value) / static_cast<float>(m_divisor));
|
||||
}
|
||||
return RESULT_OK;
|
||||
}
|
||||
signedValue = (int)value; // negative signed value
|
||||
signedValue = static_cast<int>(value); // negative signed value
|
||||
} else if (negative) { // negative signed value
|
||||
signedValue = (int)value - (1 << m_bitCount);
|
||||
signedValue = static_cast<int>(value) - (1 << m_bitCount);
|
||||
} else {
|
||||
signedValue = (int)value;
|
||||
signedValue = static_cast<int>(value);
|
||||
}
|
||||
if (m_divisor < 0) {
|
||||
output << fixed << setprecision(0)
|
||||
<< static_cast<float>((float)signedValue * (float)(-m_divisor));
|
||||
<< (static_cast<float>(signedValue) * static_cast<float>(-m_divisor));
|
||||
} else if (m_divisor <= 1) {
|
||||
if (hasFlag(FIX) && hasFlag(BCD)) {
|
||||
if (outputFormat & OF_JSON) {
|
||||
@@ -793,7 +800,7 @@ result_t NumberDataType::readSymbols(SymbolString& input, const bool isMaster,
|
||||
output << static_cast<signed>(signedValue) << setw(0);
|
||||
} else {
|
||||
output << setprecision(m_precision)
|
||||
<< fixed << static_cast<float>((float)signedValue / (float)m_divisor);
|
||||
<< fixed << (static_cast<float>(signedValue) / static_cast<float>(m_divisor));
|
||||
}
|
||||
return RESULT_OK;
|
||||
}
|
||||
@@ -866,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<float>(dvalue);
|
||||
unsigned char* pval = (unsigned char*)&val;
|
||||
value = *((int32_t*)pval);
|
||||
value = *reinterpret_cast<int32_t*>(pval);
|
||||
# if HAVE_DIRECT_FLOAT_FORMAT == 2
|
||||
value = __builtin_bswap32(value);
|
||||
# endif
|
||||
@@ -896,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 {
|
||||
@@ -924,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<int>(dvalue + (1 << m_bitCount));
|
||||
} else {
|
||||
value = (int)dvalue;
|
||||
value = static_cast<int>(dvalue);
|
||||
}
|
||||
} else {
|
||||
if (dvalue < 0.0 || dvalue >= (1LL << (8 * length))) {
|
||||
@@ -1073,3 +1080,5 @@ DataType* DataTypeList::get(const string id, const unsigned char length) {
|
||||
}
|
||||
return dataType;
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
+20
-14
@@ -19,6 +19,7 @@
|
||||
#ifndef LIB_EBUS_DATATYPE_H_
|
||||
#define LIB_EBUS_DATATYPE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
@@ -46,7 +47,10 @@
|
||||
* an @a istringstream to a @a SymbolString (see @a DataType#writeSymbols()).
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::map;
|
||||
using std::list;
|
||||
|
||||
/** the separator character used between base type name and length (in CSV only). */
|
||||
#define LENGTH_SEPARATOR ':'
|
||||
@@ -155,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) {}
|
||||
|
||||
/**
|
||||
@@ -258,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;
|
||||
@@ -278,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) {}
|
||||
|
||||
@@ -324,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) {}
|
||||
|
||||
/**
|
||||
@@ -346,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,
|
||||
@@ -372,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;
|
||||
};
|
||||
|
||||
|
||||
@@ -391,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) {}
|
||||
|
||||
@@ -404,9 +408,9 @@ 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)
|
||||
: DataType(id, bitCount, flags|NUM, replacement), m_minValue(0), m_maxValue((1<<bitCount)-1), m_divisor(divisor), m_precision(0), m_firstBit(firstBit), m_baseType(NULL) {}
|
||||
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 << bitCount)-1), m_divisor(divisor), m_precision(0), m_firstBit(firstBit), m_baseType(NULL) {}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
@@ -459,7 +463,7 @@ class NumberDataType : public DataType {
|
||||
/**
|
||||
* @return the offset to the first bit.
|
||||
*/
|
||||
short getFirstBit() const { return m_firstBit; }
|
||||
int16_t getFirstBit() const { return m_firstBit; }
|
||||
|
||||
// @copydoc
|
||||
virtual result_t readRawValue(SymbolString& input,
|
||||
@@ -505,7 +509,7 @@ class NumberDataType : public DataType {
|
||||
const unsigned char m_precision;
|
||||
|
||||
/** the offset to the first bit. */
|
||||
const short m_firstBit;
|
||||
const int16_t m_firstBit;
|
||||
|
||||
/** the base @a NumberDataType for derived instances. */
|
||||
NumberDataType* m_baseType;
|
||||
@@ -584,4 +588,6 @@ class DataTypeList {
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_DATATYPE_H_
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
#include <fstream>
|
||||
#include "data.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
Device::~Device() {
|
||||
close();
|
||||
@@ -63,7 +63,7 @@ Device* Device::create(const char* name, const bool checkDevice, const bool read
|
||||
return NULL; // invalid port
|
||||
}
|
||||
struct sockaddr_in address;
|
||||
memset((char*)&address, 0, sizeof(address));
|
||||
memset(reinterpret_cast<char*>(&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<void*>(&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<void*>(&value), sizeof(value));
|
||||
}
|
||||
if (ret == 0) {
|
||||
ret = connect(m_fd, (struct sockaddr*)&m_address, sizeof(m_address));
|
||||
@@ -323,3 +323,5 @@ ssize_t NetworkDevice::read(unsigned char& value) {
|
||||
}
|
||||
return Device::read(value);
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
* to a file and/or forwarding it to a logging function.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
/**
|
||||
* Interface for listening to data received on/sent to a device.
|
||||
@@ -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.
|
||||
@@ -284,4 +284,6 @@ class NetworkDevice : public Device {
|
||||
unsigned char m_bufPos;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_DEVICE_H_
|
||||
|
||||
@@ -39,7 +39,16 @@
|
||||
* with a "*" symbol.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::string;
|
||||
using std::ostream;
|
||||
using std::ostringstream;
|
||||
using std::istream;
|
||||
using std::istringstream;
|
||||
using std::ifstream;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/** the separator character used between fields. */
|
||||
#define FIELD_SEPARATOR ','
|
||||
@@ -65,7 +74,7 @@ class FileReader {
|
||||
/**
|
||||
* Construct a new instance.
|
||||
*/
|
||||
FileReader(bool supportsDefaults)
|
||||
explicit FileReader(bool supportsDefaults)
|
||||
: m_supportsDefaults(supportsDefaults) {}
|
||||
|
||||
/**
|
||||
@@ -376,4 +385,6 @@ class FileReader {
|
||||
string m_lastError;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_FILEREADER_H_
|
||||
|
||||
+48
-40
@@ -27,7 +27,13 @@
|
||||
#include "result.h"
|
||||
#include "symbol.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::dec;
|
||||
using std::hex;
|
||||
using std::nouppercase;
|
||||
using std::setfill;
|
||||
using std::setw;
|
||||
|
||||
/** the maximum length of the command ID bytes (in addition to PB/SB) for which the key is distinct. */
|
||||
#define MAX_ID_KEYLEN 4
|
||||
@@ -36,7 +42,7 @@ using namespace std;
|
||||
#define ID_SOURCE_MASK (0x1fLL << (8 * 7))
|
||||
|
||||
/** the bit mask for the ID length and combined ID bytes in the message key. */
|
||||
#define ID_LENGTH_AND_IDS_MASK ((7LL<<(8 * 7 + 5)) | 0xffffffffLL)
|
||||
#define ID_LENGTH_AND_IDS_MASK ((7LL << (8 * 7 + 5)) | 0xffffffffLL)
|
||||
|
||||
/** the bits in the @a ID_SOURCE_MASK for arbitrary source and active read message. */
|
||||
#define ID_SOURCE_ACTIVE_WRITE (0x1fLL << (8 * 7))
|
||||
@@ -86,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();
|
||||
}
|
||||
@@ -128,19 +134,19 @@ string getDefault(const string value, vector<string>* defaults, size_t pos, bool
|
||||
return defaultStr.substr(0, insertPos)+value+defaultStr.substr(insertPos+1);
|
||||
}
|
||||
|
||||
unsigned long long Message::createKey(const vector<unsigned char> id,
|
||||
uint64_t Message::createKey(const vector<unsigned char> 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<unsigned char>::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;
|
||||
}
|
||||
@@ -148,7 +154,7 @@ unsigned long long Message::createKey(const vector<unsigned char> 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;
|
||||
}
|
||||
@@ -159,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;
|
||||
}
|
||||
@@ -184,11 +190,11 @@ result_t Message::parseId(string input, vector<unsigned char>& id) {
|
||||
break;
|
||||
}
|
||||
input.clear();
|
||||
input.push_back((char)in.get());
|
||||
input.push_back(static_cast<char>(in.get()));
|
||||
if (in.eof()) {
|
||||
return RESULT_ERR_INVALID_ARG; // too short hex
|
||||
}
|
||||
input.push_back((char)in.get());
|
||||
input.push_back(static_cast<char>(in.get()));
|
||||
|
||||
result_t result;
|
||||
unsigned char value = (unsigned char)parseInt(input.c_str(), 16, 0, 0xff, result);
|
||||
@@ -441,7 +447,7 @@ result_t Message::create(vector<string>::iterator& it, const vector<string>::ite
|
||||
unsigned char dstAddress = *it;
|
||||
string useCircuit = circuit;
|
||||
if (multiple) {
|
||||
sprintf(num, ".%d", index);
|
||||
snprintf(num, sizeof(num), ".%d", index);
|
||||
useCircuit = useCircuit + num;
|
||||
}
|
||||
Message* message;
|
||||
@@ -516,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) {
|
||||
@@ -836,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<SymbolString**>(calloc(cnt, sizeof(SymbolString*)));
|
||||
m_lastSlaveDatas = reinterpret_cast<SymbolString**>(calloc(cnt, sizeof(SymbolString*)));
|
||||
m_lastMasterUpdateTimes = reinterpret_cast<time_t*>(calloc(cnt, sizeof(time_t)));
|
||||
m_lastSlaveUpdateTimes = reinterpret_cast<time_t*>(calloc(cnt, sizeof(time_t)));
|
||||
for (size_t index = 0; index < cnt; index++) {
|
||||
m_lastMasterDatas[index] = new SymbolString();
|
||||
m_lastSlaveDatas[index] = new SymbolString();
|
||||
@@ -1325,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<Message*>* derived = messages->getByKey(key);
|
||||
if (derived == NULL) {
|
||||
message = message->derive(m_dstAddress, true);
|
||||
@@ -1518,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<unsigned long long, vector<Message*> >::iterator keyIt = m_messagesByKey.find(key);
|
||||
map<uint64_t, vector<Message*> >::iterator keyIt = m_messagesByKey.find(key);
|
||||
if (keyIt != m_messagesByKey.end()) {
|
||||
Message* other = getFirstAvailable(keyIt->second, message);
|
||||
if (other != NULL) {
|
||||
@@ -1762,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<Message*>* msgs = getByKey(key);
|
||||
if (msgs != NULL) {
|
||||
return msgs->front();
|
||||
@@ -1882,8 +1888,8 @@ string MessageMap::getLoadedFiles(unsigned char address) {
|
||||
return m_loadedFiles[address];
|
||||
}
|
||||
|
||||
vector<Message*>* MessageMap::getByKey(const unsigned long long key) {
|
||||
map<unsigned long long, vector<Message*> >::iterator it = m_messagesByKey.find(key);
|
||||
vector<Message*>* MessageMap::getByKey(const uint64_t key) {
|
||||
map<uint64_t, vector<Message*> >::iterator it = m_messagesByKey.find(key);
|
||||
if (it != m_messagesByKey.end()) {
|
||||
return &it->second;
|
||||
}
|
||||
@@ -1996,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<unsigned long long , vector<Message*> >::iterator it;
|
||||
map<uint64_t , vector<Message*> >::iterator it;
|
||||
if (withPassive) {
|
||||
it = m_messagesByKey.find(key);
|
||||
if (it != m_messagesByKey.end()) {
|
||||
@@ -2099,7 +2105,7 @@ void MessageMap::clear() {
|
||||
if (it->first[0] != '-') { // avoid double free: instances stored multiple times have a key starting with "-"
|
||||
for (vector<Message*>::iterator nit = nameMessages.begin(); nit != nameMessages.end(); nit++) {
|
||||
Message* message = *nit;
|
||||
map<unsigned long long, vector<Message*> >::iterator keyIt = m_messagesByKey.find(message->getKey());
|
||||
map<uint64_t, vector<Message*> >::iterator keyIt = m_messagesByKey.find(message->getKey());
|
||||
if (keyIt != m_messagesByKey.end()) {
|
||||
vector<Message*>* keyMessages = &keyIt->second;
|
||||
if (!keyMessages->empty()) {
|
||||
@@ -2116,7 +2122,7 @@ void MessageMap::clear() {
|
||||
nameMessages.clear();
|
||||
}
|
||||
// free remaining message instances by key
|
||||
for (map<unsigned long long, vector<Message*> >::iterator it = m_messagesByKey.begin(); it != m_messagesByKey.end(); it++) {
|
||||
for (map<uint64_t, vector<Message*> >::iterator it = m_messagesByKey.begin(); it != m_messagesByKey.end(); it++) {
|
||||
vector<Message*> keyMessages = it->second;
|
||||
for (vector<Message*>::iterator kit = keyMessages.begin(); kit != keyMessages.end(); kit++) {
|
||||
Message* message = *kit;
|
||||
@@ -2197,3 +2203,5 @@ void MessageMap::dump(ostream& output, bool withConditions) {
|
||||
output << endl;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
+18
-11
@@ -19,6 +19,7 @@
|
||||
#ifndef LIB_EBUS_MESSAGE_H_
|
||||
#define LIB_EBUS_MESSAGE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
@@ -56,7 +57,11 @@
|
||||
* template class.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::binary_function;
|
||||
using std::priority_queue;
|
||||
using std::deque;
|
||||
|
||||
class Condition;
|
||||
class SimpleCondition;
|
||||
@@ -156,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<unsigned char> id,
|
||||
static uint64_t createKey(const vector<unsigned char> id,
|
||||
const bool isWrite, const bool isPassive,
|
||||
const unsigned char srcAddress, const unsigned char dstAddress);
|
||||
|
||||
@@ -167,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);
|
||||
|
||||
/**
|
||||
@@ -175,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.
|
||||
@@ -328,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.
|
||||
@@ -569,7 +574,7 @@ class Message {
|
||||
* <li>bytes 3-0: ID bytes (with cyclic xor if more than 4)</li>
|
||||
* </ul>
|
||||
*/
|
||||
unsigned long long m_key;
|
||||
uint64_t m_key;
|
||||
|
||||
/** the @a DataField for encoding/decoding the message. */
|
||||
DataField* m_data;
|
||||
@@ -702,7 +707,7 @@ class ChainedMessage : public Message {
|
||||
/**
|
||||
* A function that compares the weighted poll priority of two @a Message instances.
|
||||
*/
|
||||
struct compareMessagePriority : binary_function <Message*, Message*, bool> {
|
||||
struct compareMessagePriority : binary_function<Message*, Message*, bool> {
|
||||
/**
|
||||
* Compare the weighted poll priority of the two @a Message instances.
|
||||
* @param x the first @a Message.
|
||||
@@ -1131,7 +1136,7 @@ class MessageMap : public FileReader {
|
||||
* Construct a new instance.
|
||||
* @param addAll whether to add all messages, even if duplicate.
|
||||
*/
|
||||
MessageMap(const bool addAll = false) : FileReader::FileReader(true),
|
||||
explicit MessageMap(const bool addAll = false) : FileReader::FileReader(true),
|
||||
m_addAll(addAll), m_maxIdLength(0), m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) {
|
||||
m_scanMessage = Message::createScanMessage();
|
||||
}
|
||||
@@ -1224,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<Message*>* getByKey(const unsigned long long key);
|
||||
vector<Message*>* getByKey(const uint64_t key);
|
||||
|
||||
/**
|
||||
* Find the @a Message instance for the specified circuit and name.
|
||||
@@ -1365,7 +1370,7 @@ class MessageMap : public FileReader {
|
||||
map<string, vector<Message*> > m_messagesByName;
|
||||
|
||||
/** the known @a Message instances by key. */
|
||||
map<unsigned long long, vector<Message*> > m_messagesByKey;
|
||||
map<uint64_t, vector<Message*> > m_messagesByKey;
|
||||
|
||||
/** the known @a Message instances to poll, by priority. */
|
||||
MessagePriorityQueue m_pollMessages;
|
||||
@@ -1377,4 +1382,6 @@ class MessageMap : public FileReader {
|
||||
map<string, vector<Instruction*> > m_instructions;
|
||||
};
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_MESSAGE_H_
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#include "result.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
const char* getResultCode(result_t resultCode) {
|
||||
switch (resultCode) {
|
||||
@@ -58,3 +58,4 @@ const char* getResultCode(result_t resultCode) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
* whereas negative values indicate failure.
|
||||
*/
|
||||
|
||||
namespace ebusd {
|
||||
|
||||
/** type for result code. */
|
||||
enum result_t {
|
||||
RESULT_OK = 0, //!< success
|
||||
@@ -70,4 +72,6 @@ enum result_t {
|
||||
*/
|
||||
const char* getResultCode(result_t resultCode);
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_RESULT_H_
|
||||
|
||||
+10
-3
@@ -23,7 +23,13 @@
|
||||
#include <vector>
|
||||
#include "result.h"
|
||||
|
||||
using namespace std;
|
||||
namespace ebusd {
|
||||
|
||||
using std::ostringstream;
|
||||
using std::nouppercase;
|
||||
using std::setw;
|
||||
using std::hex;
|
||||
using std::setfill;
|
||||
|
||||
/**
|
||||
* CRC8 lookup table for the polynom 0x9b = x^8 + x^7 + x^4 + x^3 + x^1 + 1.
|
||||
@@ -69,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
|
||||
@@ -83,7 +89,7 @@ result_t SymbolString::parseHex(const string& str, const bool isEscaped) {
|
||||
}
|
||||
|
||||
const string SymbolString::getDataStr(const bool unescape, const bool skipLastSymbol) {
|
||||
stringstream sstr;
|
||||
ostringstream sstr;
|
||||
bool previousEscape = false;
|
||||
|
||||
for (size_t i = 0; i < m_data.size(); i++) {
|
||||
@@ -255,3 +261,4 @@ bool isValidAddress(unsigned char addr, bool allowBroadcast) {
|
||||
return addr != SYN && addr != ESC && (allowBroadcast || addr != BROADCAST);
|
||||
}
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
@@ -27,8 +27,6 @@
|
||||
#include <vector>
|
||||
#include "result.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** @file symbol.h
|
||||
* Classes, functions, and constants related to symbols on the eBUS.
|
||||
*
|
||||
@@ -65,6 +63,11 @@ using namespace std;
|
||||
* non-acknowledge, the receiving slave has to repeat its data once.
|
||||
*/
|
||||
|
||||
namespace ebusd {
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
static const unsigned char ESC = 0xA9; //!< escape symbol, either followed by 0x00 for the value 0xA9, or 0x01 for the value 0xAA
|
||||
static const unsigned char SYN = 0xAA; //!< synchronization symbol
|
||||
static const unsigned char ACK = 0x00; //!< positive acknowledge
|
||||
@@ -81,7 +84,7 @@ class SymbolString {
|
||||
* Creates a new empty escaped or unescaped instance.
|
||||
* @param escaped whether to create an escaped instance.
|
||||
*/
|
||||
SymbolString(const bool escaped = true) : m_unescapeState(escaped ? 0 : 1), m_crc(0) {}
|
||||
explicit SymbolString(const bool escaped = true) : m_unescapeState(escaped ? 0 : 1), m_crc(0) {}
|
||||
|
||||
/**
|
||||
* Add all symbols from the other @a SymbolString and the calculated CRC if escaped.
|
||||
@@ -257,4 +260,6 @@ unsigned char getMasterNumber(unsigned char addr);
|
||||
*/
|
||||
bool isValidAddress(unsigned char addr, bool allowBroadcast = true);
|
||||
|
||||
} // namespace ebusd
|
||||
|
||||
#endif // LIB_EBUS_SYMBOL_H_
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#include <vector>
|
||||
#include "data.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ebusd;
|
||||
|
||||
static bool error = false;
|
||||
|
||||
@@ -264,9 +264,9 @@ int main() {
|
||||
{"x,,uch,10", "3.8", "10feffff0126", "00", ""},
|
||||
{"x,,uch,-10", "380", "10feffff0126", "00", ""},
|
||||
{"x,,uch,=48", "", "10feffff01ab", "00", "W"},
|
||||
{"x,,uch,==48", "", "10feffff01ab", "00", "rW"},
|
||||
{"x,,uch,==48", "", "10feffff01ab", "00", "rW"},
|
||||
{"x,,uch,=48", "", "10feffff0130", "00", ""},
|
||||
{"x,,uch,==48", "", "10feffff0130", "00", ""},
|
||||
{"x,,uch,==48", "", "10feffff0130", "00", ""},
|
||||
{"x,,sch", "-90", "10feffff01a6", "00", ""},
|
||||
{"x,,sch", "0", "10feffff0100", "00", ""},
|
||||
{"x,,sch", "-1", "10feffff01ff", "00", ""},
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "device.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ebusd;
|
||||
|
||||
int main() {
|
||||
Device* device = Device::create("/dev/ttyUSB20", true, false, false);
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "filereader.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ebusd;
|
||||
|
||||
static bool error = false;
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <map>
|
||||
#include "message.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ebusd;
|
||||
|
||||
void verify(bool expectFailMatch, string type, string input,
|
||||
bool match, string expectStr, string gotStr) {
|
||||
@@ -45,12 +45,14 @@ void verify(bool expectFailMatch, string type, string input,
|
||||
|
||||
DataFieldTemplates* templates = NULL;
|
||||
|
||||
namespace ebusd {
|
||||
DataFieldTemplates* getTemplates(const string filename) {
|
||||
if (filename == "") { // avoid compiler warning
|
||||
return templates;
|
||||
}
|
||||
return templates;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// message: [type],[circuit],name,[comment],[QQ[;QQ]*],[ZZ],[PBSB],[ID],fields...
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "symbol.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ebusd;
|
||||
|
||||
static bool error = false;
|
||||
|
||||
|
||||
+13
-14
@@ -25,8 +25,6 @@
|
||||
#include <string.h>
|
||||
#include "clock.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/** the name of each @a LogFacility. */
|
||||
static const char *facilityNames[] = {
|
||||
"main",
|
||||
@@ -59,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<char*>(input), *value = NULL;
|
||||
int newFacilites = 0;
|
||||
while (*opt) {
|
||||
int val = getsubopt(&opt, (char *const *)facilityNames, &value);
|
||||
@@ -70,7 +68,7 @@ bool setLogFacilities(const char* facilities) {
|
||||
if (val == lf_COUNT) {
|
||||
newFacilites = LF_ALL;
|
||||
} else {
|
||||
newFacilites |= 1<<val;
|
||||
newFacilites |= 1 << val;
|
||||
}
|
||||
}
|
||||
//s_lastFacilities = newFacilites;
|
||||
@@ -81,17 +79,18 @@ bool setLogFacilities(const char* facilities) {
|
||||
|
||||
bool getLogFacilities(char* buffer) {
|
||||
if (s_logFacilites == LF_ALL) {
|
||||
return strcpy(buffer, facilityNames[lf_COUNT]) != NULL;
|
||||
return snprintf(buffer, 48, "%s", facilityNames[lf_COUNT]) != 0;
|
||||
}
|
||||
*buffer = 0; // for strcat to work
|
||||
bool found = false;
|
||||
size_t len = 0;
|
||||
for (int val = 0; val < lf_COUNT; val++) {
|
||||
if (s_logFacilites&(1<<val)) {
|
||||
if (s_logFacilites&(1 << val)) {
|
||||
if (found) {
|
||||
strcat(buffer, ",");
|
||||
len += snprintf(buffer+len, 48-len, ",");
|
||||
}
|
||||
found = true;
|
||||
strcat(buffer, facilityNames[val]);
|
||||
len += snprintf(buffer+len, 48-len, "%s", facilityNames[val]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -99,7 +98,7 @@ bool getLogFacilities(char* buffer) {
|
||||
|
||||
bool setLogLevel(const char* level) {
|
||||
char *input = strdup(level);
|
||||
char *opt = (char*)input, *value = NULL;
|
||||
char *opt = reinterpret_cast<char*>(input), *value = NULL;
|
||||
int newLevel = 0;
|
||||
if (*opt) {
|
||||
int val = getsubopt(&opt, (char *const *)levelNames, &value);
|
||||
@@ -138,20 +137,20 @@ void closeLogFile() {
|
||||
}
|
||||
|
||||
bool needsLog(const LogFacility facility, const LogLevel level) {
|
||||
return ((s_logFacilites & (1<<facility)) != 0)
|
||||
return ((s_logFacilites & (1 << facility)) != 0)
|
||||
&& (s_logLevel >= level);
|
||||
}
|
||||
|
||||
void logWrite(const char* facility, const char* level, const char* message, va_list ap) {
|
||||
struct timespec ts;
|
||||
struct tm* tm;
|
||||
struct tm td;
|
||||
clockGettime(&ts);
|
||||
tm = localtime(&ts.tv_sec);
|
||||
localtime_r(&ts.tv_sec, &td);
|
||||
char* buf;
|
||||
if (vasprintf(&buf, message, ap) >= 0 && buf) {
|
||||
fprintf(s_logFile, "%04d-%02d-%02d %02d:%02d:%02d.%03ld [%s %s] %s\n",
|
||||
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
|
||||
facility, level, buf);
|
||||
fflush(s_logFile);
|
||||
}
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ enum LogFacility {
|
||||
};
|
||||
|
||||
/** macro for enabling all log facilities. */
|
||||
#define LF_ALL ((1<<lf_main) | (1<<lf_network) | (1<<lf_bus) | (1<<lf_update) | (1<<lf_other))
|
||||
#define LF_ALL ((1 << lf_main) | (1 << lf_network) | (1 << lf_bus) | (1 << lf_update) | (1 << lf_other))
|
||||
|
||||
/** the available log levels. */
|
||||
enum LogLevel {
|
||||
@@ -53,7 +53,7 @@ bool setLogFacilities(const char* facilities);
|
||||
|
||||
/**
|
||||
* Get the log facilities.
|
||||
* @param buffer the buffer into which the facilities are written to (separated by comma, buffer needs to be at last 32 characters long).
|
||||
* @param buffer the buffer into which the facilities are written to (separated by comma, buffer needs to be at last 48 characters long).
|
||||
* @return true on success, false on error.
|
||||
*/
|
||||
bool getLogFacilities(char* buffer);
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
/** \file queue.h */
|
||||
|
||||
using namespace std;
|
||||
using std::list;
|
||||
|
||||
/**
|
||||
* Thread safe template class for queuing items.
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
#include <string>
|
||||
#include "clock.h"
|
||||
|
||||
using namespace std;
|
||||
using std::streamsize;
|
||||
|
||||
RotateFile::~RotateFile() {
|
||||
if (m_stream) {
|
||||
@@ -58,12 +58,12 @@ void RotateFile::write(unsigned char* value, unsigned int size, bool received) {
|
||||
}
|
||||
if (m_textMode) {
|
||||
struct timespec ts;
|
||||
struct tm* tm;
|
||||
struct tm td;
|
||||
clockGettime(&ts);
|
||||
tm = localtime(&ts.tv_sec);
|
||||
localtime_r(&ts.tv_sec, &td);
|
||||
fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c",
|
||||
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
|
||||
td.tm_year+1900, td.tm_mon+1, td.tm_mday,
|
||||
td.tm_hour, td.tm_min, td.tm_sec, ts.tv_nsec/1000000,
|
||||
received ? '<' : '>');
|
||||
for (unsigned int pos = 0; pos < size; pos++) {
|
||||
fprintf(m_stream, "%2.2x ", value[pos]);
|
||||
@@ -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);
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#define LIB_UTILS_ROTATEFILE_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
@@ -28,7 +29,7 @@
|
||||
* Helpers for writing to rotating files.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
using std::string;
|
||||
|
||||
/**
|
||||
* Helper class for writing to a rotating file with maximum size.
|
||||
@@ -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_
|
||||
|
||||
@@ -23,8 +23,6 @@
|
||||
#include <string.h>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd) {
|
||||
char ip[17];
|
||||
inet_ntop(AF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, (socklen_t)sizeof(ip)-1);
|
||||
@@ -41,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<char*>(&address), 0, sizeof(address));
|
||||
|
||||
if (inet_addr(server.c_str()) == INADDR_NONE) {
|
||||
struct hostent* he;
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
/** \file tcpsocket.h */
|
||||
|
||||
using namespace std;
|
||||
using std::string;
|
||||
|
||||
#ifdef __MACH__
|
||||
#ifndef MSG_NOSIGNAL
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "clock.h"
|
||||
|
||||
void* Thread::runThread(void* arg) {
|
||||
((Thread*)arg)->enter();
|
||||
reinterpret_cast<Thread*>(arg)->enter();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,10 @@
|
||||
#include <string>
|
||||
#include "tcpsocket.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
using std::ostringstream;
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/** A structure holding all program options. */
|
||||
struct options {
|
||||
|
||||
+11
-2
@@ -28,8 +28,17 @@
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include "device.h"
|
||||
#include "result.h"
|
||||
|
||||
using namespace std;
|
||||
using std::hex;
|
||||
using std::fstream;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::setw;
|
||||
using std::setfill;
|
||||
using std::ios;
|
||||
using ebusd::result_t;
|
||||
using ebusd::Device;
|
||||
|
||||
/** A structure holding all program options. */
|
||||
struct options {
|
||||
@@ -133,7 +142,7 @@ int main(int argc, char* argv[]) {
|
||||
return EINVAL;
|
||||
}
|
||||
result_t result = device->open();
|
||||
if (result != RESULT_OK) {
|
||||
if (result != ebusd::RESULT_OK) {
|
||||
cout << "unable to open " << opt.device << ": " << getResultCode(result) << endl;
|
||||
}
|
||||
if (!device->isValid()) {
|
||||
|
||||
Reference in New Issue
Block a user