Own and supervise bounded CHDKbridge child invocations for USHER. More...
#include "chdk_pipe.h"#include "camera_capture.h"#include "usher_protocol.h"#include <errno.h>#include <dirent.h>#include <fcntl.h>#include <signal.h>#include <spawn.h>#include <stdbool.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <sys/resource.h>#include <sys/wait.h>#include <time.h>#include <unistd.h>
Go to the source code of this file.
Macros | |
| #define | USHER_CHDK_COMMAND "./bin/chdkbridge" |
| #define | USHER_CHDK_ISO_MODE "1600" |
| #define | USHER_CHDK_FLASH "2" |
| #define | USHER_CHDK_RESOLUTION "0" |
| #define | USHER_CHDK_QUALITY "2" |
| #define | USHER_CHDK_DATE_STAMP "0" |
| #define | USHER_CHDK_AF_ASSIST "0" |
| #define | USHER_CHDK_RAW "0" |
| #define | USHER_CHDK_FOCUS_MODE "" |
| #define | USHER_CHDK_FOCUS_DISTANCE "" |
| #define | USHER_CHDK_FOCUS_TIMEOUT "" |
| #define | USHER_CHDK_FOCUS_TOLERANCE "" |
| #define | USHER_CHDK_SHUTTER "" |
| #define | USHER_CHDK_TV96 "" |
| #define | USHER_CHDK_ISO_REAL "" |
| #define | USHER_CHDK_ISO_MARKET "" |
| #define | USHER_CHDK_SV96 "" |
| #define | USHER_CHDK_APERTURE "" |
| #define | USHER_CHDK_AV96 "" |
| #define | USHER_CHDK_ND "" |
| #define | USHER_CHDK_EXPOSURE_TIMEOUT "" |
| #define | USHER_CHDK_EXPOSURE_TOLERANCE "" |
| #define | USHER_CHDK_TIMEOUT "60" |
| #define | USHER_CHDK_USB_TIMEOUT "5000" |
| #define | USHER_CHDK_MAX_DOWNLOAD_MIB "64" |
| #define | USHER_CHDK_DIRECT "0" |
| #define | USHER_CHDK_KEEP_AWAKE "1" |
| #define | USHER_CHDK_PORT "" |
| #define | USHER_CHDK_BUS "" |
| #define | USHER_CHDK_DEVICE "" |
Functions | |
| static void | drain_control_reply (void) |
| Collect one chunk from the nonblocking control reply pipe. | |
| void | chdk_pipe_set_running (const volatile sig_atomic_t *running) |
| Configure cancellation for controller operations. | |
| static uint64_t | chdk_monotonic_ms (void) |
| Read the clock used for controller supervision. | |
| static unsigned long | chdk_number (const char *text, unsigned long minimum, unsigned long maximum) |
| Parse a decimal build-profile limit. | |
| static int | wait_camera (int *status, unsigned timeout_ms, bool cancelable) |
| Poll the owned controller for exit while collecting its reply. | |
| static void | stop_camera (void) |
| Stop the owned controller group within bounded grace periods. | |
| void | chdk_pipe_deinit (void) |
| Terminate and reap the owned process group using bounded grace periods. | |
| static int | run_camera (const char *destination, uint8_t command, uint8_t value) |
| Run one CHDKbridge capture or lens operation. | |
| int | chdk_pipe_init (void) |
| Prepare bounded controller ownership without accessing USB. | |
| static bool | partial_output_name (const char *name) |
| True for the controller's private mkstemp name, e.g. c000123.jpg.part.Ab12Cd. | |
| static void | remove_partial_outputs (const char *destination) |
| Delete partial outputs left by a killed or powered-off controller; none is running. | |
| int | chdk_pipe_shoot (const char *filename) |
| Capture directly to a new JPEG destination without overwriting a file. | |
| 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. | |
Variables | |
| char ** | environ |
| static pid_t | camera_pid = -1 |
| static bool | camera_ready |
| static bool | partial_cleanup_pending = true |
| static const volatile sig_atomic_t * | camera_running |
| struct { | |
| int descriptor | |
| size_t size | |
| uint8_t bytes [CHDK_CONTROL_REPLY_SIZE] | |
| bool invalid | |
| } | camera_reply = {.descriptor = -1} |
Detailed Description
Own and supervise bounded CHDKbridge child invocations for USHER.
The child is launched directly, not through a shell, and remains owned until reaped. Deadlines and process-group shutdown are required because USB work can outlive a single caller; an ambiguous exposure is never replayed.
Definition in file chdk_pipe.c.
Macro Definition Documentation
◆ USHER_CHDK_AF_ASSIST
| #define USHER_CHDK_AF_ASSIST "0" |
Definition at line 63 of file chdk_pipe.c.
◆ USHER_CHDK_APERTURE
| #define USHER_CHDK_APERTURE "" |
Definition at line 96 of file chdk_pipe.c.
◆ USHER_CHDK_AV96
| #define USHER_CHDK_AV96 "" |
Definition at line 99 of file chdk_pipe.c.
◆ USHER_CHDK_BUS
| #define USHER_CHDK_BUS "" |
Definition at line 130 of file chdk_pipe.c.
◆ USHER_CHDK_COMMAND
| #define USHER_CHDK_COMMAND "./bin/chdkbridge" |
Definition at line 45 of file chdk_pipe.c.
◆ USHER_CHDK_DATE_STAMP
| #define USHER_CHDK_DATE_STAMP "0" |
Definition at line 60 of file chdk_pipe.c.
◆ USHER_CHDK_DEVICE
| #define USHER_CHDK_DEVICE "" |
Definition at line 133 of file chdk_pipe.c.
◆ USHER_CHDK_DIRECT
| #define USHER_CHDK_DIRECT "0" |
Definition at line 120 of file chdk_pipe.c.
◆ USHER_CHDK_EXPOSURE_TIMEOUT
| #define USHER_CHDK_EXPOSURE_TIMEOUT "" |
Definition at line 105 of file chdk_pipe.c.
◆ USHER_CHDK_EXPOSURE_TOLERANCE
| #define USHER_CHDK_EXPOSURE_TOLERANCE "" |
Definition at line 108 of file chdk_pipe.c.
◆ USHER_CHDK_FLASH
| #define USHER_CHDK_FLASH "2" |
Definition at line 51 of file chdk_pipe.c.
◆ USHER_CHDK_FOCUS_DISTANCE
| #define USHER_CHDK_FOCUS_DISTANCE "" |
Definition at line 72 of file chdk_pipe.c.
◆ USHER_CHDK_FOCUS_MODE
| #define USHER_CHDK_FOCUS_MODE "" |
Definition at line 69 of file chdk_pipe.c.
◆ USHER_CHDK_FOCUS_TIMEOUT
| #define USHER_CHDK_FOCUS_TIMEOUT "" |
Definition at line 75 of file chdk_pipe.c.
◆ USHER_CHDK_FOCUS_TOLERANCE
| #define USHER_CHDK_FOCUS_TOLERANCE "" |
Definition at line 78 of file chdk_pipe.c.
◆ USHER_CHDK_ISO_MARKET
| #define USHER_CHDK_ISO_MARKET "" |
Definition at line 90 of file chdk_pipe.c.
◆ USHER_CHDK_ISO_MODE
| #define USHER_CHDK_ISO_MODE "1600" |
Definition at line 48 of file chdk_pipe.c.
◆ USHER_CHDK_ISO_REAL
| #define USHER_CHDK_ISO_REAL "" |
Definition at line 87 of file chdk_pipe.c.
◆ USHER_CHDK_KEEP_AWAKE
| #define USHER_CHDK_KEEP_AWAKE "1" |
Definition at line 123 of file chdk_pipe.c.
◆ USHER_CHDK_MAX_DOWNLOAD_MIB
| #define USHER_CHDK_MAX_DOWNLOAD_MIB "64" |
Definition at line 117 of file chdk_pipe.c.
◆ USHER_CHDK_ND
| #define USHER_CHDK_ND "" |
Definition at line 102 of file chdk_pipe.c.
◆ USHER_CHDK_PORT
| #define USHER_CHDK_PORT "" |
Definition at line 126 of file chdk_pipe.c.
◆ USHER_CHDK_QUALITY
| #define USHER_CHDK_QUALITY "2" |
Definition at line 57 of file chdk_pipe.c.
◆ USHER_CHDK_RAW
| #define USHER_CHDK_RAW "0" |
Definition at line 66 of file chdk_pipe.c.
◆ USHER_CHDK_RESOLUTION
| #define USHER_CHDK_RESOLUTION "0" |
Definition at line 54 of file chdk_pipe.c.
◆ USHER_CHDK_SHUTTER
| #define USHER_CHDK_SHUTTER "" |
Definition at line 81 of file chdk_pipe.c.
◆ USHER_CHDK_SV96
| #define USHER_CHDK_SV96 "" |
Definition at line 93 of file chdk_pipe.c.
◆ USHER_CHDK_TIMEOUT
| #define USHER_CHDK_TIMEOUT "60" |
Definition at line 111 of file chdk_pipe.c.
◆ USHER_CHDK_TV96
| #define USHER_CHDK_TV96 "" |
Definition at line 84 of file chdk_pipe.c.
◆ USHER_CHDK_USB_TIMEOUT
| #define USHER_CHDK_USB_TIMEOUT "5000" |
Definition at line 114 of file chdk_pipe.c.
Function Documentation
◆ chdk_monotonic_ms()
|
static |
Read the clock used for controller supervision.
- Returns
- Monotonic milliseconds, or zero on clock failure.
Deadlines must not move when the wall clock is corrected.
Definition at line 175 of file chdk_pipe.c.
◆ chdk_number()
|
static |
Parse a decimal build-profile limit.
- Parameters
-
text Null-terminated numeric setting. minimum Inclusive lower bound. maximum Inclusive upper bound.
- Returns
- Parsed value, or zero on syntax or range error.
Limit callers use positive ranges so zero is an unambiguous failure sentinel. Reject signs and suffixes rather than accepting a prefix.
Definition at line 189 of file chdk_pipe.c.
◆ chdk_pipe_control()
| 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.
- Parameters
-
command Absolute zoom or lens retraction. value Requested zoom value; must be zero for retraction. [out] observed Returned device value; meaningful with applied status. [out] evidence Returned evidence kind; meaningful with applied status.
- Returns
- A CAM_CONTROL status; UNKNOWN means execution could not be confirmed.
Check both the reply shape and echoed command. Do not retry an ambiguous operation or infer success from the controller's exit alone.
Definition at line 477 of file chdk_pipe.c.
◆ chdk_pipe_deinit()
| void chdk_pipe_deinit | ( | void | ) |
Terminate and reap the owned process group using bounded grace periods.
Definition at line 239 of file chdk_pipe.c.
◆ chdk_pipe_init()
| int chdk_pipe_init | ( | void | ) |
Prepare bounded controller ownership without accessing USB.
- Returns
- 0 when the executable is accessible and shutdown is not requested.
Settings and camera readiness are checked in the capture invocation.
Definition at line 386 of file chdk_pipe.c.
◆ chdk_pipe_set_running()
| void chdk_pipe_set_running | ( | const volatile sig_atomic_t * | running | ) |
Configure cancellation for controller operations.
- Parameters
-
running Borrowed service flag, or NULL to disable flag-based cancellation.
Keep the flag alive for the lifetime of controller work; cleanup waits must still reap a child after cancellation.
Definition at line 167 of file chdk_pipe.c.
◆ chdk_pipe_shoot()
| int chdk_pipe_shoot | ( | const char * | filename | ) |
Capture directly to a new JPEG destination without overwriting a file.
- Parameters
-
filename Caller-owned destination path.
- Returns
- 0 for a checked saved JPEG, otherwise -1; capture is never replayed.
Definition at line 440 of file chdk_pipe.c.
◆ drain_control_reply()
|
static |
Collect one chunk from the nonblocking control reply pipe.
Retain partial bytes across polls; flag overflow and hard I/O errors so a truncated or overlong reply cannot count as confirmation.
Definition at line 151 of file chdk_pipe.c.
◆ partial_output_name()
|
static |
True for the controller's private mkstemp name, e.g. c000123.jpg.part.Ab12Cd.
Definition at line 399 of file chdk_pipe.c.
◆ remove_partial_outputs()
|
static |
Delete partial outputs left by a killed or powered-off controller; none is running.
Definition at line 413 of file chdk_pipe.c.
◆ run_camera()
|
static |
Run one CHDKbridge capture or lens operation.
- Parameters
-
destination Private capture path; unused for a control operation. command Zero for capture, otherwise the validated control command. value Control operand, unused for capture and lens retraction.
- Returns
- Zero for successful child execution, or -1 on setup, cancellation or child failure.
Validate build-profile limits and acquire exclusive child ownership before spawning. Lens operations use a distinct reply pipe; child exit alone does not establish that the camera applied the command.
Definition at line 253 of file chdk_pipe.c.
◆ stop_camera()
|
static |
Stop the owned controller group within bounded grace periods.
Allow graceful PTP cleanup before forced termination. Preserve an unreaped PID so future commands cannot acquire the camera concurrently.
Definition at line 225 of file chdk_pipe.c.
◆ wait_camera()
|
static |
Poll the owned controller for exit while collecting its reply.
- Parameters
-
[out] status Wait status; valid when the result is one. timeout_ms Polling budget; zero checks without sleeping. cancelable True to honor the service cancellation flag.
- Returns
- One if reaped, zero on timeout or cancellation, or -1 on wait/setup error.
Drain during waits and again on exit; the last reply can arrive between the initial read and the successful waitpid.
Definition at line 204 of file chdk_pipe.c.
Variable Documentation
◆ bytes
| uint8_t bytes[CHDK_CONTROL_REPLY_SIZE] |
Definition at line 144 of file chdk_pipe.c.
◆ camera_pid
|
static |
Definition at line 137 of file chdk_pipe.c.
◆ camera_ready
|
static |
Definition at line 138 of file chdk_pipe.c.
◆ [struct]
| struct { ... } camera_reply |
◆ camera_running
|
static |
Definition at line 140 of file chdk_pipe.c.
◆ descriptor
| int descriptor |
Definition at line 142 of file chdk_pipe.c.
◆ environ
|
extern |
◆ invalid
| bool invalid | ( | void | ) |
Definition at line 145 of file chdk_pipe.c.
◆ partial_cleanup_pending
|
static |
Definition at line 139 of file chdk_pipe.c.
◆ size
| size_t size |
Definition at line 143 of file chdk_pipe.c.