Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/DualShockUDPClient/DualShockUDPProto.h

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <cstring>
8 #include <optional>
9
10 #include "Common/CommonTypes.h"
11 #include "Common/Hash.h"
12 #include "Common/Logging/Log.h"
13
14 namespace ciface::DualShockUDPClient::Proto
15 {
16 // CemuHook DualShockUDP protocol implementation using UdpServer.cs from
17 // https://github.com/Ryochan7/DS4Windows as documentation.
18 //
19 // WARNING: Little endian host assumed
20
21 constexpr u16 CEMUHOOK_PROTOCOL_VERSION = 1001;
22
23 constexpr int PORT_COUNT = 4;
24
25 constexpr std::array<u8, 4> CLIENT{'D', 'S', 'U', 'C'};
26 constexpr std::array<u8, 4> SERVER{'D', 'S', 'U', 'S'};
27
28 #pragma pack(push, 1)
29
30 enum class DsState : u8
31 {
32 Disconnected = 0x00,
33 Reserved = 0x01,
34 Connected = 0x02
35 };
36
37 enum class DsConnection : u8
38 {
39 None = 0x00,
40 Usb = 0x01,
41 Bluetooth = 0x02
42 };
43
44 enum class DsModel : u8
45 {
46 None = 0,
47 PartialGyro = 1,
48 FullGyro = 2,
49 Generic = 3
50 };
51
52 enum class DsBattery : u8
53 {
54 None = 0x00,
55 Dying = 0x01,
56 Low = 0x02,
57 Medium = 0x03,
58 High = 0x04,
59 Full = 0x05,
60 Charging = 0xEE,
61 Charged = 0xEF
62 };
63
64 enum RegisterFlags : u8
65 {
66 AllPads = 0x00,
67 PadID = 0x01,
68 PadMACAdddress = 0x02
69 };
70
71 struct MessageHeader
72 {
73 std::array<u8, 4> source;
74 u16 protocol_version;
75 u16 message_length; // actually message size minus header size
76 u32 crc32;
77 u32 source_uid;
78 };
79
80 struct Touch
81 {
82 u8 active;
83 u8 id;
84 s16 x;
85 s16 y;
86 };
87
88 namespace MessageType
89 {
90 struct VersionRequest
91 {
92 static constexpr auto FROM = CLIENT;
93 static constexpr auto TYPE = 0x100000U;
94 MessageHeader header;
95 u32 message_type;
96 };
97
98 struct VersionResponse
99 {
100 static constexpr auto FROM = SERVER;
101 static constexpr auto TYPE = 0x100000U;
102 MessageHeader header;
103 u32 message_type;
104 u16 max_protocol_version;
105 std::array<u8, 2> padding;
106 };
107
108 struct ListPorts
109 {
110 static constexpr auto FROM = CLIENT;
111 static constexpr auto TYPE = 0x100001U;
112 MessageHeader header;
113 u32 message_type;
114 u32 pad_request_count;
115 std::array<u8, 4> pad_ids;
116 };
117
118 struct PortInfo
119 {
120 static constexpr auto FROM = SERVER;
121 static constexpr auto TYPE = 0x100001U;
122 MessageHeader header;
123 u32 message_type;
124 u8 pad_id;
125 DsState pad_state;
126 DsModel model;
127 DsConnection connection_type;
128 std::array<u8, 6> pad_mac_address;
129 DsBattery battery_status;
130 u8 padding;
131 };
132
133 struct PadDataRequest
134 {
135 static constexpr auto FROM = CLIENT;
136 static constexpr auto TYPE = 0x100002U;
137 MessageHeader header;
138 u32 message_type;
139 RegisterFlags register_flags;
140 u8 pad_id_to_register;
141 std::array<u8, 6> mac_address_to_register;
142 };
143
144 struct PadDataResponse
145 {
146 static constexpr auto FROM = SERVER;
147 static constexpr auto TYPE = 0x100002U;
148 MessageHeader header;
149 u32 message_type;
150 u8 pad_id;
151 DsState pad_state;
152 DsModel model;
153 DsConnection connection_type;
154 std::array<u8, 6> pad_mac_address;
155 DsBattery battery_status;
156 u8 active;
157 u32 hid_packet_counter;
158 u8 button_states1;
159 u8 button_states2;
160 u8 button_ps;
161 u8 button_touch;
162 u8 left_stick_x;
163 u8 left_stick_y_inverted;
164 u8 right_stick_x;
165 u8 right_stick_y_inverted;
166 u8 button_dpad_left_analog;
167 u8 button_dpad_down_analog;
168 u8 button_dpad_right_analog;
169 u8 button_dpad_up_analog;
170 u8 button_square_analog;
171 u8 button_cross_analog;
172 u8 button_circle_analog;
173 u8 button_triangle_analog;
174 u8 button_r1_analog;
175 u8 button_l1_analog;
176 u8 trigger_r2;
177 u8 trigger_l2;
178 Touch touch1;
179 Touch touch2;
180 u64 accelerometer_timestamp_us;
181 float accelerometer_x_g;
182 float accelerometer_y_g;
183 float accelerometer_z_g;
184 float gyro_pitch_deg_s;
185 float gyro_yaw_deg_s;
186 float gyro_roll_deg_s;
187 };
188
189 struct FromServer
190 {
191 union
192 {
193 struct
194 {
195 MessageHeader header;
196 u32 message_type;
197 };
198 MessageType::VersionResponse version_response;
199 MessageType::PortInfo port_info;
200 MessageType::PadDataResponse pad_data_response;
201 };
202 };
203
204 struct FromClient
205 {
206 union
207 {
208 struct
209 {
210 MessageHeader header;
211 u32 message_type;
212 };
213 MessageType::VersionRequest version_request;
214 MessageType::ListPorts list_ports;
215 MessageType::PadDataRequest pad_data_request;
216 };
217 };
218 } // namespace MessageType
219
220 template <typename MsgType>
221 struct Message
222 {
223 Message() = default;
224
225 explicit Message(u32 source_uid)
226 {
227 m_message.header.source = MsgType::FROM;
228 m_message.header.protocol_version = CEMUHOOK_PROTOCOL_VERSION;
229 m_message.header.message_length = sizeof(*this) - sizeof(m_message.header);
230 m_message.header.source_uid = source_uid;
231 m_message.message_type = MsgType::TYPE;
232 }
233
234 void Finish()
235 {
236 m_message.header.crc32 =
237 Common::ComputeCRC32(reinterpret_cast<const u8*>(&m_message), sizeof(m_message));
238 }
239
240 template <class ToMsgType>
241 std::optional<ToMsgType> CheckAndCastTo()
242 {
243 const u32 crc32_in_header = m_message.header.crc32;
244 // zero out the crc32 in the packet once we got it since that's what's needed for calculation
245 m_message.header.crc32 = 0;
246 const u32 crc32_calculated =
247 Common::ComputeCRC32(reinterpret_cast<const u8*>(&m_message), sizeof(ToMsgType));
248 if (crc32_in_header != crc32_calculated)
249 {
250 NOTICE_LOG_FMT(
251 CONTROLLERINTERFACE,
252 "DualShockUDPClient Received message with bad CRC in header: got {:08x}, expected {:08x}",
253 crc32_in_header, crc32_calculated);
254 return std::nullopt;
255 }
256 if (m_message.header.protocol_version > CEMUHOOK_PROTOCOL_VERSION)
257 return std::nullopt;
258 if (m_message.header.source != ToMsgType::FROM)
259 return std::nullopt;
260 if (m_message.message_type != ToMsgType::TYPE)
261 return std::nullopt;
262 if (m_message.header.message_length + sizeof(m_message.header) > sizeof(ToMsgType))
263 return std::nullopt;
264
265 ToMsgType tomsg;
266 memcpy(&tomsg, &m_message, sizeof(tomsg));
267 return tomsg;
268 }
269
270 MsgType m_message{};
271 };
272
273 #pragma pack(pop)
274 } // namespace ciface::DualShockUDPClient::Proto