3#include "TinyRobotics/utils/MemoryStream.h"
6namespace tinyrobotics {
9
10
11
12
13
14
15
16enum class MessageValueType {
24
25
26
27
28
29
31 MessageValueType
type;
34 Message<Coordinate<
float>> coordMsg;
35 Message<GPSCoordinate> gpsMsg;
37 ParsedMessage() :
type(MessageValueType::Unknown) {}
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
85 if (io.readBytes(prefix, 3) != 3)
return false;
86 if (strncmp(prefix,
"MSG", 3) != 0)
return false;
88 uint8_t size = io.read();
89 uint8_t origin_id = io.read();
92 MessageContent content;
93 if (io.readBytes((
char*)&content,
sizeof(content)) !=
sizeof(content))
96 if (io.readBytes((
char*)&unit,
sizeof(unit)) !=
sizeof(unit))
return false;
99 if (content == MessageContent::Position) {
100 result
.type = MessageValueType::Coordinate;
102 if (io.readBytes((
char*)&result.coordMsg.value,
103 sizeof(Coordinate<
float>)) !=
sizeof(Coordinate<
float>))
105 result.coordMsg.prefix =
"MSG";
106 result.coordMsg.size = size;
107 result.coordMsg.origin_id = origin_id;
108 result.coordMsg.content = content;
109 result.coordMsg.unit = unit;
111 io.readBytes((
char*)&result.coordMsg.origin,
sizeof(MessageOrigin));
112 }
else if (content == MessageContent::PositionGPS) {
113 result
.type = MessageValueType::GPSCoordinate;
114 if (io.readBytes((
char*)&result.gpsMsg.value,
sizeof(GPSCoordinate)) !=
115 sizeof(GPSCoordinate))
117 result.gpsMsg.prefix =
"MSG";
118 result.gpsMsg.size = size;
119 result.gpsMsg.origin_id = origin_id;
120 result.gpsMsg.content = content;
121 result.gpsMsg.unit = unit;
122 io.readBytes((
char*)&result.gpsMsg.origin,
sizeof(MessageOrigin));
124 result
.type = MessageValueType::Float;
125 if (io.readBytes((
char*)&result.floatMsg.value,
sizeof(
float)) !=
128 result.floatMsg.prefix =
"MSG";
129 result.floatMsg.size = size;
130 result.floatMsg.origin_id = origin_id;
131 result.floatMsg.content = content;
132 result.floatMsg.unit = unit;
133 io.readBytes((
char*)&result.floatMsg.origin,
sizeof(MessageOrigin));
139
140
141
142
143
144
145
146
147
148
149
150 bool parse(Stream& io, MessageHandler& handler) {
152 if (!parse(io, parsed))
return false;
153 switch (parsed
.type) {
154 case MessageValueType::Float:
155 handler.onMessage(parsed.floatMsg);
157 case MessageValueType::Coordinate:
158 handler.onMessage(parsed.coordMsg);
160 case MessageValueType::GPSCoordinate:
161 handler.onMessage(parsed.gpsMsg);
170
171
172
173
174
175
176
177
178
179 bool parse(uint8_t* data, size_t len, MessageHandler& handler) {
181 return parse(stream, handler);
Read-only memory stream for wrapping a buffer as an Arduino Stream.
Definition: MemoryStream.h:16
Parses binary TinyRobotics messages from a Stream and dispatches them by type.
Definition: MessageParser.h:79
bool parse(uint8_t *data, size_t len, MessageHandler &handler)
Parses a message from a raw buffer and dispatches it to the given handler.
Definition: MessageParser.h:179
bool parse(Stream &io, MessageHandler &handler)
Parses a message from the stream and dispatches it to the given handler.
Definition: MessageParser.h:150
Unit
Units for message values.
Definition: Common.h:45
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72
Holds a parsed TinyRobotics message of the correct type.
Definition: MessageParser.h:30
MessageValueType type
Indicates which union member is valid.
Definition: MessageParser.h:31