log.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 "rtc.h"
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <inttypes.h>
  12. #undef DBG
  13. #define DBG if(1)
  14. static struct ringfs fs_log;
  15. static struct ringfs fs_archive;
  16. static SemaphoreHandle_t log_mutex;
  17. //
  18. static int op_sector_erase(struct ringfs_flash_partition *flash, int address) {
  19. (void)flash;
  20. int ret;
  21. ret = spi_flash_erase_sector(address, 0);
  22. return ret;
  23. }
  24. //
  25. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size) {
  26. (void)flash;
  27. int ret;
  28. ret = spi_flash_write(address, data, size, 0);
  29. return ret;
  30. }
  31. //
  32. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size) {
  33. (void)flash;
  34. int ret;
  35. ret = spi_flash_read(address, data, size, 0);
  36. return ret;
  37. }
  38. //
  39. static struct ringfs_flash_partition ringfs_flash_log =
  40. {
  41. .sector_offset = LOG_FLASH_SECTOR_OFFSET,
  42. .sector_erase = op_sector_erase,
  43. .program = op_program,
  44. .read = op_read,
  45. };
  46. //
  47. static struct ringfs_flash_partition ringfs_flash_archive =
  48. {
  49. .sector_offset = ARCHIVE_FLASH_SECTOR_OFFSET,
  50. .sector_erase = op_sector_erase,
  51. .program = op_program,
  52. .read = op_read,
  53. };
  54. //
  55. void log_init(bool format)
  56. {
  57. DBG printf("[LOG] Init...\r\n");
  58. if (!spi_flash_desc.present)
  59. return;
  60. // ---------------------------------------------------------------------- //
  61. // Журнал
  62. ringfs_flash_log.sector_size = spi_flash_desc.sector_size;
  63. ringfs_flash_log.sector_count = LOG_FLASH_SECTOR_COUNT;
  64. ringfs_init(&fs_log, &ringfs_flash_log, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  65. if (format || ringfs_scan(&fs_log) != 0) {
  66. DBG printf("FAT1 false\r\n");
  67. ringfs_format(&fs_log);
  68. }
  69. DBG printf("FAT1 true\r\n");
  70. // ---------------------------------------------------------------------- //
  71. // Архив
  72. ringfs_flash_archive.sector_size = spi_flash_desc.sector_size;
  73. ringfs_flash_archive.sector_count = 2; //ARCHIVE_FLASH_SECTOR_COUNT;
  74. ringfs_init(&fs_archive, &ringfs_flash_archive, ARCHIV_ENTRY_VERSION, sizeof(archive_entry_t));
  75. if (format || ringfs_scan(&fs_archive) != 0) {
  76. DBG printf("FAT2 false\r\n");
  77. ringfs_format(&fs_archive);
  78. }
  79. DBG printf("FAT2 true\r\n");
  80. // ---------------------------------------------------------------------- //
  81. //fLogInit = true;
  82. log_mutex = xSemaphoreCreateMutex();
  83. //xTaskCreate(log_task, ( char * ) "log_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  84. }
  85. //
  86. int log_fetch(void *entry, entry_type_t entry_type, uint32_t timeout)
  87. {
  88. int ret;
  89. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  90. if (ret == pdFALSE)
  91. return ret;
  92. if (entry_type == LOG_ENTRY)
  93. ret = ringfs_fetch(&fs_log, entry);
  94. else if (entry_type == ARCHIVE_ENTRY)
  95. ret = ringfs_fetch(&fs_archive, entry);
  96. else ret = -1;
  97. xSemaphoreGive(log_mutex);
  98. return ret;
  99. }
  100. //
  101. int log_discard(void *entry, entry_type_t entry_type, uint32_t timeout)
  102. {
  103. int ret;
  104. ret = xSemaphoreTake(log_mutex, (TickType_t)timeout);
  105. if (ret == pdFALSE)
  106. return ret;
  107. if (entry_type == LOG_ENTRY)
  108. ret = ringfs_discard(&fs_log);
  109. else if (entry_type == ARCHIVE_ENTRY)
  110. ret = ringfs_discard(&fs_archive);
  111. else ret = -1;
  112. xSemaphoreGive(log_mutex);
  113. return ret;
  114. }
  115. //
  116. int log_append(void *entry, entry_type_t entry_type)
  117. {
  118. int ret;
  119. TM_RTC_t time;
  120. common_entry_t *entry_ptr = entry;
  121. log_entry_t *log_etnry_ptr;
  122. archive_entry_t *archive_etnry_ptr;
  123. ret = xSemaphoreTake(log_mutex, portMAX_DELAY);
  124. if (ret == pdFALSE)
  125. return ret;
  126. entry_ptr->timestamp = rtc_get_ms();
  127. if (entry_type == LOG_ENTRY)
  128. {
  129. log_etnry_ptr = entry;
  130. log_etnry_ptr->crc = crc_8(entry, sizeof(log_entry_t) - 1);
  131. ret = ringfs_append(&fs_log, entry);
  132. }
  133. else if (entry_type == ARCHIVE_ENTRY)
  134. {
  135. archive_etnry_ptr = entry;
  136. archive_etnry_ptr->crc = crc_8(entry, sizeof(archive_entry_t) - 1);
  137. ret = ringfs_append(&fs_archive, entry);
  138. }
  139. else ret = -1;
  140. xSemaphoreGive(log_mutex);
  141. return ret;
  142. }
  143. // -------------------------------------------------------------------------- //
  144. // misc
  145. uint8_t crc_8(uint8_t *data, int length)
  146. {
  147. uint8_t crc = 0x00;
  148. uint8_t extract;
  149. uint8_t sum;
  150. for (int i = 0; i < length; i++) {
  151. extract = *data;
  152. for (uint8_t tmp = 8; tmp; tmp--) {
  153. sum = (crc ^ extract) & 0x01;
  154. crc >>= 1;
  155. if (sum)
  156. crc ^= 0x8C;
  157. extract >>= 1;
  158. }
  159. data++;
  160. }
  161. return crc;
  162. }
  163. // -------------------------------------------------------------------------- //
  164. // Tests
  165. #if 0
  166. // fs_log
  167. int log_test(void)
  168. {
  169. int ret;
  170. }
  171. #endif
  172. // fs_archive
  173. int test_archive(void)
  174. {
  175. int ret;
  176. archive_entry_t entry;
  177. int capacity_flash = 0;
  178. int count_flash = 0;
  179. capacity_flash = ringfs_capacity(&fs_archive);
  180. count_flash = ringfs_count_exact(&fs_archive);
  181. DBG printf("Fetching...\r\n");
  182. if (log_fetch(&entry, ARCHIVE_ENTRY, portMAX_DELAY))
  183. {
  184. printf("%" PRId64 "\n", entry.timestamp);
  185. }
  186. else
  187. {
  188. DBG printf("fail\r\n");
  189. }
  190. #if 0
  191. DBG printf("Discarding\r\n");
  192. if (log_discard)
  193. #endif
  194. return 0;
  195. }
  196. // Добавить n архивных записей
  197. int test_add_random_archive_entry(uint32_t cnt_entry)
  198. {
  199. int ret;
  200. archive_entry_t entry= {0};
  201. DBG printf("Try append %u archive entry\r\n", cnt_entry);
  202. for (uint32_t i = 0; i < cnt_entry; i++)
  203. {
  204. entry.input_value = ringfs_count_exact(&fs_archive);
  205. ret = log_append(&entry, ARCHIVE_ENTRY);
  206. }
  207. return ret;
  208. }
  209. //
  210. void test_fetch(void)
  211. {
  212. archive_entry_t entry = {0};
  213. log_fetch(&entry, ARCHIVE_ENTRY, portMAX_DELAY);
  214. //printf("\r\n%" PRId64 " [ms]\r\n", rtc_get_ms());
  215. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n", entry.timestamp, entry.input_value, entry.crc);
  216. }
  217. //
  218. void test_archive_info(void)
  219. {
  220. int capacity_flash = 0;
  221. int count_flash = 0;
  222. int count_estimate = 0;
  223. capacity_flash = ringfs_capacity(&fs_archive);
  224. count_flash = ringfs_count_exact(&fs_archive);
  225. count_estimate = ringfs_count_estimate(&fs_archive);
  226. printf("Archive partition capasity: %u\r\n", capacity_flash);
  227. printf("Count archive entry: %u\r\n", count_flash);
  228. printf("Estimate count: %u\r\n", count_estimate);
  229. }
  230. //
  231. void test_archive_format(void)
  232. {
  233. ringfs_format(&fs_archive);
  234. }
  235. //
  236. void test_print_all_archive(void)
  237. {
  238. }
  239. #if 0
  240. char logFileBuf[FILE_BUF_MAX_LEN];
  241. char name_login[50];
  242. extern const char* logsStrShortRu[];
  243. bool flUpdateLog = false;
  244. static bool fLogInit = false; // Флаг инициализации журнала
  245. #define LOG_TIME 1000*60*10
  246. static int op_sector_erase(struct ringfs_flash_partition *flash, int address) {
  247. (void)flash;
  248. int ret;
  249. ret = spi_flash_erase_sector(address, 0);
  250. return ret;
  251. }
  252. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size) {
  253. (void)flash;
  254. int ret;
  255. ret = spi_flash_write(address, data, size, 0);
  256. return ret;
  257. }
  258. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size) {
  259. (void)flash;
  260. int ret;
  261. ret = spi_flash_read(address, data, size, 0);
  262. return ret;
  263. }
  264. static struct ringfs_flash_partition ringfs_flash = {
  265. .sector_offset = LOG_FLASH_SECTOR_OFFSET,
  266. .sector_erase = op_sector_erase,
  267. .program = op_program,
  268. .read = op_read,
  269. };
  270. static struct ringfs fs;
  271. static struct ringfs_flash_partition ringfs_flash2 = {
  272. .sector_offset = ALARM_LOG_FLASH_SECTOR_OFFSET,
  273. .sector_erase = op_sector_erase,
  274. .program = op_program,
  275. .read = op_read,
  276. };
  277. static struct ringfs fs2;
  278. static SemaphoreHandle_t log_mutex;
  279. /**
  280. * @brief Отключает журнал для безопасной перезагрузки
  281. */
  282. bool LOG_Disable(void)
  283. {
  284. if (fLogInit) {
  285. /* Ожидаем завершения работы с журнал */
  286. if ( xSemaphoreTake(log_mutex, 10000) == pdTRUE ) {
  287. //fLogInit = false;
  288. //xSemaphoreGive(logMutex);
  289. return true;
  290. }
  291. else {
  292. return false;
  293. }
  294. }
  295. else {
  296. return true;
  297. }
  298. }
  299. void log_task(void* params)
  300. {
  301. for(;;){
  302. flUpdateLog = true;
  303. vTaskDelay(LOG_TIME);
  304. /*vTaskDelay(50);
  305. log_event_data(LOG_SYSTEM_BOOT, "Администратор");
  306. log_add(")215.7;215.7;220.5;000;50.1;2.30;25.0;00000001;");*/
  307. }
  308. }
  309. void log_init(bool format) {
  310. DBG printf(">>> Event log\n");
  311. if (!spi_flash_desc.present)
  312. return;
  313. ringfs_flash.sector_size = spi_flash_desc.sector_size;
  314. ringfs_flash.sector_count = SECTOR_COUNT;
  315. ringfs_init(&fs, &ringfs_flash, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  316. if (format || ringfs_scan(&fs) != 0){
  317. DBG printf("FAT1 false\r\n");
  318. ringfs_format(&fs);
  319. }
  320. DBG printf("FAT1 true\r\n");
  321. ringfs_flash2.sector_size = spi_flash_desc.sector_size;
  322. ringfs_flash2.sector_count = SECTOR_COUNT;
  323. ringfs_init(&fs2, &ringfs_flash2, LOG_ENTRY_VERSION, sizeof(log_entry_t));
  324. if (format || ringfs_scan(&fs2) != 0){
  325. DBG printf("FAT2 false\r\n");
  326. ringfs_format(&fs2);
  327. }
  328. DBG printf("FAT2 true\r\n");
  329. fLogInit = true;
  330. log_mutex = xSemaphoreCreateMutex();
  331. xTaskCreate(log_task, ( char * ) "log_task", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
  332. }
  333. int capacity_flash = 0;
  334. int count_flash = 0;
  335. int log_test(void) {
  336. int ret;
  337. log_entry_t entry;
  338. log_init(false);
  339. capacity_flash = ringfs_capacity(&fs);
  340. count_flash = ringfs_count_exact(&fs);
  341. DBG printf("\tCapacity: %d\n", capacity_flash);
  342. DBG printf("\tCount: %d\n", count_flash);
  343. DBG printf("\tAppending ");
  344. // ret = log_event(LOG_SYSTEM_DEFCONFIG, 0, 0);
  345. DBG printf("%s\n", ret == 0 ? "ok" : "error");
  346. if (ret == 0)
  347. return -1;
  348. // ret = log_event(LOG_SYSTEM_DEFCONFIG, 0, 512);
  349. entry.timestamp = 0;
  350. entry.type = 0;
  351. DBG printf("\tFetching ");
  352. if (log_fetch(&entry, portMAX_DELAY) == 0){
  353. DBG printf("ok, time=%d, type=%d\n", entry.timestamp, entry.type);
  354. log_fetch(&entry, portMAX_DELAY);
  355. entry.timestamp = 0;
  356. entry.type = 0;
  357. log_fetch(&entry, portMAX_DELAY);
  358. entry.timestamp = 0;
  359. entry.type = 0;
  360. log_fetch(&entry, portMAX_DELAY);
  361. entry.timestamp = 0;
  362. entry.type = 0;
  363. log_fetch(&entry, portMAX_DELAY);
  364. return 0;
  365. }
  366. else {
  367. DBG printf("fail\n");
  368. return -1;
  369. }
  370. DBG printf("\tDiscarding ");
  371. if (log_discard(&entry,portMAX_DELAY) == 0)
  372. DBG printf("ok\n");
  373. else {
  374. DBG printf("fail\n");
  375. return -1;
  376. }
  377. return 0;
  378. }
  379. int log_append(log_entry_t *entry) {
  380. int ret;
  381. TM_RTC_t data;
  382. ret = xSemaphoreTake( log_mutex, portMAX_DELAY );
  383. if (ret == pdFALSE)
  384. return ret;
  385. if (!entry->timestamp){
  386. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  387. entry->timestamp = data.unix;
  388. }
  389. if(entry->type == LOG_VALUE)
  390. ringfs_append(&fs, entry);
  391. else
  392. ringfs_append(&fs2, entry);
  393. xSemaphoreGive(log_mutex);
  394. return ret;
  395. }
  396. int log_fetch(log_entry_t *entry, uint32_t timeout) {
  397. int ret;
  398. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  399. if (ret == pdFALSE)
  400. return ret;
  401. if(entry->type == LOG_VALUE)
  402. ret = ringfs_fetch(&fs, entry);
  403. else
  404. ret = ringfs_fetch(&fs2, entry);
  405. xSemaphoreGive(log_mutex);
  406. return ret;
  407. }
  408. int log_rewind(log_entry_t *entry, uint32_t timeout) {
  409. int ret;
  410. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  411. if (ret == pdFALSE)
  412. return ret;
  413. if(entry->type == LOG_VALUE)
  414. ret = ringfs_rewind(&fs);
  415. else
  416. ret = ringfs_rewind(&fs2);
  417. xSemaphoreGive(log_mutex);
  418. return ret;
  419. }
  420. int log_discard(log_entry_t *entry, uint32_t timeout) {
  421. int ret;
  422. ret = xSemaphoreTake( log_mutex, (TickType_t)timeout );
  423. if (ret == pdFALSE)
  424. return ret;
  425. if(entry->type == LOG_VALUE)
  426. ret = ringfs_discard(&fs);
  427. else
  428. ret = ringfs_discard(&fs2);
  429. xSemaphoreGive(log_mutex);
  430. return ret;
  431. }
  432. void log_event_data(log_type_t type, char *data)
  433. {
  434. log_entry_t entry_data;
  435. entry_data.timestamp = 0;
  436. entry_data.type = type;
  437. strncpy(entry_data.data, data, 49);
  438. if (fLogInit)
  439. log_append(&entry_data);
  440. }
  441. void log_add(char *log_data)
  442. {
  443. char buf_value[50];
  444. uint8_t i, len;
  445. memset(buf_value, 0, 50);
  446. len = strlen(log_data);
  447. if (len != UPS_DATA_STRING_SIZE) {
  448. //len = UPS_DATA_STRING_SIZE;
  449. return;
  450. }
  451. strncpy(buf_value, log_data, len);
  452. DBG printf("UPS log data: %s\r\n", log_data);
  453. buf_value[0] = '\"';
  454. for(i = 0; i < len; i++)
  455. {
  456. if(buf_value[i] == ' ')
  457. buf_value[i] = ';';
  458. }
  459. buf_value[len - 1] = ';';
  460. if(fLogInit){
  461. if(fs.write.slot>67)
  462. {
  463. log_entry_t entry_data;
  464. entry_data.timestamp = 0;
  465. log_event_data(LOG_VALUE, buf_value);
  466. }
  467. else
  468. log_event_data(LOG_VALUE, buf_value);
  469. }
  470. }
  471. /**
  472. * @brief Возвращает true если журнал проинициализирован
  473. */
  474. bool LOG_IsInit()
  475. {
  476. return fLogInit;
  477. }
  478. /**
  479. * @brief Возвращает общее количество страниц
  480. */
  481. uint32_t LOG_GetPageCount(void)
  482. {
  483. return (((ringfs_count_estimate(&fs)) / 10) + 1);
  484. }
  485. uint32_t LOG_GetTotalSTRCount(void)
  486. {
  487. return ringfs_count_estimate(&fs);
  488. }
  489. void LOG_GetPage_tabs(char *str, uint32_t page)
  490. {
  491. TM_RTC_t rtc_data;
  492. log_entry_t entry;
  493. char buf[20];
  494. uint8_t i;
  495. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  496. memset(buf, 0, 20);
  497. for(i=0; i < 10; i++){
  498. fs.cursor_position = start - 10*(page-1) - 1 - i;
  499. if(fs.cursor_position < 0)
  500. break;
  501. else{
  502. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  503. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  504. }
  505. entry.type = LOG_VALUE;
  506. log_fetch(&entry, portMAX_DELAY);
  507. entry.data[49] = 0;
  508. strncat(str, entry.data, strlen(entry.data));
  509. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  510. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  511. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  512. strcat(str, buf);
  513. strcat(str, "\",");
  514. strcat(str, "\r\n");
  515. }
  516. }
  517. void LOG_GetPage(char *str, uint32_t page)
  518. {
  519. TM_RTC_t rtc_data;
  520. log_entry_t entry;
  521. char buf[20];
  522. uint8_t i;
  523. int start =LOG_GetTotalSTRCount();//(fs.write.sector*fs.slots_per_sector + fs.write.slot);
  524. memset(buf, 0, 20);
  525. for(i=0; i < 10; i++){
  526. fs.cursor_position = start - 10*(page-1) - 1 - i;
  527. if(fs.cursor_position < 0)
  528. break;
  529. else{
  530. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  531. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  532. }
  533. entry.type = LOG_VALUE;
  534. log_fetch(&entry, portMAX_DELAY);
  535. entry.data[49] = 0;
  536. strncat(str, entry.data, strlen(entry.data));
  537. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  538. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  539. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  540. strcat(str, buf);
  541. strcat(str, "\",");
  542. }
  543. }
  544. uint32_t LOG_GetData(int ptr, char *str, uint32_t size, bool start)
  545. {
  546. TM_RTC_t rtc_data;
  547. log_entry_t entry;
  548. char buf[20];
  549. uint8_t i;
  550. entry.type = LOG_VALUE;
  551. if(start)
  552. log_rewind(&entry, portMAX_DELAY);
  553. fs.cursor_position = ptr/STRING_SIZE;
  554. fs.cursor.sector = (fs.read.sector + fs.cursor_position/fs.slots_per_sector)%fs.flash->sector_count;
  555. fs.cursor.slot = fs.cursor_position%fs.slots_per_sector;
  556. for(i = 0; i < size/STRING_SIZE; i++)
  557. {
  558. entry.type = LOG_VALUE;
  559. log_fetch(&entry, portMAX_DELAY);
  560. entry.data[49] = 0;
  561. strncat(str, &entry.data[1], (strlen(entry.data) - 1));
  562. TM_RTC_GetDateTimeFromUnix(&rtc_data, entry.timestamp);
  563. sprintf(buf, "%02i.%02i.%02i %02i:%02i:%02i", rtc_data.date, rtc_data.month,
  564. rtc_data.year, rtc_data.hours, rtc_data.minutes, rtc_data.seconds);
  565. strcat(str, buf);
  566. strcat(str, "\n");
  567. }
  568. return strlen(str);
  569. }
  570. #endif