summaryrefslogtreecommitdiff
path: root/vag-bap-frame.c
diff options
context:
space:
mode:
authornorly <ny-git@enpas.org>2019-02-27 23:16:21 +0100
committernorly <ny-git@enpas.org>2019-02-27 23:16:21 +0100
commit9c19c3d11996fac09da9d7797eca15fa49fd6e72 (patch)
treea8d095e9b89ccbd569d63bbc453fd37773815ca0 /vag-bap-frame.c
parent8c73fd11fe9cac11ba2fdd657b84f845632af3d0 (diff)
Rename vw_bap to vag_bap, add make clean
Diffstat (limited to 'vag-bap-frame.c')
-rw-r--r--vag-bap-frame.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/vag-bap-frame.c b/vag-bap-frame.c
new file mode 100644
index 0000000..4117d84
--- /dev/null
+++ b/vag-bap-frame.c
@@ -0,0 +1,92 @@
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vag-bap.h"
+
+
+
+struct BAP_Frame* vag_bap_frame_alloc(void)
+{
+ struct BAP_Frame* bap_frame;
+
+ bap_frame = calloc(1, sizeof(struct BAP_Frame));
+
+ return bap_frame;
+}
+
+
+void vag_bap_frame_free(struct BAP_Frame *bap_frame)
+{
+ free(bap_frame);
+}
+
+
+int vag_bap_frame_is_valid(struct BAP_Frame *bap_frame)
+{
+ if ( 0
+ || (bap_frame->opcode > 7)
+ || (bap_frame->node > 63)
+ || (bap_frame->port > 63)
+ || (bap_frame->len > 4095)
+ || (!bap_frame->is_multiframe && bap_frame->len > 6)
+ ) {
+ return 0;
+ }
+
+ return 1;
+}
+
+
+struct BAP_Frame* vag_bap_frame_clone(struct BAP_Frame *bap_frame)
+{
+ struct BAP_Frame *new_frame;
+
+ if (!vag_bap_frame_is_valid(bap_frame)) {
+ return NULL;
+ }
+
+ new_frame = vag_bap_frame_alloc();
+ if (!new_frame) {
+ return NULL;
+ }
+
+ memcpy(new_frame, bap_frame, sizeof(*new_frame));
+
+ return new_frame;
+}
+
+
+
+void vag_bap_frame_dump(struct BAP_Frame *bap_frame)
+{
+ unsigned i;
+
+ printf("%u. %2i/%-2i .%02i --",
+ bap_frame->opcode,
+ bap_frame->node,
+ bap_frame->port,
+ 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);
+}