-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBthDevice.cs
254 lines (194 loc) · 6.67 KB
/
BthDevice.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
using System;
using System.ComponentModel;
using System.Threading;
using System.Text;
namespace ScpControl
{
public partial class BthDevice : BthConnection, IDsDevice
{
public event EventHandler<DebugEventArgs> Debug = null;
public event EventHandler<ReportEventArgs> Report = null;
protected ReportEventArgs m_ReportArgs = new ReportEventArgs();
protected Byte m_Init = 0;
protected Boolean m_Publish = false;
protected Timer m_Timer;
protected Boolean m_Blocked = false, m_IsIdle = true, m_IsDisconnect = false;
protected UInt32 m_Queued = 0;
protected DateTime m_Last = DateTime.Now, m_Idle = DateTime.Now, m_Tick = DateTime.Now, m_Disconnect = DateTime.Now;
protected IBthDevice m_Device;
protected Byte[] m_Master = new Byte[6];
protected Byte m_ControllerId = 0;
protected Byte m_BatteryStatus = 0;
protected Byte m_CableStatus = 0;
protected Byte m_PlugStatus = 0;
protected DsState m_State = DsState.Disconnected;
protected UInt32 m_Packet = 0;
public DsState State
{
get { return m_State; }
}
public DsConnection Connection
{
get { return DsConnection.BTH; }
}
public DsBattery Battery
{
get { return (DsBattery)m_BatteryStatus; }
}
public String Local
{
get { return m_Mac; }
}
public 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 DsPadId PadId
{
get { return (DsPadId) m_ControllerId; }
set { m_ControllerId = (Byte) value; }
}
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 LogDebug(String Data)
{
DebugEventArgs args = new DebugEventArgs(Data);
if (Debug != null)
{
Debug(this, args);
}
}
public BthDevice()
{
InitializeComponent();
}
public BthDevice(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public BthDevice(IBthDevice Device, Byte[] Master, Byte Lsb, Byte Msb) : base(new BthHandle(Lsb, Msb))
{
InitializeComponent();
m_Device = Device;
m_Master = Master;
m_Timer = new Timer(On_Timer, null, Timeout.Infinite, Timeout.Infinite);
}
public virtual Boolean Start()
{
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_Timer.Change(10, 10);
return m_State == DsState.Connected;
}
public virtual Boolean Stop()
{
if (m_State == DsState.Connected)
{
m_Timer.Change(Timeout.Infinite, Timeout.Infinite);
m_State = DsState.Reserved;
m_Packet = 0;
m_Publish = false;
Publish();
}
return m_State == DsState.Reserved;
}
public virtual Boolean Close()
{
Stop();
if (m_State == DsState.Reserved)
{
m_State = DsState.Disconnected;
m_Packet = 0;
m_Publish = false;
Publish();
}
return m_State == DsState.Disconnected;
}
public virtual void Parse(Byte[] Report)
{
}
public virtual Boolean Rumble(Byte Large, Byte Small)
{
return false;
}
public virtual Boolean Pair(Byte[] Master)
{
return false;
}
public virtual Boolean Disconnect()
{
m_Publish = false;
return m_Device.HCI_Disconnect(m_HCI_Handle) > 0;
}
public virtual Boolean InitReport(Byte[] Report)
{
return true;
}
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();
}
public virtual void Completed()
{
lock (this)
{
m_Blocked = false;
}
}
protected virtual void Process(DateTime Now)
{
}
protected virtual void On_Timer(object State)
{
{
if (m_State == DsState.Connected)
{
DateTime Now = DateTime.Now;
if (m_IsIdle && Global.IdleDisconnect)
{
if ((Now - m_Idle).TotalMilliseconds >= Global.IdleTimeout)
{
LogDebug("++ Idle Disconnect Triggered");
m_IsDisconnect = false;
m_IsIdle = false;
Disconnect();
return;
}
}
else if (m_IsDisconnect)
{
if ((Now - m_Disconnect).TotalMilliseconds >= 2000)
{
LogDebug("++ Quick Disconnect Triggered");
m_IsDisconnect = false;
m_IsIdle = false;
Disconnect();
return;
}
}
Process(Now);
}
}
}
}
}