open62541pp 0.17.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 return read(connection, detail::createReadRequest(timestamps, nodesToRead));
52}
53
54/**
55 * @copydoc read
56 * @param token @completiontoken{void(ReadResponse&)}
57 * @return @asyncresult{ReadResponse}
58 */
59template <typename CompletionToken>
60auto readAsync(Client& connection, const ReadRequest& request, CompletionToken&& token) {
61 return detail::sendRequestAsync<ReadRequest, ReadResponse>(
62 connection, request, std::forward<CompletionToken>(token)
63 );
64}
65
66/// @overload
67template <typename CompletionToken>
69 Client& connection,
70 Span<const ReadValueId> nodesToRead,
71 TimestampsToReturn timestamps,
72 CompletionToken&& token
73) {
74 return readAsync(
75 connection,
76 detail::createReadRequest(timestamps, nodesToRead),
77 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 return write(connection, detail::createWriteRequest(nodesToWrite));
149}
150
151/**
152 * @copydoc write
153 * @param token @completiontoken{void(WriteResponse&)}
154 * @return @asyncresult{WriteResponse}
155 */
156template <typename CompletionToken>
157auto writeAsync(Client& connection, const WriteRequest& request, CompletionToken&& token) {
158 return detail::sendRequestAsync<WriteRequest, WriteResponse>(
159 connection, request, std::forward<CompletionToken>(token)
160 );
161}
162
163/// @overload
164template <typename CompletionToken>
165auto writeAsync(Client& connection, Span<const WriteValue> nodesToWrite, CompletionToken&& token) {
166 return writeAsync(
167 connection, detail::createWriteRequest(nodesToWrite), std::forward<CompletionToken>(token)
168 );
169}
170
171/**
172 * Write node attribute.
173 * @param connection Instance of type Client
174 * @param id Node to write
175 * @param attributeId Attribute to write
176 * @param value Value to write
177 */
178template <typename T>
180 T& connection, const NodeId& id, AttributeId attributeId, const DataValue& value
181) noexcept;
182
183/**
184 * @copydoc writeAttribute
185 * @param token @completiontoken{void(StatusCode)}
186 * @return @asyncresult{StatusCode}
187 */
188template <typename CompletionToken>
190 Client& connection,
191 const NodeId& id,
192 AttributeId attributeId,
193 const DataValue& value,
194 CompletionToken&& token
195) {
196 auto item = detail::createWriteValue(id, attributeId, value);
197 const auto request = detail::createWriteRequest(item);
198 return writeAsync(
199 connection,
201 detail::TransformToken(
202 detail::getSingleStatus<UA_WriteResponse>, std::forward<CompletionToken>(token)
203 )
204 );
205}
206
207/**
208 * @}
209 */
210
211/* --------------------------------- Specialized read functions --------------------------------- */
212
213namespace detail {
214
215template <AttributeId Attribute, typename T>
216auto readAttributeImpl(T& connection, const NodeId& id) noexcept {
217 using Handler = typename detail::AttributeHandler<Attribute>;
218 return readAttribute(connection, id, Attribute, TimestampsToReturn::Neither)
219 .andThen(Handler::fromDataValue);
220}
221
222template <AttributeId Attribute, typename CompletionToken>
223auto readAttributeAsyncImpl(Client& connection, const NodeId& id, CompletionToken&& token) {
224 using Handler = typename detail::AttributeHandler<Attribute>;
225 return readAttributeAsync(
226 connection,
227 id,
228 Attribute,
229 TimestampsToReturn::Neither,
230 detail::TransformToken(
231 [](Result<DataValue>& result) {
232 return std::move(result).andThen(Handler::fromDataValue);
233 },
234 std::forward<CompletionToken>(token)
235 )
236 );
237}
238
239template <AttributeId Attribute, typename T, typename U>
240StatusCode writeAttributeImpl(T& connection, const NodeId& id, U&& value) noexcept {
241 using Handler = detail::AttributeHandler<Attribute>;
242 return writeAttribute(connection, id, Attribute, Handler::toDataValue(std::forward<U>(value)));
243}
244
245template <AttributeId Attribute, typename T, typename U, typename CompletionToken>
246auto writeAttributeAsyncImpl(T& connection, const NodeId& id, U&& value, CompletionToken&& token) {
247 using Handler = detail::AttributeHandler<Attribute>;
248 return writeAttributeAsync(
249 connection,
250 id,
251 Attribute,
252 Handler::toDataValue(std::forward<U>(value)),
253 std::forward<CompletionToken>(token)
254 );
255}
256
257} // namespace detail
258
259/**
260 * Read the `AttributeId::Value` attribute of a node as a DataValue object.
261 * @param connection Instance of type Client (or Server)
262 * @param id Node to read
263 * @ingroup Read
264 */
265template <typename T>
266Result<DataValue> readDataValue(T& connection, const NodeId& id) noexcept {
267 return readAttribute(connection, id, AttributeId::Value, TimestampsToReturn::Both);
268}
269
270/**
271 * @copydoc readDataValue
272 * @param token @completiontoken{void(Result<DataValue>&)}
273 * @return @asyncresult{Result<DataValue>}
274 * @ingroup Read
275 */
276template <typename CompletionToken>
277auto readDataValueAsync(Client& connection, const NodeId& id, CompletionToken&& token) {
278 return readAttributeAsync(
279 connection,
280 id,
282 TimestampsToReturn::Both,
283 std::forward<CompletionToken>(token)
284 );
285}
286
287/**
288 * Write the AttributeId::Value attribute of a node as a DataValue object.
289 * @param connection Instance of type Client (or Server)
290 * @param id Node to write
291 * @param value Value to write
292 * @ingroup Write
293 */
294template <typename T>
295StatusCode writeDataValue(T& connection, const NodeId& id, const DataValue& value) noexcept {
296 return writeAttribute(connection, id, AttributeId::Value, value);
297}
298
299/**
300 * @copydoc writeDataValue
301 * @param token @completiontoken{void(StatusCode)}
302 * @return @asyncresult{StatusCode}
303 * @ingroup Write
304 */
305template <typename CompletionToken>
307 Client& connection, const NodeId& id, const DataValue& value, CompletionToken&& token
308) {
309 return writeAttributeAsync(
310 connection, id, AttributeId::Value, value, std::forward<CompletionToken>(token)
311 );
312}
313
314/**
315 * @}
316 */
317
318} // namespace opcua::services
High-level client class.
Definition client.hpp:122
Client * asWrapper(UA_Client *client) noexcept
Convert native UA_Client pointer to its wrapper instance.
UA_DataValue wrapper class.
Definition types.hpp:1832
UA_NodeId wrapper class.
Definition types.hpp:666
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:28
UA_StatusCode wrapper class.
Definition types.hpp:46
UA_ReadRequest wrapper class.
Definition types.hpp:1419
UA_ReadResponse wrapper class.
Definition types.hpp:1446
UA_WriteRequest wrapper class.
Definition types.hpp:1484
UA_WriteResponse wrapper class.
Definition types.hpp:1502
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:60
TimestampsToReturn
Timestamps to return.
Definition types.hpp:1379
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
@ NodeId
Unambiguous identifier of a node.
@ Value
The most recent value of the variable that the server has.