// SPDX-License-Identifier: GPL-3.0-only #include "net_wifi.h" #include #include #include "config.h" namespace { bool apMode = false; uint32_t lastReconnect = 0; void startMdns() { // openscribe.local -> device; advertise the HTTP API service. if (MDNS.begin("openscribe")) { MDNS.addService("http", "tcp", 80); } } } // namespace namespace net_wifi { bool begin() { const String ssid = config::wifiSsid(); if (!ssid.isEmpty()) { WiFi.mode(WIFI_STA); WiFi.setSleep(false); WiFi.begin(ssid.c_str(), config::wifiPsk().c_str()); const uint32_t t0 = millis(); while (WiFi.status() != WL_CONNECTED && (millis() - t0) < 15000) { delay(200); } } if (WiFi.status() != WL_CONNECTED) { // Provisioning fallback: open a SoftAP so a client can reach the API and set WiFi. apMode = true; WiFi.mode(WIFI_AP); String ap = "OpenScribe-" + config::deviceId().substring(11); // MAC suffix // WPA2 needs an 8+ char passphrase; note in TODO to make this user-set (M4). WiFi.softAP(ap.c_str(), "openscribe"); } startMdns(); return true; } void loop() { if (apMode) return; if (WiFi.status() != WL_CONNECTED && (millis() - lastReconnect) > 10000) { lastReconnect = millis(); WiFi.reconnect(); } } bool connected() { return WiFi.status() == WL_CONNECTED; } bool isAccessPoint() { return apMode; } String ip() { return apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString(); } } // namespace net_wifi