Skip to content

Commit

Permalink
Removed hard-coded data for BT device details (0xF2) in favor of a da…
Browse files Browse the repository at this point in the history
…ta struct.

Randomized BT addresses on start for 0xF2 and 0xF5 reports.
  • Loading branch information
mikepparks committed Sep 8, 2024
1 parent 6c8cfeb commit c89d34a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
8 changes: 8 additions & 0 deletions headers/drivers/ps3/PS3Descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ typedef struct __attribute((packed, aligned(1)))
uint8_t reserved3[38];
} PS3Features; // 48 length

typedef struct __attribute((packed, aligned(1)))
{
uint8_t reserved[2];
uint8_t deviceAddress[7]; // leading zero followed by address
uint8_t hostAddress[7]; // leading zero followed by address
uint8_t reserved1;
} PS3BTInfo;

static const uint8_t ps3_string_language[] = { 0x09, 0x04 };
static const uint8_t ps3_string_manufacturer[] = "Open Stick Community";
static const uint8_t ps3_string_product[] = "GP2040-CE (PS3)";
Expand Down
1 change: 1 addition & 0 deletions headers/drivers/ps3/PS3Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PS3Driver : public GPDriver {
uint8_t last_report[CFG_TUD_ENDPOINT0_SIZE] = { };
PS3Report ps3Report;
PS3Features ps3Features;
PS3BTInfo ps3BTInfo;

// this is an identification byte from the H2D 0xEF feature report that needs to be the same
// in multiple D2H reports for the controller to function
Expand Down
32 changes: 22 additions & 10 deletions src/drivers/ps3/PS3Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "drivers/ps3/PS3Descriptors.h"
#include "drivers/shared/driverhelper.h"
#include "storagemanager.h"
#include "pico/rand.h"

void PS3Driver::initialize() {
ps3Report = {
Expand Down Expand Up @@ -35,6 +36,21 @@ void PS3Driver::initialize() {
.reserved4 = PS3_CENTER_SIXAXIS
};

// generate addresses
ps3BTInfo = {
.reserved = {0xFF,0xFF},
.deviceAddress = { 0x00, 0x20, 0x40, 0xCE, 0x00, 0x00, 0x00 },
.hostAddress = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};

for (uint8_t addr = 0; addr < 3; addr++) {
ps3BTInfo.deviceAddress[4+addr] = (uint8_t)(get_rand_32() % 0xff);
}

for (uint8_t addr = 0; addr < 6; addr++) {
ps3BTInfo.hostAddress[1+addr] = (uint8_t)(get_rand_32() % 0xff);
}

class_driver = {
#if CFG_TUSB_DEBUG >= 2
.name = "PS3",
Expand Down Expand Up @@ -150,17 +166,9 @@ static constexpr uint8_t output_ps3_0xef[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
};

// bluetooth data
static constexpr uint8_t output_ps3_0xf2[] = {
0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // device address
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // host address
0x00,
};

// unknown
static constexpr uint8_t output_ps3_0xf5[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // host address - must match 0xf2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -201,6 +209,7 @@ uint16_t PS3Driver::get_report(uint8_t report_id, hid_report_type_t report_type,
return sizeof(PS3Report);
} else if ( report_type == HID_REPORT_TYPE_FEATURE ) {
uint16_t responseLen = 0;
uint8_t ctr = 0;
switch(report_id) {
case PS3ReportTypes::PS3_FEATURE_01:
responseLen = reqlen;
Expand All @@ -213,11 +222,14 @@ uint16_t PS3Driver::get_report(uint8_t report_id, hid_report_type_t report_type,
return responseLen;
case PS3ReportTypes::PS3_GET_PAIRING_INFO:
responseLen = reqlen;
memcpy(buffer, output_ps3_0xf2, responseLen);
memcpy(buffer, &ps3BTInfo, responseLen);
return responseLen;
case PS3ReportTypes::PS3_FEATURE_F5:
responseLen = reqlen;
memcpy(buffer, output_ps3_0xf5, responseLen);
for (ctr = 0; ctr < 6; ctr++) {
buffer[1+ctr] = ps3BTInfo.hostAddress[ctr];
}
return responseLen;
case PS3ReportTypes::PS3_FEATURE_F7:
responseLen = reqlen;
Expand Down

0 comments on commit c89d34a

Please sign in to comment.