diff --git a/bin/dash2can.c b/bin/dash2can.c index 64175cc..dd62acf 100644 --- a/bin/dash2can.c +++ b/bin/dash2can.c @@ -36,12 +36,15 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(1); } -static hexagram_schedule_slot table[] = { - { 16670, NULL, { .can_id = 0x280, .can_dlc = 8 }}, - { 16670, NULL, { .can_id = 0x288, .can_dlc = 8 }}, - { 16670, NULL, { .can_id = 0x320, .can_dlc = 8 }}, - { 16670, NULL, { .can_id = 0x5a0, .can_dlc = 8 }}, - { 0, NULL, {} } +static int ev_handler(struct can_frame *frame, hexagram_can_if *can_if) { + return hexagram_can_if_write(can_if, frame); +} + +static hexagram_schedule_slot table[4] = { + { 16670, { .can_id = 0x280, .can_dlc = 8 }, ev_handler }, + { 16670, { .can_id = 0x288, .can_dlc = 8 }, ev_handler }, + { 16670, { .can_id = 0x320, .can_dlc = 8 }, ev_handler }, + { 16670, { .can_id = 0x5a0, .can_dlc = 8 }, ev_handler } }; static void table_update(hexagram_schedule_slot *table, @@ -68,8 +71,6 @@ int hexagram_main_dash2can(int argc, char **argv) { hexagram_can_if *can_if; hexagram_schedule *schedule; - size_t i, count = 4; - if (argc < 2) { usage(argc, argv, "No listening UDP port provided"); } else if (argc < 3) { @@ -97,17 +98,13 @@ int hexagram_main_dash2can(int argc, char **argv) { goto error_can_if_open; } - for (i=0; i +typedef int (*hexagram_schedule_handler)(struct can_frame *, void *); + typedef struct _hexagram_schedule_slot { time_t interval_us; - hexagram_can_if *iface; struct can_frame frame; + hexagram_schedule_handler handler; } hexagram_schedule_slot; typedef struct _hexagram_schedule { @@ -22,11 +24,13 @@ typedef struct _hexagram_schedule { size_t current, count; + void *ctx; int error; } hexagram_schedule; hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table, - size_t count); + size_t count, + void *ctx); void hexagram_schedule_destroy(hexagram_schedule *schedule); diff --git a/src/schedule.c b/src/schedule.c index 3e2f416..08c58a6 100644 --- a/src/schedule.c +++ b/src/schedule.c @@ -42,7 +42,7 @@ static void _ev_notify(hexagram_schedule *schedule) { hexagram_schedule_slot *slot = _slot(schedule, schedule->current); time_t delay = slot->interval_us; - if (hexagram_can_if_write(slot->iface, &slot->frame) < 0) { + if (slot->handler(&slot->frame, schedule->ctx) < 0) { schedule->error = errno; break; @@ -66,13 +66,16 @@ static void _ev_notify(hexagram_schedule *schedule) { static int _schedule_init(hexagram_schedule *schedule, hexagram_schedule_slot *table, - size_t count) { + size_t count, + void *ctx) { size_t i; memset(schedule, '\0', sizeof(*schedule)); schedule->current = 0; + schedule->error = 0; schedule->count = count; + schedule->ctx = ctx; schedule->ev.sigev_notify = SIGEV_THREAD; schedule->ev.sigev_notify_function = _ev_notify; @@ -87,6 +90,7 @@ static int _schedule_init(hexagram_schedule *schedule, for (i=0; iframe.data, '\0', sizeof(*(slot->frame.data))); memcpy(slot, &table[i], sizeof(hexagram_schedule_slot)); } @@ -99,14 +103,15 @@ error_timer_create: } hexagram_schedule *hexagram_schedule_create(hexagram_schedule_slot *table, - size_t count) { + size_t count, + void *ctx) { hexagram_schedule *schedule; if ((schedule = malloc(sizeof(*schedule) + count * sizeof(hexagram_schedule_slot))) == 0) { goto error_malloc_schedule; } - _schedule_init(schedule, table, count); + _schedule_init(schedule, table, count, ctx); return schedule;