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
lwir_cam_pipe.c File Reference

USHER-side supervisor for the persistent LWIR capture-server process. More...

#include "lwir_cam_pipe.h"
#include "camera_capture.h"
#include "../modules/lwir_cam/lwir_features.h"
#include "path_utils.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <poll.h>
#include <signal.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "camera_backend.h"
Include dependency graph for lwir_cam_pipe.c:

Go to the source code of this file.

Macros

#define USHER_LWIR_CAM_PHOTO_DIR   "~/Pictures/sensoraia"
#define USHER_LWIR_CAM_COMMAND   "./bin/lwircam"
#define CHILD_STOP_GRACE_MS   500
#define CHILD_WAIT_INTERVAL_MS   10
#define IMAGE_PROCESS_TIMEOUT_MS   30000
#define LWIR_SERVER_START_TIMEOUT_MS   30000
#define LWIR_WARMUP_TIMEOUT_MS   30000
#define LWIR_REQUEST_TIMEOUT_MS   8000

Functions

void lwir_cam_pipe_configure (const struct camera_backend_host *host)
 Bind the owner's long-lived shutdown flag before starting workers.
int lwir_cam_pipe_set_config (const char *path)
 Apply a bounded worker configuration before starting the capture server.
static uint64_t monotonic_ms (void)
 Read the capture-server watchdog clock.
static int wait_child (pid_t pid, int *status, int timeout_ms)
 Wait up to a bounded time for the capture child to exit.
static int terminate_child (pid_t pid, int *status)
 Stop an owned capture child, escalating only after its grace period.
static int create_cloexec_pipe (int descriptors[2])
 Open a close-on-exec pipe for child protocol setup.
void lwir_cam_pipe_set_photo_directory (const char *directory)
 Override the output directory for an isolated standalone benchmark.
void lwir_cam_pipe_set_native_raw (int enabled)
 Select whether successful captures keep an explicit .raw companion.
int lwir_cam_pipe_set_calibration (const char *path)
 Set the optional calibration file used by server-side geolocation.
struct capture_timing lwir_cam_pipe_capture_timing (void)
 Return detailed callback or polling timing evidence for the last capture.
static int parse_capture_timing (const char *text, struct capture_timing *timing, bool callback)
 Parse one server timing payload after its protocol prefix.
double lwir_cam_pipe_capture_delay (void)
 Return the delay of the last successful capture request in seconds.
static int read_server_status (const char *expected, int timeout_ms, const volatile sig_atomic_t *keep_running)
 Read server diagnostic lines until an expected terminal reply arrives.
static void stop_capture_server (void)
 Close pipes and reap or terminate the owned capture server.
static int send_request_and_wait (const char *request, size_t request_size, int timeout_ms, const char *expected)
 Send one newline-terminated server request and require an OK reply.
static int start_capture_server (const char *unused, const volatile sig_atomic_t *keep_running)
 Start the persistent thermal capture subprocess.
int lwir_cam_pipe_init (const char *unused)
 Start and qualify the persistent LWIR capture server.
int lwir_cam_pipe_init_cancelable (const char *unused, const volatile sig_atomic_t *keep_running)
 Start the server but abort promptly when USHER begins shutdown.
int lwir_cam_pipe_warmup (void)
 Open, stabilize, and close the sensor without writing an image.
int lwir_cam_pipe_process_mock (char *filename)
 Process a mock thermal image for hardware-free local testing.
int lwir_cam_pipe_geolocate (char *filename)
 Add thermal hotspot/geolocation metadata through the running server.
int lwir_cam_pipe_shoot (char *filename, size_t filename_size, int image_number)
 Request one thermal JPEG from an initialized capture server.
void lwir_cam_pipe_deinit (void)
 Stop, reap, and forget the persistent LWIR capture server.

Variables

char ** environ
static pid_t capture_server_pid = -1
static int capture_server_input = -1
static int capture_server_output = -1
static char status_buffer [512]
static size_t status_buffer_size
static size_t status_buffer_pos
static double capture_delay_s = -1
static struct capture_timing capture_times
static bool native_raw_enabled
static const char * calibration_path
static const char * photo_directory = USHER_LWIR_CAM_PHOTO_DIR
static const char * capture_command = USHER_LWIR_CAM_COMMAND
static const char * lwir_config
static const volatile sig_atomic_t * running

