ua_types.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 "ua_config.h"
  22. /**
  23. * @defgroup types Datatypes
  24. *
  25. * @brief This module describes the datatypes used in OPC UA. There is a
  26. * division into built-in datatypes (integers, strings, etc.) and more complex
  27. * datatypes that are comprise of built-in datatypes (structs defined in the OPC
  28. * UA standard).
  29. *
  30. * All datatypes follow the same schema in the naming of relevant functions.
  31. *
  32. * - <type>_init: Sets all values to a "safe" standard. For example, if the
  33. * datatype contains a string-element, its size will be set to zero.
  34. *
  35. * - <type>_new: Allocates the memory for the type and runs <type>_init on the
  36. * returned pointer.
  37. *
  38. * - <type>_copy: Copies a datatype. This performs a deep copy that iterates
  39. * over the members. The copy function assumes that the target structure can
  40. * be overwritten (do a deleteMembers before if necessary). With one
  41. * exception: copying into a variant that points to an external datasource is
  42. * not permitted.
  43. *
  44. * - <type>_delete: Frees the memory where the datatype was stored.
  45. *
  46. * - <type>_deleteMembers: Frees the memory of dynamically sized members (e.g. a
  47. * string) of a datatype. This is useful when the datatype was allocated on
  48. * the stack, whereas the dynamically sized members is heap-allocated.
  49. *
  50. * @{
  51. */
  52. #define UA_NULL ((void *)0)
  53. /* Function return values */
  54. #define UA_SUCCESS 0
  55. #define UA_NO_ERROR UA_SUCCESS
  56. #define UA_ERROR (0x01 << 31)
  57. #define UA_ERR_INCONSISTENT (UA_ERROR | (0x01 << 1))
  58. #define UA_ERR_INVALID_VALUE (UA_ERROR | (0x01 << 2))
  59. #define UA_ERR_NO_MEMORY (UA_ERROR | (0x01 << 3))
  60. #define UA_ERR_NOT_IMPLEMENTED (UA_ERROR | (0x01 << 4))
  61. /* Boolean values and null */
  62. #define UA_TRUE (42 == 42)
  63. #define UA_FALSE (!UA_TRUE)
  64. /* Compare values */
  65. typedef enum UA_EQUALITY {
  66. UA_EQUAL,
  67. UA_NOT_EQUAL
  68. } UA_EQUALITY;
  69. /** @brief A two-state logical value (true or false). */
  70. typedef _Bool UA_Boolean;
  71. /** @brief An integer value between -129 and 127. */
  72. typedef int8_t UA_SByte;
  73. #define UA_SBYTE_MAX -128
  74. #define UA_SBYTE_MIN 127
  75. /** @brief An integer value between 0 and 256. */
  76. typedef uint8_t UA_Byte;
  77. #define UA_BYTE_MAX 256
  78. #define UA_BYTE_MIN 0
  79. /** @brief An integer value between -32 768 and 32 767. */
  80. typedef int16_t UA_Int16;
  81. #define UA_INT16_MAX 32767
  82. #define UA_INT16_MIN -32768
  83. /** @brief An integer value between 0 and 65 535. */
  84. typedef uint16_t UA_UInt16;
  85. #define UA_UINT16_MAX 65535
  86. #define UA_UINT16_MIN 0
  87. /** @brief An integer value between -2 147 483 648 and 2 147 483 647. */
  88. typedef int32_t UA_Int32;
  89. #define UA_INT32_MAX 2147483647
  90. #define UA_INT32_MIN −2147483648
  91. /** @brief An integer value between 0 and 429 4967 295. */
  92. typedef uint32_t UA_UInt32;
  93. #define UA_UINT32_MAX 4294967295
  94. #define UA_UINT32_MIN 0
  95. /** @brief An integer value between -10 223 372 036 854 775 808 and 9 223 372 036 854 775 807 */
  96. typedef int64_t UA_Int64;
  97. #define UA_INT64_MAX 9223372036854775807
  98. #define UA_INT64_MIN −9223372036854775808
  99. /** @brief An integer value between 0 and 18 446 744 073 709 551 615. */
  100. typedef uint64_t UA_UInt64;
  101. #define UA_UINT64_MAX = 18446744073709551615
  102. #define UA_UINT64_MIN = 0
  103. /** @brief An IEEE single precision (32 bit) floating point value. */
  104. typedef float UA_Float;
  105. /** @brief An IEEE double precision (64 bit) floating point value. */
  106. typedef double UA_Double;
  107. /** @brief A sequence of Unicode characters. */
  108. typedef struct UA_String {
  109. UA_Int32 length;
  110. UA_Byte *data;
  111. } UA_String;
  112. /** @brief An instance in time. */
  113. typedef UA_Int64 UA_DateTime; //100 nanosecond resolution
  114. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  115. typedef struct UA_Guid {
  116. UA_UInt32 data1;
  117. UA_UInt16 data2;
  118. UA_UInt16 data3;
  119. UA_Byte data4[8];
  120. } UA_Guid;
  121. /** @brief A sequence of octets. */
  122. typedef struct UA_String UA_ByteString;
  123. /** @brief An XML element. */
  124. typedef struct UA_String UA_XmlElement;
  125. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  126. /* The shortened numeric types are introduced during encoding. */
  127. typedef struct UA_NodeId {
  128. UA_UInt16 namespaceIndex;
  129. enum {
  130. UA_NODEIDTYPE_NUMERIC = 2,
  131. UA_NODEIDTYPE_STRING = 3,
  132. UA_NODEIDTYPE_GUID = 4,
  133. UA_NODEIDTYPE_BYTESTRING = 5
  134. } identifierType;
  135. union {
  136. UA_UInt32 numeric;
  137. UA_String string;
  138. UA_Guid guid;
  139. UA_ByteString byteString;
  140. } identifier;
  141. } UA_NodeId;
  142. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  143. typedef struct UA_ExpandedNodeId {
  144. UA_NodeId nodeId;
  145. UA_String namespaceUri; // not encoded if length=-1
  146. UA_UInt32 serverIndex; // not encoded if 0
  147. } UA_ExpandedNodeId;
  148. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  149. typedef UA_UInt32 UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  150. /** @brief A name qualified by a namespace. */
  151. typedef struct UA_QualifiedName {
  152. UA_UInt16 namespaceIndex;
  153. UA_String name;
  154. } UA_QualifiedName;
  155. /** @brief Human readable text with an optional locale identifier. */
  156. typedef struct UA_LocalizedText {
  157. UA_String locale;
  158. UA_String text;
  159. } UA_LocalizedText;
  160. /** @brief A structure that contains an application specific data type that may
  161. not be recognized by the receiver. */
  162. typedef struct UA_ExtensionObject {
  163. UA_NodeId typeId;
  164. enum {
  165. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0,
  166. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 1,
  167. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 2
  168. } encoding;
  169. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  170. } UA_ExtensionObject;
  171. struct UA_VTable_Entry; // forwards declaration
  172. typedef struct UA_VTable_Entry UA_VTable_Entry;
  173. /* Variant */
  174. /** @defgroup variant Variant
  175. *
  176. * @brief Variants may contain (arrays of) any other datatype.
  177. *
  178. * For that, we provide a reference to the utility functions of the type as well
  179. * as possible encoding by pointing an entry in the vtable of types. References
  180. * may either contain a structure with the variants data, or a datasource where
  181. * this structure may be obtained (using reference counting for async access).
  182. *
  183. * @{
  184. */
  185. typedef struct UA_VariantData {
  186. UA_Int32 arrayLength; // total number of elements in the data-pointer
  187. void *dataPtr;
  188. UA_Int32 arrayDimensionsLength;
  189. UA_Int32 *arrayDimensions;
  190. } UA_VariantData;
  191. /** @brief A datasource is the interface to interact with a local data provider.
  192. *
  193. * Implementors of datasources need to provide functions for the callbacks in
  194. * this structure. As a rule, datasources are never copied, but only their
  195. * content. The only way to write into a datasource is via the write-service. */
  196. typedef struct UA_VariantDataSource {
  197. const void *identifier; /**< whatever id the datasource uses internally. Can be ignored if the datasource functions do not use it. */
  198. 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. */
  199. 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. */
  200. 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! */
  201. void (*delete)(const void *identifier); /**< Indicates that the node with the datasource was removed from the namespace. */
  202. } UA_VariantDataSource;
  203. /** @brief A union of all of the types specified above.
  204. *
  205. * Variants store (arrays of) built-in types. If you want to store a more
  206. * complex (or self-defined) type, you have to use an UA_ExtensionObject.*/
  207. typedef struct UA_Variant {
  208. const UA_VTable_Entry *vt; // internal entry into vTable
  209. enum {
  210. UA_VARIANT_DATA,
  211. UA_VARIANT_DATA_NODELETE, // do not free the data (e.g. because it is "borrowed" and points into a larger structure)
  212. UA_VARIANT_DATASOURCE
  213. } storageType;
  214. union {
  215. UA_VariantData data;
  216. UA_VariantDataSource datasource;
  217. } storage;
  218. } UA_Variant;
  219. /** @} */
  220. /** @brief A data value with an associated status code and timestamps. */
  221. typedef struct UA_DataValue {
  222. UA_Byte encodingMask;
  223. UA_Variant value;
  224. UA_StatusCode status;
  225. UA_DateTime sourceTimestamp;
  226. UA_Int16 sourcePicoseconds;
  227. UA_DateTime serverTimestamp;
  228. UA_Int16 serverPicoseconds;
  229. } UA_DataValue;
  230. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  231. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  232. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  233. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  234. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  235. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  236. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  237. };
  238. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  239. typedef struct UA_DiagnosticInfo {
  240. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  241. UA_Int32 symbolicId;
  242. UA_Int32 namespaceUri;
  243. UA_Int32 localizedText;
  244. UA_Int32 locale;
  245. UA_String additionalInfo;
  246. UA_StatusCode innerStatusCode;
  247. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  248. } UA_DiagnosticInfo;
  249. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  250. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  251. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  252. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  253. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  254. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  255. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  256. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  257. };
  258. /** @brief Type use internally to denote an invalid datatype. */
  259. typedef void UA_InvalidType;
  260. /*************/
  261. /* Functions */
  262. /*************/
  263. #ifdef DEBUG
  264. #define UA_TYPE_PROTOTYPES(TYPE) \
  265. UA_Int32 UA_LIBEXPORT TYPE##_new(TYPE **p); \
  266. void UA_LIBEXPORT TYPE##_init(TYPE * p); \
  267. void UA_LIBEXPORT TYPE##_delete(TYPE * p); \
  268. void UA_LIBEXPORT TYPE##_deleteMembers(TYPE * p); \
  269. UA_Int32 UA_LIBEXPORT TYPE##_copy(const TYPE *src, TYPE *dst); \
  270. void UA_LIBEXPORT TYPE##_print(const TYPE *p, FILE *stream);
  271. #else
  272. #define UA_TYPE_PROTOTYPES(TYPE) \
  273. UA_Int32 UA_LIBEXPORT TYPE##_new(TYPE **p); \
  274. void UA_LIBEXPORT TYPE##_init(TYPE * p); \
  275. void UA_LIBEXPORT TYPE##_delete(TYPE * p); \
  276. void UA_LIBEXPORT TYPE##_deleteMembers(TYPE * p); \
  277. UA_Int32 UA_LIBEXPORT TYPE##_copy(const TYPE *src, TYPE *dst);
  278. #endif
  279. /* Functions for all types */
  280. UA_TYPE_PROTOTYPES(UA_Boolean)
  281. UA_TYPE_PROTOTYPES(UA_SByte)
  282. UA_TYPE_PROTOTYPES(UA_Byte)
  283. UA_TYPE_PROTOTYPES(UA_Int16)
  284. UA_TYPE_PROTOTYPES(UA_UInt16)
  285. UA_TYPE_PROTOTYPES(UA_Int32)
  286. UA_TYPE_PROTOTYPES(UA_UInt32)
  287. UA_TYPE_PROTOTYPES(UA_Int64)
  288. UA_TYPE_PROTOTYPES(UA_UInt64)
  289. UA_TYPE_PROTOTYPES(UA_Float)
  290. UA_TYPE_PROTOTYPES(UA_Double)
  291. UA_TYPE_PROTOTYPES(UA_String)
  292. UA_TYPE_PROTOTYPES(UA_DateTime)
  293. UA_TYPE_PROTOTYPES(UA_Guid)
  294. UA_TYPE_PROTOTYPES(UA_ByteString)
  295. UA_TYPE_PROTOTYPES(UA_XmlElement)
  296. UA_TYPE_PROTOTYPES(UA_NodeId)
  297. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  298. UA_TYPE_PROTOTYPES(UA_StatusCode)
  299. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  300. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  301. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  302. UA_TYPE_PROTOTYPES(UA_DataValue)
  303. UA_TYPE_PROTOTYPES(UA_Variant)
  304. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  305. UA_TYPE_PROTOTYPES(UA_InvalidType)
  306. /* String */
  307. #define UA_STRING_NULL (UA_String) {-1, UA_NULL }
  308. #define UA_STRING_STATIC(VARIABLE, STRING) do { \
  309. VARIABLE.length = sizeof(STRING)-1; \
  310. VARIABLE.data = (UA_Byte *)STRING; } while(0)
  311. UA_Int32 UA_LIBEXPORT UA_String_copycstring(char const *src, UA_String *dst);
  312. UA_Int32 UA_LIBEXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  313. UA_EQUALITY UA_LIBEXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  314. #ifdef DEBUG
  315. void UA_LIBEXPORT UA_String_printf(char const *label, const UA_String *string);
  316. void UA_LIBEXPORT UA_String_printx(char const *label, const UA_String *string);
  317. void UA_LIBEXPORT UA_String_printx_hex(char const *label, const UA_String *string);
  318. #endif
  319. /* DateTime */
  320. UA_DateTime UA_LIBEXPORT UA_DateTime_now();
  321. typedef struct UA_DateTimeStruct {
  322. UA_Int16 nanoSec;
  323. UA_Int16 microSec;
  324. UA_Int16 milliSec;
  325. UA_Int16 sec;
  326. UA_Int16 min;
  327. UA_Int16 hour;
  328. UA_Int16 day;
  329. UA_Int16 mounth;
  330. UA_Int16 year;
  331. } UA_DateTimeStruct;
  332. UA_DateTimeStruct UA_LIBEXPORT UA_DateTime_toStruct(UA_DateTime time);
  333. UA_Int32 UA_LIBEXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  334. /* Guid */
  335. UA_EQUALITY UA_LIBEXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  336. /* ByteString */
  337. UA_EQUALITY UA_LIBEXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  338. UA_Int32 UA_LIBEXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  339. #ifdef DEBUG
  340. void UA_LIBEXPORT UA_ByteString_printf(char *label, const UA_ByteString *string);
  341. void UA_LIBEXPORT UA_ByteString_printx(char *label, const UA_ByteString *string);
  342. void UA_LIBEXPORT UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  343. #endif
  344. /* NodeId */
  345. UA_EQUALITY UA_LIBEXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  346. UA_Boolean UA_LIBEXPORT UA_NodeId_isNull(const UA_NodeId *p);
  347. UA_Boolean UA_LIBEXPORT UA_NodeId_isBasicType(UA_NodeId const *id);
  348. #define NS0NODEID(NUMERIC_ID) \
  349. (UA_NodeId) {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = \
  350. NUMERIC_ID }
  351. /* ExpandedNodeId */
  352. UA_Boolean UA_LIBEXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  353. #define NS0EXPANDEDNODEID(VARIABLE, NUMERIC_ID) do { \
  354. VARIABLE.nodeId = NS0NODEID(NUMERIC_ID); \
  355. VARIABLE.namespaceUri = UA_STRING_NULL; \
  356. VARIABLE.serverIndex = 0; } while(0)
  357. /* QualifiedName */
  358. #define UA_QUALIFIEDNAME_STATIC(VARIABLE, STRING) do { \
  359. VARIABLE.namespaceIndex = 0; \
  360. UA_STRING_STATIC(VARIABLE.name, STRING); } while(0)
  361. UA_Int32 UA_LIBEXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  362. #ifdef DEBUG
  363. void UA_LIBEXPORT UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  364. #endif
  365. /* LocalizedText */
  366. #define UA_LOCALIZEDTEXT_STATIC(VARIABLE, STRING) do { \
  367. UA_STRING_STATIC(VARIABLE.locale, "en"); \
  368. UA_STRING_STATIC(VARIABLE.text, STRING); } while(0)
  369. UA_Int32 UA_LIBEXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  370. /* Variant */
  371. UA_Int32 UA_LIBEXPORT UA_Variant_copySetValue(UA_Variant *v, const UA_VTable_Entry *vt, const void *value);
  372. UA_Int32 UA_LIBEXPORT UA_Variant_copySetArray(UA_Variant *v, const UA_VTable_Entry *vt, UA_Int32 arrayLength, const void *array);
  373. /* Array operations */
  374. UA_Int32 UA_LIBEXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  375. void UA_LIBEXPORT UA_Array_init(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  376. void UA_LIBEXPORT UA_Array_delete(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  377. /* @brief The destination array is allocated according to noElements. */
  378. UA_Int32 UA_LIBEXPORT UA_Array_copy(const void *src, UA_Int32 noElements, const UA_VTable_Entry *vt, void **dst);
  379. #ifdef DEBUG
  380. void UA_LIBEXPORT UA_Array_print(const void *p, UA_Int32 noElements, const UA_VTable_Entry *vt, FILE *stream);
  381. #endif
  382. /**********/
  383. /* VTable */
  384. /**********/
  385. typedef struct UA_Encoding {
  386. /** Returns the size of the encoded element.*/
  387. UA_Int32 (*calcSize)(const void *p);
  388. /** Encodes the type into the destination bytestring. */
  389. UA_Int32 (*encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  390. /** Decodes a ByteString into an UA datatype. */
  391. UA_Int32 (*decode)(const UA_ByteString *src, UA_UInt32 *offset, void *dst);
  392. } UA_Encoding;
  393. #define UA_ENCODING_BINARY 0 // Binary encoding is always available
  394. struct UA_VTable_Entry {
  395. UA_NodeId typeId;
  396. UA_Byte *name;
  397. UA_Int32 (*new)(void **p);
  398. void (*init)(void *p);
  399. UA_Int32 (*copy)(void const *src, void *dst);
  400. void (*delete)(void *p);
  401. void (*deleteMembers)(void *p);
  402. #ifdef DEBUG
  403. void (*print)(const void *p, FILE *stream);
  404. #endif
  405. UA_UInt32 memSize; // size of the struct only in memory (no dynamic components)
  406. UA_Boolean dynMembers; // does the type contain members that are dynamically
  407. // allocated (strings, ..)? Otherwise, the size on
  408. // the wire == size in memory.
  409. UA_Encoding encodings[UA_ENCODING_AMOUNT]; // binary, xml, ... UA_ENCODING_AMOUNT is set by the build script
  410. };
  411. /***********************************/
  412. /* Macros for type implementations */
  413. /***********************************/
  414. #define UA_TYPE_DEFAULT(TYPE) \
  415. UA_TYPE_DELETE_DEFAULT(TYPE) \
  416. UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  417. UA_TYPE_INIT_DEFAULT(TYPE) \
  418. UA_TYPE_NEW_DEFAULT(TYPE) \
  419. UA_TYPE_COPY_DEFAULT(TYPE) \
  420. #define UA_TYPE_NEW_DEFAULT(TYPE) \
  421. UA_Int32 TYPE##_new(TYPE **p) { \
  422. if(UA_alloc((void **)p, sizeof(TYPE)) != UA_SUCCESS) \
  423. return UA_ERROR; \
  424. TYPE##_init(*p); \
  425. return UA_SUCCESS; \
  426. }
  427. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  428. void TYPE##_init(TYPE * p) { \
  429. if(!p) return; \
  430. *p = (TYPE)0; \
  431. }
  432. #define UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  433. void TYPE##_init(TYPE * p) { \
  434. TYPE_AS##_init((TYPE_AS *)p); \
  435. }
  436. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  437. void TYPE##_delete(TYPE *p) { \
  438. if(!p) return; \
  439. TYPE##_deleteMembers(p); \
  440. UA_free(p); \
  441. }
  442. #define UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  443. void TYPE##_delete(TYPE * p) { \
  444. TYPE_AS##_delete((TYPE_AS *)p); \
  445. }
  446. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  447. void TYPE##_deleteMembers(TYPE * p) { return; }
  448. #define UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  449. void TYPE##_deleteMembers(TYPE * p) { TYPE_AS##_deleteMembers((TYPE_AS *)p); }
  450. /* Use only when the type has no arrays. Otherwise, implement deep copy */
  451. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  452. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  453. if(src == UA_NULL || dst == UA_NULL) return UA_ERROR; \
  454. *dst = *src; \
  455. return UA_SUCCESS; \
  456. }
  457. #define UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  458. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  459. return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
  460. }
  461. #define UA_TYPE_PRINT_AS(TYPE, TYPE_AS) \
  462. void TYPE##_print(TYPE const *p, FILE *stream) { \
  463. TYPE_AS##_print((TYPE_AS *)p, stream); \
  464. } \
  465. #ifdef DEBUG //print functions only in debug mode
  466. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  467. UA_TYPE_NEW_DEFAULT(TYPE) \
  468. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  469. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  470. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  471. UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  472. UA_TYPE_PRINT_AS(TYPE, TYPE_AS)
  473. #else
  474. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  475. UA_TYPE_NEW_DEFAULT(TYPE) \
  476. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  477. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  478. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  479. UA_TYPE_COPY_AS(TYPE, TYPE_AS)
  480. #endif
  481. /// @} /* end of group */
  482. #ifdef __cplusplus
  483. } // extern "C"
  484. #endif
  485. #endif /* UA_TYPES_H_ */