From 41abc005cd4eb9efd750f15e9ca35abcb7483996 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 27 Dec 2023 01:01:37 -0500 Subject: [PATCH] Refactor dash2can using hexagram_schedule --- bin/dash2can.c | 75 ++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/bin/dash2can.c b/bin/dash2can.c index 9d2156c..be77c5e 100644 --- a/bin/dash2can.c +++ b/bin/dash2can.c @@ -13,6 +13,7 @@ #include #include +#include #include #include "dash2can.h" @@ -35,11 +36,22 @@ 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, {} } +}; + int hexagram_main_dash2can(int argc, char **argv) { int sock; struct sockaddr_in sockaddr; hexagram_can_if *can_if; + hexagram_schedule *schedule; + + size_t i, count = 4; if (argc < 2) { usage(argc, argv, "No listening UDP port provided"); @@ -68,9 +80,24 @@ int hexagram_main_dash2can(int argc, char **argv) { goto error_can_if_open; } + for (i=0; i> 8; - frame.data[2] = engine_rpm & 0x00ff; - - if (hexagram_can_if_write(can_if, &frame) < 0) { - goto error_io; - } - - /* Engine temperature */ - memset(frame.data, '\0', sizeof(frame.data)); - frame.can_id = 0x288; - frame.can_dlc = 8; - frame.data[1] = (uint8_t)((87.0 / 0.75) + 48); - - if (hexagram_can_if_write(can_if, &frame) < 0) { - goto error_io; - } - - /* Fuel status */ - memset(frame.data, '\0', sizeof(frame.data)); - frame.can_id = 0x320; - frame.can_dlc = 8; - frame.data[2] = 0x0e; - - if (hexagram_can_if_write(can_if, &frame) < 0) { - goto error_io; - } + table[0].frame.data[3] = (engine_rpm & 0xff00) >> 8; + table[0].frame.data[2] = engine_rpm & 0x00ff; /* Vehicle speed */ - memset(frame.data, '\0', sizeof(frame.data)); - frame.can_id = 0x5a0; - frame.can_dlc = 8; - frame.data[1] = (speed_rps & 0xff00) >> 8; - frame.data[2] = speed_rps & 0x00ff; - - if (hexagram_can_if_write(can_if, &frame) < 0) { - goto error_io; - } + table[3].frame.data[1] = (speed_rps & 0xff00) >> 8; + table[3].frame.data[2] = speed_rps & 0x00ff; } + hexagram_schedule_destroy(schedule); hexagram_can_if_close(can_if); close(sock); @@ -135,6 +129,9 @@ int hexagram_main_dash2can(int argc, char **argv) { return 0; error_io: + hexagram_schedule_destroy(schedule); + +error_schedule_create: hexagram_can_if_close(can_if); error_can_if_open: