Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Remote Command Over WiFi #255

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/GlobalVars.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -40,5 +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
2 changes: 2 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

#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 true
#define LED_INTERVAL_STANDBY 10000
#define PRINT_STATE_EVERY_MS 60000

Expand Down
7 changes: 7 additions & 0 deletions src/logging/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ 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
}
}
}
16 changes: 16 additions & 0 deletions src/logging/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Level.h"
#include "debug.h"
#include <Arduino.h>
#include "RemoteLogHelper.h"

namespace SlimeVR
{
Expand Down Expand Up @@ -98,6 +99,21 @@ namespace SlimeVR
}

Serial.println();

#if USE_REMOTE_COMMAND
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();
}
#endif
}

const char *const m_Prefix;
Expand Down
11 changes: 11 additions & 0 deletions src/logging/RemoteLogHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <Arduino.h>

#include "GlobalVars.h"

namespace SlimeVR {
namespace Logging {
bool getRemoteCmdConncted() { return networkRemoteCmd.isConnected() && networkConnection.isConnected(); }

Stream& getRemoteCmdStream() { return networkRemoteCmd.getStream(); }
} // namespace Logging
} // namespace SlimeVR
13 changes: 13 additions & 0 deletions src/logging/RemoteLogHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef LOGGING_RETOMELOGHELPER_H
#define LOGGING_RETOMELOGHELPER_H

#include <Arduino.h>

namespace SlimeVR {
namespace Logging {
bool getRemoteCmdConncted();
Stream& getRemoteCmdStream();
} // namespace Logging
} // namespace SlimeVR

#endif // LOGGING_RETOMELOGHELPER_H
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +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;
Expand Down
3 changes: 3 additions & 0 deletions src/network/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/network/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "sensors/sensor.h"
#include "wifihandler.h"
#include "featureflags.h"
#include "remotecmd.h"

namespace SlimeVR {
namespace Network {
Expand Down Expand Up @@ -124,6 +125,8 @@ class Connection {
bool beginBundle();
bool endBundle();

friend RemoteCmd;

private:
void updateSensorState(std::vector<Sensor *> & sensors);
void maybeRequestFeatureFlags();
Expand Down
6 changes: 5 additions & 1 deletion src/network/featureflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <cstring>
#include <algorithm>
#include "debug.h"

/**
* Bit packed flags, enum values start with 0 and indicate which bit it is.
Expand Down Expand Up @@ -73,8 +74,8 @@ class FirmwareFeatures {
public:
enum EFirmwareFeatureFlags: uint32_t {
// EXAMPLE_FEATURE,
REMOTE_COMMAND = 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this really be 0? Is there a reason we started at 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just from that the first version of remote command is written before WiFi scanning

B64_WIFI_SCANNING = 1,

// Add new flags here

BITS_TOTAL,
Expand All @@ -83,6 +84,9 @@ class FirmwareFeatures {
// Flags to send
static constexpr const std::initializer_list<EFirmwareFeatureFlags> flagsEnabled = {
// EXAMPLE_FEATURE,
#ifdef USE_REMOTE_COMMAND
REMOTE_COMMAND,
#endif
B64_WIFI_SCANNING,

// Add enabled flags here
Expand Down
6 changes: 6 additions & 0 deletions src/network/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +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
Expand Down
56 changes: 56 additions & 0 deletions src/network/remotecmd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "remotecmd.h"

#include "GlobalVars.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 {
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) {
rejectedIP = rcmdClient.remoteIP();
rcmdClient.stop();
}
}
#if !ALLOW_REMOTE_WIFI_PROV
else {
rejectedIP = rcmdClient.remoteIP();
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",
rejectedIP.toString().c_str()
);
}
}
}
}

bool RemoteCmd::isConnected() { return rcmdClient.connected(); }

Stream& RemoteCmd::getStream() { return rcmdClient; }

} // namespace Network
} // namespace SlimeVR
53 changes: 53 additions & 0 deletions src/network/remotecmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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 <WiFiClient.h>
#include <WiFiServer.h>

#include "globals.h"
#include "logging/Logger.h"

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_
Loading