From e9e2e6ffaacba3d1a7cd918091ea38bd38a469d4 Mon Sep 17 00:00:00 2001 From: norly Date: Sat, 18 Mar 2017 22:00:18 +0100 Subject: [PATCH] Forgot to commit .h files --- vw-nm-tools.h | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ vw-nm.h | 66 ++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 vw-nm-tools.h create mode 100644 vw-nm.h diff --git a/vw-nm-tools.h b/vw-nm-tools.h new file mode 100644 index 0000000..e9e26c8 --- /dev/null +++ b/vw-nm-tools.h @@ -0,0 +1,119 @@ +static char* nm_main_to_string(NM_State state) +{ + switch(state & NM_MAIN_MASK) { + case NM_MAIN_OFF: + return "Off"; + case NM_MAIN_ON: + return "Ready"; + case NM_MAIN_LOGIN: + return "Login"; + case NM_MAIN_LIMPHOME: + return "Limp home"; + default: + return "Unknown?"; + } +} + +static char* nm_sleep_to_string(NM_State state) +{ + switch(state & NM_SLEEP_MASK) { + case NM_SLEEP_CANCEL: + return "No"; + case NM_SLEEP_REQUEST: + return "Request"; + case NM_SLEEP_ACK: + return "Acknowledged"; + default: + return "Unknown?"; + } +} + + + +static void nm_dump_all(struct NM_Main *nm) +{ + unsigned id; + + printf("\n"); + printf(" Node | next | Main | Sleep\n"); + printf("----------------------------------------\n"); + + for (id = 0; id < nm->max_nodes; id++) { + struct NM_Node *node = &nm->nodes[id]; + + if (node->state & NM_MAIN_MASK) { + printf(" %02x %02x %9s %s\n", + id, + node->next, + nm_main_to_string(node->state), + nm_sleep_to_string(node->state)); + + } + } + + printf("\n"); +} + + + + + + +static void can_tx(int socket, struct can_frame *frame) +{ + ssize_t ret; + + ret = write(socket, frame, sizeof(*frame)); + if (ret != sizeof(*frame)) { + perror("write to CAN socket"); + exit(1); + } +} + + + + + + + + +static struct NM_Main* nm_alloc(unsigned node_bits, NM_ID my_id, canid_t can_base) +{ + struct NM_Main *nm; + + if (node_bits < 1 || node_bits > 6) { + return NULL; + } + + nm = malloc(sizeof(*nm)); + if (!nm) { + return NULL; + } + + nm->max_nodes = 1 << node_bits; + nm->nodes = malloc(nm->max_nodes * sizeof(*nm->nodes)); + if (!nm->nodes) { + free(nm); + return NULL; + } + + nm->my_id = my_id; + nm->can_base = can_base; + + nm->nodes[nm->my_id].next = nm->my_id; + nm->nodes[nm->my_id].state = NM_MAIN_LOGIN; + + nm->tv.tv_sec = 0; + nm->tv.tv_usec = NM_USECS_OTHER_TURN; + + return nm; +} + + + + +static void nm_free(struct NM_Main *nm) +{ + free(nm->nodes); + free(nm); +} diff --git a/vw-nm.h b/vw-nm.h new file mode 100644 index 0000000..d092bdc --- /dev/null +++ b/vw-nm.h @@ -0,0 +1,66 @@ +#ifndef __VW_NM_H__ +#define __VW_NM_H__ + +#include + + +enum { + /* OSEK/VDX NM Level 0 */ + + NM_MAIN_OFF = 0x00, + NM_MAIN_ON = 0x01, + NM_MAIN_LOGIN = 0x02, + NM_MAIN_LIMPHOME = 0x04, + NM_MAIN_MASK = 0x0F, + + NM_SLEEP_CANCEL = 0x00, + NM_SLEEP_REQUEST = 0x10, + NM_SLEEP_ACK = 0x20, + NM_SLEEP_MASK = 0xF0, +}; + +typedef unsigned char NM_ID; +typedef unsigned char NM_State; + +struct NM_Node { + NM_ID next; + NM_State state; +}; + +struct NM_Main { + unsigned max_nodes; + struct NM_Node *nodes; + NM_ID my_id; + canid_t can_base; + struct timeval tv; +}; + + + + +/* This timeout is ~49 ms in: + * - 0x19 (RCD 310, Bosch) + * + * and ~45ms in: + * - 0x0b Instrument cluster? + * - 0x15 MDI + * - 0x1A Phone + * + * We may reduce it since we're not on a real-time OS. + */ +#define NM_USECS_MY_TURN 40000 + + +/* This timeout is 50 ms in: + * - 0x19 (RCD 310, Bosch) + */ +#define NM_USECS_OTHER_TURN 50000 + + +/* This timeout is 500 ms in: + * - 0x19 (RCD 310, Bosch) + */ +#define NM_USECS_LIMP_HOME 500000 + + +#endif /* __VW_NM_H__ */ -- 2.30.2