Detailed Description

USHER-side supervisor for the persistent LWIR capture-server process.

The server holds the Tiny1-C stream open, avoiding per-shot sensor warmup. This parent owns its pipes and child lifecycle, validates line-protocol replies and capture timing. All failure paths clear state before returning so USHER's supervisor can schedule a later retry without reusing a stale descriptor.

Definition in file lwir_cam_pipe.c.

Macro Definition Documentation

◆ CHILD_STOP_GRACE_MS

#define CHILD_STOP_GRACE_MS   500

Definition at line 59 of file lwir_cam_pipe.c.

◆ CHILD_WAIT_INTERVAL_MS

#define CHILD_WAIT_INTERVAL_MS   10

Definition at line 61 of file lwir_cam_pipe.c.

◆ IMAGE_PROCESS_TIMEOUT_MS

#define IMAGE_PROCESS_TIMEOUT_MS   30000

Definition at line 62 of file lwir_cam_pipe.c.

◆ LWIR_REQUEST_TIMEOUT_MS

#define LWIR_REQUEST_TIMEOUT_MS   8000

Definition at line 68 of file lwir_cam_pipe.c.

◆ LWIR_SERVER_START_TIMEOUT_MS

#define LWIR_SERVER_START_TIMEOUT_MS   30000

Definition at line 64 of file lwir_cam_pipe.c.

◆ LWIR_WARMUP_TIMEOUT_MS

#define LWIR_WARMUP_TIMEOUT_MS   30000

Definition at line 66 of file lwir_cam_pipe.c.

◆ USHER_LWIR_CAM_COMMAND

#define USHER_LWIR_CAM_COMMAND   "./bin/lwircam"

Definition at line 55 of file lwir_cam_pipe.c.

◆ USHER_LWIR_CAM_PHOTO_DIR

#define USHER_LWIR_CAM_PHOTO_DIR   "~/Pictures/sensoraia"

Definition at line 51 of file lwir_cam_pipe.c.

Function Documentation

◆ create_cloexec_pipe()

int create_cloexec_pipe ( int descriptors[2])
static

Open a close-on-exec pipe for child protocol setup.

Parameters
[out]descriptorsRead and write descriptors; valid only on success.
Returns
Zero on success, or -1 with errno on failure.

Keep both descriptors above stderr so child's descriptor redirection cannot alias an endpoint when a standard descriptor was closed.

Definition at line 156 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_capture_delay()

double lwir_cam_pipe_capture_delay ( void )

Return the delay of the last successful capture request in seconds.

Returns
Nonnegative delay, or a negative value when no timing is available.

Definition at line 242 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_capture_timing()

struct capture_timing lwir_cam_pipe_capture_timing ( void )

Return detailed callback or polling timing evidence for the last capture.

Returns
Timing structure; use capture_timing_valid() before consuming it.

Definition at line 202 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_configure()

void lwir_cam_pipe_configure ( const struct camera_backend_host * host)

Bind the owner's long-lived shutdown flag before starting workers.

Parameters
hostOnly its running flag is retained; NULL clears the binding.

Definition at line 90 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_deinit()

void lwir_cam_pipe_deinit ( void )

Stop, reap, and forget the persistent LWIR capture server.

Definition at line 707 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_geolocate()

int lwir_cam_pipe_geolocate ( char * filename)

Add thermal hotspot/geolocation metadata through the running server.

Parameters
[in]filenameJPEG to update.
Returns
0 on success or -1 if the operation cannot be completed.

Definition at line 623 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_init()

int lwir_cam_pipe_init ( const char * unused)

Start and qualify the persistent LWIR capture server.

Parameters
unusedReserved common backend argument.
Returns
0 after the server reports ready, otherwise -1.

Definition at line 520 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_init_cancelable()

int lwir_cam_pipe_init_cancelable ( const char * unused,
const volatile sig_atomic_t * keep_running )

Start the server but abort promptly when USHER begins shutdown.

