summaryrefslogtreecommitdiff
path: root/vag-bap-frame.c
blob: 4117d84292ed28e9c3bd0473b4db5f48ecbaee91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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);
}