diff options
| author | 2021-09-20 17:33:19 -0500 | |
|---|---|---|
| committer | 2021-11-24 20:30:22 -0600 | |
| commit | 10241886ddbead1a24a5924debf83a4fad0ade0d (patch) | |
| tree | 6ae225005be45394225533ff2a45f1aae660ab59 /src/input_common/drivers/udp_client.cpp | |
| parent | input_common: Rewrite tas input (diff) | |
| download | yuzu-10241886ddbead1a24a5924debf83a4fad0ade0d.tar.gz yuzu-10241886ddbead1a24a5924debf83a4fad0ade0d.tar.xz yuzu-10241886ddbead1a24a5924debf83a4fad0ade0d.zip | |
input_common: Rewrite udp client
Diffstat (limited to 'src/input_common/drivers/udp_client.cpp')
| -rw-r--r-- | src/input_common/drivers/udp_client.cpp | 364 |
1 files changed, 364 insertions, 0 deletions
diff --git a/src/input_common/drivers/udp_client.cpp b/src/input_common/drivers/udp_client.cpp new file mode 100644 index 000000000..6fcc3a01b --- /dev/null +++ b/src/input_common/drivers/udp_client.cpp | |||
| @@ -0,0 +1,364 @@ | |||
| 1 | // Copyright 2018 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <random> | ||
| 6 | #include <boost/asio.hpp> | ||
| 7 | |||
| 8 | #include "common/logging/log.h" | ||
| 9 | #include "common/param_package.h" | ||
| 10 | #include "common/settings.h" | ||
| 11 | #include "input_common/drivers/udp_client.h" | ||
| 12 | #include "input_common/helpers/udp_protocol.h" | ||
| 13 | |||
| 14 | using boost::asio::ip::udp; | ||
| 15 | |||
| 16 | namespace InputCommon::CemuhookUDP { | ||
| 17 | |||
| 18 | struct SocketCallback { | ||
| 19 | std::function<void(Response::Version)> version; | ||
| 20 | std::function<void(Response::PortInfo)> port_info; | ||
| 21 | std::function<void(Response::PadData)> pad_data; | ||
| 22 | }; | ||
| 23 | |||
| 24 | class Socket { | ||
| 25 | public: | ||
| 26 | using clock = std::chrono::system_clock; | ||
| 27 | |||
| 28 | explicit Socket(const std::string& host, u16 port, SocketCallback callback_) | ||
| 29 | : callback(std::move(callback_)), timer(io_service), | ||
| 30 | socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(GenerateRandomClientId()) { | ||
| 31 | boost::system::error_code ec{}; | ||
| 32 | auto ipv4 = boost::asio::ip::make_address_v4(host, ec); | ||
| 33 | if (ec.value() != boost::system::errc::success) { | ||
| 34 | LOG_ERROR(Input, "Invalid IPv4 address \"{}\" provided to socket", host); | ||
| 35 | ipv4 = boost::asio::ip::address_v4{}; | ||
| 36 | } | ||
| 37 | |||
| 38 | send_endpoint = {udp::endpoint(ipv4, port)}; | ||
| 39 | } | ||
| 40 | |||
| 41 | void Stop() { | ||
| 42 | io_service.stop(); | ||
| 43 | } | ||
| 44 | |||
| 45 | void Loop() { | ||
| 46 | io_service.run(); | ||
| 47 | } | ||
| 48 | |||
| 49 | void StartSend(const clock::time_point& from) { | ||
| 50 | timer.expires_at(from + std::chrono::seconds(3)); | ||
| 51 | timer.async_wait([this](const boost::system::error_code& error) { HandleSend(error); }); | ||
| 52 | } | ||
| 53 | |||
| 54 | void StartReceive() { | ||
| 55 | socket.async_receive_from( | ||
| 56 | boost::asio::buffer(receive_buffer), receive_endpoint, | ||
| 57 | [this](const boost::system::error_code& error, std::size_t bytes_transferred) { | ||
| 58 | HandleReceive(error, bytes_transferred); | ||
| 59 | }); | ||
| 60 | } | ||
| 61 | |||
| 62 | private: | ||
| 63 | u32 GenerateRandomClientId() const { | ||
| 64 | std::random_device device; | ||
| 65 | return device(); | ||
| 66 | } | ||
| 67 | |||
| 68 | void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) { | ||
| 69 | if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) { | ||
| 70 | switch (*type) { | ||
| 71 | case Type::Version: { | ||
| 72 | Response::Version version; | ||
| 73 | std::memcpy(&version, &receive_buffer[sizeof(Header)], sizeof(Response::Version)); | ||
| 74 | callback.version(std::move(version)); | ||
| 75 | break; | ||
| 76 | } | ||
| 77 | case Type::PortInfo: { | ||
| 78 | Response::PortInfo port_info; | ||
| 79 | std::memcpy(&port_info, &receive_buffer[sizeof(Header)], | ||
| 80 | sizeof(Response::PortInfo)); | ||
| 81 | callback.port_info(std::move(port_info)); | ||
| 82 | break; | ||
| 83 | } | ||
| 84 | case Type::PadData: { | ||
| 85 | Response::PadData pad_data; | ||
| 86 | std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData)); | ||
| 87 | callback.pad_data(std::move(pad_data)); | ||
| 88 | break; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | StartReceive(); | ||
| 93 | } | ||
| 94 | |||
| 95 | void HandleSend(const boost::system::error_code&) { | ||
| 96 | boost::system::error_code _ignored{}; | ||
| 97 | // Send a request for getting port info for the pad | ||
| 98 | const Request::PortInfo port_info{4, {0, 1, 2, 3}}; | ||
| 99 | const auto port_message = Request::Create(port_info, client_id); | ||
| 100 | std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); | ||
| 101 | socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); | ||
| 102 | |||
| 103 | // Send a request for getting pad data for the pad | ||
| 104 | const Request::PadData pad_data{ | ||
| 105 | Request::PadData::Flags::AllPorts, | ||
| 106 | 0, | ||
| 107 | EMPTY_MAC_ADDRESS, | ||
| 108 | }; | ||
| 109 | const auto pad_message = Request::Create(pad_data, client_id); | ||
| 110 | std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); | ||
| 111 | socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); | ||
| 112 | StartSend(timer.expiry()); | ||
| 113 | } | ||
| 114 | |||
| 115 | SocketCallback callback; | ||
| 116 | boost::asio::io_service io_service; | ||
| 117 | boost::asio::basic_waitable_timer<clock> timer; | ||
| 118 | udp::socket socket; | ||
| 119 | |||
| 120 | const u32 client_id; | ||
| 121 | |||
| 122 | static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); | ||
| 123 | static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); | ||
| 124 | std::array<u8, PORT_INFO_SIZE> send_buffer1; | ||
| 125 | std::array<u8, PAD_DATA_SIZE> send_buffer2; | ||
| 126 | udp::endpoint send_endpoint; | ||
| 127 | |||
| 128 | std::array<u8, MAX_PACKET_SIZE> receive_buffer; | ||
| 129 | udp::endpoint receive_endpoint; | ||
| 130 | }; | ||
| 131 | |||
| 132 | static void SocketLoop(Socket* socket) { | ||
| 133 | socket->StartReceive(); | ||
| 134 | socket->StartSend(Socket::clock::now()); | ||
| 135 | socket->Loop(); | ||
| 136 | } | ||
| 137 | |||
| 138 | UDPClient::UDPClient(const std::string& input_engine_) : InputEngine(input_engine_) { | ||
| 139 | LOG_INFO(Input, "Udp Initialization started"); | ||
| 140 | ReloadSockets(); | ||
| 141 | } | ||
| 142 | |||
| 143 | UDPClient::~UDPClient() { | ||
| 144 | Reset(); | ||
| 145 | } | ||
| 146 | |||
| 147 | UDPClient::ClientConnection::ClientConnection() = default; | ||
| 148 | |||
| 149 | UDPClient::ClientConnection::~ClientConnection() = default; | ||
| 150 | |||
| 151 | void UDPClient::ReloadSockets() { | ||
| 152 | Reset(); | ||
| 153 | |||
| 154 | std::stringstream servers_ss(Settings::values.udp_input_servers.GetValue()); | ||
| 155 | std::string server_token; | ||
| 156 | std::size_t client = 0; | ||
| 157 | while (std::getline(servers_ss, server_token, ',')) { | ||
| 158 | if (client == MAX_UDP_CLIENTS) { | ||
| 159 | break; | ||
| 160 | } | ||
| 161 | std::stringstream server_ss(server_token); | ||
| 162 | std::string token; | ||
| 163 | std::getline(server_ss, token, ':'); | ||
| 164 | std::string udp_input_address = token; | ||
| 165 | std::getline(server_ss, token, ':'); | ||
| 166 | char* temp; | ||
| 167 | const u16 udp_input_port = static_cast<u16>(std::strtol(token.c_str(), &temp, 0)); | ||
| 168 | if (*temp != '\0') { | ||
| 169 | LOG_ERROR(Input, "Port number is not valid {}", token); | ||
| 170 | continue; | ||
| 171 | } | ||
| 172 | |||
| 173 | const std::size_t client_number = GetClientNumber(udp_input_address, udp_input_port); | ||
| 174 | if (client_number != MAX_UDP_CLIENTS) { | ||
| 175 | LOG_ERROR(Input, "Duplicated UDP servers found"); | ||
| 176 | continue; | ||
| 177 | } | ||
| 178 | StartCommunication(client++, udp_input_address, udp_input_port); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | std::size_t UDPClient::GetClientNumber(std::string_view host, u16 port) const { | ||
| 183 | for (std::size_t client = 0; client < clients.size(); client++) { | ||
| 184 | if (clients[client].active == -1) { | ||
| 185 | continue; | ||
| 186 | } | ||
| 187 | if (clients[client].host == host && clients[client].port == port) { | ||
| 188 | return client; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | return MAX_UDP_CLIENTS; | ||
| 192 | } | ||
| 193 | |||
| 194 | void UDPClient::OnVersion([[maybe_unused]] Response::Version data) { | ||
| 195 | LOG_TRACE(Input, "Version packet received: {}", data.version); | ||
| 196 | } | ||
| 197 | |||
| 198 | void UDPClient::OnPortInfo([[maybe_unused]] Response::PortInfo data) { | ||
| 199 | LOG_TRACE(Input, "PortInfo packet received: {}", data.model); | ||
| 200 | } | ||
| 201 | |||
| 202 | void UDPClient::OnPadData(Response::PadData data, std::size_t client) { | ||
| 203 | const std::size_t pad_index = (client * PADS_PER_CLIENT) + data.info.id; | ||
| 204 | |||
| 205 | if (pad_index >= pads.size()) { | ||
| 206 | LOG_ERROR(Input, "Invalid pad id {}", data.info.id); | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | LOG_TRACE(Input, "PadData packet received"); | ||
| 211 | if (data.packet_counter == pads[pad_index].packet_sequence) { | ||
| 212 | LOG_WARNING( | ||
| 213 | Input, | ||
| 214 | "PadData packet dropped because its stale info. Current count: {} Packet count: {}", | ||
| 215 | pads[pad_index].packet_sequence, data.packet_counter); | ||
| 216 | pads[pad_index].connected = false; | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | |||
| 220 | clients[client].active = 1; | ||
| 221 | pads[pad_index].connected = true; | ||
| 222 | pads[pad_index].packet_sequence = data.packet_counter; | ||
| 223 | |||
| 224 | const auto now = std::chrono::steady_clock::now(); | ||
| 225 | const auto time_difference = static_cast<u64>( | ||
| 226 | std::chrono::duration_cast<std::chrono::microseconds>(now - pads[pad_index].last_update) | ||
| 227 | .count()); | ||
| 228 | pads[pad_index].last_update = now; | ||
| 229 | |||
| 230 | // Gyroscope values are not it the correct scale from better joy. | ||
| 231 | // Dividing by 312 allows us to make one full turn = 1 turn | ||
| 232 | // This must be a configurable valued called sensitivity | ||
| 233 | const float gyro_scale = 1.0f / 312.0f; | ||
| 234 | |||
| 235 | const BasicMotion motion{ | ||
| 236 | .gyro_x = data.gyro.pitch * gyro_scale, | ||
| 237 | .gyro_y = data.gyro.roll * gyro_scale, | ||
| 238 | .gyro_z = -data.gyro.yaw * gyro_scale, | ||
| 239 | .accel_x = data.accel.x, | ||
| 240 | .accel_y = -data.accel.z, | ||
| 241 | .accel_z = data.accel.y, | ||
| 242 | .delta_timestamp = time_difference, | ||
| 243 | }; | ||
| 244 | const PadIdentifier identifier = GetPadIdentifier(pad_index); | ||
| 245 | SetMotion(identifier, 0, motion); | ||
| 246 | } | ||
| 247 | |||
| 248 | void UDPClient::StartCommunication(std::size_t client, const std::string& host, u16 port) { | ||
| 249 | SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, | ||
| 250 | [this](Response::PortInfo info) { OnPortInfo(info); }, | ||
| 251 | [this, client](Response::PadData data) { OnPadData(data, client); }}; | ||
| 252 | LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port); | ||
| 253 | clients[client].host = host; | ||
| 254 | clients[client].port = port; | ||
| 255 | clients[client].active = 0; | ||
| 256 | clients[client].socket = std::make_unique<Socket>(host, port, callback); | ||
| 257 | clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; | ||
| 258 | for (std::size_t index = 0; index < PADS_PER_CLIENT; ++index) { | ||
| 259 | const PadIdentifier identifier = GetPadIdentifier(client * PADS_PER_CLIENT + index); | ||
| 260 | PreSetController(identifier); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | const PadIdentifier UDPClient::GetPadIdentifier(std::size_t pad_index) const { | ||
| 265 | const std::size_t client = pad_index / PADS_PER_CLIENT; | ||
| 266 | return { | ||
| 267 | .guid = Common::UUID{clients[client].host}, | ||
| 268 | .port = static_cast<std::size_t>(clients[client].port), | ||
| 269 | .pad = pad_index, | ||
| 270 | }; | ||
| 271 | } | ||
| 272 | |||
| 273 | void UDPClient::Reset() { | ||
| 274 | for (auto& client : clients) { | ||
| 275 | if (client.thread.joinable()) { | ||
| 276 | client.active = -1; | ||
| 277 | client.socket->Stop(); | ||
| 278 | client.thread.join(); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | void TestCommunication(const std::string& host, u16 port, | ||
| 284 | const std::function<void()>& success_callback, | ||
| 285 | const std::function<void()>& failure_callback) { | ||
| 286 | std::thread([=] { | ||
| 287 | Common::Event success_event; | ||
| 288 | SocketCallback callback{ | ||
| 289 | .version = [](Response::Version) {}, | ||
| 290 | .port_info = [](Response::PortInfo) {}, | ||
| 291 | .pad_data = [&](Response::PadData) { success_event.Set(); }, | ||
| 292 | }; | ||
| 293 | Socket socket{host, port, std::move(callback)}; | ||
| 294 | std::thread worker_thread{SocketLoop, &socket}; | ||
| 295 | const bool result = | ||
| 296 | success_event.WaitUntil(std::chrono::steady_clock::now() + std::chrono::seconds(10)); | ||
| 297 | socket.Stop(); | ||
| 298 | worker_thread.join(); | ||
| 299 | if (result) { | ||
| 300 | success_callback(); | ||
| 301 | } else { | ||
| 302 | failure_callback(); | ||
| 303 | } | ||
| 304 | }).detach(); | ||
| 305 | } | ||
| 306 | |||
| 307 | CalibrationConfigurationJob::CalibrationConfigurationJob( | ||
| 308 | const std::string& host, u16 port, std::function<void(Status)> status_callback, | ||
| 309 | std::function<void(u16, u16, u16, u16)> data_callback) { | ||
| 310 | |||
| 311 | std::thread([=, this] { | ||
| 312 | Status current_status{Status::Initialized}; | ||
| 313 | SocketCallback callback{ | ||
| 314 | [](Response::Version) {}, [](Response::PortInfo) {}, | ||
| 315 | [&](Response::PadData data) { | ||
| 316 | static constexpr u16 CALIBRATION_THRESHOLD = 100; | ||
| 317 | static constexpr u16 MAX_VALUE = UINT16_MAX; | ||
| 318 | |||
| 319 | if (current_status == Status::Initialized) { | ||
| 320 | // Receiving data means the communication is ready now | ||
| 321 | current_status = Status::Ready; | ||
| 322 | status_callback(current_status); | ||
| 323 | } | ||
| 324 | const auto& touchpad_0 = data.touch[0]; | ||
| 325 | if (touchpad_0.is_active == 0) { | ||
| 326 | return; | ||
| 327 | } | ||
| 328 | LOG_DEBUG(Input, "Current touch: {} {}", touchpad_0.x, touchpad_0.y); | ||
| 329 | const u16 min_x = std::min(MAX_VALUE, static_cast<u16>(touchpad_0.x)); | ||
| 330 | const u16 min_y = std::min(MAX_VALUE, static_cast<u16>(touchpad_0.y)); | ||
| 331 | if (current_status == Status::Ready) { | ||
| 332 | // First touch - min data (min_x/min_y) | ||
| 333 | current_status = Status::Stage1Completed; | ||
| 334 | status_callback(current_status); | ||
| 335 | } | ||
| 336 | if (touchpad_0.x - min_x > CALIBRATION_THRESHOLD && | ||
| 337 | touchpad_0.y - min_y > CALIBRATION_THRESHOLD) { | ||
| 338 | // Set the current position as max value and finishes configuration | ||
| 339 | const u16 max_x = touchpad_0.x; | ||
| 340 | const u16 max_y = touchpad_0.y; | ||
| 341 | current_status = Status::Completed; | ||
| 342 | data_callback(min_x, min_y, max_x, max_y); | ||
| 343 | status_callback(current_status); | ||
| 344 | |||
| 345 | complete_event.Set(); | ||
| 346 | } | ||
| 347 | }}; | ||
| 348 | Socket socket{host, port, std::move(callback)}; | ||
| 349 | std::thread worker_thread{SocketLoop, &socket}; | ||
| 350 | complete_event.Wait(); | ||
| 351 | socket.Stop(); | ||
| 352 | worker_thread.join(); | ||
| 353 | }).detach(); | ||
| 354 | } | ||
| 355 | |||
| 356 | CalibrationConfigurationJob::~CalibrationConfigurationJob() { | ||
| 357 | Stop(); | ||
| 358 | } | ||
| 359 | |||
| 360 | void CalibrationConfigurationJob::Stop() { | ||
| 361 | complete_event.Set(); | ||
| 362 | } | ||
| 363 | |||
| 364 | } // namespace InputCommon::CemuhookUDP | ||