udp_protocol.h 8.3 KB

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