Bounded asynchronous CSV recorder for flight-controller pose evidence. More...
#include <errno.h>#include <fcntl.h>#include <limits.h>#include <pthread.h>#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>#include "pose_log.h"#include "usher_protocol.h"#include "boot_id.h"#include "path_utils.h"
Go to the source code of this file.
Data Structures | |
| struct | pose_record |
Macros | |
| #define | POSE_LOG_CAPACITY 256 |
| #define | POSE_LOG_MAX_BYTES (64U * 1024U * 1024U) |
| #define | POSE_LOG_BATCH 32 |
| #define | POSE_LOG_COORDINATE_SCALE 10000000 |
| #define | POSE_LOG_MAX_SAMPLE_WINDOW_US 1000000U |
| #define | POSE_LOG_SCHEMA_VERSION 2 |
Functions | |
| static void | initialize_condition (void) |
| static int | write_record (const struct pose_record *record) |
| static void * | write_queue (void *unused) |
| int | pose_log_start (const char *directory) |
| Start a durable asynchronous CSV session, or disable logging when directory is empty. | |
| int | pose_log_record (const uint8_t *payload, size_t length, uint64_t receive_monotonic_us) |
| Validate and enqueue a raw pose payload without clock mapping evidence. | |
| int | pose_log_record_clocked (const uint8_t *payload, size_t length, uint64_t receive_monotonic_us, const struct pose_clock_evidence *evidence) |
| Validate and enqueue a pose payload with optional mapped timing provenance. | |
| struct pose_log_stats | pose_log_status (void) |
| Return a synchronized snapshot of logging health and loss counters. | |
| int | pose_log_stop (void) |
| Flush queued records, join the writer, and close the CSV file. | |
Variables | |
| static pthread_mutex_t | mutex = PTHREAD_MUTEX_INITIALIZER |
| static pthread_cond_t | available |
| static pthread_once_t | condition_once = PTHREAD_ONCE_INIT |
| static int | condition_error |
| static pthread_t | writer |
| static FILE * | output |
| static struct pose_record | queue [POSE_LOG_CAPACITY] |
| static size_t | head |
| static size_t | count |
| static size_t | bytes_written |
| static bool | active |
| static bool | stopping |
| static uint64_t | accepted |
| static uint64_t | synced |
| static uint64_t | rejected |
| static atomic_uint_fast64_t | dropped |
| static int | log_error |
| static char | boot_id [37] |
| static const char | header [] |
Detailed Description
Bounded asynchronous CSV recorder for flight-controller pose evidence.
The UART path uses trylock and drops rather than blocks when the writer is busy. A dedicated thread batches durable writes, preserving acquisition latency while recording queue loss and clock-alignment provenance explicitly in each row.
Definition in file pose_log.c.
Macro Definition Documentation
◆ POSE_LOG_BATCH
| #define POSE_LOG_BATCH 32 |
Writer batch size amortizes durable sync calls while bounding unsynced records.
Definition at line 54 of file pose_log.c.
◆ POSE_LOG_CAPACITY
| #define POSE_LOG_CAPACITY 256 |
Fixed record slots keep UART enqueueing allocation-free and memory-predictable.
Definition at line 47 of file pose_log.c.
◆ POSE_LOG_COORDINATE_SCALE
| #define POSE_LOG_COORDINATE_SCALE 10000000 |
Pose coordinates on the wire are signed degrees scaled by 10^7.
Definition at line 56 of file pose_log.c.
◆ POSE_LOG_MAX_BYTES
| #define POSE_LOG_MAX_BYTES (64U * 1024U * 1024U) |
Rotate by starting a new session rather than allowing unbounded storage growth.
Definition at line 51 of file pose_log.c.
◆ POSE_LOG_MAX_SAMPLE_WINDOW_US
| #define POSE_LOG_MAX_SAMPLE_WINDOW_US 1000000U |
Reject sample intervals longer than one second as malformed timing evidence.
Definition at line 58 of file pose_log.c.
◆ POSE_LOG_SCHEMA_VERSION
| #define POSE_LOG_SCHEMA_VERSION 2 |
CSV schema revision stored in the first column.
Definition at line 60 of file pose_log.c.
Function Documentation
◆ initialize_condition()
|
static |
Configure the writer wake condition against the same monotonic clock as its deadline.
Definition at line 96 of file pose_log.c.
◆ pose_log_record()
| int pose_log_record | ( | const uint8_t * | payload, |
| size_t | length, | ||
| uint64_t | receive_monotonic_us ) |
Validate and enqueue a raw pose payload without clock mapping evidence.
Enqueue a raw pose with no mapped-clock evidence.
- Parameters
-
payload Wire-format USHER_POSE_SAMPLE bytes. length Must equal the packed message size. receive_monotonic_us UART receive timestamp from CLOCK_MONOTONIC.
- Returns
- 1 queued, 0 best-effort drop, or -1 invalid payload.
Definition at line 296 of file pose_log.c.
◆ pose_log_record_clocked()
| int pose_log_record_clocked | ( | const uint8_t * | payload, |
| size_t | length, | ||
| uint64_t | receive_monotonic_us, | ||
| const struct pose_clock_evidence * | evidence ) |
Validate and enqueue a pose payload with optional mapped timing provenance.
Validate and enqueue a pose with optional token-bound FC/MORA clock evidence.
- Parameters
-
payload Wire-format USHER_POSE_SAMPLE bytes. length Must equal the packed message size. receive_monotonic_us UART receive timestamp from CLOCK_MONOTONIC. evidence Optional probe and mapped interval copied into this row.
- Returns
- 1 queued, 0 best-effort drop, or -1 malformed timing/pose data.
Definition at line 307 of file pose_log.c.
◆ pose_log_start()
| int pose_log_start | ( | const char * | directory | ) |
Start a durable asynchronous CSV session, or disable logging when directory is empty.
Start one durable CSV session; an empty path leaves optional logging disabled.
- Returns
- Zero on active/disabled success, otherwise -1 with errno set.
Definition at line 197 of file pose_log.c.
◆ pose_log_status()
| struct pose_log_stats pose_log_status | ( | void | ) |
Return a synchronized snapshot of logging health and loss counters.
Return a mutex-consistent snapshot of accepted, durable, dropped and rejected records.
- Returns
- Statistics from the current logging session.
Definition at line 365 of file pose_log.c.
◆ pose_log_stop()
| int pose_log_stop | ( | void | ) |
Flush queued records, join the writer, and close the CSV file.
Stop accepting records, drain the queue, join the writer, and close the CSV.
- Returns
- Zero after orderly durable shutdown, otherwise -1 with errno set.
Definition at line 375 of file pose_log.c.
◆ write_queue()
|
static |
Drain bounded batches off the UART thread and fdatasync before counting them synced.
- Parameters
-
unused pthread entry-point argument; unused.
Definition at line 148 of file pose_log.c.
◆ write_record()
|
static |
Serialize one validated wire sample and its optional clock mapping as a CSV row.
- Parameters
-
record Queue-owned immutable snapshot.
- Returns
- Zero when written, otherwise errno-style I/O or bounds error.
Definition at line 109 of file pose_log.c.
Variable Documentation
◆ accepted
|
static |
Definition at line 81 of file pose_log.c.
◆ active
|
static |
Definition at line 80 of file pose_log.c.
◆ available
|
static |
Definition at line 73 of file pose_log.c.
◆ boot_id
|
static |
Definition at line 84 of file pose_log.c.
◆ bytes_written
|
static |
Definition at line 79 of file pose_log.c.
◆ condition_error
|
static |
Definition at line 75 of file pose_log.c.
◆ condition_once
|
static |
Definition at line 74 of file pose_log.c.
◆ count
|
static |
Definition at line 79 of file pose_log.c.
◆ dropped
|
static |
Definition at line 82 of file pose_log.c.
◆ head
|
static |
Definition at line 79 of file pose_log.c.
◆ header
|
static |
Definition at line 87 of file pose_log.c.
◆ log_error
|
static |
Definition at line 83 of file pose_log.c.
◆ mutex
|
static |
Definition at line 72 of file pose_log.c.
◆ output
|
static |
Definition at line 77 of file pose_log.c.
◆ queue
|
static |
Definition at line 78 of file pose_log.c.
◆ rejected
|
static |
Definition at line 81 of file pose_log.c.
◆ stopping
|
static |
Definition at line 80 of file pose_log.c.
◆ synced
|
static |
Definition at line 81 of file pose_log.c.
◆ writer
|
static |
Definition at line 76 of file pose_log.c.