aboutsummaryrefslogtreecommitdiff
path: root/proto/puzbusv1.c
blob: 3ff7c633bd87eec13eb9c5ffd5004321d3a9aca2 (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
#include <mpack.h>
#include <stdio.h>

#include "puzbusv1.h"

int pb_read(struct pb_msg* target, char* buf, size_t buf_sz) {
	mpack_reader_t reader;
	printf("read %lu bytes...\n", buf_sz);

	mpack_reader_init_data(&reader, buf, buf_sz);

	uint16_t address = mpack_expect_u16(&reader);
	char data_buf[80];
	size_t data_size = mpack_expect_bin_buf(&reader, data_buf, sizeof(data_buf));

	printf("0x%02x\n", address);
	printf("\"%.*s\"\n", data_size, data_buf);

	return 0;
}

int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz) {
	mpack_writer_t writer;
	mpack_writer_init_growable(&writer, buf, buf_sz);

	mpack_write_u16(&writer, target->addr);
	mpack_write_bin(&writer, target->data, target->length);

	// finish writing
	return mpack_writer_destroy(&writer) == mpack_ok;
}