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.

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