Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
pose_log.c File Reference

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"
Include dependency graph for pose_log.c:

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()

void initialize_condition ( void )
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
payloadWire-format USHER_POSE_SAMPLE bytes.
lengthMust equal the packed message size.
receive_monotonic_usUART 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
payloadWire-format USHER_POSE_SAMPLE bytes.
lengthMust equal the packed message size.
receive_monotonic_usUART receive timestamp from CLOCK_MONOTONIC.
evidenceOptional 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()

void * write_queue ( void * unused)
static

Drain bounded batches off the UART thread and fdatasync before counting them synced.

Parameters
unusedpthread entry-point argument; unused.

Definition at line 148 of file pose_log.c.

◆ write_record()

int write_record ( const struct pose_record * record)
static

Serialize one validated wire sample and its optional clock mapping as a CSV row.

Parameters
recordQueue-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

uint64_t accepted
static

Definition at line 81 of file pose_log.c.

◆ active

bool active
static

Definition at line 80 of file pose_log.c.

◆ available

pthread_cond_t available
static

Definition at line 73 of file pose_log.c.

◆ boot_id

char boot_id[37]
static

Definition at line 84 of file pose_log.c.

◆ bytes_written

size_t bytes_written
static

Definition at line 79 of file pose_log.c.

◆ condition_error

int condition_error
static

Definition at line 75 of file pose_log.c.

◆ condition_once

pthread_once_t condition_once = PTHREAD_ONCE_INIT
static

Definition at line 74 of file pose_log.c.

◆ count

size_t count
static

Definition at line 79 of file pose_log.c.

◆ dropped

atomic_uint_fast64_t dropped
static

Definition at line 82 of file pose_log.c.

◆ head

size_t head
static

Definition at line 79 of file pose_log.c.

◆ header

const char header[]
static
Initial value:
=
"schema,mora_boot_id,receive_monotonic_us,accepted_index,queue_dropped,fc_sequence,fc_sample_begin_us,fc_sample_end_us,"
"next_shot_nr,lat_e7deg,lon_e7deg,ellipsoid_alt_mm,roll_bfp,pitch_bfp,yaw_bfp,ground_speed_bfp,"
"course_bfp,agl_bfp,velocity_north_bfp,velocity_east_bfp,velocity_down_bfp,gps_tow_ms,gps_week,"
"gps_hacc_cm,gps_vacc_cm,gps_sacc_cm_s,gps_fix,gps_num_sv,gps_valid_fields,flags,"
"clock_token_low,clock_token_high,clock_mapped,sample_earliest_monotonic_us,sample_latest_monotonic_us,"
"probe_sent_monotonic_us,probe_received_monotonic_us,probe_receive_fc_us,probe_transmit_fc_us,clock_drift_bound_ppm\n"

Definition at line 87 of file pose_log.c.

◆ log_error

int log_error
static

Definition at line 83 of file pose_log.c.

◆ mutex

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 72 of file pose_log.c.

◆ output

FILE* output
static

Definition at line 77 of file pose_log.c.

◆ queue

struct pose_record queue[POSE_LOG_CAPACITY]
static

Definition at line 78 of file pose_log.c.

◆ rejected

uint64_t rejected
static

Definition at line 81 of file pose_log.c.

◆ stopping

bool stopping
static

Definition at line 80 of file pose_log.c.

◆ synced

uint64_t synced
static

Definition at line 81 of file pose_log.c.

◆ writer

pthread_t writer
static

Definition at line 76 of file pose_log.c.