udp_protocol.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-FileCopyrightText: 2018 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <optional>
  6. #include <type_traits>
  7. #ifdef _MSC_VER
  8. #pragma warning(push)
  9. #pragma warning(disable : 4701) // Potentially uninitialized local variable 'result' used
  10. #endif
  11. #include <boost/crc.hpp>
  12. #ifdef _MSC_VER
  13. #pragma warning(pop)
  14. #endif
  15. #include "common/swap.h"
  16. namespace InputCommon::CemuhookUDP {
  17. constexpr std::size_t MAX_PACKET_SIZE = 100;
  18. constexpr u16 PROTOCOL_VERSION = 1001;
  19. constexpr u32 CLIENT_MAGIC = 0x43555344; // DSUC (but flipped for LE)
  20. constexpr u32 SERVER_MAGIC = 0x53555344; // DSUS (but flipped for LE)
  21. enum class Type : u32 {
  22. Version = 0x00100000,
  23. PortInfo = 0x00100001,
  24. PadData = 0x00100002,
  25. };
  26. struct Header {
  27. u32_le magic{};
  28. u16_le protocol_version{};
  29. u16_le payload_length{};
  30. u32_le crc{};
  31. u32_le id{};
  32. ///> In the protocol, the type of the packet is not part of the header, but its convenient to
  33. ///> include in the header so the callee doesn't have to duplicate the type twice when building
  34. ///> the data
  35. Type type{};
  36. };
  37. static_assert(sizeof(Header) == 20, "UDP Message Header struct has wrong size");
  38. static_assert(std::is_trivially_copyable_v<Header>, "UDP Message Header is not trivially copyable");
  39. using MacAddress = std::array<u8, 6>;
  40. constexpr MacAddress EMPTY_MAC_ADDRESS = {0, 0, 0, 0, 0, 0};
  41. #pragma pack(push, 1)
  42. template <typename T>
  43. struct Message {
  44. Header header{};
  45. T data;
  46. };
  47. #pragma pack(pop)
  48. template <typename T>
  49. constexpr Type GetMessageType();
  50. template <typename T>
  51. Message<T> CreateMessage(const u32 magic, const T data, const u32 sender_id) {
  52. boost::crc_32_type crc;
  53. Header header{
  54. magic, PROTOCOL_VERSION, sizeof(T) + sizeof(Type), 0, sender_id, GetMessageType<T>(),
  55. };
  56. Message<T> message{header, data};
  57. crc.process_bytes(&message, sizeof(Message<T>));
  58. message.header.crc = crc.checksum();
  59. return message;
  60. }
  61. namespace Request {
  62. enum RegisterFlags : u8 {
  63. AllPads,
  64. PadID,
  65. PadMACAdddress,
  66. };
  67. struct Version {};
  68. /**
  69. * Requests the server to send information about what controllers are plugged into the ports
  70. * In yuzu's case, we only have one controller, so for simplicity's sake, we can just send a
  71. * request explicitly for the first controller port and leave it at that. In the future it would be
  72. * nice to make this configurable
  73. */
  74. constexpr u32 MAX_PORTS = 4;
  75. struct PortInfo {
  76. u32_le pad_count{}; ///> Number of ports to request data for
  77. std::array<u8, MAX_PORTS> port;
  78. };
  79. static_assert(std::is_trivially_copyable_v<PortInfo>,
  80. "UDP Request PortInfo is not trivially copyable");
  81. /**
  82. * Request the latest pad information from the server. If the server hasn't received this message
  83. * from the client in a reasonable time frame, the server will stop sending updates. The default
  84. * timeout seems to be 5 seconds.
  85. */
  86. struct PadData {
  87. /// Determines which method will be used as a look up for the controller
  88. RegisterFlags flags{};
  89. /// Index of the port of the controller to retrieve data about
  90. u8 port_id{};
  91. /// Mac address of the controller to retrieve data about
  92. MacAddress mac;
  93. };
  94. static_assert(sizeof(PadData) == 8, "UDP Request PadData struct has wrong size");
  95. static_assert(std::is_trivially_copyable_v<PadData>,
  96. "UDP Request PadData is not trivially copyable");
  97. /**
  98. * Creates a message with the proper header data that can be sent to the server.
  99. * @param data Request body to send
  100. * @param client_id ID of the udp client (usually not checked on the server)
  101. */
  102. template <typename T>
  103. Message<T> Create(const T data, const u32 client_id = 0) {
  104. return CreateMessage(CLIENT_MAGIC, data, client_id);
  105. }
  106. } // namespace Request
  107. namespace Response {
  108. enum class ConnectionType : u8 {
  109. None,
  110. Usb,
  111. Bluetooth,
  112. };
  113. enum class State : u8 {
  114. Disconnected,
  115. Reserved,
  116. Connected,
  117. };
  118. enum class Model : u8 {
  119. None,
  120. PartialGyro,
  121. FullGyro,
  122. Generic,
  123. };
  124. enum class Battery : u8 {
  125. None = 0x00,
  126. Dying = 0x01,
  127. Low = 0x02,
  128. Medium = 0x03,
  129. High = 0x04,
  130. Full = 0x05,
  131. Charging = 0xEE,
  132. Charged = 0xEF,
  133. };
  134. struct Version {
  135. u16_le version{};
  136. };
  137. static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size");
  138. static_assert(std::is_trivially_copyable_v<Version>,
  139. "UDP Response Version is not trivially copyable");
  140. struct PortInfo {
  141. u8 id{};
  142. State state{};
  143. Model model{};
  144. ConnectionType connection_type{};
  145. MacAddress mac;
  146. Battery battery{};
  147. u8 is_pad_active{};
  148. };
  149. static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size");
  150. static_assert(std::is_trivially_copyable_v<PortInfo>,
  151. "UDP Response PortInfo is not trivially copyable");
  152. struct TouchPad {
  153. u8 is_active{};
  154. u8 id{};
  155. u16_le x{};
  156. u16_le y{};
  157. };
  158. static_assert(sizeof(TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
  159. #pragma pack(push, 1)
  160. struct PadData {
  161. PortInfo info{};
  162. u32_le packet_counter{};
  163. u16_le digital_button{};
  164. // The following union isn't trivially copyable but we don't use this input anyway.
  165. // union DigitalButton {
  166. // u16_le button;
  167. // BitField<0, 1, u16> button_1; // Share
  168. // BitField<1, 1, u16> button_2; // L3
  169. // BitField<2, 1, u16> button_3; // R3
  170. // BitField<3, 1, u16> button_4; // Options
  171. // BitField<4, 1, u16> button_5; // Up
  172. // BitField<5, 1, u16> button_6; // Right
  173. // BitField<6, 1, u16> button_7; // Down
  174. // BitField<7, 1, u16> button_8; // Left
  175. // BitField<8, 1, u16> button_9; // L2
  176. // BitField<9, 1, u16> button_10; // R2
  177. // BitField<10, 1, u16> button_11; // L1
  178. // BitField<11, 1, u16> button_12; // R1
  179. // BitField<12, 1, u16> button_13; // Triangle
  180. // BitField<13, 1, u16> button_14; // Circle
  181. // BitField<14, 1, u16> button_15; // Cross
  182. // BitField<15, 1, u16> button_16; // Square
  183. // } digital_button;
  184. u8 home;
  185. /// If the device supports a "click" on the touchpad, this will change to 1 when a click happens
  186. u8 touch_hard_press{};
  187. u8 left_stick_x{};
  188. u8 left_stick_y{};
  189. u8 right_stick_x{};
  190. u8 right_stick_y{};
  191. struct AnalogButton {
  192. u8 button_dpad_left_analog{};
  193. u8 button_dpad_down_analog{};
  194. u8 button_dpad_right_analog{};
  195. u8 button_dpad_up_analog{};
  196. u8 button_square_analog{};
  197. u8 button_cross_analog{};
  198. u8 button_circle_analog{};
  199. u8 button_triangle_analog{};
  200. u8 button_r1_analog{};
  201. u8 button_l1_analog{};
  202. u8 trigger_r2{};
  203. u8 trigger_l2{};
  204. } analog_button;
  205. std::array<TouchPad, 2> touch;
  206. u64_le motion_timestamp;
  207. struct Accelerometer {
  208. float x{};
  209. float y{};
  210. float z{};
  211. } accel;
  212. struct Gyroscope {
  213. float pitch{};
  214. float yaw{};
  215. float roll{};
  216. } gyro;
  217. };
  218. #pragma pack(pop)
  219. static_assert(sizeof(PadData) == 80, "UDP Response PadData struct has wrong size ");
  220. static_assert(std::is_trivially_copyable_v<PadData>,
  221. "UDP Response PadData is not trivially copyable");
  222. static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE,
  223. "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>");
  224. static_assert(sizeof(PadData::AnalogButton) == 12,
  225. "UDP Response AnalogButton struct has wrong size ");
  226. static_assert(sizeof(PadData::Accelerometer) == 12,
  227. "UDP Response Accelerometer struct has wrong size ");
  228. static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size ");
  229. /**
  230. * Create a Response Message from the data
  231. * @param data array of bytes sent from the server
  232. * @return boost::none if it failed to parse or Type if it succeeded. The client can then safely
  233. * copy the data into the appropriate struct for that Type
  234. */
  235. std::optional<Type> Validate(u8* data, std::size_t size);
  236. } // namespace Response
  237. template <>
  238. constexpr Type GetMessageType<Request::Version>() {
  239. return Type::Version;
  240. }
  241. template <>
  242. constexpr Type GetMessageType<Request::PortInfo>() {
  243. return Type::PortInfo;
  244. }
  245. template <>
  246. constexpr Type GetMessageType<Request::PadData>() {
  247. return Type::PadData;
  248. }
  249. template <>
  250. constexpr Type GetMessageType<Response::Version>() {
  251. return Type::Version;
  252. }
  253. template <>
  254. constexpr Type GetMessageType<Response::PortInfo>() {
  255. return Type::PortInfo;
  256. }
  257. template <>
  258. constexpr Type GetMessageType<Response::PadData>() {
  259. return Type::PadData;
  260. }
  261. } // namespace InputCommon::CemuhookUDP