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`` or ``UA_STATUSCODE_BADOUTOFMEMORY``.
  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. * In OPC UA, arrays can have a length of zero or more with the usual meaning.
  136. * In addition, arrays can be undefined. Then, they don't even have a length. In
  137. * the binary encoding, this is indicated by an array of length -1.
  138. *
  139. * In open62541 however, we use ``size_t`` for array lengths. An undefined array
  140. * has length 0 and the data pointer is NULL. An array of length 0 also has
  141. * length 0 but points to a sentinel memory address. */
  142. #define UA_EMPTY_ARRAY_SENTINEL ((void*)0x01)
  143. /** Forward Declaration of UA_DataType. See Section `Generic Type Handling`_
  144. for details. */
  145. struct UA_DataType;
  146. typedef struct UA_DataType UA_DataType;
  147. /** The following functions are used for handling arrays of an data type. */
  148. /* Allocates and initializes an array of variables of a specific type
  149. *
  150. * @param size The requested array length
  151. * @param type The datatype description
  152. * @return Returns the memory location of the variable or (void*)0 if no memory
  153. could be allocated */
  154. void UA_EXPORT * UA_Array_new(size_t size, const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  155. /* Allocates and copies an array
  156. *
  157. * @param src The memory location of the source array
  158. * @param size The size of the array
  159. * @param dst The location of the pointer to the new array
  160. * @param type The datatype of the array members
  161. * @return Returns UA_STATUSCODE_GOOD or UA_STATUSCODE_BADOUTOFMEMORY */
  162. 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;
  163. /* Deletes an array.
  164. *
  165. * @param p The memory location of the array
  166. * @param size The size of the array
  167. * @param type The datatype of the array members */
  168. void UA_EXPORT UA_Array_delete(void *p, size_t size, const UA_DataType *type);
  169. /**
  170. * Builtin Types, Part 2
  171. * ---------------------
  172. *
  173. * String
  174. * ^^^^^^
  175. * A sequence of Unicode characters. Strings are just an array of UA_Byte. */
  176. typedef struct {
  177. size_t length; /* The length of the string */
  178. UA_Byte *data; /* The content (not null-terminated) */
  179. } UA_String;
  180. /* Copies the content on the heap. Returns a null-string when alloc fails */
  181. UA_String UA_EXPORT UA_String_fromChars(char const src[]) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  182. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *s1, const UA_String *s2);
  183. UA_EXPORT extern const UA_String UA_STRING_NULL;
  184. /**
  185. * ``UA_STRING`` returns a string pointing to the preallocated char-array.
  186. * ``UA_STRING_ALLOC`` is shorthand for ``UA_String_fromChars`` and makes a copy
  187. * of the char-array. */
  188. static UA_INLINE UA_String UA_STRING(char *chars) {
  189. UA_String str; str.length = strlen(chars);
  190. str.data = (UA_Byte*)chars; return str; }
  191. #define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
  192. /**
  193. * DateTime
  194. * ^^^^^^^^
  195. * An instance in time. A DateTime value is encoded as a 64-bit signed integer
  196. * which represents the number of 100 nanosecond intervals since January 1, 1601
  197. * (UTC). */
  198. typedef int64_t UA_DateTime;
  199. /* Multiply to convert units for time difference computations */
  200. #define UA_USEC_TO_DATETIME 10LL
  201. #define UA_MSEC_TO_DATETIME (UA_USEC_TO_DATETIME * 1000LL)
  202. #define UA_SEC_TO_DATETIME (UA_MSEC_TO_DATETIME * 1000LL)
  203. /* Datetime of 1 Jan 1970 00:00 UTC */
  204. #define UA_DATETIME_UNIX_EPOCH (11644473600LL * UA_SEC_TO_DATETIME)
  205. /* The current time */
  206. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  207. /* CPU clock invariant to system time changes. Use only for time diffs, not current time */
  208. UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void);
  209. typedef struct UA_DateTimeStruct {
  210. UA_UInt16 nanoSec;
  211. UA_UInt16 microSec;
  212. UA_UInt16 milliSec;
  213. UA_UInt16 sec;
  214. UA_UInt16 min;
  215. UA_UInt16 hour;
  216. UA_UInt16 day;
  217. UA_UInt16 month;
  218. UA_UInt16 year;
  219. } UA_DateTimeStruct;
  220. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime t);
  221. UA_String UA_EXPORT UA_DateTime_toString(UA_DateTime t);
  222. /**
  223. * Guid
  224. * ^^^^
  225. * A 16 byte value that can be used as a globally unique identifier. */
  226. typedef struct {
  227. UA_UInt32 data1;
  228. UA_UInt16 data2;
  229. UA_UInt16 data3;
  230. UA_Byte data4[8];
  231. } UA_Guid;
  232. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  233. /* do not use for cryptographic entropy */
  234. UA_Guid UA_EXPORT UA_Guid_random(void);
  235. /**
  236. * ByteString
  237. * ^^^^^^^^^^
  238. * A sequence of octets. */
  239. typedef UA_String UA_ByteString;
  240. static UA_INLINE UA_Boolean
  241. UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2) {
  242. return UA_String_equal((const UA_String*)string1, (const UA_String*)string2); }
  243. /* Allocates memory of size length for the bytestring. The content is not set to zero. */
  244. UA_StatusCode UA_EXPORT UA_ByteString_allocBuffer(UA_ByteString *bs, size_t length);
  245. UA_EXPORT extern const UA_ByteString UA_BYTESTRING_NULL;
  246. static UA_INLINE UA_ByteString UA_BYTESTRING(char *chars) {
  247. UA_ByteString str; str.length = strlen(chars); str.data = (UA_Byte*)chars; return str; }
  248. static UA_INLINE UA_ByteString UA_BYTESTRING_ALLOC(const char *chars) {
  249. UA_String str = UA_String_fromChars(chars); UA_ByteString bstr;
  250. bstr.length = str.length; bstr.data = str.data; return bstr;
  251. }
  252. /**
  253. * XmlElement
  254. * ^^^^^^^^^^
  255. * An XML element. */
  256. typedef UA_String UA_XmlElement;
  257. /**
  258. * NodeId
  259. * ^^^^^^
  260. * An identifier for a node in the address space of an OPC UA Server. */
  261. enum UA_NodeIdType {
  262. UA_NODEIDTYPE_NUMERIC = 0, /* In the binary encoding, this can become 0
  263. or 1 for 2byte and 4byte encoding of small
  264. nodeids. */
  265. UA_NODEIDTYPE_STRING = 3,
  266. UA_NODEIDTYPE_GUID = 4,
  267. UA_NODEIDTYPE_BYTESTRING = 5
  268. };
  269. typedef struct {
  270. UA_UInt16 namespaceIndex;
  271. enum UA_NodeIdType identifierType;
  272. union {
  273. UA_UInt32 numeric;
  274. UA_String string;
  275. UA_Guid guid;
  276. UA_ByteString byteString;
  277. } identifier;
  278. } UA_NodeId;
  279. UA_EXPORT extern const UA_NodeId UA_NODEID_NULL;
  280. static UA_INLINE UA_Boolean UA_NodeId_isNull(const UA_NodeId *p) {
  281. return (p->namespaceIndex == 0 && p->identifierType == UA_NODEIDTYPE_NUMERIC &&
  282. p->identifier.numeric == 0);
  283. }
  284. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  285. /** The following functions are shorthand for creating NodeIds. */
  286. static UA_INLINE UA_NodeId UA_NODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  287. UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_NUMERIC;
  288. id.identifier.numeric = identifier; return id; }
  289. static UA_INLINE UA_NodeId UA_NODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  290. UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_STRING;
  291. id.identifier.string = UA_STRING(chars); return id; }
  292. static UA_INLINE UA_NodeId UA_NODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  293. UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_STRING;
  294. id.identifier.string = UA_STRING_ALLOC(chars); return id; }
  295. static UA_INLINE UA_NodeId UA_NODEID_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  296. UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_GUID;
  297. id.identifier.guid = guid; return id; }
  298. static UA_INLINE UA_NodeId UA_NODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  299. UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  300. id.identifier.byteString = UA_BYTESTRING(chars); return id; }
  301. static UA_INLINE UA_NodeId UA_NODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  302. UA_NodeId id; id.namespaceIndex = nsIndex; id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  303. id.identifier.byteString = UA_BYTESTRING_ALLOC(chars); return id; }
  304. /**
  305. * ExpandedNodeId
  306. * ^^^^^^^^^^^^^^
  307. * A NodeId that allows the namespace URI to be specified instead of an index. */
  308. typedef struct {
  309. UA_NodeId nodeId;
  310. UA_String namespaceUri;
  311. UA_UInt32 serverIndex;
  312. } UA_ExpandedNodeId;
  313. /** The following functions are shorthand for creating ExpandedNodeIds. */
  314. static UA_INLINE UA_ExpandedNodeId
  315. UA_EXPANDEDNODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  316. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_NUMERIC(nsIndex, identifier);
  317. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id; }
  318. static UA_INLINE UA_ExpandedNodeId
  319. UA_EXPANDEDNODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  320. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING(nsIndex, chars);
  321. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id; }
  322. static UA_INLINE UA_ExpandedNodeId
  323. UA_EXPANDEDNODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  324. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING_ALLOC(nsIndex, chars);
  325. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id; }
  326. static UA_INLINE UA_ExpandedNodeId
  327. UA_EXPANDEDNODEID_STRING_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  328. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_GUID(nsIndex, guid);
  329. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id; }
  330. static UA_INLINE UA_ExpandedNodeId
  331. UA_EXPANDEDNODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  332. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING(nsIndex, chars);
  333. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id; }
  334. static UA_INLINE UA_ExpandedNodeId
  335. UA_EXPANDEDNODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  336. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING_ALLOC(nsIndex, chars);
  337. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id; }
  338. /**
  339. * QualifiedName
  340. * ^^^^^^^^^^^^^
  341. * A name qualified by a namespace. */
  342. typedef struct {
  343. UA_UInt16 namespaceIndex;
  344. UA_String name;
  345. } UA_QualifiedName;
  346. static UA_INLINE UA_QualifiedName
  347. UA_QUALIFIEDNAME(UA_UInt16 nsIndex, char *chars) {
  348. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  349. qn.name = UA_STRING(chars); return qn; }
  350. static UA_INLINE UA_QualifiedName
  351. UA_QUALIFIEDNAME_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  352. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  353. qn.name = UA_STRING_ALLOC(chars); return qn; }
  354. /**
  355. * LocalizedText
  356. * ^^^^^^^^^^^^^
  357. * Human readable text with an optional locale identifier. */
  358. typedef struct {
  359. UA_String locale;
  360. UA_String text;
  361. } UA_LocalizedText;
  362. static UA_INLINE UA_LocalizedText
  363. UA_LOCALIZEDTEXT(char *locale, char *text) {
  364. UA_LocalizedText lt; lt.locale = UA_STRING(locale);
  365. lt.text = UA_STRING(text); return lt; }
  366. static UA_INLINE UA_LocalizedText
  367. UA_LOCALIZEDTEXT_ALLOC(const char *locale, const char *text) {
  368. UA_LocalizedText lt; lt.locale = UA_STRING_ALLOC(locale);
  369. lt.text = UA_STRING_ALLOC(text); return lt; }
  370. /**
  371. * ExtensionObject
  372. * ^^^^^^^^^^^^^^^
  373. * ExtensionObjects may contain scalars of any data type. Even those that are
  374. * unknown to the receiver. See the Section `Generic Type Handling`_ on how
  375. * types are described. An ExtensionObject always contains the NodeId of the
  376. * Data Type. If the data cannot be decoded, we keep the encoded string and the
  377. * NodeId. */
  378. typedef struct {
  379. enum {
  380. UA_EXTENSIONOBJECT_ENCODED_NOBODY = 0,
  381. UA_EXTENSIONOBJECT_ENCODED_BYTESTRING = 1,
  382. UA_EXTENSIONOBJECT_ENCODED_XML = 2,
  383. UA_EXTENSIONOBJECT_DECODED = 3,
  384. UA_EXTENSIONOBJECT_DECODED_NODELETE = 4 /* Don't delete the decoded content
  385. at the lifecycle end */
  386. } encoding;
  387. union {
  388. struct {
  389. UA_NodeId typeId; /* The nodeid of the datatype */
  390. UA_ByteString body; /* The bytestring of the encoded data */
  391. } encoded;
  392. struct {
  393. const UA_DataType *type;
  394. void *data;
  395. } decoded;
  396. } content;
  397. } UA_ExtensionObject;
  398. /**
  399. * Variant
  400. * ^^^^^^^
  401. * Variants may contain data of any type. See the Section `Generic Type
  402. * Handling`_ on how types are described. If the data is not of one of the 25
  403. * builtin types, it will be encoded as an `ExtensionObject`_ on the wire. (The
  404. * standard says that a variant is a union of the built-in types. open62541
  405. * generalizes this to any data type by transparently de- and encoding
  406. * ExtensionObjects in the background. If the decoding fails, the variant
  407. * contains the original ExtensionObject.)
  408. *
  409. * Variants can contain a single scalar or an array. For details on the handling
  410. * of arrays, see the Section `Array Handling`_. Array variants can have an
  411. * additional dimensionality (matrix, 3-tensor, ...) defined in an array of
  412. * dimension sizes. Higher rank dimensions are serialized first.
  413. *
  414. * The differentiation between variants containing a scalar, an array or no data
  415. * is as follows:
  416. *
  417. * - arrayLength == 0 && data == NULL: no existing data
  418. * - arrayLength == 0 && data == UA_EMPTY_ARRAY_SENTINEL: array of length 0
  419. * - arrayLength == 0 && data > UA_EMPTY_ARRAY_SENTINEL: scalar value
  420. * - arrayLength > 0: array of the given length */
  421. typedef struct {
  422. const UA_DataType *type; /* The data type description */
  423. enum {
  424. UA_VARIANT_DATA, /* The data has the same lifecycle as the variant */
  425. UA_VARIANT_DATA_NODELETE, /* The data is "borrowed" by the variant and shall not be
  426. deleted at the end of the variant's lifecycle. */
  427. } storageType;
  428. size_t arrayLength; // The number of elements in the data array
  429. void *data; // Points to the scalar or array data
  430. size_t arrayDimensionsSize; // The number of dimensions the data-array has
  431. UA_UInt32 *arrayDimensions; // The length of each dimension of the data-array
  432. } UA_Variant;
  433. /* Returns true if the variant contains a scalar value. Note that empty variants contain
  434. * an array of length -1 (undefined).
  435. *
  436. * @param v The variant
  437. * @return Does the variant contain a scalar value. */
  438. static UA_INLINE UA_Boolean UA_Variant_isScalar(const UA_Variant *v) {
  439. return (v->arrayLength == 0 && v->data > UA_EMPTY_ARRAY_SENTINEL); }
  440. /* Set the variant to a scalar value that already resides in memory. The value takes on
  441. * the lifecycle of the variant and is deleted with it.
  442. *
  443. * @param v The variant
  444. * @param p A pointer to the value data
  445. * @param type The datatype of the value in question */
  446. void UA_EXPORT
  447. UA_Variant_setScalar(UA_Variant *v, void * UA_RESTRICT p, const UA_DataType *type);
  448. /* Set the variant to a scalar value that is copied from an existing variable.
  449. * @param v The variant
  450. * @param p A pointer to the value data
  451. * @param type The datatype of the value
  452. * @return Indicates whether the operation succeeded or returns an error code */
  453. UA_StatusCode UA_EXPORT
  454. UA_Variant_setScalarCopy(UA_Variant *v, const void *p, const UA_DataType *type);
  455. /* Set the variant to an array that already resides in memory. The array takes on the
  456. * lifecycle of the variant and is deleted with it.
  457. *
  458. * @param v The variant
  459. * @param array A pointer to the array data
  460. * @param arraySize The size of the array
  461. * @param type The datatype of the array */
  462. void UA_EXPORT
  463. UA_Variant_setArray(UA_Variant *v, void * UA_RESTRICT array,
  464. size_t arraySize, const UA_DataType *type);
  465. /* Set the variant to an array that is copied from an existing array.
  466. *
  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.
  489. * Returns an error code if the variant is not an array or if the indicated range does not
  490. * fit.
  491. *
  492. * @param src The source variant
  493. * @param dst The target variant
  494. * @param range The range of the copied data
  495. * @return Returns UA_STATUSCODE_GOOD or an error code */
  496. UA_StatusCode UA_EXPORT
  497. UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst, const UA_NumericRange range);
  498. /* Insert a range of data into an existing variant. The data array can't be reused afterwards if it
  499. * contains types without a fixed size (e.g. strings) since the members are moved into the variant
  500. * and take on its lifecycle.
  501. *
  502. * @param v The variant
  503. * @param dataArray The data array. The type must match the variant
  504. * @param dataArraySize The length of the data array. This is checked to match the range size.
  505. * @param range The range of where the new data is inserted
  506. * @return Returns UA_STATUSCODE_GOOD or an error code */
  507. UA_StatusCode UA_EXPORT
  508. UA_Variant_setRange(UA_Variant *v, void * UA_RESTRICT array,
  509. size_t arraySize, const UA_NumericRange range);
  510. /* Deep-copy a range of data into an existing variant.
  511. *
  512. * @param v The variant
  513. * @param dataArray The data array. The type must match the variant
  514. * @param dataArraySize The length of the data array. This is checked to match the range size.
  515. * @param range The range of where the new data is inserted
  516. * @return Returns UA_STATUSCODE_GOOD or an error code */
  517. UA_StatusCode UA_EXPORT
  518. UA_Variant_setRangeCopy(UA_Variant *v, const void *array,
  519. size_t arraySize, const UA_NumericRange range);
  520. /**
  521. * DataValue
  522. * ^^^^^^^^^
  523. * A data value with an associated status code and timestamps. */
  524. typedef struct {
  525. UA_Boolean hasValue : 1;
  526. UA_Boolean hasStatus : 1;
  527. UA_Boolean hasSourceTimestamp : 1;
  528. UA_Boolean hasServerTimestamp : 1;
  529. UA_Boolean hasSourcePicoseconds : 1;
  530. UA_Boolean hasServerPicoseconds : 1;
  531. UA_Variant value;
  532. UA_StatusCode status;
  533. UA_DateTime sourceTimestamp;
  534. UA_UInt16 sourcePicoseconds;
  535. UA_DateTime serverTimestamp;
  536. UA_UInt16 serverPicoseconds;
  537. } UA_DataValue;
  538. /**
  539. * DiagnosticInfo
  540. * ^^^^^^^^^^^^^^
  541. * A structure that contains detailed error and diagnostic information
  542. * associated with a StatusCode. */
  543. typedef struct UA_DiagnosticInfo {
  544. UA_Boolean hasSymbolicId : 1;
  545. UA_Boolean hasNamespaceUri : 1;
  546. UA_Boolean hasLocalizedText : 1;
  547. UA_Boolean hasLocale : 1;
  548. UA_Boolean hasAdditionalInfo : 1;
  549. UA_Boolean hasInnerStatusCode : 1;
  550. UA_Boolean hasInnerDiagnosticInfo : 1;
  551. UA_Int32 symbolicId;
  552. UA_Int32 namespaceUri;
  553. UA_Int32 localizedText;
  554. UA_Int32 locale;
  555. UA_String additionalInfo;
  556. UA_StatusCode innerStatusCode;
  557. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  558. } UA_DiagnosticInfo;
  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 element?
  572. For arrays this is the padding before the size_t
  573. lenght member. (No padding between size_t and the
  574. 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_ */