74 lines
2.3 KiB
C
74 lines
2.3 KiB
C
#ifndef TELEMETRY_LOGGER_H
|
|
#define TELEMETRY_LOGGER_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#define SLOG_MAX_CHANNELS 40U
|
|
|
|
typedef struct {
|
|
uint64_t time_us;
|
|
uint64_t sequence;
|
|
uint64_t valid;
|
|
uint16_t event;
|
|
int32_t values[SLOG_MAX_CHANNELS];
|
|
} slog_sample_t;
|
|
|
|
typedef struct {
|
|
slog_sample_t *rows;
|
|
size_t oldest;
|
|
size_t count;
|
|
} slog_bank_t;
|
|
|
|
typedef struct {
|
|
slog_bank_t active;
|
|
slog_bank_t archive;
|
|
size_t capacity;
|
|
size_t post_rows;
|
|
size_t remaining;
|
|
size_t trigger_index;
|
|
uint64_t last_us;
|
|
uint64_t next_sequence;
|
|
uint32_t generation;
|
|
uint32_t missed_triggers;
|
|
uint16_t channels;
|
|
bool has_sample;
|
|
} slog_t;
|
|
|
|
/* Banks are caller-owned, disjoint arrays of capacity rows each. A single bank
|
|
* (archive == NULL) supports rolling history. The caller serializes all calls. */
|
|
bool slog_init(slog_t *log, uint16_t channels, slog_sample_t *active,
|
|
slog_sample_t *archive, size_t capacity, size_t post_rows);
|
|
|
|
/* Values has log->channels elements. An accepted trigger keeps this row and
|
|
* post_rows following rows before swapping banks. Timestamp is monotonic. */
|
|
bool slog_push(slog_t *log, uint64_t time_us, uint64_t valid,
|
|
uint16_t event, const int32_t *values, bool trigger);
|
|
|
|
/* Returns the requested sequence if retained, or the oldest available sample
|
|
* after it. Initialize next_sequence to 0. A successful read advances cursor;
|
|
* subtract its previous value from row.sequence to count missed samples. */
|
|
bool slog_read(slog_t const *log, uint64_t *next_sequence, slog_sample_t *row);
|
|
|
|
/* Capture the rolling bank immediately. The archive uses count as its trigger
|
|
* index when the capture was requested manually. */
|
|
bool slog_snapshot(slog_t *log);
|
|
bool slog_latest(const slog_t *log, slog_sample_t *row);
|
|
|
|
/* A completed archive is immutable until released with its generation. */
|
|
bool slog_archive_info(slog_t const *log, uint32_t *generation, size_t *count,
|
|
size_t *trigger_index);
|
|
bool slog_archive_read(slog_t const *log, uint32_t generation, size_t index,
|
|
slog_sample_t *row);
|
|
bool slog_archive_release(slog_t *log, uint32_t generation);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#endif
|