aboutsummaryrefslogtreecommitdiff
path: root/car/pb_stream.cpp
blob: f5c3dc5d02346d81bd06c9338e80b1d657e3798e (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
// From https://github.com/amorellgarcia/arduino-nanopb

#include "pb_stream.h"

#define MAXSZ (size_t)-1

bool
os_read(pb_istream_t *stream, uint8_t *buf, size_t count)
{
	Stream *s = static_cast<Stream *>(stream->state);
	while (count > 0) {
		if (s->available() > 0) {
			size_t readCount = s->readBytes((char *)buf, count);
			count -= readCount;
		}
	}
	return true;
}


void
pb_istream_from_stream(Stream &stream, pb_istream_t &istream)
{
	istream.callback = &os_read;
	istream.state = &stream;
	istream.bytes_left = MAXSZ;
#ifndef PB_NO_ERRMSG
	istream.errmsg = NULL;
#endif
}

bool
os_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
{
	if (stream == NULL || buf == NULL) {
		return false;
	}
	Print *s = static_cast<Print *>(stream->state);
	return (s->write(buf, count) == count);
}

void
pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream) {
	ostream.callback = &os_write;
	ostream.state = &stream;
	ostream.max_size = MAXSZ;
	ostream.bytes_written = 0;
#ifndef PB_NO_ERRMSG
	ostream.errmsg = NULL;
#endif
}