ua_types.h 28 KB

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