Add disabled code for authenticating wiimotes on Windows.

This commit is contained in:
Jordan Woyak 2013-02-25 00:28:25 -06:00
parent 692e39d1bb
commit e435851e7a

View File

@ -35,6 +35,8 @@
#include <bthdef.h> #include <bthdef.h>
#include <BluetoothAPIs.h> #include <BluetoothAPIs.h>
//#define AUTHENTICATE_WIIMOTES
typedef struct _HIDD_ATTRIBUTES typedef struct _HIDD_ATTRIBUTES
{ {
ULONG Size; ULONG Size;
@ -56,6 +58,7 @@ typedef BOOL (__stdcall *PBth_BluetoothFindRadioClose)(HBLUETOOTH_RADIO_FIND);
typedef DWORD (__stdcall *PBth_BluetoothGetRadioInfo)(HANDLE, PBLUETOOTH_RADIO_INFO); typedef DWORD (__stdcall *PBth_BluetoothGetRadioInfo)(HANDLE, PBLUETOOTH_RADIO_INFO);
typedef DWORD (__stdcall *PBth_BluetoothRemoveDevice)(const BLUETOOTH_ADDRESS*); typedef DWORD (__stdcall *PBth_BluetoothRemoveDevice)(const BLUETOOTH_ADDRESS*);
typedef DWORD (__stdcall *PBth_BluetoothSetServiceState)(HANDLE, const BLUETOOTH_DEVICE_INFO*, const GUID*, DWORD); typedef DWORD (__stdcall *PBth_BluetoothSetServiceState)(HANDLE, const BLUETOOTH_DEVICE_INFO*, const GUID*, DWORD);
typedef DWORD (__stdcall *PBth_BluetoothAuthenticateDevice)(HWND, HANDLE, BLUETOOTH_DEVICE_INFO*, PWCHAR, ULONG);
PHidD_GetHidGuid HidD_GetHidGuid = NULL; PHidD_GetHidGuid HidD_GetHidGuid = NULL;
PHidD_GetAttributes HidD_GetAttributes = NULL; PHidD_GetAttributes HidD_GetAttributes = NULL;
@ -70,6 +73,7 @@ PBth_BluetoothFindRadioClose Bth_BluetoothFindRadioClose = NULL;
PBth_BluetoothGetRadioInfo Bth_BluetoothGetRadioInfo = NULL; PBth_BluetoothGetRadioInfo Bth_BluetoothGetRadioInfo = NULL;
PBth_BluetoothRemoveDevice Bth_BluetoothRemoveDevice = NULL; PBth_BluetoothRemoveDevice Bth_BluetoothRemoveDevice = NULL;
PBth_BluetoothSetServiceState Bth_BluetoothSetServiceState = NULL; PBth_BluetoothSetServiceState Bth_BluetoothSetServiceState = NULL;
PBth_BluetoothAuthenticateDevice Bth_BluetoothAuthenticateDevice = NULL;
HINSTANCE hid_lib = NULL; HINSTANCE hid_lib = NULL;
HINSTANCE bthprops_lib = NULL; HINSTANCE bthprops_lib = NULL;
@ -114,12 +118,13 @@ inline void init_lib()
Bth_BluetoothGetRadioInfo = (PBth_BluetoothGetRadioInfo)GetProcAddress(bthprops_lib, "BluetoothGetRadioInfo"); Bth_BluetoothGetRadioInfo = (PBth_BluetoothGetRadioInfo)GetProcAddress(bthprops_lib, "BluetoothGetRadioInfo");
Bth_BluetoothRemoveDevice = (PBth_BluetoothRemoveDevice)GetProcAddress(bthprops_lib, "BluetoothRemoveDevice"); Bth_BluetoothRemoveDevice = (PBth_BluetoothRemoveDevice)GetProcAddress(bthprops_lib, "BluetoothRemoveDevice");
Bth_BluetoothSetServiceState = (PBth_BluetoothSetServiceState)GetProcAddress(bthprops_lib, "BluetoothSetServiceState"); Bth_BluetoothSetServiceState = (PBth_BluetoothSetServiceState)GetProcAddress(bthprops_lib, "BluetoothSetServiceState");
Bth_BluetoothAuthenticateDevice = (PBth_BluetoothAuthenticateDevice)GetProcAddress(bthprops_lib, "BluetoothAuthenticateDevice");
if (!Bth_BluetoothFindDeviceClose || !Bth_BluetoothFindFirstDevice || if (!Bth_BluetoothFindDeviceClose || !Bth_BluetoothFindFirstDevice ||
!Bth_BluetoothFindFirstRadio || !Bth_BluetoothFindNextDevice || !Bth_BluetoothFindFirstRadio || !Bth_BluetoothFindNextDevice ||
!Bth_BluetoothFindNextRadio || !Bth_BluetoothFindRadioClose || !Bth_BluetoothFindNextRadio || !Bth_BluetoothFindRadioClose ||
!Bth_BluetoothGetRadioInfo || !Bth_BluetoothRemoveDevice || !Bth_BluetoothGetRadioInfo || !Bth_BluetoothRemoveDevice ||
!Bth_BluetoothSetServiceState) !Bth_BluetoothSetServiceState || !Bth_BluetoothAuthenticateDevice)
{ {
PanicAlertT("Failed to load bthprops.cpl"); PanicAlertT("Failed to load bthprops.cpl");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -135,9 +140,9 @@ namespace WiimoteReal
template <typename T> template <typename T>
void ProcessWiimotes(bool new_scan, T& callback); void ProcessWiimotes(bool new_scan, T& callback);
bool AttachWiimote(HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi); bool AttachWiimote(HANDLE hRadio, const BLUETOOTH_RADIO_INFO&, BLUETOOTH_DEVICE_INFO_STRUCT&);
void RemoveWiimote(HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi); void RemoveWiimote(BLUETOOTH_DEVICE_INFO_STRUCT&);
bool ForgetWiimote(HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi); bool ForgetWiimote(BLUETOOTH_DEVICE_INFO_STRUCT&);
WiimoteScanner::WiimoteScanner() WiimoteScanner::WiimoteScanner()
: m_run_thread() : m_run_thread()
@ -149,16 +154,19 @@ WiimoteScanner::WiimoteScanner()
WiimoteScanner::~WiimoteScanner() WiimoteScanner::~WiimoteScanner()
{ {
// TODO: what do we want here? // TODO: what do we want here?
ProcessWiimotes(false, RemoveWiimote); ProcessWiimotes(false, [](HANDLE, BLUETOOTH_RADIO_INFO&, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
{
RemoveWiimote(btdi);
});
} }
void WiimoteScanner::Update() void WiimoteScanner::Update()
{ {
bool forgot_some = false; bool forgot_some = false;
ProcessWiimotes(false, [&](HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi) ProcessWiimotes(false, [&](HANDLE, BLUETOOTH_RADIO_INFO&, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
{ {
forgot_some |= ForgetWiimote(hRadio, btdi); forgot_some |= ForgetWiimote(btdi);
}); });
// Some hacks that allows disconnects to be detected before connections are handled // Some hacks that allows disconnects to be detected before connections are handled
@ -173,10 +181,10 @@ void WiimoteScanner::Update()
// Returns the total number of found and connected wiimotes. // Returns the total number of found and connected wiimotes.
std::vector<Wiimote*> WiimoteScanner::FindWiimotes() std::vector<Wiimote*> WiimoteScanner::FindWiimotes()
{ {
ProcessWiimotes(true, [](HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi) ProcessWiimotes(true, [](HANDLE hRadio, const BLUETOOTH_RADIO_INFO& rinfo, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
{ {
ForgetWiimote(hRadio, btdi); ForgetWiimote(btdi);
AttachWiimote(hRadio, btdi); AttachWiimote(hRadio, rinfo, btdi);
}); });
// Get the device id // Get the device id
@ -485,31 +493,32 @@ void ProcessWiimotes(bool new_scan, T& callback)
BLUETOOTH_RADIO_INFO radioInfo; BLUETOOTH_RADIO_INFO radioInfo;
radioInfo.dwSize = sizeof(radioInfo); radioInfo.dwSize = sizeof(radioInfo);
// TODO: check for SUCCEEDED() auto const rinfo_result = Bth_BluetoothGetRadioInfo(hRadio, &radioInfo);
Bth_BluetoothGetRadioInfo(hRadio, &radioInfo); if (ERROR_SUCCESS == rinfo_result)
srch.hRadio = hRadio;
BLUETOOTH_DEVICE_INFO btdi;
btdi.dwSize = sizeof(btdi);
// Enumerate BT devices
HBLUETOOTH_DEVICE_FIND hFindDevice = Bth_BluetoothFindFirstDevice(&srch, &btdi);
while (hFindDevice)
{ {
// btdi.szName is sometimes missings it's content - it's a bt feature.. srch.hRadio = hRadio;
DEBUG_LOG(WIIMOTE, "authed %i connected %i remembered %i ",
btdi.fAuthenticated, btdi.fConnected, btdi.fRemembered);
if (std::regex_match(btdi.szName, wiimote_device_name)) BLUETOOTH_DEVICE_INFO btdi;
{ btdi.dwSize = sizeof(btdi);
callback(hRadio, btdi);
}
if (false == Bth_BluetoothFindNextDevice(hFindDevice, &btdi)) // Enumerate BT devices
HBLUETOOTH_DEVICE_FIND hFindDevice = Bth_BluetoothFindFirstDevice(&srch, &btdi);
while (hFindDevice)
{ {
Bth_BluetoothFindDeviceClose(hFindDevice); // btdi.szName is sometimes missings it's content - it's a bt feature..
hFindDevice = NULL; DEBUG_LOG(WIIMOTE, "authed %i connected %i remembered %i ",
btdi.fAuthenticated, btdi.fConnected, btdi.fRemembered);
if (std::regex_match(btdi.szName, wiimote_device_name))
{
callback(hRadio, radioInfo, btdi);
}
if (false == Bth_BluetoothFindNextDevice(hFindDevice, &btdi))
{
Bth_BluetoothFindDeviceClose(hFindDevice);
hFindDevice = NULL;
}
} }
} }
@ -521,7 +530,7 @@ void ProcessWiimotes(bool new_scan, T& callback)
} }
} }
void RemoveWiimote(HANDLE, BLUETOOTH_DEVICE_INFO_STRUCT& btdi) void RemoveWiimote(BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
{ {
//if (btdi.fConnected) //if (btdi.fConnected)
{ {
@ -532,14 +541,26 @@ void RemoveWiimote(HANDLE, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
} }
} }
bool AttachWiimote(HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi) bool AttachWiimote(HANDLE hRadio, const BLUETOOTH_RADIO_INFO& radio_info, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
{ {
// We don't want "remembered" devices. // We don't want "remembered" devices.
// SetServiceState will just fail with them.. // SetServiceState will just fail with them..
if (!btdi.fConnected && !btdi.fRemembered) if (!btdi.fConnected && !btdi.fRemembered)
{ {
NOTICE_LOG(WIIMOTE, "Found wiimote. Enabling HID service."); auto const& wm_addr = btdi.Address.rgBytes;
NOTICE_LOG(WIIMOTE, "Found wiimote (%02x:%02x:%02x:%02x:%02x:%02x). Enabling HID service.",
wm_addr[0], wm_addr[1], wm_addr[2], wm_addr[3], wm_addr[4], wm_addr[5]);
#if defined(AUTHENTICATE_WIIMOTES)
// Authenticate
auto const& radio_addr = radio_info.address.rgBytes;
const DWORD auth_result = Bth_BluetoothAuthenticateDevice(NULL, hRadio, &btdi,
std::vector<WCHAR>(radio_addr, radio_addr + 6).data(), 6);
if (ERROR_SUCCESS != auth_result)
ERROR_LOG(WIIMOTE, "AttachWiimote: BluetoothAuthenticateDevice returned %08x", auth_result);
#endif
// Activate service // Activate service
const DWORD hr = Bth_BluetoothSetServiceState(hRadio, &btdi, const DWORD hr = Bth_BluetoothSetServiceState(hRadio, &btdi,
&HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE); &HumanInterfaceDeviceServiceClass_UUID, BLUETOOTH_SERVICE_ENABLE);
@ -547,7 +568,7 @@ bool AttachWiimote(HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
g_connect_times[btdi.Address.ullLong] = std::time(nullptr); g_connect_times[btdi.Address.ullLong] = std::time(nullptr);
if (FAILED(hr)) if (FAILED(hr))
ERROR_LOG(WIIMOTE, "Pair-Up: BluetoothSetServiceState() returned %08x", hr); ERROR_LOG(WIIMOTE, "AttachWiimote: BluetoothSetServiceState returned %08x", hr);
else else
return true; return true;
} }
@ -556,7 +577,7 @@ bool AttachWiimote(HANDLE hRadio, BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
} }
// Removes remembered non-connected devices // Removes remembered non-connected devices
bool ForgetWiimote(HANDLE, BLUETOOTH_DEVICE_INFO_STRUCT& btdi) bool ForgetWiimote(BLUETOOTH_DEVICE_INFO_STRUCT& btdi)
{ {
if (!btdi.fConnected && btdi.fRemembered) if (!btdi.fConnected && btdi.fRemembered)
{ {