[sim] Add XRP-specific plugin (#5631)

Provides an implementation of a XRP-specific plugin that sends binary messages over UDP (to account for the less performant hardware on the XRP).

This plugin leverages the work already done for the WebSocket protocol and does a translation to/from JSON/binary.
This commit is contained in:
Zhiquan Yeo
2023-09-15 23:08:02 -04:00
committed by GitHub
parent 575348b81c
commit 9047682202
15 changed files with 1095 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <functional>
#include <memory>
#include <span>
#include <string>
#include <HALSimBaseWebSocketConnection.h>
#include <WSProviderContainer.h>
#include <WSProvider_SimDevice.h>
#include <wpinet/uv/Async.h>
#include <wpinet/uv/Buffer.h>
#include <wpinet/uv/Loop.h>
#include <wpinet/uv/Timer.h>
#include <wpinet/uv/Udp.h>
#include "XRP.h"
namespace wpi {
class json;
} // namespace wpi
namespace wpilibxrp {
// This masquerades as a "WebSocket" so that we can reuse the
// stuff in halsim_ws_core
class HALSimXRP : public wpilibws::HALSimBaseWebSocketConnection,
public std::enable_shared_from_this<HALSimXRP> {
public:
using LoopFunc = std::function<void(void)>;
using UvExecFunc = wpi::uv::Async<LoopFunc>;
HALSimXRP(wpi::uv::Loop& loop, wpilibws::ProviderContainer& providers,
wpilibws::HALSimWSProviderSimDevices& simDevicesProvider);
HALSimXRP(const HALSimXRP&) = delete;
HALSimXRP& operator=(const HALSimXRP&) = delete;
bool Initialize();
void Start();
void ParsePacket(std::span<const uint8_t> packet);
void OnNetValueChanged(const wpi::json& msg);
void OnSimValueChanged(const wpi::json& simData) override;
const std::string& GetTargetHost() const { return m_host; }
int GetTargetPort() const { return m_port; }
wpi::uv::Loop& GetLoop() { return m_loop; }
UvExecFunc& GetExec() { return *m_exec; }
private:
XRP m_xrp;
wpi::uv::Loop& m_loop;
std::shared_ptr<wpi::uv::Udp> m_udp_client;
std::shared_ptr<UvExecFunc> m_exec;
wpilibws::ProviderContainer& m_providers;
wpilibws::HALSimWSProviderSimDevices& m_simDevicesProvider;
std::string m_host;
int m_port;
void SendStateToXRP();
wpi::uv::SimpleBufferPool<4>& GetBufferPool();
std::mutex m_buffer_mutex;
struct sockaddr_in m_dest;
};
} // namespace wpilibxrp