2019-09-06 09:09:30 -06:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-09-06 09:09:30 -06:00
|
|
|
|
2019-11-22 13:43:33 -07:00
|
|
|
#pragma once
|
|
|
|
|
2019-11-22 14:07:12 -07:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
2019-09-06 09:09:30 -06:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2021-12-10 14:34:40 -07:00
|
|
|
#include "Common/Hash.h"
|
2019-09-06 09:09:30 -06:00
|
|
|
|
2019-10-26 10:05:16 -06:00
|
|
|
namespace ciface::DualShockUDPClient::Proto
|
2019-09-06 09:09:30 -06:00
|
|
|
{
|
2019-10-26 10:05:16 -06:00
|
|
|
// CemuHook DualShockUDP protocol implementation using UdpServer.cs from
|
2019-09-06 09:09:30 -06:00
|
|
|
// https://github.com/Ryochan7/DS4Windows as documentation.
|
|
|
|
//
|
|
|
|
// WARNING: Little endian host assumed
|
|
|
|
|
2019-11-22 14:07:12 -07:00
|
|
|
constexpr u16 CEMUHOOK_PROTOCOL_VERSION = 1001;
|
2019-09-06 09:09:30 -06:00
|
|
|
|
2019-11-22 14:07:12 -07:00
|
|
|
constexpr int PORT_COUNT = 4;
|
2019-09-06 09:09:30 -06:00
|
|
|
|
2019-11-22 14:07:12 -07:00
|
|
|
constexpr std::array<u8, 4> CLIENT{'D', 'S', 'U', 'C'};
|
|
|
|
constexpr std::array<u8, 4> SERVER{'D', 'S', 'U', 'S'};
|
2019-09-06 09:09:30 -06: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 14:44:45 -06:00
|
|
|
PartialGyro = 1,
|
|
|
|
FullGyro = 2,
|
2019-09-06 09:09:30 -06: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 14:07:12 -07:00
|
|
|
std::array<u8, 4> source;
|
2019-09-06 09:09:30 -06: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 14:07:12 -07:00
|
|
|
std::array<u8, 2> padding;
|
2019-09-06 09:09:30 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ListPorts
|
|
|
|
{
|
|
|
|
static constexpr auto FROM = CLIENT;
|
|
|
|
static constexpr auto TYPE = 0x100001U;
|
|
|
|
MessageHeader header;
|
|
|
|
u32 message_type;
|
|
|
|
u32 pad_request_count;
|
2021-05-15 03:28:52 -06:00
|
|
|
std::array<u8, 4> pad_ids;
|
2019-09-06 09:09:30 -06: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 14:07:12 -07:00
|
|
|
std::array<u8, 6> pad_mac_address;
|
2019-09-06 09:09:30 -06: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 14:07:12 -07:00
|
|
|
std::array<u8, 6> mac_address_to_register;
|
2019-09-06 09:09:30 -06: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 14:07:12 -07:00
|
|
|
std::array<u8, 6> pad_mac_address;
|
2019-09-06 09:09:30 -06: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;
|
2021-05-15 03:28:52 -06:00
|
|
|
u64 accelerometer_timestamp_us;
|
2019-09-06 09:09:30 -06:00
|
|
|
float accelerometer_x_g;
|
|
|
|
float accelerometer_y_g;
|
2019-11-17 16:10:35 -07:00
|
|
|
float accelerometer_z_g;
|
2019-09-06 09:09:30 -06: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
|
|
|
|
|
|
|
|
template <typename MsgType>
|
|
|
|
struct Message
|
|
|
|
{
|
2019-11-22 14:07:12 -07:00
|
|
|
Message() = default;
|
2019-09-06 09:09:30 -06:00
|
|
|
|
2019-11-22 14:07:12 -07:00
|
|
|
explicit Message(u32 source_uid)
|
2019-09-06 09:09:30 -06:00
|
|
|
{
|
2019-11-22 14:07:12 -07:00
|
|
|
m_message.header.source = MsgType::FROM;
|
2019-09-06 09:09:30 -06: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;
|
|
|
|
}
|
|
|
|
|
2021-12-10 14:34:40 -07:00
|
|
|
void Finish()
|
|
|
|
{
|
|
|
|
m_message.header.crc32 =
|
|
|
|
Common::ComputeCRC32(reinterpret_cast<const u8*>(&m_message), sizeof(m_message));
|
|
|
|
}
|
2019-09-06 09:09:30 -06:00
|
|
|
|
|
|
|
template <class ToMsgType>
|
|
|
|
std::optional<ToMsgType> CheckAndCastTo()
|
|
|
|
{
|
2020-10-22 15:49:29 -06:00
|
|
|
const u32 crc32_in_header = m_message.header.crc32;
|
2019-09-06 09:09:30 -06:00
|
|
|
// zero out the crc32 in the packet once we got it since that's whats needed for calculation
|
|
|
|
m_message.header.crc32 = 0;
|
2021-12-10 14:34:40 -07:00
|
|
|
const u32 crc32_calculated =
|
|
|
|
Common::ComputeCRC32(reinterpret_cast<const u8*>(&m_message), sizeof(ToMsgType));
|
2019-09-06 09:09:30 -06:00
|
|
|
if (crc32_in_header != crc32_calculated)
|
|
|
|
{
|
2020-10-22 15:49:29 -06:00
|
|
|
NOTICE_LOG_FMT(
|
2021-05-04 14:37:06 -06:00
|
|
|
CONTROLLERINTERFACE,
|
2020-10-22 15:49:29 -06:00
|
|
|
"DualShockUDPClient Received message with bad CRC in header: got {:08x}, expected {:08x}",
|
|
|
|
crc32_in_header, crc32_calculated);
|
2019-09-06 09:09:30 -06:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
if (m_message.header.protocol_version > CEMUHOOK_PROTOCOL_VERSION)
|
|
|
|
return std::nullopt;
|
2019-11-22 14:07:12 -07:00
|
|
|
if (m_message.header.source != ToMsgType::FROM)
|
2019-09-06 09:09:30 -06: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 14:07:12 -07:00
|
|
|
MsgType m_message{};
|
2019-09-06 09:09:30 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
2019-10-26 10:05:16 -06:00
|
|
|
} // namespace ciface::DualShockUDPClient::Proto
|