ua_basictypes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * ua_basictypes.h
  3. *
  4. * Created on: 13.03.2014
  5. * Author: mrt
  6. */
  7. #ifndef OPCUA_BASICTYPES_H_
  8. #define OPCUA_BASICTYPES_H_
  9. #include <stdint.h>
  10. #define DBG_VERBOSE(expression) // omit debug code
  11. #define DBG_ERR(expression) // omit debug code
  12. #define DBG(expression) // omit debug code
  13. #if defined(DEBUG) // --enable-debug=(yes|verbose)
  14. # undef DBG
  15. # define DBG(expression) expression
  16. # undef DBG_ERR
  17. # define DBG_ERR(expression) expression
  18. # if defined(VERBOSE) // --enable-debug=verbose
  19. # undef DBG_VERBOSE
  20. # define DBG_VERBOSE(expression) expression
  21. # endif
  22. #endif
  23. /* Basic types */
  24. typedef _Bool UA_Boolean;
  25. typedef uint8_t UA_Byte;
  26. typedef int8_t UA_SByte;
  27. typedef int16_t UA_Int16;
  28. typedef uint16_t UA_UInt16;
  29. typedef int32_t UA_Int32;
  30. typedef uint32_t UA_UInt32;
  31. typedef int64_t UA_Int64;
  32. typedef uint64_t UA_UInt64;
  33. typedef float UA_Float;
  34. typedef double UA_Double;
  35. /* ByteString - Part: 6, Chapter: 5.2.2.7, Page: 17 */
  36. typedef struct T_UA_ByteString
  37. {
  38. UA_Int32 length;
  39. UA_Byte* data;
  40. }
  41. UA_ByteString;
  42. /* Function return values */
  43. #define UA_SUCCESS 0
  44. #define UA_NO_ERROR UA_SUCCESS
  45. #define UA_ERROR (0x01 << 31)
  46. #define UA_ERR_INCONSISTENT (UA_ERROR | (0x01 << 1))
  47. #define UA_ERR_INVALID_VALUE (UA_ERROR | (0x01 << 2))
  48. #define UA_ERR_NO_MEMORY (UA_ERROR | (0x01 << 3))
  49. #define UA_ERR_NOT_IMPLEMENTED (UA_ERROR | (0x01 << 4))
  50. /* Boolean values and null */
  51. #define UA_TRUE (42==42)
  52. #define TRUE UA_TRUE
  53. #define UA_FALSE (!UA_TRUE)
  54. #define FALSE UA_FALSE
  55. /* Compare values */
  56. #define UA_EQUAL 0
  57. #define UA_NOT_EQUAL (!UA_EQUAL)
  58. /* heap memory functions */
  59. #define UA_NULL ((void*)0)
  60. extern void const * UA_alloc_lastptr;
  61. #define UA_free(ptr) _UA_free(ptr,#ptr,__FILE__,__LINE__)
  62. UA_Int32 _UA_free(void * ptr,char*,char*,int);
  63. UA_Int32 UA_memcpy(void *dst, void const *src, int size);
  64. #define UA_alloc(ptr,size) _UA_alloc(ptr,size,#ptr,#size,__FILE__,__LINE__)
  65. UA_Int32 _UA_alloc(void ** dst, int size,char*,char*,char*,int);
  66. /* Stop decoding at the first failure. Free members that were already allocated.
  67. It is assumed that retval is already defined. */
  68. #define CHECKED_DECODE(DECODE, CLEAN_UP) do { \
  69. retval |= DECODE; \
  70. if(retval != UA_SUCCESS) { \
  71. CLEAN_UP; \
  72. return retval; \
  73. } } while(0) \
  74. /* Array operations */
  75. UA_Int32 UA_Array_calcSize(UA_Int32 noElements, UA_Int32 type, void const * const * ptr);
  76. UA_Int32 UA_Array_encodeBinary(void const * const *src, UA_Int32 noElements, UA_Int32 type, UA_Int32* pos, UA_ByteString * dst);
  77. UA_Int32 UA_Array_decodeBinary(UA_ByteString const * src,UA_Int32 noElements, UA_Int32 type, UA_Int32* pos, void *** dst);
  78. UA_Int32 UA_Array_delete(void ***p,UA_Int32 noElements, UA_Int32 type);
  79. UA_Int32 UA_Array_init(void **p,UA_Int32 noElements, UA_Int32 type);
  80. UA_Int32 UA_Array_new(void ***p,UA_Int32 noElements, UA_Int32 type);
  81. UA_Int32 UA_Array_copy(void const * const *src,UA_Int32 noElements, UA_Int32 type, void ***dst);
  82. #define UA_TYPE_METHOD_PROTOTYPES(TYPE) \
  83. UA_Int32 TYPE##_calcSize(TYPE const * ptr); \
  84. UA_Int32 TYPE##_encodeBinary(TYPE const * src, UA_Int32* pos, UA_ByteString * dst); \
  85. UA_Int32 TYPE##_decodeBinary(UA_ByteString const * src, UA_Int32* pos, TYPE * dst); \
  86. UA_Int32 TYPE##_delete(TYPE * p); \
  87. UA_Int32 TYPE##_deleteMembers(TYPE * p); \
  88. UA_Int32 TYPE##_init(TYPE * p); \
  89. UA_Int32 TYPE##_new(TYPE ** p);
  90. #define UA_TYPE_METHOD_CALCSIZE_SIZEOF(TYPE) \
  91. UA_Int32 TYPE##_calcSize(TYPE const * p) { return sizeof(TYPE); }
  92. #define UA_TYPE_METHOD_CALCSIZE_AS(TYPE, TYPE_AS) \
  93. UA_Int32 TYPE##_calcSize(TYPE const * p) { return TYPE_AS##_calcSize((TYPE_AS*) p); }
  94. #define UA_TYPE_METHOD_DELETE_FREE(TYPE) \
  95. UA_Int32 TYPE##_delete(TYPE * p) { return UA_free(p); }
  96. #define UA_TYPE_METHOD_DELETE_AS(TYPE, TYPE_AS) \
  97. UA_Int32 TYPE##_delete(TYPE * p) { return TYPE_AS##_delete((TYPE_AS*) p);}
  98. #define UA_TYPE_METHOD_DELETE_STRUCT(TYPE) \
  99. UA_Int32 TYPE##_delete(TYPE *p) { \
  100. UA_Int32 retval = UA_SUCCESS; \
  101. retval |= TYPE##_deleteMembers(p); \
  102. retval |= UA_free(p); \
  103. return retval; \
  104. }
  105. #define UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(TYPE) \
  106. UA_Int32 TYPE##_deleteMembers(TYPE * p) { return UA_SUCCESS; }
  107. #define UA_TYPE_METHOD_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  108. UA_Int32 TYPE##_deleteMembers(TYPE * p) { return TYPE_AS##_deleteMembers((TYPE_AS*) p);}
  109. #define UA_TYPE_METHOD_DECODEBINARY_AS(TYPE,TYPE_AS) \
  110. UA_Int32 TYPE##_decodeBinary(UA_ByteString const * src, UA_Int32* pos, TYPE *dst) { \
  111. return TYPE_AS##_decodeBinary(src,pos,(TYPE_AS*) dst); \
  112. }
  113. #define UA_TYPE_METHOD_ENCODEBINARY_AS(TYPE,TYPE_AS) \
  114. UA_Int32 TYPE##_encodeBinary(TYPE const * src, UA_Int32* pos, UA_ByteString *dst) { \
  115. return TYPE_AS##_encodeBinary((TYPE_AS*) src,pos,dst); \
  116. }
  117. #define UA_TYPE_METHOD_INIT_AS(TYPE, TYPE_AS) \
  118. UA_Int32 TYPE##_init(TYPE * p){ \
  119. return TYPE_AS##_init((TYPE_AS*)p); \
  120. }
  121. #define UA_TYPE_METHOD_PROTOTYPES_AS(TYPE, TYPE_AS) \
  122. UA_TYPE_METHOD_CALCSIZE_AS(TYPE, TYPE_AS) \
  123. UA_TYPE_METHOD_ENCODEBINARY_AS(TYPE, TYPE_AS) \
  124. UA_TYPE_METHOD_DECODEBINARY_AS(TYPE, TYPE_AS) \
  125. UA_TYPE_METHOD_DELETE_AS(TYPE, TYPE_AS) \
  126. UA_TYPE_METHOD_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  127. UA_TYPE_METHOD_INIT_AS(TYPE, TYPE_AS)
  128. #define UA_TYPE_METHOD_NEW_DEFAULT(TYPE) \
  129. UA_Int32 TYPE##_new(TYPE ** p){ \
  130. UA_Int32 retval = UA_SUCCESS;\
  131. retval |= UA_alloc((void**)p, TYPE##_calcSize(UA_NULL));\
  132. retval |= TYPE##_init(*p);\
  133. return retval;\
  134. }
  135. #define UA_TYPE_METHOD_INIT_DEFAULT(TYPE) \
  136. UA_Int32 TYPE##_init(TYPE * p){ \
  137. if(p==UA_NULL)return UA_ERROR;\
  138. *p = (TYPE)0;\
  139. return UA_SUCCESS;\
  140. }
  141. /*** Prototypes for basic types **/
  142. UA_TYPE_METHOD_PROTOTYPES (UA_Boolean)
  143. UA_TYPE_METHOD_PROTOTYPES (UA_Byte)
  144. UA_TYPE_METHOD_PROTOTYPES (UA_SByte)
  145. UA_TYPE_METHOD_PROTOTYPES (UA_Int16)
  146. UA_TYPE_METHOD_PROTOTYPES (UA_UInt16)
  147. UA_TYPE_METHOD_PROTOTYPES (UA_Int32)
  148. UA_TYPE_METHOD_PROTOTYPES (UA_UInt32)
  149. UA_TYPE_METHOD_PROTOTYPES (UA_Int64)
  150. UA_TYPE_METHOD_PROTOTYPES (UA_UInt64)
  151. UA_TYPE_METHOD_PROTOTYPES (UA_Float)
  152. UA_TYPE_METHOD_PROTOTYPES (UA_Double)
  153. /**
  154. * StatusCodeBinaryEncoding
  155. * Part: 6
  156. * Chapter: 5.2.2.11
  157. * Page: 20
  158. *
  159. * StatusCodes aren't an enum (=int), since 32 unsigned bits are needed.
  160. * See also ua_statuscodes.h
  161. */
  162. typedef UA_UInt32 UA_StatusCode;
  163. UA_TYPE_METHOD_PROTOTYPES (UA_StatusCode)
  164. /** IntegerId - Part: 4, Chapter: 7.13, Page: 118 */
  165. typedef float UA_IntegerId;
  166. UA_TYPE_METHOD_PROTOTYPES (UA_IntegerId)
  167. typedef struct T_UA_VTable {
  168. UA_UInt32 ns0Id;
  169. UA_Int32 (*calcSize)(void const * ptr);
  170. UA_Int32 (*decodeBinary)(UA_ByteString const * src, UA_Int32* pos, void* dst);
  171. UA_Int32 (*encodeBinary)(void const * src, UA_Int32* pos, UA_ByteString* dst);
  172. UA_Int32 (*init)(void * p);
  173. UA_Int32 (*new)(void ** p);
  174. UA_Int32 (*delete)(void * p);
  175. UA_Byte* name;
  176. } UA_VTable;
  177. /* VariantBinaryEncoding - Part: 6, Chapter: 5.2.2.16, Page: 22 */
  178. enum UA_VARIANT_ENCODINGMASKTYPE_enum
  179. {
  180. UA_VARIANT_ENCODINGMASKTYPE_TYPEID_MASK = 0x3F, // bits 0:5
  181. UA_VARIANT_ENCODINGMASKTYPE_DIMENSIONS = (0x01 << 6), // bit 6
  182. UA_VARIANT_ENCODINGMASKTYPE_ARRAY = ( 0x01 << 7) // bit 7
  183. };
  184. typedef struct T_UA_Variant {
  185. UA_VTable* vt; // internal entry into vTable
  186. UA_Byte encodingMask; // Type of UA_Variant_EncodingMaskType_enum
  187. UA_Int32 arrayLength; // total number of elements
  188. void** data;
  189. } UA_Variant;
  190. UA_TYPE_METHOD_PROTOTYPES (UA_Variant)
  191. /* String - Part: 6, Chapter: 5.2.2.4, Page: 16 */
  192. typedef struct T_UA_String
  193. {
  194. UA_Int32 length;
  195. UA_Byte* data;
  196. }
  197. UA_String;
  198. UA_TYPE_METHOD_PROTOTYPES (UA_String)
  199. UA_Int32 UA_String_copy(UA_String const * src, UA_String* dst);
  200. UA_Int32 UA_String_copycstring(char const * src, UA_String* dst);
  201. UA_Int32 UA_String_copyprintf(char const * fmt, UA_String* dst, ...);
  202. UA_Int32 UA_String_compare(const UA_String *string1, const UA_String *string2);
  203. void UA_String_printf(char const * label, const UA_String* string);
  204. void UA_String_printx(char const * label, const UA_String* string);
  205. void UA_String_printx_hex(char const * label, const UA_String* string);
  206. /* ByteString - Part: 6, Chapter: 5.2.2.7, Page: 17 */
  207. UA_TYPE_METHOD_PROTOTYPES (UA_ByteString)
  208. UA_Int32 UA_ByteString_compare(const UA_ByteString *string1, const UA_ByteString *string2);
  209. UA_Int32 UA_ByteString_copy(UA_ByteString const * src, UA_ByteString* dst);
  210. UA_Int32 UA_ByteString_newMembers(UA_ByteString* p, UA_Int32 length);
  211. extern UA_ByteString UA_ByteString_securityPoliceNone;
  212. /** LocalizedTextBinaryEncoding - Part: 6, Chapter: 5.2.2.14, Page: 21 */
  213. enum UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_enum
  214. {
  215. UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE = 0x01,
  216. UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT = 0x02
  217. };
  218. typedef struct T_UA_LocalizedText
  219. {
  220. UA_Byte encodingMask;
  221. UA_String locale;
  222. UA_String text;
  223. }
  224. UA_LocalizedText;
  225. UA_TYPE_METHOD_PROTOTYPES (UA_LocalizedText)
  226. UA_Int32 UA_LocalizedText_copycstring(char const * src, UA_LocalizedText* dst);
  227. void UA_ByteString_printf(char* label, const UA_ByteString* string);
  228. void UA_ByteString_printx(char* label, const UA_ByteString* string);
  229. void UA_ByteString_printx_hex(char* label, const UA_ByteString* string);
  230. /* GuidType - Part: 6, Chapter: 5.2.2.6 Page: 17 */
  231. typedef struct T_UA_Guid
  232. {
  233. UA_UInt32 data1;
  234. UA_UInt16 data2;
  235. UA_UInt16 data3;
  236. UA_Byte data4[8];
  237. } UA_Guid;
  238. UA_TYPE_METHOD_PROTOTYPES (UA_Guid)
  239. UA_Int32 UA_Guid_compare(const UA_Guid *g1, const UA_Guid *g2);
  240. /* DateTime - Part: 6, Chapter: 5.2.2.5, Page: 16 */
  241. typedef UA_Int64 UA_DateTime; //100 nanosecond resolution
  242. UA_TYPE_METHOD_PROTOTYPES (UA_DateTime)
  243. UA_DateTime UA_DateTime_now();
  244. typedef struct T_UA_DateTimeStruct
  245. {
  246. UA_Int16 nanoSec;
  247. UA_Int16 microSec;
  248. UA_Int16 milliSec;
  249. UA_Int16 sec;
  250. UA_Int16 min;
  251. UA_Int16 hour;
  252. UA_Int16 day;
  253. UA_Int16 mounth;
  254. UA_Int16 year;
  255. } UA_DateTimeStruct;
  256. UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime time);
  257. UA_Int32 UA_DateTime_toString(UA_DateTime time, UA_String* timeString);
  258. typedef struct T_UA_NodeId
  259. {
  260. UA_Byte encodingByte; //enum BID_NodeIdEncodingValuesType
  261. UA_UInt16 namespace;
  262. union
  263. {
  264. UA_UInt32 numeric;
  265. UA_String string;
  266. UA_Guid guid;
  267. UA_ByteString byteString;
  268. }
  269. identifier;
  270. } UA_NodeId;
  271. UA_TYPE_METHOD_PROTOTYPES (UA_NodeId)
  272. UA_Int32 UA_NodeId_compare(const UA_NodeId *n1, const UA_NodeId *n2);
  273. void UA_NodeId_printf(char* label, const UA_NodeId* node);
  274. /** XmlElement - Part: 6, Chapter: 5.2.2.8, Page: 17 */
  275. typedef struct T_UA_XmlElement
  276. {
  277. //TODO Überlegung ob man es direkt als ByteString speichert oder als String
  278. UA_ByteString data;
  279. } UA_XmlElement;
  280. UA_TYPE_METHOD_PROTOTYPES (UA_XmlElement)
  281. /* ExpandedNodeId - Part: 6, Chapter: 5.2.2.10, Page: 19 */
  282. // 62541-6 Chapter 5.2.2.9, Table 5
  283. #define UA_NODEIDTYPE_NAMESPACE_URI_FLAG 0x80
  284. #define UA_NODEIDTYPE_SERVERINDEX_FLAG 0x40
  285. #define UA_NODEIDTYPE_MASK (~(UA_NODEIDTYPE_NAMESPACE_URI_FLAG | UA_NODEIDTYPE_SERVERINDEX_FLAG))
  286. typedef struct T_UA_ExpandedNodeId
  287. {
  288. UA_NodeId nodeId;
  289. UA_String namespaceUri;
  290. UA_UInt32 serverIndex;
  291. }
  292. UA_ExpandedNodeId;
  293. UA_TYPE_METHOD_PROTOTYPES(UA_ExpandedNodeId)
  294. typedef UA_Int32 UA_IdentifierType;
  295. UA_TYPE_METHOD_PROTOTYPES(UA_IdentifierType)
  296. /* ExtensionObjectBinaryEncoding - Part: 6, Chapter: 5.2.2.15, Page: 21 */
  297. typedef struct T_UA_ExtensionObject {
  298. UA_NodeId typeId;
  299. UA_Byte encoding; //Type of the enum UA_ExtensionObjectEncodingMaskType
  300. UA_ByteString body;
  301. } UA_ExtensionObject;
  302. UA_TYPE_METHOD_PROTOTYPES(UA_ExtensionObject)
  303. enum UA_ExtensionObject_EncodingMaskType_enum
  304. {
  305. UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED = 0x00,
  306. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING = 0x01,
  307. UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML = 0x02
  308. };
  309. /* QualifiedNameBinaryEncoding - Part: 6, Chapter: 5.2.2.13, Page: 20 */
  310. typedef struct T_UA_QualifiedName {
  311. UA_UInt16 namespaceIndex;
  312. UA_UInt16 reserved;
  313. UA_String name;
  314. } UA_QualifiedName;
  315. UA_TYPE_METHOD_PROTOTYPES(UA_QualifiedName)
  316. /* DataValue - Part: 6, Chapter: 5.2.2.17, Page: 23 */
  317. typedef struct UA_DataValue {
  318. UA_Byte encodingMask;
  319. UA_Variant value;
  320. UA_StatusCode status;
  321. UA_DateTime sourceTimestamp;
  322. UA_Int16 sourcePicoseconds;
  323. UA_DateTime serverTimestamp;
  324. UA_Int16 serverPicoseconds;
  325. } UA_DataValue;
  326. UA_TYPE_METHOD_PROTOTYPES(UA_DataValue)
  327. /** 62541-6, §5.2.2.17, Table 15 */
  328. enum UA_DATAVALUE_ENCODINGMASKTYPE_enum
  329. {
  330. UA_DATAVALUE_ENCODINGMASK_VARIANT = 0x01,
  331. UA_DATAVALUE_ENCODINGMASK_STATUSCODE = 0x02,
  332. UA_DATAVALUE_ENCODINGMASK_SOURCETIMESTAMP = 0x04,
  333. UA_DATAVALUE_ENCODINGMASK_SERVERTIMESTAMP = 0x08,
  334. UA_DATAVALUE_ENCODINGMASK_SOURCEPICOSECONDS = 0x10,
  335. UA_DATAVALUE_ENCODINGMASK_SERVERPICOSECONDS = 0x20
  336. };
  337. /* DiagnosticInfo - Part: 6, Chapter: 5.2.2.12, Page: 20 */
  338. typedef struct T_UA_DiagnosticInfo {
  339. UA_Byte encodingMask; //Type of the Enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE
  340. UA_Int32 symbolicId;
  341. UA_Int32 namespaceUri;
  342. UA_Int32 localizedText;
  343. UA_Int32 locale;
  344. UA_String additionalInfo;
  345. UA_StatusCode innerStatusCode;
  346. struct T_UA_DiagnosticInfo* innerDiagnosticInfo;
  347. } UA_DiagnosticInfo;
  348. UA_TYPE_METHOD_PROTOTYPES(UA_DiagnosticInfo)
  349. enum UA_DIAGNOSTICINFO_ENCODINGMASKTYPE_enum
  350. {
  351. UA_DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID = 0x01,
  352. UA_DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE = 0x02,
  353. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT = 0x04,
  354. UA_DIAGNOSTICINFO_ENCODINGMASK_LOCALE = 0x08,
  355. UA_DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO = 0x10,
  356. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE = 0x20,
  357. UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO = 0x40
  358. };
  359. typedef void UA_InvalidType;
  360. UA_TYPE_METHOD_PROTOTYPES (UA_InvalidType)
  361. #endif /* OPCUA_BASICTYPES_H_ */