ua_types.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #define UA_TRUE true
  61. #define UA_FALSE false
  62. /** @brief An integer value between -129 and 127. */
  63. typedef int8_t UA_SByte;
  64. #define UA_SBYTE_MAX 127
  65. #define UA_SBYTE_MIN -128
  66. /** @brief An integer value between 0 and 256. */
  67. typedef uint8_t UA_Byte;
  68. #define UA_BYTE_MAX 256
  69. #define UA_BYTE_MIN 0
  70. /** @brief An integer value between -32 768 and 32 767. */
  71. typedef int16_t UA_Int16;
  72. #define UA_INT16_MAX 32767
  73. #define UA_INT16_MIN -32768
  74. /** @brief An integer value between 0 and 65 535. */
  75. typedef uint16_t UA_UInt16;
  76. #define UA_UINT16_MAX 65535
  77. #define UA_UINT16_MIN 0
  78. /** @brief An integer value between -2 147 483 648 and 2 147 483 647. */
  79. typedef int32_t UA_Int32;
  80. #define UA_INT32_MAX 2147483647
  81. #define UA_INT32_MIN −2147483648
  82. /** @brief An integer value between 0 and 429 4967 295. */
  83. typedef uint32_t UA_UInt32;
  84. #define UA_UINT32_MAX 4294967295
  85. #define UA_UINT32_MIN 0
  86. /** @brief An integer value between -10 223 372 036 854 775 808 and 9 223 372 036 854 775 807 */
  87. typedef int64_t UA_Int64;
  88. #define UA_INT64_MAX 9223372036854775807
  89. #define UA_INT64_MIN −9223372036854775808
  90. /** @brief An integer value between 0 and 18 446 744 073 709 551 615. */
  91. typedef uint64_t UA_UInt64;
  92. #define UA_UINT64_MAX = 18446744073709551615
  93. #define UA_UINT64_MIN = 0
  94. /** @brief An IEEE single precision (32 bit) floating point value. */
  95. typedef float UA_Float;
  96. /** @brief An IEEE double precision (64 bit) floating point value. */
  97. typedef double UA_Double;
  98. /** @brief A sequence of Unicode characters. */
  99. typedef struct {
  100. UA_Int32 length;
  101. UA_Byte *data;
  102. } UA_String;
  103. /** @brief An instance in time.
  104. *
  105. * A DateTime value is encoded as a 64-bit signed integer which represents the
  106. * number of 100 nanosecond intervals since January 1, 1601 (UTC).
  107. */
  108. typedef UA_Int64 UA_DateTime; // 100 nanosecond resolution
  109. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  110. typedef struct {
  111. UA_UInt32 data1;
  112. UA_UInt16 data2;
  113. UA_UInt16 data3;
  114. UA_Byte data4[8];
  115. } UA_Guid;
  116. /** @brief A sequence of octets. */
  117. typedef UA_String UA_ByteString;
  118. /** @brief An XML element. */
  119. typedef UA_String UA_XmlElement;
  120. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  121. /* The shortened numeric types are introduced during encoding. */
  122. typedef struct {
  123. UA_UInt16 namespaceIndex;
  124. enum {
  125. UA_NODEIDTYPE_NUMERIC = 2,
  126. UA_NODEIDTYPE_STRING = 3,
  127. UA_NODEIDTYPE_GUID = 4,
  128. UA_NODEIDTYPE_BYTESTRING = 5
  129. } identifierType;
  130. union {
  131. UA_UInt32 numeric;
  132. UA_String string;
  133. UA_Guid guid;
  134. UA_ByteString byteString;
  135. } identifier;
  136. } UA_NodeId;
  137. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  138. typedef struct {
  139. UA_NodeId nodeId;
  140. UA_String namespaceUri; // not encoded if length=-1
  141. UA_UInt32 serverIndex; // not encoded if 0
  142. } UA_ExpandedNodeId;
  143. #include "ua_statuscodes.h"
  144. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  145. typedef enum UA_StatusCode UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  146. /** @brief A name qualified by a namespace. */
  147. typedef struct {
  148. UA_UInt16 namespaceIndex;
  149. UA_String name;
  150. } UA_QualifiedName;
  151. /** @brief Human readable text with an optional locale identifier. */
  152. typedef struct {
  153. UA_String locale;
  154. UA_String text;
  155. } UA_LocalizedText;
  156. /** @brief A structure that contains an application specific data type that may
  157. not be recognized by the receiver. */
  158. typedef struct {
  159. UA_NodeId typeId;
  160. enum {
  161. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0,
  162. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 1,
  163. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 2
  164. } encoding;
  165. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  166. } UA_ExtensionObject;
  167. struct UA_TypeVTable; // forward declaration
  168. typedef struct UA_TypeVTable UA_TypeVTable;
  169. /** @brief Pointers to data that is stored in memory. The "type" of the data is
  170. stored in the variant itself. */
  171. typedef struct {
  172. UA_Int32 arrayLength; // total number of elements in the data-pointer
  173. void *dataPtr;
  174. UA_Int32 arrayDimensionsLength;
  175. UA_Int32 *arrayDimensions;
  176. } UA_VariantData;
  177. /** @brief A datasource is the interface to interact with a local data provider.
  178. *
  179. * Implementors of datasources need to provide functions for the callbacks in
  180. * this structure. As a rule, datasources are never copied, but only their
  181. * content. The only way to write into a datasource is via the write-service. */
  182. typedef struct {
  183. const void *identifier; /**< whatever id the datasource uses internally. Can be ignored if the datasource functions do not use it. */
  184. 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. */
  185. 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. */
  186. 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! */
  187. void (*delete)(const void *identifier); /**< Indicates that the node with the datasource was removed from the namespace. */
  188. } UA_VariantDataSource;
  189. /** @brief Variants store (arrays of) any data type. Either they provide a
  190. pointer to the data in memory, or functions from which the data can be
  191. accessed. */
  192. typedef struct {
  193. const UA_TypeVTable *vt; /// The VTable of the datatype in question
  194. enum {
  195. UA_VARIANT_DATA, ///< The data is stored in memory and "owned" by this variant
  196. 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
  197. UA_VARIANT_DATASOURCE ///< The data is provided externally. Call the functions in the datasource to get a current version
  198. } storageType;
  199. union {
  200. UA_VariantData data;
  201. UA_VariantDataSource datasource;
  202. } storage;
  203. } UA_Variant;
  204. /** @brief A data value with an associated status code and timestamps. */
  205. typedef struct {
  206. UA_Byte encodingMask;
  207. UA_Variant value;
  208. UA_StatusCode status;
  209. UA_DateTime sourceTimestamp;
  210. UA_Int16 sourcePicoseconds;
  211. UA_DateTime serverTimestamp;
  212. UA_Int16 serverPicoseconds;
  213. } UA_DataValue;
  214. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  215. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  216. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  217. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  218. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  219. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  220. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  221. };
  222. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  223. typedef struct UA_DiagnosticInfo {
  224. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  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. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  234. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  235. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  236. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  237. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  238. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  239. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  240. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  241. };
  242. /** @brief Type use internally to denote an invalid datatype. */
  243. typedef void UA_InvalidType;
  244. /*************/
  245. /* Functions */
  246. /*************/
  247. #ifdef UA_DEBUG
  248. #define PRINTTYPE(TYPE) void UA_EXPORT TYPE##_print(const TYPE *p, FILE *stream);
  249. #define PRINTTYPE_NOEXPORT(TYPE) void TYPE##_print(const TYPE *p, FILE *stream);
  250. #else
  251. #define PRINTTYPE(TYPE)
  252. #define PRINTTYPE_NOEXPORT(TYPE)
  253. #endif
  254. #define UA_TYPE_PROTOTYPES(TYPE) \
  255. TYPE UA_EXPORT * TYPE##_new(void); \
  256. void UA_EXPORT TYPE##_init(TYPE * p); \
  257. void UA_EXPORT TYPE##_delete(TYPE * p); \
  258. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  259. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst); \
  260. PRINTTYPE(TYPE)
  261. #define UA_TYPE_PROTOTYPES_NOEXPORT(TYPE) \
  262. TYPE * TYPE##_new(void); \
  263. void TYPE##_init(TYPE * p); \
  264. void TYPE##_delete(TYPE * p); \
  265. void TYPE##_deleteMembers(TYPE * p); \
  266. UA_StatusCode TYPE##_copy(const TYPE *src, TYPE *dst); \
  267. PRINTTYPE_NOEXPORT(TYPE)
  268. /* Functions for all types */
  269. UA_TYPE_PROTOTYPES(UA_Boolean)
  270. UA_TYPE_PROTOTYPES(UA_SByte)
  271. UA_TYPE_PROTOTYPES(UA_Byte)
  272. UA_TYPE_PROTOTYPES(UA_Int16)
  273. UA_TYPE_PROTOTYPES(UA_UInt16)
  274. UA_TYPE_PROTOTYPES(UA_Int32)
  275. UA_TYPE_PROTOTYPES(UA_UInt32)
  276. UA_TYPE_PROTOTYPES(UA_Int64)
  277. UA_TYPE_PROTOTYPES(UA_UInt64)
  278. UA_TYPE_PROTOTYPES(UA_Float)
  279. UA_TYPE_PROTOTYPES(UA_Double)
  280. UA_TYPE_PROTOTYPES(UA_String)
  281. UA_TYPE_PROTOTYPES(UA_DateTime)
  282. UA_TYPE_PROTOTYPES(UA_Guid)
  283. UA_TYPE_PROTOTYPES(UA_ByteString)
  284. UA_TYPE_PROTOTYPES(UA_XmlElement)
  285. UA_TYPE_PROTOTYPES(UA_NodeId)
  286. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  287. UA_TYPE_PROTOTYPES(UA_StatusCode)
  288. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  289. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  290. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  291. UA_TYPE_PROTOTYPES(UA_DataValue)
  292. UA_TYPE_PROTOTYPES(UA_Variant)
  293. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  294. UA_TYPE_PROTOTYPES(UA_InvalidType)
  295. /**********************************************/
  296. /* Custom functions for the builtin datatypes */
  297. /**********************************************/
  298. /* String */
  299. #define UA_STRING_NULL (UA_String) {-1, (UA_Byte*)0 }
  300. #define UA_STRING_STATIC(VARIABLE, STRING) do { \
  301. VARIABLE.length = sizeof(STRING)-1; \
  302. VARIABLE.data = (UA_Byte *)STRING; } while(0)
  303. UA_StatusCode UA_EXPORT UA_String_copycstring(char const *src, UA_String *dst);
  304. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  305. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  306. #ifdef UA_DEBUG
  307. void UA_EXPORT UA_String_printf(char const *label, const UA_String *string);
  308. void UA_EXPORT UA_String_printx(char const *label, const UA_String *string);
  309. void UA_EXPORT UA_String_printx_hex(char const *label, const UA_String *string);
  310. #endif
  311. /* DateTime */
  312. UA_DateTime UA_EXPORT UA_DateTime_now(void);
  313. typedef struct UA_DateTimeStruct {
  314. UA_Int16 nanoSec;
  315. UA_Int16 microSec;
  316. UA_Int16 milliSec;
  317. UA_Int16 sec;
  318. UA_Int16 min;
  319. UA_Int16 hour;
  320. UA_Int16 day;
  321. UA_Int16 month;
  322. UA_Int16 year;
  323. } UA_DateTimeStruct;
  324. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  325. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  326. /* Guid */
  327. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  328. /** Do not use for security-critical entropy! */
  329. UA_Guid UA_EXPORT UA_Guid_random(UA_UInt32 *seed);
  330. /* ByteString */
  331. UA_Boolean UA_EXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  332. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  333. #ifdef UA_DEBUG
  334. void UA_EXPORT UA_ByteString_printf(char *label, const UA_ByteString *string);
  335. void UA_EXPORT UA_ByteString_printx(char *label, const UA_ByteString *string);
  336. void UA_EXPORT UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  337. #endif
  338. /* NodeId */
  339. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  340. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  341. /* ExpandedNodeId */
  342. UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  343. /* QualifiedName */
  344. #define UA_QUALIFIEDNAME_STATIC(VARIABLE, STRING) do { \
  345. VARIABLE.namespaceIndex = 0; \
  346. UA_STRING_STATIC(VARIABLE.name, STRING); } while(0)
  347. UA_StatusCode UA_EXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  348. #ifdef UA_DEBUG
  349. void UA_EXPORT UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  350. #endif
  351. /* LocalizedText */
  352. #define UA_LOCALIZEDTEXT_STATIC(VARIABLE, STRING) do { \
  353. UA_STRING_STATIC(VARIABLE.locale, "en"); \
  354. UA_STRING_STATIC(VARIABLE.text, STRING); } while(0)
  355. UA_StatusCode UA_EXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  356. /* Variant */
  357. UA_StatusCode UA_EXPORT UA_Variant_copySetValue(UA_Variant *v, const UA_TypeVTable *vt, const void *value);
  358. UA_StatusCode UA_EXPORT UA_Variant_copySetArray(UA_Variant *v, const UA_TypeVTable *vt, UA_Int32 arrayLength, const void *array);
  359. /* Array operations */
  360. UA_StatusCode UA_EXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_TypeVTable *vt);
  361. void UA_EXPORT UA_Array_init(void *p, UA_Int32 noElements, const UA_TypeVTable *vt);
  362. void UA_EXPORT UA_Array_delete(void *p, UA_Int32 noElements, const UA_TypeVTable *vt);
  363. /* @brief The destination array is allocated with size noElements. */
  364. UA_StatusCode UA_EXPORT UA_Array_copy(const void *src, UA_Int32 noElements, const UA_TypeVTable *vt, void **dst);
  365. #ifdef UA_DEBUG
  366. void UA_EXPORT UA_Array_print(const void *p, UA_Int32 noElements, const UA_TypeVTable *vt, FILE *stream);
  367. #endif
  368. /**********/
  369. /* VTable */
  370. /**********/
  371. typedef struct UA_Encoding {
  372. /** Returns the size of the encoded element.*/
  373. UA_UInt32 (*calcSize)(const void *p);
  374. /** Encodes the type into the destination bytestring. */
  375. UA_StatusCode (*encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  376. /** Decodes a ByteString into an UA datatype. */
  377. UA_StatusCode (*decode)(const UA_ByteString *src, UA_UInt32 *offset, void *dst);
  378. } UA_Encoding;
  379. #define UA_ENCODING_BINARY 0 // Binary encoding is always available
  380. struct UA_TypeVTable {
  381. UA_NodeId typeId;
  382. UA_Byte *name;
  383. void * (*new)(void);
  384. void (*init)(void *p);
  385. UA_StatusCode (*copy)(void const *src, void *dst);
  386. void (*delete)(void *p);
  387. void (*deleteMembers)(void *p);
  388. #ifdef UA_DEBUG
  389. void (*print)(const void *p, FILE *stream);
  390. #endif
  391. UA_UInt32 memSize; // size of the struct
  392. UA_Boolean dynMembers; // does the type contain members that are dynamically on the heap?
  393. UA_Encoding encodings[UA_ENCODING_AMOUNT]; // binary, xml, ... UA_ENCODING_AMOUNT is set by the build script
  394. };
  395. /** @} */
  396. #ifdef __cplusplus
  397. } // extern "C"
  398. #endif
  399. #endif /* UA_TYPES_H_ */