|
|
|
|
#include "ATParser.h"
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* AT 行检查函数,给 com 模块使用。
|
|
|
|
|
*
|
|
|
|
|
* 返回值规则:
|
|
|
|
|
* > 0:发现完整一行,返回这一行长度
|
|
|
|
|
* = 0:数据还不完整
|
|
|
|
|
* < 0:数据异常,需要丢弃同步
|
|
|
|
|
*/
|
|
|
|
|
static int at_com_check(char *_pBuffer, uint32_t _iSize)
|
|
|
|
|
{
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < _iSize; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_pBuffer[i] == '\n')
|
|
|
|
|
{
|
|
|
|
|
return (int)(i + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i >= AT_LINE_MAX_LEN - 1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_iSize >= AT_LINE_MAX_LEN - 1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool at_starts_with(const char *str, const char *prefix)
|
|
|
|
|
{
|
|
|
|
|
return strncmp(str, prefix, strlen(prefix)) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int at_trim_line(char *line, int len)
|
|
|
|
|
{
|
|
|
|
|
int start = 0;
|
|
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
|
{
|
|
|
|
|
char ch = line[len - 1];
|
|
|
|
|
|
|
|
|
|
if (ch == '\r' || ch == '\n' || ch == '\0')
|
|
|
|
|
{
|
|
|
|
|
len--;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (start < len)
|
|
|
|
|
{
|
|
|
|
|
char ch = line[start];
|
|
|
|
|
|
|
|
|
|
if (ch == '\r' || ch == '\n' || ch == ' ')
|
|
|
|
|
{
|
|
|
|
|
start++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (start > 0)
|
|
|
|
|
{
|
|
|
|
|
memmove(line, &line[start], (size_t)(len - start));
|
|
|
|
|
len -= start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line[len] = '\0';
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void at_parser_handle_line(at_parser_t *parser, char *line, int len)
|
|
|
|
|
{
|
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
|
|
len = at_trim_line(line, len);
|
|
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 标准成功响应
|
|
|
|
|
*/
|
|
|
|
|
if (strcmp(line, "OK") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
parser->m_eLastStatus = AT_OK;
|
|
|
|
|
parser->m_bWaitingResponse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 标准失败响应
|
|
|
|
|
*/
|
|
|
|
|
if (strcmp(line, "ERROR") == 0)
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
parser->m_eLastStatus = AT_ERROR;
|
|
|
|
|
parser->m_bWaitingResponse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 常见扩展错误。
|
|
|
|
|
* 例如:
|
|
|
|
|
* +CME ERROR: 10
|
|
|
|
|
* +CMS ERROR: 500
|
|
|
|
|
*/
|
|
|
|
|
if (at_starts_with(line, "+CME ERROR:") ||
|
|
|
|
|
at_starts_with(line, "+CMS ERROR:"))
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
parser->m_eLastStatus = AT_ERROR;
|
|
|
|
|
parser->m_bWaitingResponse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 常见 busy 响应。
|
|
|
|
|
*/
|
|
|
|
|
if (strstr(line, "BUSY") != NULL ||
|
|
|
|
|
strstr(line, "busy") != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
parser->m_eLastStatus = AT_BUSY;
|
|
|
|
|
parser->m_bWaitingResponse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 注册响应或 URC。
|
|
|
|
|
*
|
|
|
|
|
* 例如:
|
|
|
|
|
* +CSQ: 20,99
|
|
|
|
|
* +CREG: 0,1
|
|
|
|
|
* +CMTI: "SM",1
|
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < parser->m_u8ResponseCount; i++)
|
|
|
|
|
{
|
|
|
|
|
if (at_starts_with(line, parser->m_stResponses[i].prefix))
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_stResponses[i].callback != NULL)
|
|
|
|
|
{
|
|
|
|
|
parser->m_stResponses[i].callback(line, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 未知行默认忽略。
|
|
|
|
|
*
|
|
|
|
|
* 有些模块会回显命令:
|
|
|
|
|
* AT+CSQ
|
|
|
|
|
* +CSQ: 20,99
|
|
|
|
|
* OK
|
|
|
|
|
*
|
|
|
|
|
* 这里的 AT+CSQ 就可以直接忽略。
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int at_parser_init(at_parser_t *parser,
|
|
|
|
|
void (*send_cb)(const uint8_t *data, uint16_t len),
|
|
|
|
|
uint32_t (*tick_cb)(void))
|
|
|
|
|
{
|
|
|
|
|
memset(parser, 0, sizeof(at_parser_t));
|
|
|
|
|
|
|
|
|
|
parser->m_pfHwSend = send_cb;
|
|
|
|
|
parser->m_pfGetTick = tick_cb;
|
|
|
|
|
parser->m_eLastStatus = AT_UNKNOWN;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* com 只负责接收缓存和按行分包。
|
|
|
|
|
*
|
|
|
|
|
* check : at_com_check
|
|
|
|
|
* decode : NULL
|
|
|
|
|
* send : NULL
|
|
|
|
|
*
|
|
|
|
|
* AT 发送直接走 m_pfHwSend。
|
|
|
|
|
*/
|
|
|
|
|
parser->m_ptCom = rd_ComCreate(at_com_check,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
AT_RX_BUFFER_SIZE,
|
|
|
|
|
parser);
|
|
|
|
|
|
|
|
|
|
if (parser->m_ptCom == NULL)
|
|
|
|
|
{
|
|
|
|
|
return RD_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RD_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void at_parser_deinit(at_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_ptCom != NULL)
|
|
|
|
|
{
|
|
|
|
|
rd_ComDelete(parser->m_ptCom);
|
|
|
|
|
parser->m_ptCom = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool at_parser_register_response(at_parser_t *parser,
|
|
|
|
|
const char *prefix,
|
|
|
|
|
at_response_cbk cb)
|
|
|
|
|
{
|
|
|
|
|
if (parser->m_u8ResponseCount >= AT_MAX_RESPONSES)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser->m_stResponses[parser->m_u8ResponseCount].prefix = prefix;
|
|
|
|
|
parser->m_stResponses[parser->m_u8ResponseCount].callback = cb;
|
|
|
|
|
parser->m_u8ResponseCount++;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void at_parser_feed(at_parser_t *parser,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
uint16_t len)
|
|
|
|
|
{
|
|
|
|
|
rd_ComRecvProc(parser->m_ptCom, (const char *)data, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void at_parser_feed_byte(at_parser_t *parser,
|
|
|
|
|
uint8_t data)
|
|
|
|
|
{
|
|
|
|
|
at_parser_feed(parser, &data, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int at_parser_recv_process(at_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = rd_ComRead(parser->m_ptCom,
|
|
|
|
|
parser->m_acLineBuffer,
|
|
|
|
|
AT_LINE_MAX_LEN - 1);
|
|
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
|
{
|
|
|
|
|
parser->m_acLineBuffer[ret] = '\0';
|
|
|
|
|
at_parser_handle_line(parser, parser->m_acLineBuffer, ret);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
at_status_t at_parser_send_cmd(at_parser_t *parser,
|
|
|
|
|
const char *cmd,
|
|
|
|
|
uint32_t timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
uint8_t tx_buf[AT_CMD_MAX_LEN + 2];
|
|
|
|
|
size_t cmd_len;
|
|
|
|
|
uint16_t send_len;
|
|
|
|
|
|
|
|
|
|
if (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
return AT_BUSY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd_len = strlen(cmd);
|
|
|
|
|
|
|
|
|
|
if (cmd_len + 2 > sizeof(tx_buf))
|
|
|
|
|
{
|
|
|
|
|
return AT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 关键:
|
|
|
|
|
* 先设置等待状态,再发送命令。
|
|
|
|
|
* 避免模块响应过快,OK 在等待状态建立前就返回。
|
|
|
|
|
*/
|
|
|
|
|
parser->m_bWaitingResponse = true;
|
|
|
|
|
parser->m_eLastStatus = AT_UNKNOWN;
|
|
|
|
|
parser->m_u32TimeoutMs = timeout_ms;
|
|
|
|
|
parser->m_u32WaitStartTime = parser->m_pfGetTick();
|
|
|
|
|
|
|
|
|
|
memcpy(tx_buf, cmd, cmd_len);
|
|
|
|
|
|
|
|
|
|
send_len = (uint16_t)cmd_len;
|
|
|
|
|
tx_buf[send_len++] = '\r';
|
|
|
|
|
tx_buf[send_len++] = '\n';
|
|
|
|
|
|
|
|
|
|
parser->m_pfHwSend(tx_buf, send_len);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 这里不调用 at_parser_recv_process()。
|
|
|
|
|
*
|
|
|
|
|
* 原因:
|
|
|
|
|
* at_parser_recv_process() 内部会调用 rd_ComRead(),
|
|
|
|
|
* rd_ComRead() 在无数据时会阻塞等待信号量。
|
|
|
|
|
*
|
|
|
|
|
* 所以推荐模型是:
|
|
|
|
|
* 1. 一个 AT 接收任务一直跑 at_parser_recv_process()
|
|
|
|
|
* 2. 当前任务在这里等待 m_bWaitingResponse 变 false
|
|
|
|
|
*/
|
|
|
|
|
while (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
at_parser_process(parser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parser->m_eLastStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void at_parser_send_raw(at_parser_t *parser,
|
|
|
|
|
const uint8_t *data,
|
|
|
|
|
uint16_t len)
|
|
|
|
|
{
|
|
|
|
|
parser->m_pfHwSend(data, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void at_parser_process(at_parser_t *parser)
|
|
|
|
|
{
|
|
|
|
|
uint32_t now;
|
|
|
|
|
uint32_t elapsed;
|
|
|
|
|
|
|
|
|
|
if (parser->m_bWaitingResponse)
|
|
|
|
|
{
|
|
|
|
|
now = parser->m_pfGetTick();
|
|
|
|
|
elapsed = now - parser->m_u32WaitStartTime;
|
|
|
|
|
|
|
|
|
|
if (elapsed > parser->m_u32TimeoutMs)
|
|
|
|
|
{
|
|
|
|
|
parser->m_bWaitingResponse = false;
|
|
|
|
|
parser->m_eLastStatus = AT_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|