diff --git a/libraries/WiFiS3/src/Modem.cpp b/libraries/WiFiS3/src/Modem.cpp index 9aaa2ce9..d68aff88 100644 --- a/libraries/WiFiS3/src/Modem.cpp +++ b/libraries/WiFiS3/src/Modem.cpp @@ -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" "OK" + // 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; @@ -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 { @@ -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; @@ -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: @@ -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) {