- Dual-Core Architecture: Dedicated 1000Hz USB gaming pipeline on Core 1 and BLE/Wi-Fi/Web telemetry on Core 0. - Multi-Input BLE Host: Parsers for Xbox Wireless, Google Stadia, 8BitDo (Ultimate/Pro2/SN30), and Generic HOGP gamepads. - Multi-Console USB Emulation: Nintendo Switch, PC DirectInput, PS3, PS4, Xbox 360 (libxsm3 cryptographic challenge-response), Original Xbox, and Couch Mouse/Keyboard. - Onboard Web Portal & Captive Portal: Real-time SVG gamepad visualizer, stick deadzone calibration, button combo remapping, and Wi-Fi manager. - Low-Latency Radio Management: Interactive SoftAP toggle to drop to 0-latency gaming mode. - Dual OTA Firmware Flashing: Browser-based wireless firmware updater and automated Python OTA script. - Strict Build Quality: Clean build under -Wall -Wextra with zero warnings or errors. - Versioning & Licensing: Semantic Versioning v1.0.0 and GNU GPLv3 with Commercial Restrictions.
498 lines
18 KiB
C
498 lines
18 KiB
C
#include "xinput_device.h"
|
|
#include "ogx_descriptors.h"
|
|
#include "xsm3.h"
|
|
#include "profile_manager.h"
|
|
#include "ble_hid_host.h"
|
|
#include "esp_log.h"
|
|
#include "esp_mac.h"
|
|
#include "device/usbd_pvt.h"
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
extern void usb_gamepad_get_last_state(controller_state_t *st);
|
|
|
|
static const char *TAG = "XINPUT_DEV";
|
|
|
|
#define XINPUT_ENDPOINT_SIZE 32
|
|
#define DESC_TYPE_VENDOR 0x21
|
|
#define DESC_TYPE_SEC 0x41
|
|
|
|
/* Device Descriptor */
|
|
const tusb_desc_device_t xinput_device_desc = {
|
|
.bLength = sizeof(tusb_desc_device_t),
|
|
.bDescriptorType = TUSB_DESC_DEVICE,
|
|
.bcdUSB = 0x0200,
|
|
.bDeviceClass = 0xFF,
|
|
.bDeviceSubClass = 0xFF,
|
|
.bDeviceProtocol = 0xFF,
|
|
.bMaxPacketSize0 = 64,
|
|
.idVendor = XINPUT_VID, /* 0x045E */
|
|
.idProduct = XINPUT_PID, /* 0x028E */
|
|
.bcdDevice = 0x0114,
|
|
.iManufacturer = 0x01,
|
|
.iProduct = 0x02,
|
|
.iSerialNumber = 0x03,
|
|
.bNumConfigurations = 0x01
|
|
};
|
|
|
|
/* Configuration Descriptor (153 bytes) - Authentic Xbox 360 Wired Controller */
|
|
const uint8_t xinput_config_desc[153] = {
|
|
// Configuration descriptor (9 bytes)
|
|
0x09, 0x02, 0x99, 0x00, 0x04, 0x01, 0x00, 0xA0, 0xFA,
|
|
|
|
// ---- Interface 0: Gamepad (SubClass 0x5D, Protocol 0x01) ----
|
|
0x09, 0x04, 0x00, 0x00, 0x02, 0xFF, 0x5D, 0x01, 0x00,
|
|
0x11, 0x21, 0x00, 0x01, 0x01, 0x25, 0x81, 0x14,
|
|
0x00, 0x00, 0x00, 0x00, 0x13, 0x02, 0x08, 0x00, 0x00,
|
|
0x07, 0x05, 0x81, 0x03, 0x20, 0x00, 0x04,
|
|
0x07, 0x05, 0x02, 0x03, 0x20, 0x00, 0x08,
|
|
|
|
// ---- Interface 1: Audio (SubClass 0x5D, Protocol 0x03) ----
|
|
0x09, 0x04, 0x01, 0x00, 0x04, 0xFF, 0x5D, 0x03, 0x00,
|
|
0x1B, 0x21, 0x00, 0x01, 0x01, 0x01, 0x83, 0x40,
|
|
0x01, 0x04, 0x20, 0x16, 0x85, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x16, 0x06, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00,
|
|
0x07, 0x05, 0x83, 0x03, 0x20, 0x00, 0x02,
|
|
0x07, 0x05, 0x04, 0x03, 0x20, 0x00, 0x04,
|
|
0x07, 0x05, 0x85, 0x03, 0x20, 0x00, 0x40,
|
|
0x07, 0x05, 0x06, 0x03, 0x20, 0x00, 0x10,
|
|
|
|
// ---- Interface 2: Plugin Module (SubClass 0x5D, Protocol 0x02) ----
|
|
0x09, 0x04, 0x02, 0x00, 0x01, 0xFF, 0x5D, 0x02, 0x00,
|
|
0x09, 0x21, 0x00, 0x01, 0x01, 0x22, 0x86, 0x03, 0x00,
|
|
0x07, 0x05, 0x86, 0x03, 0x20, 0x00, 0x10,
|
|
|
|
// ---- Interface 3: Security (SubClass 0xFD, Protocol 0x13), iInterface=4 ----
|
|
0x09, 0x04, 0x03, 0x00, 0x00, 0xFF, 0xFD, 0x13, 0x04,
|
|
0x06, 0x41, 0x00, 0x01, 0x01, 0x03,
|
|
};
|
|
|
|
static char s_usb_serial[13] = "000000000000";
|
|
|
|
const char *xinput_string_desc[5] = {
|
|
(char[]){0x09, 0x04},
|
|
"\xa9Microsoft Corporation",
|
|
"Xbox 360 Controller",
|
|
s_usb_serial,
|
|
"Xbox Security Method 3, Version 1.00, \xa9 2005 Microsoft Corporation. All rights reserved."
|
|
};
|
|
|
|
/* State and Buffers */
|
|
static uint8_t s_endpoint_in = 0xFF;
|
|
static uint8_t s_endpoint_out = 0xFF;
|
|
static uint8_t s_ep_in_buffer[XINPUT_ENDPOINT_SIZE];
|
|
static uint8_t s_ep_out_buffer[XINPUT_ENDPOINT_SIZE];
|
|
|
|
typedef enum {
|
|
XSM3_AUTH_STATE_IDLE = 0,
|
|
XSM3_AUTH_STATE_RESPONDED = 2,
|
|
XSM3_AUTH_STATE_AUTHENTICATED = 4,
|
|
} xsm3_auth_state_t;
|
|
|
|
static volatile xsm3_auth_state_t s_xsm3_auth_state = XSM3_AUTH_STATE_IDLE;
|
|
static uint8_t s_xsm3_identification[0x1D];
|
|
static uint8_t s_xsm3_buf_82[0x22];
|
|
static uint8_t s_xsm3_buf_87[0x16];
|
|
static uint8_t s_xsm3_state_buf[2] = { 0x01, 0x00 };
|
|
static bool s_identity_initialized = false;
|
|
|
|
static void init_xsm3_identity(void)
|
|
{
|
|
if (s_identity_initialized) return;
|
|
|
|
xsm3_initialise_state();
|
|
|
|
uint8_t mac[6];
|
|
esp_efuse_mac_get_default(mac);
|
|
snprintf(s_usb_serial, sizeof(s_usb_serial), "%02X%02X%02X%02X%02X%02X",
|
|
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
|
|
memcpy(s_xsm3_identification, xsm3_id_data_ms_controller, sizeof(s_xsm3_identification));
|
|
memcpy(&s_xsm3_identification[0x05], s_usb_serial, 12);
|
|
|
|
/* Calculate XOR checksum of packet starting at offset 5 */
|
|
uint8_t packet_end = s_xsm3_identification[4] + 5;
|
|
uint8_t checksum = 0;
|
|
for (uint8_t i = 5; i < packet_end; ++i) {
|
|
checksum ^= s_xsm3_identification[i];
|
|
}
|
|
s_xsm3_identification[0x1C] = checksum;
|
|
|
|
xsm3_set_identification_data(s_xsm3_identification);
|
|
s_identity_initialized = true;
|
|
|
|
ESP_LOGI(TAG, "XSM3 Identity ready: Serial=%s Checksum=0x%02X", s_usb_serial, checksum);
|
|
}
|
|
|
|
void xinput_device_init(void)
|
|
{
|
|
s_endpoint_in = 0xFF;
|
|
s_endpoint_out = 0xFF;
|
|
s_xsm3_auth_state = XSM3_AUTH_STATE_IDLE;
|
|
memset(s_ep_in_buffer, 0, sizeof(s_ep_in_buffer));
|
|
memset(s_ep_out_buffer, 0, sizeof(s_ep_out_buffer));
|
|
|
|
init_xsm3_identity();
|
|
}
|
|
|
|
static bool xinput_deinit(void)
|
|
{
|
|
s_endpoint_in = 0xFF;
|
|
s_endpoint_out = 0xFF;
|
|
return true;
|
|
}
|
|
|
|
static void xinput_reset(uint8_t rhport)
|
|
{
|
|
(void)rhport;
|
|
s_endpoint_in = 0xFF;
|
|
s_endpoint_out = 0xFF;
|
|
s_xsm3_auth_state = XSM3_AUTH_STATE_IDLE;
|
|
}
|
|
|
|
static uint16_t xinput_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
|
|
{
|
|
/* Claim vendor-class interfaces: Xbox 360 (0xFF) and Original Xbox (0x58) */
|
|
if (itf_desc->bInterfaceClass != 0xFF && itf_desc->bInterfaceClass != 0x58) {
|
|
return 0;
|
|
}
|
|
|
|
uint16_t drv_len = sizeof(tusb_desc_interface_t);
|
|
uint8_t const *p_desc = tu_desc_next((uint8_t const*)itf_desc);
|
|
|
|
/* Original Xbox: Interface Class 0x58, SubClass 0x42 */
|
|
if (itf_desc->bInterfaceClass == 0x58 && itf_desc->bInterfaceSubClass == 0x42) {
|
|
for (uint8_t i = 0; i < itf_desc->bNumEndpoints; i++) {
|
|
tusb_desc_endpoint_t const *ep_desc = (tusb_desc_endpoint_t const *)p_desc;
|
|
if (TUSB_DESC_ENDPOINT != tu_desc_type(ep_desc)) break;
|
|
TU_VERIFY(usbd_edpt_open(rhport, ep_desc), 0);
|
|
if (tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN) {
|
|
s_endpoint_in = ep_desc->bEndpointAddress;
|
|
} else {
|
|
s_endpoint_out = ep_desc->bEndpointAddress;
|
|
}
|
|
drv_len += sizeof(tusb_desc_endpoint_t);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
if (s_endpoint_out != 0xFF) {
|
|
usbd_edpt_xfer(rhport, s_endpoint_out, s_ep_out_buffer, XINPUT_ENDPOINT_SIZE, false);
|
|
}
|
|
TU_VERIFY(max_len >= drv_len, 0);
|
|
return drv_len;
|
|
}
|
|
|
|
/* Interface 0: Gamepad (0x5D/0x01) — open EP 0x81 (IN) & EP 0x02 (OUT) */
|
|
if (itf_desc->bInterfaceSubClass == 0x5D && itf_desc->bInterfaceProtocol == 0x01) {
|
|
if (p_desc[1] == DESC_TYPE_VENDOR) {
|
|
drv_len += tu_desc_len(p_desc);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
for (uint8_t i = 0; i < itf_desc->bNumEndpoints; i++) {
|
|
tusb_desc_endpoint_t const *ep_desc = (tusb_desc_endpoint_t const *)p_desc;
|
|
if (TUSB_DESC_ENDPOINT != tu_desc_type(ep_desc)) break;
|
|
TU_VERIFY(usbd_edpt_open(rhport, ep_desc), 0);
|
|
if (tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN) {
|
|
s_endpoint_in = ep_desc->bEndpointAddress;
|
|
} else {
|
|
s_endpoint_out = ep_desc->bEndpointAddress;
|
|
}
|
|
drv_len += sizeof(tusb_desc_endpoint_t);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
if (s_endpoint_out != 0xFF) {
|
|
usbd_edpt_xfer(rhport, s_endpoint_out, s_ep_out_buffer, XINPUT_ENDPOINT_SIZE, false);
|
|
}
|
|
TU_VERIFY(max_len >= drv_len, 0);
|
|
return drv_len;
|
|
}
|
|
|
|
/* Interface 1: Audio (0x5D/0x03) — skip endpoints (stub) */
|
|
if (itf_desc->bInterfaceSubClass == 0x5D && itf_desc->bInterfaceProtocol == 0x03) {
|
|
if (p_desc[1] == DESC_TYPE_VENDOR) {
|
|
drv_len += tu_desc_len(p_desc);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
for (uint8_t i = 0; i < itf_desc->bNumEndpoints; i++) {
|
|
drv_len += sizeof(tusb_desc_endpoint_t);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
TU_VERIFY(max_len >= drv_len, 0);
|
|
return drv_len;
|
|
}
|
|
|
|
/* Interface 2: Plugin (0x5D/0x02) — skip endpoints (stub) */
|
|
if (itf_desc->bInterfaceSubClass == 0x5D && itf_desc->bInterfaceProtocol == 0x02) {
|
|
if (p_desc[1] == DESC_TYPE_VENDOR) {
|
|
drv_len += tu_desc_len(p_desc);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
for (uint8_t i = 0; i < itf_desc->bNumEndpoints; i++) {
|
|
drv_len += sizeof(tusb_desc_endpoint_t);
|
|
p_desc = tu_desc_next(p_desc);
|
|
}
|
|
TU_VERIFY(max_len >= drv_len, 0);
|
|
return drv_len;
|
|
}
|
|
|
|
/* Interface 3: Security (0xFD/0x13) — skip 0x41 descriptor, no EPs */
|
|
if (itf_desc->bInterfaceSubClass == 0xFD && itf_desc->bInterfaceProtocol == 0x13) {
|
|
if (p_desc[1] == DESC_TYPE_SEC) {
|
|
drv_len += tu_desc_len(p_desc);
|
|
}
|
|
TU_VERIFY(max_len >= drv_len, 0);
|
|
return drv_len;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static bool xinput_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
|
{
|
|
(void)result;
|
|
(void)xferred_bytes;
|
|
if (ep_addr == s_endpoint_out) {
|
|
console_mode_t current_mode = profile_manager_get_mode();
|
|
if (current_mode == CONSOLE_MODE_XBOX360) {
|
|
/* Xbox 360 Rumble Report: s_ep_out_buffer[0] == 0x00, s_ep_out_buffer[1] == 0x08 */
|
|
if (s_ep_out_buffer[0] == 0x00 && s_ep_out_buffer[1] == 0x08) {
|
|
uint8_t heavy = s_ep_out_buffer[3];
|
|
uint8_t light = s_ep_out_buffer[4];
|
|
ble_hid_host_send_rumble(heavy, light);
|
|
}
|
|
} else if (current_mode == CONSOLE_MODE_XBOXOG) {
|
|
/* Original Xbox Rumble Report: s_ep_out_buffer[0] == 0x00, s_ep_out_buffer[1] == 0x06 */
|
|
if (s_ep_out_buffer[0] == 0x00 && s_ep_out_buffer[1] == 0x06) {
|
|
uint8_t heavy = s_ep_out_buffer[3];
|
|
uint8_t light = s_ep_out_buffer[5];
|
|
ble_hid_host_send_rumble(heavy, light);
|
|
}
|
|
}
|
|
/* Re-prime OUT endpoint to receive rumble / LED packets */
|
|
usbd_edpt_xfer(rhport, s_endpoint_out, s_ep_out_buffer, XINPUT_ENDPOINT_SIZE, false);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool xinput_control_handler(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
|
|
{
|
|
console_mode_t current_mode = profile_manager_get_mode();
|
|
|
|
/* Original Xbox XID control request handling */
|
|
if (current_mode == CONSOLE_MODE_XBOXOG) {
|
|
/* XID Descriptor Request: bRequest == 0x06 (GET_DESCRIPTOR) and wValue == 0x4200 */
|
|
if (request->bRequest == 0x06 && request->wValue == 0x4200) {
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
uint16_t len = (request->wLength < sizeof(ogx_xid_descriptor)) ? request->wLength : sizeof(ogx_xid_descriptor);
|
|
return tud_control_xfer(rhport, request, (void *)ogx_xid_descriptor, len);
|
|
}
|
|
return true;
|
|
}
|
|
/* GET_REPORT on control endpoint (bRequest == 0x01) */
|
|
if (request->bRequest == 0x01 && (request->bmRequestType & 0x80)) {
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
controller_state_t st;
|
|
ogx_report_t rep;
|
|
usb_gamepad_get_last_state(&st);
|
|
ogx_build_report(&st, &rep);
|
|
uint16_t len = (request->wLength < sizeof(rep)) ? request->wLength : sizeof(rep);
|
|
return tud_control_xfer(rhport, request, &rep, len);
|
|
}
|
|
return true;
|
|
}
|
|
/* SET_REPORT on control endpoint (bRequest == 0x09) */
|
|
if (request->bRequest == 0x09 && !(request->bmRequestType & 0x80)) {
|
|
return tud_control_status(rhport, request);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
if (current_mode != CONSOLE_MODE_XBOX360) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t type_bits = request->bmRequestType & 0x60;
|
|
if (type_bits != 0x40 && type_bits != 0x20) {
|
|
return false;
|
|
}
|
|
|
|
switch (request->bRequest) {
|
|
case 0x81: /* GET Identification */
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
ESP_LOGI(TAG, "XSM3: 0x81 GET_SERIAL (wLength=%d)", request->wLength);
|
|
if (request->wLength >= 0x1D) {
|
|
return tud_control_xfer(rhport, request, s_xsm3_identification, 0x1D);
|
|
}
|
|
}
|
|
return true;
|
|
|
|
case 0x82: /* OUT Challenge Init */
|
|
if (stage == CONTROL_STAGE_SETUP && request->wLength >= 0x22) {
|
|
return tud_control_xfer(rhport, request, s_xsm3_buf_82, 0x22);
|
|
}
|
|
if (stage == CONTROL_STAGE_DATA) {
|
|
ESP_LOGI(TAG, "XSM3: 0x82 challenge init received, executing crypt...");
|
|
xsm3_do_challenge_init(s_xsm3_buf_82);
|
|
s_xsm3_auth_state = XSM3_AUTH_STATE_RESPONDED;
|
|
}
|
|
return true;
|
|
|
|
case 0x83: /* GET Challenge Response */
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
if (s_xsm3_auth_state == XSM3_AUTH_STATE_RESPONDED) {
|
|
ESP_LOGI(TAG, "XSM3: 0x83 sending init response (46 bytes)");
|
|
uint16_t len = (request->wLength < 46) ? request->wLength : 46;
|
|
bool ok = tud_control_xfer(rhport, request, xsm3_challenge_response, len);
|
|
if (ok) s_xsm3_auth_state = XSM3_AUTH_STATE_IDLE;
|
|
return ok;
|
|
}
|
|
if (s_xsm3_auth_state == XSM3_AUTH_STATE_AUTHENTICATED) {
|
|
ESP_LOGI(TAG, "XSM3: 0x83 sending verify response (22 bytes)");
|
|
uint16_t len = (request->wLength < 22) ? request->wLength : 22;
|
|
bool ok = tud_control_xfer(rhport, request, xsm3_challenge_response, len);
|
|
if (ok) s_xsm3_auth_state = XSM3_AUTH_STATE_IDLE;
|
|
return ok;
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
|
|
case 0x84: /* GET Keepalive */
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
return tud_control_xfer(rhport, request, NULL, 0);
|
|
}
|
|
return true;
|
|
|
|
case 0x86: /* GET State */
|
|
if (stage == CONTROL_STAGE_SETUP && request->wLength >= 2) {
|
|
uint8_t state_val = (s_xsm3_auth_state == XSM3_AUTH_STATE_RESPONDED ||
|
|
s_xsm3_auth_state == XSM3_AUTH_STATE_AUTHENTICATED)
|
|
? 2 /* Ready */ : 1 /* Processing */;
|
|
s_xsm3_state_buf[0] = state_val;
|
|
s_xsm3_state_buf[1] = 0x00;
|
|
return tud_control_xfer(rhport, request, s_xsm3_state_buf, 2);
|
|
}
|
|
return true;
|
|
|
|
case 0x87: /* OUT Challenge Verify */
|
|
if (stage == CONTROL_STAGE_SETUP && request->wLength >= 0x16) {
|
|
return tud_control_xfer(rhport, request, s_xsm3_buf_87, 0x16);
|
|
}
|
|
if (stage == CONTROL_STAGE_DATA) {
|
|
ESP_LOGI(TAG, "XSM3: 0x87 verify received, authenticating...");
|
|
xsm3_do_challenge_verify(s_xsm3_buf_87);
|
|
s_xsm3_auth_state = XSM3_AUTH_STATE_AUTHENTICATED;
|
|
ESP_LOGI(TAG, "XSM3: Controller authenticated successfully!");
|
|
}
|
|
return true;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static bool xinput_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
|
|
{
|
|
return xinput_control_handler(rhport, stage, request);
|
|
}
|
|
|
|
/* Intercept vendor-specific control requests dispatched directly from usbd.c */
|
|
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
|
|
{
|
|
return xinput_control_handler(rhport, stage, request);
|
|
}
|
|
|
|
static const usbd_class_driver_t s_xinput_class_driver = {
|
|
#if CFG_TUSB_DEBUG >= 2
|
|
.name = "XINPUT",
|
|
#else
|
|
.name = NULL,
|
|
#endif
|
|
.init = xinput_device_init,
|
|
.deinit = xinput_deinit,
|
|
.reset = xinput_reset,
|
|
.open = xinput_open,
|
|
.control_xfer_cb = xinput_control_xfer_cb,
|
|
.xfer_cb = xinput_xfer_cb,
|
|
.xfer_isr = NULL,
|
|
.sof = NULL
|
|
};
|
|
|
|
/* Application class driver registration for TinyUSB */
|
|
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count)
|
|
{
|
|
*driver_count = 1;
|
|
return &s_xinput_class_driver;
|
|
}
|
|
|
|
void xinput_device_build_report(const controller_state_t *state, xinput_in_report_t *rep)
|
|
{
|
|
memset(rep, 0, sizeof(xinput_in_report_t));
|
|
rep->report_id = 0x00;
|
|
rep->report_size = sizeof(xinput_in_report_t);
|
|
|
|
/* Buttons 0: D-Pad, Start, Back, L3, R3 */
|
|
if (state->dpad_up) rep->buttons0 |= XINPUT_BTN_DPAD_UP;
|
|
if (state->dpad_down) rep->buttons0 |= XINPUT_BTN_DPAD_DOWN;
|
|
if (state->dpad_left) rep->buttons0 |= XINPUT_BTN_DPAD_LEFT;
|
|
if (state->dpad_right) rep->buttons0 |= XINPUT_BTN_DPAD_RIGHT;
|
|
if (state->btn_start) rep->buttons0 |= XINPUT_BTN_START;
|
|
if (state->btn_select) rep->buttons0 |= XINPUT_BTN_BACK;
|
|
if (state->btn_l3) rep->buttons0 |= XINPUT_BTN_L3;
|
|
if (state->btn_r3) rep->buttons0 |= XINPUT_BTN_R3;
|
|
|
|
/* Buttons 1: LB, RB, Guide, A, B, X, Y */
|
|
if (state->btn_lb) rep->buttons1 |= XINPUT_BTN_LB;
|
|
if (state->btn_rb) rep->buttons1 |= XINPUT_BTN_RB;
|
|
if (state->btn_system) rep->buttons1 |= XINPUT_BTN_GUIDE;
|
|
if (state->btn_a) rep->buttons1 |= XINPUT_BTN_A;
|
|
if (state->btn_b) rep->buttons1 |= XINPUT_BTN_B;
|
|
if (state->btn_x) rep->buttons1 |= XINPUT_BTN_X;
|
|
if (state->btn_y) rep->buttons1 |= XINPUT_BTN_Y;
|
|
|
|
/* Triggers: 0..255 */
|
|
rep->trigger_l = state->lt;
|
|
rep->trigger_r = state->rt;
|
|
|
|
/* Joysticks: int16_t (-32768..32767).
|
|
controller_state_t: Up is positive.
|
|
XInput specification: Up is positive.
|
|
*/
|
|
rep->joystick_lx = state->lx;
|
|
rep->joystick_ly = state->ly;
|
|
rep->joystick_rx = state->rx;
|
|
rep->joystick_ry = state->ry;
|
|
}
|
|
|
|
bool xinput_device_send_report(const xinput_in_report_t *rep)
|
|
{
|
|
if (!tud_mounted() || s_endpoint_in == 0xFF) return false;
|
|
if (usbd_edpt_busy(0, s_endpoint_in)) return false;
|
|
|
|
memcpy(s_ep_in_buffer, rep, sizeof(xinput_in_report_t));
|
|
usbd_edpt_claim(0, s_endpoint_in);
|
|
usbd_edpt_xfer(0, s_endpoint_in, s_ep_in_buffer, sizeof(xinput_in_report_t), false);
|
|
usbd_edpt_release(0, s_endpoint_in);
|
|
return true;
|
|
}
|
|
|
|
void ogx_device_init(void)
|
|
{
|
|
s_endpoint_in = 0xFF;
|
|
s_endpoint_out = 0xFF;
|
|
memset(s_ep_in_buffer, 0, sizeof(s_ep_in_buffer));
|
|
memset(s_ep_out_buffer, 0, sizeof(s_ep_out_buffer));
|
|
}
|
|
|
|
bool ogx_device_send_report(const void *rep, uint16_t len)
|
|
{
|
|
if (!tud_mounted() || s_endpoint_in == 0xFF) return false;
|
|
if (usbd_edpt_busy(0, s_endpoint_in)) return false;
|
|
|
|
memcpy(s_ep_in_buffer, rep, len);
|
|
usbd_edpt_claim(0, s_endpoint_in);
|
|
usbd_edpt_xfer(0, s_endpoint_in, s_ep_in_buffer, len, false);
|
|
usbd_edpt_release(0, s_endpoint_in);
|
|
return true;
|
|
}
|