ua_types.h 33 KB

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