ua_types.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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.
  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_VTable_Entry; // forwards declaration
  164. typedef struct UA_VTable_Entry UA_VTable_Entry;
  165. /* Variant */
  166. /** @defgroup variant Variant
  167. *
  168. * @brief Variants may contain (arrays of) any other datatype.
  169. *
  170. * For that, we provide a reference to the utility functions of the type as well
  171. * as possible encoding by pointing an entry in the vtable of types. References
  172. * may either contain a structure with the variants data, or a datasource where
  173. * this structure may be obtained (using reference counting for async access).
  174. *
  175. * @{
  176. */
  177. typedef struct UA_VariantData {
  178. UA_Int32 arrayLength; // total number of elements in the data-pointer
  179. void *dataPtr;
  180. UA_Int32 arrayDimensionsLength;
  181. UA_Int32 *arrayDimensions;
  182. } UA_VariantData;
  183. /** @brief A datasource is the interface to interact with a local data provider.
  184. *
  185. * Implementors of datasources need to provide functions for the callbacks in
  186. * this structure. As a rule, datasources are never copied, but only their
  187. * content. The only way to write into a datasource is via the write-service. */
  188. typedef struct UA_VariantDataSource {
  189. const void *identifier; /**< whatever id the datasource uses internally. Can be ignored if the datasource functions do not use it. */
  190. 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. */
  191. 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. */
  192. 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! */
  193. void (*delete)(const void *identifier); /**< Indicates that the node with the datasource was removed from the namespace. */
  194. } UA_VariantDataSource;
  195. /** @brief A union of all of the types specified above.
  196. *
  197. * Variants store (arrays of) built-in types. If you want to store a more
  198. * complex (or self-defined) type, you have to use an UA_ExtensionObject.*/
  199. typedef struct UA_Variant {
  200. const UA_VTable_Entry *vt; // internal entry into vTable
  201. enum {
  202. UA_VARIANT_DATA,
  203. UA_VARIANT_DATA_NODELETE, // do not free the data (e.g. because it is "borrowed" and points into a larger structure)
  204. UA_VARIANT_DATASOURCE
  205. } storageType;
  206. union {
  207. UA_VariantData data;
  208. UA_VariantDataSource datasource;
  209. } storage;
  210. } UA_Variant;
  211. /** @} */
  212. /** @brief A data value with an associated status code and timestamps. */
  213. typedef struct UA_DataValue {
  214. UA_Byte encodingMask;
  215. UA_Variant value;
  216. UA_StatusCode status;
  217. UA_DateTime sourceTimestamp;
  218. UA_Int16 sourcePicoseconds;
  219. UA_DateTime serverTimestamp;
  220. UA_Int16 serverPicoseconds;
  221. } UA_DataValue;
  222. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  223. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  224. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  225. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  226. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  227. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  228. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  229. };
  230. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  231. typedef struct UA_DiagnosticInfo {
  232. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  233. UA_Int32 symbolicId;
  234. UA_Int32 namespaceUri;
  235. UA_Int32 localizedText;
  236. UA_Int32 locale;
  237. UA_String additionalInfo;
  238. UA_StatusCode innerStatusCode;
  239. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  240. } UA_DiagnosticInfo;
  241. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  242. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  243. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  244. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  245. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  246. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  247. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  248. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  249. };
  250. /** @brief Type use internally to denote an invalid datatype. */
  251. typedef void UA_InvalidType;
  252. /*************/
  253. /* Functions */
  254. /*************/
  255. #ifdef DEBUG
  256. #define PRINTTYPE(TYPE) void UA_EXPORT TYPE##_print(const TYPE *p, FILE *stream);
  257. #define PRINTTYPE_NOEXPORT(TYPE) void TYPE##_print(const TYPE *p, FILE *stream);
  258. #else
  259. #define PRINTTYPE(TYPE)
  260. #define PRINTTYPE_NOEXPORT(TYPE)
  261. #endif
  262. #define UA_TYPE_PROTOTYPES(TYPE) \
  263. UA_StatusCode UA_EXPORT TYPE##_new(TYPE **p); \
  264. void UA_EXPORT TYPE##_init(TYPE * p); \
  265. void UA_EXPORT TYPE##_delete(TYPE * p); \
  266. void UA_EXPORT TYPE##_deleteMembers(TYPE * p); \
  267. UA_StatusCode UA_EXPORT TYPE##_copy(const TYPE *src, TYPE *dst); \
  268. PRINTTYPE(TYPE)
  269. #define UA_TYPE_PROTOTYPES_NOEXPORT(TYPE) \
  270. UA_StatusCode TYPE##_new(TYPE **p); \
  271. void TYPE##_init(TYPE * p); \
  272. void TYPE##_delete(TYPE * p); \
  273. void TYPE##_deleteMembers(TYPE * p); \
  274. UA_StatusCode TYPE##_copy(const TYPE *src, TYPE *dst); \
  275. PRINTTYPE_NOEXPORT(TYPE)
  276. /* Functions for all types */
  277. UA_TYPE_PROTOTYPES(UA_Boolean)
  278. UA_TYPE_PROTOTYPES(UA_SByte)
  279. UA_TYPE_PROTOTYPES(UA_Byte)
  280. UA_TYPE_PROTOTYPES(UA_Int16)
  281. UA_TYPE_PROTOTYPES(UA_UInt16)
  282. UA_TYPE_PROTOTYPES(UA_Int32)
  283. UA_TYPE_PROTOTYPES(UA_UInt32)
  284. UA_TYPE_PROTOTYPES(UA_Int64)
  285. UA_TYPE_PROTOTYPES(UA_UInt64)
  286. UA_TYPE_PROTOTYPES(UA_Float)
  287. UA_TYPE_PROTOTYPES(UA_Double)
  288. UA_TYPE_PROTOTYPES(UA_String)
  289. UA_TYPE_PROTOTYPES(UA_DateTime)
  290. UA_TYPE_PROTOTYPES(UA_Guid)
  291. UA_TYPE_PROTOTYPES(UA_ByteString)
  292. UA_TYPE_PROTOTYPES(UA_XmlElement)
  293. UA_TYPE_PROTOTYPES(UA_NodeId)
  294. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  295. UA_TYPE_PROTOTYPES(UA_StatusCode)
  296. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  297. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  298. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  299. UA_TYPE_PROTOTYPES(UA_DataValue)
  300. UA_TYPE_PROTOTYPES(UA_Variant)
  301. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  302. UA_TYPE_PROTOTYPES(UA_InvalidType)
  303. /* String */
  304. #define UA_STRING_NULL (UA_String) {-1, (UA_Byte*)0 }
  305. #define UA_STRING_STATIC(VARIABLE, STRING) do { \
  306. VARIABLE.length = sizeof(STRING)-1; \
  307. VARIABLE.data = (UA_Byte *)STRING; } while(0)
  308. UA_StatusCode UA_EXPORT UA_String_copycstring(char const *src, UA_String *dst);
  309. UA_StatusCode UA_EXPORT UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  310. UA_Boolean UA_EXPORT UA_String_equal(const UA_String *string1, const UA_String *string2);
  311. #ifdef DEBUG
  312. void UA_EXPORT UA_String_printf(char const *label, const UA_String *string);
  313. void UA_EXPORT UA_String_printx(char const *label, const UA_String *string);
  314. void UA_EXPORT UA_String_printx_hex(char const *label, const UA_String *string);
  315. #endif
  316. /* DateTime */
  317. UA_DateTime UA_EXPORT UA_DateTime_now();
  318. typedef struct UA_DateTimeStruct {
  319. UA_Int16 nanoSec;
  320. UA_Int16 microSec;
  321. UA_Int16 milliSec;
  322. UA_Int16 sec;
  323. UA_Int16 min;
  324. UA_Int16 hour;
  325. UA_Int16 day;
  326. UA_Int16 mounth;
  327. UA_Int16 year;
  328. } UA_DateTimeStruct;
  329. UA_DateTimeStruct UA_EXPORT UA_DateTime_toStruct(UA_DateTime time);
  330. UA_StatusCode UA_EXPORT UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  331. /* Guid */
  332. UA_Boolean UA_EXPORT UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  333. /* ByteString */
  334. UA_Boolean UA_EXPORT UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  335. UA_StatusCode UA_EXPORT UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  336. #ifdef DEBUG
  337. void UA_EXPORT UA_ByteString_printf(char *label, const UA_ByteString *string);
  338. void UA_EXPORT UA_ByteString_printx(char *label, const UA_ByteString *string);
  339. void UA_EXPORT UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  340. #endif
  341. /* NodeId */
  342. UA_Boolean UA_EXPORT UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  343. UA_Boolean UA_EXPORT UA_NodeId_isNull(const UA_NodeId *p);
  344. UA_Boolean UA_EXPORT UA_NodeId_isBasicType(UA_NodeId const *id);
  345. #define NS0NODEID(NUMERIC_ID) \
  346. (UA_NodeId) {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = \
  347. NUMERIC_ID }
  348. /* ExpandedNodeId */
  349. UA_Boolean UA_EXPORT UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  350. #define NS0EXPANDEDNODEID(VARIABLE, NUMERIC_ID) do { \
  351. VARIABLE.nodeId = NS0NODEID(NUMERIC_ID); \
  352. VARIABLE.namespaceUri = UA_STRING_NULL; \
  353. VARIABLE.serverIndex = 0; } while(0)
  354. /* QualifiedName */
  355. #define UA_QUALIFIEDNAME_STATIC(VARIABLE, STRING) do { \
  356. VARIABLE.namespaceIndex = 0; \
  357. UA_STRING_STATIC(VARIABLE.name, STRING); } while(0)
  358. UA_StatusCode UA_EXPORT UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst);
  359. #ifdef DEBUG
  360. void UA_EXPORT UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  361. #endif
  362. /* LocalizedText */
  363. #define UA_LOCALIZEDTEXT_STATIC(VARIABLE, STRING) do { \
  364. UA_STRING_STATIC(VARIABLE.locale, "en"); \
  365. UA_STRING_STATIC(VARIABLE.text, STRING); } while(0)
  366. UA_StatusCode UA_EXPORT UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  367. /* Variant */
  368. UA_StatusCode UA_EXPORT UA_Variant_copySetValue(UA_Variant *v, const UA_VTable_Entry *vt, const void *value);
  369. UA_StatusCode UA_EXPORT UA_Variant_copySetArray(UA_Variant *v, const UA_VTable_Entry *vt, UA_Int32 arrayLength, const void *array);
  370. /* Array operations */
  371. UA_StatusCode UA_EXPORT UA_Array_new(void **p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  372. void UA_EXPORT UA_Array_init(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  373. void UA_EXPORT UA_Array_delete(void *p, UA_Int32 noElements, const UA_VTable_Entry *vt);
  374. /* @brief The destination array is allocated according to noElements. */
  375. UA_StatusCode UA_EXPORT UA_Array_copy(const void *src, UA_Int32 noElements, const UA_VTable_Entry *vt, void **dst);
  376. #ifdef DEBUG
  377. void UA_EXPORT UA_Array_print(const void *p, UA_Int32 noElements, const UA_VTable_Entry *vt, FILE *stream);
  378. #endif
  379. /**********/
  380. /* VTable */
  381. /**********/
  382. typedef struct UA_Encoding {
  383. /** Returns the size of the encoded element.*/
  384. UA_Int32 (*calcSize)(const void *p);
  385. /** Encodes the type into the destination bytestring. */
  386. UA_StatusCode (*encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  387. /** Decodes a ByteString into an UA datatype. */
  388. UA_StatusCode (*decode)(const UA_ByteString *src, UA_UInt32 *offset, void *dst);
  389. } UA_Encoding;
  390. #define UA_ENCODING_BINARY 0 // Binary encoding is always available
  391. struct UA_VTable_Entry {
  392. UA_NodeId typeId;
  393. UA_Byte *name;
  394. UA_StatusCode (*new)(void **p);
  395. void (*init)(void *p);
  396. UA_StatusCode (*copy)(void const *src, void *dst);
  397. void (*delete)(void *p);
  398. void (*deleteMembers)(void *p);
  399. #ifdef DEBUG
  400. void (*print)(const void *p, FILE *stream);
  401. #endif
  402. UA_UInt32 memSize; // size of the struct only in memory (no dynamic components)
  403. UA_Boolean dynMembers; // does the type contain members that are dynamically
  404. // allocated (strings, ..)? Otherwise, the size on
  405. // the wire == size in memory.
  406. UA_Encoding encodings[UA_ENCODING_AMOUNT]; // binary, xml, ... UA_ENCODING_AMOUNT is set by the build script
  407. };
  408. /***********************************/
  409. /* Macros for type implementations */
  410. /***********************************/
  411. #define UA_TYPE_DEFAULT(TYPE) \
  412. UA_TYPE_DELETE_DEFAULT(TYPE) \
  413. UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  414. UA_TYPE_INIT_DEFAULT(TYPE) \
  415. UA_TYPE_NEW_DEFAULT(TYPE) \
  416. UA_TYPE_COPY_DEFAULT(TYPE) \
  417. #define UA_TYPE_NEW_DEFAULT(TYPE) \
  418. UA_StatusCode TYPE##_new(TYPE **p) { \
  419. if(!(*p = UA_alloc(sizeof(TYPE)))) \
  420. return UA_STATUSCODE_BADOUTOFMEMORY; \
  421. TYPE##_init(*p); \
  422. return UA_STATUSCODE_GOOD; \
  423. }
  424. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  425. void TYPE##_init(TYPE * p) { \
  426. *p = (TYPE)0; \
  427. }
  428. #define UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  429. void TYPE##_init(TYPE * p) { \
  430. TYPE_AS##_init((TYPE_AS *)p); \
  431. }
  432. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  433. void TYPE##_delete(TYPE *p) { \
  434. TYPE##_deleteMembers(p); \
  435. UA_free(p); \
  436. }
  437. #define UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  438. void TYPE##_delete(TYPE * p) { \
  439. TYPE_AS##_delete((TYPE_AS *)p); \
  440. }
  441. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  442. void TYPE##_deleteMembers(TYPE * p) { return; }
  443. #define UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  444. void TYPE##_deleteMembers(TYPE * p) { TYPE_AS##_deleteMembers((TYPE_AS *)p); }
  445. /* Use only when the type has no arrays. Otherwise, implement deep copy */
  446. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  447. UA_StatusCode TYPE##_copy(TYPE const *src, TYPE *dst) { \
  448. *dst = *src; \
  449. return UA_STATUSCODE_GOOD; \
  450. }
  451. #define UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  452. UA_StatusCode TYPE##_copy(TYPE const *src, TYPE *dst) { \
  453. return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
  454. }
  455. #define UA_TYPE_PRINT_AS(TYPE, TYPE_AS) \
  456. void TYPE##_print(TYPE const *p, FILE *stream) { \
  457. TYPE_AS##_print((TYPE_AS *)p, stream); \
  458. } \
  459. #ifdef DEBUG //print functions only in debug mode
  460. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  461. UA_TYPE_NEW_DEFAULT(TYPE) \
  462. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  463. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  464. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  465. UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  466. UA_TYPE_PRINT_AS(TYPE, TYPE_AS)
  467. #else
  468. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  469. UA_TYPE_NEW_DEFAULT(TYPE) \
  470. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  471. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  472. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  473. UA_TYPE_COPY_AS(TYPE, TYPE_AS)
  474. #endif
  475. /// @} /* end of group */
  476. #ifdef __cplusplus
  477. } // extern "C"
  478. #endif
  479. #endif /* UA_TYPES_H_ */