mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[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:
@@ -30,7 +30,7 @@ class SimCallbackRegistryBase {
|
||||
public:
|
||||
void Cancel(int32_t uid) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_callbacks) m_callbacks->erase(uid - 1);
|
||||
if (m_callbacks && uid > 0) m_callbacks->erase(uid - 1);
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
|
||||
@@ -32,6 +32,9 @@ include 'simulation:frc_gazebo_plugins'
|
||||
include 'simulation:halsim_gazebo'
|
||||
include 'simulation:halsim_ds_socket'
|
||||
include 'simulation:halsim_gui'
|
||||
include 'simulation:halsim_ws_core'
|
||||
include 'simulation:halsim_ws_client'
|
||||
include 'simulation:halsim_ws_server'
|
||||
include 'cameraserver'
|
||||
include 'cameraserver:multiCameraServer'
|
||||
include 'wpilibOldCommands'
|
||||
|
||||
@@ -3,3 +3,6 @@ add_subdirectory(halsim_gui)
|
||||
#add_subdirectory(frc_gazebo_plugins)
|
||||
#add_subdirectory(halsim_gazebo)
|
||||
add_subdirectory(halsim_ds_socket)
|
||||
add_subdirectory(halsim_ws_core)
|
||||
add_subdirectory(halsim_ws_client)
|
||||
add_subdirectory(halsim_ws_server)
|
||||
|
||||
16
simulation/halsim_ws_client/CMakeLists.txt
Normal file
16
simulation/halsim_ws_client/CMakeLists.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
project(halsim_ws_client)
|
||||
|
||||
include(CompileWarnings)
|
||||
|
||||
file(GLOB halsim_ws_client_src src/main/native/cpp/*.cpp)
|
||||
|
||||
add_library(halsim_ws_client MODULE ${halsim_ws_client_src})
|
||||
wpilib_target_warnings(halsim_ws_client)
|
||||
set_target_properties(halsim_ws_client PROPERTIES DEBUG_POSTFIX "d")
|
||||
target_link_libraries(halsim_ws_client PUBLIC hal halsim_ws_core)
|
||||
|
||||
target_include_directories(halsim_ws_client PRIVATE src/main/native/include)
|
||||
|
||||
set_property(TARGET halsim_ws_client PROPERTY FOLDER "libraries")
|
||||
|
||||
install(TARGETS halsim_ws_client EXPORT halsim_ws_client DESTINATION "${main_lib_dest}")
|
||||
35
simulation/halsim_ws_client/build.gradle
Normal file
35
simulation/halsim_ws_client/build.gradle
Normal file
@@ -0,0 +1,35 @@
|
||||
if (!project.hasProperty('onlylinuxathena')) {
|
||||
|
||||
description = "WebSocket Client Extension"
|
||||
|
||||
ext {
|
||||
includeWpiutil = true
|
||||
pluginName = 'halsim_ws_client'
|
||||
}
|
||||
|
||||
apply plugin: 'google-test-test-suite'
|
||||
|
||||
|
||||
ext {
|
||||
staticGtestConfigs = [:]
|
||||
}
|
||||
|
||||
staticGtestConfigs["${pluginName}Test"] = []
|
||||
apply from: "${rootDir}/shared/googletest.gradle"
|
||||
|
||||
apply from: "${rootDir}/shared/plugins/setupBuild.gradle"
|
||||
|
||||
model {
|
||||
binaries {
|
||||
all {
|
||||
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
|
||||
it.buildable = false
|
||||
return
|
||||
}
|
||||
|
||||
lib project: ":simulation:halsim_ws_core", library: "halsim_ws_core", linkage: "static"
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
simulation/halsim_ws_client/src/dev/native/cpp/main.cpp
Normal file
32
simulation/halsim_ws_client/src/dev/native/cpp/main.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include <hal/DriverStation.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <hal/Main.h>
|
||||
|
||||
extern "C" int HALSIM_InitExtension(void);
|
||||
|
||||
int main() {
|
||||
HAL_Initialize(500, 0);
|
||||
HALSIM_InitExtension();
|
||||
|
||||
HAL_RunMain();
|
||||
|
||||
int cycleCount = 0;
|
||||
while (cycleCount < 100) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
cycleCount++;
|
||||
std::cout << "Count: " << cycleCount << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "DONE" << std::endl;
|
||||
HAL_ExitMain();
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 "HALSimWSClient.h"
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/uv/util.h>
|
||||
|
||||
#include "HALSimWSClientConnection.h"
|
||||
|
||||
static constexpr int kTcpConnectAttemptTimeout = 1000;
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
std::shared_ptr<HALSimWS> HALSimWS::g_instance;
|
||||
|
||||
bool HALSimWS::Initialize() {
|
||||
const char* host = std::getenv("HALSIMWS_HOST");
|
||||
if (host != NULL) {
|
||||
m_host = host;
|
||||
} else {
|
||||
m_host = "localhost";
|
||||
}
|
||||
|
||||
const char* port = std::getenv("HALSIMWS_PORT");
|
||||
if (port != NULL) {
|
||||
try {
|
||||
m_port = std::stoi(port);
|
||||
} catch (const std::invalid_argument& err) {
|
||||
wpi::errs() << "Error decoding HALSIMWS_PORT (" << err.what() << ")\n";
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
m_port = 8080;
|
||||
}
|
||||
|
||||
const char* uri = std::getenv("HALSIMWS_URI");
|
||||
if (uri != NULL) {
|
||||
m_uri = uri;
|
||||
} else {
|
||||
m_uri = "/wpilibws";
|
||||
}
|
||||
|
||||
m_loop = uv::Loop::Create();
|
||||
if (!m_loop) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_loop->error.connect([](uv::Error err) {
|
||||
wpi::errs() << "HALSim WS Client libuv Error: " << err.str() << "\n";
|
||||
});
|
||||
|
||||
m_tcp_client = uv::Tcp::Create(m_loop);
|
||||
if (!m_tcp_client) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_tcp_client->SetNoDelay(true);
|
||||
|
||||
// Hook up TCP client events
|
||||
m_tcp_client->error.connect(
|
||||
[this, socket = m_tcp_client.get()](wpi::uv::Error err) {
|
||||
if (m_tcp_connected) {
|
||||
m_tcp_connected = false;
|
||||
m_connect_attempts = 0;
|
||||
m_loop->Stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// If we weren't previously connected, attempt a reconnection
|
||||
m_connect_timer->Start(uv::Timer::Time(kTcpConnectAttemptTimeout));
|
||||
});
|
||||
|
||||
m_tcp_client->closed.connect(
|
||||
[]() { wpi::outs() << "TCP connection closed\n"; });
|
||||
|
||||
// Set up the connection timer
|
||||
m_connect_timer = uv::Timer::Create(m_loop);
|
||||
if (!m_connect_timer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
wpi::outs() << "HALSimWS Initialized\n";
|
||||
wpi::outs() << "Will attempt to connect to: " << m_host << ":" << m_port
|
||||
<< " " << m_uri << "\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HALSimWS::Main(void* param) {
|
||||
GetInstance()->MainLoop();
|
||||
SetInstance(nullptr);
|
||||
}
|
||||
|
||||
void HALSimWS::MainLoop() {
|
||||
// Set up the timer to attempt connection
|
||||
m_connect_timer->timeout.connect([this] { AttemptConnect(); });
|
||||
|
||||
// Run the initial connect immediately
|
||||
m_connect_timer->Start(uv::Timer::Time(0));
|
||||
|
||||
m_loop->Run();
|
||||
}
|
||||
|
||||
void HALSimWS::AttemptConnect() {
|
||||
m_connect_attempts++;
|
||||
|
||||
wpi::outs() << "Connection Attempt " << m_connect_attempts << "\n";
|
||||
|
||||
struct sockaddr_in dest;
|
||||
uv::NameToAddr(m_host, m_port, &dest);
|
||||
|
||||
m_tcp_client->Connect(dest, [this, socket = m_tcp_client.get()]() {
|
||||
m_tcp_connected = true;
|
||||
auto wsConn = std::make_shared<HALSimWSClientConnection>(m_tcp_client);
|
||||
|
||||
wsConn->Initialize();
|
||||
});
|
||||
}
|
||||
|
||||
void HALSimWS::Exit(void* param) {
|
||||
auto inst = GetInstance();
|
||||
if (!inst) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto loop = inst->m_loop;
|
||||
loop->Walk([](uv::Handle& h) {
|
||||
h.SetLoopClosing(true);
|
||||
h.Close();
|
||||
});
|
||||
}
|
||||
|
||||
bool HALSimWS::RegisterWebsocket(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
|
||||
if (m_hws.lock()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_hws = hws;
|
||||
|
||||
m_simDevicesProvider.OnNetworkConnected(hws);
|
||||
|
||||
m_providers.ForEach([hws](std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
provider->OnNetworkConnected(hws);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HALSimWS::CloseWebsocket(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
|
||||
// Inform the providers that they need to cancel callbacks
|
||||
m_simDevicesProvider.OnNetworkDisconnected();
|
||||
|
||||
m_providers.ForEach([](std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
provider->OnNetworkDisconnected();
|
||||
});
|
||||
|
||||
if (hws == m_hws.lock()) {
|
||||
m_hws.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWS::OnNetValueChanged(const wpi::json& msg) {
|
||||
// Look for "type" and "device" fields so that we can
|
||||
// generate the key
|
||||
|
||||
try {
|
||||
auto& type = msg.at("type").get_ref<const std::string&>();
|
||||
auto& device = msg.at("device").get_ref<const std::string&>();
|
||||
|
||||
wpi::SmallString<64> key;
|
||||
key.append(type);
|
||||
key.append("/");
|
||||
key.append(device);
|
||||
|
||||
auto provider = m_providers.Get(key.str());
|
||||
if (provider) {
|
||||
provider->OnNetValueChanged(msg.at("data"));
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,122 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 "HALSimWSClientConnection.h"
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/raw_uv_ostream.h>
|
||||
|
||||
#include "HALSimWSClient.h"
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSClientConnection::Initialize() {
|
||||
// Get a shared pointer to ourselves
|
||||
auto self = this->shared_from_this();
|
||||
|
||||
auto hws = HALSimWS::GetInstance();
|
||||
std::string reqHost =
|
||||
hws->GetTargetHost() + ":" + std::to_string(hws->GetTargetPort());
|
||||
|
||||
auto ws =
|
||||
wpi::WebSocket::CreateClient(*m_stream, hws->GetTargetUri(), reqHost);
|
||||
|
||||
ws->SetData(self);
|
||||
|
||||
m_websocket = ws.get();
|
||||
|
||||
// Hook up events
|
||||
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
|
||||
conn.disconnect();
|
||||
|
||||
m_buffers = std::make_unique<BufferPool>();
|
||||
|
||||
m_exec =
|
||||
UvExecFunc::Create(m_stream->GetLoop(), [](auto out, LoopFunc func) {
|
||||
func();
|
||||
out.set_value();
|
||||
});
|
||||
|
||||
auto hws = HALSimWS::GetInstance();
|
||||
if (!hws) {
|
||||
wpi::errs() << "Unable to get hws instance\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hws->RegisterWebsocket(shared_from_this())) {
|
||||
wpi::errs() << "Unable to register websocket\n";
|
||||
return;
|
||||
}
|
||||
|
||||
m_ws_connected = true;
|
||||
wpi::outs() << "HALSimWS: WebSocket Connected\n";
|
||||
});
|
||||
|
||||
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
|
||||
auto hws = HALSimWS::GetInstance();
|
||||
if (!m_ws_connected || !hws) {
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::json j;
|
||||
try {
|
||||
j = wpi::json::parse(msg);
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
std::string err("JSON parse failed: ");
|
||||
err += e.what();
|
||||
wpi::errs() << err << "\n";
|
||||
m_websocket->Fail(1003, err);
|
||||
return;
|
||||
}
|
||||
|
||||
hws->OnNetValueChanged(j);
|
||||
});
|
||||
|
||||
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
|
||||
if (m_ws_connected) {
|
||||
wpi::outs() << "HALSimWS: Websocket Disconnected\n";
|
||||
m_ws_connected = false;
|
||||
|
||||
auto hws = HALSimWS::GetInstance();
|
||||
if (hws) {
|
||||
hws->CloseWebsocket(shared_from_this());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void HALSimWSClientConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
if (msg.empty()) {
|
||||
return;
|
||||
}
|
||||
wpi::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers->Allocate();
|
||||
}};
|
||||
|
||||
os << msg;
|
||||
|
||||
// Call the websocket send function on the uv loop
|
||||
m_exec->Call([this, sendBufs]() mutable {
|
||||
m_websocket->SendText(sendBufs, [this](auto bufs, wpi::uv::Error err) {
|
||||
{
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
m_buffers->Release(bufs);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
wpi::errs() << err.str() << "\n";
|
||||
wpi::errs().flush();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
67
simulation/halsim_ws_client/src/main/native/cpp/main.cpp
Normal file
67
simulation/halsim_ws_client/src/main/native/cpp/main.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 "HALSimWSClient.h"
|
||||
|
||||
using namespace wpilibws;
|
||||
|
||||
static ProviderContainer providers;
|
||||
static HALSimWSProviderSimDevices simDevices(providers);
|
||||
|
||||
extern "C" {
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
|
||||
int HALSIM_InitExtension(void) {
|
||||
wpi::outs() << "HALSim WS Client Extension Initializing\n";
|
||||
|
||||
auto hws = std::make_shared<HALSimWS>(providers, simDevices);
|
||||
HALSimWS::SetInstance(hws);
|
||||
|
||||
if (!hws->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(hws->GetLoop());
|
||||
|
||||
HAL_SetMain(nullptr, HALSimWS::Main, HALSimWS::Exit);
|
||||
|
||||
wpi::outs() << "HALSim WS Client Extension Initialized\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,74 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <WSProviderContainer.h>
|
||||
#include <WSProvider_SimDevice.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/uv/AsyncFunction.h>
|
||||
#include <wpi/uv/Loop.h>
|
||||
#include <wpi/uv/Tcp.h>
|
||||
#include <wpi/uv/Timer.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSClientConnection;
|
||||
|
||||
class HALSimWS {
|
||||
public:
|
||||
static std::shared_ptr<HALSimWS> GetInstance() { return g_instance; }
|
||||
static void SetInstance(std::shared_ptr<HALSimWS> inst) { g_instance = inst; }
|
||||
|
||||
explicit HALSimWS(ProviderContainer& providers,
|
||||
HALSimWSProviderSimDevices& simDevicesProvider)
|
||||
: m_providers(providers), m_simDevicesProvider(simDevicesProvider) {}
|
||||
HALSimWS(const HALSimWS&) = delete;
|
||||
HALSimWS& operator=(const HALSimWS&) = delete;
|
||||
|
||||
bool Initialize();
|
||||
static void Main(void*);
|
||||
static void Exit(void*);
|
||||
|
||||
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
|
||||
void OnNetValueChanged(const wpi::json& msg);
|
||||
|
||||
std::string GetTargetHost() const { return m_host; }
|
||||
std::string GetTargetUri() const { return m_uri; }
|
||||
int GetTargetPort() { return m_port; }
|
||||
std::shared_ptr<wpi::uv::Loop> GetLoop() { return m_loop; }
|
||||
|
||||
private:
|
||||
static std::shared_ptr<HALSimWS> g_instance;
|
||||
|
||||
void MainLoop();
|
||||
|
||||
void AttemptConnect();
|
||||
|
||||
bool m_tcp_connected = false;
|
||||
std::shared_ptr<wpi::uv::Timer> m_connect_timer;
|
||||
int m_connect_attempts = 0;
|
||||
|
||||
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
|
||||
|
||||
ProviderContainer& m_providers;
|
||||
HALSimWSProviderSimDevices& m_simDevicesProvider;
|
||||
|
||||
std::shared_ptr<wpi::uv::Loop> m_loop;
|
||||
std::shared_ptr<wpi::uv::Tcp> m_tcp_client;
|
||||
|
||||
std::string m_host;
|
||||
std::string m_uri;
|
||||
int m_port;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,50 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <HALSimBaseWebSocketConnection.h>
|
||||
#include <wpi/WebSocket.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/uv/AsyncFunction.h>
|
||||
#include <wpi/uv/Buffer.h>
|
||||
#include <wpi/uv/Stream.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWS;
|
||||
|
||||
class HALSimWSClientConnection
|
||||
: public HALSimBaseWebSocketConnection,
|
||||
public std::enable_shared_from_this<HALSimWSClientConnection> {
|
||||
public:
|
||||
using BufferPool = wpi::uv::SimpleBufferPool<4>;
|
||||
using LoopFunc = std::function<void(void)>;
|
||||
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
|
||||
|
||||
explicit HALSimWSClientConnection(std::shared_ptr<wpi::uv::Stream> stream)
|
||||
: m_stream(stream) {}
|
||||
|
||||
public:
|
||||
void OnSimValueChanged(const wpi::json& msg) override;
|
||||
void Initialize();
|
||||
|
||||
private:
|
||||
std::shared_ptr<wpi::uv::Stream> m_stream;
|
||||
|
||||
bool m_ws_connected = false;
|
||||
wpi::WebSocket* m_websocket = nullptr;
|
||||
|
||||
std::shared_ptr<UvExecFunc> m_exec;
|
||||
std::unique_ptr<BufferPool> m_buffers;
|
||||
std::mutex m_buffers_mutex;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
16
simulation/halsim_ws_core/CMakeLists.txt
Normal file
16
simulation/halsim_ws_core/CMakeLists.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
project(halsim_ws_core)
|
||||
|
||||
include(CompileWarnings)
|
||||
|
||||
file(GLOB halsim_ws_core_src src/main/native/cpp/*.cpp)
|
||||
|
||||
add_library(halsim_ws_core STATIC ${halsim_ws_core_src})
|
||||
wpilib_target_warnings(halsim_ws_core)
|
||||
set_target_properties(halsim_ws_core PROPERTIES DEBUG_POSTFIX "d" POSITION_INDEPENDENT_CODE ON)
|
||||
target_link_libraries(halsim_ws_core PUBLIC hal)
|
||||
|
||||
target_include_directories(halsim_ws_core PUBLIC src/main/native/include)
|
||||
|
||||
set_property(TARGET halsim_ws_core PROPERTY FOLDER "libraries")
|
||||
|
||||
install(TARGETS halsim_ws_core EXPORT halsim_ws_core DESTINATION "${main_lib_dest}")
|
||||
58
simulation/halsim_ws_core/build.gradle
Normal file
58
simulation/halsim_ws_core/build.gradle
Normal file
@@ -0,0 +1,58 @@
|
||||
apply plugin: 'cpp'
|
||||
apply plugin: 'edu.wpi.first.NativeUtils'
|
||||
apply plugin: ExtraTasks
|
||||
|
||||
if (!project.hasProperty('onlylinuxathena')) {
|
||||
|
||||
description = "Core library for WebSocket extensions"
|
||||
|
||||
ext {
|
||||
includeWpiutil = true
|
||||
includeNtCore = true
|
||||
pluginName = 'halsim_ws_core'
|
||||
}
|
||||
|
||||
apply plugin: 'google-test-test-suite'
|
||||
|
||||
|
||||
ext {
|
||||
staticGtestConfigs = [:]
|
||||
}
|
||||
|
||||
staticGtestConfigs["${pluginName}Test"] = []
|
||||
apply from: "${rootDir}/shared/googletest.gradle"
|
||||
|
||||
apply from: "${rootDir}/shared/config.gradle"
|
||||
|
||||
model {
|
||||
components {
|
||||
halsim_ws_core(NativeLibrarySpec) {
|
||||
sources {
|
||||
cpp {
|
||||
source {
|
||||
srcDirs = ['src/main/native/cpp']
|
||||
includes = ["**/*.cpp"]
|
||||
}
|
||||
exportedHeaders {
|
||||
srcDirs = ["src/main/native/include"]
|
||||
}
|
||||
}
|
||||
}
|
||||
binaries.all {
|
||||
project(':hal').addHalDependency(it, 'shared')
|
||||
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
|
||||
|
||||
}
|
||||
appendDebugPathToBinaries(binaries)
|
||||
}
|
||||
}
|
||||
binaries {
|
||||
all {
|
||||
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
|
||||
it.buildable = false
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSBaseProvider.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
HALSimWSBaseProvider::HALSimWSBaseProvider(const std::string& key,
|
||||
const std::string& type)
|
||||
: m_key(key), m_type(type) {}
|
||||
|
||||
void HALSimWSBaseProvider::OnNetValueChanged(const wpi::json& json) {
|
||||
// empty
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,40 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSHalProvider::OnNetworkConnected(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> ws) {
|
||||
{
|
||||
// store a weak reference to the websocket object
|
||||
m_ws = ws;
|
||||
}
|
||||
|
||||
RegisterCallbacks();
|
||||
}
|
||||
|
||||
void HALSimWSHalProvider::OnNetworkDisconnected() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSHalProvider::ProcessHalCallback(const wpi::json& payload) {
|
||||
auto ws = m_ws.lock();
|
||||
if (ws) {
|
||||
wpi::json netValue = {
|
||||
{"type", m_type}, {"device", m_deviceId}, {"data", payload}};
|
||||
ws->OnSimValueChanged(netValue);
|
||||
}
|
||||
}
|
||||
|
||||
HALSimWSHalChanProvider::HALSimWSHalChanProvider(int32_t channel,
|
||||
const std::string& key,
|
||||
const std::string& type)
|
||||
: HALSimWSHalProvider(key, type), m_channel(channel) {
|
||||
m_deviceId = std::to_string(channel);
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,128 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_Analog.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/AnalogInData.h>
|
||||
#include <hal/simulation/AnalogOutData.h>
|
||||
|
||||
#define REGISTER_AIN(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterAnalogIn##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderAnalogIn*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
#define REGISTER_AIN_ACCUM(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterAnalogInAccumulator##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderAnalogIn*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
#define REGISTER_AOUT(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterAnalogOut##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderAnalogOut*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderAnalogIn::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderAnalogIn>("AI", HAL_GetNumAnalogInputs(),
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderAnalogIn::~HALSimWSProviderAnalogIn() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderAnalogIn::RegisterCallbacks() {
|
||||
m_initCbKey = REGISTER_AIN(Initialized, "<init", bool, boolean);
|
||||
m_avgbitsCbKey = REGISTER_AIN(AverageBits, "<avg_bits", int32_t, int);
|
||||
m_oversampleCbKey =
|
||||
REGISTER_AIN(OversampleBits, "<oversample_bits", int32_t, int);
|
||||
m_voltageCbKey = REGISTER_AIN(Voltage, ">voltage", double, double);
|
||||
|
||||
m_accumInitCbKey =
|
||||
REGISTER_AIN_ACCUM(Initialized, "<accum_init", bool, boolean);
|
||||
m_accumValueCbKey = REGISTER_AIN_ACCUM(Value, ">accum_value", int64_t,
|
||||
long); // NOLINT(runtime/int)
|
||||
m_accumCountCbKey = REGISTER_AIN_ACCUM(Count, ">accum_count", int64_t,
|
||||
long); // NOLINT(runtime/int)
|
||||
m_accumCenterCbKey =
|
||||
REGISTER_AIN_ACCUM(Center, "<accum_center", int32_t, int);
|
||||
m_accumDeadbandCbKey =
|
||||
REGISTER_AIN_ACCUM(Deadband, "<accum_deadband", int32_t, int);
|
||||
}
|
||||
|
||||
void HALSimWSProviderAnalogIn::CancelCallbacks() {
|
||||
// Cancel callbacks
|
||||
HALSIM_CancelAnalogInInitializedCallback(m_channel, m_initCbKey);
|
||||
HALSIM_CancelAnalogInAverageBitsCallback(m_channel, m_avgbitsCbKey);
|
||||
HALSIM_CancelAnalogInOversampleBitsCallback(m_channel, m_oversampleCbKey);
|
||||
HALSIM_CancelAnalogInVoltageCallback(m_channel, m_voltageCbKey);
|
||||
HALSIM_CancelAnalogInAccumulatorInitializedCallback(m_channel,
|
||||
m_accumInitCbKey);
|
||||
HALSIM_CancelAnalogInAccumulatorValueCallback(m_channel, m_accumValueCbKey);
|
||||
HALSIM_CancelAnalogInAccumulatorCountCallback(m_channel, m_accumCountCbKey);
|
||||
HALSIM_CancelAnalogInAccumulatorCenterCallback(m_channel, m_accumCenterCbKey);
|
||||
HALSIM_CancelAnalogInAccumulatorDeadbandCallback(m_channel,
|
||||
m_accumDeadbandCbKey);
|
||||
|
||||
// Reset callback IDs
|
||||
m_initCbKey = 0;
|
||||
m_avgbitsCbKey = 0;
|
||||
m_oversampleCbKey = 0;
|
||||
m_voltageCbKey = 0;
|
||||
m_accumInitCbKey = 0;
|
||||
m_accumValueCbKey = 0;
|
||||
m_accumCountCbKey = 0;
|
||||
m_accumCenterCbKey = 0;
|
||||
m_accumDeadbandCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderAnalogIn::OnNetValueChanged(const wpi::json& json) {
|
||||
wpi::json::const_iterator it;
|
||||
if ((it = json.find(">voltage")) != json.end()) {
|
||||
HALSIM_SetAnalogInVoltage(m_channel, it.value());
|
||||
}
|
||||
if ((it = json.find(">accum_value")) != json.end()) {
|
||||
HALSIM_SetAnalogInAccumulatorValue(m_channel, it.value());
|
||||
}
|
||||
if ((it = json.find(">accum_count")) != json.end()) {
|
||||
HALSIM_SetAnalogInAccumulatorCount(m_channel, it.value());
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWSProviderAnalogOut::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderAnalogOut>("AO", HAL_GetNumAnalogOutputs(),
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderAnalogOut::~HALSimWSProviderAnalogOut() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderAnalogOut::RegisterCallbacks() {
|
||||
m_initCbKey = REGISTER_AOUT(Initialized, "<init", bool, boolean);
|
||||
m_voltageCbKey = REGISTER_AOUT(Voltage, "<voltage", double, double);
|
||||
}
|
||||
|
||||
void HALSimWSProviderAnalogOut::CancelCallbacks() {
|
||||
HALSIM_CancelAnalogOutInitializedCallback(m_channel, m_initCbKey);
|
||||
HALSIM_CancelAnalogOutVoltageCallback(m_channel, m_voltageCbKey);
|
||||
|
||||
m_initCbKey = 0;
|
||||
m_voltageCbKey = 0;
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,57 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_DIO.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/DIOData.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterDIO##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderDIO*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderDIO::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderDIO>("DIO", HAL_GetNumDigitalChannels(),
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderDIO::~HALSimWSProviderDIO() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderDIO::RegisterCallbacks() {
|
||||
m_initCbKey = REGISTER(Initialized, "<init", bool, boolean);
|
||||
m_valueCbKey = REGISTER(Value, "<>value", bool, boolean);
|
||||
m_pulseLengthCbKey = REGISTER(PulseLength, "<pulse_length", double, double);
|
||||
m_inputCbKey = REGISTER(IsInput, "<input", bool, boolean);
|
||||
}
|
||||
|
||||
void HALSimWSProviderDIO::CancelCallbacks() {
|
||||
HALSIM_CancelDIOInitializedCallback(m_channel, m_initCbKey);
|
||||
HALSIM_CancelDIOValueCallback(m_channel, m_valueCbKey);
|
||||
HALSIM_CancelDIOPulseLengthCallback(m_channel, m_pulseLengthCbKey);
|
||||
HALSIM_CancelDIOIsInputCallback(m_channel, m_inputCbKey);
|
||||
|
||||
m_initCbKey = 0;
|
||||
m_valueCbKey = 0;
|
||||
m_pulseLengthCbKey = 0;
|
||||
m_inputCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderDIO::OnNetValueChanged(const wpi::json& json) {
|
||||
wpi::json::const_iterator it;
|
||||
if ((it = json.find("<>value")) != json.end()) {
|
||||
HALSIM_SetDIOValue(m_channel, static_cast<bool>(it.value()));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,150 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_DriverStation.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <hal/DriverStation.h>
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/DriverStationData.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterDriverStation##halsim##Callback( \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderDriverStation*>(param) \
|
||||
->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderDriverStation::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateSingleProvider<HALSimWSProviderDriverStation>("DriverStation",
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderDriverStation::~HALSimWSProviderDriverStation() {
|
||||
CancelCallbacks();
|
||||
}
|
||||
|
||||
void HALSimWSProviderDriverStation::RegisterCallbacks() {
|
||||
m_enabledCbKey = REGISTER(Enabled, ">enabled", bool, boolean);
|
||||
m_autonomousCbKey = REGISTER(Autonomous, ">autonomous", bool, boolean);
|
||||
m_testCbKey = REGISTER(Test, ">test", bool, boolean);
|
||||
m_estopCbKey = REGISTER(EStop, ">estop", bool, boolean);
|
||||
m_fmsCbKey = REGISTER(FmsAttached, ">fms", bool, boolean);
|
||||
m_dsCbKey = REGISTER(DsAttached, ">ds", bool, boolean);
|
||||
|
||||
// Special case for new data, since the HAL_Value is empty
|
||||
m_newDataCbKey = HALSIM_RegisterDriverStationNewDataCallback(
|
||||
[](const char* name, void* param, const struct HAL_Value* value) {
|
||||
static_cast<HALSimWSProviderDriverStation*>(param)->ProcessHalCallback(
|
||||
{{">new_data", true}});
|
||||
},
|
||||
this, true);
|
||||
|
||||
m_allianceCbKey = HALSIM_RegisterDriverStationAllianceStationIdCallback(
|
||||
[](const char* name, void* param, const struct HAL_Value* value) {
|
||||
std::string station;
|
||||
switch (static_cast<HAL_AllianceStationID>(value->data.v_enum)) {
|
||||
case HAL_AllianceStationID_kRed1:
|
||||
station = "red1";
|
||||
break;
|
||||
case HAL_AllianceStationID_kBlue1:
|
||||
station = "blue1";
|
||||
break;
|
||||
case HAL_AllianceStationID_kRed2:
|
||||
station = "red2";
|
||||
break;
|
||||
case HAL_AllianceStationID_kBlue2:
|
||||
station = "blue2";
|
||||
break;
|
||||
case HAL_AllianceStationID_kRed3:
|
||||
station = "red3";
|
||||
break;
|
||||
case HAL_AllianceStationID_kBlue3:
|
||||
station = "blue3";
|
||||
break;
|
||||
}
|
||||
static_cast<HALSimWSProviderDriverStation*>(param)->ProcessHalCallback(
|
||||
{{">station", station}});
|
||||
},
|
||||
this, true);
|
||||
|
||||
m_matchTimeCbKey = REGISTER(MatchTime, "<match_time", double, double);
|
||||
}
|
||||
|
||||
void HALSimWSProviderDriverStation::CancelCallbacks() {
|
||||
HALSIM_CancelDriverStationEnabledCallback(m_enabledCbKey);
|
||||
HALSIM_CancelDriverStationAutonomousCallback(m_autonomousCbKey);
|
||||
HALSIM_CancelDriverStationTestCallback(m_testCbKey);
|
||||
HALSIM_CancelDriverStationEStopCallback(m_estopCbKey);
|
||||
HALSIM_CancelDriverStationFmsAttachedCallback(m_fmsCbKey);
|
||||
HALSIM_CancelDriverStationDsAttachedCallback(m_dsCbKey);
|
||||
HALSIM_CancelDriverStationNewDataCallback(m_newDataCbKey);
|
||||
HALSIM_CancelDriverStationAllianceStationIdCallback(m_allianceCbKey);
|
||||
HALSIM_CancelDriverStationMatchTimeCallback(m_matchTimeCbKey);
|
||||
|
||||
m_enabledCbKey = 0;
|
||||
m_autonomousCbKey = 0;
|
||||
m_testCbKey = 0;
|
||||
m_estopCbKey = 0;
|
||||
m_fmsCbKey = 0;
|
||||
m_dsCbKey = 0;
|
||||
m_newDataCbKey = 0;
|
||||
m_allianceCbKey = 0;
|
||||
m_matchTimeCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderDriverStation::OnNetValueChanged(const wpi::json& json) {
|
||||
wpi::json::const_iterator it;
|
||||
if ((it = json.find(">enabled")) != json.end()) {
|
||||
HALSIM_SetDriverStationEnabled(it.value());
|
||||
}
|
||||
if ((it = json.find(">autonomous")) != json.end()) {
|
||||
HALSIM_SetDriverStationAutonomous(it.value());
|
||||
}
|
||||
if ((it = json.find(">test")) != json.end()) {
|
||||
HALSIM_SetDriverStationTest(it.value());
|
||||
}
|
||||
if ((it = json.find(">estop")) != json.end()) {
|
||||
HALSIM_SetDriverStationEStop(it.value());
|
||||
}
|
||||
if ((it = json.find(">fms")) != json.end()) {
|
||||
HALSIM_SetDriverStationFmsAttached(it.value());
|
||||
}
|
||||
if ((it = json.find(">ds")) != json.end()) {
|
||||
HALSIM_SetDriverStationDsAttached(it.value());
|
||||
}
|
||||
|
||||
if ((it = json.find(">station")) != json.end()) {
|
||||
auto& station = it.value().get_ref<const std::string&>();
|
||||
if (station == "red1") {
|
||||
HALSIM_SetDriverStationAllianceStationId(HAL_AllianceStationID_kRed1);
|
||||
} else if (station == "red2") {
|
||||
HALSIM_SetDriverStationAllianceStationId(HAL_AllianceStationID_kRed2);
|
||||
} else if (station == "red3") {
|
||||
HALSIM_SetDriverStationAllianceStationId(HAL_AllianceStationID_kRed3);
|
||||
} else if (station == "blue1") {
|
||||
HALSIM_SetDriverStationAllianceStationId(HAL_AllianceStationID_kBlue1);
|
||||
} else if (station == "blue2") {
|
||||
HALSIM_SetDriverStationAllianceStationId(HAL_AllianceStationID_kBlue2);
|
||||
} else if (station == "blue3") {
|
||||
HALSIM_SetDriverStationAllianceStationId(HAL_AllianceStationID_kBlue3);
|
||||
}
|
||||
}
|
||||
|
||||
// Only notify usercode if we get the new data message
|
||||
if ((it = json.find(">new_data")) != json.end()) {
|
||||
HALSIM_NotifyDriverStationNewData();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,87 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_Encoder.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/EncoderData.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterEncoder##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderEncoder*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderEncoder::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderEncoder>("Encoder", HAL_GetNumEncoders(),
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderEncoder::~HALSimWSProviderEncoder() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderEncoder::RegisterCallbacks() {
|
||||
// Special case for initialization since we will need to send
|
||||
// the digital channels down the line as well
|
||||
m_initCbKey = HALSIM_RegisterEncoderInitializedCallback(
|
||||
m_channel,
|
||||
[](const char* name, void* param, const struct HAL_Value* value) {
|
||||
auto provider = static_cast<HALSimWSProviderEncoder*>(param);
|
||||
bool init = static_cast<bool>(value->data.v_boolean);
|
||||
|
||||
wpi::json payload = {{"<init", init}};
|
||||
|
||||
if (init) {
|
||||
payload["<channel_a"] =
|
||||
HALSIM_GetEncoderDigitalChannelA(provider->GetChannel());
|
||||
payload["<channel_b"] =
|
||||
HALSIM_GetEncoderDigitalChannelB(provider->GetChannel());
|
||||
}
|
||||
|
||||
provider->ProcessHalCallback(payload);
|
||||
},
|
||||
this, true);
|
||||
m_countCbKey = REGISTER(Count, ">count", int32_t, int);
|
||||
m_periodCbKey = REGISTER(Period, ">period", double, double);
|
||||
m_resetCbKey = REGISTER(Reset, "<reset", bool, boolean);
|
||||
m_reverseDirectionCbKey =
|
||||
REGISTER(ReverseDirection, "<reverse_direction", bool, boolean);
|
||||
m_samplesCbKey = REGISTER(SamplesToAverage, "<samples_to_avg", int32_t, int);
|
||||
}
|
||||
|
||||
void HALSimWSProviderEncoder::CancelCallbacks() {
|
||||
HALSIM_CancelEncoderInitializedCallback(m_channel, m_initCbKey);
|
||||
HALSIM_CancelEncoderCountCallback(m_channel, m_countCbKey);
|
||||
HALSIM_CancelEncoderPeriodCallback(m_channel, m_periodCbKey);
|
||||
HALSIM_CancelEncoderResetCallback(m_channel, m_resetCbKey);
|
||||
HALSIM_CancelEncoderReverseDirectionCallback(m_channel,
|
||||
m_reverseDirectionCbKey);
|
||||
HALSIM_CancelEncoderSamplesToAverageCallback(m_channel, m_samplesCbKey);
|
||||
|
||||
m_initCbKey = 0;
|
||||
m_countCbKey = 0;
|
||||
m_periodCbKey = 0;
|
||||
m_resetCbKey = 0;
|
||||
m_reverseDirectionCbKey = 0;
|
||||
m_samplesCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderEncoder::OnNetValueChanged(const wpi::json& json) {
|
||||
wpi::json::const_iterator it;
|
||||
if ((it = json.find(">count")) != json.end()) {
|
||||
HALSIM_SetEncoderCount(m_channel, it.value());
|
||||
}
|
||||
if ((it = json.find(">period")) != json.end()) {
|
||||
HALSIM_SetEncoderPeriod(m_channel, it.value());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,110 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_Joystick.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/DriverStationData.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderJoystick::Initialize(WSRegisterFunc webregisterFunc) {
|
||||
CreateProviders<HALSimWSProviderJoystick>("Joystick", HAL_kMaxJoysticks,
|
||||
webregisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderJoystick::~HALSimWSProviderJoystick() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderJoystick::RegisterCallbacks() {
|
||||
m_dsNewDataCbKey = HALSIM_RegisterDriverStationNewDataCallback(
|
||||
[](const char* name, void* param, const struct HAL_Value* value) {
|
||||
auto provider = static_cast<HALSimWSProviderJoystick*>(param);
|
||||
|
||||
// Grab all joystick data and send it at once
|
||||
wpi::json payload;
|
||||
|
||||
// Axes data
|
||||
HAL_JoystickAxes axes{};
|
||||
std::vector<double> axesValues;
|
||||
HALSIM_GetJoystickAxes(provider->GetChannel(), &axes);
|
||||
|
||||
for (int i = 0; i < axes.count; i++) {
|
||||
axesValues.push_back(axes.axes[i]);
|
||||
}
|
||||
|
||||
// POVs data
|
||||
HAL_JoystickPOVs povs{};
|
||||
std::vector<int16_t> povsValues;
|
||||
HALSIM_GetJoystickPOVs(provider->GetChannel(), &povs);
|
||||
|
||||
for (int i = 0; i < povs.count; i++) {
|
||||
povsValues.push_back(povs.povs[i]);
|
||||
}
|
||||
|
||||
// Button data
|
||||
HAL_JoystickButtons buttons{};
|
||||
std::vector<bool> buttonsValues;
|
||||
|
||||
for (int i = 0; i < buttons.count; i++) {
|
||||
buttonsValues.push_back(((buttons.buttons >> i) & 0x1) == 1);
|
||||
}
|
||||
|
||||
payload[">axes"] = axesValues;
|
||||
payload[">povs"] = povsValues;
|
||||
payload[">buttons"] = buttonsValues;
|
||||
|
||||
provider->ProcessHalCallback(payload);
|
||||
},
|
||||
this, true);
|
||||
}
|
||||
|
||||
void HALSimWSProviderJoystick::CancelCallbacks() {
|
||||
HALSIM_CancelDriverStationNewDataCallback(m_dsNewDataCbKey);
|
||||
|
||||
m_dsNewDataCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderJoystick::OnNetValueChanged(const wpi::json& json) {
|
||||
wpi::json::const_iterator it;
|
||||
if ((it = json.find(">axes")) != json.end()) {
|
||||
HAL_JoystickAxes axes{};
|
||||
axes.count =
|
||||
std::min(it.value().size(), (wpi::json::size_type)HAL_kMaxJoystickAxes);
|
||||
for (int i = 0; i < axes.count; i++) {
|
||||
axes.axes[i] = it.value()[i];
|
||||
}
|
||||
|
||||
HALSIM_SetJoystickAxes(m_channel, &axes);
|
||||
}
|
||||
|
||||
if ((it = json.find(">buttons")) != json.end()) {
|
||||
HAL_JoystickButtons buttons{};
|
||||
buttons.count = std::min(it.value().size(), (wpi::json::size_type)32);
|
||||
for (int i = 0; i < buttons.count; i++) {
|
||||
if (it.value()[i]) {
|
||||
buttons.buttons |= 1 << (i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
HALSIM_SetJoystickButtons(m_channel, &buttons);
|
||||
}
|
||||
|
||||
if ((it = json.find(">povs")) != json.end()) {
|
||||
HAL_JoystickPOVs povs{};
|
||||
povs.count =
|
||||
std::min(it.value().size(), (wpi::json::size_type)HAL_kMaxJoystickPOVs);
|
||||
for (int i = 0; i < povs.count; i++) {
|
||||
povs.povs[i] = it.value()[i];
|
||||
}
|
||||
|
||||
HALSIM_SetJoystickPOVs(m_channel, &povs);
|
||||
}
|
||||
// We won't send any updates to user code until the new data message
|
||||
// is received by the driver station provider
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,55 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_PWM.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/PWMData.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterPWM##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderPWM*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderPWM::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderPWM>("PWM", HAL_GetNumPWMChannels(),
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderPWM::~HALSimWSProviderPWM() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderPWM::RegisterCallbacks() {
|
||||
m_initCbKey = REGISTER(Initialized, "<init", bool, boolean);
|
||||
m_speedCbKey = REGISTER(Speed, "<speed", double, double);
|
||||
m_positionCbKey = REGISTER(Position, "<position", double, double);
|
||||
m_rawCbKey = REGISTER(RawValue, "<raw", int32_t, int);
|
||||
m_periodScaleCbKey = REGISTER(PeriodScale, "<period_scale", int32_t, int);
|
||||
m_zeroLatchCbKey = REGISTER(ZeroLatch, "<zero_latch", bool, boolean);
|
||||
}
|
||||
|
||||
void HALSimWSProviderPWM::CancelCallbacks() {
|
||||
HALSIM_CancelPWMInitializedCallback(m_channel, m_initCbKey);
|
||||
HALSIM_CancelPWMSpeedCallback(m_channel, m_speedCbKey);
|
||||
HALSIM_CancelPWMPositionCallback(m_channel, m_positionCbKey);
|
||||
HALSIM_CancelPWMRawValueCallback(m_channel, m_rawCbKey);
|
||||
HALSIM_CancelPWMPeriodScaleCallback(m_channel, m_periodScaleCbKey);
|
||||
HALSIM_CancelPWMZeroLatchCallback(m_channel, m_zeroLatchCbKey);
|
||||
|
||||
m_initCbKey = 0;
|
||||
m_speedCbKey = 0;
|
||||
m_positionCbKey = 0;
|
||||
m_rawCbKey = 0;
|
||||
m_periodScaleCbKey = 0;
|
||||
m_zeroLatchCbKey = 0;
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,49 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_Relay.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/RelayData.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterRelay##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderRelay*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
void HALSimWSProviderRelay::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderRelay>("Relay", HAL_GetNumRelayHeaders(),
|
||||
webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderRelay::~HALSimWSProviderRelay() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderRelay::RegisterCallbacks() {
|
||||
m_initFwdCbKey = REGISTER(InitializedForward, "<init_fwd", bool, boolean);
|
||||
m_initRevCbKey = REGISTER(InitializedReverse, "<init_rev", bool, boolean);
|
||||
m_fwdCbKey = REGISTER(Forward, "<fwd", bool, boolean);
|
||||
m_revCbKey = REGISTER(Reverse, "<rev", bool, boolean);
|
||||
}
|
||||
|
||||
void HALSimWSProviderRelay::CancelCallbacks() {
|
||||
HALSIM_CancelRelayInitializedForwardCallback(m_channel, m_initFwdCbKey);
|
||||
HALSIM_CancelRelayInitializedReverseCallback(m_channel, m_initRevCbKey);
|
||||
HALSIM_CancelRelayForwardCallback(m_channel, m_fwdCbKey);
|
||||
HALSIM_CancelRelayReverseCallback(m_channel, m_revCbKey);
|
||||
|
||||
m_initFwdCbKey = 0;
|
||||
m_initRevCbKey = 0;
|
||||
m_fwdCbKey = 0;
|
||||
m_revCbKey = 0;
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,140 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_RoboRIO.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/RoboRioData.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterRoboRio##halsim##Callback( \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderRoboRIO*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderRoboRIO::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateSingleProvider<HALSimWSProviderRoboRIO>("RoboRIO", webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderRoboRIO::~HALSimWSProviderRoboRIO() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderRoboRIO::RegisterCallbacks() {
|
||||
m_fpgaCbKey = REGISTER(FPGAButton, ">fpga_button", bool, boolean);
|
||||
m_vinVoltageCbKey = REGISTER(VInVoltage, ">vin_voltage", double, double);
|
||||
m_vinCurrentCbKey = REGISTER(VInCurrent, ">vin_current", double, double);
|
||||
|
||||
m_6vVoltageCbKey = REGISTER(UserVoltage6V, ">6v_voltage", double, double);
|
||||
m_6vCurrentCbKey = REGISTER(UserCurrent6V, ">6v_current", double, double);
|
||||
m_6vActiveCbKey = REGISTER(UserActive6V, ">6v_active", bool, boolean);
|
||||
m_6vFaultsCbKey = REGISTER(UserFaults6V, ">6v_faults", int32_t, int);
|
||||
|
||||
m_5vVoltageCbKey = REGISTER(UserVoltage5V, ">5v_voltage", double, double);
|
||||
m_5vCurrentCbKey = REGISTER(UserCurrent5V, ">5v_current", double, double);
|
||||
m_5vActiveCbKey = REGISTER(UserActive5V, ">5v_active", bool, boolean);
|
||||
m_5vFaultsCbKey = REGISTER(UserFaults5V, ">5v_faults", int32_t, int);
|
||||
|
||||
m_3v3VoltageCbKey = REGISTER(UserVoltage3V3, ">3v3_voltage", double, double);
|
||||
m_3v3CurrentCbKey = REGISTER(UserCurrent3V3, ">3v3_current", double, double);
|
||||
m_3v3ActiveCbKey = REGISTER(UserActive3V3, ">3v3_active", bool, boolean);
|
||||
m_3v3FaultsCbKey = REGISTER(UserFaults3V3, ">3v3_faults", int32_t, int);
|
||||
}
|
||||
|
||||
void HALSimWSProviderRoboRIO::CancelCallbacks() {
|
||||
HALSIM_CancelRoboRioFPGAButtonCallback(m_fpgaCbKey);
|
||||
HALSIM_CancelRoboRioVInVoltageCallback(m_vinVoltageCbKey);
|
||||
HALSIM_CancelRoboRioVInCurrentCallback(m_vinCurrentCbKey);
|
||||
|
||||
HALSIM_CancelRoboRioUserVoltage6VCallback(m_6vVoltageCbKey);
|
||||
HALSIM_CancelRoboRioUserCurrent6VCallback(m_6vCurrentCbKey);
|
||||
HALSIM_CancelRoboRioUserActive6VCallback(m_6vActiveCbKey);
|
||||
HALSIM_CancelRoboRioUserFaults6VCallback(m_6vFaultsCbKey);
|
||||
|
||||
HALSIM_CancelRoboRioUserVoltage5VCallback(m_5vVoltageCbKey);
|
||||
HALSIM_CancelRoboRioUserCurrent5VCallback(m_5vCurrentCbKey);
|
||||
HALSIM_CancelRoboRioUserActive5VCallback(m_5vActiveCbKey);
|
||||
HALSIM_CancelRoboRioUserFaults5VCallback(m_5vFaultsCbKey);
|
||||
|
||||
HALSIM_CancelRoboRioUserVoltage3V3Callback(m_3v3VoltageCbKey);
|
||||
HALSIM_CancelRoboRioUserCurrent3V3Callback(m_3v3CurrentCbKey);
|
||||
HALSIM_CancelRoboRioUserActive3V3Callback(m_3v3ActiveCbKey);
|
||||
HALSIM_CancelRoboRioUserFaults3V3Callback(m_3v3FaultsCbKey);
|
||||
|
||||
m_fpgaCbKey = 0;
|
||||
m_vinVoltageCbKey = 0;
|
||||
m_vinCurrentCbKey = 0;
|
||||
m_6vActiveCbKey = 0;
|
||||
m_6vCurrentCbKey = 0;
|
||||
m_6vFaultsCbKey = 0;
|
||||
m_6vVoltageCbKey = 0;
|
||||
m_5vActiveCbKey = 0;
|
||||
m_5vCurrentCbKey = 0;
|
||||
m_5vFaultsCbKey = 0;
|
||||
m_5vVoltageCbKey = 0;
|
||||
m_3v3ActiveCbKey = 0;
|
||||
m_3v3CurrentCbKey = 0;
|
||||
m_3v3FaultsCbKey = 0;
|
||||
m_3v3VoltageCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderRoboRIO::OnNetValueChanged(const wpi::json& json) {
|
||||
wpi::json::const_iterator it;
|
||||
if ((it = json.find(">fpga_button")) != json.end()) {
|
||||
HALSIM_SetRoboRioFPGAButton(static_cast<bool>(it.value()));
|
||||
}
|
||||
|
||||
if ((it = json.find(">vin_voltage")) != json.end()) {
|
||||
HALSIM_SetRoboRioVInVoltage(it.value());
|
||||
}
|
||||
if ((it = json.find(">vin_current")) != json.end()) {
|
||||
HALSIM_SetRoboRioVInCurrent(it.value());
|
||||
}
|
||||
|
||||
if ((it = json.find(">6v_voltage")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserVoltage6V(it.value());
|
||||
}
|
||||
if ((it = json.find(">6v_current")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserCurrent6V(it.value());
|
||||
}
|
||||
if ((it = json.find(">6v_active")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserActive6V(static_cast<bool>(it.value()));
|
||||
}
|
||||
if ((it = json.find(">6v_faults")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserFaults6V(it.value());
|
||||
}
|
||||
|
||||
if ((it = json.find(">5v_voltage")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserVoltage5V(it.value());
|
||||
}
|
||||
if ((it = json.find(">5v_current")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserCurrent5V(it.value());
|
||||
}
|
||||
if ((it = json.find(">5v_active")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserActive5V(static_cast<bool>(it.value()));
|
||||
}
|
||||
if ((it = json.find(">5v_faults")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserFaults5V(it.value());
|
||||
}
|
||||
|
||||
if ((it = json.find(">3v3_voltage")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserVoltage3V3(it.value());
|
||||
}
|
||||
if ((it = json.find(">3v3_current")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserCurrent3V3(it.value());
|
||||
}
|
||||
if ((it = json.find(">3v3_active")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserActive3V3(static_cast<bool>(it.value()));
|
||||
}
|
||||
if ((it = json.find(">3v3_faults")) != json.end()) {
|
||||
HALSIM_SetRoboRioUserFaults3V3(it.value());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,197 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_SimDevice.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
HALSimWSProviderSimDevice::~HALSimWSProviderSimDevice() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderSimDevice::OnNetworkConnected(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> ws) {
|
||||
auto storedWS = m_ws.lock();
|
||||
|
||||
if (ws == storedWS) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, run the disconnection code first so we get
|
||||
// back to a clean slate (only if the stored websocket is
|
||||
// not null)
|
||||
if (storedWS) {
|
||||
OnNetworkDisconnected();
|
||||
}
|
||||
|
||||
m_ws = ws;
|
||||
|
||||
m_simValueCreatedCbKey = HALSIM_RegisterSimValueCreatedCallback(
|
||||
m_handle, this, HALSimWSProviderSimDevice::OnValueCreatedStatic, 1);
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevice::OnNetworkDisconnected() {
|
||||
// Cancel all callbacks
|
||||
CancelCallbacks();
|
||||
|
||||
m_ws.reset();
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevice::CancelCallbacks() {
|
||||
HALSIM_CancelSimValueCreatedCallback(m_simValueCreatedCbKey);
|
||||
|
||||
m_simValueCreatedCbKey = 0;
|
||||
|
||||
for (auto& kv : m_simValueChangedCbKeys) {
|
||||
HALSIM_CancelSimValueChangedCallback(kv.getValue());
|
||||
}
|
||||
|
||||
m_simValueChangedCbKeys.clear();
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevice::OnNetValueChanged(const wpi::json& json) {
|
||||
auto it = json.cbegin();
|
||||
auto end = json.cend();
|
||||
|
||||
std::shared_lock lock(m_vhLock);
|
||||
for (; it != end; ++it) {
|
||||
auto vd = m_valueHandles.find(it.key());
|
||||
if (vd != m_valueHandles.end()) {
|
||||
HAL_Value value;
|
||||
value.type = vd->second->valueType;
|
||||
switch (value.type) {
|
||||
case HAL_BOOLEAN:
|
||||
value.data.v_boolean = static_cast<bool>(it.value()) ? 1 : 0;
|
||||
break;
|
||||
case HAL_DOUBLE:
|
||||
value.data.v_double = it.value();
|
||||
break;
|
||||
case HAL_ENUM:
|
||||
value.data.v_enum = it.value();
|
||||
break;
|
||||
case HAL_INT:
|
||||
value.data.v_int = it.value();
|
||||
break;
|
||||
case HAL_LONG:
|
||||
value.data.v_long = it.value();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
HAL_SetSimValue(vd->second->handle, &value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevice::OnValueCreated(const char* name,
|
||||
HAL_SimValueHandle handle,
|
||||
HAL_Bool readonly,
|
||||
const struct HAL_Value* value) {
|
||||
wpi::Twine key = wpi::Twine(readonly ? "<" : "<>") + name;
|
||||
auto data = std::make_unique<SimDeviceValueData>();
|
||||
data->device = this;
|
||||
data->handle = handle;
|
||||
data->key = key.str();
|
||||
data->valueType = value->type;
|
||||
|
||||
auto param = data.get();
|
||||
|
||||
{
|
||||
std::unique_lock lock(m_vhLock);
|
||||
m_valueHandles[data->key] = std::move(data);
|
||||
}
|
||||
|
||||
int32_t cbKey = HALSIM_RegisterSimValueChangedCallback(
|
||||
handle, param, HALSimWSProviderSimDevice::OnValueChangedStatic, true);
|
||||
|
||||
m_simValueChangedCbKeys[key.str()] = cbKey;
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevice::OnValueChanged(SimDeviceValueData* valueData,
|
||||
const struct HAL_Value* value) {
|
||||
auto ws = m_ws.lock();
|
||||
if (ws) {
|
||||
switch (value->type) {
|
||||
case HAL_BOOLEAN:
|
||||
ProcessHalCallback({{valueData->key, value->data.v_boolean}});
|
||||
break;
|
||||
case HAL_DOUBLE:
|
||||
ProcessHalCallback({{valueData->key, value->data.v_double}});
|
||||
break;
|
||||
case HAL_ENUM:
|
||||
ProcessHalCallback({{valueData->key, value->data.v_enum}});
|
||||
break;
|
||||
case HAL_INT:
|
||||
ProcessHalCallback({{valueData->key, value->data.v_int}});
|
||||
break;
|
||||
case HAL_LONG:
|
||||
ProcessHalCallback({{valueData->key, value->data.v_long}});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevice::ProcessHalCallback(const wpi::json& payload) {
|
||||
auto ws = m_ws.lock();
|
||||
if (ws) {
|
||||
wpi::json netValue = {
|
||||
{"type", "SimDevices"}, {"device", m_deviceId}, {"data", payload}};
|
||||
ws->OnSimValueChanged(netValue);
|
||||
}
|
||||
}
|
||||
|
||||
HALSimWSProviderSimDevices::~HALSimWSProviderSimDevices() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderSimDevices::DeviceCreatedCallback(
|
||||
const char* name, HAL_SimDeviceHandle handle) {
|
||||
auto key = (wpi::Twine("SimDevices/") + name).str();
|
||||
auto dev = std::make_shared<HALSimWSProviderSimDevice>(
|
||||
handle, key, wpi::Twine(name).str());
|
||||
m_providers.Add(key, dev);
|
||||
|
||||
if (m_ws) {
|
||||
m_exec->Call([this, dev]() { dev->OnNetworkConnected(GetWSConnection()); });
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevices::DeviceFreedCallback(
|
||||
const char* name, HAL_SimDeviceHandle handle) {
|
||||
m_providers.Delete(name);
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevices::Initialize(
|
||||
std::shared_ptr<wpi::uv::Loop> loop) {
|
||||
m_deviceCreatedCbKey = HALSIM_RegisterSimDeviceCreatedCallback(
|
||||
"", this, HALSimWSProviderSimDevices::DeviceCreatedCallbackStatic, 1);
|
||||
m_deviceFreedCbKey = HALSIM_RegisterSimDeviceFreedCallback(
|
||||
"", this, HALSimWSProviderSimDevices::DeviceFreedCallbackStatic);
|
||||
|
||||
m_exec = UvExecFn::Create(loop, [](auto out, LoopFn func) {
|
||||
func();
|
||||
out.set_value();
|
||||
});
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevices::CancelCallbacks() {
|
||||
HALSIM_CancelSimDeviceCreatedCallback(m_deviceCreatedCbKey);
|
||||
HALSIM_CancelSimDeviceFreedCallback(m_deviceFreedCbKey);
|
||||
|
||||
m_deviceCreatedCbKey = 0;
|
||||
m_deviceFreedCbKey = 0;
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevices::OnNetworkConnected(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
|
||||
m_ws = hws;
|
||||
}
|
||||
|
||||
void HALSimWSProviderSimDevices::OnNetworkDisconnected() { m_ws = nullptr; }
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,47 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "WSProvider_dPWM.h"
|
||||
|
||||
#include <hal/Ports.h>
|
||||
#include <hal/simulation/DigitalPWMData.h>
|
||||
|
||||
#define REGISTER(halsim, jsonid, ctype, haltype) \
|
||||
HALSIM_RegisterDigitalPWM##halsim##Callback( \
|
||||
m_channel, \
|
||||
[](const char* name, void* param, const struct HAL_Value* value) { \
|
||||
static_cast<HALSimWSProviderDigitalPWM*>(param)->ProcessHalCallback( \
|
||||
{{jsonid, static_cast<ctype>(value->data.v_##haltype)}}); \
|
||||
}, \
|
||||
this, true)
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
void HALSimWSProviderDigitalPWM::Initialize(WSRegisterFunc webRegisterFunc) {
|
||||
CreateProviders<HALSimWSProviderDigitalPWM>(
|
||||
"dPWM", HAL_GetNumDigitalPWMOutputs(), webRegisterFunc);
|
||||
}
|
||||
|
||||
HALSimWSProviderDigitalPWM::~HALSimWSProviderDigitalPWM() { CancelCallbacks(); }
|
||||
|
||||
void HALSimWSProviderDigitalPWM::RegisterCallbacks() {
|
||||
m_initCbKey = REGISTER(Initialized, "<init", bool, boolean);
|
||||
m_dutyCycleCbKey = REGISTER(DutyCycle, "<duty_cycle", double, double);
|
||||
m_pinCbKey = REGISTER(Pin, "<dio_pin", int32_t, int);
|
||||
}
|
||||
|
||||
void HALSimWSProviderDigitalPWM::CancelCallbacks() {
|
||||
HALSIM_CancelDigitalPWMInitializedCallback(m_channel, m_initCbKey);
|
||||
HALSIM_CancelDigitalPWMDutyCycleCallback(m_channel, m_dutyCycleCbKey);
|
||||
HALSIM_CancelDigitalPWMPinCallback(m_channel, m_pinCbKey);
|
||||
|
||||
m_initCbKey = 0;
|
||||
m_dutyCycleCbKey = 0;
|
||||
m_pinCbKey = 0;
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,24 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <wpi/json.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimBaseWebSocketConnection {
|
||||
public:
|
||||
virtual void OnSimValueChanged(const wpi::json& msg) = 0;
|
||||
|
||||
protected:
|
||||
virtual ~HALSimBaseWebSocketConnection() = default;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,53 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <wpi/json.h>
|
||||
|
||||
#include "HALSimBaseWebSocketConnection.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSBaseProvider {
|
||||
public:
|
||||
explicit HALSimWSBaseProvider(const std::string& key,
|
||||
const std::string& type = "");
|
||||
virtual ~HALSimWSBaseProvider() {}
|
||||
|
||||
HALSimWSBaseProvider(const HALSimWSBaseProvider&) = delete;
|
||||
HALSimWSBaseProvider& operator=(const HALSimWSBaseProvider&) = delete;
|
||||
|
||||
// Called when the websocket connects. This will cause providers
|
||||
// to register their HAL callbacks
|
||||
virtual void OnNetworkConnected(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> ws) = 0;
|
||||
|
||||
// Called when the websocket disconnects. This will cause provider
|
||||
// to cancel their HAL callbacks
|
||||
virtual void OnNetworkDisconnected() = 0;
|
||||
|
||||
// network -> sim
|
||||
virtual void OnNetValueChanged(const wpi::json& json);
|
||||
|
||||
const std::string GetDeviceType() { return m_type; }
|
||||
const std::string GetDeviceId() { return m_deviceId; }
|
||||
|
||||
protected:
|
||||
// sim -> network
|
||||
std::weak_ptr<HALSimBaseWebSocketConnection> m_ws;
|
||||
std::string m_key;
|
||||
|
||||
std::string m_type;
|
||||
std::string m_deviceId = "";
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,68 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <hal/simulation/NotifyListener.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "WSBaseProvider.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
typedef void (*HALCbRegisterIndexedFunc)(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
typedef void (*HALCbRegisterSingleFunc)(HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
// provider generates diffs based on values
|
||||
class HALSimWSHalProvider : public HALSimWSBaseProvider {
|
||||
public:
|
||||
using HALSimWSBaseProvider::HALSimWSBaseProvider;
|
||||
|
||||
void OnNetworkConnected(std::shared_ptr<HALSimBaseWebSocketConnection> ws);
|
||||
void OnNetworkDisconnected();
|
||||
|
||||
void ProcessHalCallback(const wpi::json& payload);
|
||||
|
||||
protected:
|
||||
virtual void RegisterCallbacks() = 0;
|
||||
virtual void CancelCallbacks() = 0;
|
||||
};
|
||||
|
||||
// provider generates per-channel diffs
|
||||
class HALSimWSHalChanProvider : public HALSimWSHalProvider {
|
||||
public:
|
||||
explicit HALSimWSHalChanProvider(int32_t channel, const std::string& key,
|
||||
const std::string& type);
|
||||
|
||||
int32_t GetChannel() { return m_channel; }
|
||||
|
||||
protected:
|
||||
int32_t m_channel;
|
||||
};
|
||||
|
||||
using WSRegisterFunc = std::function<void(
|
||||
const std::string&, std::shared_ptr<HALSimWSBaseProvider>)>;
|
||||
|
||||
template <typename T>
|
||||
void CreateProviders(const std::string& prefix, int32_t numChannels,
|
||||
WSRegisterFunc webRegisterFunc);
|
||||
|
||||
template <typename T>
|
||||
void CreateSingleProvider(const std::string& key,
|
||||
WSRegisterFunc webRegisterFunc);
|
||||
|
||||
#include "WSHalProviders.inl"
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,29 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
template <typename T>
|
||||
void CreateProviders(const std::string& prefix, int numChannels,
|
||||
WSRegisterFunc webRegisterFunc) {
|
||||
for (int32_t i = 0; i < numChannels; i++) {
|
||||
auto key = (prefix + "/" + wpi::Twine(i)).str();
|
||||
auto ptr = std::make_unique<T>(i, key, prefix);
|
||||
webRegisterFunc(key, std::move(ptr));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void CreateSingleProvider(const std::string& key,
|
||||
WSRegisterFunc webRegisterFunc) {
|
||||
auto ptr = std::make_unique<T>(key, key);
|
||||
webRegisterFunc(key, std::move(ptr));
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <shared_mutex>
|
||||
|
||||
#include <wpi/StringMap.h>
|
||||
|
||||
#include "WSBaseProvider.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class ProviderContainer {
|
||||
public:
|
||||
using ProviderPtr = std::shared_ptr<HALSimWSBaseProvider>;
|
||||
using IterFn = std::function<void(ProviderPtr)>;
|
||||
|
||||
ProviderContainer() {}
|
||||
|
||||
ProviderContainer(const ProviderContainer&) = delete;
|
||||
ProviderContainer& operator=(const ProviderContainer&) = delete;
|
||||
|
||||
void Add(wpi::StringRef key, std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_providers[key] = provider;
|
||||
}
|
||||
|
||||
void Delete(wpi::StringRef key) {
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_providers.erase(key);
|
||||
}
|
||||
|
||||
void ForEach(IterFn fn) {
|
||||
std::shared_lock lock(m_mutex);
|
||||
for (auto& kv : m_providers) {
|
||||
fn(kv.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
ProviderPtr Get(wpi::StringRef key) {
|
||||
std::shared_lock lock(m_mutex);
|
||||
auto fiter = m_providers.find(key);
|
||||
return fiter != m_providers.end() ? fiter->second : ProviderPtr();
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_mutex m_mutex;
|
||||
wpi::StringMap<std::shared_ptr<HALSimWSBaseProvider>> m_providers;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,57 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderAnalogIn : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderAnalogIn();
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initCbKey = 0;
|
||||
int32_t m_avgbitsCbKey = 0;
|
||||
int32_t m_oversampleCbKey = 0;
|
||||
int32_t m_voltageCbKey = 0;
|
||||
int32_t m_accumInitCbKey = 0;
|
||||
int32_t m_accumValueCbKey = 0;
|
||||
int32_t m_accumCountCbKey = 0;
|
||||
int32_t m_accumCenterCbKey = 0;
|
||||
int32_t m_accumDeadbandCbKey = 0;
|
||||
};
|
||||
|
||||
class HALSimWSProviderAnalogOut : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderAnalogOut();
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initCbKey = 0;
|
||||
int32_t m_voltageCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderDIO : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderDIO();
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initCbKey = 0;
|
||||
int32_t m_valueCbKey = 0;
|
||||
int32_t m_pulseLengthCbKey = 0;
|
||||
int32_t m_inputCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,41 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderDriverStation : public HALSimWSHalProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalProvider::HALSimWSHalProvider;
|
||||
~HALSimWSProviderDriverStation();
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_enabledCbKey = 0;
|
||||
int32_t m_autonomousCbKey = 0;
|
||||
int32_t m_testCbKey = 0;
|
||||
int32_t m_estopCbKey = 0;
|
||||
int32_t m_fmsCbKey = 0;
|
||||
int32_t m_dsCbKey = 0;
|
||||
int32_t m_allianceCbKey = 0;
|
||||
int32_t m_matchTimeCbKey = 0;
|
||||
int32_t m_newDataCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,38 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderEncoder : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderEncoder();
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initCbKey = 0;
|
||||
int32_t m_countCbKey = 0;
|
||||
int32_t m_periodCbKey = 0;
|
||||
int32_t m_resetCbKey = 0;
|
||||
int32_t m_reverseDirectionCbKey = 0;
|
||||
int32_t m_samplesCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,33 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderJoystick : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderJoystick();
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_dsNewDataCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderPWM : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderPWM();
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initCbKey = 0;
|
||||
int32_t m_speedCbKey = 0;
|
||||
int32_t m_positionCbKey = 0;
|
||||
int32_t m_rawCbKey = 0;
|
||||
int32_t m_periodScaleCbKey = 0;
|
||||
int32_t m_zeroLatchCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,34 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderRelay : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderRelay();
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initFwdCbKey = 0;
|
||||
int32_t m_initRevCbKey = 0;
|
||||
int32_t m_fwdCbKey = 0;
|
||||
int32_t m_revCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,47 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderRoboRIO : public HALSimWSHalProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalProvider::HALSimWSHalProvider;
|
||||
~HALSimWSProviderRoboRIO();
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_fpgaCbKey = 0;
|
||||
int32_t m_vinVoltageCbKey = 0;
|
||||
int32_t m_vinCurrentCbKey = 0;
|
||||
int32_t m_6vVoltageCbKey = 0;
|
||||
int32_t m_6vCurrentCbKey = 0;
|
||||
int32_t m_6vActiveCbKey = 0;
|
||||
int32_t m_6vFaultsCbKey = 0;
|
||||
int32_t m_5vVoltageCbKey = 0;
|
||||
int32_t m_5vCurrentCbKey = 0;
|
||||
int32_t m_5vActiveCbKey = 0;
|
||||
int32_t m_5vFaultsCbKey = 0;
|
||||
int32_t m_3v3VoltageCbKey = 0;
|
||||
int32_t m_3v3CurrentCbKey = 0;
|
||||
int32_t m_3v3ActiveCbKey = 0;
|
||||
int32_t m_3v3FaultsCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,128 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <hal/simulation/SimDeviceData.h>
|
||||
#include <wpi/StringMap.h>
|
||||
#include <wpi/uv/AsyncFunction.h>
|
||||
|
||||
#include "WSBaseProvider.h"
|
||||
#include "WSProviderContainer.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderSimDevice;
|
||||
class HALSimWSProviderSimDevices;
|
||||
|
||||
struct SimDeviceValueData {
|
||||
HALSimWSProviderSimDevice* device;
|
||||
HAL_SimValueHandle handle;
|
||||
std::string key;
|
||||
HAL_Type valueType;
|
||||
};
|
||||
|
||||
class HALSimWSProviderSimDevice : public HALSimWSBaseProvider {
|
||||
public:
|
||||
HALSimWSProviderSimDevice(HAL_SimDeviceHandle handle, const std::string& key,
|
||||
const std::string& deviceId)
|
||||
: HALSimWSBaseProvider(key, "SimDevices"), m_handle(handle) {
|
||||
m_deviceId = deviceId;
|
||||
}
|
||||
|
||||
~HALSimWSProviderSimDevice();
|
||||
|
||||
void OnNetworkConnected(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> ws) override;
|
||||
|
||||
void OnNetworkDisconnected() override;
|
||||
|
||||
void OnNetValueChanged(const wpi::json& json) override;
|
||||
|
||||
void ProcessHalCallback(const wpi::json& payload);
|
||||
|
||||
private:
|
||||
static void OnValueCreatedStatic(const char* name, void* param,
|
||||
HAL_SimValueHandle handle, HAL_Bool readonly,
|
||||
const struct HAL_Value* value) {
|
||||
(reinterpret_cast<HALSimWSProviderSimDevice*>(param))
|
||||
->OnValueCreated(name, handle, readonly, value);
|
||||
}
|
||||
void OnValueCreated(const char* name, HAL_SimValueHandle handle,
|
||||
HAL_Bool readonly, const struct HAL_Value* value);
|
||||
|
||||
static void OnValueChangedStatic(const char* name, void* param,
|
||||
HAL_SimValueHandle handle, HAL_Bool readonly,
|
||||
const struct HAL_Value* value) {
|
||||
auto valueData = (reinterpret_cast<SimDeviceValueData*>(param));
|
||||
valueData->device->OnValueChanged(valueData, value);
|
||||
}
|
||||
void OnValueChanged(SimDeviceValueData* valueData,
|
||||
const struct HAL_Value* value);
|
||||
|
||||
void CancelCallbacks();
|
||||
|
||||
wpi::StringMap<std::unique_ptr<SimDeviceValueData>> m_valueHandles;
|
||||
std::shared_mutex m_vhLock;
|
||||
|
||||
HAL_SimDeviceHandle m_handle;
|
||||
|
||||
std::shared_ptr<HALSimWSProviderSimDevices> m_simDevicesBase;
|
||||
|
||||
int32_t m_simValueCreatedCbKey = 0;
|
||||
wpi::StringMap<int32_t> m_simValueChangedCbKeys;
|
||||
};
|
||||
|
||||
class HALSimWSProviderSimDevices {
|
||||
public:
|
||||
using LoopFn = std::function<void(void)>;
|
||||
using UvExecFn = wpi::uv::AsyncFunction<void(LoopFn)>;
|
||||
|
||||
explicit HALSimWSProviderSimDevices(ProviderContainer& providers)
|
||||
: m_providers(providers) {}
|
||||
~HALSimWSProviderSimDevices();
|
||||
|
||||
void Initialize(std::shared_ptr<wpi::uv::Loop> loop);
|
||||
|
||||
void OnNetworkConnected(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
void OnNetworkDisconnected();
|
||||
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> GetWSConnection() {
|
||||
return m_ws;
|
||||
}
|
||||
|
||||
private:
|
||||
static void DeviceCreatedCallbackStatic(const char* name, void* param,
|
||||
HAL_SimDeviceHandle handle) {
|
||||
(reinterpret_cast<HALSimWSProviderSimDevices*>(param))
|
||||
->DeviceCreatedCallback(name, handle);
|
||||
}
|
||||
void DeviceCreatedCallback(const char* name, HAL_SimDeviceHandle handle);
|
||||
|
||||
static void DeviceFreedCallbackStatic(const char* name, void* param,
|
||||
HAL_SimDeviceHandle handle) {
|
||||
(reinterpret_cast<HALSimWSProviderSimDevices*>(param))
|
||||
->DeviceFreedCallback(name, handle);
|
||||
}
|
||||
void DeviceFreedCallback(const char* name, HAL_SimDeviceHandle handle);
|
||||
|
||||
void CancelCallbacks();
|
||||
|
||||
ProviderContainer& m_providers;
|
||||
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> m_ws;
|
||||
std::shared_ptr<UvExecFn> m_exec;
|
||||
|
||||
int32_t m_deviceCreatedCbKey = 0;
|
||||
int32_t m_deviceFreedCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,33 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "WSHalProviders.h"
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWSProviderDigitalPWM : public HALSimWSHalChanProvider {
|
||||
public:
|
||||
static void Initialize(WSRegisterFunc webRegisterFunc);
|
||||
|
||||
using HALSimWSHalChanProvider::HALSimWSHalChanProvider;
|
||||
~HALSimWSProviderDigitalPWM();
|
||||
|
||||
protected:
|
||||
void RegisterCallbacks() override;
|
||||
void CancelCallbacks() override;
|
||||
|
||||
private:
|
||||
int32_t m_initCbKey = 0;
|
||||
int32_t m_dutyCycleCbKey = 0;
|
||||
int32_t m_pinCbKey = 0;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
16
simulation/halsim_ws_server/CMakeLists.txt
Normal file
16
simulation/halsim_ws_server/CMakeLists.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
project(halsim_ws_server)
|
||||
|
||||
include(CompileWarnings)
|
||||
|
||||
file(GLOB halsim_ws_server_src src/main/native/cpp/*.cpp)
|
||||
|
||||
add_library(halsim_ws_server MODULE ${halsim_ws_server_src})
|
||||
wpilib_target_warnings(halsim_ws_server)
|
||||
set_target_properties(halsim_ws_server PROPERTIES DEBUG_POSTFIX "d")
|
||||
target_link_libraries(halsim_ws_server PUBLIC hal halsim_ws_core)
|
||||
|
||||
target_include_directories(halsim_ws_server PRIVATE src/main/native/include)
|
||||
|
||||
set_property(TARGET halsim_ws_server PROPERTY FOLDER "libraries")
|
||||
|
||||
install(TARGETS halsim_ws_server EXPORT halsim_ws_server DESTINATION "${main_lib_dest}")
|
||||
72
simulation/halsim_ws_server/build.gradle
Normal file
72
simulation/halsim_ws_server/build.gradle
Normal file
@@ -0,0 +1,72 @@
|
||||
|
||||
description = "WebSocket Server Extension"
|
||||
|
||||
ext {
|
||||
includeWpiutil = true
|
||||
pluginName = 'halsim_ws_server'
|
||||
}
|
||||
|
||||
apply plugin: 'google-test-test-suite'
|
||||
|
||||
|
||||
ext {
|
||||
staticGtestConfigs = [:]
|
||||
}
|
||||
|
||||
staticGtestConfigs["${pluginName}Test"] = []
|
||||
apply from: "${rootDir}/shared/googletest.gradle"
|
||||
|
||||
apply from: "${rootDir}/shared/plugins/setupBuild.gradle"
|
||||
|
||||
model {
|
||||
testSuites {
|
||||
def comps = $.components
|
||||
if (!project.hasProperty('onlylinuxathena') && !project.hasProperty('onlylinuxraspbian') && !project.hasProperty('onlylinuxaarch64bionic')) {
|
||||
"${pluginName}Test"(GoogleTestTestSuiteSpec) {
|
||||
for(NativeComponentSpec c : comps) {
|
||||
if (c.name == pluginName) {
|
||||
testing c
|
||||
break
|
||||
}
|
||||
}
|
||||
sources {
|
||||
cpp {
|
||||
source {
|
||||
srcDirs 'src/test/native/cpp'
|
||||
include '**/*.cpp'
|
||||
}
|
||||
exportedHeaders {
|
||||
srcDirs 'src/test/native/include', 'src/main/native/cpp'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
binaries {
|
||||
all {
|
||||
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
|
||||
it.buildable = false
|
||||
return
|
||||
}
|
||||
|
||||
lib project: ":simulation:halsim_ws_core", library: "halsim_ws_core", linkage: "static"
|
||||
}
|
||||
|
||||
withType(GoogleTestTestSuiteBinarySpec) {
|
||||
project(':hal').addHalDependency(it, 'shared')
|
||||
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
|
||||
lib library: pluginName, linkage: 'shared'
|
||||
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
|
||||
nativeUtils.useRequiredLibrary(it, 'netcomm_shared', 'chipobject_shared', 'visa_shared', 'ni_runtime_shared')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
tasks.withType(RunTestExecutable) {
|
||||
args "--gtest_output=xml:test_detail.xml"
|
||||
outputs.dir outputDir
|
||||
}
|
||||
36
simulation/halsim_ws_server/src/dev/native/cpp/main.cpp
Normal file
36
simulation/halsim_ws_server/src/dev/native/cpp/main.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include <hal/DriverStation.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <hal/Main.h>
|
||||
#include <wpi/Format.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
extern "C" int HALSIM_InitExtension(void);
|
||||
|
||||
int main() {
|
||||
HAL_Initialize(500, 0);
|
||||
HALSIM_InitExtension();
|
||||
|
||||
// HAL_ObserveUserProgramStarting();
|
||||
|
||||
HAL_RunMain();
|
||||
|
||||
int cycleCount = 0;
|
||||
while (cycleCount < 1000) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
cycleCount++;
|
||||
std::cout << "Count: " << cycleCount << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "DONE" << std::endl;
|
||||
HAL_ExitMain();
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "HALSimHttpConnection.h"
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
#include <wpi/FileSystem.h>
|
||||
#include <wpi/MimeTypes.h>
|
||||
#include <wpi/Path.h>
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/UrlParser.h>
|
||||
#include <wpi/raw_istream.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/raw_uv_ostream.h>
|
||||
#include <wpi/uv/Request.h>
|
||||
|
||||
#include "HALSimWSServer.h"
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
bool HALSimHttpConnection::IsValidWsUpgrade(wpi::StringRef protocol) {
|
||||
auto hws = HALSimWeb::GetInstance();
|
||||
if (m_request.GetUrl() != hws->GetServerUri()) {
|
||||
MySendError(404, "invalid websocket address");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::ProcessWsUpgrade() {
|
||||
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
|
||||
conn.disconnect(); // one-shot
|
||||
|
||||
m_buffers = std::make_unique<BufferPool>();
|
||||
m_exec =
|
||||
UvExecFunc::Create(m_stream.GetLoop(), [](auto out, LoopFunc func) {
|
||||
func();
|
||||
out.set_value();
|
||||
});
|
||||
|
||||
auto hws = HALSimWeb::GetInstance();
|
||||
if (!hws) {
|
||||
Log(503);
|
||||
m_websocket->Fail(503, "HALSimWeb unavailable");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hws->RegisterWebsocket(shared_from_this())) {
|
||||
Log(409);
|
||||
m_websocket->Fail(409, "Only a single simulation websocket is allowed");
|
||||
return;
|
||||
}
|
||||
|
||||
Log(200);
|
||||
m_isWsConnected = true;
|
||||
wpi::errs() << "HALWebSim: websocket connected\n";
|
||||
});
|
||||
|
||||
// parse incoming JSON, dispatch to parent
|
||||
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
|
||||
auto hws = HALSimWeb::GetInstance();
|
||||
if (!m_isWsConnected || !hws) {
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::json j;
|
||||
try {
|
||||
j = wpi::json::parse(msg);
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
std::string err("JSON parse failed: ");
|
||||
err += e.what();
|
||||
m_websocket->Fail(400, err);
|
||||
return;
|
||||
}
|
||||
hws->OnNetValueChanged(j);
|
||||
});
|
||||
|
||||
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
|
||||
// unset the global, allow another websocket to connect
|
||||
if (m_isWsConnected) {
|
||||
wpi::errs() << "HALWebSim: websocket disconnected\n";
|
||||
m_isWsConnected = false;
|
||||
|
||||
auto hws = HALSimWeb::GetInstance();
|
||||
if (hws) {
|
||||
hws->CloseWebsocket(shared_from_this());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::OnSimValueChanged(const wpi::json& msg) {
|
||||
// render json to buffers
|
||||
wpi::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers->Allocate();
|
||||
}};
|
||||
os << msg;
|
||||
|
||||
// call the websocket send function on the uv loop
|
||||
m_exec->Call([this, sendBufs]() mutable {
|
||||
m_websocket->SendText(sendBufs, [this](auto bufs, wpi::uv::Error err) {
|
||||
{
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
m_buffers->Release(bufs);
|
||||
}
|
||||
|
||||
if (err) {
|
||||
wpi::errs() << err.str() << "\n";
|
||||
wpi::errs().flush();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class SendfileReq : public uv::RequestImpl<SendfileReq, uv_fs_t> {
|
||||
public:
|
||||
SendfileReq(uv_file out, uv_file in, int64_t inOffset, size_t len)
|
||||
: m_out(out), m_in(in), m_inOffset(inOffset), m_len(len) {
|
||||
error = [this](uv::Error err) { GetLoop().error(err); };
|
||||
}
|
||||
|
||||
uv::Loop& GetLoop() const {
|
||||
return *static_cast<uv::Loop*>(GetRaw()->loop->data);
|
||||
}
|
||||
|
||||
int Send(uv::Loop& loop) {
|
||||
int err = uv_fs_sendfile(loop.GetRaw(), GetRaw(), m_out, m_in, m_inOffset,
|
||||
m_len, [](uv_fs_t* req) {
|
||||
auto& h = *static_cast<SendfileReq*>(req->data);
|
||||
if (req->result < 0) {
|
||||
h.ReportError(req->result);
|
||||
h.complete();
|
||||
h.Release();
|
||||
return;
|
||||
}
|
||||
|
||||
h.m_inOffset += req->result;
|
||||
h.m_len -= req->result;
|
||||
if (h.m_len == 0) {
|
||||
// done
|
||||
h.complete();
|
||||
h.Release(); // this is always a one-shot
|
||||
return;
|
||||
}
|
||||
|
||||
// need to send more
|
||||
h.Send(h.GetLoop());
|
||||
});
|
||||
if (err < 0) {
|
||||
ReportError(err);
|
||||
complete();
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
wpi::sig::Signal<> complete;
|
||||
|
||||
private:
|
||||
uv_file m_out;
|
||||
uv_file m_in;
|
||||
int64_t m_inOffset;
|
||||
size_t m_len;
|
||||
};
|
||||
|
||||
void Sendfile(uv::Loop& loop, uv_file out, uv_file in, int64_t inOffset,
|
||||
size_t len, std::function<void()> complete) {
|
||||
auto req = std::make_shared<SendfileReq>(out, in, inOffset, len);
|
||||
if (complete) req->complete.connect(complete);
|
||||
int err = req->Send(loop);
|
||||
if (err >= 0) req->Keep();
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::SendFileResponse(int code,
|
||||
const wpi::Twine& codeText,
|
||||
const wpi::Twine& contentType,
|
||||
const wpi::Twine& filename,
|
||||
const wpi::Twine& extraHeader) {
|
||||
// open file
|
||||
int infd;
|
||||
if (wpi::sys::fs::openFileForRead(filename, infd)) {
|
||||
MySendError(404, "error opening file");
|
||||
return;
|
||||
}
|
||||
|
||||
// get status (to get file size)
|
||||
wpi::sys::fs::file_status status;
|
||||
if (wpi::sys::fs::status(infd, status)) {
|
||||
MySendError(404, "error getting file size");
|
||||
wpi::sys::fs::file_t file = uv_get_osfhandle(infd);
|
||||
wpi::sys::fs::closeFile(file);
|
||||
return;
|
||||
}
|
||||
|
||||
uv_os_fd_t outfd;
|
||||
int err = uv_fileno(m_stream.GetRawHandle(), &outfd);
|
||||
if (err < 0) {
|
||||
m_stream.GetLoopRef().ReportError(err);
|
||||
MySendError(404, "error getting fd");
|
||||
wpi::sys::fs::file_t file = uv_get_osfhandle(infd);
|
||||
wpi::sys::fs::closeFile(file);
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::SmallVector<uv::Buffer, 4> toSend;
|
||||
wpi::raw_uv_ostream os{toSend, 4096};
|
||||
BuildHeader(os, code, codeText, contentType, status.getSize(), extraHeader);
|
||||
SendData(os.bufs(), false);
|
||||
|
||||
Log(code);
|
||||
|
||||
// Read the file byte by byte
|
||||
wpi::SmallVector<uv::Buffer, 4> bodyData;
|
||||
wpi::raw_uv_ostream bodyOs{bodyData, 4096};
|
||||
|
||||
wpi::raw_fd_istream is{infd, true};
|
||||
std::string fileBuf;
|
||||
size_t oldSize = 0;
|
||||
|
||||
while (fileBuf.size() < status.getSize()) {
|
||||
oldSize = fileBuf.size();
|
||||
fileBuf.resize(oldSize + 1);
|
||||
is.read(&(*fileBuf.begin()) + oldSize, 1);
|
||||
}
|
||||
|
||||
bodyOs << fileBuf;
|
||||
|
||||
SendData(bodyOs.bufs(), false);
|
||||
if (!m_keepAlive) {
|
||||
m_stream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::ProcessRequest() {
|
||||
wpi::UrlParser url{m_request.GetUrl(),
|
||||
m_request.GetMethod() == wpi::HTTP_CONNECT};
|
||||
if (!url.IsValid()) {
|
||||
// failed to parse URL
|
||||
MySendError(400, "Invalid URL");
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::StringRef path;
|
||||
if (url.HasPath()) path = url.GetPath();
|
||||
|
||||
if (m_request.GetMethod() == wpi::HTTP_GET && path.startswith("/") &&
|
||||
!path.contains("..")) {
|
||||
// convert to fs native representation
|
||||
wpi::SmallVector<char, 32> nativePath;
|
||||
wpi::sys::path::native(path, nativePath);
|
||||
|
||||
if (path.startswith("/user/")) {
|
||||
std::string prefix = (wpi::sys::path::get_separator() + "user" +
|
||||
wpi::sys::path::get_separator())
|
||||
.str();
|
||||
wpi::sys::path::replace_path_prefix(nativePath, prefix, m_webroot_user);
|
||||
} else {
|
||||
wpi::sys::path::replace_path_prefix(
|
||||
nativePath, wpi::sys::path::get_separator(), m_webroot_sys);
|
||||
}
|
||||
|
||||
if (wpi::sys::fs::is_directory(nativePath)) {
|
||||
wpi::sys::path::append(nativePath, "index.html");
|
||||
}
|
||||
|
||||
if (!wpi::sys::fs::exists(nativePath) ||
|
||||
wpi::sys::fs::is_directory(nativePath)) {
|
||||
MySendError(404, "Resource '" + path + "' not found");
|
||||
} else {
|
||||
auto contentType = wpi::MimeTypeFromPath(wpi::Twine(nativePath).str());
|
||||
SendFileResponse(200, "OK", contentType, nativePath);
|
||||
}
|
||||
} else {
|
||||
MySendError(404, "Resource not found");
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::MySendError(int code, const wpi::Twine& message) {
|
||||
Log(code);
|
||||
SendError(code, message);
|
||||
}
|
||||
|
||||
void HALSimHttpConnection::Log(int code) {
|
||||
auto method = wpi::http_method_str(m_request.GetMethod());
|
||||
wpi::errs() << method << " " << m_request.GetUrl() << " HTTP/"
|
||||
<< m_request.GetMajor() << "." << m_request.GetMinor() << " "
|
||||
<< code << "\n";
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,181 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "HALSimWSServer.h"
|
||||
|
||||
#include <wpi/FileSystem.h>
|
||||
#include <wpi/Path.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/Twine.h>
|
||||
#include <wpi/UrlParser.h>
|
||||
#include <wpi/WebSocketServer.h>
|
||||
#include <wpi/raw_uv_ostream.h>
|
||||
#include <wpi/uv/Loop.h>
|
||||
#include <wpi/uv/Tcp.h>
|
||||
|
||||
#include "HALSimHttpConnection.h"
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
std::shared_ptr<HALSimWeb> HALSimWeb::g_instance;
|
||||
|
||||
bool HALSimWeb::Initialize() {
|
||||
// determine where to get static content from
|
||||
// wpi::SmallVector<char, 64> tmp;
|
||||
wpi::SmallString<64> tmp;
|
||||
|
||||
const char* webroot_sys = std::getenv("HALSIMWS_SYSROOT");
|
||||
if (webroot_sys != NULL) {
|
||||
wpi::StringRef tstr(webroot_sys);
|
||||
tmp.append(tstr);
|
||||
} else {
|
||||
wpi::sys::fs::current_path(tmp);
|
||||
wpi::sys::path::append(tmp, "sim");
|
||||
}
|
||||
wpi::sys::fs::make_absolute(tmp);
|
||||
m_webroot_sys = wpi::Twine(tmp).str();
|
||||
|
||||
tmp.clear();
|
||||
const char* webroot_user = std::getenv("HALSIMWS_USERROOT");
|
||||
if (webroot_user != NULL) {
|
||||
wpi::StringRef tstr(webroot_user);
|
||||
tmp.append(tstr);
|
||||
} else {
|
||||
wpi::sys::fs::current_path(tmp);
|
||||
wpi::sys::path::append(tmp, "sim", "user");
|
||||
}
|
||||
wpi::sys::fs::make_absolute(tmp);
|
||||
m_webroot_user = wpi::Twine(tmp).str();
|
||||
|
||||
const char* uri = std::getenv("HALSIMWS_URI");
|
||||
if (uri != NULL) {
|
||||
m_uri = uri;
|
||||
} else {
|
||||
m_uri = "/wpilibws";
|
||||
}
|
||||
|
||||
const char* port = std::getenv("HALSIMWS_PORT");
|
||||
if (port != NULL) {
|
||||
try {
|
||||
m_port = std::stoi(port);
|
||||
} catch (const std::invalid_argument& err) {
|
||||
wpi::errs() << "Error decoding HALSIMWS_PORT (" << err.what() << ")\n";
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
m_port = 8080;
|
||||
}
|
||||
|
||||
// create libuv things
|
||||
m_loop = uv::Loop::Create();
|
||||
if (!m_loop) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_loop->error.connect([](uv::Error err) {
|
||||
wpi::errs() << "HALSim WS Server libuv ERROR: " << err.str() << '\n';
|
||||
});
|
||||
|
||||
m_server = uv::Tcp::Create(m_loop);
|
||||
if (!m_server) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_server->Bind("", m_port);
|
||||
return true;
|
||||
}
|
||||
|
||||
void HALSimWeb::Main(void* param) {
|
||||
GetInstance()->MainLoop();
|
||||
SetInstance(nullptr);
|
||||
}
|
||||
|
||||
void HALSimWeb::MainLoop() {
|
||||
// when we get a connection, accept it and start reading
|
||||
m_server->connection.connect([this, srv = m_server.get()] {
|
||||
auto tcp = srv->Accept();
|
||||
if (!tcp) return;
|
||||
|
||||
tcp->SetNoDelay(true);
|
||||
|
||||
auto conn = std::make_shared<HALSimHttpConnection>(tcp, m_webroot_sys,
|
||||
m_webroot_user);
|
||||
tcp->SetData(conn);
|
||||
});
|
||||
|
||||
// start listening for incoming connections
|
||||
m_server->Listen();
|
||||
wpi::outs() << "Listening at http://localhost:" << m_port << "\n";
|
||||
wpi::outs() << "WebSocket URI: " << m_uri << "\n";
|
||||
m_loop->Run();
|
||||
}
|
||||
|
||||
void HALSimWeb::Exit(void* param) {
|
||||
auto inst = GetInstance();
|
||||
if (!inst) return;
|
||||
|
||||
auto loop = inst->m_loop;
|
||||
loop->Walk([](uv::Handle& h) {
|
||||
h.SetLoopClosing(true);
|
||||
h.Close();
|
||||
});
|
||||
}
|
||||
|
||||
bool HALSimWeb::RegisterWebsocket(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
|
||||
if (m_hws.lock()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_hws = hws;
|
||||
|
||||
m_simDevicesProvider.OnNetworkConnected(hws);
|
||||
|
||||
// notify all providers that they should use this new websocket instead
|
||||
m_providers.ForEach([hws](std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
provider->OnNetworkConnected(hws);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HALSimWeb::CloseWebsocket(
|
||||
std::shared_ptr<HALSimBaseWebSocketConnection> hws) {
|
||||
// Inform the providers that they need to cancel callbacks
|
||||
m_simDevicesProvider.OnNetworkDisconnected();
|
||||
|
||||
m_providers.ForEach([](std::shared_ptr<HALSimWSBaseProvider> provider) {
|
||||
provider->OnNetworkDisconnected();
|
||||
});
|
||||
|
||||
if (hws == m_hws.lock()) {
|
||||
m_hws.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
|
||||
// Look for "type" and "device" fields so that we can
|
||||
// generate the key
|
||||
|
||||
try {
|
||||
std::string type = msg.at("type").get<std::string>();
|
||||
std::string device = msg.at("device").get<std::string>();
|
||||
auto data = msg.at("data");
|
||||
|
||||
auto key = type + "/" + device;
|
||||
auto provider = m_providers.Get(key);
|
||||
if (provider) {
|
||||
provider->OnNetValueChanged(data);
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace wpilibws
|
||||
67
simulation/halsim_ws_server/src/main/native/cpp/main.cpp
Normal file
67
simulation/halsim_ws_server/src/main/native/cpp/main.cpp
Normal 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"
|
||||
@@ -0,0 +1,74 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cinttypes>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <HALSimBaseWebSocketConnection.h>
|
||||
#include <wpi/HttpWebSocketServerConnection.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/uv/AsyncFunction.h>
|
||||
#include <wpi/uv/Buffer.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimWeb;
|
||||
|
||||
class HALSimHttpConnection
|
||||
: public wpi::HttpWebSocketServerConnection<HALSimHttpConnection>,
|
||||
public HALSimBaseWebSocketConnection {
|
||||
public:
|
||||
using BufferPool = wpi::uv::SimpleBufferPool<4>;
|
||||
|
||||
using LoopFunc = std::function<void(void)>;
|
||||
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
|
||||
|
||||
explicit HALSimHttpConnection(std::shared_ptr<wpi::uv::Stream> stream,
|
||||
wpi::StringRef webroot_sys,
|
||||
wpi::StringRef webroot_user)
|
||||
: wpi::HttpWebSocketServerConnection<HALSimHttpConnection>(stream, {}),
|
||||
m_webroot_sys(webroot_sys),
|
||||
m_webroot_user(webroot_user) {}
|
||||
|
||||
public:
|
||||
// callable from any thread
|
||||
void OnSimValueChanged(const wpi::json& msg) override;
|
||||
|
||||
protected:
|
||||
void ProcessRequest() override;
|
||||
bool IsValidWsUpgrade(wpi::StringRef protocol) override;
|
||||
void ProcessWsUpgrade() override;
|
||||
void SendFileResponse(int code, const wpi::Twine& codeText,
|
||||
const wpi::Twine& contentType,
|
||||
const wpi::Twine& filename,
|
||||
const wpi::Twine& extraHeader = wpi::Twine{});
|
||||
|
||||
void MySendError(int code, const wpi::Twine& message);
|
||||
void Log(int code);
|
||||
|
||||
private:
|
||||
// Absolute paths of folders to retrieve data from
|
||||
// -> /
|
||||
std::string m_webroot_sys;
|
||||
// -> /user
|
||||
std::string m_webroot_user;
|
||||
|
||||
// is the websocket connected?
|
||||
bool m_isWsConnected = false;
|
||||
|
||||
// these are only valid if the websocket is connected
|
||||
std::shared_ptr<UvExecFunc> m_exec;
|
||||
std::unique_ptr<BufferPool> m_buffers;
|
||||
std::mutex m_buffers_mutex;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,81 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <WSBaseProvider.h>
|
||||
#include <WSProviderContainer.h>
|
||||
#include <WSProvider_SimDevice.h>
|
||||
#include <wpi/StringMap.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/mutex.h>
|
||||
#include <wpi/uv/AsyncFunction.h>
|
||||
#include <wpi/uv/Buffer.h>
|
||||
#include <wpi/uv/Loop.h>
|
||||
#include <wpi/uv/Tcp.h>
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class HALSimHttpConnection;
|
||||
|
||||
class HALSimWeb {
|
||||
public:
|
||||
static std::shared_ptr<HALSimWeb> GetInstance() { return g_instance; }
|
||||
static void SetInstance(std::shared_ptr<HALSimWeb> inst) {
|
||||
g_instance = inst;
|
||||
}
|
||||
|
||||
explicit HALSimWeb(ProviderContainer& providers,
|
||||
HALSimWSProviderSimDevices& simDevicesProvider)
|
||||
: m_providers(providers), m_simDevicesProvider(simDevicesProvider) {}
|
||||
|
||||
HALSimWeb(const HALSimWeb&) = delete;
|
||||
HALSimWeb& operator=(const HALSimWeb&) = delete;
|
||||
|
||||
bool Initialize();
|
||||
static void Main(void*);
|
||||
static void Exit(void*);
|
||||
|
||||
bool RegisterWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
void CloseWebsocket(std::shared_ptr<HALSimBaseWebSocketConnection> hws);
|
||||
|
||||
// network -> sim
|
||||
void OnNetValueChanged(const wpi::json& msg);
|
||||
|
||||
std::string GetServerUri() const { return m_uri; }
|
||||
int GetServerPort() { return m_port; }
|
||||
std::shared_ptr<wpi::uv::Loop> GetLoop() { return m_loop; }
|
||||
|
||||
private:
|
||||
static std::shared_ptr<HALSimWeb> g_instance;
|
||||
|
||||
void MainLoop();
|
||||
|
||||
// connected http connection that contains active websocket
|
||||
std::weak_ptr<HALSimBaseWebSocketConnection> m_hws;
|
||||
|
||||
// list of providers
|
||||
ProviderContainer& m_providers;
|
||||
HALSimWSProviderSimDevices& m_simDevicesProvider;
|
||||
|
||||
std::shared_ptr<wpi::uv::Loop> m_loop;
|
||||
std::shared_ptr<wpi::uv::Tcp> m_server;
|
||||
|
||||
// Absolute paths of folders to retrieve data from
|
||||
// -> /
|
||||
std::string m_webroot_sys;
|
||||
// -> /user
|
||||
std::string m_webroot_user;
|
||||
|
||||
std::string m_uri;
|
||||
int m_port;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
@@ -0,0 +1,177 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 "WebServerClientTest.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/raw_uv_ostream.h>
|
||||
#include <wpi/uv/util.h>
|
||||
|
||||
static constexpr int kTcpConnectAttemptTimeout = 1000;
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
std::shared_ptr<WebServerClientTest> WebServerClientTest::g_instance;
|
||||
|
||||
// Create Web Socket and specify event callbacks
|
||||
void WebServerClientTest::InitializeWebSocket(const std::string& host, int port,
|
||||
const std::string& uri) {
|
||||
std::stringstream ss;
|
||||
ss << host << ":" << port;
|
||||
wpi::outs() << "Will attempt to connect to: " << ss.str() << uri << "\n";
|
||||
m_websocket =
|
||||
wpi::WebSocket::CreateClient(*m_tcp_client.get(), uri, ss.str());
|
||||
|
||||
// Hook up events
|
||||
m_websocket->open.connect_extended([this](auto conn, wpi::StringRef) {
|
||||
conn.disconnect();
|
||||
m_buffers = std::make_unique<BufferPool>();
|
||||
|
||||
m_exec = UvExecFunc::Create(m_tcp_client->GetLoop(),
|
||||
[](auto out, LoopFunc func) {
|
||||
func();
|
||||
out.set_value();
|
||||
});
|
||||
|
||||
m_ws_connected = true;
|
||||
wpi::errs() << "WebServerClientTest: WebSocket Connected\n";
|
||||
});
|
||||
|
||||
m_websocket->text.connect([this](wpi::StringRef msg, bool) {
|
||||
wpi::json j;
|
||||
try {
|
||||
j = wpi::json::parse(msg);
|
||||
} catch (const wpi::json::parse_error& e) {
|
||||
std::string err("JSON parse failed: ");
|
||||
err += e.what();
|
||||
wpi::errs() << err << "\n";
|
||||
m_websocket->Fail(1003, err);
|
||||
return;
|
||||
}
|
||||
// Save last message received
|
||||
m_json = j;
|
||||
|
||||
// If terminate flag set, end loop after message recieved
|
||||
if (m_terminateFlag) {
|
||||
m_loop->Stop();
|
||||
}
|
||||
});
|
||||
|
||||
m_websocket->closed.connect([this](uint16_t, wpi::StringRef) {
|
||||
if (m_ws_connected) {
|
||||
wpi::errs() << "WebServerClientTest: Websocket Disconnected\n";
|
||||
m_ws_connected = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Create tcp client, specify callbacks, and create timers for loop
|
||||
bool WebServerClientTest::Initialize(std::shared_ptr<uv::Loop>& loop) {
|
||||
m_loop = loop;
|
||||
m_loop->error.connect(
|
||||
[](uv::Error err) { wpi::errs() << "uv Error: " << err.str() << "\n"; });
|
||||
|
||||
m_tcp_client = uv::Tcp::Create(m_loop);
|
||||
if (!m_tcp_client) {
|
||||
wpi::errs() << "ERROR: Could not create TCP Client\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Hook up TCP client events
|
||||
m_tcp_client->error.connect(
|
||||
[this, socket = m_tcp_client.get()](wpi::uv::Error err) {
|
||||
if (m_tcp_connected) {
|
||||
m_tcp_connected = false;
|
||||
m_connect_attempts = 0;
|
||||
m_loop->Stop();
|
||||
return;
|
||||
}
|
||||
|
||||
// If we weren't previously connected, attempt a reconnection
|
||||
m_connect_timer->Start(uv::Timer::Time(kTcpConnectAttemptTimeout));
|
||||
});
|
||||
|
||||
m_tcp_client->closed.connect(
|
||||
[]() { wpi::errs() << "TCP connection closed\n"; });
|
||||
|
||||
// Set up the connection timer
|
||||
m_connect_timer = uv::Timer::Create(m_loop);
|
||||
if (!m_connect_timer) {
|
||||
return false;
|
||||
}
|
||||
// Set up the timer to attempt connection
|
||||
m_connect_timer->timeout.connect([this] { AttemptConnect(); });
|
||||
m_connect_timer->Start(uv::Timer::Time(0));
|
||||
|
||||
wpi::outs() << "WebServerClientTest Initialized\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebServerClientTest::AttemptConnect() {
|
||||
m_connect_attempts++;
|
||||
wpi::outs() << "Test Client Connection Attempt " << m_connect_attempts
|
||||
<< "\n";
|
||||
|
||||
if (m_connect_attempts >= 5) {
|
||||
wpi::errs() << "Test Client Timeout. Unable to connect\n";
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in dest;
|
||||
uv::NameToAddr("localhost", 8080, &dest);
|
||||
m_tcp_client->Connect(dest, [this]() {
|
||||
m_tcp_connected = true;
|
||||
InitializeWebSocket("localhost", 8080, "/wpilibws");
|
||||
});
|
||||
}
|
||||
|
||||
void WebServerClientTest::Exit() {
|
||||
m_loop->Walk([](uv::Handle& h) {
|
||||
h.SetLoopClosing(true);
|
||||
h.Close();
|
||||
});
|
||||
}
|
||||
|
||||
void WebServerClientTest::SendMessage(const wpi::json& msg) {
|
||||
if (msg.empty()) {
|
||||
wpi::errs() << "Message to send is empty\n";
|
||||
return;
|
||||
}
|
||||
|
||||
wpi::SmallVector<uv::Buffer, 4> sendBufs;
|
||||
|
||||
wpi::raw_uv_ostream os{sendBufs, [this]() -> uv::Buffer {
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
return m_buffers->Allocate();
|
||||
}};
|
||||
os << msg;
|
||||
|
||||
// Call the websocket send function on the uv loop
|
||||
m_exec->Call([this, sendBufs]() mutable {
|
||||
m_websocket->SendText(sendBufs, [this](auto bufs, wpi::uv::Error err) {
|
||||
{
|
||||
std::lock_guard lock(m_buffers_mutex);
|
||||
m_buffers->Release(bufs);
|
||||
}
|
||||
if (err) {
|
||||
wpi::errs() << err.str() << "\n";
|
||||
wpi::errs().flush();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const wpi::json& WebServerClientTest::GetLastMessage() { return m_json; }
|
||||
|
||||
} // namespace wpilibws
|
||||
155
simulation/halsim_ws_server/src/test/native/cpp/main.cpp
Normal file
155
simulation/halsim_ws_server/src/test/native/cpp/main.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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 <hal/HALBase.h>
|
||||
#include <hal/Main.h>
|
||||
#include <hal/simulation/DIOData.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
#include <wpi/uv/Loop.h>
|
||||
|
||||
#include "HALSimWSServer.h"
|
||||
#include "WebServerClientTest.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace wpilibws;
|
||||
|
||||
extern "C" int HALSIM_InitExtension(
|
||||
void); // from simulation/halsim_ws_server/src/main/native/cpp/main.cpp
|
||||
|
||||
const int POLLING_SPEED = 10; // 10 ms polling
|
||||
|
||||
class WebServerIntegrationTest : public ::testing::Test {
|
||||
public:
|
||||
WebServerIntegrationTest() {
|
||||
const int MAX_TEST_TIME = 1000; // 1 second
|
||||
|
||||
// Initialize HAL layer including webserver
|
||||
HALSIM_InitExtension();
|
||||
|
||||
// Create and initialize client
|
||||
m_webserver_client =
|
||||
std::shared_ptr<WebServerClientTest>((new WebServerClientTest()));
|
||||
WebServerClientTest::SetInstance(m_webserver_client);
|
||||
auto webserver = HALSimWeb::GetInstance();
|
||||
auto loop = webserver->GetLoop();
|
||||
m_webserver_client->Initialize(loop);
|
||||
|
||||
// Create failover timer to prevent running forever
|
||||
m_failoverTimer = wpi::uv::Timer::Create(loop);
|
||||
m_failoverTimer->timeout.connect([loop] { loop->Stop(); });
|
||||
m_failoverTimer->Start(uv::Timer::Time(MAX_TEST_TIME));
|
||||
}
|
||||
|
||||
~WebServerIntegrationTest() {
|
||||
// Exit HAL layer which exits webserver
|
||||
HAL_ExitMain();
|
||||
HALSimWeb::Exit(nullptr);
|
||||
|
||||
// Exit client
|
||||
m_webserver_client->Exit();
|
||||
WebServerClientTest::SetInstance(nullptr);
|
||||
|
||||
// Unreference timers from loop
|
||||
m_failoverTimer->Unreference();
|
||||
}
|
||||
|
||||
bool IsConnectedClientWS() { return m_webserver_client->IsConnectedWS(); }
|
||||
void TerminateOnNextMessage(bool flag) {
|
||||
m_webserver_client->SetTerminateFlag(flag);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<WebServerClientTest> m_webserver_client;
|
||||
std::shared_ptr<wpi::uv::Timer> m_failoverTimer;
|
||||
};
|
||||
|
||||
TEST_F(WebServerIntegrationTest, DigitalOutput) {
|
||||
// Create expected results
|
||||
const bool EXPECTED_VALUE = false;
|
||||
const int PIN = 0;
|
||||
|
||||
// Attach timer to loop for test function
|
||||
auto ws = HALSimWeb::GetInstance();
|
||||
auto loop = ws->GetLoop();
|
||||
auto timer = wpi::uv::Timer::Create(loop);
|
||||
timer->timeout.connect([&] {
|
||||
if (IsConnectedClientWS()) {
|
||||
wpi::outs() << "***** Setting DIO value for pin " << PIN << " to "
|
||||
<< (EXPECTED_VALUE ? "true" : "false") << "\n";
|
||||
HALSIM_SetDIOValue(PIN, EXPECTED_VALUE);
|
||||
TerminateOnNextMessage(true);
|
||||
} else {
|
||||
// Recheck in POLLING_SPEED ms
|
||||
timer->Start(uv::Timer::Time(POLLING_SPEED));
|
||||
}
|
||||
});
|
||||
timer->Start(uv::Timer::Time(POLLING_SPEED));
|
||||
|
||||
HAL_RunMain();
|
||||
timer->Unreference();
|
||||
|
||||
// Get values from JSON message
|
||||
std::string test_type;
|
||||
std::string test_device;
|
||||
bool test_value = true; // Default value from HAL initialization
|
||||
try {
|
||||
auto& msg = WebServerClientTest::GetInstance()->GetLastMessage();
|
||||
test_type = msg.at("type").get_ref<const std::string&>();
|
||||
test_device = msg.at("device").get_ref<const std::string&>();
|
||||
auto& data = msg.at("data");
|
||||
wpi::json::const_iterator it = data.find("<>value");
|
||||
if (it != data.end()) {
|
||||
test_value = it.value();
|
||||
}
|
||||
} catch (wpi::json::exception& e) {
|
||||
wpi::errs() << "Error with incoming message: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
// Compare results
|
||||
EXPECT_EQ("DIO", test_type);
|
||||
EXPECT_EQ(std::to_string(PIN), test_device);
|
||||
EXPECT_EQ(EXPECTED_VALUE, test_value);
|
||||
}
|
||||
|
||||
TEST_F(WebServerIntegrationTest, DigitalInput) {
|
||||
// Create expected results
|
||||
const bool EXPECTED_VALUE = false;
|
||||
const int PIN = 0;
|
||||
|
||||
// Attach timer to loop for test function
|
||||
auto ws = HALSimWeb::GetInstance();
|
||||
auto loop = ws->GetLoop();
|
||||
auto timer = wpi::uv::Timer::Create(loop);
|
||||
timer->timeout.connect([&] {
|
||||
if (IsConnectedClientWS()) {
|
||||
wpi::json msg = {{"type", "DIO"},
|
||||
{"device", std::to_string(PIN)},
|
||||
{"data", {{"<>value", EXPECTED_VALUE}}}};
|
||||
wpi::outs() << "***** Input JSON: " << msg.dump() << "\n";
|
||||
WebServerClientTest::GetInstance()->SendMessage(msg);
|
||||
loop->Stop();
|
||||
} else {
|
||||
// Recheck in POLLING_SPEED ms
|
||||
timer->Start(uv::Timer::Time(POLLING_SPEED));
|
||||
}
|
||||
});
|
||||
timer->Start(uv::Timer::Time(POLLING_SPEED));
|
||||
|
||||
HAL_RunMain();
|
||||
timer->Unreference();
|
||||
|
||||
// Compare results
|
||||
bool test_value = HALSIM_GetDIOValue(PIN);
|
||||
EXPECT_EQ(EXPECTED_VALUE, test_value);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
HAL_Initialize(500, 0);
|
||||
int ret = RUN_ALL_TESTS();
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <wpi/WebSocket.h>
|
||||
#include <wpi/json.h>
|
||||
#include <wpi/uv/AsyncFunction.h>
|
||||
#include <wpi/uv/Buffer.h>
|
||||
#include <wpi/uv/Loop.h>
|
||||
#include <wpi/uv/Stream.h>
|
||||
#include <wpi/uv/Tcp.h>
|
||||
#include <wpi/uv/Timer.h>
|
||||
|
||||
namespace uv = wpi::uv;
|
||||
|
||||
namespace wpilibws {
|
||||
|
||||
class WebServerClientTest {
|
||||
public:
|
||||
using BufferPool = wpi::uv::SimpleBufferPool<4>;
|
||||
using LoopFunc = std::function<void(void)>;
|
||||
using UvExecFunc = wpi::uv::AsyncFunction<void(LoopFunc)>;
|
||||
|
||||
static std::shared_ptr<WebServerClientTest> GetInstance() {
|
||||
return g_instance;
|
||||
}
|
||||
static void SetInstance(std::shared_ptr<WebServerClientTest> inst) {
|
||||
g_instance = inst;
|
||||
}
|
||||
|
||||
WebServerClientTest() {}
|
||||
WebServerClientTest(const WebServerClientTest&) = delete;
|
||||
WebServerClientTest& operator=(const WebServerClientTest&) = delete;
|
||||
|
||||
bool Initialize(std::shared_ptr<uv::Loop>& loop);
|
||||
void AttemptConnect();
|
||||
void Exit();
|
||||
|
||||
void SendMessage(const wpi::json& msg);
|
||||
const wpi::json& GetLastMessage();
|
||||
bool IsConnectedWS() { return m_ws_connected; }
|
||||
void SetTerminateFlag(bool flag) { m_terminateFlag = flag; }
|
||||
|
||||
private:
|
||||
void InitializeWebSocket(const std::string& host, int port,
|
||||
const std::string& uri);
|
||||
|
||||
bool m_terminateFlag = false;
|
||||
static std::shared_ptr<WebServerClientTest> g_instance;
|
||||
bool m_tcp_connected = false;
|
||||
std::shared_ptr<wpi::uv::Timer> m_connect_timer;
|
||||
int m_connect_attempts = 0;
|
||||
std::shared_ptr<wpi::uv::Loop> m_loop;
|
||||
std::shared_ptr<wpi::uv::Tcp> m_tcp_client;
|
||||
wpi::json m_json;
|
||||
|
||||
bool m_ws_connected = false;
|
||||
std::shared_ptr<wpi::WebSocket> m_websocket;
|
||||
std::shared_ptr<UvExecFunc> m_exec;
|
||||
std::unique_ptr<BufferPool> m_buffers;
|
||||
std::mutex m_buffers_mutex;
|
||||
};
|
||||
|
||||
} // namespace wpilibws
|
||||
Reference in New Issue
Block a user