log_api.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_log;
  16. extern struct ringfs fs_archive;
  17. extern SemaphoreHandle_t log_mutex;
  18. static archive_entry_t archive_entry;
  19. static log_entry_t log_entry;
  20. //
  21. void log_get_entry_count(void)
  22. {
  23. int count_flash = 0;
  24. count_flash = ringfs_count_exact(&fs_archive);
  25. printf("Count archive entry: %u\r\n", count_flash);
  26. }
  27. //
  28. int log_get_archive_entry(uint32_t index, struct ringfs *fs, void *entry)
  29. {
  30. archive_entry_t *ent = entry;
  31. int start = ringfs_count_estimate(fs);
  32. fs->cursor_position = start - (index - 1) - 1;
  33. if (fs->cursor_position < 0)
  34. return -1;
  35. else
  36. {
  37. fs->cursor.sector = (fs->read.sector + fs->cursor_position/fs->slots_per_sector)%fs->flash->sector_count;
  38. fs->cursor.slot = fs->cursor_position%fs->slots_per_sector;
  39. }
  40. log_fetch(entry, ARCHIVE_ENTRY, 0, portMAX_DELAY);
  41. #if 1
  42. printf("[archive entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
  43. ent->timestamp, ent->input_value, ent->crc);
  44. #endif
  45. return 0;
  46. }
  47. //
  48. int log_get_log_entry(uint32_t index, struct ringfs *fs, void *entry)
  49. {
  50. log_entry_t *ent = entry;
  51. int start = ringfs_count_estimate(fs);
  52. fs->cursor_position = start - (index - 1) - 1;
  53. if (fs->cursor_position < 0)
  54. return -1;
  55. else
  56. {
  57. fs->cursor.sector = (fs->read.sector + fs->cursor_position/fs->slots_per_sector)%fs->flash->sector_count;
  58. fs->cursor.slot = fs->cursor_position%fs->slots_per_sector;
  59. }
  60. log_fetch(entry, LOG_ENTRY, 0, portMAX_DELAY);
  61. #if 0
  62. /*
  63. printf("[log entry] timestamp = % " PRId64 ", code_type = %u, code_state = %u, \
  64. channel_number = %u, value = %f, crc = %u\r\n", ent->timestamp, \
  65. ent->code_type, ent->code_state, ent->channel_number, ent->value, ent->crc);
  66. */
  67. printf("[log entry] timestamp = % " PRId64 ", code_type = %u",
  68. ent->timestamp, ent->code_type);
  69. printf(" code_state = %u, channel_number = %u", ent->code_state, ent->channel_number);
  70. printf(" value = %f, crc = %u\r\n", ent->value, ent->crc);
  71. #endif
  72. return 0;
  73. }
  74. //
  75. int mb_archive_get_entry(uint8_t *buf, uint16_t entry_index)
  76. {
  77. uint8_t pack_len = 3;
  78. buf[2] = MB_ARCHIVE_ENTRY; // Reference type
  79. log_get_archive_entry(entry_index, &fs_archive, &archive_entry);
  80. #if 0
  81. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
  82. archive_entry.timestamp, archive_entry.input_value, archive_entry.crc);
  83. #endif
  84. memcpy(&buf[3], &archive_entry, sizeof(archive_entry_t));
  85. pack_len += sizeof(archive_entry_t);
  86. buf[0] = pack_len; // Resp. data length, 1 byte
  87. buf[1] = pack_len; // File Resp. length, 1 byte
  88. return pack_len;
  89. }
  90. //
  91. int mb_log_get_entry(uint8_t *buf, uint16_t entry_index)
  92. {
  93. uint8_t pack_len = 3;
  94. buf[2] = MB_LOG_ENTRY; // Reference type
  95. log_get_log_entry(entry_index, &fs_log, &log_entry);
  96. memcpy(&buf[3], &log_entry, sizeof(log_entry_t));
  97. pack_len += sizeof(log_entry_t);
  98. buf[0] = pack_len; // Resp. data length, 1 byte
  99. buf[1] = pack_len; // File Resp. length, 1 byte
  100. return pack_len;
  101. }