summaryrefslogtreecommitdiffstats
path: root/src/input_common/udp/client.cpp
blob: e0c796040a294dd0e23028618ea154407827d206 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <chrono>
#include <cstring>
#include <functional>
#include <thread>
#include <boost/asio.hpp>
#include "common/logging/log.h"
#include "core/settings.h"
#include "input_common/udp/client.h"
#include "input_common/udp/protocol.h"

using boost::asio::ip::udp;

namespace InputCommon::CemuhookUDP {

struct SocketCallback {
    std::function<void(Response::Version)> version;
    std::function<void(Response::PortInfo)> port_info;
    std::function<void(Response::PadData)> pad_data;
};

class Socket {
public:
    using clock = std::chrono::system_clock;

    explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id,
                    SocketCallback callback)
        : callback(std::move(callback)), timer(io_service),
          socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id),
          pad_index(pad_index) {
        boost::system::error_code ec{};
        auto ipv4 = boost::asio::ip::make_address_v4(host, ec);
        if (ec.value() != boost::system::errc::success) {
            LOG_ERROR(Input, "Invalid IPv4 address \"{}\" provided to socket", host);
            ipv4 = boost::asio::ip::address_v4{};
        }

        send_endpoint = {udp::endpoint(ipv4, port)};
    }

    void Stop() {
        io_service.stop();
    }

    void Loop() {
        io_service.run();
    }

    void StartSend(const clock::time_point& from) {
        timer.expires_at(from + std::chrono::seconds(3));
        timer.async_wait([this](const boost::system::error_code& error) { HandleSend(error); });
    }

    void StartReceive() {
        socket.async_receive_from(
            boost::asio::buffer(receive_buffer), receive_endpoint,
            [this](const boost::system::error_code& error, std::size_t bytes_transferred) {
                HandleReceive(error, bytes_transferred);
            });
    }

private:
    void HandleReceive(const boost::system::error_code& error, std::size_t bytes_transferred) {
        if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) {
            switch (*type) {
            case Type::Version: {
                Response::Version version;
                std::memcpy(&version, &receive_buffer[sizeof(Header)], sizeof(Response::Version));
                callback.version(std::move(version));
                break;
            }
            case Type::PortInfo: {
                Response::PortInfo port_info;
                std::memcpy(&port_info, &receive_buffer[sizeof(Header)],
                            sizeof(Response::PortInfo));
                callback.port_info(std::move(port_info));
                break;
            }
            case Type::PadData: {
                Response::PadData pad_data;
                std::memcpy(&pad_data, &receive_buffer[sizeof(Header)], sizeof(Response::PadData));
                callback.pad_data(std::move(pad_data));
                break;
            }
            }
        }
        StartReceive();
    }

    void HandleSend(const boost::system::error_code& error) {
        boost::system::error_code _ignored{};
        // Send a request for getting port info for the pad
        Request::PortInfo port_info{1, {pad_index, 0, 0, 0}};
        const auto port_message = Request::Create(port_info, client_id);
        std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
        socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored);

        // Send a request for getting pad data for the pad
        Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS};
        const auto pad_message = Request::Create(pad_data, client_id);
        std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
        socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored);
        StartSend(timer.expiry());
    }

    SocketCallback callback;
    boost::asio::io_service io_service;
    boost::asio::basic_waitable_timer<clock> timer;
    udp::socket socket;

    u32 client_id{};
    u8 pad_index{};

    static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>);
    static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>);
    std::array<u8, PORT_INFO_SIZE> send_buffer1;
    std::array<u8, PAD_DATA_SIZE> send_buffer2;
    udp::endpoint send_endpoint;

    std::array<u8, MAX_PACKET_SIZE> receive_buffer;
    udp::endpoint receive_endpoint;
};

static void SocketLoop(Socket* socket) {
    socket->StartReceive();
    socket->StartSend(Socket::clock::now());
    socket->Loop();
}

Client::Client() {
    LOG_INFO(Input, "Udp Initialization started");
    for (std::size_t client = 0; client < clients.size(); client++) {
        u8 pad = client % 4;
        StartCommunication(client, Settings::values.udp_input_address,
                           Settings::values.udp_input_port, pad, 24872);
        // Set motion parameters
        // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode
        // Real HW values are unkown, 0.0001 is an aproximate to Standard
        clients[client].motion.SetGyroThreshold(0.0001f);
    }
}

