opcua_binaryEncDec.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * opcua_binaryEncDec.c
  3. *
  4. * Created on: Dec 18, 2013
  5. * Author: opcua
  6. */
  7. #include "opcua_binaryEncDec.h"
  8. /*
  9. * convert byte array to Byte
  10. */
  11. Byte convertToByte(char* buf, int pos)
  12. {
  13. return (Byte)buf[pos];
  14. }
  15. /*
  16. * convert byte array to UInt16
  17. */
  18. UInt16 convertToUInt16(char* buf, int pos)
  19. {
  20. Byte t1 = buf[pos];
  21. Int32 t2 = (UInt16)(buf[pos+1] << 8);
  22. return t1 + t2;
  23. }
  24. /*
  25. * convert byte array to Int32
  26. */
  27. Int32 convertToInt32(char* buf, int pos)
  28. {
  29. SByte t1 = buf[pos];
  30. Int32 t2 = (UInt32)(buf[pos+1] << 8);
  31. Int32 t3 = (UInt32)(buf[pos+2] << 16);
  32. Int32 t4 = (UInt32)(buf[pos+3] << 24);
  33. return t1 + t2 + t3 + t4;
  34. }
  35. /*
  36. * convert byte array to UInt32
  37. */
  38. UInt32 convertToUInt32(char* buf, int pos)
  39. {
  40. Byte t1 = buf[pos];
  41. UInt32 t2 = (UInt32)(buf[pos+1] << 8);
  42. UInt32 t3 = (UInt32)(buf[pos+2] << 16);
  43. UInt32 t4 = (UInt32)(buf[pos+3] << 24);
  44. return t1 + t2 + t3 + t4;
  45. }
  46. void convertUInt32ToByteArray(UInt32 value,char *buf,int pos)
  47. {
  48. buf[pos] = (char)(value && 0xFF);
  49. buf[pos + 1] = (char)((value >> 8) && 0xFF);
  50. buf[pos + 2] = (char)((value >> 16) && 0xFF);
  51. buf[pos + 3] = (char)((value >> 24) && 0xFF);
  52. }
  53. /*
  54. * convert byte array to Int64
  55. */
  56. Int64 convertToInt64(char* buf, int pos)
  57. {
  58. SByte t1 = buf[pos];
  59. UInt64 t2 = (UInt64)(buf[pos+1] << 8);
  60. UInt64 t3 = (UInt64)(buf[pos+2] << 16);
  61. UInt64 t4 = (UInt64)(buf[pos+3] << 24);
  62. UInt64 t5 = (UInt64)(buf[pos+4] << 32);
  63. UInt64 t6 = (UInt64)(buf[pos+5] << 40);
  64. UInt64 t7 = (UInt64)(buf[pos+6] << 48);
  65. UInt64 t8 = (UInt64)(buf[pos+7] << 56);
  66. return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  67. }
  68. UA_String convertToUAString(char* buf, int pos)
  69. {
  70. UA_String tmpUAString;
  71. tmpUAString.Length = convertToInt32(buf,pos);
  72. tmpUAString.Data = &(buf[sizeof(UInt32)]);
  73. return tmpUAString;
  74. }
  75. UA_Guid convertToUAGuid(char* buf, int pos)
  76. {
  77. UA_Guid tmpUAGuid;
  78. int counter = 0;
  79. UInt32 i = 0;
  80. for(i = 1; i <= 4; i++)
  81. {
  82. tmpUAGuid.Data1[i] = convertToUInt32(*buf, pos+counter);
  83. counter += sizeof(UInt32);
  84. }
  85. for(i = 1; i <= 2; i++)
  86. {
  87. tmpUAGuid.Data2[i] = convertToUInt16(*buf, pos+counter);
  88. counter += sizeof(UInt16);
  89. }
  90. for(i = 1; i <= 2; i++)
  91. {
  92. tmpUAGuid.Data3[i] = convertToUInt16(*buf, pos+counter);
  93. counter += sizeof(UInt16);
  94. }
  95. for(i = 1; i <= 8; i++)
  96. {
  97. tmpUAGuid.Data4[i] = convertToByte(*buf, pos+counter);
  98. counter += sizeof(Byte);
  99. }
  100. return tmpUAGuid;
  101. }
  102. UA_NodeId convertToUANodeId(char* buf, int pos){
  103. UA_NodeId tmpUANodeId;
  104. tmpUANodeId.EncodingByte = convertToInt32(*buf, 0);
  105. int counter = sizeof(UInt32);
  106. UA_NodeIdEncodingValuesType encodingType = tmpUANodeId.EncodingByte;
  107. switch(encodingType)
  108. {
  109. case NIEVT_TWO_BYTE:
  110. {
  111. tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
  112. counter += sizeof(UInt16);
  113. break;
  114. }
  115. case NIEVT_FOUR_BYTE:
  116. {
  117. tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
  118. counter += sizeof(Int64);
  119. break;
  120. }
  121. case NIEVT_NUMERIC:
  122. {
  123. tmpUANodeId.Identifier.Numeric = convertToInt32(*buf, counter);
  124. counter += sizeof(UInt32);
  125. break;
  126. }
  127. case NIEVT_STRING:
  128. {
  129. tmpUANodeId.Identifier.String = convertToUAString(*buf, counter);
  130. counter += sizeof(sizeof(UInt32) + tmpUANodeId.Identifier.String.Length*sizeof(char));
  131. break;
  132. }
  133. case NIEVT_GUID:
  134. {
  135. UA_Guid tempGuid = convertToUAGuid(*buf, counter);
  136. tmpUANodeId.Identifier.Guid = &tempGuid;
  137. counter += sizeof(UA_Guid);
  138. break;
  139. }
  140. case NIEVT_BYTESTRING:
  141. {
  142. //ToDo
  143. break;
  144. }
  145. case NIEVT_NAMESPACE_URI_FLAG:
  146. {
  147. break;
  148. }
  149. case NIEVT_SERVERINDEX_FLAG:
  150. {
  151. break;
  152. }
  153. }
  154. return tmpUANodeId;
  155. }