You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "atcmd.h"
  2. #include "zport.h"
  3. #include <lwip/sockets.h>
  4. #include "config.h"
  5. #define at_processer_rx_buf_size 128
  6. #define at_processer_tx_buf_size 128
  7. // 接收到了多少数据
  8. volatile uint16_t s_at_cmd_uart_rx_off = 0;
  9. // 当前是否正在处理接收到的数据
  10. volatile bool s_at_cmd_uart_rx_buf_is_processing = false;
  11. static uint8_t at_rx_buf[at_processer_rx_buf_size];
  12. // static uint8_t at_tx_buf[at_processer_tx_buf_size];
  13. /* AT指令表 */
  14. const AT_cmd_func at_cmd_func[] = {
  15. {AT_CMD_TEST, "AT", at_cmd_test},
  16. {AT_CMD_IP, "AT+IP=", at_cmd_ip},
  17. {AT_CMD_GW, "AT+GW=", NULL},
  18. {AT_CMD_NETMASK, "AT+NETMASK=", NULL},
  19. {AT_CMD_NETMODULE, "AT+NETMODULE=", NULL},
  20. {AT_END, NULL, NULL}};
  21. unsigned int mstrlen(const char *s)
  22. {
  23. const char *ss = s;
  24. while (*ss)
  25. ss++;
  26. return ss - s;
  27. }
  28. int mstrncmp(const char *s1, const char *s2, int n)
  29. {
  30. const unsigned char *c1 = (const unsigned char *)s1;
  31. const unsigned char *c2 = (const unsigned char *)s2;
  32. unsigned char ch;
  33. int d = 0;
  34. while (n--)
  35. {
  36. d = (int)(ch = *c1++) - (int)*c2++;
  37. if (d || !ch)
  38. break;
  39. }
  40. return d;
  41. }
  42. /* 指令执行函数 */
  43. AT_STATUS at_cmd_test(unsigned char *p, unsigned char len)
  44. {
  45. printf("AT+OK\r\n");
  46. return AT_SUCCESS;
  47. }
  48. AT_STATUS at_cmd_ip(unsigned char *p, unsigned char len)
  49. {
  50. ip4_addr_t int_addr;
  51. char ip_address[16];
  52. if (*p == '?')
  53. {
  54. printf("config ip address:%s\r\n", inet_ntoa(config_get()->ip));
  55. }
  56. else
  57. {
  58. if (len >= sizeof(ip_address))
  59. {
  60. printf("AT+ERR\r\n");
  61. return AT_ERR;
  62. }
  63. strncpy(ip_address, (const char *)p, len);
  64. ip_address[len] = 0;
  65. printf("ip_address:%s\r\n", ip_address);
  66. inet_aton(ip_address, &int_addr);
  67. printf("int_addr:%d\r\n", int_addr.addr);
  68. // printf("ip address:%s\r\n", p);
  69. printf("AT+OK\r\n");
  70. }
  71. return AT_SUCCESS;
  72. }
  73. /* 查找指令表中对应的指令 */
  74. unsigned char AT_cmd_search(unsigned char *p, unsigned char len)
  75. {
  76. unsigned char ret = 0;
  77. // unsigned char *pstr;
  78. unsigned char i, n;
  79. for (i = 1; at_cmd_func[i].cmd != AT_END; i++)
  80. {
  81. n = mstrlen((const char *)at_cmd_func[i].str);
  82. if (!mstrncmp((const char *)p, (const char *)at_cmd_func[i].str, n))
  83. {
  84. ret = i;
  85. break;
  86. }
  87. }
  88. return ret;
  89. }
  90. /* AT指令解析 */
  91. AT_STATUS at_cmd_parse(unsigned char *p, unsigned char len)
  92. {
  93. AT_STATUS ret = AT_SUCCESS;
  94. unsigned char index = 0;
  95. if (len < 4)
  96. return AT_ERR; /* 不符合指令最小长度 */
  97. if ((p[0] == 'A') && (p[1] == 'T') && (p[len - 2] == 0x0D) && (p[len - 1] == 0x0A))
  98. {
  99. if (len == 4)
  100. { /* 测试指令 */
  101. if (at_cmd_func[AT_CMD_TEST].cb != NULL)
  102. at_cmd_func[AT_CMD_TEST].cb(NULL, 0); /* 执行测试指令 */
  103. }
  104. else if (p[2] == '+')
  105. { /* 执行指令解析 */
  106. index = AT_cmd_search(p, len); /* 查找匹配的执行指令,0-已匹配,!0-未匹配 */
  107. if (index)
  108. {
  109. if (at_cmd_func[index].cb != NULL)
  110. { /* 判断指令对应执行函数是否存在 */
  111. unsigned char n;
  112. n = mstrlen((const char *)at_cmd_func[index].str);
  113. ret = at_cmd_func[index].cb(p + n, len - n - 2); /* 执行对应的指令函数, p+n:将指令参数传输执行函数,len-n-2:指令参数有效长度 */
  114. }
  115. else
  116. ret = AT_ERR_FUN_UNUSED; /* 没有可执行函数 */
  117. }
  118. else
  119. {
  120. ret = AT_ERR_UNINVAIL; /* 未找到匹配的指令 */
  121. }
  122. }
  123. }
  124. else
  125. { /* 格式不匹配 */
  126. return AT_ERR;
  127. }
  128. return ret;
  129. }
  130. void at_cmd_processer_push_data(uint8_t rxdata)
  131. {
  132. if (!s_at_cmd_uart_rx_buf_is_processing)
  133. {
  134. if (s_at_cmd_uart_rx_off < at_processer_rx_buf_size)
  135. {
  136. at_rx_buf[s_at_cmd_uart_rx_off] = rxdata;
  137. s_at_cmd_uart_rx_off++;
  138. }
  139. }
  140. }
  141. void at_cmd_processer_try_process_data(void)
  142. {
  143. /**
  144. * @brief
  145. * modbus协议3.5
  146. */
  147. if (s_at_cmd_uart_rx_off != 0)
  148. {
  149. uint16_t modbus_uart_rx_off_before = s_at_cmd_uart_rx_off;
  150. HAL_Delay(1);
  151. sys_critical_enter();
  152. if (s_at_cmd_uart_rx_off == modbus_uart_rx_off_before)
  153. {
  154. s_at_cmd_uart_rx_buf_is_processing = true;
  155. }
  156. sys_critical_exit();
  157. if (s_at_cmd_uart_rx_buf_is_processing)
  158. {
  159. if (at_cmd_parse(at_rx_buf, s_at_cmd_uart_rx_off) != AT_SUCCESS)
  160. {
  161. printf("at cmd parse error\r\n");
  162. }
  163. sys_critical_enter();
  164. s_at_cmd_uart_rx_off = 0;
  165. s_at_cmd_uart_rx_buf_is_processing = false;
  166. sys_critical_exit();
  167. }
  168. }
  169. }