added --lograwdatafile and --lograwdatasize options similar to --dumpfile/--dumpsize for logging received/sent bytes to a dedicated text file, extracted dump/raw file handling to a separate class, switched to an interface instead of a method for dumping/logging raw data, changed default dump file name to /tmp/ebusd_dump.bin, use "s_" for static variables, formatting

This commit is contained in:
john30
2016-12-04 17:21:47 +01:00
parent 14fd6c9a13
commit cdbcb4664d
9 changed files with 416 additions and 224 deletions
+37 -70
View File
@@ -38,6 +38,28 @@
using namespace std;
/**
* Interface for listening to data received on/sent to a device.
*/
class DeviceListener
{
public:
/**
* Destructor.
*/
virtual ~DeviceListener() {}
/**
* Listener method that is called when a data byte was received/sent.
* @param byte the data byte received/sent.
* @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.
*/
@@ -51,13 +73,10 @@ public:
* @param checkDevice whether to regularly check the device availability (only for serial devices).
* @param readOnly whether to allow read access to the device only.
* @param initialSend whether to send an initial @a ESC symbol in @a open().
* @param logRawFunc the function to call for logging raw data, or NULL.
*/
Device(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend,
void (*logRawFunc)(const unsigned char byte, bool received))
Device(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend)
: m_name(name), m_checkDevice(checkDevice), m_readOnly(readOnly), m_initialSend(initialSend), m_fd(-1),
m_logRaw(false), m_logRawFunc(logRawFunc),
m_dumpRaw(false), m_dumpRawFile(NULL), m_dumpRawMaxSize(0), m_dumpRawStream(), m_dumpRawFileSize(0) {}
m_listener(NULL) {}
/**
* Destructor.
@@ -70,12 +89,10 @@ public:
* @param checkDevice whether to regularly check the device availability (only for serial devices).
* @param readOnly whether to allow read access to the device only.
* @param initialSend whether to send an initial @a ESC symbol in @a open().
* @param logRawFunc the function to call for logging raw data, or NULL.
* @return the new @a Device, or NULL on error.
* Note: the caller needs to free the created instance.
*/
static Device* create(const char* name, const bool checkDevice=true, const bool readOnly=false, const bool initialSend=false,
void (*logRawFunc)(const unsigned char byte, bool received)=NULL);
static Device* create(const char* name, const bool checkDevice=true, const bool readOnly=false, const bool initialSend=false);
/**
* Get the transfer latency of this device.
@@ -109,42 +126,6 @@ public:
*/
result_t recv(const long timeout, unsigned char& value);
/**
* Get whether logging of raw data is enabled.
* @return whether logging of raw data is enabled.
*/
bool getLogRaw() { return m_logRaw; }
/**
* Enable or disable logging of raw data.
* @param logRaw true to enable logging of raw data, false to disable it.
*/
void setLogRaw(bool logRaw=true) { m_logRaw = logRaw; }
/**
* Get whether dumping of raw data to a file is enabled.
* @return whether dumping of raw data to a file is enabled.
*/
bool getDumpRaw() { return m_dumpRaw; }
/**
* Enable or disable dumping of raw data to a file.
* @param dumpRaw true to enable dumping of raw data to a file, false to disable it.
*/
void setDumpRaw(bool dumpRaw=true);
/**
* Set the name of the file to dump raw data to.
* @param dumpFile the name of the file to dump raw data to.
*/
void setDumpRawFile(const char* dumpFile);
/**
* Set the maximum size of a file to dump raw data to.
* @param maxSize the maximum size of a file to dump raw data to.
*/
void setDumpRawMaxSize(const long maxSize) { m_dumpRawMaxSize = maxSize; }
/**
* Return the device name.
* @return the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network).
@@ -163,6 +144,12 @@ public:
*/
bool isReadOnly() const { return m_readOnly; }
/**
* Set the @a DeviceListener.
* @param listener the @a DeviceListener.
*/
void setListener(DeviceListener* listener) { m_listener = listener; }
protected:
/**
* Check if the device is still available and close it if not.
@@ -206,26 +193,9 @@ protected:
int m_fd;
private:
/** whether logging of raw data is enabled. */
bool m_logRaw;
/** the function to call for logging raw data, or NULL. */
void (*m_logRawFunc)(const unsigned char byte, bool received);
/** whether dumping of raw data to a file is enabled. */
bool m_dumpRaw;
/** the name of the file to dump raw data to. */
const char* m_dumpRawFile;
/** the maximum size of @a m_dumpFile, or 0 for infinite. */
long m_dumpRawMaxSize;
/** the @a ofstream for dumping raw data to. */
ofstream m_dumpRawStream;
/** the number of bytes already written to the @a m_dumpFile. */
long m_dumpRawFileSize;
/** the @a DeviceListener, or NULL. */
DeviceListener* m_listener;
};
@@ -241,11 +211,9 @@ public:
* @param checkDevice whether to regularly check the device availability (only for serial devices).
* @param readOnly whether to allow read access to the device only.
* @param initialSend whether to send an initial @a ESC symbol in @a open().
* @param logRawFunc the function to call for logging raw data, or NULL.
*/
SerialDevice(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend,
void (*logRawFunc)(const unsigned char byte, bool received))
: Device(name, checkDevice, readOnly, initialSend, logRawFunc) {}
SerialDevice(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend)
: Device(name, checkDevice, readOnly, initialSend) {}
// @copydoc
virtual result_t open();
@@ -275,12 +243,11 @@ public:
* @param address the socket address of the device.
* @param readOnly whether to allow read access to the device only.
* @param initialSend whether to send an initial @a ESC symbol in @a open().
* @param logRawFunc the function to call for logging raw data, or NULL.
* @param udp true for UDP, false to TCP.
*/
NetworkDevice(const char* name, const struct sockaddr_in address, const bool readOnly, const bool initialSend,
void (*logRawFunc)(const unsigned char byte, bool received), const bool udp)
: Device(name, true, readOnly, initialSend, logRawFunc), m_address(address), m_udp(udp),
const bool udp)
: Device(name, true, readOnly, initialSend), m_address(address), m_udp(udp),
m_buffer(NULL), m_bufSize(0), m_bufLen(0), m_bufPos(0) {}
// @copydoc