Loading...
Searching...
No Matches
loom_cam.c
Go to the documentation of this file.
58#define LOOM_SEQUENCE_TEXT_BYTES 16U /**< Decimal uint32 ID plus terminator, without truncation. */
60#define LOOM_WORKER_TIMEOUT_MS 5000U /**< Whole reply operation budget in monotonic milliseconds. */
65#define LOOM_REFERENCE_SESSION_LIMIT 64U /**< Bound retained sortie directories without pruning evidence. */
73#define LOOM_LONGITUDE_LIMIT_E7 INT32_C(1800000000) /**< Longitude range in the same wire units. */
74#define LOOM_QUALITY_MAX INT32_C(1000) /**< Quality uses thousandths, not an arbitrary float score. */
75#define LOOM_REFERENCE_SEQUENCE_DIGITS 10 /**< Fits every uint32 photo number in the archive basename. */
77#define LOOM_JPEG_EXTENSION_BYTES (sizeof(LOOM_JPEG_EXTENSION) - 1U) /**< Exclude the string terminator. */
103static pthread_mutex_t loom_mutex = PTHREAD_MUTEX_INITIALIZER; /**< Protects admission, phase and mailbox state. */
104static pthread_cond_t loom_available = PTHREAD_COND_INITIALIZER; /**< Wakes the worker for pending work or shutdown. */
107static atomic_bool loom_running; /**< Lifecycle availability queried without taking the mutex. */
109 * @brief The latest copied path awaits worker consumption; older pending work is replaceable. */
110static bool loom_pending, loom_ready; /**< One completion mailbox is available for consumption. */
111static uint32_t loom_latest_sequence; /**< Highest admitted FC photo number; rejects duplicates/reversed work. */
114static uint32_t loom_session, loom_phase; /**< Generation that invalidates results crossing a phase change. */
120static char loom_filename[PATH_MAX], loom_references[PATH_MAX], loom_session_references[PATH_MAX];
122static char loom_settings[PATH_MAX]; /**< Selected assignment, immutable during worker lifetime. */
125static struct camera_point_result loom_result; /**< Single correlated matched/no-match completion. */
137 do { received = eventfd_read(loom_notification, &count); } while (received < 0 && errno == EINTR);
148 do { written = write(loom_notification, &count, sizeof(count)); } while (written < 0 && errno == EINTR);
201static bool parse_result(const char *line, uint32_t sequence, struct camera_point_result *result)
205 "LOOM_POINT_V1 sequence=", " valid=", " lat=", " lon=", " alt_mm=", " agl_mm=", " quality=", " metric="
211 if (index >= LOOM_FIELD_LATITUDE && (values[index] < INT32_MIN || values[index] > INT32_MAX)) return false;
214 || (values[LOOM_FIELD_VALID] != LOOM_WIRE_INVALID && values[LOOM_FIELD_VALID] != LOOM_WIRE_VALID)
219 .alt_mm = (int32_t)values[LOOM_FIELD_ALTITUDE_MM], .agl_mm = (int32_t)values[LOOM_FIELD_AGL_MM],
313 if (!setup) setup = posix_spawn_file_actions_adddup2(&actions, output[LOOM_PIPE_WRITE], STDOUT_FILENO);
329 loom_settings[0] ? "--settings" : "--model", loom_settings[0] ? loom_settings : loom_model, NULL
380 return complete && matcher_phase_current(phase) && WIFEXITED(status) && WEXITSTATUS(status) == 0
423 if (!base || !base[0] || usher_ensure_directory(base) != 0 || !reference_root_available(base)) return false;
441 const int length = snprintf(destination, sizeof(destination), "%s/%c%0*" PRIu32 LOOM_JPEG_EXTENSION,
484 (double)result->quality / LOOM_QUALITY_MAX < loom_return_options.limits.minimum_confidence) return false;
500 earth * scale * remainder(result->lon / 1e7 - loom_return_options.home_longitude, 360) * radians,
507 const double longitude = remainder(loom_return_options.home_longitude + candidate.east / (earth * scale) / radians, 360);
563 const int result = loom_running || !copy_path(loom_settings, sizeof(loom_settings), settings) ? -1 : 0;
574 || !copy_path(loom_model, sizeof(loom_model), model)) { pthread_mutex_unlock(&loom_mutex); return -1; }
579 if (!selected_settings && inference_settings_find("loomcam", "loom_cam", settings_path, sizeof(settings_path)))
584 if (!inference_settings_read(&inference, selected_settings, loom_return_setting, &loom_return_options,
625 const bool eligible = loom_running && !loom_stopping && session_id != 0 && session_id != loom_session;
655 const bool accepted = loom_running && !loom_stopping && session_id && session_id == loom_session;
673 if (!filename || !shot || shot->data.nr <= 0 || pthread_mutex_trylock(&loom_mutex) != 0) return;
static void wait_ms(int milliseconds)
Wait until a short polling interval elapses.
Definition ai_cam_pipe.c:114
char ** environ
bool inference_settings_find(const char *application, const char *module, char *filename, size_t capacity)
Discover application_settings.ini in adjacent, development-module, then installed-share order.
Definition inference_settings.c:202
bool inference_settings_read(struct inference_settings *settings, const char *filename, ini_field_handler extra, void *context, char *error, size_t capacity)
Parse one flat INI, delegate application keys and resolve paths against the selected file.
Definition inference_settings.c:158
void inference_settings_init(struct inference_settings *settings, enum inference_backend backend)
Reset storage and select conservative thread/timeout defaults.
Definition inference_settings.c:28
Shared application_settings.ini discovery with module-owned extra keys.
bool loom_cam_poll_result(struct camera_point_result *result)
Consume a result on the serial owner; session_id carries the photo number for this stream.
Definition loom_cam.c:685
#define USHER_LOOMCAM_COMMAND
Saved-photo child executable; not another camera-acquisition process.
Definition loom_cam.c:51
bool loom_cam_start(uint32_t session_id)
Select outbound reference collection and discard old-phase mailbox work.
Definition loom_cam.c:621
static bool loom_pending
The latest copied path awaits worker consumption; older pending work is replaceable.
Definition loom_cam.c:110
bool loom_cam_stop(uint32_t session_id)
Select return matching and discard old-phase mailbox work.
Definition loom_cam.c:652
static bool copy_path(char *destination, size_t capacity, const char *source)
Copy a complete NUL-terminated path into owned, fixed-size storage.
Definition loom_cam.c:158
static uint64_t loom_monotonic_ms(void)
Read the watchdog clock in milliseconds.
Definition loom_cam.c:235
void loom_cam_deinit(void)
Cancel pending work and join the worker before reusing adapter state.
Definition loom_cam.c:695
int loom_cam_set_settings(const char *settings)
Select executable settings before starting the saved-photo worker.
Definition loom_cam.c:560
static char loom_references[PATH_MAX]
Reference-directory configuration, fixed until the worker has been joined.
Definition loom_cam.c:120
static bool filter_return_point(struct camera_point_result *result)
Validate a return candidate in the persistent owner.
Definition loom_cam.c:479
static void notify_completion(void)
Wake the event loop when completion state may have changed.
Definition loom_cam.c:143
static uint32_t loom_session
START token used to admit the corresponding STOP, not a photo number.
Definition loom_cam.c:114
static bool run_matcher(const char *filename, const char *references, uint32_t sequence, struct camera_point_result *result, uint32_t phase)
Run the saved-photo matcher with bounded output and a watchdog.
Definition loom_cam.c:296
static bool matcher_phase_current(uint32_t phase)
Check whether an in-flight match still belongs to the active phase.
Definition loom_cam.c:276
static bool reap_matcher(unsigned timeout_ms, int *status)
Wait up to a bounded time for the owned matcher to exit.
Definition loom_cam.c:250
loom_wire_validity
The validity field accepts only these two numeric wire values.
Definition loom_cam.c:95
static char loom_filename[PATH_MAX]
Latest admitted immutable saved-photo path, copied into bounded storage.
Definition loom_cam.c:120
loom_pipe_endpoint
POSIX pipe endpoints; names distinguish child output from parent input.
Definition loom_cam.c:97
static bool read_result_field(const char **cursor, const char *key, intmax_t *value)
Consume one exact field prefix followed by a signed decimal integer.
Definition loom_cam.c:176
static bool reference_directory_available(const char *directory)
Check whether a session reference directory has room for another entry.
Definition loom_cam.c:391
static void * loom_worker(void *unused)
Process the newest pending photo and suppress superseded completions.
Definition loom_cam.c:524
bool loom_cam_enabled(void)
Report whether this optional stream is running and not stopping.
Definition loom_cam.c:608
static bool record_reference(const char *filename, const char *directory, uint32_t sequence)
Archive a published outbound JPEG without copying its pixels.
Definition loom_cam.c:438
static bool reference_root_available(const char *directory)
Check whether the archive root can admit another session.
Definition loom_cam.c:455
static bool create_session_reference_directory(const char *base, uint32_t session_id, char *destination, size_t capacity)
Create an exclusive archive directory for a new session.
Definition loom_cam.c:420
void loom_cam_image_saved(const char *filename, const union dc_shot_union *shot)
Copy the newest completed AIcam photo request into bounded worker storage.
Definition loom_cam.c:669
int loom_cam_completion_fd(void)
Borrow the nonblocking completion descriptor; minus one when not initialized.
Definition loom_cam.c:613
static bool parse_result(const char *line, uint32_t sequence, struct camera_point_result *result)
Validate and correlate one complete reply before publishing any fields.
Definition loom_cam.c:201
int loom_cam_init(const char *references, const char *model)
Start one saved-photo worker; does not acquire another camera.
Definition loom_cam.c:568
Bounded asynchronous saved-photo adapter for LOOM_cam.
#define LOOM_IMAGE_PREFIX
LOOM photographs and outbound references use 'o', not AIcam's 'a'.
Definition loom_cam.h:38
void loom_return_options_init(struct loom_return_options *options)
Initialize return limits with feature-only semantics by default.
Definition loom_return.c:40
bool loom_return_options_valid(const struct loom_return_options *options)
Check that optional destination coordinates are either both present or both omitted.
Definition loom_return.c:47
bool loom_return_update(struct loom_return_state *state, const struct loom_return_config *config, uint32_t sequence, struct loom_return_point candidate, struct loom_return_point *target)
Admit a fresh bounded target and limit its movement.
Definition loom_return.c:171
bool loom_return_setting(void *context, const char *key, const char *value)
Parse one LOOM-owned settings field, rejecting duplicates and unknown keys.
Definition loom_return.c:55
Bounded direction-to-target policy, independent of inference and FC transport.
#define LOOM_DECIMAL_RADIX
The CLI accepts decimal photo numbers, never signed or truncated IDs.
Definition loomcam.c:46
Small path and writable-directory helpers shared by USHER backends.
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
Definition path_utils.h:97
Definition camera_backend.h:50
Per-application configuration; all resolved paths belong to this caller-owned value.
Definition inference_settings.h:39
Module-owned inference settings, separate from universal runtime settings.
Definition loom_return.h:65
Local ground point in metres east/north of the reference origin.
Definition loom_return.h:31
Caller-owned sequence and smoothing state; first observation seeds target.
Definition loom_return.h:51
One timestamped geographic position for a replay annotation.
Definition ear_heatmap_overlay.c:79
Definition usher_protocol.h:51