Skip to content

Commit

Permalink
fixup! fixup! [WIP] Improving parse function in modem class
Browse files Browse the repository at this point in the history
  • Loading branch information
andreagilardoni committed Jul 19, 2024
1 parent c7a83f2 commit d2b67ac
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions libraries/WiFiS3/src/Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,16 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
unsigned int result_parse = 0;
bool restart = false;

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

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

unsigned long start_time = millis();
while(state != at_parse_state_t::Completed &&
state != at_parse_state_t::ParseError) {
while(state != at_parse_state_t::Completed) {

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

if(c == '+') {
// the answer doesn't match the expected form, we need to restart
restart = !check_prompt;

commandName += c; // prompt includes also '+'
state = at_parse_state_t::Cmd;
} else if(c == RESULT_OK[result_parse]) { // FIXME this should also account for following characters
} else if(c == RESULT_OK[result_parse]) { // FIXME this should also account for following character
// the answer doesn't match the expected form, we need to restart
restart = check_prompt;

state = at_parse_state_t::Ok;
result_parse++;
} else if(c == RESULT_ERROR[result_parse]) { // FIXME this should also account for following characters
// the answer doesn't match the expected form, we need to restart
restart = check_prompt;

state = at_parse_state_t::Error;
result_parse++;
} else {
data_res = "";
}
// if we uncomment this we can force strict response matching
// else {
Expand All @@ -238,7 +247,7 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_
if(c == ':' || c == '=') {
commandName += c; // prompt includes also ':'

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

if(result_parse == strlen(RESULT_OK)) {
res = Ok;

if(restart) {
state = at_parse_state_t::Begin;
restart = false;
} else {
state = at_parse_state_t::Completed;
}
state = at_parse_state_t::Completed;
}
break;
case at_parse_state_t::Error:
Expand All @@ -353,27 +356,23 @@ ModemClass::ParseResult ModemClass::buf_read(const string &prompt, string &data_

if(result_parse == strlen(RESULT_ERROR)) {
res = Error;

if(restart) {
state = at_parse_state_t::Begin;
restart = false;
} else {
state = at_parse_state_t::Completed;
}
state = at_parse_state_t::Completed;
}
break;
case at_parse_state_t::ParseError:
res = ParseError;
if(restart) {
state = at_parse_state_t::Begin;
restart = false;
} else {
state = at_parse_state_t::Completed;
}
// if we get a parseError, we go back from the beginning and try again to parse, unitl the timeout expires
state = at_parse_state_t::Begin;
restart = false;
break;
case at_parse_state_t::Completed:
break;
}

if(restart && state == at_parse_state_t::Completed) {
state = at_parse_state_t::Begin;
restart = false;
}
}

if(_serial_debug && _debug_level >= 3) {
Expand Down

0 comments on commit d2b67ac

Please sign in to comment.