Loading...
Searching...
No Matches
lwir_device_v4l2.c
Go to the documentation of this file.
163 if (now.tv_sec < 0 || now.tv_nsec < 0 || now.tv_nsec >= 1000000000L) { errno = EPROTO; return false; }
290static bool video_identity(const struct stat *node, enum lwir_frame_layout *layout, char serial[64],
295 int length = snprintf(path, sizeof(path), "/sys/dev/char/%u:%u/device", major(node->st_rdev), minor(node->st_rdev));
299 if (video_text(resolved, "idVendor", vendor, sizeof(vendor)) && video_text(resolved, "idProduct", product, sizeof(product))) {
318 const unsigned long bus_number = strtoul(bus, NULL, 10), device_number = strtoul(address, NULL, 10);
360static int video_probe(const char *path, const struct lwir_device_options *options, struct lwir_device *candidate)
367 || !video_identity(&node, &layout, serial, power_key, power_port, &usb_bus, &usb_address)) return 0;
373 if (fstat(descriptor, &opened) || !S_ISCHR(opened.st_mode) || opened.st_dev != node.st_dev || opened.st_rdev != node.st_rdev
376 /* device_caps describes this node; capabilities covers the whole physical device when DEVICE_CAPS is set. */
377 const uint32_t capabilities = capability.capabilities & V4L2_CAP_DEVICE_CAPS ? capability.device_caps : capability.capabilities;
378 const bool capture = !error && !strncmp((const char *)capability.driver, "uvcvideo", sizeof(capability.driver))
388 .layout = layout, .cancelled = options->cancelled, .video_usb_bus = usb_bus, .video_usb_address = usb_address};
389 for (size_t index = 0; index < sizeof(candidate->serial); ++index) candidate->serial[index] = serial[index];
390 for (size_t index = 0; index < sizeof(candidate->video_power_key); ++index) candidate->video_power_key[index] = power_key[index];
391 for (size_t index = 0; index < sizeof(candidate->video_power_port); ++index) candidate->video_power_port[index] = power_port[index];
434 int count = snprintf(directory, sizeof(directory), "/run/lock/sensoraia-lwir-%lu", (unsigned long)geteuid());
435 if (count < 0 || (size_t)count >= sizeof(directory) || !device->video_power_key[0] || !device->video_power_port[0]) { errno = EINVAL; return false; }
437 const int folder = open(directory, O_RDONLY | O_DIRECTORY | O_NONBLOCK | O_CLOEXEC | O_NOFOLLOW);
444 int pending = snprintf(temporary, sizeof(temporary), "usb-%s.pending", device->video_power_port);
445 if (count < 0 || (size_t)count >= sizeof(filename) || pending < 0 || (size_t)pending >= sizeof(temporary)) {
457 valid = fstat(file, &identity) == 0 && S_ISREG(identity.st_mode) && identity.st_uid == geteuid()
458 && identity.st_nlink == 1 && !(identity.st_mode & 0077) && identity.st_size > 0 && identity.st_size < (off_t)sizeof(key);
482 if (unlinkat(folder, temporary, 0) && errno != ENOENT) { const int error = errno; close(folder); errno = error; return false; }
552 if (close(descriptor)) fprintf(stderr, "LWIR: camera close reported an error: %s\n", strerror(errno));
585static bool video_setup(struct lwir_device *device, struct lwir_device_options *options, uint64_t deadline)
589 if (device->layout == LWIR_YUYV_IMAGE && options->requires_thermal) { errno = ENOTSUP; return false; }
611 void *address = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, MAP_SHARED, device->video_fd, buffer.m.offset);
621 /* Set first: a STREAMON that fails may still have changed driver state that STREAMOFF must undo. */
626 fprintf(stderr, "LWIR: native V4L2 YUYV %ux%u transport, %ux%u %s\n", width, format.fmt.pix.height,
627 width, height, device->layout == LWIR_COMBINED ? "image and temperature planes" : "image only");
653 if (!options || !device || !options->timeout_ms || options->timeout_ms > 120000 || options->warmup_ms >= options->timeout_ms
655 || (options->serial && strnlen(options->serial, sizeof(device->serial)) >= sizeof(device->serial))) {
690 if (!video_power_marker(device, false) || !video_setup(device, options, deadline)) { lwir_device_close(device); return false; }
715 if (!distance || distance > UINT32_MAX / 2U || device->video_sequence > UINT64_MAX - distance) return false;
755 || (device->layout != LWIR_COMBINED && device->layout != LWIR_YUYV_IMAGE)) { errno = EINVAL; return false; }
759 const size_t bytes = lwir_frame_bytes(width, height) / (device->layout == LWIR_COMBINED ? 1U : 2U);
760 if (device->video_width != width || device->video_height != height || device->video_bytes != bytes) { errno = EINVAL; return false; }
764 || buffer->length < bytes || buffer->length > LWIR_MAX_FRAME_BYTES) { errno = EINVAL; return false; }
781static bool video_destination_valid(const struct lwir_device *device, const void *destination, size_t bytes)
806static bool video_ranges_separate(const void *first, size_t first_bytes, const void *second, size_t second_bytes)
843static bool video_dequeue(struct lwir_device *device, struct v4l2_buffer *buffer, bool *received)
874static bool video_timestamp_fresh(const struct v4l2_buffer *buffer, uint64_t start, uint64_t now)
876 if ((buffer->flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) != V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) return true;
877 if (buffer->timestamp.tv_sec < 0 || buffer->timestamp.tv_usec < 0 || buffer->timestamp.tv_usec >= 1000000
878 || (uint64_t)buffer->timestamp.tv_sec > (UINT64_MAX - (uint64_t)buffer->timestamp.tv_usec) / 1000000U) return false;
897static bool video_frame_qualified(const struct lwir_device *device, const struct v4l2_buffer *buffer,
902 || buffer->length < buffer->bytesused || buffer->length > device->video_buffers[buffer->index].length
905 device->video_buffers[buffer->index].address, buffer->bytesused, device->video_width, device->video_height);
919static void video_qualification(struct lwir_device *device, bool valid, bool continuous, uint64_t now)
968 if (capacity < device->video_bytes || !video_destination_valid(device, destination, device->video_bytes)
972 || !video_ranges_separate(observation, sizeof(*observation), device, sizeof(*device)) || options->width != device->video_width
973 || options->height != device->video_height || !options->timeout_ms || options->timeout_ms > 120000
980 /* Only the first startup of a USB instance may wait for warmup; it also makes the non-MONOTONIC drain redundant. */
986 const size_t image_bytes = device->layout == LWIR_COMBINED ? device->video_bytes / 2 : device->video_bytes;
1012 const bool usable = clock_valid && sequence_valid && video_frame_qualified(device, &buffer, 0, now);
1015 /* The scene check exists only for combined data, so requesting it on image-only rejects every frame. */
1017 || (device->layout == LWIR_COMBINED && lwir_frame_plausible(source, buffer.bytesused, options->width, options->height)));
1019 const bool monotonic = (buffer.flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) == V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1039 && (!options->settle || lwir_frame_settled(source, destination, options->width, options->height));
1043 accepted = qualified && settled_now && (!first_startup || now - start >= (uint64_t)options->warmup_ms * 1000U);
1045 if (options->settle || accepted) for (size_t index = 0; index < image_bytes; ++index) destination[index] = source[index];
1047 for (size_t index = image_bytes; index < device->video_bytes; ++index) destination[index] = source[index];
1053 selected = (struct lwir_device_observation){.sequence = device->video_sequence, .arrival_us = now,
1054 .kernel_monotonic_us = monotonic ? (uint64_t)buffer.timestamp.tv_sec * 1000000U + (uint64_t)buffer.timestamp.tv_usec : 0,
1055 .kernel_timestamp_flags = buffer.flags & (V4L2_BUF_FLAG_TIMESTAMP_MASK | V4L2_BUF_FLAG_TSTAMP_SRC_MASK)};
1062 && now - start >= (uint64_t)options->warmup_ms * 1000U && !video_power_marker(device, true)) return video_fail(device, errno);
1066 if (!video_now(&finished) || finished < now || finished >= deadline) { errno = ETIMEDOUT; return false; }
1108 video_qualification(device, sequence_valid && video_frame_qualified(device, &buffer, 0, now), continuous, now);
1110 if (!device->video_power_ready && device->video_good_frames >= 8 && !video_power_marker(device, true)) return video_fail(device, errno);
1203 if (!video_identity(&node, &layout, serial, key, port, &bus, &address)) { errno = ENODEV; return false; }
1205 || bus != device->video_usb_bus || address != device->video_usb_address) { errno = ENODEV; return false; }
1234 const int length = snprintf(path, sizeof(path), "/dev/bus/usb/%03u/%03u", device->video_usb_bus, device->video_usb_address);
1239 const dev_t expected = makedev(189, (device->video_usb_bus - 1U) * 128U + device->video_usb_address - 1U);
1264static bool video_control(struct lwir_device *device, const struct lwir_control_request *request,
1269 if (!lwir_control_valid(request) || (read_only && request->kind == LWIR_CONTROL_FFC)) { errno = EINVAL; return false; }
1272 result->reply.status = errno == EINTR ? LWIR_STATE_CANCELLED : errno == ENOTSUP ? LWIR_STATE_UNSUPPORTED : LWIR_STATE_IO_ERROR;
1277 const struct lwir_state_io io = {&transport, video_state_transfer, video_state_now, video_state_pause};
1278 bool success = read_only ? lwir_state_control_read(&io, request->kind, result) : lwir_state_control(&io, request, result);
1289bool lwir_device_control(struct lwir_device *device, const struct lwir_control_request *request,
1303 /* Placeholder value only; presumably needed to satisfy lwir_control_valid() for emissivity. Nothing is written. */
1304 const struct lwir_control_request request = {.kind = kind, .value = kind == LWIR_CONTROL_EMISSIVITY ? 1 : 0};
1328 for (unsigned index = 0; index < LWIR_STATE_FIELDS; ++index) state->values[index].status = LWIR_STATE_SKIPPED;
1330 struct video_state_transport transport = {.device = device, .descriptor = video_state_open(device)};
static bool capture(struct transport *transport, FILE *file, const struct capture_settings *settings)
Capture to the camera's SD card and download the resulting JPEG.
Definition chdkbridge.c:1463
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
Definition dipla_camera_backend.c:170
static uint64_t milliseconds(void)
Read elapsed-time evidence unaffected by changes to civil time.
Definition dipla_camera_backend.c:59
Tiny1C/TDK acquisition and Tiny1C state, without image or mission logic.
#define LWIR_VIDEO_BUFFERS
Number of V4L2 MMAP buffers requested from uvcvideo.
Definition lwir_device.h:70
static bool video_now(uint64_t *microseconds)
Read CLOCK_MONOTONIC in microseconds.
Definition lwir_device_v4l2.c:159
static bool video_dequeue(struct lwir_device *device, struct v4l2_buffer *buffer, bool *received)
Non-blocking VIDIOC_DQBUF with validation of what the kernel returned.
Definition lwir_device_v4l2.c:843
static int video_ioctl(int descriptor, unsigned long request, void *argument)
ioctl() with a bounded retry on EINTR.
Definition lwir_device_v4l2.c:200
static bool video_checkpoint(const struct lwir_device *device, uint64_t deadline)
Combined cancellation and deadline check placed between setup steps.
Definition lwir_device_v4l2.c:180
bool lwir_device_discard(struct lwir_device *device, bool *received)
Dequeue and immediately requeue at most one pending frame.
Definition lwir_device_v4l2.c:1090
static bool video_state_pause(void *context, unsigned milliseconds)
Short cancellable delay between mailbox polls for lwir_state.c.
Definition lwir_device_v4l2.c:1175
static int video_probe(const char *path, const struct lwir_device_options *options, struct lwir_device *candidate)
Test one V4L2 node and, if it is a usable camera, open and lock it.
Definition lwir_device_v4l2.c:360
void lwir_device_read_state(struct lwir_device *device, struct lwir_device_state *state)
Best-effort read-only snapshot of Tiny1-C state; never writes settings.
Definition lwir_device_v4l2.c:1319
bool lwir_device_acquire(struct lwir_device *device, const struct lwir_device_options *options, uint8_t *destination, size_t capacity, struct lwir_device_observation *observation)
Wait for, validate and copy the first acceptable frame that started after this call.
Definition lwir_device_v4l2.c:963
static uint64_t video_state_now(void *context)
Clock callback for lwir_state.c.
Definition lwir_device_v4l2.c:1159
bool lwir_device_open(struct lwir_device_options *options, struct lwir_device *device)
Find, lock and start exactly one supported camera within the timeout.
Definition lwir_device_v4l2.c:651
bool lwir_device_control(struct lwir_device *device, const struct lwir_control_request *request, struct lwir_control_result *result)
Public control write; see lwir_device.h.
Definition lwir_device_v4l2.c:1289
static bool video_sequence(struct lwir_device *device, uint32_t sequence, bool *continuous)
Extend the kernel's 32-bit frame sequence to a monotonic 64-bit count.
Definition lwir_device_v4l2.c:709
static int video_state_open(struct lwir_device *device)
Open the usbfs node of the streaming Tiny1-C for one state/control operation.
Definition lwir_device_v4l2.c:1227
static bool video_identity(const struct stat *node, enum lwir_frame_layout *layout, char serial[64], char power_key[160], char power_port[64], unsigned *usb_bus, unsigned *usb_address)
Resolve a V4L2 character device to its USB camera identity through sysfs.
Definition lwir_device_v4l2.c:290
static bool video_frame_qualified(const struct lwir_device *device, const struct v4l2_buffer *buffer, uint64_t start, uint64_t now)
Structural and content check of a dequeued frame, independent of the request.
Definition lwir_device_v4l2.c:897
static bool video_setup(struct lwir_device *device, struct lwir_device_options *options, uint64_t deadline)
Negotiate the native format, map the capture buffers and start streaming.
Definition lwir_device_v4l2.c:585
bool lwir_device_control_read(struct lwir_device *device, enum lwir_control_kind kind, struct lwir_control_result *result)
Public control read; see lwir_device.h.
Definition lwir_device_v4l2.c:1300
static bool video_cancelled(const struct lwir_device *device)
Poll the borrowed cancellation flag.
Definition lwir_device_v4l2.c:144
static bool video_ranges_separate(const void *first, size_t first_bytes, const void *second, size_t second_bytes)
Test that two byte ranges do not overlap.
Definition lwir_device_v4l2.c:806
static bool video_fail(struct lwir_device *device, int error)
Poison the session after a fatal kernel or protocol error.
Definition lwir_device_v4l2.c:733
static int video_state_transfer(void *context, bool input, uint16_t address, uint8_t *data, unsigned length, unsigned timeout)
One Tiny1-C vendor mailbox control transfer over usbfs.
Definition lwir_device_v4l2.c:1139
static bool video_destination_valid(const struct lwir_device *device, const void *destination, size_t bytes)
Reject caller memory that overlaps any mapped camera buffer.
Definition lwir_device_v4l2.c:781
static bool video_requeue(struct lwir_device *device, unsigned index)
Return a dequeued buffer to the driver.
Definition lwir_device_v4l2.c:822
static bool video_ready(const struct lwir_device *device)
Verify session invariants before touching the stream.
Definition lwir_device_v4l2.c:751
static bool video_control(struct lwir_device *device, const struct lwir_control_request *request, struct lwir_control_result *result, bool read_only)
Shared implementation of control write and read-back.
Definition lwir_device_v4l2.c:1264
static bool video_text(const char *directory, const char *name, char *text, size_t capacity)
Read one short sysfs/procfs attribute as a validated single-line string.
Definition lwir_device_v4l2.c:225
static bool video_format_valid(const struct lwir_device_options *options, enum lwir_frame_layout layout, const struct v4l2_format *format)
Check that the format the driver actually granted is exactly the native camera format.
Definition lwir_device_v4l2.c:122
static bool video_power_marker(struct lwir_device *device, bool complete)
Read or publish the per-USB-instance startup qualification marker.
Definition lwir_device_v4l2.c:431
void lwir_device_close(struct lwir_device *device)
Release a session in the reverse order of setup.
Definition lwir_device_v4l2.c:525
static void video_qualification(struct lwir_device *device, bool valid, bool continuous, uint64_t now)
Advance the eight-consecutive-good-frames startup counter.
Definition lwir_device_v4l2.c:919
static bool video_state_identity(struct lwir_device *device)
Confirm the streaming camera is still the same USB power instance.
Definition lwir_device_v4l2.c:1196
static bool video_timestamp_fresh(const struct v4l2_buffer *buffer, uint64_t start, uint64_t now)
Check that a MONOTONIC kernel timestamp lies within [start, now].
Definition lwir_device_v4l2.c:874
bool lwir_measurement_valid(unsigned width, unsigned height, const struct lwir_measurement *measurement)
Validate a measurement request against native geometry.
Definition lwir_frame.c:84
bool lwir_frame_capture_valid(const uint8_t *frame, size_t bytes, unsigned width, unsigned height)
Live-capture qualification.
Definition lwir_frame.c:446
bool lwir_frame_plausible(const uint8_t *frame, size_t bytes, unsigned width, unsigned height)
Conservative scene-plausibility heuristic.
Definition lwir_frame.c:465
size_t lwir_frame_bytes(unsigned width, unsigned height)
Combined frame size for supported geometry.
Definition lwir_frame.c:56
bool lwir_frame_settled(const uint8_t *frame, const uint8_t *previous_image, unsigned width, unsigned height)
Image-stability heuristic between consecutive frames.
Definition lwir_frame.c:498
lwir_frame_layout
Buffer content kinds: full image+thermal frame, or the YUYV image plane alone.
Definition lwir_frame.h:54
#define LWIR_MAX_FRAME_BYTES
Largest combined frame: two planes of 2 bytes per pixel.
Definition lwir_frame.h:51
void lwir_state_read(const struct lwir_state_io *io, struct lwir_device_state *state)
Read all fields of struct lwir_device_state within LWIR_STATE_BUDGET_MS.
Definition lwir_state.c:159
bool lwir_state_control_read(const struct lwir_state_io *io, enum lwir_control_kind kind, struct lwir_control_result *result)
Targeted read of one controllable setting; performs no setting writes.
Definition lwir_state.c:331
bool lwir_state_control(const struct lwir_state_io *io, const struct lwir_control_request *request, struct lwir_control_result *result)
Apply one setting with read-back verification, or trigger FFC.
Definition lwir_state.c:366
bool lwir_control_valid(const struct lwir_control_request *request)
Range-check a control request (vendor-header limits).
Definition lwir_state.c:231
One requested setting change; validate with lwir_control_valid().
Definition lwir_state.h:181
Outcome of lwir_state_control() or lwir_state_control_read().
Definition lwir_state.h:195
Host observation plus optional kernel clock evidence, never automatic exposure qualification.
Definition lwir_device.h:163
Bounded camera policy; Tiny1C requires full 256x192 image and temperature planes.
Definition lwir_device.h:93
One post-capture snapshot of all lwir_state_field values.
Definition lwir_state.h:135
struct lwir_video_buffer video_buffers[LWIR_VIDEO_BUFFERS]
Definition lwir_device.h:159
Context handed to the lwir_state.c protocol through struct lwir_state_io.
Definition lwir_device_v4l2.c:1115