summaryrefslogtreecommitdiff
path: root/src/input_common/udp/client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/udp/client.cpp')
-rw-r--r--src/input_common/udp/client.cpp78
1 files changed, 44 insertions, 34 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 9d0b9f31d..7039d6fc3 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -26,11 +26,11 @@ class Socket {
26public: 26public:
27 using clock = std::chrono::system_clock; 27 using clock = std::chrono::system_clock;
28 28
29 explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id, 29 explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_,
30 SocketCallback callback) 30 SocketCallback callback_)
31 : callback(std::move(callback)), timer(io_service), 31 : callback(std::move(callback_)), timer(io_service),
32 socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id), 32 socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_),
33 pad_index(pad_index) { 33 pad_index(pad_index_) {
34 boost::system::error_code ec{}; 34 boost::system::error_code ec{};
35 auto ipv4 = boost::asio::ip::make_address_v4(host, ec); 35 auto ipv4 = boost::asio::ip::make_address_v4(host, ec);
36 if (ec.value() != boost::system::errc::success) { 36 if (ec.value() != boost::system::errc::success) {
@@ -93,13 +93,17 @@ private:
93 void HandleSend(const boost::system::error_code& error) { 93 void HandleSend(const boost::system::error_code& error) {
94 boost::system::error_code _ignored{}; 94 boost::system::error_code _ignored{};
95 // Send a request for getting port info for the pad 95 // Send a request for getting port info for the pad
96 Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; 96 const Request::PortInfo port_info{1, {static_cast<u8>(pad_index), 0, 0, 0}};
97 const auto port_message = Request::Create(port_info, client_id); 97 const auto port_message = Request::Create(port_info, client_id);
98 std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); 98 std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
99 socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); 99 socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored);
100 100
101 // Send a request for getting pad data for the pad 101 // Send a request for getting pad data for the pad
102 Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; 102 const Request::PadData pad_data{
103 Request::PadData::Flags::Id,
104 static_cast<u8>(pad_index),
105 EMPTY_MAC_ADDRESS,
106 };
103 const auto pad_message = Request::Create(pad_data, client_id); 107 const auto pad_message = Request::Create(pad_data, client_id);
104 std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); 108 std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
105 socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); 109 socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored);
@@ -112,7 +116,7 @@ private:
112 udp::socket socket; 116 udp::socket socket;
113 117
114 u32 client_id{}; 118 u32 client_id{};
115 u8 pad_index{}; 119 std::size_t pad_index{};
116 120
117 static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); 121 static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>);
118 static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); 122 static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>);
@@ -133,7 +137,7 @@ static void SocketLoop(Socket* socket) {
133Client::Client() { 137Client::Client() {
134 LOG_INFO(Input, "Udp Initialization started"); 138 LOG_INFO(Input, "Udp Initialization started");
135 for (std::size_t client = 0; client < clients.size(); client++) { 139 for (std::size_t client = 0; client < clients.size(); client++) {
136 u8 pad = client % 4; 140 const auto pad = client % 4;
137 StartCommunication(client, Settings::values.udp_input_address, 141 StartCommunication(client, Settings::values.udp_input_address,
138 Settings::values.udp_input_port, pad, 24872); 142 Settings::values.udp_input_port, pad, 24872);
139 // Set motion parameters 143 // Set motion parameters
@@ -166,9 +170,9 @@ std::vector<Common::ParamPackage> Client::GetInputDevices() const {
166bool Client::DeviceConnected(std::size_t pad) const { 170bool Client::DeviceConnected(std::size_t pad) const {
167 // Use last timestamp to detect if the socket has stopped sending data 171 // Use last timestamp to detect if the socket has stopped sending data
168 const auto now = std::chrono::system_clock::now(); 172 const auto now = std::chrono::system_clock::now();
169 u64 time_difference = 173 const auto time_difference = static_cast<u64>(
170 std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update) 174 std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update)
171 .count(); 175 .count());
172 return time_difference < 1000 && clients[pad].active == 1; 176 return time_difference < 1000 && clients[pad].active == 1;
173} 177}
174 178
@@ -177,9 +181,9 @@ void Client::ReloadUDPClient() {
177 ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); 181 ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client);
178 } 182 }
179} 183}
180void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { 184void Client::ReloadSocket(const std::string& host, u16 port, std::size_t pad_index, u32 client_id) {
181 // client number must be determined from host / port and pad index 185 // client number must be determined from host / port and pad index
182 std::size_t client = pad_index; 186 const std::size_t client = pad_index;
183 clients[client].socket->Stop(); 187 clients[client].socket->Stop();
184 clients[client].thread.join(); 188 clients[client].thread.join();
185 StartCommunication(client, host, port, pad_index, client_id); 189 StartCommunication(client, host, port, pad_index, client_id);
@@ -194,8 +198,8 @@ void Client::OnPortInfo(Response::PortInfo data) {
194} 198}
195 199
196void Client::OnPadData(Response::PadData data) { 200void Client::OnPadData(Response::PadData data) {
197 // client number must be determined from host / port and pad index 201 // Client number must be determined from host / port and pad index
198 std::size_t client = data.info.id; 202 const std::size_t client = data.info.id;
199 LOG_TRACE(Input, "PadData packet received"); 203 LOG_TRACE(Input, "PadData packet received");
200 if (data.packet_counter == clients[client].packet_sequence) { 204 if (data.packet_counter == clients[client].packet_sequence) {
201 LOG_WARNING( 205 LOG_WARNING(
@@ -207,11 +211,12 @@ void Client::OnPadData(Response::PadData data) {
207 clients[client].active = data.info.is_pad_active; 211 clients[client].active = data.info.is_pad_active;
208 clients[client].packet_sequence = data.packet_counter; 212 clients[client].packet_sequence = data.packet_counter;
209 const auto now = std::chrono::system_clock::now(); 213 const auto now = std::chrono::system_clock::now();
210 u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>( 214 const auto time_difference =
211 now - clients[client].last_motion_update) 215 static_cast<u64>(std::chrono::duration_cast<std::chrono::microseconds>(
212 .count(); 216 now - clients[client].last_motion_update)
217 .count());
213 clients[client].last_motion_update = now; 218 clients[client].last_motion_update = now;
214 Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; 219 const Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw};
215 clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); 220 clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y});
216 // Gyroscope values are not it the correct scale from better joy. 221 // Gyroscope values are not it the correct scale from better joy.
217 // Dividing by 312 allows us to make one full turn = 1 turn 222 // Dividing by 312 allows us to make one full turn = 1 turn
@@ -237,9 +242,11 @@ void Client::OnPadData(Response::PadData data) {
237 const u16 min_y = clients[client].status.touch_calibration->min_y; 242 const u16 min_y = clients[client].status.touch_calibration->min_y;
238 const u16 max_y = clients[client].status.touch_calibration->max_y; 243 const u16 max_y = clients[client].status.touch_calibration->max_y;
239 244
240 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / 245 x = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) -
246 min_x) /
241 static_cast<float>(max_x - min_x); 247 static_cast<float>(max_x - min_x);
242 y = (std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - min_y) / 248 y = static_cast<float>(std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) -
249 min_y) /
243 static_cast<float>(max_y - min_y); 250 static_cast<float>(max_y - min_y);
244 } 251 }
245 252
@@ -253,8 +260,8 @@ void Client::OnPadData(Response::PadData data) {
253 } 260 }
254} 261}
255 262
256void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, 263void Client::StartCommunication(std::size_t client, const std::string& host, u16 port,
257 u32 client_id) { 264 std::size_t pad_index, u32 client_id) {
258 SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, 265 SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
259 [this](Response::PortInfo info) { OnPortInfo(info); }, 266 [this](Response::PortInfo info) { OnPortInfo(info); },
260 [this](Response::PadData data) { OnPadData(data); }}; 267 [this](Response::PadData data) { OnPadData(data); }};
@@ -264,9 +271,9 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16
264} 271}
265 272
266void Client::Reset() { 273void Client::Reset() {
267 for (std::size_t client = 0; client < clients.size(); client++) { 274 for (auto& client : clients) {
268 clients[client].socket->Stop(); 275 client.socket->Stop();
269 clients[client].thread.join(); 276 client.thread.join();
270 } 277 }
271} 278}
272 279
@@ -325,16 +332,19 @@ const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() cons
325 return pad_queue; 332 return pad_queue;
326} 333}
327 334
328void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, 335void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id,
329 std::function<void()> success_callback, 336 const std::function<void()>& success_callback,
330 std::function<void()> failure_callback) { 337 const std::function<void()>& failure_callback) {
331 std::thread([=] { 338 std::thread([=] {
332 Common::Event success_event; 339 Common::Event success_event;
333 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {}, 340 SocketCallback callback{
334 [&](Response::PadData data) { success_event.Set(); }}; 341 .version = [](Response::Version) {},
342 .port_info = [](Response::PortInfo) {},
343 .pad_data = [&](Response::PadData) { success_event.Set(); },
344 };
335 Socket socket{host, port, pad_index, client_id, std::move(callback)}; 345 Socket socket{host, port, pad_index, client_id, std::move(callback)};
336 std::thread worker_thread{SocketLoop, &socket}; 346 std::thread worker_thread{SocketLoop, &socket};
337 bool result = success_event.WaitFor(std::chrono::seconds(8)); 347 const bool result = success_event.WaitFor(std::chrono::seconds(8));
338 socket.Stop(); 348 socket.Stop();
339 worker_thread.join(); 349 worker_thread.join();
340 if (result) { 350 if (result) {
@@ -346,7 +356,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie
346} 356}
347 357
348CalibrationConfigurationJob::CalibrationConfigurationJob( 358CalibrationConfigurationJob::CalibrationConfigurationJob(
349 const std::string& host, u16 port, u8 pad_index, u32 client_id, 359 const std::string& host, u16 port, std::size_t pad_index, u32 client_id,
350 std::function<void(Status)> status_callback, 360 std::function<void(Status)> status_callback,
351 std::function<void(u16, u16, u16, u16)> data_callback) { 361 std::function<void(u16, u16, u16, u16)> data_callback) {
352 362
@@ -366,7 +376,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
366 current_status = Status::Ready; 376 current_status = Status::Ready;
367 status_callback(current_status); 377 status_callback(current_status);
368 } 378 }
369 if (!data.touch_1.is_active) { 379 if (data.touch_1.is_active == 0) {
370 return; 380 return;
371 } 381 }
372 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, 382 LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x,