-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXUI_ScreenScan.cpp
More file actions
173 lines (139 loc) · 4.42 KB
/
XUI_ScreenScan.cpp
File metadata and controls
173 lines (139 loc) · 4.42 KB
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
#include "XUI_Engine.h"
#include "XUI_ScreenScan.h"
#include "WifiScanner.h"
#include "ESP8266WiFi.h"
XUI_ScreenScan::XUI_ScreenScan(string title, uint8_t scanMode, WifiScanner wifiScanner)
: XUI_Screen(title)
{
this->scanMode = scanMode;
this->wifiScanner = wifiScanner;
_draw = true;
xuiScroller = new XUI_Scroller(4, dynamic_cast<XUI_EventListener*> (this));
}
#define SCAN_TIME 15000
#define STA_SCAN_OUTPUT_TIME 3000
#define STA_SCAN_CHANNEL_TIME 500
void XUI_ScreenScan::start(){
uint32_t startTime = millis();
selectedAPs.clear();
selectedStations.clear();
packets.clear();
// scan APS
this->wifiScanner.startScan(true, true);
do{
this->wifiScanner.runScan();
this->showScanInfo(wifiScanner.APCount, STACount, (millis()-startTime)*100/SCAN_TIME);
} while (this->wifiScanner.APCount < 0);
if (this->wifiScanner.APCount == 0){
// no APs foun
}else{
// scan stations
uint32_t currentTime, outputTime, channelTime = millis();
uint8_t currentAP = 0;
this->wifiScanner.setChannel(WiFi.channel(currentAP));
this->wifiScanner.startSniff();
while (currentTime - startTime < SCAN_TIME){
STACount = this->wifiScanner.runSniff();
// print status every 3s
if (currentTime - outputTime > STA_SCAN_OUTPUT_TIME) {
outputTime = currentTime;
this->showScanInfo(wifiScanner.APCount, STACount, (millis()-startTime)*100/SCAN_TIME);
}
// channel hopping
if (currentTime - channelTime > STA_SCAN_CHANNEL_TIME) {
channelTime = currentTime;
// go to next AP channel
if (++currentAP == wifiScanner.APCount) currentAP = 0;
this->wifiScanner.setChannel(WiFi.channel(currentAP));
}
currentTime = millis();
}
// end scan
this->wifiScanner.stopSniff();
}
}
void XUI_ScreenScan::run(){
if (scanMode == SCAN_MODE_STATIONS) {
stations.sort();
stations.printAll();
}
start(SCAN_MODE_OFF);
}
do{
xuiDisplay->clear();
xuiDisplay->drawTextCenter("Scanning", 1, DISPLAY_LINES_5);
xuiDisplay->drawUnknowProgress(3, DISPLAY_LINES_5);
xuiDisplay->display();
networkCount = WiFi.scanComplete();
} while (networkCount < 0);
size_t networkCount = getSize();
if (networkCount == 0 && _draw){ // avoid drawing when we have no items to draw
xuiDisplay->clear();
} else if (_draw){ // avoid drawing when nothing has changed
_draw = xuiScroller->scrolling();
xuiDisplay->clear();
xuiDisplay->drawList(dynamic_cast<XUI_List*> (this), DISPLAY_LINES_5, currentIndex, xuiScroller->getOffset());
xuiDisplay->drawLineItemSelector(DISPLAY_LINES_5);
xuiDisplay->drawScrollIndicator(currentIndex, networkCount);
xuiDisplay->display();
}
}
size_t XUI_ScreenScan::showScanInfo(uint8_t APCount, uint8_t STACount, uint8_t progress){
xuiDisplay->clear();
xuiDisplay->drawText("Scanning...", 1, DISPLAY_LINES_4);
xuiDisplay->drawProgress(progress, 2, DISPLAY_LINES_4);
xuiDisplay->drawText("APs: " + APCount, 3, DISPLAY_LINES_4);
xuiDisplay->drawText("Stations: " + STACount, 3, DISPLAY_LINES_4);
xuiDisplay->display();
}
string XUI_ScreenScan::getElement(uint8_t index){
WiFi.getNetworkInfo(index, ssid, encryptionType, RSSI, BSSID, channel, isHidden);
string power = " ";
if (RSSI >= -30){
power = "IIII ";
}else if (RSSI >= -65){
power = " III ";
}else if (RSSI >= -75){
power = " II ";
}else if (RSSI >= -90){
power = " I ";
}
return string(power).append(ssid.c_str()); //.append(encryptionType == ENC_TYPE_NONE ? "open " : " ").append(isHidden ? "hidden" : "");
};
size_t XUI_ScreenScan::getSize(){
return networkCount;
}
void XUI_ScreenScan::event(uint8_t event){
switch (event) {
case EVT_SCROLL_UP_END:
currentIndex++;
if (currentIndex == networkCount) currentIndex = 0;
break;
case EVT_SCROLL_DOWN_END:
if (currentIndex == 0) {
currentIndex = networkCount-1;
}else{
currentIndex--;
}
break;
}
};
void XUI_ScreenScan::bttn_event(uint8_t button){
_draw = true;
switch (button){
case BTTN_UP:
xuiScroller->scrollDown();
_draw = true;
break;
case BTTN_DOWN:
xuiScroller->scrollUp();
_draw = true;
break;
case BTTN_LEFT:
xuiEngine->nav(prevScreen);
break;
case BTTN_RIGHT:
// TODO ir al detalle de la entrada seleccionada
break;
}
}