远程通信协议更改为 状态机处理

This commit is contained in:
2026-05-11 15:27:25 +08:00
parent 773b506f40
commit 6e3f7ade3d
5 changed files with 223 additions and 171 deletions

View File

@@ -1,13 +1,13 @@
/* P0 测试remoteDisplay 协议核心
* 目标:
* - 校验 CRC 纯函数行为calc_crc
* - 校验 RemoDispBus 帧解析边界parse_frame
* - 校验 Protocol_ByteHandler 状态机解析行为
*/
#include <stdint.h>
#include <string.h>
#include "test_common.h"
/* 直接包含 .c 以访问 static 函数calc_crc / parse_frame */
#include "../src/remoteDisplay.c"
#include "../src/remoteDisplayProtocol.h"
/* 用例1CRC 计算
* - 空数据长度应返回 0
@@ -21,49 +21,64 @@ static int test_calc_crc(void)
return 0;
}
/* 用例2合法帧解析
* 验证:
* - parse_frame 返回成功
* - cmd/data/data_len/consume 与期望一致
/* 用例2Protocol_ByteHandler 基本帧解析
* 验证:完整 RemoDispBus 帧按字节输入后,状态机完成帧解析并保持正确 cmd/data
*/
static int test_parse_frame_ok(void)
static int test_protocol_byte_handler_ok(void)
{
uint8_t cmd = 0;
const uint8_t* data = NULL;
unsigned int data_len = 0;
unsigned int consume = 0;
protocol_instance_t proto_ins;
const uint8_t payload[] = {0x10, 0x20};
const uint8_t frame[] = {0xAA, 0x03, 0x00, 0x02, 0x10, 0x20, (uint8_t)(0x10 ^ 0x20)};
const uint8_t frame[] = {
TAG_CLIENT,
CMD_KEY,
0x00,
(uint8_t)sizeof(payload),
payload[0],
payload[1],
(uint8_t)(payload[0] ^ payload[1])
};
ASSERT_EQ_INT(1, parse_frame(frame, sizeof(frame), &cmd, &data, &data_len, &consume));
ASSERT_EQ_INT(0x03, cmd);
ASSERT_EQ_INT(2, (int)data_len);
ASSERT_EQ_INT((int)sizeof(frame), (int)consume);
ASSERT_EQ_INT(payload[0], data[0]);
ASSERT_EQ_INT(payload[1], data[1]);
protocol_init(&proto_ins);
ASSERT_EQ_INT(PROTO_STATE_WAIT_TAG, proto_ins.state);
ASSERT_EQ_INT(0, proto_ins.frame_done_flag);
for (unsigned int i = 0; i < sizeof(frame); ++i) {
protocol_byte_handler(&proto_ins, frame[i]);
}
ASSERT_EQ_INT(1, proto_ins.frame_done_flag);
ASSERT_EQ_INT(CMD_KEY, proto_ins.recv_cmd);
ASSERT_EQ_INT((int)sizeof(payload), (int)proto_ins.expect_data_len);
ASSERT_EQ_INT((int)sizeof(payload), (int)proto_ins.recv_len);
ASSERT_EQ_INT(payload[0], proto_ins.recv_buf[0]);
ASSERT_EQ_INT(payload[1], proto_ins.recv_buf[1]);
return 0;
}
/* 用例3非法输入集合
* - TAG 非客户端 TAG
* - 帧长度不足(截断)
* - CRC 错误
* 期望均解析失败(返回 0
/* 用例3Protocol_ByteHandler CRC 错误处理
* 验证CRC 校验失败后状态机重置为 WAIT_TAG不置完成标志
*/
static int test_parse_frame_invalid_cases(void)
static int test_protocol_byte_handler_bad_crc(void)
{
uint8_t cmd = 0;
const uint8_t* data = NULL;
unsigned int data_len = 0;
unsigned int consume = 0;
const uint8_t bad_tag[] = {0xBB, 0x03, 0x00, 0x00, 0x00};
const uint8_t short_buf[] = {0xAA, 0x03, 0x00, 0x01};
const uint8_t bad_crc[] = {0xAA, 0x02, 0x00, 0x01, 0x55, 0x00};
protocol_instance_t proto_ins;
const uint8_t payload[] = {0x10, 0x20};
const uint8_t frame[] = {
TAG_CLIENT,
CMD_KEY,
0x00,
(uint8_t)sizeof(payload),
payload[0],
payload[1],
0xFF
};
ASSERT_EQ_INT(0, parse_frame(bad_tag, sizeof(bad_tag), &cmd, &data, &data_len, &consume));
ASSERT_EQ_INT(0, parse_frame(short_buf, sizeof(short_buf), &cmd, &data, &data_len, &consume));
ASSERT_EQ_INT(0, parse_frame(bad_crc, sizeof(bad_crc), &cmd, &data, &data_len, &consume));
protocol_init(&proto_ins);
for (unsigned int i = 0; i < sizeof(frame); ++i) {
protocol_byte_handler(&proto_ins, frame[i]);
}
ASSERT_EQ_INT(PROTO_STATE_WAIT_TAG, proto_ins.state);
ASSERT_EQ_INT(0, proto_ins.frame_done_flag);
return 0;
}
@@ -71,7 +86,7 @@ static int test_parse_frame_invalid_cases(void)
int main(void)
{
if (test_calc_crc() != 0) return 1;
if (test_parse_frame_ok() != 0) return 1;
if (test_parse_frame_invalid_cases() != 0) return 1;
if (test_protocol_byte_handler_ok() != 0) return 1;
if (test_protocol_byte_handler_bad_crc() != 0) return 1;
return 0;
}