ua_types.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * Copyright (C) 2014 the contributors as stated in the AUTHORS file
  3. *
  4. * This file is part of open62541. open62541 is free software: you can
  5. * redistribute it and/or modify it under the terms of the GNU Lesser General
  6. * Public License, version 3 (as published by the Free Software Foundation) with
  7. * a static linking exception as stated in the LICENSE file provided with
  8. * open62541.
  9. *
  10. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. * details.
  14. */
  15. #ifndef UA_TYPES_H_
  16. #define UA_TYPES_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include "ua_config.h"
  24. /**
  25. * @defgroup types Datatypes
  26. *
  27. * @brief The built-in datatypes. The remaining datatypes are autogenerated from
  28. * XML descriptions as they are all enums or structures made up of the built-in
  29. * datatypes.
  30. *
  31. * All datatypes have similar functions with a common postfix. DO NOT CALL THESE
  32. * FUNCTIONS WITH NULL-POINTERS IF IT IS NOT EXPLICITLY PERMITTED.
  33. *
  34. * - _new: Allocates the memory for the type and runs _init on the returned
  35. * variable. Returns null if no memory could be allocated.
  36. *
  37. * - _init: Sets all members to a "safe" standard, usually zero. Arrays (e.g.
  38. * for strings) are set to a length of -1.
  39. *
  40. * - _copy: Copies a datatype. This performs a deep copy that iterates over the
  41. * members. Copying into variants with an external data source is not
  42. * permitted. If copying fails, a deleteMembers is performed and an error
  43. * code returned.
  44. *
  45. * - _delete: Frees the memory where the datatype was stored. This performs an
  46. * _deleteMembers internally if required.
  47. *
  48. * - _deleteMembers: Frees the memory of dynamically sized members (e.g. a
  49. * string) of a datatype. This is useful when the datatype was allocated on
  50. * the stack, whereas the dynamically sized members is heap-allocated. To
  51. * reuse the variable, the remaining members (not dynamically allocated) need
  52. * to be cleaned up with an _init.
  53. *
  54. * @{
  55. */
  56. /** @brief A two-state logical value (true or false). */
  57. typedef bool UA_Boolean;
  58. #define UA_TRUE true
  59. #define UA_FALSE false
  60. /** @brief An integer value between -129 and 127. */
  61. typedef int8_t UA_SByte;
  62. #define UA_SBYTE_MAX 127
  63. #define UA_SBYTE_MIN -128
  64. /** @brief An integer value between 0 and 256. */
  65. typedef uint8_t UA_Byte;
  66. #define UA_BYTE_MAX 256
  67. #define UA_BYTE_MIN 0
  68. /** @brief An integer value between -32 768 and 32 767. */
  69. typedef int16_t UA_Int16;
  70. #define UA_INT16_MAX 32767
  71. #define UA_INT16_MIN -32768
  72. /** @brief An integer value between 0 and 65 535. */
  73. typedef uint16_t UA_UInt16;
  74. #define UA_UINT16_MAX 65535
  75. #define UA_UINT16_MIN 0
  76. /** @brief An integer value between -2 147 483 648 and 2 147 483 647. */
  77. typedef int32_t UA_Int32;
  78. #define UA_INT32_MAX 2147483647
  79. #define UA_INT32_MIN -2147483648
  80. /** @brief An integer value between 0 and 429 4967 295. */
  81. typedef uint32_t UA_UInt32;
  82. #define UA_UINT32_MAX 4294967295
  83. #define UA_UINT32_MIN 0
  84. /** @brief An integer value between -10 223 372 036 854 775 808 and 9 223 372 036 854 775 807 */
  85. typedef int64_t UA_Int64;
  86. #define UA_INT64_MAX 9223372036854775807
  87. #define UA_INT64_MIN -9223372036854775808
  88. /** @brief An integer value between 0 and 18 446 744 073 709 551 615. */
  89. typedef uint64_t UA_UInt64;
  90. #define UA_UINT64_MAX = 18446744073709551615
  91. #define UA_UINT64_MIN = 0
  92. /** @brief An IEEE single precision (32 bit) floating point value. */
  93. typedef float UA_Float;
  94. /** @brief An IEEE double precision (64 bit) floating point value. */
  95. typedef double UA_Double;
  96. /** @brief A sequence of Unicode characters. */
  97. typedef struct {
  98. UA_Int32 length;
  99. UA_Byte *data;
  100. } UA_String;
  101. /** @brief An instance in time.
  102. * A DateTime value is encoded as a 64-bit signed integer which represents the
  103. * number of 100 nanosecond intervals since January 1, 1601 (UTC).
  104. */
  105. typedef UA_Int64 UA_DateTime; // 100 nanosecond resolution
  106. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  107. typedef struct {
  108. UA_UInt32 data1;
  109. UA_UInt16 data2;
  110. UA_UInt16 data3;
  111. UA_Byte data4[8];
  112. } UA_Guid;
  113. /** @brief A sequence of octets. */
  114. typedef UA_String UA_ByteString;
  115. /** @brief An XML element. */
  116. typedef UA_String UA_XmlElement;
  117. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  118. /* The shortened numeric types are introduced during encoding. */
  119. typedef struct {
  120. UA_UInt16 namespaceIndex;
  121. enum {
  122. UA_NODEIDTYPE_NUMERIC = 2,
  123. UA_NODEIDTYPE_STRING = 3,
  124. UA_NODEIDTYPE_GUID = 4,
  125. UA_NODEIDTYPE_BYTESTRING = 5
  126. } identifierType;
  127. union {
  128. UA_UInt32 numeric;
  129. UA_String string;
  130. UA_Guid guid;
  131. UA_ByteString byteString;
  132. } identifier;
  133. } UA_NodeId;
  134. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  135. typedef struct {
  136. UA_NodeId nodeId;
  137. UA_String namespaceUri; // not encoded if length=-1
  138. UA_UInt32 serverIndex; // not encoded if 0
  139. } UA_ExpandedNodeId;
  140. #include "ua_statuscodes.h"
  141. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  142. typedef enum UA_StatusCode UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  143. /** @brief A name qualified by a namespace. */
  144. typedef struct {
  145. UA_UInt16 namespaceIndex;
  146. UA_String name;
  147. } UA_QualifiedName;
  148. /** @brief Human readable text with an optional locale identifier. */
  149. typedef struct {
  150. UA_String locale;
  151. UA_String text;
  152. } UA_LocalizedText;
  153. /** @brief A structure that contains an application specific data type that may
  154. not be recognized by the receiver. */
  155. typedef struct {
  156. UA_NodeId typeId;
  157. enum {
  158. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0,
  159. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 1,
  160. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 2
  161. } encoding;
  162. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  163. } UA_ExtensionObject;
  164. struct UA_DataType;
  165. typedef struct UA_DataType UA_DataType;
  166. /** @brief NumericRanges are used select a subset in a (multidimensional) variant array.
  167. NumericRange has no official type structure in the standard. Officially, it only exists as an
  168. encoded string, such as "1:2,0:3,5". The colon separates min/max index and the comma separates
  169. dimensions. A single value indicates a range with a single element (min==max). */
  170. typedef struct {
  171. UA_Int32 dimensionsSize;
  172. struct UA_NumericRangeDimension {
  173. UA_UInt32 min;
  174. UA_UInt32 max;
  175. } *dimensions;
  176. } UA_NumericRange;
  177. /** @brief Variants store (arrays of) any data type. Either they provide a pointer to the data in
  178. * memory, or functions from which the data can be accessed. Variants are replaced together with
  179. * the data they store (exception: use a data source).
  180. *
  181. * Variant semantics:
  182. * - arrayLength = -1 && data == NULL: empty variant
  183. * - arrayLength = -1 && data == !NULL: variant holds a single element (a scalar)
  184. * - arrayLength >= 0: variant holds an array of the appropriate length
  185. * data can be NULL if arrayLength == 0
  186. */
  187. typedef struct {
  188. const UA_DataType *type;
  189. enum {
  190. UA_VARIANT_DATA, ///< The data is "owned" by this variant (copied and deleted together)
  191. UA_VARIANT_DATA_NODELETE, /**< The data is "borrowed" by the variant and shall not be
  192. deleted at the end of this variant's lifecycle. It is not
  193. possible to overwrite borrowed data due to concurrent access.
  194. Use a custom datasource with a mutex. */
  195. } storageType;
  196. UA_Int32 arrayLength; ///< the number of elements in the data-pointer
  197. void *data; ///< points to the scalar or array data
  198. UA_Int32 arrayDimensionsSize; ///< the number of dimensions the data-array has
  199. UA_Int32 *arrayDimensions; ///< the length of each dimension of the data-array
  200. } UA_Variant;
  201. /** @brief A data value with an associated status code and timestamps. */
  202. typedef struct {
  203. UA_Boolean hasValue : 1;
  204. UA_Boolean hasStatus : 1;
  205. UA_Boolean hasSourceTimestamp : 1;
  206. UA_Boolean hasServerTimestamp : 1;
  207. UA_Boolean hasSourcePicoseconds : 1;
  208. UA_Boolean hasServerPicoseconds : 1;
  209. UA_Variant value;
  210. UA_StatusCode status;
  211. UA_DateTime sourceTimestamp;
  212. UA_Int16 sourcePicoseconds;
  213. UA_DateTime serverTimestamp;
  214. UA_Int16 serverPicoseconds;
  215. } UA_DataValue;
  216. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  217. typedef struct UA_DiagnosticInfo {
  218. UA_Boolean hasSymbolicId : 1;
  219. UA_Boolean hasNamespaceUri : 1;
  220. UA_Boolean hasLocalizedText : 1;
  221. UA_Boolean hasLocale : 1;
  222. UA_Boolean hasAdditionalInfo : 1;
  223. UA_Boolean hasInnerStatusCode : 1;
  224. UA_Boolean hasInnerDiagnosticInfo : 1;
  225. UA_Int32 symbolicId;
  226. UA_Int32 namespaceUri;
  227. UA_Int32 localizedText;
  228. UA_Int32 locale;
  229. UA_String additionalInfo;
  230. UA_StatusCode innerStatusCode;
  231. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  232. } UA_DiagnosticInfo;
  233. #ifndef SWIG
  234. #define UA_TYPE_HANDLING_FUNCTIONS(TYPE) \
  235. TYPE UA_EXPORT * TYPE##_new(void); \
  236. void UA_EXPORT TYPE##_init(TYPE * p); \
  237. void UA_EXPORT TYPE##_delete(TYPE * p); \
  238. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  239. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst);
  240. #else
  241. #define UA_TYPE_HANDLING_FUNCTIONS(TYPE)
  242. #endif
  243. /* Functions for all types */
  244. UA_TYPE_HANDLING_FUNCTIONS(UA_Boolean)
  245. UA_TYPE_HANDLING_FUNCTIONS(UA_SByte)
  246. UA_TYPE_HANDLING_FUNCTIONS(UA_Byte)
  247. UA_TYPE_HANDLING_FUNCTIONS(UA_Int16)
  248. UA_TYPE_HANDLING_FUNCTIONS(UA_UInt16)
  249. UA_TYPE_HANDLING_FUNCTIONS(UA_Int32)
  250. UA_TYPE_HANDLING_FUNCTIONS(UA_UInt32)
  251. UA_TYPE_HANDLING_FUNCTIONS(UA_Int64)
  252. UA_TYPE_HANDLING_FUNCTIONS(UA_UInt64)
  253. UA_TYPE_HANDLING_FUNCTIONS(UA_Float)
  254. UA_TYPE_HANDLING_FUNCTIONS(UA_Double)
  255. UA_TYPE_HANDLING_FUNCTIONS(UA_String)
  256. #define UA_DateTime_new UA_Int64_new
  257. #define UA_DateTime_init UA_Int64_init
  258. #define UA_DateTime_delete UA_Int64_delete
  259. #define UA_DateTime_deleteMembers UA_Int64_deleteMembers
  260. #define UA_DateTime_copy UA_Int64_copy
  261. UA_TYPE_HANDLING_FUNCTIONS(UA_Guid)
  262. #define UA_ByteString_new UA_String_new
  263. #define UA_ByteString_init UA_String_init
  264. #define UA_ByteString_delete UA_String_delete
  265. #define UA_ByteString_deleteMembers UA_String_deleteMembers
  266. #define UA_ByteString_copy UA_String_copy
  267. #define UA_XmlElement_new UA_String_new
  268. #define UA_XmlElement_init UA_String_init
  269. #define UA_XmlElement_delete UA_String_delete
  270. #define UA_XmlElement_deleteMembers UA_String_deleteMembers
  271. #define UA_XmlElement_copy UA_String_copy
  272. UA_TYPE_HANDLING_FUNCTIONS(UA_NodeId)
  273. UA_TYPE_HANDLING_FUNCTIONS(UA_ExpandedNodeId)
  274. #define UA_StatusCode_new(p) UA_Int32_new((UA_Int32*)p)
  275. #define UA_StatusCode_init(p) UA_Int32_init((UA_Int32*)p)
  276. #define UA_StatusCode_delete(p) UA_Int32_delete((UA_Int32*)p)
  277. #define UA_StatusCode_deleteMembers(p) UA_Int32_deleteMembers((UA_Int32*)p)
  278. #define UA_StatusCode_copy(p) UA_Int32_copy((UA_Int32*)p)
  279. UA_TYPE_HANDLING_FUNCTIONS(UA_QualifiedName)
  280. UA_TYPE_HANDLING_FUNCTIONS(UA_LocalizedText)
  281. UA_TYPE_HANDLING_FUNCTIONS(UA_ExtensionObject)
  282. UA_TYPE_HANDLING_FUNCTIONS(UA_DataValue)
  283. UA_TYPE_HANDLING_FUNCTIONS(UA_Variant)
  284. UA_TYPE_HANDLING_FUNCTIONS(UA_DiagnosticInfo)
  285. /**********************************************/
  286. /* Custom functions for the builtin datatypes */
  287. /**********************************************/
  288. #ifdef __cplusplus
  289. #define CPP_ONLY(STR) STR
  290. #else
  291. #define CPP_ONLY(STR)
  292. #endif
  293. /* String */
  294. /** Copy a (zero terminated) char-array into a UA_String. Memory for the string data is
  295. allocated. If the memory cannot be allocated, a null-string is returned. */
  296. UA_String UA_EXPORT UA_String_fromChars(char const *src);
  297. #define UA_STRING_ALLOC(CHARS) UA_String_fromChars(CHARS)
  298. #define UA_STRING(CHARS) (const UA_String) {strlen(CHARS), (UA_Byte*)CHARS }
  299. #define UA_STRING_NULL (UA_String) {-1, (UA_Byte*)0 }
  300. /** Printf a char-array into a UA_String. Memory for the string data is allocated. */
  301. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  302. /** Compares two strings */
  303. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  304. /** Compares an UA String with a char array */
  305. UA_Boolean UA_EXPORT UA_String_equalchars(const UA_String *string1, char *charString);
  306. /* DateTime */
  307. /** Returns the current time */
  308. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  309. typedef struct UA_DateTimeStruct {
  310. UA_Int16 nanoSec;
  311. UA_Int16 microSec;
  312. UA_Int16 milliSec;
  313. UA_Int16 sec;
  314. UA_Int16 min;
  315. UA_Int16 hour;
  316. UA_Int16 day;
  317. UA_Int16 month;
  318. UA_Int16 year;
  319. } UA_DateTimeStruct;
  320. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  321. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  322. /* Guid */
  323. /** Compares two guids */
  324. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  325. /** Returns a randomly generated guid. Do not use for security-critical entropy! */
  326. UA_Guid UA_EXPORT UA_Guid_random(UA_UInt32 *seed);
  327. /* ByteString */
  328. #define UA_BYTESTRING_NULL (UA_ByteString) {-1, (UA_Byte*)0 }
  329. #define UA_ByteString_equal(string1, string2) \
  330. UA_String_equal((const UA_String*) string1, (const UA_String*)string2)
  331. /** Allocates memory of size length for the bytestring. The content is not set to zero. */
  332. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  333. /* NodeId */
  334. /** Compares two nodeids */
  335. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  336. /** Is the nodeid a null-nodeid? */
  337. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  338. #ifndef __cplusplus
  339. #define UA_NODEID_NUMERIC(NS_INDEX, NUMERICID) (UA_NodeId) { \
  340. .namespaceIndex = NS_INDEX, \
  341. .identifierType = UA_NODEIDTYPE_NUMERIC, \
  342. .identifier.numeric = NUMERICID }
  343. #define UA_NODEID_STRING(NS_INDEX, CHARS) (const UA_NodeId) { \
  344. .namespaceIndex = NS_INDEX, \
  345. .identifierType = UA_NODEIDTYPE_STRING, \
  346. .identifier.string = UA_STRING(CHARS) }
  347. #define UA_NODEID_STRING_ALLOC(NS_INDEX, CHARS) (const UA_NodeId) { \
  348. .namespaceIndex = NS_INDEX, \
  349. .identifierType = UA_NODEIDTYPE_STRING, \
  350. .identifier.string = UA_STRING_ALLOC(CHARS) }
  351. #define UA_NODEID_GUID(NS_INDEX, GUID) (UA_NodeId) { \
  352. .namespaceIndex = NS_INDEX, \
  353. .identifierType = UA_NODEIDTYPE_GUID, \
  354. .identifier.guid = GUID }
  355. #define UA_NODEID_BYTESTRING(NS_INDEX, CHARS) (const UA_NodeId) { \
  356. .namespaceIndex = NS_INDEX, \
  357. .identifierType = UA_NODEIDTYPE_BYTESTRING, \
  358. .identifier.byteString = UA_STRING(CHARS) }
  359. #define UA_NODEID_BYTESTRING_ALLOC(NS_INDEX, CHARS) (const UA_NodeId) {\
  360. .namespaceIndex = NS_INDEX, \
  361. .identifierType = UA_NODEIDTYPE_BYTESTRING, \
  362. .identifier.byteString = UA_STRING_ALLOC(CHARS) }
  363. #else
  364. #define UA_NODEID_NUMERIC(NS_INDEX, NUMERICID) (UA_NodeId) { \
  365. NS_INDEX, UA_NodeId::UA_NODEIDTYPE_NUMERIC, NUMERICID }
  366. #define UA_NODEID_STRING(NS_INDEX, CHARS) (const UA_NodeId) { \
  367. NS_INDEX, UA_NodeId::UA_NODEIDTYPE_STRING, UA_STRING(CHARS) }
  368. #define UA_NODEID_STRING_ALLOC(NS_INDEX, CHARS) (const UA_NodeId) { \
  369. NS_INDEX, UA_NodeId::UA_NODEIDTYPE_STRING, UA_STRING_ALLOC(CHARS) }
  370. #define UA_NODEID_GUID(NS_INDEX, GUID) (UA_NodeId) { \
  371. NS_INDEX, UA_NodeId::UA_NODEIDTYPE_GUID, GUID }
  372. #define UA_NODEID_BYTESTRING(NS_INDEX, CHARS) (const UA_NodeId) { \
  373. NS_INDEX, UA_NodeId::UA_NODEIDTYPE_BYTESTRING, UA_STRING(CHARS) }
  374. #define UA_NODEID_BYTESTRING_ALLOC(NS_INDEX, CHARS) (const UA_NodeId) { \
  375. NS_INDEX, UA_NodeId::UA_NODEIDTYPE_BYTESTRING, UA_STRING_ALLOC(CHARS) }
  376. #endif
  377. #define UA_NODEID_NULL UA_NODEID_NUMERIC(0,0)
  378. /* ExpandedNodeId */
  379. UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  380. #define UA_EXPANDEDNODEID_NUMERIC(NS_INDEX, NUMERICID) (UA_ExpandedNodeId) { \
  381. .nodeId = {.namespaceIndex = NS_INDEX, .identifierType = UA_NODEIDTYPE_NUMERIC, \
  382. .identifier.numeric = NUMERICID }, \
  383. .serverIndex = 0, .namespaceUri = {.data = (UA_Byte*)0, .length = -1} }
  384. /* QualifiedName */
  385. #define UA_QUALIFIEDNAME(NS_INDEX, CHARS) (const UA_QualifiedName) { \
  386. .namespaceIndex = NS_INDEX, .name = UA_STRING(CHARS) }
  387. #define UA_QUALIFIEDNAME_ALLOC(NS_INDEX, CHARS) (UA_QualifiedName) { \
  388. .namespaceIndex = NS_INDEX, .name = UA_STRING_ALLOC(CHARS) }
  389. /* LocalizedText */
  390. #define UA_LOCALIZEDTEXT(LOCALE, TEXT) (const UA_LocalizedText) { \
  391. .locale = UA_STRING(LOCALE), .text = UA_STRING(TEXT) }
  392. #define UA_LOCALIZEDTEXT_ALLOC(LOCALE, TEXT) (UA_LocalizedText) { \
  393. .locale = UA_STRING_ALLOC(LOCALE), .text = UA_STRING_ALLOC(TEXT) }
  394. /* Variant */
  395. /**
  396. * Returns true if the variant contains a scalar value. Note that empty variants contain an array of
  397. * length -1 (undefined).
  398. *
  399. * @param v The variant
  400. * @return Does the variant contain a scalar value.
  401. */
  402. UA_Boolean UA_EXPORT UA_Variant_isScalar(UA_Variant *v);
  403. /**
  404. * Set the variant to a scalar value that already resides in memory. The value takes on the
  405. * lifecycle of the variant and is deleted with it.
  406. *
  407. * @param v The variant
  408. * @param p A pointer to the value data
  409. * @param type The datatype of the value in question
  410. * @return Indicates whether the operation succeeded or returns an error code
  411. */
  412. UA_StatusCode UA_EXPORT UA_Variant_setScalar(UA_Variant *v, void *p, const UA_DataType *type);
  413. /**
  414. * Set the variant to a scalar value that is copied from an existing variable.
  415. *
  416. * @param v The variant
  417. * @param p A pointer to the value data
  418. * @param type The datatype of the value
  419. * @return Indicates whether the operation succeeded or returns an error code
  420. */
  421. UA_StatusCode UA_EXPORT UA_Variant_setScalarCopy(UA_Variant *v, const void *p, const UA_DataType *type);
  422. /**
  423. * Set the variant to an array that already resides in memory. The array takes on the lifecycle of
  424. * the variant and is deleted with it.
  425. *
  426. * @param v The variant
  427. * @param array A pointer to the array data
  428. * @param noElements The size of the array
  429. * @param type The datatype of the array
  430. * @return Indicates whether the operation succeeded or returns an error code
  431. */
  432. UA_StatusCode UA_EXPORT UA_Variant_setArray(UA_Variant *v, void *array, UA_Int32 noElements,
  433. const UA_DataType *type);
  434. /**
  435. * Set the variant to an array that is copied from an existing array.
  436. *
  437. * @param v The variant
  438. * @param array A pointer to the array data
  439. * @param noElements The size of the array
  440. * @param type The datatype of the array
  441. * @return Indicates whether the operation succeeded or returns an error code
  442. */
  443. UA_StatusCode UA_EXPORT UA_Variant_setArrayCopy(UA_Variant *v, const void *array, UA_Int32 noElements,
  444. const UA_DataType *type);
  445. /**
  446. * Copy the variant, but use only a subset of the (multidimensional) array. Returns an error code if
  447. * the variant is no array or if the indicated range does not fit.
  448. */
  449. UA_StatusCode UA_EXPORT UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst, UA_NumericRange range);
  450. /**
  451. * Insert a range of data into an existing variant of the dimensionality. This overwrites data in
  452. * the variant. The inserted data is managed by the variant (members are deleted with it).
  453. *
  454. * @param v The variant
  455. * @param data The data array. Obviously the type must match the variant and the length the range.
  456. * @param range The range of where the new data is inserted
  457. * @return Indicates whether the operation succeeded or returns an error code
  458. */
  459. UA_StatusCode UA_EXPORT UA_Variant_setRange(UA_Variant *v, void *data, const UA_NumericRange range);
  460. /**
  461. * Copies the variant and inserts data from the range. The inserted data is managed by the variant
  462. * (members are deleted with it).
  463. *
  464. * @param src The source variant
  465. * @param dst The target variant
  466. * @param data The data array. Obviously the type must match the variant and the length the range.
  467. * @param range The range of where the new data is inserted
  468. * @return Indicates whether the operation succeeded or returns an error code
  469. */
  470. UA_StatusCode UA_EXPORT UA_Variant_setCopyRange(const UA_Variant *src, UA_Variant *dst, void *data,
  471. const UA_NumericRange range);
  472. /****************************/
  473. /* Structured Type Handling */
  474. /****************************/
  475. #define UA_MAX_TYPE_MEMBERS 13 // Maximum number of members per complex type
  476. #ifndef _WIN32
  477. # define UA_BITFIELD(SIZE) : SIZE
  478. #else
  479. # define UA_BITFIELD(SIZE)
  480. #endif
  481. #define UA_IS_BUILTIN(ID) (ID <= UA_TYPES_DIAGNOSTICINFO)
  482. typedef struct {
  483. UA_UInt16 memberTypeIndex UA_BITFIELD(9); ///< Index of the member in the datatypetable
  484. UA_Boolean namespaceZero UA_BITFIELD(1); /**< The type of the member is defined in namespace
  485. zero. In this implementation, types from custom
  486. namespace may contain members from the same
  487. namespace or ns0 only.*/
  488. UA_Byte padding UA_BITFIELD(5); /**< How much padding is there before this member element? For
  489. arrays this is split into 2 bytes padding for for the
  490. length index (max 4 bytes) and 3 bytes padding for the
  491. pointer (max 8 bytes) */
  492. UA_Boolean isArray UA_BITFIELD(1); ///< The member is an array of the given type
  493. } UA_DataTypeMember;
  494. struct UA_DataType {
  495. UA_NodeId typeId; ///< The nodeid of the type
  496. ptrdiff_t memSize UA_BITFIELD(16); ///< Size of the struct in memory
  497. UA_UInt16 typeIndex UA_BITFIELD(13); ///< Index of the type in the datatypetable
  498. UA_Boolean namespaceZero UA_BITFIELD(1); ///< The type is defined in namespace zero.
  499. UA_Boolean fixedSize UA_BITFIELD(1); ///< The type (and its members) contains no pointers
  500. UA_Boolean zeroCopyable UA_BITFIELD(1); ///< Can the type be copied directly off the stream?
  501. UA_Byte membersSize; ///< How many members does the type have?
  502. UA_DataTypeMember members[UA_MAX_TYPE_MEMBERS];
  503. };
  504. /**
  505. * Allocates and initializes a variable of type dataType
  506. *
  507. * @param dataType The datatype description
  508. * @return Returns the memory location of the variable or (void*)0 if no memory is available
  509. */
  510. void UA_EXPORT * UA_new(const UA_DataType *dataType);
  511. /**
  512. * Initializes a variable to default values
  513. *
  514. * @param p The memory location of the variable
  515. * @param dataType The datatype description
  516. */
  517. void UA_EXPORT UA_init(void *p, const UA_DataType *dataType);
  518. /**
  519. * Copies the content of two variables. If copying fails (e.g. because no memory was available for
  520. * an array), then dst is emptied and initialized to prevent memory leaks.
  521. *
  522. * @param src The memory location of the source variable
  523. * @param dst The memory location of the destination variable
  524. * @param dataType The datatype description
  525. * @return Indicates whether the operation succeeded or returns an error code
  526. */
  527. UA_StatusCode UA_EXPORT UA_copy(const void *src, void *dst, const UA_DataType *dataType);
  528. /**
  529. * Deletes the dynamically assigned content of a variable (e.g. a member-array). Afterwards, the
  530. * variable can be safely deleted without causing memory leaks. But the variable is not initialized
  531. * and may contain old data that is not memory-relevant.
  532. *
  533. * @param p The memory location of the variable
  534. * @param dataType The datatype description of the variable
  535. */
  536. void UA_EXPORT UA_deleteMembers(void *p, const UA_DataType *dataType);
  537. /**
  538. * Deletes (frees) a variable and all of its content.
  539. *
  540. * @param p The memory location of the variable
  541. * @param dataType The datatype description of the variable
  542. */
  543. void UA_EXPORT UA_delete(void *p, const UA_DataType *dataType);
  544. /********************/
  545. /* Array operations */
  546. /********************/
  547. #define MAX_ARRAY_SIZE 104857600 // arrays must be smaller than 100MB
  548. /**
  549. * Allocates and initializes an array of variables of a specific type
  550. *
  551. * @param dataType The datatype description
  552. * @return Returns the memory location of the variable or (void*)0 if no memory could be allocated
  553. */
  554. void UA_EXPORT * UA_Array_new(const UA_DataType *dataType, UA_Int32 noElements);
  555. /**
  556. * Allocates and copies an array. dst is set to (void*)0 if not enough memory is available.
  557. *
  558. * @param src The memory location of the souce array
  559. * @param dst The memory location where the pointer to the destination array is written
  560. * @param dataType The datatype of the array members
  561. * @param noElements The size of the array
  562. * @return Indicates whether the operation succeeded or returns an error code
  563. */
  564. UA_StatusCode UA_EXPORT UA_Array_copy(const void *src, void **dst, const UA_DataType *dataType, UA_Int32 noElements);
  565. /**
  566. * Deletes an array.
  567. *
  568. * @param src The memory location of the array
  569. * @param dataType The datatype of the array members
  570. * @param noElements The size of the array
  571. */
  572. void UA_EXPORT UA_Array_delete(void *p, const UA_DataType *dataType, UA_Int32 noElements);
  573. /** @} */
  574. /* These are not generated from XML. Server *and* client need them. */
  575. typedef enum {
  576. UA_ATTRIBUTEID_NODEID = 1,
  577. UA_ATTRIBUTEID_NODECLASS = 2,
  578. UA_ATTRIBUTEID_BROWSENAME = 3,
  579. UA_ATTRIBUTEID_DISPLAYNAME = 4,
  580. UA_ATTRIBUTEID_DESCRIPTION = 5,
  581. UA_ATTRIBUTEID_WRITEMASK = 6,
  582. UA_ATTRIBUTEID_USERWRITEMASK = 7,
  583. UA_ATTRIBUTEID_ISABSTRACT = 8,
  584. UA_ATTRIBUTEID_SYMMETRIC = 9,
  585. UA_ATTRIBUTEID_INVERSENAME = 10,
  586. UA_ATTRIBUTEID_CONTAINSNOLOOPS = 11,
  587. UA_ATTRIBUTEID_EVENTNOTIFIER = 12,
  588. UA_ATTRIBUTEID_VALUE = 13,
  589. UA_ATTRIBUTEID_DATATYPE = 14,
  590. UA_ATTRIBUTEID_VALUERANK = 15,
  591. UA_ATTRIBUTEID_ARRAYDIMENSIONS = 16,
  592. UA_ATTRIBUTEID_ACCESSLEVEL = 17,
  593. UA_ATTRIBUTEID_USERACCESSLEVEL = 18,
  594. UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL = 19,
  595. UA_ATTRIBUTEID_HISTORIZING = 20,
  596. UA_ATTRIBUTEID_EXECUTABLE = 21,
  597. UA_ATTRIBUTEID_USEREXECUTABLE = 22
  598. } UA_AttributeId;
  599. #ifdef __cplusplus
  600. } // extern "C"
  601. #endif
  602. #endif /* UA_TYPES_H_ */