ua_types.h 33 KB

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