gagent_tool.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. {
  32. pkcs[i] = cs;
  33. }
  34. rt_memcpy(src + len, pkcs, cs);
  35. return (len + cs);
  36. }
  37. uint16_t gagent_parse_rem_len(const uint8_t* buf)
  38. {
  39. uint16_t multiplier = 1;
  40. uint16_t value = 0;
  41. uint8_t digit;
  42. do
  43. {
  44. digit = *buf;
  45. value += (digit & 0x7F) * multiplier;
  46. multiplier *= 0x80;
  47. buf++;
  48. } while((digit & 0x80) != 0);
  49. return value;
  50. }
  51. uint8_t gagent_num_rem_len_bytes(const uint8_t* buf)
  52. {
  53. uint8_t num_bytes = 1;
  54. if ((buf[0] & 0x80) == 0x80)
  55. {
  56. num_bytes++;
  57. if ((buf[1] & 0x80) == 0x80)
  58. {
  59. num_bytes ++;
  60. if ((buf[2] & 0x80) == 0x80)
  61. {
  62. num_bytes ++;
  63. }
  64. }
  65. }
  66. return num_bytes;
  67. }
  68. uint8_t gagent_get_rem_len(int length, char *buf)
  69. {
  70. uint8_t cnt = 0, digit;
  71. do
  72. {
  73. digit = length % 128;
  74. length /= 128;
  75. if (length > 0)
  76. digit = digit | 0x80;
  77. buf[cnt] = digit;
  78. cnt ++;
  79. } while(length > 0);
  80. return cnt;
  81. }
  82. int gagent_get_one_packet(char *packet, int *data_len, rt_uint8_t *len_num, int remain_len)
  83. {
  84. char *index;
  85. uint16_t len;
  86. uint8_t length_len;
  87. if(packet == NULL || data_len == NULL || len_num == NULL || remain_len <= 0)
  88. return -1;
  89. index = packet;
  90. len = gagent_parse_rem_len((const uint8_t *)index + 4);
  91. length_len = gagent_num_rem_len_bytes((const uint8_t*)index + 4);
  92. *data_len = len;
  93. *len_num = length_len;
  94. return RT_EOK;
  95. }
  96. int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t buf_len)
  97. {
  98. char length_bytes[4];
  99. rt_uint32_t packet_len;
  100. rt_uint8_t length_num, i;
  101. char *index = packet;
  102. //head
  103. *index ++ = 0x00;
  104. *index ++ = 0x00;
  105. *index ++ = 0x00;
  106. *index ++ = 0x03;
  107. //
  108. packet_len = buf_len + 3; //flag + cmd * 2
  109. if(buf && (buf_len > 0))
  110. {
  111. packet_len += 1; //action
  112. }
  113. memset(length_bytes, 0, sizeof(length_bytes));
  114. length_num = gagent_get_rem_len(packet_len, length_bytes);
  115. for(i = 0; i < length_num; i ++)
  116. {
  117. *index ++ = length_bytes[i];
  118. }
  119. //flag
  120. *index ++ = 0x00;
  121. //
  122. *index ++ = 0x00;
  123. *index ++ = 0x91;
  124. *index ++ = action;
  125. if(buf && buf_len > 0)
  126. {
  127. memcpy(index, buf, buf_len);
  128. index += buf_len;
  129. }
  130. return (index - packet);
  131. }
  132. int gagent_strtohex(char *dst, char *src, int len)
  133. {
  134. char h1,h2;
  135. int i;
  136. for (i = 0; i< len; i += 2)
  137. {
  138. h1 = src[i];
  139. if((h1 >= 'A') && (h1 <= 'F'))
  140. h1 = h1 - 'A' + 10;
  141. else if((h1 >= 'a') && (h1 <= 'f'))
  142. h1 = h1 - 'a' + 10;
  143. else
  144. h1 = h1 - '0';
  145. h2 = src[i + 1];
  146. if((h2 >= 'A') && (h2 <= 'F'))
  147. h2 = h2 - 'A' + 10;
  148. else if((h2 >= 'a') && (h2 <= 'f'))
  149. h2 = h2 - 'a' + 10;
  150. else
  151. h2 = h2 - '0';
  152. dst[i / 2] = ((h1 << 4) & 0xf0) + (h2 & 0x0f);
  153. }
  154. return len / 2;
  155. }