From 605bca9ff0606d92bc2723be35b3d4fb2728d525 Mon Sep 17 00:00:00 2001 From: john30 Date: Fri, 1 May 2015 08:35:02 +0200 Subject: [PATCH] introduced read-only mode --- src/lib/ebus/device.cpp | 8 ++++---- src/lib/ebus/device.h | 19 ++++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/lib/ebus/device.cpp b/src/lib/ebus/device.cpp index 4f3ccd9f..771b2e42 100644 --- a/src/lib/ebus/device.cpp +++ b/src/lib/ebus/device.cpp @@ -42,7 +42,7 @@ Device::~Device() m_dumpRawStream.close(); } -Device* Device::create(const char* name, const bool checkDevice, +Device* Device::create(const char* name, const bool checkDevice, const bool readonly, void (*logRawFunc)(const unsigned char byte, bool received)) { if (strchr(name, '/') == NULL) { @@ -68,10 +68,10 @@ Device* Device::create(const char* name, const bool checkDevice, free(host); address.sin_family = AF_INET; address.sin_port = htons((uint16_t)port); - return new NetworkDevice(name, address, logRawFunc); + return new NetworkDevice(name, address, readonly, logRawFunc); } } - return new SerialDevice(name, checkDevice, logRawFunc); + return new SerialDevice(name, checkDevice, readonly, logRawFunc); } void Device::close() @@ -98,7 +98,7 @@ result_t Device::send(const unsigned char value) if (!isValid()) return RESULT_ERR_DEVICE; - if (write(m_fd, &value, 1) != 1) + if (m_readonly || write(m_fd, &value, 1) != 1) return RESULT_ERR_SEND; if (m_logRaw && m_logRawFunc != NULL) diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index 3c2c7e29..94d1139d 100644 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -42,11 +42,12 @@ 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 logRawFunc the function to call for logging raw data, or NULL. */ - Device(const char* name, const bool checkDevice, + Device(const char* name, const bool checkDevice, const bool readonly, void (*logRawFunc)(const unsigned char byte, bool received)) - : m_name(name), m_checkDevice(checkDevice), m_fd(-1), + : m_name(name), m_checkDevice(checkDevice), m_readonly(readonly), m_fd(-1), m_logRaw(false), m_logRawFunc(logRawFunc), m_dumpRaw(false), m_dumpRawFile(NULL), m_dumpRawMaxSize(0), m_dumpRawStream(NULL), m_dumpRawFileSize(0) {} @@ -59,11 +60,12 @@ 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 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, + static Device* create(const char* name, const bool checkDevice=true, const bool readonly=false, void (*logRawFunc)(const unsigned char byte, bool received)=NULL); /** @@ -153,6 +155,9 @@ protected: /** whether to regularly check the device availability (only for serial devices). */ const bool m_checkDevice; + /** whether to allow read access to the device only. */ + const bool m_readonly; + /** the opened file descriptor, or -1. */ int m_fd; @@ -192,9 +197,9 @@ public: * @param checkDevice whether to regularly check the device availability (only for serial devices). * @param logRawFunc the function to call for logging raw data, or NULL. */ - SerialDevice(const char* name, const bool checkDevice, + SerialDevice(const char* name, const bool checkDevice, const bool readonly, void (*logRawFunc)(const unsigned char byte, bool received)) - : Device(name, checkDevice, logRawFunc) {} + : Device(name, checkDevice, readonly, logRawFunc) {} // @copydoc virtual result_t open(); @@ -224,9 +229,9 @@ public: * @param address the socket address of the device. * @param logRawFunc the function to call for logging raw data, or NULL. */ - NetworkDevice(const char* name, const struct sockaddr_in address, + NetworkDevice(const char* name, const struct sockaddr_in address, const bool readonly, void (*logRawFunc)(const unsigned char byte, bool received)) - : Device(name, true, logRawFunc), m_address(address) {} + : Device(name, true, readonly, logRawFunc), m_address(address) {} // @copydoc virtual result_t open();