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
|
||||
|
||||
@@ -11,7 +11,9 @@ libutils_a_SOURCES = log.cpp \
|
||||
clock.h \
|
||||
clock.cpp \
|
||||
queue.h \
|
||||
notify.h
|
||||
notify.h \
|
||||
rotatefile.h \
|
||||
rotatefile.cpp
|
||||
|
||||
distclean-local:
|
||||
-rm -f Makefile.in
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* ebusd - daemon for communication with eBUS heating systems.
|
||||
* Copyright (C) 2016 John Baier <ebusd@ebusd.eu>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include "rotatefile.h"
|
||||
#include "clock.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/file.h>
|
||||
#include <errno.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
RotateFile::~RotateFile()
|
||||
{
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool RotateFile::setEnabled(bool enabled)
|
||||
{
|
||||
if (enabled==m_enabled) {
|
||||
return false;
|
||||
}
|
||||
m_enabled = enabled;
|
||||
if (m_stream) {
|
||||
fclose(m_stream);
|
||||
m_stream = NULL;
|
||||
}
|
||||
if (enabled) {
|
||||
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
|
||||
m_fileSize = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void RotateFile::write(unsigned char* value, unsigned int size, bool received)
|
||||
{
|
||||
if (!m_enabled || !m_stream) {
|
||||
return;
|
||||
}
|
||||
if (m_textMode) {
|
||||
struct timespec ts;
|
||||
struct tm* tm;
|
||||
clockGettime(&ts);
|
||||
tm = localtime(&ts.tv_sec);
|
||||
char* buf;
|
||||
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 ? '<' : '>'
|
||||
);
|
||||
for (unsigned int pos=0; pos<size; pos++) {
|
||||
fprintf(m_stream, "%2.2x ", value[pos]);
|
||||
}
|
||||
fprintf(m_stream, "\n");
|
||||
m_fileSize += 25+3*size+1;
|
||||
} else {
|
||||
fwrite(value, (streamsize)size, 1, m_stream);
|
||||
m_fileSize += size;
|
||||
}
|
||||
if ((m_fileSize%1024) == 0) {
|
||||
fflush(m_stream);
|
||||
}
|
||||
if (m_fileSize >= m_maxSize * 1024) {
|
||||
string oldfile = string(m_fileName)+".old";
|
||||
if (rename(m_fileName.c_str(), oldfile.c_str()) == 0) {
|
||||
fclose(m_stream);
|
||||
m_stream = fopen(m_fileName.c_str(), m_textMode ? "w" : "wb");
|
||||
m_fileSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* ebusd - daemon for communication with eBUS heating systems.
|
||||
* Copyright (C) 2016 John Baier <ebusd@ebusd.eu>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBUTILS_ROTATEFILE_H_
|
||||
#define LIBUTILS_ROTATEFILE_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
/** @file rotatefile.h
|
||||
* Helpers for writing to rotating files.
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* Helper class for writing to a rotating file with maximum size.
|
||||
*/
|
||||
class RotateFile
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct a new instance.
|
||||
* @param fileName the name of the file write to.
|
||||
* @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 long maxSize, const bool textMode=false)
|
||||
: m_enabled(false), m_fileName(fileName), m_maxSize(maxSize), m_textMode(textMode), m_stream(), m_fileSize(0) {}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
virtual ~RotateFile();
|
||||
|
||||
/**
|
||||
* Enable or disable writing to the file.
|
||||
* @param enabled @p true to enable writing to the file, @p false to disable it.
|
||||
* @return @p true when the state was changed, @p false otherwise.
|
||||
*/
|
||||
bool setEnabled(bool enabled=true);
|
||||
|
||||
/**
|
||||
* Return whether writing to the file is enabled.
|
||||
* @return whether writing to the file is enabled.
|
||||
*/
|
||||
bool isEnabled() { return m_enabled; }
|
||||
|
||||
/**
|
||||
* Write a number of bytes to the stream.
|
||||
* @param value the pointer to the bytes to write.
|
||||
* @param size the number of bytes to write.
|
||||
* @param received @a true on reception, @a false on sending (only relevant in text mode).
|
||||
*/
|
||||
void write(unsigned char* value, unsigned int size, bool received=true);
|
||||
|
||||
private:
|
||||
/** whether writing to the file is enabled. */
|
||||
bool m_enabled;
|
||||
|
||||
/** the name of the file write to. */
|
||||
const string m_fileName;
|
||||
|
||||
/** the maximum size of @a m_file, or 0 for infinite. */
|
||||
const long m_maxSize;
|
||||
|
||||
/** whether to write each byte with prefixed timestamp and direction as text. */
|
||||
const bool m_textMode;
|
||||
|
||||
/** the @a FILE to writing to. */
|
||||
FILE* m_stream;
|
||||
|
||||
/** the number of bytes already written to the @a m_file. */
|
||||
unsigned long m_fileSize;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIBUTILS_ROTATEFILE_H_
|
||||
|
||||
Reference in New Issue
Block a user