sys_api.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * sys_api.c
  3. *
  4. * Created on: 12.03.2017
  5. * Author: jesstr
  6. */
  7. #include <stdint.h>
  8. #include "sys_api.h"
  9. #include "sys_hal.h"
  10. #include "settings_api.h"
  11. #include "common_config.h"
  12. #ifdef PRINTF_STDLIB
  13. #include <stdio.h>
  14. #endif
  15. #ifdef PRINTF_CUSTOM
  16. #include "tinystdio.h"
  17. #endif
  18. #include "main.h"
  19. /* Backup old style device mac and serial */
  20. /* TODO Remove when all old devices will be updated */
  21. #define BACKUP_OLD_SYS
  22. static char mac_backup[MAC_LEN];
  23. static char serial_backup[MAC_LEN];
  24. static bool backup_done = false;
  25. /**
  26. * @brief Общая структура настроек
  27. */
  28. extern SETTINGS_t sSettings;
  29. #ifdef BACKUP_OLD_SYS
  30. bool SYS_BackupInfo(char *mac, char *serial) {
  31. snprintf(mac_backup, MAC_LEN, mac);
  32. snprintf(serial_backup, SER_LEN, serial);
  33. backup_done = true;
  34. return backup_done;
  35. }
  36. bool SYS_RestoreInfo(SYS_t *settings) {
  37. if (backup_done) {
  38. snprintf(settings->mac, MAC_LEN, mac_backup);
  39. snprintf(settings->serial, SER_LEN, serial_backup);
  40. return true;
  41. }
  42. return false;
  43. }
  44. #endif
  45. bool SYS_SetDefault(SYS_t *settings) {
  46. if (!settings)
  47. return false;
  48. #ifdef BACKUP_OLD_SYS
  49. /* Try to backup device MAC and serial. */
  50. if (sSettings.CritSecCRC == SETTINGS_GetCritSecCRC()) {
  51. if (SYS_BackupInfo(sSettings.sInfo.mac, sSettings.sInfo.serialNumber)) {
  52. DBG printf("Sys info backupped\r\n");
  53. DBG printf("MAC: %s\r\n", sSettings.sInfo.mac);
  54. DBG printf("Serial: %s\r\n", sSettings.sInfo.serialNumber);
  55. }
  56. else {
  57. DBG printf("Sys info back up error!\r\n");
  58. }
  59. }
  60. /* Try to restore device MAC and serial.
  61. * If not, store defaults */
  62. if (!SYS_RestoreInfo(settings)) {
  63. snprintf(settings->mac, MAC_LEN, SYS_MAC);
  64. snprintf(settings->serial, SER_LEN, SYS_SERIAL);
  65. DBG printf("Sys info restore error! Defaults used.\r\n");
  66. }
  67. else {
  68. DBG printf("Sys info restored!\r\n");
  69. }
  70. #endif
  71. snprintf(settings->customer, CUST_LEN, SYS_CUSTOMER);
  72. snprintf(settings->proddate, PROD_LEN, "00.00.00");
  73. settings->controlword = SETTINGS_CONTROL_WORD;
  74. return true;
  75. }
  76. /**
  77. * @brief
  78. * @retval
  79. */
  80. uint32_t SYS_GetCRC(SYS_t *settings)
  81. {
  82. CRC_ResetDR();
  83. return CRC_CalcBlockCRC((uint32_t *)settings, sizeof(*settings)/4 - 1);
  84. }
  85. /**
  86. * @brief Загрузка структуры системных настроек из flash
  87. */
  88. bool SYS_Load(SYS_t *settings)
  89. {
  90. uint32_t loadCRC; // CRC из flash
  91. uint32_t newCRC; // CRC загруженной структуры настроек
  92. bool need_default = false;
  93. if (!settings)
  94. return false;
  95. SYS_ReadFromFlash((uint8_t*)settings, sizeof(*settings), SYS_SECTOR);
  96. /* Считываем CRC из флеш памяти */
  97. loadCRC = (*(uint32_t*)SYS_CRC_ADDRESS);
  98. /* Рассчитываем CRC для структуры настроек */
  99. newCRC = SYS_GetCRC(settings);
  100. /* Если CRC не совпадают нужно прошивать дефолтные настройки */
  101. if (loadCRC != newCRC)
  102. {
  103. DBG printf("Bad system sector CRC. Factory defaults restored.\r\n");
  104. need_default = true;
  105. }
  106. /* CRC совпала, проверяем контрольное слово если слово не совпадает
  107. то это значит, что поплыла структура нстроек, прошиваем дефолт */
  108. else if (settings->controlword != SETTINGS_CONTROL_WORD)
  109. {
  110. DBG printf("Bad system sector control word. Factory defaults restored.\r\n");
  111. need_default = true;
  112. }
  113. /* Прошиваем дефолтные настройки если нужно */
  114. if (need_default) {
  115. SYS_SetDefault(settings);
  116. SYS_Save(settings);
  117. }
  118. return true;
  119. }
  120. /**
  121. * @brief Запись структуры настроек во flash
  122. */
  123. bool SYS_Save(SYS_t *settings)
  124. {
  125. uint32_t crc_user = 0;
  126. if (!settings)
  127. return false;
  128. crc_user = SYS_GetCRC(settings);
  129. SYS_WriteToFlash((uint8_t*)settings, sizeof(*settings), crc_user);
  130. return true;
  131. }