ua_types.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #ifndef UA_TYPES_H_
  2. #define UA_TYPES_H_
  3. #include <stdint.h>
  4. /**
  5. * @defgroup types Datatypes
  6. *
  7. * @brief This module describes the datatypes used in OPC UA. There is a
  8. * division into built-in datatypes (integers, strings, etc.) and more complex
  9. * datatypes that are comprise of built-in datatypes (structs defined in the OPC
  10. * UA standard).
  11. *
  12. * All datatypes follow the same schema in the naming of relevant functions.
  13. *
  14. * - <type>_init: Sets all values to a "safe" standard. For example, if the
  15. * datatype contains a string-element, its size will be set to zero.
  16. *
  17. * - <type>_new: Allocates the memory for the type and runs <type>_init on the
  18. * returned pointer.
  19. *
  20. * - <type>_copy: Copies a datatype. This performs a deep copy that iterates
  21. * over the members.
  22. *
  23. * - <type>_delete: Frees the memory where the datatype was stored.
  24. *
  25. * - <type>_deleteMembers: Frees the memory of dynamically sized members (e.g. a
  26. string) of a datatype. This is useful when the datatype was allocated on
  27. the stack, whereas the dynamically sized members is heap-allocated.
  28. *
  29. * @{
  30. */
  31. /* Function return values */
  32. #define UA_SUCCESS 0
  33. #define UA_NO_ERROR UA_SUCCESS
  34. #define UA_ERROR (0x01 << 31)
  35. #define UA_ERR_INCONSISTENT (UA_ERROR | (0x01 << 1))
  36. #define UA_ERR_INVALID_VALUE (UA_ERROR | (0x01 << 2))
  37. #define UA_ERR_NO_MEMORY (UA_ERROR | (0x01 << 3))
  38. #define UA_ERR_NOT_IMPLEMENTED (UA_ERROR | (0x01 << 4))
  39. /* Boolean values and null */
  40. #define UA_TRUE (42 == 42)
  41. #define TRUE UA_TRUE
  42. #define UA_FALSE (!UA_TRUE)
  43. #define FALSE UA_FALSE
  44. /* Compare values */
  45. #define UA_EQUAL 0
  46. #define UA_NOT_EQUAL (!UA_EQUAL)
  47. /** @brief A two-state logical value (true or false). */
  48. typedef _Bool UA_Boolean;
  49. /** @brief An integer value between −128 and 127. */
  50. typedef int8_t UA_SByte;
  51. /** @brief An integer value between 0 and 256. */
  52. typedef uint8_t UA_Byte;
  53. /** @brief An integer value between −32 768 and 32 767. */
  54. typedef int16_t UA_Int16;
  55. /** @brief An integer value between 0 and 65 535. */
  56. typedef uint16_t UA_UInt16;
  57. /** @brief An integer value between −2 147 483 648 and 2 147 483 647. */
  58. typedef int32_t UA_Int32;
  59. /** @brief An integer value between 0 and 429 4967 295. */
  60. typedef uint32_t UA_UInt32;
  61. /** @brief An integer value between −9 223 372 036 854 775 808 and 9 223 372 036 854 775 807 */
  62. typedef int64_t UA_Int64;
  63. /** @brief An integer value between 0 and 18 446 744 073 709 551 615. */
  64. typedef uint64_t UA_UInt64;
  65. /** @brief An IEEE single precision (32 bit) floating point value. */
  66. typedef float UA_Float;
  67. /** @brief An IEEE double precision (64 bit) floating point value. */
  68. typedef double UA_Double;
  69. /** @brief A sequence of Unicode characters. */
  70. typedef struct UA_String {
  71. UA_Int32 length;
  72. UA_Byte *data;
  73. } UA_String;
  74. /** @brief An instance in time. */
  75. typedef UA_Int64 UA_DateTime; //100 nanosecond resolution
  76. /** @brief A 16 byte value that can be used as a globally unique identifier. */
  77. typedef struct UA_Guid {
  78. UA_UInt32 data1;
  79. UA_UInt16 data2;
  80. UA_UInt16 data3;
  81. UA_Byte data4[8];
  82. } UA_Guid;
  83. /** @brief A sequence of octets. */
  84. typedef struct UA_ByteString {
  85. UA_Int32 length;
  86. UA_Byte *data;
  87. } UA_ByteString;
  88. /** @brief An XML element. */
  89. typedef struct UA_ByteString UA_XmlElement;
  90. /** @brief An identifier for a node in the address space of an OPC UA Server. */
  91. typedef struct UA_NodeId {
  92. UA_Byte encodingByte; //enum BID_NodeIdEncodingValuesType
  93. UA_UInt16 namespace;
  94. union {
  95. UA_UInt32 numeric;
  96. UA_String string;
  97. UA_Guid guid;
  98. UA_ByteString byteString;
  99. } identifier;
  100. } UA_NodeId;
  101. #define UA_NODEIDTYPE_NAMESPACE_URI_FLAG 0x80
  102. #define UA_NODEIDTYPE_SERVERINDEX_FLAG 0x40
  103. #define UA_NODEIDTYPE_MASK (~(UA_NODEIDTYPE_NAMESPACE_URI_FLAG | UA_NODEIDTYPE_SERVERINDEX_FLAG))
  104. /** @brief A NodeId that allows the namespace URI to be specified instead of an index. */
  105. typedef struct UA_ExpandedNodeId {
  106. UA_NodeId nodeId;
  107. UA_String namespaceUri;
  108. UA_UInt32 serverIndex;
  109. } UA_ExpandedNodeId;
  110. /** @brief A numeric identifier for a error or condition that is associated with a value or an operation. */
  111. typedef UA_UInt32 UA_StatusCode; // StatusCodes aren't an enum(=int) since 32 unsigned bits are needed. See also ua_statuscodes.h */
  112. /** @brief A name qualified by a namespace. */
  113. typedef struct UA_QualifiedName {
  114. UA_UInt16 namespaceIndex;
  115. UA_String name;
  116. } UA_QualifiedName;
  117. /** @brief Human readable text with an optional locale identifier. */
  118. typedef struct UA_LocalizedText {
  119. UA_Byte encodingMask;
  120. UA_String locale;
  121. UA_String text;
  122. } UA_LocalizedText;
  123. enum UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_enum {
  124. UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE = 0x01,
  125. UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT = 0x02
  126. };
  127. /** @brief A structure that contains an application specific data type that may not be recognized by the receiver. */
  128. typedef struct UA_ExtensionObject {
  129. UA_NodeId typeId;
  130. UA_Byte encoding; // Type of the enum UA_ExtensionObjectEncodingMaskType
  131. UA_ByteString body; // contains either the bytestring or a pointer to the memory-object
  132. } UA_ExtensionObject;
  133. enum UA_ExtensionObject_EncodingMaskType_enum {
  134. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0x00,
  135. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 0x01,
  136. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 0x02
  137. };
  138. struct UA_VTable_Entry; // forwards declaration
  139. typedef struct UA_VTable_Entry UA_VTable_Entry;
  140. /** @brief A union of all of the types specified above. */
  141. typedef struct UA_Variant {
  142. UA_VTable_Entry *vt; // internal entry into vTable
  143. UA_Byte encodingMask; // Type of UA_Variant_EncodingMaskType_enum
  144. UA_Int32 arrayLength; // total number of elements
  145. UA_Int32 arrayDimensionsLength;
  146. UA_Int32 *arrayDimensions;
  147. void *data;
  148. } UA_Variant;
  149. /* VariantBinaryEncoding - Part: 6, Chapter: 5.2.2.16, Page: 22 */
  150. enum UA_VARIANT_ENCODINGMASKTYPE_enum {
  151. UA_VARIANT_ENCODINGMASKTYPE_TYPEID_MASK = 0x3F, // bits 0:5
  152. UA_VARIANT_ENCODINGMASKTYPE_DIMENSIONS = (0x01 << 6), // bit 6
  153. UA_VARIANT_ENCODINGMASKTYPE_ARRAY = (0x01 << 7) // bit 7
  154. };
  155. /** @brief A data value with an associated status code and timestamps. */
  156. typedef struct UA_DataValue {
  157. UA_Byte encodingMask;
  158. UA_Variant value;
  159. UA_StatusCode status;
  160. UA_DateTime sourceTimestamp;
  161. UA_Int16 sourcePicoseconds;
  162. UA_DateTime serverTimestamp;
  163. UA_Int16 serverPicoseconds;
  164. } UA_DataValue;
  165. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum {
  166. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  167. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  168. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  169. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  170. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  171. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  172. };
  173. /** @brief A structure that contains detailed error and diagnostic information associated with a StatusCode. */
  174. typedef struct UA_DiagnosticInfo {
  175. UA_Byte encodingMask; // Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  176. UA_Int32 symbolicId;
  177. UA_Int32 namespaceUri;
  178. UA_Int32 localizedText;
  179. UA_Int32 locale;
  180. UA_String additionalInfo;
  181. UA_StatusCode innerStatusCode;
  182. struct UA_DiagnosticInfo *innerDiagnosticInfo;
  183. } UA_DiagnosticInfo;
  184. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum {
  185. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  186. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  187. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  188. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  189. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  190. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  191. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  192. };
  193. /** @brief Type use internally to denote an invalid datatype. */
  194. typedef void UA_InvalidType;
  195. /*************/
  196. /* Functions */
  197. /*************/
  198. #define UA_TYPE_PROTOTYPES(TYPE) \
  199. UA_Int32 TYPE##_new(TYPE **p); \
  200. UA_Int32 TYPE##_init(TYPE * p); \
  201. UA_Int32 TYPE##_delete(TYPE * p); \
  202. UA_Int32 TYPE##_deleteMembers(TYPE * p); \
  203. UA_Int32 TYPE##_copy(const TYPE *src, TYPE *dst);
  204. /* Functions for all types */
  205. UA_TYPE_PROTOTYPES(UA_Boolean)
  206. UA_TYPE_PROTOTYPES(UA_SByte)
  207. UA_TYPE_PROTOTYPES(UA_Byte)
  208. UA_TYPE_PROTOTYPES(UA_Int16)
  209. UA_TYPE_PROTOTYPES(UA_UInt16)
  210. UA_TYPE_PROTOTYPES(UA_Int32)
  211. UA_TYPE_PROTOTYPES(UA_UInt32)
  212. UA_TYPE_PROTOTYPES(UA_Int64)
  213. UA_TYPE_PROTOTYPES(UA_UInt64)
  214. UA_TYPE_PROTOTYPES(UA_Float)
  215. UA_TYPE_PROTOTYPES(UA_Double)
  216. UA_TYPE_PROTOTYPES(UA_String)
  217. UA_TYPE_PROTOTYPES(UA_DateTime)
  218. UA_TYPE_PROTOTYPES(UA_Guid)
  219. UA_TYPE_PROTOTYPES(UA_ByteString)
  220. UA_TYPE_PROTOTYPES(UA_XmlElement)
  221. UA_TYPE_PROTOTYPES(UA_NodeId)
  222. UA_TYPE_PROTOTYPES(UA_ExpandedNodeId)
  223. UA_TYPE_PROTOTYPES(UA_StatusCode)
  224. UA_TYPE_PROTOTYPES(UA_QualifiedName)
  225. UA_TYPE_PROTOTYPES(UA_LocalizedText)
  226. UA_TYPE_PROTOTYPES(UA_ExtensionObject)
  227. UA_TYPE_PROTOTYPES(UA_DataValue)
  228. UA_TYPE_PROTOTYPES(UA_Variant)
  229. UA_TYPE_PROTOTYPES(UA_DiagnosticInfo)
  230. UA_TYPE_PROTOTYPES(UA_InvalidType)
  231. /* String */
  232. UA_Int32 UA_String_copycstring(char const *src, UA_String *dst);
  233. UA_Int32 UA_String_copyprintf(char const *fmt, UA_String *dst, ...);
  234. UA_Int32 UA_String_equal(const UA_String *string1, const UA_String *string2);
  235. void UA_String_printf(char const *label, const UA_String *string);
  236. void UA_String_printx(char const *label, const UA_String *string);
  237. void UA_String_printx_hex(char const *label, const UA_String *string);
  238. /* DateTime */
  239. UA_DateTime UA_DateTime_now();
  240. typedef struct UA_DateTimeStruct {
  241. UA_Int16 nanoSec;
  242. UA_Int16 microSec;
  243. UA_Int16 milliSec;
  244. UA_Int16 sec;
  245. UA_Int16 min;
  246. UA_Int16 hour;
  247. UA_Int16 day;
  248. UA_Int16 mounth;
  249. UA_Int16 year;
  250. } UA_DateTimeStruct;
  251. UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime time);
  252. UA_Int32 UA_DateTime_toString(UA_DateTime time, UA_String *timeString);
  253. /* Guid */
  254. UA_Int32 UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2);
  255. /* ByteString */
  256. UA_Int32 UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2);
  257. UA_Int32 UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length);
  258. void UA_ByteString_printf(char *label, const UA_ByteString *string);
  259. void UA_ByteString_printx(char *label, const UA_ByteString *string);
  260. void UA_ByteString_printx_hex(char *label, const UA_ByteString *string);
  261. /* NodeId */
  262. UA_Int32 UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2);
  263. void UA_NodeId_printf(char *label, const UA_NodeId *node);
  264. UA_Boolean UA_NodeId_isNull(const UA_NodeId *p);
  265. UA_Int16 UA_NodeId_getNamespace(UA_NodeId const *id);
  266. UA_Int16 UA_NodeId_getIdentifier(UA_NodeId const *id);
  267. UA_Boolean UA_NodeId_isBasicType(UA_NodeId const *id);
  268. /* ExpandedNodeId */
  269. UA_Boolean UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p);
  270. /* QualifiedName */
  271. void UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn);
  272. /* LocalizedText */
  273. UA_Int32 UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst);
  274. /* Variant */
  275. UA_Int32 UA_Variant_copySetValue(UA_Variant *v, UA_Int32 type, const void *data);
  276. UA_Int32 UA_Variant_copySetArray(UA_Variant *v, UA_Int32 type_id, UA_Int32 arrayLength, UA_UInt32 elementSize,
  277. const void *array);
  278. /** @brief Functions UA_Variant_borrowSetValue and ..Array allow to define
  279. variants whose payload will not be deleted. This is achieved by a second
  280. vtable. The functionality can be used e.g. when UA_VariableNodes point into a
  281. "father" structured object that is stored in an UA_VariableNode itself. */
  282. UA_Int32 UA_Variant_borrowSetValue(UA_Variant *v, UA_Int32 type, const void *data);
  283. UA_Int32 UA_Variant_borrowSetArray(UA_Variant *v, UA_Int32 type, UA_Int32 arrayLength, const void *data);
  284. /* Array operations */
  285. UA_Int32 UA_Array_new(void **p, UA_Int32 noElements, UA_Int32 type);
  286. UA_Int32 UA_Array_init(void *p, UA_Int32 noElements, UA_Int32 type);
  287. UA_Int32 UA_Array_delete(void *p, UA_Int32 noElements, UA_Int32 type);
  288. /* @brief The destination array is allocated according to noElements. */
  289. UA_Int32 UA_Array_copy(const void *src, UA_Int32 noElements, UA_Int32 type, void **dst);
  290. /**********/
  291. /* VTable */
  292. /**********/
  293. /* @brief Returns the size of the encoded element.*/
  294. typedef UA_Int32 (*UA_calcSize)(const void *p);
  295. /* @brief The encoding function allocates the target bytestring. */
  296. typedef UA_Int32 (*UA_encode)(const void *src, UA_ByteString *dst, UA_UInt32 *offset);
  297. /* @brief The decoding function decodes a ByteString into an UA datatype. */
  298. typedef UA_Int32 (*UA_decode)(UA_ByteString *src, UA_UInt32 *offset, void *dst);
  299. typedef struct UA_Encoding {
  300. UA_calcSize calcSize;
  301. UA_encode encode;
  302. UA_decode decode;
  303. } UA_Encoding;
  304. struct UA_VTable_Entry {
  305. UA_NodeId typeId;
  306. UA_Byte *name;
  307. UA_Int32 (*new)(void **p);
  308. UA_Int32 (*init)(void *p);
  309. UA_Int32 (*copy)(void const *src, void *dst);
  310. UA_Int32 (*delete)(void *p);
  311. UA_Int32 (*deleteMembers)(void *p);
  312. UA_UInt32 memSize; // size of the struct only in memory (no dynamic components)
  313. UA_Boolean dynMembers; // does the type contain members that are dynamically
  314. // allocated (strings, ..)? Otherwise, the size on
  315. // the wire == size in memory.
  316. UA_Encoding encodings[2]; // binary, xml, ...
  317. };
  318. typedef UA_Int32 (*UA_nodeIdToVTableIndex)(const UA_NodeId *id);
  319. typedef struct UA_VTable {
  320. UA_nodeIdToVTableIndex getTypeIndex;
  321. UA_VTable_Entry *types;
  322. } UA_VTable;
  323. /***********************************/
  324. /* Macros for type implementations */
  325. /***********************************/
  326. #define UA_TYPE_DEFAULT(TYPE) \
  327. UA_TYPE_DELETE_DEFAULT(TYPE) \
  328. UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  329. UA_TYPE_INIT_DEFAULT(TYPE) \
  330. UA_TYPE_NEW_DEFAULT(TYPE) \
  331. UA_TYPE_COPY_DEFAULT(TYPE) \
  332. #define UA_TYPE_NEW_DEFAULT(TYPE) \
  333. UA_Int32 TYPE##_new(TYPE **p) { \
  334. UA_Int32 retval = UA_SUCCESS; \
  335. retval |= UA_alloc((void **)p, sizeof(TYPE)); \
  336. retval |= TYPE##_init(*p); \
  337. return retval; \
  338. }
  339. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  340. UA_Int32 TYPE##_init(TYPE * p) { \
  341. if(p == UA_NULL) return UA_ERROR; \
  342. *p = (TYPE)0; \
  343. return UA_SUCCESS; \
  344. }
  345. #define UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  346. UA_Int32 TYPE##_init(TYPE * p) { \
  347. return TYPE_AS##_init((TYPE_AS *)p); \
  348. }
  349. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  350. UA_Int32 TYPE##_delete(TYPE *p) { \
  351. UA_Int32 retval = UA_SUCCESS; \
  352. retval |= TYPE##_deleteMembers(p); \
  353. retval |= UA_free(p); \
  354. return retval; \
  355. }
  356. #define UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  357. UA_Int32 TYPE##_delete(TYPE * p) { return TYPE_AS##_delete((TYPE_AS *)p); }
  358. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  359. UA_Int32 TYPE##_deleteMembers(TYPE * p) { return UA_SUCCESS; }
  360. #define UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  361. UA_Int32 TYPE##_deleteMembers(TYPE * p) { return TYPE_AS##_deleteMembers((TYPE_AS *)p); }
  362. /* Use only when the type has no arrays. Otherwise, implement deep copy */
  363. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  364. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  365. UA_Int32 retval = UA_SUCCESS; \
  366. retval |= UA_memcpy(dst, src, sizeof(TYPE)); \
  367. return retval; \
  368. }
  369. #define UA_TYPE_COPY_AS(TYPE, TYPE_AS) \
  370. UA_Int32 TYPE##_copy(TYPE const *src, TYPE *dst) { \
  371. return TYPE_AS##_copy((TYPE_AS *)src, (TYPE_AS *)dst); \
  372. }
  373. #define UA_TYPE_AS(TYPE, TYPE_AS) \
  374. UA_TYPE_NEW_DEFAULT(TYPE) \
  375. UA_TYPE_INIT_AS(TYPE, TYPE_AS) \
  376. UA_TYPE_DELETE_AS(TYPE, TYPE_AS) \
  377. UA_TYPE_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  378. UA_TYPE_COPY_AS(TYPE, TYPE_AS)
  379. /// @} /* end of group */
  380. #endif /* UA_TYPES_H_ */