log.c 15 KB

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