ua_types.h 28 KB

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