added support for UDP devices, added buffer for network devices, added option for sending an initial symbol after opening a network device, added option for setting transfer latency and use default 10 ms for network device, set TCP_NODELAY for TCP device socket, flush potentially buffered input on network device when connecting

This commit is contained in:
john30
2016-05-05 12:39:52 +02:00
parent 516bbe0edf
commit d5b07f5783
9 changed files with 220 additions and 56 deletions
+76 -13
View File
@@ -19,6 +19,7 @@
#ifndef LIBEBUS_DEVICE_H_
#define LIBEBUS_DEVICE_H_
#include <unistd.h>
#include <termios.h>
#include <iostream>
#include <fstream>
@@ -48,12 +49,13 @@ public:
* Construct a new instance.
* @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network).
* @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 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,
Device(const char* name, const bool checkDevice, const bool readOnly, const bool initialSend,
void (*logRawFunc)(const unsigned char byte, bool received))
: m_name(name), m_checkDevice(checkDevice), m_readonly(readonly), m_fd(-1),
: 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) {}
@@ -66,14 +68,21 @@ public:
* Factory method for creating a new instance.
* @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network).
* @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 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,
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);
/**
* Get the transfer latency of this device.
* @return the transfer latency in microseconds.
*/
virtual unsigned int getLatency() const { return 0; }
/**
* Open the file descriptor.
* @return the @a result_t code.
@@ -154,6 +163,26 @@ protected:
*/
virtual void checkDevice() = 0; // abstract
/**
* Check whether a byte is available immediately (without waiting).
* @return true when a a byte is available immediately.
*/
virtual bool available() { return false; }
/**
* Write a single byte.
* @param value the byte value to write.
* @return the number of bytes written, or -1 on error.
*/
virtual ssize_t write(const unsigned char value) { return ::write(m_fd, &value, 1); }
/**
* Read a single byte.
* @param value the reference in which the read byte value is stored.
* @return the number of bytes read, or -1 on error.
*/
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;
@@ -162,7 +191,10 @@ protected:
const bool m_checkDevice;
/** whether to allow read access to the device only. */
const bool m_readonly;
const bool m_readOnly;
/** whether to send an initial @a ESC symbol in @a open(). */
const bool m_initialSend;
/** the opened file descriptor, or -1. */
int m_fd;
@@ -201,12 +233,13 @@ public:
* Construct a new instance.
* @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network).
* @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 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,
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, logRawFunc) {}
: Device(name, checkDevice, readOnly, initialSend, logRawFunc) {}
// @copydoc
virtual result_t open();
@@ -234,12 +267,18 @@ public:
* Construct a new instance.
* @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network).
* @param address the socket address of the device.
* @param readonly whether to allow read access to the device only.
* @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,
void (*logRawFunc)(const unsigned char byte, bool received))
: Device(name, true, readonly, logRawFunc), m_address(address) {}
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),
m_buffer(NULL), m_bufSize(0), m_bufLen(0), m_bufPos(0) {}
// @copydoc
virtual unsigned int getLatency() const { return 10000; }
// @copydoc
virtual result_t open();
@@ -248,10 +287,34 @@ protected:
// @copydoc
virtual void checkDevice();
// @copydoc
virtual bool available();
// @copydoc
virtual ssize_t write(const unsigned char value);
// @copydoc
virtual ssize_t read(unsigned char& value);
private:
/** the socket address of the device. */
const struct sockaddr_in m_address;
/** true for UDP, false to TCP. */
const bool m_udp;
/** the buffer memory, or NULL. */
unsigned char* m_buffer;
/** the buffer size. */
unsigned char m_bufSize;
/** the buffer fill length. */
unsigned char m_bufLen;
/** the buffer read position. */
unsigned char m_bufPos;
};
#endif // LIBEBUS_DEVICE_H_