diff options
author | norly <ny-git@enpas.org> | 2017-03-22 22:44:07 +0100 |
---|---|---|
committer | norly <ny-git@enpas.org> | 2017-03-22 22:44:07 +0100 |
commit | 54c0ca2077820b3672b67528e1dc8006c0f9f2b1 (patch) | |
tree | 3f4dea440a92c2e33b4b5863ac9baca033f1e895 | |
parent | 74194b9c9322a232c8bcff606fdddc29784e7af3 (diff) |
Split bap library
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | vw-bap-dump.c | 226 | ||||
-rw-r--r-- | vw-bap.c | 205 | ||||
-rw-r--r-- | vw-bap.h | 41 |
4 files changed, 248 insertions, 226 deletions
@@ -1,3 +1,3 @@ CFLAGS += -Wall -g -vw-bap-dump: +vw-bap-dump: vw-bap-dump.c vw-bap.c diff --git a/vw-bap-dump.c b/vw-bap-dump.c index 605cf36..0b4e6a2 100644 --- a/vw-bap-dump.c +++ b/vw-bap-dump.c @@ -6,8 +6,6 @@ * by the Free Software Foundation. */ -#include <ctype.h> - #include <stdio.h> #include <stdint.h> #include <unistd.h> @@ -19,199 +17,8 @@ #include <linux/can/raw.h> #include <net/if.h> #include <sys/ioctl.h> -#include <endian.h> -#include <sys/time.h> - - - - -typedef unsigned char BAP_OpCode; -typedef unsigned char BAP_SubNode; -typedef unsigned char BAP_SubFunction; -typedef unsigned short BAP_FrameLen; - - -struct BAP_Frame { - BAP_OpCode opcode; - BAP_SubNode subnode; - BAP_SubFunction function; - BAP_FrameLen len; - BAP_FrameLen len_done; - char data[4096]; -}; - - -struct BAP_RXer { - struct BAP_Frame *mfchannel[8]; -}; - - - - - -static void bap_frame_dump(struct BAP_Frame *bap_frame) -{ - unsigned i; - - printf("%u. %2i/%-2i .%02i --", - bap_frame->opcode, - bap_frame->subnode, - bap_frame->function, - bap_frame->len); - - for (i = 0; i < bap_frame->len; i++) { - if (!(i % 4)) { - printf(" "); - } - printf("%02x", (unsigned char)(bap_frame->data[i])); - } - - printf("\n '"); - for (i = 0; i < bap_frame->len; i++) { - unsigned char c = bap_frame->data[i]; - if (!isprint(c) || c == '\n' || c == '\r') { - c = '.'; - } - printf("%c", c); - } - printf("'"); - - printf("\n"); - - fflush(stdout); -} - - - - - -static struct BAP_Frame* bap_handle_can_frame(struct BAP_RXer *bap, struct can_frame *frame) -{ - struct BAP_Frame *bap_frame = NULL; - unsigned short header; - unsigned this_len; - - //printf("Received BAP frame from CAN ID %03x\n", frame->can_id); - - if (frame->can_dlc < 2) { - printf("Error: Frame too short\n"); - } - - if (frame->data[0] & 0x80) { - /* This is a multi-frame BAP message */ - int mfchannel = (frame->data[0] >> 4) & 0x3; - - if (!(frame->data[0] & 0x40)) { - /* Start frame */ - - unsigned short header; - unsigned this_len; - - if (frame->can_dlc < 4) { - printf("Error: Frame too short\n"); - } - - if (bap->mfchannel[mfchannel]) { - printf("bap_handle_can_frame: new start frame for open channel\n"); - } - bap->mfchannel[mfchannel] = NULL; - - bap_frame = calloc(1, sizeof(struct BAP_Frame)); - if (!bap_frame) { - printf("bap_handle_can_frame: Failed to allocate new frame\n"); - return NULL; - } - - header = (frame->data[2] << 8) | frame->data[3]; - bap_frame->opcode = (header >> 12) & 0x7; - bap_frame->subnode = (header >> 6) & 0x3F; - bap_frame->function = (header >> 0) & 0x3F; - - bap_frame->len = ((frame->data[0] & 0xF) << 8) | frame->data[1]; - - this_len = frame->can_dlc - 4; - - if (this_len > bap_frame->len) { - printf("bap_handle_can_frame: this_len > len\n"); - - free(bap_frame); - bap->mfchannel[mfchannel] = NULL; - - return NULL; - } - - memcpy(&bap_frame->data[0], &frame->data[frame->can_dlc - this_len], this_len); - bap_frame->len_done = this_len; - - if (bap_frame->len_done == bap_frame->len) { - /* Frame is complete, remove from buffer */ - bap->mfchannel[mfchannel] = NULL; - return bap_frame; - } else { - /* Frame is incomplete, don't emit it yet */ - bap->mfchannel[mfchannel] = bap_frame; - return NULL; - } - } else { - /* Continuation frame */ - - bap_frame = bap->mfchannel[mfchannel]; - - if (!bap_frame) { - printf("bap_handle_can_frame: continuation frame for unknown mf channel %d\n", - mfchannel); - } - - this_len = frame->can_dlc - 1; - - if ((bap_frame->len_done + this_len) > bap_frame->len) { - printf("bap_handle_can_frame: len_done + this_len > len\n"); - - free(bap_frame); - bap->mfchannel[mfchannel] = NULL; - - return NULL; - } - - memcpy(&bap_frame->data[bap_frame->len_done], - &frame->data[frame->can_dlc - this_len], - this_len); - bap_frame->len_done += this_len; - - if (bap_frame->len_done == bap_frame->len) { - /* Frame is complete, remove from buffer */ - bap->mfchannel[mfchannel] = NULL; - return bap_frame; - } else { - /* Frame is incomplete, don't emit it yet */ - return NULL; - } - } - } else { - /* Single-frame BAP message */ - - bap_frame = calloc(1, sizeof(struct BAP_Frame)); - if (!bap_frame) { - printf("bap_handle_can_frame: Failed to allocate new frame\n"); - return NULL; - } - - header = (frame->data[0] << 8) | frame->data[1]; - bap_frame->opcode = (header >> 12) & 0x7; - bap_frame->subnode = (header >> 6) & 0x3F; - bap_frame->function = (header >> 0) & 0x3F; - - this_len = frame->can_dlc - 2; - - bap_frame->len = this_len; - - memcpy(&bap_frame->data[0], &frame->data[frame->can_dlc - this_len], this_len); - bap_frame->len_done = this_len; - - return bap_frame; - } -} +#include "vw-bap.h" @@ -255,37 +62,6 @@ static int net_init(char *ifname) -static struct BAP_RXer* bap_alloc() -{ - struct BAP_RXer *bap; - - bap = calloc(1, sizeof(*bap)); - if (!bap) { - return NULL; - } - - return bap; -} - - - - -static void bap_free(struct BAP_RXer *bap) -{ - /* TODO */ - free(bap); -} - - - -static void bap_frame_free(struct BAP_Frame *bap_frame) -{ - /* TODO */ - free(bap_frame); -} - - - int main(int argc, char **argv) diff --git a/vw-bap.c b/vw-bap.c new file mode 100644 index 0000000..34921db --- /dev/null +++ b/vw-bap.c @@ -0,0 +1,205 @@ +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <linux/can.h> + +#include "vw-bap.h" + + +void bap_frame_dump(struct BAP_Frame *bap_frame) +{ + unsigned i; + + printf("%u. %2i/%-2i .%02i --", + bap_frame->opcode, + bap_frame->subnode, + bap_frame->function, + bap_frame->len); + + for (i = 0; i < bap_frame->len; i++) { + if (!(i % 4)) { + printf(" "); + } + printf("%02x", (unsigned char)(bap_frame->data[i])); + } + + printf("\n '"); + for (i = 0; i < bap_frame->len; i++) { + unsigned char c = bap_frame->data[i]; + if (!isprint(c) || c == '\n' || c == '\r') { + c = '.'; + } + printf("%c", c); + } + printf("'"); + + printf("\n"); + + fflush(stdout); +} + + + + + +struct BAP_Frame* bap_handle_can_frame(struct BAP_RXer *bap, struct can_frame *frame) +{ + struct BAP_Frame *bap_frame = NULL; + unsigned short header; + unsigned this_len; + + //printf("Received BAP frame from CAN ID %03x\n", frame->can_id); + + if (frame->can_dlc < 2) { + printf("Error: Frame too short\n"); + } + + if (frame->data[0] & 0x80) { + /* This is a multi-frame BAP message */ + int mfchannel = (frame->data[0] >> 4) & 0x3; + + if (!(frame->data[0] & 0x40)) { + /* Start frame */ + + unsigned short header; + unsigned this_len; + + if (frame->can_dlc < 4) { + printf("Error: Frame too short\n"); + } + + if (bap->mfchannel[mfchannel]) { + printf("bap_handle_can_frame: new start frame for open channel\n"); + } + bap->mfchannel[mfchannel] = NULL; + + bap_frame = calloc(1, sizeof(struct BAP_Frame)); + if (!bap_frame) { + printf("bap_handle_can_frame: Failed to allocate new frame\n"); + return NULL; + } + + header = (frame->data[2] << 8) | frame->data[3]; + bap_frame->opcode = (header >> 12) & 0x7; + bap_frame->subnode = (header >> 6) & 0x3F; + bap_frame->function = (header >> 0) & 0x3F; + + bap_frame->len = ((frame->data[0] & 0xF) << 8) | frame->data[1]; + + this_len = frame->can_dlc - 4; + + if (this_len > bap_frame->len) { + printf("bap_handle_can_frame: this_len > len\n"); + + free(bap_frame); + bap->mfchannel[mfchannel] = NULL; + + return NULL; + } + + memcpy(&bap_frame->data[0], &frame->data[frame->can_dlc - this_len], this_len); + bap_frame->len_done = this_len; + + if (bap_frame->len_done == bap_frame->len) { + /* Frame is complete, remove from buffer */ + bap->mfchannel[mfchannel] = NULL; + return bap_frame; + } else { + /* Frame is incomplete, don't emit it yet */ + bap->mfchannel[mfchannel] = bap_frame; + return NULL; + } + } else { + /* Continuation frame */ + + bap_frame = bap->mfchannel[mfchannel]; + + if (!bap_frame) { + printf("bap_handle_can_frame: continuation frame for unknown mf channel %d\n", + mfchannel); + } + + this_len = frame->can_dlc - 1; + + if ((bap_frame->len_done + this_len) > bap_frame->len) { + printf("bap_handle_can_frame: len_done + this_len > len\n"); + + free(bap_frame); + bap->mfchannel[mfchannel] = NULL; + + return NULL; + } + + memcpy(&bap_frame->data[bap_frame->len_done], + &frame->data[frame->can_dlc - this_len], + this_len); + bap_frame->len_done += this_len; + + if (bap_frame->len_done == bap_frame->len) { + /* Frame is complete, remove from buffer */ + bap->mfchannel[mfchannel] = NULL; + return bap_frame; + } else { + /* Frame is incomplete, don't emit it yet */ + return NULL; + } + } + } else { + /* Single-frame BAP message */ + + bap_frame = calloc(1, sizeof(struct BAP_Frame)); + if (!bap_frame) { + printf("bap_handle_can_frame: Failed to allocate new frame\n"); + return NULL; + } + + header = (frame->data[0] << 8) | frame->data[1]; + bap_frame->opcode = (header >> 12) & 0x7; + bap_frame->subnode = (header >> 6) & 0x3F; + bap_frame->function = (header >> 0) & 0x3F; + + this_len = frame->can_dlc - 2; + + bap_frame->len = this_len; + + memcpy(&bap_frame->data[0], &frame->data[frame->can_dlc - this_len], this_len); + bap_frame->len_done = this_len; + + return bap_frame; + } +} + + + + + +struct BAP_RXer* bap_alloc() +{ + struct BAP_RXer *bap; + + bap = calloc(1, sizeof(*bap)); + if (!bap) { + return NULL; + } + + return bap; +} + + + + +void bap_free(struct BAP_RXer *bap) +{ + /* TODO */ + free(bap); +} + + + +void bap_frame_free(struct BAP_Frame *bap_frame) +{ + /* TODO */ + free(bap_frame); +} diff --git a/vw-bap.h b/vw-bap.h new file mode 100644 index 0000000..0345605 --- /dev/null +++ b/vw-bap.h @@ -0,0 +1,41 @@ +#ifndef __VW_BAP_H__ +#define __VW_BAP_H__ + +#include <linux/can.h> + + +typedef unsigned char BAP_OpCode; +typedef unsigned char BAP_SubNode; +typedef unsigned char BAP_SubFunction; +typedef unsigned short BAP_FrameLen; + + +struct BAP_Frame { + BAP_OpCode opcode; + BAP_SubNode subnode; + BAP_SubFunction function; + BAP_FrameLen len; + BAP_FrameLen len_done; + char data[4096]; +}; + + +struct BAP_RXer { + struct BAP_Frame *mfchannel[8]; +}; + + + + + +void bap_frame_dump(struct BAP_Frame *bap_frame); + +struct BAP_Frame* bap_handle_can_frame(struct BAP_RXer *bap, struct can_frame *frame); + +struct BAP_RXer* bap_alloc(); +void bap_free(struct BAP_RXer *bap); +void bap_frame_free(struct BAP_Frame *bap_frame); + + + +#endif |