-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathWifiControl.cpp
More file actions
253 lines (220 loc) · 6.82 KB
/
WifiControl.cpp
File metadata and controls
253 lines (220 loc) · 6.82 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
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
#include "../Configuration.hpp"
#include "Utility.hpp"
#include "WifiControl.hpp"
#include "MeadeCommandProcessor.hpp"
#include "Mount.hpp"
#if (WIFI_ENABLED == 1)
WifiControl::WifiControl(Mount *mount, LcdMenu *lcdMenu)
{
_mount = mount;
_lcdMenu = lcdMenu;
}
void WifiControl::setup()
{
LOG(DEBUG_WIFI, "[WIFI]: Starting up Wifi As Mode %d\n", WIFI_MODE);
_cmdProcessor = MeadeCommandProcessor::instance();
switch (WIFI_MODE)
{
case WIFI_MODE_INFRASTRUCTURE: // startup Infrastructure Mode
startInfrastructureMode();
break;
case WIFI_MODE_AP_ONLY: // startup AP mode
startAccessPointMode();
break;
case WIFI_MODE_ATTEMPT_INFRASTRUCTURE_FAIL_TO_AP: // Attempt Infra, fail over to AP
startInfrastructureMode();
_infraStart = millis();
break;
case WIFI_MODE_DISABLED: // Disabled
WiFi.mode(WIFI_OFF);
btStop();
break;
}
}
void WifiControl::startInfrastructureMode()
{
LOG(DEBUG_WIFI, "[WIFI]: Starting Infrastructure Mode Wifi");
LOG(DEBUG_WIFI, "[WIFI]: with host name: %s", String(WIFI_HOSTNAME).c_str());
LOG(DEBUG_WIFI, "[WIFI]: for SSID: %s", String(WIFI_INFRASTRUCTURE_MODE_SSID).c_str());
LOG(DEBUG_WIFI, "[WIFI]: and WPA key: %s", String(WIFI_INFRASTRUCTURE_MODE_WPAKEY).c_str());
#if defined(ESP32)
WiFi.setHostname(WIFI_HOSTNAME);
#endif
WiFi.begin(WIFI_INFRASTRUCTURE_MODE_SSID, WIFI_INFRASTRUCTURE_MODE_WPAKEY);
}
void WifiControl::startAccessPointMode()
{
LOG(DEBUG_WIFI, "[WIFI]: Starting AP Mode Wifi");
IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
#if defined(ESP32)
WiFi.setHostname(WIFI_HOSTNAME);
#endif
WiFi.softAP(WIFI_HOSTNAME, WIFI_AP_MODE_WPAKEY);
WiFi.softAPConfig(local_ip, gateway, subnet);
}
String wifiStatus(int status)
{
if (status == WL_IDLE_STATUS)
return "Idle.";
if (status == WL_NO_SSID_AVAIL)
return "No SSID available.";
if (status == WL_SCAN_COMPLETED)
return "Scan completed.";
if (status == WL_CONNECTED)
return "Connected!";
if (status == WL_CONNECT_FAILED)
return "Connect failed.";
if (status == WL_CONNECTION_LOST)
return "Connection Lost.";
if (status == WL_DISCONNECTED)
return "Disconnected.";
return "Status " + String(status);
}
String WifiControl::getStatus()
{
if (WIFI_MODE == WIFI_MODE_DISABLED)
{
return "0,";
}
String result = "1,";
if (WIFI_MODE == WIFI_MODE_INFRASTRUCTURE)
{
result += "Infrastructure,";
}
else if (WIFI_MODE == WIFI_MODE_AP_ONLY)
{
result += "Access Point,";
}
else if (WIFI_MODE == WIFI_MODE_ATTEMPT_INFRASTRUCTURE_FAIL_TO_AP)
{
result += "Infra-Fail-To-AP,";
}
result += wifiStatus(WiFi.status()) + ",";
#if defined(ESP32)
result += WiFi.getHostname();
#endif
result += "," + WiFi.localIP().toString() + ":" + WIFI_PORT;
result += "," + String(WIFI_INFRASTRUCTURE_MODE_SSID) + "," + String(WIFI_HOSTNAME);
return result;
}
void WifiControl::loop()
{
if (WIFI_MODE == WIFI_MODE_DISABLED)
{
return;
}
if (_status != WiFi.status())
{
_status = WiFi.status();
LOG(DEBUG_WIFI, "[WIFI]: Connected status changed to %s", wifiStatus(_status).c_str());
if (_status == WL_CONNECTED)
{
delete _tcpServer;
_tcpServer = new WiFiServer(WIFI_PORT);
_tcpServer->begin();
_tcpServer->setNoDelay(true);
delete _udp;
_udp = new WiFiUDP();
_udp->begin(4031);
LOG(DEBUG_WIFI,
"[WIFI]: Connecting to SSID %s at %s:%d",
WIFI_INFRASTRUCTURE_MODE_SSID,
WiFi.localIP().toString().c_str(),
WIFI_PORT);
}
}
_mount->loop();
if (_status != WL_CONNECTED)
{
infraToAPFailover();
return;
}
tcpLoop();
udpLoop();
}
void WifiControl::infraToAPFailover()
{
if (_infraStart != 0 && !WiFi.isConnected() && _infraStart + _infraWait < millis())
{
WiFi.disconnect();
startAccessPointMode();
_infraStart = 0;
LOG(DEBUG_WIFI, "[WIFI]: Could not connect to Infra, Starting AP.");
}
}
void WifiControl::tcpLoop()
{
if (client && client.connected())
{
while (client.available())
{
LOG(DEBUG_WIFI, "[WIFITCP]: Available bytes %d. Peeking.", client.available());
// Peek first byte and check for ACK (0x06) handshake
LOG(DEBUG_WIFI, "[WIFITCP]: First byte is %x", client.peek());
if (client.peek() == 0x06)
{
client.read();
LOG(DEBUG_WIFI, "[WIFITCP]: Query <-- Handshake request");
client.write("P");
LOG(DEBUG_WIFI, "[WIFITCP]: Reply --> P (polar mode)");
}
else
{
String cmd = client.readStringUntil('#');
LOG(DEBUG_WIFI, "[WIFITCP]: Query <-- %s#", cmd.c_str());
const char *retVal = _cmdProcessor->processCommand(cmd);
if (retVal[0] != '\0')
{
client.write(retVal);
LOG(DEBUG_WIFI, "[WIFITCP]: Reply --> %s", retVal);
}
else
{
LOG(DEBUG_WIFI, "[WIFITCP]: No Reply");
}
}
_mount->loop();
}
}
else
{
client = _tcpServer->available();
}
}
void WifiControl::udpLoop()
{
int packetSize = _udp->parsePacket();
if (packetSize)
{
String lookingFor = "skyfi:";
String reply = "skyfi:";
reply += WIFI_HOSTNAME;
reply += "@";
reply += WiFi.localIP().toString();
LOG(DEBUG_WIFI,
"[WIFIUDP]: Received %d bytes from %s, port %d",
packetSize,
_udp->remoteIP().toString().c_str(),
_udp->remotePort());
char incomingPacket[256];
int len = _udp->read(incomingPacket, sizeof(incomingPacket) - 1);
incomingPacket[len] = 0;
LOG(DEBUG_WIFI, "[WIFIUDP]: Received: %s", incomingPacket);
incomingPacket[lookingFor.length()] = 0;
if (lookingFor.equalsIgnoreCase(incomingPacket))
{
_udp->beginPacket(_udp->remoteIP(), 4031);
/*unsigned char bytes[255];
reply.getBytes(bytes, 255);
_udp->write(bytes, reply.length());*/
#if defined(ESP32)
_udp->print(reply.c_str());
#endif
_udp->endPacket();
LOG(DEBUG_WIFI, "[WIFIUDP]: Replied: %s", reply.c_str());
}
}
}
#endif