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 | Sets the values from \a rhs in this extension object. |
59 | */ |
60 | QOpcUaExtensionObject &QOpcUaExtensionObject::operator=(const QOpcUaExtensionObject &rhs) |
61 | { |
62 | if (this != &rhs) |
63 | data.operator=(o: rhs.data); |
64 | return *this; |
65 | } |
66 | |
67 | /*! |
68 | Returns \c true if this extension object has the same value as \a rhs. |
69 | */ |
70 | bool QOpcUaExtensionObject::operator==(const QOpcUaExtensionObject &rhs) const |
71 | { |
72 | return data->encoding == rhs.encoding() && |
73 | QOpcUa::nodeIdEquals(first: data->encodingTypeId, second: rhs.encodingTypeId()) && |
74 | data->encodedBody == rhs.encodedBody(); |
75 | } |
76 | |
77 | /*! |
78 | Converts this extension object to \l QVariant. |
79 | */ |
80 | QOpcUaExtensionObject::operator QVariant() const |
81 | { |
82 | return QVariant::fromValue(value: *this); |
83 | } |
84 | |
85 | QOpcUaExtensionObject::~QOpcUaExtensionObject() |
86 | { |
87 | } |
88 | |
89 | /*! |
90 | Returns the \l {QOpcUaExtensionObject::Encoding} {encoding} of the body. |
91 | */ |
92 | QOpcUaExtensionObject::Encoding QOpcUaExtensionObject::encoding() const |
93 | { |
94 | return data->encoding; |
95 | } |
96 | |
97 | /*! |
98 | Sets the encoding of the body to \a encoding. |
99 | */ |
100 | void QOpcUaExtensionObject::setEncoding(QOpcUaExtensionObject::Encoding encoding) |
101 | { |
102 | data->encoding = encoding; |
103 | } |
104 | |
105 | /*! |
106 | Returns the body of this extension object. It contains the encoded data. |
107 | */ |
108 | QByteArray QOpcUaExtensionObject::encodedBody() const |
109 | { |
110 | return data->encodedBody; |
111 | } |
112 | |
113 | /*! |
114 | Returns a reference to the body of this extension object. |
115 | */ |
116 | QByteArray &QOpcUaExtensionObject::encodedBodyRef() |
117 | { |
118 | return data->encodedBody; |
119 | } |
120 | /*! |
121 | Sets the body of this extension object to \a encodedBody. |
122 | */ |
123 | void QOpcUaExtensionObject::setEncodedBody(const QByteArray &encodedBody) |
124 | { |
125 | data->encodedBody = encodedBody; |
126 | } |
127 | |
128 | /*! |
129 | \since 5.13 |
130 | |
131 | Sets the body of this extension object to \a encodedBody, |
132 | sets the encoding of the body to \l ByteString and |
133 | sets the type id of the encoded data to \a typeId. |
134 | |
135 | \sa setEncodedBody(), setEncoding(), setEncodingTypeId() |
136 | */ |
137 | void QOpcUaExtensionObject::setBinaryEncodedBody(const QByteArray &encodedBody, const QString &typeId) |
138 | { |
139 | setEncodedBody(encodedBody); |
140 | setEncoding(Encoding::ByteString); |
141 | setEncodingTypeId(typeId); |
142 | } |
143 | |
144 | /*! |
145 | \since 5.13 |
146 | |
147 | Sets the body of this extension object to \a encodedBody, |
148 | sets the encoding of the body to \l Xml and sets the type |
149 | id of the encoded data to \a typeId. |
150 | |
151 | \sa setEncodedBody(), setEncoding(), setEncodingTypeId() |
152 | */ |
153 | void QOpcUaExtensionObject::setXmlEncodedBody(const QByteArray &encodedBody, const QString &typeId) |
154 | { |
155 | setEncodedBody(encodedBody); |
156 | setEncoding(Encoding::Xml); |
157 | setEncodingTypeId(typeId); |
158 | } |
159 | |
160 | /*! |
161 | Returns the node id of the encoding for the type stored by this extension object, for example ns=0;i=886 for |
162 | Range_Encoding_DefaultBinary. All encoding ids are listed in \l {https://opcfoundation.org/UA/schemas/1.05/NodeIds.csv}. |
163 | */ |
164 | QString QOpcUaExtensionObject::encodingTypeId() const |
165 | { |
166 | return data->encodingTypeId; |
167 | } |
168 | |
169 | /*! |
170 | Sets the node id of the encoding for the type stored by this extension object to \a encodingTypeId. |
171 | */ |
172 | void QOpcUaExtensionObject::setEncodingTypeId(const QString &encodingTypeId) |
173 | { |
174 | data->encodingTypeId = encodingTypeId; |
175 | } |
176 | |
177 | QT_END_NAMESPACE |
178 | |