Parameters
unusedReserved common backend argument.
keep_runningSignal-safe flag that remains nonzero while startup may continue.
Returns
0 after the server reports ready, otherwise -1 after cleanup.

Definition at line 525 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_process_mock()

int lwir_cam_pipe_process_mock ( char * filename)

Process a mock thermal image for hardware-free local testing.

Parameters
[in,out]filenameInput/output image path.
Returns
0 on success or -1 on processing failure.

Definition at line 612 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_set_calibration()

int lwir_cam_pipe_set_calibration ( const char * path)

Set the optional calibration file used by server-side geolocation.

Parameters
pathNonempty calibration YAML path.
Returns
0 on acceptance or -1 for an invalid path.

Definition at line 194 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_set_config()

int lwir_cam_pipe_set_config ( const char * path)

Apply a bounded worker configuration before starting the capture server.

Parameters
[in]pathConfiguration file path.
Returns
0 when accepted, otherwise -1; live reconfiguration is not implied.

Definition at line 95 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_set_native_raw()

void lwir_cam_pipe_set_native_raw ( int enabled)

Select whether successful captures keep an explicit .raw companion.

Parameters
enabledNonzero to request native raw output.

Definition at line 189 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_set_photo_directory()

void lwir_cam_pipe_set_photo_directory ( const char * directory)

Override the output directory for an isolated standalone benchmark.

Definition at line 184 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_shoot()

int lwir_cam_pipe_shoot ( char * filename,
size_t filename_size,
int image_number )

Request one thermal JPEG from an initialized capture server.

Parameters
filenameOutput path buffer, cleared on failure.
filename_sizeOutput path capacity.
image_numberUSHER shot sequence.
Returns
0 only for a verified nonempty output image, otherwise -1; USHER owns retry policy.

Definition at line 653 of file lwir_cam_pipe.c.

◆ lwir_cam_pipe_warmup()

int lwir_cam_pipe_warmup ( void )

Open, stabilize, and close the sensor without writing an image.

Returns
0 after a stable warmup frame, otherwise -1.

Open the sensor once, wait past its power-on "wiggly" image, then close it again. Runs regardless of whether LWIR ends up selected for a shot, so a later real open (persistent server or standalone capture) never has to absorb that transient inline. Failure here is not fatal to USHER.

Definition at line 531 of file lwir_cam_pipe.c.

◆ monotonic_ms()

uint64_t monotonic_ms ( void )
static

Read the capture-server watchdog clock.

Returns
Monotonic milliseconds, or zero on failure.

Keep supervision independent of system time corrections.

Definition at line 105 of file lwir_cam_pipe.c.

◆ parse_capture_timing()

int parse_capture_timing ( const char * text,
struct capture_timing * timing,
bool callback )
static

Parse one server timing payload after its protocol prefix.

Parameters
textSpace-separated unsigned timing fields.
timingReceives validated timing evidence.
callbackTrue for callback sequence/drop fields, false for polling timing.
Returns
1 for valid evidence, 0 for malformed or implausible evidence.

Parsing is intentionally strict: timing metadata enriches EXIF but must never be accepted merely because a server process printed a vaguely similar line.

Definition at line 214 of file lwir_cam_pipe.c.

◆ read_server_status()

int read_server_status ( const char * expected,
int timeout_ms,
const volatile sig_atomic_t * keep_running )
static

Read server diagnostic lines until an expected terminal reply arrives.

Parameters
expectedSuccess line required to complete the operation.
timeout_msWhole-operation deadline.
keep_runningOptional cancellation flag; checked between bounded polls so stopping USHER need not wait for a thermal camera's full startup deadline.
Returns
0 on the expected reply, -1 for cancellation, timeout, EOF, or server error.

Informational timing lines are parsed before the terminal reply. The buffer avoids a syscall per character while retaining line framing. One absolute deadline prevents a noisy or malicious child from extending an operation indefinitely.

Definition at line 256 of file lwir_cam_pipe.c.

◆ send_request_and_wait()

int send_request_and_wait ( const char * request,
size_t request_size,
int timeout_ms,
const char * expected )
static

Send one newline-terminated server request and require an OK reply.

