-
Notifications
You must be signed in to change notification settings - Fork 96
/
dongle.cpp
309 lines (252 loc) · 7.18 KB
/
dongle.cpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (C) 2019 Medusalix
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "dongle.h"
#include "../utils/log.h"
Dongle::Dongle(
std::unique_ptr<UsbDevice> usbDevice
) : Mt76(std::move(usbDevice)), stopThreads(false)
{
Log::info("Dongle initialized");
threads.emplace_back(
&Dongle::readBulkPackets,
this,
MT_EP_READ
);
threads.emplace_back(
&Dongle::readBulkPackets,
this,
MT_EP_READ_PACKET
);
}
Dongle::~Dongle()
{
stopThreads = true;
// Wait for all threads to shut down
for (std::thread &thread : threads)
{
if (thread.joinable())
{
thread.join();
}
}
}
void Dongle::handleControllerConnect(Bytes address)
{
std::lock_guard<std::mutex> lock(controllerMutex);
uint8_t wcid = associateClient(address);
if (wcid == 0)
{
Log::error("Failed to associate controller");
return;
}
GipDevice::SendPacket sendPacket = std::bind(
&Dongle::sendClientPacket,
this,
wcid,
address,
std::placeholders::_1
);
controllers[wcid - 1].reset(new Controller(sendPacket));
Log::info("Controller '%d' connected", wcid);
}
void Dongle::handleControllerDisconnect(uint8_t wcid)
{
// Ignore invalid WCIDs
if (wcid == 0 || wcid > MT_WCID_COUNT)
{
return;
}
std::lock_guard<std::mutex> lock(controllerMutex);
// Ignore unconnected controllers
if (!controllers[wcid - 1])
{
return;
}
controllers[wcid - 1].reset();
if (!removeClient(wcid))
{
Log::error("Failed to remove controller");
return;
}
Log::info("Controller '%d' disconnected", wcid);
}
void Dongle::handleControllerPair(Bytes address, const Bytes &packet)
{
// Ignore invalid packets
if (packet.size() < sizeof(ReservedFrame))
{
return;
}
const ReservedFrame *frame = packet.toStruct<ReservedFrame>();
// Type 0x01 is for pairing requests
if (frame->type != 0x01)
{
return;
}
if (!pairClient(address))
{
Log::error("Failed to pair controller");
return;
}
if (!setPairingStatus(false))
{
Log::error("Failed to disable pairing");
return;
}
Log::debug(
"Controller paired: %s",
Log::formatBytes(address).c_str()
);
}
void Dongle::handleControllerPacket(uint8_t wcid, const Bytes &packet)
{
// Invalid WCID
if (wcid == 0 || wcid > MT_WCID_COUNT)
{
return;
}
// Ignore invalid or empty packets
if (packet.size() <= sizeof(QosFrame) + sizeof(uint16_t))
{
return;
}
// Skip 2 bytes of padding
const Bytes data(packet, sizeof(QosFrame) + sizeof(uint16_t));
std::lock_guard<std::mutex> lock(controllerMutex);
// Ignore unconnected controllers
if (!controllers[wcid - 1])
{
return;
}
if (!controllers[wcid - 1]->handlePacket(data))
{
Log::error("Error handling packet for controller '%d'", wcid);
}
}
void Dongle::handleWlanPacket(const Bytes &packet)
{
// Ignore invalid or empty packets
if (packet.size() <= sizeof(RxWi) + sizeof(WlanFrame))
{
return;
}
const RxWi *rxWi = packet.toStruct<RxWi>();
const WlanFrame *wlanFrame = packet.toStruct<WlanFrame>(sizeof(RxWi));
const Bytes source(
wlanFrame->source,
wlanFrame->source + macAddress.size()
);
const Bytes destination(
wlanFrame->destination,
wlanFrame->destination + macAddress.size()
);
// Packet has wrong destination address
if (destination != macAddress)
{
return;
}
uint8_t type = wlanFrame->frameControl.type;
uint8_t subtype = wlanFrame->frameControl.subtype;
if (type == MT_WLAN_MANAGEMENT)
{
switch (subtype)
{
case MT_WLAN_ASSOCIATION_REQ:
handleControllerConnect(source);
break;
// Only kept for compatibility with 1537 controllers
// They associate, disassociate and associate again during pairing
// Disassociations happen without triggering EVT_CLIENT_LOST
case MT_WLAN_DISASSOCIATION:
handleControllerDisconnect(rxWi->wcid);
break;
// Reserved frames are used for different purposes
// Most of them are yet to be discovered
case MT_WLAN_RESERVED:
const Bytes innerPacket(
packet,
sizeof(RxWi) + sizeof(WlanFrame)
);
handleControllerPair(source, innerPacket);
break;
}
}
else if (type == MT_WLAN_DATA && subtype == MT_WLAN_QOS_DATA)
{
const Bytes innerPacket(
packet,
sizeof(RxWi) + sizeof(WlanFrame)
);
handleControllerPacket(rxWi->wcid, innerPacket);
}
}
void Dongle::handleBulkData(const Bytes &data)
{
// Ignore invalid or empty data
if (data.size() <= sizeof(RxInfoGeneric) + sizeof(uint32_t))
{
return;
}
// Skip packet end marker (4 bytes, identical to header)
const RxInfoGeneric *rxInfo = data.toStruct<RxInfoGeneric>();
const Bytes packet(data, sizeof(RxInfoGeneric), sizeof(uint32_t));
if (rxInfo->port == CPU_RX_PORT)
{
const RxInfoCommand *info = data.toStruct<RxInfoCommand>();
switch (info->eventType)
{
case EVT_BUTTON_PRESS:
// Setting the pairing status doesn't require locking the mutex
setPairingStatus(true);
break;
case EVT_PACKET_RX:
handleWlanPacket(packet);
break;
case EVT_CLIENT_LOST:
// Packet is guaranteed not to be empty
handleControllerDisconnect(packet[0]);
break;
}
}
else if (rxInfo->port == WLAN_PORT)
{
const RxInfoPacket *info = data.toStruct<RxInfoPacket>();
if (info->is80211)
{
handleWlanPacket(packet);
}
}
}
void Dongle::readBulkPackets(uint8_t endpoint)
{
FixedBytes<USB_MAX_BULK_TRANSFER_SIZE> buffer;
while (!stopThreads)
{
int transferred = usbDevice->bulkRead(endpoint, buffer);
// Bulk read failed
if (transferred < 0)
{
break;
}
if (transferred > 0)
{
Bytes data = buffer.toBytes(transferred);
handleBulkData(data);
}
}
}