From 7ece3e436dbf3c42099661b3c614760f17a6dc3b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 1 May 2015 09:53:19 +0800 Subject: [PATCH 1/3] wiring_digital: fix interrupts, initialize pins to INPUT on startup --- .../cores/esp8266/core_esp8266_wiring_digital.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring_digital.c b/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring_digital.c index 51f049eb77..5ac670e630 100644 --- a/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring_digital.c +++ b/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring_digital.c @@ -154,7 +154,15 @@ extern void __detachInterrupt(uint8_t pin) { } void initPins() { - ETS_GPIO_INTR_ATTACH(interrupt_handlers, &interrupt_reg); + for (int i = 0; i <= 5; ++i) { + pinMode(i, INPUT); + } + // pins 6-11 are used for the SPI flash interface + for (int i = 12; i <= 16; ++i) { + pinMode(i, INPUT); + } + + ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg); ETS_GPIO_INTR_ENABLE(); } From 2ccfb32a5a05fc352fc79d3f03c1f18d2deb377e Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 1 May 2015 11:15:22 +0800 Subject: [PATCH 2/3] ESP8266WebServer code review, fix indentation, add keywords --- .../examples/HelloServer/HelloServer.ino | 15 +- .../libraries/ESP8266WebServer/keywords.txt | 4 + .../ESP8266WebServer/src/ESP8266WebServer.cpp | 483 +++++++++--------- .../ESP8266WebServer/src/ESP8266WebServer.h | 87 ++-- 4 files changed, 301 insertions(+), 288 deletions(-) diff --git a/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino b/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino index 5ed9e90dcb..10da0e9b3d 100644 --- a/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino +++ b/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino @@ -9,24 +9,23 @@ MDNSResponder mdns; ESP8266WebServer server(80); -void handle_root() { +void handleRoot() { server.send(200, "text/plain", "hello from esp8266!"); } -bool handle_not_found(){ +void handleNotFound(){ String message = "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; + message += "\nNot Found!\n\n"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; inext; - delete handler; - handler = next; - } + if (!_firstHandler) + return; + RequestHandler* handler = _firstHandler; + while (handler) { + RequestHandler* next = handler->next; + delete handler; + handler = next; + } } void ESP8266WebServer::begin() { - _server.begin(); + _server.begin(); } void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler) { - on(uri, HTTP_ANY, handler); + on(uri, HTTP_ANY, handler); } void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn) { - RequestHandler* handler = new RequestHandler(fn, uri, method); - if (!_lastHandler) { - _firstHandler = handler; - _lastHandler = handler; - } - else { - _lastHandler->next = handler; - _lastHandler = handler; - } + RequestHandler* handler = new RequestHandler(fn, uri, method); + if (!_lastHandler) { + _firstHandler = handler; + _lastHandler = handler; + } + else { + _lastHandler->next = handler; + _lastHandler = handler; + } } void ESP8266WebServer::handleClient() { - WiFiClient client = _server.available(); - if (!client) { - return; - } + WiFiClient client = _server.available(); + if (!client) { + return; + } #ifdef DEBUG - Serial.println("New client"); + Serial.println("New client"); #endif - // Wait for data from client to become available - while(client.connected() && !client.available()){ - delay(1); - } - - // Read the first line of HTTP request - String req = client.readStringUntil('\r'); - client.readStringUntil('\n'); - - HTTPMethod method = HTTP_GET; - if (req.startsWith("POST")) { - method = HTTP_POST; - } - - - // First line of HTTP request looks like "GET /path HTTP/1.1" - // Retrieve the "/path" part by finding the spaces - int addr_start = req.indexOf(' '); - int addr_end = req.indexOf(' ', addr_start + 1); - if (addr_start == -1 || addr_end == -1) { + // Wait for data from client to become available + while(client.connected() && !client.available()){ + delay(1); + } + + // Read the first line of HTTP request + String req = client.readStringUntil('\r'); + client.readStringUntil('\n'); + + HTTPMethod method = HTTP_GET; + if (req.startsWith("POST")) { + method = HTTP_POST; + } + + // First line of HTTP request looks like "GET /path HTTP/1.1" + // Retrieve the "/path" part by finding the spaces + int addr_start = req.indexOf(' '); + int addr_end = req.indexOf(' ', addr_start + 1); + if (addr_start == -1 || addr_end == -1) { #ifdef DEBUG - Serial.print("Invalid request: "); - Serial.println(req); + Serial.print("Invalid request: "); + Serial.println(req); #endif - return; - } - - req = req.substring(addr_start + 1, addr_end); - - String formData; - if (method == HTTP_POST) - { - int contentLength = -1; - int headerCount = 0; - while(headerCount < 1024) { // there shouldn't be that much really - String line = client.readStringUntil('\r'); - client.readStringUntil('\n'); - - if (line.length() > 0) { // this is a header - ++headerCount; - if (contentLength < 0 && line.startsWith("Content-Length")) { - // get content length from the header - int valuePos = line.indexOf(' ', 14); - if (valuePos > 0) { - String valueStr = line.substring(valuePos+1); - contentLength = valueStr.toInt(); + return; + } + + req = req.substring(addr_start + 1, addr_end); + + String formData; + if (method == HTTP_POST) { + int contentLength = -1; + int headerCount = 0; + while(headerCount < 1024) { // there shouldn't be that much really + String line = client.readStringUntil('\r'); + client.readStringUntil('\n'); + + if (line.length() > 0) { // this is a header + ++headerCount; + if (contentLength < 0 && line.startsWith("Content-Length")) { + // get content length from the header + int valuePos = line.indexOf(' ', 14); + if (valuePos > 0) { + String valueStr = line.substring(valuePos+1); + contentLength = valueStr.toInt(); #ifdef DEBUG - Serial.print("Content-Length: "); - Serial.println(contentLength); + Serial.print("Content-Length: "); + Serial.println(contentLength); #endif - } - } - } - else { - break; - } - } + } + } + } + else { + break; + } + } #ifdef DEBUG - Serial.print("headerCount="); - Serial.println(headerCount); + Serial.print("headerCount="); + Serial.println(headerCount); #endif - if (contentLength >= 0) { - formData = ""; - int n = 0; // timeout counter - while (formData.length() < contentLength && ++n < 3) - formData += client.readString(); - } - else { - formData = client.readStringUntil('\r'); // will return after timing out once - } - // read form data - // formData = - } - else if (method == HTTP_GET) - { - int args_start = req.indexOf('?'); - if (args_start != -1) - { - formData = req.substring(args_start + 1); - req = req.substring(0, args_start); - } - } - - client.flush(); + if (contentLength >= 0) { + formData = ""; + int n = 0; // timeout counter + while (formData.length() < contentLength && ++n < 3) + formData += client.readString(); + } + else { + formData = client.readStringUntil('\r'); // will return after timing out once + } + } + else if (method == HTTP_GET) { + int args_start = req.indexOf('?'); + if (args_start != -1) { + formData = req.substring(args_start + 1); + req = req.substring(0, args_start); + } + } + + client.flush(); #ifdef DEBUG - Serial.print("Request: "); - Serial.println(req); - Serial.print("Args: "); - Serial.println(formData); + Serial.print("Request: "); + Serial.println(req); + Serial.print("Args: "); + Serial.println(formData); #endif - _parseArguments(formData); - _handleRequest(client, req, method); + _parseArguments(formData); + _handleRequest(client, req, method); } void ESP8266WebServer::send(int code, const char* content_type, String content) { - String response = "HTTP/1.1 "; - response += String(code); - response += " "; - if (code == 200) - response += "OK"; - else if (code == 404) - response += "Not found"; - response += "\r\n"; - if (!content_type) - content_type = "text/html"; - response += "Content-Type: "; - response += content_type; - response += "\r\n\r\n"; - response += content; - _currentClient.print(response); + String response = "HTTP/1.1 "; + response += String(code); + response += " "; + response += _responseCodeToString(code); + response += "\r\n"; + + if (!content_type) + content_type = "text/html"; + _appendHeader(response, "Content-Type", content_type); + + response += "\r\n"; + response += content; + _currentClient.print(response); } -String ESP8266WebServer::arg(const char* name) -{ - for (int i = 0; i < _currentArgCount; ++i) { - if (_currentArgs[i].key == name) - return _currentArgs[i].value; - } - return String(); +String ESP8266WebServer::arg(const char* name) { + for (int i = 0; i < _currentArgCount; ++i) { + if (_currentArgs[i].key == name) + return _currentArgs[i].value; + } + return String(); } -String ESP8266WebServer::arg(int i) -{ - if (i < _currentArgCount) - return _currentArgs[i].value; - return String(); +String ESP8266WebServer::arg(int i) { + if (i < _currentArgCount) + return _currentArgs[i].value; + return String(); } -String ESP8266WebServer::argName(int i) -{ - if (i < _currentArgCount) - return _currentArgs[i].key; - return String(); +String ESP8266WebServer::argName(int i) { + if (i < _currentArgCount) + return _currentArgs[i].key; + return String(); } -int ESP8266WebServer::args(){ +int ESP8266WebServer::args() { return _currentArgCount; } -bool ESP8266WebServer::hasArg(const char* name) -{ - for (int i = 0; i < _currentArgCount; ++i) { - if (_currentArgs[i].key == name) - return true; - } - return false; +bool ESP8266WebServer::hasArg(const char* name) { + for (int i = 0; i < _currentArgCount; ++i) { + if (_currentArgs[i].key == name) + return true; + } + return false; } -void ESP8266WebServer::_parseArguments(String data) -{ - if (_currentArgs) - delete[] _currentArgs; - _currentArgs = 0; - if (data.length() == 0) { - _currentArgCount = 0; - return; - } - _currentArgCount = 1; - - for (int i = 0; i < data.length(); ) { - i = data.indexOf('&', i); - if (i == -1) - break; - ++i; - ++_currentArgCount; - } +void ESP8266WebServer::_parseArguments(String data) { + if (_currentArgs) + delete[] _currentArgs; + _currentArgs = 0; + if (data.length() == 0) { + _currentArgCount = 0; + return; + } + _currentArgCount = 1; + + for (int i = 0; i < data.length(); ) { + i = data.indexOf('&', i); + if (i == -1) + break; + ++i; + ++_currentArgCount; + } #ifdef DEBUG - Serial.print("args count: "); - Serial.println(_currentArgCount); + Serial.print("args count: "); + Serial.println(_currentArgCount); #endif - _currentArgs = new RequestArgument[_currentArgCount]; - int pos = 0; - int iarg; - for (iarg = 0; iarg < _currentArgCount;) { - int equal_sign_index = data.indexOf('=', pos); - int next_arg_index = data.indexOf('&', pos); + _currentArgs = new RequestArgument[_currentArgCount]; + int pos = 0; + int iarg; + for (iarg = 0; iarg < _currentArgCount;) { + int equal_sign_index = data.indexOf('=', pos); + int next_arg_index = data.indexOf('&', pos); #ifdef DEBUG - Serial.print("pos "); - Serial.print(pos); - Serial.print("=@ "); - Serial.print(equal_sign_index); - Serial.print(" &@ "); - Serial.println(next_arg_index); + Serial.print("pos "); + Serial.print(pos); + Serial.print("=@ "); + Serial.print(equal_sign_index); + Serial.print(" &@ "); + Serial.println(next_arg_index); #endif - if ((equal_sign_index == -1) || ((equal_sign_index > next_arg_index) && (next_arg_index != -1))) { + if ((equal_sign_index == -1) || ((equal_sign_index > next_arg_index) && (next_arg_index != -1))) { #ifdef DEBUG - Serial.print("arg missing value: "); - Serial.println(iarg); + Serial.print("arg missing value: "); + Serial.println(iarg); #endif - if (next_arg_index == -1) - break; - pos = next_arg_index + 1; - continue; - } - RequestArgument& arg = _currentArgs[iarg]; - arg.key = data.substring(pos, equal_sign_index); - arg.value = data.substring(equal_sign_index + 1, next_arg_index); + if (next_arg_index == -1) + break; + pos = next_arg_index + 1; + continue; + } + RequestArgument& arg = _currentArgs[iarg]; + arg.key = data.substring(pos, equal_sign_index); + arg.value = data.substring(equal_sign_index + 1, next_arg_index); #ifdef DEBUG - Serial.print("arg "); - Serial.print(iarg); - Serial.print(" key: "); - Serial.print(arg.key); - Serial.print(" value: "); - Serial.println(arg.value); + Serial.print("arg "); + Serial.print(iarg); + Serial.print(" key: "); + Serial.print(arg.key); + Serial.print(" value: "); + Serial.println(arg.value); #endif - ++iarg; - if (next_arg_index == -1) - break; - pos = next_arg_index + 1; - } - _currentArgCount = iarg; + ++iarg; + if (next_arg_index == -1) + break; + pos = next_arg_index + 1; + } + _currentArgCount = iarg; #ifdef DEBUG - Serial.print("args count: "); - Serial.println(_currentArgCount); + Serial.print("args count: "); + Serial.println(_currentArgCount); #endif } -void ESP8266WebServer::onNotFound(TNotFoundHandlerFunction fn){ +void ESP8266WebServer::onNotFound(THandlerFunction fn) { _notFoundHandler = fn; } void ESP8266WebServer::_handleRequest(WiFiClient& client, String uri, HTTPMethod method) { - _currentClient = client; - _currentUri = uri; - _currentMethod = method; + _currentClient = client; + _currentUri = uri; + _currentMethod = method; - RequestHandler* handler; - for (handler = _firstHandler; handler; handler = handler->next) - { - if (handler->method != HTTP_ANY && handler->method != method) - continue; + RequestHandler* handler; + for (handler = _firstHandler; handler; handler = handler->next) + { + if (handler->method != HTTP_ANY && handler->method != method) + continue; - if (handler->uri != uri) - continue; + if (handler->uri != uri) + continue; - handler->fn(); - break; - } + handler->fn(); + break; + } - if (!handler){ + if (!handler){ #ifdef DEBUG - Serial.println("request handler not found"); + Serial.println("request handler not found"); #endif - if(!_notFoundHandler || !_notFoundHandler()) + + if(_notFoundHandler) { + _notFoundHandler(); + } + else { send(404, "text/plain", String("Not found: ") + uri); - } + } + } + + _currentClient = WiFiClient(); + _currentUri = String(); + +} + +const char* ESP8266WebServer::_responseCodeToString(int code) { + switch (code) { + case 200: return "OK"; + case 404: return "Not found"; + default: return ""; + } +} - _currentClient = WiFiClient(); - _currentUri = String(); - +void ESP8266WebServer::_appendHeader(String& response, const char* name, const char* value) { + response += name; + response += ": "; + response += value; + response += "\r\n"; } diff --git a/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 2c30004477..3b31eab5a7 100644 --- a/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -32,56 +32,57 @@ class ESP8266WebServer { public: - ESP8266WebServer(int port = 80); - ~ESP8266WebServer(); - - void begin(); - void handleClient(); - - typedef std::function THandlerFunction; - typedef std::function TNotFoundHandlerFunction; - void on(const char* uri, THandlerFunction handler); - void on(const char* uri, HTTPMethod method, THandlerFunction fn); - void onNotFound(TNotFoundHandlerFunction fn);//called when handler is not assigned - - String uri() { return _currentUri; } - HTTPMethod method() { return _currentMethod; } - WiFiClient client() { return _currentClient; } - - String arg(const char* name);// get request argument value - String arg(int i);// get request argument value buy number - String argName(int i);// get request argument name buy number - int args();//get arguments count - bool hasArg(const char* name);//check if argument exists + ESP8266WebServer(int port = 80); + ~ESP8266WebServer(); + + void begin(); + void handleClient(); + + typedef std::function THandlerFunction; + void on(const char* uri, THandlerFunction handler); + void on(const char* uri, HTTPMethod method, THandlerFunction fn); + void onNotFound(THandlerFunction fn); //called when handler is not assigned + + String uri() { return _currentUri; } + HTTPMethod method() { return _currentMethod; } + WiFiClient client() { return _currentClient; } - // send response to the client - // code - HTTP response code, can be 200 or 404 - // content_type - HTTP content type, like "text/plain" or "image/png" - // content - actual content body - void send(int code, const char* content_type = NULL, String content = String("")); + String arg(const char* name); // get request argument value by name + String arg(int i); // get request argument value by number + String argName(int i); // get request argument name by number + int args(); // get arguments count + bool hasArg(const char* name); // check if argument exists + + // send response to the client + // code - HTTP response code, can be 200 or 404 + // content_type - HTTP content type, like "text/plain" or "image/png" + // content - actual content body + void send(int code, const char* content_type = NULL, String content = String("")); protected: - void _handleRequest(WiFiClient& client, String uri, HTTPMethod method); - void _parseArguments(String data); - - struct RequestHandler; - struct RequestArgument { - String key; - String value; - }; + void _handleRequest(WiFiClient& client, String uri, HTTPMethod method); + void _parseArguments(String data); + static const char* _responseCodeToString(int code); + static void _appendHeader(String& response, const char* name, const char* value); + + struct RequestHandler; + struct RequestArgument { + String key; + String value; + }; - WiFiServer _server; + WiFiServer _server; - WiFiClient _currentClient; - HTTPMethod _currentMethod; - String _currentUri; + WiFiClient _currentClient; + HTTPMethod _currentMethod; + String _currentUri; - size_t _currentArgCount; - RequestArgument* _currentArgs; + size_t _currentArgCount; + RequestArgument* _currentArgs; - RequestHandler* _firstHandler; - RequestHandler* _lastHandler; - TNotFoundHandlerFunction _notFoundHandler; + RequestHandler* _firstHandler; + RequestHandler* _lastHandler; + THandlerFunction _notFoundHandler; }; From f0bc116ebb8f88fb9c5fecaa5b745c46e1509542 Mon Sep 17 00:00:00 2001 From: tsrman Date: Fri, 1 May 2015 11:49:57 +0700 Subject: [PATCH 3/3] Update build.xml fix launch4j download url --- build/build.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/build.xml b/build/build.xml index 934982707f..20566b252d 100644 --- a/build/build.xml +++ b/build/build.xml @@ -784,12 +784,12 @@ - + - +