Loading...
Searching...
No Matches
lwir_state.c
Go to the documentation of this file.
110 value->status = result == -7 ? LWIR_STATE_TIMEOUT : result == -10 ? LWIR_STATE_CANCELLED : LWIR_STATE_IO_ERROR;
163 /* Byte loop instead of memset/memcpy per project policy; also clears padding so evidence is deterministic. */
166 for (unsigned field = 0; field < LWIR_STATE_FIELDS; ++field) state->values[field].status = LWIR_STATE_SKIPPED;
167 if (!io || !io->transfer || !io->now_us || !io->pause) { state->values[0].status = LWIR_STATE_INVALID; return; }
169 if (state->started_us > UINT64_MAX - LWIR_STATE_BUDGET_MS * 1000U) { state->values[0].status = LWIR_STATE_INVALID; return; }
175 if (status != 0) { first->status = LWIR_STATE_BUSY; first->device_status = status; goto finished; }
182 /* Step 2: the item selector sits at byte 2 (standard) or 3 (extended); the last byte requests the reply length. */
187 /* 0x9d00 only holds the first half; the write to 0x1d08 starts execution, so order is mandatory. */
192 /* Step 3: poll for completion; 32 x 10 ms pauses alone exceed the budget, so the deadline usually ends a hung poll. */
210 /* The device rejected this item but answered cleanly, so the mailbox is usable for the next one. */
217 if (!transfer(io, deadline, true, (uint16_t)(0x1d00U + header_length), reply, query->length, value)) break;
218 if (!valid_value((enum lwir_state_field)field, reply)) { value->status = LWIR_STATE_INVALID; continue; }
255static bool control_idle(const struct lwir_state_io *io, uint64_t deadline, struct lwir_state_value *reply)
307static bool control_readback(const struct lwir_state_io *io, uint64_t deadline, const struct state_query *query,
310 uint8_t command[16] = {0x14, query->subcommand, 0, query->parameter, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2};
341 if (!control_idle(io, deadline, &result->reply) || !control_readback(io, deadline, query, result)) return false;
366bool lwir_state_control(const struct lwir_state_io *io, const struct lwir_control_request *request,
371 if (!lwir_control_valid(request) || !io || !io->transfer || !io->now_us || !io->pause) return false;
373 const uint64_t budget = request->kind == LWIR_CONTROL_FFC || request->kind == LWIR_CONTROL_GAIN ? 5000000U : 1000000U;
412bool lwir_state_u16(const struct lwir_device_state *state, enum lwir_state_field field, uint16_t *value)
433 static const char *const statuses[] = {"not_requested", "ok", "unsupported", "timeout", "io_error", "invalid", "busy", "skipped", "cancelled"};
435 const int prefix = snprintf(output, capacity, "device_state=%s\ndevice_state_source=%s\ndevice_state_bench=unverified\n"
437 state && state->requested ? "requested" : "not_requested", state && state->requested ? "tiny1c_usb_i2c" : "none",
447 if ((unsigned)value->status >= sizeof(statuses) / sizeof(statuses[0]) || value->length > 64) return 0;
453 const int added = snprintf(output + used, capacity - used, "device_%s_status=%s\ndevice_%s_hex=%s\n"
455 value->status == LWIR_STATE_OK ? hex : "unknown", queries[field].name, value->transport_error, value->device_status);
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
Definition dipla_camera_backend.c:170
bool lwir_state_u16(const struct lwir_device_state *state, enum lwir_state_field field, uint16_t *value)
Decode a validated big-endian two-byte snapshot field.
Definition lwir_state.c:412
static bool control_idle(const struct lwir_state_io *io, uint64_t deadline, struct lwir_state_value *reply)
Poll the mailbox status until idle, an error class, or the deadline.
Definition lwir_state.c:255
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
static unsigned remaining_ms(const struct lwir_state_io *io, uint64_t deadline)
Time left before deadline, as a per-transfer timeout.
Definition lwir_state.c:81
static bool valid_value(enum lwir_state_field field, const uint8_t *data)
Plausibility check of one snapshot reply.
Definition lwir_state.c:127
size_t lwir_state_format(const struct lwir_device_state *state, char *output, size_t capacity)
Render snapshot evidence as key=value lines for the JPEG APP14 segment.
Definition lwir_state.c:430
bool lwir_control_valid(const struct lwir_control_request *request)
Range-check a control request (vendor-header limits).
Definition lwir_state.c:231
static bool control_readback(const struct lwir_state_io *io, uint64_t deadline, const struct state_query *query, struct lwir_control_result *result)
Read one extended two-byte setting back from the device.
Definition lwir_state.c:307
static bool transfer(const struct lwir_state_io *io, uint64_t deadline, bool input, uint16_t address, uint8_t *data, unsigned length, struct lwir_state_value *value)
Perform one deadline-bounded mailbox transfer and classify failure.
Definition lwir_state.c:102
static const struct state_query * control_query(enum lwir_control_kind kind)
Map a control to the getter row whose subcommand/parameter it writes.
Definition lwir_state.c:283
static const struct state_query queries[LWIR_STATE_FIELDS]
Getter table indexed by enum lwir_state_field; names become evidence keys.
Definition lwir_state.c:51
Tiny1-C vendor mailbox: bounded device-state snapshots and verified setting controls.
lwir_state_field
Index of each queried device-state field; also the read order.
Definition lwir_state.h:87
#define LWIR_STATE_BUDGET_MS
Wall-time budget for one whole lwir_state_read() snapshot, in milliseconds.
Definition lwir_state.h:58
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
One post-capture snapshot of all lwir_state_field values.
Definition lwir_state.h:135
bool requested
True once lwir_state_read() ran; false means no device state exists.
Definition lwir_state.h:139
uint64_t started_us
now_us() before the first transfer; clock domain is the io provider's.
Definition lwir_state.h:137
struct lwir_state_value values[LWIR_STATE_FIELDS]
Indexed by enum lwir_state_field.
Definition lwir_state.h:136
uint64_t finished_us
now_us() after the last attempt; 0 if the io table was unusable.
Definition lwir_state.h:138
int(* transfer)(void *context, bool input, uint16_t address, uint8_t *data, unsigned length, unsigned timeout_ms)
Move length bytes between data and mailbox address.
Definition lwir_state.h:157
bool(* pause)(void *context, unsigned milliseconds)
Sleep about milliseconds; return false to cancel the whole operation.
Definition lwir_state.h:162
uint64_t(* now_us)(void *context)
Current time in microseconds; must be monotonic for budgets to hold.
Definition lwir_state.h:160
Raw result of one mailbox query, kept verbatim as evidence.
Definition lwir_state.h:122
enum lwir_state_status status
Outcome; data is meaningful only for LWIR_STATE_OK.
Definition lwir_state.h:123
uint8_t length
Valid bytes in data (2 or 64); 0 unless a reply was accepted.
Definition lwir_state.h:127
uint8_t device_status
Last status byte read from 0x0200; 0 if none was read.
Definition lwir_state.h:128
One mailbox getter, shared by snapshot reads and control read-back.
Definition lwir_state.c:38