ua_types.h 31 KB

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