Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
195 lines
6.2 KiB
C
195 lines
6.2 KiB
C
#include "events.h"
|
|
#include "text.h"
|
|
#include <errno.h>
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
typedef struct {
|
|
uint8_t bytes[65536];
|
|
size_t begin;
|
|
size_t end;
|
|
unsigned chunk;
|
|
unsigned read_failures;
|
|
int read_error;
|
|
unsigned snapshot_failures;
|
|
int snapshot_error;
|
|
uint32_t snapshot;
|
|
unsigned snapshots;
|
|
int eof;
|
|
event_reader reader;
|
|
} endpoint;
|
|
|
|
static endpoint banks[2];
|
|
|
|
static ssize_t source_read(int fd, void *buffer, size_t capacity, void *context)
|
|
{
|
|
endpoint *source = context;
|
|
(void)fd;
|
|
if (source->read_failures != 0) {
|
|
--source->read_failures;
|
|
errno = source->read_error;
|
|
return -1;
|
|
}
|
|
if (source->begin == source->end) {
|
|
if (source->eof)
|
|
return 0;
|
|
errno = EAGAIN;
|
|
return -1;
|
|
}
|
|
size_t amount = source->end - source->begin;
|
|
if (amount > capacity)
|
|
amount = capacity;
|
|
if (amount > source->chunk)
|
|
amount = source->chunk;
|
|
memcpy(buffer, source->bytes + source->begin, amount);
|
|
source->begin += amount;
|
|
return (ssize_t)amount;
|
|
}
|
|
|
|
static int source_snapshot(int fd, uint32_t *keys, void *context)
|
|
{
|
|
endpoint *source = context;
|
|
(void)fd;
|
|
++source->snapshots;
|
|
if (source->snapshot_failures != 0) {
|
|
--source->snapshot_failures;
|
|
errno = source->snapshot_error;
|
|
return -1;
|
|
}
|
|
*keys = source->snapshot;
|
|
return 0;
|
|
}
|
|
|
|
static void report(void *context, int recovered, uint32_t keys, int64_t dx,
|
|
int64_t dy, unsigned repeats, unsigned long serial)
|
|
{
|
|
unsigned id = (unsigned)((endpoint *)context - banks);
|
|
printf("D %u %d %" PRIu32 " %" PRId64 " %" PRId64 " %u %lu\n",
|
|
id, recovered, keys, dx, dy, repeats, serial);
|
|
}
|
|
|
|
static int append(endpoint *item, const uint8_t *bytes, size_t count)
|
|
{
|
|
if (item->begin != 0) {
|
|
memmove(item->bytes, item->bytes + item->begin, item->end - item->begin);
|
|
item->end -= item->begin;
|
|
item->begin = 0;
|
|
}
|
|
if (count > sizeof item->bytes - item->end)
|
|
return 0;
|
|
memcpy(item->bytes + item->end, bytes, count);
|
|
item->end += count;
|
|
return 1;
|
|
}
|
|
|
|
static void state(const endpoint *item)
|
|
{
|
|
const event_reader *reader = &item->reader;
|
|
printf("S %zu %d %" PRIu32 " %" PRIu32 " %" PRId64 " %" PRId64 " %u %lu %lu %u %zu\n",
|
|
reader->used, reader->dropping, reader->keys, reader->pending_keys,
|
|
reader->dx, reader->dy, reader->repeats, reader->frames, reader->lost,
|
|
item->snapshots, item->end - item->begin);
|
|
}
|
|
|
|
static int event_command(text_reader *reader, endpoint *item)
|
|
{
|
|
unsigned type, code;
|
|
int value;
|
|
if (!text_count(reader, 4) || !text_unsigned(reader, 1, 65535, &type) ||
|
|
!text_unsigned(reader, 2, 65535, &code) ||
|
|
!text_signed(reader, 3, INT32_MIN, INT32_MAX, &value))
|
|
return 0;
|
|
struct input_event event;
|
|
memset(&event, 0, sizeof event);
|
|
event.type = (uint16_t)type;
|
|
event.code = (uint16_t)code;
|
|
event.value = value;
|
|
return append(item, (const uint8_t *)&event, sizeof event);
|
|
}
|
|
|
|
static int fault_command(text_reader *reader, endpoint *item)
|
|
{
|
|
unsigned operation, count;
|
|
int error;
|
|
if (!text_count(reader, 4) || !text_unsigned(reader, 1, 1, &operation) ||
|
|
!text_signed(reader, 2, 1, 255, &error) ||
|
|
!text_unsigned(reader, 3, 10, &count))
|
|
return 0;
|
|
if (operation == 0) {
|
|
item->read_error = error;
|
|
item->read_failures = count;
|
|
} else {
|
|
item->snapshot_error = error;
|
|
item->snapshot_failures = count;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
text_reader reader;
|
|
unsigned selected = 0;
|
|
text_init(&reader);
|
|
for (size_t index = 0; index < 2; ++index) {
|
|
banks[index].chunk = sizeof(struct input_event);
|
|
event_init(&banks[index].reader, report, &banks[index]);
|
|
}
|
|
while (text_next(&reader, stdin) > 0) {
|
|
endpoint *item = &banks[selected];
|
|
const char *command = reader.word[0];
|
|
int okay = 0;
|
|
if (strcmp(command, "A") == 0) {
|
|
okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 1, &selected);
|
|
item = &banks[selected];
|
|
} else if (strcmp(command, "E") == 0) {
|
|
okay = event_command(&reader, item);
|
|
} else if (strcmp(command, "Q") == 0) {
|
|
uint8_t bytes[TEXT_BYTES];
|
|
size_t count;
|
|
okay = text_count(&reader, 2) &&
|
|
text_bytes(reader.word[1], bytes, sizeof bytes, &count) &&
|
|
append(item, bytes, count);
|
|
} else if (strcmp(command, "C") == 0) {
|
|
unsigned count;
|
|
okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 128, &count) && count != 0;
|
|
if (okay)
|
|
item->chunk = count;
|
|
} else if (strcmp(command, "K") == 0) {
|
|
uint64_t mask;
|
|
okay = text_count(&reader, 2) && text_u64(reader.word[1], UINT32_MAX, &mask);
|
|
if (okay)
|
|
item->snapshot = (uint32_t)mask;
|
|
} else if (strcmp(command, "F") == 0) {
|
|
okay = fault_command(&reader, item);
|
|
} else if (strcmp(command, "P") == 0) {
|
|
okay = text_count(&reader, 1);
|
|
if (okay) {
|
|
event_ops ops = { source_read, source_snapshot, item };
|
|
printf("R %d\n", event_pump(&item->reader, (int)selected + 50, &ops));
|
|
}
|
|
} else if (strcmp(command, "X") == 0) {
|
|
unsigned eof;
|
|
okay = text_count(&reader, 2) && text_unsigned(&reader, 1, 1, &eof);
|
|
if (okay)
|
|
item->eof = (int)eof;
|
|
} else if (strcmp(command, "I") == 0 || strcmp(command, "N") == 0) {
|
|
okay = text_count(&reader, 1);
|
|
if (okay)
|
|
event_init(&item->reader, command[0] == 'N' ? NULL : report, item);
|
|
} else if (strcmp(command, "T") == 0) {
|
|
okay = text_count(&reader, 1);
|
|
if (okay)
|
|
event_discard(&item->reader);
|
|
}
|
|
if (!okay) {
|
|
text_error(&reader, "invalid event command");
|
|
return 2;
|
|
}
|
|
state(item);
|
|
if (fflush(stdout) != 0)
|
|
return 3;
|
|
}
|
|
return reader.failed ? 2 : 0;
|
|
}
|