ua_basictypes.h 13 KB

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