log.c 18 KB

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