opcua_basictypes.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. Int32 UA_ExtensionObject_delete(UA_ExtensionObject *p) {
  592. Int32 retval = UA_SUCCESS;
  593. retval |= UA_ExtensionObject_deleteMembers(p);
  594. retval |= UA_memfree(p);
  595. return retval;
  596. }
  597. // TODO: UA_ExtensionObject_delete
  598. Int32 UA_ExtensionObject_deleteMembers(UA_ExtensionObject *p) {
  599. Int32 retval = UA_SUCCESS;
  600. retval |= UA_NodeId_deleteMembers(&(p->typeId));
  601. retval |= UA_String_deleteMembers(&(p->body));
  602. return retval;
  603. }
  604. // TODO: UA_DataValue_encode
  605. // TODO: UA_DataValue_decode
  606. // TODO: UA_DataValue_delete
  607. // TODO: UA_DataValue_deleteMembers
  608. // TODO: UA_DiagnosticInfo_encode [Sten: done]
  609. // TODO: UA_DiagnosticInfo_decode [Sten: done]
  610. // TODO: UA_DiagnosticInfo_delete
  611. // TODO: UA_DiagnosticInfo_deleteMembers
  612. /**
  613. * DiagnosticInfo
  614. * Part: 4
  615. * Chapter: 7.9
  616. * Page: 116
  617. */
  618. Int32 UA_DiagnosticInfo_decode(char const * src, Int32 *pos, UA_DiagnosticInfo *dst) {
  619. Int32 retval = UA_SUCCESS;
  620. //FIXME SURE?
  621. //pos seems not to be incremented
  622. Byte encodingByte = (src[*pos]);
  623. /*
  624. * retval |= UA_Byte_decode(src, pos, encodingByte);
  625. */
  626. Byte mask;
  627. for (mask = 1; mask <= 0x40; mask << 1) {
  628. switch (mask & encodingByte) {
  629. case DIEMT_SYMBOLIC_ID:
  630. /* decoder_decodeBuiltInDatatype(buf, INT32, pos,
  631. &(dst->SymbolicId)); */
  632. retval |= UA_Int32_decode(src, pos, &(dst->symbolicId));
  633. break;
  634. case DIEMT_NAMESPACE:
  635. /* decoder_decodeBuiltInDatatype(buf, INT32, pos,
  636. &(dst->NamespaceUri)); */
  637. retval |= UA_Int32_decode(src, pos, &(dst->namespaceUri));
  638. break;
  639. case DIEMT_LOCALIZED_TEXT:
  640. /* decoder_decodeBuiltInDatatype(buf, INT32, pos,
  641. &(dst->LocalizedText)); */
  642. retval |= UA_Int32_decode(src, pos, &(dst->localizedText));
  643. break;
  644. case DIEMT_LOCALE:
  645. /* decoder_decodeBuiltInDatatype(src, INT32, pos,
  646. &(dst->Locale)); */
  647. retval |= UA_Int32_decode(src, pos, &(dst->locale));
  648. break;
  649. case DIEMT_ADDITIONAL_INFO:
  650. /* decoder_decodeBuiltInDatatype(buf, STRING, pos,
  651. &(dst->AdditionalInfo)); */
  652. retval |= UA_String_decode(src, pos, &(dst->additionalInfo));
  653. break;
  654. case DIEMT_INNER_STATUS_CODE:
  655. /* decoder_decodeBuiltInDatatype(buf, STATUS_CODE, pos,
  656. &(dstDiagnosticInfo->InnerStatusCode)); */
  657. retval |= UA_StatusCode_decode(src, pos, &(dst->innerStatusCode));
  658. break;
  659. case DIEMT_INNER_DIAGNOSTIC_INFO:
  660. //TODO memory management should be checked (getting memory within a function)
  661. //TODO: Sten: not sure
  662. /*
  663. dstDiagnosticInfo->innerDiagnosticInfo =
  664. (UA_DiagnosticInfo*) opcua_malloc(
  665. sizeof(UA_DiagnosticInfo));
  666. decoder_decodeBuiltInDatatype(src, DIAGNOSTIC_INFO, pos,
  667. &(dstDiagnosticInfo->innerDiagnosticInfo));
  668. */
  669. retval |= UA_DiagnosticInfo_decode(src, pos, dst->innerDiagnosticInfo);
  670. break;
  671. }
  672. }
  673. //FIXME: sure?
  674. *pos += 1;
  675. return retval;
  676. }
  677. Int32 UA_DiagnosticInfo_encode(UA_DiagnosticInfo const *src, Int32 *pos, char *dst) {
  678. Int32 retval = UA_SUCCESS;
  679. Byte mask;
  680. int i;
  681. UA_ByteString_encode(&(src->encodingMask), pos, dst);
  682. /*encoder_encodeBuiltInDatatype((void*) (&(diagnosticInfo->encodingMask)),
  683. BYTE, pos, dst);*/
  684. for (i = 0; i < 7; i++) {
  685. switch ( (0x01 << i) & src->encodingMask) {
  686. case DIEMT_SYMBOLIC_ID:
  687. // puts("diagnosticInfo symbolic id");
  688. retval |= UA_Int32_encode(&(src->symbolicId), pos, dst);
  689. /*encoder_encodeBuiltInDatatype((void*) &(diagnosticInfo->symbolicId),
  690. INT32, pos, dst);*/
  691. break;
  692. case DIEMT_NAMESPACE:
  693. /*encoder_encodeBuiltInDatatype(
  694. (void*) &(diagnosticInfo->namespaceUri), INT32, pos,
  695. dst);*/
  696. retval |= UA_Int32_encode( &(src->namespaceUri), pos, dst);
  697. break;
  698. case DIEMT_LOCALIZED_TEXT:
  699. /*encoder_encodeBuiltInDatatype(
  700. (void*) &(diagnosticInfo->localizedText), INT32, pos,
  701. dst);*/
  702. retval |= UA_Int32_encode(&(src->localizedText), pos, dst);
  703. break;
  704. case DIEMT_LOCALE:
  705. /*encoder_encodeBuiltInDatatype((void*) &(diagnosticInfo->locale),
  706. INT32, pos, dst);*/
  707. retval |= UA_Int32_encode(&(src->locale), pos, dst);
  708. break;
  709. case DIEMT_ADDITIONAL_INFO:
  710. /*encoder_encodeBuiltInDatatype(
  711. (void*) &(diagnosticInfo->additionalInfo), STRING, pos,
  712. dst);*/
  713. retval |= UA_String_encode(&(src->additionalInfo), pos, dst);
  714. break;
  715. case DIEMT_INNER_STATUS_CODE:
  716. /*encoder_encodeBuiltInDatatype(
  717. (void*) &(diagnosticInfo->innerStatusCode), STATUS_CODE,
  718. pos, dst);*/
  719. retval |= UA_StatusCode_encode(&(src->innerStatusCode), pos, dst);
  720. break;
  721. case DIEMT_INNER_DIAGNOSTIC_INFO:
  722. /*encoder_encodeBuiltInDatatype(
  723. (void*) &(diagnosticInfo->innerDiagnosticInfo),
  724. DIAGNOSTIC_INFO, pos, dst);*/
  725. retval |= UA_DiagnosticInfo_encode(src->innerDiagnosticInfo, pos, dst);
  726. break;
  727. }
  728. }
  729. return retval;
  730. }
  731. Int32 UA_DiagnosticInfo_calcSize(UA_DiagnosticInfo const * ptr) {
  732. Int32 length = 0;
  733. Byte mask;
  734. length += sizeof(Byte); // EncodingMask
  735. for (mask = 0x01; mask <= 0x40; mask *= 2) {
  736. switch (mask & (ptr->encodingMask)) {
  737. case DIEMT_SYMBOLIC_ID:
  738. // puts("diagnosticInfo symbolic id");
  739. length += sizeof(Int32);
  740. break;
  741. case DIEMT_NAMESPACE:
  742. length += sizeof(Int32);
  743. break;
  744. case DIEMT_LOCALIZED_TEXT:
  745. length += sizeof(Int32);
  746. break;
  747. case DIEMT_LOCALE:
  748. length += sizeof(Int32);
  749. break;
  750. case DIEMT_ADDITIONAL_INFO:
  751. length += UA_String_calcSize(&(ptr->additionalInfo));
  752. break;
  753. case DIEMT_INNER_STATUS_CODE:
  754. length += sizeof(UA_StatusCode);
  755. break;
  756. case DIEMT_INNER_DIAGNOSTIC_INFO:
  757. length += UA_DiagnosticInfo_calcSize(ptr->innerDiagnosticInfo);
  758. break;
  759. }
  760. }
  761. return length;
  762. }
  763. UA_TYPE_METHOD_CALCSIZE_SIZEOF(UA_DateTime)
  764. UA_TYPE_METHOD_ENCODE_AS(UA_DateTime,UA_Int64)
  765. UA_TYPE_METHOD_DECODE_AS(UA_DateTime,UA_Int64)
  766. UA_TYPE_METHOD_DELETE_MEMFREE(UA_DateTime)
  767. UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_DateTime)
  768. UA_TYPE_METHOD_CALCSIZE_AS(UA_XmlElement, UA_ByteString)
  769. UA_TYPE_METHOD_ENCODE_AS(UA_XmlElement, UA_ByteString)
  770. UA_TYPE_METHOD_DECODE_AS(UA_XmlElement, UA_ByteString)
  771. UA_TYPE_METHOD_DELETE_AS(UA_XmlElement, UA_ByteString)
  772. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_XmlElement, UA_ByteString)
  773. /** IntegerId - Part: 4, Chapter: 7.13, Page: 118 */
  774. UA_TYPE_METHOD_CALCSIZE_AS(UA_IntegerId, UA_Int32)
  775. UA_TYPE_METHOD_ENCODE_AS(UA_IntegerId, UA_Int32)
  776. UA_TYPE_METHOD_DECODE_AS(UA_IntegerId, UA_Int32)
  777. UA_TYPE_METHOD_DELETE_AS(UA_IntegerId, UA_Int32)
  778. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_IntegerId, UA_Int32)
  779. UA_TYPE_METHOD_CALCSIZE_AS(UA_StatusCode, UA_UInt32)
  780. UA_TYPE_METHOD_ENCODE_AS(UA_StatusCode, UA_UInt32)
  781. UA_TYPE_METHOD_DECODE_AS(UA_StatusCode, UA_UInt32)
  782. UA_TYPE_METHOD_DELETE_AS(UA_StatusCode, UA_UInt32)
  783. UA_TYPE_METHOD_DELETEMEMBERS_AS(UA_StatusCode, UA_UInt32)
  784. Int32 UA_QualifiedName_calcSize(UA_QualifiedName const * p) {
  785. Int32 length = 0;
  786. length += sizeof(UInt16); //qualifiedName->namespaceIndex
  787. length += sizeof(UInt16); //qualifiedName->reserved
  788. length += UA_String_calcSize(&(p->name)); //qualifiedName->name
  789. return length;
  790. }
  791. Int32 UA_QualifiedName_decode(char const * src, Int32 *pos,
  792. UA_QualifiedName *dst) {
  793. Int32 retval = UA_SUCCESS;
  794. retval |= UA_UInt16_decode(src,pos,&(dst->namespaceIndex));
  795. retval |= UA_UInt16_decode(src,pos,&(dst->reserved));
  796. retval |= UA_String_decode(src,pos,&(dst->name));
  797. return retval;
  798. }
  799. Int32 UA_QualifiedName_encode(UA_QualifiedName const *src, Int32* pos,
  800. char *dst) {
  801. Int32 retval = UA_SUCCESS;
  802. retval |= UA_UInt16_encode(&(src->namespaceIndex),pos,dst);
  803. retval |= UA_UInt16_encode(&(src->reserved),pos,dst);
  804. retval |= UA_String_encode(&(src->name),pos,dst);
  805. return retval;
  806. }
  807. Int32 UA_Variant_calcSize(UA_Variant const * p) {
  808. Int32 length = 0;
  809. Int32 ns0Id = p->encodingMask & 0x1F; // Bits 1-5
  810. Boolean isArray = p->encodingMask & (0x01 << 7); // Bit 7
  811. Boolean hasDimensions = p->encodingMask & (0x01 << 6); // Bit 6
  812. int i;
  813. if (p->vt == UA_NULL || p->encodingMask != p->vt->Id) {
  814. return UA_ERR_INCONSISTENT;
  815. }
  816. length += sizeof(Byte); //p->encodingMask
  817. if (isArray) { // array length is encoded
  818. length += sizeof(Int32); //p->arrayLength
  819. if (p->arrayLength > 0) {
  820. // TODO: add suggestions of @jfpr to not iterate over arrays with fixed len elements
  821. for (i=0;i<p->arrayLength;i++) {
  822. length += p->vt->calcSize(p->data[i]);
  823. }
  824. }
  825. } else { //single value to encode
  826. length += p->vt->calcSize(p->data[0]);
  827. }
  828. if (hasDimensions) {
  829. //ToDo: tobeInsert: length += the calcSize for dimensions
  830. }
  831. return length;
  832. }
  833. Int32 UA_Variant_encode(UA_Variant const *src, Int32* pos, char *dst) {
  834. Int32 retval = UA_SUCCESS;
  835. int i;
  836. if (src->vt == UA_NULL || src->encodingMask != src->vt->Id) {
  837. return UA_ERR_INCONSISTENT;
  838. }
  839. retval |= UA_Byte_encode(&(src->encodingMask),pos,dst);
  840. if (src->encodingMask & (0x01 << 7)) { // encode array length
  841. retval |= UA_Int32_encode(&(src->arrayLength),pos,dst);
  842. }
  843. if (src->arrayLength > 0) {
  844. //encode array as given by variant type
  845. for (i=0;i<src->arrayLength;i++) {
  846. retval |= src->vt->encode(src->data[i],pos,dst);
  847. }
  848. } else {
  849. retval |= src->vt->encode(src->data[i],pos,dst);
  850. }
  851. if (src->encodingMask & (1 << 6)) { // encode array dimension field
  852. // TODO: encode array dimension field
  853. }
  854. return retval;
  855. }
  856. //FIXME:
  857. Int32 UA_Variant_decode(char const * src, Int32 *pos, UA_Variant *dst) {
  858. return UA_SUCCESS;
  859. //FIXME:
  860. Int32 retval = UA_SUCCESS;
  861. Int32 ns0Id;
  862. int i;
  863. retval |= UA_Byte_decode(src,pos,&(dst->encodingMask));
  864. ns0Id = dst->encodingMask & 0x1F;
  865. // initialize vTable
  866. if (ns0Id < UA_BOOLEAN && ns0Id > UA_DOUBLECOMPLEXNUMBERTYPE) {
  867. return UA_ERR_INVALID_VALUE;
  868. } else {
  869. dst->vt = &UA_namespace_zero[UA_namespace_zero_to_index(ns0Id)];
  870. }
  871. // get size of array
  872. if (dst->encodingMask & (0x01 << 7)) { // encode array length
  873. retval |= UA_Int32_decode(src,pos,&(dst->arrayLength));
  874. } else {
  875. dst->arrayLength = 1;
  876. }
  877. // allocate place for arrayLength pointers to any type
  878. retval |= UA_alloc(&(dst->data),dst->arrayLength * sizeof(void*));
  879. for (i=0;i<dst->arrayLength;i++) {
  880. // TODO: this is crazy, how to work with variants with variable size?
  881. // actually we have two different sizes - the storage size without
  882. // dynamic members and the storage size with the dynamic members, e.g.
  883. // for a string we here need to allocate definitely 8 byte (length=4, data*=4)
  884. // on a 32-bit architecture - so this code is definitely wrong
  885. retval |= UA_alloc(&(dst->data[i]),dst->vt->calcSize(UA_NULL));
  886. retval |= dst->vt->decode(src,pos,dst->data[i]);
  887. }
  888. if (dst->encodingMask & (1 << 6)) {
  889. // TODO: decode array dimension field
  890. }
  891. return retval;
  892. }
  893. //TODO: place this define at the server configuration
  894. #define MAX_PICO_SECONDS 1000
  895. Int32 UA_DataValue_decode(char const * src, Int32* pos, UA_DataValue* dst) {
  896. Int32 retval = UA_SUCCESS;
  897. retval |= UA_Byte_decode(src,pos,&(dst->encodingMask));
  898. if (dst->encodingMask & 0x01) {
  899. retval |= UA_Variant_decode(src,pos,&(dst->value));
  900. }
  901. if (dst->encodingMask & 0x02) {
  902. retval |= UA_StatusCode_decode(src,pos,&(dst->status));
  903. }
  904. if (dst->encodingMask & 0x04) {
  905. retval |= UA_DateTime_decode(src,pos,&(dst->sourceTimestamp));
  906. }
  907. if (dst->encodingMask & 0x08) {
  908. retval |= UA_DateTime_decode(src,pos,&(dst->serverTimestamp));
  909. }
  910. if (dst->encodingMask & 0x10) {
  911. retval |= UA_UInt16_decode(src,pos,&(dst->sourcePicoseconds));
  912. if (dst->sourcePicoseconds > MAX_PICO_SECONDS) {
  913. dst->sourcePicoseconds = MAX_PICO_SECONDS;
  914. }
  915. }
  916. if (dst->encodingMask & 0x20) {
  917. retval |= UA_UInt16_decode(src,pos,&(dst->serverPicoseconds));
  918. if (dst->serverPicoseconds > MAX_PICO_SECONDS) {
  919. dst->serverPicoseconds = MAX_PICO_SECONDS;
  920. }
  921. }
  922. return retval;
  923. }
  924. Int32 UA_DataValue_encode(UA_DataValue const * src, Int32* pos, char *dst) {
  925. Int32 retval = UA_SUCCESS;
  926. retval |= UA_Byte_encode(&(src->encodingMask),pos,dst);
  927. if (src->encodingMask & 0x01) {
  928. retval |= UA_Variant_encode(&(src->value),pos,dst);
  929. }
  930. if (src->encodingMask & 0x02) {
  931. retval |= UA_StatusCode_encode(&(src->status),pos,dst);
  932. }
  933. if (src->encodingMask & 0x04) {
  934. retval |= UA_DateTime_encode(&(src->sourceTimestamp),pos,dst);
  935. }
  936. if (src->encodingMask & 0x08) {
  937. retval |= UA_DateTime_encode(&(src->serverTimestamp),pos,dst);
  938. }
  939. if (src->encodingMask & 0x10) {
  940. retval |= UA_UInt16_encode(&(src->sourcePicoseconds),pos,dst);
  941. }
  942. if (src->encodingMask & 0x10) {
  943. retval |= UA_UInt16_encode(&(src->serverPicoseconds),pos,dst);
  944. }
  945. return retval;
  946. }
  947. Int32 UA_DataValue_calcSize(UA_DataValue const * p) {
  948. Int32 length = 0;
  949. if (p == UA_NULL) { // get static storage size
  950. length = sizeof(UA_DataValue);
  951. } else { // get decoding size
  952. length = sizeof(UA_Byte);
  953. if (p->encodingMask & 0x01) {
  954. length += UA_Variant_calcSize(&(p->value));
  955. }
  956. if (p->encodingMask & 0x02) {
  957. length += sizeof(UInt32); //dataValue->status
  958. }
  959. if (p->encodingMask & 0x04) {
  960. length += sizeof(Int64); //dataValue->sourceTimestamp
  961. }
  962. if (p->encodingMask & 0x08) {
  963. length += sizeof(Int64); //dataValue->serverTimestamp
  964. }
  965. if (p->encodingMask & 0x10) {
  966. length += sizeof(Int64); //dataValue->sourcePicoseconds
  967. }
  968. if (p->encodingMask & 0x20) {
  969. length += sizeof(Int64); //dataValue->serverPicoseconds
  970. }
  971. }
  972. return length;
  973. }
  974. /**
  975. * RequestHeader
  976. * Part: 4
  977. * Chapter: 7.26
  978. * Page: 132
  979. */
  980. /** \copydoc decodeRequestHeader */
  981. /*** Sten: removed to compile
  982. Int32 decodeRequestHeader(const AD_RawMessage *srcRaw, Int32 *pos,
  983. UA_AD_RequestHeader *dstRequestHeader) {
  984. return decoder_decodeRequestHeader(srcRaw->message, pos, dstRequestHeader);
  985. }
  986. ***/
  987. /*** Sten: removed to compile
  988. Int32 decoder_decodeRequestHeader(char const * message, Int32 *pos,
  989. UA_AD_RequestHeader *dstRequestHeader) {
  990. // 62541-4 §5.5.2.2 OpenSecureChannelServiceParameters
  991. // requestHeader - common request parameters. The authenticationToken is always omitted
  992. decoder_decodeBuiltInDatatype(message, NODE_ID, pos,
  993. &(dstRequestHeader->authenticationToken));
  994. decoder_decodeBuiltInDatatype(message, DATE_TIME, pos,
  995. &(dstRequestHeader->timestamp));
  996. decoder_decodeBuiltInDatatype(message, UINT32, pos,
  997. &(dstRequestHeader->requestHandle));
  998. decoder_decodeBuiltInDatatype(message, UINT32, pos,
  999. &(dstRequestHeader->returnDiagnostics));
  1000. decoder_decodeBuiltInDatatype(message, STRING, pos,
  1001. &(dstRequestHeader->auditEntryId));
  1002. decoder_decodeBuiltInDatatype(message, UINT32, pos,
  1003. &(dstRequestHeader->timeoutHint));
  1004. decoder_decodeBuiltInDatatype(message, EXTENSION_OBJECT, pos,
  1005. &(dstRequestHeader->additionalHeader));
  1006. // AdditionalHeader will stay empty, need to be changed if there is relevant information
  1007. return 0;
  1008. }
  1009. ***/
  1010. /**
  1011. * ResponseHeader
  1012. * Part: 4
  1013. * Chapter: 7.27
  1014. * Page: 133
  1015. */
  1016. /** \copydoc encodeResponseHeader */
  1017. /*** Sten: removed to compile
  1018. Int32 encodeResponseHeader(UA_AD_ResponseHeader const * responseHeader,
  1019. Int32 *pos, UA_ByteString *dstBuf) {
  1020. encodeUADateTime(responseHeader->timestamp, pos, dstBuf->data);
  1021. encodeIntegerId(responseHeader->requestHandle, pos, dstBuf->data);
  1022. encodeUInt32(responseHeader->serviceResult, pos, dstBuf->data);
  1023. encodeDiagnosticInfo(responseHeader->serviceDiagnostics, pos, dstBuf->data);
  1024. encoder_encodeBuiltInDatatypeArray(responseHeader->stringTable,
  1025. responseHeader->noOfStringTable, STRING_ARRAY, pos, dstBuf->data);
  1026. encodeExtensionObject(responseHeader->additionalHeader, pos, dstBuf->data);
  1027. //Kodieren von String Datentypen
  1028. return 0;
  1029. }
  1030. ***/
  1031. /*** Sten: removed to compile
  1032. Int32 extensionObject_calcSize(UA_ExtensionObject *extensionObject) {
  1033. Int32 length = 0;
  1034. length += nodeId_calcSize(&(extensionObject->typeId));
  1035. length += sizeof(Byte); //The EncodingMask Byte
  1036. if (extensionObject->encoding == BODY_IS_BYTE_STRING
  1037. || extensionObject->encoding == BODY_IS_XML_ELEMENT) {
  1038. length += UAByteString_calcSize(&(extensionObject->body));
  1039. }
  1040. return length;
  1041. }
  1042. ***/
  1043. /*** Sten: removed to compile
  1044. Int32 responseHeader_calcSize(UA_AD_ResponseHeader *responseHeader) {
  1045. Int32 i;
  1046. Int32 length = 0;
  1047. // UtcTime timestamp 8
  1048. length += sizeof(UA_DateTime);
  1049. // IntegerId requestHandle 4
  1050. length += sizeof(UA_AD_IntegerId);
  1051. // StatusCode serviceResult 4
  1052. length += sizeof(UA_StatusCode);
  1053. // DiagnosticInfo serviceDiagnostics
  1054. length += diagnosticInfo_calcSize(responseHeader->serviceDiagnostics);
  1055. // String stringTable[], see 62541-6 § 5.2.4
  1056. length += sizeof(Int32); // Length of Stringtable always
  1057. if (responseHeader->noOfStringTable > 0) {
  1058. for (i = 0; i < responseHeader->noOfStringTable; i++) {
  1059. length += UAString_calcSize(responseHeader->stringTable[i]);
  1060. }
  1061. }
  1062. // ExtensibleObject additionalHeader
  1063. length += extensionObject_calcSize(responseHeader->additionalHeader);
  1064. return length;
  1065. }
  1066. ***/