forked from nefarius/DsHidMini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BthPS3FilterDriver.cs
177 lines (156 loc) · 7.45 KB
/
BthPS3FilterDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Runtime.InteropServices;
using Nefarius.DsHidMini.Util;
using PInvoke;
namespace Nefarius.DsHidMini.Drivers
{
public static class BthPS3FilterDriver
{
private const uint IOCTL_BTHPS3PSM_ENABLE_PSM_PATCHING = 0x002AAC04;
private const uint IOCTL_BTHPS3PSM_DISABLE_PSM_PATCHING = 0x002AAC08;
private const uint IOCTL_BTHPS3PSM_GET_PSM_PATCHING = 0x002A6C0C;
private static readonly string BTHPS3PSM_CONTROL_DEVICE_PATH = "\\\\.\\BthPS3PSMControl";
private static string ErrorMessage =>
"BthPS3 filter driver access failed. Is Bluetooth turned on? Are the drivers installed?";
/// <summary>
/// True if filter driver is currently loaded and operational, false otherwise.
/// </summary>
public static bool IsFilterAvailable
{
get
{
if (!BluetoothHelper.IsBluetoothRadioAvailable)
return false;
using (var handle = Kernel32.CreateFile(BTHPS3PSM_CONTROL_DEVICE_PATH,
Kernel32.ACCESS_MASK.GenericRight.GENERIC_READ | Kernel32.ACCESS_MASK.GenericRight.GENERIC_WRITE,
Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
IntPtr.Zero, Kernel32.CreationDisposition.OPEN_EXISTING,
Kernel32.CreateFileFlags.FILE_ATTRIBUTE_NORMAL
| Kernel32.CreateFileFlags.FILE_FLAG_NO_BUFFERING
| Kernel32.CreateFileFlags.FILE_FLAG_WRITE_THROUGH,
Kernel32.SafeObjectHandle.Null
))
{
var error = (Win32ErrorCode)Marshal.GetLastWin32Error();
return (error == Win32ErrorCode.ERROR_SUCCESS || error == Win32ErrorCode.ERROR_ACCESS_DENIED);
}
}
}
/// <summary>
/// Gets or sets current filter patching state.
/// </summary>
public static bool IsFilterEnabled
{
get
{
if (!BluetoothHelper.IsBluetoothRadioAvailable)
return false;
using (var handle = Kernel32.CreateFile(BTHPS3PSM_CONTROL_DEVICE_PATH,
Kernel32.ACCESS_MASK.GenericRight.GENERIC_READ | Kernel32.ACCESS_MASK.GenericRight.GENERIC_WRITE,
Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
IntPtr.Zero, Kernel32.CreationDisposition.OPEN_EXISTING,
Kernel32.CreateFileFlags.FILE_ATTRIBUTE_NORMAL
| Kernel32.CreateFileFlags.FILE_FLAG_NO_BUFFERING
| Kernel32.CreateFileFlags.FILE_FLAG_WRITE_THROUGH,
Kernel32.SafeObjectHandle.Null
))
{
if (handle.IsInvalid)
throw new Exception(ErrorMessage);
var payloadBuffer = Marshal.AllocHGlobal(Marshal.SizeOf<BTHPS3PSM_GET_PSM_PATCHING>());
var payload = new BTHPS3PSM_GET_PSM_PATCHING {DeviceIndex = 0};
try
{
Marshal.StructureToPtr(payload, payloadBuffer, false);
Kernel32.DeviceIoControl(
handle,
unchecked((int) IOCTL_BTHPS3PSM_GET_PSM_PATCHING),
payloadBuffer,
Marshal.SizeOf<BTHPS3PSM_GET_PSM_PATCHING>(),
payloadBuffer,
Marshal.SizeOf<BTHPS3PSM_GET_PSM_PATCHING>(),
out _,
IntPtr.Zero
);
payload = Marshal.PtrToStructure<BTHPS3PSM_GET_PSM_PATCHING>(payloadBuffer);
}
finally
{
Marshal.FreeHGlobal(payloadBuffer);
}
return payload.IsEnabled > 0;
}
}
set
{
using (var handle = Kernel32.CreateFile(BTHPS3PSM_CONTROL_DEVICE_PATH,
Kernel32.ACCESS_MASK.GenericRight.GENERIC_READ | Kernel32.ACCESS_MASK.GenericRight.GENERIC_WRITE,
Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
IntPtr.Zero, Kernel32.CreationDisposition.OPEN_EXISTING,
Kernel32.CreateFileFlags.FILE_ATTRIBUTE_NORMAL
| Kernel32.CreateFileFlags.FILE_FLAG_NO_BUFFERING
| Kernel32.CreateFileFlags.FILE_FLAG_WRITE_THROUGH,
Kernel32.SafeObjectHandle.Null
))
{
if (handle.IsInvalid)
throw new Exception(ErrorMessage);
var payloadEnableBuffer = Marshal.AllocHGlobal(Marshal.SizeOf<BTHPS3PSM_ENABLE_PSM_PATCHING>());
var payloadEnable = new BTHPS3PSM_ENABLE_PSM_PATCHING {DeviceIndex = 0};
var payloadDisableBuffer = Marshal.AllocHGlobal(Marshal.SizeOf<BTHPS3PSM_DISABLE_PSM_PATCHING>());
var payloadDisable = new BTHPS3PSM_DISABLE_PSM_PATCHING {DeviceIndex = 0};
try
{
Marshal.StructureToPtr(payloadEnable, payloadEnableBuffer, false);
Marshal.StructureToPtr(payloadDisable, payloadDisableBuffer, false);
if (value)
Kernel32.DeviceIoControl(
handle,
unchecked((int) IOCTL_BTHPS3PSM_ENABLE_PSM_PATCHING),
payloadEnableBuffer,
Marshal.SizeOf<BTHPS3PSM_ENABLE_PSM_PATCHING>(),
IntPtr.Zero,
0,
out _,
IntPtr.Zero
);
else
Kernel32.DeviceIoControl(
handle,
unchecked((int) IOCTL_BTHPS3PSM_DISABLE_PSM_PATCHING),
payloadDisableBuffer,
Marshal.SizeOf<BTHPS3PSM_DISABLE_PSM_PATCHING>(),
IntPtr.Zero,
0,
out _,
IntPtr.Zero
);
}
finally
{
Marshal.FreeHGlobal(payloadEnableBuffer);
Marshal.FreeHGlobal(payloadDisableBuffer);
}
}
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct BTHPS3PSM_ENABLE_PSM_PATCHING
{
public uint DeviceIndex;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct BTHPS3PSM_DISABLE_PSM_PATCHING
{
public uint DeviceIndex;
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
private struct BTHPS3PSM_GET_PSM_PATCHING
{
public uint DeviceIndex;
public readonly uint IsEnabled;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xC8)]
public readonly string SymbolicLinkName;
}
}
}