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
chdk_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 chdk_pipe.c
20 * @brief Own and supervise bounded CHDKbridge child invocations for USHER.
21 * @details The child is launched directly, not through a shell, and remains
22 * owned until reaped. Deadlines and process-group shutdown are required because
23 * USB work can outlive a single caller; an ambiguous exposure is never replayed.
24 */
25#include "chdk_pipe.h"
26#include "camera_capture.h"
27#include "usher_protocol.h"
28#include <errno.h>
29#include <dirent.h>
30#include <fcntl.h>
31#include <signal.h>
32#include <spawn.h>
33#include <stdbool.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/stat.h>
39#include <sys/resource.h>
40#include <sys/wait.h>
41#include <time.h>
42#include <unistd.h>
43
44#ifndef USHER_CHDK_COMMAND
45#define USHER_CHDK_COMMAND "./bin/chdkbridge"
46#endif
47#ifndef USHER_CHDK_ISO_MODE
48#define USHER_CHDK_ISO_MODE "1600"
49#endif
50#ifndef USHER_CHDK_FLASH
51#define USHER_CHDK_FLASH "2"
52#endif
53#ifndef USHER_CHDK_RESOLUTION
54#define USHER_CHDK_RESOLUTION "0"
55#endif
56#ifndef USHER_CHDK_QUALITY
57#define USHER_CHDK_QUALITY "2"
58#endif
59#ifndef USHER_CHDK_DATE_STAMP
60#define USHER_CHDK_DATE_STAMP "0"
61#endif
62#ifndef USHER_CHDK_AF_ASSIST
63#define USHER_CHDK_AF_ASSIST "0"
64#endif
65#ifndef USHER_CHDK_RAW
66#define USHER_CHDK_RAW "0"
67#endif
68#ifndef USHER_CHDK_FOCUS_MODE
69#define USHER_CHDK_FOCUS_MODE ""
70#endif
71#ifndef USHER_CHDK_FOCUS_DISTANCE
72#define USHER_CHDK_FOCUS_DISTANCE ""
73#endif
74#ifndef USHER_CHDK_FOCUS_TIMEOUT
75#define USHER_CHDK_FOCUS_TIMEOUT ""
76#endif
77#ifndef USHER_CHDK_FOCUS_TOLERANCE
78#define USHER_CHDK_FOCUS_TOLERANCE ""
79#endif
80#ifndef USHER_CHDK_SHUTTER
81#define USHER_CHDK_SHUTTER ""
82#endif
83#ifndef USHER_CHDK_TV96
84#define USHER_CHDK_TV96 ""
85#endif
86#ifndef USHER_CHDK_ISO_REAL
87#define USHER_CHDK_ISO_REAL ""
88#endif
89#ifndef USHER_CHDK_ISO_MARKET
90#define USHER_CHDK_ISO_MARKET ""
91#endif
92#ifndef USHER_CHDK_SV96
93#define USHER_CHDK_SV96 ""
94#endif
95#ifndef USHER_CHDK_APERTURE
96#define USHER_CHDK_APERTURE ""
97#endif
98#ifndef USHER_CHDK_AV96
99#define USHER_CHDK_AV96 ""
100#endif
101#ifndef USHER_CHDK_ND
102#define USHER_CHDK_ND ""
103#endif
104#ifndef USHER_CHDK_EXPOSURE_TIMEOUT
105#define USHER_CHDK_EXPOSURE_TIMEOUT ""
106#endif
107#ifndef USHER_CHDK_EXPOSURE_TOLERANCE
108#define USHER_CHDK_EXPOSURE_TOLERANCE ""
109#endif
110#ifndef USHER_CHDK_TIMEOUT
111#define USHER_CHDK_TIMEOUT "60"
112#endif
113#ifndef USHER_CHDK_USB_TIMEOUT
114#define USHER_CHDK_USB_TIMEOUT "5000"
115#endif
116#ifndef USHER_CHDK_MAX_DOWNLOAD_MIB
117#define USHER_CHDK_MAX_DOWNLOAD_MIB "64"
118#endif
119#ifndef USHER_CHDK_DIRECT
120#define USHER_CHDK_DIRECT "0"
121#endif
122#ifndef USHER_CHDK_KEEP_AWAKE
123#define USHER_CHDK_KEEP_AWAKE "1"
124#endif
125#ifndef USHER_CHDK_PORT
126#define USHER_CHDK_PORT ""
127#endif
128
129#ifndef USHER_CHDK_BUS
130#define USHER_CHDK_BUS ""
131#endif
132#ifndef USHER_CHDK_DEVICE
133#define USHER_CHDK_DEVICE ""
134#endif
135
136extern char **environ;
137static pid_t camera_pid = -1;
138static bool camera_ready;
139static bool partial_cleanup_pending = true;
140static const volatile sig_atomic_t *camera_running;
141static struct {
143 size_t size;
146} camera_reply = {.descriptor = -1};
147
148/** @brief Collect one chunk from the nonblocking control reply pipe.
149 * @details Retain partial bytes across polls; flag overflow and hard I/O
150 * errors so a truncated or overlong reply cannot count as confirmation. */
151static void drain_control_reply(void)
152{
153 if (camera_reply.descriptor < 0) return;
154 uint8_t bytes[32];
155 ssize_t received = read(camera_reply.descriptor, bytes, sizeof(bytes));
156 if (received < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) camera_reply.invalid = true;
157 for (ssize_t index = 0; index < received; ++index) {
158 if (camera_reply.size < sizeof(camera_reply.bytes)) camera_reply.bytes[camera_reply.size++] = bytes[index];
159 else camera_reply.invalid = true;
160 }
161}
162
163/** @brief Configure cancellation for controller operations.
164 * @param running Borrowed service flag, or NULL to disable flag-based cancellation.
165 * @details Keep the flag alive for the lifetime of controller work; cleanup
166 * waits must still reap a child after cancellation. */
167void chdk_pipe_set_running(const volatile sig_atomic_t *running)
168{
170}
171
172/** @brief Read the clock used for controller supervision.
173 * @return Monotonic milliseconds, or zero on clock failure.
174 * @details Deadlines must not move when the wall clock is corrected. */
175static uint64_t chdk_monotonic_ms(void)
176{
177 struct timespec now;
178 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) return 0;
179 return (uint64_t)now.tv_sec * 1000 + (uint64_t)now.tv_nsec / 1000000;
180}
181
182/** @brief Parse a decimal build-profile limit.
183 * @param text Null-terminated numeric setting.
184 * @param minimum Inclusive lower bound.
185 * @param maximum Inclusive upper bound.
186 * @return Parsed value, or zero on syntax or range error.
187 * @details Limit callers use positive ranges so zero is an unambiguous
188 * failure sentinel. Reject signs and suffixes rather than accepting a prefix. */
189static unsigned long chdk_number(const char *text, unsigned long minimum, unsigned long maximum)
190{
191 char *end;
192 errno = 0;
193 unsigned long value = strtoul(text, &end, 10);
194 return errno || *text < '0' || *text > '9' || *end || value < minimum || value > maximum ? 0 : value;
195}
196
197/** @brief Poll the owned controller for exit while collecting its reply.
198 * @param[out] status Wait status; valid when the result is one.
199 * @param timeout_ms Polling budget; zero checks without sleeping.
200 * @param cancelable True to honor the service cancellation flag.
201 * @return One if reaped, zero on timeout or cancellation, or -1 on wait/setup error.
202 * @details Drain during waits and again on exit; the last reply can arrive
203 * between the initial read and the successful waitpid. */
204static int wait_camera(int *status, unsigned timeout_ms, bool cancelable)
205{
206 const uint64_t started = chdk_monotonic_ms();
207 if (!started) return -1;
208 for (;;) {
210 pid_t result = waitpid(camera_pid, status, WNOHANG);
211 if (result == camera_pid) { drain_control_reply(); camera_pid = -1; return 1; }
212 if (result < 0 && errno == ECHILD) { camera_pid = -1; return -1; }
213 if (result < 0 && errno != EINTR) return -1;
214 if (cancelable && !capture_running(camera_running)) return 0;
215 uint64_t now = chdk_monotonic_ms();
216 if (!now || now - started >= timeout_ms) return 0;
217 struct timespec pause = {.tv_sec = 0, .tv_nsec = 10000000};
218 nanosleep(&pause, NULL);
219 }
220}
221
222/** @brief Stop the owned controller group within bounded grace periods.
223 * @details Allow graceful PTP cleanup before forced termination. Preserve an
224 * unreaped PID so future commands cannot acquire the camera concurrently. */
225static void stop_camera(void)
226{
227 if (camera_pid <= 0) return;
228 int status = 0;
229 if (wait_camera(&status, 0, false) == 1 || camera_pid <= 0) return;
230 kill(-camera_pid, SIGTERM);
231 /* The controller closes its PTP session on SIGTERM; SIGKILL would leave camera state behind. */
232 if (wait_camera(&status, 3000, false) == 1 || camera_pid <= 0) return;
233 kill(-camera_pid, SIGKILL);
235 if (wait_camera(&status, 1500, false) != 1 && camera_pid > 0)
236 fprintf(stderr, "CHDK: killed controller has not exited; retaining process ownership\n");
237}
238
240{
241 camera_ready = false;
242 stop_camera();
243}
244
245/** @brief Run one CHDKbridge capture or lens operation.
246 * @param destination Private capture path; unused for a control operation.
247 * @param command Zero for capture, otherwise the validated control command.
248 * @param value Control operand, unused for capture and lens retraction.
249 * @return Zero for successful child execution, or -1 on setup, cancellation or child failure.
250 * @details Validate build-profile limits and acquire exclusive child
251 * ownership before spawning. Lens operations use a distinct reply pipe;
252 * child exit alone does not establish that the camera applied the command. */
253static int run_camera(const char *destination, uint8_t command, uint8_t value)
254{
255 if ((!destination && !command) || !capture_running(camera_running)) return -1;
256 if (camera_pid > 0) {
257 int status = 0;
258 wait_camera(&status, 0, false);
259 if (camera_pid > 0) return -1;
260 }
261 unsigned long timeout = chdk_number(USHER_CHDK_TIMEOUT, 1, 3600);
262 if (!timeout || !chdk_number(USHER_CHDK_USB_TIMEOUT, 50, 10000) ||
264 (USHER_CHDK_DIRECT[0] != '0' && USHER_CHDK_DIRECT[0] != '1') || USHER_CHDK_DIRECT[1] ||
265 (USHER_CHDK_KEEP_AWAKE[0] && ((USHER_CHDK_KEEP_AWAKE[0] != '0' && USHER_CHDK_KEEP_AWAKE[0] != '1') ||
268 ((USHER_CHDK_BUS[0] == '\0') != (USHER_CHDK_DEVICE[0] == '\0'))) {
269 fprintf(stderr, "CHDK: invalid timeout, size limit or conflicting USB selection in build profile\n");
270 return -1;
271 }
272 char *arguments[64];
273 char timeout_text[12], value_text[4];
274 if (command && timeout > 60) timeout = 60;
275 if (snprintf(timeout_text, sizeof(timeout_text), "%lu", timeout) < 1
276 || snprintf(value_text, sizeof(value_text), "%u", value) < 1) return -1;
277 size_t count = 0;
278 arguments[count++] = (char *)USHER_CHDK_COMMAND;
279 arguments[count++] = "--timeout";
280 arguments[count++] = timeout_text;
281 arguments[count++] = "--usb-timeout";
282 arguments[count++] = (char *)USHER_CHDK_USB_TIMEOUT;
283 arguments[count++] = "--max-download-mib";
284 arguments[count++] = (char *)USHER_CHDK_MAX_DOWNLOAD_MIB;
285 if (USHER_CHDK_PORT[0]) {
286 arguments[count++] = "--port";
287 arguments[count++] = (char *)USHER_CHDK_PORT;
288 }
289 if (USHER_CHDK_BUS[0]) {
290 arguments[count++] = "--bus";
291 arguments[count++] = (char *)USHER_CHDK_BUS;
292 arguments[count++] = "--device";
293 arguments[count++] = (char *)USHER_CHDK_DEVICE;
294 }
295 static const char *const options[][2] = {
296 {"--iso-mode", USHER_CHDK_ISO_MODE}, {"--flash", USHER_CHDK_FLASH},
297 {"--resolution", USHER_CHDK_RESOLUTION}, {"--quality", USHER_CHDK_QUALITY},
298 {"--date-stamp", USHER_CHDK_DATE_STAMP}, {"--af-assist", USHER_CHDK_AF_ASSIST},
299 {"--raw", USHER_CHDK_RAW},
300 {"--focus-mode", USHER_CHDK_FOCUS_MODE}, {"--focus-distance", USHER_CHDK_FOCUS_DISTANCE},
301 {"--focus-timeout", USHER_CHDK_FOCUS_TIMEOUT}, {"--focus-tolerance", USHER_CHDK_FOCUS_TOLERANCE},
302 {"--shutter", USHER_CHDK_SHUTTER}, {"--tv96", USHER_CHDK_TV96},
303 {"--iso-real", USHER_CHDK_ISO_REAL}, {"--iso-market", USHER_CHDK_ISO_MARKET}, {"--sv96", USHER_CHDK_SV96},
304 {"--aperture", USHER_CHDK_APERTURE}, {"--av96", USHER_CHDK_AV96}, {"--nd", USHER_CHDK_ND},
305 {"--exposure-timeout", USHER_CHDK_EXPOSURE_TIMEOUT}, {"--exposure-tolerance", USHER_CHDK_EXPOSURE_TOLERANCE},
306 {"--keep-awake", USHER_CHDK_KEEP_AWAKE}
307 };
308 for (size_t index = 0; index < sizeof(options) / sizeof(options[0]); ++index) {
309 if (command) break;
310 if (!options[index][1][0]) continue;
311 if (index == 0 && (USHER_CHDK_ISO_REAL[0] || USHER_CHDK_ISO_MARKET[0] || USHER_CHDK_SV96[0])) continue;
312 if (count + 2 >= sizeof(arguments) / sizeof(arguments[0])) return -1;
313 arguments[count++] = (char *)options[index][0];
314 arguments[count++] = (char *)options[index][1];
315 }
316 if (command) {
317 arguments[count++] = "--control-result-fd";
318 arguments[count++] = "3";
319 arguments[count++] = command == CAM_CMD_ZOOM_ABSOLUTE ? "zoom" : "--retract-lens";
320 if (command == CAM_CMD_ZOOM_ABSOLUTE) arguments[count++] = value_text;
321 } else {
322 if (USHER_CHDK_DIRECT[0] == '1') arguments[count++] = "--direct-download";
323 arguments[count++] = (char *)destination;
324 }
325 arguments[count] = NULL;
326 posix_spawnattr_t attributes;
327 int error = posix_spawnattr_init(&attributes);
328 if (error) return -1;
329 sigset_t mask, defaults;
330 sigemptyset(&mask);
331 sigemptyset(&defaults);
332 sigaddset(&defaults, SIGINT);
333 sigaddset(&defaults, SIGTERM);
334 error = posix_spawnattr_setsigmask(&attributes, &mask);
335 if (!error) error = posix_spawnattr_setsigdefault(&attributes, &defaults);
336 if (!error) error = posix_spawnattr_setpgroup(&attributes, 0);
337 if (!error) error = posix_spawnattr_setflags(&attributes,
338 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETPGROUP);
339 posix_spawn_file_actions_t actions;
340 bool actions_ready = false;
341 int descriptors[2] = {-1, -1};
342 if (!error && command) {
343 if (pipe(descriptors) != 0) error = errno;
344 if (!error && (fcntl(descriptors[0], F_SETFD, FD_CLOEXEC) < 0
345 || fcntl(descriptors[1], F_SETFD, FD_CLOEXEC) < 0
346 || fcntl(descriptors[0], F_SETFL, O_NONBLOCK) < 0)) error = errno;
347 if (!error) { error = posix_spawn_file_actions_init(&actions); actions_ready = error == 0; }
348 if (!error) error = posix_spawn_file_actions_adddup2(&actions, descriptors[1], 3);
349 if (!error && descriptors[0] != 3) error = posix_spawn_file_actions_addclose(&actions, descriptors[0]);
350 if (!error && descriptors[1] != 3) error = posix_spawn_file_actions_addclose(&actions, descriptors[1]);
351 }
352 if (!error) error = posix_spawn(&camera_pid, USHER_CHDK_COMMAND, actions_ready ? &actions : NULL,
353 &attributes, arguments, environ);
354 if (actions_ready) posix_spawn_file_actions_destroy(&actions);
355 posix_spawnattr_destroy(&attributes);
356 if (descriptors[1] >= 0) close(descriptors[1]);
357 if (error) {
358 if (descriptors[0] >= 0) close(descriptors[0]);
359 camera_pid = -1;
360 fprintf(stderr, "CHDK: cannot start %s: %s\n", USHER_CHDK_COMMAND, strerror(error));
361 return -1;
362 }
363 camera_reply.descriptor = descriptors[0];
364 camera_reply.size = 0;
365 camera_reply.invalid = false;
366 errno = 0;
367 if (setpriority(PRIO_PROCESS, (id_t)camera_pid, 10) != 0 && errno != ESRCH)
368 fprintf(stderr, "CHDK: cannot lower controller scheduling priority: %s\n", strerror(errno));
369 int status = 0;
370 int result = wait_camera(&status, (unsigned)timeout * 1000 + 2000, true);
371 if (camera_reply.descriptor >= 0) close(camera_reply.descriptor);
372 camera_reply.descriptor = -1;
373 if (result != 1) {
374 fprintf(stderr, "CHDK: operation cancelled or deadline exceeded; no automatic capture retry\n");
375 stop_camera();
376 return -1;
377 }
378 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
379 if (WIFSIGNALED(status)) fprintf(stderr, "CHDK: controller stopped by signal %d; no capture replay\n", WTERMSIG(status));
380 else fprintf(stderr, "CHDK: controller exited with code %d; camera state may have changed\n", WIFEXITED(status) ? WEXITSTATUS(status) : -1);
381 return -1;
382 }
383 return 0;
384}
385
387{
389 if (camera_pid > 0 || !capture_running(camera_running)) return -1;
390 if (access(USHER_CHDK_COMMAND, X_OK) != 0) {
391 fprintf(stderr, "CHDK: controller unavailable at %s: %s\n", USHER_CHDK_COMMAND, strerror(errno));
392 return -1;
393 }
394 camera_ready = true;
395 return 0;
396}
397
398/** @brief True for the controller's private mkstemp name, e.g. c000123.jpg.part.Ab12Cd. */
399static bool partial_output_name(const char *name)
400{
401 static const char middle[] = ".jpg.part.";
402 if (name[0] != 'c') return false;
403 for (size_t index = 1; index <= 6; ++index)
404 if (name[index] < '0' || name[index] > '9') return false;
405 for (size_t index = 0; index < sizeof(middle) - 1; ++index)
406 if (name[7 + index] != middle[index]) return false;
407 for (size_t index = 17; index < 23; ++index)
408 if (!name[index] || name[index] == '/') return false;
409 return name[23] == '\0';
410}
411
412/** @brief Delete partial outputs left by a killed or powered-off controller; none is running. */
413static void remove_partial_outputs(const char *destination)
414{
415 char directory[4096];
416 size_t separator = 0, length = 0;
417 bool found = false;
418 for (; destination[length]; ++length) {
419 if (length + 1 >= sizeof(directory)) return;
420 directory[length] = destination[length];
421 if (destination[length] == '/') { separator = length; found = true; }
422 }
423 if (found) directory[separator ? separator : 1] = '\0';
424 else { directory[0] = '.'; directory[1] = '\0'; }
425 DIR *listing = opendir(directory);
426 if (!listing) return;
427 unsigned removed = 0;
428 struct dirent *entry;
429 while ((entry = readdir(listing))) {
430 struct stat status;
431 if (!partial_output_name(entry->d_name) ||
432 fstatat(dirfd(listing), entry->d_name, &status, AT_SYMLINK_NOFOLLOW) != 0 ||
433 !S_ISREG(status.st_mode) || unlinkat(dirfd(listing), entry->d_name, 0) != 0) continue;
434 ++removed;
435 }
436 closedir(listing);
437 if (removed) fprintf(stderr, "CHDK: removed %u partial download(s) left by an interrupted controller\n", removed);
438}
439
440int chdk_pipe_shoot(const char *filename)
441{
442 if (!camera_ready || !filename || !filename[0] || filename[0] == '-') return -1;
444 remove_partial_outputs(filename);
446 }
447 struct stat status;
448 if (lstat(filename, &status) == 0 || errno != ENOENT) return -1;
449 if (run_camera(filename, 0, 0) != 0) return -1;
450 int descriptor = open(filename, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
451 if (descriptor < 0) {
452 fprintf(stderr, "CHDK: controller did not publish an accessible JPEG: %s\n", strerror(errno));
453 return -1;
454 }
455 unsigned char first[2], last[2];
456 uint64_t size_limit = chdk_number(USHER_CHDK_MAX_DOWNLOAD_MIB, 1, 256) * UINT64_C(1048576);
457 bool valid = size_limit && fstat(descriptor, &status) == 0 && S_ISREG(status.st_mode) && status.st_size >= 4 &&
458 (uint64_t)status.st_size <= size_limit &&
459 pread(descriptor, first, sizeof(first), 0) == (ssize_t)sizeof(first) &&
460 pread(descriptor, last, sizeof(last), status.st_size - 2) == (ssize_t)sizeof(last) &&
461 first[0] == 0xff && first[1] == 0xd8 && last[0] == 0xff && last[1] == 0xd9;
462 if (close(descriptor) != 0) valid = false;
463 if (!valid) {
464 fprintf(stderr, "CHDK: controller returned no valid saved JPEG\n");
465 }
466 return valid ? 0 : -1;
467}
468
469/** @brief Apply one supported lens command and qualify device evidence.
470 * @param command Absolute zoom or lens retraction.
471 * @param value Requested zoom value; must be zero for retraction.
472 * @param[out] observed Returned device value; meaningful with applied status.
473 * @param[out] evidence Returned evidence kind; meaningful with applied status.
474 * @return A CAM_CONTROL status; UNKNOWN means execution could not be confirmed.
475 * @details Check both the reply shape and echoed command. Do not retry
476 * an ambiguous operation or infer success from the controller's exit alone. */
477uint8_t chdk_pipe_control(uint8_t command, uint8_t value, uint8_t *observed, uint8_t *evidence)
478{
479 if (command != CAM_CMD_ZOOM_ABSOLUTE && command != CAM_CMD_LENS_RETRACT) return CAM_CONTROL_UNSUPPORTED;
480 if (command == CAM_CMD_LENS_RETRACT && value) return CAM_CONTROL_INVALID_VALUE;
481 if (!observed || !evidence || !camera_ready || !capture_running(camera_running)) return CAM_CONTROL_FAILED;
482 if (camera_pid > 0) {
483 int status = 0;
484 wait_camera(&status, 0, false);
485 if (camera_pid > 0) return CAM_CONTROL_BUSY;
486 }
487 *observed = 0;
489 if (run_camera(NULL, command, value) != 0 || camera_reply.invalid
491 || camera_reply.bytes[1] != command
493 || (command == CAM_CMD_LENS_RETRACT && camera_reply.bytes[2])) return CAM_CONTROL_UNKNOWN;
494 *observed = camera_reply.bytes[2];
495 *evidence = camera_reply.bytes[3];
496 return CAM_CONTROL_APPLIED;
497}
char ** environ
Optional split-phase capture contract for USHER image backends.
static bool capture_running(const volatile sig_atomic_t *running)
Atomically poll a borrowed cancellation flag; NULL means no cancellation.
static struct @145320073173174126233360327220100135011156163126 camera_reply
static const volatile sig_atomic_t * camera_running
Definition chdk_pipe.c:140
#define USHER_CHDK_ISO_REAL
Definition chdk_pipe.c:87
static void stop_camera(void)
Stop the owned controller group within bounded grace periods.
Definition chdk_pipe.c:225
#define USHER_CHDK_MAX_DOWNLOAD_MIB
Definition chdk_pipe.c:117
#define USHER_CHDK_FOCUS_MODE
Definition chdk_pipe.c:69
static bool partial_cleanup_pending
Definition chdk_pipe.c:139
uint8_t chdk_pipe_control(uint8_t command, uint8_t value, uint8_t *observed, uint8_t *evidence)
Apply one supported lens command and qualify device evidence.
Definition chdk_pipe.c:477
static pid_t camera_pid
Definition chdk_pipe.c:137
static void remove_partial_outputs(const char *destination)
Delete partial outputs left by a killed or powered-off controller; none is running.
Definition chdk_pipe.c:413
#define USHER_CHDK_QUALITY
Definition chdk_pipe.c:57
bool invalid
Definition chdk_pipe.c:145
#define USHER_CHDK_EXPOSURE_TIMEOUT
Definition chdk_pipe.c:105
static bool camera_ready
Definition chdk_pipe.c:138
void chdk_pipe_set_running(const volatile sig_atomic_t *running)
Configure cancellation for controller operations.
Definition chdk_pipe.c:167
#define USHER_CHDK_DATE_STAMP
Definition chdk_pipe.c:60
int chdk_pipe_init(void)
Prepare bounded controller ownership without accessing USB.
Definition chdk_pipe.c:386
#define USHER_CHDK_SV96
Definition chdk_pipe.c:93
#define USHER_CHDK_AV96
Definition chdk_pipe.c:99
static int run_camera(const char *destination, uint8_t command, uint8_t value)
Run one CHDKbridge capture or lens operation.
Definition chdk_pipe.c:253
#define USHER_CHDK_ISO_MARKET
Definition chdk_pipe.c:90
#define USHER_CHDK_RAW
Definition chdk_pipe.c:66
#define USHER_CHDK_PORT
Definition chdk_pipe.c:126
#define USHER_CHDK_COMMAND
Definition chdk_pipe.c:45
int chdk_pipe_shoot(const char *filename)
Capture directly to a new JPEG destination without overwriting a file.
Definition chdk_pipe.c:440
#define USHER_CHDK_USB_TIMEOUT
Definition chdk_pipe.c:114
static bool partial_output_name(const char *name)
True for the controller's private mkstemp name, e.g. c000123.jpg.part.Ab12Cd.
Definition chdk_pipe.c:399
size_t size
Definition chdk_pipe.c:143
#define USHER_CHDK_FLASH
Definition chdk_pipe.c:51
#define USHER_CHDK_EXPOSURE_TOLERANCE
Definition chdk_pipe.c:108
static int wait_camera(int *status, unsigned timeout_ms, bool cancelable)
Poll the owned controller for exit while collecting its reply.
Definition chdk_pipe.c:204
#define USHER_CHDK_TIMEOUT
Definition chdk_pipe.c:111
static unsigned long chdk_number(const char *text, unsigned long minimum, unsigned long maximum)
Parse a decimal build-profile limit.
Definition chdk_pipe.c:189
#define USHER_CHDK_TV96
Definition chdk_pipe.c:84
#define USHER_CHDK_RESOLUTION
Definition chdk_pipe.c:54
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
#define USHER_CHDK_APERTURE
Definition chdk_pipe.c:96
#define USHER_CHDK_ND
Definition chdk_pipe.c:102
#define USHER_CHDK_SHUTTER
Definition chdk_pipe.c:81
#define USHER_CHDK_ISO_MODE
Definition chdk_pipe.c:48
#define USHER_CHDK_FOCUS_DISTANCE
Definition chdk_pipe.c:72
#define USHER_CHDK_DIRECT
Definition chdk_pipe.c:120
#define USHER_CHDK_AF_ASSIST
Definition chdk_pipe.c:63
static uint64_t chdk_monotonic_ms(void)
Read the clock used for controller supervision.
Definition chdk_pipe.c:175
static void drain_control_reply(void)
Collect one chunk from the nonblocking control reply pipe.
Definition chdk_pipe.c:151
#define USHER_CHDK_DEVICE
Definition chdk_pipe.c:133
#define USHER_CHDK_FOCUS_TIMEOUT
Definition chdk_pipe.c:75
#define USHER_CHDK_BUS
Definition chdk_pipe.c:130
void chdk_pipe_deinit(void)
Terminate and reap the owned process group using bounded grace periods.
Definition chdk_pipe.c:239
int descriptor
Definition chdk_pipe.c:142
#define USHER_CHDK_FOCUS_TOLERANCE
Definition chdk_pipe.c:78
#define USHER_CHDK_KEEP_AWAKE
Definition chdk_pipe.c:123
Bounded CHDKbridge subprocess lifecycle for USHER's visible-camera backend.
#define CHDK_CONTROL_REPLY_SIZE
Definition chdk_pipe.h:27
#define CHDK_CONTROL_REPLY_TAG
Definition chdk_pipe.h:28
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
static bool started
static const volatile sig_atomic_t * running
char evidence[8192]
Definition lwir_image.c:66
struct jpeg_error_mgr error
Definition lwir_image.c:60
static size_t count
Definition pose_log.c:79
Validated capture and reporting policy, fixed before the sample loop.
Definition earcam.c:137
bool valid
Definition usher.c:537
struct usher_control_result result
Definition usher.c:377
Shared FC/MORA byte-stream UART framing, messages and payload layouts.
#define CAM_CONTROL_EVIDENCE_VALUE
#define CAM_CMD_ZOOM_ABSOLUTE
#define CAM_CONTROL_UNSUPPORTED
#define CAM_CONTROL_EVIDENCE_PLAYBACK
#define CAM_CONTROL_FAILED
#define CAM_CMD_LENS_RETRACT
#define CAM_CONTROL_BUSY
#define CAM_CONTROL_EVIDENCE_NONE
#define CAM_CONTROL_UNKNOWN
#define CAM_CONTROL_INVALID_VALUE
#define CAM_CONTROL_APPLIED