Add README.md and COPYING
[revag-bap.git] / src / bap-frame.c
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "vag-bap.h"
7
8
9
10 struct BAP_Frame* vag_bap_frame_alloc(void)
11 {
12         struct BAP_Frame* bap_frame;
13
14         bap_frame = calloc(1, sizeof(struct BAP_Frame));
15
16         return bap_frame;
17 }
18
19
20 void vag_bap_frame_free(struct BAP_Frame *bap_frame)
21 {
22         free(bap_frame);
23 }
24
25
26 int vag_bap_frame_is_valid(struct BAP_Frame *bap_frame)
27 {
28         if ( 0
29                 || (bap_frame->opcode > 7)
30                 || (bap_frame->node > 63)
31                 || (bap_frame->port > 63)
32                 || (bap_frame->len > 4095)
33                 || (!bap_frame->is_multiframe && bap_frame->len > 6)
34                 ) {
35                 return 0;
36         }
37
38         return 1;
39 }
40
41
42 struct BAP_Frame* vag_bap_frame_clone(struct BAP_Frame *bap_frame)
43 {
44         struct BAP_Frame *new_frame;
45
46         if (!vag_bap_frame_is_valid(bap_frame)) {
47                 return NULL;
48         }
49
50         new_frame = vag_bap_frame_alloc();
51         if (!new_frame) {
52                 return NULL;
53         }
54
55         memcpy(new_frame, bap_frame, sizeof(*new_frame));
56
57         return new_frame;
58 }
59
60
61
62 void vag_bap_frame_dump(struct BAP_Frame *bap_frame)
63 {
64         unsigned i;
65
66         printf("%u. %2i/%-2i .%02i --",
67                 bap_frame->opcode,
68                 bap_frame->node,
69                 bap_frame->port,
70                 bap_frame->len);
71
72         for (i = 0; i < bap_frame->len; i++) {
73                 if (!(i % 4)) {
74                         printf(" ");
75                 }
76                 printf("%02x", (unsigned char)(bap_frame->data[i]));
77         }
78
79         printf("\n        '");
80         for (i = 0; i < bap_frame->len; i++) {
81                 unsigned char c = bap_frame->data[i];
82                 if (!isprint(c) || c == '\n' || c == '\r') {
83                         c = '.';
84                 }
85                 printf("%c", c);
86         }
87         printf("'");
88
89         printf("\n");
90
91         fflush(stdout);
92 }