[sim] Add WebSocket extension (client/server) (#2589)

This allows access to HAL-level simulation data via a WebSocket connection.

The server additionally serves local files.

The following environment variables can be used for configuration:
HALSIMWS_USERROOT (server) - local directory to use for file serving for /user/ URIs, defaults to ./sim/user
HALSIMWS_SYSROOT (server) - local directory to use for file serving for all other URIs, defaults to ./sim
HALSIMWS_URI (client or server) - WebSocket URI, defaults to /wpilibws
HALSIMWS_PORT (client or server) - port number, defaults to 8080
HALSIMWS_HOST (client) - host to connect to, defaults to localhost

Co-authored-by: Zhiquan Yeo <zyeo8@bloomberg.net>
Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
Co-authored-by: jpokornyiii <jpokornyiii@gmail.com>
This commit is contained in:
Zhiquan Yeo
2020-08-20 01:14:03 -04:00
committed by GitHub
parent e127bac7fd
commit 932bfcf374
51 changed files with 3696 additions and 1 deletions

View File

@@ -0,0 +1,67 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <WSProviderContainer.h>
#include <WSProvider_Analog.h>
#include <WSProvider_DIO.h>
#include <WSProvider_DriverStation.h>
#include <WSProvider_Encoder.h>
#include <WSProvider_Joystick.h>
#include <WSProvider_PWM.h>
#include <WSProvider_Relay.h>
#include <WSProvider_RoboRIO.h>
#include <WSProvider_SimDevice.h>
#include <WSProvider_dPWM.h>
#include <hal/Main.h>
#include <wpi/raw_ostream.h>
#include "HALSimWSServer.h"
using namespace std::placeholders;
using namespace wpilibws;
// Currently, robots never terminate, so we keep static objects that are
// never properly released or cleaned up.
static ProviderContainer providers;
static HALSimWSProviderSimDevices simDevices(providers);
extern "C" {
#if defined(WIN32) || defined(_WIN32)
__declspec(dllexport)
#endif
int HALSIM_InitExtension(void) {
wpi::outs() << "Websocket Simulator Initializing.\n";
auto hsw = std::make_shared<HALSimWeb>(providers, simDevices);
HALSimWeb::SetInstance(hsw);
if (!hsw->Initialize()) {
return -1;
}
WSRegisterFunc registerFunc = [&](auto key, auto provider) {
providers.Add(key, provider);
};
HALSimWSProviderAnalogIn::Initialize(registerFunc);
HALSimWSProviderAnalogOut::Initialize(registerFunc);
HALSimWSProviderDIO::Initialize(registerFunc);
HALSimWSProviderDigitalPWM::Initialize(registerFunc);
HALSimWSProviderDriverStation::Initialize(registerFunc);
HALSimWSProviderEncoder::Initialize(registerFunc);
HALSimWSProviderJoystick::Initialize(registerFunc);
HALSimWSProviderPWM::Initialize(registerFunc);
HALSimWSProviderRelay::Initialize(registerFunc);
HALSimWSProviderRoboRIO::Initialize(registerFunc);
simDevices.Initialize(hsw->GetLoop());
HAL_SetMain(nullptr, HALSimWeb::Main, HALSimWeb::Exit);
wpi::outs() << "Websocket Simulator Initialized!\n";
return 0;
}
} // extern "C"