opcua_basictypes.c 35 KB

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