7 changed files with 480 additions and 356 deletions
@ -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)); |
|
||||
|
|
||||
/**
|
|
||||
* @brief 注册响应前缀和回调 |
|
||||
* @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)); |
|
||||
|
|
||||
/**
|
void at_parser_feed_byte(at_parser_t *parser, |
||||
* @brief 将串口接收到的字节喂给解析器 (需在中断或轮询中调用) |
uint8_t data); |
||||
* @param parser 解析器实例 |
|
||||
* @param data 接收到的字节 |
/*
|
||||
|
* 接收处理函数。 |
||||
|
* 建议放在 AT 接收线程里循环调用。 |
||||
|
* |
||||
|
* 注意: |
||||
|
* 这个函数内部会调用 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