-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageParser.cpp
276 lines (246 loc) · 9.96 KB
/
MessageParser.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
romi-rover
Copyright (C) 2019 Sony Computer Science Laboratories
Author(s) Peter Hanappe
romi-rover is collection of applications for the Romi Rover.
romi-rover is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
//#include <stdio.h>
#include <string.h>
#include "MessageParser.h"
#include "RomiSerialErrors.h"
#include "Log.h"
namespace romiserial {
#define START_MESSAGE(_c) ((_c) == '#')
#define END_MESSAGE(_c) ((_c) == '\0')
#define MINUS(_c) ((_c) == '-')
#define QUOTE(_c) ((_c) == '"')
#define DIGIT(_c) ((_c) >= '0' && (_c) <= '9')
#define OPENBRACKET(_c) ((_c) == '[')
#define CLOSEBRACKET(_c) ((_c) == ']')
#define CARRIAGE_RETURN(_c) ((_c) == '\r')
#define COMMA(_c) ((_c) == ',')
#define START_METADATA(_c) ((_c) == ':')
#define VALUE(_c) ((int16_t)((_c) - '0'))
#define VALID_OPCODE(_c) (('a' <= (_c) && (_c) <= 'z') \
|| ('A' <= (_c) && (_c) <= 'Z') \
|| ('0' <= (_c) && (_c) <= '9') \
|| ((_c) == '?'))
#define VALID_STRING_CHAR(_c) (('a' <= (_c) && (_c) <= 'z') \
|| ('A' <= (_c) && (_c) <= 'Z') \
|| ('0' <= (_c) && (_c) <= '9') \
|| strchr("-_ !?%()[]{}&=+*/.,;:'", (_c)) != NULL)
#define VALID_HEX_CHAR(_c) (('a' <= (_c) && (_c) <= 'f') \
|| ('0' <= (_c) && (_c) <= '9'))
MessageParser::MessageParser()
: _state(wait_opcode), _error(0), _opcode('0'),
_length(0), _has_string(false),
_string_length(0), _tmpval(0), _sign(0)
{
reset();
}
void MessageParser::set_error(char character, int8_t what)
{
#if defined(ARDUINO)
// FIXME
log_print("set_error");
log_print((int) what);
log_print(character);
#endif
(void) character;
_error = what;
_state = wait_opcode;
}
void MessageParser::append_char(char c)
{
if (VALID_STRING_CHAR(c)) {
if (_string_length < PARSER_MAXIMUM_STRING_LENGTH) {
_string[_string_length++] = c;
// Assure that the buffer is always zero-terminted
_string[_string_length] = 0;
} else {
set_error(c, kStringTooLong);
}
} else {
set_error(c, kInvalidString);
}
}
void MessageParser::init_value(char c, int16_t sign)
{
_tmpval = VALUE(c);
_sign = sign;
}
int MessageParser::append_value(int16_t v)
{
int r = -1;
if (_length < PARSER_MAXIMUM_ARGUMENTS) {
_value[_length++] = v;
r = 0;
} else {
set_error('?', kVectorTooLong);
r = -1;
}
return r;
}
int MessageParser::append_digit(char c)
{
int r = 0;
int32_t calcval = 10 * _tmpval + _sign * VALUE(c);
if (calcval > INT16_MAX || calcval < INT16_MIN) {
#if defined(ARDUINO)
// FIXME
log_print("Out of range");
log_print(calcval);
#endif
set_error(c, kValueOutOfRange);
r = -1;
}
else{
_tmpval = (int16_t)calcval;
}
return r;
}
void MessageParser::reset_values()
{
_length = 0;
for (unsigned int i = 0; i < PARSER_MAXIMUM_ARGUMENTS; i++)
_value[i] = 0;
}
void MessageParser::reset_string()
{
_has_string = 0;
_string_length = 0;
for (unsigned int i = 0; i < PARSER_MAXIMUM_STRING_LENGTH+1; i++)
_string[i] = 0;
}
void MessageParser::reset()
{
_state = wait_opcode;
_error = kNoError;
_opcode = 0;
reset_values();
reset_string();
}
bool MessageParser::parse(const char *s, size_t len)
{
bool success = false;
reset();
for (size_t i = 0; i < len-1; i++) {
process(s[i]);
if (_error != 0) {
#if defined(ARDUINO)
// FIXME
log_print(s);
#endif
break;
}
}
if (_error == 0) {
success = process(s[len-1]);
}
return success;
}
bool MessageParser::process(char c)
{
bool has_request = false;
_error = 0;
switch(_state) {
case wait_opcode:
if (VALID_OPCODE(c)) {
_opcode = c;
_state = wait_bracket_or_end;
} else {
set_error(c, kInvalidOpcode);
}
break;
case wait_bracket_or_end:
if (OPENBRACKET(c)) {
_state = wait_value;
} else if (END_MESSAGE(c)) {
has_request = true;
} else {
set_error(c, kUnexpectedChar);
}
break;
case wait_end_message:
if (END_MESSAGE(c)) {
_state = wait_opcode;
has_request = true;
} else {
set_error(c, kUnexpectedChar);
}
break;
case wait_value:
if (MINUS(c)) {
init_value('0', -1);
_state = wait_digit;
} else if (DIGIT(c)) {
init_value(c, 1);
_state = wait_digits_or_comma_or_bracket;
} else if (QUOTE(c)) {
if (_has_string) {
set_error(c, kTooManyStrings);
} else {
_state = wait_string;
}
} else {
set_error(c, kUnexpectedChar);
}
break;
case wait_digit:
if (DIGIT(c)) {
if (append_digit(c) == 0)
_state = wait_digits_or_comma_or_bracket;
} else {
set_error(c, kUnexpectedChar);
}
break;
case wait_digits_or_comma_or_bracket:
if (DIGIT(c)) {
if (append_digit(c) == 0)
_state = wait_digits_or_comma_or_bracket;
} else if (COMMA(c)) {
if (append_value(_tmpval) == 0)
_state = wait_value;
} else if (CLOSEBRACKET(c)) {
if (append_value(_tmpval) == 0)
_state = wait_end_message;
} else {
set_error(c, kUnexpectedChar);
}
break;
case wait_string:
if (QUOTE(c)) {
_has_string = 1;
_state = wait_comma_or_bracket;
} else if (END_MESSAGE(c)) {
set_error(c, kInvalidString);
} else {
append_char(c);
}
break;
case wait_comma_or_bracket:
if (COMMA(c)) {
_state = wait_value;
} else if (CLOSEBRACKET(c)) {
_state = wait_end_message;
} else {
set_error(c, kUnexpectedChar);
}
break;
default:
break;
}
return has_request;
}
}