log.c 15 KB

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