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: ESP8266_WebServer mbed
Fork of WiFiLamp by
Diff: ESP8266_WebServer/ESP8266_WebServer.cpp
- Revision:
- 20:f5a6527bfda6
- Parent:
- 18:cdfe5eb7d3ad
diff -r 7fdccfd5b50b -r f5a6527bfda6 ESP8266_WebServer/ESP8266_WebServer.cpp
--- a/ESP8266_WebServer/ESP8266_WebServer.cpp	Mon Dec 29 21:00:26 2014 +0000
+++ b/ESP8266_WebServer/ESP8266_WebServer.cpp	Wed Dec 31 15:15:50 2014 +0000
@@ -1,27 +1,69 @@
 #include "ESP8266_WebServer.h"
-#include <string>
-#include <map>
 
 ESP8266_WebServer::ESP8266_WebServer(Serial *espUART) {
     serial = espUART;
     rxptr = buffer;
     debugSerial = NULL;
+    reqMode = false;
+    reqLen = 0;
 }
 
 void ESP8266_WebServer::rxint(void) {
     char c = serial->getc();
     if( (c != 0x0A) && (c != 0x0D) && ((c < 0x20) || (c > 0x80)) ) return;
-    
-    if( debugSerial != NULL ) {
+    if( !reqMode && c == '+' ) {
+        if( echoMode ) {
+            debugSerial->putc('~');
+        }
+        rxptrStored = rxptr;
+        rxptr = reqBuffer;
+        reqMode = true;
+    }
+    if( echoMode ) {
         debugSerial->putc(c);
     }
+    if( reqMode && reqLen == 0 && c == ':' ) {
+        if( echoMode ) {
+            debugSerial->putc('!');
+        }
+        int numMatched = sscanf(reqBuffer,"+IPD,%*d,%d%n", &reqLen, &ipdLen);
+        if( numMatched < 1 ) {
+            reqMode = false;
+            reqLen = 0;
+            rxptr = rxptrStored;
+            return;
+        }
+        reqLen += ipdLen + 1;
+    }
+    if( echoMode ) {
+        debugSerial->putc('@');
+    }
     *rxptr = c;
     rxptr++;
-    *rxptr = 0;    
+    *rxptr = 0;
+    if( reqMode && reqLen > 0 ) {
+        if( echoMode ) {
+            debugSerial->putc('#');
+        }
+        if( (int)(rxptr - reqBuffer) == reqLen ) {
+            // Process it
+            queueRequest();
+            // Return to normal buffer mode
+            reqMode = false;
+            reqLen = 0;
+            rxptr = rxptrStored;
+        }
+    }
+}
+
+void ESP8266_WebServer::debugBuffers(Serial* target) {
+    target->printf("\r\n\r\nRequest Buffer '%s'\r\nReqLen=%d,ReqMode=%d\r\n", reqBuffer, reqLen, reqMode);    
 }
 
 void ESP8266_WebServer::readBuffer(void) {
     strncpy(reply, buffer, 1024);
+    while(reqMode == true ) { wait_ms(10); }
+    
     rxptr = buffer;
     *rxptr = 0;
 }
@@ -96,58 +138,53 @@
     readBuffer();
 }
 
