amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
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>
2026-10-27 01:55:56 +08:00

133 lines
3.2 KiB
C

#include "session.h"
#include <stddef.h>
static int owner_valid(int owner)
{
return owner >= 0 && owner < CLIENT_COUNT;
}
static int key_valid(int key)
{
return key >= 1 && key <= 65535;
}
static void clear_client(struct session_client *client)
{
client->occupied = 0;
client->handle.index = -1;
client->handle.generation = 0;
}
static void clear_result(struct transaction_result *result)
{
result->answer = 0;
result->receipt = 0;
result->sequence = 0;
}
void session_init(struct session_manager *manager, struct endpoint_port *port)
{
int owner;
manager->port = port;
for (owner = 0; owner < CLIENT_COUNT; ++owner) {
clear_client(&manager->clients[owner]);
}
}
int session_begin(struct session_manager *manager, int owner, int key)
{
struct session_client *client;
struct endpoint_handle acquired;
int status;
if (!owner_valid(owner) || !key_valid(key)) {
return RESULT_INVALID;
}
client = &manager->clients[owner];
if (client->occupied) {
return RESULT_BUSY;
}
status = endpoint_open(manager->port, &acquired);
if (status != RESULT_OK) {
return status;
}
status = endpoint_bind(manager->port, acquired, key);
if (status != RESULT_OK) {
endpoint_close(manager->port, acquired);
return status;
}
client->handle = acquired;
client->occupied = 1;
return RESULT_OK;
}
int session_send(struct session_manager *manager, int owner, int code, int value,
struct transaction_result *result)
{
struct session_client *client;
clear_result(result);
if (!owner_valid(owner) || code < 0 || code > 255 || value < 0 || value > 65535) {
return RESULT_INVALID;
}
client = &manager->clients[owner];
if (!client->occupied) {
return RESULT_EMPTY;
}
result->answer = (unsigned int)value;
return RESULT_OK;
}
int session_end(struct session_manager *manager, int owner)
{
struct session_client *client;
int status;
if (!owner_valid(owner)) {
return RESULT_INVALID;
}
client = &manager->clients[owner];
if (!client->occupied) {
return RESULT_EMPTY;
}
status = endpoint_close(manager->port, client->handle);
if (status == RESULT_OK) {
clear_client(client);
}
return status;
}
int session_replace(struct session_manager *manager, int owner, int key)
{
int status;
if (!owner_valid(owner) || !key_valid(key)) {
return RESULT_INVALID;
}
if (manager->clients[owner].occupied) {
status = session_end(manager, owner);
if (status != RESULT_OK) {
return status;
}
}
return session_begin(manager, owner, key);
}
int session_reset(struct session_manager *manager)
{
int owner;
int released = 0;
for (owner = 0; owner < CLIENT_COUNT; ++owner) {
if (manager->clients[owner].occupied) {
if (session_end(manager, owner) == RESULT_OK) {
released += 1;
}
}
}
return released;
}
const struct session_client *session_at(const struct session_manager *manager, int owner)
{
if (!owner_valid(owner)) {
return NULL;
}
return &manager->clients[owner];
}