323 lines
11 KiB
C++
323 lines
11 KiB
C++
#include "webinterface.h"
|
|
#include <Arduino.h>
|
|
#include <DNSServer.h>
|
|
#include <Preferences.h>
|
|
#include <Update.h>
|
|
#include <WebServer.h>
|
|
#include <WiFi.h>
|
|
#include <cctype>
|
|
|
|
namespace
|
|
{
|
|
constexpr char default_access_point_password[] = "telemetry";
|
|
constexpr char settings_namespace[] = "telemetry";
|
|
constexpr char default_callsign[] = "D1234";
|
|
constexpr int default_min_speed = 80;
|
|
constexpr int default_max_speed = 150;
|
|
constexpr unsigned int default_active_time_seconds = 90;
|
|
constexpr unsigned long access_point_timeout_ms = 5UL * 60UL * 1000UL;
|
|
constexpr char telemetry_ap_prefix[] = "Telemetry-";
|
|
|
|
String default_access_point_name()
|
|
{
|
|
#ifdef DEVICE_SAILPLANE
|
|
return String(telemetry_ap_prefix) + default_callsign;
|
|
#else
|
|
return String(telemetry_ap_prefix) + "Winch";
|
|
#endif
|
|
}
|
|
|
|
String generated_access_point_name(const String &callsign)
|
|
{
|
|
#ifdef DEVICE_SAILPLANE
|
|
String safe_callsign = callsign;
|
|
if (safe_callsign.length() == 0)
|
|
safe_callsign = default_callsign;
|
|
return String(telemetry_ap_prefix) + safe_callsign;
|
|
#else
|
|
return String(telemetry_ap_prefix) + "Winch";
|
|
#endif
|
|
}
|
|
|
|
String url_encode(const String &value)
|
|
{
|
|
String encoded;
|
|
encoded.reserve(value.length() * 3);
|
|
for (unsigned int i = 0; i < value.length(); ++i)
|
|
{
|
|
const char c = value.charAt(i);
|
|
if (std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_' || c == '.' || c == '~')
|
|
encoded += c;
|
|
else
|
|
{
|
|
char buffer[4];
|
|
snprintf(buffer, sizeof(buffer), "%%%02X", static_cast<unsigned char>(c));
|
|
encoded += buffer;
|
|
}
|
|
}
|
|
return encoded;
|
|
}
|
|
|
|
WebServer server(80);
|
|
DNSServer dns_server;
|
|
Preferences preferences;
|
|
TelemetrySettings *active_settings = nullptr;
|
|
bool restart_after_update = false;
|
|
bool update_failed = false;
|
|
bool access_point_restart_pending = false;
|
|
unsigned long access_point_started_ms = 0UL;
|
|
|
|
String html_escape(const String &value)
|
|
{
|
|
String escaped = value;
|
|
escaped.replace("&", "&");
|
|
escaped.replace("<", "<");
|
|
escaped.replace(">", ">");
|
|
escaped.replace("\"", """);
|
|
escaped.replace("'", "'");
|
|
return escaped;
|
|
}
|
|
|
|
void redirect_to_root(const String &message, bool success)
|
|
{
|
|
String location = String("/?status=") + (success ? "success" : "error") + "&msg=" + url_encode(message);
|
|
server.sendHeader("Location", location, true);
|
|
server.send(303, "text/plain; charset=utf-8", "");
|
|
}
|
|
|
|
void send_page(const String &message = String())
|
|
{
|
|
String page;
|
|
page.reserve(2600);
|
|
page += F("<!doctype html><html lang='de'><head><meta charset='utf-8'>"
|
|
"<meta name='viewport' content='width=device-width,initial-scale=1'>"
|
|
"<title>Winden-Telemetry</title><style>"
|
|
"body{font-family:sans-serif;max-width:38rem;margin:2rem auto;padding:0 1rem}"
|
|
"label{display:block;margin-top:1rem}input{box-sizing:border-box;width:100%;padding:.6rem;font-size:1rem}"
|
|
"button{margin-top:1rem;padding:.65rem 1rem;font-size:1rem}"
|
|
".msg{padding:.6rem;background:#e5f4e8}.error{padding:.6rem;background:#fbe5e5}small{color:#555}"
|
|
"</style></head><body><h1>Winden-Telemetry</h1>");
|
|
if (message.length() > 0)
|
|
{
|
|
page += F("<p class='");
|
|
page += String(message.startsWith("Fehler") || message.startsWith("Ungueltig") || message.startsWith("Invalid") ? "error" : "msg");
|
|
page += F("'>");
|
|
page += html_escape(message);
|
|
page += F("</p>");
|
|
}
|
|
page += F("<p><small>Firmware: ");
|
|
page += html_escape(firmware_version_string());
|
|
page += F("</small></p><form method='post' action='/settings'>"
|
|
"<label>WLAN-Name<input name='ap_name' maxlength='32' required value='");
|
|
page += html_escape(active_settings->access_point_name);
|
|
page += F("'></label><label>WLAN-Passwort<input name='ap_password' type='password' minlength='8' maxlength='63' required value='");
|
|
page += html_escape(active_settings->access_point_password);
|
|
page += F("'></label><label>Callsign<input name='callsign' maxlength='16' required value='");
|
|
page += html_escape(active_settings->callsign);
|
|
page += F("'></label><label>Min Speed (km/h)<input name='min' type='number' min='0' max='300' required value='");
|
|
page += String(active_settings->min_speed);
|
|
page += F("'></label><label>Max Speed (km/h)<input name='max' type='number' min='1' max='300' required value='");
|
|
page += String(active_settings->max_speed);
|
|
page += F("'></label><label>Uebertragungszeit (Sekunden)<input name='active_time' type='number' min='1' max='600' required value='");
|
|
page += String(active_settings->active_time_seconds);
|
|
page += F("'></label><button type='submit'>Speichern</button></form>"
|
|
"<h2>Firmware aktualisieren</h2><form method='post' action='/update' enctype='multipart/form-data'>"
|
|
"<input name='firmware' type='file' accept='.bin' required><button type='submit'>Upload starten</button>"
|
|
"</form><p><small>Access Point: ");
|
|
page += html_escape(active_settings->access_point_name);
|
|
page += F("</small></p></body></html>");
|
|
server.send(200, "text/html; charset=utf-8", page);
|
|
}
|
|
|
|
bool parse_settings(TelemetrySettings &settings)
|
|
{
|
|
String callsign = server.arg("callsign");
|
|
String access_point_name = server.arg("ap_name");
|
|
String access_point_password = server.arg("ap_password");
|
|
int min_speed = server.arg("min").toInt();
|
|
int max_speed = server.arg("max").toInt();
|
|
int active_time_seconds = server.arg("active_time").toInt();
|
|
callsign.trim();
|
|
access_point_name.trim();
|
|
access_point_password.trim();
|
|
|
|
if (callsign.length() == 0 || callsign.length() > 16 || access_point_password.length() < 8 || access_point_password.length() > 63 || min_speed < 0 || max_speed > 300 ||
|
|
min_speed >= max_speed || active_time_seconds < 1 || active_time_seconds > 600)
|
|
return false;
|
|
|
|
settings.callsign = callsign;
|
|
settings.access_point_name = generated_access_point_name(callsign);
|
|
settings.access_point_password = access_point_password;
|
|
settings.min_speed = min_speed;
|
|
settings.max_speed = max_speed;
|
|
settings.active_time_seconds = active_time_seconds;
|
|
return true;
|
|
}
|
|
|
|
void handle_root()
|
|
{
|
|
String message = server.arg("msg");
|
|
String status = server.arg("status");
|
|
if (status == "error" && message.length() == 0)
|
|
message = "Ungueltige Einstellungen";
|
|
else if (status == "success" && message.length() == 0)
|
|
message = "Einstellungen gespeichert.";
|
|
send_page(message);
|
|
}
|
|
|
|
void handle_settings()
|
|
{
|
|
TelemetrySettings updated = *active_settings;
|
|
if (!parse_settings(updated))
|
|
{
|
|
redirect_to_root("Ungueltige Einstellungen", false);
|
|
return;
|
|
}
|
|
|
|
active_settings->callsign = updated.callsign;
|
|
active_settings->access_point_name = updated.access_point_name;
|
|
active_settings->access_point_password = updated.access_point_password;
|
|
active_settings->min_speed = updated.min_speed;
|
|
active_settings->max_speed = updated.max_speed;
|
|
active_settings->active_time_seconds = updated.active_time_seconds;
|
|
preferences.putString("callsign", active_settings->callsign);
|
|
preferences.putString("ap_name", active_settings->access_point_name);
|
|
preferences.putString("ap_password", active_settings->access_point_password);
|
|
preferences.putInt("min", active_settings->min_speed);
|
|
preferences.putInt("max", active_settings->max_speed);
|
|
preferences.putUInt("active_time", active_settings->active_time_seconds);
|
|
access_point_restart_pending = true;
|
|
redirect_to_root("Einstellungen gespeichert.", true);
|
|
}
|
|
|
|
void handle_update_upload()
|
|
{
|
|
HTTPUpload &upload = server.upload();
|
|
if (upload.status == UPLOAD_FILE_START)
|
|
{
|
|
restart_after_update = false;
|
|
update_failed = false;
|
|
Serial.printf("Firmware-Upload: %s\n", upload.filename.c_str());
|
|
if (!Update.begin(UPDATE_SIZE_UNKNOWN))
|
|
{
|
|
update_failed = true;
|
|
Update.printError(Serial);
|
|
}
|
|
}
|
|
else if (upload.status == UPLOAD_FILE_WRITE)
|
|
{
|
|
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
|
|
{
|
|
update_failed = true;
|
|
Update.printError(Serial);
|
|
}
|
|
}
|
|
else if (upload.status == UPLOAD_FILE_END)
|
|
{
|
|
if (Update.end(true))
|
|
{
|
|
restart_after_update = true;
|
|
Serial.printf("Firmware-Upload abgeschlossen: %u Bytes\n", upload.totalSize);
|
|
}
|
|
else
|
|
{
|
|
update_failed = true;
|
|
Update.printError(Serial);
|
|
}
|
|
}
|
|
}
|
|
|
|
void handle_update_result()
|
|
{
|
|
if (!update_failed && restart_after_update)
|
|
{
|
|
server.send(200, "text/plain; charset=utf-8", "Upload erfolgreich. Neustart...");
|
|
if (restart_after_update)
|
|
{
|
|
delay(500);
|
|
ESP.restart();
|
|
}
|
|
}
|
|
else
|
|
server.send(500, "text/plain; charset=utf-8", "Firmware-Upload fehlgeschlagen");
|
|
}
|
|
} // namespace
|
|
|
|
String firmware_version_string(void)
|
|
{
|
|
#ifdef FIRMWARE_VERSION
|
|
return String(FIRMWARE_VERSION);
|
|
#else
|
|
return "dev";
|
|
#endif
|
|
}
|
|
|
|
bool webinterface_begin(TelemetrySettings &settings)
|
|
{
|
|
active_settings = &settings;
|
|
preferences.begin(settings_namespace, false);
|
|
settings.callsign = preferences.getString("callsign", default_callsign);
|
|
settings.access_point_name = preferences.getString("ap_name", default_access_point_name());
|
|
settings.access_point_password = preferences.getString("ap_password", default_access_point_password);
|
|
settings.min_speed = preferences.getInt("min", default_min_speed);
|
|
settings.max_speed = preferences.getInt("max", default_max_speed);
|
|
settings.active_time_seconds = preferences.getUInt("active_time", default_active_time_seconds);
|
|
if (settings.access_point_name.length() == 0 || settings.access_point_name.length() > 32 || settings.access_point_password.length() < 8 || settings.access_point_password.length() > 63 || settings.callsign.length() == 0 || settings.min_speed < 0 || settings.min_speed >= settings.max_speed ||
|
|
settings.max_speed > 300 || settings.active_time_seconds < 1 || settings.active_time_seconds > 600)
|
|
{
|
|
settings.callsign = default_callsign;
|
|
settings.access_point_name = default_access_point_name();
|
|
settings.access_point_password = default_access_point_password;
|
|
settings.min_speed = default_min_speed;
|
|
settings.max_speed = default_max_speed;
|
|
settings.active_time_seconds = default_active_time_seconds;
|
|
}
|
|
else
|
|
{
|
|
settings.access_point_name = generated_access_point_name(settings.callsign);
|
|
}
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
bool ap_started = WiFi.softAP(settings.access_point_name, settings.access_point_password);
|
|
if (!ap_started)
|
|
return false;
|
|
|
|
access_point_started_ms = millis();
|
|
dns_server.start(53, "*", WiFi.softAPIP());
|
|
server.on("/", HTTP_GET, handle_root);
|
|
server.on("/settings", HTTP_POST, handle_settings);
|
|
server.on("/update", HTTP_POST, handle_update_result, handle_update_upload);
|
|
server.begin();
|
|
Serial.print("Firmware-Version: ");
|
|
Serial.println(firmware_version_string());
|
|
Serial.print("Webinterface: http://");
|
|
Serial.println(WiFi.softAPIP());
|
|
return true;
|
|
}
|
|
|
|
void webinterface_handle(void)
|
|
{
|
|
server.handleClient();
|
|
dns_server.processNextRequest();
|
|
if (access_point_restart_pending)
|
|
{
|
|
access_point_restart_pending = false;
|
|
WiFi.softAPdisconnect(true);
|
|
delay(100);
|
|
if (!WiFi.softAP(active_settings->access_point_name, active_settings->access_point_password))
|
|
Serial.println("Access Point konnte nicht neu gestartet werden");
|
|
access_point_started_ms = millis();
|
|
}
|
|
|
|
if (WiFi.softAPgetStationNum() == 0 && (millis() - access_point_started_ms) >= access_point_timeout_ms)
|
|
{
|
|
Serial.println("Access Point nach 5 Minuten ohne Client deaktiviert");
|
|
WiFi.softAPdisconnect(true);
|
|
}
|
|
}
|
|
|
|
const TelemetrySettings &webinterface_settings(void)
|
|
{
|
|
return *active_settings;
|
|
}
|