升龙物业 老版本 ocx IPO, 加密狗 转值班电话

PduEntityHead.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "StdAfx.h"
  2. #include "PduEntityHead.h"
  3. CPduEntityHead::CPduEntityHead(void)
  4. {
  5. Initialization(0, PDU_DEV_TYPE_UNKNOWN, 0, PDU_DEV_TYPE_UNKNOWN, PDU_CMD_UNKNOWN, FALSE, 0);
  6. }
  7. CPduEntityHead::~CPduEntityHead(void)
  8. {
  9. }
  10. /*****************************************************************
  11. **【函数名称】 UShortToBytes
  12. **【函数功能】 32位整数转化为4字节
  13. **【参数】 nValue:16位整数
  14. ** byte1-byte4:32位整数的从高到低位对应的4个字节
  15. **【返回值】
  16. ****************************************************************/
  17. void CPduEntityHead::UShortToBytes(USHORT nValue, UCHAR *byte1, UCHAR *byte2)
  18. {
  19. *byte2 = (unsigned char)(nValue & 0xFF);
  20. *byte1 = (unsigned char)((nValue >> 8) & 0xFF);
  21. }
  22. /*****************************************************************
  23. **【函数名称】 BytesToUShort
  24. **【函数功能】 2字节转化为16位整数
  25. **【参数】 byte1:16位整数的高位字节
  26. ** byte2:16位整数的低位字节
  27. **【返回值】 16位整数
  28. ****************************************************************/
  29. USHORT CPduEntityHead::BytesToUShort(UCHAR byte1, UCHAR byte2)
  30. {
  31. unsigned short nValue;
  32. nValue = byte1;
  33. nValue = nValue << 8;
  34. nValue ^= byte2;
  35. return nValue;
  36. }
  37. /*****************************************************************
  38. **【函数名称】 ULongToBytes
  39. **【函数功能】 32位整数转化为4字节
  40. **【参数】 nValue:32位整数
  41. ** byte1-byte4:32位整数的从高到低位对应的4个字节
  42. **【返回值】
  43. ****************************************************************/
  44. void CPduEntityHead::ULongToBytes(ULONG nValue, UCHAR *byte1, UCHAR *byte2, UCHAR *byte3, UCHAR *byte4)
  45. {
  46. *byte4 = (unsigned char)(nValue & 0xFF);
  47. *byte3 = (unsigned char)((nValue >> 8) & 0xFF);
  48. *byte2 = (unsigned char)((nValue >> 16) & 0xFF);
  49. *byte1 = (unsigned char)((nValue >> 24) & 0xFF);
  50. }
  51. /*****************************************************************
  52. **【函数名称】 BytesToULong
  53. **【函数功能】 4字节转化为32位整数
  54. **【参数】 byte1-byte4:32位整数的从高到低位对应的4个字节
  55. **【返回值】 32位整数
  56. ****************************************************************/
  57. ULONG CPduEntityHead::BytesToULong(UCHAR byte1, UCHAR byte2, UCHAR byte3, UCHAR byte4)
  58. {
  59. unsigned long nValue;
  60. nValue = byte1;
  61. nValue = nValue << 8;
  62. nValue ^= byte2;
  63. nValue = nValue << 8;
  64. nValue ^= byte3;
  65. nValue = nValue << 8;
  66. nValue ^= byte4;
  67. return nValue;
  68. }
  69. /*****************************************************************
  70. **【函数名称】 Initialization
  71. **【函数功能】 初始化函数
  72. **【参数】 本端设备ID,本端设备类型,对端设备ID,对端设备类型,
  73. ** 具体命令类型,具体命令占用BUF长度
  74. **【返回值】
  75. ****************************************************************/
  76. void CPduEntityHead::Initialization(ULONG a_nSrcId, PDU_DEV_TYPE a_nSrcType, ULONG a_nDstId, PDU_DEV_TYPE a_nDstType, PDU_CMD_TYPE a_nCmdType, BOOL a_bIsExecReturn, USHORT a_nDataLen)
  77. {
  78. m_nSrcId = a_nSrcId;
  79. m_nSrcType = a_nSrcType;
  80. m_nDstId = a_nDstId;
  81. m_nDstType = a_nDstType;
  82. m_nCmdType = a_nCmdType;
  83. m_nDataLen = a_nDataLen;
  84. m_nIsExecReturn = a_bIsExecReturn ? 1 : 0;
  85. m_nUnusedType2 = 0;
  86. m_nCrcCheck = 0;
  87. }
  88. /*****************************************************************
  89. **【函数名称】 Incode
  90. **【函数功能】 打包包头
  91. **【参数】
  92. **【返回值】 szBuf[]
  93. ****************************************************************/
  94. BOOL CPduEntityHead::Incode(UCHAR szBuf[])
  95. {
  96. ULongToBytes(m_nSrcId, &(szBuf[0]), &(szBuf[1]), &(szBuf[2]), &(szBuf[3]));
  97. szBuf[4]=(unsigned char)m_nSrcType;
  98. ULongToBytes(m_nDstId, &(szBuf[5]), &(szBuf[6]), &(szBuf[7]), &(szBuf[8]));
  99. szBuf[9] = (unsigned char)m_nDstType;
  100. UShortToBytes((unsigned short)m_nCmdType,&(szBuf[10]),&(szBuf[11]));
  101. ULongToBytes(m_nIsExecReturn,&(szBuf[12]),&(szBuf[13]),&(szBuf[14]),&(szBuf[15]));
  102. ULongToBytes(m_nUnusedType2,&(szBuf[16]),&(szBuf[17]),&(szBuf[18]),&(szBuf[19]));
  103. UShortToBytes((unsigned short)m_nDataLen,&(szBuf[20]),&(szBuf[21]));
  104. ULongToBytes(m_nCrcCheck,&(szBuf[22]),&(szBuf[23]),&(szBuf[24]),&(szBuf[25]));
  105. return TRUE;
  106. }
  107. /*****************************************************************
  108. **【函数名称】 Decode
  109. **【函数功能】 解析包头
  110. **【参数】 szBuf[]
  111. **【返回值】
  112. ****************************************************************/
  113. BOOL CPduEntityHead::Decode(UCHAR szBuf[])
  114. {
  115. m_nSrcId = BytesToULong(szBuf[0],szBuf[1],szBuf[2],szBuf[3]);
  116. m_nSrcType = (PDU_DEV_TYPE)szBuf[4];
  117. m_nDstId = BytesToULong(szBuf[5],szBuf[6],szBuf[7],szBuf[8]);
  118. m_nDstType = (PDU_DEV_TYPE)szBuf[9];
  119. m_nCmdType = (PDU_CMD_TYPE)BytesToUShort(szBuf[10],szBuf[11]);
  120. m_nIsExecReturn = BytesToULong(szBuf[12],szBuf[13],szBuf[14],szBuf[15]);
  121. m_nUnusedType2 = BytesToULong(szBuf[16],szBuf[17],szBuf[18],szBuf[19]);
  122. m_nDataLen = BytesToUShort(szBuf[20],szBuf[21]);
  123. m_nCrcCheck = BytesToULong(szBuf[22],szBuf[23],szBuf[24],szBuf[25]);
  124. return m_nCrcCheck == 0 ? TRUE : FALSE;
  125. }