use result_t, move raw logging and dumping to Port and avoid permanent open/close on ofstream

This commit is contained in:
john30
2014-11-29 20:10:57 +01:00
parent bd4d36b27f
commit 29f60af55c
3 changed files with 140 additions and 22 deletions
Regular → Executable
+2 -1
View File
@@ -1,6 +1,7 @@
AM_CXXFLAGS = -fpic \
-Wall \
-Wextra
-Wextra \
-I$(top_srcdir)/src/lib/utils
noinst_LIBRARIES = libebus.a
+70 -12
View File
@@ -22,12 +22,15 @@
#endif
#include "port.h"
#include "result.h"
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <fstream>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "logger.h"
#ifdef HAVE_PPOLL
#include <poll.h>
@@ -101,8 +104,8 @@ ssize_t Device::recvBytes(const long timeout, size_t maxCount)
#endif
#endif
if (ret == -1) return -1; // TODO RESULT_ERR_DEVICE
if (ret == 0) return -2; // TODO RESULT_ERR_TIMEOUT
if (ret == -1) return RESULT_ERR_DEVICE;
if (ret == 0) return RESULT_ERR_TIMEOUT;
}
if (maxCount > sizeof(m_buffer))
@@ -132,7 +135,7 @@ unsigned char Device::getByte()
}
void DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck)
result_t DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck)
{
m_noDeviceCheck = noDeviceCheck;
@@ -144,7 +147,7 @@ void DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck)
m_fd = open(deviceName.c_str(), O_RDWR | O_NOCTTY);
if (m_fd < 0)
return;
return RESULT_ERR_FILENOTFOUND;
// save current settings of serial device
tcgetattr(m_fd, &m_oldSettings);
@@ -169,7 +172,7 @@ void DeviceSerial::openDevice(const string deviceName, const bool noDeviceCheck)
fcntl(m_fd, F_SETFL, fcntl(m_fd, F_GETFL) & ~O_NONBLOCK);
m_open = true;
return RESULT_OK;
}
void DeviceSerial::closeDevice()
@@ -190,7 +193,7 @@ void DeviceSerial::closeDevice()
}
void DeviceNetwork::openDevice(const string deviceName, const bool noDeviceCheck)
result_t DeviceNetwork::openDevice(const string deviceName, const bool noDeviceCheck)
{
m_noDeviceCheck = noDeviceCheck;
@@ -211,13 +214,13 @@ void DeviceNetwork::openDevice(const string deviceName, const bool noDeviceCheck
he = gethostbyname(host);
if (he == NULL)
return;
return RESULT_ERR_FILENOTFOUND;
memcpy(&sock.sin_addr, he->h_addr_list[0], he->h_length);
} else {
ret = inet_aton(host, &sock.sin_addr);
if (ret == 0)
return;
return RESULT_ERR_FILENOTFOUND;
}
sock.sin_family = AF_INET;
@@ -225,14 +228,16 @@ void DeviceNetwork::openDevice(const string deviceName, const bool noDeviceCheck
m_fd = socket(AF_INET, SOCK_STREAM, 0);
if (m_fd < 0)
return;
return RESULT_ERR_INVALID_ARG;
ret = connect(m_fd, (struct sockaddr*) &sock, sizeof(sock));
if (ret < 0)
return;
return RESULT_ERR_INVALID_ARG;
free(hostport);
m_open = true;
return RESULT_OK;
}
void DeviceNetwork::closeDevice()
@@ -247,8 +252,11 @@ void DeviceNetwork::closeDevice()
}
Port::Port(const string deviceName, const bool noDeviceCheck)
: m_deviceName(deviceName), m_noDeviceCheck(noDeviceCheck)
Port::Port(const string deviceName, const bool noDeviceCheck, const bool logRaw, Logger* loggerRaw,
const bool dumpRaw, const char* dumpRawFile, const long dumpRawMaxSize)
: m_deviceName(deviceName), m_noDeviceCheck(noDeviceCheck),
m_logRaw(logRaw), m_loggerRaw(loggerRaw),
m_dumpRawFile(dumpRawFile), m_dumpRawMaxSize(dumpRawMaxSize)
{
m_device = NULL;
@@ -257,6 +265,56 @@ Port::Port(const string deviceName, const bool noDeviceCheck)
setType(dt_network);
else
setType(dt_serial);
m_dumpRaw = false;
setDumpRaw(dumpRaw); // open fstream if necessary
}
unsigned char Port::byte()
{
unsigned char byte = m_device->getByte();
if (m_logRaw == true && m_loggerRaw != NULL)
m_loggerRaw->log(bus, event, "%02x", byte);
if (m_dumpRaw == true && m_dumpRawStream.is_open() == true) {
m_dumpRawStream.write((char*)&byte, 1);
if (m_dumpRawStream.tellp() >= m_dumpRawMaxSize * 1024) {
string oldfile = m_dumpRawFile + ".old";
if (rename(m_dumpRawFile.c_str(), oldfile.c_str()) == 0) {
m_dumpRawStream.close();
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
}
}
}
return byte;
}
void Port::setDumpRaw(bool dumpRaw)
{
if (dumpRaw == m_dumpRaw)
return;
m_dumpRaw = dumpRaw;
if (dumpRaw == false)
m_dumpRawStream.close();
else
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
}
void Port::setDumpRawFile(const string& dumpFile) {
if (dumpFile == m_dumpRawFile)
return;
m_dumpRawStream.close();
m_dumpRawFile = dumpFile;
if (m_dumpRaw == true)
m_dumpRawStream.open(m_dumpRawFile.c_str(), ios::out | ios::binary | ios::app);
}
void Port::setType(const DeviceType type)
+68 -9
View File
@@ -24,6 +24,10 @@
#include <queue>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include "logger.h"
#include "result.h"
using namespace std;
@@ -64,7 +68,7 @@ public:
* @param deviceName to determine device type.
* @param noDeviceCheck en-/disable device check.
*/
virtual void openDevice(const string deviceName, const bool noDeviceCheck) = 0;
virtual result_t openDevice(const string deviceName, const bool noDeviceCheck) = 0;
/**
* @brief virtual close function for closing opened file descriptor
@@ -147,7 +151,7 @@ public:
* @param deviceName to determine device type.
* @param noDeviceCheck en-/disable device check.
*/
void openDevice(const string deviceName, const bool noDeviceCheck);
virtual result_t openDevice(const string deviceName, const bool noDeviceCheck);
/**
* @brief close function for closing opened file descriptor
@@ -177,7 +181,7 @@ public:
* @param deviceName to determine device type.
* @param noDeviceCheck en-/disable device check.
*/
void openDevice(const string deviceName, const bool noDeviceCheck);
virtual result_t openDevice(const string deviceName, const bool noDeviceCheck);
/**
* @brief close opened file descriptor
@@ -200,17 +204,18 @@ public:
* @param deviceName to determine device type.
* @param noDeviceCheck en-/disable device check.
*/
Port(const string deviceName, const bool noDeviceCheck);
Port(const string deviceName, const bool noDeviceCheck, const bool logRaw, Logger* loggerRaw,
const bool dumpRaw, const char* dumpRawFile, const long dumpRawMaxSize);
/**
* @brief destructor.
*/
~Port() { delete m_device; }
~Port() { delete m_device; m_dumpRawStream.close(); }
/**
* @brief open device
*/
void open() { m_device->openDevice(m_deviceName, m_noDeviceCheck); }
result_t open() { return m_device->openDevice(m_deviceName, m_noDeviceCheck); }
/**
* @brief close device
@@ -234,7 +239,7 @@ public:
/**
* @brief recv read bytes from opened file descriptor.
* @param timeout max time out for new input data.
* @param timeout max time out for new input data [usec].
* @param maxCount max size of receive buffer.
* @return number of read bytes or -1 if an error has occured.
*/
@@ -245,7 +250,7 @@ public:
* @brief fetch first byte from receive buffer.
* @return first byte (raw)
*/
unsigned char byte() { return m_device->getByte(); }
unsigned char byte();
/**
* @brief get current size (bytes) of the receive buffer.
@@ -253,9 +258,45 @@ public:
*/
ssize_t size() const { return m_device->sizeRecvBuffer(); }
/**
* @brief Get whether logging of raw data is enabled.
* @return whether logging of raw data is enabled.
*/
bool getLogRaw() { return m_logRaw; }
/**
* @brief Enable or disable logging of raw data.
* @param logRawData true to enable logging of raw data, false to disable it.
*/
void setLogRaw(bool logRaw=true) { m_logRaw = logRaw; }
/**
* @brief 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; }
/**
* @brief 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);
/**
* @brief 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 string& dumpFile);
/**
* @brief 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; }
private:
/** the device name */
string m_deviceName;
const string m_deviceName;
/** the device instance */
Device* m_device;
@@ -263,6 +304,24 @@ private:
/** true if device check is disabled */
bool m_noDeviceCheck;
/** whether logging of raw data is enabled. */
bool m_logRaw;
/** the @a Logger used for logging of raw data, or NULL. */
Logger* m_loggerRaw;
/** whether dumping of raw data to a file is enabled. */
bool m_dumpRaw;
/** the name of the file to dump raw data to. */
string m_dumpRawFile;
/** the maximum size of @a m_dumpFile. */
long m_dumpRawMaxSize;
/** the @a ofstream for dumping raw data to. */
ofstream m_dumpRawStream;
/**
* @brief internal setter for device type.
* @param type of device