feat(M2): 实现核心/存储/网络/应用四层并端到端跑通
- 28 个源文件落地:core(device/sensor/scene/schedule/system)、hal(gpio/i2c/uart/sim)、 protocol(crc/frame)、storage(config/logger/storage)、net(json/http_request/http_server/router)、 web(内嵌单页界面)、app(menu/cli/commands/replay/web_app)、common(新增 bitops.c) - 全文件在 -std=c17 -Wall -Wextra -Wpedantic -Werror 下零告警 跨模块缺陷修复(集成阶段发现): 1. HAL I2C 字节序与业务层不一致(模拟后端小端 / sensor.c 按大端解析), 导致 16 路采样全部被判「传感器故障」、温度显示 125.0℃;统一为大端后 读数恢复 20.4℃ / 50.0%RH,报警等级「正常」 2. 采样路数固定 16 路导致同一探头重复 16 条;改为「传感器设备台数(至少 1 路)」, system_tick 与 CLI sensors 命令保持一致 3. 补齐 app.h 中缺失的 app_run_web()(新增 src/app/web_app.c),并给监听套接字 设 1 秒接收超时,使服务主循环得以周期性推进 system_tick 4. 收敛接口约束:schedule.h 只读矩阵接口去 const(C17 -Wpedantic 不允许 非 const → const 的二维数组隐式转换);system_save/system_render_dashboard 形参同步去 const;网页 JS 由整段字面量改为分段字面量 + 首次拼装(ISO C99 强制支持的 4095 字节单字面量下限);HTTP 响应体缓冲 4KB → 16KB 验证:debug/release/asan 三预设零告警构建通过;ctest 3/3;scripts/acceptance.sh A1~A7 全通过;HTTP 17 条路由实测(首页 2107B / CSS 2313B / JS 6740B / 设备列表 8 台 / 传感器 / 日志 / 报表 / 配置读写 / 协议帧 CRC 校验 / 自检 / 404 兜底);回放脚本 23/23 命令成功
This commit is contained in:
1 parent
e0cc5d578f
commit
b96d5690aa
16 files changed
+3325
-75
No files matched your search
+188
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* @file cli.c
|
||||
* @brief 控制台交互层:菜单主循环 + 文本命令分发(严格按冻结的 include/shs/app.h 实现)
|
||||
*
|
||||
* 职责边界:
|
||||
* - 本文件只负责"人机交互":渲染菜单、读入一行、判断是菜单编号还是文本命令、
|
||||
* 把命令交给命令层 app_execute_command(),并维护主循环的退出条件。
|
||||
* - 菜单内容的排版在 menu.c;文本命令的实现(help/list/power/...)在 commands.c。
|
||||
*
|
||||
* 教材知识点落点:
|
||||
* - 项目一 程序结构与格式化输出(欢迎横幅、提示符)
|
||||
* - 项目三 分支与布尔状态(数字/命令分流、app_stop_requested 循环条件)
|
||||
* - 项目四 循环与输入校验(while 主循环 + 非法输入不退出 + 超长行丢弃不失控)
|
||||
* - 项目六 函数与 static 变量(停止标志)
|
||||
* - 项目九 信号与文件(SIGINT 经由 app_request_stop 生效;退出自动保存)
|
||||
* - 项目十 项目集成(把 menu.c 与 commands.c 串成可交互的控制台)
|
||||
*/
|
||||
#include "shs/app.h"
|
||||
#include "shs/menu.h"
|
||||
|
||||
#include "shs/system.h"
|
||||
#include "shs/config.h"
|
||||
#include "shs/logger.h"
|
||||
#include "shs/string_utils.h"
|
||||
#include "shs/num_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* ---------------------------------------------------------------- 模块常量 */
|
||||
#define CLI_TAG "cli" /**< 日志标签 */
|
||||
#define CLI_MENU_BUF 4096u /**< 菜单文本缓冲 */
|
||||
#define CLI_OUT_BUF (SHS_LINE_MAX * 4u) /**< 单条命令输出缓冲 */
|
||||
#define CLI_DRAIN_MAX 4096 /**< 超长行最多丢弃的字符数(防死循环) */
|
||||
|
||||
/* ---------------------------------------------------------------- 停止标志 */
|
||||
/**
|
||||
* static 停止标志(项目六 static;项目九 信号)。
|
||||
* app_request_stop() 由 SIGINT 处理函数、回放脚本的 quit 命令或测试调用;
|
||||
* app_run_cli() / app_run_web() 把它当作主循环条件。
|
||||
*/
|
||||
static volatile bool g_app_stop_requested = false;
|
||||
|
||||
/** 请求主循环退出(幂等;置位后不可逆,符合"已请求停止"的语义) */
|
||||
void app_request_stop(void)
|
||||
{
|
||||
g_app_stop_requested = true;
|
||||
}
|
||||
|
||||
/** 是否已请求停止(主循环条件:项目三 布尔状态) */
|
||||
bool app_stop_requested(void)
|
||||
{
|
||||
return g_app_stop_requested;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- 内部工具 */
|
||||
/** 丢弃当前输入行中 '\n' 之后的残留字符(带上限,保证绝不死循环:项目四) */
|
||||
static void cli_discard_rest_of_line(void)
|
||||
{
|
||||
int drained = 0;
|
||||
int c;
|
||||
|
||||
while (drained < CLI_DRAIN_MAX) {
|
||||
c = getchar();
|
||||
if (c == '\n' || c == EOF) {
|
||||
break; /* 行尾或输入结束:正常退出循环 */
|
||||
}
|
||||
drained++;
|
||||
}
|
||||
}
|
||||
|
||||
/** 打印欢迎横幅(项目一 格式化输出) */
|
||||
static void cli_print_banner(const SmartHomeSystem *sys)
|
||||
{
|
||||
printf("\n");
|
||||
printf("==============================================================\n");
|
||||
printf(" 智能家居控制系统 · 控制台(--cli 交互模式)\n");
|
||||
printf("--------------------------------------------------------------\n");
|
||||
printf(" 系统名称 : %s\n", sys->config.system_name);
|
||||
printf(" 运行模式 : %s\n", system_mode_name(sys->mode));
|
||||
printf(" 设备/房间 : %d 台 / %d 个\n", sys->device_count, sys->room_count);
|
||||
printf(" 数据目录 : %s\n", sys->config.data_dir);
|
||||
printf(" 命令帮助 : 输入 help 查看命令表;输入 0 或 quit 退出\n");
|
||||
printf("==============================================================\n");
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- 主循环 */
|
||||
/**
|
||||
* @brief CLI 交互模式主循环
|
||||
*
|
||||
* 每轮结构(项目四 while 主循环):
|
||||
* 1) menu_render 渲染主菜单(menu.c,项目一/四)
|
||||
* 2) 从 stdin 读入一行(prompt 交互模式)
|
||||
* 3) 输入为空 → 重绘;输入是整数 → menu_dispatch 分派(项目三 switch)
|
||||
* 输入是文本 → app_execute_command 分派(README 第 4 节命令表)
|
||||
* 4) 非法输入只提示不退出;EOF / 0 / quit 才结束循环(项目三 退出分支)
|
||||
*
|
||||
* 返回:SHS_OK(正常退出);参数非法 → SHS_ERR_PARAM;系统未初始化 → SHS_ERR_STATE。
|
||||
*/
|
||||
shs_err_t app_run_cli(SmartHomeSystem *sys)
|
||||
{
|
||||
char menu_text[CLI_MENU_BUF];
|
||||
char line[SHS_LINE_MAX];
|
||||
char out[CLI_OUT_BUF];
|
||||
int choice = 0;
|
||||
int rounds = 0;
|
||||
shs_err_t err;
|
||||
|
||||
if (sys == NULL) {
|
||||
printf("控制台启动失败:系统对象为空。\n");
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
if (!sys->initialized) {
|
||||
printf("控制台启动失败:系统尚未初始化,请先调用 system_init()。\n");
|
||||
return SHS_ERR_STATE;
|
||||
}
|
||||
|
||||
cli_print_banner(sys);
|
||||
(void)logger_write(SHS_LOG_INFO, CLI_TAG, "控制台交互模式启动(设备 %d 台)",
|
||||
sys->device_count);
|
||||
|
||||
while (!app_stop_requested()) { /* 项目四 while:停止标志为循环条件 */
|
||||
int item_count = menu_item_count();
|
||||
|
||||
rounds++;
|
||||
|
||||
/* 1) 渲染菜单:即使缓冲不足也已安全截断,内容可用 */
|
||||
err = menu_render(sys, menu_text, sizeof(menu_text));
|
||||
if (err == SHS_ERR_FULL) {
|
||||
printf("(提示:本终端菜单文本过长,已安全截断)\n");
|
||||
}
|
||||
printf("%s", menu_text);
|
||||
|
||||
/* 2) 提示符 + 读入一行(prompt 模式从 stdin 读取) */
|
||||
printf("请选择 [1-%d] 或输入命令(0/quit 退出,help 帮助):", item_count);
|
||||
(void)fflush(stdout);
|
||||
|
||||
if (fgets(line, (int)sizeof(line), stdin) == NULL) {
|
||||
printf("\n检测到输入结束(EOF),控制台正常退出。\n");
|
||||
break; /* EOF:干净退出,不会死循环 */
|
||||
}
|
||||
if (strchr(line, '\n') == NULL) {
|
||||
cli_discard_rest_of_line(); /* 超长行:丢弃残留,避免读到"半行" */
|
||||
}
|
||||
(void)shs_str_trim(line);
|
||||
if (line[0] == '\0') {
|
||||
continue; /* 空行:重绘菜单继续,不算错误 */
|
||||
}
|
||||
|
||||
/* 3a) 纯数字 → 菜单编号(项目三 switch 分派) */
|
||||
if (shs_parse_int(line, &choice) == SHS_OK) {
|
||||
if (choice == 0) {
|
||||
printf("收到退出指令(0),准备退出控制台。\n");
|
||||
break;
|
||||
}
|
||||
(void)menu_dispatch(sys, choice); /* 编号非法时菜单层提示后重输 */
|
||||
continue;
|
||||
}
|
||||
|
||||
/* 3b) 文本命令 → README 第 4 节命令表分发(help/list/power/dim/lock/
|
||||
* sensors/ac/scene/schedule/report/log/frame/save/load/selftest/quit) */
|
||||
err = app_execute_command(sys, line, out, sizeof(out));
|
||||
if (out[0] != '\0') {
|
||||
printf("%s", out);
|
||||
if (out[shs_strlen(out) - 1u] != '\n') {
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
if (err != SHS_OK && out[0] == '\0') {
|
||||
/* 命令层未给出文本时兜底提示(非法命令 → NOT_FOUND,参数错 → PARAM/RANGE) */
|
||||
printf("命令执行失败:%s(输入 help 查看命令表)\n", shs_strerror(err));
|
||||
}
|
||||
/* 注意:quit 命令内部会调用 app_request_stop(),下一轮 while 条件为假 → 退出 */
|
||||
}
|
||||
|
||||
/* 4) 退出清理:按配置自动保存(项目九 文件) */
|
||||
if (sys->config.auto_save) {
|
||||
shs_err_t save_err = system_save(sys);
|
||||
|
||||
printf("退出前自动保存(auto_save=1):%s\n", shs_strerror(save_err));
|
||||
(void)logger_write(save_err == SHS_OK ? SHS_LOG_INFO : SHS_LOG_WARN, CLI_TAG,
|
||||
"退出自动保存:%s", shs_strerror(save_err));
|
||||
}
|
||||
|
||||
(void)logger_write(SHS_LOG_INFO, CLI_TAG, "控制台会话结束,共交互 %d 轮", rounds);
|
||||
printf("控制台已退出(本次共交互 %d 轮)。\n", rounds);
|
||||
return SHS_OK;
|
||||
}
|
||||
Reference in new issue
Block a user