ua_types.h 27 KB

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