ua_types.h 28 KB

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