log_api.c 3.2 KB

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