diff --git a/.vscode/settings.json b/.vscode/settings.json index 03671897..3f9b2dc8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,14 @@ "files.associations": { "*.tcc": "cpp", "fstream": "cpp", - "string": "cpp" + "string": "cpp", + "array": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp" }, "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/ESP3D-features.xls b/ESP3D-features.xls deleted file mode 100644 index 00659626..00000000 Binary files a/ESP3D-features.xls and /dev/null differ diff --git a/Features.md b/Features.md index b84b7b30..655c1394 100644 --- a/Features.md +++ b/Features.md @@ -7,6 +7,7 @@ * Raw TCP / serial bridge support (light telnet) * Boot delay configuration * Websocket / serial bridge support +* Serial / Serial bridge support * Bluetooth Serial bridge support (when BT supported) * MKS Serial protocol support * Serial commands configurations diff --git a/docs/Commands.md b/docs/Commands.md index c1e6b1e3..85eb9695 100644 --- a/docs/Commands.md +++ b/docs/Commands.md @@ -209,7 +209,10 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling ESP_WEBDAV_ON 1024 //1 byte = flag ESP_WEBDAV_PORT 1025 //4 bytes = int ESP_STA_DNS_VALUE 1029 //4 bytes= int - ESP_SECURE_SERIAL 1033 //1 byte = flag \ + ESP_SECURE_SERIAL 1033 //1 byte = flag + ESP_SERIAL_BRIDGE_ON 1036 //1 byte = flag + ESP_SERIAL_BRIDGE_FLAG 1037 //1 byte = flag + ESP_SERIAL_BRIDGE_BAUD 1038 //4 bytes= int ``` * Get/Set Check update at boot state which can be ON, OFF @@ -285,15 +288,21 @@ label can be: light/framesize/quality/contrast/brightness/saturation/gainceiling * Get state / Set Enable / Disable Serial Communication `[ESP900] json= pwd=` - -* Get state / Set Enable / Disable verbose boot - `[ESP901] json= pwd=` + +* Get / Set Serial Baud Rate + `[ESP901] json= pwd=` * Get state / Set Enable / Disable buzzer `[ESP910] json= pwd=` * Get state / Set state of output message clients - `[ESP920]= json= pwd=` + `[ESP920]= json= pwd=` + +* Get state / Set Enable / Disable Serial Bridge Communication + `[ESP930] json= pwd=` +* Get / Set Serial Bridge Baud Rate + `[ESP931] json= pwd=` + * Set quiet boot if strapping pin is High `[ESP999]QUIETBOOT pwd=` \ No newline at end of file diff --git a/docs/esp3dcnf.ini b/docs/esp3dcnf.ini index ec9f38bc..5fec3b16 100644 --- a/docs/esp3dcnf.ini +++ b/docs/esp3dcnf.ini @@ -45,6 +45,12 @@ AP_IP = 192.168.0.1 AP_channel = 11 [services] +#Active or not Serial Bridge Yes / No +Serial_Bridge_active = Yes + +# Serial Bridge baudrate +Serial_Bridge_Baud = 115200 + #Active or not HTTP Yes / No HTTP_active = Yes @@ -150,6 +156,8 @@ Active_Remote_Screen = Yes Active_ESP3D_Screen = Yes #ESP3D Serial Yes / No Active_Serial = Yes +#Serial Bridge Yes / No +Active_Serial_Bridge = Yes #Websocket Yes / No Active_WebSocket = Yes #Telnet Yes / No diff --git a/esp3d/configuration.h b/esp3d/configuration.h index 9dc604a9..d23443bb 100644 --- a/esp3d/configuration.h +++ b/esp3d/configuration.h @@ -50,14 +50,24 @@ */ #define COMMUNICATION_PROTOCOL RAW_SERIAL -/* Serial port +/* Main Serial port * which serial ESP use to communicate to printer (ESP32 has 3 serials available, ESP8266 only 2) * USE_SERIAL_0 //for ESP8266/32, also used by bootloader output, so consider to make it quiet * USE_SERIAL_1 //for ESP8266/32 * USE_SERIAL_2 //for ESP32 Only */ +//Main serial port #define ESP_SERIAL_OUTPUT USE_SERIAL_0 +/* Bridge Serial port (deprecated on esp8266 as second serial is) +* which serial ESP use to bridge to another device (ESP32 has 3 serials available, ESP8266 only 2) +* USE_SERIAL_0 //for ESP8266/32, also used by bootloader output, so consider to make it quiet +* USE_SERIAL_1 //for ESP8266/32 +* USE_SERIAL_2 //for ESP32 Only\ +* Comment if not used +*/ +//#define ESP_SERIAL_BRIDGE_OUTPUT USE_SERIAL_1 + /* Serial buffer size * Maximum size of the serial buffer */ diff --git a/esp3d/src/core/commands.cpp b/esp3d/src/core/commands.cpp index 2043ea74..3feadafd 100644 --- a/esp3d/src/core/commands.cpp +++ b/esp3d/src/core/commands.cpp @@ -40,8 +40,10 @@ Commands::~Commands() //dispatch the command void Commands::process(uint8_t * sbuf, size_t len, ESP3DOutput * output, level_authenticate_type auth, ESP3DOutput * outputonly, uint8_t outputignore ) { + static bool lastIsESP3D = false; log_esp3d("Client is %d, has only %d, has ignore %d", output?output->client():0, outputonly?outputonly->client():0, outputignore); if(is_esp_command(sbuf,len)) { + lastIsESP3D = true; size_t slen = len; String tmpbuf = (const char*)sbuf; if (tmpbuf.startsWith("echo:")) { @@ -59,6 +61,12 @@ void Commands::process(uint8_t * sbuf, size_t len, ESP3DOutput * output, level_a log_esp3d("Respond to client %d",(outputonly == nullptr)?output->client():outputonly->client()); execute_internal_command (String((const char*)cmd).toInt(), (slen > (strlen((const char *)cmd)+5))?(const char*)&tmpbuf[strlen((const char *)cmd)+5]:"", auth, (outputonly == nullptr)?output:outputonly); } else { + //Work around to avoid to dispatch single \n to everyone as it is part of previous ESP3D command + if (lastIsESP3D && len==1 && sbuf[0]=='\n') { + lastIsESP3D = false; + return; + } + lastIsESP3D = false; //Dispatch to all clients but current or to define output #if defined(HTTP_FEATURE) //the web command will never get answer as answer go to websocket @@ -737,6 +745,11 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ case 900: response = ESP900(cmd_params, auth_type, output); break; + //Get / Set Serial Baud Rate + //[ESP901] json= pwd= + case 901: + response = ESP901(cmd_params, auth_type, output); + break; #endif //COMMUNICATION_PROTOCOL != SOCKET_SERIAL #ifdef BUZZER_DEVICE //Get state / Set Enable / Disable buzzer @@ -750,6 +763,18 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ //[ESP910]=[pwd=] response = ESP920(cmd_params, auth_type, output); break; +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + //Get state / Set Enable / Disable Serial Bridge Communication + //[ESP930] + case 930: + response = ESP930(cmd_params, auth_type, output); + break; + //Get / Set Serial Bridge Baud Rate + //[ESP931] json= pwd= + case 931: + response = ESP931(cmd_params, auth_type, output); + break; +#endif //defined(ESP_SERIAL_BRIDGE_OUTPUT) #if defined(ARDUINO_ARCH_ESP32) && (CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3) case 999: //Set quiet boot if strapping pin is High diff --git a/esp3d/src/core/commands.h b/esp3d/src/core/commands.h index 41d64bef..d12ee3e1 100644 --- a/esp3d/src/core/commands.h +++ b/esp3d/src/core/commands.h @@ -158,8 +158,13 @@ public: bool ESP800(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); #if COMMUNICATION_PROTOCOL != SOCKET_SERIAL bool ESP900(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); + bool ESP901(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); #endif //COMMUNICATION_PROTOCOL != SOCKET_SERIAL bool ESP920(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); +#if defined (ESP_SERIAL_BRIDGE_OUTPUT) + bool ESP930(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); + bool ESP931(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); +#endif //defined (ESP_SERIAL_BRIDGE_OUTPUT) #ifdef BUZZER_DEVICE bool ESP910(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); bool ESP250(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); diff --git a/esp3d/src/core/esp3d.cpp b/esp3d/src/core/esp3d.cpp index a3c874ca..336f14da 100644 --- a/esp3d/src/core/esp3d.cpp +++ b/esp3d/src/core/esp3d.cpp @@ -27,7 +27,7 @@ #include "esp3d.h" #include "../include/esp3d_config.h" #include "settings_esp3d.h" -#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL +#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || ESP_SERIAL_BRIDGE_OUTPUT #include "../modules/serial/serial_service.h" #endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL #if COMMUNICATION_PROTOCOL ==SOCKET_SERIAL @@ -109,11 +109,18 @@ bool Esp3D::begin() //BT do not start automaticaly so should be OK #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL //Serial service - if (!serial_service.begin()) { + if (!serial_service.begin(ESP_SERIAL_OUTPUT)) { log_esp3d("Error with serial service"); res = false; } #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL + //Serial bridge +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + if (!serial_bridge_service.begin(ESP_SERIAL_BRIDGE_OUTPUT)) { + log_esp3d("Error with serial bridge service"); + res = false; + } +#endif //ESP_SERIAL_BRIDGE_OUTPUT //Setup Filesystem #if defined(FILESYSTEM_FEATURE) if (!ESP_FileSystem::begin()) { @@ -160,6 +167,9 @@ void Esp3D::handle() #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL serial_service.handle(); #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + serial_bridge_service.handle(); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if COMMUNICATION_PROTOCOL ==SOCKET_SERIAL Serial2Socket.handle(); #endif //COMMUNICATION_PROTOCOL == SOCKET_SERIAL @@ -186,6 +196,9 @@ bool Esp3D::end() #if defined(CONNECTED_DEVICES_FEATURE) DevicesServices::end(); #endif //CONNECTED_DEVICES_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + serial_bridge_service.end(); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined(WIFI_FEATURE) || defined(ETH_FEATURE) NetConfig::end(); #endif //WIFI_FEATURE || ETH_FEATURE @@ -208,6 +221,12 @@ bool Esp3D::reset() log_esp3d("Reset serial error"); } #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + if (!serial_bridge_service.reset()) { + resetOk = false; + log_esp3d("Reset serial bridge error"); + } +#endif //ESP_SERIAL_BRIDGE_OUTPUT if (!Settings_ESP3D::reset()) { log_esp3d("Reset settings error"); resetOk = false; @@ -226,7 +245,7 @@ void Esp3D::restart_now() log_esp3d("Restarting"); #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL if (!serial_service.started()) { - serial_service.begin(); + serial_service.begin(ESP_SERIAL_OUTPUT); } serial_service.flush(); #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL diff --git a/esp3d/src/core/esp3doutput.cpp b/esp3d/src/core/esp3doutput.cpp index cee64161..d9189da0 100644 --- a/esp3d/src/core/esp3doutput.cpp +++ b/esp3d/src/core/esp3doutput.cpp @@ -1,5 +1,5 @@ /* - esp3Doutput.h - output functions class + esp3d output.h - output functions class Copyright (c) 2014 Luc Lebosse. All rights reserved. @@ -20,7 +20,7 @@ //#define ESP_DEBUG_FEATURE DEBUG_OUTPUT_SERIAL0 #include "../include/esp3d_config.h" #include "esp3doutput.h" -#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL +#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT) #include "../modules/serial/serial_service.h" #endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL #if COMMUNICATION_PROTOCOL == SOCKET_SERIAL @@ -61,6 +61,9 @@ uint8_t ESP3DOutput::_screenoutputflags = DEFAULT_SCREEN_FLAG; #if defined (BLUETOOTH_FEATURE) uint8_t ESP3DOutput::_BToutputflags = DEFAULT_BT_FLAG; #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) +uint8_t ESP3DOutput::_serialBridgeoutputflags = DEFAULT_SERIAL_BRIDGE_FLAG; +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (HTTP_FEATURE) #if defined (ARDUINO_ARCH_ESP32) #include @@ -77,6 +80,9 @@ const uint8_t activeClients [] = { #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL ESP_SERIAL_CLIENT, #endif // ESP_SERIAL_CLIENT +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + ESP_SERIAL_BRIDGE_CLIENT, +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) ESP_TELNET_CLIENT, #endif //TELNET_FEATURE @@ -199,6 +205,9 @@ bool ESP3DOutput::isOutput(uint8_t flag, bool fromsettings) #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL || COMMUNICATION_PROTOCOL == SOCKET_SERIAL _serialoutputflags= Settings_ESP3D::read_byte (ESP_SERIAL_FLAG); #endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + _serialBridgeoutputflags= Settings_ESP3D::read_byte (ESP_SERIAL_BRIDGE_FLAG); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY) _remotescreenoutputflags= Settings_ESP3D::read_byte (ESP_REMOTE_SCREEN_FLAG); #endif // defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY) @@ -222,6 +231,11 @@ bool ESP3DOutput::isOutput(uint8_t flag, bool fromsettings) return _serialoutputflags; #endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL return 0; + case ESP_SERIAL_BRIDGE_CLIENT: +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + return _serialBridgeoutputflags; +#endif //ESP_SERIAL_BRIDGE_OUTPUT + return 0; case ESP_REMOTE_SCREEN_CLIENT: #if defined(HAS_DISPLAY) || defined(HAS_SERIAL_DISPLAY) return _remotescreenoutputflags; @@ -299,7 +313,15 @@ size_t ESP3DOutput::dispatch (const uint8_t * sbuf, size_t len, uint8_t ignoreCl bt_service.write(sbuf, len); } } -#endif //BLUETOOTH_FEATURE +#endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + if (!(_client == ESP_SERIAL_BRIDGE_CLIENT || ESP_SERIAL_BRIDGE_CLIENT==ignoreClient)) { + if (isOutput(ESP_SERIAL_BRIDGE_CLIENT) && serial_bridge_service.started()) { + log_esp3d("Dispatch to serial bridge"); + serial_bridge_service.write(sbuf, len); + } + } +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) if (!(_client == ESP_TELNET_CLIENT || ESP_TELNET_CLIENT==ignoreClient)) { if (isOutput(ESP_TELNET_CLIENT) && telnet_server.started()) { @@ -346,6 +368,11 @@ void ESP3DOutput::flush() bt_service.flush(); break; #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_CLIENT: + serial_bridge_service.flush(); + break; +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) case ESP_TELNET_CLIENT: telnet_server.flush(); @@ -611,6 +638,10 @@ int ESP3DOutput::availableforwrite() case ESP_SERIAL_CLIENT: return serial_service.availableForWrite(); #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_CLIENT: + return serial_bridge_service.availableForWrite(); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (BLUETOOTH_FEATURE) case ESP_BT_CLIENT: return bt_service.availableForWrite(); @@ -653,10 +684,13 @@ size_t ESP3DOutput::write(uint8_t c) #endif //COMMUNICATION_PROTOCOL == SOCKET_SERIAL #if defined (BLUETOOTH_FEATURE) case ESP_BT_CLIENT: - if(bt_service.started()) { - return bt_service.write(c); - } + return bt_service.write(c); #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_CLIENT: + return serial_bridge_service.write(c); + break; +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) case ESP_TELNET_CLIENT: return telnet_server.write(c); @@ -667,21 +701,21 @@ size_t ESP3DOutput::write(uint8_t c) #endif //WS_DATA_FEATURE case ESP_ALL_CLIENTS: #if defined (BLUETOOTH_FEATURE) - if(bt_service.started()) { - bt_service.write(c); - } + bt_service.write(c); #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + serial_bridge_service.write(c); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) - if(telnet_server.started()) { - telnet_server.write(c); - } + telnet_server.write(c); #endif //TELNET_FEATURE #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL - return serial_service.write(c); + serial_service.write(c); #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL #if COMMUNICATION_PROTOCOL == SOCKET_SERIAL - return MYSERIAL1.write(c); + MYSERIAL1.write(c); #endif //COMMUNICATION_PROTOCOL == SOCKET_SERIAL + return 1; default : return 0; } @@ -716,24 +750,19 @@ size_t ESP3DOutput::write(const uint8_t *buffer, size_t size) #endif //DISPLAY_DEVICE #if defined (BLUETOOTH_FEATURE) case ESP_BT_CLIENT: - if(bt_service.started()) { - return bt_service.write(buffer, size); - } - break; + return bt_service.write(buffer, size); #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_CLIENT: + return serial_bridge_service.write(buffer, size); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) case ESP_TELNET_CLIENT: - if(telnet_server.started()) { - return telnet_server.write(buffer, size); - } - break; + return telnet_server.write(buffer, size); #endif //TELNET_FEATURE #if defined (WS_DATA_FEATURE) case ESP_WEBSOCKET_CLIENT: - if(websocket_data_server.started()) { - return websocket_data_server.write(buffer, size); - } - break; + return websocket_data_server.write(buffer, size); #endif //WS_DATA_FEATURE #if defined(GCODE_HOST_FEATURE) case ESP_STREAM_HOST_CLIENT: { @@ -770,21 +799,21 @@ size_t ESP3DOutput::write(const uint8_t *buffer, size_t size) #endif //COMMUNICATION_PROTOCOL == SOCKET_SERIAL case ESP_ALL_CLIENTS: #if defined (BLUETOOTH_FEATURE) - if(bt_service.started()) { - bt_service.write(buffer, size); - } + bt_service.write(buffer, size); #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + serial_bridge_service.write(buffer, size); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (TELNET_FEATURE) - if(telnet_server.started()) { - telnet_server.write(buffer, size); - } + telnet_server.write(buffer, size); #endif //TELNET_FEATURE #if COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL - return serial_service.write(buffer, size); + serial_service.write(buffer, size); #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL #if COMMUNICATION_PROTOCOL == SOCKET_SERIAL - return MYSERIAL1.write(buffer, size); + MYSERIAL1.write(buffer, size); #endif //COMMUNICATION_PROTOCOL == SOCKET_SERIAL + return size; default : break; } diff --git a/esp3d/src/core/esp3doutput.h b/esp3d/src/core/esp3doutput.h index c4c49579..5a8e3b4c 100644 --- a/esp3d/src/core/esp3doutput.h +++ b/esp3d/src/core/esp3doutput.h @@ -1,5 +1,5 @@ /* - esp3Doutput.h - output functions class + esp3doutput.h - output functions class Copyright (c) 2014 Luc Lebosse. All rights reserved. @@ -30,6 +30,7 @@ #define ESP_WEBSOCKET_CLIENT 128 #define ESP_SOCKET_SERIAL_CLIENT 129 #define ESP_ECHO_SERIAL_CLIENT 130 +#define ESP_SERIAL_BRIDGE_CLIENT 150 #define ESP_ALL_CLIENTS 255 #define ESP_STREAM_HOST_OUTPUT ESP_SERIAL_CLIENT @@ -132,6 +133,9 @@ private: #if defined (BLUETOOTH_FEATURE) static uint8_t _BToutputflags; #endif //BLUETOOTH_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + static uint8_t _serialBridgeoutputflags; +#endif //ESP_SERIAL_BRIDGE_OUTPUT }; #endif //_ESP3DOUTPUT_H diff --git a/esp3d/src/core/espcmd/ESP0.cpp b/esp3d/src/core/espcmd/ESP0.cpp index 3e2d80d9..9106d7b0 100644 --- a/esp3d/src/core/espcmd/ESP0.cpp +++ b/esp3d/src/core/espcmd/ESP0.cpp @@ -150,11 +150,16 @@ const char * help[]= {"[ESP] (id) - display this help", "[ESP800](plain)(time=YYYY-MM-DD-HH-MM-SS) - display FW Informations in plain/JSON", #if COMMUNICATION_PROTOCOL != SOCKET_SERIAL "[ESP900](ENABLE/DISABLE) - display/set serial state", + "[ESP901] - display/set serial baud rate", #endif //COMMUNICATION_PROTOCOL != SOCKET_SERIAL #ifdef BUZZER_DEVICE "[ESP910](ENABLE/DISABLE) - display/set buzzer state", #endif //BUZZER_DEVICE "[ESP920](client)=(ON/OFF) - display/set SERIAL / SCREEN / REMOTE_SCREEN / WEBSOCKET / TELNET /BT / ALL client state if available", +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + "[ESP930](ON/OFF/CLOSE) - display/set serial bridge state", + "[ESP931] - display/set serial bridge baud rate", +#endif //defined(ESP_SERIAL_BRIDGE_OUTPUT) #if defined(ARDUINO_ARCH_ESP32) && (CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3) "[ESP999](QUIETBOOT) [pwd=] - set quiet boot mode", #endif //ARDUINO_ARCH_ESP32 @@ -284,12 +289,17 @@ const uint cmdlist[]= {0, 800, #if COMMUNICATION_PROTOCOL != SOCKET_SERIAL 900, + 901, #endif //COMMUNICATION_PROTOCOL != SOCKET_SERIAL #ifdef BUZZER_DEVICE 910, #endif //BUZZER_DEVICE 920, +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + 930, + 935, +#endif //defined(ESP_SERIAL_BRIDGE_OUTPUT) #if defined(ARDUINO_ARCH_ESP32) && (CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3) 999, #endif //ARDUINO_ARCH_ESP32 && CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 diff --git a/esp3d/src/core/espcmd/ESP220.cpp b/esp3d/src/core/espcmd/ESP220.cpp index 7d72f370..431d85c3 100644 --- a/esp3d/src/core/espcmd/ESP220.cpp +++ b/esp3d/src/core/espcmd/ESP220.cpp @@ -197,7 +197,7 @@ bool Commands::ESP220(const char* cmd_params, level_authenticate_type auth_type, } line=""; #endif //BUZZER_DEVICE -#ifdef PIN_RESET_FEATURE +#if defined(PIN_RESET_FEATURE) && defined(ESP3D_RESET_PIN) && ESP3D_RESET_PIN !=-1 hasPin = true; if (json) { line += "{\"id\":\""; diff --git a/esp3d/src/core/espcmd/ESP400.cpp b/esp3d/src/core/espcmd/ESP400.cpp index ba3f9a7f..c26f46c4 100644 --- a/esp3d/src/core/espcmd/ESP400.cpp +++ b/esp3d/src/core/espcmd/ESP400.cpp @@ -37,6 +37,8 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, bool json = has_tag (cmd_params, "json"); String response; String parameter; + uint8_t count = 0; + const long *bl = NULL; int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead #ifdef AUTHENTICATION_FEATURE if (auth_type != LEVEL_ADMIN) { @@ -368,6 +370,30 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, output->print ("\"}"); #endif //FTP_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + //Serial bridge On service + output->print (",{\"F\":\"service/serial_bridge\",\"P\":\""); + output->print (ESP_SERIAL_BRIDGE_ON); + output->print ("\",\"T\":\"B\",\"V\":\""); + output->print (Settings_ESP3D::read_byte(ESP_SERIAL_BRIDGE_ON)); + output->print ("\",\"H\":\"enable\",\"O\":[{\"no\":\"0\"},{\"yes\":\"1\"}]}"); + //Baud Rate + output->print (",{\"F\":\"service/serial_bridge\",\"P\":\""); + output->print (ESP_SERIAL_BRIDGE_BAUD); + output->print ("\",\"T\":\"I\",\"V\":\""); + output->print (Settings_ESP3D::read_uint32(ESP_SERIAL_BRIDGE_BAUD)); + output->print ("\",\"H\":\"baud\",\"O\":["); + + bl = serial_service.get_baudratelist(&count); + for (uint8_t i = 0 ; i < count ; i++) { + if (i > 0) { + output->print (","); + } + output->printf("{\"%ld\":\"%ld\"}", bl[i], bl[i]); + } + output->print ("]}"); +#endif //defined(ESP_SERIAL_BRIDGE_OUTPUT) + #ifdef TIMESTAMP_FEATURE //Internet Time @@ -565,8 +591,8 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, output->print ("\",\"T\":\"I\",\"V\":\""); output->print (Settings_ESP3D::read_uint32(ESP_BAUD_RATE)); output->print ("\",\"H\":\"baud\",\"O\":["); - uint8_t count = 0; - const long *bl = serial_service.get_baudratelist(&count); + + bl = serial_service.get_baudratelist(&count); for (uint8_t i = 0 ; i < count ; i++) { if (i > 0) { output->print (","); @@ -600,6 +626,13 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, output->print (Settings_ESP3D::read_byte(ESP_SERIAL_FLAG)); output->print ("\",\"H\":\"serial\",\"O\":[{\"no\":\"0\"},{\"yes\":\"1\"}]}"); #endif //COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + output->print (",{\"F\":\"system/outputmsg\",\"P\":\""); + output->print (ESP_SERIAL_BRIDGE_FLAG); + output->print ("\",\"T\":\"B\",\"V\":\""); + output->print (Settings_ESP3D::read_byte(ESP_SERIAL_BRIDGE_FLAG)); + output->print ("\",\"H\":\"serial_bridge\",\"O\":[{\"no\":\"0\"},{\"yes\":\"1\"}]}"); +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if (defined(ESP3DLIB_ENV) && defined(HAS_DISPLAY)) || defined(HAS_SERIAL_DISPLAY) //Printer SCREEN output->print (",{\"F\":\"system/outputmsg\",\"P\":\""); @@ -640,7 +673,6 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, output->print (Settings_ESP3D::read_byte(ESP_TELNET_FLAG)); output->print ("\",\"H\":\"telnet\",\"O\":[{\"no\":\"0\"},{\"yes\":\"1\"}]}"); #endif //TELNET_FEATURE - output->print ("]}"); if (!json) { output->printLN(""); diff --git a/esp3d/src/core/espcmd/ESP401.cpp b/esp3d/src/core/espcmd/ESP401.cpp index ed97393e..e58055bd 100644 --- a/esp3d/src/core/espcmd/ESP401.cpp +++ b/esp3d/src/core/espcmd/ESP401.cpp @@ -23,7 +23,7 @@ #include "../settings_esp3d.h" #include "../../modules/authentication/authentication_service.h" #define COMMANDID 401 -#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL +#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT) #include "../../modules/serial/serial_service.h" #endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL #ifdef SENSOR_DEVICE @@ -90,6 +90,14 @@ bool Commands::ESP401(const char* cmd_params, level_authenticate_type auth_type, } else { //dynamique refresh is better than restart the boards switch(spos.toInt()) { +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_ON: + if (!serial_bridge_service.started()) { + serial_bridge_service.begin(ESP_SERIAL_BRIDGE_OUTPUT); + } + break; +#endif //ESP_SERIAL_BRIDGE_OUTPUT + case ESP_SERIAL_BRIDGE_FLAG: case ESP_SERIAL_FLAG: case ESP_REMOTE_SCREEN_FLAG: case ESP_WEBSOCKET_FLAG: @@ -166,6 +174,11 @@ bool Commands::ESP401(const char* cmd_params, level_authenticate_type auth_type, serial_service.updateBaudRate(sval.toInt()); break; #endif // COMMUNICATION_PROTOCOL == RAW_SERIAL || COMMUNICATION_PROTOCOL == MKS_SERIAL +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_BAUD: + serial_bridge_service.updateBaudRate(sval.toInt()); + break; +#endif //defined(ESP_SERIAL_BRIDGE_OUTPUT) default: break; } diff --git a/esp3d/src/core/espcmd/ESP420.cpp b/esp3d/src/core/espcmd/ESP420.cpp index 201f8771..de0de314 100644 --- a/esp3d/src/core/espcmd/ESP420.cpp +++ b/esp3d/src/core/espcmd/ESP420.cpp @@ -22,7 +22,7 @@ #include "../esp3doutput.h" #include "../settings_esp3d.h" #include "../../modules/authentication/authentication_service.h" -#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL +#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT) #include "../../modules/serial/serial_service.h" #endif // COMMUNICATION_PROTOCOL != SOCKET_SERIAL #ifdef FILESYSTEM_FEATURE @@ -1354,6 +1354,47 @@ bool Commands::ESP420(const char* cmd_params, level_authenticate_type auth_type, } line=""; #endif //AUTHENTICATION_FEATURE +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + if (json) { + line +=",{\"id\":\""; + } + line +="serial_bridge"; + if (json) { + line +="\",\"value\":\""; + } else { + line +=": "; + } + if(serial_bridge_service.started()) { + line+="ON"; + } else { + line+="OFF"; + } + if (json) { + line +="\"}"; + output->print (line.c_str()); + } else { + output->printMSGLine(line.c_str()); + } + line=""; + if (json) { + line +=",{\"id\":\""; + } + line +="baud"; + if (json) { + line +="\",\"value\":\""; + } else { + line +=": "; + } + line+=serial_bridge_service.baudRate(); + if (json) { + line +="\"}"; + output->print (line.c_str()); + } else { + output->printMSGLine(line.c_str()); + } + line=""; + +#endif //ESP_SERIAL_BRIDGE_OUTPUT #if defined (HAS_SERIAL_DISPLAY) if (json) { line +=",{\"id\":\""; diff --git a/esp3d/src/core/espcmd/ESP900.cpp b/esp3d/src/core/espcmd/ESP900.cpp index ea801552..96a0132f 100644 --- a/esp3d/src/core/espcmd/ESP900.cpp +++ b/esp3d/src/core/espcmd/ESP900.cpp @@ -46,15 +46,18 @@ bool Commands::ESP900(const char* cmd_params, level_authenticate_type auth_type, if (noError) { parameter = clean_param(get_param (cmd_params, "")); //get + String r; if (parameter.length() == 0) { if (serial_service.started()) { - response = format_response(COMMANDID, json, true, "ENABLED"); + r="ENABLED"; } else { - response = format_response(COMMANDID, json, true, "DISABLED"); + r="DISABLED"; } + r+=" - Serial" + String(serial_service.serialIndex()); + response = format_response(COMMANDID, json, true, r.c_str()); } else { //set if (parameter == "ENABLE" ) { - if (serial_service.begin()) { + if (serial_service.begin(ESP_SERIAL_OUTPUT)) { response = format_response(COMMANDID, json, true, "ok"); } else { response = format_response(COMMANDID, json, false, "Cannot enable serial communication"); diff --git a/esp3d/src/core/espcmd/ESP901.cpp b/esp3d/src/core/espcmd/ESP901.cpp new file mode 100644 index 00000000..0bb613e8 --- /dev/null +++ b/esp3d/src/core/espcmd/ESP901.cpp @@ -0,0 +1,87 @@ +/* + ESP901.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This code 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 code 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 code; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "../../include/esp3d_config.h" +#if COMMUNICATION_PROTOCOL != SOCKET_SERIAL +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/serial/serial_service.h" +#define COMMANDID 901 +//Set Serial baudrate +//[ESP901] json= pwd= +bool Commands::ESP901(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool noError = true; + bool json = has_tag (cmd_params, "json"); + String response; + String parameter; + int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + response = format_response(COMMANDID, json, false, "Guest user can't use this command"); + noError = false; + errorCode = 401; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + if (noError) { + parameter = clean_param(get_param (cmd_params, "")); + //get + if (parameter.length() == 0) { + response = format_response(COMMANDID, json, true,String(Settings_ESP3D::read_uint32(ESP_BAUD_RATE)).c_str()); + } else { //set +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + response = format_response(COMMANDID, json, false, "Wrong authentication level"); + noError = false; + errorCode = 401; + } +#endif //AUTHENTICATION_FEATURE + if (noError) { + uint ibuf = parameter.toInt(); + if (serial_service.is_valid_baudrate(ibuf)) { + response = format_response(COMMANDID, json, false, "Incorrect port"); + noError = false; + } else { + if (!Settings_ESP3D::write_uint32 (ESP_BAUD_RATE, ibuf)) { + response = format_response(COMMANDID, json, false, "Set failed"); + noError = false; + } else { + response = format_response(COMMANDID, json, true, "ok"); + } + } + } + } + } + if (noError) { + if (json) { + output->printLN (response.c_str() ); + } else { + output->printMSG (response.c_str() ); + } + } else { + output->printERROR(response.c_str(), errorCode); + } + return noError; +} + +#endif //COMMUNICATION_PROTOCOL != SOCKET_SERIAL diff --git a/esp3d/src/core/espcmd/ESP930.cpp b/esp3d/src/core/espcmd/ESP930.cpp new file mode 100644 index 00000000..4b0275c2 --- /dev/null +++ b/esp3d/src/core/espcmd/ESP930.cpp @@ -0,0 +1,97 @@ +/* + ESP930.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This code 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 code 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 code; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "../../include/esp3d_config.h" +#if defined (ESP_SERIAL_BRIDGE_OUTPUT) +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/serial/serial_service.h" +#define COMMANDID 930 +//Set Bridge Serial state which can be ON, OFF, CLOSE +//[ESP930] json= pwd= +bool Commands::ESP930(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool noError = true; + bool json = has_tag (cmd_params, "json"); + String response; + String parameter; + int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + response = format_response(COMMANDID, json, false, "Guest user can't use this command"); + noError = false; + errorCode = 401; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + if (noError) { + parameter = clean_param(get_param (cmd_params, "")); + //get + if (parameter.length() == 0) { + String r = (Settings_ESP3D::read_byte(ESP_SERIAL_BRIDGE_ON) == 0)?"OFF":"ON"; + r+=" - Serial" + String(serial_bridge_service.serialIndex()); + response = format_response(COMMANDID, json, true, r.c_str() ); + } else { //set +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + response = format_response(COMMANDID, json, false, "Wrong authentication level"); + noError = false; + errorCode = 401; + } +#endif //AUTHENTICATION_FEATURE + if (noError) { + parameter.toUpperCase(); + if (!((parameter == "ON") || (parameter == "OFF") || (parameter == "CLOSE"))) { + response = format_response(COMMANDID, json, false, "Only ON or OFF or CLOSE mode supported!"); + noError = false; + } else { + if (parameter == "CLOSE") { + serial_bridge_service.end(); + } else { + if (!Settings_ESP3D::write_byte (ESP_SERIAL_BRIDGE_ON, (parameter == "ON")?1:0)) { + response = format_response(COMMANDID, json, false, "Set failed"); + noError = false; + } + if(noError && parameter == "ON" && !serial_bridge_service.started()) { + serial_bridge_service.begin(ESP_SERIAL_BRIDGE_OUTPUT); + } + } + if (noError) { + response = format_response(COMMANDID, json, true, "ok"); + } + } + } + } + } + if (noError) { + if (json) { + output->printLN (response.c_str() ); + } else { + output->printMSG (response.c_str() ); + } + } else { + output->printERROR(response.c_str(), errorCode); + } + return noError; +} + +#endif //TELNET_FEATURE diff --git a/esp3d/src/core/espcmd/ESP931.cpp b/esp3d/src/core/espcmd/ESP931.cpp new file mode 100644 index 00000000..27991c07 --- /dev/null +++ b/esp3d/src/core/espcmd/ESP931.cpp @@ -0,0 +1,87 @@ +/* + ESP931.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This code 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 code 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 code; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "../../include/esp3d_config.h" +#if defined (ESP_SERIAL_BRIDGE_OUTPUT) +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/serial/serial_service.h" +#define COMMANDID 931 +//Set Serial bridge baudrate +//[ESP931] json= pwd= +bool Commands::ESP931(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool noError = true; + bool json = has_tag (cmd_params, "json"); + String response; + String parameter; + int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + response = format_response(COMMANDID, json, false, "Guest user can't use this command"); + noError = false; + errorCode = 401; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + if (noError) { + parameter = clean_param(get_param (cmd_params, "")); + //get + if (parameter.length() == 0) { + response = format_response(COMMANDID, json, true,String(Settings_ESP3D::read_uint32(ESP_SERIAL_BRIDGE_BAUD)).c_str()); + } else { //set +#ifdef AUTHENTICATION_FEATURE + if (auth_type != LEVEL_ADMIN) { + response = format_response(COMMANDID, json, false, "Wrong authentication level"); + noError = false; + errorCode = 401; + } +#endif //AUTHENTICATION_FEATURE + if (noError) { + uint ibuf = parameter.toInt(); + if (serial_bridge_service.is_valid_baudrate(ibuf)) { + response = format_response(COMMANDID, json, false, "Incorrect port"); + noError = false; + } else { + if (!Settings_ESP3D::write_uint32 (ESP_SERIAL_BRIDGE_BAUD, ibuf)) { + response = format_response(COMMANDID, json, false, "Set failed"); + noError = false; + } else { + response = format_response(COMMANDID, json, true, "ok"); + } + } + } + } + } + if (noError) { + if (json) { + output->printLN (response.c_str() ); + } else { + output->printMSG (response.c_str() ); + } + } else { + output->printERROR(response.c_str(), errorCode); + } + return noError; +} + +#endif //TELNET_FEATURE diff --git a/esp3d/src/core/settings_esp3d.cpp b/esp3d/src/core/settings_esp3d.cpp index 90c504f6..64de36ea 100644 --- a/esp3d/src/core/settings_esp3d.cpp +++ b/esp3d/src/core/settings_esp3d.cpp @@ -114,6 +114,7 @@ #define DEFAULT_SENSOR_TYPE NO_SENSOR_DEVICE #define DEFAULT_HTTP_ON 1 #define DEFAULT_FTP_ON 1 +#define DEFAULT_SERIAL_BRIDGE_ON 1 #define DEFAULT_WEBDAV_ON 1 #define DEFAULT_TELNET_ON 1 #define DEFAULT_WEBSOCKET_ON 1 @@ -128,6 +129,7 @@ //default int values #define DEFAULT_ESP_INT 0L #define DEFAULT_BAUD_RATE 115200L +#define DEFAULT_SERIAL_BRIDGE_BAUD_RATE 115200L #define DEFAULT_HTTP_PORT 80L #define DEFAULT_FTP_CTRL_PORT 21L #define DEFAULT_FTP_ACTIVE_PORT 20L @@ -319,6 +321,14 @@ uint8_t Settings_ESP3D::get_default_byte_value(int pos) case ESP_BT_FLAG: res = DEFAULT_BT_FLAG; break; +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case ESP_SERIAL_BRIDGE_FLAG: + res = DEFAULT_SERIAL_BRIDGE_FLAG; + break; + case ESP_SERIAL_BRIDGE_ON: + res = DEFAULT_SERIAL_BRIDGE_ON; + break; +#endif //defined() case ESP_SCREEN_FLAG: res = DEFAULT_SCREEN_FLAG; break; @@ -394,6 +404,11 @@ uint32_t Settings_ESP3D::get_default_int32_value(int pos) case ESP_BAUD_RATE: res = DEFAULT_BAUD_RATE; break; + case ESP_SERIAL_BRIDGE_BAUD: +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + res = DEFAULT_SERIAL_BRIDGE_BAUD_RATE; + break; +#endif //ESP_SERIAL_BRIDGE_OUT;UT case ESP_BOOT_DELAY: res = DEFAULT_BOOT_DELAY; break; @@ -1217,6 +1232,7 @@ bool Settings_ESP3D::reset(bool networkonly) Settings_ESP3D::write_byte(ESP_TARGET_FW,Settings_ESP3D::get_default_byte_value(ESP_TARGET_FW)); //Output flags Settings_ESP3D::write_byte(ESP_SERIAL_FLAG,Settings_ESP3D::get_default_byte_value(ESP_SERIAL_FLAG)); + Settings_ESP3D::write_byte(ESP_SERIAL_BRIDGE_FLAG,Settings_ESP3D::get_default_byte_value(ESP_SERIAL_BRIDGE_FLAG)); Settings_ESP3D::write_byte(ESP_REMOTE_SCREEN_FLAG,Settings_ESP3D::get_default_byte_value(ESP_REMOTE_SCREEN_FLAG)); Settings_ESP3D::write_byte(ESP_WEBSOCKET_FLAG,Settings_ESP3D::get_default_byte_value(ESP_WEBSOCKET_FLAG)); Settings_ESP3D::write_byte(ESP_TELNET_FLAG,Settings_ESP3D::get_default_byte_value(ESP_TELNET_FLAG)); diff --git a/esp3d/src/include/defines.h b/esp3d/src/include/defines.h index 72b6aac5..5662e068 100644 --- a/esp3d/src/include/defines.h +++ b/esp3d/src/include/defines.h @@ -32,8 +32,8 @@ #define MARLIN_EMBEDDED 30 #define SMOOTHIEWARE 40 #define REPETIER 50 -#define FLUIDNC 60 #define REPRAP 70 +#define GRBLHAL 80 //Default flags #define DEFAULT_SERIAL_OUTPUT_FLAG 1 @@ -42,6 +42,7 @@ #define DEFAULT_TELNET_FLAG 1 #define DEFAULT_BT_FLAG 1 #define DEFAULT_SCREEN_FLAG 1 +#define DEFAULT_SERIAL_BRIDGE_FLAG 1 //position in EEPROM / preferences will use `P_` + to make a string : P_0 for 0 #define ESP_RADIO_MODE 0 //1 byte = flag @@ -110,6 +111,9 @@ #define ESP_SECURE_SERIAL 1033 //1 byte = flag #define ESP_BOOT_RADIO_STATE 1034 //1 byte = flag #define ESP_STA_FALLBACK_MODE 1035 //1 byte = flag +#define ESP_SERIAL_BRIDGE_ON 1036 //1 byte = flag +#define ESP_SERIAL_BRIDGE_FLAG 1037 //1 byte = flag +#define ESP_SERIAL_BRIDGE_BAUD 1038 //4 bytes= int //Hidden password #define HIDDEN_PASSWORD "********" @@ -127,6 +131,11 @@ #define USE_SERIAL_1 2 #define USE_SERIAL_2 3 +//Serial service ID +#define MAIN_SERIAL 1 +#define BRIDGE_SERIAL 2 + + //Communication protocols #define RAW_SERIAL 0 #define MKS_SERIAL 1 diff --git a/esp3d/src/include/esp3d_config.h b/esp3d/src/include/esp3d_config.h index 18ed0753..30db6e6d 100644 --- a/esp3d/src/include/esp3d_config.h +++ b/esp3d/src/include/esp3d_config.h @@ -61,7 +61,7 @@ * **********************************/ //Make Flag more generic -#if defined(PIN_RESET_FEATURE) || defined(SD_RECOVERY_FEATURE) +#if (defined(PIN_RESET_FEATURE) && defined(ESP3D_RESET_PIN) && ESP3D_RESET_PIN!=-1) || defined(SD_RECOVERY_FEATURE) #define RECOVERY_FEATURE #endif //PIN_RESET_FEATURE || SD_RECOVERY_FEATURE diff --git a/esp3d/src/include/pins.h b/esp3d/src/include/pins.h index 039f254c..03b5af03 100644 --- a/esp3d/src/include/pins.h +++ b/esp3d/src/include/pins.h @@ -24,17 +24,26 @@ // * UART 1 allows only TX on 2 if UART 0 is not (2, 3) #ifndef ESP_RX_PIN #define ESP_RX_PIN -1 -#endif //~ESP_RX_PIN +#endif //ESP_RX_PIN #ifndef ESP_TX_PIN #define ESP_TX_PIN -1 -#endif //~ESP_TX_PIN +#endif //ESP_TX_PIN + +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) +#ifndef ESP_BRIDGE_RX_PIN +#define ESP_BRIDGE_RX_PIN -1 +#endif //ESP_BRIDGE_RX_PIN +#ifndef ESP_BRIDGE_TX_PIN +#define ESP_BRIDGE_TX_PIN -1 +#endif //ESP_BRIDGE_TX_PIN +#endif //ESP_SERIAL_BRIDGE_OUTPUT #ifndef ESP_DEBUG_RX_PIN #define ESP_DEBUG_RX_PIN -1 -#endif //~ESP_DEBUG_RX_PIN +#endif //ESP_DEBUG_RX_PIN #ifndef ESP_DEBUG_TX_PIN #define ESP_DEBUG_TX_PIN -1 -#endif //~ESP_DEBUG_TX_PIN +#endif //ESP_DEBUG_TX_PIN //I2C Pins #ifndef ESP_SDA_PIN @@ -397,7 +406,7 @@ #endif //ESP_SD_DETECT_PIN #if defined (PIN_RESET_FEATURE) && !defined(ESP3D_RESET_PIN) -#define ESP3D_RESET_PIN 0 +#define ESP3D_RESET_PIN -1 #endif //PIN_RESET_FEATURE #ifdef SD_DEVICE_CONNECTION diff --git a/esp3d/src/include/sanity_esp3d.h b/esp3d/src/include/sanity_esp3d.h index a5288c13..5acb0321 100644 --- a/esp3d/src/include/sanity_esp3d.h +++ b/esp3d/src/include/sanity_esp3d.h @@ -53,7 +53,12 @@ #if !defined(ESP_SERIAL_OUTPUT) && COMMUNICATION_PROTOCOL!=SOCKET_SERIAL #error ESP_SERIAL_OUTPUT must be defined -#endif +#endif //!defined(ESP_SERIAL_OUTPUT) && COMMUNICATION_PROTOCOL!=SOCKET_SERIAL + +#if COMMUNICATION_PROTOCOL!=SOCKET_SERIAL && defined(ESP_SERIAL_BRIDGE_OUTPUT) && ESP_SERIAL_OUTPUT==ESP_SERIAL_BRIDGE_OUTPUT +#error ESP_SERIAL_OUTPUT cannot be same as ESP_SERIAL_BRIDGE_OUTPUT +#endif //!defined(ESP_SERIAL_OUTPUT) && COMMUNICATION_PROTOCOL!=SOCKET_SERIAL + #if (ESP_SERIAL_OUTPUT == USE_SERIAL2) && defined( ARDUINO_ARCH_ESP8266) #error Serial 2 is not available in ESP8266 @@ -106,7 +111,7 @@ #endif #endif -#if defined (SD_DEVICE_CONNECTION) && defined(PIN_RESET_FEATURE) +#if defined (SD_DEVICE_CONNECTION) && defined(PIN_RESET_FEATURE) && ESP3D_RESET_PIN!=-1 #if SD_DEVICE_CONNECTION == ESP_SHARED_SD && ESP_FLAG_SHARED_SD_PIN == ESP3D_RESET_PIN #error ESP_FLAG_SHARED_SD_PIN and ESP3D_RESET_PIN are same, it is not allowed. #endif diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index 94cb941b..20cc0a22 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a205" +#define FW_VERSION "3.0.0.a206" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/bluetooth/BT_service.cpp b/esp3d/src/modules/bluetooth/BT_service.cpp index 0197e44d..81efb522 100644 --- a/esp3d/src/modules/bluetooth/BT_service.cpp +++ b/esp3d/src/modules/bluetooth/BT_service.cpp @@ -48,6 +48,7 @@ String BTService::_btclient = ""; BTService::BTService() { _buffer_size = 0; + _started=false; } BTService::~BTService() @@ -150,6 +151,7 @@ void BTService::end() flush(); SerialBT.end(); _buffer_size = 0; + _started=false; } /** @@ -166,7 +168,8 @@ bool BTService::reset() */ bool BTService::started() { - return btStarted(); + _started =btStarted(); + return _started; } /** diff --git a/esp3d/src/modules/bluetooth/BT_service.h b/esp3d/src/modules/bluetooth/BT_service.h index 2771f197..a51da710 100644 --- a/esp3d/src/modules/bluetooth/BT_service.h +++ b/esp3d/src/modules/bluetooth/BT_service.h @@ -75,6 +75,7 @@ private : size_t _buffer_size; void push2buffer(uint8_t * sbuf, size_t len); void flushbuffer(); + bool _started; }; extern BTService bt_service; diff --git a/esp3d/src/modules/recovery/recovery_service.cpp b/esp3d/src/modules/recovery/recovery_service.cpp index f7bd703d..5d1a8e3e 100644 --- a/esp3d/src/modules/recovery/recovery_service.cpp +++ b/esp3d/src/modules/recovery/recovery_service.cpp @@ -25,7 +25,7 @@ #include "../../core/esp3doutput.h" RecoveryService recovery_service; -#ifdef PIN_RESET_FEATURE +#if defined(PIN_RESET_FEATURE)&& defined(ESP3D_RESET_PIN) && ESP3D_RESET_PIN !=-1 #include "../../core/esp3d.h" bool interruptswitch =false; diff --git a/esp3d/src/modules/serial/serial_service.cpp b/esp3d/src/modules/serial/serial_service.cpp index 89f42a93..4b6ee842 100644 --- a/esp3d/src/modules/serial/serial_service.cpp +++ b/esp3d/src/modules/serial/serial_service.cpp @@ -19,7 +19,7 @@ */ #include "../../include/esp3d_config.h" -#if COMMUNICATION_PROTOCOL == MKS_SERIAL || COMMUNICATION_PROTOCOL == RAW_SERIAL +#if COMMUNICATION_PROTOCOL == MKS_SERIAL || COMMUNICATION_PROTOCOL == RAW_SERIAL || defined(ESP_SERIAL_BRIDGE_OUTPUT) #include "serial_service.h" #include "../../core/settings_esp3d.h" #include "../../core/esp3doutput.h" @@ -28,27 +28,29 @@ #include "../mks/mks_service.h" #endif //COMMUNICATION_PROTOCOL == MKS_SERIAL #include "../authentication/authentication_service.h" +#if defined (ARDUINO_ARCH_ESP8266) +#define MAX_SERIAL 2 +HardwareSerial * Serials[MAX_SERIAL] = {&Serial, &Serial1}; +#endif //ARDUINO_ARCH_ESP8266 + +#if defined (ARDUINO_ARCH_ESP32) +#define MAX_SERIAL 3 +HardwareSerial * Serials[MAX_SERIAL] = {&Serial, &Serial1, &Serial2}; +#endif //ARDUINO_ARCH_ESP32 + //Serial Parameters #define ESP_SERIAL_PARAM SERIAL_8N1 -#if ESP_SERIAL_OUTPUT == USE_SERIAL_0 -#define ESP3D_SERIAL Serial -#endif //USE_SERIAL_0 - -#if ESP_SERIAL_OUTPUT == USE_SERIAL_1 -#define ESP3D_SERIAL Serial1 -#endif //USE_SERIAL_1 - -#if ESP_SERIAL_OUTPUT == USE_SERIAL_2 -#define ESP3D_SERIAL Serial2 -#endif //USE_SERIAL_2 - #define ESP3DSERIAL_RUNNING_PRIORITY 1 #define ESP3DSERIAL_RUNNING_CORE 1 #define SERIAL_YIELD 10 -SerialService serial_service; +SerialService serial_service = SerialService(MAIN_SERIAL); +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) +SerialService serial_bridge_service = SerialService(BRIDGE_SERIAL); +#endif //ESP_SERIAL_BRIDGE_OUTPUT + #if defined(ARDUINO_ARCH_ESP32) && defined(SERIAL_INDEPENDANT_TASK) TaskHandle_t _hserialtask= nullptr; #endif //ARDUINO_ARCH_ESP32 @@ -57,11 +59,31 @@ const long SupportedBaudList[] = {9600, 19200, 38400, 57600, 74880, 115200, 2304 #define TIMEOUT_SERIAL_FLUSH 1500 //Constructor -SerialService::SerialService() +SerialService::SerialService(uint8_t id) { _buffer_size = 0; _started = false; _needauthentication = true; + _id = id; + switch (_id) { + case MAIN_SERIAL: + _rxPin = ESP_RX_PIN; + _txPin = ESP_TX_PIN; + _client=ESP_SERIAL_CLIENT; + break; +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case BRIDGE_SERIAL: + _rxPin = ESP_BRIDGE_RX_PIN; + _txPin = ESP_BRIDGE_TX_PIN; + _client=ESP_SERIAL_BRIDGE_CLIENT; + break; +#endif //ESP_SERIAL_BRIDGE_OUTPUT + default: + _rxPin = ESP_RX_PIN; + _txPin = ESP_TX_PIN; + _client=ESP_SERIAL_CLIENT; + break; + } } //Destructor @@ -93,30 +115,59 @@ void SerialService::setParameters() } //Setup Serial -bool SerialService::begin() +bool SerialService::begin(uint8_t serialIndex) { + _serialIndex = serialIndex-1; + log_esp3d("Serial %d begin for %d", _serialIndex, _id); + if (_id== BRIDGE_SERIAL && Settings_ESP3D::read_byte(ESP_SERIAL_BRIDGE_ON)==0) { + log_esp3d("Serial %d for %d is disabled", _serialIndex, _id); + return true; + } + if(_serialIndex >= MAX_SERIAL) { + log_esp3d("Serial %d begin for %d failed, index out of range", _serialIndex, _id); + return false; + } _lastflush = millis(); //read from settings - long br = Settings_ESP3D::read_uint32(ESP_BAUD_RATE); + long br = 0; + long defaultBr = 0; + switch (_id) { + case MAIN_SERIAL: + br = Settings_ESP3D::read_uint32(ESP_BAUD_RATE); + defaultBr = Settings_ESP3D::get_default_int32_value(ESP_BAUD_RATE); + break; +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case BRIDGE_SERIAL: + br = Settings_ESP3D::read_uint32(ESP_SERIAL_BRIDGE_BAUD); + defaultBr = Settings_ESP3D::get_default_int32_value(ESP_SERIAL_BRIDGE_BAUD); + break; +#endif //ESP_SERIAL_BRIDGE_OUTPUT + default: + log_esp3d("Serial %d begin for %d failed, unknown id", _serialIndex, _id); + return false; + } setParameters(); + log_esp3d("Baud rate is %d , default is %d",br, defaultBr); _buffer_size = 0; //change only if different from current - if (br != baudRate() || (ESP_RX_PIN != -1) || (ESP_TX_PIN != -1)) { + if (br != baudRate() || (_rxPin != -1) || (_txPin != -1)) { if ( !is_valid_baudrate(br)) { - br = Settings_ESP3D::get_default_int32_value(ESP_BAUD_RATE); + br = defaultBr; } - ESP3D_SERIAL.setRxBufferSize (SERIAL_RX_BUFFER_SIZE); + Serials[_serialIndex]->setRxBufferSize (SERIAL_RX_BUFFER_SIZE); #ifdef ARDUINO_ARCH_ESP8266 - ESP3D_SERIAL.begin(br, ESP_SERIAL_PARAM, SERIAL_FULL, (ESP_TX_PIN == -1)?1:ESP_TX_PIN); -#if ESP_RX_PIN != -1 - ESP3D_SERIAL.pins((ESP_TX_PIN == -1)?1:ESP_TX_PIN, ESP_RX_PIN) -#endif //ESP_RX_PIN != -1 + Serials[_serialIndex]->begin(br, ESP_SERIAL_PARAM, SERIAL_FULL, (_txPin == -1)?1:_txPin); + if (_rx != -1) { + Serials[_serialIndex]->pins((_txPin == -1)?1:_txPin, _rxPin) + } + #endif //ARDUINO_ARCH_ESP8266 #if defined(ARDUINO_ARCH_ESP32) - ESP3D_SERIAL.begin (br, ESP_SERIAL_PARAM, ESP_RX_PIN, ESP_TX_PIN); + Serials[_serialIndex]->begin (br, ESP_SERIAL_PARAM, ESP_RX_PIN, ESP_TX_PIN); #if defined(SERIAL_INDEPENDANT_TASK) //create serial task once - if (_hserialtask == nullptr) { + log_esp3d("Serial %d for %d Task creation", _serialIndex,_id); + if (_hserialtask == nullptr && _id==MAIN_SERIAL) { xTaskCreatePinnedToCore( ESP3DSerialTaskfn, /* Task function. */ "ESP3D Serial Task", /* name of task. */ @@ -128,13 +179,14 @@ bool SerialService::begin() ); } if (_hserialtask == nullptr) { - log_esp3d("Serial Task creation failed"); + log_esp3d("Serial %d for %d Task creation failed",_serialIndex, _id); return false; } #endif //SERIAL_INDEPENDANT_TASK #endif //ARDUINO_ARCH_ESP32 } _started = true; + log_esp3d("Serial %d for %d is started", _serialIndex, _id); return true; } //End serial @@ -143,7 +195,7 @@ bool SerialService::end() flush(); delay (100); swap(); - ESP3D_SERIAL.end(); + Serials[_serialIndex]->end(); _buffer_size = 0; _started = false; return true; @@ -173,6 +225,9 @@ bool SerialService::is_valid_baudrate(long br) //Function which could be called in other loop void SerialService::process() { + if (!_started) { + return; + } //Do we have some data waiting size_t len = available(); if (len > 0) { @@ -199,19 +254,25 @@ void SerialService::process() //Function which could be called in other loop void SerialService::handle() { -//for ESP32 there is dedicated task for it -#if !(defined(ARDUINO_ARCH_ESP32) && defined(SERIAL_INDEPENDANT_TASK)) + //the serial bridge do not use independant task + //not sure if it is sill necessary to do it for the main serial + //TBC.. +#if defined(ARDUINO_ARCH_ESP32) && defined(SERIAL_INDEPENDANT_TASK) + if (_id==MAIN_SERIAL) { + return; + } +#endif //ARDUINO_ARCH_ESP32 && SERIAL_INDEPENDANT_TASK0 process(); -#endif //ARDUINO_ARCH_ESP8266 - } void SerialService::flushbuffer() { - ESP3DOutput output(ESP_SERIAL_CLIENT); + ESP3DOutput output(_client); _buffer[_buffer_size] = 0x0; //dispatch command - esp3d_commands.process(_buffer, _buffer_size, &output,_needauthentication?LEVEL_GUEST:LEVEL_ADMIN); + if (_started) { + esp3d_commands.process(_buffer, _buffer_size, &output,_needauthentication?LEVEL_GUEST:LEVEL_ADMIN); + } _lastflush = millis(); _buffer_size = 0; } @@ -219,6 +280,9 @@ void SerialService::flushbuffer() //push collected data to buffer and proceed accordingly void SerialService::push2buffer(uint8_t * sbuf, size_t len) { + if (!_started) { + return; + } log_esp3d("buffer get %d data ", len); #if COMMUNICATION_PROTOCOL == MKS_SERIAL static bool isFrameStarted = false; @@ -350,14 +414,25 @@ void SerialService::push2buffer(uint8_t * sbuf, size_t len) bool SerialService::reset() { log_esp3d("Reset serial"); - return Settings_ESP3D::write_uint32 (ESP_BAUD_RATE, Settings_ESP3D::get_default_int32_value(ESP_BAUD_RATE)); + bool res = false; + switch (_id) { + case MAIN_SERIAL: + return Settings_ESP3D::write_uint32 (ESP_BAUD_RATE, Settings_ESP3D::get_default_int32_value(ESP_BAUD_RATE)); +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) + case BRIDGE_SERIAL: + res = Settings_ESP3D::write_byte (ESP_SERIAL_BRIDGE_ON, Settings_ESP3D::get_default_byte_value(ESP_SERIAL_BRIDGE_ON)); + return res && Settings_ESP3D::write_uint32 (ESP_SERIAL_BRIDGE_BAUD, Settings_ESP3D::get_default_int32_value(ESP_SERIAL_BRIDGE_BAUD)); +#endif //ESP_SERIAL_BRIDGE_OUTPUT + default: + return res; + } } void SerialService::updateBaudRate(long br) { if (br!=baudRate()) { - ESP3D_SERIAL.flush(); - ESP3D_SERIAL.updateBaudRate(br); + Serials[_serialIndex]->flush(); + Serials[_serialIndex]->updateBaudRate(br); } } @@ -365,7 +440,7 @@ void SerialService::updateBaudRate(long br) long SerialService::baudRate() { long br = 0; - br = ESP3D_SERIAL.baudRate(); + br = Serials[_serialIndex]->baudRate(); #ifdef ARDUINO_ARCH_ESP32 //workaround for ESP32 if (br == 115201) { @@ -380,13 +455,19 @@ long SerialService::baudRate() size_t SerialService::write(uint8_t c) { - return ESP3D_SERIAL.write(c); + if (!_started) { + return 0; + } + return Serials[_serialIndex]->write(c); } size_t SerialService::write(const uint8_t *buffer, size_t size) { - if ((uint)ESP3D_SERIAL.availableForWrite() >= size) { - return ESP3D_SERIAL.write(buffer, size); + if (!_started) { + return 0; + } + if ((uint)Serials[_serialIndex]->availableForWrite() >= size) { + return Serials[_serialIndex]->write(buffer, size); } else { size_t sizetosend = size; size_t sizesent = 0; @@ -394,10 +475,10 @@ size_t SerialService::write(const uint8_t *buffer, size_t size) uint32_t starttime = millis(); //loop until all is sent or timeout while (sizetosend>0 && ((millis() - starttime) < 100)) { - size_t available = ESP3D_SERIAL.availableForWrite(); + size_t available = Serials[_serialIndex]->availableForWrite(); if(available>0) { //in case less is sent - available = ESP3D_SERIAL.write(&buffertmp[sizesent], (available >= sizetosend)?sizetosend:available); + available = Serials[_serialIndex]->write(&buffertmp[sizesent], (available >= sizetosend)?sizetosend:available); sizetosend-=available; sizesent+=available; starttime=millis(); @@ -411,33 +492,48 @@ size_t SerialService::write(const uint8_t *buffer, size_t size) int SerialService::availableForWrite() { - return ESP3D_SERIAL.availableForWrite(); + if (!_started) { + return 0; + } + return Serials[_serialIndex]->availableForWrite(); } int SerialService::available() { - return ESP3D_SERIAL.available(); + if (!_started) { + return 0; + } + return Serials[_serialIndex]->available(); } int SerialService::read() { - return ESP3D_SERIAL.read(); + if (!_started) { + return -1; + } + return Serials[_serialIndex]->read(); } size_t SerialService::readBytes(uint8_t * sbuf, size_t len) { - return ESP3D_SERIAL.readBytes(sbuf, len); + if (!_started) { + return -1; + } + return Serials[_serialIndex]->readBytes(sbuf, len); } void SerialService::flush() { - ESP3D_SERIAL.flush(); + if (!_started) { + return ; + } + Serials[_serialIndex]->flush(); } void SerialService::swap() { #ifdef ARDUINO_ARCH_ESP8266 - ESP3D_SERIAL.swap(); + Serials[_serialIndex]->swap(); #endif //ARDUINO_ARCH_ESP8266 } diff --git a/esp3d/src/modules/serial/serial_service.h b/esp3d/src/modules/serial/serial_service.h index 943318b4..692c5d30 100644 --- a/esp3d/src/modules/serial/serial_service.h +++ b/esp3d/src/modules/serial/serial_service.h @@ -28,16 +28,20 @@ class SerialService : public Print { public: - SerialService(); + SerialService(uint8_t id); ~SerialService(); void setParameters(); - bool begin(); + bool begin(uint8_t serialIndex); bool end(); void updateBaudRate(long br); void handle(); void process(); bool reset(); long baudRate(); + uint8_t serialIndex() + { + return _serialIndex; + } const long * get_baudratelist(uint8_t * count); void flush(); void swap(); @@ -73,6 +77,11 @@ public: return _started; } private: + uint8_t _serialIndex; + uint8_t _client; + uint8_t _id; + uint8_t _rxPin; + uint8_t _txPin; bool _started; bool _needauthentication; uint32_t _lastflush; @@ -84,5 +93,9 @@ private: extern SerialService serial_service; +#if defined(ESP_SERIAL_BRIDGE_OUTPUT) +extern SerialService serial_bridge_service; +#endif //ESP_SERIAL_BRIDGE_OUTPUT + #endif //_SERIAL_SERVICES_H diff --git a/esp3d/src/modules/telnet/telnet_server.cpp b/esp3d/src/modules/telnet/telnet_server.cpp index ee05012d..4ff16199 100644 --- a/esp3d/src/modules/telnet/telnet_server.cpp +++ b/esp3d/src/modules/telnet/telnet_server.cpp @@ -241,7 +241,7 @@ size_t Telnet_Server::write(uint8_t c) size_t Telnet_Server::write(const uint8_t *buffer, size_t size) { - if (isConnected() && (size>0)) { + if (isConnected() && (size>0) && _started) { if ((size_t)availableForWrite() >= size) { //push data to connected telnet client return _telnetClients.write(buffer, size); diff --git a/esp3d/src/modules/update/update_service.cpp b/esp3d/src/modules/update/update_service.cpp index 8a08bd4f..a3cb21c7 100644 --- a/esp3d/src/modules/update/update_service.cpp +++ b/esp3d/src/modules/update/update_service.cpp @@ -93,6 +93,7 @@ const uint16_t IPKeysPos[] = {ESP_STA_IP_VALUE, } ; const char * ServintKeysVal[] = { + "Serial_Bridge_Baud" "HTTP_Port", "TELNET_Port", "SENSOR_INTERVAL", @@ -104,6 +105,7 @@ const char * ServintKeysVal[] = { } ; const uint16_t ServintKeysPos[] = { + ESP_SERIAL_BRIDGE_BAUD, ESP_HTTP_PORT, ESP_TELNET_PORT, ESP_SENSOR_INTERVAL, @@ -122,7 +124,8 @@ const uint16_t SysintKeysPos[] = {ESP_BAUD_RATE, ESP_BOOT_DELAY } ; -const char * ServboolKeysVal[] = {"HTTP_active", +const char * ServboolKeysVal[] = {"Serial_Bridge_active", + "HTTP_active", "TELNET_active", "WebSocket_active", "WebDav_active", @@ -133,7 +136,8 @@ const char * ServboolKeysVal[] = {"HTTP_active", "Radio_enabled" } ; -const uint16_t ServboolKeysPos[] = {ESP_HTTP_ON, +const uint16_t ServboolKeysPos[] = {ESP_SERIAL_BRIDGE_ON, + ESP_HTTP_ON, ESP_TELNET_ON, ESP_WEBSOCKET_ON, ESP_WEBDAV_ON, @@ -144,7 +148,8 @@ const uint16_t ServboolKeysPos[] = {ESP_HTTP_ON, ESP_BOOT_RADIO_STATE } ; -const char * SysboolKeysVal[] = {"Active_Remote_Screen", +const char * SysboolKeysVal[] = {"Active_Serial_Bridge", + "Active_Remote_Screen", "Active_ESP3D_Screen", "Active_Serial ", "Active_WebSocket", @@ -154,7 +159,8 @@ const char * SysboolKeysVal[] = {"Active_Remote_Screen", "Secure_serial" } ; -const uint16_t SysboolKeysPos[] = {ESP_REMOTE_SCREEN_FLAG, +const uint16_t SysboolKeysPos[] = {ESP_SERIAL_BRIDGE_FLAG, + ESP_REMOTE_SCREEN_FLAG, ESP_SCREEN_FLAG, ESP_SERIAL_FLAG, ESP_WEBSOCKET_FLAG,