open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
util.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 2018 (c) Stefan Profanter, fortiss GmbH
6 */
7
8#ifndef UA_HELPER_H_
9#define UA_HELPER_H_
10
11#include <open62541/types.h>
14
16
17
18
19struct UA_Server;
20typedef struct UA_Server UA_Server;
21
22struct UA_ServerConfig;
24
25typedef void (*UA_ServerCallback)(UA_Server *server, void *data);
26
27struct UA_Client;
28typedef struct UA_Client UA_Client;
29
30/** Timer policy to handle cycle misses */
35
36
37
38/** Makes a copy of the value. Can reallocate the underlying array. This
39 * invalidates pointers into the previous array. If the key exists already, the
40 * value is overwritten. */
41UA_EXPORT UA_StatusCode
43 const UA_QualifiedName *key,
44 const UA_Variant *value);
45
46/** Simplified version that assumes the key is in namespace 0 */
47UA_EXPORT UA_StatusCode
48UA_KeyValueMap_set(UA_KeyValuePair **map, size_t *mapSize,
49 const char *key, const UA_Variant *value);
50
51/** Returns a pointer into underlying array or NULL if the key is not found.*/
52UA_EXPORT const UA_Variant *
54 const UA_QualifiedName *key);
55
56/** Simplified version that assumes the key is in namespace 0 */
57UA_EXPORT const UA_Variant *
59 const char *key);
60
61/** Returns NULL if the value for the key is not defined or not of the right
62 * datatype and scalar/array */
63UA_EXPORT const UA_Variant *
65 const char *key, const UA_DataType *type);
66
67UA_EXPORT const UA_Variant *
69 const char *key, const UA_DataType *type);
70
71/** Remove a single entry. To delete the entire map, use UA_Array_delete. */
72UA_EXPORT void
74 const UA_QualifiedName *key);
75
76/** Simplified version that assumes the key is in namespace 0 */
77UA_EXPORT void
79 const char *key);
80
81
82
83/** Split the given endpoint url into hostname, port and path. All arguments must
84 * be non-NULL. EndpointUrls have the form "opc.tcp://hostname:port/path", port
85 * and path may be omitted (together with the prefix colon and slash).
86 *
87 * @param endpointUrl The endpoint URL.
88 * @param outHostname Set to the parsed hostname. The string points into the
89 * original endpointUrl, so no memory is allocated. If an IPv6 address is
90 * given, hostname contains e.g. '[2001:0db8:85a3::8a2e:0370:7334]'
91 * @param outPort Set to the port of the url or left unchanged.
92 * @param outPath Set to the path if one is present in the endpointUrl.
93 * Starting or trailing '/' are NOT included in the path. The string
94 * points into the original endpointUrl, so no memory is allocated.
95 * @return Returns UA_STATUSCODE_BADTCPENDPOINTURLINVALID if parsing failed. */
98 UA_UInt16 *outPort, UA_String *outPath);
99
100/** Split the given endpoint url into hostname, vid and pcp. All arguments must
101 * be non-NULL. EndpointUrls have the form "opc.eth://<host>[:<VID>[.PCP]]".
102 * The host is a MAC address, an IP address or a registered name like a
103 * hostname. The format of a MAC address is six groups of hexadecimal digits,
104 * separated by hyphens (e.g. 01-23-45-67-89-ab). A system may also accept
105 * hostnames and/or IP addresses if it provides means to resolve it to a MAC
106 * address (e.g. DNS and Reverse-ARP).
107 *
108 * Note: currently only parsing MAC address is supported.
109 *
110 * @param endpointUrl The endpoint URL.
111 * @param vid Set to VLAN ID.
112 * @param pcp Set to Priority Code Point.
113 * @return Returns UA_STATUSCODE_BADINTERNALERROR if parsing failed. */
116 UA_UInt16 *vid, UA_Byte *pcp);
117
118/** Convert given byte string to a positive number. Returns the number of valid
119 * digits. Stops if a non-digit char is found and returns the number of digits
120 * up to that point. */
121size_t
122UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number);
123
124/** Same as UA_ReadNumber but with a base parameter */
125size_t
126UA_readNumberWithBase(const UA_Byte *buf, size_t buflen,
127 UA_UInt32 *number, UA_Byte base);
128
129#ifndef UA_MIN
130#define UA_MIN(A, B) ((A) > (B) ? (B) : (A))
131#endif
132
133#ifndef UA_MAX
134#define UA_MAX(A, B) ((A) > (B) ? (A) : (B))
135#endif
136
137
138#ifdef UA_ENABLE_PARSING
139UA_EXPORT UA_StatusCode
140UA_RelativePath_parse(UA_RelativePath *rp, const UA_String str);
141#endif
142
143
144#define UA_PRINTF_GUID_FORMAT "%08" PRIx32 "-%04" PRIx16 "-%04" PRIx16 \
145 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8
146#define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
147 (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
148 (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
149
150#define UA_PRINTF_STRING_FORMAT "\"%.*s\""
151#define UA_PRINTF_STRING_DATA(STRING) (int)(STRING).length, (STRING).data
152
153
154
155/** Compare memory in constant time to mitigate timing attacks.
156 * Returns true if ptr1 and ptr2 are equal for length bytes. */
157static UA_Boolean
158UA_constantTimeEqual(const void *ptr1, const void *ptr2, size_t length) {
159 volatile const UA_Byte *a = (volatile const UA_Byte *)ptr1;
160 volatile const UA_Byte *b = (volatile const UA_Byte *)ptr2;
161 volatile UA_Byte c = 0;
162 for(size_t i = 0; i < length; ++i) {
163 UA_Byte x = a[i], y = b[i];
164 c = c | (x ^ y);
165 }
166 return !c;
167}
168
170
171#endif /* UA_HELPER_H_ */
#define _UA_BEGIN_DECLS
#undef UA_DEBUG_DUMP_PKGS
Definition config.h:89
#define _UA_END_DECLS
Definition config.h:96
UA_String endpointUrl
_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
uint32_t UA_UInt32
Definition types.h:56
uint32_t UA_StatusCode
Definition types.h:77
uint8_t UA_Byte
Definition types.h:36
UA_EXPORT const UA_Variant * UA_KeyValueMap_getQualified(UA_KeyValuePair *map, size_t mapSize, const UA_QualifiedName *key)
Returns a pointer into underlying array or NULL if the key is not found.
UA_EXPORT const UA_Variant * UA_KeyValueMap_getScalar(UA_KeyValuePair *map, size_t mapSize, const char *key, const UA_DataType *type)
Returns NULL if the value for the key is not defined or not of the right datatype and scalar/array.
UA_EXPORT UA_StatusCode UA_KeyValueMap_set(UA_KeyValuePair **map, size_t *mapSize, const char *key, const UA_Variant *value)
Simplified version that assumes the key is in namespace 0.
UA_StatusCode UA_parseEndpointUrlEthernet(const UA_String *endpointUrl, UA_String *target, UA_UInt16 *vid, UA_Byte *pcp)
Split the given endpoint url into hostname, vid and pcp.
void(* UA_ServerCallback)(UA_Server *server, void *data)
Definition util.h:25
UA_StatusCode UA_parseEndpointUrl(const UA_String *endpointUrl, UA_String *outHostname, UA_UInt16 *outPort, UA_String *outPath)
Split the given endpoint url into hostname, port and path.
UA_EXPORT void UA_KeyValueMap_deleteQualified(UA_KeyValuePair **map, size_t *mapSize, const UA_QualifiedName *key)
Remove a single entry.
UA_EXPORT const UA_Variant * UA_KeyValueMap_getArray(UA_KeyValuePair *map, size_t mapSize, const char *key, const UA_DataType *type)
size_t UA_readNumberWithBase(const UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base)
Same as UA_ReadNumber but with a base parameter.
UA_TimerPolicy
Timer policy to handle cycle misses.
Definition util.h:31
@ UA_TIMER_HANDLE_CYCLEMISS_WITH_CURRENTTIME
Definition util.h:32
@ UA_TIMER_HANDLE_CYCLEMISS_WITH_BASETIME
Definition util.h:33
UA_EXPORT UA_StatusCode UA_KeyValueMap_setQualified(UA_KeyValuePair **map, size_t *mapSize, const UA_QualifiedName *key, const UA_Variant *value)
Makes a copy of the value.
UA_EXPORT const UA_Variant * UA_KeyValueMap_get(UA_KeyValuePair *map, size_t mapSize, const char *key)
Simplified version that assumes the key is in namespace 0.
size_t UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number)
Convert given byte string to a positive number.
UA_EXPORT void UA_KeyValueMap_delete(UA_KeyValuePair **map, size_t *mapSize, const char *key)
Simplified version that assumes the key is in namespace 0.