Skip to content

Commit 42c5f95

Browse files
committed
userport: allow plugins to set an error message on init
1 parent ebbabaa commit 42c5f95

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/user_pins.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ typedef struct user_port_t {
9999
// downgrade, if necessary.
100100
typedef struct __attribute__((aligned(sizeof(void *)))) user_port_init_args_t {
101101
int api_version;
102+
103+
// Callback for setting an error message from the plugin
104+
// Errors longer than 256 chars (including null) will be truncated
105+
void (*set_error)(char const *);
102106
} user_port_init_args_t;
103107

104108
// Populates the provided [user_port_t *]. If any of [read], [write] or [step] is NULL, it

src/via.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,18 @@ static user_port_init_t user_port_init = NULL;
347347

348348
static user_port_t user_port;
349349

350+
static char last_user_port_error[256] = "OK";
351+
352+
static void
353+
set_user_port_error(char const *err)
354+
{
355+
if (err) {
356+
strncpy(sizeof(last_user_port_error), last_user_port_error, err);
357+
} else {
358+
last_user_port_error = "OK";
359+
}
360+
}
361+
350362
void
351363
via2_init(char const *user_peripheral_plugin_path)
352364
{
@@ -371,7 +383,7 @@ via2_init(char const *user_peripheral_plugin_path)
371383
.api_version = X16_USER_PORT_API_VERSION,
372384
};
373385
if (user_port_init(&init_args, &user_port) < 0) {
374-
fprintf(stderr, "error initializing user peripheral\n");
386+
fprintf(stderr, "error initializing user peripheral: %s\n", last_user_port_error);
375387
error = true;
376388
}
377389
else if (user_port.api_version != X16_USER_PORT_API_VERSION) {

0 commit comments

Comments
 (0)