opcua_binaryEncDec.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Byte convertToByte(const char *buf, Int32 *pos)
  10. {
  11. *pos = (*pos) + 1;
  12. return (Byte) buf[(*pos)-1];
  13. }
  14. void encodeByte(Byte encodeByte, Int32 *pos, AD_RawMessage *dstBuf)
  15. {
  16. dstBuf->message[*pos] = encodeByte;
  17. *pos = (*pos) + 1;
  18. dstBuf->length = dstBuf->length + 1;
  19. }
  20. UInt16 convertToUInt16(const char* buf, Int32 *pos)
  21. {
  22. Byte t1 = buf[*pos];
  23. UInt16 t2 = (UInt16) (buf[*pos + 1] << 8);
  24. *pos += 2;
  25. return t1 + t2;
  26. }
  27. Int16 convertToInt16(const char* buf, Int32 *pos)
  28. {
  29. Byte t1 = buf[*pos];
  30. Int32 t2 = (Int16) (buf[*pos + 1] << 8);
  31. *pos += 2;
  32. return t1 + t2;
  33. }
  34. Int32 convertToInt32(const char* buf, Int32 *pos)
  35. {
  36. SByte t1 = buf[*pos];
  37. Int32 t2 = (UInt32) (buf[*pos + 1] << 8);
  38. Int32 t3 = (UInt32) (buf[*pos + 2] << 16);
  39. Int32 t4 = (UInt32) (buf[*pos + 3] << 24);
  40. *pos += 4;
  41. return t1 + t2 + t3 + t4;
  42. }
  43. UInt32 convertToUInt32(const char* buf, Int32 *pos)
  44. {
  45. Byte t1 = buf[*pos];
  46. UInt32 t2 = (UInt32) (buf[*pos + 1] << 8);
  47. UInt32 t3 = (UInt32) (buf[*pos + 2] << 16);
  48. UInt32 t4 = (UInt32) (buf[*pos + 3] << 24);
  49. *pos += 4;
  50. return t1 + t2 + t3 + t4;
  51. }
  52. void convertUInt32ToByteArray(UInt32 value, char *dstBuf, Int32 *pos)
  53. {
  54. memcpy(&(dstBuf[*pos]), &value, sizeof(value));
  55. pos += 4;
  56. /*buf[pos] = (char)(value && 0xFF);
  57. buf[pos + 1] = (char)((value >> 8) && 0xFF);
  58. buf[pos + 2] = (char)((value >> 16) && 0xFF);
  59. buf[pos + 3] = (char)((value >> 24) && 0xFF);
  60. */
  61. }
  62. Int64 convertToInt64(const char* buf, Int32 *pos)
  63. {
  64. SByte t1 = buf[*pos];
  65. UInt64 t2 = (UInt64) (buf[*pos + 1] << 8);
  66. UInt64 t3 = (UInt64) (buf[*pos + 2] << 16);
  67. UInt64 t4 = (UInt64) (buf[*pos + 3] << 24);
  68. UInt64 t5 = (UInt64) (buf[*pos + 4] << 32);
  69. UInt64 t6 = (UInt64) (buf[*pos + 5] << 40);
  70. UInt64 t7 = (UInt64) (buf[*pos + 6] << 48);
  71. UInt64 t8 = (UInt64) (buf[*pos + 7] << 56);
  72. pos += 8;
  73. return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  74. }
  75. Int32 convertToUAString(const char* buf, Int32 *pos, UA_String *dstUAString)
  76. {
  77. dstUAString->Length = convertToInt32(buf, pos);
  78. if (dstUAString->Length > 0)
  79. {
  80. dstUAString->Data = &(buf[*pos]);
  81. }
  82. else
  83. {
  84. dstUAString->Length = 0;
  85. dstUAString->Data = NULL;
  86. }
  87. *pos += dstUAString->Length;
  88. }
  89. Int32 convertToUAGuid(const char *buf, Int32 *pos, UA_Guid *dstGUID)
  90. {
  91. dstGUID->Data1 = convertToUInt32(buf, pos);
  92. dstGUID->Data2 = convertToUInt16(buf, pos);
  93. dstGUID->Data3 = convertToUInt16(buf, pos);
  94. convertToUAByteString(buf, pos, &(dstGUID->Data4));
  95. return 0;
  96. }
  97. convertToUAByteString(const char *buf, Int32* pos, UA_ByteString *dstBytestring)
  98. {
  99. convertToUAString(buf,pos,dstBytestring);
  100. }
  101. UA_DateTime convertToUADateTime(const char *buf, Int32 *pos)
  102. {
  103. return convertToInt64(buf, pos);
  104. }
  105. UA_StatusCode convertToUAStatusCode(const char* buf, Int32 *pos)
  106. {
  107. return convertToUInt32(buf, pos);
  108. }
  109. Int32 convertToUANodeId(const char* buf, Int32 *pos, UA_NodeId *dstNodeId)
  110. {
  111. dstNodeId->EncodingByte = convertToInt32(buf, pos);
  112. switch (dstNodeId->EncodingByte)
  113. {
  114. case NIEVT_TWO_BYTE:
  115. {
  116. dstNodeId->Identifier.Numeric = convertToByte(buf, pos);
  117. break;
  118. }
  119. case NIEVT_FOUR_BYTE:
  120. {
  121. dstNodeId->Identifier.Numeric = convertToInt16(buf, pos);
  122. break;
  123. }
  124. case NIEVT_NUMERIC:
  125. {
  126. dstNodeId->Identifier.Numeric = convertToInt32(buf, pos);
  127. break;
  128. }
  129. case NIEVT_STRING:
  130. {
  131. convertToUAString(buf, pos, &dstNodeId->Identifier.String);
  132. break;
  133. }
  134. case NIEVT_GUID:
  135. {
  136. convertToUAGuid(buf, pos, &(dstNodeId->Identifier.Guid));
  137. break;
  138. }
  139. case NIEVT_BYTESTRING:
  140. {
  141. convertToUAByteString(buf, pos,&(dstNodeId->Identifier.OPAQUE));
  142. break;
  143. }
  144. case NIEVT_NAMESPACE_URI_FLAG:
  145. {
  146. //TODO implement
  147. break;
  148. }
  149. default:
  150. break;
  151. }
  152. return 0;
  153. }