7 changed files with 480 additions and 356 deletions
@ -1,197 +1,358 @@ |
|||||
/******************************************************************************
|
#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); |
||||
|
} |
||||
|
|
||||
版权所有 (C), 2018-2099, Radkil |
if (i >= AT_LINE_MAX_LEN - 1) |
||||
|
{ |
||||
|
return -1; |
||||
|
} |
||||
|
} |
||||
|
|
||||
****************************************************************************** |
if (_iSize >= AT_LINE_MAX_LEN - 1) |
||||
文 件 名 : ATParser.c |
{ |
||||
版 本 号 : 初稿 |
return -1; |
||||
作 者 : Radkil |
} |
||||
生成日期 : 2026年3月14日星期六 |
|
||||
最近修改 : |
|
||||
功能描述 : AT命令解释器 |
|
||||
|
|
||||
修改历史 : |
return 0; |
||||
1.日 期 : 2026年3月14日星期六 |
} |
||||
作 者 : Radkil |
|
||||
修改内容 : 创建文件 |
|
||||
|
|
||||
******************************************************************************/ |
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'; |
||||
* 宏定义 * |
|
||||
*----------------------------------------------*/ |
|
||||
#include <stdio.h> |
|
||||
#include "ATParser.h" |
|
||||
|
|
||||
// 内部辅助函数:检查字符串是否以某前缀开头
|
return len; |
||||
static bool starts_with(const char *str, const char *prefix) { |
|
||||
if (!str || !prefix) return false; |
|
||||
return strncmp(str, prefix, strlen(prefix)) == 0; |
|
||||
} |
} |
||||
|
|
||||
void at_parser_init(at_parser_t *parser, |
static void at_parser_handle_line(at_parser_t *parser, char *line, int len) |
||||
void (*send_cb)(const uint8_t*, uint16_t), |
{ |
||||
uint32_t (*tick_cb)(void)) { |
uint8_t i; |
||||
memset(parser, 0, sizeof(at_parser_t)); |
|
||||
parser->hw_send = send_cb; |
len = at_trim_line(line, len); |
||||
parser->hw_get_tick = tick_cb; |
|
||||
parser->response_count = 0; |
if (len <= 0) |
||||
parser->last_status = AT_UNKNOWN; |
{ |
||||
|
return; |
||||
} |
} |
||||
|
|
||||
bool at_parser_register_response(at_parser_t *parser, const char *prefix, void (*cb)(const char*, int)) { |
/*
|
||||
if (parser->response_count >= AT_MAX_RESPONSES) return false; |
* 标准成功响应 |
||||
parser->responses[parser->response_count].prefix = prefix; |
*/ |
||||
parser->responses[parser->response_count].callback = cb; |
if (strcmp(line, "OK") == 0) |
||||
parser->response_count++; |
{ |
||||
return true; |
if (parser->m_bWaitingResponse) |
||||
|
{ |
||||
|
parser->m_eLastStatus = AT_OK; |
||||
|
parser->m_bWaitingResponse = false; |
||||
} |
} |
||||
|
|
||||
void at_parser_feed(at_parser_t *p, uint8_t data) { |
return; |
||||
// 存入环形缓冲区
|
|
||||
uint16_t next_head = (p->head + 1) % AT_RX_BUFFER_SIZE; |
|
||||
if (next_head != p->tail) { // 缓冲区未满
|
|
||||
p->rx_buffer[p->head] = data; |
|
||||
p->head = next_head; |
|
||||
} |
|
||||
|
|
||||
// 简单的状态机解析行结束符 (\n)
|
|
||||
// 注意:AT 命令通常以 \r\n 结尾,这里简化处理,遇到 \n 即认为一行结束
|
|
||||
// 更严谨的做法是检测 \r\n 序列
|
|
||||
if (data == '\n') { |
|
||||
if (p->cmd_len > 0 && p->cmd_len < AT_CMD_MAX_LEN) { |
|
||||
p->cmd_buffer[p->cmd_len] = '\0'; // 封口
|
|
||||
|
|
||||
// 1. 检查是否是标准响应 (OK / ERROR)
|
|
||||
if (strcmp(p->cmd_buffer, "OK") == 0 || strcmp(p->cmd_buffer, "\rOK") == 0) { |
|
||||
if (p->is_waiting_response) { |
|
||||
p->last_status = AT_OK; |
|
||||
p->is_waiting_response = false; |
|
||||
} |
|
||||
} else if (strcmp(p->cmd_buffer, "ERROR") == 0 || strcmp(p->cmd_buffer, "\rERROR") == 0) { |
|
||||
if (p->is_waiting_response) { |
|
||||
p->last_status = AT_ERROR; |
|
||||
p->is_waiting_response = false; |
|
||||
} |
|
||||
} else { |
|
||||
// 2. 检查注册的自定义前缀 (URC 或 带参数响应)
|
|
||||
for (int i = 0; i < p->response_count; i++) { |
|
||||
if (starts_with(p->cmd_buffer, p->responses[i].prefix)) { |
|
||||
if (p->responses[i].callback) { |
|
||||
// 去掉前缀部分,只传递参数部分给回调 (可选优化)
|
|
||||
// 这里传递整行,用户回调内自行解析
|
|
||||
p->responses[i].callback(p->cmd_buffer, strlen(p->cmd_buffer)); |
|
||||
} |
|
||||
// 注意:如果是 URC (异步消息),不应停止等待标志,除非它是命令的直接响应
|
|
||||
// 这里简化逻辑:如果是等待响应期间收到的非 OK/ERROR,通常视为中间数据
|
|
||||
break; |
|
||||
} |
} |
||||
|
|
||||
|
/*
|
||||
|
* 标准失败响应 |
||||
|
*/ |
||||
|
if (strcmp(line, "ERROR") == 0) |
||||
|
{ |
||||
|
if (parser->m_bWaitingResponse) |
||||
|
{ |
||||
|
parser->m_eLastStatus = AT_ERROR; |
||||
|
parser->m_bWaitingResponse = false; |
||||
} |
} |
||||
|
|
||||
// 如果正在等待响应,且收到了非 OK/ERROR 的未知行,通常忽略或视为中间数据
|
return; |
||||
// 某些模块会在 OK 之前返回多行数据
|
|
||||
} |
} |
||||
|
|
||||
// 重置行缓冲
|
/*
|
||||
p->cmd_len = 0; |
* 常见扩展错误。 |
||||
} else { |
* 例如: |
||||
p->cmd_len = 0; |
* +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; |
||||
} |
} |
||||
} else if (data != '\r') { |
|
||||
// 忽略 \r,只存有效字符,防止缓冲溢出
|
return; |
||||
if (p->cmd_len < AT_CMD_MAX_LEN - 1) { |
|
||||
p->cmd_buffer[p->cmd_len++] = data; |
|
||||
} |
} |
||||
|
|
||||
|
/*
|
||||
|
* 常见 busy 响应。 |
||||
|
*/ |
||||
|
if (strstr(line, "BUSY") != NULL || |
||||
|
strstr(line, "busy") != NULL) |
||||
|
{ |
||||
|
if (parser->m_bWaitingResponse) |
||||
|
{ |
||||
|
parser->m_eLastStatus = AT_BUSY; |
||||
|
parser->m_bWaitingResponse = false; |
||||
} |
} |
||||
|
|
||||
|
return; |
||||
} |
} |
||||
|
|
||||
at_status_t at_parser_send_cmd(at_parser_t *p, const char *cmd, uint32_t timeout_ms) { |
/*
|
||||
if (!p->hw_send) return AT_ERROR; |
* 注册响应或 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); |
||||
|
} |
||||
|
|
||||
// 1. 发送命令 + \r\n
|
return; |
||||
uint8_t tx_buf[AT_CMD_MAX_LEN + 4]; |
} |
||||
uint16_t len = strlen(cmd); |
} |
||||
memcpy(tx_buf, cmd, len); |
|
||||
tx_buf[len++] = '\r'; |
|
||||
tx_buf[len++] = '\n'; |
|
||||
|
|
||||
p->hw_send(tx_buf, len); |
/*
|
||||
|
* 未知行默认忽略。 |
||||
|
* |
||||
|
* 有些模块会回显命令: |
||||
|
* AT+CSQ |
||||
|
* +CSQ: 20,99 |
||||
|
* OK |
||||
|
* |
||||
|
* 这里的 AT+CSQ 就可以直接忽略。 |
||||
|
*/ |
||||
|
} |
||||
|
|
||||
// 2. 设置等待状态
|
int at_parser_init(at_parser_t *parser, |
||||
p->is_waiting_response = true; |
void (*send_cb)(const uint8_t *data, uint16_t len), |
||||
p->last_status = AT_UNKNOWN; |
uint32_t (*tick_cb)(void)) |
||||
p->timeout_ms = timeout_ms; |
{ |
||||
p->wait_start_time = p->hw_get_tick(); |
memset(parser, 0, sizeof(at_parser_t)); |
||||
|
|
||||
// 3. 阻塞等待 (或在外部轮询 process)
|
parser->m_pfHwSend = send_cb; |
||||
// 为了兼容性,这里使用简单的轮询等待,实际嵌入式系统中建议配合 RTOS 信号量
|
parser->m_pfGetTick = tick_cb; |
||||
while (p->is_waiting_response) { |
parser->m_eLastStatus = AT_UNKNOWN; |
||||
// 在等待期间,必须不断调用 feed 或让中断去 feed
|
|
||||
// 这里假设 feed 是在中断中调用的,所以这里只需要检查超时
|
/*
|
||||
|
* 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; |
||||
|
} |
||||
|
|
||||
uint32_t current_tick = p->hw_get_tick(); |
return RD_SUCCESS; |
||||
// 处理时间回绕
|
} |
||||
uint32_t elapsed = (current_tick >= p->wait_start_time) ? |
|
||||
(current_tick - p->wait_start_time) : |
|
||||
(0xFFFFFFFF - p->wait_start_time + current_tick); |
|
||||
|
|
||||
if (elapsed > timeout_ms) { |
void at_parser_deinit(at_parser_t *parser) |
||||
p->is_waiting_response = false; |
{ |
||||
p->last_status = AT_TIMEOUT; |
if (parser->m_ptCom != NULL) |
||||
break; |
{ |
||||
|
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; |
||||
} |
} |
||||
|
|
||||
// 短暂延时,避免死循环占用 CPU (根据平台调整)
|
void at_parser_feed(at_parser_t *parser, |
||||
// 如果是 RTOS,这里应该是 osDelay(1);
|
const uint8_t *data, |
||||
// 如果是裸机,确保中断能打断此处
|
uint16_t len) |
||||
|
{ |
||||
|
rd_ComRecvProc(parser->m_ptCom, (const char *)data, len); |
||||
} |
} |
||||
|
|
||||
return p->last_status; |
void at_parser_feed_byte(at_parser_t *parser, |
||||
|
uint8_t data) |
||||
|
{ |
||||
|
at_parser_feed(parser, &data, 1); |
||||
} |
} |
||||
|
|
||||
void at_parser_send_raw(at_parser_t *p, const uint8_t *data, uint16_t len) { |
int at_parser_recv_process(at_parser_t *parser) |
||||
if (p->hw_send) { |
{ |
||||
p->hw_send(data, len); |
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; |
||||
} |
} |
||||
|
|
||||
void at_parser_process(at_parser_t *p) { |
at_status_t at_parser_send_cmd(at_parser_t *parser, |
||||
// 主要用于处理超时逻辑的非阻塞版本
|
const char *cmd, |
||||
// 如果使用了上面的阻塞式 send_cmd,此函数主要用于看门狗或额外逻辑
|
uint32_t timeout_ms) |
||||
if (p->is_waiting_response) { |
{ |
||||
uint32_t current_tick = p->hw_get_tick(); |
uint8_t tx_buf[AT_CMD_MAX_LEN + 2]; |
||||
uint32_t elapsed = (current_tick >= p->wait_start_time) ? |
size_t cmd_len; |
||||
(current_tick - p->wait_start_time) : |
uint16_t send_len; |
||||
(0xFFFFFFFF - p->wait_start_time + current_tick); |
|
||||
|
if (parser->m_bWaitingResponse) |
||||
|
{ |
||||
|
return AT_BUSY; |
||||
|
} |
||||
|
|
||||
|
cmd_len = strlen(cmd); |
||||
|
|
||||
|
if (cmd_len + 2 > sizeof(tx_buf)) |
||||
|
{ |
||||
|
return AT_ERROR; |
||||
|
} |
||||
|
|
||||
if (elapsed > p->timeout_ms) { |
/*
|
||||
p->is_waiting_response = false; |
* 关键: |
||||
p->last_status = AT_TIMEOUT; |
* 先设置等待状态,再发送命令。 |
||||
|
* 避免模块响应过快,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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|||||
@ -1,184 +1,100 @@ |
|||||
/******************************************************************************
|
#ifndef __AT_PARSER_H__ |
||||
|
#define __AT_PARSER_H__ |
||||
版权所有 (C), 2018-2099, Radkil |
|
||||
|
|
||||
****************************************************************************** |
|
||||
文 件 名 : ATParser.h |
|
||||
版 本 号 : 初稿 |
|
||||
作 者 : Radkil |
|
||||
生成日期 : 2026年3月14日星期六 |
|
||||
最近修改 : |
|
||||
功能描述 : ATParser.c 的头文件 |
|
||||
|
|
||||
修改历史 : |
|
||||
1.日 期 : 2026年3月14日星期六 |
|
||||
作 者 : Radkil |
|
||||
修改内容 : 创建文件 |
|
||||
|
|
||||
******************************************************************************/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 外部变量说明 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 外部函数原型说明 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 内部函数原型说明 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 全局变量 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 模块级变量 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 常量定义 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
/*----------------------------------------------*
|
|
||||
* 宏定义 * |
|
||||
*----------------------------------------------*/ |
|
||||
|
|
||||
#ifndef __ATPARSER_H__ |
|
||||
#define __ATPARSER_H__ |
|
||||
|
|
||||
|
|
||||
#ifdef __cplusplus |
#ifdef __cplusplus |
||||
#if __cplusplus |
|
||||
extern "C" { |
extern "C" { |
||||
#endif |
#endif |
||||
#endif /* __cplusplus */ |
|
||||
|
|
||||
|
|
||||
/*==============================================*
|
|
||||
* include header files * |
|
||||
*----------------------------------------------*/ |
|
||||
#include <stdint.h> |
#include <stdint.h> |
||||
#include <string.h> |
|
||||
#include <stdbool.h> |
#include <stdbool.h> |
||||
#include <stdlib.h> |
#include "com.h" |
||||
|
|
||||
/*==============================================*
|
#define AT_RX_BUFFER_SIZE 256 |
||||
* constants or macros define * |
#define AT_CMD_MAX_LEN 64 |
||||
*----------------------------------------------*/ |
#define AT_LINE_MAX_LEN 128 |
||||
#define AT_RX_BUFFER_SIZE 256 // 接收缓冲区大小
|
#define AT_MAX_RESPONSES 8 |
||||
#define AT_CMD_MAX_LEN 64 // 单条命令最大长度
|
|
||||
#define AT_MAX_RESPONSES 8 // 支持的最大响应回调数量
|
typedef enum |
||||
|
{ |
||||
/**
|
AT_OK = 0, |
||||
* @brief AT 命令执行结果状态 |
AT_ERROR, |
||||
*/ |
AT_TIMEOUT, |
||||
typedef enum { |
AT_BUSY, |
||||
AT_OK, // 成功
|
AT_UNKNOWN |
||||
AT_ERROR, // 失败 (ERROR)
|
|
||||
AT_TIMEOUT, // 超时
|
|
||||
AT_BUSY, // 模块忙
|
|
||||
AT_UNKNOWN // 未知响应
|
|
||||
} at_status_t; |
} at_status_t; |
||||
|
|
||||
/**
|
typedef void (*at_response_cbk)(const char *data, int len); |
||||
* @brief 响应结构体 |
|
||||
*/ |
|
||||
typedef struct { |
|
||||
const char *prefix; // 响应前缀,例如 "+CSQ:" 或 "OK"
|
|
||||
void (*callback)(const char *data, int len); // 匹配到该前缀时的回调
|
|
||||
} at_response_item_t; |
|
||||
|
|
||||
/**
|
typedef struct |
||||
* @brief AT 解析器主结构体 |
{ |
||||
*/ |
const char *prefix; |
||||
typedef struct { |
at_response_cbk callback; |
||||
uint8_t rx_buffer[AT_RX_BUFFER_SIZE]; // 环形缓冲区
|
} at_response_item_t; |
||||
uint16_t head; // 写指针
|
|
||||
uint16_t tail; // 读指针
|
|
||||
|
|
||||
char cmd_buffer[AT_CMD_MAX_LEN]; // 当前正在解析的命令缓冲
|
typedef struct |
||||
uint16_t cmd_len; // 当前命令长度
|
{ |
||||
|
TComCtrl *m_ptCom; |
||||
|
|
||||
bool is_waiting_response; // 是否正在等待响应
|
char m_acLineBuffer[AT_LINE_MAX_LEN]; |
||||
uint32_t wait_start_time; // 等待开始的时间戳 (ms)
|
|
||||
uint32_t timeout_ms; // 超时时间设置
|
|
||||
|
|
||||
at_status_t last_status; // 最后一次执行状态
|
volatile bool m_bWaitingResponse; |
||||
|
uint32_t m_u32WaitStartTime; |
||||
|
uint32_t m_u32TimeoutMs; |
||||
|
volatile at_status_t m_eLastStatus; |
||||
|
|
||||
// 注册的响应列表
|
at_response_item_t m_stResponses[AT_MAX_RESPONSES]; |
||||
at_response_item_t responses[AT_MAX_RESPONSES]; |
uint8_t m_u8ResponseCount; |
||||
uint8_t response_count; |
|
||||
|
|
||||
// 硬件抽象层回调 (用户需实现)
|
void (*m_pfHwSend)(const uint8_t *data, uint16_t len); |
||||
void (*hw_send)(const uint8_t *data, uint16_t len); |
uint32_t (*m_pfGetTick)(void); |
||||
uint32_t (*hw_get_tick)(void); // 获取系统毫秒时间
|
|
||||
} at_parser_t; |
} at_parser_t; |
||||
|
|
||||
/*==============================================*
|
int at_parser_init(at_parser_t *parser, |
||||
* project-wide global variables * |
void (*send_cb)(const uint8_t *data, uint16_t len), |
||||
*----------------------------------------------*/ |
uint32_t (*tick_cb)(void)); |
||||
|
|
||||
|
void at_parser_deinit(at_parser_t *parser); |
||||
|
|
||||
|
bool at_parser_register_response(at_parser_t *parser, |
||||
|
const char *prefix, |
||||
|
at_response_cbk cb); |
||||
|
|
||||
/*==============================================*
|
void at_parser_feed(at_parser_t *parser, |
||||
* routines' or functions' implementations * |
const uint8_t *data, |
||||
*----------------------------------------------*/ |
uint16_t len); |
||||
/**
|
|
||||
* @brief 初始化解析器 |
|
||||
* @param parser 解析器实例指针 |
|
||||
* @param send_cb 发送数据回调 (必须实现) |
|
||||
* @param tick_cb 获取时间戳回调 (必须实现) |
|
||||
*/ |
|
||||
void at_parser_init(at_parser_t *parser, |
|
||||
void (*send_cb)(const uint8_t*, uint16_t), |
|
||||
uint32_t (*tick_cb)(void)); |
|
||||
|
|
||||
/**
|
void at_parser_feed_byte(at_parser_t *parser, |
||||
* @brief 注册响应前缀和回调 |
uint8_t data); |
||||
* @param parser 解析器实例 |
|
||||
* @param prefix 前缀字符串 (如 "+CSQ:") |
|
||||
* @param cb 回调函数,当收到该前缀时触发 |
|
||||
* @return true 成功,false 列表已满 |
|
||||
*/ |
|
||||
bool at_parser_register_response(at_parser_t *parser, const char *prefix, void (*cb)(const char*, int)); |
|
||||
|
|
||||
/**
|
/*
|
||||
* @brief 将串口接收到的字节喂给解析器 (需在中断或轮询中调用) |
* 接收处理函数。 |
||||
* @param parser 解析器实例 |
* 建议放在 AT 接收线程里循环调用。 |
||||
* @param data 接收到的字节 |
* |
||||
|
* 注意: |
||||
|
* 这个函数内部会调用 rd_ComRead()。 |
||||
|
* 你的 rd_ComRead() 在无数据时会等待信号量,所以它适合放在单独接收任务中。 |
||||
*/ |
*/ |
||||
void at_parser_feed(at_parser_t *parser, uint8_t data); |
int at_parser_recv_process(at_parser_t *parser); |
||||
|
|
||||
/**
|
/*
|
||||
* @brief 发送 AT 命令并等待响应 (阻塞式) |
* 阻塞式发送命令。 |
||||
* @param parser 解析器实例 |
* |
||||
* @param cmd 命令字符串 (不含 \r\n,函数会自动添加) |
* 重要: |
||||
* @param timeout_ms 超时时间 |
* 使用这个函数时,必须同时有另一个 AT 接收任务在调用 at_parser_recv_process()。 |
||||
* @return AT_OK, AT_ERROR, AT_TIMEOUT 等 |
* 否则收到的数据只会进入 com 缓冲区,不会被解析成 OK / ERROR。 |
||||
*/ |
*/ |
||||
at_status_t at_parser_send_cmd(at_parser_t *parser, const char *cmd, uint32_t timeout_ms); |
at_status_t at_parser_send_cmd(at_parser_t *parser, |
||||
|
const char *cmd, |
||||
|
uint32_t timeout_ms); |
||||
|
|
||||
/**
|
void at_parser_send_raw(at_parser_t *parser, |
||||
* @brief 发送原始数据 (不等待响应,用于发送透传数据) |
const uint8_t *data, |
||||
* @param parser 解析器实例 |
uint16_t len); |
||||
* @param data 数据 |
|
||||
* @param len 长度 |
|
||||
*/ |
|
||||
void at_parser_send_raw(at_parser_t *parser, const uint8_t *data, uint16_t len); |
|
||||
|
|
||||
/**
|
|
||||
* @brief 后台处理函数 (需在主循环中调用,处理超时逻辑) |
|
||||
* @param parser 解析器实例 |
|
||||
*/ |
|
||||
void at_parser_process(at_parser_t *parser); |
void at_parser_process(at_parser_t *parser); |
||||
|
|
||||
#ifdef __cplusplus |
#ifdef __cplusplus |
||||
#if __cplusplus |
|
||||
} |
} |
||||
#endif |
#endif |
||||
#endif /* __cplusplus */ |
|
||||
|
|
||||
|
|
||||
#endif /* __ATPARSER_H__ */ |
#endif |
||||
|
|||||
Loading…
Reference in new issue