| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- * Anti-loss parameter mirror implementation.
- *
- * Lives in the FAL "easyflash" partition. Uses two slots (A/B) for redundancy,
- * each slot is one flash sector (4096 bytes on this board).
- */
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <fal.h>
- #include <string.h>
- #include "param_mirror.h"
- #include "control.h"
- #include "dtu_uart2.h" /* dtu_request_param_backup:参数变更→备份到RTU 的触发接口 */
- /* 2026-10-29 修复:本文件原来缺 DBG_ENABLE —— rtdbg.h:69 只有在 DBG_ENABLE 定义时
- * 才展开 dbg_log_line,否则 LOG_x 全部退化为空宏。也就是说本模块自建立起所有日志
- * (含 LOG_W("param mirror save failed") 等失败告警)都从未输出过,参数镜像写失败
- * 在现场完全不可见。补上后镜像擦/写异常才能被日志和上报捕获。 */
- #define DBG_ENABLE
- #define DBG_TAG "param_mirror"
- #define DBG_LVL DBG_LOG
- #include <rtdbg.h>
- #define MIRROR_PART_NAME "easyflash"
- #define MIRROR_MAGIC 0x50415242U /* 'PARB' */
- /* Header lives in the first 16 bytes of each slot, payload follows. */
- #define HDR_SIZE 16U
- Device_Status_typedef device_status;
- static const struct fal_partition *s_part = RT_NULL;
- /* ---- 防写放大/并发保护 ----
- * 1) 内容未变化时直接跳过(避免每条 JSON/每次同步都擦写 2 个扇区,耗尽 Flash 寿命);
- * 2) 用互斥锁串行化多线程的镜像读写(HMI/DTU/工作流程线程都会调用)。
- */
- static struct rt_mutex s_mirror_lock;
- static rt_bool_t s_mirror_lock_ready = RT_FALSE;
- static param_payload_t s_last_payload;
- static rt_bool_t s_last_valid = RT_FALSE;
- static void mirror_lock(void)
- {
- if (!s_mirror_lock_ready)
- {
- rt_base_t level = rt_hw_interrupt_disable();
- if (!s_mirror_lock_ready)
- {
- if (rt_mutex_init(&s_mirror_lock, "pm_mtx", RT_IPC_FLAG_PRIO) == RT_EOK)
- {
- s_mirror_lock_ready = RT_TRUE;
- }
- }
- rt_hw_interrupt_enable(level);
- }
- if (s_mirror_lock_ready)
- {
- rt_mutex_take(&s_mirror_lock, RT_WAITING_FOREVER);
- }
- }
- static void mirror_unlock(void)
- {
- if (s_mirror_lock_ready)
- {
- rt_mutex_release(&s_mirror_lock);
- }
- }
- /* 只比较配置体(跳过 magic/version/write_seq/crc32 头部) */
- static rt_bool_t payload_body_equal(const param_payload_t *a, const param_payload_t *b)
- {
- return (rt_memcmp((const rt_uint8_t *)a + HDR_SIZE,
- (const rt_uint8_t *)b + HDR_SIZE,
- sizeof(param_payload_t) - HDR_SIZE) == 0) ? RT_TRUE : RT_FALSE;
- }
- static uint32_t calc_crc32(const void *buf, size_t size)
- {
- /* EF ships ef_calc_crc32; reuse to avoid bringing our own table. */
- extern uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size);
- return ef_calc_crc32(0, buf, size);
- }
- static int slot_read(uint32_t slot_addr, param_payload_t *out)
- {
- if (s_part == RT_NULL)
- {
- return -RT_ERROR;
- }
- return (fal_partition_read(s_part, slot_addr, (uint8_t *)out,
- sizeof(param_payload_t)) > 0)
- ? RT_EOK : -RT_ERROR;
- }
- static int slot_write(uint32_t slot_addr, const param_payload_t *in)
- {
- if (s_part == RT_NULL)
- {
- return -RT_ERROR;
- }
- if (fal_partition_erase(s_part, slot_addr, MIRROR_SLOT_SIZE) < 0)
- {
- return -RT_ERROR;
- }
- if (fal_partition_write(s_part, slot_addr, (const uint8_t *)in,
- sizeof(param_payload_t)) < 0)
- {
- return -RT_ERROR;
- }
- return RT_EOK;
- }
- static bool slot_is_valid(const param_payload_t *p)
- {
- if (p->magic != MIRROR_MAGIC)
- {
- return false;
- }
- if (p->version != PARAM_PAYLOAD_VER)
- {
- return false;
- }
- /* crc32 covers everything after the 16-byte header. */
- const uint8_t *body = (const uint8_t *)p + HDR_SIZE;
- size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
- uint32_t crc = calc_crc32(body, body_len);
- return crc == p->crc32;
- }
- int param_mirror_load(param_payload_t *dst)
- {
- if (dst == RT_NULL)
- {
- return -RT_ERROR;
- }
- if (s_part == RT_NULL)
- {
- s_part = fal_partition_find(MIRROR_PART_NAME);
- if (s_part == RT_NULL)
- {
- return -RT_ERROR;
- }
- }
- param_payload_t a, b;
- bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
- bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
- if (!va && !vb)
- {
- return -RT_ERROR;
- }
- if (va && !vb)
- {
- *dst = a;
- }
- else if (!va && vb)
- {
- *dst = b;
- }
- else
- {
- /* both valid: pick the one with higher write_seq */
- *dst = (a.write_seq >= b.write_seq) ? a : b;
- }
- /* 记录已加载内容:后续 param_mirror_sync() 内容相同则跳过擦写(防写放大) */
- s_last_payload = *dst;
- s_last_valid = RT_TRUE;
- return RT_EOK;
- }
- /* 擦除两个镜像槽(2026-10-29 新增,供远程擦除 Flash 指令使用)。
- * 关键:擦完必须把 s_last_valid 清掉 —— 否则后续 param_mirror_sync() 会因
- * "内容与上次相同"直接 return,镜像将永久为空,防丢保护静默失效。 */
- int param_mirror_erase_all(void)
- {
- if (s_part == RT_NULL)
- {
- s_part = fal_partition_find(MIRROR_PART_NAME);
- if (s_part == RT_NULL)
- {
- return -RT_ERROR;
- }
- }
- mirror_lock();
- /* 两个槽各占一个 4KB 扇区且相邻,但分两次擦以兼容 blk_size 校验 */
- if (fal_partition_erase(s_part, MIRROR_SLOT_A_ADDR, MIRROR_SLOT_SIZE) < 0)
- {
- LOG_W("mirror erase slot A failed");
- mirror_unlock();
- return -RT_ERROR;
- }
- if (fal_partition_erase(s_part, MIRROR_SLOT_B_ADDR, MIRROR_SLOT_SIZE) < 0)
- {
- LOG_W("mirror erase slot B failed");
- mirror_unlock();
- return -RT_ERROR;
- }
- s_last_valid = RT_FALSE; /* 清 RAM 缓存:下一次 sync 必定重新落盘 */
- mirror_unlock();
- LOG_I("param mirror erased (slot A/B)");
- return RT_EOK;
- }
- int param_mirror_save(const param_payload_t *src)
- {
- if (src == RT_NULL)
- {
- return -RT_ERROR;
- }
- if (s_part == RT_NULL)
- {
- s_part = fal_partition_find(MIRROR_PART_NAME);
- if (s_part == RT_NULL)
- {
- return -RT_ERROR;
- }
- }
- /* Determine which slot is older by reading both and comparing write_seq. */
- param_payload_t a, b;
- bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
- bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
- param_payload_t newer, older;
- uint32_t newer_addr, older_addr;
- uint32_t next_seq;
- if (!va && !vb)
- {
- /* cold start: both empty. Write B first, then A. */
- newer_addr = MIRROR_SLOT_B_ADDR;
- older_addr = MIRROR_SLOT_A_ADDR;
- next_seq = 1;
- }
- else if (va && !vb)
- {
- newer = a;
- newer_addr = MIRROR_SLOT_A_ADDR;
- older_addr = MIRROR_SLOT_B_ADDR;
- next_seq = newer.write_seq + 1;
- }
- else if (!va && vb)
- {
- newer = b;
- newer_addr = MIRROR_SLOT_B_ADDR;
- older_addr = MIRROR_SLOT_A_ADDR;
- next_seq = newer.write_seq + 1;
- }
- else
- {
- /* both valid */
- if (a.write_seq >= b.write_seq)
- {
- newer = a; newer_addr = MIRROR_SLOT_A_ADDR;
- older = b; older_addr = MIRROR_SLOT_B_ADDR;
- }
- else
- {
- newer = b; newer_addr = MIRROR_SLOT_B_ADDR;
- older = a; older_addr = MIRROR_SLOT_A_ADDR;
- }
- next_seq = newer.write_seq + 1;
- }
- /* Build the record. Reuse the caller's payload for everything except
- * magic/version/seq/crc32, which we own. */
- param_payload_t out = *src;
- out.magic = MIRROR_MAGIC;
- out.version = PARAM_PAYLOAD_VER;
- out.write_seq = next_seq;
- /* Recompute crc32 over the body (everything past the 16-byte header). */
- uint8_t *body = (uint8_t *)&out + HDR_SIZE;
- size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
- out.crc32 = calc_crc32(body, body_len);
- /* Write the older slot first, then the newer slot.
- * If we crash between the two writes, the loader still finds a valid
- * copy in the previous-newer slot (with seq = next_seq - 1). */
- if (slot_write(older_addr, &out) != RT_EOK)
- {
- LOG_W("mirror write older slot (0x%08x) failed", older_addr);
- return -RT_ERROR;
- }
- if (slot_write(newer_addr, &out) != RT_EOK)
- {
- LOG_W("mirror write newer slot (0x%08x) failed", newer_addr);
- /* Still acceptable: loader can fall back to the older copy. */
- }
- return RT_EOK;
- }
- void param_mirror_pack_defaults(param_payload_t *out)
- {
- memset(out, 0, sizeof(*out));
- /* Caller will overwrite magic/version/seq/crc32 when actually saving.
- * Pre-fill magic/version here so a subsequent read still validates. */
- out->magic = MIRROR_MAGIC;
- out->version = PARAM_PAYLOAD_VER;
- /* write_seq left at 0; will be set on first save. */
- }
- /* The three helpers below bridge between device_status (defined in control.h)
- * and the packed mirror layout. They live here (instead of hmi.c) so any
- * code path that touches persistent config can call param_mirror_sync()
- * without needing to know device_status internals. */
- static void device_status_to_mirror(param_payload_t *out)
- {
- /* Zero the whole struct so header fields (magic/version/seq/crc32)
- * are clean before param_mirror_save() overwrites them. */
- memset(out, 0, sizeof(*out));
- out->iSave = device_status.iSave;
- out->dtype = device_status.dtype;
- out->ds = device_status.ds;
- out->clt_time = device_status.clt_time;
- out->heattime = device_status.heattime;
- out->heatvalue = device_status.heatvalue;
- out->thsp = device_status.thsp;
- out->sunBar = device_status.sunBar;
- out->startBar = device_status.startBar;
- out->stopBar = device_status.stopBar;
- out->timeSwitch = device_status.timeSwitch;
- out->tph = device_status.tph;
- out->tpl = device_status.tpl;
- out->shake_ws = device_status.shake_ws;
- out->shake_sec = device_status.shake_sec;
- out->passWord = device_status.passWord;
- out->dattim = device_status.dattim;
- out->version_type = device_status.version_type;
- out->Power_Type = device_status.Power_Type;
- out->handle_bugger = device_status.handle_bugger;
- }
- uint32_t mirror_to_device_status(const param_payload_t *in)
- {
- device_status.iSave = in->iSave;
- device_status.dtype = in->dtype;
- device_status.ds = in->ds;
- device_status.clt_time = in->clt_time;
- device_status.heattime = in->heattime;
- device_status.heatvalue = in->heatvalue;
- device_status.thsp = in->thsp;
- device_status.sunBar = in->sunBar;
- device_status.startBar = in->startBar;
- device_status.stopBar = in->stopBar;
- device_status.timeSwitch = in->timeSwitch;
- device_status.tph = in->tph;
- device_status.tpl = in->tpl;
- device_status.shake_ws = in->shake_ws;
- device_status.shake_sec = in->shake_sec;
- device_status.passWord = in->passWord;
- device_status.dattim = in->dattim;
- device_status.version_type = in->version_type;
- device_status.Power_Type = in->Power_Type;
- device_status.handle_bugger = in->handle_bugger;
- return in->write_seq;
- }
- /* NOTE: do NOT name the local `p` here — rtdbg.h defines a file-scope
- * `struct tm *p` and any LOG_* call inside this function would expand
- * `p = localtime(&timep)` and clash with our local type. */
- void param_mirror_sync(void)
- {
- param_payload_t payload;
- device_status_to_mirror(&payload);
- mirror_lock();
- /* 内容未变化:直接跳过。避免每条 JSON(心跳/查询应答) 都触发
- * 两次 4KB 扇区擦除,既保护 Flash 寿命也避免阻塞串口接收线程。 */
- if (s_last_valid && payload_body_equal(&payload, &s_last_payload))
- {
- mirror_unlock();
- return;
- }
- if (param_mirror_save(&payload) != RT_EOK)
- {
- LOG_W("param mirror save failed");
- mirror_unlock();
- return;
- }
- s_last_payload = payload;
- s_last_valid = RT_TRUE;
- /* 2026-10-15:参数内容确有变更 → 请求 DTU 线程把最新参数备份到 RTU
- * (本函数是所有参数变更路径的唯一汇聚点;实际发送由 dtu_param_pending_service
- * 在 RTU 通信正常后执行,避免与恢复流程抢跑) */
- dtu_request_param_backup();
- mirror_unlock();
- }
|