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
local_pipe.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file local_pipe.c
20 * @brief Hardware-free camera backend for fixture copies and cached map views.
21 * @details Use the requested camera identity for naming while keeping simulation
22 * outputs distinct from hardware captures. Map rendering delegates projection and
23 * publication to the shared image workspace. Fixture mode copies an existing image
24 * without recoding it. */
25#include "local_pipe.h"
27#include "path_utils.h"
28
29#include <dirent.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <limits.h>
33#include <stdint.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include <string.h>
37#include <strings.h>
38#include <sys/stat.h>
39#include <unistd.h>
40
41#ifndef USHER_LOCAL_PHOTO_DIR
42 #define USHER_LOCAL_PHOTO_DIR "~/Pictures/sensoraia"
43#endif
44
45#ifndef USHER_MOCK_IMAGE
46 #define USHER_MOCK_IMAGE "resources/mock_image_01.jpg"
47#endif
48
49#ifndef PATH_MAX
50 #define PATH_MAX 4096
51#endif
52
56
57static int prepare_photo_directory(void);
58static int resolve_photo_directory(void);
59static int find_test_photo_directory(char *directory, size_t directory_size);
60static int select_random_test_photo(char *path, size_t path_size);
61static bool is_readable_jpg(DIR *directory, const struct dirent *entry);
62static int random_index(size_t count, size_t *index);
63
65{
66 const size_t source_length = mock_image ? strnlen(mock_image, sizeof(source_image)) : 0;
67 if (!mock_image || source_length >= sizeof(source_image)) {
68 errno = mock_image ? ENAMETOOLONG : EINVAL;
69 return -1;
70 }
71 if (mock_image == NULL || access(mock_image, R_OK) != 0) {
72 fprintf(stderr, "LOCAL_PIPE:\tfailed to access mock image %s: %s\n",
73 mock_image != NULL ? mock_image : "(null)", strerror(errno));
74 return -1;
75 }
76
77 if (prepare_photo_directory() != 0) {
78 return -1;
79 }
80
81 for (size_t index = 0; index <= source_length; ++index) source_image[index] = mock_image[index];
82 test_photo_directory[0] = '\0';
83 printf("LOCAL_PIPE:\tphoto directory: %s\n", photo_directory);
84 return 0;
85}
86
88{
89 if (mock_image != NULL) {
91 }
92
93 if (prepare_photo_directory() != 0) {
94 return -1;
95 }
96
98 if (photo_count < 0) {
99 return -1;
100 }
101 if (photo_count == 0) {
102 fprintf(stderr, "LOCAL_PIPE:\tno readable .jpg files found beside the executable; using %s\n",
105 }
106
107 source_image[0] = '\0';
108 printf("LOCAL_PIPE:\ttest photo directory: %s (%d image%s)\n",
109 test_photo_directory, photo_count, photo_count == 1 ? "" : "s");
110 printf("LOCAL_PIPE:\tphoto directory: %s\n", photo_directory);
111 return 0;
112}
113
114/** @brief Prepare usable output storage before accepting local shots.
115 * @return Zero for a writable directory, or -1 on resolution or storage failure.
116 * @details Confirm directory type after creation; an existing regular file
117 * at the configured path is not a usable output location. */
119{
120 if (resolve_photo_directory() != 0) {
121 return -1;
122 }
124 fprintf(stderr, "LOCAL_PIPE:\tfailed to create photo directory %s: %s\n",
125 photo_directory, strerror(errno));
126 return -1;
127 }
128
129 struct stat directory_status;
130 if (stat(photo_directory, &directory_status) != 0 || !S_ISDIR(directory_status.st_mode)
131 || access(photo_directory, W_OK) != 0) {
132 fprintf(stderr, "LOCAL_PIPE:\tphoto directory is not writable: %s\n", photo_directory);
133 return -1;
134 }
135 return 0;
136}
137
138/** @brief Resolve the configured local output path into backend storage.
139 * @return Zero if the path fits, or -1 on invalid configuration or path lookup failure.
140 * @details Anchor relative paths beside the executable rather than the
141 * caller's working directory so service and interactive launches agree. */
143{
144 char resolved_dir[PATH_MAX];
145 const char *configured = usher_resolve_path(USHER_LOCAL_PHOTO_DIR, resolved_dir, sizeof(resolved_dir));
146 if (configured == NULL || configured[0] == '\0') {
147 return -1;
148 }
149 if (configured[0] == '/') {
150 int length = snprintf(photo_directory, sizeof(photo_directory), "%s", configured);
151 return length > 0 && (size_t)length < sizeof(photo_directory) ? 0 : -1;
152 }
153
154 ssize_t length = readlink("/proc/self/exe", photo_directory, sizeof(photo_directory) - 1);
155 if (length < 0 || (size_t)length >= sizeof(photo_directory) - 1) {
156 fprintf(stderr, "LOCAL_PIPE:\tfailed to locate USHER executable: %s\n", strerror(errno));
157 return -1;
158 }
159 photo_directory[length] = '\0';
160 char *separator = strrchr(photo_directory, '/');
161 if (separator == NULL) {
162 return -1;
163 }
164 *separator = '\0';
165 size_t directory_length = (size_t)(separator - photo_directory);
166 int path_length = snprintf(photo_directory + directory_length,
167 sizeof(photo_directory) - directory_length, "/%s", configured);
168 return path_length > 0 && (size_t)path_length < sizeof(photo_directory) - directory_length ? 0 : -1;
169}
170
171int local_pipe_map_init(const char *source)
172{
173 (void)source;
174 if (prepare_photo_directory() != 0) return -1;
175 printf("LOCAL_PIPE:\tmap mock photo directory: %s\n", photo_directory);
176 return 0;
177}
178
179int local_pipe_map_shoot(char *filename, size_t filename_size, const union dc_shot_union *shot,
180 const struct camera_backend *identity, const char *cache_directory,
181 double horizontal_fov_deg, double vertical_fov_deg)
182{
183 if (filename == NULL || filename_size == 0 || shot == NULL || photo_directory[0] == '\0'
184 || !identity || !identity->prefix || !identity->mock.width || !identity->mock.height) return -1;
185 const char *marker = identity->mock.filename_marker ? identity->mock.filename_marker : "m";
186 int length = snprintf(filename, filename_size, "%s/%s%c%06d.jpg", photo_directory,
187 marker, identity->prefix, shot->data.nr);
188 if (length < 0 || (size_t)length >= filename_size) return -1;
189 return image_mock_map(filename, cache_directory, shot, identity->mock.width, identity->mock.height,
190 horizontal_fov_deg, vertical_fov_deg);
191}
192
193int local_pipe_shoot(char *filename, size_t filename_size, int image_number, const struct camera_backend *identity)
194{
195 if ((source_image[0] == '\0' && test_photo_directory[0] == '\0')
196 || filename == NULL || filename_size == 0
197 || identity == NULL || identity->prefix == '\0') {
198 return -1;
199 }
200
201 char selected_source[PATH_MAX];
202 const char *current_source = source_image;
203 if (test_photo_directory[0] != '\0') {
204 if (select_random_test_photo(selected_source, sizeof(selected_source)) != 0) {
205 return -1;
206 }
207 current_source = selected_source;
208 printf("LOCAL_PIPE:\tselected test image: %s\n", current_source);
209 }
210
211 int length = snprintf(filename, filename_size, "%s/%s%c%06d.jpg", photo_directory,
212 identity->mock.filename_marker ? identity->mock.filename_marker : "m",
213 identity->prefix, image_number);
214 if (length < 0 || (size_t)length >= filename_size) {
215 return -1;
216 }
217
218 int source_fd = open(current_source, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
219 if (source_fd < 0) {
220 fprintf(stderr, "LOCAL_PIPE:\tfailed to open mock image %s: %s\n", current_source, strerror(errno));
221 return -1;
222 }
223
224 struct stat source_status;
225 if (fstat(source_fd, &source_status) != 0 || !S_ISREG(source_status.st_mode)
226 || source_status.st_size <= 0) {
227 close(source_fd);
228 errno = EINVAL;
229 return -1;
230 }
231
232 int destination_fd = open(filename, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644);
233 if (destination_fd < 0) {
234 fprintf(stderr, "LOCAL_PIPE:\tfailed to create %s: %s\n", filename, strerror(errno));
235 const int error = errno;
236 close(source_fd);
237 errno = error;
238 return -1;
239 }
240
241 char buffer[4096];
242 /* Copy only the initially observed size and compare metadata afterward,
243 * so a growing or ordinarily modified fixture is not accepted as a shot. */
244 off_t remaining = source_status.st_size;
245 int result = 0;
246 while (remaining > 0) {
247 const size_t requested = remaining > (off_t)sizeof(buffer) ? sizeof(buffer) : (size_t)remaining;
248 const ssize_t bytes_read = read(source_fd, buffer, requested);
249 if (bytes_read < 0 && errno == EINTR) continue;
250 if (bytes_read <= 0) {
251 if (bytes_read == 0) errno = EIO;
252 result = -1;
253 break;
254 }
255 ssize_t written = 0;
256 while (written < bytes_read) {
257 ssize_t count = write(destination_fd, &buffer[written], (size_t)(bytes_read - written));
258 if (count < 0 && errno == EINTR) continue;
259 if (count <= 0) {
260 if (count == 0) errno = EIO;
261 result = -1;
262 break;
263 }
264 written += count;
265 }
266 if (result != 0) {
267 break;
268 }
269 remaining -= bytes_read;
270 }
271 struct stat completed;
272 if (result == 0 && (fstat(source_fd, &completed) != 0
273 || completed.st_size != source_status.st_size
274 || completed.st_mtim.tv_sec != source_status.st_mtim.tv_sec
275 || completed.st_mtim.tv_nsec != source_status.st_mtim.tv_nsec
276 || completed.st_ctim.tv_sec != source_status.st_ctim.tv_sec
277 || completed.st_ctim.tv_nsec != source_status.st_ctim.tv_nsec)) {
278 errno = EIO;
279 result = -1;
280 }
281
282 int error = result != 0 ? errno : 0;
283 if (close(destination_fd) != 0 && result == 0) {
284 error = errno;
285 result = -1;
286 }
287 close(source_fd);
288 if (result != 0) {
289 unlink(filename);
290 errno = error;
291 }
292 return result;
293}
294
296{
297 source_image[0] = '\0';
298 test_photo_directory[0] = '\0';
299}
300
301/** @brief Locate and count eligible fixtures beside the executable.
302 * @param[out] directory Buffer for the discovered path.
303 * @param directory_size Capacity including the terminator.
304 * @return Eligible fixture count, zero when absent or empty, or -1 on setup failure.
305 * @details Missing fixture storage is a normal case that lets the caller
306 * fall back to the bundled image. */
307static int find_test_photo_directory(char *directory, size_t directory_size)
308{
309 char executable[PATH_MAX];
310 ssize_t length = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
311 if (length < 0 || (size_t)length >= sizeof(executable) - 1) {
312 fprintf(stderr, "LOCAL_PIPE:\tfailed to locate USHER executable: %s\n", strerror(errno));
313 return -1;
314 }
315 executable[length] = '\0';
316
317 char *separator = strrchr(executable, '/');
318 if (separator == NULL) {
319 fprintf(stderr, "LOCAL_PIPE:\tUSHER executable path has no directory: %s\n", executable);
320 return -1;
321 }
322 if (separator == executable) {
323 separator[1] = '\0';
324 } else {
325 *separator = '\0';
326 }
327
328 int path_length = snprintf(directory, directory_size, "%s%stestphotos",
329 executable, separator == executable ? "" : "/");
330 if (path_length < 0 || (size_t)path_length >= directory_size) {
331 fprintf(stderr, "LOCAL_PIPE:\ttest photo directory path is too long\n");
332 return -1;
333 }
334
335 DIR *stream = opendir(directory);
336 if (stream == NULL) {
337 if (errno != ENOENT && errno != ENOTDIR) {
338 fprintf(stderr, "LOCAL_PIPE:\tfailed to open test photo directory %s: %s\n",
339 directory, strerror(errno));
340 return -1;
341 }
342 directory[0] = '\0';
343 return 0;
344 }
345
346 int count = 0;
347 struct dirent *entry;
348 while ((entry = readdir(stream)) != NULL) {
349 if (is_readable_jpg(stream, entry)) {
350 count++;
351 }
352 }
353 closedir(stream);
354 return count;
355}
356
357/** @brief Select a fixture from the current test photo directory.
358 * @param[out] path Buffer for the selected pathname.
359 * @param path_size Capacity including the terminator.
360 * @return Zero when a fixture path fits, or -1 when none can be selected.
361 * @details Count entries, choose an index, then rescan. This needs no
362 * directory-sized allocation, at the cost of a second scan that may see changes. */
363static int select_random_test_photo(char *path, size_t path_size)
364{
365 DIR *stream = opendir(test_photo_directory);
366 if (stream == NULL) {
367 fprintf(stderr, "LOCAL_PIPE:\tfailed to open test photo directory %s: %s\n",
368 test_photo_directory, strerror(errno));
369 return -1;
370 }
371
372 size_t count = 0;
373 struct dirent *entry;
374 while ((entry = readdir(stream)) != NULL) {
375 if (is_readable_jpg(stream, entry)) {
376 count++;
377 }
378 }
379 if (count == 0) {
380 fprintf(stderr, "LOCAL_PIPE:\ttest photo directory contains no readable .jpg files\n");
381 closedir(stream);
382 return -1;
383 }
384
385 size_t selected_index;
386 if (random_index(count, &selected_index) != 0) {
387 closedir(stream);
388 return -1;
389 }
390
391 rewinddir(stream);
392 size_t current_index = 0;
393 while ((entry = readdir(stream)) != NULL) {
394 if (!is_readable_jpg(stream, entry)) {
395 continue;
396 }
397 if (current_index == selected_index) {
398 int length = snprintf(path, path_size, "%s/%s", test_photo_directory, entry->d_name);
399 closedir(stream);
400 if (length < 0 || (size_t)length >= path_size) {
401 fprintf(stderr, "LOCAL_PIPE:\tselected test image path is too long\n");
402 return -1;
403 }
404 return 0;
405 }
406 current_index++;
407 }
408
409 closedir(stream);
410 fprintf(stderr, "LOCAL_PIPE:\ttest photo directory changed during image selection\n");
411 return -1;
412}
413
414/** @brief Test whether a directory entry is an eligible JPEG fixture.
415 * @param directory Open stream containing the entry.
416 * @param entry Current directory entry.
417 * @return True for a readable regular file with a case-insensitive .jpg suffix.
418 * @details Check metadata relative to the open directory without building
419 * a pathname per candidate. This is eligibility only, not JPEG validation. */
420static bool is_readable_jpg(DIR *directory, const struct dirent *entry)
421{
422 size_t name_length = strlen(entry->d_name);
423 if (name_length < 4 || strcasecmp(&entry->d_name[name_length - 4], ".jpg") != 0) {
424 return false;
425 }
426
427 struct stat status;
428 const int descriptor = dirfd(directory);
429 if (descriptor < 0) return false;
430 return fstatat(descriptor, entry->d_name, &status, 0) == 0
431 && S_ISREG(status.st_mode)
432 && faccessat(descriptor, entry->d_name, R_OK, 0) == 0;
433}
434
435/** @brief Select an index for fixture playback.
436 * @param count Nonzero number of eligible entries.
437 * @param[out] index Selected index in [0, count).
438 * @return Zero on success, or -1 if the random word cannot be read.
439 * @details Use a fixed-size random word and modulo reduction. The slight
440 * modulo bias is acceptable for fixture variety, not for statistical sampling. */
441static int random_index(size_t count, size_t *index)
442{
443 int random_fd = open("/dev/urandom", O_RDONLY);
444 if (random_fd < 0) {
445 fprintf(stderr, "LOCAL_PIPE:\tfailed to open /dev/urandom: %s\n", strerror(errno));
446 return -1;
447 }
448
449 uint64_t random_value;
450 unsigned char *bytes = (unsigned char *)&random_value;
451 size_t bytes_read = 0;
452 while (bytes_read < sizeof(random_value)) {
453 ssize_t count_read = read(random_fd, &bytes[bytes_read], sizeof(random_value) - bytes_read);
454 if (count_read < 0 && errno == EINTR) {
455 continue;
456 }
457 if (count_read <= 0) {
458 fprintf(stderr, "LOCAL_PIPE:\tfailed to read /dev/urandom: %s\n", strerror(errno));
459 close(random_fd);
460 return -1;
461 }
462 bytes_read += (size_t)count_read;
463 }
464 close(random_fd);
465
466 *index = (size_t)(random_value % (uint64_t)count);
467 return 0;
468}
static const char * photo_directory
Definition ai_cam_pipe.c:85
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
int descriptor
Definition chdk_pipe.c:142
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
static struct ear_inference_result queued_evidence completed
int image_mock_map(const char *filename, const char *cache_directory, const union dc_shot_union *shot, size_t width, size_t height, double horizontal_fov_deg, double vertical_fov_deg)
Render a map-backed simulated exposure.
Local-simulation JPEG attitude transform API.
int local_pipe_map_shoot(char *filename, size_t filename_size, const union dc_shot_union *shot, const struct camera_backend *identity, const char *cache_directory, double horizontal_fov_deg, double vertical_fov_deg)
Render a map-backed capture using the trigger's camera geometry.
Definition local_pipe.c:179
static bool is_readable_jpg(DIR *directory, const struct dirent *entry)
Test whether a directory entry is an eligible JPEG fixture.
Definition local_pipe.c:420
int local_pipe_test_init(const char *mock_image)
Initialize test mode with a supplied JPEG or a random fixture beside USHER.
Definition local_pipe.c:87
#define USHER_LOCAL_PHOTO_DIR
Definition local_pipe.c:42
static int select_random_test_photo(char *path, size_t path_size)
Select a fixture from the current test photo directory.
Definition local_pipe.c:363
void local_pipe_deinit(void)
Forget selected local fixture sources.
Definition local_pipe.c:295
static int resolve_photo_directory(void)
Resolve the configured local output path into backend storage.
Definition local_pipe.c:142
int local_pipe_shoot(char *filename, size_t filename_size, int image_number, const struct camera_backend *identity)
Create a mock capture file for a shot.
Definition local_pipe.c:193
static int random_index(size_t count, size_t *index)
Select an index for fixture playback.
Definition local_pipe.c:441
#define USHER_MOCK_IMAGE
Definition local_pipe.c:46
static char test_photo_directory[PATH_MAX]
Definition local_pipe.c:54
static char source_image[PATH_MAX]
Definition local_pipe.c:53
static int prepare_photo_directory(void)
Prepare usable output storage before accepting local shots.
Definition local_pipe.c:118
int local_pipe_map_init(const char *source)
Prepare output storage for map-backed simulation.
Definition local_pipe.c:171
static int find_test_photo_directory(char *directory, size_t directory_size)
Locate and count eligible fixtures beside the executable.
Definition local_pipe.c:307
int local_pipe_init(const char *mock_image)
Initialize a deterministic mock image source for local camera simulation.
Definition local_pipe.c:64
Local mock-camera backend API for simulator and desk testing.
uint8_t marker[LWIR_THERMAL_HEADER_BYTES+LWIR_THERMAL_CHUNK_CAPACITY]
Definition lwir_image.c:62
struct jpeg_error_mgr error
Definition lwir_image.c:60
Small path and writable-directory helpers shared by USHER backends.
static const char * usher_resolve_path(const char *path, char *buffer, size_t buffer_size)
Expand a leading home-directory shorthand without allocating memory.
Definition path_utils.h:74
#define PATH_MAX
Definition path_utils.h:39
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
Definition path_utils.h:97
static size_t count
Definition pose_log.c:79
Function table describing one backend's owned camera operations.
static const char * mock_image
Definition usher.c:562
struct usher_control_result result
Definition usher.c:377
union dc_shot_union shot
Definition usher.c:534