Client::~Client() {
    Reset();
}

std::vector<Common::ParamPackage> Client::GetInputDevices() const {
    std::vector<Common::ParamPackage> devices;
    for (std::size_t client = 0; client < clients.size(); client++) {
        if (!DeviceConnected(client)) {
            continue;
        }
        std::string name = fmt::format("UDP Controller{} {} {}", clients[client].active,
                                       clients[client].active == 1, client);
        devices.emplace_back(Common::ParamPackage{
            {"class", "cemuhookudp"},
            {"display", std::move(name)},
            {"port", std::to_string(client)},
        });
    }
    return devices;
}

bool Client::DeviceConnected(std::size_t pad) const {
    // Use last timestamp to detect if the socket has stopped sending data
    const auto now = std::chrono::system_clock::now();
    u64 time_difference =
        std::chrono::duration_cast<std::chrono::milliseconds>(now - clients[pad].last_motion_update)
            .count();
    return time_difference < 1000 && clients[pad].active == 1;
}

void Client::ReloadUDPClient() {
    for (std::size_t client = 0; client < clients.size(); client++) {
        ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client);
    }
}
void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) {
    // client number must be determined from host / port and pad index
    std::size_t client = pad_index;
    clients[client].socket->Stop();
    clients[client].thread.join();
    StartCommunication(client, host, port, pad_index, client_id);
}

void Client::OnVersion(Response::Version data) {
    LOG_TRACE(Input, "Version packet received: {}", data.version);
}

void Client::OnPortInfo(Response::PortInfo data) {
    LOG_TRACE(Input, "PortInfo packet received: {}", data.model);
}

void Client::OnPadData(Response::PadData data) {
    // client number must be determined from host / port and pad index
    std::size_t client = data.info.id;
    LOG_TRACE(Input, "PadData packet received");
    if (data.packet_counter == clients[client].packet_sequence) {
        LOG_WARNING(
            Input,
            "PadData packet dropped because its stale info. Current count: {} Packet count: {}",
            clients[client].packet_sequence, data.packet_counter);
        return;
    }
    clients[client].active = data.info.is_pad_active;
    clients[client].packet_sequence = data.packet_counter;
    const auto now = std::chrono::system_clock::now();
    u64 time_difference = std::chrono::duration_cast<std::chrono::microseconds>(
                              now - clients[client].last_motion_update)
                              .count();
    clients[client].last_motion_update = now;
    Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw};
    clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y});
    // Gyroscope values are not it the correct scale from better joy.
    // By dividing by 312 allow us to make one full turn = 1 turn
    // This must be a configurable valued called sensitivity
    clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f);
    clients[client].motion.UpdateRotation(time_difference);
    clients[client].motion.UpdateOrientation(time_difference);
    Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
    Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
    Common::Vec3f rotation = clients[client].motion.GetRotations();
    std::array<Common::Vec3f, 3> orientation = clients[client].motion.GetOrientation();

    {
        std::lock_guard guard(clients[client].status.update_mutex);
        clients[client].status.motion_status = {accelerometer, gyroscope, rotation, orientation};

        // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates
        // between a simple "tap" and a hard press that causes the touch screen to click.
        const bool is_active = data.touch_1.is_active != 0;

        float x = 0;
        float y = 0;

        if (is_active && clients[client].status.touch_calibration) {
            const u16 min_x = clients[client].status.touch_calibration->min_x;
            const u16 max_x = clients[client].status.touch_calibration->max_x;
            const u16 min_y = clients[client].status.touch_calibration->min_y;
            const u16 max_y = clients[client].status.touch_calibration->max_y;

            x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) /
                static_cast<float>(max_x - min_x);
            y = (std::clamp(static_cast<u16>(data.touch_1.y), min_y, max_y) - min_y) /
                static_cast<float>(max_y - min_y);
        }

        clients[client].status.touch_status = {x, y, is_active};

        if (configuring) {
            UpdateYuzuSettings(client, accelerometer, gyroscope, is_active);
        }
    }
}

void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index,
                                u32 client_id) {
    SocketCallback callback{[this](Response::Version version) { OnVersion(version); },
                            [this](Response::PortInfo info) { OnPortInfo(info); },
                            [this](Response::PadData data) { OnPadData(data); }};
    LOG_INFO(Input, "Starting communication with UDP input server on {}:{}", host, port);
    clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback);
    clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()};
}

