Skip to content

Commit

Permalink
Added SDP support to the BTHID, as needed for the Xbox One S controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauszus committed Nov 15, 2020
1 parent a347b3b commit 7714e80
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 34 deletions.
53 changes: 32 additions & 21 deletions BTD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bAddress(0), // Device address - mandatory
bNumEP(1), // If config descriptor needs to be parsed
qNextPollTime(0), // Reset NextPollTime
pollInterval(0),
simple_pairing_supported(false),
bPollEnable(false) // Don't start polling before dongle is connected
{
for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++)
Expand Down Expand Up @@ -321,6 +322,7 @@ void BTD::Initialize() {
qNextPollTime = 0; // Reset next poll time
pollInterval = 0;
bPollEnable = false; // Don't start polling before dongle is connected
simple_pairing_supported = false;
}

/* Extracts interrupt-IN, bulk-IN, bulk-OUT endpoint information from config descriptor */
Expand Down Expand Up @@ -431,10 +433,13 @@ void BTD::HCI_event_task() {
#ifdef DEBUG_USB_HOST
if(hcibuf[6] == 0) { // Page 0
Notify(PSTR("\r\nDongle "), 0x80);
if(hcibuf[8 + 6] & (1U << 3))
if(hcibuf[8 + 6] & (1U << 3)) {
simple_pairing_supported = true;
Notify(PSTR("supports"), 0x80);
else
} else {
simple_pairing_supported = false;
Notify(PSTR("does NOT support"), 0x80);
}
Notify(PSTR(" secure simple pairing (controller support)"), 0x80);
} else if(hcibuf[6] == 1) { // Page 1
Notify(PSTR("\r\nDongle "), 0x80);
Expand Down Expand Up @@ -485,7 +490,7 @@ void BTD::HCI_event_task() {

case EV_INQUIRY_RESULT:
if(hcibuf[2]) { // Check that there is more than zero responses
#if defined(EXTRADEBUG) && 0
#ifdef EXTRADEBUG
Notify(PSTR("\r\nNumber of responses: "), 0x80);
Notify(hcibuf[2], 0x80);
#endif
Expand All @@ -495,7 +500,7 @@ void BTD::HCI_event_task() {
for(uint8_t j = 0; j < 3; j++)
classOfDevice[j] = hcibuf[j + 4 + offset];

#if defined(EXTRADEBUG) && 0
#ifdef EXTRADEBUG
Notify(PSTR("\r\nClass of device: "), 0x80);
D_PrintHex<uint8_t > (classOfDevice[2], 0x80);
Notify(PSTR(" "), 0x80);
Expand Down Expand Up @@ -710,8 +715,8 @@ void BTD::HCI_event_task() {

/* We will just ignore the following events */
case EV_MAX_SLOTS_CHANGE:
break;
case EV_NUM_COMPLETE_PKT:
break;
case EV_ROLE_CHANGED:
case EV_PAGE_SCAN_REP_MODE:
case EV_LOOPBACK_COMMAND:
Expand Down Expand Up @@ -806,6 +811,23 @@ void BTD::HCI_task() {

case HCI_LOCAL_VERSION_STATE: // The local version is used by the PS3BT class
if(hci_check_flag(HCI_FLAG_READ_VERSION)) {
if(btdName != NULL) {
hci_write_local_name(btdName);
hci_state = HCI_WRITE_NAME_STATE;
} else {
hci_read_local_extended_features(0); // "Requests the normal LMP features as returned by Read_Local_Supported_Features"
//hci_read_local_extended_features(1); // Read page 1
hci_state = HCI_LOCAL_EXTENDED_FEATURES_STATE;
}
}
break;

case HCI_WRITE_NAME_STATE:
if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nThe name was set to: "), 0x80);
NotifyStr(btdName, 0x80);
#endif
hci_read_local_extended_features(0); // "Requests the normal LMP features as returned by Read_Local_Supported_Features"
//hci_read_local_extended_features(1); // Read page 1
hci_state = HCI_LOCAL_EXTENDED_FEATURES_STATE;
Expand All @@ -814,29 +836,18 @@ void BTD::HCI_task() {

case HCI_LOCAL_EXTENDED_FEATURES_STATE:
if(hci_check_flag(HCI_FLAG_LOCAL_EXTENDED_FEATURES)) {
hci_write_simple_pairing_mode(true);
hci_state = HCI_WRITE_SIMPLE_PAIRING_STATE;
}
break;

case HCI_WRITE_SIMPLE_PAIRING_STATE:
if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nSimple pairing was enabled"), 0x80);
#endif
if(btdName != NULL) {
hci_write_local_name(btdName);
hci_state = HCI_WRITE_NAME_STATE;
if(simple_pairing_supported) {
hci_write_simple_pairing_mode(true);
hci_state = HCI_WRITE_SIMPLE_PAIRING_STATE;
} else
hci_state = HCI_CHECK_DEVICE_SERVICE;
}
break;

case HCI_WRITE_NAME_STATE:
case HCI_WRITE_SIMPLE_PAIRING_STATE:
if(hci_check_flag(HCI_FLAG_CMD_COMPLETE)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nThe name was set to: "), 0x80);
NotifyStr(btdName, 0x80);
Notify(PSTR("\r\nSimple pairing was enabled"), 0x80);
#endif
hci_state = HCI_CHECK_DEVICE_SERVICE;
}
Expand Down
12 changes: 12 additions & 0 deletions BTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@
#define HID_CTRL_PSM 0x11 // HID_Control PSM Value
#define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value

/* Used for SDP */
#define SDP_SERVICE_SEARCH_REQUEST 0x02
#define SDP_SERVICE_SEARCH_RESPONSE 0x03
#define SDP_SERVICE_ATTRIBUTE_REQUEST 0x04
#define SDP_SERVICE_ATTRIBUTE_RESPONSE 0x05
#define SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST_PDU 0x06 // See the RFCOMM specs
#define SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE_PDU 0x07 // See the RFCOMM specs
#define PNP_INFORMATION_UUID 0x1200
#define SERIALPORT_UUID 0x1101 // See http://www.bluetooth.org/Technical/AssignedNumbers/service_discovery.htm
#define L2CAP_UUID 0x0100

// Used to determine if it is a Bluetooth dongle
#define WI_SUBCLASS_RF 0x01 // RF Controller
#define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
Expand Down Expand Up @@ -553,6 +564,7 @@ class BTD : public USBDeviceConfig, public UsbConfigXtracter {
uint16_t PID, VID; // PID and VID of device connected

uint8_t pollInterval;
bool simple_pairing_supported;
bool bPollEnable;

bool pairWiiUsingSync; // True if pairing was done using the Wii SYNC button.
Expand Down
Loading

0 comments on commit 7714e80

Please sign in to comment.