log.c 15 KB

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