autoconnect + preferences

This commit is contained in:
Kyle Isom 2023-10-06 23:12:31 -07:00
parent fbeb54a830
commit 802a6f6f52
5 changed files with 143 additions and 21 deletions

View File

@ -9,6 +9,7 @@
bool SetupWiFi(); bool SetupWiFi();
bool Autoconnect(Dictionary &pb); bool Autoconnect(Dictionary &pb);
bool Autoconnect(Dictionary &pb, bool reset); bool Autoconnect(Dictionary &pb, bool reset);
bool TryConnect(Dictionary &pb);
#endif #endif

22
include/prefs.h Normal file
View File

@ -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

View File

@ -19,27 +19,13 @@ SetupWiFi()
static bool 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; size_t waitedFor = 0;
if (ssidLen == 0) { Serial.print("CONNECT ");
return false;
}
Serial.print("MODEM: CHECK ");
Serial.println(ssid); Serial.println(ssid);
WiFi.begin(ssid, wpakey);
if (!pb.Lookup(ssid, uint8_t(ssidLen), password)) {
Serial.println("SSID NOT IN PHONEBOOK");
return false;
}
Serial.println("CONNECTING");
WiFi.begin(ssid, password.Val);
while ((WiFi.status() != WL_CONNECTED) && (waitedFor < MAX_WAIT)) { while ((WiFi.status() != WL_CONNECTED) && (waitedFor < MAX_WAIT)) {
waitedFor += CONN_WAIT; waitedFor += CONN_WAIT;
if ((waitedFor % 1000) == 0) { if ((waitedFor % 1000) == 0) {
@ -58,10 +44,56 @@ tryConnect(Dictionary &pb, int network)
return false; 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 bool
Autoconnect(Dictionary &pb) Autoconnect(Dictionary &pb)
{ {
Autoconnect(pb, true); return Autoconnect(pb, true);
} }

View File

@ -4,6 +4,7 @@
#include "Arena.h" #include "Arena.h"
#include "Dictionary.h" #include "Dictionary.h"
#include "WiFiMgr.h" #include "WiFiMgr.h"
#include "prefs.h"
#include "homenet.h" #include "homenet.h"
@ -64,10 +65,12 @@ void setup() {
} }
setupPhonebook(); setupPhonebook();
while (!Autoconnect(phonebook)) { if (config::StartTryConnecting()) {
Serial.println("STANDBY"); if (!TryConnect(phonebook)) {
delay(1000); Autoconnect(phonebook);
} }
}
Serial.println("MODEM READY"); Serial.println("MODEM READY");
} }

64
src/prefs.cc Normal file
View File

@ -0,0 +1,64 @@
#include <Preferences.h>
#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();
}
}