ua_types.h 30 KB

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