log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. #include "log.h"
  2. #include "rtc.h"
  3. #include "ringfs.h"
  4. #include "spi_flash.h"
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include "semphr.h"
  8. #include <string.h>
  9. char logFileBuf[FILE_BUF_MAX_LEN];
  10. char name_login[50];
  11. char name_login_telnet[50];
  12. bool telnet_act = false;
  13. const char* logsStrShortRu[] =
  14. {
  15. "Перезагрузка контроллера",
  16. "Сброс настроек",
  17. "Обновление ПО",
  18. "Включение",
  19. "Смена пароля",
  20. "Сохранение настроек",
  21. #if defined HARDWARE_BT6706 || HARDWARE_BT6708
  22. "Авторизация (Telnet)",
  23. #endif
  24. "Авторизация",
  25. "Тест ИБП",
  26. "Откл. нагрузки ИБП",
  27. #ifndef HARDWARE_BT6708
  28. "Авария дискр. входа 1",
  29. "Состояние выхода 1",
  30. "Состояние выхода 2",
  31. #if defined HARDWARE_BT6706
  32. "Состояние выхода 3",
  33. #endif
  34. #endif
  35. #if defined HARDWARE_BT6706 || HARDWARE_BT6708
  36. "Авария вых. напряжения",
  37. #endif
  38. "Авария температуры",
  39. "Авария вх. напряжения",
  40. "Низкий заряд АКБ",
  41. "Авария нагрузки",
  42. "Авария связи с ИБП",
  43. "Авария отключения АКБ",
  44. };
  45. bool flUpdateLog = false;
  46. static bool fLogInit = false; // Флаг инициализации журнала
  47. #define LOG_TIME 1000*60*10
  48. #define LOG_FLASH_SECTOR_OFFSET 4
  49. #define ALARM_LOG_FLASH_SECTOR_OFFSET 258
  50. static int op_sector_erase(struct ringfs_flash_partition *flash, int address) {
  51. (void)flash;
  52. int ret;
  53. ret = spi_flash_erase_sector(address, 0);
  54. return ret;
  55. }
  56. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size) {
  57. (void)flash;
  58. int ret;
  59. ret = spi_flash_write(address, data, size, 0);
  60. return ret;
  61. }
  62. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size) {
  63. (void)flash;
  64. int ret;
  65. ret = spi_flash_read(address, data, size, 0);
  66. return ret;
  67. }
  68. static struct ringfs_flash_partition ringfs_flash = {
  69. .sector_offset = LOG_FLASH_SECTOR_OFFSET,
  70. .sector_erase = op_sector_erase,
  71. .program = op_program,
  72. .read = op_read,
  73. };
  74. static struct ringfs fs;
  75. static struct ringfs_flash_partition ringfs_flash2 = {
  76. .sector_offset = ALARM_LOG_FLASH_SECTOR_OFFSET,
  77. .sector_erase = op_sector_erase,
  78. .program = op_program,
  79. .read = op_read,
  80. };
  81. static struct ringfs fs2;
  82. static SemaphoreHandle_t log_mutex;
  83. /**
  84. * @brief Отключает журнал для безопасной перезагрузки
  85. */
  86. bool LOG_Disable(void)
  87. {
  88. if (fLogInit) {
  89. /* Ожидаем завершения работы с журнал */
  90. if ( xSemaphoreTake(log_mutex, 10000) == pdTRUE ) {
  91. //fLogInit = false;
  92. //xSemaphoreGive(logMutex);
  93. return true;
  94. }
  95. else {
  96. return false;
  97. }
  98. }
  99. else {
  100. return true;
  101. }
  102. }
  103. void log_task(void* params)
  104. {
  105. for(;;){
  106. flUpdateLog = true;
  107. vTaskDelay(LOG_TIME);
  108. /*vTaskDelay(50);
  109. log_event_data(LOG_SYSTEM_BOOT, "Администратор");
  110. log_add(")215.7;215.7;220.5;000;50.1;2.30;25.0;00000001;");*/
  111. }
  112. }
  113. void log_init(bool format) {
  114. DBG printf(">>> Event log\n");
  115. if (!spi_flash_desc.present)
  116. return;
  117. ringfs_flash.sector_size = spi_flash_desc.sector_size;
  118. ringfs_flash.sector_count = spi_flash_desc.sector_count/2 - LOG_FLASH_SECTOR_OFFSET;
  119. ringfs_init(&fs, &ringfs_flash, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  120. if (format || ringfs_scan(&fs) != 0){
  121. DBG printf("FAT1 false\r\n");
  122. ringfs_format(&fs);
  123. }
  124. DBG printf("FAT1 true\r\n");
  125. ringfs_flash2.sector_size = spi_flash_desc.sector_size;
  126. ringfs_flash2.sector_count = spi_flash_desc.sector_count/2 - LOG_FLASH_SECTOR_OFFSET;
  127. ringfs_init(&fs2, &ringfs_flash2, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  128. if (format || ringfs_scan(&fs2) != 0){
  129. DBG printf("FAT2 false\r\n");
  130. ringfs_format(&fs2);
  131. }
  132. DBG printf("FAT2 true\r\n");
  133. fLogInit = true;
  134. log_mutex = xSemaphoreCreateMutex();
  135. xTaskCreate(log_task, ( char * ) "log_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  136. }
  137. int capacity_flash = 0;
  138. int count_flash = 0;
  139. int log_test(void) {
  140. int ret;
  141. log_entry_t entry;
  142. log_init(false);
  143. capacity_flash = ringfs_capacity(&fs);
  144. count_flash = ringfs_count_exact(&fs);
  145. DBG printf("\tCapacity: %d\n", capacity_flash);
  146. DBG printf("\tCount: %d\n", count_flash);
  147. DBG printf("\tAppending ");
  148. // ret = log_event(LOG_SYSTEM_DEFCONFIG, 0, 0);
  149. DBG printf("%s\n", ret == 0 ? "ok" : "error");
  150. if (ret == 0)
  151. return -1;
  152. // ret = log_event(LOG_SYSTEM_DEFCONFIG, 0, 512);
  153. entry.timestamp = 0;
  154. entry.type = 0;
  155. DBG printf("\tFetching ");
  156. if (log_fetch(&entry, portMAX_DELAY) == 0){
  157. DBG printf("ok, time=%d, type=%d\n", entry.timestamp, entry.type);
  158. log_fetch(&entry, portMAX_DELAY);
  159. entry.timestamp = 0;
  160. entry.type = 0;
  161. log_fetch(&entry, portMAX_DELAY);
  162. entry.timestamp = 0;
  163. entry.type = 0;
  164. log_fetch(&entry, portMAX_DELAY);
  165. entry.timestamp = 0;
  166. entry.type = 0;
  167. log_fetch(&entry, portMAX_DELAY);
  168. return 0;
  169. }
  170. else {
  171. DBG printf("fail\n");
  172. return -1;
  173. }
  174. DBG printf("\tDiscarding ");
  175. if (log_discard(&entry,portMAX_DELAY) == 0)
  176. DBG printf("ok\n");
  177. else {
  178. DBG printf("fail\n");
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. int log_append(log_entry_t *entry) {
  184. int ret;
  185. TM_RTC_t data;
  186. ret = xSemaphoreTake( log_mutex, portMAX_DELAY );
  187. if (ret == pdFALSE)
  188. return ret;
  189. if (!entry->timestamp){
  190. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  191. entry->timestamp = data.unix;
  192. }
  193. if(entry->type == LOG_VALUE)
  194. ringfs_append(&fs, entry);
  195. else
  196. ringfs_append(&fs2, entry);
  197. xSemaphoreGive(log_mutex);
  198. return ret;
  199. }
  200. int log_fetch(log_entry_t *entry, uint32_t timeout) {
  201. int ret;
  202. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  203. if (ret == pdFALSE)
  204. return ret;
  205. if(entry->type == LOG_VALUE)
  206. ret = ringfs_fetch(&fs, entry);
  207. else
  208. ret = ringfs_fetch(&fs2, entry);
  209. xSemaphoreGive(log_mutex);
  210. return ret;
  211. }
  212. int log_rewind(log_entry_t *entry, uint32_t timeout) {
  213. int ret;
  214. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  215. if (ret == pdFALSE)
  216. return ret;
  217. if(entry->type == LOG_VALUE)
  218. ret = ringfs_rewind(&fs);
  219. else
  220. ret = ringfs_rewind(&fs2);
  221. xSemaphoreGive(log_mutex);
  222. return ret;
  223. }
  224. int log_discard(log_entry_t *entry, uint32_t timeout) {
  225. int ret;
  226. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  227. if (ret == pdFALSE)
  228. return ret;
  229. if(entry->type == LOG_VALUE)
  230. ret = ringfs_discard(&fs);
  231. else
  232. ret = ringfs_discard(&fs2);
  233. xSemaphoreGive(log_mutex);
  234. return ret;
  235. }
  236. void log_event_data(log_type_t type, char *data)
  237. {
  238. log_entry_t entry_data;
  239. entry_data.timestamp = 0;
  240. entry_data.type = type;
  241. strncpy(entry_data.data, data, 49);
  242. if (fLogInit)
  243. log_append(&entry_data);
  244. }
  245. void log_add(char *log_data)
  246. {
  247. char buf_value[50];
  248. uint8_t i, len;
  249. memset(buf_value, 0, 50);
  250. len = strlen(log_data);
  251. strncpy(buf_value, log_data, len);
  252. DBG printf("UPS log data: %s\r\n", log_data);
  253. buf_value[0] = '\"';
  254. for(i = 0; i < len; i++)
  255. {
  256. if(buf_value[i] == ' ')
  257. buf_value[i] = ';';
  258. }
  259. buf_value[len - 1] = ';';
  260. if(fLogInit){
  261. if(fs.write.slot>67)
  262. {
  263. log_entry_t entry_data;
  264. entry_data.timestamp = 0;
  265. log_event_data(LOG_VALUE, buf_value);
  266. }
  267. else
  268. log_event_data(LOG_VALUE, buf_value);
  269. }
  270. }
  271. /**
  272. * @brief Возвращает true если журнал проинициализирован
  273. */
  274. bool LOG_IsInit()
  275. {
  276. return fLogInit;
  277. }
  278. /**
  279. * @brief Возвращает общее количество страниц
  280. */
  281. uint32_t LOG_GetPageCount(void)
  282. {
  283. return (((ringfs_count_estimate(&fs)) / 10) + 1);
  284. }
  285. uint32_t LOG_GetTotalSTRCount(void)
  286. {
  287. return ringfs_count_estimate(&fs);
  288. }
  289. void LOG_GetPage_tabs(char *str, uint32_t page)
  290. {
  291. TM_RTC_t rtc_data;
  292. log_entry_t entry;
  293. char buf[20];
  294. uint8_t i;
  295. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  296. memset(buf, 0, 20);
  297. for(i=0; i < 10; i++){
  298. fs.cursor_position = start - 10*(page-1) - 1 - i;
  299. if(fs.cursor_position < 0)
  300. break;
  301. else{
  302. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  303. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  304. }
  305. entry.type = LOG_VALUE;
  306. log_fetch(&entry, portMAX_DELAY);
  307. entry.data[49] = 0;
  308. strncat(str, entry.data, strlen(entry.data));
  309. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  310. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  311. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  312. strcat(str, buf);
  313. strcat(str, "\",");
  314. strcat(str, "\r\n");
  315. }
  316. }
  317. void LOG_GetPage(char *str, uint32_t page)
  318. {
  319. TM_RTC_t rtc_data;
  320. log_entry_t entry;
  321. char buf[20];
  322. uint8_t i;
  323. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  324. memset(buf, 0, 20);
  325. for(i=0; i < 10; i++){
  326. fs.cursor_position = start - 10*(page-1) - 1 - i;
  327. if(fs.cursor_position < 0)
  328. break;
  329. else{
  330. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  331. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  332. }
  333. entry.type = LOG_VALUE;
  334. log_fetch(&entry, portMAX_DELAY);
  335. entry.data[49] = 0;
  336. strncat(str, entry.data, strlen(entry.data));
  337. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  338. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  339. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  340. strcat(str, buf);
  341. strcat(str, "\",");
  342. }
  343. }
  344. uint32_t LOG_GetData(int ptr, char *str, uint32_t size, bool start)
  345. {
  346. TM_RTC_t rtc_data;
  347. log_entry_t entry;
  348. char buf[20];
  349. uint8_t i;
  350. entry.type = LOG_VALUE;
  351. if(start)
  352. log_rewind(&entry, portMAX_DELAY);
  353. fs.cursor_position = ptr/STRING_SIZE;
  354. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  355. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  356. for(i = 0; i < size/STRING_SIZE; i++)
  357. {
  358. entry.type = LOG_VALUE;
  359. log_fetch(&entry, portMAX_DELAY);
  360. entry.data[49] = 0;
  361. strncat(str, &entry.data[1], (strlen(entry.data) - 1));
  362. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  363. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  364. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  365. strcat(str, buf);
  366. strcat(str, "\n");
  367. }
  368. return strlen(str);
  369. }
  370. /**
  371. * @brief Возвращает общее количество страниц
  372. */
  373. uint32_t History_GetPageCount(void)
  374. {
  375. return (((ringfs_count_estimate(&fs2)) / 10) + 1);
  376. }
  377. uint32_t History_GetTotalSTRCount(void)
  378. {
  379. return ringfs_count_estimate(&fs2);
  380. }
  381. void History_GetPage(char *str, uint32_t page)
  382. {
  383. TM_RTC_t rtc_data;
  384. log_entry_t entry;
  385. char buf[20];
  386. uint8_t i;
  387. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  388. memset(buf, 0, 20);
  389. for(i=0; i < 10; i++){
  390. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  391. if(fs2.cursor_position < 0)
  392. break;
  393. else{
  394. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  395. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  396. }
  397. entry.type = LOG_LOGIN;
  398. log_fetch(&entry, portMAX_DELAY);
  399. strcat(str, "\"");
  400. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  401. strcat(str, ";");
  402. entry.data[49] = 0;
  403. strncat(str, entry.data, (strlen(entry.data) ));
  404. strcat(str, ";");
  405. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  406. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  407. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  408. strcat(str, buf);
  409. strcat(str, "\",");
  410. }
  411. }
  412. void History_GetPage_tabs(char *str, uint32_t page)
  413. {
  414. TM_RTC_t rtc_data;
  415. log_entry_t entry;
  416. char buf[50];
  417. uint8_t i;
  418. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  419. memset(buf, 0, 50);
  420. for(i=0; i < 10; i++){
  421. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  422. if(fs2.cursor_position < 0)
  423. break;
  424. else{
  425. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  426. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  427. }
  428. entry.type = LOG_LOGIN;
  429. log_fetch(&entry, portMAX_DELAY);
  430. //sprintf(buf, "%-50s", logsStrShortRu[entry.type]);
  431. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  432. for(uint8_t j = 0; j < (25 - strlen(logsStrShortRu[entry.type])/2); j ++)
  433. strcat(str, " ");
  434. strcat(str, "\t");
  435. memset(buf, 0, 50);
  436. entry.data[49] = 0;
  437. strncat(str, entry.data, (strlen(entry.data) ));
  438. for(uint8_t j = 0; j < (25 - strlen(entry.data)/2); j ++)
  439. strcat(str, " ");
  440. strcat(str, "\t");
  441. memset(buf, 0, 50);
  442. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  443. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  444. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  445. strcat(str, buf);
  446. strcat(str, "\r\n");
  447. }
  448. }
  449. uint32_t History_GetData(int ptr, char *str, uint32_t size, bool start)
  450. {
  451. TM_RTC_t rtc_data;
  452. log_entry_t entry;
  453. char buf[20];
  454. char temp_str[FILE_BUF_MAX_LEN];
  455. uint8_t i;
  456. uint16_t len;
  457. entry.type = LOG_LOGIN;
  458. if(start)
  459. log_rewind(&entry, portMAX_DELAY);
  460. fs2.cursor_position = ptr/STRING_SIZE_HISTORY;
  461. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs2.slots_per_sector)%fs2.flash->sector_count;
  462. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  463. for(i = 0; i < size/STRING_SIZE_HISTORY; i++)
  464. {
  465. memset(temp_str, 0, 100);
  466. log_fetch(&entry, portMAX_DELAY);
  467. strncat(temp_str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type])));
  468. strcat(temp_str, ";");
  469. entry.data[49] = 0;
  470. strncat(temp_str, entry.data, (strlen(entry.data)));
  471. strcat(temp_str, ";");
  472. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  473. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  474. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  475. strcat(temp_str, buf);
  476. len = strlen(temp_str);
  477. if (len <= STRING_SIZE_HISTORY - 1)
  478. {
  479. memset(&temp_str[len], ' ', STRING_SIZE_HISTORY - len - 1);
  480. strcat(temp_str, "\n");
  481. }
  482. else
  483. {
  484. temp_str[STRING_SIZE - 1] = 0xa;
  485. }
  486. strncat(str, temp_str, STRING_SIZE_HISTORY);
  487. }
  488. return strlen(str);
  489. }