ua_types.h 17 KB

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