39#include <sys/resource.h>
44#ifndef USHER_CHDK_COMMAND
45#define USHER_CHDK_COMMAND "./bin/chdkbridge"
47#ifndef USHER_CHDK_ISO_MODE
48#define USHER_CHDK_ISO_MODE "1600"
50#ifndef USHER_CHDK_FLASH
51#define USHER_CHDK_FLASH "2"
53#ifndef USHER_CHDK_RESOLUTION
54#define USHER_CHDK_RESOLUTION "0"
56#ifndef USHER_CHDK_QUALITY
57#define USHER_CHDK_QUALITY "2"
59#ifndef USHER_CHDK_DATE_STAMP
60#define USHER_CHDK_DATE_STAMP "0"
62#ifndef USHER_CHDK_AF_ASSIST
63#define USHER_CHDK_AF_ASSIST "0"
66#define USHER_CHDK_RAW "0"
68#ifndef USHER_CHDK_FOCUS_MODE
69#define USHER_CHDK_FOCUS_MODE ""
71#ifndef USHER_CHDK_FOCUS_DISTANCE
72#define USHER_CHDK_FOCUS_DISTANCE ""
74#ifndef USHER_CHDK_FOCUS_TIMEOUT
75#define USHER_CHDK_FOCUS_TIMEOUT ""
77#ifndef USHER_CHDK_FOCUS_TOLERANCE
78#define USHER_CHDK_FOCUS_TOLERANCE ""
80#ifndef USHER_CHDK_SHUTTER
81#define USHER_CHDK_SHUTTER ""
83#ifndef USHER_CHDK_TV96
84#define USHER_CHDK_TV96 ""
86#ifndef USHER_CHDK_ISO_REAL
87#define USHER_CHDK_ISO_REAL ""
89#ifndef USHER_CHDK_ISO_MARKET
90#define USHER_CHDK_ISO_MARKET ""
92#ifndef USHER_CHDK_SV96
93#define USHER_CHDK_SV96 ""
95#ifndef USHER_CHDK_APERTURE
96#define USHER_CHDK_APERTURE ""
98#ifndef USHER_CHDK_AV96
99#define USHER_CHDK_AV96 ""
102#define USHER_CHDK_ND ""
104#ifndef USHER_CHDK_EXPOSURE_TIMEOUT
105#define USHER_CHDK_EXPOSURE_TIMEOUT ""
107#ifndef USHER_CHDK_EXPOSURE_TOLERANCE
108#define USHER_CHDK_EXPOSURE_TOLERANCE ""
110#ifndef USHER_CHDK_TIMEOUT
111#define USHER_CHDK_TIMEOUT "60"
113#ifndef USHER_CHDK_USB_TIMEOUT
114#define USHER_CHDK_USB_TIMEOUT "5000"
116#ifndef USHER_CHDK_MAX_DOWNLOAD_MIB
117#define USHER_CHDK_MAX_DOWNLOAD_MIB "64"
119#ifndef USHER_CHDK_DIRECT
120#define USHER_CHDK_DIRECT "0"
122#ifndef USHER_CHDK_KEEP_AWAKE
123#define USHER_CHDK_KEEP_AWAKE "1"
125#ifndef USHER_CHDK_PORT
126#define USHER_CHDK_PORT ""
129#ifndef USHER_CHDK_BUS
130#define USHER_CHDK_BUS ""
132#ifndef USHER_CHDK_DEVICE
133#define USHER_CHDK_DEVICE ""
156 if (received < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
camera_reply.invalid =
true;
157 for (ssize_t index = 0; index < received; ++index) {
178 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0)
return 0;
179 return (uint64_t)now.tv_sec * 1000 + (uint64_t)now.tv_nsec / 1000000;
189static unsigned long chdk_number(
const char *text,
unsigned long minimum,
unsigned long maximum)
193 unsigned long value = strtoul(text, &end, 10);
194 return errno || *text <
'0' || *text >
'9' || *end || value < minimum || value > maximum ? 0 : value;
204static int wait_camera(
int *status,
unsigned timeout_ms,
bool cancelable)
213 if (
result < 0 && errno != EINTR)
return -1;
216 if (!now || now -
started >= timeout_ms)
return 0;
217 struct timespec pause = {.tv_sec = 0, .tv_nsec = 10000000};
218 nanosleep(&pause, NULL);
236 fprintf(stderr,
"CHDK: killed controller has not exited; retaining process ownership\n");
253static int run_camera(
const char *destination, uint8_t command, uint8_t value)
269 fprintf(stderr,
"CHDK: invalid timeout, size limit or conflicting USB selection in build profile\n");
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;
279 arguments[
count++] =
"--timeout";
280 arguments[
count++] = timeout_text;
281 arguments[
count++] =
"--usb-timeout";
283 arguments[
count++] =
"--max-download-mib";
286 arguments[
count++] =
"--port";
290 arguments[
count++] =
"--bus";
292 arguments[
count++] =
"--device";
295 static const char *
const options[][2] = {
308 for (
size_t index = 0; index <
sizeof(
options) /
sizeof(
options[0]); ++index) {
310 if (!
options[index][1][0])
continue;
312 if (
count + 2 >=
sizeof(arguments) /
sizeof(arguments[0]))
return -1;
317 arguments[
count++] =
"--control-result-fd";
318 arguments[
count++] =
"3";
323 arguments[
count++] = (
char *)destination;
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;
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]);
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]);
358 if (descriptors[0] >= 0)
close(descriptors[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));
374 fprintf(stderr,
"CHDK: operation cancelled or deadline exceeded; no automatic capture retry\n");
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);
391 fprintf(stderr,
"CHDK: controller unavailable at %s: %s\n",
USHER_CHDK_COMMAND, strerror(errno));
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';
415 char directory[4096];
416 size_t separator = 0, length = 0;
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; }
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))) {
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;
437 if (removed) fprintf(stderr,
"CHDK: removed %u partial download(s) left by an interrupted controller\n", removed);
442 if (!
camera_ready || !filename || !filename[0] || filename[0] ==
'-')
return -1;
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);
452 fprintf(stderr,
"CHDK: controller did not publish an accessible JPEG: %s\n", strerror(errno));
455 unsigned char first[2], last[2];
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;
464 fprintf(stderr,
"CHDK: controller returned no valid saved JPEG\n");
466 return valid ? 0 : -1;
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
#define USHER_CHDK_ISO_REAL
static void stop_camera(void)
Stop the owned controller group within bounded grace periods.
#define USHER_CHDK_MAX_DOWNLOAD_MIB
#define USHER_CHDK_FOCUS_MODE
static bool partial_cleanup_pending
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.
static void remove_partial_outputs(const char *destination)
Delete partial outputs left by a killed or powered-off controller; none is running.
#define USHER_CHDK_QUALITY
#define USHER_CHDK_EXPOSURE_TIMEOUT
void chdk_pipe_set_running(const volatile sig_atomic_t *running)
Configure cancellation for controller operations.
#define USHER_CHDK_DATE_STAMP
int chdk_pipe_init(void)
Prepare bounded controller ownership without accessing USB.
static int run_camera(const char *destination, uint8_t command, uint8_t value)
Run one CHDKbridge capture or lens operation.
#define USHER_CHDK_ISO_MARKET
#define USHER_CHDK_COMMAND
int chdk_pipe_shoot(const char *filename)
Capture directly to a new JPEG destination without overwriting a file.
#define USHER_CHDK_USB_TIMEOUT
static bool partial_output_name(const char *name)
True for the controller's private mkstemp name, e.g. c000123.jpg.part.Ab12Cd.
#define USHER_CHDK_EXPOSURE_TOLERANCE
static int wait_camera(int *status, unsigned timeout_ms, bool cancelable)
Poll the owned controller for exit while collecting its reply.
#define USHER_CHDK_TIMEOUT
static unsigned long chdk_number(const char *text, unsigned long minimum, unsigned long maximum)
Parse a decimal build-profile limit.
#define USHER_CHDK_RESOLUTION
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
#define USHER_CHDK_APERTURE
#define USHER_CHDK_SHUTTER
#define USHER_CHDK_ISO_MODE
#define USHER_CHDK_FOCUS_DISTANCE
#define USHER_CHDK_DIRECT
#define USHER_CHDK_AF_ASSIST
static uint64_t chdk_monotonic_ms(void)
Read the clock used for controller supervision.
static void drain_control_reply(void)
Collect one chunk from the nonblocking control reply pipe.
#define USHER_CHDK_DEVICE
#define USHER_CHDK_FOCUS_TIMEOUT
void chdk_pipe_deinit(void)
Terminate and reap the owned process group using bounded grace periods.
#define USHER_CHDK_FOCUS_TOLERANCE
#define USHER_CHDK_KEEP_AWAKE
Bounded CHDKbridge subprocess lifecycle for USHER's visible-camera backend.
#define CHDK_CONTROL_REPLY_SIZE
#define CHDK_CONTROL_REPLY_TAG
void(* close)(struct usbfs_handle *)
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
static const volatile sig_atomic_t * running
struct jpeg_error_mgr error
Validated capture and reporting policy, fixed before the sample loop.
struct usher_control_result result
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_EVIDENCE_NONE
#define CAM_CONTROL_UNKNOWN
#define CAM_CONTROL_INVALID_VALUE
#define CAM_CONTROL_APPLIED