| 22 | } |
| 23 | |
| 24 | void AtClient::processor(Stream& source, char arrivedChar, uint16_t availableCharsCount) |
| 25 | { |
| 26 | if(!currentCommand.text.length()) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if(state == State::Error) { |
| 31 | // discard input at error state |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | if(currentCommand.onReceive) { |
| 36 | if(currentCommand.onReceive(*this, source)) { |
| 37 | next(); |
| 38 | } |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if(arrivedChar != '\n') { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | commandTimer.stop(); |
| 47 | debug_d("Processing: %d ms, %s", millis(), currentCommand.text.substring(0, 20).c_str()); |
| 48 | |
| 49 | char response[availableCharsCount]; |
| 50 | for(int i = 0; i < availableCharsCount; i++) { |
| 51 | response[i] = stream.read(); |
| 52 | if(response[i] == '\r' || response[i] == '\n') { |
| 53 | response[i] = '\0'; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | debug_d("Got response: %s", response); |
| 58 | |
| 59 | String reply(response); |
| 60 | if(reply.indexOf(AT_REPLY_OK) + reply.indexOf(currentCommand.response2) == -2) { |
| 61 | // we did not get what we wanted. Check if we should repeat. |
| 62 | if(--currentCommand.retries > 0) { |
| 63 | sendDirect(currentCommand); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if(currentCommand.breakOnError) { |
| 68 | state = State::Error; |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if(currentCommand.onComplete) { |
| 74 | if(!currentCommand.onComplete(*this, reply)) { |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | next(); |
| 80 | } |
| 81 | |