ua_types.h 33 KB

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