protocol.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. struct Version {};
  47. /**
  48. * Requests the server to send information about what controllers are plugged into the ports
  49. * In citra's case, we only have one controller, so for simplicity's sake, we can just send a
  50. * request explicitly for the first controller port and leave it at that. In the future it would be
  51. * nice to make this configurable
  52. */
  53. constexpr u32 MAX_PORTS = 4;
  54. struct PortInfo {
  55. u32_le pad_count{}; ///> Number of ports to request data for
  56. std::array<u8, MAX_PORTS> port;
  57. };
  58. static_assert(std::is_trivially_copyable_v<PortInfo>,
  59. "UDP Request PortInfo is not trivially copyable");
  60. /**
  61. * Request the latest pad information from the server. If the server hasn't received this message
  62. * from the client in a reasonable time frame, the server will stop sending updates. The default
  63. * timeout seems to be 5 seconds.
  64. */
  65. struct PadData {
  66. enum class Flags : u8 {
  67. AllPorts,
  68. Id,
  69. Mac,
  70. };
  71. /// Determines which method will be used as a look up for the controller
  72. Flags 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. struct Version {
  100. u16_le version{};
  101. };
  102. static_assert(sizeof(Version) == 2, "UDP Response Version struct has wrong size");
  103. static_assert(std::is_trivially_copyable_v<Version>,
  104. "UDP Response Version is not trivially copyable");
  105. struct PortInfo {
  106. u8 id{};
  107. u8 state{};
  108. u8 model{};
  109. u8 connection_type{};
  110. MacAddress mac;
  111. u8 battery{};
  112. u8 is_pad_active{};
  113. };
  114. static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size");
  115. static_assert(std::is_trivially_copyable_v<PortInfo>,
  116. "UDP Response PortInfo is not trivially copyable");
  117. struct TouchPad {
  118. u8 is_active{};
  119. u8 id{};
  120. u16_le x{};
  121. u16_le y{};
  122. };
  123. static_assert(sizeof(TouchPad) == 6, "UDP Response TouchPad struct has wrong size ");
  124. #pragma pack(push, 1)
  125. struct PadData {
  126. PortInfo info{};
  127. u32_le packet_counter{};
  128. u16_le digital_button{};
  129. // The following union isn't trivially copyable but we don't use this input anyway.
  130. // union DigitalButton {
  131. // u16_le button;
  132. // BitField<0, 1, u16> button_1; // Share
  133. // BitField<1, 1, u16> button_2; // L3
  134. // BitField<2, 1, u16> button_3; // R3
  135. // BitField<3, 1, u16> button_4; // Options
  136. // BitField<4, 1, u16> button_5; // Up
  137. // BitField<5, 1, u16> button_6; // Right
  138. // BitField<6, 1, u16> button_7; // Down
  139. // BitField<7, 1, u16> button_8; // Left
  140. // BitField<8, 1, u16> button_9; // L2
  141. // BitField<9, 1, u16> button_10; // R2
  142. // BitField<10, 1, u16> button_11; // L1
  143. // BitField<11, 1, u16> button_12; // R1
  144. // BitField<12, 1, u16> button_13; // Triangle
  145. // BitField<13, 1, u16> button_14; // Circle
  146. // BitField<14, 1, u16> button_15; // Cross
  147. // BitField<15, 1, u16> button_16; // Square
  148. // } digital_button;
  149. u8 home;
  150. /// If the device supports a "click" on the touchpad, this will change to 1 when a click happens
  151. u8 touch_hard_press{};
  152. u8 left_stick_x{};
  153. u8 left_stick_y{};
  154. u8 right_stick_x{};
  155. u8 right_stick_y{};
  156. struct AnalogButton {
  157. u8 button_8{};
  158. u8 button_7{};
  159. u8 button_6{};
  160. u8 button_5{};
  161. u8 button_12{};
  162. u8 button_11{};
  163. u8 button_10{};
  164. u8 button_9{};
  165. u8 button_16{};
  166. u8 button_15{};
  167. u8 button_14{};
  168. u8 button_13{};
  169. } analog_button;
  170. std::array<TouchPad, 2> touch;
  171. u64_le motion_timestamp;
  172. struct Accelerometer {
  173. float x{};
  174. float y{};
  175. float z{};
  176. } accel;
  177. struct Gyroscope {
  178. float pitch{};
  179. float yaw{};
  180. float roll{};
  181. } gyro;
  182. };
  183. #pragma pack(pop)
  184. static_assert(sizeof(PadData) == 80, "UDP Response PadData struct has wrong size ");
  185. static_assert(std::is_trivially_copyable_v<PadData>,
  186. "UDP Response PadData is not trivially copyable");
  187. static_assert(sizeof(Message<PadData>) == MAX_PACKET_SIZE,
  188. "UDP MAX_PACKET_SIZE is no longer larger than Message<PadData>");
  189. static_assert(sizeof(PadData::AnalogButton) == 12,
  190. "UDP Response AnalogButton struct has wrong size ");
  191. static_assert(sizeof(PadData::Accelerometer) == 12,
  192. "UDP Response Accelerometer struct has wrong size ");
  193. static_assert(sizeof(PadData::Gyroscope) == 12, "UDP Response Gyroscope struct has wrong size ");
  194. /**
  195. * Create a Response Message from the data
  196. * @param data array of bytes sent from the server
  197. * @return boost::none if it failed to parse or Type if it succeeded. The client can then safely
  198. * copy the data into the appropriate struct for that Type
  199. */
  200. std::optional<Type> Validate(u8* data, std::size_t size);
  201. } // namespace Response
  202. template <>
  203. constexpr Type GetMessageType<Request::Version>() {
  204. return Type::Version;
  205. }
  206. template <>
  207. constexpr Type GetMessageType<Request::PortInfo>() {
  208. return Type::PortInfo;
  209. }
  210. template <>
  211. constexpr Type GetMessageType<Request::PadData>() {
  212. return Type::PadData;
  213. }
  214. template <>
  215. constexpr Type GetMessageType<Response::Version>() {
  216. return Type::Version;
  217. }
  218. template <>
  219. constexpr Type GetMessageType<Response::PortInfo>() {
  220. return Type::PortInfo;
  221. }
  222. template <>
  223. constexpr Type GetMessageType<Response::PadData>() {
  224. return Type::PadData;
  225. }
  226. } // namespace InputCommon::CemuhookUDP