ua_types.h 25 KB

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