Files
TencentOS-tiny/board/RHF76_STM32L072CBxx_Lora/apps/classA/at_parser.c
supowang edb2879617 first commit for opensource
first commit for opensource
2019-09-16 13:19:50 +08:00

78 lines
1.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "at_parser.h"
#include "uart-board.h"
#ifndef AT_CMD_BUF_LEN
#define AT_CMD_BUF_LEN 512
#endif
#ifndef UART_RECV_TIMEOUT
#define UART_RECV_TIMEOUT 15000
#endif
static at_uart_ops_t *uart_ops;
static at_table_t *cmd_table;
int at_parser_init(at_uart_ops_t *ops, at_table_t *table)
{
if (ops && table)
{
uart_ops = ops;
cmd_table = table;
if(ops->init)
return ops->init();
else
return 0;
}
else
return -1;
}
int at_cmd_handle(void)
{
uint8_t tmp_buf[AT_CMD_BUF_LEN];
uint16_t len = 0, i;
uint32_t n = 0;
uint16_t cmd_len = 0;
if(!uart_ops || !cmd_table)
return -1;
/*收到\r\n或超时接收完成*/
while(n++ < UART_RECV_TIMEOUT)
{
if(uart_ops->recv(tmp_buf+len, 1) == 1)
{
len++;
n = 0;
}
}
/*指令结束标志*/
if(tmp_buf[len-2] == 0x0D/*'\r'*/ && tmp_buf[len-1] == 0x0A/*'\n'*/)
{
/*长度不包含\r\n*/
len -= 2;
}
/*查询AT指令并执行*/
for(i = 0; cmd_table[i].cmd; i++)
{
cmd_len = strlen(cmd_table[i].cmd);
if(len >= cmd_len)
{
#ifdef AT_IGNORE_CASE
if(strncasecmp((const char*)cmd_table[i].cmd, (const char*)tmp_buf, cmd_len) == 0)
#else
if(memcmp((const char*)cmd_table[i].cmd, (const char*)tmp_buf, cmd_len) == 0)
#endif
{
/*将=后的参数输入*/
return cmd_table[i].func(tmp_buf + cmd_len, len - cmd_len);
}
}
}
return -1;
}