summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/input_common/udp/client.cpp24
-rw-r--r--src/input_common/udp/client.h8
-rw-r--r--src/input_common/udp/protocol.cpp4
-rw-r--r--src/input_common/udp/protocol.h101
4 files changed, 72 insertions, 65 deletions
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index c31236c7c..3c51f72a0 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -88,15 +88,15 @@ private:
88 void HandleSend(const boost::system::error_code& error) { 88 void HandleSend(const boost::system::error_code& error) {
89 // Send a request for getting port info for the pad 89 // Send a request for getting port info for the pad
90 Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; 90 Request::PortInfo port_info{1, {pad_index, 0, 0, 0}};
91 auto port_message = Request::Create(port_info, client_id); 91 const auto port_message = Request::Create(port_info, client_id);
92 std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); 92 std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE);
93 std::size_t len = socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint); 93 socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint);
94 94
95 // Send a request for getting pad data for the pad 95 // Send a request for getting pad data for the pad
96 Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; 96 Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS};
97 auto pad_message = Request::Create(pad_data, client_id); 97 const auto pad_message = Request::Create(pad_data, client_id);
98 std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); 98 std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE);
99 std::size_t len2 = socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint); 99 socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint);
100 StartSend(timer.expiry()); 100 StartSend(timer.expiry());
101 } 101 }
102 102
@@ -105,8 +105,8 @@ private:
105 boost::asio::basic_waitable_timer<clock> timer; 105 boost::asio::basic_waitable_timer<clock> timer;
106 udp::socket socket; 106 udp::socket socket;
107 107
108 u32 client_id; 108 u32 client_id{};
109 u8 pad_index; 109 u8 pad_index{};
110 110
111 static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); 111 static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>);
112 static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>); 112 static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message<Request::PadData>);
@@ -170,16 +170,16 @@ void Client::OnPadData(Response::PadData data) {
170 170
171 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 171 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates
172 // between a simple "tap" and a hard press that causes the touch screen to click. 172 // between a simple "tap" and a hard press that causes the touch screen to click.
173 bool is_active = data.touch_1.is_active != 0; 173 const bool is_active = data.touch_1.is_active != 0;
174 174
175 float x = 0; 175 float x = 0;
176 float y = 0; 176 float y = 0;
177 177
178 if (is_active && status->touch_calibration) { 178 if (is_active && status->touch_calibration) {
179 u16 min_x = status->touch_calibration->min_x; 179 const u16 min_x = status->touch_calibration->min_x;
180 u16 max_x = status->touch_calibration->max_x; 180 const u16 max_x = status->touch_calibration->max_x;
181 u16 min_y = status->touch_calibration->min_y; 181 const u16 min_y = status->touch_calibration->min_y;
182 u16 max_y = status->touch_calibration->max_y; 182 const u16 max_y = status->touch_calibration->max_y;
183 183
184 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) / 184 x = (std::clamp(static_cast<u16>(data.touch_1.x), min_x, max_x) - min_x) /
185 static_cast<float>(max_x - min_x); 185 static_cast<float>(max_x - min_x);
@@ -229,7 +229,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob(
229 constexpr u16 CALIBRATION_THRESHOLD = 100; 229 constexpr u16 CALIBRATION_THRESHOLD = 100;
230 230
231 u16 min_x{UINT16_MAX}, min_y{UINT16_MAX}; 231 u16 min_x{UINT16_MAX}, min_y{UINT16_MAX};
232 u16 max_x, max_y; 232 u16 max_x{}, max_y{};
233 233
234 Status current_status{Status::Initialized}; 234 Status current_status{Status::Initialized};
235 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {}, 235 SocketCallback callback{[](Response::Version version) {}, [](Response::PortInfo info) {},
diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h
index 5177f46be..b06a3f85a 100644
--- a/src/input_common/udp/client.h
+++ b/src/input_common/udp/client.h
@@ -36,10 +36,10 @@ struct DeviceStatus {
36 36
37 // calibration data for scaling the device's touch area to 3ds 37 // calibration data for scaling the device's touch area to 3ds
38 struct CalibrationData { 38 struct CalibrationData {
39 u16 min_x; 39 u16 min_x{};
40 u16 min_y; 40 u16 min_y{};
41 u16 max_x; 41 u16 max_x{};
42 u16 max_y; 42 u16 max_y{};
43 }; 43 };
44 std::optional<CalibrationData> touch_calibration; 44 std::optional<CalibrationData> touch_calibration;
45}; 45};
diff --git a/src/input_common/udp/protocol.cpp b/src/input_common/udp/protocol.cpp
index d65069207..16da706d5 100644
--- a/src/input_common/udp/protocol.cpp
+++ b/src/input_common/udp/protocol.cpp
@@ -9,7 +9,7 @@
9 9
10namespace InputCommon::CemuhookUDP { 10namespace InputCommon::CemuhookUDP {
11 11
12static const std::size_t GetSizeOfResponseType(Type t) { 12static constexpr std::size_t GetSizeOfResponseType(Type t) {
13 switch (t) { 13 switch (t) {
14 case Type::Version: 14 case Type::Version:
15 return sizeof(Response::Version); 15 return sizeof(Response::Version);
@@ -34,7 +34,7 @@ std::optional<Type> Validate(u8* data, std::size_t size) {
34 LOG_DEBUG(Input, "Invalid UDP packet received"); 34 LOG_DEBUG(Input, "Invalid UDP packet received");
35 return {}; 35 return {};
36 } 36 }
37 Header header; 37 Header header{};
38 std::memcpy(&header, data, sizeof(Header)); 38 std::memcpy(&header, data, sizeof(Header));
39 if (header.magic != SERVER_MAGIC) { 39 if (header.magic != SERVER_MAGIC) {
40 LOG_ERROR(Input, "UDP Packet has an unexpected magic value"); 40 LOG_ERROR(Input, "UDP Packet has an unexpected magic value");
diff --git a/src/input_common/udp/protocol.h b/src/input_common/udp/protocol.h
index d31bbeb89..1b521860a 100644
--- a/src/input_common/udp/protocol.h
+++ b/src/input_common/udp/protocol.h
@@ -26,15 +26,15 @@ enum class Type : u32 {
26}; 26};
27 27
28struct Header { 28struct Header {
29 u32_le magic; 29 u32_le magic{};
30 u16_le protocol_version; 30 u16_le protocol_version{};
31 u16_le payload_length; 31 u16_le payload_length{};
32 u32_le crc; 32 u32_le crc{};
33 u32_le id; 33 u32_le id{};
34 ///> In the protocol, the type of the packet is not part of the header, but its convenient to 34 ///> In the protocol, the type of the packet is not part of the header, but its convenient to
35 ///> include in the header so the callee doesn't have to duplicate the type twice when building 35 ///> include in the header so the callee doesn't have to duplicate the type twice when building
36 ///> the data 36 ///> the data
37 Type type; 37 Type type{};
38}; 38};
39static_assert(sizeof(Header) == 20, "UDP Message Header struct has wrong size"); 39static_assert(sizeof(Header) == 20, "UDP Message Header struct has wrong size");
40static_assert(std::is_trivially_copyable_v<Header>, "UDP Message Header is not trivially copyable"); 40static_assert(std::is_trivially_copyable_v<Header>, "UDP Message Header is not trivially copyable");
@@ -45,7 +45,7 @@ constexpr MacAddress EMPTY_MAC_ADDRESS = {0, 0, 0, 0, 0, 0};
45#pragma pack(push, 1) 45#pragma pack(push, 1)
46template <typename T> 46template <typename T>
47struct Message { 47struct Message {
48 Header header; 48 Header header{};
49 T data; 49 T data;
50}; 50};
51#pragma pack(pop) 51#pragma pack(pop)
@@ -64,7 +64,7 @@ struct Version {};
64 */ 64 */
65constexpr u32 MAX_PORTS = 4; 65constexpr u32 MAX_PORTS = 4;
66struct PortInfo { 66struct PortInfo {
67 u32_le pad_count; ///> Number of ports to request data for 67 u32_le pad_count{}; ///> Number of ports to request data for
68 std::array<u8, MAX_PORTS> port; 68 std::array<u8, MAX_PORTS> port;
69}; 69};
70static_assert(std::is_trivially_copyable_v<PortInfo>, 70static_assert(std::is_trivially_copyable_v<PortInfo>,
@@ -82,9 +82,9 @@ struct PadData {
82 Mac, 82 Mac,
83 }; 83 };
84 /// Determines which method will be used as a look up for the controller 84 /// Determines which method will be used as a look up for the controller
85 Flags flags; 85 Flags flags{};
86 /// Index of the port of the controller to retrieve data about 86 /// Index of the port of the controller to retrieve data about
87 u8 port_id; 87 u8 port_id{};
88 /// Mac address of the controller to retrieve data about 88 /// Mac address of the controller to retrieve data about
89 MacAddress mac; 89 MacAddress mac;
90}; 90};
@@ -113,20 +113,20 @@ Message<T> Create(const T data, const u32 client_id = 0) {
113namespace Response { 113namespace Response {
114 114
115struct Version { 115struct Version {
116 u16_le version; 116 u16_le version{};
117}; 117};
118static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size"); 118static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size");
119static_assert(std::is_trivially_copyable_v<Version>, 119static_assert(std::is_trivially_copyable_v<Version>,
120 "UDP Response Version is not trivially copyable"); 120 "UDP Response Version is not trivially copyable");
121 121
122struct PortInfo { 122struct PortInfo {
123 u8 id; 123 u8 id{};
124 u8 state; 124 u8 state{};
125 u8 model; 125 u8 model{};
126 u8 connection_type; 126 u8 connection_type{};
127 MacAddress mac; 127 MacAddress mac;
128 u8 battery; 128 u8 battery{};
129 u8 is_pad_active; 129 u8 is_pad_active{};
130}; 130};
131static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size"); 131static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size");
132static_assert(std::is_trivially_copyable_v<PortInfo>, 132static_assert(std::is_trivially_copyable_v<PortInfo>,
@@ -134,10 +134,10 @@ static_assert(std::is_trivially_copyable_v<PortInfo>,
134 134
135#pragma pack(push, 1) 135#pragma pack(push, 1)
136struct PadData { 136struct PadData {
137 PortInfo info; 137 PortInfo info{};
138 u32_le packet_counter; 138 u32_le packet_counter{};
139 139
140 u16_le digital_button; 140 u16_le digital_button{};
141 // The following union isn't trivially copyable but we don't use this input anyway. 141 // The following union isn't trivially copyable but we don't use this input anyway.
142 // union DigitalButton { 142 // union DigitalButton {
143 // u16_le button; 143 // u16_le button;
@@ -161,46 +161,46 @@ struct PadData {
161 161
162 u8 home; 162 u8 home;
163 /// If the device supports a "click" on the touchpad, this will change to 1 when a click happens 163 /// If the device supports a "click" on the touchpad, this will change to 1 when a click happens
164 u8 touch_hard_press; 164 u8 touch_hard_press{};
165 u8 left_stick_x; 165 u8 left_stick_x{};
166 u8 left_stick_y; 166 u8 left_stick_y{};
167 u8 right_stick_x; 167 u8 right_stick_x{};
168 u8 right_stick_y; 168 u8 right_stick_y{};
169 169
170 struct AnalogButton { 170 struct AnalogButton {
171 u8 button_8; 171 u8 button_8{};
172 u8 button_7; 172 u8 button_7{};
173 u8 button_6; 173 u8 button_6{};
174 u8 button_5; 174 u8 button_5{};
175 u8 button_12; 175 u8 button_12{};
176 u8 button_11; 176 u8 button_11{};
177 u8 button_10; 177 u8 button_10{};
178 u8 button_9; 178 u8 button_9{};
179 u8 button_16; 179 u8 button_16{};
180 u8 button_15; 180 u8 button_15{};
181 u8 button_14; 181 u8 button_14{};
182 u8 button_13; 182 u8 button_13{};
183 } analog_button; 183 } analog_button;
184 184
185 struct TouchPad { 185 struct TouchPad {
186 u8 is_active; 186 u8 is_active{};
187 u8 id; 187 u8 id{};
188 u16_le x; 188 u16_le x{};
189 u16_le y; 189 u16_le y{};
190 } touch_1, touch_2; 190 } touch_1, touch_2;
191 191
192 u64_le motion_timestamp; 192 u64_le motion_timestamp;
193 193
194 struct Accelerometer { 194 struct Accelerometer {
195 float x; 195 float x{};
196 float y; 196 float y{};
197 float z; 197 float z{};
198 } accel; 198 } accel;
199 199
200 struct Gyroscope { 200 struct Gyroscope {
201 float pitch; 201 float pitch{};
202 float yaw; 202 float yaw{};
203 float roll; 203 float roll{};
204 } gyro; 204 } gyro;
205}; 205};
206#pragma pack(pop) 206#pragma pack(pop)
@@ -212,6 +212,13 @@ static_assert(std::is_trivially_copyable_v<PadData>,
212static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE, 212static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE,
213 "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>"); 213 "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>");
214 214
215static_assert(sizeof(PadData::AnalogButton) == 12,
216 "UDP Response AnalogButton struct has wrong size ");
217static_assert(sizeof(PadData::TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
218static_assert(sizeof(PadData::Accelerometer) == 12,
219 "UDP Response Accelerometer struct has wrong size ");
220static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size ");
221
215/** 222/**
216 * Create a Response Message from the data 223 * Create a Response Message from the data
217 * @param data array of bytes sent from the server 224 * @param data array of bytes sent from the server