ua_types.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef UA_TYPES_H_
  5. #define UA_TYPES_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_config.h"
  10. #include "ua_constants.h"
  11. #define UA_BUILTIN_TYPES_COUNT 25U
  12. /**
  13. * .. _types:
  14. *
  15. * Data Types
  16. * ==========
  17. *
  18. * The OPC UA protocol defines 25 builtin data types and three ways of combining
  19. * them into higher-order types: arrays, structures and unions. In open62541,
  20. * only the builtin data types are defined manually. All other data types are
  21. * generated from standard XML definitions. Their exact definitions can be
  22. * looked up at https://opcfoundation.org/UA/schemas/Opc.Ua.Types.bsd.xml.
  23. *
  24. * For users that are new to open62541, take a look at the :ref:`tutorial for
  25. * working with data types<types-tutorial>` before diving into the
  26. * implementation details.
  27. *
  28. * Builtin Types
  29. * -------------
  30. *
  31. * Boolean
  32. * ^^^^^^^
  33. * A two-state logical value (true or false). */
  34. typedef bool UA_Boolean;
  35. #define UA_TRUE true
  36. #define UA_FALSE false
  37. /**
  38. * SByte
  39. * ^^^^^
  40. * An integer value between -128 and 127. */
  41. typedef int8_t UA_SByte;
  42. #define UA_SBYTE_MIN (-128)
  43. #define UA_SBYTE_MAX 127
  44. /**
  45. * Byte
  46. * ^^^^
  47. * An integer value between 0 and 255. */
  48. typedef uint8_t UA_Byte;
  49. #define UA_BYTE_MIN 0
  50. #define UA_BYTE_MAX 255
  51. /**
  52. * Int16
  53. * ^^^^^
  54. * An integer value between -32 768 and 32 767. */
  55. typedef int16_t UA_Int16;
  56. #define UA_INT16_MIN (-32768)
  57. #define UA_INT16_MAX 32767
  58. /**
  59. * UInt16
  60. * ^^^^^^
  61. * An integer value between 0 and 65 535. */
  62. typedef uint16_t UA_UInt16;
  63. #define UA_UINT16_MIN 0
  64. #define UA_UINT16_MAX 65535
  65. /**
  66. * Int32
  67. * ^^^^^
  68. * An integer value between -2 147 483 648 and 2 147 483 647. */
  69. typedef int32_t UA_Int32;
  70. #define UA_INT32_MIN (-2147483648)
  71. #define UA_INT32_MAX 2147483647
  72. /**
  73. * UInt32
  74. * ^^^^^^
  75. * An integer value between 0 and 4 294 967 295. */
  76. typedef uint32_t UA_UInt32;
  77. #define UA_UINT32_MIN 0
  78. #define UA_UINT32_MAX 4294967295
  79. /**
  80. * Int64
  81. * ^^^^^
  82. * An integer value between -9 223 372 036 854 775 808 and
  83. * 9 223 372 036 854 775 807. */
  84. typedef int64_t UA_Int64;
  85. #define UA_INT64_MIN ((int64_t)-9223372036854775808)
  86. #define UA_INT64_MAX (int64_t)9223372036854775807
  87. /**
  88. * UInt64
  89. * ^^^^^^
  90. * An integer value between 0 and 18 446 744 073 709 551 615. */
  91. typedef uint64_t UA_UInt64;
  92. #define UA_UINT64_MIN (int64_t)0
  93. #define UA_UINT64_MAX (int64_t)18446744073709551615
  94. /**
  95. * Float
  96. * ^^^^^
  97. * An IEEE single precision (32 bit) floating point value. */
  98. typedef float UA_Float;
  99. /**
  100. * Double
  101. * ^^^^^^
  102. * An IEEE double precision (64 bit) floating point value. */
  103. typedef double UA_Double;
  104. /**
  105. * .. _statuscode:
  106. *
  107. * StatusCode
  108. * ^^^^^^^^^^
  109. * A numeric identifier for a error or condition that is associated with a value
  110. * or an operation. See the section :ref:`statuscodes` for the meaning of a
  111. * specific code. */
  112. typedef uint32_t UA_StatusCode;
  113. /* Returns the human-readable name of the StatusCode. If no matching StatusCode
  114. * is found, a default string for "Unknown" is returned. This feature might be
  115. * disabled to create a smaller binary with the
  116. * UA_ENABLE_STATUSCODE_DESCRIPTIONS build-flag. Then the function returns an
  117. * empty string for every StatusCode. */
  118. UA_EXPORT const char *
  119. UA_StatusCode_name(UA_StatusCode code);
  120. /**
  121. * String
  122. * ^^^^^^
  123. * A sequence of Unicode characters. Strings are just an array of UA_Byte. */
  124. typedef struct {
  125. size_t length; /* The length of the string */
  126. UA_Byte *data; /* The content (not null-terminated) */
  127. } UA_String;
  128. /* Copies the content on the heap. Returns a null-string when alloc fails */
  129. UA_String UA_EXPORT UA_String_fromChars(char const src[]) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  130. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *s1, const UA_String *s2);
  131. UA_EXPORT extern const UA_String UA_STRING_NULL;
  132. /**
  133. * ``UA_STRING`` returns a string pointing to the original char-array.
  134. * ``UA_STRING_ALLOC`` is shorthand for ``UA_String_fromChars`` and makes a copy
  135. * of the char-array. */
  136. static UA_INLINE UA_String
  137. UA_STRING(char *chars) {
  138. UA_String str; str.length = strlen(chars);
  139. str.data = (UA_Byte*)chars; return str;
  140. }
  141. #define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
  142. /**
  143. * .. _datetime:
  144. *
  145. * DateTime
  146. * ^^^^^^^^
  147. * An instance in time. A DateTime value is encoded as a 64-bit signed integer
  148. * which represents the number of 100 nanosecond intervals since January 1, 1601
  149. * (UTC). */
  150. typedef int64_t UA_DateTime;
  151. /* Multiply to convert units for time difference computations */
  152. #define UA_USEC_TO_DATETIME 10LL
  153. #define UA_MSEC_TO_DATETIME (UA_USEC_TO_DATETIME * 1000LL)
  154. #define UA_SEC_TO_DATETIME (UA_MSEC_TO_DATETIME * 1000LL)
  155. #define UA_DATETIME_TO_USEC (1/10.0)
  156. #define UA_DATETIME_TO_MSEC (UA_DATETIME_TO_USEC / 1000.0)
  157. #define UA_DATETIME_TO_SEC (UA_DATETIME_TO_MSEC / 1000.0)
  158. /* Datetime of 1 Jan 1970 00:00 UTC */
  159. #define UA_DATETIME_UNIX_EPOCH (11644473600LL * UA_SEC_TO_DATETIME)
  160. /* The current time */
  161. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  162. /* CPU clock invariant to system time changes. Use only for time diffs, not
  163. * current time */
  164. UA_DateTime UA_EXPORT UA_DateTime_nowMonotonic(void);
  165. typedef struct UA_DateTimeStruct {
  166. UA_UInt16 nanoSec;
  167. UA_UInt16 microSec;
  168. UA_UInt16 milliSec;
  169. UA_UInt16 sec;
  170. UA_UInt16 min;
  171. UA_UInt16 hour;
  172. UA_UInt16 day;
  173. UA_UInt16 month;
  174. UA_UInt16 year;
  175. } UA_DateTimeStruct;
  176. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime t);
  177. UA_String UA_EXPORT UA_DateTime_toString(UA_DateTime t);
  178. /**
  179. * Guid
  180. * ^^^^
  181. * A 16 byte value that can be used as a globally unique identifier. */
  182. typedef struct {
  183. UA_UInt32 data1;
  184. UA_UInt16 data2;
  185. UA_UInt16 data3;
  186. UA_Byte data4[8];
  187. } UA_Guid;
  188. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  189. UA_EXPORT extern const UA_Guid UA_GUID_NULL;
  190. /**
  191. * ByteString
  192. * ^^^^^^^^^^
  193. * A sequence of octets. */
  194. typedef UA_String UA_ByteString;
  195. static UA_INLINE UA_Boolean
  196. UA_ByteString_equal(const UA_ByteString *string1,
  197. const UA_ByteString *string2) {
  198. return UA_String_equal((const UA_String*)string1,
  199. (const UA_String*)string2);
  200. }
  201. /* Allocates memory of size length for the bytestring.
  202. * The content is not set to zero. */
  203. UA_StatusCode UA_EXPORT
  204. UA_ByteString_allocBuffer(UA_ByteString *bs, size_t length);
  205. UA_EXPORT extern const UA_ByteString UA_BYTESTRING_NULL;
  206. static UA_INLINE UA_ByteString
  207. UA_BYTESTRING(char *chars) {
  208. UA_ByteString str; str.length = strlen(chars);
  209. str.data = (UA_Byte*)chars; return str;
  210. }
  211. static UA_INLINE UA_ByteString
  212. UA_BYTESTRING_ALLOC(const char *chars) {
  213. UA_String str = UA_String_fromChars(chars); UA_ByteString bstr;
  214. bstr.length = str.length; bstr.data = str.data; return bstr;
  215. }
  216. /**
  217. * XmlElement
  218. * ^^^^^^^^^^
  219. * An XML element. */
  220. typedef UA_String UA_XmlElement;
  221. /**
  222. * .. _nodeid:
  223. *
  224. * NodeId
  225. * ^^^^^^
  226. * An identifier for a node in the address space of an OPC UA Server. */
  227. enum UA_NodeIdType {
  228. UA_NODEIDTYPE_NUMERIC = 0, /* In the binary encoding, this can also
  229. become 1 or 2 (2byte and 4byte encoding of
  230. small numeric nodeids) */
  231. UA_NODEIDTYPE_STRING = 3,
  232. UA_NODEIDTYPE_GUID = 4,
  233. UA_NODEIDTYPE_BYTESTRING = 5
  234. };
  235. typedef struct {
  236. UA_UInt16 namespaceIndex;
  237. enum UA_NodeIdType identifierType;
  238. union {
  239. UA_UInt32 numeric;
  240. UA_String string;
  241. UA_Guid guid;
  242. UA_ByteString byteString;
  243. } identifier;
  244. } UA_NodeId;
  245. UA_EXPORT extern const UA_NodeId UA_NODEID_NULL;
  246. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  247. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  248. /* Returns a non-cryptographic hash for the NodeId */
  249. UA_UInt32 UA_EXPORT UA_NodeId_hash(const UA_NodeId *n);
  250. /** The following functions are shorthand for creating NodeIds. */
  251. static UA_INLINE UA_NodeId
  252. UA_NODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  253. UA_NodeId id; id.namespaceIndex = nsIndex;
  254. id.identifierType = UA_NODEIDTYPE_NUMERIC;
  255. id.identifier.numeric = identifier; return id;
  256. }
  257. static UA_INLINE UA_NodeId
  258. UA_NODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  259. UA_NodeId id; id.namespaceIndex = nsIndex;
  260. id.identifierType = UA_NODEIDTYPE_STRING;
  261. id.identifier.string = UA_STRING(chars); return id;
  262. }
  263. static UA_INLINE UA_NodeId
  264. UA_NODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  265. UA_NodeId id; id.namespaceIndex = nsIndex;
  266. id.identifierType = UA_NODEIDTYPE_STRING;
  267. id.identifier.string = UA_STRING_ALLOC(chars); return id;
  268. }
  269. static UA_INLINE UA_NodeId
  270. UA_NODEID_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  271. UA_NodeId id; id.namespaceIndex = nsIndex;
  272. id.identifierType = UA_NODEIDTYPE_GUID;
  273. id.identifier.guid = guid; return id;
  274. }
  275. static UA_INLINE UA_NodeId
  276. UA_NODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  277. UA_NodeId id; id.namespaceIndex = nsIndex;
  278. id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  279. id.identifier.byteString = UA_BYTESTRING(chars); return id;
  280. }
  281. static UA_INLINE UA_NodeId
  282. UA_NODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  283. UA_NodeId id; id.namespaceIndex = nsIndex;
  284. id.identifierType = UA_NODEIDTYPE_BYTESTRING;
  285. id.identifier.byteString = UA_BYTESTRING_ALLOC(chars); return id;
  286. }
  287. /**
  288. * ExpandedNodeId
  289. * ^^^^^^^^^^^^^^
  290. * A NodeId that allows the namespace URI to be specified instead of an index. */
  291. typedef struct {
  292. UA_NodeId nodeId;
  293. UA_String namespaceUri;
  294. UA_UInt32 serverIndex;
  295. } UA_ExpandedNodeId;
  296. UA_EXPORT extern const UA_ExpandedNodeId UA_EXPANDEDNODEID_NULL;
  297. /** The following functions are shorthand for creating ExpandedNodeIds. */
  298. static UA_INLINE UA_ExpandedNodeId
  299. UA_EXPANDEDNODEID_NUMERIC(UA_UInt16 nsIndex, UA_UInt32 identifier) {
  300. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_NUMERIC(nsIndex, identifier);
  301. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  302. }
  303. static UA_INLINE UA_ExpandedNodeId
  304. UA_EXPANDEDNODEID_STRING(UA_UInt16 nsIndex, char *chars) {
  305. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING(nsIndex, chars);
  306. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  307. }
  308. static UA_INLINE UA_ExpandedNodeId
  309. UA_EXPANDEDNODEID_STRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  310. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_STRING_ALLOC(nsIndex, chars);
  311. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  312. }
  313. static UA_INLINE UA_ExpandedNodeId
  314. UA_EXPANDEDNODEID_STRING_GUID(UA_UInt16 nsIndex, UA_Guid guid) {
  315. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_GUID(nsIndex, guid);
  316. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  317. }
  318. static UA_INLINE UA_ExpandedNodeId
  319. UA_EXPANDEDNODEID_BYTESTRING(UA_UInt16 nsIndex, char *chars) {
  320. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING(nsIndex, chars);
  321. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  322. }
  323. static UA_INLINE UA_ExpandedNodeId
  324. UA_EXPANDEDNODEID_BYTESTRING_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  325. UA_ExpandedNodeId id; id.nodeId = UA_NODEID_BYTESTRING_ALLOC(nsIndex, chars);
  326. id.serverIndex = 0; id.namespaceUri = UA_STRING_NULL; return id;
  327. }
  328. /**
  329. * .. _qualifiedname:
  330. *
  331. * QualifiedName
  332. * ^^^^^^^^^^^^^
  333. * A name qualified by a namespace. */
  334. typedef struct {
  335. UA_UInt16 namespaceIndex;
  336. UA_String name;
  337. } UA_QualifiedName;
  338. static UA_INLINE UA_Boolean
  339. UA_QualifiedName_isNull(const UA_QualifiedName *q) {
  340. return (q->namespaceIndex == 0 && q->name.length == 0);
  341. }
  342. static UA_INLINE UA_QualifiedName
  343. UA_QUALIFIEDNAME(UA_UInt16 nsIndex, char *chars) {
  344. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  345. qn.name = UA_STRING(chars); return qn;
  346. }
  347. static UA_INLINE UA_QualifiedName
  348. UA_QUALIFIEDNAME_ALLOC(UA_UInt16 nsIndex, const char *chars) {
  349. UA_QualifiedName qn; qn.namespaceIndex = nsIndex;
  350. qn.name = UA_STRING_ALLOC(chars); return qn;
  351. }
  352. /**
  353. * LocalizedText
  354. * ^^^^^^^^^^^^^
  355. * Human readable text with an optional locale identifier. */
  356. typedef struct {
  357. UA_String locale;
  358. UA_String text;
  359. } UA_LocalizedText;
  360. static UA_INLINE UA_LocalizedText
  361. UA_LOCALIZEDTEXT(char *locale, char *text) {
  362. UA_LocalizedText lt; lt.locale = UA_STRING(locale);
  363. lt.text = UA_STRING(text); return lt;
  364. }
  365. static UA_INLINE UA_LocalizedText
  366. UA_LOCALIZEDTEXT_ALLOC(const char *locale, const char *text) {
  367. UA_LocalizedText lt; lt.locale = UA_STRING_ALLOC(locale);
  368. lt.text = UA_STRING_ALLOC(text); return lt;
  369. }
  370. /**
  371. * .. _numericrange:
  372. *
  373. * NumericRange
  374. * ^^^^^^^^^^^^
  375. *
  376. * NumericRanges are used to indicate subsets of a (multidimensional) array.
  377. * They no official data type in the OPC UA standard and are transmitted only
  378. * with a string encoding, such as "1:2,0:3,5". The colon separates min/max
  379. * index and the comma separates dimensions. A single value indicates a range
  380. * with a single element (min==max). */
  381. typedef struct {
  382. UA_UInt32 min;
  383. UA_UInt32 max;
  384. } UA_NumericRangeDimension;
  385. typedef struct {
  386. size_t dimensionsSize;
  387. UA_NumericRangeDimension *dimensions;
  388. } UA_NumericRange;
  389. /**
  390. * .. _variant:
  391. *
  392. * Variant
  393. * ^^^^^^^
  394. *
  395. * Variants may contain values of any type together with a description of the
  396. * content. See the section on :ref:`generic-types` on how types are described.
  397. * The standard mandates that variants contain built-in data types only. If the
  398. * value is not of a builtin type, it is wrapped into an :ref:`extensionobject`.
  399. * open62541 hides this wrapping transparently in the encoding layer. If the
  400. * data type is unknown to the receiver, the variant contains the original
  401. * ExtensionObject in binary or XML encoding.
  402. *
  403. * Variants may contain a scalar value or an array. For details on the handling
  404. * of arrays, see the section on :ref:`array-handling`. Array variants can have
  405. * an additional dimensionality (matrix, 3-tensor, ...) defined in an array of
  406. * dimension lengths. The actual values are kept in an array of dimensions one.
  407. * For users who work with higher-dimensions arrays directly, keep in mind that
  408. * dimensions of higher rank are serialized first (the highest rank dimension
  409. * has stride 1 and elements follow each other directly). Usually it is simplest
  410. * to interact with higher-dimensional arrays via ``UA_NumericRange``
  411. * descriptions (see :ref:`array-handling`).
  412. *
  413. * To differentiate between scalar / array variants, the following definition is
  414. * used. ``UA_Variant_isScalar`` provides simplified access to these checks.
  415. *
  416. * - ``arrayLength == 0 && data == NULL``: undefined array of length -1
  417. * - ``arrayLength == 0 && data == UA_EMPTY_ARRAY_SENTINEL``: array of length 0
  418. * - ``arrayLength == 0 && data > UA_EMPTY_ARRAY_SENTINEL``: scalar value
  419. * - ``arrayLength > 0``: array of the given length
  420. *
  421. * Variants can also be *empty*. Then, the pointer to the type description is
  422. * ``NULL``. */
  423. /* Forward declaration. See the section on Generic Type Handling */
  424. struct UA_DataType;
  425. typedef struct UA_DataType UA_DataType;
  426. #define UA_EMPTY_ARRAY_SENTINEL ((void*)0x01)
  427. typedef enum {
  428. UA_VARIANT_DATA, /* The data has the same lifecycle as the
  429. variant */
  430. UA_VARIANT_DATA_NODELETE /* The data is "borrowed" by the variant and
  431. shall not be deleted at the end of the
  432. variant's lifecycle. */
  433. } UA_VariantStorageType;
  434. typedef struct {
  435. const UA_DataType *type; /* The data type description */
  436. UA_VariantStorageType storageType;
  437. size_t arrayLength; /* The number of elements in the data array */
  438. void *data; /* Points to the scalar or array data */
  439. size_t arrayDimensionsSize; /* The number of dimensions */
  440. UA_UInt32 *arrayDimensions; /* The length of each dimension */
  441. } UA_Variant;
  442. /* Returns true if the variant has no value defined (contains neither an array
  443. * nor a scalar value).
  444. *
  445. * @param v The variant
  446. * @return Is the variant empty */
  447. static UA_INLINE UA_Boolean
  448. UA_Variant_isEmpty(const UA_Variant *v) {
  449. return v->type == NULL;
  450. }
  451. /* Returns true if the variant contains a scalar value. Note that empty variants
  452. * contain an array of length -1 (undefined).
  453. *
  454. * @param v The variant
  455. * @return Does the variant contain a scalar value */
  456. static UA_INLINE UA_Boolean
  457. UA_Variant_isScalar(const UA_Variant *v) {
  458. return (v->arrayLength == 0 && v->data > UA_EMPTY_ARRAY_SENTINEL);
  459. }
  460. /* Returns true if the variant contains a scalar value of the given type.
  461. *
  462. * @param v The variant
  463. * @param type The data type
  464. * @return Does the variant contain a scalar value of the given type */
  465. static UA_INLINE UA_Boolean
  466. UA_Variant_hasScalarType(const UA_Variant *v, const UA_DataType *type) {
  467. return UA_Variant_isScalar(v) && type == v->type;
  468. }
  469. /* Returns true if the variant contains an array of the given type.
  470. *
  471. * @param v The variant
  472. * @param type The data type
  473. * @return Does the variant contain an array of the given type */
  474. static UA_INLINE UA_Boolean
  475. UA_Variant_hasArrayType(const UA_Variant *v, const UA_DataType *type) {
  476. return (!UA_Variant_isScalar(v)) && type == v->type;
  477. }
  478. /* Set the variant to a scalar value that already resides in memory. The value
  479. * takes on the lifecycle of the variant and is deleted with it.
  480. *
  481. * @param v The variant
  482. * @param p A pointer to the value data
  483. * @param type The datatype of the value in question */
  484. void UA_EXPORT
  485. UA_Variant_setScalar(UA_Variant *v, void * UA_RESTRICT p,
  486. const UA_DataType *type);
  487. /* Set the variant to a scalar value that is copied from an existing variable.
  488. * @param v The variant
  489. * @param p A pointer to the value data
  490. * @param type The datatype of the value
  491. * @return Indicates whether the operation succeeded or returns an error code */
  492. UA_StatusCode UA_EXPORT
  493. UA_Variant_setScalarCopy(UA_Variant *v, const void *p,
  494. const UA_DataType *type);
  495. /* Set the variant to an array that already resides in memory. The array takes
  496. * on the lifecycle of the variant and is deleted with it.
  497. *
  498. * @param v The variant
  499. * @param array A pointer to the array data
  500. * @param arraySize The size of the array
  501. * @param type The datatype of the array */
  502. void UA_EXPORT
  503. UA_Variant_setArray(UA_Variant *v, void * UA_RESTRICT array,
  504. size_t arraySize, const UA_DataType *type);
  505. /* Set the variant to an array that is copied from an existing array.
  506. *
  507. * @param v The variant
  508. * @param array A pointer to the array data
  509. * @param arraySize The size of the array
  510. * @param type The datatype of the array
  511. * @return Indicates whether the operation succeeded or returns an error code */
  512. UA_StatusCode UA_EXPORT
  513. UA_Variant_setArrayCopy(UA_Variant *v, const void *array,
  514. size_t arraySize, const UA_DataType *type);
  515. /* Copy the variant, but use only a subset of the (multidimensional) array into
  516. * a variant. Returns an error code if the variant is not an array or if the
  517. * indicated range does not fit.
  518. *
  519. * @param src The source variant
  520. * @param dst The target variant
  521. * @param range The range of the copied data
  522. * @return Returns UA_STATUSCODE_GOOD or an error code */
  523. UA_StatusCode UA_EXPORT
  524. UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst,
  525. const UA_NumericRange range);
  526. /* Insert a range of data into an existing variant. The data array can't be
  527. * reused afterwards if it contains types without a fixed size (e.g. strings)
  528. * since the members are moved into the variant and take on its lifecycle.
  529. *
  530. * @param v The variant
  531. * @param dataArray The data array. The type must match the variant
  532. * @param dataArraySize The length of the data array. This is checked to match
  533. * the range size.
  534. * @param range The range of where the new data is inserted
  535. * @return Returns UA_STATUSCODE_GOOD or an error code */
  536. UA_StatusCode UA_EXPORT
  537. UA_Variant_setRange(UA_Variant *v, void * UA_RESTRICT array,
  538. size_t arraySize, const UA_NumericRange range);
  539. /* Deep-copy a range of data into an existing variant.
  540. *
  541. * @param v The variant
  542. * @param dataArray The data array. The type must match the variant
  543. * @param dataArraySize The length of the data array. This is checked to match
  544. * the range size.
  545. * @param range The range of where the new data is inserted
  546. * @return Returns UA_STATUSCODE_GOOD or an error code */
  547. UA_StatusCode UA_EXPORT
  548. UA_Variant_setRangeCopy(UA_Variant *v, const void *array,
  549. size_t arraySize, const UA_NumericRange range);
  550. /**
  551. * .. _extensionobject:
  552. *
  553. * ExtensionObject
  554. * ^^^^^^^^^^^^^^^
  555. *
  556. * ExtensionObjects may contain scalars of any data type. Even those that are
  557. * unknown to the receiver. See the section on :ref:`generic-types` on how types
  558. * are described. If the received data type is unkown, the encoded string and
  559. * target NodeId is stored instead of the decoded value. */
  560. typedef enum {
  561. UA_EXTENSIONOBJECT_ENCODED_NOBODY = 0,
  562. UA_EXTENSIONOBJECT_ENCODED_BYTESTRING = 1,
  563. UA_EXTENSIONOBJECT_ENCODED_XML = 2,
  564. UA_EXTENSIONOBJECT_DECODED = 3,
  565. UA_EXTENSIONOBJECT_DECODED_NODELETE = 4 /* Don't delete the content
  566. together with the
  567. ExtensionObject */
  568. } UA_ExtensionObjectEncoding;
  569. typedef struct {
  570. UA_ExtensionObjectEncoding encoding;
  571. union {
  572. struct {
  573. UA_NodeId typeId; /* The nodeid of the datatype */
  574. UA_ByteString body; /* The bytestring of the encoded data */
  575. } encoded;
  576. struct {
  577. const UA_DataType *type;
  578. void *data;
  579. } decoded;
  580. } content;
  581. } UA_ExtensionObject;
  582. /**
  583. * .. _datavalue:
  584. *
  585. * DataValue
  586. * ^^^^^^^^^
  587. * A data value with an associated status code and timestamps. */
  588. typedef struct {
  589. UA_Boolean hasValue : 1;
  590. UA_Boolean hasStatus : 1;
  591. UA_Boolean hasSourceTimestamp : 1;
  592. UA_Boolean hasServerTimestamp : 1;
  593. UA_Boolean hasSourcePicoseconds : 1;
  594. UA_Boolean hasServerPicoseconds : 1;
  595. UA_Variant value;
  596. UA_StatusCode status;
  597. UA_DateTime sourceTimestamp;
  598. UA_UInt16 sourcePicoseconds;
  599. UA_DateTime serverTimestamp;
  600. UA_UInt16 serverPicoseconds;
  601. } UA_DataValue;
  602. /**
  603. * DiagnosticInfo
  604. * ^^^^^^^^^^^^^^
  605. * A structure that contains detailed error and diagnostic information
  606. * associated with a StatusCode. */
  607. typedef struct UA_DiagnosticInfo {
  608. UA_Boolean hasSymbolicId : 1;
  609. UA_Boolean hasNamespaceUri : 1;
  610. UA_Boolean hasLocalizedText : 1;
  611. UA_Boolean hasLocale : 1;
  612. UA_Boolean hasAdditionalInfo : 1;
  613. UA_Boolean hasInnerStatusCode : 1;
  614. UA_Boolean hasInnerDiagnosticInfo : 1;
  615. UA_Int32 symbolicId;
  616. UA_Int32 namespaceUri;
  617. UA_Int32 localizedText;
  618. UA_Int32 locale;
  619. UA_String additionalInfo;
  620. UA_StatusCode innerStatusCode;
  621. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  622. } UA_DiagnosticInfo;
  623. /**
  624. * .. _generic-types:
  625. *
  626. * Generic Type Handling
  627. * ---------------------
  628. *
  629. * All information about a (builtin/structured) data type is stored in a
  630. * ``UA_DataType``. The array ``UA_TYPES`` contains the description of all
  631. * standard-defined types. This type description is used for the following
  632. * generic operations that work on all types:
  633. *
  634. * - ``void T_init(T *ptr)``: Initialize the data type. This is synonymous with
  635. * zeroing out the memory, i.e. ``memset(ptr, 0, sizeof(T))``.
  636. * - ``T* T_new()``: Allocate and return the memory for the data type. The
  637. * value is already initialized.
  638. * - ``UA_StatusCode T_copy(const T *src, T *dst)``: Copy the content of the
  639. * data type. Returns ``UA_STATUSCODE_GOOD`` or
  640. * ``UA_STATUSCODE_BADOUTOFMEMORY``.
  641. * - ``void T_deleteMembers(T *ptr)``: Delete the dynamically allocated content
  642. * of the data type and perform a ``T_init`` to reset the type.
  643. * - ``void T_delete(T *ptr)``: Delete the content of the data type and the
  644. * memory for the data type itself.
  645. *
  646. * Specializations, such as ``UA_Int32_new()`` are derived from the generic
  647. * type operations as static inline functions. */
  648. typedef struct {
  649. #ifdef UA_ENABLE_TYPENAMES
  650. const char *memberName;
  651. #endif
  652. UA_UInt16 memberTypeIndex; /* Index of the member in the array of data
  653. types */
  654. UA_Byte padding; /* How much padding is there before this
  655. member element? For arrays this is the
  656. padding before the size_t lenght member.
  657. (No padding between size_t and the
  658. following ptr.) */
  659. UA_Boolean namespaceZero : 1; /* The type of the member is defined in
  660. namespace zero. In this implementation,
  661. types from custom namespace may contain
  662. members from the same namespace or
  663. namespace zero only.*/
  664. UA_Boolean isArray : 1; /* The member is an array */
  665. } UA_DataTypeMember;
  666. struct UA_DataType {
  667. #ifdef UA_ENABLE_TYPENAMES
  668. const char *typeName;
  669. #endif
  670. UA_NodeId typeId; /* The nodeid of the type */
  671. UA_UInt16 memSize; /* Size of the struct in memory */
  672. UA_UInt16 typeIndex; /* Index of the type in the datatypetable */
  673. UA_Byte membersSize; /* How many members does the type have? */
  674. UA_Boolean builtin : 1; /* The type is "builtin" and has dedicated de-
  675. and encoding functions */
  676. UA_Boolean pointerFree : 1; /* The type (and its members) contains no
  677. pointers that need to be freed */
  678. UA_Boolean overlayable : 1; /* The type has the identical memory layout in
  679. memory and on the binary stream. */
  680. UA_UInt16 binaryEncodingId; /* NodeId of datatype when encoded as binary */
  681. //UA_UInt16 xmlEncodingId; /* NodeId of datatype when encoded as XML */
  682. UA_DataTypeMember *members;
  683. };
  684. /* The following is used to exclude type names in the definition of UA_DataType
  685. * structures if the feature is disabled. */
  686. #ifdef UA_ENABLE_TYPENAMES
  687. # define UA_TYPENAME(name) name,
  688. #else
  689. # define UA_TYPENAME(name)
  690. #endif
  691. /**
  692. * Builtin data types can be accessed as UA_TYPES[UA_TYPES_XXX], where XXX is
  693. * the name of the data type. If only the NodeId of a type is known, use the
  694. * following method to retrieve the data type description. */
  695. /* Returns the data type description for the type's identifier or NULL if no
  696. * matching data type was found. */
  697. const UA_DataType UA_EXPORT *
  698. UA_findDataType(const UA_NodeId *typeId);
  699. /** The following functions are used for generic handling of data types. */
  700. /* Allocates and initializes a variable of type dataType
  701. *
  702. * @param type The datatype description
  703. * @return Returns the memory location of the variable or NULL if no
  704. * memory could be allocated */
  705. void UA_EXPORT * UA_new(const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  706. /* Initializes a variable to default values
  707. *
  708. * @param p The memory location of the variable
  709. * @param type The datatype description */
  710. static UA_INLINE void
  711. UA_init(void *p, const UA_DataType *type) {
  712. memset(p, 0, type->memSize);
  713. }
  714. /* Copies the content of two variables. If copying fails (e.g. because no memory
  715. * was available for an array), then dst is emptied and initialized to prevent
  716. * memory leaks.
  717. *
  718. * @param src The memory location of the source variable
  719. * @param dst The memory location of the destination variable
  720. * @param type The datatype description
  721. * @return Indicates whether the operation succeeded or returns an error code */
  722. UA_StatusCode UA_EXPORT
  723. UA_copy(const void *src, void *dst, const UA_DataType *type);
  724. /* Deletes the dynamically allocated content of a variable (e.g. resets all
  725. * arrays to undefined arrays). Afterwards, the variable can be safely deleted
  726. * without causing memory leaks. But the variable is not initialized and may
  727. * contain old data that is not memory-relevant.
  728. *
  729. * @param p The memory location of the variable
  730. * @param type The datatype description of the variable */
  731. void UA_EXPORT UA_deleteMembers(void *p, const UA_DataType *type);
  732. /* Frees a variable and all of its content.
  733. *
  734. * @param p The memory location of the variable
  735. * @param type The datatype description of the variable */
  736. void UA_EXPORT UA_delete(void *p, const UA_DataType *type);
  737. /**
  738. * .. _array-handling:
  739. *
  740. * Array handling
  741. * --------------
  742. * In OPC UA, arrays can have a length of zero or more with the usual meaning.
  743. * In addition, arrays can be undefined. Then, they don't even have a length. In
  744. * the binary encoding, this is indicated by an array of length -1.
  745. *
  746. * In open62541 however, we use ``size_t`` for array lengths. An undefined array
  747. * has length 0 and the data pointer is ``NULL``. An array of length 0 also has
  748. * length 0 but a data pointer ``UA_EMPTY_ARRAY_SENTINEL``. */
  749. /* Allocates and initializes an array of variables of a specific type
  750. *
  751. * @param size The requested array length
  752. * @param type The datatype description
  753. * @return Returns the memory location of the variable or NULL if no memory
  754. could be allocated */
  755. void UA_EXPORT * UA_Array_new(size_t size, const UA_DataType *type) UA_FUNC_ATTR_MALLOC;
  756. /* Allocates and copies an array
  757. *
  758. * @param src The memory location of the source array
  759. * @param size The size of the array
  760. * @param dst The location of the pointer to the new array
  761. * @param type The datatype of the array members
  762. * @return Returns UA_STATUSCODE_GOOD or UA_STATUSCODE_BADOUTOFMEMORY */
  763. UA_StatusCode UA_EXPORT
  764. UA_Array_copy(const void *src, size_t size, void **dst,
  765. const UA_DataType *type) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  766. /* Deletes an array.
  767. *
  768. * @param p The memory location of the array
  769. * @param size The size of the array
  770. * @param type The datatype of the array members */
  771. void UA_EXPORT UA_Array_delete(void *p, size_t size, const UA_DataType *type);
  772. /**
  773. * Random Number Generator
  774. * -----------------------
  775. * If UA_ENABLE_MULTITHREADING is defined, then the seed is stored in thread
  776. * local storage. The seed is initialized for every thread in the
  777. * server/client. */
  778. void UA_EXPORT UA_random_seed(UA_UInt64 seed);
  779. UA_UInt32 UA_EXPORT UA_UInt32_random(void); /* no cryptographic entropy */
  780. UA_Guid UA_EXPORT UA_Guid_random(void); /* no cryptographic entropy */
  781. /**
  782. * .. _generated-types:
  783. *
  784. * Generated Data Type Definitions
  785. * -------------------------------
  786. *
  787. * The following data types were auto-generated from a definition in XML format.
  788. *
  789. * .. toctree::
  790. *
  791. * types_generated */
  792. /**
  793. * Deprecated Data Types API
  794. * -------------------------
  795. * The following definitions are deprecated and will be removed in future
  796. * releases of open62541. */
  797. typedef struct {
  798. UA_StatusCode code; /* The numeric value of the StatusCode */
  799. const char* name; /* The symbolic name */
  800. const char* explanation; /* Short message explaining the StatusCode */
  801. } UA_StatusCodeDescription;
  802. UA_EXPORT extern const UA_StatusCodeDescription statusCodeExplanation_default;
  803. UA_DEPRECATED static UA_INLINE const UA_StatusCodeDescription *
  804. UA_StatusCode_description(UA_StatusCode code) {
  805. return &statusCodeExplanation_default;
  806. }
  807. UA_DEPRECATED static UA_INLINE const char *
  808. UA_StatusCode_explanation(UA_StatusCode code) {
  809. return statusCodeExplanation_default.name;
  810. }
  811. #ifdef __cplusplus
  812. } // extern "C"
  813. #endif
  814. #endif /* UA_TYPES_H_ */