diff --git a/src/lib/ebus/device.cpp b/src/lib/ebus/device.cpp index 13a2889c..da3ee80c 100755 --- a/src/lib/ebus/device.cpp +++ b/src/lib/ebus/device.cpp @@ -102,50 +102,6 @@ Device::Device(const char* name) : m_name(name), m_listener(nullptr) { } -Device* Device::create(const char* name, unsigned int extraLatency, bool checkDevice) { - EnhancedLevel enhanced = el_none; - if (strncmp(name, "en", 2) == 0 && name[2] && name[3] == ':') { - switch (name[2]) { - case 's': - enhanced = el_speed; - break; - case 'h': - enhanced = el_basic; - break; - } - if (enhanced) { - name += 4; - } - } - if (strchr(name, '/') == nullptr && strchr(name, ':') != nullptr) { - char* in = strdup(name); - bool udp = false; - char* addrpos = in; - char* portpos = strchr(addrpos, ':'); - // support tcp:: and udp:: - if (portpos == addrpos+3 && (strncmp(addrpos, "tcp", 3) == 0 || (udp=(strncmp(addrpos, "udp", 3) == 0)))) { - addrpos += 4; - portpos = strchr(addrpos, ':'); - } - if (portpos == nullptr) { - free(in); - return nullptr; // invalid protocol or missing port - } - result_t result = RESULT_OK; - uint16_t port = (uint16_t)parseInt(portpos+1, 10, 1, 65535, &result); - if (result != RESULT_OK) { - free(in); - return nullptr; // invalid port - } - *portpos = 0; - char* hostOrIp = strdup(addrpos); - free(in); - return new NetworkDevice(name, hostOrIp, port, extraLatency, udp, enhanced); - } - // support enh:/dev/, ens:/dev/, and /dev/ - return new SerialDevice(name, checkDevice, extraLatency, enhanced); -} - FileDevice::FileDevice(const char* name, bool checkDevice, unsigned int latency, diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index 238297b7..60bfa53b 100755 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -108,16 +108,6 @@ class Device { */ virtual ~Device() { } - /** - * 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 extraLatency the extra bus transfer latency in milliseconds. - * @param checkDevice whether to regularly check the device availability (only for serial devices). - * @return the new @a Device, or nullptr on error. - * Note: the caller needs to free the created instance. - */ - static Device* create(const char* name, unsigned int extraLatency = 0, bool checkDevice = true); - /** * Get the device name. * @return the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). diff --git a/src/lib/ebus/protocol.cpp b/src/lib/ebus/protocol.cpp index 7eb657d8..fa31bd4c 100644 --- a/src/lib/ebus/protocol.cpp +++ b/src/lib/ebus/protocol.cpp @@ -44,9 +44,49 @@ bool ActiveBusRequest::notify(result_t result, const SlaveSymbolString& slave) { ProtocolHandler* ProtocolHandler::create(const ebus_protocol_config_t config, ProtocolListener* listener) { - Device *device = Device::create(config.device, config.extraLatency, !config.noDeviceCheck); - if (device == nullptr) { - return nullptr; + const char* name = config.device; + EnhancedLevel enhanced = el_none; + if (strncmp(name, "en", 2) == 0 && name[2] && name[3] == ':') { + switch (name[2]) { + case 's': + enhanced = el_speed; + break; + case 'h': + enhanced = el_basic; + break; + } + if (enhanced) { + name += 4; + } + } + FileDevice* device = nullptr; + if (strchr(name, '/') == nullptr && strchr(name, ':') != nullptr) { + char* in = strdup(name); + bool udp = false; + char* addrpos = in; + char* portpos = strchr(addrpos, ':'); + // support tcp:: and udp:: + if (portpos == addrpos+3 && (strncmp(addrpos, "tcp", 3) == 0 || (udp=(strncmp(addrpos, "udp", 3) == 0)))) { + addrpos += 4; + portpos = strchr(addrpos, ':'); + } + if (portpos == nullptr) { + free(in); + return nullptr; // invalid protocol or missing port + } + result_t result = RESULT_OK; + uint16_t port = (uint16_t)parseInt(portpos+1, 10, 1, 65535, &result); + if (result != RESULT_OK) { + free(in); + return nullptr; // invalid port + } + *portpos = 0; + char* hostOrIp = strdup(addrpos); + free(in); + device = new NetworkDevice(name, hostOrIp, port, config.extraLatency, udp, enhanced); + } else { + // support enx:/dev/, ens:/dev/, enh:/dev/, and /dev/ + device = new SerialDevice(name, !config.noDeviceCheck, config.extraLatency, enhanced); } return new DirectProtocolHandler(config, device, listener); }