opcua_basictypes.c 35 KB

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