udp_protocol.h 8.6 KB

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