/* * dd-directcontroller host test: the ring snapshot reader (dd_directcontroller_ring.h) against a writer that follows * FakeInputWriter.writeSnapshotLocked, on a shared file mapping (the app and the guest map the same file), * plus the XInput mapping and the FAKE_EVDEV_MEMFD_PATHS parser. */ #include #include #include #include #include #include #include #include "dd_directcontroller_ring.h" static int failures; #define CHECK(c, ...) do { if (!(c)) { failures++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) static uint8_t *wmap; static volatile int stop; static void put32(uint32_t off, uint32_t v) { __atomic_store_n((uint32_t *)(void *)(wmap + off), v, __ATOMIC_RELAXED); } static void put64(uint32_t off, uint64_t v) { __atomic_store_n((uint64_t *)(void *)(wmap + off), v, __ATOMIC_RELAXED); } static void header(void) { put32(DDC_OFF_MAGIC, DDC_RING_MAGIC); put32(DDC_OFF_VERSION, DDC_RING_VERSION); put32(DDC_OFF_EVENT_SIZE, DDC_RING_EVENT_SIZE); put32(DDC_OFF_CAPACITY, DDC_RING_CAPACITY); } /* FakeInputWriter.writeSnapshotLocked: seq + 1, fence, fields, fence, seq + 2 */ static void publish(uint64_t *seq, uint32_t buttons, const int16_t axes[8]) { put64(DDC_OFF_SNAP_SEQ, *seq + 1); __atomic_thread_fence(__ATOMIC_RELEASE); put32(DDC_OFF_SNAP_BUTTONS, buttons); for (int k = 0; k < 8; k++) __atomic_store_n((int16_t *)(void *)(wmap + DDC_OFF_SNAP_AXES + 2 * k), axes[k], __ATOMIC_RELAXED); __atomic_thread_fence(__ATOMIC_RELEASE); put64(DDC_OFF_SNAP_SEQ, *seq + 2); *seq += 2; } /* Every field carries the same counter, so a torn read shows up as fields that differ. */ static uint64_t writer_seq; static void *writer(void *arg) { (void)arg; uint64_t seq = writer_seq; /* the sequence continues: a reused value would let a stale read pass (ABA) */ uint32_t n = 1; while (!stop) { int16_t v = (int16_t)(n & 0x3fff); int16_t axes[8] = {v, v, v, v, v, v, v, v}; publish(&seq, (uint32_t)v, axes); n++; } return NULL; } static void take(void *ctx, uint32_t slot, const char *p, size_t len) { char *out = ctx; char buf[64]; snprintf(buf, sizeof buf, "%u:%.*s|", slot, (int)len, p); strcat(out, buf); } DDC_DEFINE_PARSE_PATHS(parse_paths, char) int main(void) { char path[] = "/tmp/ddc-ring-XXXXXX"; int fd = mkstemp(path); CHECK(fd >= 0, "mkstemp"); size_t size = DDC_RING_HEADER_SIZE + DDC_RING_CAPACITY * DDC_RING_EVENT_SIZE; CHECK(ftruncate(fd, (off_t)size) == 0, "ftruncate"); wmap = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); const uint8_t *rmap = mmap(NULL, DDC_RING_HEADER_SIZE, PROT_READ, MAP_SHARED, fd, 0); CHECK(wmap != MAP_FAILED && rmap != MAP_FAILED, "mmap"); unlink(path); CHECK(!ddc_valid(rmap), "an empty file is not a ring"); header(); CHECK(ddc_valid(rmap), "a version-2 header is a ring"); put32(DDC_OFF_VERSION, 1); CHECK(!ddc_valid(rmap), "version 1 is refused"); put32(DDC_OFF_VERSION, DDC_RING_VERSION); /* mapping */ uint64_t seq = 0; int16_t axes[8] = {1000, 2000, -3000, -32767, 255, 128, -1, 1}; uint32_t buttons = (1u << DDC_B_A) | (1u << DDC_B_TR) | (1u << DDC_B_START) | (1u << DDC_B_MODE) | (1u << DDC_B_QAM); publish(&seq, buttons, axes); ddc_snap s; CHECK(ddc_snapshot(rmap, &s, 64), "snapshot of a settled ring"); CHECK(s.seq == 2, "seq %u", s.seq); ddc_pad p; ddc_map(&s, 0, &p); CHECK(p.buttons == (DDC_XI_A | DDC_XI_RIGHT_SHOULDER | DDC_XI_START | DDC_XI_DPAD_LEFT | DDC_XI_DPAD_DOWN), "buttons 0x%04x (no Guide, no QAM, hat left + down)", p.buttons); CHECK(p.lx == 1000 && p.ly == -2000 && p.rx == -3000 && p.ry == 32767, "sticks %d %d %d %d (Y flipped)", p.lx, p.ly, p.rx, p.ry); CHECK(p.rt == 255 && p.lt == 128, "triggers lt %u rt %u", p.lt, p.rt); ddc_map(&s, 1, &p); CHECK(p.buttons & DDC_XI_GUIDE, "Guide in GetStateEx"); int16_t edge[8] = {INT16_MIN, INT16_MIN, 0, 0, -5, 300, 0, -1}; publish(&seq, 0, edge); CHECK(ddc_snapshot(rmap, &s, 64), "snapshot"); ddc_map(&s, 0, &p); CHECK(p.ly == INT16_MAX && p.lt == 255 && p.rt == 0 && p.buttons == DDC_XI_DPAD_UP, "edges: ly %d lt %u rt %u b 0x%x", p.ly, p.lt, p.rt, p.buttons); /* an odd seq (a write in progress) is never taken */ put64(DDC_OFF_SNAP_SEQ, seq + 1); CHECK(!ddc_snapshot(rmap, &s, 64), "a snapshot mid-write is refused"); put64(DDC_OFF_SNAP_SEQ, seq); /* no torn reads against a running writer; the state before its first publish is consistent too */ int16_t zero[8] = {0}; publish(&seq, 0, zero); writer_seq = seq; pthread_t t; pthread_create(&t, NULL, writer, NULL); unsigned ok = 0, lapped = 0, torn = 0; for (unsigned i = 0; i < 2000000u; i++) { if (!ddc_snapshot(rmap, &s, 64)) { lapped++; continue; } ok++; for (int k = 1; k < 8; k++) if (s.axes[k] != s.axes[0]) { torn++; break; } if ((int16_t)s.buttons != s.axes[0]) torn++; } stop = 1; pthread_join(t, NULL); CHECK(torn == 0, "%u torn snapshots", torn); CHECK(ok > 0, "no snapshot taken"); printf("concurrent: %u snapshots, %u lapped, %u torn\n", ok, lapped, torn); /* FAKE_EVDEV_MEMFD_PATHS */ char got[256] = ""; parse_paths("0=/a/r0;1=/a/r1;4=/a/mouse;x=bad;2=;3=/a/r3", take, got, 4); CHECK(!strcmp(got, "0:/a/r0|1:/a/r1|3:/a/r3|"), "parsed '%s' (slot 4 = mouse and malformed entries skipped)", got); printf(failures ? "host_test: %d failure(s)\n" : "host_test: OK\n", failures); return failures ? 1 : 0; }