opcua_binaryEncDec.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. const char *TEST_PASSED = "PASSED";
  10. /*
  11. * convert byte array to Byte
  12. */
  13. Byte convertToByte(char* buf, int pos)
  14. {
  15. return (Byte)buf[pos];
  16. }
  17. /*
  18. * convert byte array to UInt16
  19. */
  20. UInt16 convertToUInt16(char* buf, int pos)
  21. {
  22. Byte t1 = buf[pos];
  23. Int32 t2 = (UInt16)(buf[pos+1] << 8);
  24. return t1 + t2;
  25. }
  26. /*
  27. * convert byte array to Int32
  28. */
  29. Int32 convertToInt32(char* buf, int pos)
  30. {
  31. SByte t1 = buf[pos];
  32. Int32 t2 = (UInt32)(buf[pos+1] << 8);
  33. Int32 t3 = (UInt32)(buf[pos+2] << 16);
  34. Int32 t4 = (UInt32)(buf[pos+3] << 24);
  35. return t1 + t2 + t3 + t4;
  36. }
  37. /*
  38. * convert byte array to UInt32
  39. */
  40. UInt32 convertToUInt32(char* buf, int pos)
  41. {
  42. Byte t1 = buf[pos];
  43. UInt32 t2 = (UInt32)(buf[pos+1] << 8);
  44. UInt32 t3 = (UInt32)(buf[pos+2] << 16);
  45. UInt32 t4 = (UInt32)(buf[pos+3] << 24);
  46. return t1 + t2 + t3 + t4;
  47. }
  48. void convertUInt32ToByteArray(UInt32 value,char *buf,int pos)
  49. {
  50. buf[pos] = (char)(value && 0xFF);
  51. buf[pos + 1] = (char)((value >> 8) && 0xFF);
  52. buf[pos + 2] = (char)((value >> 16) && 0xFF);
  53. buf[pos + 3] = (char)((value >> 24) && 0xFF);
  54. }
  55. /*
  56. * convert byte array to Int64
  57. */
  58. Int64 convertToInt64(char* buf, int pos)
  59. {
  60. SByte t1 = buf[pos];
  61. UInt64 t2 = (UInt64)(buf[pos+1] << 8);
  62. UInt64 t3 = (UInt64)(buf[pos+2] << 16);
  63. UInt64 t4 = (UInt64)(buf[pos+3] << 24);
  64. UInt64 t5 = (UInt64)(buf[pos+4] << 32);
  65. UInt64 t6 = (UInt64)(buf[pos+5] << 40);
  66. UInt64 t7 = (UInt64)(buf[pos+6] << 48);
  67. UInt64 t8 = (UInt64)(buf[pos+7] << 56);
  68. return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  69. }
  70. Int64 convertToInt64_test(char* buf, int pos)
  71. {
  72. printf("");
  73. }
  74. UA_String convertToUAString(char* buf, int pos)
  75. {
  76. UA_String tmpUAString;
  77. tmpUAString.Length = convertToInt32(buf,pos);
  78. tmpUAString.Data = &(buf[sizeof(UInt32)]);
  79. return tmpUAString;
  80. }
  81. UA_Guid convertToUAGuid(char* buf, int pos)
  82. {
  83. UA_Guid tmpUAGuid;
  84. int counter = 0;
  85. UInt32 i = 0;
  86. for(i = 1; i <= 4; i++)
  87. {
  88. tmpUAGuid.Data1[i] = convertToUInt32(buf, pos+counter);
  89. counter += sizeof(UInt32);
  90. }
  91. for(i = 1; i <= 2; i++)
  92. {
  93. tmpUAGuid.Data2[i] = convertToUInt16(buf, pos+counter);
  94. counter += sizeof(UInt16);
  95. }
  96. for(i = 1; i <= 2; i++)
  97. {
  98. tmpUAGuid.Data3[i] = convertToUInt16(buf, pos+counter);
  99. counter += sizeof(UInt16);
  100. }
  101. for(i = 1; i <= 8; i++)
  102. {
  103. tmpUAGuid.Data4[i] = convertToByte(buf, pos+counter);
  104. counter += sizeof(Byte);
  105. }
  106. return tmpUAGuid;
  107. }
  108. UA_ByteString convertToUAByteString(char* buf, int pos){
  109. UA_ByteString tmpUAByteString;
  110. int counter = sizeof(Int32);
  111. int i = 0;
  112. tmpUAByteString.Length = convertToInt32(buf, pos);
  113. Byte byteStringData[tmpUAByteString.Length];
  114. if(tmpUAByteString.Length == -1){
  115. return tmpUAByteString;
  116. }else{
  117. for(i = 0; i < tmpUAByteString.Length; i++)
  118. {
  119. byteStringData[i] = convertToByte(buf, pos+counter);
  120. counter += sizeof(Byte);
  121. }
  122. }
  123. tmpUAByteString.Data = byteStringData;
  124. return tmpUAByteString;
  125. }
  126. UA_DateTime convertToUADateTime(char* buf, int pos){
  127. UA_DateTime tmpUADateTime;
  128. tmpUADateTime = convertToInt64(buf, pos);
  129. return tmpUADateTime;
  130. }
  131. UA_StatusCode convertToUAStatusCode(char* buf, int pos){
  132. return convertToUInt32(buf, pos);
  133. }
  134. UA_NodeId convertToUANodeId(char* buf, int pos){
  135. UA_NodeId tmpUANodeId;
  136. tmpUANodeId.EncodingByte = convertToInt32(buf, 0);
  137. int counter = sizeof(UInt32);
  138. UA_NodeIdEncodingValuesType encodingType = tmpUANodeId.EncodingByte;
  139. switch(encodingType)
  140. {
  141. case NIEVT_TWO_BYTE:
  142. {
  143. tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
  144. counter += sizeof(UInt16);
  145. break;
  146. }
  147. case NIEVT_FOUR_BYTE:
  148. {
  149. tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
  150. counter += sizeof(Int64);
  151. break;
  152. }
  153. case NIEVT_NUMERIC:
  154. {
  155. tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
  156. counter += sizeof(UInt32);
  157. break;
  158. }
  159. case NIEVT_STRING:
  160. {
  161. tmpUANodeId.Identifier.String = convertToUAString(buf, counter);
  162. counter += sizeof(sizeof(UInt32) + tmpUANodeId.Identifier.String.Length*sizeof(char));
  163. break;
  164. }
  165. case NIEVT_GUID:
  166. {
  167. tmpUANodeId.Identifier.Guid = convertToUAGuid(buf, counter);
  168. counter += sizeof(UA_Guid);
  169. break;
  170. }
  171. case NIEVT_BYTESTRING:
  172. {
  173. tmpUANodeId.Identifier.OPAQUE = convertToUAByteString(buf, counter);
  174. //If Length == -1 then the ByteString is null
  175. if(tmpUANodeId.Identifier.OPAQUE.Length == -1)
  176. {
  177. counter += sizeof(Int32);
  178. }else{
  179. counter += sizeof(Int32)+sizeof(Byte)*tmpUANodeId.Identifier.OPAQUE.Length;
  180. }
  181. break;
  182. }
  183. default:
  184. break;
  185. }
  186. return tmpUANodeId;
  187. }