From a86b3a23351511d46d40ca8cd47c7d5f08041bd4 Mon Sep 17 00:00:00 2001 From: redpanther Date: Wed, 21 Sep 2016 23:08:13 +0200 Subject: [PATCH] add ability for default values in providerudp --- libsrc/leddevice/LedDeviceTpm2net.cpp | 2 +- libsrc/leddevice/LedDeviceUdpRaw.cpp | 2 +- libsrc/leddevice/ProviderUdp.cpp | 17 +++++++++++++---- libsrc/leddevice/ProviderUdp.h | 2 +- libsrc/leddevice/schemas/schema-e131.json | 2 ++ libsrc/leddevice/schemas/schema-tpm2net.json | 14 +++++++++++--- libsrc/leddevice/schemas/schema-udpraw.json | 2 ++ 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/libsrc/leddevice/LedDeviceTpm2net.cpp b/libsrc/leddevice/LedDeviceTpm2net.cpp index 29c7a47b..7940dd0b 100644 --- a/libsrc/leddevice/LedDeviceTpm2net.cpp +++ b/libsrc/leddevice/LedDeviceTpm2net.cpp @@ -24,7 +24,7 @@ LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig) { - ProviderUdp::setConfig(deviceConfig); + ProviderUdp::setConfig(deviceConfig,50200); _LatchTime_ns = deviceConfig.get("latchtime",104000).asInt(); _tpm2_max = deviceConfig.get("max-packet",170).asInt(); return true; diff --git a/libsrc/leddevice/LedDeviceUdpRaw.cpp b/libsrc/leddevice/LedDeviceUdpRaw.cpp index 81cf892f..5dacf209 100644 --- a/libsrc/leddevice/LedDeviceUdpRaw.cpp +++ b/libsrc/leddevice/LedDeviceUdpRaw.cpp @@ -19,7 +19,7 @@ LedDeviceUdpRaw::LedDeviceUdpRaw(const Json::Value &deviceConfig) bool LedDeviceUdpRaw::setConfig(const Json::Value &deviceConfig) { - ProviderUdp::setConfig(deviceConfig); + ProviderUdp::setConfig(deviceConfig,5568); _LatchTime_ns = deviceConfig.get("latchtime",500000).asInt(); return true; diff --git a/libsrc/leddevice/ProviderUdp.cpp b/libsrc/leddevice/ProviderUdp.cpp index 03ac61f0..59a1c0c2 100644 --- a/libsrc/leddevice/ProviderUdp.cpp +++ b/libsrc/leddevice/ProviderUdp.cpp @@ -18,6 +18,7 @@ ProviderUdp::ProviderUdp(const Json::Value &deviceConfig) : LedDevice() , _LatchTime_ns(-1) + , _port(0) { setConfig(deviceConfig); _udpSocket = new QUdpSocket(); @@ -28,16 +29,18 @@ ProviderUdp::~ProviderUdp() _udpSocket->close(); } -bool ProviderUdp::setConfig(const Json::Value &deviceConfig) +bool ProviderUdp::setConfig(const Json::Value &deviceConfig, int defaultPort, std::string defaultHost) { - if (_address.setAddress( QString::fromStdString(deviceConfig["host"].asString()) ) ) + QString host = QString::fromStdString(deviceConfig.get("host",defaultHost).asString()); + + if (_address.setAddress(host) ) { Debug( _log, "Successfully parsed %s as an ip address.", deviceConfig["host"].asString().c_str()); } else { Debug( _log, "Failed to parse %s as an ip address.", deviceConfig["host"].asString().c_str()); - QHostInfo info = QHostInfo::fromName( QString::fromStdString(deviceConfig["host"].asString()) ); + QHostInfo info = QHostInfo::fromName(host); if (info.addresses().isEmpty()) { Debug( _log, "Failed to parse %s as a hostname.", deviceConfig["host"].asString().c_str()); @@ -46,7 +49,13 @@ bool ProviderUdp::setConfig(const Json::Value &deviceConfig) Debug( _log, "Successfully parsed %s as a hostname.", deviceConfig["host"].asString().c_str()); _address = info.addresses().first(); } - _port = deviceConfig["port"].asUInt(); + + _port = deviceConfig.get("port", defaultPort).asUInt(); + if ( _port<=0 || _port > 65535) + { + throw std::runtime_error("invalid target port"); + } + Debug( _log, "UDP using %s:%d", _address.toString().toStdString().c_str() , _port ); return true; diff --git a/libsrc/leddevice/ProviderUdp.h b/libsrc/leddevice/ProviderUdp.h index b5f9ec37..e3b53877 100644 --- a/libsrc/leddevice/ProviderUdp.h +++ b/libsrc/leddevice/ProviderUdp.h @@ -29,7 +29,7 @@ public: /// /// @param deviceConfig the json device config /// @return true if success - bool setConfig(const Json::Value &deviceConfig); + bool setConfig(const Json::Value &deviceConfig, int defaultPort=0, std::string defaultHost="127.0.0.1"); /// /// Opens and configures the output device diff --git a/libsrc/leddevice/schemas/schema-e131.json b/libsrc/leddevice/schemas/schema-e131.json index ef14834e..85e97e4c 100644 --- a/libsrc/leddevice/schemas/schema-e131.json +++ b/libsrc/leddevice/schemas/schema-e131.json @@ -11,6 +11,8 @@ "type": "integer", "title":"Port", "default": 5568, + "minimum" : 0, + "maximum" : 65535, "propertyOrder" : 2 }, "universe": { diff --git a/libsrc/leddevice/schemas/schema-tpm2net.json b/libsrc/leddevice/schemas/schema-tpm2net.json index f9a01219..535beef3 100644 --- a/libsrc/leddevice/schemas/schema-tpm2net.json +++ b/libsrc/leddevice/schemas/schema-tpm2net.json @@ -4,21 +4,29 @@ "properties":{ "host" : { "type": "string", - "title":"Target IP", + "title":"Target IP/hostname", "propertyOrder" : 1 }, + "port": { + "type": "integer", + "title":"Target Port", + "minimum" : 0, + "maximum" : 65535, + "default" : 50200, + "propertyOrder" : 2 + }, "latchtime": { "type": "integer", "title":"Latchtime", "default": 104000, - "propertyOrder" : 2 + "propertyOrder" : 3 }, "max-packet": { "type": "integer", "title":"Max-packet", "minimum" : 0, "default" : 170, - "propertyOrder" : 3 + "propertyOrder" : 4 } }, "additionalProperties": true diff --git a/libsrc/leddevice/schemas/schema-udpraw.json b/libsrc/leddevice/schemas/schema-udpraw.json index a9da54ef..f8115b42 100644 --- a/libsrc/leddevice/schemas/schema-udpraw.json +++ b/libsrc/leddevice/schemas/schema-udpraw.json @@ -11,6 +11,8 @@ "type": "integer", "title":"Port", "default": 5568, + "minimum" : 0, + "maximum" : 65535, "propertyOrder" : 2 } },