log.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. buf_value[0] = '\"';
  249. for(i = 0; i < len; i++)
  250. {
  251. if(buf_value[i] == ' ')
  252. buf_value[i] = ';';
  253. }
  254. buf_value[len - 1] = ';';
  255. if(fLogInit){
  256. if(fs.write.slot>67)
  257. {
  258. log_entry_t entry_data;
  259. entry_data.timestamp = 0;
  260. log_event_data(LOG_VALUE, buf_value);
  261. }
  262. else
  263. log_event_data(LOG_VALUE, buf_value);
  264. }
  265. }
  266. /**
  267. * @brief Возвращает true если журнал проинициализирован
  268. */
  269. bool LOG_IsInit()
  270. {
  271. return fLogInit;
  272. }
  273. /**
  274. * @brief Возвращает общее количество страниц
  275. */
  276. uint32_t LOG_GetPageCount(void)
  277. {
  278. return (((ringfs_count_estimate(&fs)) / 10) + 1);
  279. }
  280. uint32_t LOG_GetTotalSTRCount(void)
  281. {
  282. return ringfs_count_estimate(&fs);
  283. }
  284. void LOG_GetPage_tabs(char *str, uint32_t page)
  285. {
  286. TM_RTC_t rtc_data;
  287. log_entry_t entry;
  288. char buf[20];
  289. uint8_t i;
  290. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  291. memset(buf, 0, 20);
  292. for(i=0; i < 10; i++){
  293. fs.cursor_position = start - 10*(page-1) - 1 - i;
  294. if(fs.cursor_position < 0)
  295. break;
  296. else{
  297. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  298. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  299. }
  300. entry.type = LOG_VALUE;
  301. log_fetch(&entry, portMAX_DELAY);
  302. entry.data[49] = 0;
  303. strncat(str, entry.data, strlen(entry.data));
  304. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  305. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  306. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  307. strcat(str, buf);
  308. strcat(str, "\",");
  309. strcat(str, "\r\n");
  310. }
  311. }
  312. void LOG_GetPage(char *str, uint32_t page)
  313. {
  314. TM_RTC_t rtc_data;
  315. log_entry_t entry;
  316. char buf[20];
  317. uint8_t i;
  318. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  319. memset(buf, 0, 20);
  320. for(i=0; i < 10; i++){
  321. fs.cursor_position = start - 10*(page-1) - 1 - i;
  322. if(fs.cursor_position < 0)
  323. break;
  324. else{
  325. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  326. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  327. }
  328. entry.type = LOG_VALUE;
  329. log_fetch(&entry, portMAX_DELAY);
  330. entry.data[49] = 0;
  331. strncat(str, entry.data, strlen(entry.data));
  332. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  333. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  334. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  335. strcat(str, buf);
  336. strcat(str, "\",");
  337. }
  338. }
  339. uint32_t LOG_GetData(int ptr, char *str, uint32_t size, bool start)
  340. {
  341. TM_RTC_t rtc_data;
  342. log_entry_t entry;
  343. char buf[20];
  344. uint8_t i;
  345. entry.type = LOG_VALUE;
  346. if(start)
  347. log_rewind(&entry, portMAX_DELAY);
  348. fs.cursor_position = ptr/STRING_SIZE;
  349. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  350. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  351. for(i = 0; i < size/STRING_SIZE; i++)
  352. {
  353. entry.type = LOG_VALUE;
  354. log_fetch(&entry, portMAX_DELAY);
  355. entry.data[49] = 0;
  356. strncat(str, &entry.data[1], (strlen(entry.data) - 1));
  357. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  358. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  359. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  360. strcat(str, buf);
  361. strcat(str, "\n");
  362. }
  363. return strlen(str);
  364. }
  365. /**
  366. * @brief Возвращает общее количество страниц
  367. */
  368. uint32_t History_GetPageCount(void)
  369. {
  370. return (((ringfs_count_estimate(&fs2)) / 10) + 1);
  371. }
  372. uint32_t History_GetTotalSTRCount(void)
  373. {
  374. return ringfs_count_estimate(&fs2);
  375. }
  376. void History_GetPage(char *str, uint32_t page)
  377. {
  378. TM_RTC_t rtc_data;
  379. log_entry_t entry;
  380. char buf[20];
  381. uint8_t i;
  382. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  383. memset(buf, 0, 20);
  384. for(i=0; i < 10; i++){
  385. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  386. if(fs2.cursor_position < 0)
  387. break;
  388. else{
  389. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  390. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  391. }
  392. entry.type = LOG_LOGIN;
  393. log_fetch(&entry, portMAX_DELAY);
  394. strcat(str, "\"");
  395. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  396. strcat(str, ";");
  397. entry.data[49] = 0;
  398. strncat(str, entry.data, (strlen(entry.data) ));
  399. strcat(str, ";");
  400. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  401. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  402. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  403. strcat(str, buf);
  404. strcat(str, "\",");
  405. }
  406. }
  407. void History_GetPage_tabs(char *str, uint32_t page)
  408. {
  409. TM_RTC_t rtc_data;
  410. log_entry_t entry;
  411. char buf[50];
  412. uint8_t i;
  413. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  414. memset(buf, 0, 50);
  415. for(i=0; i < 10; i++){
  416. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  417. if(fs2.cursor_position < 0)
  418. break;
  419. else{
  420. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  421. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  422. }
  423. entry.type = LOG_LOGIN;
  424. log_fetch(&entry, portMAX_DELAY);
  425. //sprintf(buf, "%-50s", logsStrShortRu[entry.type]);
  426. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  427. for(uint8_t j = 0; j < (25 - strlen(logsStrShortRu[entry.type])/2); j ++)
  428. strcat(str, " ");
  429. strcat(str, "\t");
  430. memset(buf, 0, 50);
  431. entry.data[49] = 0;
  432. strncat(str, entry.data, (strlen(entry.data) ));
  433. for(uint8_t j = 0; j < (25 - strlen(entry.data)/2); j ++)
  434. strcat(str, " ");
  435. strcat(str, "\t");
  436. memset(buf, 0, 50);
  437. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  438. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  439. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  440. strcat(str, buf);
  441. strcat(str, "\r\n");
  442. }
  443. }
  444. uint32_t History_GetData(int ptr, char *str, uint32_t size, bool start)
  445. {
  446. TM_RTC_t rtc_data;
  447. log_entry_t entry;
  448. char buf[20];
  449. char temp_str[FILE_BUF_MAX_LEN];
  450. uint8_t i;
  451. uint16_t len;
  452. entry.type = LOG_LOGIN;
  453. if(start)
  454. log_rewind(&entry, portMAX_DELAY);
  455. fs2.cursor_position = ptr/STRING_SIZE_HISTORY;
  456. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs2.slots_per_sector)%fs2.flash->sector_count;
  457. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  458. for(i = 0; i < size/STRING_SIZE_HISTORY; i++)
  459. {
  460. memset(temp_str, 0, 100);
  461. log_fetch(&entry, portMAX_DELAY);
  462. strncat(temp_str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type])));
  463. strcat(temp_str, ";");
  464. entry.data[49] = 0;
  465. strncat(temp_str, entry.data, (strlen(entry.data)));
  466. strcat(temp_str, ";");
  467. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  468. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  469. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  470. strcat(temp_str, buf);
  471. len = strlen(temp_str);
  472. if (len <= STRING_SIZE_HISTORY - 1)
  473. {
  474. memset(&temp_str[len], ' ', STRING_SIZE_HISTORY - len - 1);
  475. strcat(temp_str, "\n");
  476. }
  477. else
  478. {
  479. temp_str[STRING_SIZE - 1] = 0xa;
  480. }
  481. strncat(str, temp_str, STRING_SIZE_HISTORY);
  482. }
  483. return strlen(str);
  484. }