Files
smart-home-control-system/include/shs/system.h
T
sambu 3e10ee8488 test(S6): 补齐 app 层与 HTTP 第二组集成测试,行覆盖率 72.2%→80.3%(NFR-05 达标)
新增
- tests/integration/test_menu.c:菜单 14 项全分派、命令全集正/负例、参数解析、脚本化 stdin 主循环
- tests/integration/test_http_api.c:其余 11 个接口 + 错误路径(400/404/协议层 400)
- src/app/app_options.c:app_parse_args/app_print_usage 从 main.c 迁入静态库(app.h 声明此前无实现)
- app_clear_stop():复用主循环时清除退出标志
- tools/coverage.sh:A10 覆盖率门限自动判定;acceptance 扩到 A1~A10 共 25 项

修复(本轮集成测试暴露)
- menu_render 漏渲染首项「设备列表」(行号 0 既当标题又当表项 0)
- http_server_stop 只置标志不关套接字,导致同进程无法二次启动(聚合运行 70 项失败)
- POST /api/config 未匹配到字段时假成功(现返回 400)
- CRC/I2C 字节序、采样路数、离线设备负例等此前已在集成阶段修复

实测:CTest 19/19;聚合运行 19 套件 2450 断言 0 失败;ASan 19/19;覆盖率 80.3%
2026-09-14 22:23:17 +08:00

70 lines
3.6 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 system.h
* @brief 系统级对象:聚合设备、房间、区域树、定时矩阵、采样与配置
*
* 教材对应:项目八 结构体嵌套 · 项目五 数组 · 项目十 项目集成与部署。
*/
#ifndef SHS_SYSTEM_H
#define SHS_SYSTEM_H
#include "shs/common.h"
#include "shs/errors.h"
#include "shs/device.h"
#include "shs/sensor.h"
#include "shs/schedule.h"
#include "shs/config.h"
/** 系统运行模式 */
typedef enum {
SHS_MODE_CLI = 0, /**< 控制台交互模式 */
SHS_MODE_WEB, /**< 网页服务模式 */
SHS_MODE_REPLAY /**< 脚本回放模式(自动演示) */
} shs_run_mode_t;
/** 智能家居系统总对象(全局唯一) */
typedef struct {
Device devices[SHS_MAX_DEVICES]; /**< 设备表(一维数组) */
int device_count; /**< 当前设备数 */
Room rooms[SHS_MAX_ROOMS]; /**< 房间表 */
int room_count;
Zone zones[SHS_MAX_ZONES]; /**< 区域树(父指针数组) */
int zone_count;
shs_matrix_t schedule; /**< 周×时定时矩阵(二维数组) */
SensorSample samples[SHS_MAX_SAMPLES]; /**< 最近采样缓存 */
int sample_count;
SystemConfig config; /**< 系统配置 */
char data_dir[64]; /**< 数据目录(配置文件/日志/设备表) */
char config_path[128]; /**< 配置文件路径 */
char devices_path[128]; /**< 设备表文件路径 */
char log_path[128]; /**< 日志文件路径 */
shs_run_mode_t mode; /**< 运行模式 */
uint32_t start_ts; /**< 启动时间戳 */
uint32_t tick_count; /**< 主循环计数(循环次数统计) */
bool initialized; /**< 是否已初始化 */
} SmartHomeSystem;
/** 取全局系统对象(全局变量 + 单例访问,项目六 生命周期) */
SmartHomeSystem *system_instance(void);
/* * 初始化:填默认配置、生成数据文件路径、初始化 HAL 与日志。
* data_dir 传 NULL 时使用默认目录 "data"(便于不带参数启动);sys 为 NULL 返回 SHS_ERR_PARAM。 */
shs_err_t system_init(SmartHomeSystem *sys, const char *data_dir, shs_run_mode_t mode);
/** 从磁盘加载配置与设备表(文件不存在时使用默认值与演示数据) */
shs_err_t system_load(SmartHomeSystem *sys);
/* 说明:形参不带 const —— C17 下把 uint8_t[7][24] 隐式转成 const 限定指针是 -Wpedantic 禁止的
* (定时矩阵会往下传给 schedule_* 只读接口),因此只读语义靠约定与注释保证,不改写数据。 */
/** 保存配置与设备表 */
shs_err_t system_save(SmartHomeSystem *sys);
/** 载入演示数据:8 个设备、3 个房间、区域树、一周定时计划 */
shs_err_t system_seed_demo(SmartHomeSystem *sys);
/** 主循环单步:采集传感器、检查报警、执行定时任务(返回执行的动作数) */
int system_tick(SmartHomeSystem *sys, uint32_t now_ts);
/** 系统自检:逐项检查并输出报告文本(项目十 整体测试) */
shs_err_t system_self_test(SmartHomeSystem *sys, char *report, size_t report_size);
/** 把全屋状态渲染为文本(CLI 仪表盘);buf_size 过小返回 SHS_ERR_FULL,NULL/0 返回 SHS_ERR_PARAM */
shs_err_t system_render_dashboard(SmartHomeSystem *sys, char *buf, size_t buf_size);
/** 运行模式名称 */
const char *system_mode_name(shs_run_mode_t mode);
#endif /* SHS_SYSTEM_H */