log.c 14 KB

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