code style

This commit is contained in:
john30
2017-01-14 15:02:10 +01:00
parent b3bb2e08ac
commit 54a6c0f673
32 changed files with 484 additions and 665 deletions
+5 -11
View File
@@ -45,8 +45,7 @@ using namespace std;
* @return the string corresponding to the @a BusState.
*/
const char* getStateCode(BusState state) {
switch (state)
{
switch (state) {
case bs_noSignal: return "no signal";
case bs_skip: return "skip";
case bs_ready: return "ready";
@@ -322,8 +321,7 @@ result_t BusHandler::handleSymbol() {
BusRequest* startRequest = NULL;
// check if another symbol has to be sent and determine timeout for receive
switch (m_state)
{
switch (m_state) {
case bs_noSignal:
timeout = m_generateSynInterval > 0 ? m_generateSynInterval : SIGNAL_TIMEOUT;
break;
@@ -489,8 +487,7 @@ result_t BusHandler::handleSymbol() {
unsigned int headerLen, crcPos;
switch (m_state)
{
switch (m_state) {
case bs_noSignal:
return setState(bs_skip, RESULT_OK);
@@ -728,7 +725,6 @@ result_t BusHandler::handleSymbol() {
return setState(bs_skip, RESULT_OK);
}
return setState(bs_skip, RESULT_ERR_INVALID_ARG);
}
return RESULT_OK;
}
@@ -740,16 +736,14 @@ result_t BusHandler::setState(BusState state, result_t result, bool firstRepetit
m_currentRequest->m_busLostRetries++;
m_nextRequests.push(m_currentRequest); // repeat
m_currentRequest = NULL;
}
else if (state == bs_sendSyn || (result != RESULT_OK && !firstRepetition)) {
} else if (state == bs_sendSyn || (result != RESULT_OK && !firstRepetition)) {
logDebug(lf_bus, "notify request: %s", getResultCode(result));
unsigned char dstAddress = m_currentRequest->m_master[1];
if (result == RESULT_OK) {
addSeenAddress(dstAddress);
}
bool restart = m_currentRequest->notify(
result == RESULT_ERR_SYN && (m_state == bs_recvCmdAck || m_state == bs_recvRes) ? RESULT_ERR_TIMEOUT : result, m_response
);
result == RESULT_ERR_SYN && (m_state == bs_recvCmdAck || m_state == bs_recvRes) ? RESULT_ERR_TIMEOUT : result, m_response);
if (restart) {
m_currentRequest->m_busLostRetries = 0;
m_nextRequests.push(m_currentRequest);
+18 -35
View File
@@ -93,11 +93,10 @@ class BusHandler;
/**
* Generic request for sending to and receiving from the bus.
*/
class BusRequest
{
class BusRequest {
friend class BusHandler;
public:
public:
/**
* Constructor.
* @param master the escaped master data @a SymbolString to send.
@@ -120,8 +119,8 @@ public:
*/
virtual bool notify(result_t result, SymbolString& slave) = 0;
protected:
protected:
/** the escaped master data @a SymbolString to send. */
SymbolString& m_master;
@@ -130,18 +129,16 @@ protected:
/** whether to automatically delete this @a BusRequest when finished. */
const bool m_deleteOnFinish;
};
/**
* A poll @a BusRequest handled by @a BusHandler itself.
*/
class PollRequest : public BusRequest
{
class PollRequest : public BusRequest {
friend class BusHandler;
public:
public:
/**
* Constructor.
* @param message the associated @a Message.
@@ -164,8 +161,8 @@ public:
// @copydoc
virtual bool notify(result_t result, SymbolString& slave);
private:
private:
/** the escaped master data @a SymbolString. */
SymbolString m_master;
@@ -174,18 +171,16 @@ private:
/** the current part index in @a m_message. */
unsigned char m_index;
};
/**
* A scan @a BusRequest handled by @a BusHandler itself.
*/
class ScanRequest : public BusRequest
{
class ScanRequest : public BusRequest {
friend class BusHandler;
public:
public:
/**
* Constructor.
* @param messageMap the @a MessageMap instance.
@@ -194,8 +189,7 @@ public:
* @param busHandler the @a BusHandler instance to notify of final scan result.
*/
ScanRequest(MessageMap* messageMap, deque<Message*> messages, deque<unsigned char> slaves, BusHandler* busHandler)
: BusRequest(m_master, true), m_messageMap(messageMap), m_index(0), m_allMessages(messages), m_messages(messages), m_slaves(slaves), m_busHandler(busHandler)
{
: BusRequest(m_master, true), m_messageMap(messageMap), m_index(0), m_allMessages(messages), m_messages(messages), m_slaves(slaves), m_busHandler(busHandler) {
m_message = m_messages.front();
m_messages.pop_front();
}
@@ -215,8 +209,8 @@ public:
// @copydoc
virtual bool notify(result_t result, SymbolString& slave);
private:
private:
/** the @a MessageMap instance. */
MessageMap* m_messageMap;
@@ -243,18 +237,16 @@ private:
/** the @a BusHandler instance to notify of final scan result. */
BusHandler* m_busHandler;
};
/**
* An active @a BusRequest that can be waited for.
*/
class ActiveBusRequest : public BusRequest
{
class ActiveBusRequest : public BusRequest {
friend class BusHandler;
public:
public:
/**
* Constructor.
* @param master the escaped master data @a SymbolString to send.
@@ -271,25 +263,21 @@ public:
// @copydoc
virtual bool notify(result_t result, SymbolString& slave);
private:
private:
/** the result of handling the request. */
result_t m_result;
/** reference to @a SymbolString for filling in the received slave data. */
SymbolString& m_slave;
};
/**
* Helper class for keeping track of grabbed messages.
*/
class GrabbedMessage
{
class GrabbedMessage {
public:
/**
* Construct a new instance.
*/
@@ -321,8 +309,8 @@ public:
*/
bool dump(const bool unknown, MessageMap* messages, bool first, ostringstream& output);
private:
private:
/** the last master @a SymbolString. */
SymbolString m_lastMaster;
@@ -331,17 +319,14 @@ private:
/** the number of times this message was seen. */
unsigned int m_count;
};
/**
* Handles input from and output to the bus with respect to the eBUS protocol.
*/
class BusHandler : public WaitThread
{
class BusHandler : public WaitThread {
public:
/**
* Construct a new instance.
* @param device the @a Device instance for accessing the bus.
@@ -375,8 +360,7 @@ public:
m_symPerSec(0), m_maxSymPerSec(0),
m_state(bs_noSignal), m_repeat(false),
m_command(false), m_commandCrcValid(false), m_response(false), m_responseCrcValid(false),
m_grabMessages(true)
{
m_grabMessages(true) {
memset(m_seenAddresses, 0, sizeof(m_seenAddresses));
}
@@ -525,8 +509,8 @@ public:
*/
void setScanConfigLoaded(unsigned char address, string file);
private:
private:
/**
* Handle the next symbol on the bus.
* @return RESULT_OK on success, or an error code.
@@ -664,7 +648,6 @@ private:
/** the grabbed messages by key.*/
map<unsigned long long, GrabbedMessage> m_grabbedMessages;
};
#endif // EBUSD_BUSHANDLER_H_
+5 -13
View File
@@ -54,8 +54,7 @@ bool datahandler_register(BusHandler* busHandler, MessageMap* messages, list<Dat
/**
* Base class for all kinds of data handlers.
*/
class DataHandler
{
class DataHandler {
public:
/**
* Constructor.
@@ -83,17 +82,14 @@ public:
* @return whether this is a @a DataSource instance.
*/
virtual bool isDataSource() { return false; }
};
/**
* Base class for listening to data updates.
*/
class DataSink : virtual public DataHandler
{
class DataSink : virtual public DataHandler {
public:
/**
* Constructor.
*/
@@ -113,21 +109,18 @@ public:
// @copydoc
virtual bool isDataSink() { return true; }
protected:
protected:
/** a map of updated @p Message instances. */
map<Message*, int> m_updatedMessages;
};
/**
* Base class providing data to be sent on the bus.
*/
class DataSource : virtual public DataHandler
{
class DataSource : virtual public DataHandler {
public:
/**
* Constructor.
* @param busHandler the @a BusHandler instance.
@@ -143,11 +136,10 @@ public:
// @copydoc
virtual bool isDataSource() { return true; }
protected:
protected:
/** the @a BusHandler instance. */
BusHandler* m_busHandler;
};
#endif // EBUSD_DATAHANDLER_H_
+4 -7
View File
@@ -50,10 +50,8 @@ DataHandler* mqtthandler_register(BusHandler* busHandler, MessageMap* messages);
/**
* The main class supporting MQTT data handling.
*/
class MqttHandler : public DataSink, public DataSource, public Thread
{
class MqttHandler : public DataSink, public DataSource, public Thread {
public:
/**
* Constructor.
* @param busHandler the @a BusHandler instance.
@@ -76,13 +74,13 @@ public:
*/
void notifyTopic(string topic, string data);
protected:
protected:
// @copydoc
virtual void run();
private:
private:
/**
* Called regularly to handle MQTT traffic.
*/
@@ -128,7 +126,6 @@ private:
/** the mosquitto structure if initialized, or NULL. */
struct mosquitto* m_mosquitto;
};
#endif // EBUSD_DATAHANDLER_H_
#endif // EBUSD_MQTTHANDLER_H_
-2
View File
@@ -141,7 +141,6 @@ void Connection::run() {
break;
}
}
}
delete m_socket;
@@ -230,7 +229,6 @@ void Network::run() {
}
#endif
#endif
while (true) {
#ifdef HAVE_PPOLL
// wait for new fd event
+16 -25
View File
@@ -38,17 +38,14 @@ class Connection;
/**
* Class for data/message transfer between @a Connection and @a MainLoop.
*/
class NetMessage
{
class NetMessage {
public:
/**
* Constructor.
* @param isHttp whether this is a HTTP message.
*/
NetMessage(const bool isHttp)
: m_isHttp(isHttp), m_resultSet(false), m_disconnect(false), m_listening(false), m_listenSince(0)
{
: 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);
}
@@ -56,12 +53,12 @@ public:
/**
* Destructor.
*/
~NetMessage()
{
~NetMessage() {
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
private:
/**
* Hidden copy constructor.
@@ -69,15 +66,14 @@ private:
*/
NetMessage(const NetMessage& src);
public:
public:
/**
* Add request data received from the client.
* @param request the request data from the client.
* @return true when the request is complete and the response shall be prepared.
*/
bool add(string request)
{
bool add(string request) {
if (request.length() > 0) {
request.erase(remove(request.begin(), request.end(), '\r'), request.end());
m_request.append(request);
@@ -125,8 +121,7 @@ public:
* Wait for the result being set and return the result string.
* @return the result string.
*/
string getResult()
{
string getResult() {
pthread_mutex_lock(&m_mutex);
while (!m_resultSet)
@@ -148,8 +143,7 @@ public:
* @param listenUntil the end time to which to updates were added (exclusive).
* @param disconnect true when the client shall be disconnected.
*/
void setResult(const string result, const bool listening, const time_t listenUntil, const bool disconnect)
{
void setResult(const string result, const bool listening, const time_t listenUntil, const bool disconnect) {
pthread_mutex_lock(&m_mutex);
m_result = result;
m_disconnect = disconnect;
@@ -173,6 +167,7 @@ public:
*/
bool isDisconnect() { return m_disconnect; }
private:
/** whether this is a HTTP message. */
const bool m_isHttp;
@@ -200,15 +195,12 @@ private:
/** start timestamp of listening update. */
time_t m_listenSince;
};
/**
* class connection which handle client and baseloop communication.
*/
class Connection : public Thread
{
class Connection : public Thread {
public:
/**
* Constructor.
@@ -217,8 +209,9 @@ public:
* @param netQueue the reference to the @a NetMessage @a Queue.
*/
Connection(TCPSocket* socket, const bool isHttp, Queue<NetMessage*>* netQueue)
: Thread(), m_isHttp(isHttp), m_socket(socket), m_netQueue(netQueue)
{ m_id = ++m_ids; }
: Thread(), m_isHttp(isHttp), m_socket(socket), m_netQueue(netQueue) {
m_id = ++m_ids;
}
virtual ~Connection() { if (m_socket) delete m_socket; }
/**
@@ -237,6 +230,7 @@ public:
*/
int getID() { return m_id; }
private:
/** whether this is a HTTP connection. */
const bool m_isHttp;
@@ -255,15 +249,12 @@ private:
/** the IF of the last opened connection. */
static int m_ids;
};
/**
* class network which listening on tcp socket for incoming connections.
*/
class Network : public Thread
{
class Network : public Thread {
public:
/**
* create a network instance and listening for incoming connections.
@@ -289,6 +280,7 @@ public:
*/
void stop() const { m_notify.notify(); usleep(100000); }
private:
/** the list of active @a Connection instances. */
list<Connection*> m_connections;
@@ -312,7 +304,6 @@ private:
* clean inactive connections from container.
*/
void cleanConnections();
};
#endif // EBUSD_NETWORK_H_
+3 -3
View File
@@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIB_EBUS_CONTRIB_H_
#define LIB_EBUS_CONTRIB_H_
#ifndef LIB_EBUS_CONTRIB_CONTRIB_H_
#define LIB_EBUS_CONTRIB_CONTRIB_H_
/** @file contrib.h
* Contributed sources that may be excluded from regular builds.
@@ -32,4 +32,4 @@ using namespace std;
*/
bool libebus_contrib_register();
#endif // LIB_EBUS_CONTRIB_H_
#endif // LIB_EBUS_CONTRIB_CONTRIB_H_
+1 -4
View File
@@ -40,10 +40,8 @@ using namespace std;
* A special variant of @a NumberDataType for TEM/Dungs ParamID in master/slave
* data.
*/
class TemParamDataType : public NumberDataType
{
class TemParamDataType : public NumberDataType {
public:
/**
* Constructs a new instance.
* @param id the type identifier.
@@ -63,7 +61,6 @@ public:
virtual result_t writeSymbols(istringstream& input,
const unsigned char offset, const unsigned char length,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
};
/**
+3 -6
View File
@@ -340,8 +340,7 @@ result_t SingleDataField::read(const PartType partType,
if (partType != m_partType) {
return RESULT_EMPTY;
}
switch (m_partType)
{
switch (m_partType) {
case pt_masterData:
offset = (unsigned char)(offset + 5); // skip QQ ZZ PB SB NN
break;
@@ -368,8 +367,7 @@ result_t SingleDataField::read(const PartType partType,
if (partType != m_partType) {
return RESULT_OK;
}
switch (m_partType)
{
switch (m_partType) {
case pt_masterData:
offset = (unsigned char)(offset + 5); // skip QQ ZZ PB SB NN
break;
@@ -435,8 +433,7 @@ result_t SingleDataField::write(istringstream& input,
if (partType != m_partType) {
return RESULT_OK;
}
switch (m_partType)
{
switch (m_partType) {
case pt_masterData:
offset = (unsigned char)(offset + 5); // skip QQ ZZ PB SB NN
break;
+15 -36
View File
@@ -53,10 +53,8 @@ class SingleDataField;
/**
* Base class for all kinds of data fields.
*/
class DataField
{
class DataField {
public:
/**
* Constructs a new instance.
* @param name the field name.
@@ -205,24 +203,21 @@ public:
const PartType partType, SymbolString& data,
unsigned char offset, char separator = UI_FIELD_SEPARATOR, unsigned char* length = NULL) = 0;
protected:
protected:
/** the field name. */
const string m_name;
/** the field comment. */
const string m_comment;
};
/**
* A single @a DataField holding a value.
*/
class SingleDataField : public DataField
{
class SingleDataField : public DataField {
public:
/**
* Constructs a new instance.
* @param name the field name.
@@ -325,8 +320,8 @@ public:
const PartType partType, SymbolString& data,
unsigned char offset, char separator = UI_FIELD_SEPARATOR, unsigned char* length = NULL);
protected:
protected:
/**
* Internal method for reading the field from a @a SymbolString.
* @param input the unescaped @a SymbolString to read the binary value from.
@@ -353,8 +348,6 @@ protected:
const unsigned char offset,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
protected:
/** the value unit. */
const string m_unit;
@@ -366,17 +359,14 @@ protected:
/** the number of symbols in the message part in which the field is stored. */
const unsigned char m_length;
};
/**
* A numeric data field with a list of value=text assignments and a string representation.
*/
class ValueListDataField : public SingleDataField
{
class ValueListDataField : public SingleDataField {
public:
/**
* Constructs a new instance.
* @param name the field name.
@@ -410,8 +400,8 @@ public:
// @copydoc
virtual void dump(ostream& output);
protected:
protected:
// @copydoc
virtual result_t readSymbols(SymbolString& input, const bool isMaster,
const unsigned char offset,
@@ -422,21 +412,18 @@ protected:
const unsigned char offset,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
private:
private:
/** the value=text assignments. */
map<unsigned int, string> m_values;
};
/**
* A data field with a constant value.
*/
class ConstantDataField : public SingleDataField
{
class ConstantDataField : public SingleDataField {
public:
/**
* Constructs a new instance.
* @param name the field name.
@@ -471,8 +458,8 @@ public:
// @copydoc
virtual void dump(ostream& output);
protected:
protected:
// @copydoc
virtual result_t readSymbols(SymbolString& input, const bool isMaster,
const unsigned char offset,
@@ -483,24 +470,21 @@ protected:
const unsigned char offset,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
private:
private:
/** the constant value. */
const string m_value;
/** whether to verify the read value against the constant value. */
const bool m_verify;
};
/**
* A set of @a DataField instances.
*/
class DataFieldSet : public DataField
{
class DataFieldSet : public DataField {
public:
/**
* Get the @a DataFieldSet for parsing the identification message (service 0x07 0x04).
* @return the @a DataFieldSet for parsing the identification message. This is:<ul>
@@ -522,8 +506,7 @@ public:
DataFieldSet(const string name, const string comment,
const vector<SingleDataField*> fields)
: DataField(name, comment),
m_fields(fields)
{
m_fields(fields) {
bool uniqueNames = true;
map<string, string> names;
for (vector<SingleDataField*>::const_iterator it = fields.begin(); it != fields.end(); it++) {
@@ -603,8 +586,8 @@ public:
const PartType partType, SymbolString& data,
unsigned char offset, char separator = UI_FIELD_SEPARATOR, unsigned char* length = NULL);
private:
private:
/** the @a DataFieldSet containing the ident message @a SingleDataField instances, or NULL. */
static DataFieldSet* s_identFields;
@@ -613,17 +596,14 @@ private:
/** whether all fields have a unique name. */
bool m_uniqueNames;
};
/**
* A map of template @a DataField instances.
*/
class DataFieldTemplates : public FileReader
{
class DataFieldTemplates : public FileReader {
public:
/**
* Constructs a new instance.
*/
@@ -670,11 +650,10 @@ public:
*/
DataField* get(string name);
private:
private:
/** the known template @a DataField instances by name. */
map<string, DataField*> m_fieldsByName;
};
#endif // LIB_EBUS_DATA_H_
+4 -8
View File
@@ -123,8 +123,7 @@ void printErrorPos(ostream& out, vector<string>::iterator begin, const vector<st
}
bool DataType::dump(ostream& output, const unsigned char length) const
{
bool DataType::dump(ostream& output, const unsigned char length) const {
output << m_id;
if (isAdjustableLength()) {
if (length == REMAIN_LEN) {
@@ -312,8 +311,7 @@ result_t DateTimeDataType::readSymbols(SymbolString& input, const bool isMaster,
}
ch = (unsigned char)((ch >> 4) * 10 + (ch & 0x0f));
}
switch (type)
{
switch (type) {
case 2: // date only
if (!hasFlag(REQ) && ch == m_replacement) {
if (i + 1 != length) {
@@ -442,8 +440,7 @@ result_t DateTimeDataType::writeSymbols(istringstream& input,
bool skip = false;
for (offset = start; i < count; offset += skip ? 0 : incr, i++) {
skip = false;
switch (type)
{
switch (type) {
case 2: // date only
if (length == 4 && i == 2) {
continue; // skip weekday in between
@@ -584,8 +581,7 @@ unsigned char NumberDataType::calcPrecision(const int divisor) {
return precision;
}
bool NumberDataType::dump(ostream& output, unsigned char length) const
{
bool NumberDataType::dump(ostream& output, unsigned char length) const {
if (m_bitCount < 8) {
DataType::dump(output, m_bitCount);
} else {
+10 -25
View File
@@ -146,10 +146,8 @@ void printErrorPos(ostream& out, vector<string>::iterator begin, const vector<st
/**
* Base class for all kinds of data types.
*/
class DataType
{
class DataType {
public:
/**
* Constructs a new instance.
* @param id the type identifier.
@@ -251,8 +249,8 @@ public:
const unsigned char offset, const unsigned char length,
SymbolString& output, const bool isMaster, unsigned char* usedLength) = 0;
protected:
protected:
/** the type identifier. */
const string m_id;
@@ -264,17 +262,14 @@ protected:
/** the replacement value (fill-up value for @a StringDataType, no replacement if equal to @a NumberDataType#minValue). */
const unsigned int m_replacement;
};
/**
* A string based @a DataType.
*/
class StringDataType : public DataType
{
class StringDataType : public DataType {
public:
/**
* Constructs a new instance.
* @param id the type identifier.
@@ -307,21 +302,18 @@ public:
const unsigned char offset, const unsigned char length,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
private:
private:
/** true for hex digits instead of characters. */
const bool m_isHex;
};
/**
* A date/time based @a DataType.
*/
class DateTimeDataType : public DataType
{
class DateTimeDataType : public DataType {
public:
/**
* Constructs a new instance.
* @param id the type identifier.
@@ -371,8 +363,8 @@ public:
const unsigned char offset, const unsigned char length,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
private:
private:
/** true if date part is present. */
const bool m_hasDate;
@@ -381,17 +373,14 @@ private:
/** the resolution in minutes for time types, or 1. */
const short m_resolution;
};
/**
* A number based @a DataType.
*/
class NumberDataType : public DataType
{
class NumberDataType : public DataType {
public:
/**
* Constructs a new instance for multiple of 8 bits.
* @param id the type identifier.
@@ -501,8 +490,8 @@ public:
const unsigned char offset, const unsigned char length,
SymbolString& output, const bool isMaster, unsigned char* usedLength);
private:
private:
/** the minimum raw value. */
const unsigned int m_minValue;
@@ -520,17 +509,14 @@ private:
/** the base @a NumberDataType for derived instances. */
NumberDataType* m_baseType;
};
/**
* A map of base @a DataType instances.
*/
class DataTypeList
{
class DataTypeList {
public:
/**
* Constructs a new instance and registers the known base data types.
*/
@@ -577,8 +563,8 @@ public:
*/
DataType* get(const string id, const unsigned char length = 0);
private:
private:
/** the known @a DataType instances by ID only. */
map<string, DataType*> m_typesById;
@@ -596,7 +582,6 @@ private:
/** true when contributed datatypes were successfully initialized. */
static bool s_contrib_initialized;
#endif
};
#endif // LIB_EBUS_DATATYPE_H_
+2 -1
View File
@@ -269,7 +269,8 @@ result_t NetworkDevice::open() {
if (ioctl(m_fd, FIONREAD, &cnt) >= 0 && cnt > 1) {
// skip buffered input
unsigned char buf[256];
while (::read(m_fd, &buf, 256) > 0);
while (::read(m_fd, &buf, 256) > 0) {
}
}
if (m_bufSize == 0) {
m_bufSize = MAX_LEN+1;
+10 -16
View File
@@ -41,10 +41,8 @@ using namespace std;
/**
* Interface for listening to data received on/sent to a device.
*/
class DeviceListener
{
class DeviceListener {
public:
/**
* Destructor.
*/
@@ -56,16 +54,13 @@ public:
* @param received @a true on reception, @a false on sending.
*/
virtual void notifyDeviceData(const unsigned char byte, bool received) = 0; // abstract
};
/**
* The base class for accessing an eBUS.
*/
class Device
{
class Device {
public:
/**
* Construct a new instance.
@@ -150,6 +145,7 @@ public:
*/
void setListener(DeviceListener* listener) { m_listener = listener; }
protected:
/**
* Check if the device is still available and close it if not.
@@ -176,7 +172,6 @@ protected:
*/
virtual ssize_t read(unsigned char& value) { return ::read(m_fd, &value, 1); }
protected:
/** the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). */
const char* m_name;
@@ -192,18 +187,16 @@ protected:
/** the opened file descriptor, or -1. */
int m_fd;
private:
private:
/** the @a DeviceListener, or NULL. */
DeviceListener* m_listener;
};
/**
* The @a Device for directly connected serial interfaces (tty).
*/
class SerialDevice : public Device
{
class SerialDevice : public Device {
public:
/**
* Construct a new instance.
@@ -221,21 +214,21 @@ public:
// @copydoc
virtual void close();
protected:
// @copydoc
virtual void checkDevice();
private:
/** the previous settings of the device for restoring. */
termios m_oldSettings;
};
/**
* The @a Device for remote network interfaces.
*/
class NetworkDevice : public Device
{
class NetworkDevice : public Device {
public:
/**
* Construct a new instance.
@@ -256,6 +249,7 @@ public:
// @copydoc
virtual result_t open();
protected:
// @copydoc
virtual void checkDevice();
@@ -269,6 +263,7 @@ protected:
// @copydoc
virtual ssize_t read(unsigned char& value);
private:
/** the socket address of the device. */
const struct sockaddr_in m_address;
@@ -287,7 +282,6 @@ private:
/** the buffer read position. */
unsigned char m_bufPos;
};
#endif // LIB_EBUS_DEVICE_H_
+10 -20
View File
@@ -60,10 +60,8 @@ extern unsigned int parseInt(const char* str, int base, const unsigned int minVa
/**
* An abstract class that support reading definitions from a file.
*/
class FileReader
{
class FileReader {
public:
/**
* Construct a new instance.
*/
@@ -85,8 +83,7 @@ public:
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t readFromFile(const string filename, bool verbose = false,
string defaultDest = "", string defaultCircuit = "", string defaultSuffix = "")
{
string defaultDest = "", string defaultCircuit = "", string defaultSuffix = "") {
ifstream ifs;
ifs.open(filename.c_str(), ifstream::in);
if (!ifs.is_open()) {
@@ -177,8 +174,7 @@ public:
*/
virtual result_t addDefaultFromFile(vector< vector<string> >& defaults, vector<string>& row,
vector<string>::iterator& begin, string defaultDest, string defaultCircuit, string defaultSuffix,
const string& filename, unsigned int lineNo)
{
const string& filename, unsigned int lineNo) {
defaults.push_back(row);
begin = row.end();
return RESULT_OK;
@@ -204,8 +200,7 @@ public:
* Left and right trim the string.
* @param str the @a string to trim.
*/
static void trim(string& str)
{
static void trim(string& str) {
size_t pos = str.find_first_not_of(" \t");
if (pos != string::npos) {
str.erase(0, pos);
@@ -220,8 +215,7 @@ public:
* Convert all upper case characters in the string to lower case.
* @param str the @a string to convert.
*/
static void tolower(string& str)
{
static void tolower(string& str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
}
@@ -232,8 +226,7 @@ public:
* @param lineNo the current line number (incremented with each line read).
* @return true if there are more lines to read, false when there are no more lines left.
*/
static bool splitFields(istream& ifs, vector<string>& row, unsigned int& lineNo)
{
static bool splitFields(istream& ifs, vector<string>& row, unsigned int& lineNo) {
row.clear();
string line;
bool quotedText = false, wasQuoted = false;
@@ -251,8 +244,7 @@ public:
}
for (size_t pos = 0; pos < length; pos++) {
char ch = line[pos];
switch (ch)
{
switch (ch) {
case FIELD_SEPARATOR:
if (quotedText) {
field << ch;
@@ -317,8 +309,7 @@ public:
* @return true if at least the address and the identification part were extracted, false otherwise.
*/
static bool extractDefaultsFromFilename(string name, unsigned char& dest, string& ident, string& circuit,
string& suffix, unsigned int& software, unsigned int& hardware)
{
string& suffix, unsigned int& software, unsigned int& hardware) {
ident = circuit = suffix = "";
software = hardware = UINT_MAX;
if (name.length() > 4 && name.substr(name.length()-4) == ".csv") {
@@ -374,16 +365,15 @@ public:
return true;
}
private:
private:
/** whether this instance supports rows with defaults (starting with a star). */
bool m_supportsDefaults;
protected:
protected:
/** a @a string describing the last error position. */
string m_lastError;
};
#endif // LIB_EBUS_FILEREADER_H_
+1 -2
View File
@@ -834,8 +834,7 @@ ChainedMessage::ChainedMessage(const string circuit, const string name,
srcAddress, dstAddress, id,
data, deleteData, pollPriority, condition),
m_ids(ids), m_lengths(lengths),
m_maxTimeDiff(m_ids.size()*15) // 15 seconds per message
{
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*));
+36 -67
View File
@@ -93,11 +93,9 @@ class MessageMap;
/**
* Defines parameters of a message sent or received on the bus.
*/
class Message
{
class Message {
friend class MessageMap;
public:
/**
* Construct a new instance.
* @param circuit the optional circuit name.
@@ -122,6 +120,7 @@ public:
const unsigned char pollPriority = 0,
Condition* condition = NULL);
private:
/**
* Construct a new scan @a Message instance.
@@ -140,6 +139,7 @@ private:
const unsigned char pb, const unsigned char sb,
DataField* data, const bool deleteData);
public:
/**
* Destructor.
@@ -394,8 +394,8 @@ public:
istringstream& input, char separator = UI_FIELD_SEPARATOR,
const unsigned char dstAddress = SYN, unsigned char index = 0);
protected:
protected:
/**
* Prepare a part of the master data @a SymbolString for sending (everything including NN).
* @param master the master data @a SymbolString for writing symbols to.
@@ -406,8 +406,8 @@ protected:
*/
virtual result_t prepareMasterPart(SymbolString& master, istringstream& input, char separator, unsigned char index);
public:
public:
/**
* Prepare the slave @a SymbolString for sending an answer to the bus.
* @param input the @a istringstream to parse the formatted value(s) from.
@@ -521,8 +521,8 @@ public:
*/
virtual void dumpColumn(ostream& output, size_t column, bool withConditions = false);
protected:
protected:
/** the optional circuit name. */
const string m_circuit;
@@ -606,17 +606,14 @@ protected:
/** the system time when this message was last polled for, 0 for never. */
time_t m_lastPollTime;
};
/**
* A chained @a Message that needs more than one read/write on the bus to collect/send the data.
*/
class ChainedMessage : public Message
{
class ChainedMessage : public Message {
public:
/**
* Construct a new instance.
* @param circuit the optional circuit name.
@@ -659,26 +656,26 @@ public:
// @copydoc
virtual unsigned char getCount() { return (unsigned char)m_ids.size(); }
protected:
protected:
// @copydoc
virtual result_t prepareMasterPart(SymbolString& master, istringstream& input, char separator, unsigned char index);
public:
public:
// @copydoc
virtual result_t storeLastData(SymbolString& master, SymbolString& slave);
// @copydoc
virtual result_t storeLastData(const PartType partType, SymbolString& data, unsigned char index);
protected:
protected:
// @copydoc
virtual void dumpColumn(ostream& output, size_t column, bool withConditions = false);
private:
private:
/** the primary, secondary, and optional further ID bytes for each part of the chain. */
const vector< vector<unsigned char> > m_ids;
@@ -699,7 +696,6 @@ private:
/** array of the system times when the corresponding slave data was last updated, 0 for never. */
time_t* m_lastSlaveUpdateTimes;
};
@@ -713,7 +709,7 @@ struct compareMessagePriority : binary_function <Message*,Message*,bool> {
* @param y the second @a Message.
* @return whether @a x is smaller than @a y with regard to their weighted poll priority.
*/
bool operator() (Message* x, Message* y) const { return x->isLessPollWeight(y); };
bool operator() (Message* x, Message* y) const { return x->isLessPollWeight(y); }
};
@@ -721,15 +717,13 @@ struct compareMessagePriority : binary_function <Message*,Message*,bool> {
* Helper class extending @a priority_queue to hold distinct values only.
*/
class MessagePriorityQueue
: public priority_queue<Message*, vector<Message*>, compareMessagePriority>
{
: public priority_queue<Message*, vector<Message*>, compareMessagePriority> {
public:
/**
* Add data to the queue and ensure it is contained only once.
* @param __x the element to add.
*/
void push(const value_type& __x)
{
void push(const value_type& __x) {
for (vector<Message*>::iterator it = c.begin(); it != c.end(); it++) {
if (*it == __x) {
c.erase(it);
@@ -744,10 +738,8 @@ public:
/**
* An abstract condition based on the value of one or more @a Message instances.
*/
class Condition
{
class Condition {
public:
/**
* Construct a new instance.
*/
@@ -776,7 +768,7 @@ public:
* @param valueList the @a string with the new list of values.
* @return the derived @a SimpleCondition instance, or NULL if the value list is invalid.
*/
virtual SimpleCondition* derive(string valueList) { return NULL; };
virtual SimpleCondition* derive(string valueList) { return NULL; }
/**
* Write the condition definition or resolved expression to the @a ostream.
@@ -807,24 +799,21 @@ public:
*/
virtual bool isTrue() = 0;
protected:
protected:
/** the system time when the condition was last checked, 0 for never. */
time_t m_lastCheckTime;
/** whether the condition was @a true during the last check. */
bool m_isTrue;
};
/**
* A simple @a Condition based on the value of one @a Message.
*/
class SimpleCondition : public Condition
{
class SimpleCondition : public Condition {
public:
/**
* Construct a new instance.
* @param condName the name of the condition.
@@ -865,8 +854,8 @@ public:
*/
virtual bool isNumeric() { return true; }
protected:
protected:
/**
* Check the values against the field in the @a Message.
* @param message the @a Message to check against.
@@ -878,8 +867,8 @@ protected:
/** the value that matched in @a checkValue. */
string m_matchedValue;
private:
private:
/** the condition name. */
const string m_condName;
@@ -903,17 +892,14 @@ private:
/** the resolved @a Message instance, or NULL. */
Message* m_message;
};
/**
* A simple @a Condition based on the numeric value of one @a Message.
*/
class SimpleNumericCondition : public SimpleCondition
{
class SimpleNumericCondition : public SimpleCondition {
public:
/**
* Construct a new instance.
* @param condName the name of the condition.
@@ -933,26 +919,23 @@ public:
*/
virtual ~SimpleNumericCondition() {}
protected:
protected:
// @copydoc
virtual bool checkValue(Message* message, const string field);
private:
private:
/** the valid value ranges (pairs of from/to inclusive), empty for @a m_message seen check. */
const vector<unsigned int> m_valueRanges;
};
/**
* A simple @a Condition based on the string value of one @a Message.
*/
class SimpleStringCondition : public SimpleCondition
{
class SimpleStringCondition : public SimpleCondition {
public:
/**
* Construct a new instance.
* @param condName the name of the condition.
@@ -975,26 +958,23 @@ public:
// @copydoc
virtual bool isNumeric() { return false; }
protected:
protected:
// @copydoc
virtual bool checkValue(Message* message, const string field);
private:
private:
/** the valid values. */
const vector<string> m_values;
};
/**
* A @a Condition combining two or more @a SimpleCondition instances with a logical and.
*/
class CombinedCondition : public Condition
{
class CombinedCondition : public Condition {
public:
/**
* Construct a new instance.
*/
@@ -1018,21 +998,18 @@ public:
// @copydoc
virtual bool isTrue();
private:
private:
/** the @a Condition instances used. */
vector<Condition*> m_conditions;
};
/**
* An abstract instruction based on the value of one or more @a Message instances.
*/
class Instruction
{
class Instruction {
public:
/**
* Construct a new instance.
* @param condition the @a Condition this instruction requires, or null.
@@ -1092,16 +1069,16 @@ public:
*/
virtual result_t execute(MessageMap* messages, ostringstream& log, Condition* condition) = 0;
private:
private:
/** the @a Condition this instruction requires, or null. */
Condition* m_condition;
/** whether this @a Instruction belongs to a set of instructions of which only the first one may be executed for the same source file. */
bool m_singleton;
protected:
protected:
/** the default destination address, or empty. */
const string m_defaultDest;
@@ -1110,17 +1087,14 @@ protected:
/** the default circuit name suffix (starting with a "."), or empty. */
const string m_defaultSuffix;
};
/**
* An @a Instruction allowing to load another file.
*/
class LoadInstruction : public Instruction
{
class LoadInstruction : public Instruction {
public:
/**
* Construct a new instance.
* @param condition the @a Condition this instruction requires, or null.
@@ -1141,28 +1115,24 @@ public:
// @copydoc
virtual result_t execute(MessageMap* messages, ostringstream& log, Condition* condition);
private:
private:
/** the name of the file to load. */
const string m_filename;
};
/**
* Holds a map of all known @a Message instances.
*/
class MessageMap : public FileReader
{
class MessageMap : public FileReader {
public:
/**
* Construct a new instance.
* @param addAll whether to add all messages, even if duplicate.
*/
MessageMap(const bool addAll = false) : FileReader::FileReader(true),
m_addAll(addAll), m_maxIdLength(0), m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0)
{
m_addAll(addAll), m_maxIdLength(0), m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) {
m_scanMessage = Message::createScanMessage();
}
@@ -1368,8 +1338,8 @@ public:
*/
void dump(ostream& output, bool withConditions = false);
private:
private:
/** whether to add all messages, even if duplicate. */
const bool m_addAll;
@@ -1405,7 +1375,6 @@ private:
/** the list of @a Instruction instances by filename. */
map<string, vector<Instruction*> > m_instructions;
};
#endif // LIB_EBUS_MESSAGE_H_
+2 -4
View File
@@ -28,8 +28,7 @@ using namespace std;
/**
* CRC8 lookup table for the polynom 0x9b = x^8 + x^7 + x^4 + x^3 + x^1 + 1.
*/
static const unsigned char CRC_LOOKUP_TABLE[] =
{
static const unsigned char CRC_LOOKUP_TABLE[] = {
0x00, 0x9b, 0xad, 0x36, 0xc1, 0x5a, 0x6c, 0xf7, 0x19, 0x82, 0xb4, 0x2f, 0xd8, 0x43, 0x75, 0xee,
0x32, 0xa9, 0x9f, 0x04, 0xf3, 0x68, 0x5e, 0xc5, 0x2b, 0xb0, 0x86, 0x1d, 0xea, 0x71, 0x47, 0xdc,
0x64, 0xff, 0xc9, 0x52, 0xa5, 0x3e, 0x08, 0x93, 0x7d, 0xe6, 0xd0, 0x4b, 0xbc, 0x27, 0x11, 0x8a,
@@ -194,8 +193,7 @@ void SymbolString::addCRC(const unsigned char value) {
* @return the 1-based index of the upper or lower 4 bits of a master address (1 to 5), or 0.
*/
unsigned char getMasterPartIndex(unsigned char bits) {
switch (bits)
{
switch (bits) {
case 0x0:
return 1;
case 0x1:
+2 -4
View File
@@ -75,9 +75,7 @@ static const unsigned char BROADCAST = 0xFE; //!< the broadcast destination addr
/**
* A string of escaped or unescaped bus symbols.
*/
class SymbolString
{
class SymbolString {
public:
/**
* Creates a new empty escaped or unescaped instance.
@@ -186,8 +184,8 @@ public:
*/
void clear(const bool escape) { m_data.clear(); m_unescapeState = escape ? 0 : 1; m_crc = 0; }
private:
private:
/**
* Hidden copy constructor.
* @param str the @a SymbolString to copy from.
-1
View File
@@ -418,5 +418,4 @@ int main() {
delete *it;
}
return 0;
}
+2 -6
View File
@@ -27,15 +27,12 @@
/**
* class to notify other thread per pipe.
*/
class Notify
{
class Notify {
public:
/**
* constructs a new instance and do notifying.
*/
Notify()
{
Notify() {
int pipefd[2];
int ret = pipe(pipefd);
@@ -70,7 +67,6 @@ private:
/** file descriptor to notify */
int m_sendfd;
};
#endif // LIB_UTILS_NOTIFY_H_
+10 -18
View File
@@ -33,15 +33,12 @@ using namespace std;
* @param T the item type.
*/
template <typename T>
class Queue
{
class Queue {
public:
/**
* Constructor.
*/
Queue()
{
Queue() {
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
}
@@ -49,28 +46,26 @@ public:
/**
* Destructor.
*/
~Queue()
{
~Queue() {
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
private:
private:
/**
* Hidden copy constructor.
* @param src the object to copy from.
*/
Queue(const Queue& src);
public:
public:
/**
* Add an item to the end of queue.
* @param item the item to add.
*/
void push(T item)
{
void push(T item) {
pthread_mutex_lock(&m_mutex);
m_queue.push_back(item);
pthread_cond_broadcast(&m_cond);
@@ -82,8 +77,7 @@ public:
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
* @return the item, or NULL if no item is available within the specified time.
*/
T pop(int timeout = 0)
{
T pop(int timeout = 0) {
T item;
pthread_mutex_lock(&m_mutex);
if (timeout > 0) {
@@ -112,8 +106,7 @@ public:
* @param wait true to wait for the item to appear in the queue.
* @return whether the item was removed.
*/
bool remove(T item, bool wait = false)
{
bool remove(T item, bool wait = false) {
bool ret = false;
pthread_mutex_lock(&m_mutex);
do {
@@ -135,8 +128,7 @@ public:
* Return the first item in the queue without removing it.
* @return the item, or NULL if no item is available.
*/
T peek()
{
T peek() {
T item;
pthread_mutex_lock(&m_mutex);
if (m_queue.empty()) {
@@ -148,6 +140,7 @@ public:
return item;
}
private:
/** the queue itself */
list<T> m_queue;
@@ -157,7 +150,6 @@ private:
/** condition variable for exclusive lock */
pthread_cond_t m_cond;
};
#endif // LIB_UTILS_QUEUE_H_
+1 -2
View File
@@ -64,8 +64,7 @@ void RotateFile::write(unsigned char* value, unsigned int size, bool received) {
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,
received ? '<' : '>'
);
received ? '<' : '>');
for (unsigned int pos = 0; pos < size; pos++) {
fprintf(m_stream, "%2.2x ", value[pos]);
}
+2 -3
View File
@@ -33,8 +33,7 @@ using namespace std;
/**
* Helper class for writing to a rotating file with maximum size.
*/
class RotateFile
{
class RotateFile {
public:
/**
* Construct a new instance.
@@ -71,6 +70,7 @@ public:
*/
void write(unsigned char* value, unsigned int size, bool received = true);
private:
/** whether writing to the file is enabled. */
bool m_enabled;
@@ -89,7 +89,6 @@ private:
/** the number of bytes already written to the @a m_file. */
unsigned long m_fileSize;
};
#endif // LIB_UTILS_ROTATEFILE_H_
+5 -12
View File
@@ -37,9 +37,7 @@ using namespace std;
/**
* class for low level tcp socket operations. (open, close, send, receive).
*/
class TCPSocket
{
class TCPSocket {
public:
/** grant access for friend class TCPClient */
friend class TCPClient;
@@ -92,6 +90,7 @@ public:
*/
bool isValid();
private:
/** file descriptor from tcp socket */
int m_sfd;
@@ -108,15 +107,12 @@ private:
* @param address struct which holds the ip address.
*/
TCPSocket(int sfd, struct sockaddr_in* address);
};
/**
* class to initiate a tcp socket connection to a listening server.
*/
class TCPClient
{
class TCPClient {
public:
/**
* initiate a tcp socket connection to a listening server.
@@ -125,15 +121,12 @@ public:
* @return pointer to an opened tcp socket.
*/
TCPSocket* connect(const string& server, const uint16_t& port);
};
/**
* class for a tcp based network server.
*/
class TCPServer
{
class TCPServer {
public:
/**
* creates a new instance of a listening tcp server.
@@ -166,6 +159,7 @@ public:
*/
int getFD() const { return m_lfd; }
private:
/** file descriptor from listening tcp socket */
int m_lfd;
@@ -178,7 +172,6 @@ private:
/** true if object is already listening */
bool m_listening;
};
#endif // LIB_UTILS_TCPSOCKET_H_
-9
View File
@@ -36,37 +36,28 @@ Thread::~Thread() {
}
bool Thread::start(const char* name) {
int result = pthread_create(&m_threadid, NULL, runThread, this);
if (result == 0) {
#ifdef HAVE_PTHREAD_SETNAME_NP
#ifndef __MACH__
pthread_setname_np(m_threadid, name);
#endif
#endif
m_started = true;
return true;
}
return false;
}
bool Thread::join() {
int result = -1;
if (m_started) {
m_stopped = true;
result = pthread_join(m_threadid, NULL);
if (result == 0) {
m_started = false;
}
}
return result == 0;
}
+5 -10
View File
@@ -26,9 +26,7 @@
/**
* wrapper class for pthread.
*/
class Thread
{
class Thread {
public:
/**
* constructor.
@@ -77,15 +75,15 @@ public:
*/
pthread_t self() { return m_threadid; }
protected:
protected:
/**
* Thread entry method to be overridden by derived class.
*/
virtual void run() = 0;
private:
private:
/**
* Enter the Thread loop by calling run().
*/
@@ -102,16 +100,13 @@ private:
/** Whether the thread was stopped by @a stop() or @a join(). */
bool m_stopped;
};
/**
* A @a Thread that can be waited on.
*/
class WaitThread : public Thread
{
class WaitThread : public Thread {
public:
/**
* Constructor.
@@ -136,13 +131,13 @@ public:
*/
bool Wait(int seconds);
private:
/** the mutex for waiting. */
pthread_mutex_t m_mutex;
/** the condition for waiting. */
pthread_cond_t m_cond;
};
#endif // LIB_UTILS_THREAD_H_
+2 -5
View File
@@ -36,8 +36,7 @@
using namespace std;
/** A structure holding all program options. */
struct options
{
struct options {
const char* server; //!< ebusd server host (name or ip) [localhost]
uint16_t port; //!< ebusd server port [8888]
@@ -229,7 +228,6 @@ string fetchData(TCPSocket* socket, bool& listening) {
}
void connect(const char* host, uint16_t port, char* const *args, int argCount) {
TCPClient* client = new TCPClient();
TCPSocket* socket = client->connect(host, port);
@@ -242,8 +240,7 @@ void connect(const char* host, uint16_t port, char* const *args, int argCount) {
if (!once) {
cout << host << ": ";
getline(cin, message);
}
else {
} else {
for (int i = 0; i < argCount; i++) {
if (i > 0) {
message += " ";
+3 -3
View File
@@ -32,8 +32,7 @@
using namespace std;
/** A structure holding all program options. */
struct options
{
struct options {
const char* device; //!< device to write to [/dev/ttyUSB60]
unsigned int time; //!< delay between bytes in us [10000]
@@ -111,8 +110,9 @@ error_t parse_opt(int key, char *arg, struct argp_state *state) {
return EINVAL;
}
opt->dumpFile = arg;
} else
} else {
return ARGP_ERR_UNKNOWN;
}
break;
default:
return ARGP_ERR_UNKNOWN;