From a2cb09990f11a17504608272966f1185b211ce23 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Thu, 7 Sep 2017 11:23:48 -0500 Subject: [PATCH] Add function to process out-of-band data This patch adds the function process_oob() which process out of band data if there is any. If there is no data this function returns immediately. --- ATParser.cpp | 38 ++++++++++++++++++++++++++++++++++++++ ATParser.h | 10 ++++++++++ 2 files changed, 48 insertions(+) diff --git a/ATParser.cpp b/ATParser.cpp index 3e583f3..0acc764 100644 --- a/ATParser.cpp +++ b/ATParser.cpp @@ -334,3 +334,41 @@ void ATParser::oob(const char *prefix, Callback cb) oob.cb = cb; _oobs.push_back(oob); } + +bool ATParser::process_oob() +{ + if (!_serial->readable()) { + return false; + } + + int i = 0; + while (true) { + // Receive next character + int c = getc(); + if (c < 0) { + return false; + } + _buffer[i++] = c; + _buffer[i] = 0; + + // Check for oob data + for (unsigned int j = 0; j < _oobs.size(); j++) { + if (i == (int)_oobs[j].len && memcmp( + _oobs[j].prefix, _buffer, _oobs[j].len) == 0) { + debug_if(dbg_on, "AT! %s\r\n", _oobs[j].prefix); + _oobs[j].cb(); + return true; + } + } + + // Clear the buffer when we hit a newline or ran out of space + // running out of space usually means we ran into binary data + if (i+1 >= _buffer_size || + strcmp(&_buffer[i-_recv_delim_size], _recv_delimiter) == 0) { + + debug_if(dbg_on, "AT< %s", _buffer); + i = 0; + } + } + +} diff --git a/ATParser.h b/ATParser.h index 5d0351e..dbcd579 100644 --- a/ATParser.h +++ b/ATParser.h @@ -256,6 +256,16 @@ class ATParser */ void oob(const char *prefix, mbed::Callback func); + /** + * Process out-of-band data + * + * Process out-of-band data in the receive buffer. This function + * returns immediately if there is no data to process. + * + * @return true if oob data processed, false otherwise + */ + bool process_oob(void); + /** * Attach a callback for out-of-band data *