-
Notifications
You must be signed in to change notification settings - Fork 4
/
BthConnection.cs
275 lines (208 loc) · 7.27 KB
/
BthConnection.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
266
267
268
269
270
271
272
273
274
275
using System;
using System.ComponentModel;
namespace ScpControl
{
public partial class BthConnection : Component, IEquatable<BthConnection>, IComparable<BthConnection>
{
protected static UInt16 m_DCID = 0x40;
protected BthHandle m_HCI_Handle;
protected BthHandle[] m_L2CAP_Cmd_Handle = new BthHandle[2];
protected BthHandle[] m_L2CAP_Int_Handle = new BthHandle[2];
protected BthHandle[] m_L2CAP_Svc_Handle = new BthHandle[2];
protected Byte[] m_Local = new Byte[6] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
protected String m_Remote_Name = String.Empty, m_Mac = String.Empty;
public virtual BthHandle HCI_Handle
{
get { return m_HCI_Handle; }
}
public virtual Byte[] BD_Address
{
get { return m_Local; }
set { m_Local = value; m_Mac = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}", m_Local[0], m_Local[1], m_Local[2], m_Local[3], m_Local[4], m_Local[5]); }
}
public virtual String Remote_Name
{
get { return m_Remote_Name; }
set
{
m_Remote_Name = value;
if (m_Remote_Name == "Wireless Controller") m_Model = DsModel.DS4;
}
}
protected Boolean m_CanStartHid = false;
public virtual Boolean CanStartHid
{
get { return m_CanStartHid; }
set { m_CanStartHid = value; }
}
protected Boolean m_CanStartSvc = false;
public virtual Boolean CanStartSvc
{
get { return m_CanStartSvc; }
set { m_CanStartSvc = value; }
}
protected Boolean m_SvcStarted = false;
public virtual Boolean SvcStarted
{
get { return m_SvcStarted; }
set { m_SvcStarted = value; }
}
protected Boolean m_Started = false;
public virtual Boolean Started
{
get { return m_Started; }
set { m_Started = value; }
}
protected DsModel m_Model = DsModel.DS3;
public virtual DsModel Model
{
get { return m_Model; }
}
public static UInt16 DCID
{
get { return m_DCID; }
set { if (value < 0xFFFF) m_DCID = value; else m_DCID = 0x40; }
}
public BthConnection()
{
InitializeComponent();
}
public BthConnection(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public BthConnection(BthHandle HCI_Handle)
{
InitializeComponent();
m_HCI_Handle = HCI_Handle;
}
public virtual Byte[] Set(L2CAP.PSM ConnectionType, Byte Lsb, Byte Msb, UInt16 Dcid = 0)
{
switch (ConnectionType)
{
case L2CAP.PSM.HID_Command:
m_L2CAP_Cmd_Handle[0] = new BthHandle(Lsb, Msb);
m_L2CAP_Cmd_Handle[1] = new BthHandle(DCID++);
return m_L2CAP_Cmd_Handle[1].Bytes;
case L2CAP.PSM.HID_Interrupt:
CanStartSvc = true;
m_L2CAP_Int_Handle[0] = new BthHandle(Lsb, Msb);
m_L2CAP_Int_Handle[1] = new BthHandle(DCID++);
return m_L2CAP_Int_Handle[1].Bytes;
case L2CAP.PSM.HID_Service:
SvcStarted = true; CanStartSvc = false;
m_L2CAP_Svc_Handle[0] = new BthHandle(Lsb, Msb);
m_L2CAP_Svc_Handle[1] = new BthHandle(Dcid);
return m_L2CAP_Svc_Handle[1].Bytes;
}
throw new Exception("Invalid L2CAP Connection Type");
}
public virtual Byte[] Set(L2CAP.PSM ConnectionType, Byte[] Handle)
{
return Set(ConnectionType, Handle[0], Handle[1]);
}
public virtual Byte[] Get_DCID(Byte Lsb, Byte Msb)
{
if (m_L2CAP_Cmd_Handle[0].Equals(Lsb, Msb))
{
return m_L2CAP_Cmd_Handle[1].Bytes;
}
if (m_L2CAP_Int_Handle[0].Equals(Lsb, Msb))
{
return m_L2CAP_Int_Handle[1].Bytes;
}
if (m_L2CAP_Svc_Handle[0].Equals(Lsb, Msb))
{
return m_L2CAP_Svc_Handle[1].Bytes;
}
throw new Exception("L2CAP DCID Not Found");
}
public virtual Byte[] Get_DCID(L2CAP.PSM ConnectionType)
{
switch (ConnectionType)
{
case L2CAP.PSM.HID_Command:
return m_L2CAP_Cmd_Handle[1].Bytes;
case L2CAP.PSM.HID_Interrupt:
return m_L2CAP_Int_Handle[1].Bytes;
case L2CAP.PSM.HID_Service:
return m_L2CAP_Svc_Handle[1].Bytes;
}
throw new Exception("Invalid L2CAP Connection Type");
}
public virtual Byte[] Get_SCID(Byte Lsb, Byte Msb)
{
try
{
if (m_L2CAP_Cmd_Handle[1].Equals(Lsb, Msb))
{
return m_L2CAP_Cmd_Handle[0].Bytes;
}
}
catch { }
try
{
if (m_L2CAP_Int_Handle[1].Equals(Lsb, Msb))
{
return m_L2CAP_Int_Handle[0].Bytes;
}
}
catch { }
try
{
if (m_L2CAP_Svc_Handle[1].Equals(Lsb, Msb))
{
return m_L2CAP_Svc_Handle[0].Bytes;
}
}
catch { }
throw new Exception("L2CAP SCID Not Found");
}
public virtual Byte[] Get_SCID(L2CAP.PSM ConnectionType)
{
switch (ConnectionType)
{
case L2CAP.PSM.HID_Command:
return m_L2CAP_Cmd_Handle[0].Bytes;
case L2CAP.PSM.HID_Interrupt:
return m_L2CAP_Int_Handle[0].Bytes;
case L2CAP.PSM.HID_Service:
return m_L2CAP_Svc_Handle[0].Bytes;
}
throw new Exception("Invalid L2CAP Connection Type");
}
public override string ToString()
{
return String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2} - {6}",
m_Local[5],
m_Local[4],
m_Local[3],
m_Local[2],
m_Local[1],
m_Local[0],
m_Remote_Name
);
}
#region IEquatable<ScpBthConnection> Members
public virtual bool Equals(BthConnection other)
{
return m_HCI_Handle.Equals(other.m_HCI_Handle);
}
public virtual bool Equals(Byte Lsb, Byte Msb)
{
return m_HCI_Handle.Equals(Lsb, Msb);
}
public virtual bool Equals(Byte[] other)
{
return m_HCI_Handle.Equals(other);
}
#endregion
#region IComparable<ScpBthConnection> Members
public virtual int CompareTo(BthConnection other)
{
return m_HCI_Handle.CompareTo(other.m_HCI_Handle);
}
#endregion
}
}