-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStations.cpp
373 lines (282 loc) · 8.03 KB
/
Stations.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* This software is licensed under the MIT License: https://github.com/spacehuhntech/esp8266_deauther */
#include "Stations.h"
Stations::Stations() {
list = new SimpleList<Station>();
}
void Stations::add(uint8_t* mac, int accesspointNum) {
int stationNum = findStation(mac);
if (stationNum < 0) {
internal_add(mac, accesspointNum);
// print(list->size() - 1, list->size() == 1, false);
} else {
*getPkts(stationNum) += 1;
*getTime(stationNum) = currentTime;
}
changed = true;
}
int Stations::findStation(uint8_t* mac) {
int c = count();
for (int i = 0; i < c; i++) {
if (memcmp(getMac(i), mac, 6) == 0) return i;
}
return -1;
}
void Stations::sort() {
list->setCompare([](Station& a, Station& b) -> int {
if (*(a.pkts) > *(b.pkts)) return -1;
if (*(a.pkts) == *(b.pkts)) return 0;
return 1;
});
list->sort();
}
void Stations::sortAfterChannel() {
list->setCompare([](Station& a, Station& b) -> int {
if (a.ch < b.ch) return -1;
if (a.ch == b.ch) return 0;
return 1;
});
list->sort();
}
void Stations::removeAll() {
internal_removeAll();
prntln(ST_CLEARED_LIST);
changed = true;
}
void Stations::removeOldest() {
int oldest = 0;
int c = count();
for (int i = 1; i < c; i++) {
if (*getTime(i) > *getTime(oldest)) oldest = i;
}
internal_remove(oldest);
changed = true;
}
void Stations::printAll() {
prntln(ST_HEADER);
int c = count();
if (c == 0) prntln(ST_LIST_EMPTY);
else
for (int i = 0; i < c; i++) print(i, i == 0, i == c - 1);
}
void Stations::printSelected() {
prntln(ST_HEADER);
int max = selected();
int c = count();
if (max == 0) {
prntln(ST_NO_DEVICES_SELECTED);
return;
}
for (int i = 0, j = 0; i < c && j < max; i++) {
if (getSelected(i)) {
print(i, j == 0, j == max - 1);
j++;
}
}
}
void Stations::print(int num) {
print(num, true, true);
}
void Stations::print(int num, bool header, bool footer) {
if (!check(num)) return;
if (header) {
prntln(ST_TABLE_HEADER);
prntln(ST_TABLE_DIVIDER);
}
prnt(leftRight(String(), (String)num, 2));
prnt(leftRight(String(SPACE) + getMacStr(num), String(), 18));
prnt(leftRight(String(SPACE), (String)getCh(num), 3));
prnt(leftRight(String(SPACE) + getNameStr(num), String(), 17));
prnt(leftRight(String(SPACE) + getVendorStr(num), String(), 9));
prnt(leftRight(String(SPACE), (String) * getPkts(num), 9));
prnt(leftRight(String(SPACE) + getAPStr(num), String(), 33));
prnt(leftRight(String(SPACE) + getTimeStr(num), String(), 10));
prntln(leftRight(String(SPACE) + getSelectedStr(num), String(), 9));
if (footer) prntln(ST_TABLE_DIVIDER);
}
String Stations::getAPStr(int num) {
if (getAP(num) < 0) return String();
return accesspoints.getSSID(getAP(num));
}
uint8_t* Stations::getAPMac(int num) {
if (!check(num)) return 0;
return WiFi.BSSID(list->get(num).ap);
}
String Stations::getAPMacStr(int num) {
if (!check(num)) return String();
uint8_t* mac = getAPMac(num);
return bytesToStr(mac, 6);
}
int Stations::getAP(int num) {
if (!check(num)) return -1;
return accesspoints.find(list->get(num).ap);
}
String Stations::getNameStr(int num) {
if (!check(num)) return String();
return names.find(getMac(num));
}
bool Stations::hasName(int num) {
if (!check(num)) return false;
return names.findID(getMac(num)) >= 0;
}
uint8_t* Stations::getMac(int num) {
if (!check(num)) return 0;
return list->get(num).mac;
}
String Stations::getMacStr(int num) {
if (!check(num)) return String();
uint8_t* mac = getMac(num);
return bytesToStr(mac, 6);
}
String Stations::getMacVendorStr(int num) {
String value;
if (check(num)) {
value = getVendorStr(num) + ":";
uint8_t* mac = getMac(num);
for (int i = 3; i < 6; i++) {
if (mac[i] < 0x10) value += "0";
value += String(mac[i], HEX);
if (i < 5) value += ":";
}
}
return value;
}
String Stations::getVendorStr(int num) {
if (!check(num)) return String();
return searchVendor(list->get(num).mac);
}
String Stations::getSelectedStr(int num) {
return b2a(getSelected(num));
}
uint32_t* Stations::getPkts(int num) {
if (!check(num)) return NULL;
return list->get(num).pkts;
}
uint32_t* Stations::getTime(int num) {
if (!check(num)) return NULL;
return list->get(num).time;
}
String Stations::getTimeStr(int num) {
if (!check(num)) return String();
uint32_t difference = currentTime - *getTime(num);
if (difference < 1000) return str(ST_SMALLER_ONESEC);
else if (difference < 60000) return str(ST_SMALLER_ONEMIN);
else {
uint32_t minutes = difference / 60000;
if (minutes > 60) return str(ST_BIGER_ONEHOUR);
else return (String)minutes + str(STR_MIN);
}
}
bool Stations::getSelected(int num) {
if (!check(num)) return false;
return list->get(num).selected;
}
uint8_t Stations::getCh(int num) {
if (!check(num)) return 0;
return list->get(num).ch;
}
void Stations::select(int num) {
if (!check(num)) return;
internal_select(num);
prnt(ST_SELECTED_STATION);
prntln(num);
changed = true;
}
void Stations::deselect(int num) {
if (!check(num)) return;
internal_deselect(num);
prnt(ST_DESELECTED_STATION);
prntln(num);
changed = true;
}
void Stations::remove(int num) {
if (!check(num)) return;
prnt(ST_REMOVED_STATION);
prntln(num);
internal_remove(num);
changed = true;
}
void Stations::select(String ssid) {
for (int i = 0; i < list->size(); i++) {
if (getAPStr(i).equalsIgnoreCase(ssid)) select(i);
}
}
void Stations::deselect(String ssid) {
for (int i = 0; i < list->size(); i++) {
if (getAPStr(i).equalsIgnoreCase(ssid)) deselect(i);
}
}
void Stations::remove(String ssid) {
for (int i = 0; i < list->size(); i++) {
if (getAPStr(i).equalsIgnoreCase(ssid)) remove(i);
}
}
void Stations::selectAll() {
for (int i = 0; i < count(); i++) internal_select(i);
prntln(ST_SELECTED_ALL);
changed = true;
}
void Stations::deselectAll() {
for (int i = 0; i < count(); i++) internal_deselect(i);
prntln(ST_DESELECTED_ALL);
changed = true;
}
int Stations::count() {
return list->size();
}
int Stations::selected() {
int num = 0;
for (int i = 0; i < count(); i++)
if (getSelected(i)) num++;
return num;
}
bool Stations::check(int num) {
if (internal_check(num)) {
return true;
} else {
prnt(ST_ERROR_ID);
prntln(num);
return false;
}
}
bool Stations::internal_check(int num) {
return num >= 0 && num < count();
}
void Stations::internal_select(int num) {
Station changedStation = list->get(num);
changedStation.selected = true;
list->replace(num, changedStation);
}
void Stations::internal_deselect(int num) {
Station changedStation = list->get(num);
changedStation.selected = false;
list->replace(num, changedStation);
}
void Stations::internal_remove(int num) {
free(getMac(num));
free(getPkts(num));
free(getTime(num));
list->remove(num);
}
void Stations::internal_add(uint8_t* mac, int accesspointNum) {
if (count() >= STATION_LIST_SIZE) removeOldest();
Station newStation;
newStation.ap = accesspointNum;
newStation.ch = wifi_channel;
newStation.mac = (uint8_t*)malloc(6);
newStation.pkts = (uint32_t*)malloc(sizeof(uint32_t));
newStation.time = (uint32_t*)malloc(sizeof(uint32_t));
newStation.selected = false;
memcpy(newStation.mac, mac, 6);
*newStation.pkts = 1;
*newStation.time = currentTime;
list->add(newStation);
}
void Stations::internal_removeAll() {
int c = count();
for (int i = 0; i < c; i++) {
free(getMac(i));
free(getPkts(i));
free(getTime(i));
}
list->clear();
}