ua_types.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (C) 2013-2015 the contributors as stated in the AUTHORS file
  3. *
  4. * This file is part of open62541. open62541 is free software: you can
  5. * redistribute it and/or modify it under the terms of the GNU Lesser General
  6. * Public License, version 3 (as published by the Free Software Foundation) with
  7. * a static linking exception as stated in the LICENSE file provided with
  8. * open62541.
  9. *
  10. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. * details.
  14. */
  15. #ifndef UA_TYPES_H_
  16. #define UA_TYPES_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include "ua_config.h"
  21. #include "ua_constants.h"
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. /**
  25. * Data Types
  26. * ==========
  27. *
  28. * In open62541, all data types share the same basic API for creation, copying
  29. * and deletion. The header ua_types.h defines the builtin types. In addition,
  30. * we auto-generate ua_types_generated.h with additional types as well as the
  31. * following function definitions for all (builtin and generated) data types
  32. * ``T``.
  33. *
  34. * ``void T_init(T *ptr)``
  35. * Initialize the data type. This is synonymous with zeroing out the memory,
  36. * i.e. ``memset(dataptr, 0, sizeof(T))``.
  37. * ``T* T_new()``
  38. * Allocate and return the memory for the data type. The memory is already
  39. * initialized.
  40. * ``UA_StatusCode T_copy(const T *src, T *dst)``
  41. * Copy the content of the data type. Returns ``UA_STATUSCODE_GOOD`` or
  42. * ``UA_STATUSCODE_BADOUTOFMEMORY``.
  43. * ``void T_deleteMembers(T *ptr)``
  44. * Delete the dynamically allocated content of the data type and perform a
  45. * ``T_init`` to reset the type.
  46. * ``void T_delete(T *ptr)``
  47. * Delete the content of the data type and the memory for the data type itself.
  48. *
  49. * OPC UA defines 25 builtin data types. All other data types are combinations
  50. * of the 25 builtin data types. */
  51. #define UA_BUILTIN_TYPES_COUNT 25U
  52. /**
  53. * Builtin Types Part 1
  54. * --------------------
  55. *
  56. * Boolean
  57. * ^^^^^^^
  58. * A two-state logical value (true or false). */
  59. typedef bool UA_Boolean;
  60. #define UA_TRUE true
  61. #define UA_FALSE false
  62. /**
  63. * SByte
  64. * ^^^^^
  65. * An integer value between -128 and 127. */
  66. typedef int8_t UA_SByte;
  67. #define UA_SBYTE_MAX 127
  68. #define UA_SBYTE_MIN (-128)
  69. /**
  70. * Byte
  71. * ^^^^
  72. * An integer value between 0 and 256. */
  73. typedef uint8_t UA_Byte;
  74. #define UA_BYTE_MAX 256
  75. #define UA_BYTE_MIN 0
  76. /**
  77. * Int16
  78. * ^^^^^
  79. * An integer value between -32 768 and 32 767. */
  80. typedef int16_t UA_Int16;
  81. #define UA_INT16_MAX 32767
  82. #define UA_INT16_MIN (-32768)
  83. /**
  84. * UInt16
  85. * ^^^^^^
  86. * An integer value between 0 and 65 535. */
  87. typedef uint16_t UA_UInt16;
  88. #define UA_UINT16_MAX 65535
  89. #define UA_UINT16_MIN 0
  90. /**
  91. * Int32
  92. * ^^^^^
  93. * An integer value between -2 147 483 648 and 2 147 483 647. */
  94. typedef int32_t UA_Int32;
  95. #define UA_INT32_MAX 2147483647
  96. #define UA_INT32_MIN (-2147483648)
  97. /**
  98. * UInt32
  99. * ^^^^^^
  100. * An integer value between 0 and 4 294 967 295. */
  101. typedef uint32_t UA_UInt32;
  102. #define UA_UINT32_MAX 4294967295
  103. #define UA_UINT32_MIN 0
  104. /**
  105. * Int64
  106. * ^^^^^
  107. * An integer value between -10 223 372 036 854 775 808 and
  108. * 9 223 372 036 854 775 807. */
  109. typedef int64_t UA_Int64;
  110. #define UA_INT64_MAX (int64_t)9223372036854775807
  111. #define UA_INT64_MIN ((int64_t)-9223372036854775808)
  112. /**
  113. * UInt64
  114. * ^^^^^^
  115. * An integer value between 0 and 18 446 744 073 709 551 615. */
  116. typedef uint64_t UA_UInt64;
  117. #define UA_UINT64_MAX (int64_t)18446744073709551615
  118. #define UA_UINT64_MIN (int64_t)0
  119. /**
  120. * Float
  121. * ^^^^^
  122. * An IEEE single precision (32 bit) floating point value. */
  123. typedef float UA_Float;
  124. /**
  125. * Double
  126. * ^^^^^^
  127. * An IEEE double precision (64 bit) floating point value. */
  128. typedef double UA_Double;
  129. /**
  130. * .. _statuscode:
  131. *
  132. * StatusCode
  133. * ^^^^^^^^^^
  134. * A numeric identifier for a error or condition that is associated with a value
  135. * or an operation. See the section :ref:`statuscodes` for the meaning of a
  136. * specific code. */
  137. typedef uint32_t UA_StatusCode;
  138. /**
  139. * Array handling
  140. * --------------
  141. * In OPC UA, arrays can have a length of zero or more with the usual meaning.
  142. * In addition, arrays can be undefined. Then, they don't even have a length. In
  143. * the binary encoding, this is indicated by an array of length -1.
  144. *
  145. * In open62541 however, we use ``size_t`` for array lengths. An undefined array
  146. * has length 0 and the data pointer is NULL. An array of length 0 also has
  147. * length 0 but points to a sentinel memory address. */
  148. #define UA_EMPTY_ARRAY_SENTINEL ((void*)0x01)
  149. /** Forward Declaration of UA_DataType. See Section `Generic Type Handling`_
  150. for details. */
  151. struct UA_DataType;
  152. typedef struct UA_DataType UA_DataType;
  153. /** The following functions are used for handling arrays of any data type. */
  154. /* Allocates and initializes an array of variables of a specific type
  155. *
  156. * @param size The requested array length
  157. * @param type The datatype description
  158. * @return Returns the memory location of the variable or (void*)0 if no memory
  159. could be allocated */
  160. void UA_EXPORT * UA_Array_new(size_t size, const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  161. /* Allocates and copies an array
  162. *
  163. * @param src The memory location of the source array
  164. * @param size The size of the array
  165. * @param dst The location of the pointer to the new array
  166. * @param type The datatype of the array members
  167. * @return Returns UA_STATUSCODE_GOOD or UA_STATUSCODE_BADOUTOFMEMORY */
  168. UA_StatusCode UA_EXPORT
  169. UA_Array_copy(const void *src, size_t size, void **dst,
  170. const UA_DataType *type) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  171. /* Deletes an array.
  172. *
  173. * @param p The memory location of the array
  174. * @param size The size of the array
  175. * @param type The datatype of the array members */
  176. void UA_EXPORT UA_Array_delete(void *p, size_t size, const UA_DataType *type);
  177. /**
  178. * .. _numericrange:
  179. *
  180. * NumericRange
  181. * ^^^^^^^^^^^^
  182. *
  183. * NumericRanges are used to indicate subsets of a (multidimensional) variant
  184. * array. NumericRange has no official type structure in the standard. On the
  185. * wire, it only exists as an encoded string, such as "1:2,0:3,5". The colon
  186. * separates min/max index and the comma separates dimensions. A single value
  187. * indicates a range with a single element (min==max). */
  188. typedef struct {
  189. size_t dimensionsSize;
  190. struct UA_NumericRangeDimension {
  191. UA_UInt32 min;
  192. UA_UInt32 max;
  193. } *dimensions;
  194. } UA_NumericRange;
  195. /**
  196. * Builtin Types, Part 2
  197. * ---------------------
  198. *
  199. * String
  200. * ^^^^^^
  201. * A sequence of Unicode characters. Strings are just an array of UA_Byte. */
  202. typedef struct {
  203. size_t length; /* The length of the string */
  204. UA_Byte *data; /* The content (not null-terminated) */
  205. } UA_String;
  206. /* Copies the content on the heap. Returns a null-string when alloc fails */
  207. UA_String UA_EXPORT UA_String_fromChars(char const src[]) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  208. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *s1, const UA_String *s2);
  209. UA_EXPORT extern const UA_String UA_STRING_NULL;
  210. /**
  211. * ``UA_STRING`` returns a string pointing to the preallocated char-array.
  212. * ``UA_STRING_ALLOC`` is shorthand for ``UA_String_fromChars`` and makes a copy
  213. * of the char-array. */
  214. static UA_INLINE UA_String
  215. UA_STRING(char *chars) {
  216. UA_String str; str.length = strlen(chars);
  217. str.data = (UA_Byte*)chars; return str;
  218. }
  219. #define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
  220. /**
  221. * DateTime
  222. * ^^^^^^^^
  223. * An instance in time. A DateTime value is encoded as a 64-bit signed integer
  224. * which represents the number of 100 nanosecond intervals since January 1, 1601
  225. * (UTC). */
  226. typedef int64_t UA_DateTime;
  227. /* Multiply to convert units for time difference computations */
  228. #define UA_USEC_TO_DATETIME 10LL
  229. #define UA_MSEC_TO_DATETIME (UA_USEC_TO_DATETIME * 1000LL)
  230. #define UA_SEC_TO_DATETIME (UA_MSEC_TO_DATETIME * 1000LL)
  231. /* Datetime of 1 Jan 1970 00:00 UTC */
  232. #define UA_DATETIME_UNIX_EPOCH (11644473600LL * UA_SEC_TO_DATETIME)
  233. /* The current time */
  234. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  235. /* CPU clock invariant to system time changes. Use only for time diffs, not
  236. * current time */
  237. UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void);
  238. typedef struct UA_DateTimeStruct {
  239. UA_UInt16 nanoSec;
  240. UA_UInt16 microSec;
  241. UA_UInt16 milliSec;
  242. UA_UInt16 sec;
  243. UA_UInt16 min;
  244. UA_UInt16 hour;
  245. UA_UInt16 day;
  246. UA_UInt16 month;
  247. UA_UInt16 year;
  248. } UA_DateTimeStruct;
  249. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime t);
  250. UA_String UA_EXPORT UA_DateTime_toString(UA_DateTime t);
  251. /**
  252. * Guid
  253. * ^^^^
  254. * A 16 byte value that can be used as a globally unique identifier. */
  255. typedef struct {
  256. UA_UInt32 data1;
  257. UA_UInt16 data2;
  258. UA_UInt16 data3;
  259. UA_Byte data4[8];
  260. } UA_Guid;
  261. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  262. UA_EXPORT extern const UA_Guid UA_GUID_NULL;
  263. /**
  264. * ByteString
  265. * ^^^^^^^^^^
  266. * A sequence of octets. */
  267. typedef UA_String UA_ByteString;
  268. static UA_INLINE UA_Boolean
  269. UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2) {
  270. return UA_String_equal((const UA_String*)string1, (const UA_String*)string2);
  271. }
  272. /* Allocates memory of size length for the bytestring.
  273. * The content is not set to zero. */
  274. UA_StatusCode UA_EXPORT
  275. UA_ByteString_allocBuffer(UA_ByteString *bs, size_t length);
  276. UA_EXPORT extern const UA_ByteString UA_BYTESTRING_NULL;
  277. static UA_INLINE UA_ByteString
  278. UA_BYTESTRING(char *chars) {
  279. UA_ByteString str; str.length = strlen(chars);
  280. str.data = (UA_Byte*)chars; return str;
  281. }
  282. static UA_INLINE UA_ByteString
  283. UA_BYTESTRING_ALLOC(const char *chars) {
  284. UA_String str = UA_String_fromChars(chars); UA_ByteString bstr;
  285. bstr.length = str.length; bstr.data = str.data; return bstr;
  286. }
  287. /**
  288. * XmlElement
  289. * ^^^^^^^^^^
  290. * An XML element. */
  291. typedef UA_String UA_XmlElement;
  292. /**
  293. * NodeId
  294. * ^^^^^^
  295. * An identifier for a node in the address space of an OPC UA Server. */
  296. enum UA_NodeIdType {
  297. UA_NODEIDTYPE_NUMERIC = 0, /* In the binary encoding, this can also
  298. become 1 or 2 (2byte and 4byte encoding of
  299. small numeric nodeids) */
  300. UA_NODEIDTYPE_STRING = 3,
  301. UA_NODEIDTYPE_GUID = 4,
  302. UA_NODEIDTYPE_BYTESTRING = 5
  303. };
  304. typedef struct {
  305. UA_UInt16 namespaceIndex;
  306. enum UA_NodeIdType identifierType;
  307. union {
  308. UA_UInt32 numeric;
  309. UA_String string;
  310. UA_Guid guid;
  311. UA_ByteString byteString;
  312. } identifier;
  313. } UA_NodeId;
  314. UA_EXPORT extern const UA_NodeId UA_NODEID_NULL;
  315. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  316. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  317. /** The following functions are shorthand for creating NodeIds. */
  318. static UA_INLINE UA_NodeId
  319. UA_NODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  320. UA_NodeId id; id.namespaceIndex = nsIndex;
  321. id.identifierType = UA_NODEIDTYPE_NUMERIC;
  322. id.identifier.numeric = identifier; return id;
  323. }
  324. static UA_INLINE UA_NodeId
  325. UA_NODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  326. UA_NodeId id; id.namespaceIndex = nsIndex;
  327. id.identifierType = UA_NODEIDTYPE_STRING;
  328. id.identifier.string = UA_STRING(chars); return id;
  329. }
  330. static UA_INLINE UA_NodeId
  331. UA_NODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  332. UA_NodeId id; id.namespaceIndex = nsIndex;
  333. id.identifierType = UA_NODEIDTYPE_STRING;
  334. id.identifier.string = UA_STRING_ALLOC(chars); return id;
  335. }
  336. static UA_INLINE UA_NodeId
  337. UA_NODEID_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  338. UA_NodeId id; id.namespaceIndex = nsIndex;
  339. id.identifierType = UA_NODEIDTYPE_GUID;
  340. id.identifier.guid = guid; return id;
  341. }
  342. static UA_INLINE UA_NodeId
  343. UA_NODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  344. UA_NodeId id; id.namespaceIndex = nsIndex;
  345. id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  346. id.identifier.byteString = UA_BYTESTRING(chars); return id;
  347. }
  348. static UA_INLINE UA_NodeId
  349. UA_NODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  350. UA_NodeId id; id.namespaceIndex = nsIndex;
  351. id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  352. id.identifier.byteString = UA_BYTESTRING_ALLOC(chars); return id;
  353. }
  354. /**
  355. * ExpandedNodeId
  356. * ^^^^^^^^^^^^^^
  357. * A NodeId that allows the namespace URI to be specified instead of an index. */
  358. typedef struct {
  359. UA_NodeId nodeId;
  360. UA_String namespaceUri;
  361. UA_UInt32 serverIndex;
  362. } UA_ExpandedNodeId;
  363. /** The following functions are shorthand for creating ExpandedNodeIds. */
  364. static UA_INLINE UA_ExpandedNodeId
  365. UA_EXPANDEDNODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  366. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_NUMERIC(nsIndex, identifier);
  367. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  368. }
  369. static UA_INLINE UA_ExpandedNodeId
  370. UA_EXPANDEDNODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  371. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING(nsIndex, chars);
  372. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  373. }
  374. static UA_INLINE UA_ExpandedNodeId
  375. UA_EXPANDEDNODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  376. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING_ALLOC(nsIndex, chars);
  377. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  378. }
  379. static UA_INLINE UA_ExpandedNodeId
  380. UA_EXPANDEDNODEID_STRING_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  381. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_GUID(nsIndex, guid);
  382. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  383. }
  384. static UA_INLINE UA_ExpandedNodeId
  385. UA_EXPANDEDNODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  386. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING(nsIndex, chars);
  387. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  388. }
  389. static UA_INLINE UA_ExpandedNodeId
  390. UA_EXPANDEDNODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  391. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING_ALLOC(nsIndex, chars);
  392. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  393. }
  394. /**
  395. * QualifiedName
  396. * ^^^^^^^^^^^^^
  397. * A name qualified by a namespace. */
  398. typedef struct {
  399. UA_UInt16 namespaceIndex;
  400. UA_String name;
  401. } UA_QualifiedName;
  402. static UA_INLINE UA_Boolean
  403. UA_QualifiedName_isNull(const UA_QualifiedName *q) {
  404. return (q->namespaceIndex == 0 && q->name.length == 0);
  405. }
  406. static UA_INLINE UA_QualifiedName
  407. UA_QUALIFIEDNAME(UA_UInt16 nsIndex, char *chars) {
  408. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  409. qn.name = UA_STRING(chars); return qn;
  410. }
  411. static UA_INLINE UA_QualifiedName
  412. UA_QUALIFIEDNAME_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  413. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  414. qn.name = UA_STRING_ALLOC(chars); return qn;
  415. }
  416. /**
  417. * LocalizedText
  418. * ^^^^^^^^^^^^^
  419. * Human readable text with an optional locale identifier. */
  420. typedef struct {
  421. UA_String locale;
  422. UA_String text;
  423. } UA_LocalizedText;
  424. static UA_INLINE UA_LocalizedText
  425. UA_LOCALIZEDTEXT(char *locale, char *text) {
  426. UA_LocalizedText lt; lt.locale = UA_STRING(locale);
  427. lt.text = UA_STRING(text); return lt;
  428. }
  429. static UA_INLINE UA_LocalizedText
  430. UA_LOCALIZEDTEXT_ALLOC(const char *locale, const char *text) {
  431. UA_LocalizedText lt; lt.locale = UA_STRING_ALLOC(locale);
  432. lt.text = UA_STRING_ALLOC(text); return lt;
  433. }
  434. /**
  435. * ExtensionObject
  436. * ^^^^^^^^^^^^^^^
  437. * ExtensionObjects may contain scalars of any data type. Even those that are
  438. * unknown to the receiver. See the Section `Generic Type Handling`_ on how
  439. * types are described. An ExtensionObject always contains the NodeId of the
  440. * Data Type. If the data cannot be decoded, we keep the encoded string and the
  441. * NodeId. */
  442. typedef struct {
  443. enum {
  444. UA_EXTENSIONOBJECT_ENCODED_NOBODY = 0,
  445. UA_EXTENSIONOBJECT_ENCODED_BYTESTRING = 1,
  446. UA_EXTENSIONOBJECT_ENCODED_XML = 2,
  447. UA_EXTENSIONOBJECT_DECODED = 3,
  448. UA_EXTENSIONOBJECT_DECODED_NODELETE = 4 /* Don't delete the content
  449. together with the
  450. ExtensionObject */
  451. } encoding;
  452. union {
  453. struct {
  454. UA_NodeId typeId; /* The nodeid of the datatype */
  455. UA_ByteString body; /* The bytestring of the encoded data */
  456. } encoded;
  457. struct {
  458. const UA_DataType *type;
  459. void *data;
  460. } decoded;
  461. } content;
  462. } UA_ExtensionObject;
  463. /**
  464. * .. _variant:
  465. *
  466. * Variant
  467. * ^^^^^^^
  468. * Variants may contain data of any type. See the Section `Generic Type
  469. * Handling`_ on how types are described. If the data is not of one of the 25
  470. * builtin types, it will be encoded as an `ExtensionObject`_ on the wire. (The
  471. * standard says that a variant is a union of the built-in types. open62541
  472. * generalizes this to any data type by transparently de- and encoding
  473. * ExtensionObjects in the background. If the decoding fails, the variant
  474. * contains the original ExtensionObject.)
  475. *
  476. * Variants can contain a single scalar or an array. For details on the handling
  477. * of arrays, see the Section `Array Handling`_. Array variants can have an
  478. * additional dimensionality (matrix, 3-tensor, ...) defined in an array of
  479. * dimension sizes. Higher rank dimensions are serialized first.
  480. *
  481. * The differentiation between variants containing a scalar, an array or no data
  482. * is as follows:
  483. *
  484. * - arrayLength == 0 && data == NULL: no existing data
  485. * - arrayLength == 0 && data == UA_EMPTY_ARRAY_SENTINEL: array of length 0
  486. * - arrayLength == 0 && data > UA_EMPTY_ARRAY_SENTINEL: scalar value
  487. * - arrayLength > 0: array of the given length */
  488. typedef struct {
  489. const UA_DataType *type; /* The data type description */
  490. enum {
  491. UA_VARIANT_DATA, /* The data has the same lifecycle as the
  492. variant */
  493. UA_VARIANT_DATA_NODELETE, /* The data is "borrowed" by the variant and
  494. shall not be deleted at the end of the
  495. variant's lifecycle. */
  496. } storageType;
  497. size_t arrayLength; /* The number of elements in the data array */
  498. void *data; /* Points to the scalar or array data */
  499. size_t arrayDimensionsSize; /* The number of dimensions the data-array has */
  500. UA_Int32 *arrayDimensions; /* The length of each dimension */
  501. } UA_Variant;
  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. /* Set the variant to a scalar value that already resides in memory. The value
  512. * takes on the lifecycle of the variant and is deleted with it.
  513. *
  514. * @param v The variant
  515. * @param p A pointer to the value data
  516. * @param type The datatype of the value in question */
  517. void UA_EXPORT
  518. UA_Variant_setScalar(UA_Variant *v, void * UA_RESTRICT p,
  519. const UA_DataType *type);
  520. /* Set the variant to a scalar value that is copied from an existing variable.
  521. * @param v The variant
  522. * @param p A pointer to the value data
  523. * @param type The datatype of the value
  524. * @return Indicates whether the operation succeeded or returns an error code */
  525. UA_StatusCode UA_EXPORT
  526. UA_Variant_setScalarCopy(UA_Variant *v, const void *p,
  527. const UA_DataType *type);
  528. /* Set the variant to an array that already resides in memory. The array takes
  529. * on the lifecycle of the variant and is deleted with it.
  530. *
  531. * @param v The variant
  532. * @param array A pointer to the array data
  533. * @param arraySize The size of the array
  534. * @param type The datatype of the array */
  535. void UA_EXPORT
  536. UA_Variant_setArray(UA_Variant *v, void * UA_RESTRICT array,
  537. size_t arraySize, const UA_DataType *type);
  538. /* Set the variant to an array that is copied from an existing array.
  539. *
  540. * @param v The variant
  541. * @param array A pointer to the array data
  542. * @param arraySize The size of the array
  543. * @param type The datatype of the array
  544. * @return Indicates whether the operation succeeded or returns an error code */
  545. UA_StatusCode UA_EXPORT
  546. UA_Variant_setArrayCopy(UA_Variant *v, const void *array,
  547. size_t arraySize, const UA_DataType *type);
  548. /* Copy the variant, but use only a subset of the (multidimensional) array into
  549. * a variant. Returns an error code if the variant is not an array or if the
  550. * indicated range does not fit.
  551. *
  552. * @param src The source variant
  553. * @param dst The target variant
  554. * @param range The range of the copied data
  555. * @return Returns UA_STATUSCODE_GOOD or an error code */
  556. UA_StatusCode UA_EXPORT
  557. UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst,
  558. const UA_NumericRange range);
  559. /* Insert a range of data into an existing variant. The data array can't be
  560. * reused afterwards if it contains types without a fixed size (e.g. strings)
  561. * since the members are moved into the variant and take on its lifecycle.
  562. *
  563. * @param v The variant
  564. * @param dataArray The data array. The type must match the variant
  565. * @param dataArraySize The length of the data array. This is checked to match
  566. * the range size.
  567. * @param range The range of where the new data is inserted
  568. * @return Returns UA_STATUSCODE_GOOD or an error code */
  569. UA_StatusCode UA_EXPORT
  570. UA_Variant_setRange(UA_Variant *v, void * UA_RESTRICT array,
  571. size_t arraySize, const UA_NumericRange range);
  572. /* Deep-copy a range of data into an existing variant.
  573. *
  574. * @param v The variant
  575. * @param dataArray The data array. The type must match the variant
  576. * @param dataArraySize The length of the data array. This is checked to match
  577. * the range size.
  578. * @param range The range of where the new data is inserted
  579. * @return Returns UA_STATUSCODE_GOOD or an error code */
  580. UA_StatusCode UA_EXPORT
  581. UA_Variant_setRangeCopy(UA_Variant *v, const void *array,
  582. size_t arraySize, const UA_NumericRange range);
  583. /**
  584. * DataValue
  585. * ^^^^^^^^^
  586. * A data value with an associated status code and timestamps. */
  587. typedef struct {
  588. UA_Boolean hasValue : 1;
  589. UA_Boolean hasStatus : 1;
  590. UA_Boolean hasSourceTimestamp : 1;
  591. UA_Boolean hasServerTimestamp : 1;
  592. UA_Boolean hasSourcePicoseconds : 1;
  593. UA_Boolean hasServerPicoseconds : 1;
  594. UA_Variant value;
  595. UA_StatusCode status;
  596. UA_DateTime sourceTimestamp;
  597. UA_UInt16 sourcePicoseconds;
  598. UA_DateTime serverTimestamp;
  599. UA_UInt16 serverPicoseconds;
  600. } UA_DataValue;
  601. /**
  602. * DiagnosticInfo
  603. * ^^^^^^^^^^^^^^
  604. * A structure that contains detailed error and diagnostic information
  605. * associated with a StatusCode. */
  606. typedef struct UA_DiagnosticInfo {
  607. UA_Boolean hasSymbolicId : 1;
  608. UA_Boolean hasNamespaceUri : 1;
  609. UA_Boolean hasLocalizedText : 1;
  610. UA_Boolean hasLocale : 1;
  611. UA_Boolean hasAdditionalInfo : 1;
  612. UA_Boolean hasInnerStatusCode : 1;
  613. UA_Boolean hasInnerDiagnosticInfo : 1;
  614. UA_Int32 symbolicId;
  615. UA_Int32 namespaceUri;
  616. UA_Int32 localizedText;
  617. UA_Int32 locale;
  618. UA_String additionalInfo;
  619. UA_StatusCode innerStatusCode;
  620. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  621. } UA_DiagnosticInfo;
  622. /**
  623. * Generic Type Handling
  624. * ---------------------
  625. * The builtin types can be combined to data structures. All information about a
  626. * (structured) data type is stored in a ``UA_DataType``. The array ``UA_TYPES``
  627. * contains the description of all standard-defined types and is used for
  628. * handling of generic types. */
  629. typedef struct {
  630. #ifdef UA_ENABLE_TYPENAMES
  631. const char *memberName;
  632. #endif
  633. UA_UInt16 memberTypeIndex; /* Index of the member in the array of data
  634. types */
  635. UA_Byte padding; /* How much padding is there before this
  636. member element? For arrays this is the
  637. padding before the size_t lenght member.
  638. (No padding between size_t and the
  639. following ptr.) */
  640. UA_Boolean namespaceZero : 1; /* The type of the member is defined in
  641. namespace zero. In this implementation,
  642. types from custom namespace may contain
  643. members from the same namespace or ns0
  644. only.*/
  645. UA_Boolean isArray : 1; /* The member is an array */
  646. } UA_DataTypeMember;
  647. struct UA_DataType {
  648. #ifdef UA_ENABLE_TYPENAMES
  649. const char *typeName;
  650. #endif
  651. UA_NodeId typeId; /* The nodeid of the type */
  652. UA_UInt16 memSize; /* Size of the struct in memory */
  653. UA_UInt16 typeIndex; /* Index of the type in the datatypetable */
  654. UA_Byte membersSize; /* How many members does the type have? */
  655. UA_Boolean builtin : 1; /* The type is "builtin" and has dedicated de-
  656. and encoding functions */
  657. UA_Boolean fixedSize : 1; /* The type (and its members) contains no
  658. pointers */
  659. UA_Boolean overlayable : 1; /* The type has the identical memory layout in
  660. memory and on the binary stream. */
  661. UA_DataTypeMember *members;
  662. };
  663. /** The following functions are used for generic handling of data types. */
  664. /* Allocates and initializes a variable of type dataType
  665. *
  666. * @param type The datatype description
  667. * @return Returns the memory location of the variable or (void*)0 if no
  668. * memory is available */
  669. void UA_EXPORT * UA_new(const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  670. /* Initializes a variable to default values
  671. *
  672. * @param p The memory location of the variable
  673. * @param type The datatype description */
  674. static UA_INLINE void
  675. UA_init(void *p, const UA_DataType *type) {
  676. memset(p, 0, type->memSize);
  677. }
  678. /* Copies the content of two variables. If copying fails (e.g. because no memory
  679. * was available for an array), then dst is emptied and initialized to prevent
  680. * memory leaks.
  681. *
  682. * @param src The memory location of the source variable
  683. * @param dst The memory location of the destination variable
  684. * @param type The datatype description
  685. * @return Indicates whether the operation succeeded or returns an error code */
  686. UA_StatusCode UA_EXPORT
  687. UA_copy(const void *src, void *dst, const UA_DataType *type);
  688. /* Deletes the dynamically allocated content of a variable (e.g. resets all
  689. * arrays to undefined arrays). Afterwards, the variable can be safely deleted
  690. * without causing memory leaks. But the variable is not initialized and may
  691. * contain old data that is not memory-relevant.
  692. *
  693. * @param p The memory location of the variable
  694. * @param type The datatype description of the variable */
  695. void UA_EXPORT UA_deleteMembers(void *p, const UA_DataType *type);
  696. /* Frees a variable and all of its content.
  697. *
  698. * @param p The memory location of the variable
  699. * @param type The datatype description of the variable */
  700. void UA_EXPORT UA_delete(void *p, const UA_DataType *type);
  701. /**
  702. * Random Number Generator
  703. * -----------------------
  704. * If UA_ENABLE_MULTITHREADING is defined, then the seed is stored in thread
  705. * local storage. The seed is initialized for every thread in the
  706. * server/client. */
  707. void UA_EXPORT UA_random_seed(UA_UInt64 seed);
  708. UA_UInt32 UA_EXPORT UA_UInt32_random(void); /* no cryptographic entropy */
  709. UA_Guid UA_EXPORT UA_Guid_random(void); /* no cryptographic entropy */
  710. #ifdef __cplusplus
  711. } // extern "C"
  712. #endif
  713. #endif /* UA_TYPES_H_ */