now this shit works
Fork of ESP8266NodeMCUInterface by
Revision 45:3cfb7406d993, committed 2015-06-04
- Comitter:
- geky
- Date:
- Thu Jun 04 19:26:35 2015 +0000
- Parent:
- 44:16da10e7b3f7
- Child:
- 47:04632d22a723
- Commit message:
- Sanitized both transmitted and recieved strings
Changed in this revision
| ESP8266/ESP8266.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ESP8266/ESP8266.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ESP8266/ESP8266.cpp Wed Jun 03 21:44:20 2015 +0000
+++ b/ESP8266/ESP8266.cpp Thu Jun 04 19:26:35 2015 +0000
@@ -137,8 +137,14 @@
// Setup functions for sending and recieving characters on the esp side
if (!(command("cm='';") &&
command("function cs(n) c:send(n) end;") &&
- command("function cr(n) print(cm:sub(1,n)); cm=cm:sub(n+1,-1) end;") &&
command("function ca() print(#cm) end;") &&
+ command("function cr(n) "
+ "d=cm:sub(1,n):gsub('.', function(s) "
+ "return s.format('\\\\%03d', s:byte(1)) "
+ "end);"
+ "cm=cm:sub(n+1,-1);"
+ "print(d) "
+ "end;") &&
command("c:on('receive',function(c,n) cm=cm..n end)") &&
execute()))
return false;
@@ -183,10 +189,22 @@
}
bool ESP8266::send(const char *buffer, int len) {
- if (!(command("cs('") &&
- payload(buffer, len) &&
- command("')") &&
- execute()))
+ if (!command("cs('"))
+ return false;
+
+ for (int i = 0; i < len; i++) {
+ int a = buffer[i] / 100;
+ int b = (buffer[i] - a*100) / 10;
+ int c = (buffer[i] - a*100 - b*10);
+
+ if (serialputc('\\') < 0 ||
+ serialputc(a + '0') < 0 ||
+ serialputc(b + '0') < 0 ||
+ serialputc(c + '0') < 0)
+ return false;
+ }
+
+ if (!(command("')") && execute()))
return false;
return true;
@@ -198,11 +216,35 @@
if (!(command("cr(") &&
command(len_buf) &&
- command(")") &&
- execute(buffer, len)))
+ command(")")))
+ return false;
+
+ if (!(command("\r\n") && discardEcho()))
return false;
+
+ // Read in response
+ for (int i = 0; i < *len; i++) {
+ int e = serialgetc();
+
+ if (e == '\r') {
+ *len = i;
+ break;
+ } else if (e != '\\') {
+ return false;
+ }
+
+ int a = serialgetc();
+ int b = serialgetc();
+ int c = serialgetc();
+
+ if (a < 0 || b < 0 || c < 0)
+ return false;
+
+ buffer[i] = (a-'0')*100 + (b-'0')*10 + (c-'0');
+ }
- return true;
+ // Flush to next prompt
+ return flush();
}
int ESP8266::putc(char c) {
@@ -322,6 +364,17 @@
if (c < 0)
return false;
+ else if (c == '\n')
+ return true;
+ }
+}
+
+bool ESP8266::flush() {
+ while (true) {
+ int c = serialgetc();
+
+ if (c < 0)
+ return false;
else if (c == '>')
return true;
}
@@ -336,39 +389,13 @@
}
return true;
-}
-
-bool ESP8266::payload(const char *data, int len) {
- for (int i = 0; i < len; i++) {
- int a = data[i] / 100;
- int b = (data[i] - a*100) / 10;
- int c = (data[i] - a*100 - b*10);
-
- if (serialputc('\\') < 0 ||
- serialputc(a + '0') < 0 ||
- serialputc(b + '0') < 0 ||
- serialputc(c + '0') < 0)
- return false;
- }
-
- return true;
-}
+}
bool ESP8266::execute(char *resp_buf, int *resp_len) {
// Finish command with a newline
- if (serialputc('\r') < 0 || serialputc('\n') < 0)
+ if (!(command("\r\n") && discardEcho()))
return false;
- // Drop echoed string
- while (true) {
- int c = serialgetc();
-
- if (c < 0)
- return false;
- else if (c == '\n')
- break;
- }
-
// Read in response if any
if (resp_buf && resp_len) {
int i;
@@ -390,6 +417,5 @@
DBG("command response:\t %.*s", *resp_len, resp_buf);
}
- // Flush to next prompt
- return discardEcho();
+ return flush();
}
--- a/ESP8266/ESP8266.h Wed Jun 03 21:44:20 2015 +0000
+++ b/ESP8266/ESP8266.h Thu Jun 04 19:26:35 2015 +0000
@@ -194,6 +194,13 @@
bool discardEcho();
/**
+ * Flushes to next prompt
+ *
+ * @return true if successful
+ */
+ bool flush();
+
+ /**
* Send part of a command to the wifi module.
*
* @param cmd string to be sent
@@ -204,15 +211,6 @@
bool command(const char *cmd);
/**
- * Sanitizes and sends payload to wifi module
- *
- * @param data data to send
- * @param len length of data
- * @return true if successful
- */
- bool payload(const char *data, int len);
-
- /**
* Execute the command sent by command
*
* @param resp_buf pointer to buffer to store response from the wifi module
