ua_types.h 17 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 "ua_config.h"
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #ifdef UA_DEBUG
  24. #include <stdio.h>
  25. #endif
  26. /**
  27. * @defgroup types Datatypes
  28. *
  29. * @brief The built-in datatypes. The remaining datatypes are autogenerated from
  30. * XML descriptions as they are all enums or structures made up of the built-in
  31. * datatypes.
  32. *
  33. * All datatypes have similar functions with a common postfix. DO NOT CALL THESE
  34. * FUNCTIONS WITH NULL-POINTERS IF IT IS NOT EXPLICITLY PERMITTED.
  35. *
  36. * - _new: Allocates the memory for the type and runs _init on the returned
  37. * variable. Returns null if no memory could be allocated.
  38. *
  39. * - _init: Sets all members to a "safe" standard, usually zero. Arrays (e.g.
  40. * for strings) are set to a length of -1.
  41. *
  42. * - _copy: Copies a datatype. This performs a deep copy that iterates over the
  43. * members. Copying into variants with an external data source is not
  44. * permitted. If copying fails, a deleteMembers is performed and an error
  45. * code returned.
  46. *
  47. * - _delete: Frees the memory where the datatype was stored. This performs an
  48. * _deleteMembers internally if required.
  49. *
  50. * - _deleteMembers: Frees the memory of dynamically sized members (e.g. a
  51. * string) of a datatype. This is useful when the datatype was allocated on
  52. * the stack, whereas the dynamically sized members is heap-allocated. To
  53. * reuse the variable, the remaining members (not dynamically allocated) need
  54. * to be cleaned up with an _init.
  55. *
  56. * @{
  57. */
  58. /** @brief A two-state logical value (true or false). */
  59. typedef bool UA_Boolean;
  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. *
  103. * A DateTime value is encoded as a 64-bit signed integer which represents the
  104. * number of 100 nanosecond intervals since January 1, 1601 (UTC).
  105. */
  106. typedef UA_Int64 UA_DateTime; // 100 nanosecond resolution
  107. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  108. typedef struct {
  109. UA_UInt32 data1;
  110. UA_UInt16 data2;
  111. UA_UInt16 data3;
  112. UA_Byte data4[8];
  113. } UA_Guid;
  114. /** @brief A sequence of octets. */
  115. typedef UA_String UA_ByteString;
  116. /** @brief An XML element. */
  117. typedef UA_String UA_XmlElement;
  118. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  119. /* The shortened numeric types are introduced during encoding. */
  120. typedef struct {
  121. UA_UInt16 namespaceIndex;
  122. enum {
  123. UA_NODEIDTYPE_NUMERIC = 2,
  124. UA_NODEIDTYPE_STRING = 3,
  125. UA_NODEIDTYPE_GUID = 4,
  126. UA_NODEIDTYPE_BYTESTRING = 5
  127. } identifierType;
  128. union {
  129. UA_UInt32 numeric;
  130. UA_String string;
  131. UA_Guid guid;
  132. UA_ByteString byteString;
  133. } identifier;
  134. } UA_NodeId;
  135. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  136. typedef struct {
  137. UA_NodeId nodeId;
  138. UA_String namespaceUri; // not encoded if length=-1
  139. UA_UInt32 serverIndex; // not encoded if 0
  140. } UA_ExpandedNodeId;
  141. #include "ua_statuscodes.h"
  142. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  143. typedef enum UA_StatusCode UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  144. /** @brief A name qualified by a namespace. */
  145. typedef struct {
  146. UA_UInt16 namespaceIndex;
  147. UA_String name;
  148. } UA_QualifiedName;
  149. /** @brief Human readable text with an optional locale identifier. */
  150. typedef struct {
  151. UA_String locale;
  152. UA_String text;
  153. } UA_LocalizedText;
  154. /** @brief A structure that contains an application specific data type that may
  155. not be recognized by the receiver. */
  156. typedef struct {
  157. UA_NodeId typeId;
  158. enum {
  159. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0,
  160. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 1,
  161. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 2
  162. } encoding;
  163. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  164. } UA_ExtensionObject;
  165. struct UA_TypeVTable; // forward declaration
  166. typedef struct UA_TypeVTable UA_TypeVTable;
  167. /** @brief Pointers to data that is stored in memory. The "type" of the data is
  168. stored in the variant itself. */
  169. typedef struct {
  170. UA_Int32 arrayLength; // total number of elements in the data-pointer
  171. void *dataPtr;
  172. UA_Int32 arrayDimensionsLength;
  173. UA_Int32 *arrayDimensions;
  174. } UA_VariantData;
  175. /** @brief A datasource is the interface to interact with a local data provider.
  176. *
  177. * Implementors of datasources need to provide functions for the callbacks in
  178. * this structure. As a rule, datasources are never copied, but only their
  179. * content. The only way to write into a datasource is via the write-service. */
  180. typedef struct {
  181. const void *identifier; /**< whatever id the datasource uses internally. Can be ignored if the datasource functions do not use it. */
  182. UA_Int32 (*read)(const void *identifier, const UA_VariantData **); /**< Get the current data from the datasource. When it is no longer used, the data has to be relased. */
  183. void (*release)(const void *identifier, const UA_VariantData *); /**< For concurrent access, the datasource needs to know when the last reader releases replaced/deleted data. Release decreases the reference count. */
  184. UA_Int32 (*write)(const void **identifier, const UA_VariantData *); /**< Replace the data in the datasource. Also used to replace the identifier pointer for lookups. Do not free the variantdata afterwards! */
  185. void (*delete)(const void *identifier); /**< Indicates that the node with the datasource was removed from the namespace. */
  186. } UA_VariantDataSource;
  187. /** @brief Variants store (arrays of) any data type. Either they provide a
  188. pointer to the data in memory, or functions from which the data can be
  189. accessed. */
  190. typedef struct {
  191. const UA_TypeVTable *vt; /// The VTable of the datatype in question
  192. enum {
  193. UA_VARIANT_DATA, ///< The data is stored in memory and "owned" by this variant
  194. UA_VARIANT_DATA_NODELETE, ///< The data is stored in memory, but only "borrowed" and shall not be deleted at the end of this variant's lifecycle
  195. UA_VARIANT_DATASOURCE ///< The data is provided externally. Call the functions in the datasource to get a current version
  196. } storageType;
  197. union {
  198. UA_VariantData data;
  199. UA_VariantDataSource datasource;
  200. } storage;
  201. } UA_Variant;
  202. /** @brief A data value with an associated status code and timestamps. */
  203. typedef struct {
  204. UA_Byte encodingMask;
  205. UA_Variant value;
  206. UA_StatusCode status;
  207. UA_DateTime sourceTimestamp;
  208. UA_Int16 sourcePicoseconds;
  209. UA_DateTime serverTimestamp;
  210. UA_Int16 serverPicoseconds;
  211. } UA_DataValue;
  212. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  213. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  214. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  215. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  216. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  217. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  218. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  219. };
  220. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  221. typedef struct UA_DiagnosticInfo {
  222. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  223. UA_Int32 symbolicId;
  224. UA_Int32 namespaceUri;
  225. UA_Int32 localizedText;
  226. UA_Int32 locale;
  227. UA_String additionalInfo;
  228. UA_StatusCode innerStatusCode;
  229. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  230. } UA_DiagnosticInfo;
  231. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  232. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  233. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  234. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  235. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  236. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  237. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  238. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  239. };
  240. /** @brief Type use internally to denote an invalid datatype. */
  241. typedef void UA_InvalidType;
  242. /*************/
  243. /* Functions */
  244. /*************/
  245. #ifdef UA_DEBUG
  246. #define PRINTTYPE(TYPE) void UA_EXPORT TYPE##_print(const TYPE *p, FILE *stream);
  247. #define PRINTTYPE_NOEXPORT(TYPE) void TYPE##_print(const TYPE *p, FILE *stream);
  248. #else
  249. #define PRINTTYPE(TYPE)
  250. #define PRINTTYPE_NOEXPORT(TYPE)
  251. #endif
  252. #define UA_TYPE_PROTOTYPES(TYPE) \
  253. TYPE UA_EXPORT * TYPE##_new(void); \
  254. void UA_EXPORT TYPE##_init(TYPE * p); \
  255. void UA_EXPORT TYPE##_delete(TYPE * p); \
  256. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  257. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst); \
  258. PRINTTYPE(TYPE)
  259. #define UA_TYPE_PROTOTYPES_NOEXPORT(TYPE) \
  260. TYPE * TYPE##_new(void); \
  261. void TYPE##_init(TYPE * p); \
  262. void TYPE##_delete(TYPE * p); \
  263. void TYPE##_deleteMembers(TYPE * p); \
  264. UA_StatusCode TYPE##_copy(const TYPE *src, TYPE *dst); \
  265. PRINTTYPE_NOEXPORT(TYPE)
  266. /* Functions for all types */
  267. UA_TYPE_PROTOTYPES(UA_Boolean)
  268. UA_TYPE_PROTOTYPES(UA_SByte)
  269. UA_TYPE_PROTOTYPES(UA_Byte)
  270. UA_TYPE_PROTOTYPES(UA_Int16)
  271. UA_TYPE_PROTOTYPES(UA_UInt16)
  272. UA_TYPE_PROTOTYPES(UA_Int32)
  273. UA_TYPE_PROTOTYPES(UA_UInt32)
  274. UA_TYPE_PROTOTYPES(UA_Int64)
  275. UA_TYPE_PROTOTYPES(UA_UInt64)
  276. UA_TYPE_PROTOTYPES(UA_Float)
  277. UA_TYPE_PROTOTYPES(UA_Double)
  278. UA_TYPE_PROTOTYPES(UA_String)
  279. UA_TYPE_PROTOTYPES(UA_DateTime)
  280. UA_TYPE_PROTOTYPES(UA_Guid)
  281. UA_TYPE_PROTOTYPES(UA_ByteString)
  282. UA_TYPE_PROTOTYPES(UA_XmlElement)
  283. UA_TYPE_PROTOTYPES(UA_NodeId)
  284. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  285. UA_TYPE_PROTOTYPES(UA_StatusCode)
  286. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  287. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  288. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  289. UA_TYPE_PROTOTYPES(UA_DataValue)
  290. UA_TYPE_PROTOTYPES(UA_Variant)
  291. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  292. UA_TYPE_PROTOTYPES(UA_InvalidType)
  293. /**********************************************/
  294. /* Custom functions for the builtin datatypes */
  295. /**********************************************/
  296. /* String */
  297. #define UA_STRING_NULL (UA_String) {-1, (UA_Byte*)0 }
  298. #define UA_STRING_STATIC(VARIABLE, STRING) do { \
  299. VARIABLE.length = sizeof(STRING)-1; \
  300. VARIABLE.data = (UA_Byte *)STRING; } while(0)
  301. UA_StatusCode UA_EXPORT UA_String_copycstring(char const *src, UA_String *dst);
  302. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  303. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  304. #ifdef UA_DEBUG
  305. void UA_EXPORT UA_String_printf(char const *label, const UA_String *string);
  306. void UA_EXPORT UA_String_printx(char const *label, const UA_String *string);
  307. void UA_EXPORT UA_String_printx_hex(char const *label, const UA_String *string);
  308. #endif
  309. /* DateTime */
  310. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  311. typedef struct UA_DateTimeStruct {
  312. UA_Int16 nanoSec;
  313. UA_Int16 microSec;
  314. UA_Int16 milliSec;
  315. UA_Int16 sec;
  316. UA_Int16 min;
  317. UA_Int16 hour;
  318. UA_Int16 day;
  319. UA_Int16 month;
  320. UA_Int16 year;
  321. } UA_DateTimeStruct;
  322. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  323. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  324. /* Guid */
  325. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  326. /** Do not use for security-critical entropy! */
  327. UA_Guid UA_EXPORT UA_Guid_random(UA_UInt32 *seed);
  328. /* ByteString */
  329. UA_Boolean UA_EXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  330. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  331. #ifdef UA_DEBUG
  332. void UA_EXPORT UA_ByteString_printf(char *label, const UA_ByteString *string);
  333. void UA_EXPORT UA_ByteString_printx(char *label, const UA_ByteString *string);
  334. void UA_EXPORT UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  335. #endif
  336. /* NodeId */
  337. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  338. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  339. /* ExpandedNodeId */
  340. UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  341. /* QualifiedName */
  342. #define UA_QUALIFIEDNAME_STATIC(VARIABLE, STRING) do { \
  343. VARIABLE.namespaceIndex = 0; \
  344. UA_STRING_STATIC(VARIABLE.name, STRING); } while(0)
  345. UA_StatusCode UA_EXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  346. #ifdef UA_DEBUG
  347. void UA_EXPORT UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  348. #endif
  349. /* LocalizedText */
  350. #define UA_LOCALIZEDTEXT_STATIC(VARIABLE, STRING) do { \
  351. UA_STRING_STATIC(VARIABLE.locale, "en"); \
  352. UA_STRING_STATIC(VARIABLE.text, STRING); } while(0)
  353. UA_StatusCode UA_EXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  354. /* Variant */
  355. UA_StatusCode UA_EXPORT UA_Variant_copySetValue(UA_Variant *v, const UA_TypeVTable *vt, const void *value);
  356. UA_StatusCode UA_EXPORT UA_Variant_copySetArray(UA_Variant *v, const UA_TypeVTable *vt, UA_Int32 arrayLength, const void *array);
  357. /* Array operations */
  358. UA_StatusCode UA_EXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_TypeVTable *vt);
  359. void UA_EXPORT UA_Array_init(void *p, UA_Int32 noElements, const UA_TypeVTable *vt);
  360. void UA_EXPORT UA_Array_delete(void *p, UA_Int32 noElements, const UA_TypeVTable *vt);
  361. /* @brief The destination array is allocated with size noElements. */
  362. UA_StatusCode UA_EXPORT UA_Array_copy(const void *src, UA_Int32 noElements, const UA_TypeVTable *vt, void **dst);
  363. #ifdef UA_DEBUG
  364. void UA_EXPORT UA_Array_print(const void *p, UA_Int32 noElements, const UA_TypeVTable *vt, FILE *stream);
  365. #endif
  366. /**********/
  367. /* VTable */
  368. /**********/
  369. typedef struct UA_Encoding {
  370. /** Returns the size of the encoded element.*/
  371. UA_UInt32 (*calcSize)(const void *p);
  372. /** Encodes the type into the destination bytestring. */
  373. UA_StatusCode (*encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  374. /** Decodes a ByteString into an UA datatype. */
  375. UA_StatusCode (*decode)(const UA_ByteString *src, UA_UInt32 *offset, void *dst);
  376. } UA_Encoding;
  377. #define UA_ENCODING_BINARY 0 // Binary encoding is always available
  378. struct UA_TypeVTable {
  379. UA_NodeId typeId;
  380. UA_Byte *name;
  381. void * (*new)(void);
  382. void (*init)(void *p);
  383. UA_StatusCode (*copy)(void const *src, void *dst);
  384. void (*delete)(void *p);
  385. void (*deleteMembers)(void *p);
  386. #ifdef UA_DEBUG
  387. void (*print)(const void *p, FILE *stream);
  388. #endif
  389. UA_UInt32 memSize; // size of the struct
  390. UA_Boolean dynMembers; // does the type contain members that are dynamically on the heap?
  391. UA_Encoding encodings[UA_ENCODING_AMOUNT]; // binary, xml, ... UA_ENCODING_AMOUNT is set by the build script
  392. };
  393. /** @} */
  394. #ifdef __cplusplus
  395. } // extern "C"
  396. #endif
  397. #endif /* UA_TYPES_H_ */