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
spotter_cam_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 spotter_cam_pipe.c
20 * @brief Native USHER adapter for on-sensor IMX500 object detection.
21 * @details The native Spottercam worker owns capture. This adapter checks
22 * its bounded object sidecar and retains the highest-confidence box for EXIF/SODA.
23 * Missing analysis does not delete a useful photograph, and a valid empty result
24 * is not a hit. Review copies happen only after USHER has tagged the original.
25 * Shared process supervision and frame timing deliberately remain separate from
26 * detector interpretation; LOOM does not use this adapter's result semantics.
27 */
28
29#include <errno.h>
30#include <fcntl.h>
31#include <limits.h>
32#include <math.h>
33#include <poll.h>
34#include <signal.h>
35#include <spawn.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/stat.h>
41#include <sys/socket.h>
42#include <sys/types.h>
43#include <sys/wait.h>
44#include <time.h>
45#include <unistd.h>
46
47#include "spotter_cam_pipe.h"
48#include "camera_backend.h"
49#include "imx500_pipe.h"
50#include "path_utils.h"
51
52#include "bounded_ini.h"
53#include "sha256fst.h"
54#include "exifite.h"
55
56/** Defaults to the same directory as plain AICam (USHER_AI_CAM_PHOTO_DIR, already
57 * defined globally via the build's CFLAGS): --spottercam is still conceptually "the
58 * AI camera", just with detection added, so it belongs in the same photo directory. */
59#ifndef USHER_SPOTTERCAM_PHOTO_DIR
60 #define USHER_SPOTTERCAM_PHOTO_DIR USHER_AI_CAM_PHOTO_DIR
61#endif
62
63/** A sidecar is generated locally; 32 KiB bounds 64 boxes with precise centres. */
64#define OBJECT_METADATA_LIMIT 32768
65/** Bound candidate records even when a producer sends a syntactically valid oversized array. */
66#define OBJECT_MAX_DETECTIONS 64
67/** Bound labels copied from model metadata into the fixed EXIF summary. */
68#define OBJECT_LABEL_SIZE 64
69/** Stream large photo copies in fixed chunks instead of allocating image-sized RAM. */
70#define OBJECT_COPY_BUFFER_SIZE (64U * 1024U)
71
72#ifndef USHER_SPOTTERCAM_COMMAND
73 /** @brief Standalone object-worker executable, never the LOOM entry point. */
74 #define USHER_SPOTTERCAM_COMMAND "./bin/spottercam"
75#endif
76#ifndef USHER_SPOTTERCAM_MODEL
77 /** @brief Native detector RPK path borrowed for the worker's lifetime. */
78 #define USHER_SPOTTERCAM_MODEL "models/ai_cam/nn_vehicles.rpk"
79#endif
80
81static struct imx500_pipe spotter_capture = IMX500_PIPE_INITIALIZER; /**< Exclusive native-detector process state. */
83static const char *spotter_settings;
85
86static const char *photo_directory = USHER_SPOTTERCAM_PHOTO_DIR; /**< Borrowed configured photo directory. */
87
88/** Validity distinguishes an empty detection result from missing/corrupt analysis. */
90 bool valid; /**< Sidecar validated against the image and schema. */
92 bool has_frame_identity; /**< Sequence/time were both present and exactly typed. */
93 /** @var object_detection_result::sequence
94 * @brief Producer camera sequence, not an FC photo number. */
95 uint64_t sequence, sensor_timestamp_ns; /**< Sensor timestamp paired with that producer sequence. */
96 int count; /**< Number of object boxes in the bounded result. */
97 double conf; /**< Confidence of the retained best box. */
98 char label[OBJECT_LABEL_SIZE]; /**< Model-provided label for the retained box. */
99 /** @var object_detection_result::box_x
100 * @brief Selected box's left edge in JPEG pixels. */
101 /** @var object_detection_result::box_y
102 * @brief Selected box's top edge in JPEG pixels. */
103 /** @var object_detection_result::box_w
104 * @brief Selected box's validated JPEG pixel width. */
105 int box_x, box_y, box_w, box_h; /**< Selected box's validated JPEG pixel height. */
107};
108static struct object_detection_result last_detection; /**< Current shot only; cleared on capture/binding failure. */
109
111{
112 spotter_capture.running = host ? host->running : NULL;
113 spotter_analysis.running = host ? host->running : NULL;
114}
115
117{
118 if (spotter_capture.pid > 0 || spotter_analysis.pid > 0) return -1;
120 return 0;
121}
122
123/** @brief Report whether capture was configured without analysis.
124 * @return True in photo-only mode.
125 * @details This is a configuration check, not a camera readiness check. */
127{
128 return spotter_photo_only;
129}
130
131/** Build the sidecar name by replacing the final `.jpg` suffix.
132 * @param filename Completed JPEG filename.
133 * @param path Caller-owned output buffer.
134 * @param path_size Output capacity.
135 * @return Zero on a bounded JPEG path, otherwise -1. */
136static int metadata_path(const char *filename, char *path, size_t path_size)
137{
138 if (!filename || !path || !path_size) return -1;
139 size_t filename_length = strnlen(filename, PATH_MAX);
140 if (filename_length < 4 || filename_length >= PATH_MAX || strcmp(filename + filename_length - 4, ".jpg") != 0) {
141 return -1;
142 }
143 int length = snprintf(path, path_size, "%.*s.objects.ini",
144 (int)(filename_length - 4), filename);
145 return length >= 0 && (size_t)length < path_size ? 0 : -1;
146}
147
157
158/** @brief Parse one bounded detector sidecar field.
159 * @param[in,out] context Pointer to the parser's detection_fields accumulator.
160 * @param key Current key from the INI reader.
161 * @param value Associated text value.
162 * @return True when the field is accepted.
163 * @details Track field presence independently of values so duplicate
164 * metadata or box fields cannot replace the first reading silently. */
165static bool detection_field(void *context, const char *key, const char *value)
166{
167 struct detection_fields *fields = context;
168 const char *const names[] = {"schema", "sequence", "sensor_timestamp_ns", "image_width", "image_height", "count"};
169 size_t field = 0;
170 while (field < 6 && strcmp(key, names[field])) ++field;
171 if (field < 6) {
172 if (fields->header_seen & (1U << field)) return false;
173 fields->header_seen |= 1U << field;
174 if (!field) return !strcmp(value, "USHER_OBJECTS_V2");
175 uint64_t parsed;
176 if (!ini_uint(value, &parsed)) return false;
177 if (field == 1) fields->sequence = parsed;
178 if (field == 2) fields->sensor_timestamp_ns = parsed;
179 if (field == 3) fields->width = parsed;
180 if (field == 4) fields->height = parsed;
181 if (field == 5) fields->count = parsed;
182 return true;
183 }
184 const char *const hashes[] = {"model_sha256", "model_descriptor_sha256", "image_sha256"};
185 for (unsigned index = 0; index < 3; ++index) {
186 if (strcmp(key, hashes[index])) continue;
187 if ((fields->hashes_seen & (1U << index)) || strlen(value) != 64 || strspn(value, "0123456789abcdef") != 64) return false;
188 fields->hashes_seen |= 1U << index;
189 char *destination = index == 0 ? fields->model_hash : index == 1 ? fields->descriptor_hash : fields->image_hash;
190 for (size_t character = 0; character < SHA256_HEX_SIZE; ++character) destination[character] = value[character];
191 return true;
192 }
193 if (strncmp(key, "detection.", 10)) return false;
194 const char *suffix = strchr(key + 10, '.');
195 if (!suffix || suffix == key + 10 || (size_t)(suffix - key - 10) >= 3) return false;
196 char index_text[3] = {0};
197 for (size_t index = 0; index < (size_t)(suffix - key - 10); ++index) index_text[index] = key[10 + index];
198 uint64_t object_index;
199 if (!ini_uint(index_text, &object_index) || object_index >= OBJECT_MAX_DETECTIONS) return false;
200 const char *const object_names[] = {"name", "confidence", "x", "y", "width", "height", "center_x", "center_y"};
201 field = 0;
202 while (field < 8 && strcmp(suffix + 1, object_names[field])) ++field;
203 if (field == 8 || (fields->seen[object_index] & (1U << field))) return false;
204 fields->seen[object_index] |= 1U << field;
205 struct object_detection_result *object = &fields->objects[object_index];
206 if (!field) {
207 const size_t length = strlen(value);
208 if (length >= sizeof(object->label)) return false;
209 for (size_t index = 0; index < length; ++index) {
210 const unsigned char character = (unsigned char)value[index];
211 if (character < 32 || character == 127 || character == ';') return false;
212 }
213 for (size_t index = 0; index <= length; ++index) object->label[index] = value[index];
214 return true;
215 }
216 if (field == 1) return ini_real(value, &object->conf) && object->conf >= 0 && object->conf <= 1;
217 if (field >= 6) return ini_real(value, field == 6 ? &object->center_x : &object->center_y);
218 uint64_t parsed;
219 if (!ini_uint(value, &parsed) || parsed > INT_MAX) return false;
220 if (field == 2) object->box_x = (int)parsed;
221 if (field == 3) object->box_y = (int)parsed;
222 if (field == 4) object->box_w = (int)parsed;
223 if (field == 5) object->box_h = (int)parsed;
224 return true;
225}
226
227/** @brief Read a stable bounded sidecar and validate schema, dimensions, and every box.
228 * @param filename Matching JPEG path.
229 * @param[out] result Cleared on any I/O, parse, or schema failure.
230 * @param[out] detections Optional expanded detection list and provenance.
231 * @return True for valid metadata, including a valid zero-detection frame.
232 * @details Size/mtime checks reject ordinary changes during the read, while
233 * caller-owned buffers bound parser storage. Exposure association is a later
234 * exact sequence/timestamp check, not a property of INI syntax alone. */
235static bool read_detection_sidecar(const char *filename, struct object_detection_result *result,
237{
238 if (!result) return false;
239 *result = (struct object_detection_result) {0};
240 if (detections) *detections = (struct spotter_detections) {0};
241 char sidecar[PATH_MAX];
242 if (metadata_path(filename, sidecar, sizeof(sidecar)) != 0) {
243 return false;
244 }
245 char input[OBJECT_METADATA_LIMIT + 1];
246 size_t length = 0;
247 bool valid = false;
248 if (!ini_read_file(sidecar, input, sizeof(input), &length)) return false;
249
250 struct detection_fields fields = {0};
251 unsigned long expected_width = strtoul(USHER_AI_CAM_WIDTH, NULL, 10);
252 unsigned long expected_height = strtoul(USHER_AI_CAM_HEIGHT, NULL, 10);
253 if (!ini_parse(input, length, detection_field, &fields)) goto done;
254 if (fields.hashes_seen) {
255 if (fields.hashes_seen != 7) goto done;
256 char digest[SHA256_HEX_SIZE];
257 if (!sha256_file(filename, digest) || strcmp(digest, fields.image_hash)) goto done;
258 FILE *jpeg = fopen(filename, "rb");
259 uint32_t width = 0, height = 0;
260 unsigned components = 0;
261 if (!jpeg) goto done;
262 const bool dimensions = exif_jpeg_dimensions(jpeg, &width, &height, &components) == 0;
263 const int closed = fclose(jpeg);
264 if (!dimensions || closed || !width || !height) goto done;
265 expected_width = width;
266 expected_height = height;
267 }
268 if (fields.header_seen != 63
269 || fields.width != expected_width || fields.height != expected_height
270 || expected_width == 0 || expected_width > UINT_MAX
271 || expected_height == 0 || expected_height > UINT_MAX
272 || fields.count > OBJECT_MAX_DETECTIONS || (detections && fields.hashes_seen != 7)) {
273 goto done;
274 }
275 *result = (struct object_detection_result) {.valid = true, .has_frame_identity = fields.sensor_timestamp_ns != 0,
276 .hash_bound = fields.hashes_seen == 7,
277 .sequence = fields.sequence, .sensor_timestamp_ns = fields.sensor_timestamp_ns, .count = (int)fields.count};
278 for (size_t index = 0; index < OBJECT_MAX_DETECTIONS; ++index) {
279 if (index >= fields.count) {
280 if (fields.seen[index]) goto done;
281 continue;
282 }
283 const struct object_detection_result *object = &fields.objects[index];
284 if ((fields.seen[index] != 63 && fields.seen[index] != 255) || object->box_w <= 0 || object->box_h <= 0
285 || (uint64_t)object->box_x + object->box_w > expected_width
286 || (uint64_t)object->box_y + object->box_h > expected_height) goto done;
287 if (fields.seen[index] == 255 && (object->center_x < object->box_x || object->center_y < object->box_y
288 || object->center_x > object->box_x + object->box_w || object->center_y > object->box_y + object->box_h
289 || object->center_x >= expected_width || object->center_y >= expected_height)) goto done;
290 if (detections) {
291 struct spotter_detection *box = &detections->objects[index];
292 for (size_t character = 0; character < sizeof(box->label); ++character) box->label[character] = object->label[character];
293 box->confidence = object->conf;
294 box->precise_center = fields.seen[index] == 255;
295 box->center_x = object->center_x; box->center_y = object->center_y;
296 box->x = (unsigned)object->box_x; box->y = (unsigned)object->box_y;
297 box->width = (unsigned)object->box_w; box->height = (unsigned)object->box_h;
298 }
299 if (!index || object->conf > result->conf) {
300 result->conf = object->conf;
301 for (size_t character = 0; character < sizeof(result->label); ++character) result->label[character] = object->label[character];
302 result->box_x = object->box_x;
303 result->box_y = object->box_y;
304 result->box_w = object->box_w;
305 result->box_h = object->box_h;
306 }
307 }
308 if (detections) {
309 detections->sequence = fields.sequence;
310 detections->width = (unsigned)fields.width;
311 detections->height = (unsigned)fields.height;
312 detections->count = (unsigned)fields.count;
313 for (size_t index = 0; index < sizeof(detections->image_sha256); ++index) {
314 detections->image_sha256[index] = fields.image_hash[index];
315 detections->model_sha256[index] = fields.model_hash[index];
316 detections->descriptor_sha256[index] = fields.descriptor_hash[index];
317 }
318 }
319 valid = true;
320done:
321 if (!valid) {
322 *result = (struct object_detection_result) {0};
323 if (detections) *detections = (struct spotter_detections) {0};
324 }
325 return valid;
326}
327
328/** @brief Read only the capture-facing detector summary.
329 * @param filename JPEG paired with the sidecar.
330 * @param[out] result Summary; cleared on read or validation failure.
331 * @return True for valid evidence, including zero detections.
332 * @details Reuse the full parser so requesting only the summary never
333 * skips box or provenance validation. */
334static bool parse_detection_sidecar(const char *filename, struct object_detection_result *result)
335{
336 return read_detection_sidecar(filename, result, NULL);
337}
338
339/** @brief Read the full detector result associated with a JPEG.
340 * @param filename JPEG paired with the sidecar.
341 * @param[out] detections Required destination for boxes and provenance.
342 * @return True for a valid sidecar, including an empty detector result.
343 * @details Use the same validation as the capture summary so downstream
344 * projection cannot admit results the capture pipeline rejects. */
346{
347 struct object_detection_result summary;
348 return detections && read_detection_sidecar(filename, &summary, detections);
349}
350
352{
353 if (last_detection.valid && last_detection.hash_bound && !last_detection.sensor_timestamp_ns)
354 return true;
355 /* A correctly shaped record may still belong to another exposure. Erase it
356 * on mismatch so later summary/copy calls cannot reuse that stale hit. */
357 if (!sensor_timestamp_ns || !last_detection.valid || !last_detection.has_frame_identity
358 || last_detection.sequence != sequence || last_detection.sensor_timestamp_ns != sensor_timestamp_ns) {
360 return false;
361 }
362 return true;
363}
364
365/* Keep the directory borrowed for backend lifetime; configuration does not
366 * acquire a sensor or copy the caller's path into hidden storage. */
367void spotter_cam_pipe_set_photo_directory(const char *directory)
368{
369 photo_directory = directory == NULL ? USHER_SPOTTERCAM_PHOTO_DIR : directory;
370}
371
372/* Preserve the distinction between failed analysis, valid empty output and a
373 * hit. All three outcomes can accompany an otherwise usable photograph. */
374void spotter_cam_pipe_last_detection_summary(char *buffer, size_t buffer_size)
375{
376 if (buffer == NULL || buffer_size == 0) return;
377 if (!last_detection.valid) {
378 snprintf(buffer, buffer_size, "status=analysis_failed; coordinates_omitted=true");
379 } else if (last_detection.count <= 0) {
380 snprintf(buffer, buffer_size, "status=ok; count=0");
381 } else {
382 snprintf(buffer, buffer_size,
383 "status=ok; count=%d; label=%s; confidence=%.4f; box=%d,%d,%d,%d",
386 }
387}
388
389/** @brief Stream a review copy without allocating another image-sized buffer.
390 * @param source_path Readable original photograph, which is never modified.
391 * @param dest_path New review destination; existing files and links are never replaced.
392 * @return Zero after writes/close succeed, otherwise -1; opened partial output is removed.
393 * @details This is a convenience copy, not the exclusive flight-image publication
394 * path. It neither fsyncs the destination nor guarantees power-loss durability. */
395static int copy_file(const char *source_path, const char *dest_path)
396{
397 if (!source_path || !dest_path || !source_path[0] || !dest_path[0]) { errno = EINVAL; return -1; }
398 int source_fd = open(source_path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
399 if (source_fd < 0) {
400 return -1;
401 }
402 struct stat source;
403 if (fstat(source_fd, &source) != 0 || !S_ISREG(source.st_mode)) {
404 close(source_fd);
405 errno = EINVAL;
406 return -1;
407 }
408 int dest_fd = open(dest_path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0644);
409 if (dest_fd < 0) {
410 const int error = errno;
411 close(source_fd);
412 errno = error;
413 return -1;
414 }
415
416 char buffer[OBJECT_COPY_BUFFER_SIZE];
417 ssize_t bytes_read;
418 int result = 0;
419 for (;;) {
420 bytes_read = read(source_fd, buffer, sizeof(buffer));
421 if (bytes_read < 0 && errno == EINTR) continue;
422 if (bytes_read <= 0) break;
423 size_t written = 0;
424 while (written < (size_t)bytes_read) {
425 ssize_t bytes_written = write(dest_fd, buffer + written, (size_t)bytes_read - written);
426 if (bytes_written <= 0) {
427 if (bytes_written < 0 && errno == EINTR) {
428 continue;
429 }
430 if (!bytes_written) errno = EIO;
431 result = -1;
432 break;
433 }
434 written += (size_t)bytes_written;
435 }
436 if (result != 0) {
437 break;
438 }
439 }
440 if (bytes_read < 0) {
441 result = -1;
442 }
443
444 int error = errno;
445 if (close(source_fd) != 0 && result == 0) { result = -1; error = errno; }
446 if (close(dest_fd) != 0) {
447 if (result == 0) error = errno;
448 result = -1;
449 }
450 if (result != 0) {
451 unlink(dest_path);
452 }
453 errno = error;
454 return result;
455}
456
457/* Copy after EXIF publication so review evidence matches the saved original.
458 * The original remains authoritative if this optional convenience copy fails. */
459void spotter_cam_pipe_save_detection_copy(const char *filename)
460{
461 if (filename == NULL || filename[0] == '\0' || !last_detection.valid || last_detection.count <= 0) {
462 return;
463 }
464
465 char resolved_dir[PATH_MAX];
466 const char *photo_dir = usher_resolve_path(photo_directory, resolved_dir, sizeof(resolved_dir));
467
468 char hits_dir[PATH_MAX];
469 int dir_len = snprintf(hits_dir, sizeof(hits_dir), "%s/detections", photo_dir);
470 if (dir_len < 0 || (size_t)dir_len >= sizeof(hits_dir) || usher_ensure_directory(hits_dir) != 0) {
471 fprintf(stderr, "SPOTTER_CAM_PIPE:\tfailed to prepare detections directory under %s\n", photo_dir);
472 return;
473 }
474
475 const char *base_name = strrchr(filename, '/');
476 base_name = base_name != NULL ? base_name + 1 : filename;
477
478 char dest_path[PATH_MAX];
479 int dest_len = snprintf(dest_path, sizeof(dest_path), "%s/%s", hits_dir, base_name);
480 if (dest_len < 0 || (size_t)dest_len >= sizeof(dest_path)) {
481 fprintf(stderr, "SPOTTER_CAM_PIPE:\tdetections path too long for %s\n", filename);
482 return;
483 }
484
485 if (copy_file(filename, dest_path) != 0) {
486 fprintf(stderr, "SPOTTER_CAM_PIPE:\tfailed to copy %s into detections directory: %s\n",
487 filename, strerror(errno));
488 } else {
489 printf("SPOTTER_CAM_PIPE:\t%s detected (count=%d, conf=%.2f); copied to %s\n",
490 last_detection.label, last_detection.count, last_detection.conf, dest_path);
491 }
492}
493
494/* Qualify the selected producer, not just process existence. The native path
495 * delegates mechanics to the neutral supervisor; object interpretation stays here. */
496int spotter_cam_pipe_init(const char *unused)
497{
498 (void)unused;
504 spotter_capture.prefix = 'a';
506}
507
508/* Capture success does not require successful inference. Inspect only this
509 * image's sidecar and keep failure explicit in the summary, not in its pixels. */
510int spotter_cam_pipe_shoot(char *filename, size_t filename_size, int image_number)
511{
513 /* Clearing before capture prevents a successful old inference from surviving
514 * a new photograph whose producer or sidecar failed. */
515 if (filename == NULL || filename_size == 0 || image_number < 0) {
516 return -1;
517 }
518
519 int status = imx500_pipe_shoot(&spotter_capture, filename, filename_size, image_number);
520 if (status != 0) {
521 filename[0] = '\0';
522 return -1;
523 }
525 fprintf(stderr, "SPOTTER_CAM_PIPE:\tmissing or invalid native detection metadata for %s\n",
526 filename);
527 }
528 return 0;
529}
530
531/** Stop the selected camera owner and release detector-specific configuration. */
538
539int spotter_cam_pipe_set_settings(const char *settings)
540{
541 if (!settings || !settings[0] || spotter_capture.pid > 0 || spotter_analysis.pid > 0) return -1;
542 spotter_settings = settings;
543 return 0;
544}
545
546int spotter_cam_pipe_analyze(const char *filename, unsigned number)
547{
549 if (!filename || !filename[0] || !number || strnlen(filename, PATH_MAX) == PATH_MAX ||
550 strchr(filename, '\n') || strchr(filename, '\r')) return -1;
551 if (!spotter_settings) return 0;
552 if (spotter_analysis.pid <= 0) {
553 spotter_analysis.command = USHER_SPOTTERCAM_COMMAND;
554 spotter_analysis.settings = spotter_settings;
555 spotter_analysis.analyze = true;
556 if (imx500_pipe_start(&spotter_analysis)) return -1;
557 }
558 if (imx500_pipe_analyze(&spotter_analysis, filename, number) ||
559 !parse_detection_sidecar(filename, &last_detection) || last_detection.sequence != number ||
560 last_detection.sensor_timestamp_ns) {
561 last_detection = (struct object_detection_result){0};
562 return -1;
563 }
564 return 0;
565}
#define USHER_AI_CAM_HEIGHT
Definition ai_cam_pipe.c:60
static const char * photo_directory
Definition ai_cam_pipe.c:85
#define USHER_AI_CAM_WIDTH
Definition ai_cam_pipe.c:57
Allocation-free flat key=value mechanics; callers own schemas and storage.
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
Definition bounded_ini.h:52
static bool ini_real(const char *text, double *number)
Parse finite decimal/exponent notation, rejecting range errors and suffixes.
Definition bounded_ini.h:67
static bool ini_read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read a nonempty regular file smaller than capacity and append NUL.
Definition bounded_ini.h:98
static bool ini_parse(char *buffer, size_t length, ini_field_handler handler, void *context)
Destructively parse flat fields with blank lines and #/; comment lines.
Shared capability and lifecycle contract for USHER camera backends.
#define USHER_SPOTTERCAM_PHOTO_DIR
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
static bool enabled(void)
Report explicit opt-in independently of process or model readiness.
Bounded C11 EXIF metadata and JPEG streaming interfaces.
int exif_jpeg_dimensions(FILE *input, uint32_t *width, uint32_t *height, unsigned *components)
Inspect the frame header without allocating or decoding image pixels.
Definition exifite.c:1309
void imx500_pipe_stop(struct imx500_pipe *pipe)
Close the channel, allow an exit grace period, then kill/reap if needed.
int imx500_pipe_shoot(struct imx500_pipe *pipe, char *filename, size_t capacity, int number)
Request one saved photograph and qualify its acknowledgement/file.
int imx500_pipe_start(struct imx500_pipe *pipe)
Replace any owned worker and require its exact READY response.
int imx500_pipe_analyze(struct imx500_pipe *pipe, const char *filename, unsigned number)
Submit one saved-picture request to a warm analysis worker without recapturing pixels.
READY/SHOT/OK process ownership without model-analysis semantics.
#define IMX500_PIPE_INITIALIZER
Safe empty lifecycle state: descriptor zero must never mean uninitialized.
Definition imx500_pipe.h:49
struct jpeg_error_mgr error
Definition lwir_image.c:60
#define USHER_SPOTTERCAM_COMMAND
static struct spotter_detections detections
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 bytes_written
Definition pose_log.c:79
bool sha256_file(const char *filename, char output[SHA256_HEX_SIZE])
Hash an immutable nonempty regular file with bounded streaming scratch.
Definition sha256fst.c:286
Allocation-free, portable SHA-256 checksums for memory and streams.
#define SHA256_HEX_SIZE
Required hexadecimal output capacity, including the trailing NUL.
Definition sha256fst.h:47
static const char * spotter_settings
static bool read_detection_sidecar(const char *filename, struct object_detection_result *result, struct spotter_detections *detections)
Read a stable bounded sidecar and validate schema, dimensions, and every box.
void spotter_cam_pipe_deinit(void)
Stop the native camera process and clear detector-specific configuration.
bool spotter_cam_pipe_photo_only(void)
Report whether capture was configured without analysis.
void spotter_cam_pipe_last_detection_summary(char *buffer, size_t buffer_size)
Format the most recent shot's detection result for EXIF writing.
void spotter_cam_pipe_save_detection_copy(const char *filename)
Copy filename into a "detections" subdirectory of the photo directory if the last spotter_cam_pipe_sh...
int spotter_cam_pipe_set_settings(const char *settings)
Select the detector assignment before either capture or saved-picture analysis starts.
static struct object_detection_result last_detection
static bool parse_detection_sidecar(const char *filename, struct object_detection_result *result)
Read only the capture-facing detector summary.
#define OBJECT_METADATA_LIMIT
void spotter_cam_pipe_configure(const struct camera_backend_host *host)
Borrow USHER's shutdown flag for both serialized native worker owners.
static bool detection_field(void *context, const char *key, const char *value)
Parse one bounded detector sidecar field.
#define OBJECT_COPY_BUFFER_SIZE
#define OBJECT_MAX_DETECTIONS
#define OBJECT_LABEL_SIZE
#define USHER_SPOTTERCAM_MODEL
Native detector RPK path borrowed for the worker's lifetime.
static bool spotter_photo_only
int spotter_cam_pipe_shoot(char *filename, size_t filename_size, int image_number)
Request one JPEG and, when enabled, parse its matching detection sidecar.
bool spotter_cam_pipe_bind_frame(uint64_t sequence, uint64_t sensor_timestamp_ns)
Reject a detection result whose identity differs from the validated saved frame.
static struct imx500_pipe spotter_analysis
bool spotter_cam_pipe_read_detections(const char *filename, struct spotter_detections *detections)
Read the full detector result associated with a JPEG.
int spotter_cam_pipe_analyze(const char *filename, unsigned number)
Analyze a saved simulation/private picture and bind results to its source hash and request.
int spotter_cam_pipe_init(const char *unused)
Start and qualify native persistent capture with the selected analysis policy.
static int metadata_path(const char *filename, char *path, size_t path_size)
static struct imx500_pipe spotter_capture
void spotter_cam_pipe_set_photo_directory(const char *directory)
Set the borrowed output directory before backend initialization.
static int copy_file(const char *source_path, const char *dest_path)
Stream a review copy without allocating another image-sized buffer.
int spotter_cam_pipe_set_photo_only(bool enabled)
Select or inspect the model-free capture mode before worker startup.
Native USHER adapter for on-sensor detection or photo-only capture.
const volatile sig_atomic_t * running
Borrowed shutdown flag; backend workers must stop when it clears.
char image_hash[SHA256_HEX_SIZE]
unsigned seen[OBJECT_MAX_DETECTIONS]
char descriptor_hash[SHA256_HEX_SIZE]
struct object_detection_result objects[OBJECT_MAX_DETECTIONS]
char model_hash[SHA256_HEX_SIZE]
One caller-serialized native worker and its borrowed configuration.
Definition imx500_pipe.h:35
uint64_t sequence
Producer camera sequence, not an FC photo number.
char label[OBJECT_LABEL_SIZE]
int box_w
Selected box's validated JPEG pixel width.
int box_y
Selected box's top edge in JPEG pixels.
int box_x
Selected box's left edge in JPEG pixels.
char label[SPOTTER_LABEL_CAPACITY]
bool valid
Definition usher.c:537
struct usher_control_result result
Definition usher.c:377