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:
+36
-77
@@ -42,11 +42,9 @@ using namespace std;
|
||||
Device::~Device()
|
||||
{
|
||||
close();
|
||||
m_dumpRawStream.close();
|
||||
}
|
||||
|
||||
Device* Device::create(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend,
|
||||
void (*logRawFunc)(const unsigned char byte, bool received))
|
||||
Device* Device::create(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend)
|
||||
{
|
||||
if (strchr(name, '/') == NULL && strchr(name, ':') != NULL) {
|
||||
char* in = strdup(name);
|
||||
@@ -81,9 +79,9 @@ Device* Device::create(const char* name, const bool checkDevice, const bool read
|
||||
free(in);
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = (in_port_t)htons((uint16_t)port);
|
||||
return new NetworkDevice(name, address, readOnly, initialSend, logRawFunc, udp);
|
||||
return new NetworkDevice(name, address, readOnly, initialSend, udp);
|
||||
}
|
||||
return new SerialDevice(name, checkDevice, readOnly, initialSend, logRawFunc);
|
||||
return new SerialDevice(name, checkDevice, readOnly, initialSend);
|
||||
}
|
||||
|
||||
void Device::close()
|
||||
@@ -96,34 +94,34 @@ void Device::close()
|
||||
|
||||
bool Device::isValid()
|
||||
{
|
||||
if (m_fd == -1)
|
||||
if (m_fd == -1) {
|
||||
return false;
|
||||
|
||||
if (m_checkDevice)
|
||||
}
|
||||
if (m_checkDevice) {
|
||||
checkDevice();
|
||||
|
||||
}
|
||||
return m_fd != -1;
|
||||
}
|
||||
|
||||
result_t Device::send(const unsigned char value)
|
||||
{
|
||||
if (!isValid())
|
||||
if (!isValid()) {
|
||||
return RESULT_ERR_DEVICE;
|
||||
|
||||
if (m_readOnly || write(value) != 1)
|
||||
}
|
||||
if (m_readOnly || write(value) != 1) {
|
||||
return RESULT_ERR_SEND;
|
||||
|
||||
if (m_logRaw && m_logRawFunc != NULL)
|
||||
(*m_logRawFunc)(value, false);
|
||||
|
||||
}
|
||||
if (m_listener != NULL) {
|
||||
m_listener->notifyDeviceData(value, false);
|
||||
}
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
result_t Device::recv(const long timeout, unsigned char& value)
|
||||
{
|
||||
if (!isValid())
|
||||
if (!isValid()) {
|
||||
return RESULT_ERR_DEVICE;
|
||||
|
||||
}
|
||||
if (!available() && timeout > 0) {
|
||||
int ret;
|
||||
struct timespec tdiff;
|
||||
@@ -154,80 +152,42 @@ result_t Device::recv(const long timeout, unsigned char& value)
|
||||
ret = 1; // ignore timeout if neither ppoll nor pselect are available
|
||||
#endif
|
||||
#endif
|
||||
if (ret == -1) return RESULT_ERR_DEVICE;
|
||||
if (ret == 0) return RESULT_ERR_TIMEOUT;
|
||||
if (ret == -1) {
|
||||
return RESULT_ERR_DEVICE;
|
||||
}
|
||||
if (ret == 0) {
|
||||
return RESULT_ERR_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
// directly read byte from device
|
||||
ssize_t nbytes = read(value);
|
||||
if (nbytes == 0)
|
||||
if (nbytes == 0) {
|
||||
return RESULT_ERR_EOF;
|
||||
if (nbytes < 0)
|
||||
}
|
||||
if (nbytes < 0) {
|
||||
return RESULT_ERR_DEVICE;
|
||||
|
||||
if (m_logRaw && m_logRawFunc != NULL)
|
||||
(*m_logRawFunc)(value, true);
|
||||
|
||||
if (m_dumpRaw && m_dumpRawStream.is_open()) {
|
||||
m_dumpRawStream.write((char*)&value, 1);
|
||||
m_dumpRawFileSize++;
|
||||
if ((m_dumpRawFileSize%1024) == 0)
|
||||
m_dumpRawStream.flush();
|
||||
|
||||
if (m_dumpRawFileSize >= m_dumpRawMaxSize * 1024) {
|
||||
string oldfile = string(m_dumpRawFile) + ".old";
|
||||
if (rename(m_dumpRawFile, oldfile.c_str()) == 0) {
|
||||
m_dumpRawStream.close();
|
||||
m_dumpRawStream.open(m_dumpRawFile, ios::out | ios::binary | ios::app);
|
||||
m_dumpRawFileSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_listener != NULL) {
|
||||
m_listener->notifyDeviceData(value, true);
|
||||
}
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
void Device::setDumpRaw(bool dumpRaw)
|
||||
{
|
||||
if (dumpRaw == m_dumpRaw)
|
||||
return;
|
||||
|
||||
m_dumpRaw = dumpRaw;
|
||||
|
||||
if (!dumpRaw || m_dumpRawFile == NULL)
|
||||
m_dumpRawStream.close();
|
||||
else {
|
||||
m_dumpRawStream.open(m_dumpRawFile, ios::out | ios::binary | ios::app);
|
||||
m_dumpRawFileSize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Device::setDumpRawFile(const char* dumpFile) {
|
||||
if ((dumpFile == NULL) ? (m_dumpRawFile == NULL) : (m_dumpRawFile != NULL && (m_dumpRawFile == dumpFile || strcmp(dumpFile, m_dumpRawFile) == 0)))
|
||||
return;
|
||||
|
||||
m_dumpRawStream.close();
|
||||
m_dumpRawFile = dumpFile;
|
||||
|
||||
if (m_dumpRaw && m_dumpRawFile != NULL) {
|
||||
m_dumpRawStream.open(m_dumpRawFile, ios::out | ios::binary | ios::app);
|
||||
m_dumpRawFileSize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
result_t SerialDevice::open()
|
||||
{
|
||||
if (m_fd != -1)
|
||||
if (m_fd != -1) {
|
||||
close();
|
||||
|
||||
}
|
||||
struct termios newSettings;
|
||||
|
||||
// open file descriptor
|
||||
m_fd = ::open(m_name, O_RDWR | O_NOCTTY);
|
||||
|
||||
if (m_fd < 0)
|
||||
if (m_fd < 0) {
|
||||
return RESULT_ERR_NOTFOUND;
|
||||
}
|
||||
if (isatty(m_fd) == 0) {
|
||||
close();
|
||||
return RESULT_ERR_NOTFOUND;
|
||||
@@ -283,7 +243,6 @@ void SerialDevice::close()
|
||||
void SerialDevice::checkDevice()
|
||||
{
|
||||
int port;
|
||||
|
||||
if (ioctl(m_fd, TIOCMGET, &port) == -1) {
|
||||
close();
|
||||
}
|
||||
@@ -292,13 +251,13 @@ void SerialDevice::checkDevice()
|
||||
|
||||
result_t NetworkDevice::open()
|
||||
{
|
||||
if (m_fd != -1)
|
||||
if (m_fd != -1) {
|
||||
close();
|
||||
|
||||
}
|
||||
m_fd = socket(AF_INET, m_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
|
||||
if (m_fd < 0)
|
||||
if (m_fd < 0) {
|
||||
return RESULT_ERR_GENERIC_IO;
|
||||
|
||||
}
|
||||
int ret;
|
||||
if (m_udp) {
|
||||
struct sockaddr_in address = m_address;
|
||||
|
||||
+37
-70
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user