ua_types.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. /** @brief Pointers to data that is stored in memory. The "type" of the data is
  165. stored in the variant itself. */
  166. typedef struct {
  167. UA_Int32 arrayLength; // total number of elements in the data-pointer
  168. void *dataPtr;
  169. UA_Int32 arrayDimensionsSize;
  170. UA_Int32 *arrayDimensions;
  171. } UA_VariantData;
  172. /** @brief A datasource is the interface to interact with a local data provider.
  173. *
  174. * Implementors of datasources need to provide functions for the callbacks in
  175. * this structure. After every read, the handle needs to be released to
  176. * indicate that the pointer is no longer accessed. As a rule, datasources are
  177. * never copied, but only their content. The only way to write into a
  178. * datasource is via the write-service. */
  179. typedef struct {
  180. const void *handle;
  181. UA_StatusCode (*read)(const void *handle, UA_VariantData *data);
  182. void (*release)(const void *handle, UA_VariantData *data);
  183. UA_StatusCode (*write)(const void *handle, const UA_VariantData *data);
  184. } UA_VariantDataSource;
  185. struct UA_DataType;
  186. typedef struct UA_DataType UA_DataType;
  187. /** @brief Variants store (arrays of) any data type. Either they provide a pointer to the data in
  188. memory, or functions from which the data can be accessed. Variants are replaced together with
  189. the data they store (exception: use a data source).*/
  190. typedef struct {
  191. const UA_DataType *type;
  192. UA_NodeId typeId;
  193. enum {
  194. UA_VARIANT_DATA, ///< The data is "owned" by this variant (copied and deleted together)
  195. UA_VARIANT_DATA_NODELETE, /**< The data is "borrowed" by the variant and shall not be
  196. deleted at the end of this variant's lifecycle. It is not
  197. possible to overwrite borrowed data due to concurrent access.
  198. Use a custom datasource with a mutex. */
  199. UA_VARIANT_DATASOURCE /**< The data is provided externally. Call the functions in the
  200. datasource to get a current version */
  201. } storageType;
  202. union {
  203. UA_VariantData data;
  204. UA_VariantDataSource datasource;
  205. } storage;
  206. } UA_Variant;
  207. /** @brief A data value with an associated status code and timestamps. */
  208. typedef struct {
  209. UA_Boolean hasVariant : 1;
  210. UA_Boolean hasStatus : 1;
  211. UA_Boolean hasSourceTimestamp : 1;
  212. UA_Boolean hasServerTimestamp : 1;
  213. UA_Boolean hasSourcePicoseconds : 1;
  214. UA_Boolean hasServerPicoseconds : 1;
  215. UA_Variant value;
  216. UA_StatusCode status;
  217. UA_DateTime sourceTimestamp;
  218. UA_Int16 sourcePicoseconds;
  219. UA_DateTime serverTimestamp;
  220. UA_Int16 serverPicoseconds;
  221. } UA_DataValue;
  222. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  223. typedef struct UA_DiagnosticInfo {
  224. UA_Boolean hasSymbolicId : 1;
  225. UA_Boolean hasNamespaceUri : 1;
  226. UA_Boolean hasLocalizedText : 1;
  227. UA_Boolean hasLocale : 1;
  228. UA_Boolean hasAdditionalInfo : 1;
  229. UA_Boolean hasInnerStatusCode : 1;
  230. UA_Boolean hasInnerDiagnosticInfo : 1;
  231. UA_Int32 symbolicId;
  232. UA_Int32 namespaceUri;
  233. UA_Int32 localizedText;
  234. UA_Int32 locale;
  235. UA_String additionalInfo;
  236. UA_StatusCode innerStatusCode;
  237. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  238. } UA_DiagnosticInfo;
  239. #define UA_TYPE_HANDLING_FUNCTIONS(TYPE) \
  240. TYPE UA_EXPORT * TYPE##_new(void); \
  241. void UA_EXPORT TYPE##_init(TYPE * p); \
  242. void UA_EXPORT TYPE##_delete(TYPE * p); \
  243. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  244. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst);
  245. /* Functions for all types */
  246. UA_TYPE_HANDLING_FUNCTIONS(UA_Boolean)
  247. UA_TYPE_HANDLING_FUNCTIONS(UA_SByte)
  248. UA_TYPE_HANDLING_FUNCTIONS(UA_Byte)
  249. UA_TYPE_HANDLING_FUNCTIONS(UA_Int16)
  250. UA_TYPE_HANDLING_FUNCTIONS(UA_UInt16)
  251. UA_TYPE_HANDLING_FUNCTIONS(UA_Int32)
  252. UA_TYPE_HANDLING_FUNCTIONS(UA_UInt32)
  253. UA_TYPE_HANDLING_FUNCTIONS(UA_Int64)
  254. UA_TYPE_HANDLING_FUNCTIONS(UA_UInt64)
  255. UA_TYPE_HANDLING_FUNCTIONS(UA_Float)
  256. UA_TYPE_HANDLING_FUNCTIONS(UA_Double)
  257. UA_TYPE_HANDLING_FUNCTIONS(UA_String)
  258. #define UA_DateTime_new UA_Int64_new
  259. #define UA_DateTime_init UA_Int64_init
  260. #define UA_DateTime_delete UA_Int64_delete
  261. #define UA_DateTime_deleteMembers UA_Int64_deleteMembers
  262. #define UA_DateTime_copy UA_Int64_copy
  263. UA_TYPE_HANDLING_FUNCTIONS(UA_Guid)
  264. #define UA_ByteString_new UA_String_new
  265. #define UA_ByteString_init UA_String_init
  266. #define UA_ByteString_delete UA_String_delete
  267. #define UA_ByteString_deleteMembers UA_String_deleteMembers
  268. #define UA_ByteString_copy UA_String_copy
  269. #define UA_XmlElement_new UA_String_new
  270. #define UA_XmlElement_init UA_String_init
  271. #define UA_XmlElement_delete UA_String_delete
  272. #define UA_XmlElement_deleteMembers UA_String_deleteMembers
  273. #define UA_XmlElement_copy UA_String_copy
  274. UA_TYPE_HANDLING_FUNCTIONS(UA_NodeId)
  275. UA_TYPE_HANDLING_FUNCTIONS(UA_ExpandedNodeId)
  276. #define UA_StatusCode_new UA_Int32_new
  277. #define UA_StatusCode_init UA_Int32_init
  278. #define UA_StatusCode_delete UA_Int32_delete
  279. #define UA_StatusCode_deleteMembers UA_Int32_deleteMembers
  280. #define UA_StatusCode_copy UA_Int32_copy
  281. UA_TYPE_HANDLING_FUNCTIONS(UA_QualifiedName)
  282. UA_TYPE_HANDLING_FUNCTIONS(UA_LocalizedText)
  283. UA_TYPE_HANDLING_FUNCTIONS(UA_ExtensionObject)
  284. UA_TYPE_HANDLING_FUNCTIONS(UA_DataValue)
  285. UA_TYPE_HANDLING_FUNCTIONS(UA_Variant)
  286. UA_TYPE_HANDLING_FUNCTIONS(UA_DiagnosticInfo)
  287. /**********************************************/
  288. /* Custom functions for the builtin datatypes */
  289. /**********************************************/
  290. #ifdef __cplusplus
  291. #define CPP_ONLY(STR) STR
  292. #else
  293. #define CPP_ONLY(STR)
  294. #endif
  295. /* String */
  296. #define UA_STRING_NULL (UA_String) {-1, (UA_Byte*)0 }
  297. #define UA_STRING_ASSIGN(VARIABLE, STRING) do { \
  298. VARIABLE.length = sizeof(STRING)-1; \
  299. VARIABLE.data = (UA_Byte *)STRING; } while(0)
  300. UA_StatusCode UA_EXPORT UA_String_copycstring(char const *src, UA_String *dst);
  301. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  302. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  303. /* DateTime */
  304. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  305. typedef struct UA_DateTimeStruct {
  306. UA_Int16 nanoSec;
  307. UA_Int16 microSec;
  308. UA_Int16 milliSec;
  309. UA_Int16 sec;
  310. UA_Int16 min;
  311. UA_Int16 hour;
  312. UA_Int16 day;
  313. UA_Int16 month;
  314. UA_Int16 year;
  315. } UA_DateTimeStruct;
  316. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  317. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  318. /* Guid */
  319. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  320. /** Do not use for security-critical entropy! */
  321. UA_Guid UA_EXPORT UA_Guid_random(UA_UInt32 *seed);
  322. /* ByteString */
  323. UA_Boolean UA_EXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  324. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  325. /* NodeId */
  326. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  327. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  328. #define UA_NODEID_ASSIGN(VARIABLE, NUMERICID, NAMESPACE) do { \
  329. VARIABLE.namespaceIndex = NAMESPACE; \
  330. VARIABLE.identifierType = CPP_ONLY(UA_NodeId::)UA_NODEIDTYPE_NUMERIC; \
  331. VARIABLE.identifier.numeric = NUMERICID; } while(0);
  332. #define UA_NODEID_STATIC(NUMERICID, NAMESPACE) \
  333. (UA_NodeId){.namespaceIndex = NAMESPACE, .identifierType = UA_NODEIDTYPE_NUMERIC, \
  334. .identifier.numeric = NUMERICID}
  335. #define UA_NODEID_NULL UA_NODEID_STATIC(0,0)
  336. /* ExpandedNodeId */
  337. UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  338. #define UA_EXPANDEDNODEID_STATIC(NUMERICID, NAMESPACE) \
  339. (UA_ExpandedNodeId){.nodeId = {.namespaceIndex = NAMESPACE, .identifierType = UA_NODEIDTYPE_NUMERIC, \
  340. .identifier.numeric = NUMERICID}, \
  341. .serverIndex = 0, .namespaceUri = {.length = -1, .data = UA_NULL} }
  342. /* QualifiedName */
  343. UA_StatusCode UA_EXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  344. #define UA_QUALIFIEDNAME_ASSIGN(VARIABLE, STRING) do { \
  345. VARIABLE.namespaceIndex = 0; \
  346. UA_STRING_ASSIGN(VARIABLE.name, STRING); } while(0)
  347. /* LocalizedText */
  348. UA_StatusCode UA_EXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  349. /* Variant */
  350. UA_StatusCode UA_EXPORT UA_Variant_setValue(UA_Variant *v, void *p, UA_UInt16 typeIndex);
  351. UA_StatusCode UA_EXPORT UA_Variant_copySetValue(UA_Variant *v, const void *p, UA_UInt16 typeIndex);
  352. UA_StatusCode UA_EXPORT UA_Variant_setArray(UA_Variant *v, void *array, UA_Int32 noElements, UA_UInt16 typeIndex);
  353. UA_StatusCode UA_EXPORT UA_Variant_copySetArray(UA_Variant *v, const void *array, UA_Int32 noElements, UA_UInt16 typeIndex);
  354. /****************************/
  355. /* Structured Type Handling */
  356. /****************************/
  357. #define UA_MAX_TYPE_MEMBERS 13 // Maximum number of members per complex type
  358. #ifndef _WIN32
  359. # define UA_BITFIELD(SIZE) : SIZE
  360. #else
  361. # define UA_BITFIELD(SIZE)
  362. #endif
  363. typedef struct {
  364. UA_UInt16 memberTypeIndex UA_BITFIELD(9); ///< Index of the member in the datatypetable
  365. UA_Boolean namespaceZero UA_BITFIELD(1); /**< The type of the member is defined in namespace
  366. zero. In this implementation, types from custom
  367. namespace may contain members from the same
  368. namespace or ns0 only.*/
  369. UA_Byte padding UA_BITFIELD(5); /**< How much padding is there before this member element? For
  370. arrays this is split into 2 bytes padding for for the
  371. length index (max 4 bytes) and 3 bytes padding for the
  372. pointer (max 8 bytes) */
  373. UA_Boolean isArray UA_BITFIELD(1); ///< The member is an array of the given type
  374. } UA_DataTypeMember;
  375. struct UA_DataType {
  376. size_t memSize UA_BITFIELD(16); ///< Size of the struct in memory
  377. size_t typeIndex UA_BITFIELD(13); ///< Index of the type in the datatytypetable
  378. UA_Boolean namespaceZero UA_BITFIELD(1); ///< The type is defined in namespace zero.
  379. UA_Boolean fixedSize UA_BITFIELD(1); ///< The type (and its members) contains no pointers
  380. UA_Boolean zeroCopyable UA_BITFIELD(1); ///< Can the type be copied directly off the stream?
  381. UA_Byte membersSize; ///< How many members does the type have?
  382. UA_DataTypeMember members[UA_MAX_TYPE_MEMBERS];
  383. };
  384. void UA_EXPORT * UA_new(const UA_DataType *dataType);
  385. void UA_EXPORT UA_init(void *p, const UA_DataType *dataType);
  386. UA_StatusCode UA_EXPORT UA_copy(const void *src, void *dst, const UA_DataType *dataType);
  387. void UA_EXPORT UA_deleteMembers(void *p, const UA_DataType *dataType);
  388. void UA_EXPORT UA_delete(void *p, const UA_DataType *dataType);
  389. /********************/
  390. /* Array operations */
  391. /********************/
  392. #define MAX_ARRAY_SIZE 104857600 // arrays must be smaller than 100MB
  393. UA_StatusCode UA_EXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_DataType *dataType);
  394. UA_StatusCode UA_EXPORT UA_Array_copy(const void *src, UA_Int32 noElements, void **dst, const UA_DataType *dataType);
  395. void UA_EXPORT UA_Array_delete(void *p, UA_Int32 noElements, const UA_DataType *dataType);
  396. /** @} */
  397. #ifdef __cplusplus
  398. } // extern "C"
  399. #endif
  400. #endif /* UA_TYPES_H_ */