77 lines
1.8 KiB
C
77 lines
1.8 KiB
C
/**
|
||
******************************************************************************
|
||
* @file cmd_parser.h
|
||
* @brief ASCII指令解析模块头文件
|
||
* @author Application Layer
|
||
* @version 1.2
|
||
******************************************************************************
|
||
* @attention
|
||
* 本模块实现ASCII文本指令的解析和处理
|
||
* 指令格式: $CMD,param1,param2*CS\r\n
|
||
* 支持异或校验,FF为调试特权后门
|
||
* 包含完善的安全防护机制
|
||
*
|
||
* 修订历史:
|
||
* v1.2 - 增加响应回调机制,支持多端口路由
|
||
******************************************************************************
|
||
*/
|
||
|
||
#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;
|
||
uint8_t source_port;
|
||
bool valid;
|
||
bool skip_checksum;
|
||
} cmd_frame_t;
|
||
|
||
typedef void (*cmd_response_callback_t)(uint8_t source_port, const char *response);
|
||
|
||
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);
|
||
|
||
void CmdParser_SetResponseCallback(cmd_response_callback_t callback);
|
||
|
||
void CmdParser_SetSourcePort(uint8_t port_id);
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif
|