open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
ua_util_internal.h
Go to the documentation of this file.
1/** This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 *
5 * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
6 * Copyright 2014, 2017 (c) Florian Palm
7 * Copyright 2015 (c) LEvertz
8 * Copyright 2015-2016 (c) Sten GrĂ¼ner
9 * Copyright 2015 (c) Chris Iatrou
10 * Copyright 2015-2016 (c) Oleksiy Vasylyev
11 * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
12 * Copyright 2021 (c) Fraunhofer IOSB (Author: Jan Hermes)
13 */
14
15#ifndef UA_UTIL_H_
16#define UA_UTIL_H_
17
18#define UA_INTERNAL
19#include <open62541/types.h>
20#include <open62541/util.h>
22
24
26
27/** Macro-Expand for MSVC workarounds */
28#define UA_MACRO_EXPAND(x) x
29
30/** Print a NodeId in logs */
31#define UA_LOG_NODEID_INTERNAL(NODEID, LOG) \
32 do { \
33 UA_String nodeIdStr = UA_STRING_NULL; \
34 UA_NodeId_print(NODEID, &nodeIdStr); \
35 LOG; \
36 UA_String_clear(&nodeIdStr); \
37 } while(0)
38
39#if UA_LOGLEVEL <= 100
40# define UA_LOG_NODEID_TRACE(NODEID, LOG) \
41 UA_LOG_NODEID_INTERNAL(NODEID, LOG)
42#else
43# define UA_LOG_NODEID_TRACE(NODEID, LOG)
44#endif
45
46#if UA_LOGLEVEL <= 200
47# define UA_LOG_NODEID_DEBUG(NODEID, LOG) \
48 UA_LOG_NODEID_INTERNAL(NODEID, LOG)
49#else
50# define UA_LOG_NODEID_DEBUG(NODEID, LOG)
51#endif
52
53#if UA_LOGLEVEL <= 300
54# define UA_LOG_NODEID_INFO(NODEID, LOG) \
55 UA_LOG_NODEID_INTERNAL(NODEID, LOG)
56#else
57# define UA_LOG_NODEID_INFO(NODEID, LOG)
58#endif
59
60#if UA_LOGLEVEL <= 400
61# define UA_LOG_NODEID_WARNING(NODEID, LOG) \
62 UA_LOG_NODEID_INTERNAL(NODEID, LOG)
63#else
64# define UA_LOG_NODEID_WARNING(NODEID, LOG)
65#endif
66
67#if UA_LOGLEVEL <= 500
68# define UA_LOG_NODEID_ERROR(NODEID, LOG) \
69 UA_LOG_NODEID_INTERNAL(NODEID, LOG)
70#else
71# define UA_LOG_NODEID_ERROR(NODEID, LOG)
72#endif
73
74#if UA_LOGLEVEL <= 600
75# define UA_LOG_NODEID_FATAL(NODEID, LOG) \
76 UA_LOG_NODEID_INTERNAL(NODEID, LOG)
77#else
78# define UA_LOG_NODEID_FATAL(NODEID, LOG)
79#endif
80
81/** Short names for integer. These are not exposed on the public API, since many
82 * user-applications make the same definitions in their headers. */
83typedef UA_Byte u8;
84typedef UA_SByte i8;
85typedef UA_UInt16 u16;
86typedef UA_Int16 i16;
87typedef UA_UInt32 u32;
88typedef UA_Int32 i32;
89typedef UA_UInt64 u64;
90typedef UA_Int64 i64;
92
93
94
95static UA_Boolean
96isGood(UA_StatusCode code) {
97 return code == UA_STATUSCODE_GOOD;
98}
99
100static UA_Boolean
101isNonNull(const void *ptr) {
102 return ptr != NULL;
103}
104
105static UA_Boolean
106isTrue(uint8_t expr) {
107 return expr;
108}
109
110#define UA_CHECK(A, EVAL_ON_ERROR) \
111 do { \
112 if(UA_UNLIKELY(!isTrue(A))) { \
113 EVAL_ON_ERROR; \
114 } \
115 } while(0)
116
117#define UA_CHECK_STATUS(STATUSCODE, EVAL_ON_ERROR) \
118 UA_CHECK(isGood(STATUSCODE), EVAL_ON_ERROR)
119
120#define UA_CHECK_MEM(STATUSCODE, EVAL_ON_ERROR) \
121 UA_CHECK(isNonNull(STATUSCODE), EVAL_ON_ERROR)
122
123#ifdef UA_DEBUG_FILE_LINE_INFO
124#define UA_CHECK_LOG_INTERNAL(A, STATUSCODE, EVAL, LOG, LOGGER, CAT, MSG, ...) \
125 UA_MACRO_EXPAND( \
126 UA_CHECK(A, LOG(LOGGER, CAT, "" MSG "%s (%s:%d: statuscode: %s)", __VA_ARGS__, \
127 __FILE__, __LINE__, UA_StatusCode_name(STATUSCODE)); \
128 EVAL))
129#else
130#define UA_CHECK_LOG_INTERNAL(A, STATUSCODE, EVAL, LOG, LOGGER, CAT, MSG, ...) \
131 UA_MACRO_EXPAND( \
132 UA_CHECK(A, LOG(LOGGER, CAT, "" MSG "%s (statuscode: %s)", __VA_ARGS__, \
133 UA_StatusCode_name(STATUSCODE)); \
134 EVAL))
135#endif
136
137#define UA_CHECK_LOG(A, EVAL, LEVEL, LOGGER, CAT, ...) \
138 UA_MACRO_EXPAND(UA_CHECK_LOG_INTERNAL(A, UA_STATUSCODE_BAD, EVAL, UA_LOG_##LEVEL, \
139 LOGGER, CAT, __VA_ARGS__, ""))
140
141#define UA_CHECK_STATUS_LOG(STATUSCODE, EVAL, LEVEL, LOGGER, CAT, ...) \
142 UA_MACRO_EXPAND(UA_CHECK_LOG_INTERNAL(isGood(STATUSCODE), STATUSCODE, \
143 EVAL, UA_LOG_##LEVEL, LOGGER, CAT, \
144 __VA_ARGS__, ""))
145
146#define UA_CHECK_MEM_LOG(PTR, EVAL, LEVEL, LOGGER, CAT, ...) \
147 UA_MACRO_EXPAND(UA_CHECK_LOG_INTERNAL(isNonNull(PTR), UA_STATUSCODE_BADOUTOFMEMORY, \
148 EVAL, UA_LOG_##LEVEL, LOGGER, CAT, \
149 __VA_ARGS__, ""))
150
151
152#define UA_CHECK_FATAL(A, EVAL, LOGGER, CAT, ...) \
153 UA_MACRO_EXPAND(UA_CHECK_LOG(A, EVAL, FATAL, LOGGER, CAT, __VA_ARGS__))
154#define UA_CHECK_ERROR(A, EVAL, LOGGER, CAT, ...) \
155 UA_MACRO_EXPAND(UA_CHECK_LOG(A, EVAL, ERROR, LOGGER, CAT, __VA_ARGS__))
156#define UA_CHECK_WARN(A, EVAL, LOGGER, CAT, ...) \
157 UA_MACRO_EXPAND(UA_CHECK_LOG(A, EVAL, WARNING, LOGGER, CAT, __VA_ARGS__))
158#define UA_CHECK_INFO(A, EVAL, LOGGER, CAT, ...) \
159 UA_MACRO_EXPAND(UA_CHECK_LOG(A, EVAL, INFO, LOGGER, CAT, __VA_ARGS__))
160
161#define UA_CHECK_STATUS_FATAL(STATUSCODE, EVAL, LOGGER, CAT, ...) \
162 UA_MACRO_EXPAND( \
163 UA_CHECK_STATUS_LOG(STATUSCODE, EVAL, FATAL, LOGGER, CAT, __VA_ARGS__))
164#define UA_CHECK_STATUS_ERROR(STATUSCODE, EVAL, LOGGER, CAT, ...) \
165 UA_MACRO_EXPAND( \
166 UA_CHECK_STATUS_LOG(STATUSCODE, EVAL, ERROR, LOGGER, CAT, __VA_ARGS__))
167#define UA_CHECK_STATUS_WARN(STATUSCODE, EVAL, LOGGER, CAT, ...) \
168 UA_MACRO_EXPAND( \
169 UA_CHECK_STATUS_LOG(STATUSCODE, EVAL, WARNING, LOGGER, CAT, __VA_ARGS__))
170#define UA_CHECK_STATUS_INFO(STATUSCODE, EVAL, LOGGER, CAT, ...) \
171 UA_MACRO_EXPAND( \
172 UA_CHECK_STATUS_LOG(STATUSCODE, EVAL, INFO, LOGGER, CAT, __VA_ARGS__))
173
174#define UA_CHECK_MEM_FATAL(PTR, EVAL, LOGGER, CAT, ...) \
175 UA_MACRO_EXPAND( \
176 UA_CHECK_MEM_LOG(PTR, EVAL, FATAL, LOGGER, CAT, __VA_ARGS__))
177#define UA_CHECK_MEM_ERROR(PTR, EVAL, LOGGER, CAT, ...) \
178 UA_MACRO_EXPAND( \
179 UA_CHECK_MEM_LOG(PTR, EVAL, ERROR, LOGGER, CAT, __VA_ARGS__))
180#define UA_CHECK_MEM_WARN(PTR, EVAL, LOGGER, CAT, ...) \
181 UA_MACRO_EXPAND( \
182 UA_CHECK_MEM_LOG(PTR, EVAL, WARNING, LOGGER, CAT, __VA_ARGS__))
183#define UA_CHECK_MEM_INFO(PTR, EVAL, LOGGER, CAT, ...) \
184 UA_MACRO_EXPAND( \
185 UA_CHECK_MEM_LOG(PTR, EVAL, INFO, LOGGER, CAT, __VA_ARGS__))
186
187
188
189const UA_DataType *
191 const UA_DataTypeArray *customTypes);
192
193/** Get the number of optional fields contained in an structure type */
194size_t
196
197/** Dump packet for debugging / fuzzing */
198#ifdef UA_DEBUG_DUMP_PKGS
199void
200UA_dump_hex_pkg(UA_Byte* buffer, size_t bufferLen);
201#endif
202
203/** Unions that represent any of the supported request or response message */
204typedef union {
208#ifdef UA_ENABLE_DISCOVERY
209# ifdef UA_ENABLE_DISCOVERY_MULTICAST
210 UA_FindServersOnNetworkRequest findServersOnNetworkRequest;
211# endif
212 UA_RegisterServerRequest registerServerRequest;
213 UA_RegisterServer2Request registerServer2Request;
214#endif
230#ifdef UA_ENABLE_HISTORIZING
231 UA_HistoryReadRequest historyReadRequest;
232 UA_HistoryUpdateRequest historyUpdateRequest;
233#endif
234#ifdef UA_ENABLE_METHODCALLS
236#endif
237#ifdef UA_ENABLE_SUBSCRIPTIONS
248#endif
249} UA_Request;
250
251typedef union {
255#ifdef UA_ENABLE_DISCOVERY
256# ifdef UA_ENABLE_DISCOVERY_MULTICAST
257 UA_FindServersOnNetworkResponse findServersOnNetworkResponse;
258# endif
259 UA_RegisterServerResponse registerServerResponse;
260 UA_RegisterServer2Response registerServer2Response;
261#endif
277#ifdef UA_ENABLE_HISTORIZING
278 UA_HistoryReadResponse historyReadResponse;
279 UA_HistoryUpdateResponse historyUpdateResponse;
280#endif
281#ifdef UA_ENABLE_METHODCALLS
283#endif
284#ifdef UA_ENABLE_SUBSCRIPTIONS
295#endif
297
298/** Do not expose UA_String_equal_ignorecase to public API as it currently only handles
299 * ASCII strings, and not UTF8! */
302
303
304/** Encoding Helpers */
305
306
307#define UA_ENCODING_HELPERS(TYPE, UPCASE_TYPE) \
308 static size_t \
309 UA_##TYPE##_calcSizeBinary(const UA_##TYPE *src) { \
310 return UA_calcSizeBinary(src, &UA_TYPES[UA_TYPES_##UPCASE_TYPE]); \
311 } \
312 static UA_StatusCode \
313 UA_##TYPE##_encodeBinary(const UA_##TYPE *src, UA_Byte **bufPos, const UA_Byte *bufEnd) { \
314 return UA_encodeBinaryInternal(src, &UA_TYPES[UA_TYPES_##UPCASE_TYPE], \
315 bufPos, &bufEnd, NULL, NULL); \
316 } \
317 static UA_StatusCode \
318 UA_##TYPE##_decodeBinary(const UA_ByteString *src, size_t *offset, UA_##TYPE *dst) { \
319 return UA_decodeBinaryInternal(src, offset, dst, \
320 &UA_TYPES[UA_TYPES_##UPCASE_TYPE], NULL); \
321 }
322
323UA_ENCODING_HELPERS(Boolean, BOOLEAN)
324UA_ENCODING_HELPERS(SByte, SBYTE)
325UA_ENCODING_HELPERS(Byte, BYTE)
326UA_ENCODING_HELPERS(Int16, INT16)
327UA_ENCODING_HELPERS(UInt16, UINT16)
328UA_ENCODING_HELPERS(Int32, INT32)
329UA_ENCODING_HELPERS(UInt32, UINT32)
330UA_ENCODING_HELPERS(Int64, INT64)
331UA_ENCODING_HELPERS(UInt64, UINT64)
332UA_ENCODING_HELPERS(Float, FLOAT)
333UA_ENCODING_HELPERS(Double, DOUBLE)
334UA_ENCODING_HELPERS(String, STRING)
335UA_ENCODING_HELPERS(DateTime, DATETIME)
336UA_ENCODING_HELPERS(Guid, GUID)
337UA_ENCODING_HELPERS(ByteString, BYTESTRING)
338UA_ENCODING_HELPERS(XmlElement, XMLELEMENT)
339UA_ENCODING_HELPERS(NodeId, NODEID)
340UA_ENCODING_HELPERS(ExpandedNodeId, EXPANDEDNODEID)
341UA_ENCODING_HELPERS(StatusCode, STATUSCODE)
342UA_ENCODING_HELPERS(QualifiedName, QUALIFIEDNAME)
343UA_ENCODING_HELPERS(LocalizedText, LOCALIZEDTEXT)
344UA_ENCODING_HELPERS(ExtensionObject, EXTENSIONOBJECT)
345UA_ENCODING_HELPERS(DataValue, DATAVALUE)
346UA_ENCODING_HELPERS(Variant, VARIANT)
347UA_ENCODING_HELPERS(DiagnosticInfo, DIAGNOSTICINFO)
348
350
351#endif /* UA_UTIL_H_ */
#define _UA_BEGIN_DECLS
#undef UA_DEBUG_DUMP_PKGS
Definition config.h:89
#define _UA_END_DECLS
Definition config.h:96
#define UA_STATUSCODE_GOOD
"The operation succeeded."
Definition statuscodes.h:10
Datatype arrays with custom type definitions can be added in a linked list to the client or server co...
Definition types.h:859
int32_t UA_Int32
Definition types.h:51
_UA_BEGIN_DECLS typedef bool UA_Boolean
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition types.h:26
uint16_t UA_UInt16
Definition types.h:46
int16_t UA_Int16
Definition types.h:41
int8_t UA_SByte
Definition types.h:31
uint32_t UA_UInt32
Definition types.h:56
uint32_t UA_StatusCode
Definition types.h:77
uint8_t UA_Byte
Definition types.h:36
uint64_t UA_UInt64
Definition types.h:66
int64_t UA_Int64
Definition types.h:61
#define UA_ENCODING_HELPERS(TYPE, UPCASE_TYPE)
Encoding Helpers.
UA_Int32 i32
UA_SByte i8
UA_Byte u8
Short names for integer.
UA_Boolean UA_String_equal_ignorecase(const UA_String *s1, const UA_String *s2)
Do not expose UA_String_equal_ignorecase to public API as it currently only handles ASCII strings,...
UA_UInt64 u64
const UA_DataType * UA_findDataTypeWithCustom(const UA_NodeId *typeId, const UA_DataTypeArray *customTypes)
UA_UInt16 u16
UA_Int64 i64
UA_StatusCode status
UA_Int16 i16
UA_UInt32 u32
size_t getCountOfOptionalFields(const UA_DataType *type)
Get the number of optional fields contained in an structure type.
Dump packet for debugging / fuzzing.
UA_DeleteReferencesRequest deleteReferencesRequest
UA_AddNodesRequest addNodesRequest
UA_DeleteMonitoredItemsRequest deleteMonitoredItemsRequest
UA_ActivateSessionRequest activateSessionRequest
UA_HistoryReadRequest historyReadRequest
UA_ModifySubscriptionRequest modifySubscriptionRequest
UA_CloseSessionRequest closeSessionRequest
UA_CreateMonitoredItemsRequest createMonitoredItemsRequest
UA_AddReferencesRequest addReferencesRequest
UA_RegisterNodesRequest registerNodesRequest
UA_CreateSessionRequest createSessionRequest
UA_DeleteNodesRequest deleteNodesRequest
UA_SetPublishingModeRequest setPublishingModeRequest
UA_RegisterServerRequest registerServerRequest
UA_OpenSecureChannelRequest openSecureChannelRequest
UA_HistoryUpdateRequest historyUpdateRequest
UA_TranslateBrowsePathsToNodeIdsRequest translateBrowsePathsToNodeIdsRequest
UA_UnregisterNodesRequest unregisterNodesRequest
UA_CreateSubscriptionRequest createSubscriptionRequest
UA_BrowseRequest browseRequest
UA_RequestHeader requestHeader
UA_CallRequest callRequest
UA_FindServersRequest findServersRequest
UA_PublishRequest publishRequest
UA_SetMonitoringModeRequest setMonitoringModeRequest
UA_GetEndpointsRequest getEndpointsRequest
UA_ReadRequest readRequest
UA_DeleteSubscriptionsRequest deleteSubscriptionsRequest
UA_RepublishRequest republishRequest
UA_BrowseNextRequest browseNextRequest
UA_WriteRequest writeRequest
UA_RegisterServer2Request registerServer2Request
UA_ModifyMonitoredItemsRequest modifyMonitoredItemsRequest
UA_ReadResponse readResponse
UA_HistoryUpdateResponse historyUpdateResponse
UA_HistoryReadResponse historyReadResponse
UA_OpenSecureChannelResponse openSecureChannelResponse
UA_ActivateSessionResponse activateSessionResponse
UA_CreateSessionResponse createSessionResponse
UA_ResponseHeader responseHeader
UA_AddNodesResponse addNodesResponse
UA_RepublishResponse republishResponse
UA_ModifySubscriptionResponse modifySubscriptionResponse
UA_GetEndpointsResponse getEndpointsResponse
UA_WriteResponse writeResponse
UA_CreateMonitoredItemsResponse createMonitoredItemsResponse
UA_DeleteSubscriptionsResponse deleteSubscriptionsResponse
UA_DeleteMonitoredItemsResponse deleteMonitoredItemsResponse
UA_SetPublishingModeResponse setPublishingModeResponse
UA_FindServersResponse findServersResponse
UA_DeleteReferencesResponse deleteReferencesResponse
UA_BrowseResponse browseResponse
UA_RegisterServerResponse registerServerResponse
UA_BrowseNextResponse browseNextResponse
UA_CloseSessionResponse closeSessionResponse
UA_RegisterServer2Response registerServer2Response
UA_SetMonitoringModeResponse setMonitoringModeResponse
UA_UnregisterNodesResponse unregisterNodesResponse
UA_CallResponse callResponse
UA_AddReferencesResponse addReferencesResponse
UA_DeleteNodesResponse deleteNodesResponse
UA_CreateSubscriptionResponse createSubscriptionResponse
UA_RegisterNodesResponse registerNodesResponse
UA_ModifyMonitoredItemsResponse modifyMonitoredItemsResponse
UA_PublishResponse publishResponse
UA_TranslateBrowsePathsToNodeIdsResponse translateBrowsePathsToNodeIdsResponse