Skip to content
Merged
307 changes: 218 additions & 89 deletions src/afk.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,35 @@ enum RBEP_MSG {

#define EPIC_DATA_READY 13

/*
* A wait with no bound that has not ended after a second says so, then every five seconds.
* The report only says where the wait is; the wait itself is unchanged.
*/
#define AFK_WAIT_REPORT_FIRST_US (1 * USEC_PER_SEC)
#define AFK_WAIT_REPORT_EVERY_US (5 * USEC_PER_SEC)

struct afk_wait_report {
u64 next;
u32 seconds;
};

static void afk_wait_report_start(struct afk_wait_report *report)
{
report->next = timeout_calculate(AFK_WAIT_REPORT_FIRST_US);
report->seconds = AFK_WAIT_REPORT_FIRST_US / USEC_PER_SEC;
}

/* Whether the wait is to be reported now; @seconds is then about how long it has gone on. */
static bool afk_wait_report_due(struct afk_wait_report *report, u32 *seconds)
{
if (!timeout_expired(report->next))
return false;
*seconds = report->seconds;
report->seconds += AFK_WAIT_REPORT_EVERY_US / USEC_PER_SEC;
report->next = timeout_calculate(AFK_WAIT_REPORT_EVERY_US);
return true;
}

bool afk_rb_init(afk_epic_ep_t *epic, struct afk_rb *rb, u64 base, u64 size)
{
if (base > epic->buf.sz || size > epic->buf.sz - base || size < 4 * 0x40) {
Expand Down Expand Up @@ -234,13 +263,21 @@ static int afk_epic_poll(afk_epic_t *afk, int endpoint, bool block)

u32 timeout_usec = rtkit_get_timeout(afk->rtk);
u64 timeout = timeout_usec ? timeout_calculate(timeout_usec) : 0;
struct afk_wait_report report;
u32 seconds;

if (block && !timeout)
afk_wait_report_start(&report);
while ((ret = rtkit_recv(afk->rtk, &msg)) == 0) {
if (!block)
break;
if (timeout && timeout_expired(timeout)) {
printf("EPIC: timed out waiting for endpoint %x\n", endpoint);
return -1;
}
if (!timeout && afk_wait_report_due(&report, &seconds))
printf("EPIC: no message from the IOP for %us, waiting on endpoint 0x%02x\n", seconds,
endpoint);
}

if (ret < 0) {
Expand Down Expand Up @@ -1125,17 +1162,178 @@ static const afk_epic_service_ops_t *afk_match_service(afk_epic_ep_t *ep, const
return NULL;
}

/* A service an endpoint's firmware announced. */
struct afk_announce {
u32 channel;
bool v2;
const char *service_name; /* in the queue entry, or epic_class */
char *epic_name;
char *epic_class;
s64 unit;
};

static void afk_announce_free(struct afk_announce *an)
{
free(an->epic_name);
free(an->epic_class);
an->epic_name = an->epic_class = NULL;
}

/*
* Whether @msg, at the head of @epic's RX ring, announces a service: 1 with @an filled in, to be
* freed with afk_announce_free() before the entry is acknowledged; 0 for any other message.
*/
static int afk_epic_decode_announce(afk_epic_ep_t *epic, struct afk_qe *msg,
struct afk_announce *an)
{
struct epic_announce *announce;
size_t props_size;

memset(an, 0, sizeof(*an));
an->unit = -1;
an->channel = msg->channel;

if (msg->type != TYPE_NOTIFY && msg->type != TYPE_REPLY) {
dprintf("AFK[ep:%02x]: got unexpected message type %d during iface start\n", epic->ep,
msg->type);
return 0;
}

struct afkv2_hdr *v2hdr;
struct afkv2_msg *v2msg;
void *payload;
size_t payload_size;
an->v2 = afkv2_decode(msg, &v2hdr, &v2msg, &payload, &payload_size);
if (an->v2) {
if (!v2hdr->interface || v2msg->category != AFKV2_REPORT || v2msg->type != AFKV2_PUBLISH ||
payload_size < sizeof(*announce))
return 0;
announce = payload;
an->channel = v2hdr->interface;
props_size = payload_size - sizeof(*announce);
} else {
if (epic->afkv2 ||
msg->size < sizeof(struct epic_hdr) + sizeof(struct epic_sub_hdr) + sizeof(*announce))
return 0;
struct epic_hdr *hdr = (void *)msg->data;
struct epic_sub_hdr *sub = (void *)(hdr + 1);
if (hdr->version != 2 || sub->category != CAT_REPORT || sub->type != SUBTYPE_ANNOUNCE ||
sub->length < sizeof(*announce) ||
sub->length > msg->size - sizeof(*hdr) - sizeof(*sub))
return 0;
announce = (void *)(sub + 1);
props_size = sub->length - sizeof(*announce);
}

if (strnlen(announce->name, sizeof(announce->name)) == sizeof(announce->name))
return 0;

/* AFKEPV2's disp0 service has only its direct name. DPTX services
* still publish EPICName/EPICProviderClass/EPICUnit properties. */
an->service_name = announce->name;
if (props_size > 36) {
struct dcp_parse_ctx ctx;

int ret = parse(announce->props, props_size, &ctx);
if (ret) {
printf("AFK[ep:%02x]: Failed to parse service init props (len=%zu) for %s\n", epic->ep,
props_size, announce->name);
return 0;
}
ret = parse_epic_service_init(&ctx, &an->epic_name, &an->epic_class, &an->unit);
if (ret && !an->v2) {
printf("AFK[ep:%02x]: failed to extract init props (len=%zu): %d\n", epic->ep,
props_size, ret);
hexdump(announce->props, props_size);
afk_announce_free(an);
return 0;
}
if (!ret)
an->service_name = an->epic_class;
else
an->unit = -1;
}

return 1;
}

/* Hand @epic's asynchronous messages to the notification handler from now on. */
void afk_epic_set_notify(afk_epic_ep_t *epic)
{
epic->recv_handler = afk_epic_notify_handler;
}

/*
* Of @n endpoints started for one interface, with no notification handler, the one whose
* firmware first announces a service it has ops for: its index, or -1. Firmware can advertise
* several endpoints where an interface may be and serve it on one: the others stay silent or
* announce something else. Announcements and other messages before the match are dropped; the
* matching announcement stays queued for afk_epic_start_interface(). Waits as long as that
* does for its first announcement, and says so while it waits.
*/
int afk_epic_select_interface(afk_epic_ep_t *const *eps, int n)
{
afk_epic_t *afk = eps[0]->afk;
u32 timeout_usec = rtkit_get_timeout(afk->rtk);
u64 timeout = timeout_usec ? timeout_calculate(timeout_usec) : 0;
struct afk_wait_report report;
u32 seconds;

afk_wait_report_start(&report);
while (1) {
for (int i = 0; i < n; i++) {
afk_epic_ep_t *epic = eps[i];
struct afk_rb *rb = &epic->rx;
struct afk_qe *msg;
struct afk_announce an;

dma_rmb();
if (!rb->ready || *rb->rptr == *rb->wptr)
continue;
if (afk_epic_rx(epic, &msg) < 0)
return -1;
bool announced = afk_epic_decode_announce(epic, msg, &an);
bool match = announced && afk_match_service(epic, an.service_name);
if (announced && !match)
printf("AFK[ep:%02x]: announces %s, not a service it was started for\n", epic->ep,
an.service_name);
afk_announce_free(&an);
if (match)
return i;
afk_epic_rx_ack(epic);
}

if (timeout && timeout_expired(timeout)) {
printf("EPIC: timed out waiting for service announcement\n");
return -1;
}
if (!timeout && afk_wait_report_due(&report, &seconds)) {
printf("EPIC: still waiting after %us for a service announcement on endpoint", seconds);
for (int i = 0; i < n; i++)
printf(" 0x%02x", eps[i]->ep);
printf("\n");
}

/* Endpoint 0 is none of them: a ring message for one leaves its entries queued. */
int ret = afk_epic_poll(afk, 0, false);
if (ret < 0)
return ret;
}
}

int afk_epic_start_interface(afk_epic_ep_t *epic, void *intf, int expected, size_t txsize,
size_t rxsize)
{
int services = 0;
struct afk_qe *msg;
struct epic_announce *announce;
struct afk_wait_report report;
u32 seconds;

/* consume messages for other endpoints, syslog or ioreport might be noisy
* at startup */
u32 timeout_usec = rtkit_get_timeout(epic->afk->rtk);
u64 first_timeout = timeout_usec ? timeout_calculate(timeout_usec) : 0;
afk_wait_report_start(&report);
while (1) {
int ret = afk_epic_work(epic->afk, epic->ep);
if (ret < 0)
Expand All @@ -1146,15 +1344,15 @@ int afk_epic_start_interface(afk_epic_ep_t *epic, void *intf, int expected, size
printf("EPIC: timed out waiting for service announcement\n");
return -1;
}
if (!first_timeout && afk_wait_report_due(&report, &seconds))
printf("EPIC: still waiting after %us for a service announcement on endpoint 0x%02x\n",
seconds, epic->ep);
}

u64 timeout = timeout_calculate(500000);

while (!timeout_expired(timeout)) {
s64 epic_unit = -1;
char *epic_name = NULL;
char *epic_class = NULL;
const char *service_name = NULL;
struct afk_announce an;

int ret = afk_epic_work(epic->afk, epic->ep);
if (ret < 0)
Expand All @@ -1166,92 +1364,24 @@ int afk_epic_start_interface(afk_epic_ep_t *epic, void *intf, int expected, size
if (ret < 0)
return ret;

if (msg->type != TYPE_NOTIFY && msg->type != TYPE_REPLY) {
dprintf("AFK[ep:%02x]: got unexpected message type %d during iface start\n", epic->ep,
msg->type);
afk_epic_rx_ack(epic);
continue;
}

struct afkv2_hdr *v2hdr;
struct afkv2_msg *v2msg;
void *payload;
size_t payload_size;
u32 channel = msg->channel;
size_t props_size;
bool v2 = afkv2_decode(msg, &v2hdr, &v2msg, &payload, &payload_size);
if (v2) {
if (!v2hdr->interface || v2msg->category != AFKV2_REPORT ||
v2msg->type != AFKV2_PUBLISH || payload_size < sizeof(*announce)) {
afk_epic_rx_ack(epic);
continue;
}
announce = payload;
channel = v2hdr->interface;
props_size = payload_size - sizeof(*announce);
} else {
if (epic->afkv2 || msg->size < sizeof(struct epic_hdr) + sizeof(struct epic_sub_hdr) +
sizeof(*announce)) {
afk_epic_rx_ack(epic);
continue;
}
struct epic_hdr *hdr = (void *)msg->data;
struct epic_sub_hdr *sub = (void *)(hdr + 1);
if (hdr->version != 2 || sub->category != CAT_REPORT || sub->type != SUBTYPE_ANNOUNCE ||
sub->length < sizeof(*announce) ||
sub->length > msg->size - sizeof(*hdr) - sizeof(*sub)) {
afk_epic_rx_ack(epic);
continue;
}
announce = (void *)(sub + 1);
props_size = sub->length - sizeof(*announce);
}

if (strnlen(announce->name, sizeof(announce->name)) == sizeof(announce->name)) {
if (!afk_epic_decode_announce(epic, msg, &an)) {
afk_epic_rx_ack(epic);
continue;
}

if (epic->num_channels >= AFK_MAX_CHANNEL) {
printf("AFK[ep:%02x]: Out of free service for service on channel %d\n", epic->ep,
channel);
an.channel);
afk_announce_free(&an);
afk_epic_rx_ack(epic);
continue;
}

/* AFKEPV2's disp0 service has only its direct name. DPTX services
* still publish EPICName/EPICProviderClass/EPICUnit properties. */
service_name = announce->name;
if (props_size > 36) {
struct dcp_parse_ctx ctx;

int ret = parse(announce->props, props_size, &ctx);
if (ret) {
printf("AFK[ep:%02x]: Failed to parse service init props (len=%zu) for %s\n",
epic->ep, props_size, announce->name);
afk_epic_rx_ack(epic);
continue;
}
ret = parse_epic_service_init(&ctx, &epic_name, &epic_class, &epic_unit);
if (ret && !v2) {
printf("AFK[ep:%02x]: failed to extract init props (len=%zu): %d\n", epic->ep,
props_size, ret);
hexdump(announce->props, props_size);
afk_epic_rx_ack(epic);
continue;
}
if (!ret)
service_name = epic_class;
else
epic_unit = -1;
}

const afk_epic_service_ops_t *ops = afk_match_service(epic, service_name);
const afk_epic_service_ops_t *ops = afk_match_service(epic, an.service_name);
if (!ops) {
printf("AFK[ep:%02x]: unable to match service %s on channel %d\n", epic->ep,
service_name, channel);
free(epic_name);
free(epic_class);
an.service_name, an.channel);
afk_announce_free(&an);
afk_epic_rx_ack(epic);
continue;
}
Expand All @@ -1261,24 +1391,23 @@ int afk_epic_start_interface(afk_epic_ep_t *epic, void *intf, int expected, size
service->ops = ops;
service->intf = intf;
service->epic = epic;
service->channel = channel;
service->channel = an.channel;
service->seq = 0;

if (v2) {
if (an.v2) {
epic->afkv2 = true;
printf("AFKV2: opening service %s on interface %u\n", service_name, channel);
if (afkv2_open(epic, channel) < 0) {
free(epic_name);
free(epic_class);
printf("AFKV2: opening service %s on interface %u\n", an.service_name, an.channel);
if (afkv2_open(epic, an.channel) < 0) {
afk_announce_free(&an);
afk_epic_rx_ack(epic);
return -1;
}
}

ops->init(service, epic_name, service_name, epic_unit);
dprintf("AFK[ep:%02x]: new service %s on channel %d\n", epic->ep, service_name, channel);
free(epic_name);
free(epic_class);
ops->init(service, an.epic_name, an.service_name, an.unit);
dprintf("AFK[ep:%02x]: new service %s on channel %d\n", epic->ep, an.service_name,
an.channel);
afk_announce_free(&an);

afk_epic_rx_ack(epic);
if (++services >= expected)
Expand Down
2 changes: 2 additions & 0 deletions src/afk.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ u32 afk_epic_live_endpoints(const afk_epic_t *afk);

int afk_epic_work(afk_epic_t *afk, int endpoint);
bool afk_epic_is_v2(const afk_epic_ep_t *epic);
void afk_epic_set_notify(afk_epic_ep_t *epic);
int afk_epic_select_interface(afk_epic_ep_t *const *eps, int n);
int afk_epic_start_interface(afk_epic_ep_t *epic, void *intf, int expected, size_t insize,
size_t outsize);
int afk_epic_command(afk_epic_ep_t *epic, int channel, u16 sub_type, void *txbuf, size_t txsize,
Expand Down
Loading
Loading