2019-09-06 15:09:30 +00:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-11-22 20:43:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-11-22 21:07:12 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
2019-09-06 15:09:30 +00:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2019-10-26 16:05:16 +00:00
|
|
|
namespace ciface::DualShockUDPClient::Proto
|
2019-09-06 15:09:30 +00:00
|
|
|
{
|
2019-10-26 16:05:16 +00:00
|
|
|
// CemuHook DualShockUDP protocol implementation using UdpServer.cs from
|
2019-09-06 15:09:30 +00:00
|
|
|
// https://github.com/Ryochan7/DS4Windows as documentation.
|
|
|
|
//
|
|
|
|
// WARNING: Little endian host assumed
|
|
|
|
|
2019-11-22 21:07:12 +00:00
|
|
|
constexpr u16 CEMUHOOK_PROTOCOL_VERSION = 1001;
|
2019-09-06 15:09:30 +00:00
|
|
|
|
2019-11-22 21:07:12 +00:00
|
|
|
constexpr int PORT_COUNT = 4;
|
2019-09-06 15:09:30 +00:00
|
|
|
|
2019-11-22 21:07:12 +00:00
|
|
|
constexpr std::array<u8, 4> CLIENT{'D', 'S', 'U', 'C'};
|
|
|
|
constexpr std::array<u8, 4> SERVER{'D', 'S', 'U', 'S'};
|
2019-09-06 15:09:30 +00:00
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
enum class DsState : u8
|
|
|
|
{
|
|
|
|
Disconnected = 0x00,
|
|
|
|
Reserved = 0x01,
|
|
|
|
Connected = 0x02
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class DsConnection : u8
|
|
|
|
{
|
|
|
|
None = 0x00,
|
|
|
|
Usb = 0x01,
|
|
|
|
Bluetooth = 0x02
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class DsModel : u8
|
|
|
|
{
|
|
|
|
None = 0,
|
2020-05-09 20:44:45 +00:00
|
|
|
PartialGyro = 1,
|
|
|
|
FullGyro = 2,
|
2019-09-06 15:09:30 +00:00
|
|
|
Generic = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class DsBattery : u8
|
|
|
|
{
|
|
|
|
None = 0x00,
|
|
|
|
Dying = 0x01,
|
|
|
|
Low = 0x02,
|
|
|
|
Medium = 0x03,
|
|
|
|
High = 0x04,
|
|
|
|
Full = 0x05,
|
|
|
|
Charging = 0xEE,
|
|
|
|
Charged = 0xEF
|
|
|
|
};
|
|
|
|
|
|
|
|
enum RegisterFlags : u8
|
|
|
|
{
|
|
|
|
AllPads = 0x00,
|
|
|
|
PadID = 0x01,
|
|
|
|
PadMACAdddress = 0x02
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MessageHeader
|
|
|
|
{
|
2019-11-22 21:07:12 +00:00
|
|
|
std::array<u8, 4> source;
|
2019-09-06 15:09:30 +00:00
|
|
|
u16 protocol_version;
|
|
|
|
u16 message_length; // actually message size minus header size
|
|
|
|
u32 crc32;
|
|
|
|
u32 source_uid;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Touch
|
|
|
|
{
|
|
|
|
u8 active;
|
|
|
|
u8 id;
|
|
|
|
s16 x;
|
|
|
|
s16 y;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace MessageType
|
|
|
|
{
|
|
|
|
struct VersionRequest
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = CLIENT;
|
|
|
|
static constexpr auto TYPE = 0x100000U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VersionResponse
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = SERVER;
|
|
|
|
static constexpr auto TYPE = 0x100000U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
u16 max_protocol_version;
|
2019-11-22 21:07:12 +00:00
|
|
|
std::array<u8, 2> padding;
|
2019-09-06 15:09:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ListPorts
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = CLIENT;
|
|
|
|
static constexpr auto TYPE = 0x100001U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
u32 pad_request_count;
|
2019-11-22 21:07:12 +00:00
|
|
|
std::array<u8, 4> pad_id;
|
2019-09-06 15:09:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PortInfo
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = SERVER;
|
|
|
|
static constexpr auto TYPE = 0x100001U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
u8 pad_id;
|
|
|
|
DsState pad_state;
|
|
|
|
DsModel model;
|
|
|
|
DsConnection connection_type;
|
2019-11-22 21:07:12 +00:00
|
|
|
std::array<u8, 6> pad_mac_address;
|
2019-09-06 15:09:30 +00:00
|
|
|
DsBattery battery_status;
|
|
|
|
u8 padding;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PadDataRequest
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = CLIENT;
|
|
|
|
static constexpr auto TYPE = 0x100002U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
RegisterFlags register_flags;
|
|
|
|
u8 pad_id_to_register;
|
2019-11-22 21:07:12 +00:00
|
|
|
std::array<u8, 6> mac_address_to_register;
|
2019-09-06 15:09:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PadDataResponse
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = SERVER;
|
|
|
|
static constexpr auto TYPE = 0x100002U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
u8 pad_id;
|
|
|
|
DsState pad_state;
|
|
|
|
DsModel model;
|
|
|
|
DsConnection connection_type;
|
2019-11-22 21:07:12 +00:00
|
|
|
std::array<u8, 6> pad_mac_address;
|
2019-09-06 15:09:30 +00:00
|
|
|
DsBattery battery_status;
|
|
|
|
u8 active;
|
|
|
|
u32 hid_packet_counter;
|
|
|
|
u8 button_states1;
|
|
|
|
u8 button_states2;
|
|
|
|
u8 button_ps;
|
|
|
|
u8 button_touch;
|
|
|
|
u8 left_stick_x;
|
|
|
|
u8 left_stick_y_inverted;
|
|
|
|
u8 right_stick_x;
|
|
|
|
u8 right_stick_y_inverted;
|
|
|
|
u8 button_dpad_left_analog;
|
|
|
|
u8 button_dpad_down_analog;
|
|
|
|
u8 button_dpad_right_analog;
|
|
|
|
u8 button_dpad_up_analog;
|
|
|
|
u8 button_square_analog;
|
|
|
|
u8 button_cross_analog;
|
|
|
|
u8 button_circle_analog;
|
|
|
|
u8 button_triangle_analog;
|
|
|
|
u8 button_r1_analog;
|
|
|
|
u8 button_l1_analog;
|
|
|
|
u8 trigger_r2;
|
|
|
|
u8 trigger_l2;
|
|
|
|
Touch touch1;
|
|
|
|
Touch touch2;
|
|
|
|
u64 timestamp_us;
|
|
|
|
float accelerometer_x_g;
|
|
|
|
float accelerometer_y_g;
|
2019-11-17 23:10:35 +00:00
|
|
|
float accelerometer_z_g;
|
2019-09-06 15:09:30 +00:00
|
|
|
float gyro_pitch_deg_s;
|
|
|
|
float gyro_yaw_deg_s;
|
|
|
|
float gyro_roll_deg_s;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FromServer
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
};
|
|
|
|
MessageType::VersionResponse version_response;
|
|
|
|
MessageType::PortInfo port_info;
|
|
|
|
MessageType::PadDataResponse pad_data_response;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FromClient
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
};
|
|
|
|
MessageType::VersionRequest version_request;
|
|
|
|
MessageType::ListPorts list_ports;
|
|
|
|
MessageType::PadDataRequest pad_data_request;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} // namespace MessageType
|
|
|
|
|
|
|
|
static inline u32 CRC32(const void* buffer, unsigned length)
|
|
|
|
{
|
|
|
|
return crc32(crc32(0L, Z_NULL, 0), static_cast<const Bytef*>(buffer), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename MsgType>
|
|
|
|
struct Message
|
|
|
|
{
|
2019-11-22 21:07:12 +00:00
|
|
|
Message() = default;
|
2019-09-06 15:09:30 +00:00
|
|
|
|
2019-11-22 21:07:12 +00:00
|
|
|
explicit Message(u32 source_uid)
|
2019-09-06 15:09:30 +00:00
|
|
|
{
|
2019-11-22 21:07:12 +00:00
|
|
|
m_message.header.source = MsgType::FROM;
|
2019-09-06 15:09:30 +00:00
|
|
|
m_message.header.protocol_version = CEMUHOOK_PROTOCOL_VERSION;
|
|
|
|
m_message.header.message_length = sizeof(*this) - sizeof(m_message.header);
|
|
|
|
m_message.header.source_uid = source_uid;
|
|
|
|
m_message.message_type = MsgType::TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Finish() { m_message.header.crc32 = CRC32(&m_message, sizeof(m_message)); }
|
|
|
|
|
|
|
|
template <class ToMsgType>
|
|
|
|
std::optional<ToMsgType> CheckAndCastTo()
|
|
|
|
{
|
2020-10-22 21:49:29 +00:00
|
|
|
const u32 crc32_in_header = m_message.header.crc32;
|
2019-09-06 15:09:30 +00:00
|
|
|
// zero out the crc32 in the packet once we got it since that's whats needed for calculation
|
|
|
|
m_message.header.crc32 = 0;
|
2020-10-22 21:49:29 +00:00
|
|
|
const u32 crc32_calculated = CRC32(&m_message, sizeof(ToMsgType));
|
2019-09-06 15:09:30 +00:00
|
|
|
if (crc32_in_header != crc32_calculated)
|
|
|
|
{
|
2020-10-22 21:49:29 +00:00
|
|
|
NOTICE_LOG_FMT(
|
|
|
|
SERIALINTERFACE,
|
|
|
|
"DualShockUDPClient Received message with bad CRC in header: got {:08x}, expected {:08x}",
|
|
|
|
crc32_in_header, crc32_calculated);
|
2019-09-06 15:09:30 +00:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
if (m_message.header.protocol_version > CEMUHOOK_PROTOCOL_VERSION)
|
|
|
|
return std::nullopt;
|
2019-11-22 21:07:12 +00:00
|
|
|
if (m_message.header.source != ToMsgType::FROM)
|
2019-09-06 15:09:30 +00:00
|
|
|
return std::nullopt;
|
|
|
|
if (m_message.message_type != ToMsgType::TYPE)
|
|
|
|
return std::nullopt;
|
|
|
|
if (m_message.header.message_length + sizeof(m_message.header) > sizeof(ToMsgType))
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
ToMsgType tomsg;
|
|
|
|
memcpy(&tomsg, &m_message, sizeof(tomsg));
|
|
|
|
return tomsg;
|
|
|
|
}
|
|
|
|
|
2019-11-22 21:07:12 +00:00
|
|
|
MsgType m_message{};
|
2019-09-06 15:09:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
2019-10-26 16:05:16 +00:00
|
|
|
} // namespace ciface::DualShockUDPClient::Proto
|