log_api.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "log_api.h"
  2. #include "log.h"
  3. #include "rtc.h"
  4. #include "ringfs.h"
  5. #include "spi_flash.h"
  6. #include "FreeRTOS.h"
  7. #include "task.h"
  8. #include "semphr.h"
  9. #include "rtc.h"
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <inttypes.h>
  13. #undef DBG
  14. #define DBG if(1)
  15. extern struct ringfs fs_archive;
  16. extern SemaphoreHandle_t log_mutex;
  17. static archive_entry_t archive_entry;
  18. //
  19. void log_get_entry_count(void)
  20. {
  21. int count_flash = 0;
  22. count_flash = ringfs_count_exact(&fs_archive);
  23. printf("Count archive entry: %u\r\n", count_flash);
  24. }
  25. //
  26. int log_get_entry(uint32_t index, struct ringfs *fs, void *entry)
  27. {
  28. archive_entry_t *ent = entry;
  29. int start = ringfs_count_estimate(fs);
  30. fs->cursor_position = start - (index - 1) - 1;
  31. if (fs->cursor_position < 0)
  32. return -1;
  33. else
  34. {
  35. fs->cursor.sector = (fs->read.sector + fs->cursor_position/fs->slots_per_sector)%fs->flash->sector_count;
  36. fs->cursor.slot = fs->cursor_position%fs->slots_per_sector;
  37. }
  38. log_fetch(entry, ARCHIVE_ENTRY, portMAX_DELAY);
  39. #if 1
  40. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
  41. ent->timestamp, ent->input_value, ent->crc);
  42. #endif
  43. return 0;
  44. }
  45. //
  46. int mb_log_get_entry(uint8_t *buf, uint16_t entry_index)
  47. {
  48. uint8_t pack_len = 3;
  49. buf[2] = 0x06; // Reference type
  50. log_get_entry(entry_index, &fs_archive, &archive_entry);
  51. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
  52. archive_entry.timestamp, archive_entry.input_value, archive_entry.crc);
  53. memcpy(&buf[3], &archive_entry, sizeof(archive_entry_t));
  54. pack_len += sizeof(archive_entry_t);
  55. buf[0] = pack_len; // Resp. data length, 1 byte
  56. buf[1] = pack_len; // File Resp. length, 1 byte
  57. return pack_len;
  58. }