open62541pp 0.18.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
attribute.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
13#include "open62541pp/span.hpp"
14#include "open62541pp/types.hpp"
16
17namespace opcua {
18class Client;
19} // namespace opcua
20
21namespace opcua::services {
22
23/* -------------------------------------- Generic functions ------------------------------------- */
24
25/**
26 * @defgroup Attribute Attribute service set
27 * Read and write node attributes.
28 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/5.11
29 * @ingroup Services
30 * @{
31 */
32
33/**
34 * @defgroup Read Read service
35 * This service is used to read attributes of nodes.
36 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/5.11.2
37 * @{
38 */
39
40/**
41 * Read one or more attributes of one or more nodes (client only).
42 * @param connection Instance of type Client
43 * @param request Read request
44 */
45ReadResponse read(Client& connection, const ReadRequest& request) noexcept;
46
47/// @overload
49 Client& connection, Span<const ReadValueId> nodesToRead, TimestampsToReturn timestamps
50) noexcept {
51 const auto request = detail::createReadRequest(timestamps, nodesToRead);
52 return read(connection, asWrapper<ReadRequest>(request));
53}
54
55/**
56 * @copydoc read
57 * @param token @completiontoken{void(ReadResponse&)}
58 * @return @asyncresult{ReadResponse}
59 */
60template <typename CompletionToken>
61auto readAsync(Client& connection, const ReadRequest& request, CompletionToken&& token) {
62 return detail::sendRequestAsync<ReadRequest, ReadResponse>(
63 connection, request, std::forward<CompletionToken>(token)
64 );
65}
66
67/// @overload
68template <typename CompletionToken>
70 Client& connection,
71 Span<const ReadValueId> nodesToRead,
72 TimestampsToReturn timestamps,
73 CompletionToken&& token
74) {
75 const auto request = detail::createReadRequest(timestamps, nodesToRead);
76 return readAsync(
77 connection, asWrapper<ReadRequest>(request), std::forward<CompletionToken>(token)
78 );
79}
80
81/**
82 * Read node attribute.
83 * @param connection Instance of type Client (or Server)
84 * @param id Node to read
85 * @param attributeId Attribute to read
86 * @param timestamps Timestamps to return, e.g. TimestampsToReturn::Neither
87 */
88template <typename T>
90 T& connection, const NodeId& id, AttributeId attributeId, TimestampsToReturn timestamps
91) noexcept;
92
93/**
94 * @copydoc readAttribute
95 * @param token @completiontoken{void(Result<DataValue>&)}
96 * @return @asyncresult{Result<DataValue>}
97 */
98template <typename CompletionToken>
100 Client& connection,
101 const NodeId& id,
102 AttributeId attributeId,
103 TimestampsToReturn timestamps,
104 CompletionToken&& token
105) {
106 auto item = detail::createReadValueId(id, attributeId);
107 const auto request = detail::createReadRequest(timestamps, item);
108 return readAsync(
109 connection,
110 asWrapper<ReadRequest>(request),
111 detail::TransformToken(
112 detail::wrapSingleResult<DataValue, UA_ReadResponse>,
113 std::forward<CompletionToken>(token)
114 )
115 );
116}
117
118/**
119 * @}
120 * @defgroup Write Write service
121 * This service is used to write attributes of nodes.
122 *
123 * The following node attributes cannot be changed once a node has been created:
124 * - AttributeId::NodeClass
125 * - AttributeId::NodeId
126 * - AttributeId::Symmetric
127 * - AttributeId::ContainsNoLoops
128 *
129 * The following attributes cannot be written from the server, as they are specific to the different
130 * users and set by the access control callback:
131 * - AttributeId::UserWriteMask
132 * - AttributeId::UserAccessLevel
133 * - AttributeId::UserExecutable
134 *
135 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/5.11.4
136 * @{
137 */
138
139/**
140 * Write one or more attributes of one or more nodes (client only).
141 * @param connection Instance of type Client
142 * @param request Write request
143 */
144WriteResponse write(Client& connection, const WriteRequest& request) noexcept;
145
146/// @overload
147inline WriteResponse write(Client& connection, Span<const WriteValue> nodesToWrite) noexcept {
148 const auto request = detail::createWriteRequest(nodesToWrite);
149 return write(connection, asWrapper<WriteRequest>(request));
150}
151
152/**
153 * @copydoc write
154 * @param token @completiontoken{void(WriteResponse&)}
155 * @return @asyncresult{WriteResponse}
156 */
157template <typename CompletionToken>
158auto writeAsync(Client& connection, const WriteRequest& request, CompletionToken&& token) {
159 return detail::sendRequestAsync<WriteRequest, WriteResponse>(
160 connection, request, std::forward<CompletionToken>(token)
161 );
162}
163
164/// @overload
165template <typename CompletionToken>
166auto writeAsync(Client& connection, Span<const WriteValue> nodesToWrite, CompletionToken&& token) {
167 const auto request = detail::createWriteRequest(nodesToWrite);
168 return writeAsync(
169 connection, asWrapper<WriteRequest>(request), std::forward<CompletionToken>(token)
170 );
171}
172
173/**
174 * Write node attribute.
175 * @param connection Instance of type Client
176 * @param id Node to write
177 * @param attributeId Attribute to write
178 * @param value Value to write
179 */
180template <typename T>
182 T& connection, const NodeId& id, AttributeId attributeId, const DataValue& value
183) noexcept;
184
185/**
186 * @copydoc writeAttribute
187 * @param token @completiontoken{void(StatusCode)}
188 * @return @asyncresult{StatusCode}
189 */
190template <typename CompletionToken>
192 Client& connection,
193 const NodeId& id,
194 AttributeId attributeId,
195 const DataValue& value,
196 CompletionToken&& token
197) {
198 auto item = detail::createWriteValue(id, attributeId, value);
199 const auto request = detail::createWriteRequest(item);
200 return writeAsync(
201 connection,
202 asWrapper<WriteRequest>(request),
203 detail::TransformToken(
204 detail::getSingleStatus<UA_WriteResponse>, std::forward<CompletionToken>(token)
205 )
206 );
207}
208
209/**
210 * @}
211 */
212
213/* --------------------------------- Specialized read functions --------------------------------- */
214
215namespace detail {
216
217template <AttributeId Attribute, typename T>
218auto readAttributeImpl(T& connection, const NodeId& id) noexcept {
219 using Handler = typename detail::AttributeHandler<Attribute>;
220 return readAttribute(connection, id, Attribute, TimestampsToReturn::Neither)
221 .andThen(Handler::fromDataValue);
222}
223
224template <AttributeId Attribute, typename CompletionToken>
225auto readAttributeAsyncImpl(Client& connection, const NodeId& id, CompletionToken&& token) {
226 using Handler = typename detail::AttributeHandler<Attribute>;
227 return readAttributeAsync(
228 connection,
229 id,
230 Attribute,
231 TimestampsToReturn::Neither,
232 detail::TransformToken(
233 [](Result<DataValue>& result) {
234 return std::move(result).andThen(Handler::fromDataValue);
235 },
236 std::forward<CompletionToken>(token)
237 )
238 );
239}
240
241template <AttributeId Attribute, typename T, typename U>
242StatusCode writeAttributeImpl(T& connection, const NodeId& id, U&& value) noexcept {
243 using Handler = detail::AttributeHandler<Attribute>;
244 return writeAttribute(connection, id, Attribute, Handler::toDataValue(std::forward<U>(value)));
245}
246
247template <AttributeId Attribute, typename T, typename U, typename CompletionToken>
248auto writeAttributeAsyncImpl(T& connection, const NodeId& id, U&& value, CompletionToken&& token) {
249 using Handler = detail::AttributeHandler<Attribute>;
250 return writeAttributeAsync(
251 connection,
252 id,
253 Attribute,
254 Handler::toDataValue(std::forward<U>(value)),
255 std::forward<CompletionToken>(token)
256 );
257}
258
259} // namespace detail
260
261/**
262 * Read the `AttributeId::Value` attribute of a node as a DataValue object.
263 * @param connection Instance of type Client (or Server)
264 * @param id Node to read
265 * @ingroup Read
266 */
267template <typename T>
268Result<DataValue> readDataValue(T& connection, const NodeId& id) noexcept {
269 return readAttribute(connection, id, AttributeId::Value, TimestampsToReturn::Both);
270}
271
272/**
273 * @copydoc readDataValue
274 * @param token @completiontoken{void(Result<DataValue>&)}
275 * @return @asyncresult{Result<DataValue>}
276 * @ingroup Read
277 */
278template <typename CompletionToken>
279auto readDataValueAsync(Client& connection, const NodeId& id, CompletionToken&& token) {
280 return readAttributeAsync(
281 connection,
282 id,
284 TimestampsToReturn::Both,
285 std::forward<CompletionToken>(token)
286 );
287}
288
289/**
290 * Write the AttributeId::Value attribute of a node as a DataValue object.
291 * @param connection Instance of type Client (or Server)
292 * @param id Node to write
293 * @param value Value to write
294 * @ingroup Write
295 */
296template <typename T>
297StatusCode writeDataValue(T& connection, const NodeId& id, const DataValue& value) noexcept {
298 return writeAttribute(connection, id, AttributeId::Value, value);
299}
300
301/**
302 * @copydoc writeDataValue
303 * @param token @completiontoken{void(StatusCode)}
304 * @return @asyncresult{StatusCode}
305 * @ingroup Write
306 */
307template <typename CompletionToken>
309 Client& connection, const NodeId& id, const DataValue& value, CompletionToken&& token
310) {
311 return writeAttributeAsync(
312 connection, id, AttributeId::Value, value, std::forward<CompletionToken>(token)
313 );
314}
315
316/**
317 * @}
318 */
319
320} // namespace opcua::services
High-level client class.
Definition client.hpp:126
UA_DataValue wrapper class.
Definition types.hpp:1849
UA_NodeId wrapper class.
Definition types.hpp:665
The template class Result encapsulates a StatusCode and optionally a value.
Definition result.hpp:53
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:30
UA_StatusCode wrapper class.
Definition types.hpp:47
UA_ReadRequest wrapper class.
Definition types.hpp:1451
UA_ReadResponse wrapper class.
Definition types.hpp:1478
UA_WriteRequest wrapper class.
Definition types.hpp:1516
UA_WriteResponse wrapper class.
Definition types.hpp:1534
auto readDataValueAsync(Client &connection, const NodeId &id, CompletionToken &&token)
Read the AttributeId::Value attribute of a node as a DataValue object.
Result< DataValue > readDataValue(T &connection, const NodeId &id) noexcept
Read the AttributeId::Value attribute of a node as a DataValue object.
ReadResponse read(Client &connection, const ReadRequest &request) noexcept
Read one or more attributes of one or more nodes (client only).
Result< DataValue > readAttribute(T &connection, const NodeId &id, AttributeId attributeId, TimestampsToReturn timestamps) noexcept
Read node attribute.
auto readAttributeAsync(Client &connection, const NodeId &id, AttributeId attributeId, TimestampsToReturn timestamps, CompletionToken &&token)
Read node attribute.
Definition attribute.hpp:99
auto readAsync(Client &connection, const ReadRequest &request, CompletionToken &&token)
Read one or more attributes of one or more nodes (client only).
Definition attribute.hpp:61
TimestampsToReturn
Timestamps to return.
Definition types.hpp:1411
StatusCode writeAttribute(T &connection, const NodeId &id, AttributeId attributeId, const DataValue &value) noexcept
Write node attribute.
auto writeAsync(Client &connection, const WriteRequest &request, CompletionToken &&token)
Write one or more attributes of one or more nodes (client only).
auto writeDataValueAsync(Client &connection, const NodeId &id, const DataValue &value, CompletionToken &&token)
Write the AttributeId::Value attribute of a node as a DataValue object.
auto writeAttributeAsync(Client &connection, const NodeId &id, AttributeId attributeId, const DataValue &value, CompletionToken &&token)
Write node attribute.
StatusCode writeDataValue(T &connection, const NodeId &id, const DataValue &value) noexcept
Write the AttributeId::Value attribute of a node as a DataValue object.
WriteResponse write(Client &connection, const WriteRequest &request) noexcept
Write one or more attributes of one or more nodes (client only).
OPC UA services as free functions.
Definition attribute.hpp:21
AttributeId
Attribute identifiers.
Definition common.hpp:28