| 1 | // Copyright (C) 2015 basysKom GmbH, opensource@basyskom.com |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qopcuaextensionobject.h" |
| 5 | #include "qopcuatype.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \class QOpcUaExtensionObject |
| 11 | \inmodule QtOpcUa |
| 12 | \brief The OPC UA ExtensionObject. |
| 13 | |
| 14 | This is the Qt OPC UA representation for an extension object. |
| 15 | Extension objects are used as a container in OPC UA whenever a non-builtin type is stored |
| 16 | in a Variant. It contains information about the type and encoding of the data as well as |
| 17 | the data itself encoded with one of the encodings specified in OPC UA part 6. |
| 18 | Decoders are supposed to decode extension objects if they can handle the type. If the type |
| 19 | is not supported by the decoder, the extension object is not decoded and decoding is left |
| 20 | to the user. |
| 21 | */ |
| 22 | |
| 23 | /*! |
| 24 | \enum QOpcUaExtensionObject::Encoding |
| 25 | |
| 26 | Enumerates the possible encodings of the body. |
| 27 | |
| 28 | \value NoBody |
| 29 | \value ByteString |
| 30 | \value Xml |
| 31 | */ |
| 32 | |
| 33 | class QOpcUaExtensionObjectData : public QSharedData |
| 34 | { |
| 35 | public: |
| 36 | QString encodingTypeId; |
| 37 | QByteArray encodedBody; |
| 38 | QOpcUaExtensionObject::Encoding encoding{QOpcUaExtensionObject::Encoding::NoBody}; |
| 39 | }; |
| 40 | |
| 41 | /*! |
| 42 | Default constructs an extension object with no parameters set. |
| 43 | */ |
| 44 | QOpcUaExtensionObject::QOpcUaExtensionObject() |
| 45 | : data(new QOpcUaExtensionObjectData) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | /*! |
| 50 | Constructs an extension object from \a rhs. |
| 51 | */ |
| 52 | QOpcUaExtensionObject::QOpcUaExtensionObject(const QOpcUaExtensionObject &rhs) |
| 53 | : data(rhs.data) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | /*! |
| 58 | \since 6.9 |
| 59 | Constructs an extension object with encoding type id \a typeId and body type \a encoding. |
| 60 | */ |
| 61 | QOpcUaExtensionObject::QOpcUaExtensionObject(const QString &typeId, Encoding encoding) |
| 62 | : data(new QOpcUaExtensionObjectData) |
| 63 | { |
| 64 | data->encodingTypeId = typeId; |
| 65 | data->encoding = encoding; |
| 66 | } |
| 67 | |
| 68 | /*! |
| 69 | Sets the values from \a rhs in this extension object. |
| 70 | */ |
| 71 | QOpcUaExtensionObject &QOpcUaExtensionObject::operator=(const QOpcUaExtensionObject &rhs) |
| 72 | { |
| 73 | if (this != &rhs) |
| 74 | data.operator=(o: rhs.data); |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | /*! |
| 79 | Returns \c true if this extension object has the same value as \a rhs. |
| 80 | */ |
| 81 | bool QOpcUaExtensionObject::operator==(const QOpcUaExtensionObject &rhs) const |
| 82 | { |
| 83 | return data->encoding == rhs.encoding() && |
| 84 | QOpcUa::nodeIdEquals(first: data->encodingTypeId, second: rhs.encodingTypeId()) && |
| 85 | data->encodedBody == rhs.encodedBody(); |
| 86 | } |
| 87 | |
| 88 | /*! |
| 89 | Converts this extension object to \l QVariant. |
| 90 | */ |
| 91 | QOpcUaExtensionObject::operator QVariant() const |
| 92 | { |
| 93 | return QVariant::fromValue(value: *this); |
| 94 | } |
| 95 | |
| 96 | QOpcUaExtensionObject::~QOpcUaExtensionObject() |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | /*! |
| 101 | Returns the \l {QOpcUaExtensionObject::Encoding} {encoding} of the body. |
| 102 | */ |
| 103 | QOpcUaExtensionObject::Encoding QOpcUaExtensionObject::encoding() const |
| 104 | { |
| 105 | return data->encoding; |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | Sets the encoding of the body to \a encoding. |
| 110 | */ |
| 111 | void QOpcUaExtensionObject::setEncoding(QOpcUaExtensionObject::Encoding encoding) |
| 112 | { |
| 113 | data->encoding = encoding; |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | Returns the body of this extension object. It contains the encoded data. |
| 118 | */ |
| 119 | QByteArray QOpcUaExtensionObject::encodedBody() const |
| 120 | { |
| 121 | return data->encodedBody; |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | Returns a reference to the body of this extension object. |
| 126 | */ |
| 127 | QByteArray &QOpcUaExtensionObject::encodedBodyRef() |
| 128 | { |
| 129 | return data->encodedBody; |
| 130 | } |
| 131 | /*! |
| 132 | Sets the body of this extension object to \a encodedBody. |
| 133 | */ |
| 134 | void QOpcUaExtensionObject::setEncodedBody(const QByteArray &encodedBody) |
| 135 | { |
| 136 | data->encodedBody = encodedBody; |
| 137 | } |
| 138 | |
| 139 | /*! |
| 140 | \since 5.13 |
| 141 | |
| 142 | Sets the body of this extension object to \a encodedBody, |
| 143 | sets the encoding of the body to \l ByteString and |
| 144 | sets the type id of the encoded data to \a typeId. |
| 145 | |
| 146 | \sa setEncodedBody(), setEncoding(), setEncodingTypeId() |
| 147 | */ |
| 148 | void QOpcUaExtensionObject::setBinaryEncodedBody(const QByteArray &encodedBody, const QString &typeId) |
| 149 | { |
| 150 | setEncodedBody(encodedBody); |
| 151 | setEncoding(Encoding::ByteString); |
| 152 | setEncodingTypeId(typeId); |
| 153 | } |
| 154 | |
| 155 | /*! |
| 156 | \since 5.13 |
| 157 | |
| 158 | Sets the body of this extension object to \a encodedBody, |
| 159 | sets the encoding of the body to \l Xml and sets the type |
| 160 | id of the encoded data to \a typeId. |
| 161 | |
| 162 | \sa setEncodedBody(), setEncoding(), setEncodingTypeId() |
| 163 | */ |
| 164 | void QOpcUaExtensionObject::setXmlEncodedBody(const QByteArray &encodedBody, const QString &typeId) |
| 165 | { |
| 166 | setEncodedBody(encodedBody); |
| 167 | setEncoding(Encoding::Xml); |
| 168 | setEncodingTypeId(typeId); |
| 169 | } |
| 170 | |
| 171 | /*! |
| 172 | Returns the node id of the encoding for the type stored by this extension object, for example ns=0;i=886 for |
| 173 | Range_Encoding_DefaultBinary. All encoding ids are listed in \l {https://opcfoundation.org/UA/schemas/1.05/NodeIds.csv}. |
| 174 | */ |
| 175 | QString QOpcUaExtensionObject::encodingTypeId() const |
| 176 | { |
| 177 | return data->encodingTypeId; |
| 178 | } |
| 179 | |
| 180 | /*! |
| 181 | Sets the node id of the encoding for the type stored by this extension object to \a encodingTypeId. |
| 182 | */ |
| 183 | void QOpcUaExtensionObject::setEncodingTypeId(const QString &encodingTypeId) |
| 184 | { |
| 185 | data->encodingTypeId = encodingTypeId; |
| 186 | } |
| 187 | |
| 188 | QT_END_NAMESPACE |
| 189 | |