Send one line request to the running server and wait for its reply. On any failure (broken pipe, dead process, or a timed-out/erroring reply) the server state is fully reset so a caller's retry starts with a clean respawn instead of repeatedly hitting the same stale, already-dead connection.

Returns
0 on end-to-end success or -1 after fully resetting failed server state.

Definition at line 363 of file lwir_cam_pipe.c.

◆ start_capture_server()

int start_capture_server ( const char * unused,
const volatile sig_atomic_t * keep_running )
static

Start the persistent thermal capture subprocess.

Parameters
unusedReserved backend argument.
keep_runningOptional shutdown flag used during the readiness wait.
Returns
Zero after readiness, or -1 on ownership, setup, or protocol failure.

Resolve any old owner before launching. Connect separate command and response pipes and require the startup acknowledgement before capture.

Definition at line 406 of file lwir_cam_pipe.c.

◆ stop_capture_server()

void stop_capture_server ( void )
static

Close pipes and reap or terminate the owned capture server.

Buffered response state is reset before the next spawn. Closing stdin asks cooperative server versions to exit; SIGTERM is used only when it remains alive.

Definition at line 333 of file lwir_cam_pipe.c.

◆ terminate_child()

int terminate_child ( pid_t pid,
int * status )
static

Stop an owned capture child, escalating only after its grace period.

Parameters
pidOwned positive child PID.
[out]statusWait status, set if the child is reaped here.
Returns
One if ownership ended, zero if still waiting after escalation, or -1 on error.

Reap an already exited child first. Give normal teardown a chance to release the camera before sending SIGKILL.

Definition at line 140 of file lwir_cam_pipe.c.

◆ wait_child()

int wait_child ( pid_t pid,
int * status,
int timeout_ms )
static

Wait up to a bounded time for the capture child to exit.

Parameters
pidOwned positive child PID.
[out]statusWait status, set if this call reaps the child.
timeout_msNonnegative budget; zero checks without sleeping.
Returns
One if reaped or no longer a child, zero on timeout, or -1 on error.

Use nonblocking waits so a child stuck in a device call does not indefinitely block its supervisor.

Definition at line 119 of file lwir_cam_pipe.c.

Variable Documentation

◆ calibration_path

const char* calibration_path
static

Definition at line 84 of file lwir_cam_pipe.c.

◆ capture_command

const char* capture_command = USHER_LWIR_CAM_COMMAND
static

Definition at line 86 of file lwir_cam_pipe.c.

◆ capture_delay_s

double capture_delay_s = -1
static

Definition at line 81 of file lwir_cam_pipe.c.

◆ capture_server_input

int capture_server_input = -1
static

Definition at line 74 of file lwir_cam_pipe.c.

◆ capture_server_output

int capture_server_output = -1
static

Definition at line 75 of file lwir_cam_pipe.c.

◆ capture_server_pid

pid_t capture_server_pid = -1
static

Definition at line 73 of file lwir_cam_pipe.c.

◆ capture_times

struct capture_timing capture_times
static

Definition at line 82 of file lwir_cam_pipe.c.

◆ environ

char** environ
extern

◆ lwir_config

const char* lwir_config
static

Definition at line 87 of file lwir_cam_pipe.c.

◆ native_raw_enabled

bool native_raw_enabled
static

Definition at line 83 of file lwir_cam_pipe.c.

◆ photo_directory

const char* photo_directory = USHER_LWIR_CAM_PHOTO_DIR
static

Definition at line 85 of file lwir_cam_pipe.c.

◆ running

const volatile sig_atomic_t* running
static

Definition at line 88 of file lwir_cam_pipe.c.

◆ status_buffer

char status_buffer[512]
static

Buffered reassembly for read_server_status(): avoids one poll()+read() syscall pair per byte, which otherwise adds up to hundreds of syscalls per response line set on every single shot.

Definition at line 79 of file lwir_cam_pipe.c.

◆ status_buffer_pos

size_t status_buffer_pos
static

Definition at line 80 of file lwir_cam_pipe.c.

◆ status_buffer_size

size_t status_buffer_size
static

Definition at line 80 of file lwir_cam_pipe.c.