param_mirror.c 11 KB

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