ua_types.h 27 KB

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