diff --git a/docs/Commands.txt b/docs/Commands.txt new file mode 100644 index 0000000..5d6c576 --- /dev/null +++ b/docs/Commands.txt @@ -0,0 +1,97 @@ +Note: +1 - add space to separate parameters + +* Display command list +[ESP] + +* Set/Get STA SSID +[ESP100] + +* Set STA Password +[ESP101] + +* Set/Get STA IP mode (DHCP/STATIC) +[ESP102] + +* Set/Get STA IP/Mask/GW +[ESP103]IP= MSK= GW= + +* Set/Get AP SSID +[ESP105] + +* Change AP Password +[ESP106] + +* Set/Get AP IP +[ESP107] + +* Set/Get AP channel +[ESP108] + +* Set/Get radio state which can be STA, AP, OFF +[ESP110] + +* Get current IP +[ESP111] + +* Get/Set hostname +[ESP112] + +* Get/Set HTTP state which can be ON, OFF +[ESP120] + +* Get/Set HTTP port +[ESP121] + +* Get SD Card Status +[ESP200] + +* Get full EEPROM settings content +but do not give any passwords +[ESP400] + +*Set EEPROM setting +position in EEPROM, type: B(byte), I(integer/long), S(string), A(IP address / mask) +[ESP401]P= T= V= +Description: Positions: +HOSTNAME_ENTRY "ESP_HOSTNAME" +STA_SSID_ENTRY "STA_SSID" +STA_PWD_ENTRY "STA_PWD" +STA_IP_ENTRY "STA_IP" +STA_GW_ENTRY "STA_GW" +STA_MK_ENTRY "STA_MK" +ESP_RADIO_MODE "WIFI_MODE" +AP_SSID_ENTRY "AP_SSID" +AP_PWD_ENTRY "AP_PWD" +AP_IP_ENTRY "AP_IP" +AP_CHANNEL_ENTRY "AP_CHANNEL" +HTTP_ENABLE_ENTRY "HTTP_ON" +HTTP_PORT_ENTRY "HTTP_PORT" +TELNET_ENABLE_ENTRY "TELNET_ON" +TELNET_PORT_ENTRY "TELNET_PORT" +STA_IP_MODE_ENTRY "STA_IP_MODE" + +*Get available AP list (limited to 30) +output is JSON or plain text according parameter +[ESP410] + +*Get current settings of ESP3D +output is plain text +[ESP420] + +* Set ESP State +cmd is RESTART / RESET +[ESP444] + +* Change admin password +[ESP550] + +* Change user password +[ESP555] + +* Format ESP Filesystem +[ESP710]FORMAT + +* FW Informations +[ESP800] + diff --git a/src/command.cpp b/src/command.cpp new file mode 100644 index 0000000..f231a81 --- /dev/null +++ b/src/command.cpp @@ -0,0 +1,1422 @@ +/* + command.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + 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 "esp3dlibconfig.h" + +#if defined(ESP3D_WIFISUPPORT) +#include "command.h" +#include "wificonfig.h" +#if defined(SDSUPPORT) +#include "sd_ESP32.h" +#endif +#include +#include +#include +#include +#include +#if defined(HTTP_FEATURE) +//#include "web_server.h" +#include +#endif //HTTP_FEATURE +#include "serial2socket.h" +#include MARLIN_PATH(inc/Version.h) + +String COMMAND::get_param (String & cmd_params, const char * id, bool withspace) +{ + static String parameter; + String sid = id; + int start; + int end = -1; + parameter = ""; + //if no id it means it is first part of cmd + if (strlen (id) == 0) { + start = 0; + } + //else find id position + else { + start = cmd_params.indexOf (id); + } + //if no id found and not first part leave + if (start == -1 ) { + return parameter; + } + //password and SSID can have space so handle it + //if no space expected use space as delimiter + if (!withspace) { + end = cmd_params.indexOf (" ", start); + } + //if no end found - take all + if (end == -1) { + end = cmd_params.length(); + } + //extract parameter + parameter = cmd_params.substring (start + strlen (id), end); + //be sure no extra space + parameter.trim(); + return parameter; +} + + +bool COMMAND::execute_internal_command (int cmd, String cmd_params, level_authenticate_type auth_level, ESPResponseStream *espresponse) +{ + bool response = true; + level_authenticate_type auth_type = auth_level; + + //manage parameters + String parameter; + switch (cmd) { + case 0: + espresponse->println ("[List of ESP3D commands]"); + espresponse->println ("[ESP] - display this help"); + espresponse->println ("[ESP100](SSID) - display/set STA SSID"); + espresponse->println ("[ESP101](Password) - set STA password"); + espresponse->println ("[ESP102](Mode) - display/set STA IP mode (DHCP/STATIC)"); + espresponse->println ("[ESP103](IP=xxxx MSK=xxxx GW=xxxx) - display/set STA IP/Mask/GW "); + espresponse->println ("[ESP105](SSID) - display/set AP SSID"); + espresponse->println ("[ESP106](Password) - set AP password"); + espresponse->println ("[ESP107](IP) - display/set AP IP"); + espresponse->println ("[ESP108](Chanel) - display/set AP chanel"); + espresponse->println ("[ESP110](State) - display/set radio state which can be STA, AP, OFF"); + espresponse->println ("[ESP111]display current IP"); + espresponse->println ("[ESP112](Hostname) - display/set Hostname"); + espresponse->println ("[ESP120](State) - display/set HTTP state which can be ON, OFF"); + espresponse->println ("[ESP121](Port) - display/set HTTP port "); + espresponse->println ("[ESP200] - display SD Card Status"); + espresponse->println ("[ESP400] - display ESP3D settings in JSON"); + espresponse->println ("[ESP401]P=(position) T=(type) V=(value) - Set specific setting"); + espresponse->println ("[ESP410] - display available AP list (limited to 30) in JSON"); + espresponse->println ("[ESP420] - display ESP3D current status"); + espresponse->println ("[ESP444](Cmd) - set ESP3D state (RESET/RESTART)"); +#ifdef AUTHENTICATION_FEATURE + espresponse->println ("[ESP550](Password) - set admin password"); + espresponse->println ("[ESP555](Password) - set user password"); +#endif //AUTHENTICATION_FEATURE + espresponse->println ("[ESP710]FORMAT - Format ESP Filesystem"); + espresponse->println ("[ESP800] - display FW Informations"); + break; + + //STA SSID + //[ESP100][pwd=] + case 100: + { +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif //AUTHENTICATION_FEATURE + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + String defV = DEFAULT_STA_SSID; + espresponse->println(prefs.getString(STA_SSID_ENTRY, defV).c_str()); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + if (!WiFiConfig::isSSIDValid (parameter.c_str() ) ) { + if(espresponse)espresponse->println ("Error: Incorrect SSID!"); + response = false; + } else { + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(STA_SSID_ENTRY, parameter) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + } + break; + //STA Password + //[ESP101] + case 101: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if (!WiFiConfig::isPasswordValid (parameter.c_str() ) ) { + if(espresponse)espresponse->println ("Error: Incorrect password!"); + response = false; + return false; + } else { + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(STA_PWD_ENTRY, parameter) != parameter.length()){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + break; + //Get/Change STA IP mode (DHCP/STATIC) + //[ESP102] + case 102: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + int8_t resp = prefs.getChar(STA_IP_MODE_ENTRY, DEFAULT_STA_IP_MODE); + if (resp == DHCP_MODE) espresponse->println("DHCP"); + else if (resp == STATIC_MODE) espresponse->println("STATIC"); + else espresponse->println("???"); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter.toUpperCase(); + if (!((parameter == "STATIC") || (parameter == "DHCP"))) { + if(espresponse)espresponse->println ("Error: only STATIC or DHCP mode supported!"); + response = false; + return false; + } else { + Preferences prefs; + prefs.begin(NAMESPACE, false); + int8_t bbuf = (parameter == "DHCP")?DHCP_MODE:STATIC_MODE; + if (prefs.putChar(STA_IP_MODE_ENTRY, bbuf) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + } + break; + //Get/Change STA IP/Mask/GW + //[ESP103]IP= MSK= GW= + case 103: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + //IP + String defV = DEFAULT_STA_IP; + int32_t IP = prefs.getInt(STA_IP_ENTRY, WiFiConfig::IP_int_from_string(defV)); + //GW + defV = DEFAULT_STA_GW; + int32_t GW = prefs.getInt(STA_GW_ENTRY, WiFiConfig::IP_int_from_string(defV)); + //MK + defV = DEFAULT_STA_MK; + int32_t MK = prefs.getInt(STA_MK_ENTRY, WiFiConfig::IP_int_from_string(defV)); + defV = "IP:" + WiFiConfig::IP_string_from_int(IP) + ", GW:" + WiFiConfig::IP_string_from_int(GW) + ", MSK:" + WiFiConfig::IP_string_from_int(MK); + espresponse->println(defV.c_str()); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + + String IP = get_param (cmd_params, "IP=", false); + String GW = get_param (cmd_params, "GW=", false); + String MSK = get_param (cmd_params, "MSK=", false); + Serial.println(IP); + Serial.println(GW); + if ( !WiFiConfig::isValidIP(IP.c_str())) { + if(espresponse)espresponse->println ("Error: Incorrect IP!"); + response = false; + return false; + } + if ( !WiFiConfig::isValidIP(GW.c_str())) { + if(espresponse)espresponse->println ("Error: Incorrect Gateway!"); + response = false; + return false; + } + if ( !WiFiConfig::isValidIP(MSK.c_str())) { + if(espresponse)espresponse->println ("Error: Incorrect Mask!"); + response = false; + return false; + } + Preferences prefs; + prefs.begin(NAMESPACE, false); + if ((prefs.putInt(STA_IP_ENTRY, WiFiConfig::IP_int_from_string(IP)) == 0) || + (prefs.putInt(STA_GW_ENTRY, WiFiConfig::IP_int_from_string(GW)) == 0) || + (prefs.putInt(STA_MK_ENTRY, WiFiConfig::IP_int_from_string(MSK)) == 0)){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + break; + //Change AP SSID + //[ESP105] + case 105: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + String defV = DEFAULT_AP_SSID; + espresponse->println(prefs.getString(AP_SSID_ENTRY, defV).c_str()); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + if (!WiFiConfig::isSSIDValid (parameter.c_str() ) ) { + if(espresponse)espresponse->println ("Error: Incorrect SSID!"); + response = false; + } + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(AP_SSID_ENTRY, parameter) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + break; + //Change AP Password + //[ESP106] + case 106: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if (!WiFiConfig::isPasswordValid (parameter.c_str() ) ) { + if(espresponse)espresponse->println ("Error: Incorrect password!"); + response = false; + return false; + } + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(AP_PWD_ENTRY, parameter) != parameter.length()){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + + } + break; + //Change AP IP + //[ESP107] + case 107: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + //IP + String defV = DEFAULT_AP_IP; + int32_t IP = prefs.getInt(AP_IP_ENTRY, WiFiConfig::IP_int_from_string(defV)); + espresponse->println(WiFiConfig::IP_string_from_int(IP).c_str()); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + if ( !WiFiConfig::isValidIP(parameter.c_str())) { + if(espresponse)espresponse->println ("Error: Incorrect IP!"); + response = false; + return false; + } + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putInt(AP_IP_ENTRY, WiFiConfig::IP_int_from_string(parameter)) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + break; + //Change AP channel + //[ESP108] + case 108: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + int8_t channel = prefs.getChar(AP_CHANNEL_ENTRY, DEFAULT_AP_CHANNEL); + espresponse->println(String(channel).c_str()); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + int8_t bbuf = parameter.toInt(); + if ((bbuf > MAX_CHANNEL) || (bbuf < MIN_CHANNEL)) { + if(espresponse)espresponse->println ("Error: Incorrect channel!"); + response = false; + return false; + } + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putChar(AP_CHANNEL_ENTRY, bbuf) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + break; + //Set radio state at boot which can be STA, AP, OFF + //[ESP110] + case 110: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + int8_t wifiMode = prefs.getChar(ESP_RADIO_MODE, DEFAULT_RADIO_MODE); + if (wifiMode == ESP_RADIO_OFF) espresponse->println("OFF"); + else if (wifiMode == ESP_WIFI_AP) espresponse->println("AP"); + else if (wifiMode == ESP_WIFI_STA) espresponse->println("STA"); + else espresponse->println("??"); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter.toUpperCase(); + if (!( + (parameter == "STA") || (parameter == "AP") || + (parameter == "OFF"))) { + + if(espresponse)espresponse->println ("Error: only " + "STA or AP or " + "OFF mode supported!"); + response = false; + return false; + } else { + Preferences prefs; + prefs.begin(NAMESPACE, false); + int8_t bbuf = ESP_RADIO_OFF; + + if(parameter == "STA")bbuf = ESP_WIFI_STA; + if(parameter == "AP")bbuf = ESP_WIFI_AP; + if (prefs.putChar(ESP_RADIO_MODE, bbuf) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + } + break; + //Get current IP + //[ESP111] + case 111: + { + if (!espresponse) return false; + String currentIP = cmd_params; + if (WiFi.getMode() == WIFI_STA) { + currentIP += WiFi.localIP().toString(); + } else { + currentIP += WiFi.softAPIP().toString(); + } + espresponse->println (currentIP.c_str()); + } + break; + + //Get/Set hostname + //[ESP112] + case 112: { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //Get hostname + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + String defV = DEFAULT_HOSTNAME; + espresponse->println(prefs.getString(HOSTNAME_ENTRY, defV).c_str()); + prefs.end(); + } else { //set host name +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + if (!WiFiConfig::isHostnameValid (parameter.c_str() ) ) { + if(espresponse)espresponse->println ("Error: Incorrect hostname!"); + response = false; + } else { + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(HOSTNAME_ENTRY, parameter) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + } + break; + //Set HTTP state which can be ON, OFF + //[ESP120]pwd= + case 120: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + int8_t Mode = prefs.getChar(HTTP_ENABLE_ENTRY, DEFAULT_HTTP_STATE); + espresponse->println((Mode == 0)?"OFF":"ON"); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter.toUpperCase(); + if (!((parameter == "ON") || (parameter == "OFF"))) { + if(espresponse)espresponse->println ("Error: only ON or OFF mode supported!"); + response = false; + return false; + } else { + Preferences prefs; + prefs.begin(NAMESPACE, false); + int8_t bbuf = (parameter == "ON")?1:0; + if (prefs.putChar(HTTP_ENABLE_ENTRY, bbuf) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + } + } + break; + //Set HTTP port + //[ESP121]pwd= + case 121: + { +#ifdef ENABLE_AUTHENTICATION + if (auth_type == LEVEL_GUEST) { + if(espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + parameter = get_param (cmd_params, "", true); + if ((parameter.length() == 0) && !espresponse) return false; + //get + if (parameter.length() == 0) { + Preferences prefs; + prefs.begin(NAMESPACE, true); + int port = prefs.getUShort(HTTP_PORT_ENTRY, DEFAULT_WEBSERVER_PORT); + espresponse->println(String(port).c_str()); + prefs.end(); + } else { //set +#ifdef ENABLE_AUTHENTICATION + if (auth_type != LEVEL_ADMIN) { + if (espresponse)espresponse->println ("Error: Wrong authentication!"); + return false; + } +#endif + int ibuf = parameter.toInt(); + if ((ibuf > MAX_HTTP_PORT) || (ibuf < MIN_HTTP_PORT)) { + if(espresponse)espresponse->println ("Error: Incorrect port!"); + response = false; + return false; + } + Preferences prefs; + prefs.begin(NAMESPACE, false); + + if (prefs.putUShort(HTTP_PORT_ENTRY, ibuf) == 0){ + response = false; + if(espresponse)espresponse->println ("Error: Set failed!"); + } else if(espresponse)espresponse->println ("ok"); + prefs.end(); + } + + } + break; + //Get SD Card Status + //[ESP200] + case 200: + { + if (!espresponse) return false; + String resp = "No SD card"; +#if defined(SDSUPPORT) + ESP_SD card; + int8_t state = card.card_status(); + if (state == -1)resp="Busy"; + else if (state == 1)resp="SD card detected"; + else resp="No SD card"; +#endif + espresponse->println (resp.c_str()); + } + break; + //Get full ESP3D settings + //[ESP400] + case 400: + { + String v; + String defV; + Preferences prefs; + if (!espresponse) return false; +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) return false; +#endif + int8_t vi; + espresponse->print("{\"EEPROM\":["); + prefs.begin(NAMESPACE, true); + //1 - Hostname + defV = DEFAULT_HOSTNAME; + v = prefs.getString(HOSTNAME_ENTRY, defV); + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (HOSTNAME_ENTRY); + espresponse->print ("\",\"T\":\"S\",\"V\":\""); + espresponse->print (v.c_str()); + espresponse->print ("\",\"H\":\"Hostname\" ,\"S\":\""); + espresponse->print (String(MAX_HOSTNAME_LENGTH).c_str()); + espresponse->print ("\", \"M\":\""); + espresponse->print (String(MIN_HOSTNAME_LENGTH).c_str()); + espresponse->print ("\"}"); + espresponse->print (","); + + //2 - http protocol mode + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (HTTP_ENABLE_ENTRY); + espresponse->print ("\",\"T\":\"B\",\"V\":\""); + vi = prefs.getChar(HTTP_ENABLE_ENTRY, 1); + espresponse->print (String(vi).c_str()); + espresponse->print ("\",\"H\":\"HTTP protocol\",\"O\":[{\"Enabled\":\"1\"},{\"Disabled\":\"0\"}]}"); + espresponse->print (","); + + //3 - http port + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (HTTP_PORT_ENTRY); + espresponse->print ("\",\"T\":\"I\",\"V\":\""); + espresponse->print (String(prefs.getUShort(HTTP_PORT_ENTRY, DEFAULT_WEBSERVER_PORT)).c_str()); + espresponse->print ("\",\"H\":\"HTTP Port\",\"S\":\""); + espresponse->print (String(MAX_HTTP_PORT).c_str()); + espresponse->print ("\",\"M\":\""); + espresponse->print (String(MIN_HTTP_PORT).c_str()); + espresponse->print ("\"}"); + espresponse->print (","); + + //TODO + //4 - telnet protocol mode + /* espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (TELNET_ENABLE_ENTRY); + espresponse->print ("\",\"T\":\"B\",\"V\":\""); + vi = prefs.getChar(TELNET_ENABLE_ENTRY, 0); + espresponse->print (String(vi).c_str()); + espresponse->print ("\",\"H\":\"Telnet protocol\",\"O\":[{\"Enabled\":\"1\"},{\"Disabled\":\"0\"}]}"); + espresponse->print (",");*/ + + //5 - telnet Port + /* espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (TELNET_PORT_ENTRY); + espresponse->print ("\",\"T\":\"I\",\"V\":\""); + espresponse->print (String(_data_port).c_str()); + espresponse->print ("\",\"H\":\"Telnet Port\",\"S\":\""); + espresponse->print (String(MAX_TELNET_PORT).c_str()); + espresponse->print ("\",\"M\":\""); + espresponse->print (String(MIN_TELNET_PORT).c_str()); + espresponse->print ("\"}"); + espresponse->print (",");*/ + + //6 - wifi mode + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (ESP_RADIO_MODE); + espresponse->print ("\",\"T\":\"B\",\"V\":\""); + vi = prefs.getChar(ESP_RADIO_MODE, DEFAULT_RADIO_MODE); + espresponse->print (String(vi).c_str()); + espresponse->print ("\",\"H\":\"Wifi mode\",\"O\":[{\"STA\":\"1\"},{\"AP\":\"2\"},{\"None\":\"0\"}]}"); + espresponse->print (","); + + //7 - STA SSID + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (STA_SSID_ENTRY); + espresponse->print ("\",\"T\":\"S\",\"V\":\""); + defV = DEFAULT_STA_SSID; + espresponse->print (prefs.getString(STA_SSID_ENTRY, defV).c_str()); + espresponse->print ("\",\"S\":\""); + espresponse->print (String(MAX_SSID_LENGTH).c_str()); + espresponse->print ("\",\"H\":\"Station SSID\",\"M\":\""); + espresponse->print (String(MIN_SSID_LENGTH).c_str()); + espresponse->print ("\"}"); + espresponse->print (","); + + //8 - STA password + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (STA_PWD_ENTRY); + espresponse->print ("\",\"T\":\"S\",\"V\":\""); + espresponse->print (HIDDEN_PASSWORD); + espresponse->print ("\",\"S\":\""); + espresponse->print (String(MAX_PASSWORD_LENGTH).c_str()); + espresponse->print ("\",\"H\":\"Station Password\",\"M\":\""); + espresponse->print (String(MIN_PASSWORD_LENGTH).c_str()); + espresponse->print ("\"}"); + espresponse->print (","); + + // 9 - STA IP mode + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (STA_IP_MODE_ENTRY); + espresponse->print ("\",\"T\":\"B\",\"V\":\""); + espresponse->print (String(prefs.getChar(STA_IP_MODE_ENTRY, DEFAULT_STA_IP_MODE)).c_str()); + espresponse->print ("\",\"H\":\"Station IP Mode\",\"O\":[{\"DHCP\":\"0\"},{\"Static\":\"1\"}]}"); + espresponse->print (","); + + //10-STA static IP + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (STA_IP_ENTRY); + espresponse->print ("\",\"T\":\"A\",\"V\":\""); + espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(STA_IP_ENTRY, 0)).c_str()); + espresponse->print ("\",\"H\":\"Station Static IP\"}"); + espresponse->print (","); + + //11-STA static Gateway + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (STA_GW_ENTRY); + espresponse->print ("\",\"T\":\"A\",\"V\":\""); + espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(STA_GW_ENTRY, 0)).c_str()); + espresponse->print ("\",\"H\":\"Station Static Gateway\"}"); + espresponse->print (","); + + //12-STA static Mask + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (STA_MK_ENTRY); + espresponse->print ("\",\"T\":\"A\",\"V\":\""); + espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(STA_MK_ENTRY, 0)).c_str()); + espresponse->print ("\",\"H\":\"Station Static Mask\"}"); + espresponse->print (","); + + //13 - AP SSID + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (AP_SSID_ENTRY); + espresponse->print ("\",\"T\":\"S\",\"V\":\""); + defV = DEFAULT_AP_SSID; + espresponse->print (prefs.getString(AP_SSID_ENTRY, defV).c_str()); + espresponse->print ("\",\"S\":\""); + espresponse->print (String(MAX_SSID_LENGTH).c_str()); + espresponse->print ("\",\"H\":\"AP SSID\",\"M\":\""); + espresponse->print (String(MIN_SSID_LENGTH).c_str()); + espresponse->print ("\"}"); + espresponse->print (","); + + //14 - AP password + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (AP_PWD_ENTRY); + espresponse->print ("\",\"T\":\"S\",\"V\":\""); + espresponse->print (HIDDEN_PASSWORD); + espresponse->print ("\",\"S\":\""); + espresponse->print (String(MAX_PASSWORD_LENGTH).c_str()); + espresponse->print ("\",\"H\":\"AP Password\",\"M\":\""); + espresponse->print (String(MIN_PASSWORD_LENGTH).c_str()); + espresponse->print ("\"}"); + espresponse->print (","); + + //15 - AP static IP + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (AP_IP_ENTRY); + espresponse->print ("\",\"T\":\"A\",\"V\":\""); + defV = DEFAULT_AP_IP; + espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(AP_IP_ENTRY, WiFiConfig::IP_int_from_string(defV))).c_str()); + espresponse->print ("\",\"H\":\"AP Static IP\"}"); + espresponse->print (","); + + //16 - AP Channel + espresponse->print ("{\"F\":\"network\",\"P\":\""); + espresponse->print (AP_CHANNEL_ENTRY); + espresponse->print ("\",\"T\":\"B\",\"V\":\""); + espresponse->print (String(prefs.getChar(AP_CHANNEL_ENTRY, DEFAULT_AP_CHANNEL)).c_str()); + espresponse->print ("\",\"H\":\"AP Channel\",\"O\":["); + for (int i = MIN_CHANNEL; i <= MAX_CHANNEL ; i++) { + espresponse->print ("{\""); + espresponse->print (String(i).c_str()); + espresponse->print ("\":\""); + espresponse->print (String(i).c_str()); + espresponse->print ("\"}"); + if (i < MAX_CHANNEL) { + espresponse->print (","); + } + } + espresponse->print ("]}"); + + espresponse->println ("]}"); + prefs.end(); + } + break; + //Set EEPROM setting + //[ESP401]P= T= V= pwd= + case 401: + { +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) return false; +#endif + //check validity of parameters + String spos = get_param (cmd_params, "P=", false); + String styp = get_param (cmd_params, "T=", false); + String sval = get_param (cmd_params, "V=", true); + spos.trim(); + sval.trim(); + if (spos.length() == 0) { + response = false; + } + if (! (styp == "B" || styp == "S" || styp == "A" || styp == "I" || styp == "F") ) { + response = false; + } + if ((sval.length() == 0) && !((spos==AP_PWD_ENTRY) || (spos==STA_PWD_ENTRY))){ + response = false; + } + + if (response) { + Preferences prefs; + prefs.begin(NAMESPACE, false); + //Byte value + if ((styp == "B") || (styp == "F")){ + int8_t bbuf = sval.toInt(); + if (prefs.putChar(spos.c_str(), bbuf) ==0 ) { + response = false; + } else { + //dynamique refresh is better than restart the board + if (spos == ESP_RADIO_MODE){ + //TODO + } + if (spos == AP_CHANNEL_ENTRY) { + //TODO + } + if (spos == HTTP_ENABLE_ENTRY) { + //TODO + } + if (spos == TELNET_ENABLE_ENTRY) { + //TODO + } + } + } + //Integer value + if (styp == "I") { + int16_t ibuf = sval.toInt(); + if (prefs.putUShort(spos.c_str(), ibuf) == 0) { + response = false; + } else { + if (spos == HTTP_PORT_ENTRY){ + //TODO + } + if (spos == TELNET_PORT_ENTRY){ + //TODO + //Serial.println(ibuf); + } + } + + } + //String value + if (styp == "S") { + if (prefs.putString(spos.c_str(), sval) == 0) { + response = false; + } else { + if (spos == HOSTNAME_ENTRY){ + //TODO + } + if (spos == STA_SSID_ENTRY){ + //TODO + } + if (spos == STA_PWD_ENTRY){ + //TODO + } + if (spos == AP_SSID_ENTRY){ + //TODO + } + if (spos == AP_PWD_ENTRY){ + //TODO + } + } + + } + //IP address + if (styp == "A") { + if (prefs.putInt(spos.c_str(), WiFiConfig::IP_int_from_string(sval)) == 0) { + response = false; + } else { + if (spos == STA_IP_ENTRY){ + //TODO + } + if (spos == STA_GW_ENTRY){ + //TODO + } + if (spos == STA_MK_ENTRY){ + //TODO + } + if (spos == AP_IP_ENTRY){ + //TODO + } + } + } + prefs.end(); + } + if (!response) { + if (espresponse) espresponse->println ("Error: Incorrect Command"); + } else { + if (espresponse) espresponse->println ("ok"); + } + + } + break; + //Get available AP list (limited to 30) + //output is JSON + //[ESP410] + case 410: { + if (!espresponse)return false; +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) return false; +#endif + espresponse->print("{\"AP_LIST\":["); + int n = WiFi.scanNetworks(); + if (n > 0) { + for (int i = 0; i < n; ++i) { + if (i > 0) { + espresponse->print (","); + } + espresponse->print ("{\"SSID\":\""); + espresponse->print (WiFi.SSID (i).c_str()); + espresponse->print ("\",\"SIGNAL\":\""); + espresponse->print (String(WiFiConfig::getSignal (WiFi.RSSI (i) )).c_str()); + espresponse->print ("\",\"IS_PROTECTED\":\""); + + if (WiFi.encryptionType (i) == WIFI_AUTH_OPEN) { + espresponse->print ("0"); + } else { + espresponse->print ("1"); + } + espresponse->print ("\"}"); + Esp3DLibConfig::wait(0); + } + WiFi.scanDelete(); + } + espresponse->println ("]}"); + //Ugly fix for the AP mode + if (WiFi.getMode() == WIFI_AP_STA) { + WiFi.enableSTA (false); + } + } + break; + //Get ESP current status + case 420: + { +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) return false; +#endif + if (!espresponse)return false; + Preferences prefs; + prefs.begin(NAMESPACE, true); + espresponse->print ("Chip ID: "); + espresponse->print (String ( (uint16_t) (ESP.getEfuseMac() >> 32) ).c_str()); + espresponse->print ("\n"); + espresponse->print ("CPU Frequency: "); + espresponse->print (String (ESP.getCpuFreqMHz() ).c_str()); + espresponse->print ("Mhz"); + espresponse->print ("\n"); + espresponse->print ("CPU Temperature: "); + espresponse->print (String (temperatureRead(), 1).c_str()); + espresponse->print ("C"); + espresponse->print ("\n"); + espresponse->print ("Free memory: "); + espresponse->print (ESPResponseStream::formatBytes (ESP.getFreeHeap()).c_str()); + espresponse->print ("\n"); + espresponse->print ("SDK: "); + espresponse->print (ESP.getSdkVersion()); + espresponse->print ("\n"); + espresponse->print ("Flash Size: "); + espresponse->print (ESPResponseStream::formatBytes (ESP.getFlashChipSize()).c_str()); + espresponse->print ("\n"); + espresponse->print ("Available Size for update: "); + //Not OTA on 2Mb board per spec + if (ESP.getFlashChipSize() > 0x20000) { + espresponse->print (ESPResponseStream::formatBytes (0x140000).c_str()); + } else { + espresponse->print (ESPResponseStream::formatBytes (0x0).c_str()); + } + espresponse->print ("\n"); + espresponse->print ("Available Size for SPIFFS: "); + espresponse->print (ESPResponseStream::formatBytes (SPIFFS.totalBytes()).c_str()); + espresponse->print ("\n"); + espresponse->print ("Baud rate: "); + espresponse->print (String(ESPResponseStream::baudRate()).c_str()); + espresponse->print ("\n"); + espresponse->print ("Sleep mode: "); + if (WiFi.getSleep())espresponse->print ("Modem"); + else espresponse->print ("None"); + espresponse->print ("\n"); + espresponse->print ("Web port: "); + espresponse->print (String( prefs.getUShort(HTTP_PORT_ENTRY, DEFAULT_WEBSERVER_PORT)).c_str()); + espresponse->print ("\n"); + /*espresponse->print ("Data port: "); + //if (_data_port!=0)espresponse->print (String(_data_port).c_str()); + //else + espresponse->print ("Disabled"); + espresponse->print ("\n");*/ + espresponse->print ("Hostname: "); + String defV = DEFAULT_HOSTNAME; + espresponse->print ( prefs.getString(HOSTNAME_ENTRY, defV).c_str()); + espresponse->print ("\n"); + espresponse->print ("Active Mode: "); + if (WiFi.getMode() == WIFI_STA) { + espresponse->print ("STA ("); + espresponse->print ( WiFi.macAddress().c_str()); + espresponse->print (")"); + espresponse->print ("\n"); + espresponse->print ("Connected to: "); + if (WiFi.isConnected()){ //in theory no need but ... + espresponse->print (WiFi.SSID().c_str()); + espresponse->print ("\n"); + espresponse->print ("Signal: "); + espresponse->print ( String(WiFiConfig::getSignal (WiFi.RSSI())).c_str()); + espresponse->print ("%"); + espresponse->print ("\n"); + uint8_t PhyMode; + esp_wifi_get_protocol (ESP_IF_WIFI_STA, &PhyMode); + espresponse->print ("Phy Mode: "); + if (PhyMode == (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N)) espresponse->print ("11n"); + else if (PhyMode == (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G)) espresponse->print ("11g"); + else if (PhyMode == (WIFI_PROTOCOL_11B )) espresponse->print ("11b"); + else espresponse->print ("???"); + espresponse->print ("\n"); + espresponse->print ("Channel: "); + espresponse->print (String (WiFi.channel()).c_str()); + espresponse->print ("\n"); + espresponse->print ("IP Mode: "); + tcpip_adapter_dhcp_status_t dhcp_status; + tcpip_adapter_dhcpc_get_status (TCPIP_ADAPTER_IF_STA, &dhcp_status); + if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED)espresponse->print ("DHCP"); + else espresponse->print ("Static"); + espresponse->print ("\n"); + espresponse->print ("IP: "); + espresponse->print (WiFi.localIP().toString().c_str()); + espresponse->print ("\n"); + espresponse->print ("Gateway: "); + espresponse->print (WiFi.gatewayIP().toString().c_str()); + espresponse->print ("\n"); + espresponse->print ("Mask: "); + espresponse->print (WiFi.subnetMask().toString().c_str()); + espresponse->print ("\n"); + espresponse->print ("DNS: "); + espresponse->print (WiFi.dnsIP().toString().c_str()); + espresponse->print ("\n"); + } //this is web command so connection => no command + espresponse->print ("Disabled Mode: "); + espresponse->print ("AP ("); + espresponse->print (WiFi.softAPmacAddress().c_str()); + espresponse->print (")"); + espresponse->print ("\n"); + } else if (WiFi.getMode() == WIFI_AP) { + espresponse->print ("AP ("); + espresponse->print (WiFi.softAPmacAddress().c_str()); + espresponse->print (")"); + espresponse->print ("\n"); + wifi_config_t conf; + esp_wifi_get_config (ESP_IF_WIFI_AP, &conf); + espresponse->print ("SSID: "); + espresponse->print ((const char*) conf.ap.ssid); + espresponse->print ("\n"); + espresponse->print ("Visible: "); + espresponse->print ( (conf.ap.ssid_hidden == 0) ? "Yes" : "No"); + espresponse->print ("\n"); + espresponse->print ("Authentication: "); + if (conf.ap.authmode == WIFI_AUTH_OPEN) { + espresponse->print ("None"); + } else if (conf.ap.authmode == WIFI_AUTH_WEP) { + espresponse->print ("WEP"); + } else if (conf.ap.authmode == WIFI_AUTH_WPA_PSK) { + espresponse->print ("WPA"); + } else if (conf.ap.authmode == WIFI_AUTH_WPA2_PSK) { + espresponse->print ("WPA2"); + } else { + espresponse->print ("WPA/WPA2"); + } + espresponse->print ("\n"); + espresponse->print ("Max Connections: "); + espresponse->print (String(conf.ap.max_connection).c_str()); + espresponse->print ("\n"); + espresponse->print ("DHCP Server: "); + tcpip_adapter_dhcp_status_t dhcp_status; + tcpip_adapter_dhcps_get_status (TCPIP_ADAPTER_IF_AP, &dhcp_status); + if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED)espresponse->print ("Started"); + else espresponse->print ("Stopped"); + espresponse->print ("\n"); + espresponse->print ("IP: "); + espresponse->print (WiFi.softAPIP().toString().c_str()); + espresponse->print ("\n"); + tcpip_adapter_ip_info_t ip_AP; + tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_AP, &ip_AP); + espresponse->print ("Gateway: "); + espresponse->print (IPAddress (ip_AP.gw.addr).toString().c_str()); + espresponse->print ("\n"); + espresponse->print ("Mask: "); + espresponse->print (IPAddress (ip_AP.netmask.addr).toString().c_str()); + espresponse->print ("\n"); + espresponse->print ("Connected clients: "); + wifi_sta_list_t station; + tcpip_adapter_sta_list_t tcpip_sta_list; + esp_wifi_ap_get_sta_list (&station); + tcpip_adapter_get_sta_list (&station, &tcpip_sta_list); + espresponse->print (String(station.num).c_str()); + espresponse->print ("\n"); + for (int i = 0; i < station.num; i++) { + espresponse->print (ESPResponseStream::mac2str(tcpip_sta_list.sta[i].mac)); + espresponse->print (" "); + espresponse->print ( IPAddress (tcpip_sta_list.sta[i].ip.addr).toString().c_str()); + espresponse->print ("\n"); + } + espresponse->print ("Disabled Mode: "); + espresponse->print ("STA ("); + espresponse->print (WiFi.macAddress().c_str()); + espresponse->print (")"); + espresponse->print ("\n"); + } else if (WiFi.getMode() == WIFI_AP_STA) //we should not be in this state but just in case .... + { + espresponse->print ("Mixed"); + espresponse->print ("\n"); + espresponse->print ("STA ("); + espresponse->print (WiFi.macAddress().c_str()); + espresponse->print (")"); + espresponse->print ("\n"); + espresponse->print ("AP ("); + espresponse->print (WiFi.softAPmacAddress().c_str()); + espresponse->print (")"); + espresponse->print ("\n"); + + } else { //we should not be there if no wifi .... + espresponse->print ("Wifi Off"); + espresponse->print ("\n"); + } + prefs.end(); + //TODO to complete + espresponse->print ("FW version: Marlin "); + espresponse->print (SHORT_BUILD_VERSION); + espresponse->print (" (ESP3D:"); + espresponse->print (LIB_VERSION); + espresponse->println (")"); + } + break; + //Set ESP mode + //cmd is RESTART, RESET + //[ESP444] + case 444: + parameter = get_param(cmd_params,"", true); +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + response = false; + } else +#endif + { + if (parameter=="RESTART") { + Esp3DCom::echo("Restart ongoing"); + Esp3DLibConfig::restart_ESP(); + } else + if (parameter=="RESET") { + Esp3DCom::echo("Reset"); + Esp3DLibConfig::reset_settings(); + } else response = false; + } + if (!response) { + if (espresponse)espresponse->println ("Error: Incorrect Command, only RESET or RESTART"); + } else { + if (espresponse)espresponse->println ("ok"); + } + break; +#ifdef AUTHENTICATION_FEATURE + //Change / Reset admin password + //[ESP550] + case 550: { + if (auth_type == LEVEL_ADMIN) { + parameter = get_param (cmd_params, "", true); + if (parameter.length() == 0) { + Preferences prefs; + parameter = DEFAULT_ADMIN_PWD; + prefs.begin(NAMESPACE, false); + if (prefs.putString(ADMIN_PWD_ENTRY, parameter) != parameter.length()){ + response = false; + espresponse->println ("error"); + } else espresponse->println ("ok"); + prefs.end(); + + } else { + if (isLocalPasswordValid (parameter.c_str() ) ) { + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(ADMIN_PWD_ENTRY, parameter) != parameter.length()) { + response = false; + espresponse->println ("error"); + } else espresponse->println ("ok"); + prefs.end(); + } else { + espresponse->println ("error"); + response = false; + } + } + } else { + espresponse->println ("error"); + response = false; + } + break; + } + //Change / Reset user password + //[ESP555] + case 555: { + if (auth_type == LEVEL_ADMIN) { + parameter = get_param (cmd_params, "", true); + if (parameter.length() == 0) { + Preferences prefs; + parameter = DEFAULT_USER_PWD; + prefs.begin(NAMESPACE, false); + if (prefs.putString(USER_PWD_ENTRY, parameter) != parameter.length()){ + response = false; + espresponse->println ("error"); + } else espresponse->println ("ok"); + prefs.end(); + + } else { + if (isLocalPasswordValid (parameter.c_str() ) ) { + Preferences prefs; + prefs.begin(NAMESPACE, false); + if (prefs.putString(USER_PWD_ENTRY, parameter) != parameter.length()) { + response = false; + espresponse->println ("error"); + } else espresponse->println ("ok"); + prefs.end(); + } else { + espresponse->println ("error"); + response = false; + } + } + } else { + espresponse->println ("error"); + response = false; + } + break; + } +#endif + //[ESP700] + case 700: { //read local file +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) return false; +#endif + cmd_params.trim() ; + if ( (cmd_params.length() > 0) && (cmd_params[0] != '/') ) { + cmd_params = "/" + cmd_params; + } + File currentfile = SPIFFS.open (cmd_params, FILE_READ); + if (currentfile) {//if file open success + //until no line in file + while (currentfile.available()) { + String currentline = currentfile.readStringUntil('\n'); + currentline.replace("\n",""); + currentline.replace("\r",""); + if (currentline.length() > 0) { + int ESPpos = currentline.indexOf ("[ESP"); + if (ESPpos > -1) { + //is there the second part? + int ESPpos2 = currentline.indexOf ("]", ESPpos); + if (ESPpos2 > -1) { + //Split in command and parameters + String cmd_part1 = currentline.substring (ESPpos + 4, ESPpos2); + String cmd_part2 = ""; + //is there space for parameters? + if (ESPpos2 < currentline.length() ) { + cmd_part2 = currentline.substring (ESPpos2 + 1); + } + //if command is a valid number then execute command + if(cmd_part1.toInt()!=0) { + if (!execute_internal_command(cmd_part1.toInt(),cmd_part2, auth_type, espresponse)) response = false; + } + //if not is not a valid [ESPXXX] command ignore it + } + } else { + if (currentline.length() > 0){ + currentline+="\n"; + Serial2Socket.push(currentline.c_str()); + //GCodeQueue::enqueue_one_now(currentline.c_str()); + } + Esp3DLibConfig::wait (1); + } + Esp3DLibConfig::wait (1); + } + } + currentfile.close(); + if (espresponse)espresponse->println ("ok"); + } else { + if (espresponse)espresponse->println ("error"); + response = false; + } + break; + } + //Format SPIFFS + //[ESP710]FORMAT pwd= + case 710: +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) return false; +#endif + parameter = get_param (cmd_params, "", true); +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + espresponse->println ("error"); + response = false; + break; + } else +#endif + { + if (parameter == "FORMAT") { + if (espresponse)espresponse->print ("Formating"); + SPIFFS.format(); + if (espresponse)espresponse->println ("...Done"); + } else { + if (espresponse)espresponse->println ("error"); + response = false; + } + } + break; + //get fw version / fw target / hostname / authentication + //[ESP800] + case 800: + { + if (!espresponse)return false; + Preferences prefs; + prefs.begin(NAMESPACE, true); + String resp; + resp = "FW version:"; + resp += SHORT_BUILD_VERSION; + resp += " # FW target:marlin-embedded # FW HW:"; + #if defined(SDSUPPORT) + resp += "Direct SD"; + #else + resp += "No SD"; + #endif + resp += " # primary sd:/sd # secondary sd:none # authentication:"; + #ifdef AUTHENTICATION_FEATURE + resp += "yes"; + #else + resp += "no"; + #endif + resp += " # webcommunication: Sync: "; + resp += String(prefs.getUShort(HTTP_PORT_ENTRY, DEFAULT_WEBSERVER_PORT) + 1); + resp += "# hostname:"; + String defV = DEFAULT_HOSTNAME; + resp += prefs.getString(HOSTNAME_ENTRY, defV);; + prefs.end(); + if (WiFi.getMode() == WIFI_AP)resp += "(AP mode)"; + if (espresponse)espresponse->println (resp.c_str()); + } + break; + default: + if (espresponse)espresponse->println ("Error: Incorrect Command"); + response = false; + break; + } + return response; +} + +#endif //ESP3D_WIFISUPPORT diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..3741929 --- /dev/null +++ b/src/command.h @@ -0,0 +1,38 @@ +/* + command.h - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + 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 +*/ + +#ifndef COMMAND_h +#define COMMAND_h +#include "esp3dlibconfig.h" +#include +#include "espcom.h" + + +class COMMAND +{ +public: +// static bool check_command (String buffer, tpipe output, bool executecmd = true); + static bool execute_internal_command (int cmd, String cmd_params, level_authenticate_type auth_level, ESPResponseStream *espresponse); + static String get_param (String & cmd_params, const char * id, bool withspace = false); +// static bool isadmin (String & cmd_params); +// static bool isuser (String & cmd_params); +}; + +#endif diff --git a/src/esp3dlib.cpp b/src/esp3dlib.cpp index aee6359..d633019 100644 --- a/src/esp3dlib.cpp +++ b/src/esp3dlib.cpp @@ -29,7 +29,8 @@ #if defined(ESP3D_WIFISUPPORT) #include "esp3dlib.h" #include "wificonfig.h" - +#include "espcom.h" +#include "command.h" Esp3DLib esp3dlib; void WiFiTaskfn( void * parameter ) @@ -67,9 +68,27 @@ void Esp3DLib::init() bool Esp3DLib::parse(char * cmd) { String scmd = cmd; - if (scmd.startsWith("[ESP")) { - - return true; + int ESPpos = scmd.indexOf ("[ESP"); + if (ESPpos > -1) { + int ESPpos2 = scmd.indexOf("]",ESPpos); + if (ESPpos2>-1) { + //Split in command and parameters + String cmd_part1=scmd.substring(ESPpos+4,ESPpos2); + String cmd_part2=""; + //is there space for parameters? + if (ESPpos2=0) { + ESPResponseStream response(SERIAL_PIPE); + level_authenticate_type auth_type = LEVEL_ADMIN; //we do not care serial authentication + COMMAND::execute_internal_command (cmd_part1.toInt(), cmd_part2, auth_type, &response); + return true; + } else { + return false; + } + } else return false; } else { return false; } diff --git a/src/esp3dlibconfig.cpp b/src/esp3dlibconfig.cpp index 3b74c61..c97625a 100644 --- a/src/esp3dlibconfig.cpp +++ b/src/esp3dlibconfig.cpp @@ -20,6 +20,8 @@ #include "esp3dlibconfig.h" #if defined(ESP3D_WIFISUPPORT) +#include +#include "wificonfig.h" #include #ifdef __cplusplus extern "C" { @@ -63,4 +65,80 @@ void Esp3DLibConfig::handle() { } } +void Esp3DLibConfig::reset_settings() +{ + Preferences prefs; + prefs.begin(NAMESPACE, false); + String sval; + int8_t bbuf; + int16_t ibuf; + bool error = false; + sval = DEFAULT_HOSTNAME; + if (prefs.putString(HOSTNAME_ENTRY, sval) == 0){ + error = true; + } + sval = DEFAULT_STA_SSID; + if (prefs.putString(STA_SSID_ENTRY, sval) == 0){ + error = true; + } + sval = DEFAULT_STA_PWD; + if (prefs.putString(STA_PWD_ENTRY, sval) != sval.length()){ + error = true; + } + sval = DEFAULT_AP_SSID; + if (prefs.putString(AP_SSID_ENTRY, sval) == 0){ + error = true; + } + sval = DEFAULT_AP_PWD; + if (prefs.putString(AP_PWD_ENTRY, sval) != sval.length()){ + error = true; + } + + bbuf = DEFAULT_AP_CHANNEL; + if (prefs.putChar(AP_CHANNEL_ENTRY, bbuf) ==0 ) { + error = true; + } + bbuf = DEFAULT_STA_IP_MODE; + if (prefs.putChar(STA_IP_MODE_ENTRY, bbuf) ==0 ) { + error = true; + } + bbuf = DEFAULT_HTTP_STATE; + if (prefs.putChar(HTTP_ENABLE_ENTRY, bbuf) ==0 ) { + error = true; + } + /*bbuf = DEFAULT_TELNET_STATE; + if (prefs.putChar(TELNET_ENABLE_ENTRY, bbuf) ==0 ) { + error = true; + }*/ + bbuf = DEFAULT_RADIO_MODE; + if (prefs.putChar(ESP_RADIO_MODE, bbuf) ==0 ) { + error = true; + } + ibuf = DEFAULT_WEBSERVER_PORT; + if (prefs.putUShort(HTTP_PORT_ENTRY, ibuf) == 0) { + error = true; + } + /*ibuf = DEFAULT_TELNETSERVER_PORT; + if (prefs.putUShort(TELNET_PORT_ENTRY, ibuf) == 0) { + error = true; + }*/ + sval = DEFAULT_STA_IP; + if (prefs.putInt(STA_IP_ENTRY, WiFiConfig::IP_int_from_string(sval)) == 0) { + error = true; + } + sval = DEFAULT_STA_GW; + if (prefs.putInt(STA_GW_ENTRY, WiFiConfig::IP_int_from_string(sval)) == 0) { + error = true; + } + sval = DEFAULT_STA_MK; + if (prefs.putInt(STA_MK_ENTRY, WiFiConfig::IP_int_from_string(sval)) == 0) { + error = true; + } + sval = DEFAULT_AP_IP; + if (prefs.putInt(AP_IP_ENTRY, WiFiConfig::IP_int_from_string(sval)) == 0) { + error = true; + } + prefs.end(); +} + #endif // ESP3D_WIFISUPPORT diff --git a/src/esp3dlibconfig.h b/src/esp3dlibconfig.h index 1347d7f..2145af2 100644 --- a/src/esp3dlibconfig.h +++ b/src/esp3dlibconfig.h @@ -25,7 +25,7 @@ #undef DISABLED #undef _BV //version -#define LIB_VERSION "1.0.0" +#define LIB_VERSION "0.9.0" //Editable configuration part @@ -66,7 +66,7 @@ #define STA_IP_ENTRY "STA_IP" #define STA_GW_ENTRY "STA_GW" #define STA_MK_ENTRY "STA_MK" -#define ESP_WIFI_MODE "WIFI_MODE" +#define ESP_RADIO_MODE "WIFI_MODE" #define AP_SSID_ENTRY "AP_SSID" #define AP_PWD_ENTRY "AP_PWD" #define AP_IP_ENTRY "AP_IP" @@ -77,9 +77,47 @@ #define TELNET_PORT_ENTRY "TELNET_PORT" #define STA_IP_MODE_ENTRY "STA_IP_MODE" -//Switch -#define ESP_SAVE_ONLY 0 -#define ESP_APPLY_NOW 1 +//Wifi Mode +#define ESP_RADIO_OFF 0 +#define ESP_WIFI_STA 1 +#define ESP_WIFI_AP 2 + +#define DHCP_MODE 0 +#define STATIC_MODE 0 + +//defaults values +#define DEFAULT_HOSTNAME "marlinesp" +#define DEFAULT_STA_SSID "MARLIN_ESP" +#define DEFAULT_STA_PWD "12345678" +#define DEFAULT_STA_IP "0.0.0.0" +#define DEFAULT_STA_GW "0.0.0.0" +#define DEFAULT_STA_MK "0.0.0.0" +#define DEFAULT_STA_IP_MODE DHCP_MODE +#define DEFAULT_RADIO_MODE ESP_WIFI_AP +#define DEFAULT_AP_SSID "MARLIN_ESP" +#define DEFAULT_AP_PWD "12345678" +#define DEFAULT_AP_IP "192.168.0.1" +#define DEFAULT_AP_MK "255.255.255.0" +#define DEFAULT_AP_CHANNEL 1 +#define DEFAULT_WEBSERVER_PORT 80 +#define DEFAULT_HTTP_STATE 1 +#define HIDDEN_PASSWORD "********" + +//boundaries +#define MAX_SSID_LENGTH 32 +#define MIN_SSID_LENGTH 1 +#define MAX_PASSWORD_LENGTH 64 +//min size of password is 0 or upper than 8 char +//so let set min is 8 +#define MIN_PASSWORD_LENGTH 8 +#define MAX_HOSTNAME_LENGTH 32 +#define MIN_HOSTNAME_LENGTH 1 +#define MAX_HTTP_PORT 65001 +#define MIN_HTTP_PORT 1 +#define MAX_TELNET_PORT 65001 +#define MIN_TELNET_PORT 1 +#define MIN_CHANNEL 1 +#define MAX_CHANNEL 14 #ifndef ESP3DLIBCONFIG_H #define ESP3DLIBCONFIG_H @@ -88,6 +126,7 @@ public: static void wait(uint32_t milliseconds); static void restart_ESP(); static void handle(); + static void reset_settings(); private: static bool restart_ESP_module; }; diff --git a/src/espcom.cpp b/src/espcom.cpp index b772c05..1229fca 100644 --- a/src/espcom.cpp +++ b/src/espcom.cpp @@ -21,16 +21,107 @@ #include "esp3dlibconfig.h" #if defined(ESP3D_WIFISUPPORT) - #include MARLIN_PATH(HAL/HAL_ESP32/FlushableHardwareSerial.h) #include MARLIN_PATH(HAL/HAL_ESP32/HAL.h) #include "espcom.h" - +#if defined(HTTP_FEATURE) +//#include "web_server.h" +#include +#endif //HTTP_FEATURE void Esp3DCom::echo(const char * data) { SERIAL_ECHO_START(); SERIAL_ECHOLNPAIR("", data); } +long ESPResponseStream::baudRate() +{ + long br = flushableSerial.baudRate(); + //workaround for ESP32 + if (br == 115201) { + br = 115200; + } + if (br == 230423) { + br = 230400; + } + return br; +} +#if defined(HTTP_FEATURE) +ESPResponseStream::ESPResponseStream(WebServer * webserver){ + _header_sent=false; + _webserver = webserver; + _pipe = WEB_PIPE; +} +#endif //HTTP_FEATURE +ESPResponseStream::ESPResponseStream(tpipe pipe){ + _pipe = pipe; +} +void ESPResponseStream::println(const char *data){ + print(data); + print("\n"); +} + +void ESPResponseStream::print(const char *data){ +#if defined(HTTP_FEATURE) + if (_pipe == WEB_PIPE){ + if (!_header_sent) { + _webserver->setContentLength(CONTENT_LENGTH_UNKNOWN); + _webserver->sendHeader("Content-Type","text/html"); + _webserver->sendHeader("Cache-Control","no-cache"); + _webserver->send(200); + _header_sent = true; + } + _buffer+=data; + if (_buffer.length() > 1200) { + //send data + _webserver->sendContent(_buffer); + //reset buffer + _buffer = ""; + } + } +#endif //HTTP_FEATURE + if (_pipe == SERIAL_PIPE) { + SERIAL_ECHOPAIR_F("", data); + } +} + +void ESPResponseStream::flush(){ +#if defined(HTTP_FEATURE) + if (_pipe == WEB_PIPE){ + if(_header_sent) { + //send data + if(_buffer.length() > 0)_webserver->sendContent(_buffer); + //close connection + _webserver->sendContent(""); + } + _header_sent = false; + _buffer = ""; + } +#endif //HTTP_FEATURE +} + +//just simple helper to convert mac address to string +char * ESPResponseStream::mac2str (uint8_t mac [8]) +{ + static char macstr [18]; + if (0 > sprintf (macstr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) ) { + strcpy (macstr, "00:00:00:00:00:00"); + } + return macstr; +} + +//helper to format size to readable string +String ESPResponseStream::formatBytes (uint32_t bytes) +{ + if (bytes < 1024) { + return String (bytes) + " B"; + } else if (bytes < (1024 * 1024) ) { + return String (bytes / 1024.0) + " KB"; + } else if (bytes < (1024 * 1024 * 1024) ) { + return String (bytes / 1024.0 / 1024.0) + " MB"; + } else { + return String (bytes / 1024.0 / 1024.0 / 1024.0) + " GB"; + } +} #endif //ESP3D_WIFISUPPORT diff --git a/src/espcom.h b/src/espcom.h index aa00922..0c7101d 100644 --- a/src/espcom.h +++ b/src/espcom.h @@ -20,6 +20,53 @@ #ifndef ESPCOM_H #define ESPCOM_H +#include "esp3dlibconfig.h" +//Authentication levels +typedef enum { + LEVEL_GUEST = 0, + LEVEL_USER = 1, + LEVEL_ADMIN = 2 +} level_authenticate_type; + +//communication pipes +typedef enum { + NO_PIPE = 0, + SERIAL_PIPE = 2, +#ifdef TCP_IP_DATA_FEATURE + TCP_PIPE = 5, +#endif +#ifdef WS_DATA_FEATURE + WS_PIPE = 6, +#endif +#ifdef ESP_OLED_FEATURE + OLED_PIPE = 7, +#endif + WEB_PIPE = 8 +} tpipe; +#if defined(HTTP_FEATURE) +class WebServer; +#endif //HTTP_FEATURE +class ESPResponseStream{ + public: + void print(const char *data); + void println(const char *data); + void flush(); + tpipe pipe(){return _pipe;}; + ESPResponseStream(tpipe pipe); +#if defined(HTTP_FEATURE) + ESPResponseStream(WebServer * webserver); +#endif //HTTP_FEATURE + static char * mac2str (uint8_t mac [8]); + static String formatBytes (uint32_t bytes); + static long baudRate(); + private: +#if defined(HTTP_FEATURE) + bool _header_sent; + WebServer * _webserver; +#endif //HTTP_FEATURE + tpipe _pipe; + String _buffer; +}; class Esp3DCom { diff --git a/src/web_server.cpp b/src/web_server.cpp index 84bdcb5..3c3a968 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -20,15 +20,17 @@ #include "esp3dlibconfig.h" #if defined(ESP3D_WIFISUPPORT) && defined(HTTP_FEATURE) +#include "web_server.h" #include "espcom.h" #include "wificonfig.h" -#include MARLIN_PATH(gcode/queue.h) -#include MARLIN_PATH(inc/Version.h) +#include "command.h" +//#include MARLIN_PATH(gcode/queue.h) +//#include MARLIN_PATH(inc/Version.h) #undef DISABLED #undef _BV #include "wifiservices.h" #include "serial2socket.h" -#include "web_server.h" +#include "command.h" #include #include #include @@ -41,7 +43,7 @@ #include #include #include -#include +//#include #include #ifdef MDNS_FEATURE #include @@ -466,10 +468,10 @@ void Web_Server::handle_web_command () cmd_part2 = cmd.substring (ESPpos2 + 1); } //if command is a valid number then execute command - if (cmd_part1.toInt() != 0) { + if (cmd_part1.toInt() >= 0) { ESPResponseStream espresponse(_webserver); //commmand is web only - execute_internal_command (cmd_part1.toInt(), cmd_part2, auth_level, &espresponse); + COMMAND::execute_internal_command (cmd_part1.toInt(), cmd_part2, auth_level, &espresponse); //flush espresponse.flush(); } @@ -539,9 +541,9 @@ void Web_Server::handle_web_command_silent () cmd_part2 = cmd.substring (ESPpos2 + 1); } //if command is a valid number then execute command - if (cmd_part1.toInt() != 0) { + if (cmd_part1.toInt() >= 0) { //commmand is web only - if(execute_internal_command (cmd_part1.toInt(), cmd_part2, auth_level, NULL)) _webserver->send (200, "text/plain", "ok"); + if(COMMAND::execute_internal_command (cmd_part1.toInt(), cmd_part2, auth_level, NULL)) _webserver->send (200, "text/plain", "ok"); else _webserver->send (200, "text/plain", "error"); } //if not is not a valid [ESPXXX] command @@ -567,748 +569,6 @@ void Web_Server::handle_web_command_silent () } } - -bool Web_Server::execute_internal_command (int cmd, String cmd_params, level_authenticate_type auth_level, ESPResponseStream *espresponse) -{ - bool response = true; - level_authenticate_type auth_type = auth_level; - - //manage parameters - String parameter; - switch (cmd) { - //Get SD Card Status - //[ESP200] - case 200: - { - if (!espresponse) return false; - String resp = "No SD card"; -#if defined(SDSUPPORT) - ESP_SD card; - int8_t state = card.card_status(); - if (state == -1)resp="Busy"; - else if (state == 1)resp="SD card detected"; - else resp="No SD card"; -#endif - espresponse->println (resp.c_str()); - } - break; - //Get full ESP32 wifi settings content - //[ESP400] - case 400: - { - String v; - String defV; - Preferences prefs; - if (!espresponse) return false; -#ifdef AUTHENTICATION_FEATURE - if (auth_type == LEVEL_GUEST) return false; -#endif - int8_t vi; - espresponse->print("{\"EEPROM\":["); - prefs.begin(NAMESPACE, true); - //1 - Hostname - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (HOSTNAME_ENTRY); - espresponse->print ("\",\"T\":\"S\",\"V\":\""); - espresponse->print (_hostname.c_str()); - espresponse->print ("\",\"H\":\"Hostname\" ,\"S\":\""); - espresponse->print (String(MAX_HOSTNAME_LENGTH).c_str()); - espresponse->print ("\", \"M\":\""); - espresponse->print (String(MIN_HOSTNAME_LENGTH).c_str()); - espresponse->print ("\"}"); - espresponse->print (","); - - //2 - http protocol mode - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (HTTP_ENABLE_ENTRY); - espresponse->print ("\",\"T\":\"B\",\"V\":\""); - vi = prefs.getChar(HTTP_ENABLE_ENTRY, 1); - espresponse->print (String(vi).c_str()); - espresponse->print ("\",\"H\":\"HTTP protocol\",\"O\":[{\"Enabled\":\"1\"},{\"Disabled\":\"0\"}]}"); - espresponse->print (","); - - //3 - http port - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (HTTP_PORT_ENTRY); - espresponse->print ("\",\"T\":\"I\",\"V\":\""); - espresponse->print (String(_port).c_str()); - espresponse->print ("\",\"H\":\"HTTP Port\",\"S\":\""); - espresponse->print (String(MAX_HTTP_PORT).c_str()); - espresponse->print ("\",\"M\":\""); - espresponse->print (String(MIN_HTTP_PORT).c_str()); - espresponse->print ("\"}"); - espresponse->print (","); - - //TODO - //4 - telnet protocol mode - /* espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (TELNET_ENABLE_ENTRY); - espresponse->print ("\",\"T\":\"B\",\"V\":\""); - vi = prefs.getChar(TELNET_ENABLE_ENTRY, 0); - espresponse->print (String(vi).c_str()); - espresponse->print ("\",\"H\":\"Telnet protocol\",\"O\":[{\"Enabled\":\"1\"},{\"Disabled\":\"0\"}]}"); - espresponse->print (",");*/ - - //5 - telnet Port - /* espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (TELNET_PORT_ENTRY); - espresponse->print ("\",\"T\":\"I\",\"V\":\""); - espresponse->print (String(_data_port).c_str()); - espresponse->print ("\",\"H\":\"Telnet Port\",\"S\":\""); - espresponse->print (String(MAX_TELNET_PORT).c_str()); - espresponse->print ("\",\"M\":\""); - espresponse->print (String(MIN_TELNET_PORT).c_str()); - espresponse->print ("\"}"); - espresponse->print (",");*/ - - //6 - wifi mode - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (ESP_WIFI_MODE); - espresponse->print ("\",\"T\":\"B\",\"V\":\""); - vi = prefs.getChar(ESP_WIFI_MODE, ESP_WIFI_OFF); - espresponse->print (String(vi).c_str()); - espresponse->print ("\",\"H\":\"Wifi mode\",\"O\":[{\"STA\":\"1\"},{\"AP\":\"2\"},{\"None\":\"0\"}]}"); - espresponse->print (","); - - //7 - STA SSID - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (STA_SSID_ENTRY); - espresponse->print ("\",\"T\":\"S\",\"V\":\""); - defV = DEFAULT_STA_SSID; - espresponse->print (prefs.getString(STA_SSID_ENTRY, defV).c_str()); - espresponse->print ("\",\"S\":\""); - espresponse->print (String(MAX_SSID_LENGTH).c_str()); - espresponse->print ("\",\"H\":\"Station SSID\",\"M\":\""); - espresponse->print (String(MIN_SSID_LENGTH).c_str()); - espresponse->print ("\"}"); - espresponse->print (","); - - //8 - STA password - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (STA_PWD_ENTRY); - espresponse->print ("\",\"T\":\"S\",\"V\":\""); - espresponse->print (HIDDEN_PASSWORD); - espresponse->print ("\",\"S\":\""); - espresponse->print (String(MAX_PASSWORD_LENGTH).c_str()); - espresponse->print ("\",\"H\":\"Station Password\",\"M\":\""); - espresponse->print (String(MIN_PASSWORD_LENGTH).c_str()); - espresponse->print ("\"}"); - espresponse->print (","); - - // 9 - STA IP mode - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (STA_IP_MODE_ENTRY); - espresponse->print ("\",\"T\":\"B\",\"V\":\""); - espresponse->print (String(prefs.getChar(STA_IP_MODE_ENTRY, DHCP_MODE)).c_str()); - espresponse->print ("\",\"H\":\"Station IP Mode\",\"O\":[{\"DHCP\":\"0\"},{\"Static\":\"1\"}]}"); - espresponse->print (","); - - //10-STA static IP - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (STA_IP_ENTRY); - espresponse->print ("\",\"T\":\"A\",\"V\":\""); - espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(STA_IP_ENTRY, 0)).c_str()); - espresponse->print ("\",\"H\":\"Station Static IP\"}"); - espresponse->print (","); - - //11-STA static Gateway - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (STA_GW_ENTRY); - espresponse->print ("\",\"T\":\"A\",\"V\":\""); - espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(STA_GW_ENTRY, 0)).c_str()); - espresponse->print ("\",\"H\":\"Station Static Gateway\"}"); - espresponse->print (","); - - //12-STA static Mask - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (STA_MK_ENTRY); - espresponse->print ("\",\"T\":\"A\",\"V\":\""); - espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(STA_MK_ENTRY, 0)).c_str()); - espresponse->print ("\",\"H\":\"Station Static Mask\"}"); - espresponse->print (","); - - //13 - AP SSID - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (AP_SSID_ENTRY); - espresponse->print ("\",\"T\":\"S\",\"V\":\""); - defV = DEFAULT_AP_SSID; - espresponse->print (prefs.getString(AP_SSID_ENTRY, defV).c_str()); - espresponse->print ("\",\"S\":\""); - espresponse->print (String(MAX_SSID_LENGTH).c_str()); - espresponse->print ("\",\"H\":\"AP SSID\",\"M\":\""); - espresponse->print (String(MIN_SSID_LENGTH).c_str()); - espresponse->print ("\"}"); - espresponse->print (","); - - //14 - AP password - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (AP_PWD_ENTRY); - espresponse->print ("\",\"T\":\"S\",\"V\":\""); - espresponse->print (HIDDEN_PASSWORD); - espresponse->print ("\",\"S\":\""); - espresponse->print (String(MAX_PASSWORD_LENGTH).c_str()); - espresponse->print ("\",\"H\":\"AP Password\",\"M\":\""); - espresponse->print (String(MIN_PASSWORD_LENGTH).c_str()); - espresponse->print ("\"}"); - espresponse->print (","); - - //15 - AP static IP - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (AP_IP_ENTRY); - espresponse->print ("\",\"T\":\"A\",\"V\":\""); - defV = DEFAULT_AP_IP; - espresponse->print (WiFiConfig::IP_string_from_int(prefs.getInt(AP_IP_ENTRY, WiFiConfig::IP_int_from_string(defV))).c_str()); - espresponse->print ("\",\"H\":\"AP Static IP\"}"); - espresponse->print (","); - - //16 - AP Channel - espresponse->print ("{\"F\":\"network\",\"P\":\""); - espresponse->print (AP_CHANNEL_ENTRY); - espresponse->print ("\",\"T\":\"B\",\"V\":\""); - espresponse->print (String(prefs.getChar(AP_CHANNEL_ENTRY, DEFAULT_AP_CHANNEL)).c_str()); - espresponse->print ("\",\"H\":\"AP Channel\",\"O\":["); - for (int i = MIN_CHANNEL; i <= MAX_CHANNEL ; i++) { - espresponse->print ("{\""); - espresponse->print (String(i).c_str()); - espresponse->print ("\":\""); - espresponse->print (String(i).c_str()); - espresponse->print ("\"}"); - if (i < MAX_CHANNEL) { - espresponse->print (","); - } - } - espresponse->print ("]}"); - - espresponse->print ("]}"); - prefs.end(); - } - break; - //Set EEPROM setting - //[ESP401]P= T= V= pwd= - case 401: - { -#ifdef AUTHENTICATION_FEATURE - if (auth_type != LEVEL_ADMIN) return false; -#endif - //check validity of parameters - String spos = get_param (cmd_params, "P=", false); - String styp = get_param (cmd_params, "T=", false); - String sval = get_param (cmd_params, "V=", true); - spos.trim(); - sval.trim(); - if (spos.length() == 0) { - response = false; - } - if (! (styp == "B" || styp == "S" || styp == "A" || styp == "I" || styp == "F") ) { - response = false; - } - if ((sval.length() == 0) && !((spos==AP_PWD_ENTRY) || (spos==STA_PWD_ENTRY))){ - response = false; - } - - if (response) { - Preferences prefs; - prefs.begin(NAMESPACE, false); - //Byte value - if ((styp == "B") || (styp == "F")){ - int8_t bbuf = sval.toInt(); - if (prefs.putChar(spos.c_str(), bbuf) ==0 ) { - response = false; - } else { - //dynamique refresh is better than restart the board - if (spos == ESP_WIFI_MODE){ - //TODO - } - if (spos == AP_CHANNEL_ENTRY) { - //TODO - } - if (spos == HTTP_ENABLE_ENTRY) { - //TODO - } - if (spos == TELNET_ENABLE_ENTRY) { - //TODO - } - } - } - //Integer value - if (styp == "I") { - int16_t ibuf = sval.toInt(); - if (prefs.putUShort(spos.c_str(), ibuf) == 0) { - response = false; - } else { - if (spos == HTTP_PORT_ENTRY){ - //TODO - } - if (spos == TELNET_PORT_ENTRY){ - //TODO - //Serial.println(ibuf); - } - } - - } - //String value - if (styp == "S") { - if (prefs.putString(spos.c_str(), sval) == 0) { - response = false; - } else { - if (spos == HOSTNAME_ENTRY){ - //TODO - } - if (spos == STA_SSID_ENTRY){ - //TODO - } - if (spos == STA_PWD_ENTRY){ - //TODO - } - if (spos == AP_SSID_ENTRY){ - //TODO - } - if (spos == AP_PWD_ENTRY){ - //TODO - } - } - - } - //IP address - if (styp == "A") { - if (prefs.putInt(spos.c_str(), WiFiConfig::IP_int_from_string(sval)) == 0) { - response = false; - } else { - if (spos == STA_IP_ENTRY){ - //TODO - } - if (spos == STA_GW_ENTRY){ - //TODO - } - if (spos == STA_MK_ENTRY){ - //TODO - } - if (spos == AP_IP_ENTRY){ - //TODO - } - } - } - prefs.end(); - } - if (!response) { - if (espresponse) espresponse->println ("Error: Incorrect Command"); - } else { - if (espresponse) espresponse->println ("ok"); - } - - } - break; - //Get available AP list (limited to 30) - //output is JSON - //[ESP410] - case 410: { - if (!espresponse)return false; -#ifdef AUTHENTICATION_FEATURE - if (auth_type == LEVEL_GUEST) return false; -#endif - espresponse->print("{\"AP_LIST\":["); - int n = WiFi.scanNetworks(); - if (n > 0) { - for (int i = 0; i < n; ++i) { - if (i > 0) { - espresponse->print (","); - } - espresponse->print ("{\"SSID\":\""); - espresponse->print (WiFi.SSID (i).c_str()); - espresponse->print ("\",\"SIGNAL\":\""); - espresponse->print (String(WiFiConfig::getSignal (WiFi.RSSI (i) )).c_str()); - espresponse->print ("\",\"IS_PROTECTED\":\""); - - if (WiFi.encryptionType (i) == WIFI_AUTH_OPEN) { - espresponse->print ("0"); - } else { - espresponse->print ("1"); - } - espresponse->print ("\"}"); - Esp3DLibConfig::wait(0); - } - WiFi.scanDelete(); - } - espresponse->print ("]}"); - //Ugly fix for the AP mode - if (WiFi.getMode() == WIFI_AP_STA) { - WiFi.enableSTA (false); - } - } - break; - //Get ESP current status - case 420: - { -#ifdef AUTHENTICATION_FEATURE - if (auth_type == LEVEL_GUEST) return false; -#endif - if (!espresponse)return false; - espresponse->print ("Chip ID: "); - espresponse->print (String ( (uint16_t) (ESP.getEfuseMac() >> 32) ).c_str()); - espresponse->print ("\n"); - espresponse->print ("CPU Frequency: "); - espresponse->print (String (ESP.getCpuFreqMHz() ).c_str()); - espresponse->print ("Mhz"); - espresponse->print ("\n"); - espresponse->print ("CPU Temperature: "); - espresponse->print (String (temperatureRead(), 1).c_str()); - espresponse->print ("°C"); - espresponse->print ("\n"); - espresponse->print ("Free memory: "); - espresponse->print (formatBytes (ESP.getFreeHeap()).c_str()); - espresponse->print ("\n"); - espresponse->print ("SDK: "); - espresponse->print (ESP.getSdkVersion()); - espresponse->print ("\n"); - espresponse->print ("Flash Size: "); - espresponse->print (formatBytes (ESP.getFlashChipSize()).c_str()); - espresponse->print ("\n"); - espresponse->print ("Available Size for update: "); - //Not OTA on 2Mb board per spec - if (ESP.getFlashChipSize() > 0x20000) { - espresponse->print (formatBytes (0x140000).c_str()); - } else { - espresponse->print (formatBytes (0x0).c_str()); - } - espresponse->print ("\n"); - espresponse->print ("Available Size for SPIFFS: "); - espresponse->print (formatBytes (SPIFFS.totalBytes()).c_str()); - espresponse->print ("\n"); - espresponse->print ("Baud rate: "); - long br = Serial.baudRate(); - //workaround for ESP32 - if (br == 115201) { - br = 115200; - } - if (br == 230423) { - br = 230400; - } - espresponse->print (String(br).c_str()); - espresponse->print ("\n"); - espresponse->print ("Sleep mode: "); - if (WiFi.getSleep())espresponse->print ("Modem"); - else espresponse->print ("None"); - espresponse->print ("\n"); - espresponse->print ("Web port: "); - espresponse->print (String(_port).c_str()); - espresponse->print ("\n"); - espresponse->print ("Data port: "); - if (_data_port!=0)espresponse->print (String(_data_port).c_str()); - else espresponse->print ("Disabled"); - espresponse->print ("\n"); - espresponse->print ("Hostname: "); - espresponse->print ( _hostname.c_str()); - espresponse->print ("\n"); - espresponse->print ("Active Mode: "); - if (WiFi.getMode() == WIFI_STA) { - espresponse->print ("STA ("); - espresponse->print ( WiFi.macAddress().c_str()); - espresponse->print (")"); - espresponse->print ("\n"); - espresponse->print ("Connected to: "); - if (WiFi.isConnected()){ //in theory no need but ... - espresponse->print (WiFi.SSID().c_str()); - espresponse->print ("\n"); - espresponse->print ("Signal: "); - espresponse->print ( String(WiFiConfig::getSignal (WiFi.RSSI())).c_str()); - espresponse->print ("%"); - espresponse->print ("\n"); - uint8_t PhyMode; - esp_wifi_get_protocol (ESP_IF_WIFI_STA, &PhyMode); - espresponse->print ("Phy Mode: "); - if (PhyMode == (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N)) espresponse->print ("11n"); - else if (PhyMode == (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G)) espresponse->print ("11g"); - else if (PhyMode == (WIFI_PROTOCOL_11B )) espresponse->print ("11b"); - else espresponse->print ("???"); - espresponse->print ("\n"); - espresponse->print ("Channel: "); - espresponse->print (String (WiFi.channel()).c_str()); - espresponse->print ("\n"); - espresponse->print ("IP Mode: "); - tcpip_adapter_dhcp_status_t dhcp_status; - tcpip_adapter_dhcpc_get_status (TCPIP_ADAPTER_IF_STA, &dhcp_status); - if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED)espresponse->print ("DHCP"); - else espresponse->print ("Static"); - espresponse->print ("\n"); - espresponse->print ("IP: "); - espresponse->print (WiFi.localIP().toString().c_str()); - espresponse->print ("\n"); - espresponse->print ("Gateway: "); - espresponse->print (WiFi.gatewayIP().toString().c_str()); - espresponse->print ("\n"); - espresponse->print ("Mask: "); - espresponse->print (WiFi.subnetMask().toString().c_str()); - espresponse->print ("\n"); - espresponse->print ("DNS: "); - espresponse->print (WiFi.dnsIP().toString().c_str()); - espresponse->print ("\n"); - } //this is web command so connection => no command - espresponse->print ("Disabled Mode: "); - espresponse->print ("AP ("); - espresponse->print (WiFi.softAPmacAddress().c_str()); - espresponse->print (")"); - espresponse->print ("\n"); - } else if (WiFi.getMode() == WIFI_AP) { - espresponse->print ("AP ("); - espresponse->print (WiFi.softAPmacAddress().c_str()); - espresponse->print (")"); - espresponse->print ("\n"); - wifi_config_t conf; - esp_wifi_get_config (ESP_IF_WIFI_AP, &conf); - espresponse->print ("SSID: "); - espresponse->print ((const char*) conf.ap.ssid); - espresponse->print ("\n"); - espresponse->print ("Visible: "); - espresponse->print ( (conf.ap.ssid_hidden == 0) ? "Yes" : "No"); - espresponse->print ("\n"); - espresponse->print ("Authentication: "); - if (conf.ap.authmode == WIFI_AUTH_OPEN) { - espresponse->print ("None"); - } else if (conf.ap.authmode == WIFI_AUTH_WEP) { - espresponse->print ("WEP"); - } else if (conf.ap.authmode == WIFI_AUTH_WPA_PSK) { - espresponse->print ("WPA"); - } else if (conf.ap.authmode == WIFI_AUTH_WPA2_PSK) { - espresponse->print ("WPA2"); - } else { - espresponse->print ("WPA/WPA2"); - } - espresponse->print ("\n"); - espresponse->print ("Max Connections: "); - espresponse->print (String(conf.ap.max_connection).c_str()); - espresponse->print ("\n"); - espresponse->print ("DHCP Server: "); - tcpip_adapter_dhcp_status_t dhcp_status; - tcpip_adapter_dhcps_get_status (TCPIP_ADAPTER_IF_AP, &dhcp_status); - if (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED)espresponse->print ("Started"); - else espresponse->print ("Stopped"); - espresponse->print ("\n"); - espresponse->print ("IP: "); - espresponse->print (WiFi.softAPIP().toString().c_str()); - espresponse->print ("\n"); - tcpip_adapter_ip_info_t ip_AP; - tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_AP, &ip_AP); - espresponse->print ("Gateway: "); - espresponse->print (IPAddress (ip_AP.gw.addr).toString().c_str()); - espresponse->print ("\n"); - espresponse->print ("Mask: "); - espresponse->print (IPAddress (ip_AP.netmask.addr).toString().c_str()); - espresponse->print ("\n"); - espresponse->print ("Connected clients: "); - wifi_sta_list_t station; - tcpip_adapter_sta_list_t tcpip_sta_list; - esp_wifi_ap_get_sta_list (&station); - tcpip_adapter_get_sta_list (&station, &tcpip_sta_list); - espresponse->print (String(station.num).c_str()); - espresponse->print ("\n"); - for (int i = 0; i < station.num; i++) { - espresponse->print (mac2str(tcpip_sta_list.sta[i].mac)); - espresponse->print (" "); - espresponse->print ( IPAddress (tcpip_sta_list.sta[i].ip.addr).toString().c_str()); - espresponse->print ("\n"); - } - espresponse->print ("Disabled Mode: "); - espresponse->print ("STA ("); - espresponse->print (WiFi.macAddress().c_str()); - espresponse->print (")"); - espresponse->print ("\n"); - } else if (WiFi.getMode() == WIFI_AP_STA) //we should not be in this state but just in case .... - { - espresponse->print ("Mixed"); - espresponse->print ("\n"); - espresponse->print ("STA ("); - espresponse->print (WiFi.macAddress().c_str()); - espresponse->print (")"); - espresponse->print ("\n"); - espresponse->print ("AP ("); - espresponse->print (WiFi.softAPmacAddress().c_str()); - espresponse->print (")"); - espresponse->print ("\n"); - - } else { //we should not be there if no wifi .... - espresponse->print ("Wifi Off"); - espresponse->print ("\n"); - } - //TODO to complete - espresponse->print ("FW version: Marlin "); - espresponse->print (SHORT_BUILD_VERSION); - espresponse->print (" (ESP32)"); - } - break; - //Set ESP mode - //cmd is RESTART - //[ESP444] - case 444: - parameter = get_param(cmd_params,"", true); -#ifdef AUTHENTICATION_FEATURE - if (auth_type != LEVEL_ADMIN) { - response = false; - } else -#endif - { - if (parameter=="RESTART") { - Esp3DCom::echo("Restart ongoing"); - Esp3DLibConfig::restart_ESP(); - } else response = false; - } - if (!response) { - if (espresponse)espresponse->println ("Error: Incorrect Command"); - } else { - if (espresponse)espresponse->println ("ok"); - } - break; -#ifdef AUTHENTICATION_FEATURE - //Change / Reset user password - //[ESP555] - case 555: { - if (auth_type == LEVEL_ADMIN) { - parameter = get_param (cmd_params, "", true); - if (parameter.length() == 0) { - Preferences prefs; - parameter = DEFAULT_USER_PWD; - prefs.begin(NAMESPACE, false); - if (prefs.putString(USER_PWD_ENTRY, parameter) != parameter.length()){ - response = false; - espresponse->println ("error"); - } else espresponse->println ("ok"); - prefs.end(); - - } else { - if (isLocalPasswordValid (parameter.c_str() ) ) { - Preferences prefs; - prefs.begin(NAMESPACE, false); - if (prefs.putString(USER_PWD_ENTRY, parameter) != parameter.length()) { - response = false; - espresponse->println ("error"); - } else espresponse->println ("ok"); - prefs.end(); - } else { - espresponse->println ("error"); - response = false; - } - } - } else { - espresponse->println ("error"); - response = false; - } - break; - } -#endif - //[ESP700] - case 700: { //read local file -#ifdef AUTHENTICATION_FEATURE - if (auth_type == LEVEL_GUEST) return false; -#endif - cmd_params.trim() ; - if ( (cmd_params.length() > 0) && (cmd_params[0] != '/') ) { - cmd_params = "/" + cmd_params; - } - File currentfile = SPIFFS.open (cmd_params, FILE_READ); - if (currentfile) {//if file open success - //until no line in file - while (currentfile.available()) { - String currentline = currentfile.readStringUntil('\n'); - currentline.replace("\n",""); - currentline.replace("\r",""); - if (currentline.length() > 0) { - int ESPpos = currentline.indexOf ("[ESP"); - if (ESPpos > -1) { - //is there the second part? - int ESPpos2 = currentline.indexOf ("]", ESPpos); - if (ESPpos2 > -1) { - //Split in command and parameters - String cmd_part1 = currentline.substring (ESPpos + 4, ESPpos2); - String cmd_part2 = ""; - //is there space for parameters? - if (ESPpos2 < currentline.length() ) { - cmd_part2 = currentline.substring (ESPpos2 + 1); - } - //if command is a valid number then execute command - if(cmd_part1.toInt()!=0) { - if (!execute_internal_command(cmd_part1.toInt(),cmd_part2, auth_type, espresponse)) response = false; - } - //if not is not a valid [ESPXXX] command ignore it - } - } else { - if (currentline.length() > 0){ - currentline+="\n"; - Serial2Socket.push(currentline.c_str()); - //GCodeQueue::enqueue_one_now(currentline.c_str()); - } - Esp3DLibConfig::wait (1); - } - Esp3DLibConfig::wait (1); - } - } - currentfile.close(); - if (espresponse)espresponse->println ("ok"); - } else { - if (espresponse)espresponse->println ("error"); - response = false; - } - break; - } - //Format SPIFFS - //[ESP710]FORMAT pwd= - case 710: -#ifdef AUTHENTICATION_FEATURE - if (auth_type != LEVEL_ADMIN) return false; -#endif - parameter = get_param (cmd_params, "", true); -#ifdef AUTHENTICATION_FEATURE - if (auth_type != LEVEL_ADMIN) { - espresponse->println ("error"); - response = false; - break; - } else -#endif - { - if (parameter == "FORMAT") { - if (espresponse)espresponse->print ("Formating"); - SPIFFS.format(); - if (espresponse)espresponse->println ("...Done"); - } else { - if (espresponse)espresponse->println ("error"); - response = false; - } - } - break; - //get fw version / fw target / hostname / authentication - //[ESP800] - case 800: - { - if (!espresponse)return false; - String resp; - resp = "FW version:"; - resp += SHORT_BUILD_VERSION; - resp += " # FW target:marlin-embedded # FW HW:"; - #if defined(SDSUPPORT) - resp += "Direct SD"; - #else - resp += "No SD"; - #endif - resp += " # primary sd:/sd # secondary sd:none # authentication:"; - #ifdef AUTHENTICATION_FEATURE - resp += "yes"; - #else - resp += "no"; - #endif - resp += " # webcommunication: Sync: "; - resp += String(_port + 1); - resp += "# hostname:"; - resp += _hostname; - if (WiFi.getMode() == WIFI_AP)resp += "(AP mode)"; - if (espresponse)espresponse->println (resp.c_str()); - } - break; - default: - if (espresponse)espresponse->println ("Error: Incorrect Command"); - response = false; - break; - } - return response; -} - //login status check void Web_Server::handle_login() { @@ -1626,7 +886,7 @@ void Web_Server::handleFileList () } else { //do not add "." file if (! ( (filename == ".") || (filename == "") ) ) { - size = formatBytes (fileparsed.size() ); + size = ESPResponseStream::formatBytes (fileparsed.size() ); } else { addtolist = false; } @@ -1654,8 +914,8 @@ void Web_Server::handleFileList () size_t usedBytes; totalBytes = SPIFFS.totalBytes(); usedBytes = SPIFFS.usedBytes(); - jsonfile += "\"total\":\"" + formatBytes (totalBytes) + "\","; - jsonfile += "\"used\":\"" + formatBytes (usedBytes) + "\","; + jsonfile += "\"total\":\"" + ESPResponseStream::formatBytes (totalBytes) + "\","; + jsonfile += "\"used\":\"" + ESPResponseStream::formatBytes (usedBytes) + "\","; jsonfile.concat (F ("\"occupation\":\"") ); jsonfile += String (100 * usedBytes / totalBytes); jsonfile += "\""; @@ -1996,7 +1256,7 @@ void Web_Server::handle_direct_SDFileList() jsonfile+="\",\"shortname\":\""; jsonfile+=name; jsonfile+="\",\"size\":\""; - if (isFile)jsonfile+=formatBytes(size); + if (isFile)jsonfile+=ESPResponseStream::formatBytes(size); else jsonfile+="-1"; jsonfile+="\",\"datetime\":\""; //TODO datatime @@ -2016,10 +1276,10 @@ void Web_Server::handle_direct_SDFileList() if ( (occupedspace <= 1) && (volTotal!=volUsed)) { occupedspace=1; } - jsonfile+= formatBytes(volTotal); ; + jsonfile+= ESPResponseStream::formatBytes(volTotal); ; jsonfile+="\",\"used\":\""; - jsonfile+= formatBytes(volUsed); ; + jsonfile+= ESPResponseStream::formatBytes(volUsed); ; jsonfile+="\",\"occupation\":\""; jsonfile+=String(occupedspace); jsonfile+= "\","; @@ -2184,16 +1444,6 @@ void Web_Server::handle_Websocket_Event(uint8_t num, uint8_t type, uint8_t * pay } -//just simple helper to convert mac address to string -char * Web_Server::mac2str (uint8_t mac [8]) -{ - static char macstr [18]; - if (0 > sprintf (macstr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) ) { - strcpy (macstr, "00:00:00:00:00:00"); - } - return macstr; -} - String Web_Server::get_Splited_Value(String data, char separator, int index) { int found = 0; @@ -2212,20 +1462,6 @@ String Web_Server::get_Splited_Value(String data, char separator, int index) } -//helper to format size to readable string -String Web_Server::formatBytes (uint32_t bytes) -{ - if (bytes < 1024) { - return String (bytes) + " B"; - } else if (bytes < (1024 * 1024) ) { - return String (bytes / 1024.0) + " KB"; - } else if (bytes < (1024 * 1024 * 1024) ) { - return String (bytes / 1024.0 / 1024.0) + " MB"; - } else { - return String (bytes / 1024.0 / 1024.0 / 1024.0) + " GB"; - } -} - //helper to extract content type from file extension //Check what is the content tye according extension file String Web_Server::getContentType (String filename) @@ -2421,79 +1657,5 @@ level_authenticate_type Web_Server::ResetAuthIP (IPAddress ip, const char * sess } #endif -String Web_Server::get_param (String & cmd_params, const char * id, bool withspace) -{ - static String parameter; - String sid = id; - int start; - int end = -1; - parameter = ""; - //if no id it means it is first part of cmd - if (strlen (id) == 0) { - start = 0; - } - //else find id position - else { - start = cmd_params.indexOf (id); - } - //if no id found and not first part leave - if (start == -1 ) { - return parameter; - } - //password and SSID can have space so handle it - //if no space expected use space as delimiter - if (!withspace) { - end = cmd_params.indexOf (" ", start); - } - //if no end found - take all - if (end == -1) { - end = cmd_params.length(); - } - //extract parameter - parameter = cmd_params.substring (start + strlen (id), end); - //be sure no extra space - parameter.trim(); - return parameter; -} - -ESPResponseStream::ESPResponseStream(WebServer * webserver){ - _header_sent=false; - _webserver = webserver; -} - -void ESPResponseStream::println(const char *data){ - print(data); - print("\n"); -} - -void ESPResponseStream::print(const char *data){ - if (!_header_sent) { - _webserver->setContentLength(CONTENT_LENGTH_UNKNOWN); - _webserver->sendHeader("Content-Type","text/html"); - _webserver->sendHeader("Cache-Control","no-cache"); - _webserver->send(200); - _header_sent = true; - } - _buffer+=data; - if (_buffer.length() > 1200) { - //send data - _webserver->sendContent(_buffer); - //reset buffer - _buffer = ""; - } - -} - -void ESPResponseStream::flush(){ - if(_header_sent) { - //send data - if(_buffer.length() > 0)_webserver->sendContent(_buffer); - //close connection - _webserver->sendContent(""); - } - _header_sent = false; - _buffer = ""; - -} #endif // ESP3D_WIFISUPPORT && HTTP_FEATURE diff --git a/src/web_server.h b/src/web_server.h index c9ba283..c4a1b8a 100644 --- a/src/web_server.h +++ b/src/web_server.h @@ -22,17 +22,10 @@ #ifndef _WEB_SERVER_H #define _WEB_SERVER_H - #include "wificonfig.h" +#include "espcom.h" class WebSocketsServer; -class WebServer; -//Authentication level -typedef enum { - LEVEL_GUEST = 0, - LEVEL_USER = 1, - LEVEL_ADMIN = 2 -} level_authenticate_type; #ifdef AUTHENTICATION_FEATURE struct auth_ip { @@ -46,21 +39,6 @@ struct auth_ip { #endif - - - -class ESPResponseStream{ - public: - void print(const char *data); - void println(const char *data); - void flush(); - ESPResponseStream(WebServer * webserver); - private: - bool _header_sent; - WebServer * _webserver; - String _buffer; -}; - class Web_Server { public: Web_Server(); @@ -78,8 +56,6 @@ class Web_Server { static uint16_t _data_port; static String _hostname; static uint8_t _upload_status; - static char * mac2str (uint8_t mac [8]); - static String formatBytes (uint32_t bytes); static String getContentType (String filename); static String get_Splited_Value(String data, char separator, int index); static level_authenticate_type is_authenticated(); @@ -93,8 +69,6 @@ class Web_Server { static level_authenticate_type ResetAuthIP (IPAddress ip, const char * sessionID); static bool isLocalPasswordValid (const char * password); #endif - static String get_param (String & cmd_params, const char * id, bool withspace); - static bool execute_internal_command (int cmd, String cmd_params, level_authenticate_type auth_level, ESPResponseStream *espresponse); #ifdef SSDP_FEATURE static void handle_SSDP (); #endif //SSDP_FEATURE diff --git a/src/wificonfig.cpp b/src/wificonfig.cpp index df1a6b6..a3c0e44 100644 --- a/src/wificonfig.cpp +++ b/src/wificonfig.cpp @@ -246,7 +246,7 @@ bool WiFiConfig::StartSTA(){ //password defV = DEFAULT_STA_PWD; String password = prefs.getString(STA_PWD_ENTRY, defV); - int8_t IP_mode = prefs.getChar(STA_IP_MODE_ENTRY, DHCP_MODE); + int8_t IP_mode = prefs.getChar(STA_IP_MODE_ENTRY, DEFAULT_STA_IP_MODE); //IP defV = DEFAULT_STA_IP; int32_t IP = prefs.getInt(STA_IP_ENTRY, IP_int_from_string(defV)); @@ -346,7 +346,7 @@ void WiFiConfig::begin() { WiFi.onEvent(WiFiConfig::WiFiEvent); //open preferences as read-only prefs.begin(NAMESPACE, true); - int8_t wifiMode = prefs.getChar(ESP_WIFI_MODE, DEFAULT_WIFI_MODE); + int8_t wifiMode = prefs.getChar(ESP_RADIO_MODE, DEFAULT_RADIO_MODE); prefs.end(); if (wifiMode == ESP_WIFI_AP) { StartAP(); diff --git a/src/wificonfig.h b/src/wificonfig.h index bbc03ef..72abc96 100644 --- a/src/wificonfig.h +++ b/src/wificonfig.h @@ -21,48 +21,6 @@ //Services that need to be used #include "esp3dlibconfig.h" -//Wifi Mode -#define ESP_WIFI_OFF 0 -#define ESP_WIFI_STA 1 -#define ESP_WIFI_AP 2 - -#define DHCP_MODE 0 -#define STATIC_MODE 0 - -//defaults values -#define DEFAULT_HOSTNAME "marlinesp" -#define DEFAULT_STA_SSID "MARLIN_ESP" -#define DEFAULT_STA_PWD "12345678" -#define DEFAULT_STA_IP "0.0.0.0" -#define DEFAULT_STA_GW "0.0.0.0" -#define DEFAULT_STA_MK "0.0.0.0" -#define DEFAULT_WIFI_MODE ESP_WIFI_AP -#define DEFAULT_AP_SSID "MARLIN_ESP" -#define DEFAULT_AP_PWD "12345678" -#define DEFAULT_AP_IP "192.168.0.1" -#define DEFAULT_AP_MK "255.255.255.0" -#define DEFAULT_AP_CHANNEL 1 -#define DEFAULT_WEBSERVER_PORT 80 -#define DEFAULT_HTTP_STATE 1 -#define HIDDEN_PASSWORD "********" - -//boundaries -#define MAX_SSID_LENGTH 32 -#define MIN_SSID_LENGTH 1 -#define MAX_PASSWORD_LENGTH 64 -//min size of password is 0 or upper than 8 char -//so let set min is 8 -#define MIN_PASSWORD_LENGTH 8 -#define MAX_HOSTNAME_LENGTH 32 -#define MIN_HOSTNAME_LENGTH 1 -#define MAX_HTTP_PORT 65001 -#define MIN_HTTP_PORT 1 -#define MAX_TELNET_PORT 65001 -#define MIN_TELNET_PORT 1 -#define MIN_CHANNEL 1 -#define MAX_CHANNEL 14 - - #ifndef _WIFI_CONFIG_H #define _WIFI_CONFIG_H