opcua_binaryEncDec.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * opcua_binaryEncDec.c
  3. *
  4. * Created on: Dec 18, 2013
  5. * Author: opcua
  6. */
  7. #include "opcua_binaryEncDec.h"
  8. #include "opcua_types.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. Byte decodeByte(const char *buf, Int32 *pos)
  12. {
  13. *pos = (*pos) + 1;
  14. return (Byte) buf[(*pos) - 1];
  15. }
  16. void encodeByte(Byte encodeByte, Int32 *pos, AD_RawMessage *dstBuf)
  17. {
  18. dstBuf->message[*pos] = encodeByte;
  19. *pos = (*pos) + 1;
  20. }
  21. UInt16 decodeUInt16(const char* buf, Int32 *pos)
  22. {
  23. Byte t1 = buf[*pos];
  24. UInt16 t2 = (UInt16) (buf[*pos + 1] << 8);
  25. *pos += 2;
  26. return t1 + t2;
  27. }
  28. void encodeUInt16(UInt16 value, Int32 *pos, AD_RawMessage *dstBuf)
  29. {
  30. memcpy(dstBuf->message, &value, sizeof(UInt16));
  31. *pos = (*pos) + sizeof(UInt16);
  32. }
  33. Int16 decodeInt16(const char* buf, Int32 *pos)
  34. {
  35. Byte t1 = buf[*pos];
  36. Int32 t2 = (Int16) (buf[*pos + 1] << 8);
  37. *pos += 2;
  38. return t1 + t2;
  39. }
  40. void encodeInt16(Int16 value, Int32 *pos, AD_RawMessage *dstBuf)
  41. {
  42. memcpy(dstBuf->message, &value, sizeof(Int16));
  43. *pos = (*pos) + sizeof(Int16);
  44. }
  45. Int32 decodeInt32(const char* buf, Int32 *pos)
  46. {
  47. SByte t1 = buf[*pos];
  48. Int32 t2 = (UInt32) (buf[*pos + 1] << 8);
  49. Int32 t3 = (UInt32) (buf[*pos + 2] << 16);
  50. Int32 t4 = (UInt32) (buf[*pos + 3] << 24);
  51. *pos += 4;
  52. return t1 + t2 + t3 + t4;
  53. }
  54. void encodeInt32(Int32 value, Int32 *pos, AD_RawMessage *dstBuf)
  55. {
  56. memcpy(dstBuf->message, &value, sizeof(Int32));
  57. *pos = (*pos) + sizeof(Int32);
  58. }
  59. UInt32 decodeUInt32(const char* buf, Int32 *pos)
  60. {
  61. Byte t1 = buf[*pos];
  62. UInt32 t2 = (UInt32) (buf[*pos + 1] << 8);
  63. UInt32 t3 = (UInt32) (buf[*pos + 2] << 16);
  64. UInt32 t4 = (UInt32) (buf[*pos + 3] << 24);
  65. *pos += 4;
  66. return t1 + t2 + t3 + t4;
  67. }
  68. void encodeUInt32(UInt32 value, char *dstBuf, Int32 *pos)
  69. {
  70. memcpy(&(dstBuf[*pos]), &value, sizeof(value));
  71. pos += 4;
  72. }
  73. Int64 decodeInt64(const char* buf, Int32 *pos)
  74. {
  75. SByte t1 = buf[*pos];
  76. UInt64 t2 = (UInt64) (buf[*pos + 1] << 8);
  77. UInt64 t3 = (UInt64) (buf[*pos + 2] << 16);
  78. UInt64 t4 = (UInt64) (buf[*pos + 3] << 24);
  79. UInt64 t5 = (UInt64) (buf[*pos + 4] << 32);
  80. UInt64 t6 = (UInt64) (buf[*pos + 5] << 40);
  81. UInt64 t7 = (UInt64) (buf[*pos + 6] << 48);
  82. UInt64 t8 = (UInt64) (buf[*pos + 7] << 56);
  83. pos += 8;
  84. return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  85. }
  86. void encodeInt64(Int64 value, Int64 *pos, AD_RawMessage *dstBuf)
  87. {
  88. memcpy(dstBuf->message, &value, sizeof(Int64));
  89. *pos = (*pos) + sizeof(Int64);
  90. }
  91. Int32 decodeUAString(const char* buf, Int32 *pos, UA_String *dstUAString)
  92. {
  93. dstUAString->Length = decodeInt32(buf, pos);
  94. if (dstUAString->Length > 0)
  95. {
  96. dstUAString->Data = &(buf[*pos]);
  97. }
  98. else
  99. {
  100. dstUAString->Length = 0;
  101. dstUAString->Data = NULL;
  102. }
  103. *pos += dstUAString->Length;
  104. return 0;
  105. }
  106. Int32 decodeUAGuid(const char *buf, Int32 *pos, UA_Guid *dstGUID)
  107. {
  108. dstGUID->Data1 = decodeUInt32(buf, pos);
  109. dstGUID->Data2 = decodeUInt16(buf, pos);
  110. dstGUID->Data3 = decodeUInt16(buf, pos);
  111. decodeUAByteString(buf, pos, &(dstGUID->Data4));
  112. return 0;
  113. }
  114. void decodeUAByteString(const char *buf, Int32* pos,
  115. UA_ByteString *dstBytestring)
  116. {
  117. decodeUAString(buf, pos, (UA_String*)dstBytestring);
  118. }
  119. UA_DateTime decodeUADateTime(const char *buf, Int32 *pos)
  120. {
  121. return decodeInt64(buf, pos);
  122. }
  123. UA_StatusCode decodeUAStatusCode(const char* buf, Int32 *pos)
  124. {
  125. return decodeUInt32(buf, pos);
  126. }
  127. Int32 decodeUANodeId(const char* buf, Int32 *pos, UA_NodeId *dstNodeId)
  128. {
  129. dstNodeId->EncodingByte = decodeInt32(buf, pos);
  130. switch (dstNodeId->EncodingByte)
  131. {
  132. case NIEVT_TWO_BYTE:
  133. {
  134. dstNodeId->Identifier.Numeric = decodeByte(buf, pos);
  135. break;
  136. }
  137. case NIEVT_FOUR_BYTE:
  138. {
  139. dstNodeId->Identifier.Numeric = decodeInt16(buf, pos);
  140. break;
  141. }
  142. case NIEVT_NUMERIC:
  143. {
  144. dstNodeId->Identifier.Numeric = decodeInt32(buf, pos);
  145. break;
  146. }
  147. case NIEVT_STRING:
  148. {
  149. decodeUAString(buf, pos, &dstNodeId->Identifier.String);
  150. break;
  151. }
  152. case NIEVT_GUID:
  153. {
  154. decodeUAGuid(buf, pos, &(dstNodeId->Identifier.Guid));
  155. break;
  156. }
  157. case NIEVT_BYTESTRING:
  158. {
  159. decodeUAByteString(buf, pos, &(dstNodeId->Identifier.OPAQUE));
  160. break;
  161. }
  162. case NIEVT_NAMESPACE_URI_FLAG:
  163. {
  164. //TODO implement
  165. break;
  166. }
  167. default:
  168. break;
  169. }
  170. return 0;
  171. }
  172. /**
  173. * IntegerId
  174. * Part: 4
  175. * Chapter: 7.13
  176. * Page: 118
  177. */
  178. T_IntegerId decodeIntegerId(char* buf, Int32 *pos)
  179. {
  180. return decodeUInt32(buf, pos);
  181. }
  182. /**
  183. * DiagnosticInfo
  184. * Part: 4
  185. * Chapter: 7.9
  186. * Page: 116
  187. */
  188. enum encodingMask
  189. {
  190. encodingMask_HasSymbolicId = 0x01,
  191. encodingMask_HasNamespace = 0x02,
  192. encodingMask_HasLocalizedText = 0x04,
  193. encodingMask_HasLocale = 0x08,
  194. encodingMask_HasAdditionalInfo = 0x10,
  195. encodingMask_HasInnerStatusCode = 0x20,
  196. encodingMask_HasInnerDiagnosticInfo= 0x40
  197. };
  198. Int32 decodeToDiagnosticInfo(char* buf, Int32 *pos,
  199. T_DiagnosticInfo* dstDiagnosticInfo)
  200. {
  201. Byte encodingByte = (buf[*pos]);
  202. Byte mask;
  203. for(mask = 1; mask <= 0x40; mask << 2)
  204. {
  205. switch(mask & encodingByte)
  206. {
  207. case DIEMT_SYMBOLIC_ID:
  208. dstDiagnosticInfo->symbolicId = decodeInt32(buf, pos);
  209. break;
  210. case DIEMT_NAMESPACE:
  211. dstDiagnosticInfo->namespaceUri = decodeInt32(buf, pos);
  212. break;
  213. case DIEMT_LOCALIZED_TEXT:
  214. dstDiagnosticInfo->localizesText = decodeInt32(buf, pos);
  215. break;
  216. case DIEMT_LOCALE:
  217. dstDiagnosticInfo->locale = decodeInt32(buf, pos);
  218. break;
  219. case DIEMT_ADDITIONAL_INFO:
  220. decodeUAString(buf, pos, &dstDiagnosticInfo->additionalInfo);
  221. break;
  222. case DIEMT_INNER_STATUS_CODE:
  223. dstDiagnosticInfo->innerStatusCode = decodeUAStatusCode(buf, pos);
  224. break;
  225. case DIEMT_INNER_DIAGNOSTIC_INFO:
  226. dstDiagnosticInfo->innerDiagnosticInfo = (T_DiagnosticInfo*)opcua_malloc(sizeof(T_DiagnosticInfo));
  227. decodeToDiagnosticInfo(buf, pos,
  228. dstDiagnosticInfo->innerDiagnosticInfo);
  229. break;
  230. }
  231. }
  232. *pos += 1;
  233. return 0;
  234. }
  235. Int32 diagnosticInfo_calcSize(UA_DiagnosticInfo *diagnosticInfo)
  236. {
  237. Int32 minimumLength = 1;
  238. Int32 length = minimumLength;
  239. Byte mask;
  240. Int32 j = 0;
  241. mask = 0;
  242. //puts("diagnosticInfo called");
  243. //printf("with this mask %u", diagnosticInfo->EncodingMask);
  244. for(mask = 1; mask <= 0x40; mask *= 2)
  245. {
  246. j++;
  247. switch(mask & (diagnosticInfo->EncodingMask))
  248. {
  249. case DIEMT_SYMBOLIC_ID:
  250. // puts("diagnosticInfo symbolic id");
  251. length += sizeof(Int32);
  252. break;
  253. case DIEMT_NAMESPACE:
  254. length += sizeof(Int32);
  255. break;
  256. case DIEMT_LOCALIZED_TEXT:
  257. length += sizeof(Int32);
  258. break;
  259. case DIEMT_LOCALE:
  260. length += sizeof(Int32);
  261. break;
  262. case DIEMT_ADDITIONAL_INFO:
  263. length += diagnosticInfo->AdditionalInfo.Length;
  264. length += sizeof(Int32);
  265. break;
  266. case DIEMT_INNER_STATUS_CODE:
  267. length += sizeof(UA_StatusCode);
  268. break;
  269. case DIEMT_INNER_DIAGNOSTIC_INFO:
  270. length += diagnosticInfo_calcSize(diagnosticInfo->InnerDiagnosticInfo);
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. return length;
  277. }
  278. /**
  279. * RequestHeader
  280. * Part: 4
  281. * Chapter: 7.26
  282. * Page: 132
  283. */
  284. /** \copydoc decodeRequestHeader */
  285. Int32 decodeRequestHeader(const AD_RawMessage *srcRaw, Int32 *pos,
  286. T_RequestHeader *dstRequestHeader)
  287. {
  288. decodeUANodeId(srcRaw->message, pos,
  289. &(dstRequestHeader->authenticationToken));
  290. dstRequestHeader->timestamp = decodeUADateTime(srcRaw->message, pos);
  291. dstRequestHeader->requestHandle = decodeIntegerId(srcRaw->message, pos);
  292. dstRequestHeader->returnDiagnostics = decodeUInt32(srcRaw->message, pos);
  293. decodeUAString(srcRaw->message, pos, &dstRequestHeader->auditEntryId);
  294. dstRequestHeader->timeoutHint = decodeUInt32(srcRaw->message, pos);
  295. // AdditionalHeader will stay empty, need to be changed if there is relevant information
  296. return 0;
  297. }
  298. /**
  299. * ResponseHeader
  300. * Part: 4
  301. * Chapter: 7.27
  302. * Page: 133
  303. */
  304. /** \copydoc encodeResponseHeader */
  305. /*Int32 encodeResponseHeader(const T_ResponseHeader *responseHeader, Int32 *pos,
  306. AD_RawMessage *dstBuf)
  307. {
  308. return 0;
  309. }*/
  310. Int32 nodeId_calcSize(UA_NodeId *nodeId)
  311. {
  312. Int32 length = 0;
  313. switch(nodeId->EncodingByte)
  314. {
  315. case NIEVT_TWO_BYTE:
  316. length += 2 * sizeof(Byte);
  317. break;
  318. case NIEVT_FOUR_BYTE:
  319. length += 4 * sizeof(Byte);
  320. break;
  321. case NIEVT_NUMERIC:
  322. //TODO call registered function which knows how to calculate the length
  323. break;
  324. case NIEVT_STRING:
  325. length += sizeof(Byte) + sizeof(UInt16) + sizeof(UInt32) + nodeId->Identifier.String.Length;
  326. break;
  327. case NIEVT_GUID:
  328. length += sizeof(Byte) + sizeof(UInt16) + sizeof(UInt32) + sizeof(UInt16) + sizeof(UInt16) + 8 * sizeof(Byte);
  329. break;
  330. case NIEVT_BYTESTRING:
  331. length += sizeof(Byte) + sizeof(UInt16) + sizeof(UInt32) + nodeId->Identifier.OPAQUE.Length;
  332. break;
  333. default:
  334. break;
  335. }
  336. return length;
  337. }
  338. Int32 extensionObject_calcSize(UA_ExtensionObject *extensionObject)
  339. {
  340. Int32 length;
  341. Byte mask;
  342. length += nodeId_calcSize(&(extensionObject->TypeId));
  343. length += sizeof(Byte); //The EncodingMask Byte
  344. if(extensionObject->Encoding == 0x01 ||
  345. extensionObject->Encoding == 0x02)
  346. {
  347. length += sizeof(Int32) + extensionObject->Length;
  348. }
  349. return length;
  350. }
  351. Int32 responseHeader_calcSize(T_ResponseHeader *responseHeader)
  352. {
  353. Int32 minimumLength = 20; // summation of all simple types
  354. Int32 i, length;
  355. length += minimumLength;
  356. for (i = 0; i < responseHeader->noOfStringTable; i++)
  357. {
  358. length += responseHeader->stringTable[i].Length;
  359. length += sizeof(UInt32); // length of the encoded length field
  360. }
  361. length += diagnosticInfo_calcSize(responseHeader->serviceDiagnostics);
  362. //ToDo
  363. length += extensionObject_calcSize(&(responseHeader->additionalHeader));
  364. return length;
  365. }