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

// MIN() macro
#include <sys/param.h>
// TODO: check if this works on pico as well

#include "puzbusv1.h"

/**
 * \brief Remaining bytes to be read to target->data
 *
 * This is the only variable that needs to persist between buffer blocks. It is
 * declared in the global scope to allow resetting using the \c pb_read_reset()
 * function.
 *
 * \note \p rdata may be reset by calling \c pb_read_reset()
 */
static size_t rdata = 0;

int pb_read(struct pb_msg* target, char* buf, size_t buf_sz) {
	// a new reader is used per buffer block passed to this function
	mpack_reader_t reader;
	mpack_reader_init_data(&reader, buf, buf_sz);

	// at start of message
	if (rdata == 0) {
		// NOTE: The entire start of a message needs to be readable from the buffer
		// at this point. When target->addr can be read and target->length is past
		// the end of the current buffer block, this function will crash and burn.
		// This is a highly unlikely scenario, as pb_read is called for each chunk
		// of a TCP frame, and frames (should) include only one puzzle bus message.
		// The check here is kind of optional.
		if (buf_sz < 4) return -1;

		target->addr = mpack_expect_u16(&reader);
		target->length = rdata = mpack_expect_bin(&reader);
		target->data = (char*) malloc(target->length);
	}

	// continue reading chunks of target->data until the amount of bytes
	// specified in target->length
	size_t to_read = MIN(mpack_reader_remaining(&reader, NULL), rdata);
	char* data = target->data + target->length - rdata; // 'ol pointer arithmetic
	mpack_read_bytes(&reader, data, to_read);
	rdata -= to_read;

	// if rdata = 0, the message was completely read
	return rdata;
}

void pb_read_reset() {
	rdata = 0;
}

bool 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;
}