opcua_basictypes.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*
  2. * opcua_basictypes.c
  3. *
  4. * Created on: 13.03.2014
  5. * Author: mrt
  6. */
  7. #include "opcua.h"
  8. #include <memory.h>
  9. Int32 UA_calcSize(void* const data, UInt32 type) {
  10. return (UA_namespace_zero[type].calcSize)(data);
  11. }
  12. Int32 UA_Array_calcSize(Int32 nElements, Int32 type, void const ** data) {
  13. int length = sizeof(UA_Int32);
  14. int i;
  15. if (nElements > 0) {
  16. for(i=0; i<nElements;i++,data++) {
  17. length += UA_calcSize(data,type);
  18. }
  19. }
  20. return length;
  21. }
  22. #define UA_TYPE_METHOD_CALCSIZE_SIZEOF(TYPE) \
  23. Int32 TYPE##_calcSize(TYPE const * p) { return sizeof(TYPE); }
  24. #define UA_TYPE_METHOD_CALCSIZE_AS(TYPE, TYPE_AS) \
  25. Int32 TYPE##_calcSize(TYPE const * p) { return TYPE_AS##_calcSize((TYPE_AS*) p); }
  26. #define UA_TYPE_METHOD_DELETE_MEMFREE(TYPE) \
  27. Int32 TYPE##_delete(TYPE * p) { return UA_memfree(p); };
  28. #define UA_TYPE_METHOD_DELETE_AS(TYPE, TYPE_AS) \
  29. Int32 TYPE##_delete(TYPE * p) { return TYPE_AS##_delete((TYPE_AS*) p);};
  30. #define UA_TYPE_METHOD_DELETE_STRUCT(TYPE) \
  31. Int32 TYPE##_delete(TYPE *p) { \
  32. Int32 retval = UA_SUCCESS; \
  33. retval |= TYPE##_deleteMembers(p); \
  34. retval |= UA_memfree(p); \
  35. return retval; \
  36. }
  37. #define UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(TYPE) \
  38. Int32 TYPE##_deleteMembers(TYPE * p) { return UA_SUCCESS; };
  39. #define UA_TYPE_METHOD_DELETEMEMBERS_AS(TYPE, TYPE_AS) \
  40. Int32 TYPE##_deleteMembers(TYPE * p) { return TYPE_AS##_deleteMembers((TYPE_AS*) p);};
  41. #define UA_TYPE_METHOD_DECODE_AS(TYPE,TYPE_AS) \
  42. Int32 TYPE##_decode(char const * src, Int32* pos, TYPE *dst) { \
  43. return TYPE_AS##_decode(src,pos,(TYPE_AS*) dst); \
  44. }
  45. #define UA_TYPE_METHOD_ENCODE_AS(TYPE,TYPE_AS) \
  46. Int32 TYPE##_encode(TYPE const * src, Int32* pos, char *dst) { \
  47. return TYPE_AS##_encode((TYPE_AS*) src,pos,dst); \
  48. }
  49. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Boolean)
  50. Int32 UA_Boolean_encode(UA_Boolean const * src, Int32* pos, char * dst) {
  51. UA_Boolean tmpBool = ((*src > 0) ? UA_TRUE : UA_FALSE);
  52. memcpy(&(dst[(*pos)++]), &tmpBool, sizeof(UA_Boolean));
  53. return UA_SUCCESS;
  54. }
  55. Int32 UA_Boolean_decode(char const * src, Int32* pos, UA_Boolean * dst) {
  56. *dst = ((UA_Boolean) (src[(*pos)++]) > 0) ? UA_TRUE : UA_FALSE;
  57. return UA_SUCCESS;
  58. }
  59. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Boolean)
  60. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Boolean)
  61. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Byte)
  62. Int32 UA_Byte_encode(UA_Byte const * src, Int32* pos, char * dst) {
  63. *dst = src[(*pos)++];
  64. return UA_SUCCESS;
  65. }
  66. Int32 UA_Byte_decode(char const * src, Int32* pos, UA_Byte * dst) {
  67. memcpy(&(dst[(*pos)++]), src, sizeof(UA_Byte));
  68. return UA_SUCCESS;
  69. }
  70. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Byte)
  71. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Byte)
  72. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_SByte)
  73. Int32 UA_SByte_encode(UA_SByte const * src, Int32* pos, char * dst) {
  74. dst[(*pos)++] = *src;
  75. return UA_SUCCESS;
  76. }
  77. Int32 UA_SByte_decode(char const * src, Int32* pos, UA_SByte * dst) {
  78. *dst = src[(*pos)++];
  79. return 1;
  80. }
  81. UA_TYPE_METHOD_DELETE_MEMFREE(UA_SByte)
  82. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_SByte)
  83. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_UInt16)
  84. Int32 UA_UInt16_encode(UA_UInt16 const *src, Int32* pos, char * dst) {
  85. memcpy(&(dst[*pos]), src, sizeof(UA_UInt16));
  86. *pos += sizeof(UA_UInt16);
  87. return UA_SUCCESS;
  88. }
  89. Int32 UA_UInt16_decode(char const * src, Int32* pos, UA_UInt16* dst) {
  90. Byte t1 = src[(*pos)++];
  91. UInt16 t2 = (UInt16) (src[(*pos)++] << 8);
  92. *dst = t1 + t2;
  93. return UA_SUCCESS;
  94. }
  95. UA_TYPE_METHOD_DELETE_MEMFREE(UA_UInt16)
  96. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_UInt16)
  97. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Int16)
  98. Int32 UA_Int16_encode(UA_Int16 const * src, Int32* pos, char* dst) {
  99. memcpy(&(dst[*pos]), src, sizeof(UA_Int16));
  100. *pos += sizeof(UA_Int16);
  101. return UA_SUCCESS;
  102. }
  103. Int32 UA_Int16_decode(char const * src, Int32* pos, UA_Int16 *dst) {
  104. Int16 t1 = (Int16) (((SByte) (src[(*pos)++]) & 0xFF));
  105. Int16 t2 = (Int16) (((SByte) (src[(*pos)++]) & 0xFF) << 8);
  106. *dst = t1 + t2;
  107. return UA_SUCCESS;
  108. }
  109. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Int16)
  110. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Int16)
  111. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Int32)
  112. Int32 UA_Int32_encode(UA_Int32 const * src, Int32* pos, char *dst) {
  113. memcpy(&(dst[*pos]), src, sizeof(UA_Int32));
  114. *pos += sizeof(UA_Int32);
  115. return UA_SUCCESS;
  116. }
  117. Int32 UA_Int32_decode(char const * src, Int32* pos, UA_Int32* dst) {
  118. Int32 t1 = (Int32) (((SByte) (src[(*pos)++]) & 0xFF));
  119. Int32 t2 = (Int32) (((SByte) (src[(*pos)++]) & 0xFF) << 8);
  120. Int32 t3 = (Int32) (((SByte) (src[(*pos)++]) & 0xFF) << 16);
  121. Int32 t4 = (Int32) (((SByte) (src[(*pos)++]) & 0xFF) << 24);
  122. *dst = t1 + t2 + t3 + t4;
  123. return UA_SUCCESS;
  124. }
  125. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Int32)
  126. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Int32)
  127. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_UInt32)
  128. Int32 UA_UInt32_encode(UA_UInt32 const * src, Int32* pos, char *dst) {
  129. memcpy(&(dst[*pos]), src, sizeof(UA_UInt32));
  130. *pos += sizeof(UA_UInt32);
  131. return UA_SUCCESS;
  132. }
  133. Int32 UA_UInt32_decode(char const * src, Int32* pos, UA_UInt32 *dst) {
  134. UInt32 t1 = (UInt32) src[(*pos)++];
  135. UInt32 t2 = (UInt32) src[(*pos)++] << 8;
  136. UInt32 t3 = (UInt32) src[(*pos)++] << 16;
  137. UInt32 t4 = (UInt32) src[(*pos)++] << 24;
  138. *dst = t1 + t2 + t3 + t4;
  139. return UA_SUCCESS;
  140. }
  141. UA_TYPE_METHOD_DELETE_MEMFREE(UA_UInt32)
  142. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_UInt32)
  143. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Int64)
  144. Int32 UA_Int64_encode(UA_Int64 const * src, Int32* pos, char *dst) {
  145. memcpy(&(dst[*pos]), src, sizeof(UA_Int64));
  146. *pos += sizeof(UA_Int64);
  147. return UA_SUCCESS;
  148. }
  149. Int32 UA_Int64_decode(char const * src, Int32* pos, UA_Int64* dst) {
  150. Int64 t1 = (Int64) src[(*pos)++];
  151. Int64 t2 = (Int64) src[(*pos)++] << 8;
  152. Int64 t3 = (Int64) src[(*pos)++] << 16;
  153. Int64 t4 = (Int64) src[(*pos)++] << 24;
  154. Int64 t5 = (Int64) src[(*pos)++] << 32;
  155. Int64 t6 = (Int64) src[(*pos)++] << 40;
  156. Int64 t7 = (Int64) src[(*pos)++] << 48;
  157. Int64 t8 = (Int64) src[(*pos)++] << 56;
  158. *dst = t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  159. return UA_SUCCESS;
  160. }
  161. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Int64)
  162. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Int64)
  163. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_UInt64)
  164. Int32 UA_UInt64_encode(UA_UInt64 const * src , Int32* pos, char * dst) {
  165. memcpy(&(dst[*pos]), src, sizeof(UA_UInt64));
  166. *pos += sizeof(UInt64);
  167. return UA_SUCCESS;
  168. }
  169. Int32 UA_UInt64_decode(char const * src, Int32* pos, UA_UInt64* dst) {
  170. UInt64 t1 = (UInt64) src[(*pos)++];
  171. UInt64 t2 = (UInt64) src[(*pos)++] << 8;
  172. UInt64 t3 = (UInt64) src[(*pos)++] << 16;
  173. UInt64 t4 = (UInt64) src[(*pos)++] << 24;
  174. UInt64 t5 = (UInt64) src[(*pos)++] << 32;
  175. UInt64 t6 = (UInt64) src[(*pos)++] << 40;
  176. UInt64 t7 = (UInt64) src[(*pos)++] << 48;
  177. UInt64 t8 = (UInt64) src[(*pos)++] << 56;
  178. *dst = t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  179. return UA_SUCCESS;
  180. }
  181. UA_TYPE_METHOD_DELETE_MEMFREE(UA_UInt64)
  182. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_UInt64)
  183. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Float)
  184. Int32 UA_Float_decode(char const * src, Int32* pos, UA_Float* dst) {
  185. // TODO: not yet implemented
  186. memcpy(dst, &(src[*pos]), sizeof(UA_Float));
  187. *pos += sizeof(UA_Float);
  188. return UA_SUCCESS;
  189. }
  190. Int32 UA_Float_encode(UA_Float const * src, Int32* pos, char *dst) {
  191. // TODO: not yet implemented
  192. memcpy(&(dst[*pos]), src, sizeof(UA_Float));
  193. *pos += sizeof(UA_Float);
  194. return UA_SUCCESS;
  195. }
  196. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Float)
  197. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Float)
  198. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_Double)
  199. Int32 UA_Double_decode(char const * src, Int32* pos, UA_Double * dst) {
  200. // TODO: not yet implemented
  201. Double tmpDouble;
  202. tmpDouble = (Double) (src[*pos]);
  203. *pos += sizeof(UA_Double);
  204. *dst = tmpDouble;
  205. return UA_SUCCESS;
  206. }
  207. Int32 UA_Double_encode(UA_Double const * src, Int32 *pos, char * dst) {
  208. // TODO: not yet implemented
  209. memcpy(&(dst[*pos]), src, sizeof(UA_Double));
  210. *pos *= sizeof(UA_Double);
  211. return UA_SUCCESS;
  212. }
  213. UA_TYPE_METHOD_DELETE_MEMFREE(UA_Double)
  214. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_Double)
  215. Int32 UA_String_calcSize(UA_String const * string) {
  216. if (string->length > 0) {
  217. return string->length + sizeof(string->length);
  218. } else {
  219. return sizeof(UA_Int32);
  220. }
  221. }
  222. Int32 UA_String_encode(UA_String const * src, Int32* pos, char *dst) {
  223. UA_Int32_encode(&(src->length),pos,dst);
  224. if (src->length > 0) {
  225. UA_memcpy(&(dst[*pos]), src->data, src->length);
  226. *pos += src->length;
  227. }
  228. return UA_SUCCESS;
  229. }
  230. Int32 UA_String_decode(char const * src, Int32* pos, UA_String * dst) {
  231. Int32 retval = UA_SUCCESS;
  232. retval |= UA_Int32_decode(src,pos,&(dst->length));
  233. if (dst->length > 0) {
  234. retval |= UA_alloc(&(dst->data),dst->length);
  235. retval |= UA_memcpy(&(src[*pos]),dst->data,dst->length);
  236. *pos += dst->length;
  237. } else {
  238. dst->data = UA_NULL;
  239. }
  240. return retval;
  241. }
  242. UA_TYPE_METHOD_DELETE_STRUCT(UA_String)
  243. Int32 UA_String_deleteMembers(UA_String* p) { return UA_memfree(p->data); };
  244. Int32 UA_String_copy(UA_String const * src, UA_String* dst) {
  245. Int32 retval = UA_SUCCESS;
  246. dst->length = src->length;
  247. dst->data = UA_NULL;
  248. if (src->length > 0) {
  249. retval |= UA_alloc(&(dst->data), src->length);
  250. if (retval == UA_SUCCESS) {
  251. retval |= UA_memcpy(dst->data, src->data, src->length);
  252. }
  253. }
  254. return retval;
  255. }
  256. UA_String UA_String_null = { -1, UA_NULL };
  257. UA_Byte UA_Byte_securityPoliceNoneData[] = "http://opcfoundation.org/UA/SecurityPolicy#None";
  258. UA_String UA_String_securityPoliceNone = { sizeof(UA_Byte_securityPoliceNoneData), UA_Byte_securityPoliceNoneData };
  259. // TODO: should we really handle UA_String and UA_ByteString the same way?
  260. UA_TYPE_METHOD_CALCSIZE_AS(UA_ByteString, UA_String)
  261. UA_TYPE_METHOD_ENCODE_AS(UA_ByteString, UA_String)
  262. UA_TYPE_METHOD_DECODE_AS(UA_ByteString, UA_String)
  263. UA_TYPE_METHOD_DELETE_AS(UA_ByteString, UA_String)
  264. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_ByteString, UA_String)
  265. Int32 UA_Guid_calcSize(UA_Guid const * guid) {
  266. return 0
  267. + sizeof(guid->data1)
  268. + sizeof(guid->data2)
  269. + sizeof(guid->data3)
  270. + UA_ByteString_calcSize(&(guid->data4))
  271. ;
  272. }
  273. // TODO: UA_Guid_encode
  274. Int32 UA_Guid_encode(UA_Guid const *src, Int32* pos, char *dst) {
  275. Int32 retval = UA_SUCCESS;
  276. retval |= UA_UInt32_encode(&(src->data1), pos, dst);
  277. retval |= UA_UInt16_encode(&(src->data2), pos, dst);
  278. retval |= UA_UInt16_encode(&(src->data3), pos, dst);
  279. retval |= UA_ByteString_encode(&(src->data4), pos, dst);
  280. return UA_SUCCESS;
  281. }
  282. Int32 UA_Guid_decode(char const * src, Int32* pos, UA_Guid *dst) {
  283. Int32 retval = UA_SUCCESS;
  284. retval |= UA_Int32_decode(src,pos,&(dst->data1));
  285. retval |= UA_Int16_decode(src,pos,&(dst->data2));
  286. retval |= UA_Int16_decode(src,pos,&(dst->data3));
  287. retval |= UA_ByteString_decode(src,pos,&(dst->data4));
  288. return retval;
  289. }
  290. UA_TYPE_METHOD_DELETE_STRUCT(UA_Guid)
  291. Int32 UA_Guid_deleteMembers(UA_Guid* p) { return UA_ByteString_delete(&(p->data4)); };
  292. Int32 UA_LocalizedText_calcSize(UA_LocalizedText const * localizedText) {
  293. Int32 length = 0;
  294. length += localizedText->encodingMask;
  295. if (localizedText->encodingMask & 0x01) {
  296. length += UA_String_calcSize(&(localizedText->locale));
  297. }
  298. if (localizedText->encodingMask & 0x02) {
  299. length += UA_String_calcSize(&(localizedText->text));
  300. }
  301. return length;
  302. }
  303. Int32 UA_LocalizedText_encode(UA_LocalizedText const * src, Int32 *pos,
  304. char * dst) {
  305. Int32 retval = UA_SUCCESS;
  306. retval |= UA_Byte_encode(&(src->encodingMask),pos,dst);
  307. if (src->encodingMask & 0x01) {
  308. UA_String_encode(&(src->locale),pos,dst);
  309. }
  310. if (src->encodingMask & 0x02) {
  311. UA_String_encode(&(src->text),pos,dst);
  312. }
  313. return retval;
  314. }
  315. Int32 UA_LocalizedText_decode(char const * src, Int32 *pos,
  316. UA_LocalizedText *dst) {
  317. Int32 retval = UA_SUCCESS;
  318. retval |= UA_String_copy(&UA_String_null,&(dst->locale));
  319. retval |= UA_String_copy(&UA_String_null,&(dst->text));
  320. retval |= UA_Byte_decode(src,pos,&(dst->encodingMask));
  321. if (dst->encodingMask & 0x01) {
  322. retval |= UA_String_decode(src,pos,&(dst->locale));
  323. }
  324. if (dst->encodingMask & 0x02) {
  325. retval |= UA_String_decode(src,pos,&(dst->text));
  326. }
  327. return retval;
  328. }
  329. UA_TYPE_METHOD_DELETE_STRUCT(UA_LocalizedText)
  330. Int32 UA_LocalizedText_deleteMembers(UA_LocalizedText* p) {
  331. return UA_SUCCESS
  332. || UA_String_deleteMembers(&(p->locale))
  333. || UA_String_deleteMembers(&(p->text))
  334. ;
  335. };
  336. /* Serialization of UA_NodeID is specified in 62541-6, §5.2.2.9 */
  337. Int32 UA_NodeId_calcSize(UA_NodeId const *nodeId) {
  338. Int32 length = 0;
  339. switch (nodeId->encodingByte) {
  340. case NIEVT_TWO_BYTE:
  341. length += 2 * sizeof(UA_Byte);
  342. break;
  343. case NIEVT_FOUR_BYTE:
  344. length += 4 * sizeof(UA_Byte);
  345. break;
  346. case NIEVT_NUMERIC:
  347. length += sizeof(UA_Byte) + sizeof(UA_UInt16) + sizeof(UInt32);
  348. break;
  349. case NIEVT_STRING:
  350. length += sizeof(UA_Byte) + sizeof(UA_UInt16) + UA_String_calcSize(&(nodeId->identifier.string));
  351. break;
  352. case NIEVT_GUID:
  353. length += sizeof(UA_Byte) + sizeof(UA_UInt16) + UA_Guid_calcSize(&(nodeId->identifier.guid));
  354. break;
  355. case NIEVT_BYTESTRING:
  356. length += sizeof(UA_Byte) + sizeof(UA_UInt16) + UA_ByteString_calcSize(&(nodeId->identifier.byteString));
  357. break;
  358. default:
  359. break;
  360. }
  361. return length;
  362. }
  363. Int32 UA_NodeId_encode(UA_NodeId const * src, Int32* pos, char *dst) {
  364. // temporary variables
  365. UA_Byte srcByte;
  366. UA_UInt16 srcUInt16;
  367. int retval = UA_SUCCESS;
  368. retval |= UA_Byte_encode(&(src->encodingByte),pos,dst);
  369. switch (src->encodingByte) {
  370. case NIEVT_TWO_BYTE:
  371. srcByte = src->identifier.numeric;
  372. retval |= UA_Byte_encode(&srcByte,pos,dst);
  373. break;
  374. case NIEVT_FOUR_BYTE:
  375. srcByte = src->namespace;
  376. srcUInt16 = src->identifier.numeric;
  377. retval |= UA_Byte_encode(&srcByte,pos,dst);
  378. retval |= UA_UInt16_encode(&srcUInt16,pos,dst);
  379. break;
  380. case NIEVT_NUMERIC:
  381. retval |= UA_UInt16_encode(&(src->namespace), pos, dst);
  382. retval |= UA_UInt32_encode(&(src->identifier.numeric), pos, dst);
  383. break;
  384. case NIEVT_STRING:
  385. retval |= UA_UInt16_encode(&(src->namespace), pos, dst);
  386. retval |= UA_String_encode(&(src->identifier.string), pos, dst);
  387. break;
  388. case NIEVT_GUID:
  389. retval |= UA_UInt16_encode(&(src->namespace), pos, dst);
  390. retval |= UA_Guid_encode(&(src->identifier.guid), pos, dst);
  391. break;
  392. case NIEVT_BYTESTRING:
  393. retval |= UA_UInt16_encode(&(src->namespace), pos, dst);
  394. retval |= UA_ByteString_encode(&(src->identifier.byteString), pos, dst);
  395. break;
  396. }
  397. return retval;
  398. }
  399. Int32 UA_NodeId_decode(char const * src, Int32* pos, UA_NodeId *dst) {
  400. int retval = UA_SUCCESS;
  401. // temporary variables to overcome decoder's non-endian-saveness for datatypes
  402. Byte dstByte;
  403. UInt16 dstUInt16;
  404. retval |= UA_Byte_decode(src,pos,&(dst->encodingByte));
  405. switch (dst->encodingByte) {
  406. case NIEVT_TWO_BYTE: // Table 7
  407. retval |=UA_Byte_decode(src, pos, &dstByte);
  408. dst->identifier.numeric = dstByte;
  409. dst->namespace = 0; // default namespace
  410. break;
  411. case NIEVT_FOUR_BYTE: // Table 8
  412. retval |=UA_Byte_decode(src, pos, &dstByte);
  413. dst->namespace= dstByte;
  414. retval |=UA_UInt16_decode(src, pos, &dstUInt16);
  415. dst->identifier.numeric = dstUInt16;
  416. break;
  417. case NIEVT_NUMERIC: // Table 6, first entry
  418. retval |=UA_Int16_decode(src,pos,&(dst->namespace));
  419. retval |=UA_Int32_decode(src,pos,&(dst->identifier.numeric));
  420. break;
  421. case NIEVT_STRING: // Table 6, second entry
  422. retval |=UA_Int16_decode(src,pos,&(dst->namespace));
  423. retval |=UA_String_decode(src,pos,&(dst->identifier.string));
  424. break;
  425. case NIEVT_GUID: // Table 6, third entry
  426. retval |=UA_Int16_decode(src,pos,&(dst->namespace));
  427. retval |=UA_Guid_decode(src,pos,&(dst->identifier.guid));
  428. break;
  429. case NIEVT_BYTESTRING: // Table 6, "OPAQUE"
  430. retval |=UA_Int16_decode(src,pos,&(dst->namespace));
  431. retval |=UA_ByteString_decode(src,pos,&(dst->identifier.byteString));
  432. break;
  433. }
  434. return retval;
  435. }
  436. UA_TYPE_METHOD_DELETE_STRUCT(UA_NodeId)
  437. Int32 UA_NodeId_deleteMembers(UA_NodeId* p) {
  438. int retval = UA_SUCCESS;
  439. switch (p->encodingByte) {
  440. case NIEVT_TWO_BYTE:
  441. case NIEVT_FOUR_BYTE:
  442. case NIEVT_NUMERIC:
  443. // nothing to do
  444. break;
  445. case NIEVT_STRING: // Table 6, second entry
  446. retval |= UA_String_deleteMembers(&(p->identifier.string));
  447. break;
  448. case NIEVT_GUID: // Table 6, third entry
  449. retval |= UA_Guid_deleteMembers(&(p->identifier.guid));
  450. break;
  451. case NIEVT_BYTESTRING: // Table 6, "OPAQUE"
  452. retval |= UA_ByteString_deleteMembers(&(p->identifier.byteString));
  453. break;
  454. }
  455. return retval;
  456. }
  457. Int32 UA_ExpandedNodeId_calcSize(UA_ExpandedNodeId const * nodeId) {
  458. Int32 length = UA_NodeId_calcSize(&(nodeId->nodeId));
  459. if (nodeId->nodeId.encodingByte & NIEVT_NAMESPACE_URI_FLAG) {
  460. length += UA_String_calcSize(&(nodeId->namespaceUri)); //nodeId->namespaceUri
  461. }
  462. if (nodeId->nodeId.encodingByte & NIEVT_SERVERINDEX_FLAG) {
  463. length += sizeof(UInt32); //nodeId->serverIndex
  464. }
  465. return length;
  466. }
  467. Int32 UA_ExpandedNodeId_encode(UA_ExpandedNodeId const * src, Int32* pos, char *dst) {
  468. UInt32 retval = UA_SUCCESS;
  469. retval |= UA_NodeId_encode(&(src->nodeId),pos,dst);
  470. if (src->nodeId.encodingByte & NIEVT_NAMESPACE_URI_FLAG) {
  471. retval |= UA_String_encode(&(src->namespaceUri),pos,dst);
  472. }
  473. if (src->nodeId.encodingByte & NIEVT_SERVERINDEX_FLAG) {
  474. retval |= UA_UInt32_encode(&(src->serverIndex),pos,dst);
  475. }
  476. return retval;
  477. }
  478. Int32 UA_ExpandedNodeId_decode(char const * src, Int32* pos,
  479. UA_ExpandedNodeId *dst) {
  480. UInt32 retval = UA_SUCCESS;
  481. retval |= UA_NodeId_decode(src,pos,&(dst->nodeId));
  482. if (dst->nodeId.encodingByte & NIEVT_NAMESPACE_URI_FLAG) {
  483. dst->nodeId.namespace = 0;
  484. retval |= UA_String_decode(src,pos,&(dst->namespaceUri));
  485. } else {
  486. retval |= UA_String_copy(&UA_String_null, &(dst->namespaceUri));
  487. }
  488. if (dst->nodeId.encodingByte & NIEVT_SERVERINDEX_FLAG) {
  489. retval |= UA_UInt32_decode(src,pos,&(dst->serverIndex));
  490. }
  491. return retval;
  492. }
  493. UA_TYPE_METHOD_DELETE_STRUCT(UA_ExpandedNodeId)
  494. Int32 UA_ExpandedNodeId_deleteMembers(UA_ExpandedNodeId* p) {
  495. Int32 retval = UA_SUCCESS;
  496. retval |= UA_NodeId_deleteMembers(&(p->nodeId));
  497. retval |= UA_String_deleteMembers(&(p->namespaceUri));
  498. return retval;
  499. }
  500. Int32 UA_ExtensionObject_calcSize(UA_ExtensionObject const * extensionObject) {
  501. Int32 length = 0;
  502. length += UA_NodeId_calcSize(&(extensionObject->typeId));
  503. length += sizeof(Byte); //extensionObject->Encoding
  504. switch (extensionObject->encoding) {
  505. case 0x00:
  506. length += sizeof(Int32); //extensionObject->Body.Length
  507. break;
  508. case 0x01:
  509. length += UA_ByteString_calcSize(&(extensionObject->body));
  510. break;
  511. case 0x02:
  512. length += UA_ByteString_calcSize(&(extensionObject->body));
  513. break;
  514. }
  515. return length;
  516. }
  517. Int32 UA_ExtensionObject_encode(UA_ExtensionObject const *src, Int32* pos, char * dst) {
  518. Int32 retval = UA_SUCCESS;
  519. retval |= UA_NodeId_encode(&(src->typeId),pos,dst);
  520. retval |= UA_Byte_encode(&(src->encoding),pos,dst);
  521. switch (src->encoding) {
  522. case NO_BODY_IS_ENCODED:
  523. break;
  524. case BODY_IS_BYTE_STRING:
  525. case BODY_IS_XML_ELEMENT:
  526. retval |= UA_ByteString_encode(&(src->body),pos,dst);
  527. break;
  528. }
  529. return retval;
  530. }
  531. Int32 UA_ExtensionObject_decode(char const * src, Int32 *pos,
  532. UA_ExtensionObject *dst) {
  533. Int32 retval = UA_SUCCESS;
  534. retval |= UA_NodeId_decode(src,pos,&(dst->typeId));
  535. retval |= UA_Byte_decode(src,pos,&(dst->encoding));
  536. retval |= UA_String_copy(&UA_String_null, (UA_String*) &(dst->body));
  537. switch (dst->encoding) {
  538. case NO_BODY_IS_ENCODED:
  539. break;
  540. case BODY_IS_BYTE_STRING:
  541. case BODY_IS_XML_ELEMENT:
  542. retval |= UA_ByteString_decode(src,pos,&(dst->body));
  543. break;
  544. }
  545. return retval;
  546. }
  547. Int32 UA_ExtensionObject_delete(UA_ExtensionObject *p) {
  548. Int32 retval = UA_SUCCESS;
  549. retval |= UA_ExtensionObject_deleteMembers(p);
  550. retval |= UA_memfree(p);
  551. return retval;
  552. }
  553. // TODO: UA_ExtensionObject_delete
  554. Int32 UA_ExtensionObject_deleteMembers(UA_ExtensionObject *p) {
  555. Int32 retval = UA_SUCCESS;
  556. retval |= UA_NodeId_deleteMembers(&(p->typeId));
  557. retval |= UA_String_deleteMembers(&(p->body));
  558. return retval;
  559. }
  560. // TODO: UA_DataValue_encode
  561. // TODO: UA_DataValue_decode
  562. // TODO: UA_DataValue_delete
  563. // TODO: UA_DataValue_deleteMembers
  564. Int32 UA_DiagnosticInfo_calcSize(UA_DiagnosticInfo const * diagnosticInfo) {
  565. int i;
  566. Int32 length = sizeof(UA_Byte); // EncodingMask;
  567. for (i=0;i<8;i++) {
  568. // iterate over all bits
  569. switch (diagnosticInfo->encodingMask & (0x01 << i)) {
  570. case DIEMT_SYMBOLIC_ID:
  571. length += sizeof(Int32);
  572. break;
  573. case DIEMT_NAMESPACE:
  574. length += sizeof(Int32);
  575. break;
  576. case DIEMT_LOCALIZED_TEXT:
  577. length += sizeof(Int32);
  578. break;
  579. case DIEMT_LOCALE:
  580. length += sizeof(Int32);
  581. break;
  582. case DIEMT_ADDITIONAL_INFO:
  583. length += UA_String_calcSize(&(diagnosticInfo->additionalInfo));
  584. break;
  585. case DIEMT_INNER_STATUS_CODE:
  586. length += UA_StatusCode_calcSize(&(diagnosticInfo->innerStatusCode));
  587. break;
  588. case DIEMT_INNER_DIAGNOSTIC_INFO:
  589. length += UA_DiagnosticInfo_calcSize(
  590. diagnosticInfo->innerDiagnosticInfo);
  591. break;
  592. }
  593. }
  594. return length;
  595. }
  596. // TODO: UA_DiagnosticInfo_encode
  597. // TODO: UA_DiagnosticInfo_decode
  598. // TODO: UA_DiagnosticInfo_delete
  599. // TODO: UA_DiagnosticInfo_deleteMembers
  600. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_DateTime)
  601. UA_TYPE_METHOD_ENCODE_AS(UA_DateTime,UA_Int64)
  602. UA_TYPE_METHOD_DECODE_AS(UA_DateTime,UA_Int64)
  603. UA_TYPE_METHOD_DELETE_MEMFREE(UA_DateTime)
  604. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_DateTime)
  605. UA_TYPE_METHOD_CALCSIZE_AS(UA_XmlElement, UA_ByteString)
  606. UA_TYPE_METHOD_ENCODE_AS(UA_XmlElement, UA_ByteString)
  607. UA_TYPE_METHOD_DECODE_AS(UA_XmlElement, UA_ByteString)
  608. UA_TYPE_METHOD_DELETE_AS(UA_XmlElement, UA_ByteString)
  609. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_XmlElement, UA_ByteString)
  610. /** IntegerId - Part: 4, Chapter: 7.13, Page: 118 */
  611. UA_TYPE_METHOD_CALCSIZE_AS(UA_IntegerId, UA_Int32)
  612. UA_TYPE_METHOD_ENCODE_AS(UA_IntegerId, UA_Int32)
  613. UA_TYPE_METHOD_DECODE_AS(UA_IntegerId, UA_Int32)
  614. UA_TYPE_METHOD_DELETE_AS(UA_IntegerId, UA_Int32)
  615. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_IntegerId, UA_Int32)
  616. UA_TYPE_METHOD_CALCSIZE_AS(UA_StatusCode, UA_UInt32)
  617. UA_TYPE_METHOD_ENCODE_AS(UA_StatusCode, UA_UInt32)
  618. UA_TYPE_METHOD_DECODE_AS(UA_StatusCode, UA_UInt32)
  619. UA_TYPE_METHOD_DELETE_AS(UA_StatusCode, UA_UInt32)
  620. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_StatusCode, UA_UInt32)
  621. Int32 UA_QualifiedName_calcSize(UA_QualifiedName const * p) {
  622. Int32 length = 0;
  623. length += sizeof(UInt16); //qualifiedName->namespaceIndex
  624. length += sizeof(UInt16); //qualifiedName->reserved
  625. length += UA_String_calcSize(&(p->name)); //qualifiedName->name
  626. return length;
  627. }
  628. Int32 UA_QualifiedName_decode(char const * src, Int32 *pos,
  629. UA_QualifiedName *dst) {
  630. Int32 retval = UA_SUCCESS;
  631. retval |= UA_UInt16_decode(src,pos,&(dst->namespaceIndex));
  632. retval |= UA_UInt16_decode(src,pos,&(dst->reserved));
  633. retval |= UA_String_decode(src,pos,&(dst->name));
  634. return retval;
  635. }
  636. Int32 UA_QualifiedName_encode(UA_QualifiedName const *src, Int32* pos,
  637. char *dst) {
  638. Int32 retval = UA_SUCCESS;
  639. retval |= UA_UInt16_encode(&(src->namespaceIndex),pos,dst);
  640. retval |= UA_UInt16_encode(&(src->reserved),pos,dst);
  641. retval |= UA_String_encode(&(src->name),pos,dst);
  642. return retval;
  643. }
  644. Int32 UA_Variant_calcSize(UA_Variant const * p) {
  645. Int32 length = 0;
  646. Int32 ns0Id = p->encodingMask & 0x1F; // Bits 1-5
  647. Boolean isArray = p->encodingMask & (0x01 << 7); // Bit 7
  648. Boolean hasDimensions = p->encodingMask & (0x01 << 6); // Bit 6
  649. int i;
  650. if (p->vt == UA_NULL || p->encodingMask != p->vt->Id) {
  651. return UA_ERR_INCONSISTENT;
  652. }
  653. length += sizeof(Byte); //p->encodingMask
  654. if (isArray) { // array length is encoded
  655. length += sizeof(Int32); //p->arrayLength
  656. if (p->arrayLength > 0) {
  657. // TODO: add suggestions of @jfpr to not iterate over arrays with fixed len elements
  658. for (i=0;i<p->arrayLength;i++) {
  659. length += p->vt->calcSize(p->data[i]);
  660. }
  661. }
  662. } else { //single value to encode
  663. length += p->vt->calcSize(p->data[0]);
  664. }
  665. if (hasDimensions) {
  666. //ToDo: tobeInsert: length += the calcSize for dimensions
  667. }
  668. return length;
  669. }
  670. Int32 UA_Variant_encode(UA_Variant const *src, Int32* pos, char *dst) {
  671. Int32 retval = UA_SUCCESS;
  672. int i;
  673. if (src->vt == UA_NULL || src->encodingMask != src->vt->Id) {
  674. return UA_ERR_INCONSISTENT;
  675. }
  676. retval |= UA_Byte_encode(&(src->encodingMask),pos,dst);
  677. if (src->encodingMask & (0x01 << 7)) { // encode array length
  678. retval |= UA_Int32_encode(&(src->arrayLength),pos,dst);
  679. }
  680. if (src->arrayLength > 0) {
  681. //encode array as given by variant type
  682. for (i=0;i<src->arrayLength;i++) {
  683. retval |= src->vt->encode(src->data[i],pos,dst);
  684. }
  685. } else {
  686. retval |= src->vt->encode(src->data[i],pos,dst);
  687. }
  688. if (src->encodingMask & (1 << 6)) { // encode array dimension field
  689. // TODO: encode array dimension field
  690. }
  691. return retval;
  692. }
  693. Int32 UA_Variant_decode(char const * src, Int32 *pos, UA_Variant *dst) {
  694. Int32 retval = UA_SUCCESS;
  695. Int32 ns0Id;
  696. int i;
  697. retval |= UA_Byte_decode(src,pos,&(dst->encodingMask));
  698. ns0Id = dst->encodingMask & 0x1F;
  699. // initialize vTable
  700. if (ns0Id < UA_BOOLEAN && ns0Id > UA_DOUBLECOMPLEXNUMBERTYPE) {
  701. return UA_ERR_INVALID_VALUE;
  702. } else {
  703. dst->vt = &UA_namespace_zero[UA_namespace_zero_to_index(ns0Id)];
  704. }
  705. // get size of array
  706. if (dst->encodingMask & (0x01 << 7)) { // encode array length
  707. retval |= UA_Int32_decode(src,pos,&(dst->arrayLength));
  708. } else {
  709. dst->arrayLength = 1;
  710. }
  711. // allocate place for arrayLength pointers to any type
  712. retval |= UA_alloc(&(dst->data),dst->arrayLength * sizeof(void*));
  713. for (i=0;i<dst->arrayLength;i++) {
  714. // TODO: this is crazy, how to work with variants with variable size?
  715. // actually we have two different sizes - the storage size without
  716. // dynamic members and the storage size with the dynamic members, e.g.
  717. // for a string we here need to allocate definitely 8 byte (length=4, data*=4)
  718. // on a 32-bit architecture - so this code is definitely wrong
  719. retval |= UA_alloc(&(dst->data[i]),dst->vt->calcSize(UA_NULL));
  720. retval |= dst->vt->decode(src,pos,dst->data[i]);
  721. }
  722. if (dst->encodingMask & (1 << 6)) {
  723. // TODO: decode array dimension field
  724. }
  725. return retval;
  726. }
  727. //TODO: place this define at the server configuration
  728. #define MAX_PICO_SECONDS 1000
  729. Int32 UA_DataValue_decode(char const * src, Int32* pos, UA_DataValue* dst) {
  730. Int32 retval = UA_SUCCESS;
  731. retval |= UA_Byte_decode(src,pos,&(dst->encodingMask));
  732. if (dst->encodingMask & 0x01) {
  733. retval |= UA_Variant_decode(src,pos,&(dst->value));
  734. }
  735. if (dst->encodingMask & 0x02) {
  736. retval |= UA_StatusCode_decode(src,pos,&(dst->status));
  737. }
  738. if (dst->encodingMask & 0x04) {
  739. retval |= UA_DateTime_decode(src,pos,&(dst->sourceTimestamp));
  740. }
  741. if (dst->encodingMask & 0x08) {
  742. retval |= UA_DateTime_decode(src,pos,&(dst->serverTimestamp));
  743. }
  744. if (dst->encodingMask & 0x10) {
  745. retval |= UA_UInt16_decode(src,pos,&(dst->sourcePicoseconds));
  746. if (dst->sourcePicoseconds > MAX_PICO_SECONDS) {
  747. dst->sourcePicoseconds = MAX_PICO_SECONDS;
  748. }
  749. }
  750. if (dst->encodingMask & 0x20) {
  751. retval |= UA_UInt16_decode(src,pos,&(dst->serverPicoseconds));
  752. if (dst->serverPicoseconds > MAX_PICO_SECONDS) {
  753. dst->serverPicoseconds = MAX_PICO_SECONDS;
  754. }
  755. }
  756. return retval;
  757. }
  758. Int32 UA_DataValue_encode(UA_DataValue const * src, Int32* pos, char *dst) {
  759. Int32 retval = UA_SUCCESS;
  760. retval |= UA_Byte_encode(&(src->encodingMask),pos,dst);
  761. if (src->encodingMask & 0x01) {
  762. retval |= UA_Variant_encode(&(src->value),pos,dst);
  763. }
  764. if (src->encodingMask & 0x02) {
  765. retval |= UA_StatusCode_encode(&(src->status),pos,dst);
  766. }
  767. if (src->encodingMask & 0x04) {
  768. retval |= UA_DateTime_encode(&(src->sourceTimestamp),pos,dst);
  769. }
  770. if (src->encodingMask & 0x08) {
  771. retval |= UA_DateTime_encode(&(src->serverTimestamp),pos,dst);
  772. }
  773. if (src->encodingMask & 0x10) {
  774. retval |= UA_UInt16_encode(&(src->sourcePicoseconds),pos,dst);
  775. }
  776. if (src->encodingMask & 0x10) {
  777. retval |= UA_UInt16_encode(&(src->serverPicoseconds),pos,dst);
  778. }
  779. return retval;
  780. }
  781. Int32 UA_DataValue_calcSize(UA_DataValue const * p) {
  782. Int32 length = 0;
  783. if (p == UA_NULL) { // get static storage size
  784. length = sizeof(UA_DataValue);
  785. } else { // get decoding size
  786. length = sizeof(UA_Byte);
  787. if (p->encodingMask & 0x01) {
  788. length += UA_Variant_calcSize(&(p->value));
  789. }
  790. if (p->encodingMask & 0x02) {
  791. length += sizeof(UInt32); //dataValue->status
  792. }
  793. if (p->encodingMask & 0x04) {
  794. length += sizeof(Int64); //dataValue->sourceTimestamp
  795. }
  796. if (p->encodingMask & 0x08) {
  797. length += sizeof(Int64); //dataValue->serverTimestamp
  798. }
  799. if (p->encodingMask & 0x10) {
  800. length += sizeof(Int64); //dataValue->sourcePicoseconds
  801. }
  802. if (p->encodingMask & 0x20) {
  803. length += sizeof(Int64); //dataValue->serverPicoseconds
  804. }
  805. }
  806. return length;
  807. }
  808. /**
  809. * DiagnosticInfo
  810. * Part: 4
  811. * Chapter: 7.9
  812. * Page: 116
  813. */
  814. Int32 decodeDiagnosticInfo(char const * buf, Int32 *pos,
  815. UA_DiagnosticInfo *dstDiagnosticInfo) {
  816. Byte encodingByte = (buf[*pos]);
  817. Byte mask;
  818. for (mask = 1; mask <= 0x40; mask << 2) {
  819. switch (mask & encodingByte) {
  820. case DIEMT_SYMBOLIC_ID:
  821. decoder_decodeBuiltInDatatype(buf, INT32, pos,
  822. &(dstDiagnosticInfo->SymbolicId));
  823. //dstDiagnosticInfo->symbolicId = decodeInt32(buf, pos);
  824. break;
  825. case DIEMT_NAMESPACE:
  826. decoder_decodeBuiltInDatatype(buf, INT32, pos,
  827. &(dstDiagnosticInfo->NamespaceUri));
  828. //dstDiagnosticInfo->namespaceUri = decodeInt32(buf, pos);
  829. break;
  830. case DIEMT_LOCALIZED_TEXT:
  831. decoder_decodeBuiltInDatatype(buf, INT32, pos,
  832. &(dstDiagnosticInfo->LocalizedText));
  833. //dstDiagnosticInfo->localizesText = decodeInt32(buf, pos);
  834. break;
  835. case DIEMT_LOCALE:
  836. decoder_decodeBuiltInDatatype(buf, INT32, pos,
  837. &(dstDiagnosticInfo->Locale));
  838. //dstDiagnosticInfo->locale = decodeInt32(buf, pos);
  839. break;
  840. case DIEMT_ADDITIONAL_INFO:
  841. decoder_decodeBuiltInDatatype(buf, STRING, pos,
  842. &(dstDiagnosticInfo->AdditionalInfo));
  843. decodeUAString(buf, pos, &dstDiagnosticInfo->AdditionalInfo);
  844. break;
  845. case DIEMT_INNER_STATUS_CODE:
  846. decoder_decodeBuiltInDatatype(buf, STATUS_CODE, pos,
  847. &(dstDiagnosticInfo->InnerStatusCode));
  848. //dstDiagnosticInfo->innerStatusCode = decodeUAStatusCode(buf, pos);
  849. break;
  850. case DIEMT_INNER_DIAGNOSTIC_INFO:
  851. //TODO memory management should be checked (getting memory within a function)
  852. dstDiagnosticInfo->InnerDiagnosticInfo =
  853. (UA_DiagnosticInfo*) opcua_malloc(
  854. sizeof(UA_DiagnosticInfo));
  855. decoder_decodeBuiltInDatatype(buf, DIAGNOSTIC_INFO, pos,
  856. &(dstDiagnosticInfo->InnerDiagnosticInfo));
  857. break;
  858. }
  859. }
  860. *pos += 1;
  861. return 0;
  862. }
  863. Int32 encodeDiagnosticInfo(UA_DiagnosticInfo *diagnosticInfo, Int32 *pos,
  864. char *dstbuf) {
  865. Byte mask;
  866. int i;
  867. encoder_encodeBuiltInDatatype((void*) (&(diagnosticInfo->EncodingMask)),
  868. BYTE, pos, dstbuf);
  869. for (i = 0; i < 7; i++) {
  870. switch ( (0x01 << i) & diagnosticInfo->EncodingMask) {
  871. case DIEMT_SYMBOLIC_ID:
  872. // puts("diagnosticInfo symbolic id");
  873. encoder_encodeBuiltInDatatype((void*) &(diagnosticInfo->SymbolicId),
  874. INT32, pos, dstbuf);
  875. break;
  876. case DIEMT_NAMESPACE:
  877. encoder_encodeBuiltInDatatype(
  878. (void*) &(diagnosticInfo->NamespaceUri), INT32, pos,
  879. dstbuf);
  880. break;
  881. case DIEMT_LOCALIZED_TEXT:
  882. encoder_encodeBuiltInDatatype(
  883. (void*) &(diagnosticInfo->LocalizedText), INT32, pos,
  884. dstbuf);
  885. break;
  886. case DIEMT_LOCALE:
  887. encoder_encodeBuiltInDatatype((void*) &(diagnosticInfo->Locale),
  888. INT32, pos, dstbuf);
  889. break;
  890. case DIEMT_ADDITIONAL_INFO:
  891. encoder_encodeBuiltInDatatype(
  892. (void*) &(diagnosticInfo->AdditionalInfo), STRING, pos,
  893. dstbuf);
  894. break;
  895. case DIEMT_INNER_STATUS_CODE:
  896. encoder_encodeBuiltInDatatype(
  897. (void*) &(diagnosticInfo->InnerStatusCode), STATUS_CODE,
  898. pos, dstbuf);
  899. break;
  900. case DIEMT_INNER_DIAGNOSTIC_INFO:
  901. encoder_encodeBuiltInDatatype(
  902. (void*) &(diagnosticInfo->InnerDiagnosticInfo),
  903. DIAGNOSTIC_INFO, pos, dstbuf);
  904. break;
  905. }
  906. }
  907. return UA_NO_ERROR;
  908. }
  909. Int32 diagnosticInfo_calcSize(UA_DiagnosticInfo *diagnosticInfo) {
  910. Int32 length = 0;
  911. Byte mask;
  912. length += sizeof(Byte); // EncodingMask
  913. for (mask = 0x01; mask <= 0x40; mask *= 2) {
  914. switch (mask & (diagnosticInfo->EncodingMask)) {
  915. case DIEMT_SYMBOLIC_ID:
  916. // puts("diagnosticInfo symbolic id");
  917. length += sizeof(Int32);
  918. break;
  919. case DIEMT_NAMESPACE:
  920. length += sizeof(Int32);
  921. break;
  922. case DIEMT_LOCALIZED_TEXT:
  923. length += sizeof(Int32);
  924. break;
  925. case DIEMT_LOCALE:
  926. length += sizeof(Int32);
  927. break;
  928. case DIEMT_ADDITIONAL_INFO:
  929. length += UAString_calcSize(&(diagnosticInfo->AdditionalInfo));
  930. break;
  931. case DIEMT_INNER_STATUS_CODE:
  932. length += sizeof(UA_StatusCode);
  933. break;
  934. case DIEMT_INNER_DIAGNOSTIC_INFO:
  935. length += diagnosticInfo_calcSize(
  936. diagnosticInfo->InnerDiagnosticInfo);
  937. break;
  938. }
  939. }
  940. return length;
  941. }
  942. /**
  943. * RequestHeader
  944. * Part: 4
  945. * Chapter: 7.26
  946. * Page: 132
  947. */
  948. /** \copydoc decodeRequestHeader */
  949. Int32 decodeRequestHeader(const AD_RawMessage *srcRaw, Int32 *pos,
  950. UA_AD_RequestHeader *dstRequestHeader) {
  951. return decoder_decodeRequestHeader(srcRaw->message, pos, dstRequestHeader);
  952. }
  953. Int32 decoder_decodeRequestHeader(char const * message, Int32 *pos,
  954. UA_AD_RequestHeader *dstRequestHeader) {
  955. // 62541-4 §5.5.2.2 OpenSecureChannelServiceParameters
  956. // requestHeader - common request parameters. The authenticationToken is always omitted
  957. decoder_decodeBuiltInDatatype(message, NODE_ID, pos,
  958. &(dstRequestHeader->authenticationToken));
  959. decoder_decodeBuiltInDatatype(message, DATE_TIME, pos,
  960. &(dstRequestHeader->timestamp));
  961. decoder_decodeBuiltInDatatype(message, UINT32, pos,
  962. &(dstRequestHeader->requestHandle));
  963. decoder_decodeBuiltInDatatype(message, UINT32, pos,
  964. &(dstRequestHeader->returnDiagnostics));
  965. decoder_decodeBuiltInDatatype(message, STRING, pos,
  966. &(dstRequestHeader->auditEntryId));
  967. decoder_decodeBuiltInDatatype(message, UINT32, pos,
  968. &(dstRequestHeader->timeoutHint));
  969. decoder_decodeBuiltInDatatype(message, EXTENSION_OBJECT, pos,
  970. &(dstRequestHeader->additionalHeader));
  971. // AdditionalHeader will stay empty, need to be changed if there is relevant information
  972. return 0;
  973. }
  974. /**
  975. * ResponseHeader
  976. * Part: 4
  977. * Chapter: 7.27
  978. * Page: 133
  979. */
  980. /** \copydoc encodeResponseHeader */
  981. Int32 encodeResponseHeader(UA_AD_ResponseHeader const * responseHeader,
  982. Int32 *pos, UA_ByteString *dstBuf) {
  983. encodeUADateTime(responseHeader->timestamp, pos, dstBuf->Data);
  984. encodeIntegerId(responseHeader->requestHandle, pos, dstBuf->Data);
  985. encodeUInt32(responseHeader->serviceResult, pos, dstBuf->Data);
  986. encodeDiagnosticInfo(responseHeader->serviceDiagnostics, pos, dstBuf->Data);
  987. encoder_encodeBuiltInDatatypeArray(responseHeader->stringTable,
  988. responseHeader->noOfStringTable, STRING_ARRAY, pos, dstBuf->Data);
  989. encodeExtensionObject(responseHeader->additionalHeader, pos, dstBuf->Data);
  990. //Kodieren von String Datentypen
  991. return 0;
  992. }
  993. Int32 extensionObject_calcSize(UA_ExtensionObject *extensionObject) {
  994. Int32 length = 0;
  995. length += nodeId_calcSize(&(extensionObject->TypeId));
  996. length += sizeof(Byte); //The EncodingMask Byte
  997. if (extensionObject->Encoding == BODY_IS_BYTE_STRING
  998. || extensionObject->Encoding == BODY_IS_XML_ELEMENT) {
  999. length += UAByteString_calcSize(&(extensionObject->Body));
  1000. }
  1001. return length;
  1002. }
  1003. Int32 responseHeader_calcSize(UA_AD_ResponseHeader *responseHeader) {
  1004. Int32 i;
  1005. Int32 length = 0;
  1006. // UtcTime timestamp 8
  1007. length += sizeof(UA_DateTime);
  1008. // IntegerId requestHandle 4
  1009. length += sizeof(UA_AD_IntegerId);
  1010. // StatusCode serviceResult 4
  1011. length += sizeof(UA_StatusCode);
  1012. // DiagnosticInfo serviceDiagnostics
  1013. length += diagnosticInfo_calcSize(responseHeader->serviceDiagnostics);
  1014. // String stringTable[], see 62541-6 § 5.2.4
  1015. length += sizeof(Int32); // Length of Stringtable always
  1016. if (responseHeader->noOfStringTable > 0) {
  1017. for (i = 0; i < responseHeader->noOfStringTable; i++) {
  1018. length += UAString_calcSize(responseHeader->stringTable[i]);
  1019. }
  1020. }
  1021. // ExtensibleObject additionalHeader
  1022. length += extensionObject_calcSize(responseHeader->additionalHeader);
  1023. return length;
  1024. }