gagent_tool.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : gagent_tool.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-01-03 flyingcys first version
  23. */
  24. #include "gagent_def.h"
  25. int gagent_add_pkcs(char *src, int len)
  26. {
  27. char pkcs[16];
  28. int i, cs = 16 - len % 16;
  29. rt_memset(pkcs, 0, sizeof(pkcs));
  30. for(i = 0; i < cs; i ++ )
  31. pkcs[i] = cs;
  32. rt_memcpy(src + len, pkcs, cs);
  33. return (len + cs);
  34. }
  35. rt_uint16_t gagent_parse_rem_len(const rt_uint8_t *buf)
  36. {
  37. rt_uint16_t multiplier = 1;
  38. rt_uint16_t value = 0;
  39. rt_uint8_t digit;
  40. do
  41. {
  42. digit = *buf;
  43. value += (digit & 0x7F) * multiplier;
  44. multiplier *= 0x80;
  45. buf++;
  46. }while((digit & 0x80) != 0);
  47. return value;
  48. }
  49. rt_uint8_t gagent_num_rem_len_bytes(const uint8_t* buf)
  50. {
  51. rt_uint8_t num_bytes = 1;
  52. if((buf[0] & 0x80) == 0x80)
  53. {
  54. num_bytes++;
  55. if((buf[1] & 0x80) == 0x80)
  56. {
  57. num_bytes ++;
  58. if ((buf[2] & 0x80) == 0x80)
  59. num_bytes ++;
  60. }
  61. }
  62. return num_bytes;
  63. }
  64. rt_uint8_t gagent_get_rem_len(int length, char *buf)
  65. {
  66. rt_uint8_t cnt = 0, digit;
  67. do
  68. {
  69. digit = length % 128;
  70. length /= 128;
  71. if (length > 0)
  72. digit = digit | 0x80;
  73. buf[cnt] = digit;
  74. cnt ++;
  75. }while(length > 0);
  76. return cnt;
  77. }
  78. int gagent_get_one_packet(char *packet, int *data_len, rt_uint8_t *len_num, int remain_len)
  79. {
  80. char *index;
  81. rt_uint16_t len;
  82. rt_uint8_t length_len;
  83. if(packet == NULL || data_len == NULL || len_num == NULL || remain_len <= 0)
  84. return -1;
  85. index = packet;
  86. len = gagent_parse_rem_len((const uint8_t *)index + 4);
  87. length_len = gagent_num_rem_len_bytes((const uint8_t*)index + 4);
  88. *data_len = len;
  89. *len_num = length_len;
  90. return RT_EOK;
  91. }
  92. int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t buf_len)
  93. {
  94. char length_bytes[4];
  95. rt_uint32_t packet_len;
  96. rt_uint8_t length_num, i;
  97. char *index = packet;
  98. //head
  99. *index ++ = 0x00;
  100. *index ++ = 0x00;
  101. *index ++ = 0x00;
  102. *index ++ = 0x03;
  103. //
  104. packet_len = buf_len + 3; //flag + cmd * 2
  105. if(buf && (buf_len > 0))
  106. packet_len += 1; //action
  107. memset(length_bytes, 0, sizeof(length_bytes));
  108. length_num = gagent_get_rem_len(packet_len, length_bytes);
  109. for(i = 0; i < length_num; i ++)
  110. *index ++ = length_bytes[i];
  111. //flag
  112. *index ++ = 0x00;
  113. //
  114. *index ++ = 0x00;
  115. *index ++ = 0x91;
  116. *index ++ = action;
  117. if(buf && buf_len > 0)
  118. {
  119. memcpy(index, buf, buf_len);
  120. index += buf_len;
  121. }
  122. return (index - packet);
  123. }
  124. int gagent_strtohex(char *dst, char *src, int len)
  125. {
  126. char h1,h2;
  127. int i;
  128. for(i = 0; i< len; i += 2)
  129. {
  130. h1 = src[i];
  131. if((h1 >= 'A') && (h1 <= 'F'))
  132. h1 = h1 - 'A' + 10;
  133. else if((h1 >= 'a') && (h1 <= 'f'))
  134. h1 = h1 - 'a' + 10;
  135. else
  136. h1 = h1 - '0';
  137. h2 = src[i + 1];
  138. if((h2 >= 'A') && (h2 <= 'F'))
  139. h2 = h2 - 'A' + 10;
  140. else if((h2 >= 'a') && (h2 <= 'f'))
  141. h2 = h2 - 'a' + 10;
  142. else
  143. h2 = h2 - '0';
  144. dst[i / 2] = ((h1 << 4) & 0xf0) + (h2 & 0x0f);
  145. }
  146. return len / 2;
  147. }