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
image_lens.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 */
5#include "image_lens.h"
6
7#include <errno.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <string.h>
11#include <sys/prctl.h>
12#include <sys/stat.h>
13#include <sys/wait.h>
14#include <time.h>
15#include <unistd.h>
16
17#ifndef IMAGE_LENS_TIMEOUT_MS
18#define IMAGE_LENS_TIMEOUT_MS 90000
19#endif
20
21static bool monotonic_ms(uint64_t *value)
22{
23 struct timespec now;
24 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0 || now.tv_sec < 0) return false;
25 *value = (uint64_t)now.tv_sec * 1000U + (uint64_t)now.tv_nsec / 1000000U;
26 return true;
27}
28
29bool image_lens_emulate(const char *application, const char *input,
30 const char *profile, const char *cache,
31 char *output, size_t capacity,
32 const volatile sig_atomic_t *running)
33{
34 if (!application || !input || !profile || !output || !capacity ||
35 !application[0] || !input[0] || !profile[0]) {
36 fprintf(stderr, "IMAGE_LENS: invalid renderer request\n");
37 return false;
38 }
39 output[0] = '\0';
40 const char *extension = strrchr(input, '.');
41 const char *separator = strrchr(input, '/');
42 const size_t stem = extension && (!separator || extension > separator)
43 ? (size_t)(extension - input) : strlen(input);
44 const char suffix[] = ".lens.jpg";
45 if (stem >= capacity || sizeof(suffix) > capacity - stem) {
46 fprintf(stderr, "IMAGE_LENS: derivative path too long\n");
47 return false;
48 }
49 uint64_t started;
50 if (!monotonic_ms(&started) || (running && !*running)) {
51 fprintf(stderr, "IMAGE_LENS: clock unavailable or capture cancelled\n");
52 return false;
53 }
54 for (size_t index = 0; index < stem; ++index) output[index] = input[index];
55 for (size_t index = 0; index < sizeof(suffix); ++index) output[stem + index] = suffix[index];
56 char *arguments[9] = {(char *)application, (char *)"--profile", (char *)profile};
57 size_t count = 3;
58 if (cache) {
59 arguments[count++] = (char *)"--cache";
60 arguments[count++] = (char *)cache;
61 }
62 arguments[count++] = (char *)input;
63 arguments[count++] = output;
64 arguments[count] = NULL;
65 struct sigaction defaults = {.sa_handler = SIG_DFL};
66 sigemptyset(&defaults.sa_mask);
67 sigset_t mask;
68 sigemptyset(&mask);
69 const pid_t parent = getpid();
70 const pid_t child = fork();
71 if (child == 0) {
72 /* Only Linux/POSIX system calls after fork: this also runs in USHER's
73 * multithreaded capture worker. Never leave a renderer behind its owner. */
74 if (sigaction(SIGTERM, &defaults, NULL) != 0 || sigaction(SIGINT, &defaults, NULL) != 0 ||
75 sigprocmask(SIG_SETMASK, &mask, NULL) != 0 ||
76 prctl(PR_SET_PDEATHSIG, SIGTERM) != 0 || getppid() != parent) _exit(1);
77 execv(application, arguments);
78 _exit(127);
79 }
80 if (child < 0) {
81 fprintf(stderr, "IMAGE_LENS: cannot start %s: %s\n", application, strerror(errno));
82 output[0] = '\0';
83 return false;
84 }
85 int status = 0;
86 for (;;) {
87 const pid_t result = waitpid(child, &status, WNOHANG);
88 if (result == child) break;
89 if (result < 0 && errno != EINTR) {
90 fprintf(stderr, "IMAGE_LENS: cannot wait for renderer: %s\n", strerror(errno));
91 output[0] = '\0';
92 return false;
93 }
94 uint64_t now;
95 if (!monotonic_ms(&now) || now < started || now - started >= IMAGE_LENS_TIMEOUT_MS ||
96 (running && !*running)) {
97 fprintf(stderr, "IMAGE_LENS: renderer deadline/cancellation; analysis skipped\n");
98 kill(child, SIGKILL);
99 while (waitpid(child, &status, 0) < 0 && errno == EINTR) {}
100 output[0] = '\0';
101 return false;
102 }
103 const struct timespec delay = {.tv_nsec = 10000000L};
104 nanosleep(&delay, NULL);
105 }
106 struct stat saved;
107 if ((running && !*running) || !WIFEXITED(status) || WEXITSTATUS(status) != 0 ||
108 stat(output, &saved) != 0 || !S_ISREG(saved.st_mode) || saved.st_size <= 0) {
109 fprintf(stderr, "IMAGE_LENS: renderer failed or derivative unavailable; analysis skipped\n");
110 output[0] = '\0';
111 return false;
112 }
113 return true;
114}
static unsigned capacity[COVERAGE_CAMERAS]
static uint64_t monotonic_ms(void)
Read a wall-clock-independent time base; return zero if the clock fails.
Definition chdkbridge.c:141
static pid_t child
static bool started
bool image_lens_emulate(const char *application, const char *input, const char *profile, const char *cache, char *output, size_t capacity, const volatile sig_atomic_t *running)
Definition image_lens.c:29
#define IMAGE_LENS_TIMEOUT_MS
Definition image_lens.c:18
static const volatile sig_atomic_t * running
static struct usher_camera_profile profile
static size_t count
Definition pose_log.c:79
static FILE * output
Definition pose_log.c:77
struct usher_control_result result
Definition usher.c:377