Files
smart-home-control-system/include/shs/protocol.h
T
sambu 7651d61727 fix(protocol): 补齐命令字校验(LLD TC-ERRORS-004 契约落地)
- protocol_pack 现在校验 cmd ∈ shs_cmd_t(0x00~0x06),越界返回 SHS_ERR_PARAM,
  与帧类型校验一致;头文件契约注释同步说明三种错误分支
- unit.struct 的"载荷上限"用例改用最大合法命令字(SHS_CMD_ACK)+ 保留 dev_id/value 全 1,
  并新增 2 条越界断言把契约写进测试(断言 2460 → 2462)
- 缺陷台账补记 BUG-11;docs/03、05、07 统计数字同步

回归:ctest 19/19、聚合 19 套件 / 2462 项断言 0 失败、acceptance A1~A10 25/25、覆盖率 80.3%
2026-09-14 22:47:54 +08:00

104 lines
4.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file protocol.h
* @brief 通信帧协议:打包、解析、校验与命令分发
*
* 教材对应:项目八 任务二「共用体——数据类型转换与协议解析」·
* 项目九 位运算与文件(日志记录收发帧)·
* 项目三 switch(命令分发)
*
* 帧格式(共 8 字节,模拟 Modbus 风格):
* 字节0 帧头 0x5A
* 字节1 位域头:version(3) | type(2) | flags(3)
* 字节2 设备编号
* 字节3 命令字
* 字节4~5 数据(小端)
* 字节6~7 CRC-16/MODBUS(对前 6 字节计算,小端)
*/
#ifndef SHS_PROTOCOL_H
#define SHS_PROTOCOL_H
#include "shs/common.h"
#include "shs/errors.h"
#include "shs/device.h"
#define SHS_FRAME_SYNC 0x5Au /**< 帧头 */
#define SHS_FRAME_SIZE 8u /**< 帧总长度 */
#define SHS_FRAME_HEADER_LEN 6u /**< 参与 CRC 计算的字节数 */
#define SHS_PROTOCOL_VERSION 1u /**< 当前协议版本 */
/** 帧头位域(项目八 位域) */
typedef struct {
uint8_t version : 3; /**< 协议版本 0~7 */
uint8_t type : 2; /**< 帧类型:0 请求 / 1 应答 / 2 上报 */
uint8_t flags : 3; /**< 标志位:bit0 需要应答、bit1 紧急等 */
} FrameHeader;
/** 帧类型 */
typedef enum {
SHS_FRAME_REQ = 0,
SHS_FRAME_ACK = 1,
SHS_FRAME_REPORT = 2
} shs_frame_type_t;
/** 命令字 */
typedef enum {
SHS_CMD_NONE = 0x00,
SHS_CMD_QUERY = 0x01, /**< 查询设备状态 */
SHS_CMD_POWER = 0x02, /**< 开/关设备 */
SHS_CMD_DIM = 0x03, /**< 调光(数据=亮度) */
SHS_CMD_LOCK = 0x04, /**< 门锁控制 */
SHS_CMD_REPORT = 0x05, /**< 上报数据 */
SHS_CMD_ACK = 0x06 /**< 应答 */
} shs_cmd_t;
/** 帧载荷共用体(项目八 共用体三视图:整体 / 字节数组 / 字段) */
typedef union {
uint32_t raw; /**< 整体视图 */
uint8_t bytes[4]; /**< 字节视图(收发缓冲) */
struct {
uint8_t dev_id; /**< 设备编号 */
uint8_t cmd; /**< 命令字 */
uint16_t value; /**< 数据(小端) */
} field; /**< 字段视图 */
} FramePayload;
/** 解析结果 */
typedef struct {
FrameHeader header; /**< 位域头 */
FramePayload payload; /**< 载荷 */
uint16_t crc; /**< 帧中携带的 CRC */
uint16_t crc_calc; /**< 本地计算的 CRC */
bool crc_ok; /**< 校验是否通过 */
} Frame;
/* ---------------------------------------------------------------- 协议操作 */
/** 取当前协议版本(static 变量,可通过 setter 修改,项目六 生命周期) */
uint8_t protocol_get_version(void);
void protocol_set_version(uint8_t version);
/**
* @brief 打包一帧
* @param dev_id 设备编号;cmd 命令字;value 数据;type 帧类型
* @param out 输出缓冲;out_size 缓冲大小(至少 SHS_FRAME_SIZE)
* @return 写入的字节数(SHS_FRAME_SIZE);失败返回负错误码:
* out 为空 → SHS_ERR_PARAM;out_size 不足 → SHS_ERR_FULL;
* cmd 超出 shs_cmd_t 范围或 type 非法 → SHS_ERR_PARAM
*/
int protocol_pack(uint8_t dev_id, uint8_t cmd, uint16_t value, shs_frame_type_t type,
uint8_t *out, size_t out_size);
/** 解析一帧(校验帧头、长度与 CRC) */
shs_err_t protocol_unpack(const uint8_t *buf, size_t len, Frame *out);
/** 把帧渲染成十六进制串(项目二 进制 / 项目九 位运算) */
shs_err_t protocol_to_hex(const uint8_t *frame, size_t len, char *out, size_t out_size);
/** 命令字名称(switch 演示) */
const char *protocol_cmd_name(uint8_t cmd);
/** 把收到的帧翻译成设备动作(项目三 switch + 项目六 嵌套调用) */
shs_err_t protocol_dispatch(const Frame *frame, Device devices[], int count,
char *response, size_t response_size);
#endif /* SHS_PROTOCOL_H */