Skip to content

Commit d2b67ac

Browse files
fixup! fixup! [WIP] Improving parse function in modem class
1 parent c7a83f2 commit d2b67ac

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

libraries/WiFiS3/src/Modem.cpp

+25-26
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
162162
unsigned int result_parse = 0;
163163
bool restart = false;
164164

165+
// I expect the answer to be in this form: "ERROR<CR><LF>" "OK<CR><LF>"
166+
// if prompt == DO_NOT_CHECK_CMD
167+
const bool check_prompt = (prompt != DO_NOT_CHECK_CMD);
165168

166169
if(_serial_debug && _debug_level >= 1) {
167170
_serial_debug->print("RAW: ");
168171
}
169172

170173
unsigned long start_time = millis();
171-
while(state != at_parse_state_t::Completed &&
172-
state != at_parse_state_t::ParseError) {
174+
while(state != at_parse_state_t::Completed) {
173175

174176
if(millis() - start_time > _timeout) {
175177
res = Timeout;
@@ -212,16 +214,23 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
212214
*/
213215

214216
if(c == '+') {
217+
// the answer doesn't match the expected form, we need to restart
218+
restart = !check_prompt;
219+
215220
commandName += c; // prompt includes also '+'
216221
state = at_parse_state_t::Cmd;
217-
} else if(c == RESULT_OK[result_parse]) { // FIXME this should also account for following characters
222+
} else if(c == RESULT_OK[result_parse]) { // FIXME this should also account for following character
223+
// the answer doesn't match the expected form, we need to restart
224+
restart = check_prompt;
225+
218226
state = at_parse_state_t::Ok;
219227
result_parse++;
220228
} else if(c == RESULT_ERROR[result_parse]) { // FIXME this should also account for following characters
229+
// the answer doesn't match the expected form, we need to restart
230+
restart = check_prompt;
231+
221232
state = at_parse_state_t::Error;
222233
result_parse++;
223-
} else {
224-
data_res = "";
225234
}
226235
// if we uncomment this we can force strict response matching
227236
// else {
@@ -238,7 +247,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
238247
if(c == ':' || c == '=') {
239248
commandName += c; // prompt includes also ':'
240249

241-
if (prompt != DO_NOT_CHECK_CMD && commandName != prompt) {
250+
if (check_prompt && commandName != prompt) {
242251
// the response we got is not the one we were expecting, parse the wrong response till the end
243252
// and start the parse of the next response
244253
restart = true;
@@ -333,13 +342,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
333342

334343
if(result_parse == strlen(RESULT_OK)) {
335344
res = Ok;
336-
337-
if(restart) {
338-
state = at_parse_state_t::Begin;
339-
restart = false;
340-
} else {
341-
state = at_parse_state_t::Completed;
342-
}
345+
state = at_parse_state_t::Completed;
343346
}
344347
break;
345348
case at_parse_state_t::Error:
@@ -353,27 +356,23 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
353356

354357
if(result_parse == strlen(RESULT_ERROR)) {
355358
res = Error;
356-
357-
if(restart) {
358-
state = at_parse_state_t::Begin;
359-
restart = false;
360-
} else {
361-
state = at_parse_state_t::Completed;
362-
}
359+
state = at_parse_state_t::Completed;
363360
}
364361
break;
365362
case at_parse_state_t::ParseError:
366363
res = ParseError;
367-
if(restart) {
368-
state = at_parse_state_t::Begin;
369-
restart = false;
370-
} else {
371-
state = at_parse_state_t::Completed;
372-
}
364+
// if we get a parseError, we go back from the beginning and try again to parse, unitl the timeout expires
365+
state = at_parse_state_t::Begin;
366+
restart = false;
373367
break;
374368
case at_parse_state_t::Completed:
375369
break;
376370
}
371+
372+
if(restart && state == at_parse_state_t::Completed) {
373+
state = at_parse_state_t::Begin;
374+
restart = false;
375+
}
377376
}
378377

379378
if(_serial_debug && _debug_level >= 3) {

0 commit comments

Comments
 (0)