ua_types.h 31 KB

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