From 802a6f6f5299e703a076efa35fbc32c22a527825 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Fri, 6 Oct 2023 23:12:31 -0700 Subject: [PATCH] autoconnect + preferences --- include/WiFiMgr.h | 1 + include/prefs.h | 22 +++++++++++++++ src/WiFiMgr.cc | 68 ++++++++++++++++++++++++++++++++++------------- src/main.cc | 9 ++++--- src/prefs.cc | 64 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 21 deletions(-) create mode 100644 include/prefs.h create mode 100644 src/prefs.cc diff --git a/include/WiFiMgr.h b/include/WiFiMgr.h index b2138c5..6b7289f 100644 --- a/include/WiFiMgr.h +++ b/include/WiFiMgr.h @@ -9,6 +9,7 @@ bool SetupWiFi(); bool Autoconnect(Dictionary &pb); bool Autoconnect(Dictionary &pb, bool reset); +bool TryConnect(Dictionary &pb); #endif diff --git a/include/prefs.h b/include/prefs.h new file mode 100644 index 0000000..2fca6f5 --- /dev/null +++ b/include/prefs.h @@ -0,0 +1,22 @@ +#ifndef KIMODEM_PREFS_H +#define KIMODEM_PREFS_H + + +namespace config { + +// Networking + +size_t LastSSID(char *ssid); +void SetLastSSID(const char *ssid, size_t ssidlen); + +bool StartTryConnecting(); +void SetStartTryConnecting(bool opt); + + +// Misc. +void FactoryDefaults(); + +} + + +#endif diff --git a/src/WiFiMgr.cc b/src/WiFiMgr.cc index 2399b45..cfa542a 100644 --- a/src/WiFiMgr.cc +++ b/src/WiFiMgr.cc @@ -19,27 +19,13 @@ SetupWiFi() static bool -tryConnect(Dictionary &pb, int network) +tryConnect(const char *ssid, const char *wpakey) { - const char *ssid = WiFi.SSID(network).c_str(); - size_t ssidLen = strnlen(ssid, TLV_MAX_LEN); - TLV::Record password; size_t waitedFor = 0; - if (ssidLen == 0) { - return false; - } - - Serial.print("MODEM: CHECK "); + Serial.print("CONNECT "); Serial.println(ssid); - - if (!pb.Lookup(ssid, uint8_t(ssidLen), password)) { - Serial.println("SSID NOT IN PHONEBOOK"); - return false; - } - - Serial.println("CONNECTING"); - WiFi.begin(ssid, password.Val); + WiFi.begin(ssid, wpakey); while ((WiFi.status() != WL_CONNECTED) && (waitedFor < MAX_WAIT)) { waitedFor += CONN_WAIT; if ((waitedFor % 1000) == 0) { @@ -58,10 +44,56 @@ tryConnect(Dictionary &pb, int network) return false; } + +bool +TryConnect(Dictionary &pb) +{ + TLV::Record ssid; + TLV::Record wpakey; + uint8_t *cursor; + + while ((cursor != NULL) && (cursor[0] != TAG_EMPTY)) { + TLV::ReadFromMemory(ssid, cursor); + TLV::SkipRecord(ssid, cursor); + + TLV::ReadFromMemory(wpakey, cursor); + TLV::SkipRecord(ssid, cursor); + + if (tryConnect(ssid.Val, wpakey.Val)) { + return true; + } + } + + return false; +} + + +static bool +tryConnect(Dictionary &pb, int network) +{ + const char *ssid = WiFi.SSID(network).c_str(); + size_t ssidLen = strnlen(ssid, TLV_MAX_LEN); + TLV::Record password; + + if (ssidLen == 0) { + return false; + } + + Serial.print("MODEM: CHECK "); + Serial.println(ssid); + + if (!pb.Lookup(ssid, uint8_t(ssidLen), password)) { + Serial.println("SSID NOT IN PHONEBOOK"); + return false; + } + + return tryConnect(ssid, password.Val); +} + bool Autoconnect(Dictionary &pb) { - Autoconnect(pb, true); + return Autoconnect(pb, true); } diff --git a/src/main.cc b/src/main.cc index 85a8131..e76e0ec 100644 --- a/src/main.cc +++ b/src/main.cc @@ -4,6 +4,7 @@ #include "Arena.h" #include "Dictionary.h" #include "WiFiMgr.h" +#include "prefs.h" #include "homenet.h" @@ -64,11 +65,13 @@ void setup() { } setupPhonebook(); - while (!Autoconnect(phonebook)) { - Serial.println("STANDBY"); - delay(1000); + if (config::StartTryConnecting()) { + if (!TryConnect(phonebook)) { + Autoconnect(phonebook); + } } + Serial.println("MODEM READY"); } diff --git a/src/prefs.cc b/src/prefs.cc new file mode 100644 index 0000000..df29094 --- /dev/null +++ b/src/prefs.cc @@ -0,0 +1,64 @@ +#include +#include "prefs.h" + + +namespace config { + + +static bool isLoaded = false; +static Preferences preferences; + + +static void +checkReady() +{ + if (isLoaded) { + return; + } + + preferences.begin("kimodem", false); + isLoaded = true; +} + + +size_t +LastSSID(char *ssid, size_t ssidlen) +{ + checkReady(); + return preferences.getString("SSID", ssid, ssidlen); +} + + +void +SetLastSSID(const char *ssid) +{ + checkReady(); + preferences.putString("SSID", ssid); +} + + +bool +StartTryConnecting() +{ + checkReady(); + return preferences.getBool("STC", true); +} + + +void +SetStartTryConnecting(bool opt) +{ + checkReady(); + preferences.putBool("STC", opt); +} + + +void +FactoryDefaults() +{ + preferences.clear(); +} + + + +}