diff options
author | Jannis Voelker <jannis.voelker@basyskom.com> | 2018-04-09 08:56:29 +0200 |
---|---|---|
committer | Frank Meerkoetter <frank.meerkoetter@basyskom.com> | 2018-04-11 09:58:04 +0000 |
commit | 8b7fa3f9b8b750fb39f27e588ed9d48ae56ed405 (patch) | |
tree | ce80e9159babd35402ece1b5c22c1a6156dd6061 | |
parent | 34445111a9d39f4045cb3e727d38264b352862ce (diff) |
Fix encoding and decoding of bool values
According to part 6 of the OPC UA specification, a bool is encoded as a
byte with value 0 for false and 1 for true.
sizeof(bool) is not 1 on all platforms, a template specialization is
required to avoid decoding errors.
Change-Id: Ic39420f6bddabbaea14d821b6371b50473ddf776
Reviewed-by: Frank Meerkoetter <frank.meerkoetter@basyskom.com>
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
-rw-r--r-- | src/opcua/client/qopcuabinarydataencoding.cpp | 22 | ||||
-rw-r--r-- | src/opcua/client/qopcuabinarydataencoding_p.h | 4 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/opcua/client/qopcuabinarydataencoding.cpp b/src/opcua/client/qopcuabinarydataencoding.cpp index 5b3aa1b..e03fc37 100644 --- a/src/opcua/client/qopcuabinarydataencoding.cpp +++ b/src/opcua/client/qopcuabinarydataencoding.cpp @@ -54,6 +54,21 @@ T QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &s } template<> +bool QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &success) +{ + if (bufferSize) { + quint8 temp = *reinterpret_cast<const quint8*>(ptr); + ptr += 1; + bufferSize -= 1; + success = true; + return temp == 0 ? false : true; + } else { + success = false; + return false; + } +} + +template<> QString QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &success) { if (bufferSize < sizeof(qint32)) { @@ -204,6 +219,13 @@ void QOpcUaBinaryDataEncoding::encode(const T &src, QByteArray &dst) } template<> +void QOpcUaBinaryDataEncoding::encode<bool>(const bool &src, QByteArray &dst) +{ + const quint8 value = src ? 1 : 0; + dst.append(reinterpret_cast<const char *>(&value), 1); +} + +template<> void QOpcUaBinaryDataEncoding::encode<QString>(const QString &src, QByteArray &dst) { QByteArray arr = src.toUtf8(); diff --git a/src/opcua/client/qopcuabinarydataencoding_p.h b/src/opcua/client/qopcuabinarydataencoding_p.h index 655150f..883eb10 100644 --- a/src/opcua/client/qopcuabinarydataencoding_p.h +++ b/src/opcua/client/qopcuabinarydataencoding_p.h @@ -84,6 +84,8 @@ public: }; template <> +Q_OPCUA_EXPORT bool QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &success); +template <> Q_OPCUA_EXPORT QOpcUa::QEUInformation QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &success); template <> Q_OPCUA_EXPORT QOpcUa::QRange QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &success); @@ -96,6 +98,8 @@ Q_OPCUA_EXPORT QOpcUa::QAxisInformation QOpcUaBinaryDataEncoding::decode(const c template <> Q_OPCUA_EXPORT QOpcUa::QXValue QOpcUaBinaryDataEncoding::decode(const char *&ptr, size_t &bufferSize, bool &success); template <> +Q_OPCUA_EXPORT void QOpcUaBinaryDataEncoding::encode(const bool &src, QByteArray &dst); +template <> Q_OPCUA_EXPORT void QOpcUaBinaryDataEncoding::encode(const QOpcUa::QEUInformation &src, QByteArray &dst); template <> Q_OPCUA_EXPORT void QOpcUaBinaryDataEncoding::encode(const QOpcUa::QRange &src, QByteArray &dst); |