open62541pp 0.16.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
6#include "open62541pp/common.hpp" // TimestampsToReturn
14#include "open62541pp/span.hpp"
15#include "open62541pp/types.hpp"
17
18namespace opcua {
19class Client;
20} // namespace opcua
21
22namespace opcua::services {
23
24/* -------------------------------------- Generic functions ------------------------------------- */
25
26/**
27 * @defgroup Attribute Attribute service set
28 * Read and write node attributes.
29 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/5.10
30 * @ingroup Services
31 * @{
32 */
33
34/**
35 * @defgroup Read Read service
36 * This service is used to read attributes of nodes.
37 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/5.10.2
38 * @{
39 */
40
41/**
42 * Read one or more attributes of one or more nodes (client only).
43 * @param connection Instance of type Client
44 * @param request Read request
45 */
46ReadResponse read(Client& connection, const ReadRequest& request) noexcept;
47
48/// @overload
50 Client& connection, Span<const ReadValueId> nodesToRead, TimestampsToReturn timestamps
51) noexcept {
52 return read(connection, detail::createReadRequest(timestamps, nodesToRead));
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) {
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 return readAsync(
76 connection,
77 detail::createReadRequest(timestamps, nodesToRead),
78 std::forward<CompletionToken>(token)
79 );
80}
81
82/**
83 * Read node attribute.
84 * @param connection Instance of type Client (or Server)
85 * @param id Node to read
86 * @param attributeId Attribute to read
87 * @param timestamps Timestamps to return, e.g. TimestampsToReturn::Neither
88 */
89template <typename T>
91 T& connection, const NodeId& id, AttributeId attributeId, TimestampsToReturn timestamps
92) noexcept;
93
94/**
95 * @copydoc readAttribute
96 * @param token @completiontoken{void(Result<DataValue>&)}
97 * @return @asyncresult{Result<DataValue>}
98 */
99template <typename CompletionToken>
101 Client& connection,
102 const NodeId& id,
103 AttributeId attributeId,
104 TimestampsToReturn timestamps,
105 CompletionToken&& token
106) {
107 auto item = detail::createReadValueId(id, attributeId);
108 const auto request = detail::createReadRequest(timestamps, item);
109 return readAsync(
110 connection,
111 asWrapper<ReadRequest>(request),
114 std::forward<CompletionToken>(token)
115 )
116 );
117}
118
119/**
120 * @}
121 * @defgroup Write Write service
122 * This service is used to write attributes of nodes.
123 *
124 * The following node attributes cannot be changed once a node has been created:
125 * - AttributeId::NodeClass
126 * - AttributeId::NodeId
127 * - AttributeId::Symmetric
128 * - AttributeId::ContainsNoLoops
129 *
130 * The following attributes cannot be written from the server, as they are specific to the different
131 * users and set by the access control callback:
132 * - AttributeId::UserWriteMask
133 * - AttributeId::UserAccessLevel
134 * - AttributeId::UserExecutable
135 *
136 * @see https://reference.opcfoundation.org/Core/Part4/v105/docs/5.10.4
137 * @{
138 */
139
140/**
141 * Write one or more attributes of one or more nodes (client only).
142 * @param connection Instance of type Client
143 * @param request Write request
144 */
145WriteResponse write(Client& connection, const WriteRequest& request) noexcept;
146
147/// @overload
148inline WriteResponse write(Client& connection, Span<const WriteValue> nodesToWrite) noexcept {
149 return write(connection, detail::createWriteRequest(nodesToWrite));
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) {
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 return writeAsync(
168 connection, detail::createWriteRequest(nodesToWrite), std::forward<CompletionToken>(token)
169 );
170}
171
172/**
173 * Write node attribute.
174 * @param connection Instance of type Client
175 * @param id Node to write
176 * @param attributeId Attribute to write
177 * @param value Value to write
178 */
179template <typename T>
181 T& connection, const NodeId& id, AttributeId attributeId, const DataValue& value
182) noexcept;
183
184/**
185 * @copydoc writeAttribute
186 * @param token @completiontoken{void(StatusCode)}
187 * @return @asyncresult{StatusCode}
188 */
189template <typename CompletionToken>
191 Client& connection,
192 const NodeId& id,
193 AttributeId attributeId,
194 const DataValue& value,
195 CompletionToken&& token
196) {
197 auto item = detail::createWriteValue(id, attributeId, value);
198 const auto request = detail::createWriteRequest(item);
199 return writeAsync(
200 connection,
203 detail::getSingleStatus<UA_WriteResponse>, std::forward<CompletionToken>(token)
204 )
205 );
206}
207
208/**
209 * @}
210 */
211
212/* --------------------------------- Specialized read functions --------------------------------- */
213
214namespace detail {
215
216template <AttributeId Attribute, typename T>
217auto readAttributeImpl(T& connection, const NodeId& id) noexcept {
218 using Handler = typename detail::AttributeHandler<Attribute>;
219 return readAttribute(connection, id, Attribute, TimestampsToReturn::Neither)
220 .andThen(Handler::fromDataValue);
221}
222
223template <AttributeId Attribute, typename CompletionToken>
224auto readAttributeAsyncImpl(Client& connection, const NodeId& id, CompletionToken&& token) {
225 using Handler = typename detail::AttributeHandler<Attribute>;
226 return readAttributeAsync(
227 connection,
228 id,
229 Attribute,
232 [](Result<DataValue>& result) {
233 return std::move(result).andThen(Handler::fromDataValue);
234 },
235 std::forward<CompletionToken>(token)
236 )
237 );
238}
239
240template <AttributeId Attribute, typename T, typename U>
241StatusCode writeAttributeImpl(T& connection, const NodeId& id, U&& value) noexcept {
243 return writeAttribute(connection, id, Attribute, Handler::toDataValue(std::forward<U>(value)));
244}
245
246template <AttributeId Attribute, typename T, typename U, typename CompletionToken>
247auto writeAttributeAsyncImpl(T& connection, const NodeId& id, U&& value, CompletionToken&& token) {
249 return writeAttributeAsync(
250 connection,
251 id,
252 Attribute,
253 Handler::toDataValue(std::forward<U>(value)),
254 std::forward<CompletionToken>(token)
255 );
256}
257
258} // namespace detail
259
260/**
261 * Read the `AttributeId::Value` attribute of a node as a DataValue object.
262 * @param connection Instance of type Client (or Server)
263 * @param id Node to read
264 * @ingroup Read
265 */
266template <typename T>
267Result<DataValue> readDataValue(T& connection, const NodeId& id) noexcept {
269}
270
271/**
272 * @copydoc readDataValue
273 * @param token @completiontoken{void(Result<DataValue>&)}
274 * @return @asyncresult{Result<DataValue>}
275 * @ingroup Read
276 */
277template <typename CompletionToken>
278auto readDataValueAsync(Client& connection, const NodeId& id, CompletionToken&& token) {
279 return readAttributeAsync(
280 connection,
281 id,
284 std::forward<CompletionToken>(token)
285 );
286}
287
288/**
289 * Write the AttributeId::Value attribute of a node as a DataValue object.
290 * @param connection Instance of type Client (or Server)
291 * @param id Node to write
292 * @param value Value to write
293 * @ingroup Write
294 */
295template <typename T>
296StatusCode writeDataValue(T& connection, const NodeId& id, const DataValue& value) noexcept {
297 return writeAttribute(connection, id, AttributeId::Value, value);
298}
299
300/**
301 * @copydoc writeDataValue
302 * @param token @completiontoken{void(StatusCode)}
303 * @return @asyncresult{StatusCode}
304 * @ingroup Write
305 */
306template <typename CompletionToken>
308 Client& connection, const NodeId& id, const DataValue& value, CompletionToken&& token
309) {
310 return writeAttributeAsync(
311 connection, id, AttributeId::Value, value, std::forward<CompletionToken>(token)
312 );
313}
314
315/**
316 * @}
317 */
318
319} // namespace opcua::services
High-level client class.
Definition client.hpp:121
UA_DataValue wrapper class.
Definition types.hpp:1478
UA_NodeId wrapper class.
Definition types.hpp:590
UA_ReadRequest wrapper class.
UA_ReadResponse wrapper class.
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:26
UA_StatusCode wrapper class.
Definition types.hpp:44
UA_WriteRequest wrapper class.
UA_WriteResponse wrapper class.
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.
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
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).
UA_ReadRequest createReadRequest(TimestampsToReturn timestamps, UA_ReadValueId &item) noexcept
UA_ReadValueId createReadValueId(const NodeId &id, AttributeId attributeId) noexcept
UA_WriteRequest createWriteRequest(UA_WriteValue &item) noexcept
StatusCode writeAttributeImpl(T &connection, const NodeId &id, U &&value) noexcept
auto readAttributeAsyncImpl(Client &connection, const NodeId &id, CompletionToken &&token)
StatusCode getSingleStatus(const Response &response) noexcept
auto readAttributeImpl(T &connection, const NodeId &id) noexcept
auto sendRequestAsync(Client &client, const Request &request, CompletionToken &&token)
Async client service requests.
auto writeAttributeAsyncImpl(T &connection, const NodeId &id, U &&value, CompletionToken &&token)
UA_WriteValue createWriteValue(const NodeId &id, AttributeId attributeId, const DataValue &value) noexcept
Result< WrapperType > wrapSingleResult(Response &response) noexcept
OPC UA services as free functions.
Definition attribute.hpp:22
TimestampsToReturn
Timestamps to return.
Definition common.hpp:287
Client * asWrapper(UA_Client *client) noexcept
Convert native UA_Client pointer to its wrapper instance.
AttributeId
Attribute identifiers.
Definition common.hpp:28
@ Value
The most recent value of the variable that the server has.
Attribute handler to convert DataValue objects to/from the attribute specific types.
Special token to transform async results within the completion handler.