2026-03-27 10:09:13 +08:00
|
|
|
|
/**
|
|
|
|
|
|
******************************************************************************
|
|
|
|
|
|
* @file cmd_parser.h
|
|
|
|
|
|
* @brief ASCII指令解析模块头文件
|
|
|
|
|
|
* @author Application Layer
|
2026-03-27 16:21:00 +08:00
|
|
|
|
* @version 1.2
|
2026-03-27 10:09:13 +08:00
|
|
|
|
******************************************************************************
|
|
|
|
|
|
* @attention
|
|
|
|
|
|
* 本模块实现ASCII文本指令的解析和处理
|
|
|
|
|
|
* 指令格式: $CMD,param1,param2*CS\r\n
|
|
|
|
|
|
* 支持异或校验,FF为调试特权后门
|
|
|
|
|
|
* 包含完善的安全防护机制
|
2026-03-27 16:21:00 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 修订历史:
|
|
|
|
|
|
* v1.2 - 增加响应回调机制,支持多端口路由
|
2026-03-27 10:09:13 +08:00
|
|
|
|
******************************************************************************
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __CMD_PARSER_H
|
|
|
|
|
|
#define __CMD_PARSER_H
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define CMD_MAX_LEN 8
|
|
|
|
|
|
#define PARAM_MAX_LEN 32
|
|
|
|
|
|
#define PARSE_TIMEOUT_MS 1000
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
|
CMD_CODE_UNKNOWN = 0,
|
|
|
|
|
|
CMD_CODE_RL = 1,
|
|
|
|
|
|
CMD_CODE_DI = 2,
|
|
|
|
|
|
CMD_CODE_ECHO = 3,
|
|
|
|
|
|
CMD_CODE_FWD = 4
|
|
|
|
|
|
} cmd_code_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
|
char cmd[CMD_MAX_LEN];
|
|
|
|
|
|
char param1[PARAM_MAX_LEN];
|
|
|
|
|
|
char param2[PARAM_MAX_LEN];
|
|
|
|
|
|
uint8_t received_cs;
|
|
|
|
|
|
uint8_t calculated_cs;
|
2026-03-27 16:21:00 +08:00
|
|
|
|
uint8_t source_port;
|
2026-03-27 10:09:13 +08:00
|
|
|
|
bool valid;
|
|
|
|
|
|
bool skip_checksum;
|
|
|
|
|
|
} cmd_frame_t;
|
|
|
|
|
|
|
2026-03-27 16:21:00 +08:00
|
|
|
|
typedef void (*cmd_response_callback_t)(uint8_t source_port, const char *response);
|
|
|
|
|
|
|
2026-03-27 10:09:13 +08:00
|
|
|
|
void CmdParser_Init(void);
|
|
|
|
|
|
|
|
|
|
|
|
void CmdParser_Task(void);
|
|
|
|
|
|
|
|
|
|
|
|
void CmdParser_FeedByte(uint8_t byte, uint32_t current_tick);
|
|
|
|
|
|
|
|
|
|
|
|
bool CmdParser_HasCompleteFrame(cmd_frame_t *frame);
|
|
|
|
|
|
|
|
|
|
|
|
void CmdParser_Acknowledge(void);
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t CmdParser_GetErrorCount(void);
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t CmdParser_GetValidCount(void);
|
|
|
|
|
|
|
2026-03-27 16:21:00 +08:00
|
|
|
|
void CmdParser_SetResponseCallback(cmd_response_callback_t callback);
|
|
|
|
|
|
|
|
|
|
|
|
void CmdParser_SetSourcePort(uint8_t port_id);
|
|
|
|
|
|
|
2026-03-27 10:09:13 +08:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|