123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include "log_api.h"
- #include "log.h"
- #include "rtc.h"
- #include "ringfs.h"
- #include "spi_flash.h"
- #include "FreeRTOS.h"
- #include "task.h"
- #include "semphr.h"
- #include "rtc.h"
- #include <string.h>
- #include <stdio.h>
- #include <inttypes.h>
- #undef DBG
- #define DBG if(1)
- extern struct ringfs fs_log;
- extern struct ringfs fs_archive;
- extern SemaphoreHandle_t log_mutex;
- static archive_entry_t archive_entry;
- static log_entry_t log_entry;
- //
- void log_get_entry_count(void)
- {
- int count_flash = 0;
-
- count_flash = ringfs_count_exact(&fs_archive);
- printf("Count archive entry: %u\r\n", count_flash);
- }
- //
- int log_get_archive_entry(uint32_t index, struct ringfs *fs, void *entry)
- {
- archive_entry_t *ent = entry;
-
- int start = ringfs_count_estimate(fs);
-
- fs->cursor_position = start - (index - 1) - 1;
-
- if (fs->cursor_position < 0)
- return -1;
- else
- {
- fs->cursor.sector = (fs->read.sector + fs->cursor_position/fs->slots_per_sector)%fs->flash->sector_count;
- fs->cursor.slot = fs->cursor_position%fs->slots_per_sector;
- }
-
- log_fetch(entry, ARCHIVE_ENTRY, portMAX_DELAY);
- #if 1
- printf("[archive entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
- ent->timestamp, ent->input_value, ent->crc);
- #endif
- return 0;
- }
- //
- int log_get_log_entry(uint32_t index, struct ringfs *fs, void *entry)
- {
- log_entry_t *ent = entry;
-
- int start = ringfs_count_estimate(fs);
-
- fs->cursor_position = start - (index - 1) - 1;
-
- if (fs->cursor_position < 0)
- return -1;
- else
- {
- fs->cursor.sector = (fs->read.sector + fs->cursor_position/fs->slots_per_sector)%fs->flash->sector_count;
- fs->cursor.slot = fs->cursor_position%fs->slots_per_sector;
- }
-
- log_fetch(entry, LOG_ENTRY, portMAX_DELAY);
- #if 0
- uint64_t timestamp;
- uint8_t code_type; // код типа события
- uint8_t code_state; // код состояния
- uint8_t channel_number; // номер канала
- float value; // значение
- uint8_t crc;
- #endif
- #if 1
- /*
- printf("[log entry] timestamp = % " PRId64 ", code_type = %u, code_state = %u, \
- channel_number = %u, value = %f, crc = %u\r\n", ent->timestamp, \
- ent->code_type, ent->code_state, ent->channel_number, ent->value, ent->crc);
- */
- printf("[log entry] timestamp = % " PRId64 ", code_type = %u",
- ent->timestamp, ent->code_type);
-
- printf(" code_state = %u, channel_number = %u", ent->code_state, ent->channel_number);
-
- printf(" value = %f, crc = %u\r\n", ent->value, ent->crc);
-
- #endif
- return 0;
- }
- //
- int mb_archive_get_entry(uint8_t *buf, uint16_t entry_index)
- {
- uint8_t pack_len = 3;
- buf[2] = MB_ARCHIVE_ENTRY; // Reference type
-
- log_get_archive_entry(entry_index, &fs_archive, &archive_entry);
- #if 0
- printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n",
- archive_entry.timestamp, archive_entry.input_value, archive_entry.crc);
- #endif
- memcpy(&buf[3], &archive_entry, sizeof(archive_entry_t));
- pack_len += sizeof(archive_entry_t);
-
- buf[0] = pack_len; // Resp. data length, 1 byte
- buf[1] = pack_len; // File Resp. length, 1 byte
-
- return pack_len;
- }
- //
- int mb_log_get_entry(uint8_t *buf, uint16_t entry_index)
- {
- uint8_t pack_len = 3;
- buf[2] = MB_LOG_ENTRY; // Reference type
-
- log_get_log_entry(entry_index, &fs_log, &log_entry);
- memcpy(&buf[3], &log_entry, sizeof(log_entry_t));
- pack_len += sizeof(log_entry_t);
-
- buf[0] = pack_len; // Resp. data length, 1 byte
- buf[1] = pack_len; // File Resp. length, 1 byte
-
- return pack_len;
- }
|