Simplify schedule a bit
This commit is contained in:
parent
98bb4e5308
commit
927506f086
3 changed files with 25 additions and 19 deletions
|
@ -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<count; i++) {
|
||||
table[i].iface = can_if;
|
||||
}
|
||||
|
||||
/* Engine temperature */
|
||||
table[1].frame.data[1] = (uint8_t)((87.0 / 0.75) + 48);
|
||||
|
||||
/* Fuel status */
|
||||
table[2].frame.data[2] = 0x0e;
|
||||
|
||||
if ((schedule = hexagram_schedule_create(table, count)) == NULL) {
|
||||
if ((schedule = hexagram_schedule_create(table, 4, can_if)) == NULL) {
|
||||
goto error_schedule_create;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,10 +8,12 @@
|
|||
|
||||
#include <hexagram/can.h>
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -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; i<count; i++) {
|
||||
hexagram_schedule_slot *slot = &((hexagram_schedule_slot *)(schedule + 1))[i];
|
||||
|
||||
memset(slot->frame.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;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue