-
Notifications
You must be signed in to change notification settings - Fork 9
/
CaptivePortal.cpp
175 lines (132 loc) · 4.36 KB
/
CaptivePortal.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
#include "CaptivePortal.h"
#include "CaptivePortalInstance.h"
#include "CommandHandler.h"
#include "config/Config.h"
#include "GatewayConnectionManager.h"
#include "Logging.h"
#include "Time.h"
#include <ESPAsyncWebServer.h>
#include <WebSocketsServer.h>
#include <WiFi.h>
#include <mdns.h>
#include <memory>
static const char* TAG = "CaptivePortal";
using namespace OpenShock;
static bool s_alwaysEnabled = false;
static bool s_forceClosed = false;
static std::unique_ptr<CaptivePortalInstance> s_instance = nullptr;
bool _startCaptive() {
if (s_instance != nullptr) {
ESP_LOGD(TAG, "Already started");
return true;
}
ESP_LOGI(TAG, "Starting captive portal");
if (!WiFi.enableAP(true)) {
ESP_LOGE(TAG, "Failed to enable AP mode");
return false;
}
if (!WiFi.softAP((OPENSHOCK_FW_AP_PREFIX + WiFi.macAddress()).c_str())) {
ESP_LOGE(TAG, "Failed to start AP");
WiFi.enableAP(false);
return false;
}
IPAddress apIP(10, 10, 10, 10);
if (!WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0))) {
ESP_LOGE(TAG, "Failed to configure AP");
WiFi.softAPdisconnect(true);
return false;
}
esp_err_t err = mdns_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize mDNS");
WiFi.softAPdisconnect(true);
return false;
}
err = mdns_hostname_set(OPENSHOCK_FW_HOSTNAME);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set mDNS hostname");
WiFi.softAPdisconnect(true);
return false;
}
s_instance = std::make_unique<CaptivePortalInstance>();
return true;
}
void _stopCaptive() {
if (s_instance == nullptr) {
ESP_LOGD(TAG, "Already stopped");
return;
}
ESP_LOGI(TAG, "Stopping captive portal");
s_instance = nullptr;
mdns_free();
WiFi.softAPdisconnect(true);
}
using namespace OpenShock;
void CaptivePortal::SetAlwaysEnabled(bool alwaysEnabled) {
s_alwaysEnabled = alwaysEnabled;
Config::SetCaptivePortalConfig({
.alwaysEnabled = alwaysEnabled,
});
}
bool CaptivePortal::IsAlwaysEnabled() {
return s_alwaysEnabled;
}
bool CaptivePortal::ForceClose(std::uint32_t timeoutMs) {
s_forceClosed = true;
if (s_instance == nullptr) return true;
while (timeoutMs > 0) {
std::uint32_t delay = std::min(timeoutMs, static_cast<std::uint32_t>(10U));
vTaskDelay(pdMS_TO_TICKS(delay));
timeoutMs -= delay;
if (s_instance == nullptr) return true;
}
return false;
}
bool CaptivePortal::IsRunning() {
return s_instance != nullptr;
}
void CaptivePortal::Update() {
bool gatewayConnected = GatewayConnectionManager::IsConnected();
bool commandHandlerOk = CommandHandler::Ok();
bool shouldBeRunning = (s_alwaysEnabled || !gatewayConnected || !commandHandlerOk) && !s_forceClosed;
if (s_instance == nullptr) {
if (shouldBeRunning) {
ESP_LOGD(TAG, "Starting captive portal");
ESP_LOGD(TAG, " alwaysEnabled: %s", s_alwaysEnabled ? "true" : "false");
ESP_LOGD(TAG, " forceClosed: %s", s_forceClosed ? "true" : "false");
ESP_LOGD(TAG, " isConnected: %s", gatewayConnected ? "true" : "false");
ESP_LOGD(TAG, " commandHandlerOk: %s", commandHandlerOk ? "true" : "false");
_startCaptive();
}
return;
}
if (!shouldBeRunning) {
ESP_LOGD(TAG, "Stopping captive portal");
ESP_LOGD(TAG, " alwaysEnabled: %s", s_alwaysEnabled ? "true" : "false");
ESP_LOGD(TAG, " forceClosed: %s", s_forceClosed ? "true" : "false");
ESP_LOGD(TAG, " isConnected: %s", gatewayConnected ? "true" : "false");
ESP_LOGD(TAG, " commandHandlerOk: %s", commandHandlerOk ? "true" : "false");
_stopCaptive();
return;
}
}
bool CaptivePortal::SendMessageTXT(std::uint8_t socketId, StringView data) {
if (s_instance == nullptr) return false;
s_instance->sendMessageTXT(socketId, data);
return true;
}
bool CaptivePortal::SendMessageBIN(std::uint8_t socketId, const std::uint8_t* data, std::size_t len) {
if (s_instance == nullptr) return false;
s_instance->sendMessageBIN(socketId, data, len);
return true;
}
bool CaptivePortal::BroadcastMessageTXT(StringView data) {
if (s_instance == nullptr) return false;
s_instance->broadcastMessageTXT(data);
return true;
}
bool CaptivePortal::BroadcastMessageBIN(const std::uint8_t* data, std::size_t len) {
if (s_instance == nullptr) return false;
s_instance->broadcastMessageBIN(data, len);
return true;
}