vw-bap-frame.c: Extract frame handling functions
authornorly <ny-git@enpas.org>
Sun, 23 Apr 2017 12:54:26 +0000 (14:54 +0200)
committernorly <ny-git@enpas.org>
Sun, 23 Apr 2017 12:54:26 +0000 (14:54 +0200)
Makefile
vw-bap-frame.c [new file with mode: 0644]
vw-bap.c
vw-bap.h

index 5b235d5a1df0a631ccd73fa43c70572551b45cb9..6a0fe719b0f25d1bcb365c6cb409e6bcf4c5cef0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,11 @@
 CFLAGS += -Wall -g
 
+BAPLIB = vw-bap.c vw-bap-frame.c
+
 all: vw-bap-dump vw-bap-sniffer
 
 
-vw-bap-dump: vw-bap-dump.c vw-bap.c
+vw-bap-dump: vw-bap-dump.c $(BAPLIB)
 
-vw-bap-sniffer: vw-bap-sniffer.c vw-bap.c
+vw-bap-sniffer: vw-bap-sniffer.c $(BAPLIB)
        gcc -o $@ $^ -lncurses
diff --git a/vw-bap-frame.c b/vw-bap-frame.c
new file mode 100644 (file)
index 0000000..4fa2ec2
--- /dev/null
@@ -0,0 +1,72 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vw-bap.h"
+
+
+
+struct BAP_Frame* vw_bap_frame_alloc(void)
+{
+       struct BAP_Frame* bap_frame;
+
+       bap_frame = calloc(1, sizeof(struct BAP_Frame));
+
+       return bap_frame;
+}
+
+
+void vw_bap_frame_free(struct BAP_Frame *bap_frame)
+{
+       free(bap_frame);
+}
+
+
+int vw_bap_frame_is_valid(struct BAP_Frame *bap_frame)
+{
+       if ( 0
+               || (bap_frame->opcode > 7)
+               || (bap_frame->node > 63)
+               || (bap_frame->function > 63)
+               || (bap_frame->len > 4095)
+               || (!bap_frame->is_multiframe && bap_frame->len > 6)
+               ) {
+               return 0;
+       }
+
+       return 1;
+}
+
+
+void vw_bap_frame_dump(struct BAP_Frame *bap_frame)
+{
+       unsigned i;
+
+       printf("%u. %2i/%-2i .%02i --",
+               bap_frame->opcode,
+               bap_frame->node,
+               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);
+}
index 436bd9c911551c450d00e4e567516f8dd0100614..1864cea009e87a8e05d7a1b2214ce2f89778ea7b 100644 (file)
--- a/vw-bap.c
+++ b/vw-bap.c
@@ -9,60 +9,6 @@
 
 
 
-struct BAP_Frame* vw_bap_frame_alloc(void)
-{
-       struct BAP_Frame* bap_frame;
-
-       bap_frame = calloc(1, sizeof(struct BAP_Frame));
-
-       return bap_frame;
-}
-
-
-void vw_bap_frame_free(struct BAP_Frame *bap_frame)
-{
-       free(bap_frame);
-}
-
-
-
-
-
-void vw_bap_frame_dump(struct BAP_Frame *bap_frame)
-{
-       unsigned i;
-
-       printf("%u. %2i/%-2i .%02i --",
-               bap_frame->opcode,
-               bap_frame->node,
-               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* vw_bap_handle_can_frame(struct BAP_RXer *bap, struct can_frame *frame)
 {
index b7540ea070a816d55d60a3c56675257c139f7c79..7bccdfd0cc79b7117313c93ecb39d908bfba9b74 100644 (file)
--- a/vw-bap.h
+++ b/vw-bap.h
@@ -62,14 +62,17 @@ struct BAP_RXer {
 
 
 
+/* BAP frame struct handling */
+struct BAP_Frame* vw_bap_frame_alloc(void);
+             void vw_bap_frame_free(struct BAP_Frame *bap_frame);
 
+int vw_bap_frame_is_valid(struct BAP_Frame *bap_frame);
 
 void vw_bap_frame_dump(struct BAP_Frame *bap_frame);
 
-struct BAP_Frame* vw_bap_handle_can_frame(struct BAP_RXer *bap, struct can_frame *frame);
-
-void vw_bap_frame_free(struct BAP_Frame *bap_frame);
 
+/* BAP reception */
+struct BAP_Frame* vw_bap_handle_can_frame(struct BAP_RXer *bap, struct can_frame *frame);
 struct BAP_RXer* vw_bap_rxer_alloc();
 void vw_bap_rxer_free(struct BAP_RXer *bap);