远程通信协议更改为 状态机处理
This commit is contained in:
@@ -52,6 +52,7 @@ add_dtu_test(test_smoke tests_smoke.c)
|
||||
add_dtu_test(
|
||||
test_p0_remote_display
|
||||
test_p0_remote_display.c
|
||||
"${CMAKE_SOURCE_DIR}/src/remoteDisplayProtocol.c"
|
||||
"${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd.c"
|
||||
"${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_draw.c"
|
||||
"${CMAKE_SOURCE_DIR}/src/Drv/lcd/lcd_text.c"
|
||||
@@ -60,6 +61,7 @@ add_dtu_test(
|
||||
"${CMAKE_SOURCE_DIR}/src/TCP/tcp.c"
|
||||
"${CMAKE_SOURCE_DIR}/src/thread_utils.c"
|
||||
)
|
||||
target_compile_definitions(test_p0_remote_display PRIVATE UNIT_TEST)
|
||||
add_dtu_test(
|
||||
test_p0_utf8_hz12_get
|
||||
test_p0_utf8_hz12_get.c
|
||||
|
||||
@@ -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"
|
||||
|
||||
/* 用例1:CRC 计算
|
||||
* - 空数据长度应返回 0
|
||||
@@ -21,49 +21,64 @@ static int test_calc_crc(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 用例2:合法帧解析
|
||||
* 验证:
|
||||
* - parse_frame 返回成功
|
||||
* - cmd/data/data_len/consume 与期望一致
|
||||
/* 用例2:Protocol_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)
|
||||
/* 用例3:Protocol_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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user