autoconnect + preferences
This commit is contained in:
		
							parent
							
								
									fbeb54a830
								
							
						
					
					
						commit
						802a6f6f52
					
				|  | @ -9,6 +9,7 @@ | |||
| bool	SetupWiFi(); | ||||
| bool	Autoconnect(Dictionary &pb); | ||||
| bool	Autoconnect(Dictionary &pb, bool reset); | ||||
| bool	TryConnect(Dictionary &pb); | ||||
| 
 | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
|  | @ -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 | ||||
|  | @ -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); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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"); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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(); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
		Loading…
	
		Reference in New Issue