sys_api.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. snprintf(settings->testState, 5, "T2OK");
  74. settings->controlword = SETTINGS_CONTROL_WORD;
  75. return true;
  76. }
  77. /**
  78. * @brief
  79. * @retval
  80. */
  81. uint32_t SYS_GetCRC(SYS_t *settings)
  82. {
  83. CRC_ResetDR();
  84. return CRC_CalcBlockCRC((uint32_t *)settings, sizeof(*settings)/4 - 1);
  85. }
  86. /**
  87. * @brief Загрузка структуры системных настроек из flash
  88. */
  89. bool SYS_Load(SYS_t *settings)
  90. {
  91. uint32_t loadCRC; // CRC из flash
  92. uint32_t newCRC; // CRC загруженной структуры настроек
  93. bool need_default = false;
  94. if (!settings)
  95. return false;
  96. SYS_ReadFromFlash((uint8_t*)settings, sizeof(*settings), SYS_SECTOR);
  97. /* Считываем CRC из флеш памяти */
  98. loadCRC = (*(uint32_t*)SYS_CRC_ADDRESS);
  99. /* Рассчитываем CRC для структуры настроек */
  100. newCRC = SYS_GetCRC(settings);
  101. /* Если CRC не совпадают нужно прошивать дефолтные настройки */
  102. if (loadCRC != newCRC)
  103. {
  104. DBG printf("Bad system sector CRC. Factory defaults restored.\r\n");
  105. need_default = true;
  106. }
  107. /* CRC совпала, проверяем контрольное слово если слово не совпадает
  108. то это значит, что поплыла структура нстроек, прошиваем дефолт */
  109. else if (settings->controlword != SETTINGS_CONTROL_WORD)
  110. {
  111. DBG printf("Bad system sector control word. Factory defaults restored.\r\n");
  112. need_default = true;
  113. }
  114. /* Прошиваем дефолтные настройки если нужно */
  115. if (need_default) {
  116. SYS_SetDefault(settings);
  117. SYS_Save(settings);
  118. }
  119. return true;
  120. }
  121. /**
  122. * @brief Запись структуры настроек во flash
  123. */
  124. bool SYS_Save(SYS_t *settings)
  125. {
  126. uint32_t crc_user = 0;
  127. if (!settings)
  128. return false;
  129. crc_user = SYS_GetCRC(settings);
  130. SYS_WriteToFlash((uint8_t*)settings, sizeof(*settings), crc_user);
  131. return true;
  132. }