allow enh: prefix for serial device as well

This commit is contained in:
john
2018-09-16 16:32:04 +02:00
parent 888e26263b
commit 7696cb822b
2 changed files with 15 additions and 10 deletions
+10 -6
View File
@@ -64,7 +64,7 @@ namespace ebusd {
* @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 initialSend whether to send an initial @a ESC symbol in @a open().
* @param udp true for UDP, false to TCP. * @param udp true for UDP, false to TCP.
* @param enhancedProto whether the device supports the ebusd enhanced protocol. * @param enhancedProto whether to use the ebusd enhanced protocol.
*/ */
Device::Device(const char* name, bool checkDevice, bool readOnly, bool initialSend, bool enhancedProto) Device::Device(const char* name, bool checkDevice, bool readOnly, bool initialSend, bool enhancedProto)
: m_name(name), m_checkDevice(checkDevice), m_readOnly(readOnly), m_initialSend(initialSend), : m_name(name), m_checkDevice(checkDevice), m_readOnly(readOnly), m_initialSend(initialSend),
@@ -84,20 +84,23 @@ Device::~Device() {
} }
Device* Device::create(const char* name, bool checkDevice, bool readOnly, bool initialSend) { Device* Device::create(const char* name, bool checkDevice, bool readOnly, bool initialSend) {
bool enhanced = strncmp(name, "enh:", 4) == 0;
if (enhanced) {
name += 4;
}
if (strchr(name, '/') == nullptr && strchr(name, ':') != nullptr) { if (strchr(name, '/') == nullptr && strchr(name, ':') != nullptr) {
char* in = strdup(name); char* in = strdup(name);
bool udp = false; bool udp = false;
bool enhanced = false;
char* addrpos = in; char* addrpos = in;
char* portpos = strchr(addrpos, ':'); char* portpos = strchr(addrpos, ':');
if (portpos >= addrpos+3 && strncmp(addrpos, "enh", 3) == 0) { if (!enhanced && portpos >= addrpos+3 && strncmp(addrpos, "enh", 3) == 0) {
enhanced = true; enhanced = true; // support enhtcp:<ip>:<port> and enhudp:<ip>:<port>
addrpos += 3; addrpos += 3;
if (portpos == addrpos) { if (portpos == addrpos) {
addrpos++; addrpos++;
portpos = strchr(addrpos, ':'); portpos = strchr(addrpos, ':');
} }
} } // else: support enh:<ip>:<port> defaulting to TCP
if (portpos == addrpos+3 && (strncmp(addrpos, "tcp", 3) == 0 || (udp=(strncmp(addrpos, "udp", 3) == 0)))) { if (portpos == addrpos+3 && (strncmp(addrpos, "tcp", 3) == 0 || (udp=(strncmp(addrpos, "udp", 3) == 0)))) {
addrpos += 4; addrpos += 4;
portpos = strchr(addrpos, ':'); portpos = strchr(addrpos, ':');
@@ -128,7 +131,8 @@ Device* Device::create(const char* name, bool checkDevice, bool readOnly, bool i
address.sin_port = (in_port_t)htons((uint16_t)port); address.sin_port = (in_port_t)htons((uint16_t)port);
return new NetworkDevice(name, address, readOnly, initialSend, udp, enhanced); return new NetworkDevice(name, address, readOnly, initialSend, udp, enhanced);
} }
return new SerialDevice(name, checkDevice, readOnly, initialSend); // support enh:/dev/<device>
return new SerialDevice(name, checkDevice, readOnly, initialSend, enhanced);
} }
result_t Device::open() { result_t Device::open() {
+5 -4
View File
@@ -79,7 +79,7 @@ class Device {
* @param checkDevice whether to regularly check the device availability (only for serial devices). * @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 initialSend whether to send an initial @a ESC symbol in @a open().
* @param enhancedProto whether the device supports the ebusd enhanced protocol. * @param enhancedProto whether to use the ebusd enhanced protocol.
*/ */
Device(const char* name, bool checkDevice, bool readOnly, bool initialSend, bool enhancedProto=false); Device(const char* name, bool checkDevice, bool readOnly, bool initialSend, bool enhancedProto=false);
@@ -251,9 +251,10 @@ class SerialDevice : public Device {
* @param checkDevice whether to regularly check the device availability (only for serial devices). * @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 initialSend whether to send an initial @a ESC symbol in @a open().
* @param enhancedProto whether to use the ebusd enhanced protocol.
*/ */
SerialDevice(const char* name, bool checkDevice, bool readOnly, bool initialSend) SerialDevice(const char* name, bool checkDevice, bool readOnly, bool initialSend, bool enhancedProto=false)
: Device(name, checkDevice, readOnly, initialSend) {} : Device(name, checkDevice, readOnly, initialSend, enhancedProto) {}
// @copydoc // @copydoc
result_t open() override; result_t open() override;
@@ -284,7 +285,7 @@ class NetworkDevice : public 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 initialSend whether to send an initial @a ESC symbol in @a open().
* @param udp true for UDP, false to TCP. * @param udp true for UDP, false to TCP.
* @param enhancedProto whether the device supports the ebusd enhanced protocol. * @param enhancedProto whether to use the ebusd enhanced protocol.
*/ */
NetworkDevice(const char* name, const struct sockaddr_in& address, bool readOnly, bool initialSend, NetworkDevice(const char* name, const struct sockaddr_in& address, bool readOnly, bool initialSend,
bool udp, bool enhancedProto=false) bool udp, bool enhancedProto=false)