ua_types.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. #ifdef DEBUG
  23. #include <stdio.h>
  24. #endif
  25. /* For internal use */
  26. #define UA_NULL ((void *)0)
  27. #define UA_TRUE (42 == 42)
  28. #define UA_FALSE (!UA_TRUE)
  29. typedef enum UA_EQUALITY {
  30. UA_EQUAL,
  31. UA_NOT_EQUAL
  32. } UA_EQUALITY;
  33. /**
  34. * @defgroup types Datatypes
  35. *
  36. * @brief This module describes the datatypes used in OPC UA. There is a
  37. * division into built-in datatypes (integers, strings, etc.) and more complex
  38. * datatypes that are comprise of built-in datatypes (structs defined in the OPC
  39. * UA standard).
  40. *
  41. * All datatypes follow the same schema in the naming of relevant functions.
  42. *
  43. * DO NOT CALL THESE FUNCTIONS WITH NULL-POINTERS IF IT IS NOT EXPLICITLY
  44. * PERMITTED.
  45. *
  46. * - <type>_new: Allocates the memory for the type and runs <type>_init on the
  47. * returned variable.
  48. *
  49. * - <type>_init: Sets all members to a "safe" standard, usually zero. Arrays
  50. * (e.g. for strings) are set to a length of -1.
  51. *
  52. * - <type>_copy: Copies a datatype. This performs a deep copy that iterates
  53. * over the members. Copying into variants with an external data source is
  54. * not permitted. If copying fails, a deleteMembers is performed and an error
  55. * code returned.
  56. *
  57. * - <type>_delete: Frees the memory where the datatype was stored. This
  58. * performs an _deleteMembers internally if required.
  59. *
  60. * - <type>_deleteMembers: Frees the memory of dynamically sized members (e.g. a
  61. * string) of a datatype. This is useful when the datatype was allocated on
  62. * the stack, whereas the dynamically sized members is heap-allocated. To
  63. * reuse the variable, the remaining members (not dynamically allocated) need
  64. * to be cleaned up with an _init.
  65. *
  66. * @{
  67. */
  68. /** @brief A two-state logical value (true or false). */
  69. typedef _Bool UA_Boolean;
  70. /** @brief An integer value between -129 and 127. */
  71. typedef int8_t UA_SByte;
  72. #define UA_SBYTE_MAX -128
  73. #define UA_SBYTE_MIN 127
  74. /** @brief An integer value between 0 and 256. */
  75. typedef uint8_t UA_Byte;
  76. #define UA_BYTE_MAX 256
  77. #define UA_BYTE_MIN 0
  78. /** @brief An integer value between -32 768 and 32 767. */
  79. typedef int16_t UA_Int16;
  80. #define UA_INT16_MAX 32767
  81. #define UA_INT16_MIN -32768
  82. /** @brief An integer value between 0 and 65 535. */
  83. typedef uint16_t UA_UInt16;
  84. #define UA_UINT16_MAX 65535
  85. #define UA_UINT16_MIN 0
  86. /** @brief An integer value between -2 147 483 648 and 2 147 483 647. */
  87. typedef int32_t UA_Int32;
  88. #define UA_INT32_MAX 2147483647
  89. #define UA_INT32_MIN −2147483648
  90. /** @brief An integer value between 0 and 429 4967 295. */
  91. typedef uint32_t UA_UInt32;
  92. #define UA_UINT32_MAX 4294967295
  93. #define UA_UINT32_MIN 0
  94. /** @brief An integer value between -10 223 372 036 854 775 808 and 9 223 372 036 854 775 807 */
  95. typedef int64_t UA_Int64;
  96. #define UA_INT64_MAX 9223372036854775807
  97. #define UA_INT64_MIN −9223372036854775808
  98. /** @brief An integer value between 0 and 18 446 744 073 709 551 615. */
  99. typedef uint64_t UA_UInt64;
  100. #define UA_UINT64_MAX = 18446744073709551615
  101. #define UA_UINT64_MIN = 0
  102. /** @brief An IEEE single precision (32 bit) floating point value. */
  103. typedef float UA_Float;
  104. /** @brief An IEEE double precision (64 bit) floating point value. */
  105. typedef double UA_Double;
  106. /** @brief A sequence of Unicode characters. */
  107. typedef struct UA_String {
  108. UA_Int32 length;
  109. UA_Byte *data;
  110. } UA_String;
  111. /** @brief An instance in time. */
  112. typedef UA_Int64 UA_DateTime; //100 nanosecond resolution
  113. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  114. typedef struct UA_Guid {
  115. UA_UInt32 data1;
  116. UA_UInt16 data2;
  117. UA_UInt16 data3;
  118. UA_Byte data4[8];
  119. } UA_Guid;
  120. /** @brief A sequence of octets. */
  121. typedef struct UA_String UA_ByteString;
  122. /** @brief An XML element. */
  123. typedef struct UA_String UA_XmlElement;
  124. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  125. /* The shortened numeric types are introduced during encoding. */
  126. typedef struct UA_NodeId {
  127. UA_UInt16 namespaceIndex;
  128. enum {
  129. UA_NODEIDTYPE_NUMERIC = 2,
  130. UA_NODEIDTYPE_STRING = 3,
  131. UA_NODEIDTYPE_GUID = 4,
  132. UA_NODEIDTYPE_BYTESTRING = 5
  133. } identifierType;
  134. union {
  135. UA_UInt32 numeric;
  136. UA_String string;
  137. UA_Guid guid;
  138. UA_ByteString byteString;
  139. } identifier;
  140. } UA_NodeId;
  141. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  142. typedef struct UA_ExpandedNodeId {
  143. UA_NodeId nodeId;
  144. UA_String namespaceUri; // not encoded if length=-1
  145. UA_UInt32 serverIndex; // not encoded if 0
  146. } UA_ExpandedNodeId;
  147. #include "ua_statuscodes.h"
  148. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  149. typedef enum UA_StatusCode 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_StatusCode UA_EXPORT TYPE##_new(TYPE **p); \
  266. void UA_EXPORT TYPE##_init(TYPE * p); \
  267. void UA_EXPORT TYPE##_delete(TYPE * p); \
  268. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  269. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst); \
  270. void UA_EXPORT TYPE##_print(const TYPE *p, FILE *stream);
  271. #else
  272. #define UA_TYPE_PROTOTYPES(TYPE) \
  273. UA_StatusCode UA_EXPORT TYPE##_new(TYPE **p); \
  274. void UA_EXPORT TYPE##_init(TYPE * p); \
  275. void UA_EXPORT TYPE##_delete(TYPE * p); \
  276. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  277. UA_StatusCode UA_EXPORT 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_StatusCode UA_EXPORT UA_String_copycstring(char const *src, UA_String *dst);
  312. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  313. UA_EQUALITY UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  314. #ifdef DEBUG
  315. void UA_EXPORT UA_String_printf(char const *label, const UA_String *string);
  316. void UA_EXPORT UA_String_printx(char const *label, const UA_String *string);
  317. void UA_EXPORT UA_String_printx_hex(char const *label, const UA_String *string);
  318. #endif
  319. /* DateTime */
  320. UA_DateTime UA_EXPORT 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_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  333. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  334. /* Guid */
  335. UA_EQUALITY UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  336. /* ByteString */
  337. UA_EQUALITY UA_EXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  338. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  339. #ifdef DEBUG
  340. void UA_EXPORT UA_ByteString_printf(char *label, const UA_ByteString *string);
  341. void UA_EXPORT UA_ByteString_printx(char *label, const UA_ByteString *string);
  342. void UA_EXPORT UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  343. #endif
  344. /* NodeId */
  345. UA_EQUALITY UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  346. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  347. UA_Boolean UA_EXPORT 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_EXPORT 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_StatusCode UA_EXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  362. #ifdef DEBUG
  363. void UA_EXPORT 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_StatusCode UA_EXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  370. /* Variant */
  371. UA_StatusCode UA_EXPORT UA_Variant_copySetValue(UA_Variant *v, const UA_VTable_Entry *vt, const void *value);
  372. UA_StatusCode UA_EXPORT UA_Variant_copySetArray(UA_Variant *v, const UA_VTable_Entry *vt, UA_Int32 arrayLength, const void *array);
  373. /* Array operations */
  374. UA_StatusCode UA_EXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  375. void UA_EXPORT UA_Array_init(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  376. void UA_EXPORT 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_StatusCode UA_EXPORT UA_Array_copy(const void *src, UA_Int32 noElements, const UA_VTable_Entry *vt, void **dst);
  379. #ifdef DEBUG
  380. void UA_EXPORT 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_StatusCode (*encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  390. /** Decodes a ByteString into an UA datatype. */
  391. UA_StatusCode (*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_StatusCode (*new)(void **p);
  398. void (*init)(void *p);
  399. UA_StatusCode (*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_StatusCode TYPE##_new(TYPE **p) { \
  422. if(!(*p = UA_alloc(sizeof(TYPE)))) \
  423. return UA_STATUSCODE_BADOUTOFMEMORY; \
  424. TYPE##_init(*p); \
  425. return UA_STATUSCODE_GOOD; \
  426. }
  427. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  428. void TYPE##_init(TYPE * p) { \
  429. *p = (TYPE)0; \
  430. }
  431. #define UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  432. void TYPE##_init(TYPE * p) { \
  433. TYPE_AS##_init((TYPE_AS *)p); \
  434. }
  435. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  436. void TYPE##_delete(TYPE *p) { \
  437. TYPE##_deleteMembers(p); \
  438. UA_free(p); \
  439. }
  440. #define UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  441. void TYPE##_delete(TYPE * p) { \
  442. TYPE_AS##_delete((TYPE_AS *)p); \
  443. }
  444. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  445. void TYPE##_deleteMembers(TYPE * p) { return; }
  446. #define UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  447. void TYPE##_deleteMembers(TYPE * p) { TYPE_AS##_deleteMembers((TYPE_AS *)p); }
  448. /* Use only when the type has no arrays. Otherwise, implement deep copy */
  449. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  450. UA_StatusCode TYPE##_copy(TYPE const *src, TYPE *dst) { \
  451. *dst = *src; \
  452. return UA_STATUSCODE_GOOD; \
  453. }
  454. #define UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  455. UA_StatusCode TYPE##_copy(TYPE const *src, TYPE *dst) { \
  456. return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
  457. }
  458. #define UA_TYPE_PRINT_AS(TYPE, TYPE_AS) \
  459. void TYPE##_print(TYPE const *p, FILE *stream) { \
  460. TYPE_AS##_print((TYPE_AS *)p, stream); \
  461. } \
  462. #ifdef DEBUG //print functions only in debug mode
  463. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  464. UA_TYPE_NEW_DEFAULT(TYPE) \
  465. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  466. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  467. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  468. UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  469. UA_TYPE_PRINT_AS(TYPE, TYPE_AS)
  470. #else
  471. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  472. UA_TYPE_NEW_DEFAULT(TYPE) \
  473. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  474. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  475. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  476. UA_TYPE_COPY_AS(TYPE, TYPE_AS)
  477. #endif
  478. /// @} /* end of group */
  479. #ifdef __cplusplus
  480. } // extern "C"
  481. #endif
  482. #endif /* UA_TYPES_H_ */