param_mirror.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #define DBG_TAG "param_mirror"
  14. #define DBG_LVL DBG_LOG
  15. #include <rtdbg.h>
  16. #define MIRROR_PART_NAME "easyflash"
  17. #define MIRROR_MAGIC 0x50415242U /* 'PARB' */
  18. /* Header lives in the first 16 bytes of each slot, payload follows. */
  19. #define HDR_SIZE 16U
  20. Device_Status_typedef device_status;
  21. static const struct fal_partition *s_part = RT_NULL;
  22. static uint32_t calc_crc32(const void *buf, size_t size)
  23. {
  24. /* EF ships ef_calc_crc32; reuse to avoid bringing our own table. */
  25. extern uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size);
  26. return ef_calc_crc32(0, buf, size);
  27. }
  28. static int slot_read(uint32_t slot_addr, param_payload_t *out)
  29. {
  30. if (s_part == RT_NULL)
  31. {
  32. return -RT_ERROR;
  33. }
  34. return (fal_partition_read(s_part, slot_addr, (uint8_t *)out,
  35. sizeof(param_payload_t)) > 0)
  36. ? RT_EOK : -RT_ERROR;
  37. }
  38. static int slot_write(uint32_t slot_addr, const param_payload_t *in)
  39. {
  40. if (s_part == RT_NULL)
  41. {
  42. return -RT_ERROR;
  43. }
  44. if (fal_partition_erase(s_part, slot_addr, MIRROR_SLOT_SIZE) < 0)
  45. {
  46. return -RT_ERROR;
  47. }
  48. if (fal_partition_write(s_part, slot_addr, (const uint8_t *)in,
  49. sizeof(param_payload_t)) < 0)
  50. {
  51. return -RT_ERROR;
  52. }
  53. return RT_EOK;
  54. }
  55. static bool slot_is_valid(const param_payload_t *p)
  56. {
  57. if (p->magic != MIRROR_MAGIC)
  58. {
  59. return false;
  60. }
  61. if (p->version != PARAM_PAYLOAD_VER)
  62. {
  63. return false;
  64. }
  65. /* crc32 covers everything after the 16-byte header. */
  66. const uint8_t *body = (const uint8_t *)p + HDR_SIZE;
  67. size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
  68. uint32_t crc = calc_crc32(body, body_len);
  69. return crc == p->crc32;
  70. }
  71. int param_mirror_load(param_payload_t *dst)
  72. {
  73. if (dst == RT_NULL)
  74. {
  75. return -RT_ERROR;
  76. }
  77. if (s_part == RT_NULL)
  78. {
  79. s_part = fal_partition_find(MIRROR_PART_NAME);
  80. if (s_part == RT_NULL)
  81. {
  82. return -RT_ERROR;
  83. }
  84. }
  85. param_payload_t a, b;
  86. bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
  87. bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
  88. if (!va && !vb)
  89. {
  90. return -RT_ERROR;
  91. }
  92. if (va && !vb)
  93. {
  94. *dst = a;
  95. return RT_EOK;
  96. }
  97. if (!va && vb)
  98. {
  99. *dst = b;
  100. return RT_EOK;
  101. }
  102. /* both valid: pick the one with higher write_seq */
  103. *dst = (a.write_seq >= b.write_seq) ? a : b;
  104. return RT_EOK;
  105. }
  106. int param_mirror_save(const param_payload_t *src)
  107. {
  108. if (src == RT_NULL)
  109. {
  110. return -RT_ERROR;
  111. }
  112. if (s_part == RT_NULL)
  113. {
  114. s_part = fal_partition_find(MIRROR_PART_NAME);
  115. if (s_part == RT_NULL)
  116. {
  117. return -RT_ERROR;
  118. }
  119. }
  120. /* Determine which slot is older by reading both and comparing write_seq. */
  121. param_payload_t a, b;
  122. bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
  123. bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
  124. param_payload_t newer, older;
  125. uint32_t newer_addr, older_addr;
  126. uint32_t next_seq;
  127. if (!va && !vb)
  128. {
  129. /* cold start: both empty. Write B first, then A. */
  130. newer_addr = MIRROR_SLOT_B_ADDR;
  131. older_addr = MIRROR_SLOT_A_ADDR;
  132. next_seq = 1;
  133. }
  134. else if (va && !vb)
  135. {
  136. newer = a;
  137. newer_addr = MIRROR_SLOT_A_ADDR;
  138. older_addr = MIRROR_SLOT_B_ADDR;
  139. next_seq = newer.write_seq + 1;
  140. }
  141. else if (!va && vb)
  142. {
  143. newer = b;
  144. newer_addr = MIRROR_SLOT_B_ADDR;
  145. older_addr = MIRROR_SLOT_A_ADDR;
  146. next_seq = newer.write_seq + 1;
  147. }
  148. else
  149. {
  150. /* both valid */
  151. if (a.write_seq >= b.write_seq)
  152. {
  153. newer = a; newer_addr = MIRROR_SLOT_A_ADDR;
  154. older = b; older_addr = MIRROR_SLOT_B_ADDR;
  155. }
  156. else
  157. {
  158. newer = b; newer_addr = MIRROR_SLOT_B_ADDR;
  159. older = a; older_addr = MIRROR_SLOT_A_ADDR;
  160. }
  161. next_seq = newer.write_seq + 1;
  162. }
  163. /* Build the record. Reuse the caller's payload for everything except
  164. * magic/version/seq/crc32, which we own. */
  165. param_payload_t out = *src;
  166. out.magic = MIRROR_MAGIC;
  167. out.version = PARAM_PAYLOAD_VER;
  168. out.write_seq = next_seq;
  169. /* Recompute crc32 over the body (everything past the 16-byte header). */
  170. uint8_t *body = (uint8_t *)&out + HDR_SIZE;
  171. size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
  172. out.crc32 = calc_crc32(body, body_len);
  173. /* Write the older slot first, then the newer slot.
  174. * If we crash between the two writes, the loader still finds a valid
  175. * copy in the previous-newer slot (with seq = next_seq - 1). */
  176. if (slot_write(older_addr, &out) != RT_EOK)
  177. {
  178. LOG_W("mirror write older slot (0x%08x) failed", older_addr);
  179. return -RT_ERROR;
  180. }
  181. if (slot_write(newer_addr, &out) != RT_EOK)
  182. {
  183. LOG_W("mirror write newer slot (0x%08x) failed", newer_addr);
  184. /* Still acceptable: loader can fall back to the older copy. */
  185. }
  186. return RT_EOK;
  187. }
  188. void param_mirror_pack_defaults(param_payload_t *out)
  189. {
  190. memset(out, 0, sizeof(*out));
  191. /* Caller will overwrite magic/version/seq/crc32 when actually saving.
  192. * Pre-fill magic/version here so a subsequent read still validates. */
  193. out->magic = MIRROR_MAGIC;
  194. out->version = PARAM_PAYLOAD_VER;
  195. /* write_seq left at 0; will be set on first save. */
  196. }
  197. /* The three helpers below bridge between device_status (defined in control.h)
  198. * and the packed mirror layout. They live here (instead of hmi.c) so any
  199. * code path that touches persistent config can call param_mirror_sync()
  200. * without needing to know device_status internals. */
  201. static void device_status_to_mirror(param_payload_t *out)
  202. {
  203. /* Zero the whole struct so header fields (magic/version/seq/crc32)
  204. * are clean before param_mirror_save() overwrites them. */
  205. memset(out, 0, sizeof(*out));
  206. out->iSave = device_status.iSave;
  207. out->dtype = device_status.dtype;
  208. out->ds = device_status.ds;
  209. out->clt_time = device_status.clt_time;
  210. out->heattime = device_status.heattime;
  211. out->heatvalue = device_status.heatvalue;
  212. out->thsp = device_status.thsp;
  213. out->sunBar = device_status.sunBar;
  214. out->startBar = device_status.startBar;
  215. out->stopBar = device_status.stopBar;
  216. out->timeSwitch = device_status.timeSwitch;
  217. out->tph = device_status.tph;
  218. out->tpl = device_status.tpl;
  219. out->shake_ws = device_status.shake_ws;
  220. out->shake_sec = device_status.shake_sec;
  221. out->passWord = device_status.passWord;
  222. out->dattim = device_status.dattim;
  223. out->version_type = device_status.version_type;
  224. out->Power_Type = device_status.Power_Type;
  225. out->handle_bugger = device_status.handle_bugger;
  226. }
  227. uint32_t mirror_to_device_status(const param_payload_t *in)
  228. {
  229. device_status.iSave = in->iSave;
  230. device_status.dtype = in->dtype;
  231. device_status.ds = in->ds;
  232. device_status.clt_time = in->clt_time;
  233. device_status.heattime = in->heattime;
  234. device_status.heatvalue = in->heatvalue;
  235. device_status.thsp = in->thsp;
  236. device_status.sunBar = in->sunBar;
  237. device_status.startBar = in->startBar;
  238. device_status.stopBar = in->stopBar;
  239. device_status.timeSwitch = in->timeSwitch;
  240. device_status.tph = in->tph;
  241. device_status.tpl = in->tpl;
  242. device_status.shake_ws = in->shake_ws;
  243. device_status.shake_sec = in->shake_sec;
  244. device_status.passWord = in->passWord;
  245. device_status.dattim = in->dattim;
  246. device_status.version_type = in->version_type;
  247. device_status.Power_Type = in->Power_Type;
  248. device_status.handle_bugger = in->handle_bugger;
  249. return in->write_seq;
  250. }
  251. /* NOTE: do NOT name the local `p` here — rtdbg.h defines a file-scope
  252. * `struct tm *p` and any LOG_* call inside this function would expand
  253. * `p = localtime(&timep)` and clash with our local type. */
  254. void param_mirror_sync(void)
  255. {
  256. param_payload_t payload;
  257. device_status_to_mirror(&payload);
  258. if (param_mirror_save(&payload) != RT_EOK)
  259. {
  260. LOG_W("param mirror save failed");
  261. }
  262. }