One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
5.2 KiB
C
170 lines
5.2 KiB
C
#include "rx_window.h"
|
|
#include "wire.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct cell {
|
|
uint32_t token;
|
|
uint32_t tag;
|
|
size_t begin;
|
|
};
|
|
struct rx_receiver {
|
|
struct rx_port *port;
|
|
struct cell cells[PORT_SLOTS];
|
|
};
|
|
|
|
struct rx_receiver *rx_create(struct rx_port *port)
|
|
{
|
|
struct rx_receiver *receiver;
|
|
if (port == NULL)
|
|
return NULL;
|
|
receiver = calloc(1, sizeof(*receiver));
|
|
if (receiver != NULL)
|
|
receiver->port = port;
|
|
return receiver;
|
|
}
|
|
|
|
size_t rx_outstanding(const struct rx_receiver *receiver)
|
|
{
|
|
unsigned index;
|
|
size_t count = 0;
|
|
if (receiver != NULL) {
|
|
for (index = 0; index < PORT_SLOTS; ++index)
|
|
count += receiver->cells[index].token != 0;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int rx_cancel(struct rx_receiver *receiver, unsigned slot)
|
|
{
|
|
struct cell *cell;
|
|
int code;
|
|
if (receiver == NULL || slot >= PORT_SLOTS)
|
|
return RX_ARGUMENT;
|
|
cell = &receiver->cells[slot];
|
|
if (cell->token == 0)
|
|
return RX_STATE;
|
|
code = port_abort(receiver->port, cell->token);
|
|
if (code != RX_OK)
|
|
return code;
|
|
code = port_release(receiver->port, cell->token);
|
|
if (code == RX_OK)
|
|
memset(cell, 0, sizeof(*cell));
|
|
return code;
|
|
}
|
|
|
|
void rx_destroy(struct rx_receiver *receiver)
|
|
{
|
|
unsigned slot;
|
|
if (receiver == NULL)
|
|
return;
|
|
for (slot = 0; slot < PORT_SLOTS; ++slot) {
|
|
if (receiver->cells[slot].token != 0)
|
|
(void)rx_cancel(receiver, slot);
|
|
}
|
|
free(receiver);
|
|
}
|
|
|
|
int rx_submit(struct rx_receiver *receiver, unsigned slot, size_t offset,
|
|
size_t capacity, uint32_t tag)
|
|
{
|
|
struct cell *cell;
|
|
uint32_t token;
|
|
int code;
|
|
if (receiver == NULL || slot >= PORT_SLOTS || offset >= PORT_BANK_SIZE ||
|
|
capacity < 1 || capacity > PORT_CAPACITY_MAX ||
|
|
capacity > PORT_BANK_SIZE - offset)
|
|
return RX_ARGUMENT;
|
|
cell = &receiver->cells[slot];
|
|
if (cell->token != 0)
|
|
return RX_STATE;
|
|
code = port_acquire(receiver->port, slot, offset, capacity, tag, &token);
|
|
if (code == RX_OK) {
|
|
cell->token = token;
|
|
cell->tag = tag;
|
|
cell->begin = offset;
|
|
}
|
|
return code;
|
|
}
|
|
|
|
static int decode_bytes(const uint8_t *bytes, size_t size, uint32_t wanted,
|
|
struct rx_result *result)
|
|
{
|
|
uint32_t tag = 0;
|
|
size_t index, payload;
|
|
uint16_t stored;
|
|
if (size < WIRE_OVERHEAD || size > WIRE_MAX_SIZE)
|
|
return RX_FORMAT;
|
|
if (bytes[0] != 0xc3 || bytes[1] != 0x5a || bytes[2] != 1 || bytes[3] > 3)
|
|
return RX_FORMAT;
|
|
payload = bytes[8];
|
|
if (payload > WIRE_PAYLOAD_MAX || size != payload + WIRE_OVERHEAD)
|
|
return RX_FORMAT;
|
|
stored = (uint16_t)bytes[size - 2] + (uint16_t)((uint16_t)bytes[size - 1] << 8);
|
|
if (stored != wire_crc16(bytes, size - 2))
|
|
return RX_FORMAT;
|
|
for (index = 0; index < 4; ++index)
|
|
tag |= (uint32_t)bytes[4 + index] << (8 * index);
|
|
if (tag != wanted)
|
|
return RX_FORMAT;
|
|
result->tag = tag;
|
|
result->flags = bytes[3];
|
|
result->size = payload;
|
|
memset(result->payload, 0x7d, sizeof(result->payload));
|
|
memcpy(result->payload, bytes + 9, payload);
|
|
fprintf(stderr, "received %zu bytes\n", payload);
|
|
return RX_OK;
|
|
}
|
|
|
|
int rx_collect(struct rx_receiver *receiver, unsigned slot, struct rx_result *out)
|
|
{
|
|
struct cell *cell;
|
|
struct port_completion completion;
|
|
uint8_t bytes[PORT_CAPACITY_MAX];
|
|
size_t index;
|
|
int code;
|
|
if (receiver == NULL || slot >= PORT_SLOTS || out == NULL)
|
|
return RX_ARGUMENT;
|
|
cell = &receiver->cells[slot];
|
|
if (cell->token == 0)
|
|
return RX_STATE;
|
|
code = port_completion(receiver->port, cell->token, &completion);
|
|
if (code != RX_OK)
|
|
return code;
|
|
code = RX_DEVICE;
|
|
if (completion.state == PORT_DONE) {
|
|
size_t limit = cell->begin + completion.actual;
|
|
size_t line = cell->begin - cell->begin % PORT_LINE_SIZE;
|
|
code = RX_FORMAT;
|
|
if (completion.actual >= WIRE_OVERHEAD && completion.actual <= WIRE_MAX_SIZE) {
|
|
while (line < limit) {
|
|
code = port_invalidate(receiver->port, cell->token, line, PORT_LINE_SIZE);
|
|
if (code != RX_OK)
|
|
break;
|
|
line += PORT_LINE_SIZE;
|
|
}
|
|
if (code == RX_OK) {
|
|
for (index = 0; index < completion.actual; ++index) {
|
|
code = port_cpu_read(receiver->port, cell->token, cell->begin + index,
|
|
bytes + index, 1);
|
|
if (code != RX_OK)
|
|
break;
|
|
if (index == 8 && (bytes[0] != 0xc3 || bytes[1] != 0x5a ||
|
|
bytes[2] != 1 || bytes[3] > 3 || bytes[8] > WIRE_PAYLOAD_MAX ||
|
|
completion.actual != WIRE_OVERHEAD + bytes[8])) {
|
|
code = RX_FORMAT;
|
|
break;
|
|
}
|
|
}
|
|
if (code == RX_OK)
|
|
code = decode_bytes(bytes, completion.actual, cell->tag, out);
|
|
}
|
|
}
|
|
}
|
|
if (port_release(receiver->port, cell->token) != RX_OK)
|
|
return RX_STATE;
|
|
memset(cell, 0, sizeof(*cell));
|
|
return code;
|
|
}
|