1 | // Copyright (C) 2018 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 "qopcuabinarydataencoding.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | /*! |
9 | \class QOpcUaBinaryDataEncoding |
10 | \inmodule QtOpcUa |
11 | \brief QOpcUaBinaryDataEncoding is a partial implementation of the OPC UA binary data encoding described in OPC-UA part 6. |
12 | |
13 | It offers template functions for encoding and decoding data for reading and writing extension objects. |
14 | |
15 | The following types are supported: |
16 | |
17 | \table |
18 | \header |
19 | \li Qt type |
20 | \li OPC UA type |
21 | \row |
22 | \li quint8 |
23 | \li uint8 |
24 | \row |
25 | \li qint8 |
26 | \li int8 |
27 | \row |
28 | \li quint16 |
29 | \li uint16 |
30 | \row |
31 | \li qint16 |
32 | \li int16 |
33 | \row |
34 | \li quint32 |
35 | \li uint32 |
36 | \row |
37 | \li qint32 |
38 | \li int32 |
39 | \row |
40 | \li quint64 |
41 | \li uint64 |
42 | \row |
43 | \li qint64 |
44 | \li int64 |
45 | \row |
46 | \li float |
47 | \li float |
48 | \row |
49 | \li double |
50 | \li double |
51 | \row |
52 | \li QString |
53 | \li String |
54 | \row |
55 | \li QOpcUaQualifiedName |
56 | \li QualifiedName |
57 | \row |
58 | \li QOpcUaLocalizedText |
59 | \li LocalizedText |
60 | \row |
61 | \li QOpcUaEUInformation |
62 | \li EUInformation |
63 | \row |
64 | \li QOpcUaRange |
65 | \li Range |
66 | \row |
67 | \li QOpcUaComplexNumber |
68 | \li ComplexNumber |
69 | \row |
70 | \li QOpcUaDoubleComplexNumber |
71 | \li DoubleComplexNumber |
72 | \row |
73 | \li QOpcUaAxisInformation |
74 | \li AxisInformation |
75 | \row |
76 | \li QOpcUaXValue |
77 | \li XV |
78 | \row |
79 | \li QUuid |
80 | \li GUID |
81 | \row |
82 | \li QString node id |
83 | \li NodeId |
84 | \row |
85 | \li QByteArray |
86 | \li ByteString |
87 | \row |
88 | \li QDateTime |
89 | \li DateTime |
90 | \row |
91 | \li QOpcUa::UaStatusCode |
92 | \li StatusCode |
93 | \row |
94 | \li QOpcUaExpandedNodeId |
95 | \li ExpandedNodeId |
96 | \row |
97 | \li QOpcUaExtensionObject |
98 | \li ExtensionObject |
99 | \row |
100 | \li QOpcUaArgument |
101 | \li Argument |
102 | |
103 | \row |
104 | \li QOpcUaApplicationRecordDataType |
105 | \li ApplicationRecordDataType |
106 | \endtable |
107 | */ |
108 | |
109 | /*! |
110 | \fn template<typename T, QOpcUa::Types OVERLAY> T QOpcUaBinaryDataEncoding::decode(bool &success) |
111 | |
112 | Decodes a scalar value of type T from the data buffer. |
113 | \a success is set to \c true if the decoding was successful, \c false if not. |
114 | |
115 | The decoded value is returned. If \a success is false, the returned value is invalid. |
116 | |
117 | \sa decodeArray() |
118 | */ |
119 | |
120 | /*! |
121 | \fn template<typename T, QOpcUa::Types OVERLAY> bool QOpcUaBinaryDataEncoding::encode(const T &src) |
122 | |
123 | Encodes \a src of type T and appends the encoded value to the data buffer. |
124 | Returns \c true if the value has been successfully encoded. |
125 | |
126 | \sa encodeArray() |
127 | */ |
128 | |
129 | /*! |
130 | \fn template<typename T, QOpcUa::Types OVERLAY> QList<T> QOpcUaBinaryDataEncoding::decodeArray(bool &success) |
131 | |
132 | Decodes an array of type T from the data buffer. |
133 | \a success is set to \c true if the decoding was successful, \c false if not. |
134 | |
135 | The decoded value is returned. If \a success is false, the returned value is invalid. |
136 | |
137 | \sa decode() |
138 | */ |
139 | |
140 | /*! |
141 | \fn template<typename T, QOpcUa::Types OVERLAY> bool QOpcUaBinaryDataEncoding::encodeArray(const QList<T> &src) |
142 | |
143 | Encodes all elements of type T in \a src and appends the encoded values to the data buffer. |
144 | |
145 | Returns \c true if the value has been successfully encoded. |
146 | |
147 | \sa encode() |
148 | */ |
149 | |
150 | /*! |
151 | Constructs a binary data encoding object for the data buffer \a buffer. |
152 | \a buffer must not be deleted as long as this binary data encoding object is used. |
153 | */ |
154 | QOpcUaBinaryDataEncoding::QOpcUaBinaryDataEncoding(QByteArray *buffer) |
155 | : m_data(buffer) |
156 | { |
157 | } |
158 | |
159 | /*! |
160 | Constructs a binary data encoding object using the encoded body of \a object as data buffer. |
161 | |
162 | \a object must not be deleted as long as this binary data encoding object is used. |
163 | */ |
164 | QOpcUaBinaryDataEncoding::QOpcUaBinaryDataEncoding(QOpcUaExtensionObject &object) |
165 | : m_data(&object.encodedBodyRef()) |
166 | { |
167 | } |
168 | |
169 | bool QOpcUaBinaryDataEncoding::enoughData(int requiredSize) |
170 | { |
171 | if (!m_data) |
172 | return false; |
173 | return (m_data->size() - m_offset) >= requiredSize; |
174 | } |
175 | |
176 | /*! |
177 | Returns the current offset in the data buffer. |
178 | */ |
179 | int QOpcUaBinaryDataEncoding::offset() const |
180 | { |
181 | return m_offset; |
182 | } |
183 | |
184 | /*! |
185 | Sets the current offset in the data buffer to \a offset. |
186 | The first byte in the buffer has the offset 0. |
187 | */ |
188 | void QOpcUaBinaryDataEncoding::setOffset(int offset) |
189 | { |
190 | m_offset = offset; |
191 | } |
192 | |
193 | /*! |
194 | Truncates the data buffer to the current \l offset(). |
195 | If the offset is behind the current buffer size, this method does nothing. |
196 | |
197 | This method can be used to roll back after an unsuccessful encode by setting |
198 | the old offset and calling truncateBufferToOffset(). |
199 | */ |
200 | void QOpcUaBinaryDataEncoding::truncateBufferToOffset() |
201 | { |
202 | if (!m_data) |
203 | return; |
204 | |
205 | if (m_offset < m_data->size() - 1) |
206 | m_data->truncate(pos: m_offset + 1); |
207 | } |
208 | |
209 | QT_END_NAMESPACE |
210 | |