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