log_api.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, 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, portMAX_DELAY);
  61. #if 0
  62. uint64_t timestamp;
  63. uint8_t code_type; // код типа события
  64. uint8_t code_state; // код состояния
  65. uint8_t channel_number; // номер канала
  66. float value; // значение
  67. uint8_t crc;
  68. #endif
  69. #if 1
  70. /*
  71. printf("[log entry] timestamp = % " PRId64 ", code_type = %u, code_state = %u, \
  72. channel_number = %u, value = %f, crc = %u\r\n", ent->timestamp, \
  73. ent->code_type, ent->code_state, ent->channel_number, ent->value, ent->crc);
  74. */
  75. printf("[log entry] timestamp = % " PRId64 ", code_type = %u",
  76. ent->timestamp, ent->code_type);
  77. printf(" code_state = %u, channel_number = %u", ent->code_state, ent->channel_number);
  78. printf(" value = %f, crc = %u\r\n", ent->value, ent->crc);
  79. #endif
  80. return 0;
  81. }
  82. //
  83. int mb_archive_get_entry(uint8_t *buf, uint16_t entry_index)
  84. {
  85. uint8_t pack_len = 3;
  86. buf[2] = MB_ARCHIVE_ENTRY; // Reference type
  87. log_get_archive_entry(entry_index, &fs_archive, &archive_entry);
  88. #if 0
  89. printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
  90. archive_entry.timestamp, archive_entry.input_value, archive_entry.crc);
  91. #endif
  92. memcpy(&buf[3], &archive_entry, sizeof(archive_entry_t));
  93. pack_len += sizeof(archive_entry_t);
  94. buf[0] = pack_len; // Resp. data length, 1 byte
  95. buf[1] = pack_len; // File Resp. length, 1 byte
  96. return pack_len;
  97. }
  98. //
  99. int mb_log_get_entry(uint8_t *buf, uint16_t entry_index)
  100. {
  101. uint8_t pack_len = 3;
  102. buf[2] = MB_LOG_ENTRY; // Reference type
  103. log_get_log_entry(entry_index, &fs_log, &log_entry);
  104. memcpy(&buf[3], &log_entry, sizeof(log_entry_t));
  105. pack_len += sizeof(log_entry_t);
  106. buf[0] = pack_len; // Resp. data length, 1 byte
  107. buf[1] = pack_len; // File Resp. length, 1 byte
  108. return pack_len;
  109. }