opcua_basictypes.c 34 KB

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