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.

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