log.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. strncpy(buf_value, log_data, len);
  219. DBG printf("UPS log data: %s\r\n", log_data);
  220. buf_value[0] = '\"';
  221. for(i = 0; i < len; i++)
  222. {
  223. if(buf_value[i] == ' ')
  224. buf_value[i] = ';';
  225. }
  226. buf_value[len - 1] = ';';
  227. if(fLogInit){
  228. if(fs.write.slot>67)
  229. {
  230. log_entry_t entry_data;
  231. entry_data.timestamp = 0;
  232. log_event_data(LOG_VALUE, buf_value);
  233. }
  234. else
  235. log_event_data(LOG_VALUE, buf_value);
  236. }
  237. }
  238. /**
  239. * @brief Возвращает true если журнал проинициализирован
  240. */
  241. bool LOG_IsInit()
  242. {
  243. return fLogInit;
  244. }
  245. /**
  246. * @brief Возвращает общее количество страниц
  247. */
  248. uint32_t LOG_GetPageCount(void)
  249. {
  250. return (((ringfs_count_estimate(&fs)) / 10) + 1);
  251. }
  252. uint32_t LOG_GetTotalSTRCount(void)
  253. {
  254. return ringfs_count_estimate(&fs);
  255. }
  256. void LOG_GetPage_tabs(char *str, uint32_t page)
  257. {
  258. TM_RTC_t rtc_data;
  259. log_entry_t entry;
  260. char buf[20];
  261. uint8_t i;
  262. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  263. memset(buf, 0, 20);
  264. for(i=0; i < 10; i++){
  265. fs.cursor_position = start - 10*(page-1) - 1 - i;
  266. if(fs.cursor_position < 0)
  267. break;
  268. else{
  269. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  270. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  271. }
  272. entry.type = LOG_VALUE;
  273. log_fetch(&entry, portMAX_DELAY);
  274. entry.data[49] = 0;
  275. strncat(str, entry.data, strlen(entry.data));
  276. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  277. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  278. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  279. strcat(str, buf);
  280. strcat(str, "\",");
  281. strcat(str, "\r\n");
  282. }
  283. }
  284. void LOG_GetPage(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. }
  310. }
  311. uint32_t LOG_GetData(int ptr, char *str, uint32_t size, bool start)
  312. {
  313. TM_RTC_t rtc_data;
  314. log_entry_t entry;
  315. char buf[20];
  316. uint8_t i;
  317. entry.type = LOG_VALUE;
  318. if(start)
  319. log_rewind(&entry, portMAX_DELAY);
  320. fs.cursor_position = ptr/STRING_SIZE;
  321. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  322. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  323. for(i = 0; i < size/STRING_SIZE; i++)
  324. {
  325. entry.type = LOG_VALUE;
  326. log_fetch(&entry, portMAX_DELAY);
  327. entry.data[49] = 0;
  328. strncat(str, &entry.data[1], (strlen(entry.data) - 1));
  329. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  330. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  331. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  332. strcat(str, buf);
  333. strcat(str, "\n");
  334. }
  335. return strlen(str);
  336. }
  337. /**
  338. * @brief Возвращает общее количество страниц
  339. */
  340. uint32_t History_GetPageCount(void)
  341. {
  342. return (((ringfs_count_estimate(&fs2)) / 10) + 1);
  343. }
  344. uint32_t History_GetTotalSTRCount(void)
  345. {
  346. return ringfs_count_estimate(&fs2);
  347. }
  348. void History_GetPage(char *str, uint32_t page)
  349. {
  350. TM_RTC_t rtc_data;
  351. log_entry_t entry;
  352. char buf[20];
  353. uint8_t i;
  354. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  355. memset(buf, 0, 20);
  356. for(i=0; i < 10; i++){
  357. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  358. if(fs2.cursor_position < 0)
  359. break;
  360. else{
  361. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  362. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  363. }
  364. entry.type = LOG_LOGIN;
  365. log_fetch(&entry, portMAX_DELAY);
  366. strcat(str, "\"");
  367. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  368. strcat(str, ";");
  369. entry.data[49] = 0;
  370. strncat(str, entry.data, (strlen(entry.data) ));
  371. strcat(str, ";");
  372. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  373. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  374. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  375. strcat(str, buf);
  376. strcat(str, "\",");
  377. }
  378. }
  379. void History_GetPage_tabs(char *str, uint32_t page)
  380. {
  381. TM_RTC_t rtc_data;
  382. log_entry_t entry;
  383. char buf[50];
  384. uint8_t i;
  385. int start =History_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  386. memset(buf, 0, 50);
  387. for(i=0; i < 10; i++){
  388. fs2.cursor_position = start - 10*(page-1) - 1 - i;
  389. if(fs2.cursor_position < 0)
  390. break;
  391. else{
  392. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs.slots_per_sector)%fs2.flash->sector_count;
  393. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  394. }
  395. entry.type = LOG_LOGIN;
  396. log_fetch(&entry, portMAX_DELAY);
  397. //sprintf(buf, "%-50s", logsStrShortRu[entry.type]);
  398. strncat(str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type]) ));
  399. for(uint8_t j = 0; j < (25 - strlen(logsStrShortRu[entry.type])/2); j ++)
  400. strcat(str, " ");
  401. strcat(str, "\t");
  402. memset(buf, 0, 50);
  403. entry.data[49] = 0;
  404. strncat(str, entry.data, (strlen(entry.data) ));
  405. for(uint8_t j = 0; j < (25 - strlen(entry.data)/2); j ++)
  406. strcat(str, " ");
  407. strcat(str, "\t");
  408. memset(buf, 0, 50);
  409. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  410. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  411. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  412. strcat(str, buf);
  413. strcat(str, "\r\n");
  414. }
  415. }
  416. uint32_t History_GetData(int ptr, char *str, uint32_t size, bool start)
  417. {
  418. TM_RTC_t rtc_data;
  419. log_entry_t entry;
  420. char buf[20];
  421. char temp_str[FILE_BUF_MAX_LEN];
  422. uint8_t i;
  423. uint16_t len;
  424. entry.type = LOG_LOGIN;
  425. if(start)
  426. log_rewind(&entry, portMAX_DELAY);
  427. fs2.cursor_position = ptr/STRING_SIZE_HISTORY;
  428. fs2.cursor.sector = (fs2.read.sector + fs2.cursor_position/fs2.slots_per_sector)%fs2.flash->sector_count;
  429. fs2.cursor.slot = fs2.cursor_position%fs2.slots_per_sector;
  430. for(i = 0; i < size/STRING_SIZE_HISTORY; i++)
  431. {
  432. memset(temp_str, 0, 100);
  433. log_fetch(&entry, portMAX_DELAY);
  434. strncat(temp_str, logsStrShortRu[entry.type], (strlen(logsStrShortRu[entry.type])));
  435. strcat(temp_str, ";");
  436. entry.data[49] = 0;
  437. strncat(temp_str, entry.data, (strlen(entry.data)));
  438. strcat(temp_str, ";");
  439. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  440. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  441. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  442. strcat(temp_str, buf);
  443. len = strlen(temp_str);
  444. if (len <= STRING_SIZE_HISTORY - 1)
  445. {
  446. memset(&temp_str[len], ' ', STRING_SIZE_HISTORY - len - 1);
  447. strcat(temp_str, "\n");
  448. }
  449. else
  450. {
  451. temp_str[STRING_SIZE - 1] = 0xa;
  452. }
  453. strncat(str, temp_str, STRING_SIZE_HISTORY);
  454. }
  455. return strlen(str);
  456. }