|
@@ -0,0 +1,163 @@
|
|
|
+/*
|
|
|
+ * sys_api.c
|
|
|
+ *
|
|
|
+ * Created on: 12.03.2017
|
|
|
+ * Author: jesstr
|
|
|
+ */
|
|
|
+#include <stdint.h>
|
|
|
+#include "sys_api.h"
|
|
|
+#include "sys_hal.h"
|
|
|
+#include "settings_api.h"
|
|
|
+#include "common_config.h"
|
|
|
+#ifdef PRINTF_STDLIB
|
|
|
+#include <stdio.h>
|
|
|
+#endif
|
|
|
+#ifdef PRINTF_CUSTOM
|
|
|
+#include "tinystdio.h"
|
|
|
+#endif
|
|
|
+#include "main.h"
|
|
|
+
|
|
|
+
|
|
|
+/* Backup old style device mac and serial */
|
|
|
+/* TODO Remove when all old devices will be updated */
|
|
|
+#define BACKUP_OLD_SYS
|
|
|
+
|
|
|
+
|
|
|
+static char mac_backup[MAC_LEN];
|
|
|
+static char serial_backup[MAC_LEN];
|
|
|
+static bool backup_done = false;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief Общая структура настроек
|
|
|
+ */
|
|
|
+extern SETTINGS_t sSettings;
|
|
|
+
|
|
|
+#ifdef BACKUP_OLD_SYS
|
|
|
+bool SYS_BackupInfo(char *mac, char *serial) {
|
|
|
+ snprintf(mac_backup, MAC_LEN, mac);
|
|
|
+ snprintf(serial_backup, SER_LEN, serial);
|
|
|
+ backup_done = true;
|
|
|
+
|
|
|
+ return backup_done;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool SYS_RestoreInfo(SYS_t *settings) {
|
|
|
+ if (backup_done) {
|
|
|
+ snprintf(settings->mac, MAC_LEN, mac_backup);
|
|
|
+ snprintf(settings->serial, SER_LEN, serial_backup);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+bool SYS_SetDefault(SYS_t *settings) {
|
|
|
+ if (!settings)
|
|
|
+ return false;
|
|
|
+
|
|
|
+#ifdef BACKUP_OLD_SYS
|
|
|
+ /* Try to backup device MAC and serial. */
|
|
|
+ if (sSettings.CritSecCRC == SETTINGS_GetCritSecCRC()) {
|
|
|
+ if (SYS_BackupInfo(sSettings.sInfo.mac, sSettings.sInfo.serialNumber)) {
|
|
|
+ DBG printf("Sys info backupped\r\n");
|
|
|
+ DBG printf("MAC: %s\r\n", sSettings.sInfo.mac);
|
|
|
+ DBG printf("Serial: %s\r\n", sSettings.sInfo.serialNumber);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ DBG printf("Sys info back up error!\r\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Try to restore device MAC and serial.
|
|
|
+ * If not, store defaults */
|
|
|
+ if (!SYS_RestoreInfo(settings)) {
|
|
|
+ snprintf(settings->mac, MAC_LEN, SYS_MAC);
|
|
|
+ snprintf(settings->serial, SER_LEN, SYS_SERIAL);
|
|
|
+ DBG printf("Sys info restore error! Defaults used.\r\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ DBG printf("Sys info restored!\r\n");
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ snprintf(settings->customer, CUST_LEN, SYS_CUSTOMER);
|
|
|
+ snprintf(settings->proddate, PROD_LEN, "00.00.00");
|
|
|
+ settings->controlword = SETTINGS_CONTROL_WORD;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief
|
|
|
+ * @retval
|
|
|
+ */
|
|
|
+uint32_t SYS_GetCRC(SYS_t *settings)
|
|
|
+{
|
|
|
+ CRC_ResetDR();
|
|
|
+ return CRC_CalcBlockCRC((uint32_t *)settings, sizeof(*settings)/4 - 1);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief Загрузка структуры системных настроек из flash
|
|
|
+ */
|
|
|
+bool SYS_Load(SYS_t *settings)
|
|
|
+{
|
|
|
+ uint32_t loadCRC; // CRC из flash
|
|
|
+ uint32_t newCRC; // CRC загруженной структуры настроек
|
|
|
+ bool need_default = false;
|
|
|
+
|
|
|
+ if (!settings)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ SYS_ReadFromFlash((uint8_t*)settings, sizeof(*settings), SYS_SECTOR);
|
|
|
+
|
|
|
+ /* Считываем CRC из флеш памяти */
|
|
|
+ loadCRC = (*(uint32_t*)SYS_CRC_ADDRESS);
|
|
|
+
|
|
|
+ /* Рассчитываем CRC для структуры настроек */
|
|
|
+ newCRC = SYS_GetCRC(settings);
|
|
|
+
|
|
|
+ /* Если CRC не совпадают нужно прошивать дефолтные настройки */
|
|
|
+ if (loadCRC != newCRC)
|
|
|
+ {
|
|
|
+ DBG printf("Bad system sector CRC. Factory defaults restored.\r\n");
|
|
|
+ need_default = true;
|
|
|
+ }
|
|
|
+ /* CRC совпала, проверяем контрольное слово если слово не совпадает
|
|
|
+ то это значит, что поплыла структура нстроек, прошиваем дефолт */
|
|
|
+ else if (settings->controlword != SETTINGS_CONTROL_WORD)
|
|
|
+ {
|
|
|
+ DBG printf("Bad system sector control word. Factory defaults restored.\r\n");
|
|
|
+ need_default = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Прошиваем дефолтные настройки если нужно */
|
|
|
+ if (need_default) {
|
|
|
+ SYS_SetDefault(settings);
|
|
|
+ SYS_Save(settings);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief Запись структуры настроек во flash
|
|
|
+ */
|
|
|
+bool SYS_Save(SYS_t *settings)
|
|
|
+{
|
|
|
+ uint32_t crc_user = 0;
|
|
|
+
|
|
|
+ if (!settings)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ crc_user = SYS_GetCRC(settings);
|
|
|
+
|
|
|
+ SYS_WriteToFlash((uint8_t*)settings, sizeof(*settings), crc_user);
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|