From f2f1fad298db9a94151268e4de2bf4f677542805 Mon Sep 17 00:00:00 2001 From: ficeto Date: Tue, 12 May 2015 19:25:37 +0300 Subject: [PATCH 1/2] add TCP_NODELAY control --- libraries/ESP8266WiFi/src/WiFiServer.cpp | 11 +++++++++++ libraries/ESP8266WiFi/src/WiFiServer.h | 2 ++ libraries/ESP8266WiFi/src/include/ClientContext.h | 13 ++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/WiFiServer.cpp b/libraries/ESP8266WiFi/src/WiFiServer.cpp index 1ed2a64c1..69ad4ff81 100644 --- a/libraries/ESP8266WiFi/src/WiFiServer.cpp +++ b/libraries/ESP8266WiFi/src/WiFiServer.cpp @@ -73,6 +73,17 @@ void WiFiServer::begin() tcp_arg(listen_pcb, (void*) this); } +void WiFiServer::setNoDelay(bool nodelay){ + if(!_pcb) return; + if(nodelay) tcp_nagle_disable(_pcb); + else tcp_nagle_enable(_pcb); +} + +bool WiFiServer::getNoDelay(){ + if(!_pcb) return false; + return tcp_nagle_disabled(_pcb); +} + extern "C" uint32_t esp_micros_at_task_start(); bool WiFiServer::hasClient(){ diff --git a/libraries/ESP8266WiFi/src/WiFiServer.h b/libraries/ESP8266WiFi/src/WiFiServer.h index 7e7b0491b..3dd02da7d 100644 --- a/libraries/ESP8266WiFi/src/WiFiServer.h +++ b/libraries/ESP8266WiFi/src/WiFiServer.h @@ -46,6 +46,8 @@ public: WiFiClient available(uint8_t* status = NULL); bool hasClient(); void begin(); + void setNoDelay(bool nodelay); + bool getNoDelay(); virtual size_t write(uint8_t); virtual size_t write(const uint8_t *buf, size_t size); uint8_t status(); diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index 0001ef08b..79c68127f 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -98,7 +98,18 @@ class ClientContext { delete this; } } - + + void setNoDelay(bool nodelay){ + if(!_pcb) return; + if(nodelay) tcp_nagle_disable(_pcb); + else tcp_nagle_enable(_pcb); + } + + bool getNoDelay(){ + if(!_pcb) return false; + return tcp_nagle_disabled(_pcb); + } + uint32_t getRemoteAddress() { if(!_pcb) return 0; From 7512339b0cb687e39dc6df2e3a757a3e4dde7719 Mon Sep 17 00:00:00 2001 From: ficeto Date: Tue, 12 May 2015 20:41:31 +0300 Subject: [PATCH 2/2] remove qsort dependency and add TelnetToSerial example --- cores/esp8266/core_esp8266_wiring_pwm.c | 23 ++--- .../WiFiTelnetToSerial/WiFiTelnetToSerial.ino | 90 +++++++++++++++++++ 2 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino diff --git a/cores/esp8266/core_esp8266_wiring_pwm.c b/cores/esp8266/core_esp8266_wiring_pwm.c index 5552d53b4..b175f6e55 100644 --- a/cores/esp8266/core_esp8266_wiring_pwm.c +++ b/cores/esp8266/core_esp8266_wiring_pwm.c @@ -33,18 +33,19 @@ uint16_t pwm_steps[17]; uint8_t pwm_steps_len = 0; uint32_t pwm_steps_mask[17]; -int pwm_sort_asc(const void* a, const void* b){ - return (*((uint16_t*)a) > *((uint16_t*)b)) - (*((uint16_t*)a) < *((uint16_t*)b)); -} - int pwm_sort_array(uint16_t a[], uint16_t al){ - qsort(a, al, sizeof(uint16_t), pwm_sort_asc); - int i; - int bl = 1; - for(i = 1; i < al; i++){ - if(a[i] != a[i-1]) a[bl++] = a[i]; - } - return bl; + uint16_t i, j; + for (i = 1; i < al; i++) { + uint16_t tmp = a[i]; + for (j = i; j >= 1 && tmp < a[j-1]; j--) + a[j] = a[j-1]; + a[j] = tmp; + } + int bl = 1; + for(i = 1; i < al; i++){ + if(a[i] != a[i-1]) a[bl++] = a[i]; + } + return bl; } uint32_t pwm_get_mask(uint16_t value){ diff --git a/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino b/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino new file mode 100644 index 000000000..43a9d8259 --- /dev/null +++ b/libraries/ESP8266WiFi/examples/WiFiTelnetToSerial/WiFiTelnetToSerial.ino @@ -0,0 +1,90 @@ +/* + WiFiTelnetToSerial - Example Transparent UART to Telnet Server for esp8266 + + Copyright (c) 2015 Hristo Gochkov. All rights reserved. + This file is part of the ESP8266WiFi library for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include + +//how many clients should be able to telnet to this ESP8266 +#define MAX_SRV_CLIENTS 1 +const char* ssid = "**********"; +const char* password = "**********"; + +WiFiServer server(21); +WiFiClient serverClients[MAX_SRV_CLIENTS]; + +void setup() { + Serial1.begin(115200); + WiFi.begin(ssid, password); + Serial1.print("\nConnecting to "); Serial1.println(ssid); + uint8_t i = 0; + while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); + if(i == 21){ + Serial1.print("Could not connect to"); Serial1.println(ssid); + while(1) delay(500); + } + //start UART and the server + Serial.begin(115200); + server.begin(); + server.setNoDelay(true); + + Serial1.print("Ready! Use 'telnet "); + Serial1.print(WiFi.localIP()); + Serial1.println(" 21' to connect"); +} + +void loop() { + uint8_t i; + //check if there are any new clients + if (server.hasClient()){ + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + //find free/disconnected spot + if (!serverClients[i] || !serverClients[i].connected()){ + if(serverClients[i]) serverClients[i].stop(); + serverClients[i] = server.available(); + Serial1.print("New client: "); Serial1.print(i); + continue; + } + } + //no free/disconnected spot so reject + WiFiClient serverClient = server.available(); + serverClient.stop(); + } + //check clients for data + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + if (serverClients[i] && serverClients[i].connected()){ + if(serverClients[i].available()){ + //get data from the telnet client and push it to the UART + while(serverClients[i].available()) Serial.write(serverClients[i].read()); + } + } + } + //check UART for data + if(Serial.available()){ + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes(sbuf, len); + //push UART data to all connected telnet clients + for(i = 0; i < MAX_SRV_CLIENTS; i++){ + if (serverClients[i] && serverClients[i].connected()){ + serverClients[i].write(sbuf, len); + delay(1); + } + } + } +}