ua_types.h 31 KB

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