From 4d5eff0502d09d763bad8aba2f27363051ea2c1b Mon Sep 17 00:00:00 2001 From: nekomona Date: Tue, 4 Jul 2023 10:34:39 +0800 Subject: [PATCH 01/10] Add remote command through telnet --- src/GlobalVars.h | 2 ++ src/logging/Logger.cpp | 4 +++ src/logging/Logger.h | 13 ++++++++ src/logging/RemoteLogHelper.cpp | 18 ++++++++++ src/logging/RemoteLogHelper.h | 15 +++++++++ src/main.cpp | 1 + src/network/manager.cpp | 2 ++ src/network/remotecmd.cpp | 38 +++++++++++++++++++++ src/network/remotecmd.h | 52 +++++++++++++++++++++++++++++ src/serial/serialcommands.cpp | 59 ++++++++++++--------------------- 10 files changed, 167 insertions(+), 37 deletions(-) create mode 100644 src/logging/RemoteLogHelper.cpp create mode 100644 src/logging/RemoteLogHelper.h create mode 100644 src/network/remotecmd.cpp create mode 100644 src/network/remotecmd.h diff --git a/src/GlobalVars.h b/src/GlobalVars.h index 45adde97b..411abf8ed 100644 --- a/src/GlobalVars.h +++ b/src/GlobalVars.h @@ -30,6 +30,7 @@ #include "configuration/Configuration.h" #include "network/connection.h" #include "network/manager.h" +#include "network/remotecmd.h" #include "sensors/SensorManager.h" #include "status/StatusManager.h" @@ -40,5 +41,6 @@ extern SlimeVR::Configuration::Configuration configuration; extern SlimeVR::Sensors::SensorManager sensorManager; extern SlimeVR::Network::Manager networkManager; extern SlimeVR::Network::Connection networkConnection; +extern SlimeVR::Network::RemoteCmd networkRemoteCmd; #endif diff --git a/src/logging/Logger.cpp b/src/logging/Logger.cpp index de7a5a639..e21ba3601 100644 --- a/src/logging/Logger.cpp +++ b/src/logging/Logger.cpp @@ -77,6 +77,10 @@ namespace SlimeVR } Serial.printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer); + + if (getRemoteCmdConncted()) { + getRemoteCmdStream().printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer); + } } } } diff --git a/src/logging/Logger.h b/src/logging/Logger.h index 06e46c979..7a37a2772 100644 --- a/src/logging/Logger.h +++ b/src/logging/Logger.h @@ -4,6 +4,7 @@ #include "Level.h" #include "debug.h" #include +#include "RemoteLogHelper.h" namespace SlimeVR { @@ -98,6 +99,18 @@ namespace SlimeVR } Serial.println(); + + if (getRemoteCmdConncted()) { + Stream & networkStream = getRemoteCmdStream(); + networkStream.printf("[%-5s] [%s] %s", levelToString(level), buf, str); + + for (size_t i = 0; i < size; i++) + { + networkStream.print(array[i]); + } + + networkStream.println(); + } } const char *const m_Prefix; diff --git a/src/logging/RemoteLogHelper.cpp b/src/logging/RemoteLogHelper.cpp new file mode 100644 index 000000000..5112eb664 --- /dev/null +++ b/src/logging/RemoteLogHelper.cpp @@ -0,0 +1,18 @@ +#include +#include "GlobalVars.h" + +namespace SlimeVR +{ + namespace Logging + { + bool getRemoteCmdConncted() + { + return networkRemoteCmd.isConnected(); + } + + Stream & getRemoteCmdStream() + { + return networkRemoteCmd.getStream(); + } + } +} diff --git a/src/logging/RemoteLogHelper.h b/src/logging/RemoteLogHelper.h new file mode 100644 index 000000000..89d942ddd --- /dev/null +++ b/src/logging/RemoteLogHelper.h @@ -0,0 +1,15 @@ +#ifndef LOGGING_RETOMELOGHELPER_H +#define LOGGING_RETOMELOGHELPER_H + +#include + +namespace SlimeVR +{ + namespace Logging + { + bool getRemoteCmdConncted(); + Stream & getRemoteCmdStream(); + } +} + +#endif // LOGGING_RETOMELOGHELPER_H diff --git a/src/main.cpp b/src/main.cpp index b4a4dd8ee..3b8ed9f72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,6 +39,7 @@ SlimeVR::Status::StatusManager statusManager; SlimeVR::Configuration::Configuration configuration; SlimeVR::Network::Manager networkManager; SlimeVR::Network::Connection networkConnection; +SlimeVR::Network::RemoteCmd networkRemoteCmd; int sensorToCalibrate = -1; bool blinking = false; diff --git a/src/network/manager.cpp b/src/network/manager.cpp index c27329fd8..e71b2d99a 100644 --- a/src/network/manager.cpp +++ b/src/network/manager.cpp @@ -43,9 +43,11 @@ void Manager::update() { if (!wasConnected) { // WiFi was reconnected, rediscover the server and reconnect networkConnection.reset(); + networkRemoteCmd.reset(); } networkConnection.update(); + networkRemoteCmd.update(); } } // namespace Network diff --git a/src/network/remotecmd.cpp b/src/network/remotecmd.cpp new file mode 100644 index 000000000..a81c1cb67 --- /dev/null +++ b/src/network/remotecmd.cpp @@ -0,0 +1,38 @@ +#include "remotecmd.h" + +namespace SlimeVR { +namespace Network { + +void RemoteCmd::reset() +{ + rcmdClient = WiFiClient(); + rcmdServer.begin(); +} + +void RemoteCmd::update() +{ + // Check for new connections to remote command + if (rcmdServer.hasClient()) { + if (rcmdClient.connected()) { + // Connection already exists, drop the new one + rcmdServer.accept().stop(); + r_Logger.info("Remote command multi-connection dropped"); + } else { + rcmdClient = rcmdServer.accept(); + r_Logger.info("Remote command from %s connected", rcmdClient.remoteIP().toString().c_str()); + } + } +} + +bool RemoteCmd::isConnected() +{ + return rcmdClient.connected(); +} + +Stream & RemoteCmd::getStream() +{ + return rcmdClient; +} + +} // namespace Network +} // namespace SlimeVR diff --git a/src/network/remotecmd.h b/src/network/remotecmd.h new file mode 100644 index 000000000..3f3ba6030 --- /dev/null +++ b/src/network/remotecmd.h @@ -0,0 +1,52 @@ +/* + SlimeVR Code is placed under the MIT license + Copyright (c) 2023 SlimeVR Contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +#ifndef SLIMEVR_NETWORK_REMOTECMD_H_ +#define SLIMEVR_NETWORK_REMOTECMD_H_ + +#include "globals.h" +#include "logging/Logger.h" +#include +#include + +namespace SlimeVR { +namespace Network { + +class RemoteCmd { +public: + void reset(); + void update(); + + bool isConnected(); + Stream & getStream(); + +private: + SlimeVR::Logging::Logger r_Logger = SlimeVR::Logging::Logger("RemoteCmd"); + + WiFiServer rcmdServer = WiFiServer(23); + WiFiClient rcmdClient; +}; + +} // namespace Network +} // namespace SlimeVR + +#endif // SLIMEVR_NETWORK_REMOTECMD_H_ diff --git a/src/serial/serialcommands.cpp b/src/serial/serialcommands.cpp index a85469502..124a4f3e1 100644 --- a/src/serial/serialcommands.cpp +++ b/src/serial/serialcommands.cpp @@ -159,43 +159,22 @@ namespace SerialCommands { } if (parser->equalCmdParam(1, "CONFIG")) { - String str = - "BOARD=%d\n" - "IMU=%d\n" - "SECOND_IMU=%d\n" - "IMU_ROTATION=%f\n" - "SECOND_IMU_ROTATION=%f\n" - "BATTERY_MONITOR=%d\n" - "BATTERY_SHIELD_RESISTANCE=%d\n" - "BATTERY_SHIELD_R1=%d\n" - "BATTERY_SHIELD_R2=%d\n" - "PIN_IMU_SDA=%d\n" - "PIN_IMU_SCL=%d\n" - "PIN_IMU_INT=%d\n" - "PIN_IMU_INT_2=%d\n" - "PIN_BATTERY_LEVEL=%d\n" - "LED_PIN=%d\n" - "LED_INVERTED=%d\n"; - - Serial.printf( - str.c_str(), - BOARD, - IMU, - SECOND_IMU, - IMU_ROTATION, - SECOND_IMU_ROTATION, - BATTERY_MONITOR, - BATTERY_SHIELD_RESISTANCE, - BATTERY_SHIELD_R1, - BATTERY_SHIELD_R2, - PIN_IMU_SDA, - PIN_IMU_SCL, - PIN_IMU_INT, - PIN_IMU_INT_2, - PIN_BATTERY_LEVEL, - LED_PIN, - LED_INVERTED - ); + logger.info("BOARD=%d", BOARD); + logger.info("IMU=%d", IMU); + logger.info("SECOND_IMU=%d", SECOND_IMU); + logger.info("IMU_ROTATION=%f", IMU_ROTATION); + logger.info("SECOND_IMU_ROTATION=%f", SECOND_IMU_ROTATION); + logger.info("BATTERY_MONITOR=%d", BATTERY_MONITOR); + logger.info("BATTERY_SHIELD_RESISTANCE=%d", BATTERY_SHIELD_RESISTANCE); + logger.info("BATTERY_SHIELD_R1=%d", BATTERY_SHIELD_R1); + logger.info("BATTERY_SHIELD_R2=%d", BATTERY_SHIELD_R2); + logger.info("PIN_IMU_SDA=%d", PIN_IMU_SDA); + logger.info("PIN_IMU_SCL=%d", PIN_IMU_SCL); + logger.info("PIN_IMU_INT=%d", PIN_IMU_INT); + logger.info("PIN_IMU_INT_2=%d", PIN_IMU_INT_2); + logger.info("PIN_BATTERY_LEVEL=%d", PIN_BATTERY_LEVEL); + logger.info("LED_PIN=%d", LED_PIN); + logger.info("LED_INVERTED=%d", LED_INVERTED); } if (parser->equalCmdParam(1, "TEST")) { @@ -333,5 +312,11 @@ namespace SerialCommands { void update() { cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &Serial); + if (networkRemoteCmd.isConnected()) { + Stream & networkStream = networkRemoteCmd.getStream(); + while (networkStream.available()) { + cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &networkStream); + } + } } } From 6ba519c74dd6993f4f05296ce95d1ee8a31e58c5 Mon Sep 17 00:00:00 2001 From: nekomona Date: Tue, 4 Jul 2023 10:34:39 +0800 Subject: [PATCH 02/10] Code formatting --- src/logging/Logger.cpp | 3 ++- src/logging/Logger.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/logging/Logger.cpp b/src/logging/Logger.cpp index e21ba3601..dbad9a5fa 100644 --- a/src/logging/Logger.cpp +++ b/src/logging/Logger.cpp @@ -78,7 +78,8 @@ namespace SlimeVR Serial.printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer); - if (getRemoteCmdConncted()) { + if (getRemoteCmdConncted()) + { getRemoteCmdStream().printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer); } } diff --git a/src/logging/Logger.h b/src/logging/Logger.h index 7a37a2772..98317805f 100644 --- a/src/logging/Logger.h +++ b/src/logging/Logger.h @@ -100,7 +100,8 @@ namespace SlimeVR Serial.println(); - if (getRemoteCmdConncted()) { + if (getRemoteCmdConncted()) + { Stream & networkStream = getRemoteCmdStream(); networkStream.printf("[%-5s] [%s] %s", levelToString(level), buf, str); From 63256b06f854a8d1797c81d0694926f8238ce4c7 Mon Sep 17 00:00:00 2001 From: nekomona Date: Tue, 4 Jul 2023 10:34:39 +0800 Subject: [PATCH 03/10] Add define switch for remote console --- src/GlobalVars.h | 2 ++ src/debug.h | 1 + src/logging/Logger.cpp | 2 ++ src/logging/Logger.h | 2 ++ src/main.cpp | 2 ++ src/network/manager.cpp | 4 ++++ src/serial/serialcommands.cpp | 2 ++ 7 files changed, 15 insertions(+) diff --git a/src/GlobalVars.h b/src/GlobalVars.h index 411abf8ed..ff6fe8748 100644 --- a/src/GlobalVars.h +++ b/src/GlobalVars.h @@ -41,6 +41,8 @@ extern SlimeVR::Configuration::Configuration configuration; extern SlimeVR::Sensors::SensorManager sensorManager; extern SlimeVR::Network::Manager networkManager; extern SlimeVR::Network::Connection networkConnection; +#if USE_REMOTE_COMMAND extern SlimeVR::Network::RemoteCmd networkRemoteCmd; +#endif #endif diff --git a/src/debug.h b/src/debug.h index b2c4968d5..d8e3c1f05 100644 --- a/src/debug.h +++ b/src/debug.h @@ -47,6 +47,7 @@ #define serialDebug false // Set to true to get Serial output for debugging #define serialBaudRate 115200 +#define USE_REMOTE_COMMAND true #define LED_INTERVAL_STANDBY 10000 #define PRINT_STATE_EVERY_MS 60000 diff --git a/src/logging/Logger.cpp b/src/logging/Logger.cpp index dbad9a5fa..8fc37fecc 100644 --- a/src/logging/Logger.cpp +++ b/src/logging/Logger.cpp @@ -78,10 +78,12 @@ namespace SlimeVR Serial.printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer); + #if USE_REMOTE_COMMAND if (getRemoteCmdConncted()) { getRemoteCmdStream().printf("[%-5s] [%s] %s\n", levelToString(level), buf, buffer); } + #endif } } } diff --git a/src/logging/Logger.h b/src/logging/Logger.h index 98317805f..ba51eb204 100644 --- a/src/logging/Logger.h +++ b/src/logging/Logger.h @@ -100,6 +100,7 @@ namespace SlimeVR Serial.println(); + #if USE_REMOTE_COMMAND if (getRemoteCmdConncted()) { Stream & networkStream = getRemoteCmdStream(); @@ -112,6 +113,7 @@ namespace SlimeVR networkStream.println(); } + #endif } const char *const m_Prefix; diff --git a/src/main.cpp b/src/main.cpp index 3b8ed9f72..917835208 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,7 +39,9 @@ SlimeVR::Status::StatusManager statusManager; SlimeVR::Configuration::Configuration configuration; SlimeVR::Network::Manager networkManager; SlimeVR::Network::Connection networkConnection; +#if USE_REMOTE_COMMAND SlimeVR::Network::RemoteCmd networkRemoteCmd; +#endif int sensorToCalibrate = -1; bool blinking = false; diff --git a/src/network/manager.cpp b/src/network/manager.cpp index e71b2d99a..213685289 100644 --- a/src/network/manager.cpp +++ b/src/network/manager.cpp @@ -43,11 +43,15 @@ void Manager::update() { if (!wasConnected) { // WiFi was reconnected, rediscover the server and reconnect networkConnection.reset(); + #ifdef USE_REMOTE_COMMAND networkRemoteCmd.reset(); + #endif } networkConnection.update(); + #ifdef USE_REMOTE_COMMAND networkRemoteCmd.update(); + #endif } } // namespace Network diff --git a/src/serial/serialcommands.cpp b/src/serial/serialcommands.cpp index 124a4f3e1..b0cb539ea 100644 --- a/src/serial/serialcommands.cpp +++ b/src/serial/serialcommands.cpp @@ -312,11 +312,13 @@ namespace SerialCommands { void update() { cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &Serial); + #if USE_REMOTE_COMMAND if (networkRemoteCmd.isConnected()) { Stream & networkStream = networkRemoteCmd.getStream(); while (networkStream.available()) { cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &networkStream); } } + #endif } } From 10b3a3e7334886d573448539edc7217193cef286 Mon Sep 17 00:00:00 2001 From: nekomona Date: Wed, 5 Jul 2023 01:36:48 +0800 Subject: [PATCH 04/10] Pushing through clang-format --- src/logging/RemoteLogHelper.cpp | 21 +++++++-------------- src/logging/RemoteLogHelper.h | 16 +++++++--------- src/network/remotecmd.cpp | 21 ++++++++------------- src/network/remotecmd.h | 11 ++++++----- 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/logging/RemoteLogHelper.cpp b/src/logging/RemoteLogHelper.cpp index 5112eb664..e2eecea12 100644 --- a/src/logging/RemoteLogHelper.cpp +++ b/src/logging/RemoteLogHelper.cpp @@ -1,18 +1,11 @@ #include + #include "GlobalVars.h" -namespace SlimeVR -{ - namespace Logging - { - bool getRemoteCmdConncted() - { - return networkRemoteCmd.isConnected(); - } +namespace SlimeVR { +namespace Logging { +bool getRemoteCmdConncted() { return networkRemoteCmd.isConnected(); } - Stream & getRemoteCmdStream() - { - return networkRemoteCmd.getStream(); - } - } -} +Stream& getRemoteCmdStream() { return networkRemoteCmd.getStream(); } +} // namespace Logging +} // namespace SlimeVR diff --git a/src/logging/RemoteLogHelper.h b/src/logging/RemoteLogHelper.h index 89d942ddd..0a7fea404 100644 --- a/src/logging/RemoteLogHelper.h +++ b/src/logging/RemoteLogHelper.h @@ -3,13 +3,11 @@ #include -namespace SlimeVR -{ - namespace Logging - { - bool getRemoteCmdConncted(); - Stream & getRemoteCmdStream(); - } -} +namespace SlimeVR { +namespace Logging { +bool getRemoteCmdConncted(); +Stream& getRemoteCmdStream(); +} // namespace Logging +} // namespace SlimeVR -#endif // LOGGING_RETOMELOGHELPER_H +#endif // LOGGING_RETOMELOGHELPER_H diff --git a/src/network/remotecmd.cpp b/src/network/remotecmd.cpp index a81c1cb67..442c4cb75 100644 --- a/src/network/remotecmd.cpp +++ b/src/network/remotecmd.cpp @@ -3,14 +3,12 @@ namespace SlimeVR { namespace Network { -void RemoteCmd::reset() -{ +void RemoteCmd::reset() { rcmdClient = WiFiClient(); rcmdServer.begin(); } -void RemoteCmd::update() -{ +void RemoteCmd::update() { // Check for new connections to remote command if (rcmdServer.hasClient()) { if (rcmdClient.connected()) { @@ -19,20 +17,17 @@ void RemoteCmd::update() r_Logger.info("Remote command multi-connection dropped"); } else { rcmdClient = rcmdServer.accept(); - r_Logger.info("Remote command from %s connected", rcmdClient.remoteIP().toString().c_str()); + r_Logger.info( + "Remote command from %s connected", + rcmdClient.remoteIP().toString().c_str() + ); } } } -bool RemoteCmd::isConnected() -{ - return rcmdClient.connected(); -} +bool RemoteCmd::isConnected() { return rcmdClient.connected(); } -Stream & RemoteCmd::getStream() -{ - return rcmdClient; -} +Stream& RemoteCmd::getStream() { return rcmdClient; } } // namespace Network } // namespace SlimeVR diff --git a/src/network/remotecmd.h b/src/network/remotecmd.h index 3f3ba6030..2f39aa62a 100644 --- a/src/network/remotecmd.h +++ b/src/network/remotecmd.h @@ -23,10 +23,11 @@ #ifndef SLIMEVR_NETWORK_REMOTECMD_H_ #define SLIMEVR_NETWORK_REMOTECMD_H_ +#include +#include + #include "globals.h" #include "logging/Logger.h" -#include -#include namespace SlimeVR { namespace Network { @@ -37,13 +38,13 @@ class RemoteCmd { void update(); bool isConnected(); - Stream & getStream(); + Stream& getStream(); private: SlimeVR::Logging::Logger r_Logger = SlimeVR::Logging::Logger("RemoteCmd"); - WiFiServer rcmdServer = WiFiServer(23); - WiFiClient rcmdClient; + WiFiServer rcmdServer = WiFiServer(23); + WiFiClient rcmdClient; }; } // namespace Network From f04cc669a791bdd566edf43fc9ddf50bc17e2ad0 Mon Sep 17 00:00:00 2001 From: nekomona Date: Wed, 12 Jul 2023 19:28:21 +0800 Subject: [PATCH 05/10] Add IP check for remote command --- src/debug.h | 1 + src/network/connection.h | 2 ++ src/network/remotecmd.cpp | 28 ++++++++++++++++++++++++---- src/network/wifihandler.cpp | 1 + src/serial/serialcommands.cpp | 19 +++++++++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/debug.h b/src/debug.h index d8e3c1f05..cee871375 100644 --- a/src/debug.h +++ b/src/debug.h @@ -48,6 +48,7 @@ #define serialDebug false // Set to true to get Serial output for debugging #define serialBaudRate 115200 #define USE_REMOTE_COMMAND true +#define ALLOW_REMOTE_WIFI_PROV false #define LED_INTERVAL_STANDBY 10000 #define PRINT_STATE_EVERY_MS 60000 diff --git a/src/network/connection.h b/src/network/connection.h index 7b8f7b6a2..8a968e582 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -124,6 +124,8 @@ class Connection { bool beginBundle(); bool endBundle(); + friend RemoteCmd; + private: void updateSensorState(std::vector & sensors); void maybeRequestFeatureFlags(); diff --git a/src/network/remotecmd.cpp b/src/network/remotecmd.cpp index 442c4cb75..eb38c9108 100644 --- a/src/network/remotecmd.cpp +++ b/src/network/remotecmd.cpp @@ -1,5 +1,7 @@ #include "remotecmd.h" +#include "GlobalVars.h" + namespace SlimeVR { namespace Network { @@ -17,10 +19,28 @@ void RemoteCmd::update() { r_Logger.info("Remote command multi-connection dropped"); } else { rcmdClient = rcmdServer.accept(); - r_Logger.info( - "Remote command from %s connected", - rcmdClient.remoteIP().toString().c_str() - ); + if (networkConnection.isConnected()) { + // Only accept if rcmdClient have the same remote IP as udpmanager + if (rcmdClient.remoteIP() = networkConnection.m_ServerHost) { + rcmdClient.stop(); + } + } +#if !ALLOW_REMOTE_WIFI_PROV + else { + rcmdClient.stop(); + } +#endif + if (rcmdClient.connected()) { + r_Logger.info( + "Remote command from %s connected", + rcmdClient.remoteIP().toString().c_str() + ); + } else { + r_Logger.info( + "Remote command from %s dropped", + rcmdClient.remoteIP().toString().c_str() + ); + } } } } diff --git a/src/network/wifihandler.cpp b/src/network/wifihandler.cpp index 4119c93c9..db109ae34 100644 --- a/src/network/wifihandler.cpp +++ b/src/network/wifihandler.cpp @@ -124,6 +124,7 @@ void WiFiNetwork::setUp() { void onConnected() { WiFiNetwork::stopProvisioning(); statusManager.setStatus(SlimeVR::Status::WIFI_CONNECTING, false); + networkRemoteCmd.reset(); isWifiConnected = true; hadWifi = true; wifiHandlerLogger.info("Connected successfully to SSID '%s', ip address %s", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); diff --git a/src/serial/serialcommands.cpp b/src/serial/serialcommands.cpp index b0cb539ea..c13242b85 100644 --- a/src/serial/serialcommands.cpp +++ b/src/serial/serialcommands.cpp @@ -39,6 +39,7 @@ namespace SerialCommands { CmdCallback<6> cmdCallbacks; CmdParser cmdParser; CmdBuffer<256> cmdBuffer; + bool cmdFromRemote = false; bool lengthCheck (const char* const text, unsigned int length, const char* const cmd, const char* const name) @@ -147,6 +148,10 @@ namespace SerialCommands { } void cmdGet(CmdParser * parser) { +#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV + if (cmdFromRemote && !networkConnection.isConnected()) return; +#endif + if (parser->getParamCount() < 2) { return; } @@ -240,11 +245,19 @@ namespace SerialCommands { } void cmdReboot(CmdParser * parser) { +#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV + if (cmdFromRemote && !networkConnection.isConnected()) return; +#endif + logger.info("REBOOT"); ESP.restart(); } void cmdFactoryReset(CmdParser * parser) { +#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV + if (cmdFromRemote && !networkConnection.isConnected()) return; +#endif + logger.info("FACTORY RESET"); configuration.reset(); @@ -270,6 +283,10 @@ namespace SerialCommands { } void cmdTemperatureCalibration(CmdParser* parser) { +#if USE_REMOTE_COMMAND && ALLOW_REMOTE_WIFI_PROV + if (cmdFromRemote && !networkConnection.isConnected()) return; +#endif + if (parser->getParamCount() > 1) { if (parser->equalCmdParam(1, "PRINT")) { for (auto sensor : sensorManager.getSensors()) { @@ -315,9 +332,11 @@ namespace SerialCommands { #if USE_REMOTE_COMMAND if (networkRemoteCmd.isConnected()) { Stream & networkStream = networkRemoteCmd.getStream(); + cmdFromRemote = true; while (networkStream.available()) { cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &networkStream); } + cmdFromRemote = false; } #endif } From 3938ce5735389913036ec9982609dfda0e6e127e Mon Sep 17 00:00:00 2001 From: nekomona Date: Wed, 12 Jul 2023 20:08:05 +0800 Subject: [PATCH 06/10] Fix IP check and provision check --- src/debug.h | 2 +- src/logging/RemoteLogHelper.cpp | 2 +- src/network/connection.cpp | 3 +++ src/network/connection.h | 1 + src/network/remotecmd.cpp | 7 +++++-- src/network/wifihandler.cpp | 1 - 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/debug.h b/src/debug.h index cee871375..627ae1121 100644 --- a/src/debug.h +++ b/src/debug.h @@ -48,7 +48,7 @@ #define serialDebug false // Set to true to get Serial output for debugging #define serialBaudRate 115200 #define USE_REMOTE_COMMAND true -#define ALLOW_REMOTE_WIFI_PROV false +#define ALLOW_REMOTE_WIFI_PROV true #define LED_INTERVAL_STANDBY 10000 #define PRINT_STATE_EVERY_MS 60000 diff --git a/src/logging/RemoteLogHelper.cpp b/src/logging/RemoteLogHelper.cpp index e2eecea12..3ed48ace8 100644 --- a/src/logging/RemoteLogHelper.cpp +++ b/src/logging/RemoteLogHelper.cpp @@ -4,7 +4,7 @@ namespace SlimeVR { namespace Logging { -bool getRemoteCmdConncted() { return networkRemoteCmd.isConnected(); } +bool getRemoteCmdConncted() { return networkRemoteCmd.isConnected() && networkConnection.isConnected(); } Stream& getRemoteCmdStream() { return networkRemoteCmd.getStream(); } } // namespace Logging diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 1f60ad876..3160d8a9c 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -577,6 +577,9 @@ void Connection::searchForServer() { statusManager.setStatus(SlimeVR::Status::SERVER_CONNECTING, false); ledManager.off(); +#if USE_REMOTE_COMMAND + networkRemoteCmd.reset(); +#endif m_Logger.debug( "Handshake successful, server is %s:%d", diff --git a/src/network/connection.h b/src/network/connection.h index 8a968e582..8e7cc17f2 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -31,6 +31,7 @@ #include "sensors/sensor.h" #include "wifihandler.h" #include "featureflags.h" +#include "remotecmd.h" namespace SlimeVR { namespace Network { diff --git a/src/network/remotecmd.cpp b/src/network/remotecmd.cpp index eb38c9108..de8118f5f 100644 --- a/src/network/remotecmd.cpp +++ b/src/network/remotecmd.cpp @@ -18,15 +18,18 @@ void RemoteCmd::update() { rcmdServer.accept().stop(); r_Logger.info("Remote command multi-connection dropped"); } else { + IPAddress rejectedIP; rcmdClient = rcmdServer.accept(); if (networkConnection.isConnected()) { // Only accept if rcmdClient have the same remote IP as udpmanager - if (rcmdClient.remoteIP() = networkConnection.m_ServerHost) { + if (rcmdClient.remoteIP() != networkConnection.m_ServerHost) { + rejectedIP = rcmdClient.remoteIP(); rcmdClient.stop(); } } #if !ALLOW_REMOTE_WIFI_PROV else { + rejectedIP = rcmdClient.remoteIP(); rcmdClient.stop(); } #endif @@ -38,7 +41,7 @@ void RemoteCmd::update() { } else { r_Logger.info( "Remote command from %s dropped", - rcmdClient.remoteIP().toString().c_str() + rejectedIP.toString().c_str() ); } } diff --git a/src/network/wifihandler.cpp b/src/network/wifihandler.cpp index db109ae34..4119c93c9 100644 --- a/src/network/wifihandler.cpp +++ b/src/network/wifihandler.cpp @@ -124,7 +124,6 @@ void WiFiNetwork::setUp() { void onConnected() { WiFiNetwork::stopProvisioning(); statusManager.setStatus(SlimeVR::Status::WIFI_CONNECTING, false); - networkRemoteCmd.reset(); isWifiConnected = true; hadWifi = true; wifiHandlerLogger.info("Connected successfully to SSID '%s', ip address %s", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); From 2d92da30f4fe316975ccbc01e29fc203afc6d198 Mon Sep 17 00:00:00 2001 From: nekomona Date: Wed, 12 Jul 2023 19:28:21 +0800 Subject: [PATCH 07/10] Add IP check for remote command --- src/network/wifihandler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/network/wifihandler.cpp b/src/network/wifihandler.cpp index 4119c93c9..db109ae34 100644 --- a/src/network/wifihandler.cpp +++ b/src/network/wifihandler.cpp @@ -124,6 +124,7 @@ void WiFiNetwork::setUp() { void onConnected() { WiFiNetwork::stopProvisioning(); statusManager.setStatus(SlimeVR::Status::WIFI_CONNECTING, false); + networkRemoteCmd.reset(); isWifiConnected = true; hadWifi = true; wifiHandlerLogger.info("Connected successfully to SSID '%s', ip address %s", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); From b08d080f3616ee1cd5f54a2d53bb6c78b22d384a Mon Sep 17 00:00:00 2001 From: nekomona Date: Wed, 12 Jul 2023 20:08:05 +0800 Subject: [PATCH 08/10] Fix IP check and provision check --- src/network/wifihandler.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/network/wifihandler.cpp b/src/network/wifihandler.cpp index db109ae34..4119c93c9 100644 --- a/src/network/wifihandler.cpp +++ b/src/network/wifihandler.cpp @@ -124,7 +124,6 @@ void WiFiNetwork::setUp() { void onConnected() { WiFiNetwork::stopProvisioning(); statusManager.setStatus(SlimeVR::Status::WIFI_CONNECTING, false); - networkRemoteCmd.reset(); isWifiConnected = true; hadWifi = true; wifiHandlerLogger.info("Connected successfully to SSID '%s', ip address %s", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str()); From 8d7d64c762cba1b936699757721e16d1849be9ca Mon Sep 17 00:00:00 2001 From: nekomona Date: Mon, 31 Jul 2023 01:29:30 +0800 Subject: [PATCH 09/10] Add remote command to feature flag list --- src/network/featureflags.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/network/featureflags.h b/src/network/featureflags.h index b0820be40..94eb0a131 100644 --- a/src/network/featureflags.h +++ b/src/network/featureflags.h @@ -26,6 +26,7 @@ #include #include +#include "debug.h" /** * Bit packed flags, enum values start with 0 and indicate which bit it is. @@ -73,8 +74,8 @@ class FirmwareFeatures { public: enum EFirmwareFeatureFlags: uint32_t { // EXAMPLE_FEATURE, + REMOTE_COMMAND = 0, B64_WIFI_SCANNING = 1, - // Add new flags here BITS_TOTAL, @@ -83,6 +84,9 @@ class FirmwareFeatures { // Flags to send static constexpr const std::initializer_list flagsEnabled = { // EXAMPLE_FEATURE, +#ifdef USE_REMOTE_COMMAND + REMOTE_COMMAND, +#endif B64_WIFI_SCANNING, // Add enabled flags here From 5e9db3639ed2ac3f0573bcbb675d4ab4815e1f37 Mon Sep 17 00:00:00 2001 From: nekomona Date: Fri, 4 Oct 2024 18:57:05 +0800 Subject: [PATCH 10/10] Update src/serial/serialcommands.cpp Co-authored-by: Butterscotch! --- src/serial/serialcommands.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serial/serialcommands.cpp b/src/serial/serialcommands.cpp index c13242b85..40d94352e 100644 --- a/src/serial/serialcommands.cpp +++ b/src/serial/serialcommands.cpp @@ -39,7 +39,7 @@ namespace SerialCommands { CmdCallback<6> cmdCallbacks; CmdParser cmdParser; CmdBuffer<256> cmdBuffer; - bool cmdFromRemote = false; + bool cmdFromRemote = false; bool lengthCheck (const char* const text, unsigned int length, const char* const cmd, const char* const name)