param_mirror.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Anti-loss parameter mirror implementation.
  3. *
  4. * Lives in the FAL "easyflash" partition. Uses two slots (A/B) for redundancy,
  5. * each slot is one flash sector (4096 bytes on this board).
  6. */
  7. #include <rtthread.h>
  8. #include <rtdevice.h>
  9. #include <fal.h>
  10. #include <string.h>
  11. #include "param_mirror.h"
  12. #include "control.h"
  13. #include "dtu_uart2.h" /* dtu_request_param_backup:参数变更→备份到RTU 的触发接口 */
  14. /* 2026-10-29 修复:本文件原来缺 DBG_ENABLE —— rtdbg.h:69 只有在 DBG_ENABLE 定义时
  15. * 才展开 dbg_log_line,否则 LOG_x 全部退化为空宏。也就是说本模块自建立起所有日志
  16. * (含 LOG_W("param mirror save failed") 等失败告警)都从未输出过,参数镜像写失败
  17. * 在现场完全不可见。补上后镜像擦/写异常才能被日志和上报捕获。 */
  18. #define DBG_ENABLE
  19. #define DBG_TAG "param_mirror"
  20. #define DBG_LVL DBG_LOG
  21. #include <rtdbg.h>
  22. #define MIRROR_PART_NAME "easyflash"
  23. #define MIRROR_MAGIC 0x50415242U /* 'PARB' */
  24. /* Header lives in the first 16 bytes of each slot, payload follows. */
  25. #define HDR_SIZE 16U
  26. Device_Status_typedef device_status;
  27. static const struct fal_partition *s_part = RT_NULL;
  28. /* ---- 防写放大/并发保护 ----
  29. * 1) 内容未变化时直接跳过(避免每条 JSON/每次同步都擦写 2 个扇区,耗尽 Flash 寿命);
  30. * 2) 用互斥锁串行化多线程的镜像读写(HMI/DTU/工作流程线程都会调用)。
  31. */
  32. static struct rt_mutex s_mirror_lock;
  33. static rt_bool_t s_mirror_lock_ready = RT_FALSE;
  34. static param_payload_t s_last_payload;
  35. static rt_bool_t s_last_valid = RT_FALSE;
  36. static void mirror_lock(void)
  37. {
  38. if (!s_mirror_lock_ready)
  39. {
  40. rt_base_t level = rt_hw_interrupt_disable();
  41. if (!s_mirror_lock_ready)
  42. {
  43. if (rt_mutex_init(&s_mirror_lock, "pm_mtx", RT_IPC_FLAG_PRIO) == RT_EOK)
  44. {
  45. s_mirror_lock_ready = RT_TRUE;
  46. }
  47. }
  48. rt_hw_interrupt_enable(level);
  49. }
  50. if (s_mirror_lock_ready)
  51. {
  52. rt_mutex_take(&s_mirror_lock, RT_WAITING_FOREVER);
  53. }
  54. }
  55. static void mirror_unlock(void)
  56. {
  57. if (s_mirror_lock_ready)
  58. {
  59. rt_mutex_release(&s_mirror_lock);
  60. }
  61. }
  62. /* 只比较配置体(跳过 magic/version/write_seq/crc32 头部) */
  63. static rt_bool_t payload_body_equal(const param_payload_t *a, const param_payload_t *b)
  64. {
  65. return (rt_memcmp((const rt_uint8_t *)a + HDR_SIZE,
  66. (const rt_uint8_t *)b + HDR_SIZE,
  67. sizeof(param_payload_t) - HDR_SIZE) == 0) ? RT_TRUE : RT_FALSE;
  68. }
  69. static uint32_t calc_crc32(const void *buf, size_t size)
  70. {
  71. /* EF ships ef_calc_crc32; reuse to avoid bringing our own table. */
  72. extern uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size);
  73. return ef_calc_crc32(0, buf, size);
  74. }
  75. static int slot_read(uint32_t slot_addr, param_payload_t *out)
  76. {
  77. if (s_part == RT_NULL)
  78. {
  79. return -RT_ERROR;
  80. }
  81. return (fal_partition_read(s_part, slot_addr, (uint8_t *)out,
  82. sizeof(param_payload_t)) > 0)
  83. ? RT_EOK : -RT_ERROR;
  84. }
  85. static int slot_write(uint32_t slot_addr, const param_payload_t *in)
  86. {
  87. if (s_part == RT_NULL)
  88. {
  89. return -RT_ERROR;
  90. }
  91. if (fal_partition_erase(s_part, slot_addr, MIRROR_SLOT_SIZE) < 0)
  92. {
  93. return -RT_ERROR;
  94. }
  95. if (fal_partition_write(s_part, slot_addr, (const uint8_t *)in,
  96. sizeof(param_payload_t)) < 0)
  97. {
  98. return -RT_ERROR;
  99. }
  100. return RT_EOK;
  101. }
  102. static bool slot_is_valid(const param_payload_t *p)
  103. {
  104. if (p->magic != MIRROR_MAGIC)
  105. {
  106. return false;
  107. }
  108. if (p->version != PARAM_PAYLOAD_VER)
  109. {
  110. return false;
  111. }
  112. /* crc32 covers everything after the 16-byte header. */
  113. const uint8_t *body = (const uint8_t *)p + HDR_SIZE;
  114. size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
  115. uint32_t crc = calc_crc32(body, body_len);
  116. return crc == p->crc32;
  117. }
  118. int param_mirror_load(param_payload_t *dst)
  119. {
  120. if (dst == RT_NULL)
  121. {
  122. return -RT_ERROR;
  123. }
  124. if (s_part == RT_NULL)
  125. {
  126. s_part = fal_partition_find(MIRROR_PART_NAME);
  127. if (s_part == RT_NULL)
  128. {
  129. return -RT_ERROR;
  130. }
  131. }
  132. param_payload_t a, b;
  133. bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
  134. bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
  135. if (!va && !vb)
  136. {
  137. return -RT_ERROR;
  138. }
  139. if (va && !vb)
  140. {
  141. *dst = a;
  142. }
  143. else if (!va && vb)
  144. {
  145. *dst = b;
  146. }
  147. else
  148. {
  149. /* both valid: pick the one with higher write_seq */
  150. *dst = (a.write_seq >= b.write_seq) ? a : b;
  151. }
  152. /* 记录已加载内容:后续 param_mirror_sync() 内容相同则跳过擦写(防写放大) */
  153. s_last_payload = *dst;
  154. s_last_valid = RT_TRUE;
  155. return RT_EOK;
  156. }
  157. /* 擦除两个镜像槽(2026-10-29 新增,供远程擦除 Flash 指令使用)。
  158. * 关键:擦完必须把 s_last_valid 清掉 —— 否则后续 param_mirror_sync() 会因
  159. * "内容与上次相同"直接 return,镜像将永久为空,防丢保护静默失效。 */
  160. int param_mirror_erase_all(void)
  161. {
  162. if (s_part == RT_NULL)
  163. {
  164. s_part = fal_partition_find(MIRROR_PART_NAME);
  165. if (s_part == RT_NULL)
  166. {
  167. return -RT_ERROR;
  168. }
  169. }
  170. mirror_lock();
  171. /* 两个槽各占一个 4KB 扇区且相邻,但分两次擦以兼容 blk_size 校验 */
  172. if (fal_partition_erase(s_part, MIRROR_SLOT_A_ADDR, MIRROR_SLOT_SIZE) < 0)
  173. {
  174. LOG_W("mirror erase slot A failed");
  175. mirror_unlock();
  176. return -RT_ERROR;
  177. }
  178. if (fal_partition_erase(s_part, MIRROR_SLOT_B_ADDR, MIRROR_SLOT_SIZE) < 0)
  179. {
  180. LOG_W("mirror erase slot B failed");
  181. mirror_unlock();
  182. return -RT_ERROR;
  183. }
  184. s_last_valid = RT_FALSE; /* 清 RAM 缓存:下一次 sync 必定重新落盘 */
  185. mirror_unlock();
  186. LOG_I("param mirror erased (slot A/B)");
  187. return RT_EOK;
  188. }
  189. int param_mirror_save(const param_payload_t *src)
  190. {
  191. if (src == RT_NULL)
  192. {
  193. return -RT_ERROR;
  194. }
  195. if (s_part == RT_NULL)
  196. {
  197. s_part = fal_partition_find(MIRROR_PART_NAME);
  198. if (s_part == RT_NULL)
  199. {
  200. return -RT_ERROR;
  201. }
  202. }
  203. /* Determine which slot is older by reading both and comparing write_seq. */
  204. param_payload_t a, b;
  205. bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
  206. bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
  207. param_payload_t newer, older;
  208. uint32_t newer_addr, older_addr;
  209. uint32_t next_seq;
  210. if (!va && !vb)
  211. {
  212. /* cold start: both empty. Write B first, then A. */
  213. newer_addr = MIRROR_SLOT_B_ADDR;
  214. older_addr = MIRROR_SLOT_A_ADDR;
  215. next_seq = 1;
  216. }
  217. else if (va && !vb)
  218. {
  219. newer = a;
  220. newer_addr = MIRROR_SLOT_A_ADDR;
  221. older_addr = MIRROR_SLOT_B_ADDR;
  222. next_seq = newer.write_seq + 1;
  223. }
  224. else if (!va && vb)
  225. {
  226. newer = b;
  227. newer_addr = MIRROR_SLOT_B_ADDR;
  228. older_addr = MIRROR_SLOT_A_ADDR;
  229. next_seq = newer.write_seq + 1;
  230. }
  231. else
  232. {
  233. /* both valid */
  234. if (a.write_seq >= b.write_seq)
  235. {
  236. newer = a; newer_addr = MIRROR_SLOT_A_ADDR;
  237. older = b; older_addr = MIRROR_SLOT_B_ADDR;
  238. }
  239. else
  240. {
  241. newer = b; newer_addr = MIRROR_SLOT_B_ADDR;
  242. older = a; older_addr = MIRROR_SLOT_A_ADDR;
  243. }
  244. next_seq = newer.write_seq + 1;
  245. }
  246. /* Build the record. Reuse the caller's payload for everything except
  247. * magic/version/seq/crc32, which we own. */
  248. param_payload_t out = *src;
  249. out.magic = MIRROR_MAGIC;
  250. out.version = PARAM_PAYLOAD_VER;
  251. out.write_seq = next_seq;
  252. /* Recompute crc32 over the body (everything past the 16-byte header). */
  253. uint8_t *body = (uint8_t *)&out + HDR_SIZE;
  254. size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
  255. out.crc32 = calc_crc32(body, body_len);
  256. /* Write the older slot first, then the newer slot.
  257. * If we crash between the two writes, the loader still finds a valid
  258. * copy in the previous-newer slot (with seq = next_seq - 1). */
  259. if (slot_write(older_addr, &out) != RT_EOK)
  260. {
  261. LOG_W("mirror write older slot (0x%08x) failed", older_addr);
  262. return -RT_ERROR;
  263. }
  264. if (slot_write(newer_addr, &out) != RT_EOK)
  265. {
  266. LOG_W("mirror write newer slot (0x%08x) failed", newer_addr);
  267. /* Still acceptable: loader can fall back to the older copy. */
  268. }
  269. return RT_EOK;
  270. }
  271. void param_mirror_pack_defaults(param_payload_t *out)
  272. {
  273. memset(out, 0, sizeof(*out));
  274. /* Caller will overwrite magic/version/seq/crc32 when actually saving.
  275. * Pre-fill magic/version here so a subsequent read still validates. */
  276. out->magic = MIRROR_MAGIC;
  277. out->version = PARAM_PAYLOAD_VER;
  278. /* write_seq left at 0; will be set on first save. */
  279. }
  280. /* The three helpers below bridge between device_status (defined in control.h)
  281. * and the packed mirror layout. They live here (instead of hmi.c) so any
  282. * code path that touches persistent config can call param_mirror_sync()
  283. * without needing to know device_status internals. */
  284. static void device_status_to_mirror(param_payload_t *out)
  285. {
  286. /* Zero the whole struct so header fields (magic/version/seq/crc32)
  287. * are clean before param_mirror_save() overwrites them. */
  288. memset(out, 0, sizeof(*out));
  289. out->iSave = device_status.iSave;
  290. out->dtype = device_status.dtype;
  291. out->ds = device_status.ds;
  292. out->clt_time = device_status.clt_time;
  293. out->heattime = device_status.heattime;
  294. out->heatvalue = device_status.heatvalue;
  295. out->thsp = device_status.thsp;
  296. out->sunBar = device_status.sunBar;
  297. out->startBar = device_status.startBar;
  298. out->stopBar = device_status.stopBar;
  299. out->timeSwitch = device_status.timeSwitch;
  300. out->tph = device_status.tph;
  301. out->tpl = device_status.tpl;
  302. out->shake_ws = device_status.shake_ws;
  303. out->shake_sec = device_status.shake_sec;
  304. out->passWord = device_status.passWord;
  305. out->dattim = device_status.dattim;
  306. out->version_type = device_status.version_type;
  307. out->Power_Type = device_status.Power_Type;
  308. out->handle_bugger = device_status.handle_bugger;
  309. }
  310. uint32_t mirror_to_device_status(const param_payload_t *in)
  311. {
  312. device_status.iSave = in->iSave;
  313. device_status.dtype = in->dtype;
  314. device_status.ds = in->ds;
  315. device_status.clt_time = in->clt_time;
  316. device_status.heattime = in->heattime;
  317. device_status.heatvalue = in->heatvalue;
  318. device_status.thsp = in->thsp;
  319. device_status.sunBar = in->sunBar;
  320. device_status.startBar = in->startBar;
  321. device_status.stopBar = in->stopBar;
  322. device_status.timeSwitch = in->timeSwitch;
  323. device_status.tph = in->tph;
  324. device_status.tpl = in->tpl;
  325. device_status.shake_ws = in->shake_ws;
  326. device_status.shake_sec = in->shake_sec;
  327. device_status.passWord = in->passWord;
  328. device_status.dattim = in->dattim;
  329. device_status.version_type = in->version_type;
  330. device_status.Power_Type = in->Power_Type;
  331. device_status.handle_bugger = in->handle_bugger;
  332. return in->write_seq;
  333. }
  334. /* NOTE: do NOT name the local `p` here — rtdbg.h defines a file-scope
  335. * `struct tm *p` and any LOG_* call inside this function would expand
  336. * `p = localtime(&timep)` and clash with our local type. */
  337. void param_mirror_sync(void)
  338. {
  339. param_payload_t payload;
  340. device_status_to_mirror(&payload);
  341. mirror_lock();
  342. /* 内容未变化:直接跳过。避免每条 JSON(心跳/查询应答) 都触发
  343. * 两次 4KB 扇区擦除,既保护 Flash 寿命也避免阻塞串口接收线程。 */
  344. if (s_last_valid && payload_body_equal(&payload, &s_last_payload))
  345. {
  346. mirror_unlock();
  347. return;
  348. }
  349. if (param_mirror_save(&payload) != RT_EOK)
  350. {
  351. LOG_W("param mirror save failed");
  352. mirror_unlock();
  353. return;
  354. }
  355. s_last_payload = payload;
  356. s_last_valid = RT_TRUE;
  357. /* 2026-10-15:参数内容确有变更 → 请求 DTU 线程把最新参数备份到 RTU
  358. * (本函数是所有参数变更路径的唯一汇聚点;实际发送由 dtu_param_pending_service
  359. * 在 RTU 通信正常后执行,避免与恢复流程抢跑) */
  360. dtu_request_param_backup();
  361. mirror_unlock();
  362. }