log.c 15 KB

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