ua_types.h 32 KB

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