types.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 (c) Leon Urbas
  6. * Copyright 2014, 2016-2017 (c) Florian Palm
  7. * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  8. * Copyright 2015-2016 (c) Sten Grüner
  9. * Copyright 2015-2016 (c) Chris Iatrou
  10. * Copyright 2015 (c) Nick Goossens
  11. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  12. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  13. * Copyright 2017 (c) Thomas Stalder, Blue Time Concept SA
  14. */
  15. #ifndef UA_TYPES_H_
  16. #define UA_TYPES_H_
  17. #include <open62541/config.h>
  18. #include <open62541/constants.h>
  19. #include <open62541/statuscodes.h>
  20. _UA_BEGIN_DECLS
  21. #define UA_BUILTIN_TYPES_COUNT 25U
  22. /**
  23. * .. _types:
  24. *
  25. * Data Types
  26. * ==========
  27. *
  28. * The OPC UA protocol defines 25 builtin data types and three ways of combining
  29. * them into higher-order types: arrays, structures and unions. In open62541,
  30. * only the builtin data types are defined manually. All other data types are
  31. * generated from standard XML definitions. Their exact definitions can be
  32. * looked up at https://opcfoundation.org/UA/schemas/Opc.Ua.Types.bsd.
  33. *
  34. * For users that are new to open62541, take a look at the :ref:`tutorial for
  35. * working with data types<types-tutorial>` before diving into the
  36. * implementation details.
  37. *
  38. * Builtin Types
  39. * -------------
  40. *
  41. * Boolean
  42. * ^^^^^^^
  43. * A two-state logical value (true or false). */
  44. typedef bool UA_Boolean;
  45. #define UA_TRUE true UA_INTERNAL_DEPRECATED
  46. #define UA_FALSE false UA_INTERNAL_DEPRECATED
  47. /**
  48. * SByte
  49. * ^^^^^
  50. * An integer value between -128 and 127. */
  51. typedef int8_t UA_SByte;
  52. #define UA_SBYTE_MIN (-128)
  53. #define UA_SBYTE_MAX 127
  54. /**
  55. * Byte
  56. * ^^^^
  57. * An integer value between 0 and 255. */
  58. typedef uint8_t UA_Byte;
  59. #define UA_BYTE_MIN 0
  60. #define UA_BYTE_MAX 255
  61. /**
  62. * Int16
  63. * ^^^^^
  64. * An integer value between -32 768 and 32 767. */
  65. typedef int16_t UA_Int16;
  66. #define UA_INT16_MIN (-32768)
  67. #define UA_INT16_MAX 32767
  68. /**
  69. * UInt16
  70. * ^^^^^^
  71. * An integer value between 0 and 65 535. */
  72. typedef uint16_t UA_UInt16;
  73. #define UA_UINT16_MIN 0
  74. #define UA_UINT16_MAX 65535
  75. /**
  76. * Int32
  77. * ^^^^^
  78. * An integer value between -2 147 483 648 and 2 147 483 647. */
  79. typedef int32_t UA_Int32;
  80. #define UA_INT32_MIN (-2147483648)
  81. #define UA_INT32_MAX 2147483647
  82. /**
  83. * UInt32
  84. * ^^^^^^
  85. * An integer value between 0 and 4 294 967 295. */
  86. typedef uint32_t UA_UInt32;
  87. #define UA_UINT32_MIN 0
  88. #define UA_UINT32_MAX 4294967295
  89. /**
  90. * Int64
  91. * ^^^^^
  92. * An integer value between -9 223 372 036 854 775 808 and
  93. * 9 223 372 036 854 775 807. */
  94. typedef int64_t UA_Int64;
  95. #define UA_INT64_MAX (int64_t)9223372036854775807LL
  96. #define UA_INT64_MIN ((int64_t)-UA_INT64_MAX-1LL)
  97. /**
  98. * UInt64
  99. * ^^^^^^
  100. * An integer value between 0 and 18 446 744 073 709 551 615. */
  101. typedef uint64_t UA_UInt64;
  102. #define UA_UINT64_MIN (uint64_t)0
  103. #define UA_UINT64_MAX (uint64_t)18446744073709551615ULL
  104. /**
  105. * Float
  106. * ^^^^^
  107. * An IEEE single precision (32 bit) floating point value. */
  108. typedef float UA_Float;
  109. /**
  110. * Double
  111. * ^^^^^^
  112. * An IEEE double precision (64 bit) floating point value. */
  113. typedef double UA_Double;
  114. /**
  115. * .. _statuscode:
  116. *
  117. * StatusCode
  118. * ^^^^^^^^^^
  119. * A numeric identifier for a error or condition that is associated with a value
  120. * or an operation. See the section :ref:`statuscodes` for the meaning of a
  121. * specific code. */
  122. typedef uint32_t UA_StatusCode;
  123. /* Returns the human-readable name of the StatusCode. If no matching StatusCode
  124. * is found, a default string for "Unknown" is returned. This feature might be
  125. * disabled to create a smaller binary with the
  126. * UA_ENABLE_STATUSCODE_DESCRIPTIONS build-flag. Then the function returns an
  127. * empty string for every StatusCode. */
  128. UA_EXPORT const char *
  129. UA_StatusCode_name(UA_StatusCode code);
  130. /**
  131. * String
  132. * ^^^^^^
  133. * A sequence of Unicode characters. Strings are just an array of UA_Byte. */
  134. typedef struct {
  135. size_t length; /* The length of the string */
  136. UA_Byte *data; /* The content (not null-terminated) */
  137. } UA_String;
  138. /* Copies the content on the heap. Returns a null-string when alloc fails */
  139. UA_String UA_EXPORT UA_String_fromChars(const char *src) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  140. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *s1, const UA_String *s2);
  141. UA_EXPORT extern const UA_String UA_STRING_NULL;
  142. /**
  143. * ``UA_STRING`` returns a string pointing to the original char-array.
  144. * ``UA_STRING_ALLOC`` is shorthand for ``UA_String_fromChars`` and makes a copy
  145. * of the char-array. */
  146. static UA_INLINE UA_String
  147. UA_STRING(char *chars) {
  148. UA_String s; s.length = 0; s.data = NULL;
  149. if(!chars)
  150. return s;
  151. s.length = strlen(chars); s.data = (UA_Byte*)chars; return s;
  152. }
  153. #define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
  154. /* Define strings at compile time (in ROM) */
  155. #define UA_STRING_STATIC(CHARS) {sizeof(CHARS)-1, (UA_Byte*)CHARS}
  156. /**
  157. * .. _datetime:
  158. *
  159. * DateTime
  160. * ^^^^^^^^
  161. * An instance in time. A DateTime value is encoded as a 64-bit signed integer
  162. * which represents the number of 100 nanosecond intervals since January 1, 1601
  163. * (UTC).
  164. *
  165. * The methods providing an interface to the system clock are architecture-
  166. * specific. Usually, they provide a UTC clock that includes leap seconds. The
  167. * OPC UA standard allows the use of International Atomic Time (TAI) for the
  168. * DateTime instead. But this is still unusual and not implemented for most
  169. * SDKs. Currently (2019), UTC and TAI are 37 seconds apart due to leap
  170. * seconds. */
  171. typedef int64_t UA_DateTime;
  172. /* Multiples to convert durations to DateTime */
  173. #define UA_DATETIME_USEC 10LL
  174. #define UA_DATETIME_MSEC (UA_DATETIME_USEC * 1000LL)
  175. #define UA_DATETIME_SEC (UA_DATETIME_MSEC * 1000LL)
  176. /* The current time in UTC time */
  177. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  178. /* Offset between local time and UTC time */
  179. UA_Int64 UA_EXPORT UA_DateTime_localTimeUtcOffset(void);
  180. /* CPU clock invariant to system time changes. Use only to measure durations,
  181. * not absolute time. */
  182. UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void);
  183. /* Represents a Datetime as a structure */
  184. typedef struct UA_DateTimeStruct {
  185. UA_UInt16 nanoSec;
  186. UA_UInt16 microSec;
  187. UA_UInt16 milliSec;
  188. UA_UInt16 sec;
  189. UA_UInt16 min;
  190. UA_UInt16 hour;
  191. UA_UInt16 day; /* From 1 to 31 */
  192. UA_UInt16 month; /* From 1 to 12 */
  193. UA_UInt16 year;
  194. } UA_DateTimeStruct;
  195. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime t);
  196. /* The C99 standard (7.23.1) says: "The range and precision of times
  197. * representable in clock_t and time_t are implementation-defined." On most
  198. * systems, time_t is a 4 or 8 byte integer counting seconds since the UTC Unix
  199. * epoch. The following methods are used for conversion. */
  200. /* Datetime of 1 Jan 1970 00:00 */
  201. #define UA_DATETIME_UNIX_EPOCH (11644473600LL * UA_DATETIME_SEC)
  202. static UA_INLINE UA_Int64
  203. UA_DateTime_toUnixTime(UA_DateTime date) {
  204. return (date - UA_DATETIME_UNIX_EPOCH) / UA_DATETIME_SEC;
  205. }
  206. static UA_INLINE UA_DateTime
  207. UA_DateTime_fromUnixTime(UA_Int64 unixDate) {
  208. return (unixDate * UA_DATETIME_SEC) + UA_DATETIME_UNIX_EPOCH;
  209. }
  210. /**
  211. * Guid
  212. * ^^^^
  213. * A 16 byte value that can be used as a globally unique identifier. */
  214. typedef struct {
  215. UA_UInt32 data1;
  216. UA_UInt16 data2;
  217. UA_UInt16 data3;
  218. UA_Byte data4[8];
  219. } UA_Guid;
  220. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  221. UA_EXPORT extern const UA_Guid UA_GUID_NULL;
  222. /**
  223. * ByteString
  224. * ^^^^^^^^^^
  225. * A sequence of octets. */
  226. typedef UA_String UA_ByteString;
  227. static UA_INLINE UA_Boolean
  228. UA_ByteString_equal(const UA_ByteString *string1,
  229. const UA_ByteString *string2) {
  230. return UA_String_equal((const UA_String*)string1,
  231. (const UA_String*)string2);
  232. }
  233. /* Allocates memory of size length for the bytestring.
  234. * The content is not set to zero. */
  235. UA_StatusCode UA_EXPORT
  236. UA_ByteString_allocBuffer(UA_ByteString *bs, size_t length);
  237. UA_EXPORT extern const UA_ByteString UA_BYTESTRING_NULL;
  238. static UA_INLINE UA_ByteString
  239. UA_BYTESTRING(char *chars) {
  240. UA_ByteString bs; bs.length = 0; bs.data = NULL;
  241. if(!chars)
  242. return bs;
  243. bs.length = strlen(chars); bs.data = (UA_Byte*)chars; return bs;
  244. }
  245. static UA_INLINE UA_ByteString
  246. UA_BYTESTRING_ALLOC(const char *chars) {
  247. UA_String str = UA_String_fromChars(chars); UA_ByteString bstr;
  248. bstr.length = str.length; bstr.data = str.data; return bstr;
  249. }
  250. /**
  251. * XmlElement
  252. * ^^^^^^^^^^
  253. * An XML element. */
  254. typedef UA_String UA_XmlElement;
  255. /**
  256. * .. _nodeid:
  257. *
  258. * NodeId
  259. * ^^^^^^
  260. * An identifier for a node in the address space of an OPC UA Server. */
  261. enum UA_NodeIdType {
  262. UA_NODEIDTYPE_NUMERIC = 0, /* In the binary encoding, this can also
  263. * become 1 or 2 (two-byte and four-byte
  264. * encoding of small numeric nodeids) */
  265. UA_NODEIDTYPE_STRING = 3,
  266. UA_NODEIDTYPE_GUID = 4,
  267. UA_NODEIDTYPE_BYTESTRING = 5
  268. };
  269. typedef struct {
  270. UA_UInt16 namespaceIndex;
  271. enum UA_NodeIdType identifierType;
  272. union {
  273. UA_UInt32 numeric;
  274. UA_String string;
  275. UA_Guid guid;
  276. UA_ByteString byteString;
  277. } identifier;
  278. } UA_NodeId;
  279. UA_EXPORT extern const UA_NodeId UA_NODEID_NULL;
  280. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  281. UA_Order UA_EXPORT UA_NodeId_order(const UA_NodeId *n1, const UA_NodeId *n2);
  282. static UA_INLINE UA_Boolean
  283. UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2) {
  284. return (UA_NodeId_order(n1, n2) == UA_ORDER_EQ);
  285. }
  286. /* Returns a non-cryptographic hash for the NodeId */
  287. UA_UInt32 UA_EXPORT UA_NodeId_hash(const UA_NodeId *n);
  288. /** The following functions are shorthand for creating NodeIds. */
  289. static UA_INLINE UA_NodeId
  290. UA_NODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  291. UA_NodeId id; id.namespaceIndex = nsIndex;
  292. id.identifierType = UA_NODEIDTYPE_NUMERIC;
  293. id.identifier.numeric = identifier; return id;
  294. }
  295. static UA_INLINE UA_NodeId
  296. UA_NODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  297. UA_NodeId id; id.namespaceIndex = nsIndex;
  298. id.identifierType = UA_NODEIDTYPE_STRING;
  299. id.identifier.string = UA_STRING(chars); return id;
  300. }
  301. static UA_INLINE UA_NodeId
  302. UA_NODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  303. UA_NodeId id; id.namespaceIndex = nsIndex;
  304. id.identifierType = UA_NODEIDTYPE_STRING;
  305. id.identifier.string = UA_STRING_ALLOC(chars); return id;
  306. }
  307. static UA_INLINE UA_NodeId
  308. UA_NODEID_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  309. UA_NodeId id; id.namespaceIndex = nsIndex;
  310. id.identifierType = UA_NODEIDTYPE_GUID;
  311. id.identifier.guid = guid; return id;
  312. }
  313. static UA_INLINE UA_NodeId
  314. UA_NODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  315. UA_NodeId id; id.namespaceIndex = nsIndex;
  316. id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  317. id.identifier.byteString = UA_BYTESTRING(chars); return id;
  318. }
  319. static UA_INLINE UA_NodeId
  320. UA_NODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  321. UA_NodeId id; id.namespaceIndex = nsIndex;
  322. id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  323. id.identifier.byteString = UA_BYTESTRING_ALLOC(chars); return id;
  324. }
  325. /**
  326. * ExpandedNodeId
  327. * ^^^^^^^^^^^^^^
  328. * A NodeId that allows the namespace URI to be specified instead of an index. */
  329. typedef struct {
  330. UA_NodeId nodeId;
  331. UA_String namespaceUri;
  332. UA_UInt32 serverIndex;
  333. } UA_ExpandedNodeId;
  334. UA_EXPORT extern const UA_ExpandedNodeId UA_EXPANDEDNODEID_NULL;
  335. UA_Order UA_EXPORT
  336. UA_ExpandedNodeId_order(const UA_ExpandedNodeId *n1, const UA_ExpandedNodeId *n2);
  337. static UA_INLINE UA_Boolean
  338. UA_ExpandedNodeId_equal(const UA_ExpandedNodeId *n1, const UA_ExpandedNodeId *n2) {
  339. return (UA_ExpandedNodeId_order(n1, n2) == UA_ORDER_EQ);
  340. }
  341. /* Returns a non-cryptographic hash for the NodeId */
  342. UA_UInt32 UA_EXPORT UA_ExpandedNodeId_hash(const UA_ExpandedNodeId *n);
  343. /** The following functions are shorthand for creating ExpandedNodeIds. */
  344. static UA_INLINE UA_ExpandedNodeId
  345. UA_EXPANDEDNODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  346. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_NUMERIC(nsIndex, identifier);
  347. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  348. }
  349. static UA_INLINE UA_ExpandedNodeId
  350. UA_EXPANDEDNODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  351. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING(nsIndex, chars);
  352. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  353. }
  354. static UA_INLINE UA_ExpandedNodeId
  355. UA_EXPANDEDNODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  356. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING_ALLOC(nsIndex, chars);
  357. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  358. }
  359. static UA_INLINE UA_ExpandedNodeId
  360. UA_EXPANDEDNODEID_STRING_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  361. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_GUID(nsIndex, guid);
  362. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  363. }
  364. static UA_INLINE UA_ExpandedNodeId
  365. UA_EXPANDEDNODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  366. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING(nsIndex, chars);
  367. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  368. }
  369. static UA_INLINE UA_ExpandedNodeId
  370. UA_EXPANDEDNODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  371. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING_ALLOC(nsIndex, chars);
  372. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  373. }
  374. /**
  375. * .. _qualifiedname:
  376. *
  377. * QualifiedName
  378. * ^^^^^^^^^^^^^
  379. * A name qualified by a namespace. */
  380. typedef struct {
  381. UA_UInt16 namespaceIndex;
  382. UA_String name;
  383. } UA_QualifiedName;
  384. static UA_INLINE UA_Boolean
  385. UA_QualifiedName_isNull(const UA_QualifiedName *q) {
  386. return (q->namespaceIndex == 0 && q->name.length == 0);
  387. }
  388. static UA_INLINE UA_QualifiedName
  389. UA_QUALIFIEDNAME(UA_UInt16 nsIndex, char *chars) {
  390. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  391. qn.name = UA_STRING(chars); return qn;
  392. }
  393. static UA_INLINE UA_QualifiedName
  394. UA_QUALIFIEDNAME_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  395. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  396. qn.name = UA_STRING_ALLOC(chars); return qn;
  397. }
  398. UA_Boolean UA_EXPORT
  399. UA_QualifiedName_equal(const UA_QualifiedName *qn1,
  400. const UA_QualifiedName *qn2);
  401. /**
  402. * LocalizedText
  403. * ^^^^^^^^^^^^^
  404. * Human readable text with an optional locale identifier. */
  405. typedef struct {
  406. UA_String locale;
  407. UA_String text;
  408. } UA_LocalizedText;
  409. static UA_INLINE UA_LocalizedText
  410. UA_LOCALIZEDTEXT(char *locale, char *text) {
  411. UA_LocalizedText lt; lt.locale = UA_STRING(locale);
  412. lt.text = UA_STRING(text); return lt;
  413. }
  414. static UA_INLINE UA_LocalizedText
  415. UA_LOCALIZEDTEXT_ALLOC(const char *locale, const char *text) {
  416. UA_LocalizedText lt; lt.locale = UA_STRING_ALLOC(locale);
  417. lt.text = UA_STRING_ALLOC(text); return lt;
  418. }
  419. /**
  420. * .. _numericrange:
  421. *
  422. * NumericRange
  423. * ^^^^^^^^^^^^
  424. *
  425. * NumericRanges are used to indicate subsets of a (multidimensional) array.
  426. * They no official data type in the OPC UA standard and are transmitted only
  427. * with a string encoding, such as "1:2,0:3,5". The colon separates min/max
  428. * index and the comma separates dimensions. A single value indicates a range
  429. * with a single element (min==max). */
  430. typedef struct {
  431. UA_UInt32 min;
  432. UA_UInt32 max;
  433. } UA_NumericRangeDimension;
  434. typedef struct {
  435. size_t dimensionsSize;
  436. UA_NumericRangeDimension *dimensions;
  437. } UA_NumericRange;
  438. UA_StatusCode UA_EXPORT
  439. UA_NumericRange_parseFromString(UA_NumericRange *range, const UA_String *str);
  440. /**
  441. * .. _variant:
  442. *
  443. * Variant
  444. * ^^^^^^^
  445. *
  446. * Variants may contain values of any type together with a description of the
  447. * content. See the section on :ref:`generic-types` on how types are described.
  448. * The standard mandates that variants contain built-in data types only. If the
  449. * value is not of a builtin type, it is wrapped into an :ref:`extensionobject`.
  450. * open62541 hides this wrapping transparently in the encoding layer. If the
  451. * data type is unknown to the receiver, the variant contains the original
  452. * ExtensionObject in binary or XML encoding.
  453. *
  454. * Variants may contain a scalar value or an array. For details on the handling
  455. * of arrays, see the section on :ref:`array-handling`. Array variants can have
  456. * an additional dimensionality (matrix, 3-tensor, ...) defined in an array of
  457. * dimension lengths. The actual values are kept in an array of dimensions one.
  458. * For users who work with higher-dimensions arrays directly, keep in mind that
  459. * dimensions of higher rank are serialized first (the highest rank dimension
  460. * has stride 1 and elements follow each other directly). Usually it is simplest
  461. * to interact with higher-dimensional arrays via ``UA_NumericRange``
  462. * descriptions (see :ref:`array-handling`).
  463. *
  464. * To differentiate between scalar / array variants, the following definition is
  465. * used. ``UA_Variant_isScalar`` provides simplified access to these checks.
  466. *
  467. * - ``arrayLength == 0 && data == NULL``: undefined array of length -1
  468. * - ``arrayLength == 0 && data == UA_EMPTY_ARRAY_SENTINEL``: array of length 0
  469. * - ``arrayLength == 0 && data > UA_EMPTY_ARRAY_SENTINEL``: scalar value
  470. * - ``arrayLength > 0``: array of the given length
  471. *
  472. * Variants can also be *empty*. Then, the pointer to the type description is
  473. * ``NULL``. */
  474. /* Forward declaration. See the section on Generic Type Handling */
  475. struct UA_DataType;
  476. typedef struct UA_DataType UA_DataType;
  477. #define UA_EMPTY_ARRAY_SENTINEL ((void*)0x01)
  478. typedef enum {
  479. UA_VARIANT_DATA, /* The data has the same lifecycle as the
  480. variant */
  481. UA_VARIANT_DATA_NODELETE /* The data is "borrowed" by the variant and
  482. shall not be deleted at the end of the
  483. variant's lifecycle. */
  484. } UA_VariantStorageType;
  485. typedef struct {
  486. const UA_DataType *type; /* The data type description */
  487. UA_VariantStorageType storageType;
  488. size_t arrayLength; /* The number of elements in the data array */
  489. void *data; /* Points to the scalar or array data */
  490. size_t arrayDimensionsSize; /* The number of dimensions */
  491. UA_UInt32 *arrayDimensions; /* The length of each dimension */
  492. } UA_Variant;
  493. /* Returns true if the variant has no value defined (contains neither an array
  494. * nor a scalar value).
  495. *
  496. * @param v The variant
  497. * @return Is the variant empty */
  498. static UA_INLINE UA_Boolean
  499. UA_Variant_isEmpty(const UA_Variant *v) {
  500. return v->type == NULL;
  501. }
  502. /* Returns true if the variant contains a scalar value. Note that empty variants
  503. * contain an array of length -1 (undefined).
  504. *
  505. * @param v The variant
  506. * @return Does the variant contain a scalar value */
  507. static UA_INLINE UA_Boolean
  508. UA_Variant_isScalar(const UA_Variant *v) {
  509. return (v->arrayLength == 0 && v->data > UA_EMPTY_ARRAY_SENTINEL);
  510. }
  511. /* Returns true if the variant contains a scalar value of the given type.
  512. *
  513. * @param v The variant
  514. * @param type The data type
  515. * @return Does the variant contain a scalar value of the given type */
  516. static UA_INLINE UA_Boolean
  517. UA_Variant_hasScalarType(const UA_Variant *v, const UA_DataType *type) {
  518. return UA_Variant_isScalar(v) && type == v->type;
  519. }
  520. /* Returns true if the variant contains an array of the given type.
  521. *
  522. * @param v The variant
  523. * @param type The data type
  524. * @return Does the variant contain an array of the given type */
  525. static UA_INLINE UA_Boolean
  526. UA_Variant_hasArrayType(const UA_Variant *v, const UA_DataType *type) {
  527. return (!UA_Variant_isScalar(v)) && type == v->type;
  528. }
  529. /* Set the variant to a scalar value that already resides in memory. The value
  530. * takes on the lifecycle of the variant and is deleted with it.
  531. *
  532. * @param v The variant
  533. * @param p A pointer to the value data
  534. * @param type The datatype of the value in question */
  535. void UA_EXPORT
  536. UA_Variant_setScalar(UA_Variant *v, void * UA_RESTRICT p,
  537. const UA_DataType *type);
  538. /* Set the variant to a scalar value that is copied from an existing variable.
  539. * @param v The variant
  540. * @param p A pointer to the value data
  541. * @param type The datatype of the value
  542. * @return Indicates whether the operation succeeded or returns an error code */
  543. UA_StatusCode UA_EXPORT
  544. UA_Variant_setScalarCopy(UA_Variant *v, const void *p,
  545. const UA_DataType *type);
  546. /* Set the variant to an array that already resides in memory. The array takes
  547. * on the lifecycle of the variant and is deleted with it.
  548. *
  549. * @param v The variant
  550. * @param array A pointer to the array data
  551. * @param arraySize The size of the array
  552. * @param type The datatype of the array */
  553. void UA_EXPORT
  554. UA_Variant_setArray(UA_Variant *v, void * UA_RESTRICT array,
  555. size_t arraySize, const UA_DataType *type);
  556. /* Set the variant to an array that is copied from an existing array.
  557. *
  558. * @param v The variant
  559. * @param array A pointer to the array data
  560. * @param arraySize The size of the array
  561. * @param type The datatype of the array
  562. * @return Indicates whether the operation succeeded or returns an error code */
  563. UA_StatusCode UA_EXPORT
  564. UA_Variant_setArrayCopy(UA_Variant *v, const void *array,
  565. size_t arraySize, const UA_DataType *type);
  566. /* Copy the variant, but use only a subset of the (multidimensional) array into
  567. * a variant. Returns an error code if the variant is not an array or if the
  568. * indicated range does not fit.
  569. *
  570. * @param src The source variant
  571. * @param dst The target variant
  572. * @param range The range of the copied data
  573. * @return Returns UA_STATUSCODE_GOOD or an error code */
  574. UA_StatusCode UA_EXPORT
  575. UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst,
  576. const UA_NumericRange range);
  577. /* Insert a range of data into an existing variant. The data array can't be
  578. * reused afterwards if it contains types without a fixed size (e.g. strings)
  579. * since the members are moved into the variant and take on its lifecycle.
  580. *
  581. * @param v The variant
  582. * @param dataArray The data array. The type must match the variant
  583. * @param dataArraySize The length of the data array. This is checked to match
  584. * the range size.
  585. * @param range The range of where the new data is inserted
  586. * @return Returns UA_STATUSCODE_GOOD or an error code */
  587. UA_StatusCode UA_EXPORT
  588. UA_Variant_setRange(UA_Variant *v, void * UA_RESTRICT array,
  589. size_t arraySize, const UA_NumericRange range);
  590. /* Deep-copy a range of data into an existing variant.
  591. *
  592. * @param v The variant
  593. * @param dataArray The data array. The type must match the variant
  594. * @param dataArraySize The length of the data array. This is checked to match
  595. * the range size.
  596. * @param range The range of where the new data is inserted
  597. * @return Returns UA_STATUSCODE_GOOD or an error code */
  598. UA_StatusCode UA_EXPORT
  599. UA_Variant_setRangeCopy(UA_Variant *v, const void *array,
  600. size_t arraySize, const UA_NumericRange range);
  601. /**
  602. * .. _extensionobject:
  603. *
  604. * ExtensionObject
  605. * ^^^^^^^^^^^^^^^
  606. *
  607. * ExtensionObjects may contain scalars of any data type. Even those that are
  608. * unknown to the receiver. See the section on :ref:`generic-types` on how types
  609. * are described. If the received data type is unknown, the encoded string and
  610. * target NodeId is stored instead of the decoded value. */
  611. typedef enum {
  612. UA_EXTENSIONOBJECT_ENCODED_NOBODY = 0,
  613. UA_EXTENSIONOBJECT_ENCODED_BYTESTRING = 1,
  614. UA_EXTENSIONOBJECT_ENCODED_XML = 2,
  615. UA_EXTENSIONOBJECT_DECODED = 3,
  616. UA_EXTENSIONOBJECT_DECODED_NODELETE = 4 /* Don't delete the content
  617. together with the
  618. ExtensionObject */
  619. } UA_ExtensionObjectEncoding;
  620. typedef struct {
  621. UA_ExtensionObjectEncoding encoding;
  622. union {
  623. struct {
  624. UA_NodeId typeId; /* The nodeid of the datatype */
  625. UA_ByteString body; /* The bytestring of the encoded data */
  626. } encoded;
  627. struct {
  628. const UA_DataType *type;
  629. void *data;
  630. } decoded;
  631. } content;
  632. } UA_ExtensionObject;
  633. /**
  634. * .. _datavalue:
  635. *
  636. * DataValue
  637. * ^^^^^^^^^
  638. * A data value with an associated status code and timestamps. */
  639. typedef struct {
  640. UA_Variant value;
  641. UA_DateTime sourceTimestamp;
  642. UA_DateTime serverTimestamp;
  643. UA_UInt16 sourcePicoseconds;
  644. UA_UInt16 serverPicoseconds;
  645. UA_StatusCode status;
  646. UA_Boolean hasValue : 1;
  647. UA_Boolean hasStatus : 1;
  648. UA_Boolean hasSourceTimestamp : 1;
  649. UA_Boolean hasServerTimestamp : 1;
  650. UA_Boolean hasSourcePicoseconds : 1;
  651. UA_Boolean hasServerPicoseconds : 1;
  652. } UA_DataValue;
  653. /**
  654. * DiagnosticInfo
  655. * ^^^^^^^^^^^^^^
  656. * A structure that contains detailed error and diagnostic information
  657. * associated with a StatusCode. */
  658. typedef struct UA_DiagnosticInfo {
  659. UA_Boolean hasSymbolicId : 1;
  660. UA_Boolean hasNamespaceUri : 1;
  661. UA_Boolean hasLocalizedText : 1;
  662. UA_Boolean hasLocale : 1;
  663. UA_Boolean hasAdditionalInfo : 1;
  664. UA_Boolean hasInnerStatusCode : 1;
  665. UA_Boolean hasInnerDiagnosticInfo : 1;
  666. UA_Int32 symbolicId;
  667. UA_Int32 namespaceUri;
  668. UA_Int32 localizedText;
  669. UA_Int32 locale;
  670. UA_String additionalInfo;
  671. UA_StatusCode innerStatusCode;
  672. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  673. } UA_DiagnosticInfo;
  674. /**
  675. * .. _generic-types:
  676. *
  677. * Generic Type Handling
  678. * ---------------------
  679. *
  680. * All information about a (builtin/structured) data type is stored in a
  681. * ``UA_DataType``. The array ``UA_TYPES`` contains the description of all
  682. * standard-defined types. This type description is used for the following
  683. * generic operations that work on all types:
  684. *
  685. * - ``void T_init(T *ptr)``: Initialize the data type. This is synonymous with
  686. * zeroing out the memory, i.e. ``memset(ptr, 0, sizeof(T))``.
  687. * - ``T* T_new()``: Allocate and return the memory for the data type. The
  688. * value is already initialized.
  689. * - ``UA_StatusCode T_copy(const T *src, T *dst)``: Copy the content of the
  690. * data type. Returns ``UA_STATUSCODE_GOOD`` or
  691. * ``UA_STATUSCODE_BADOUTOFMEMORY``.
  692. * - ``void T_clear(T *ptr)``: Delete the dynamically allocated content
  693. * of the data type and perform a ``T_init`` to reset the type.
  694. * - ``void T_delete(T *ptr)``: Delete the content of the data type and the
  695. * memory for the data type itself.
  696. *
  697. * Specializations, such as ``UA_Int32_new()`` are derived from the generic
  698. * type operations as static inline functions. */
  699. typedef struct {
  700. #ifdef UA_ENABLE_TYPENAMES
  701. const char *memberName;
  702. #endif
  703. UA_UInt16 memberTypeIndex; /* Index of the member in the array of data
  704. types */
  705. UA_Byte padding; /* How much padding is there before this
  706. member element? For arrays this is the
  707. padding before the size_t length member.
  708. (No padding between size_t and the
  709. following ptr.) */
  710. UA_Boolean namespaceZero : 1; /* The type of the member is defined in
  711. namespace zero. In this implementation,
  712. types from custom namespace may contain
  713. members from the same namespace or
  714. namespace zero only.*/
  715. UA_Boolean isArray : 1; /* The member is an array */
  716. } UA_DataTypeMember;
  717. /* The DataType "kind" is an internal type classification. It is used to
  718. * dispatch handling to the correct routines. */
  719. #define UA_DATATYPEKINDS 31
  720. typedef enum {
  721. UA_DATATYPEKIND_BOOLEAN = 0,
  722. UA_DATATYPEKIND_SBYTE = 1,
  723. UA_DATATYPEKIND_BYTE = 2,
  724. UA_DATATYPEKIND_INT16 = 3,
  725. UA_DATATYPEKIND_UINT16 = 4,
  726. UA_DATATYPEKIND_INT32 = 5,
  727. UA_DATATYPEKIND_UINT32 = 6,
  728. UA_DATATYPEKIND_INT64 = 7,
  729. UA_DATATYPEKIND_UINT64 = 8,
  730. UA_DATATYPEKIND_FLOAT = 9,
  731. UA_DATATYPEKIND_DOUBLE = 10,
  732. UA_DATATYPEKIND_STRING = 11,
  733. UA_DATATYPEKIND_DATETIME = 12,
  734. UA_DATATYPEKIND_GUID = 13,
  735. UA_DATATYPEKIND_BYTESTRING = 14,
  736. UA_DATATYPEKIND_XMLELEMENT = 15,
  737. UA_DATATYPEKIND_NODEID = 16,
  738. UA_DATATYPEKIND_EXPANDEDNODEID = 17,
  739. UA_DATATYPEKIND_STATUSCODE = 18,
  740. UA_DATATYPEKIND_QUALIFIEDNAME = 19,
  741. UA_DATATYPEKIND_LOCALIZEDTEXT = 20,
  742. UA_DATATYPEKIND_EXTENSIONOBJECT = 21,
  743. UA_DATATYPEKIND_DATAVALUE = 22,
  744. UA_DATATYPEKIND_VARIANT = 23,
  745. UA_DATATYPEKIND_DIAGNOSTICINFO = 24,
  746. UA_DATATYPEKIND_DECIMAL = 25,
  747. UA_DATATYPEKIND_ENUM = 26,
  748. UA_DATATYPEKIND_STRUCTURE = 27,
  749. UA_DATATYPEKIND_OPTSTRUCT = 28, /* struct with optional fields */
  750. UA_DATATYPEKIND_UNION = 29,
  751. UA_DATATYPEKIND_BITFIELDCLUSTER = 30 /* bitfields + padding */
  752. } UA_DataTypeKind;
  753. struct UA_DataType {
  754. #ifdef UA_ENABLE_TYPENAMES
  755. const char *typeName;
  756. #endif
  757. UA_NodeId typeId; /* The nodeid of the type */
  758. UA_UInt16 memSize; /* Size of the struct in memory */
  759. UA_UInt16 typeIndex; /* Index of the type in the datatypetable */
  760. UA_UInt32 typeKind : 6; /* Dispatch index for the handling routines */
  761. UA_UInt32 pointerFree : 1; /* The type (and its members) contains no
  762. * pointers that need to be freed */
  763. UA_UInt32 overlayable : 1; /* The type has the identical memory layout
  764. * in memory and on the binary stream. */
  765. UA_UInt32 membersSize : 8; /* How many members does the type have? */
  766. UA_UInt32 binaryEncodingId : 16; /* NodeId of datatype when encoded as binary */
  767. //UA_UInt16 xmlEncodingId; /* NodeId of datatype when encoded as XML */
  768. UA_DataTypeMember *members;
  769. };
  770. /* Test if the data type is a numeric builtin data type. This includes Boolean,
  771. * integers and floating point numbers. Not included are DateTime and
  772. * StatusCode. */
  773. UA_Boolean
  774. UA_DataType_isNumeric(const UA_DataType *type);
  775. /**
  776. * Builtin data types can be accessed as UA_TYPES[UA_TYPES_XXX], where XXX is
  777. * the name of the data type. If only the NodeId of a type is known, use the
  778. * following method to retrieve the data type description. */
  779. /* Returns the data type description for the type's identifier or NULL if no
  780. * matching data type was found. */
  781. const UA_DataType UA_EXPORT *
  782. UA_findDataType(const UA_NodeId *typeId);
  783. /** The following functions are used for generic handling of data types. */
  784. /* Allocates and initializes a variable of type dataType
  785. *
  786. * @param type The datatype description
  787. * @return Returns the memory location of the variable or NULL if no
  788. * memory could be allocated */
  789. void UA_EXPORT * UA_new(const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  790. /* Initializes a variable to default values
  791. *
  792. * @param p The memory location of the variable
  793. * @param type The datatype description */
  794. static UA_INLINE void
  795. UA_init(void *p, const UA_DataType *type) {
  796. memset(p, 0, type->memSize);
  797. }
  798. /* Copies the content of two variables. If copying fails (e.g. because no memory
  799. * was available for an array), then dst is emptied and initialized to prevent
  800. * memory leaks.
  801. *
  802. * @param src The memory location of the source variable
  803. * @param dst The memory location of the destination variable
  804. * @param type The datatype description
  805. * @return Indicates whether the operation succeeded or returns an error code */
  806. UA_StatusCode UA_EXPORT
  807. UA_copy(const void *src, void *dst, const UA_DataType *type);
  808. /* Deletes the dynamically allocated content of a variable (e.g. resets all
  809. * arrays to undefined arrays). Afterwards, the variable can be safely deleted
  810. * without causing memory leaks. But the variable is not initialized and may
  811. * contain old data that is not memory-relevant.
  812. *
  813. * @param p The memory location of the variable
  814. * @param type The datatype description of the variable */
  815. void UA_EXPORT UA_clear(void *p, const UA_DataType *type);
  816. #define UA_deleteMembers(p, type) UA_clear(p, type)
  817. /* Frees a variable and all of its content.
  818. *
  819. * @param p The memory location of the variable
  820. * @param type The datatype description of the variable */
  821. void UA_EXPORT UA_delete(void *p, const UA_DataType *type);
  822. /**
  823. * .. _array-handling:
  824. *
  825. * Array handling
  826. * --------------
  827. * In OPC UA, arrays can have a length of zero or more with the usual meaning.
  828. * In addition, arrays can be undefined. Then, they don't even have a length. In
  829. * the binary encoding, this is indicated by an array of length -1.
  830. *
  831. * In open62541 however, we use ``size_t`` for array lengths. An undefined array
  832. * has length 0 and the data pointer is ``NULL``. An array of length 0 also has
  833. * length 0 but a data pointer ``UA_EMPTY_ARRAY_SENTINEL``. */
  834. /* Allocates and initializes an array of variables of a specific type
  835. *
  836. * @param size The requested array length
  837. * @param type The datatype description
  838. * @return Returns the memory location of the variable or NULL if no memory
  839. could be allocated */
  840. void UA_EXPORT *
  841. UA_Array_new(size_t size, const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  842. /* Allocates and copies an array
  843. *
  844. * @param src The memory location of the source array
  845. * @param size The size of the array
  846. * @param dst The location of the pointer to the new array
  847. * @param type The datatype of the array members
  848. * @return Returns UA_STATUSCODE_GOOD or UA_STATUSCODE_BADOUTOFMEMORY */
  849. UA_StatusCode UA_EXPORT
  850. UA_Array_copy(const void *src, size_t size, void **dst,
  851. const UA_DataType *type) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  852. /* Deletes an array.
  853. *
  854. * @param p The memory location of the array
  855. * @param size The size of the array
  856. * @param type The datatype of the array members */
  857. void UA_EXPORT UA_Array_delete(void *p, size_t size, const UA_DataType *type);
  858. /**
  859. * Random Number Generator
  860. * -----------------------
  861. * If UA_ENABLE_MULTITHREADING is defined, then the seed is stored in thread
  862. * local storage. The seed is initialized for every thread in the
  863. * server/client. */
  864. void UA_EXPORT UA_random_seed(UA_UInt64 seed);
  865. UA_UInt32 UA_EXPORT UA_UInt32_random(void); /* no cryptographic entropy */
  866. UA_Guid UA_EXPORT UA_Guid_random(void); /* no cryptographic entropy */
  867. /**
  868. * .. _generated-types:
  869. *
  870. * Generated Data Type Definitions
  871. * -------------------------------
  872. *
  873. * The following data types were auto-generated from a definition in XML format.
  874. */
  875. /* The following is used to exclude type names in the definition of UA_DataType
  876. * structures if the feature is disabled. */
  877. #ifdef UA_ENABLE_TYPENAMES
  878. # define UA_TYPENAME(name) name,
  879. #else
  880. # define UA_TYPENAME(name)
  881. #endif
  882. /* Datatype arrays with custom type definitions can be added in a linked list to
  883. * the client or server configuration. Datatype members can point to types in
  884. * the same array via the ``memberTypeIndex``. If ``namespaceZero`` is set to
  885. * true, the member datatype is looked up in the array of builtin datatypes
  886. * instead. */
  887. typedef struct UA_DataTypeArray {
  888. const struct UA_DataTypeArray *next;
  889. const size_t typesSize;
  890. const UA_DataType *types;
  891. } UA_DataTypeArray;
  892. /**
  893. *
  894. * .. toctree::
  895. *
  896. * types_generated */
  897. _UA_END_DECLS
  898. #endif /* UA_TYPES_H_ */