ua_types.h 31 KB

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