+void ESP8266_WebServer::queueRequest(void) {
+    if( strstr(reqBuffer, "HTTP") != NULL ) {
+        ESP8266_WebRequest* request = new ESP8266_WebRequest(reqBuffer, debugSerial); 
+        
+        incoming.push(request);
+    }
+}
+
 ESP8266_WebRequest* ESP8266_WebServer::GetRequest(void)
 {
-    if( (string_waiting("+IPD") == 1) && (string_waiting("\r\nOK\r\n") == 1) ) {
-        if( debugSerial != NULL ) {
-            debugSerial->printf("\r\nGot Data\r\n");
-        }
-        readBuffer();
-
-        char* ipdPacket = strstr(reply, "+IPD");
-        int bytesRecv, ipdLen, linkID;
-        int numMatched = sscanf(ipdPacket,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
-        if( numMatched != 2 ) {
-            if( debugSerial != NULL ) {
-                debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
-            }
-            return false;
-        }
-
-        if( debugSerial != NULL ) {
-            debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
-        }
-        if( strstr(ipdPacket, "HTTP") != NULL ) {
-            if( debugSerial != NULL ) {
-                debugSerial->printf("Got HTTP Request\r\n");
-            }
-            char* httpPacket = ipdPacket + ipdLen;
-            if( debugSerial != NULL ) {
-                debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
-            }
-            ESP8266_WebRequest* request = new ESP8266_WebRequest(linkID, httpPacket, debugSerial); 
-            
-            return request;
-        }
+    if( incoming.empty() == false ) {
+        ESP8266_WebRequest* temp = incoming.front();
+        incoming.pop();
+        temp->Read();
+        
+        return temp;
     }
-    
+        
     return NULL;
 }
 
 void ESP8266_WebServer::sendResponse(int linkID, int numBytes) {
+    bool sent = false;
+    
+    readBuffer();
     if( debugSerial != NULL ) {
-        debugSerial->printf("HTTP Reply Packet(%d bytes): %s\r\n", numBytes, response);
+        debugSerial->printf("HTTP Reply Packet(%d bytes)\r\n", numBytes);
     }
-    serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes);
-    wait_ms(100);
-    if( (string_waiting(">") == 1) ) {
-        for( int i=0; i<numBytes; i++ ) {
-            serial->putc(response[i]);
-        }
-        while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
-            wait_ms(10);
+    while( sent == false ) {
+        while( reqMode == true ) { wait_ms(1); }
+        serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes);
+        wait_ms(100);
+        if( (string_waiting(">") == 1) ) {
+            char* error = strstr(buffer, "ERROR\r\n");
+            if( error != NULL ) { continue; }
+            for( int i=0; i<numBytes; i++ ) {
+                serial->putc(response[i]);
+            }
+            while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
+                wait_ms(10);
+            }
+            error = strstr(buffer, "ERROR\r\n");
+            if( error != NULL ) { continue; }
+            sent = true;
         }
     }
+    readBuffer();
 }
 
 void ESP8266_WebServer::SendError(int linkID, std::string error) {
@@ -169,52 +206,82 @@
     sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\n\r\n", mimeType, replySize);
     sendResponse(linkID, strlen(response));
     memcpy(response, reply, replySize);
-    sendResponse(linkID, strlen(response));
+    sendResponse(linkID, replySize);
 }
 
-ESP8266_WebRequest::ESP8266_WebRequest(int linkID, const char* httpPacket, Serial* debugSerial) {
-    int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
+ESP8266_WebRequest::ESP8266_WebRequest(const char* packet, Serial* debug) {
+    int sz = strlen(packet);
+    data = (char *)malloc(sz+1);
+    memcpy(data, packet, sz+1);
+    debugSerial = debug;
+}
+
+void ESP8266_WebRequest::Read(void) {
+    int bytesRecv, ipdLen, linkID;
+    int numMatched = sscanf(data,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
     if( numMatched != 2 ) {
         if( debugSerial != NULL ) {
-            debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
+            debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
         }
         return;
     }
+
     if( debugSerial != NULL ) {
-        debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
+        debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
     }
-
-    LinkID = linkID;
-    Method = httpMethod;
-    URI = httpURI;
-    int pos = URI.find('?');
-    if(pos != string::npos ) {
-        string params = URI.substr(pos+1);
-        URI = URI.substr(0,pos);
+    if( strstr(data, "HTTP") != NULL ) {
+        if( debugSerial != NULL ) {
+            debugSerial->printf("Got HTTP Request\r\n");
+        }
+        char* httpPacket = data + ipdLen;
         if( debugSerial != NULL ) {
-            debugSerial->printf("HTTP %s %s\r\n", URI.c_str(), params.c_str());
+            debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
+        }
+        int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
+        if( numMatched != 2 ) {
+            if( debugSerial != NULL ) {
+                debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
+            }
+            return;
         }
-        pos = params.find('=');
-        while( pos != string::npos ) {
-            string name = params.substr(0,pos);
-            string value = params.substr(pos+1);
-            pos = params.find('&');
-            if( pos == string::npos )
-            {
+        
+        LinkID = linkID;
+        Method = httpMethod;
+        URI = httpURI;
+        int pos = URI.find('?');
+        if(pos != string::npos ) {
+            string params = URI.substr(pos+1);
+            URI = URI.substr(0,pos);
+            pos = params.find('=');
+            while( pos != string::npos ) {
+                string name = params.substr(0,pos);
+                string value = params.substr(pos+1);
+                pos = params.find('&');
+                if( pos == string::npos )
+                {
+                    if( debugSerial != NULL ) {
+                        debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
+                    }
+                    Parameters[name] = value;
+                    break;
+                }
+                
+                value = value.substr(0,value.find('&'));
+                params = params.substr(pos+1);
+                pos = params.find('=');
                 if( debugSerial != NULL ) {
                     debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
                 }
                 Parameters[name] = value;
-                break;
             }
-            
-            value = value.substr(0,value.find('&'));
-            params = params.substr(pos+1);
-            pos = params.find('=');
-            if( debugSerial != NULL ) {
-                debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
-            }
-            Parameters[name] = value;
+        }
+        if( debugSerial != NULL ) {
+            debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
         }
     }
+}
+
+ESP8266_WebRequest::~ESP8266_WebRequest()
+{
+    free(data);
 }
\ No newline at end of file
    