Files
roro9stack/lib/apps_model/src/settings_menu.cpp
T
twislaandClaude Opus 5.5 4404dd9380 VPN: a WireGuard tunnel (#8)
The device joins a WireGuard network over whatever Wi-Fi it is on: one
peer, IPv4. A client's .conf is imported from the card (/vpn/wg0.conf) and
kept in the device's settings, private key included, never shown; Settings
offers to delete the file. A switch brings the tunnel up until the next
restart, "Start with Wi-Fi" every time; it waits for the clock, which a
handshake needs. VPN shows in the Status Bar.

The protocol is esphome/wireguard 0.4.8. It calls lwIP without lwIP's lock,
which this framework checks: every call into it is made with the lock held.

What goes through the tunnel is everything (AllowedIPs 0.0.0.0/0) or the
one subnet the device's tunnel address is in: lwIP routes by an
interface's subnet or by default, nothing finer. The import says how many
ranges it can't reach.

Checked against a test peer in both directions and against a real server,
with a configuration uploaded from a phone (docs/milestones/N1.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
2026-10-08 01:49:47 +02:00

159 lines
6.9 KiB
C++

#include "settings_menu.h"
#include "choices.h"
namespace roro {
namespace {
using Row = SettingsMenu::Row;
using Kind = SettingsMenu::Kind;
struct RowDef {
Row row;
Kind kind;
const char* label;
};
const RowDef kRows[] = {
{Row::LongName, Kind::Text, "Long name"}, {Row::ShortName, Kind::Text, "Short name"},
{Row::Region, Kind::Choice, "Region"}, {Row::Timezone, Kind::Choice, "Timezone"},
{Row::Brightness, Kind::Slider, "Brightness"}, {Row::DimTimeout, Kind::Choice, "Dim after"},
{Row::OffTimeout, Kind::Choice, "Screen off after"}, {Row::Sound, Kind::Toggle, "Sound & LED"},
{Row::Gnss, Kind::Toggle, "GNSS"}, {Row::GnssQuiet, Kind::Toggle, "Pause GNSS for LoRa"},
{Row::Coordinates, Kind::Toggle, "Coordinates"},
{Row::ProbeMacs, Kind::Toggle, "Probe MACs"}, {Row::Wifi, Kind::Page, "Wi-Fi"},
{Row::CheckUpdates, Kind::Toggle, "Check for updates"}, {Row::Firmware, Kind::Page, "Firmware"},
{Row::Vpn, Kind::Page, "VPN"},
{Row::DebugConsole, Kind::Page, "Debug Console"}, {Row::About, Kind::Page, "About"},
};
const int kDimSeconds[] = {10, 15, 30, 60, 120, 300};
const int kOffSeconds[] = {30, 60, 120, 300, 600, 1800};
std::string formatSeconds(int s) { return s < 60 ? std::to_string(s) + " s" : std::to_string(s / 60) + " min"; }
template <size_t N>
std::vector<std::string> labels(const Choice (&table)[N]) {
std::vector<std::string> out;
for (auto& c : table) out.push_back(c.label);
return out;
}
template <size_t N>
std::vector<std::string> durations(const int (&seconds)[N]) {
std::vector<std::string> out;
for (int s : seconds) out.push_back(formatSeconds(s));
return out;
}
template <size_t N>
int indexOf(const Choice (&table)[N], const std::string& value) {
for (size_t i = 0; i < N; i++)
if (value == table[i].value) return static_cast<int>(i);
return -1;
}
template <size_t N>
int indexOf(const int (&table)[N], int value) {
for (size_t i = 0; i < N; i++)
if (table[i] == value) return static_cast<int>(i);
return -1;
}
} // namespace
int SettingsMenu::count() const { return sizeof(kRows) / sizeof(kRows[0]); }
SettingsMenu::Row SettingsMenu::row(int i) const { return kRows[i].row; }
SettingsMenu::Kind SettingsMenu::kind(int i) const { return kRows[i].kind; }
std::string SettingsMenu::label(int i) const { return kRows[i].label; }
std::string SettingsMenu::value(int i) const {
switch (row(i)) {
case Row::LongName: return settings_.getString(Setting::LongName);
case Row::ShortName: return settings_.getString(Setting::ShortName);
case Row::Region: return settings_.getString(Setting::Region);
case Row::Timezone: {
int c = currentChoice(i);
return c >= 0 ? kTimezones[c].label : "Custom";
}
case Row::Brightness: return std::to_string(settings_.getInt(Setting::Brightness)) + "%";
case Row::DimTimeout: return formatSeconds(settings_.getInt(Setting::DimTimeoutS));
case Row::OffTimeout: return formatSeconds(settings_.getInt(Setting::OffTimeoutS));
case Row::Sound: return settings_.getBool(Setting::Sound) ? "On" : "Off";
case Row::Gnss: return settings_.getBool(Setting::GnssEnabled) ? "On" : "Off";
case Row::GnssQuiet: return settings_.getBool(Setting::GnssQuietForLora) ? "On" : "Off";
case Row::CheckUpdates: return settings_.getBool(Setting::CheckUpdates) ? "Daily" : "Off";
case Row::Coordinates: return settings_.getBool(Setting::CoordinatesDms) ? "Deg min sec" : "Decimal";
case Row::ProbeMacs: return settings_.getBool(Setting::ProbeMacRaw) ? "Raw" : "Pseudonymised";
case Row::Wifi: return settings_.getBool(Setting::WifiEnabled) ? "On" : "Off";
case Row::Vpn: return settings_.getString(Setting::VpnConfig).empty() ? "Not set" : settings_.getBool(Setting::VpnAuto) ? "With Wi-Fi" : "By hand";
case Row::DebugConsole: return settings_.getBool(Setting::DebugConsole) ? "On" : "Off";
default: return "";
}
}
std::vector<std::string> SettingsMenu::choices(int i) const {
switch (row(i)) {
case Row::Region: return labels(kRegions);
case Row::Timezone: return labels(kTimezones);
case Row::DimTimeout: return durations(kDimSeconds);
case Row::OffTimeout: return durations(kOffSeconds);
default: return {};
}
}
int SettingsMenu::currentChoice(int i) const {
switch (row(i)) {
case Row::Region: return indexOf(kRegions, settings_.getString(Setting::Region));
case Row::Timezone: return indexOf(kTimezones, settings_.getString(Setting::Timezone));
case Row::DimTimeout: return indexOf(kDimSeconds, settings_.getInt(Setting::DimTimeoutS));
case Row::OffTimeout: return indexOf(kOffSeconds, settings_.getInt(Setting::OffTimeoutS));
default: return -1;
}
}
std::string SettingsMenu::choose(int i, int c) {
switch (row(i)) {
case Row::Region:
settings_.setString(Setting::Region, kRegions[c].value);
settings_.setBool(Setting::RegionConfirmed, true);
return "";
case Row::Timezone: settings_.setString(Setting::Timezone, kTimezones[c].value); return "";
case Row::DimTimeout:
return settings_.setInt(Setting::DimTimeoutS, kDimSeconds[c]) ? "" : "Dimming must happen before screen off";
case Row::OffTimeout:
return settings_.setInt(Setting::OffTimeoutS, kOffSeconds[c]) ? "" : "Screen off must come after dimming";
default: return "Not a choice";
}
}
void SettingsMenu::toggle(int i) {
if (row(i) == Row::Sound) settings_.setBool(Setting::Sound, !settings_.getBool(Setting::Sound));
if (row(i) == Row::Gnss) settings_.setBool(Setting::GnssEnabled, !settings_.getBool(Setting::GnssEnabled));
if (row(i) == Row::GnssQuiet) settings_.setBool(Setting::GnssQuietForLora, !settings_.getBool(Setting::GnssQuietForLora));
if (row(i) == Row::CheckUpdates) settings_.setBool(Setting::CheckUpdates, !settings_.getBool(Setting::CheckUpdates));
if (row(i) == Row::Coordinates) settings_.setBool(Setting::CoordinatesDms, !settings_.getBool(Setting::CoordinatesDms));
if (row(i) == Row::ProbeMacs) settings_.setBool(Setting::ProbeMacRaw, !settings_.getBool(Setting::ProbeMacRaw));
}
void SettingsMenu::adjust(int i, int direction) {
if (row(i) != Row::Brightness) return;
int next = settings_.getInt(Setting::Brightness) + (direction > 0 ? 10 : -10);
if (next < 10) next = 10;
if (next > 100) next = 100;
settings_.setInt(Setting::Brightness, next);
}
int SettingsMenu::maxBytes(int i) const {
return row(i) == Row::ShortName ? 4 : row(i) == Row::LongName ? 39 : 0;
}
std::string SettingsMenu::setText(int i, const std::string& text) {
Setting s = row(i) == Row::ShortName ? Setting::ShortName : Setting::LongName;
if (settings_.setString(s, text)) return "";
return "Must be 1 to " + std::to_string(maxBytes(i)) + " bytes";
}
} // namespace roro