void Client::Reset() {
    for (std::size_t client = 0; client < clients.size(); client++) {
        clients[client].socket->Stop();
        clients[client].thread.join();
    }
}

void Client::UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc,
                                const Common::Vec3<float>& gyro, bool touch) {
    if (configuring) {
        UDPPadStatus pad;
        if (touch) {
            pad.touch = PadTouch::Click;
            pad_queue[client].Push(pad);
        }
        for (size_t i = 0; i < 3; ++i) {
            if (gyro[i] > 6.0f || gyro[i] < -6.0f) {
                pad.motion = static_cast<PadMotion>(i);
                pad.motion_value = gyro[i];
                pad_queue[client].Push(pad);
            }
            if (acc[i] > 2.0f || acc[i] < -2.0f) {
                pad.motion = static_cast<PadMotion>(i + 3);
                pad.motion_value = acc[i];
                pad_queue[client].Push(pad);
            }
        }
    }
}

void Client::BeginConfiguration() {
    for (auto& pq : pad_queue) {
        pq.Clear();
    }
    configuring = true;
}

void Client::EndConfiguration() {
    for (auto& pq : pad_queue) {
        pq.Clear();
    }
    configuring = false;
}

DeviceStatus& Client::GetPadState(std::size_t pad) {
    return clients[pad].status;
}

const DeviceStatus& Client::GetPadState(std::size_t pad) const {
    return clients[pad].status;
}

std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() {
    return pad_queue;
}

const std::array<Common::SPSCQueue<UDPPadStatus>, 4>& Client::GetPadQueue() const {
    return pad_queue;
}

void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id,
                       std::function<void()> success_callback,
                       std::function<void()> failure_callback) {
    std::thread([=] {
        Common::Event success_event;
        SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
                                [&](Response::PadData data) { success_event.Set(); }};
        Socket socket{host, port, pad_index, client_id, std::move(callback)};
        std::thread worker_thread{SocketLoop, &socket};
        bool result = success_event.WaitFor(std::chrono::seconds(8));
        socket.Stop();
        worker_thread.join();
        if (result) {
            success_callback();
        } else {
            failure_callback();
        }
    }).detach();
}

CalibrationConfigurationJob::CalibrationConfigurationJob(
    const std::string& host, u16 port, u8 pad_index, u32 client_id,
    std::function<void(Status)> status_callback,
    std::function<void(u16, u16, u16, u16)> data_callback) {

    std::thread([=, this] {
        constexpr u16 CALIBRATION_THRESHOLD = 100;

        u16 min_x{UINT16_MAX};
        u16 min_y{UINT16_MAX};
        u16 max_x{};
        u16 max_y{};

        Status current_status{Status::Initialized};
        SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
                                [&](Response::PadData data) {
                                    if (current_status == Status::Initialized) {
                                        // Receiving data means the communication is ready now
                                        current_status = Status::Ready;
                                        status_callback(current_status);
                                    }
                                    if (!data.touch_1.is_active) {
                                        return;
                                    }
                                    LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x,
                                              data.touch_1.y);
                                    min_x = std::min(min_x, static_cast<u16>(data.touch_1.x));
                                    min_y = std::min(min_y, static_cast<u16>(data.touch_1.y));
                                    if (current_status == Status::Ready) {
                                        // First touch - min data (min_x/min_y)
                                        current_status = Status::Stage1Completed;
                                        status_callback(current_status);
                                    }
                                    if (data.touch_1.x - min_x > CALIBRATION_THRESHOLD &&
                                        data.touch_1.y - min_y > CALIBRATION_THRESHOLD) {
                                        // Set the current position as max value and finishes
                                        // configuration
                                        max_x = data.touch_1.x;
                                        max_y = data.touch_1.y;
                                        current_status = Status::Completed;
                                        data_callback(min_x, min_y, max_x, max_y);
                                        status_callback(current_status);

                                        complete_event.Set();
                                    }
                                }};
        Socket socket{host, port, pad_index, client_id, std::move(callback)};
        std::thread worker_thread{SocketLoop, &socket};
        complete_event.Wait();
        socket.Stop();
        worker_thread.join();
    }).detach();
}

CalibrationConfigurationJob::~CalibrationConfigurationJob() {
    Stop();
}

void CalibrationConfigurationJob::Stop() {
    complete_event.Set();
}

} // namespace InputCommon::CemuhookUDP