41#ifndef USHER_LOCAL_PHOTO_DIR
42 #define USHER_LOCAL_PHOTO_DIR "~/Pictures/sensoraia"
45#ifndef USHER_MOCK_IMAGE
46 #define USHER_MOCK_IMAGE "resources/mock_image_01.jpg"
72 fprintf(stderr,
"LOCAL_PIPE:\tfailed to access mock image %s: %s\n",
98 if (photo_count < 0) {
101 if (photo_count == 0) {
102 fprintf(stderr,
"LOCAL_PIPE:\tno readable .jpg files found beside the executable; using %s\n",
108 printf(
"LOCAL_PIPE:\ttest photo directory: %s (%d image%s)\n",
124 fprintf(stderr,
"LOCAL_PIPE:\tfailed to create photo directory %s: %s\n",
129 struct stat directory_status;
130 if (stat(
photo_directory, &directory_status) != 0 || !S_ISDIR(directory_status.st_mode)
132 fprintf(stderr,
"LOCAL_PIPE:\tphoto directory is not writable: %s\n",
photo_directory);
146 if (configured == NULL || configured[0] ==
'\0') {
149 if (configured[0] ==
'/') {
151 return length > 0 && (size_t)length <
sizeof(
photo_directory) ? 0 : -1;
156 fprintf(stderr,
"LOCAL_PIPE:\tfailed to locate USHER executable: %s\n", strerror(errno));
161 if (separator == NULL) {
168 return path_length > 0 && (size_t)path_length <
sizeof(
photo_directory) - directory_length ? 0 : -1;
175 printf(
"LOCAL_PIPE:\tmap mock photo directory: %s\n",
photo_directory);
180 const struct camera_backend *identity,
const char *cache_directory,
181 double horizontal_fov_deg,
double vertical_fov_deg)
184 || !identity || !identity->prefix || !identity->mock.width || !identity->mock.height)
return -1;
185 const char *
marker = identity->mock.filename_marker ? identity->mock.filename_marker :
"m";
186 int length = snprintf(filename, filename_size,
"%s/%s%c%06d.jpg",
photo_directory,
188 if (length < 0 || (
size_t)length >= filename_size)
return -1;
189 return image_mock_map(filename, cache_directory,
shot, identity->mock.width, identity->mock.height,
190 horizontal_fov_deg, vertical_fov_deg);
196 || filename == NULL || filename_size == 0
197 || identity == NULL || identity->prefix ==
'\0') {
207 current_source = selected_source;
208 printf(
"LOCAL_PIPE:\tselected test image: %s\n", current_source);
211 int length = snprintf(filename, filename_size,
"%s/%s%c%06d.jpg",
photo_directory,
212 identity->mock.filename_marker ? identity->mock.filename_marker :
"m",
213 identity->prefix, image_number);
214 if (length < 0 || (
size_t)length >= filename_size) {
218 int source_fd =
open(current_source, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
220 fprintf(stderr,
"LOCAL_PIPE:\tfailed to open mock image %s: %s\n", current_source, strerror(errno));
224 struct stat source_status;
225 if (fstat(source_fd, &source_status) != 0 || !S_ISREG(source_status.st_mode)
226 || source_status.st_size <= 0) {
232 int destination_fd =
open(filename, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0644);
233 if (destination_fd < 0) {
234 fprintf(stderr,
"LOCAL_PIPE:\tfailed to create %s: %s\n", filename, strerror(errno));
235 const int error = errno;
244 off_t remaining = source_status.st_size;
246 while (remaining > 0) {
247 const size_t requested = remaining > (off_t)
sizeof(buffer) ?
sizeof(buffer) : (size_t)remaining;
248 const ssize_t bytes_read = read(source_fd, buffer, requested);
249 if (bytes_read < 0 && errno == EINTR)
continue;
250 if (bytes_read <= 0) {
251 if (bytes_read == 0) errno = EIO;
256 while (written < bytes_read) {
257 ssize_t
count = write(destination_fd, &buffer[written], (
size_t)(bytes_read - written));
258 if (
count < 0 && errno == EINTR)
continue;
260 if (
count == 0) errno = EIO;
269 remaining -= bytes_read;
273 ||
completed.st_size != source_status.st_size
274 ||
completed.st_mtim.tv_sec != source_status.st_mtim.tv_sec
275 ||
completed.st_mtim.tv_nsec != source_status.st_mtim.tv_nsec
276 ||
completed.st_ctim.tv_sec != source_status.st_ctim.tv_sec
277 ||
completed.st_ctim.tv_nsec != source_status.st_ctim.tv_nsec)) {
310 ssize_t length = readlink(
"/proc/self/exe", executable,
sizeof(executable) - 1);
311 if (length < 0 || (
size_t)length >=
sizeof(executable) - 1) {
312 fprintf(stderr,
"LOCAL_PIPE:\tfailed to locate USHER executable: %s\n", strerror(errno));
315 executable[length] =
'\0';
317 char *separator = strrchr(executable,
'/');
318 if (separator == NULL) {
319 fprintf(stderr,
"LOCAL_PIPE:\tUSHER executable path has no directory: %s\n", executable);
322 if (separator == executable) {
328 int path_length = snprintf(directory, directory_size,
"%s%stestphotos",
329 executable, separator == executable ?
"" :
"/");
330 if (path_length < 0 || (
size_t)path_length >= directory_size) {
331 fprintf(stderr,
"LOCAL_PIPE:\ttest photo directory path is too long\n");
335 DIR *stream = opendir(directory);
336 if (stream == NULL) {
337 if (errno != ENOENT && errno != ENOTDIR) {
338 fprintf(stderr,
"LOCAL_PIPE:\tfailed to open test photo directory %s: %s\n",
339 directory, strerror(errno));
347 struct dirent *entry;
348 while ((entry = readdir(stream)) != NULL) {
366 if (stream == NULL) {
367 fprintf(stderr,
"LOCAL_PIPE:\tfailed to open test photo directory %s: %s\n",
373 struct dirent *entry;
374 while ((entry = readdir(stream)) != NULL) {
380 fprintf(stderr,
"LOCAL_PIPE:\ttest photo directory contains no readable .jpg files\n");
385 size_t selected_index;
392 size_t current_index = 0;
393 while ((entry = readdir(stream)) != NULL) {
397 if (current_index == selected_index) {
400 if (length < 0 || (
size_t)length >= path_size) {
401 fprintf(stderr,
"LOCAL_PIPE:\tselected test image path is too long\n");
410 fprintf(stderr,
"LOCAL_PIPE:\ttest photo directory changed during image selection\n");
422 size_t name_length = strlen(entry->d_name);
423 if (name_length < 4 || strcasecmp(&entry->d_name[name_length - 4],
".jpg") != 0) {
430 return fstatat(
descriptor, entry->d_name, &status, 0) == 0
431 && S_ISREG(status.st_mode)
432 && faccessat(
descriptor, entry->d_name, R_OK, 0) == 0;
443 int random_fd =
open(
"/dev/urandom", O_RDONLY);
445 fprintf(stderr,
"LOCAL_PIPE:\tfailed to open /dev/urandom: %s\n", strerror(errno));
449 uint64_t random_value;
450 unsigned char *
bytes = (
unsigned char *)&random_value;
451 size_t bytes_read = 0;
452 while (bytes_read <
sizeof(random_value)) {
453 ssize_t count_read = read(random_fd, &
bytes[bytes_read],
sizeof(random_value) - bytes_read);
454 if (count_read < 0 && errno == EINTR) {
457 if (count_read <= 0) {
458 fprintf(stderr,
"LOCAL_PIPE:\tfailed to read /dev/urandom: %s\n", strerror(errno));
462 bytes_read += (size_t)count_read;
466 *index = (size_t)(random_value % (uint64_t)
count);
static const char * photo_directory
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
void(* close)(struct usbfs_handle *)
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
static struct ear_inference_result queued_evidence completed
int local_pipe_map_shoot(char *filename, size_t filename_size, const union dc_shot_union *shot, const struct camera_backend *identity, const char *cache_directory, double horizontal_fov_deg, double vertical_fov_deg)
Render a map-backed capture using the trigger's camera geometry.
static bool is_readable_jpg(DIR *directory, const struct dirent *entry)
Test whether a directory entry is an eligible JPEG fixture.
int local_pipe_test_init(const char *mock_image)
Initialize test mode with a supplied JPEG or a random fixture beside USHER.
#define USHER_LOCAL_PHOTO_DIR
static int select_random_test_photo(char *path, size_t path_size)
Select a fixture from the current test photo directory.
void local_pipe_deinit(void)
Forget selected local fixture sources.
static int resolve_photo_directory(void)
Resolve the configured local output path into backend storage.
int local_pipe_shoot(char *filename, size_t filename_size, int image_number, const struct camera_backend *identity)
Create a mock capture file for a shot.
static int random_index(size_t count, size_t *index)
Select an index for fixture playback.
static char test_photo_directory[PATH_MAX]
static char source_image[PATH_MAX]
static int prepare_photo_directory(void)
Prepare usable output storage before accepting local shots.
int local_pipe_map_init(const char *source)
Prepare output storage for map-backed simulation.
static int find_test_photo_directory(char *directory, size_t directory_size)
Locate and count eligible fixtures beside the executable.
int local_pipe_init(const char *mock_image)
Initialize a deterministic mock image source for local camera simulation.
Local mock-camera backend API for simulator and desk testing.
uint8_t marker[LWIR_THERMAL_HEADER_BYTES+LWIR_THERMAL_CHUNK_CAPACITY]
struct jpeg_error_mgr error
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.
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
Function table describing one backend's owned camera operations.
static const char * mock_image
struct usher_control_result result