ua_types.h 29 KB

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