Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSerial
Dependents: ESP8266 xdot-passthru Lab_10 Lab9 ... more
Fork of ATParser by
Revision 5:26bc9255b751, committed 2015-07-17
- Comitter:
- geky
- Date:
- Fri Jul 17 17:23:57 2015 +0000
- Parent:
- 4:38acbd6f9d9e
- Child:
- 6:51f1171b5ebc
- Commit message:
- Seperated command into send/recv components for seperate use
Changed in this revision
| ATParser.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ATParser.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ATParser.cpp Fri Jul 17 16:38:44 2015 +0000
+++ b/ATParser.cpp Fri Jul 17 17:23:57 2015 +0000
@@ -19,7 +19,6 @@
*/
#include "ATParser.h"
-#include <cstdarg>
// This can be defined to assist in debugging
#define AT_ECHO 1
@@ -102,24 +101,23 @@
return false;
}
-
-bool ATParser::command(const char *command, const char *response, ...) {
- va_list args;
- va_start(args, response);
-
+
+// Command parsing with line handling
+bool ATParser::vsend(const char *command, va_list args) {
flush();
// Create and send command
- if (command) {
- if (vsprintf(_buffer, command, args) < 0 ||
- !_putline(_buffer)) {
- va_end(args);
- return false;
- }
- }
-
+ if (vsprintf(_buffer, command, args) < 0)
+ return false;
+ if (!_putline(_buffer))
+ return false;
+
+ return true;
+}
+
+bool ATParser::vrecv(const char *response, va_list args) {
// Iterate through each line in the expected response
- while (response && response[0]) {
+ while (response[0]) {
// Since response is const, we need to copy it into our buffer to
// add the line's null terminator and clobber value matches with asterisks.
//
@@ -128,8 +126,8 @@
int offset = 0;
while (response[i]) {
- if (memcmp(&response[i-_delim_size], _delimiter, _delim_size) == 0) {
- i += _delim_size;
+ if (memcmp(&response[i+1-_delim_size], _delimiter, _delim_size) == 0) {
+ i++;
break;
} else if (response[i] == '%' &&
response[i+1] != '%' &&
@@ -158,10 +156,8 @@
// derails us.
while (true) {
// Recieve response
- if (!_getline(_buffer+offset, _buffer_size-offset)) {
- va_end(args);
+ if (!_getline(_buffer+offset, _buffer_size-offset))
return false;
- }
int count = -1;
sscanf(_buffer+offset, _buffer, &count);
@@ -184,7 +180,45 @@
}
}
}
-
- va_end(args);
+
+ return true;
+}
+
+bool ATParser::vcommand(const char *command, const char *response, va_list args) {
+ if (command) {
+ if (!vsend(command, args))
+ return false;
+ }
+
+ if (response) {
+ if (!vrecv(response, args))
+ return false;
+ }
+
return true;
}
+
+// Mapping to vararg functions
+bool ATParser::send(const char *command, ...) {
+ va_list args;
+ va_start(args, command);
+ bool res = vsend(command, args);
+ va_end(args);
+ return res;
+}
+
+bool ATParser::recv(const char *response, ...) {
+ va_list args;
+ va_start(args, response);
+ bool res = vrecv(response, args);
+ va_end(args);
+ return res;
+}
+
+bool ATParser::command(const char *command, const char *response, ...) {
+ va_list args;
+ va_start(args, response);
+ bool res = vcommand(command, response, args);
+ va_end(args);
+ return res;
+}
--- a/ATParser.h Fri Jul 17 16:38:44 2015 +0000
+++ b/ATParser.h Fri Jul 17 17:23:57 2015 +0000
@@ -19,7 +19,7 @@
*/
#include "mbed.h"
-#include <stdarg.h>
+#include <cstdarg>
#include "BufferedSerial.h"
@@ -51,10 +51,9 @@
* @param serial serial interface to use for AT commands
* @param buffer_size size of internal buffer for transaction
* @param timeout timeout of the connection
- * @param echo flag to indicate if an echo of sent characters should be expected
* @param delimiter string of characters to use as line delimiters
*/
- ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 3000,
+ ATParser(BufferedSerial *serial, int buffer_size = 256, int timeout = 8000,
const char *delimiter = "\r\n") :
_serial(serial),
_buffer_size(buffer_size) {
@@ -88,22 +87,44 @@
_delimiter = delimiter;
_delim_size = strlen(delimiter);
}
+
+ /**
+ * Sends an AT command
+ *
+ * Sends a formatted command using printf style formatting
+ * @see printf
+ *
+ * @param command printf-like format string of command to send which
+ * is appended with the specified delimiter
+ * @param ... all printf-like arguments to insert into command
+ * @return true only if command is successfully sent
+ */
+ bool send(const char *command, ...);
+ bool vsend(const char *command, va_list args);
+
+ /**
+ * Recieve an AT response
+ *
+ * Recieves a formatted response using scanf style formatting
+ * @see scanf
+ *
+ * Responses are parsed line at a time using the specified delimiter.
+ * Any recieved data that does not match the response is ignored until
+ * a timeout occurs.
+ *
+ * @param response scanf-like format string of response to expect
+ * @param ... all scanf-like arguments to extract from response
+ * @return true only if response is successfully matched
+ */
+ bool recv(const char *response, ...);
+ bool vrecv(const char *response, va_list args);
/**
* Issue AT commands
*
- * Sends formatted command and waits for formatted response.
- * Any recieved data that does not match the specified response
- * is ignored until the timeout has passed.
- *
- * Both the command and response use format strings that are internally
- * passed to printf and scanf respectively.
- * @see printf
- * @see scanf
- *
- * Commands are expected to be formatted with specified delimiters.
- * Sent commands are appended with the delimiter, and responses are
- * seperated by delimiters before attempting to parse.
+ * Issues formatted commands and parses formatted responses.
+ * A command call is identical to a send call followed by a recv call.
+ * @see send, recv
*
* Here are some examples:
* @code
@@ -113,31 +134,32 @@
* @endcode
*
* @param command printf-like format string of command to send
- * @param response scanf-like format string of response to parse
+ * @param response scanf-like format string of response to expect
* @param ... all printf-like arguments to insert into command followed by
- * all scanf-like pointers to destinations for response values
+ * all scanf-like arguments to extract from response
* @return true only if response is successfully matched
*/
bool command(const char *command, const char *response, ...);
+ bool vcommand(const char *command, const char *response, va_list args);
/**
- * Write a single byte to the underlying stream
- *
- * @param c The byte to write
- * @return The byte that was written or -1 during a timeout
- */
+ * Write a single byte to the underlying stream
+ *
+ * @param c The byte to write
+ * @return The byte that was written or -1 during a timeout
+ */
int putc(char c);
/**
- * Get a single byte from the underlying stream
- *
- * @return The byte that was read or -1 during a timeout
- */
+ * Get a single byte from the underlying stream
+ *
+ * @return The byte that was read or -1 during a timeout
+ */
int getc();
/**
- * Flushes the underlying stream
- */
+ * Flushes the underlying stream
+ */
void flush();
};
