2020-08-25 19:23:38 +00:00
|
|
|
/*
|
2021-01-26 03:03:26 +00:00
|
|
|
* Copyright (c) 2020-2021 ndeadly
|
2020-08-25 19:23:38 +00:00
|
|
|
*
|
2020-08-26 22:50:34 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
2020-08-25 19:23:38 +00:00
|
|
|
*
|
2020-08-26 22:50:34 +00:00
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
2020-08-25 19:23:38 +00:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-06-02 21:24:40 +00:00
|
|
|
#pragma once
|
2021-06-20 20:39:28 +00:00
|
|
|
#include "switch_analog_stick.hpp"
|
2021-02-03 22:52:39 +00:00
|
|
|
#include "../bluetooth_mitm/bluetooth/bluetooth_types.hpp"
|
|
|
|
#include "../bluetooth_mitm/bluetooth/bluetooth_hid_report.hpp"
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
namespace ams::controller {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
constexpr auto BATTERY_MAX = 8;
|
2020-07-11 18:26:55 +00:00
|
|
|
|
2020-10-18 21:38:00 +00:00
|
|
|
enum SwitchPlayerNumber : uint8_t {
|
|
|
|
SwitchPlayerNumber_One,
|
|
|
|
SwitchPlayerNumber_Two,
|
|
|
|
SwitchPlayerNumber_Three,
|
|
|
|
SwitchPlayerNumber_Four,
|
|
|
|
SwitchPlayerNumber_Five,
|
|
|
|
SwitchPlayerNumber_Six,
|
|
|
|
SwitchPlayerNumber_Seven,
|
|
|
|
SwitchPlayerNumber_Eight,
|
|
|
|
SwitchPlayerNumber_Unknown = 0xf
|
|
|
|
};
|
|
|
|
|
2020-07-11 18:26:55 +00:00
|
|
|
struct HardwareID {
|
|
|
|
uint16_t vid;
|
|
|
|
uint16_t pid;
|
|
|
|
};
|
2020-10-04 15:16:07 +00:00
|
|
|
|
|
|
|
struct RGBColour {
|
|
|
|
uint8_t r;
|
|
|
|
uint8_t g;
|
|
|
|
uint8_t b;
|
|
|
|
} __attribute__ ((__packed__));
|
|
|
|
|
|
|
|
struct ProControllerColours {
|
|
|
|
RGBColour body;
|
|
|
|
RGBColour buttons;
|
|
|
|
RGBColour left_grip;
|
|
|
|
RGBColour right_grip;
|
|
|
|
} __attribute__ ((__packed__));
|
2020-07-01 23:34:13 +00:00
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
struct SwitchButtonData {
|
|
|
|
uint8_t Y : 1;
|
|
|
|
uint8_t X : 1;
|
|
|
|
uint8_t B : 1;
|
|
|
|
uint8_t A : 1;
|
|
|
|
uint8_t : 2; // SR, SL (Right Joy)
|
|
|
|
uint8_t R : 1;
|
|
|
|
uint8_t ZR : 1;
|
|
|
|
|
|
|
|
uint8_t minus : 1;
|
|
|
|
uint8_t plus : 1;
|
|
|
|
uint8_t rstick_press : 1;
|
|
|
|
uint8_t lstick_press : 1;
|
|
|
|
uint8_t home : 1;
|
|
|
|
uint8_t capture : 1;
|
|
|
|
uint8_t : 0;
|
|
|
|
|
|
|
|
uint8_t dpad_down : 1;
|
|
|
|
uint8_t dpad_up : 1;
|
|
|
|
uint8_t dpad_right : 1;
|
|
|
|
uint8_t dpad_left : 1;
|
|
|
|
uint8_t : 2; // SR, SL (Left Joy)
|
|
|
|
uint8_t L : 1;
|
|
|
|
uint8_t ZL : 1;
|
2020-07-11 11:43:21 +00:00
|
|
|
} __attribute__ ((__packed__));
|
2020-06-09 20:39:30 +00:00
|
|
|
|
|
|
|
struct Switch6AxisData {
|
|
|
|
uint16_t accel_x;
|
|
|
|
uint16_t accel_y;
|
|
|
|
uint16_t accel_z;
|
|
|
|
uint16_t gyro_1;
|
|
|
|
uint16_t gyro_2;
|
|
|
|
uint16_t gyro_3;
|
2020-07-11 11:43:21 +00:00
|
|
|
} __attribute__ ((__packed__));
|
2020-06-09 20:39:30 +00:00
|
|
|
|
2020-09-30 21:07:23 +00:00
|
|
|
struct SwitchRumbleData {
|
2020-12-20 16:27:58 +00:00
|
|
|
float high_band_freq;
|
|
|
|
float high_band_amp;
|
|
|
|
float low_band_freq;
|
|
|
|
float low_band_amp;
|
2020-09-30 21:07:23 +00:00
|
|
|
} __attribute__ ((__packed__));
|
|
|
|
|
2020-10-12 23:28:41 +00:00
|
|
|
enum SubCmdType : uint8_t {
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_GetControllerState = 0x00,
|
|
|
|
SubCmd_ManualPair = 0x01,
|
2020-10-14 20:11:27 +00:00
|
|
|
SubCmd_RequestDeviceInfo = 0x02,
|
|
|
|
SubCmd_SetInputReportMode = 0x03,
|
|
|
|
SubCmd_TriggersElapsedTime = 0x04,
|
2020-10-12 23:11:14 +00:00
|
|
|
SubCmd_GetPageListState = 0x05,
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_SetHciState = 0x06,
|
|
|
|
SubCmd_ResetPairingInfo = 0x07,
|
|
|
|
SubCmd_SetShipPowerState = 0x08,
|
2020-10-14 20:11:27 +00:00
|
|
|
SubCmd_SpiFlashRead = 0x10,
|
|
|
|
SubCmd_SpiFlashWrite = 0x11,
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_SpiSectorErase = 0x12,
|
|
|
|
SubCmd_ResetMcu = 0x20,
|
2020-10-14 20:11:27 +00:00
|
|
|
SubCmd_SetMcuConfig = 0x21,
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_SetMcuState = 0x22,
|
2020-10-14 20:11:27 +00:00
|
|
|
SubCmd_SetPlayerLeds = 0x30,
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_GetPlayerLeds = 0x31,
|
|
|
|
SubCmd_SetHomeLed = 0x38,
|
2020-10-14 20:11:27 +00:00
|
|
|
SubCmd_EnableImu = 0x40,
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_SetImuSensitivity = 0x41,
|
|
|
|
SubCmd_WriteImuRegisters = 0x42,
|
|
|
|
SubCmd_ReadImuRegisters = 0x43,
|
2020-10-14 20:11:27 +00:00
|
|
|
SubCmd_EnableVibration = 0x48,
|
2020-09-11 20:03:41 +00:00
|
|
|
SubCmd_GetRegulatedVoltage = 0x50,
|
2020-10-12 23:11:14 +00:00
|
|
|
SubCmd_SetGpioPinValue = 0x51,
|
|
|
|
SubCmd_GetGpioPinValue = 0x52,
|
2020-09-11 20:03:41 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 19:46:49 +00:00
|
|
|
struct SwitchSubcommand {
|
|
|
|
uint8_t id;
|
|
|
|
union {
|
|
|
|
uint8_t data[0x26];
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t address;
|
|
|
|
uint8_t size;
|
|
|
|
} spi_flash_read;
|
|
|
|
};
|
|
|
|
} __attribute__ ((__packed__));
|
|
|
|
|
2020-10-12 23:28:41 +00:00
|
|
|
struct SwitchSubcommandResponse {
|
2021-02-26 19:46:49 +00:00
|
|
|
uint8_t ack;
|
|
|
|
uint8_t id;
|
|
|
|
union {
|
|
|
|
uint8_t data[0x23];
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct {
|
|
|
|
uint8_t major;
|
|
|
|
uint8_t minor;
|
|
|
|
} fw_ver;
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t _unk0; // Always 0x02
|
|
|
|
bluetooth::Address address;
|
|
|
|
uint8_t _unk1; // Always 0x01
|
|
|
|
uint8_t _unk2; // If 01, colors in SPI are used. Otherwise default ones
|
|
|
|
} __attribute__ ((__packed__)) device_info;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
bool enabled;
|
|
|
|
} set_ship_power_state;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t address;
|
|
|
|
uint8_t size;
|
|
|
|
uint8_t data[];
|
|
|
|
} spi_flash_read;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t status;
|
|
|
|
} spi_flash_write;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t status;
|
|
|
|
} spi_sector_erase;
|
|
|
|
};
|
|
|
|
} __attribute__ ((__packed__));
|
|
|
|
|
|
|
|
struct SwitchOutputReport0x01 {
|
|
|
|
uint8_t counter;
|
2021-07-14 20:28:34 +00:00
|
|
|
struct {
|
|
|
|
uint8_t left_motor[4];
|
|
|
|
uint8_t right_motor[4];
|
|
|
|
} rumble;
|
2021-02-26 19:46:49 +00:00
|
|
|
SwitchSubcommand subcmd;
|
2020-10-12 23:28:41 +00:00
|
|
|
} __attribute__ ((__packed__));
|
|
|
|
|
2020-07-21 10:13:28 +00:00
|
|
|
struct SwitchOutputReport0x03;
|
2020-09-30 21:07:23 +00:00
|
|
|
|
|
|
|
struct SwitchOutputReport0x10 {
|
2020-10-14 23:18:25 +00:00
|
|
|
uint8_t timer;
|
2021-07-14 20:28:34 +00:00
|
|
|
struct {
|
|
|
|
uint8_t left_motor[4];
|
|
|
|
uint8_t right_motor[4];
|
|
|
|
} rumble;
|
2020-09-30 21:07:23 +00:00
|
|
|
}__attribute__ ((__packed__));
|
|
|
|
|
2020-07-21 10:13:28 +00:00
|
|
|
struct SwitchOutputReport0x11;
|
|
|
|
struct SwitchOutputReport0x12;
|
|
|
|
|
2020-07-11 13:24:00 +00:00
|
|
|
struct SwitchInputReport0x21 {
|
2020-10-12 23:28:41 +00:00
|
|
|
uint8_t timer;
|
|
|
|
uint8_t conn_info : 4;
|
|
|
|
uint8_t battery : 4;
|
|
|
|
SwitchButtonData buttons;
|
2021-06-20 20:39:28 +00:00
|
|
|
SwitchAnalogStick left_stick;
|
|
|
|
SwitchAnalogStick right_stick;
|
2020-10-12 23:28:41 +00:00
|
|
|
uint8_t vibrator;
|
|
|
|
SwitchSubcommandResponse response;
|
2020-07-11 11:43:21 +00:00
|
|
|
} __attribute__ ((__packed__));
|
2020-06-09 20:39:30 +00:00
|
|
|
|
2020-07-11 13:24:00 +00:00
|
|
|
struct SwitchInputReport0x23;
|
|
|
|
|
|
|
|
struct SwitchInputReport0x30 {
|
2020-07-09 19:21:10 +00:00
|
|
|
uint8_t timer;
|
|
|
|
uint8_t conn_info : 4;
|
|
|
|
uint8_t battery : 4;
|
|
|
|
SwitchButtonData buttons;
|
2021-06-20 20:39:28 +00:00
|
|
|
SwitchAnalogStick left_stick;
|
|
|
|
SwitchAnalogStick right_stick;
|
2020-07-09 19:21:10 +00:00
|
|
|
uint8_t vibrator;
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-09-06 16:24:41 +00:00
|
|
|
// IMU samples at 0, 5 and 10ms
|
|
|
|
Switch6AxisData motion[3];
|
2020-07-09 19:21:10 +00:00
|
|
|
} __attribute__ ((__packed__));
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-11 13:24:00 +00:00
|
|
|
struct SwitchInputReport0x31;
|
|
|
|
struct SwitchInputReport0x32;
|
|
|
|
struct SwitchInputReport0x33;
|
|
|
|
struct SwitchInputReport0x3f;
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
struct SwitchReportData {
|
|
|
|
uint8_t id;
|
|
|
|
union {
|
2021-02-26 19:46:49 +00:00
|
|
|
SwitchOutputReport0x01 output0x01;
|
2020-07-21 10:13:28 +00:00
|
|
|
//SwitchOutputReport0x03 output0x03;
|
2020-09-30 21:07:23 +00:00
|
|
|
SwitchOutputReport0x10 output0x10;
|
2020-07-21 10:13:28 +00:00
|
|
|
//SwitchOutputReport0x11 output0x11;
|
|
|
|
//SwitchOutputReport0x12 output0x12;
|
|
|
|
SwitchInputReport0x21 input0x21;
|
|
|
|
SwitchInputReport0x30 input0x30;
|
|
|
|
//SwitchInputReport0x31 input0x31;
|
|
|
|
//SwitchInputReport0x32 input0x32;
|
|
|
|
//SwitchInputReport0x33 input0x33;
|
|
|
|
//SwitchInputReport0x3f input0x3f;
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
};
|
|
|
|
} __attribute__ ((__packed__));
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-10-18 21:38:00 +00:00
|
|
|
Result LedsMaskToPlayerNumber(uint8_t led_mask, uint8_t *player_number);
|
|
|
|
|
2020-07-11 18:26:55 +00:00
|
|
|
class SwitchController {
|
|
|
|
|
|
|
|
public:
|
2020-08-26 18:52:47 +00:00
|
|
|
static constexpr const HardwareID hardware_ids[] = {
|
|
|
|
{0x057e, 0x2006}, // Official Joycon(L) Controller
|
2020-12-06 14:54:55 +00:00
|
|
|
{0x057e, 0x2007}, // Official Joycon(R) Controller/NES Online Controller
|
2020-09-17 10:11:50 +00:00
|
|
|
{0x057e, 0x2009}, // Official Switch Pro Controller
|
|
|
|
{0x057e, 0x2017} // Official SNES Online Controller
|
2020-08-26 18:52:47 +00:00
|
|
|
};
|
2020-07-11 18:26:55 +00:00
|
|
|
|
2020-08-26 18:52:47 +00:00
|
|
|
SwitchController(const bluetooth::Address *address)
|
|
|
|
: m_address(*address) { };
|
2020-07-11 18:26:55 +00:00
|
|
|
|
2021-07-09 13:20:38 +00:00
|
|
|
const bluetooth::Address& Address(void) const { return m_address; }
|
2020-07-11 18:26:55 +00:00
|
|
|
|
2021-07-09 13:20:38 +00:00
|
|
|
virtual bool IsOfficialController(void) { return true; }
|
|
|
|
virtual bool SupportsSetTsiCommand(void) { return true; }
|
2021-02-09 21:16:20 +00:00
|
|
|
|
2021-07-09 13:20:38 +00:00
|
|
|
virtual Result Initialize(void) { return ams::ResultSuccess(); }
|
2020-08-26 18:52:47 +00:00
|
|
|
virtual Result HandleIncomingReport(const bluetooth::HidReport *report);
|
|
|
|
virtual Result HandleOutgoingReport(const bluetooth::HidReport *report);
|
|
|
|
|
|
|
|
protected:
|
2021-03-14 13:24:34 +00:00
|
|
|
virtual void ApplyButtonCombos(SwitchButtonData *buttons);
|
2021-02-26 00:08:15 +00:00
|
|
|
|
2020-07-11 18:26:55 +00:00
|
|
|
bluetooth::Address m_address;
|
2021-02-25 23:57:05 +00:00
|
|
|
|
|
|
|
static bluetooth::HidReport s_input_report;
|
|
|
|
static bluetooth::HidReport s_output_report;
|
2020-07-11 18:26:55 +00:00
|
|
|
};
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|