-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathsetup_portal.cpp
More file actions
359 lines (307 loc) · 12.5 KB
/
Copy pathsetup_portal.cpp
File metadata and controls
359 lines (307 loc) · 12.5 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
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
/**
* @file setup_portal.cpp
* @brief WiFi AP captive portal for initial device configuration
*
* Starts an open AP ("WireClaw-Setup"), runs a DNS captive portal that
* redirects all domains to 192.168.4.1, and serves a config form on port 80.
* When the user submits, writes /config.json to LittleFS and reboots.
*/
#include "setup_portal.h"
#include <WiFi.h>
#include <DNSServer.h>
#include <WiFiServer.h>
#include <LittleFS.h>
#include <esp_task_wdt.h>
/* LED functions from main.cpp */
extern void led(uint8_t r, uint8_t g, uint8_t b);
extern void ledOff();
#define PORTAL_TIMEOUT_MS 600000 /* 10 minutes */
/*============================================================================
* HTML Pages (stored in flash)
*============================================================================*/
static const char SETUP_HTML[] PROGMEM = R"rawhtml(<!DOCTYPE html><html><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>WireClaw Setup</title><style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Courier New',monospace;background:#0a0a0a;color:#e0e0e0;padding:20px;max-width:480px;margin:0 auto}
h1{color:#00d4aa;font-size:1.5em;margin-bottom:4px}
.sub{color:#666;font-size:0.85em;margin-bottom:20px}
label{display:block;margin:12px 0 4px;color:#00d4aa;font-size:0.9em}
input[type=text],input[type=password]{width:100%;padding:10px;background:#1a1a1a;border:1px solid #333;color:#fff;font-family:inherit;font-size:0.95em;border-radius:4px}
input:focus{outline:none;border-color:#00d4aa}
.opt{color:#555;font-size:0.8em}
.sep{border-top:1px solid #222;margin:16px 0}
button{width:100%;padding:12px;margin-top:20px;background:#00d4aa;color:#0a0a0a;border:none;font-family:inherit;font-size:1em;font-weight:bold;cursor:pointer;border-radius:4px}
button:hover{background:#00b894}
</style></head><body>
<h1>> WireClaw Setup</h1>
<p class="sub">Configure your device</p>
<form method="POST" action="/save">
<label>WiFi SSID *</label>
<input type="text" name="wifi_ssid" required>
<label>WiFi Password *</label>
<input type="password" name="wifi_pass" required>
<div class="sep"></div>
<label>OpenRouter API Key</label>
<input type="text" name="api_key" placeholder="sk-or-v1-...">
<p class="opt">Required unless using local LLM</p>
<label>Model</label>
<input type="text" name="model" value="google/gemini-2.5-flash">
<label>Device Name</label>
<input type="text" name="device_name" value="wireclaw-01">
<div class="sep"></div>
<label>API Base URL</label>
<input type="text" name="api_base_url" placeholder="http://192.168.1.x:11434/v1">
<p class="opt">For local LLM (Ollama, etc.)</p>
<div class="sep"></div>
<label>NATS Host</label>
<input type="text" name="nats_host" placeholder="192.168.1.x">
<label>NATS Port</label>
<input type="text" name="nats_port" value="4222">
<div class="sep"></div>
<label>Telegram Bot Token</label>
<input type="text" name="telegram_token">
<label>Telegram Chat ID</label>
<input type="text" name="telegram_chat_id">
<div class="sep"></div>
<label>Timezone</label>
<input type="text" name="timezone" value="UTC0">
<p class="opt">POSIX TZ string (e.g. CET-1CEST,M3.5.0,M10.5.0/3)</p>
<button type="submit">Save & Reboot</button>
</form></body></html>)rawhtml";
static const char SAVED_HTML[] PROGMEM = R"rawhtml(<!DOCTYPE html><html><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>WireClaw - Saved</title><style>
body{font-family:'Courier New',monospace;background:#0a0a0a;color:#00d4aa;display:flex;align-items:center;justify-content:center;min-height:100vh;text-align:center}
h1{font-size:1.5em;margin-bottom:8px}p{color:#666}
</style></head><body>
<div><h1>Config saved!</h1><p>Rebooting...</p></div>
</body></html>)rawhtml";
/*============================================================================
* Helpers
*============================================================================*/
static void urlDecode(char *dst, const char *src, int maxLen) {
int w = 0;
while (*src && w < maxLen - 1) {
if (*src == '+') {
dst[w++] = ' ';
src++;
} else if (*src == '%' && src[1] && src[2]) {
char hex[3] = { src[1], src[2], 0 };
dst[w++] = (char)strtol(hex, nullptr, 16);
src += 3;
} else {
dst[w++] = *src++;
}
}
dst[w] = '\0';
}
/**
* Extract a URL-encoded form field from a POST body.
* Handles field names correctly (won't match partial names).
*/
static bool formGetField(const char *body, const char *name,
char *dst, int maxLen) {
char key[64];
snprintf(key, sizeof(key), "%s=", name);
int keyLen = strlen(key);
const char *p = body;
while ((p = strstr(p, key)) != nullptr) {
/* Ensure it's at start of body or after & */
if (p == body || *(p - 1) == '&') {
p += keyLen;
/* Extract raw value until & or end */
char encoded[256];
int w = 0;
while (*p && *p != '&' && w < (int)sizeof(encoded) - 1) {
encoded[w++] = *p++;
}
encoded[w] = '\0';
urlDecode(dst, encoded, maxLen);
return true;
}
p++;
}
dst[0] = '\0';
return false;
}
/** Write a JSON-escaped string value to a file */
static void writeJsonEscaped(File &f, const char *s) {
f.print('"');
while (*s) {
if (*s == '"' || *s == '\\') f.print('\\');
f.print(*s);
s++;
}
f.print('"');
}
/*============================================================================
* Config Writing
*============================================================================*/
static bool saveConfig(const char *body) {
/* Ensure LittleFS is mounted (format if needed for fresh flash) */
if (!LittleFS.begin(true)) {
Serial.printf("[Setup] LittleFS mount failed\n");
return false;
}
File f = LittleFS.open("/config.json", "w");
if (!f) {
Serial.printf("[Setup] Failed to open /config.json for writing\n");
return false;
}
char val[128];
f.print("{\n");
formGetField(body, "wifi_ssid", val, sizeof(val));
f.print(" \"wifi_ssid\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "wifi_pass", val, sizeof(val));
f.print(" \"wifi_pass\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "api_key", val, sizeof(val));
f.print(" \"api_key\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "model", val, sizeof(val));
if (val[0] == '\0') strncpy(val, "google/gemini-2.5-flash", sizeof(val));
f.print(" \"model\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "device_name", val, sizeof(val));
if (val[0] == '\0') strncpy(val, "wireclaw-01", sizeof(val));
f.print(" \"device_name\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "api_base_url", val, sizeof(val));
f.print(" \"api_base_url\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "nats_host", val, sizeof(val));
f.print(" \"nats_host\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "nats_port", val, sizeof(val));
if (val[0] == '\0') strncpy(val, "4222", sizeof(val));
f.print(" \"nats_port\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "telegram_token", val, sizeof(val));
f.print(" \"telegram_token\": "); writeJsonEscaped(f, val); f.print(",\n");
formGetField(body, "telegram_chat_id", val, sizeof(val));
f.print(" \"telegram_chat_id\": "); writeJsonEscaped(f, val); f.print(",\n");
f.print(" \"telegram_cooldown\": \"15\",\n");
formGetField(body, "timezone", val, sizeof(val));
if (val[0] == '\0') strncpy(val, "UTC0", sizeof(val));
f.print(" \"timezone\": "); writeJsonEscaped(f, val); f.print("\n");
f.print("}\n");
f.close();
Serial.printf("[Setup] Config saved to /config.json\n");
return true;
}
/*============================================================================
* HTTP Handler
*============================================================================*/
static void sendHtml(WiFiClient &client, const char *html) {
int len = strlen(html);
client.printf("HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Connection: close\r\n"
"Content-Length: %d\r\n\r\n", len);
/* Send in chunks to avoid WiFiClient buffer limits */
const uint8_t *p = (const uint8_t *)html;
while (len > 0) {
int chunk = len > 1024 ? 1024 : len;
client.write(p, chunk);
p += chunk;
len -= chunk;
}
}
static bool handleClient(WiFiClient &client) {
/* Wait for data with timeout */
unsigned long timeout = millis() + 3000;
while (!client.available() && millis() < timeout) {
delay(1);
}
if (!client.available()) {
client.stop();
return false;
}
/* Read request line */
String requestLine = client.readStringUntil('\n');
requestLine.trim();
bool isPost = requestLine.startsWith("POST");
/* Read headers */
int contentLength = 0;
while (client.connected()) {
String header = client.readStringUntil('\n');
header.trim();
if (header.length() == 0) break;
if (header.startsWith("Content-Length:") ||
header.startsWith("content-length:")) {
contentLength = header.substring(15).toInt();
}
}
if (isPost && contentLength > 0) {
/* Read POST body */
static char body[2048];
int toRead = contentLength < (int)sizeof(body) - 1
? contentLength : (int)sizeof(body) - 1;
int bytesRead = client.readBytes(body, toRead);
body[bytesRead] = '\0';
if (saveConfig(body)) {
sendHtml(client, SAVED_HTML);
client.stop();
Serial.printf("[Setup] Config saved, rebooting in 2s...\n");
delay(2000);
ESP.restart();
return true; /* unreachable */
} else {
client.print("HTTP/1.1 500 Error\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n\r\n"
"Failed to save config. Try again.");
}
} else {
/* Serve the setup form for any GET request */
sendHtml(client, SETUP_HTML);
}
client.stop();
return false;
}
/*============================================================================
* Main Portal Entry Point
*============================================================================*/
void runSetupPortal() {
/* Wait for serial monitor to connect (USB-CDC can be slow) */
Serial.printf("[Setup] Waiting for serial connection...\n");
unsigned long serialWait = millis();
while (!Serial && millis() - serialWait < 3000) {
delay(100);
}
/* Switch to AP mode */
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
WiFi.softAP("WireClaw-Setup");
delay(500); /* Let AP settle */
/* Register with watchdog (setup() hasn't done this yet) */
esp_task_wdt_config_t wdt_cfg = { .timeout_ms = 60000, .idle_core_mask = 0,
.trigger_panic = true };
esp_task_wdt_reconfigure(&wdt_cfg);
esp_task_wdt_add(NULL);
IPAddress apIP = WiFi.softAPIP();
Serial.printf("[Setup] AP started on %s\n", apIP.toString().c_str());
Serial.printf("[Setup] Connect to WiFi 'WireClaw-Setup' to configure\n");
Serial.printf("[Setup] Portal timeout: %d seconds\n", PORTAL_TIMEOUT_MS / 1000);
/* DNS captive portal — resolve all domains to our IP */
DNSServer dns;
dns.start(53, "*", apIP);
/* Web server on port 80 */
WiFiServer server(80);
server.begin();
unsigned long startTime = millis();
while (millis() - startTime < PORTAL_TIMEOUT_MS) {
esp_task_wdt_reset();
dns.processNextRequest();
/* Pulsing cyan LED — triangle wave, 2s period */
unsigned long ms = millis() % 2000;
uint8_t brightness = (ms < 1000)
? (uint8_t)(ms * 255 / 1000)
: (uint8_t)((2000 - ms) * 255 / 1000);
led(0, brightness, brightness);
WiFiClient client = server.accept();
if (client) {
handleClient(client);
}
delay(5);
}
/* Timeout — reboot and try again */
Serial.printf("[Setup] Portal timeout, rebooting...\n");
ledOff();
delay(1000);
ESP.restart();
}