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_camera_backend.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_camera_backend.c
20 * @brief Adapt CHDKbridge capture and lens controls to USHER's backend contract.
21 * @details This layer resolves the configured photo directory and owns naming;
22 * the subprocess layer owns USB/camera transactions. It advertises only the
23 * controls for which CHDKbridge can return command-specific evidence.
24 */
25#include "chdk_camera_backend.h"
26#include "chdk_pipe.h"
27#include "camera_control.h"
28#include "path_utils.h"
29#include <errno.h>
30#include <limits.h>
31#include <stdio.h>
32#include <sys/stat.h>
33#include <unistd.h>
34
35#ifndef USHER_CHDK_PHOTO_DIR
36#define USHER_CHDK_PHOTO_DIR "~/Pictures/sensoraia"
37#endif
38#ifndef USHER_SODA
39#define USHER_SODA "./bin/soda"
40#endif
42
43/** @brief Pass service cancellation to CHDKbridge supervision.
44 * @param host Nullable backend host whose running flag outlives capture work.
45 * @details Retain only the flag; the backend does not need the host's other callbacks. */
46static void chdk_backend_configure(const struct camera_backend_host *host)
47{
48 chdk_pipe_set_running(host ? host->running : NULL);
49}
50
51void chdk_camera_backend_set_photo_directory(const char *directory)
52{
53 photo_directory = directory ? directory : USHER_CHDK_PHOTO_DIR;
54}
55
56/** @brief Prepare capture storage and initialize the CHDK controller path.
57 * @param source_image Unused fixture argument from the shared backend interface.
58 * @return Zero for successful setup, or -1 on storage/controller setup failure.
59 * @details Reject unusable output storage before advertising capture readiness. */
60static int chdk_backend_init(const char *source_image)
61{
62 (void)source_image;
63 char resolved[PATH_MAX];
64 const char *directory = usher_resolve_path(photo_directory, resolved, sizeof(resolved));
65 if (usher_ensure_directory(directory) != 0) return -1;
66 struct stat status;
67 if (stat(directory, &status) != 0 || !S_ISDIR(status.st_mode) || access(directory, W_OK) != 0) {
68 fprintf(stderr, "USHER: CHDK photo directory is not writable: %s\n", directory);
69 return -1;
70 }
71 return chdk_pipe_init();
72}
73
74/** @brief Capture one CHDK JPEG at the path derived from its shot number.
75 * @param[out] filename Output path; cleared on failure when writable.
76 * @param capacity Path buffer size including the terminator.
77 * @param number Nonnegative photo number.
78 * @return Zero for a completed capture, or -1 on invalid input or capture failure.
79 * @details Pass the path to the controller only if it fits so truncated
80 * names cannot misassociate a saved image with another shot. */
81static int chdk_backend_shoot(char *filename, size_t capacity, int number)
82{
83 if (!filename || !capacity) return -1;
84 filename[0] = '\0';
85 if (number < 0) return -1;
86 char resolved[PATH_MAX];
87 const char *directory = usher_resolve_path(photo_directory, resolved, sizeof(resolved));
88 int length = snprintf(filename, capacity, "%s/c%06d.jpg", directory, number);
89 if (length < 0 || (size_t)length >= capacity || chdk_pipe_shoot(filename) != 0) {
90 filename[0] = '\0';
91 return -1;
92 }
93 return 0;
94}
95
97{
98 const struct camera_backend backend = {
99 .name = "chdk", .init = chdk_backend_init, .shoot = chdk_backend_shoot, .configure = chdk_backend_configure,
100 .deinit = chdk_pipe_deinit, .soda_application = USHER_SODA,
101 .camera_id = USHER_CAMERA_CHDK, .prefix = 'c', .capabilities = CAMERA_CAP_IMAGE,
103 .control_init = chdk_pipe_init, .control = chdk_pipe_control,
104 .mock = {.width = 1280, .height = 960, .filename_marker = "m"},
105 .soda_option = "--chdkcam", .photo_directory = photo_directory
106 };
107 return backend;
108}
static const char * photo_directory
Definition ai_cam_pipe.c:85
@ CAMERA_CAP_IMAGE
#define USHER_SODA
Fixed-size camera-control wire codec and FC-side ticket state.
static unsigned capacity[COVERAGE_CAMERAS]
struct camera_backend chdk_camera_backend_get(void)
#define USHER_CHDK_PHOTO_DIR
static void chdk_backend_configure(const struct camera_backend_host *host)
Pass service cancellation to CHDKbridge supervision.
void chdk_camera_backend_set_photo_directory(const char *directory)
static int chdk_backend_shoot(char *filename, size_t capacity, int number)
Capture one CHDK JPEG at the path derived from its shot number.
static int chdk_backend_init(const char *source_image)
Prepare capture storage and initialize the CHDK controller path.
CHDK-owned download and photo publication.
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
void chdk_pipe_set_running(const volatile sig_atomic_t *running)
Configure cancellation for controller operations.
Definition chdk_pipe.c:167
int chdk_pipe_init(void)
Prepare bounded controller ownership without accessing USB.
Definition chdk_pipe.c:386
int chdk_pipe_shoot(const char *filename)
Capture directly to a new JPEG destination without overwriting a file.
Definition chdk_pipe.c:440
void chdk_pipe_deinit(void)
Terminate and reap the owned process group using bounded grace periods.
Definition chdk_pipe.c:239
Bounded CHDKbridge subprocess lifecycle for USHER's visible-camera backend.
static char source_image[PATH_MAX]
Definition local_pipe.c:53
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
const volatile sig_atomic_t * running
Borrowed shutdown flag; backend workers must stop when it clears.
Function table describing one backend's owned camera operations.
#define CAM_CMD_ZOOM_ABSOLUTE
#define CAM_CMD_LENS_RETRACT
#define CAM_CONTROL_CAP(command)
#define USHER_CAMERA_CHDK