mirror of https://github.com/ARMmbed/mbed-os.git
Memory Optimizations and simplifications in ATParser
ATParser had been using std <vector> which had been pulling along a lot of standard C++ stuff. We have an alternative implementation whcih achieves a similar effect but without using <vector>. this saves a bunch of valuable RAM resource. We have also simplified the overall system by introducing proper CR and LF rather than \r\n type of delimiters. This newline simplification is borrowed from retarget.cpp.pull/4119/head
parent
4a04c9ce5d
commit
ea117e5a1a
|
@ -207,6 +207,7 @@ bool ATParser::vsend(const char *command, va_list args)
|
|||
|
||||
bool ATParser::vrecv(const char *response, va_list args)
|
||||
{
|
||||
restart:
|
||||
_aborted = false;
|
||||
// Iterate through each line in the expected response
|
||||
while (response[0]) {
|
||||
|
@ -249,8 +250,6 @@ bool ATParser::vrecv(const char *response, va_list args)
|
|||
// We keep trying the match until we succeed or some other error
|
||||
// derails us.
|
||||
int j = 0;
|
||||
char in_prev = 0;
|
||||
|
||||
|
||||
while (true) {
|
||||
// Receive next character
|
||||
|
@ -270,7 +269,7 @@ bool ATParser::vrecv(const char *response, va_list args)
|
|||
// onto next character
|
||||
continue;
|
||||
} else {
|
||||
in_prev = c;
|
||||
_in_prev = c;
|
||||
}
|
||||
_buffer[offset + j++] = c;
|
||||
_buffer[offset + j] = 0;
|
||||
|
@ -288,7 +287,7 @@ bool ATParser::vrecv(const char *response, va_list args)
|
|||
}
|
||||
// oob may have corrupted non-reentrant buffer,
|
||||
// so we need to set it up again
|
||||
return vrecv(response, args);
|
||||
goto restart;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -369,11 +368,12 @@ bool ATParser::recv(const char *response, ...)
|
|||
// oob registration
|
||||
void ATParser::oob(const char *prefix, Callback<void()> cb)
|
||||
{
|
||||
struct oob oob;
|
||||
oob.len = strlen(prefix);
|
||||
oob.prefix = prefix;
|
||||
oob.cb = cb;
|
||||
_oobs.push_back(oob);
|
||||
struct oob *oob = new struct oob;
|
||||
oob->len = strlen(prefix);
|
||||
oob->prefix = prefix;
|
||||
oob->cb = cb;
|
||||
oob->next = _oobs;
|
||||
_oobs = oob;
|
||||
}
|
||||
|
||||
void ATParser::abort()
|
||||
|
|
|
@ -57,6 +57,7 @@ private:
|
|||
// Parsing information
|
||||
const char *_output_delimiter;
|
||||
int _output_delim_size;
|
||||
char _in_prev;
|
||||
bool _dbg_on;
|
||||
bool _aborted;
|
||||
|
||||
|
@ -64,8 +65,9 @@ private:
|
|||
unsigned len;
|
||||
const char *prefix;
|
||||
mbed::Callback<void()> cb;
|
||||
oob *next;
|
||||
};
|
||||
std::vector<oob> _oobs;
|
||||
oob *_oobs;
|
||||
|
||||
// Prohibiting use of of copy constructor
|
||||
ATParser(const ATParser &);
|
||||
|
@ -83,7 +85,9 @@ public:
|
|||
*/
|
||||
ATParser(FileHandle *fh, const char *output_delimiter="\r", int buffer_size = 256, int timeout = 8000, bool debug = true) :
|
||||
_fh(fh),
|
||||
_buffer_size(buffer_size) {
|
||||
_buffer_size(buffer_size),
|
||||
_in_prev(0),
|
||||
_oobs(NULL) {
|
||||
_buffer = new char[buffer_size];
|
||||
set_timeout(timeout);
|
||||
set_delimiter(output_delimiter);
|
||||
|
@ -94,6 +98,11 @@ public:
|
|||
* Destructor
|
||||
*/
|
||||
~ATParser() {
|
||||
while (_oobs) {
|
||||
struct oob *oob = _oobs;
|
||||
_oobs = oob->next;
|
||||
delete oob;
|
||||
}
|
||||
delete [] _buffer;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue