-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUsbDevice.cs
265 lines (201 loc) · 6.66 KB
/
UsbDevice.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Text;
using System.ComponentModel;
using System.Threading;
namespace ScpControl
{
public partial class UsbDevice : ScpDevice, IDsDevice
{
protected ReportEventArgs m_ReportArgs = new ReportEventArgs();
protected String m_Instance = String.Empty, m_Mac = String.Empty;
protected Timer m_Timer;
protected Boolean m_Publish = false;
protected Boolean m_IsDisconnect = false;
protected DateTime m_Last = DateTime.Now, m_Tick = DateTime.Now, m_Disconnect = DateTime.Now;
public event EventHandler<DebugEventArgs> Debug = null;
public event EventHandler<ReportEventArgs> Report = null;
protected Byte[] m_Buffer = new Byte[64];
protected Byte[] m_Master = new Byte[6];
protected Byte[] m_Local = new Byte[6];
protected Byte m_ControllerId = 0;
protected Byte m_BatteryStatus = 0;
protected Byte m_CableStatus = 0;
protected Byte m_PlugStatus = 0;
protected Byte m_Model = 0;
protected DsState m_State = DsState.Disconnected;
protected UInt32 m_Packet = 0;
public virtual DsModel Model
{
get { return (DsModel) m_Model; }
}
public virtual DsPadId PadId
{
get { return (DsPadId) m_ControllerId; }
set
{
m_ControllerId = (Byte) value;
m_ReportArgs.Pad = PadId;
}
}
public virtual DsConnection Connection
{
get { return DsConnection.USB; }
}
public virtual DsState State
{
get { return (DsState) m_State; }
}
public virtual DsBattery Battery
{
get { return (DsBattery) m_BatteryStatus; }
}
public virtual Byte[] BD_Address
{
get { return m_Local; }
}
public virtual String Local
{
get { return m_Mac; }
}
public virtual String Remote
{
get { return String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", m_Master[0], m_Master[1], m_Master[2], m_Master[3], m_Master[4], m_Master[5]); }
}
public virtual Boolean IsShutdown
{
get { return m_IsDisconnect; }
set { m_IsDisconnect = value; }
}
protected virtual void LogDebug(String Data)
{
DebugEventArgs args = new DebugEventArgs(Data);
if (Debug != null)
{
Debug(this, args);
}
}
protected virtual void Publish()
{
m_ReportArgs.Report[0] = m_ControllerId;
m_ReportArgs.Report[1] = (Byte) m_State;
if (Report != null) Report(this, m_ReportArgs);
}
protected virtual void Process(DateTime Now)
{
}
protected virtual void Parse(Byte[] Report)
{
}
protected virtual Boolean Shutdown()
{
Stop();
return RestartDevice(m_Instance);
}
protected UsbDevice(String Guid) : base(Guid)
{
InitializeComponent();
m_Timer = new Timer(On_Timer, null, Timeout.Infinite, Timeout.Infinite);
}
public UsbDevice()
{
InitializeComponent();
}
public UsbDevice(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public override Boolean Start()
{
if (IsActive)
{
Array.Copy(m_Local, 0, m_ReportArgs.Report, (Int32) DsOffset.Address, m_Local.Length);
m_ReportArgs.Report[(Int32) DsOffset.Connection] = (Byte) Connection;
m_ReportArgs.Report[(Int32) DsOffset.Model ] = (Byte) Model;
m_State = DsState.Connected;
m_Packet = 0;
HID_Worker.RunWorkerAsync();
m_Timer.Change(16, 16);
Rumble(0, 0);
LogDebug(String.Format("-- Started Device Instance [{0}] Local [{1}] Remote [{2}]", m_Instance, Local, Remote));
}
return State == DsState.Connected;
}
public override Boolean Stop()
{
if (IsActive)
{
m_Timer.Change(Timeout.Infinite, Timeout.Infinite);
m_State = DsState.Reserved;
Publish();
}
return base.Stop();
}
public override Boolean Close()
{
if (IsActive)
{
base.Close();
m_Timer.Change(Timeout.Infinite, Timeout.Infinite);
m_State = DsState.Disconnected;
Publish();
}
return !IsActive;
}
public override String ToString()
{
switch ((DsState) m_State)
{
case DsState.Disconnected:
return String.Format("Pad {0} : Disconnected", m_ControllerId + 1);
case DsState.Reserved:
return String.Format("Pad {0} : {1} {2} - Reserved", m_ControllerId + 1, Model, Local);
case DsState.Connected:
return String.Format("Pad {0} : {1} {2} - {3} {4:X8} {5}", m_ControllerId + 1, Model,
Local,
Connection,
m_Packet,
Battery
);
}
throw new Exception();
}
protected void HID_Worker_Thread(object sender, DoWorkEventArgs e)
{
Int32 Transfered = 0;
Byte[] Buffer = new Byte[64];
LogDebug("-- USB Device : HID_Worker_Thread Starting");
while (IsActive)
{
try
{
if (ReadIntPipe(Buffer, Buffer.Length, ref Transfered) && Transfered > 0)
{
Parse(Buffer);
}
}
catch { }
}
LogDebug("-- USB Device : HID_Worker_Thread Exiting");
}
protected void On_Timer(object State)
{
lock (this)
{
Process(DateTime.Now);
}
}
public virtual Boolean Rumble(Byte Large, Byte Small)
{
return false;
}
public virtual Boolean Pair(Byte[] Master)
{
return false;
}
public virtual Boolean Disconnect()
{
return true;
}
}
}