2018-02-26 00:11:31 +00:00
|
|
|
#ifndef APSettingsConfig_h
|
|
|
|
#define APSettingsConfig_h
|
|
|
|
|
2021-01-03 17:00:36 +00:00
|
|
|
#include <SettingValue.h>
|
2020-05-14 22:23:45 +00:00
|
|
|
#include <HttpEndpoint.h>
|
|
|
|
#include <FSPersistence.h>
|
2020-07-07 21:22:38 +00:00
|
|
|
#include <JsonUtils.h>
|
2020-05-14 22:23:45 +00:00
|
|
|
|
2018-03-06 22:39:33 +00:00
|
|
|
#include <DNSServer.h>
|
2018-02-26 00:11:31 +00:00
|
|
|
#include <IPAddress.h>
|
|
|
|
|
2020-07-07 21:22:38 +00:00
|
|
|
#ifndef FACTORY_AP_PROVISION_MODE
|
|
|
|
#define FACTORY_AP_PROVISION_MODE AP_MODE_DISCONNECTED
|
|
|
|
#endif
|
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
#ifndef FACTORY_AP_SSID
|
2021-01-03 17:00:36 +00:00
|
|
|
#define FACTORY_AP_SSID "ESP8266-React-#{unique_id}"
|
2020-05-19 23:32:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_AP_PASSWORD
|
|
|
|
#define FACTORY_AP_PASSWORD "esp-react"
|
|
|
|
#endif
|
|
|
|
|
2020-07-07 21:22:38 +00:00
|
|
|
#ifndef FACTORY_AP_LOCAL_IP
|
|
|
|
#define FACTORY_AP_LOCAL_IP "192.168.4.1"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_AP_GATEWAY_IP
|
|
|
|
#define FACTORY_AP_GATEWAY_IP "192.168.4.1"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_AP_SUBNET_MASK
|
|
|
|
#define FACTORY_AP_SUBNET_MASK "255.255.255.0"
|
2020-05-19 23:32:49 +00:00
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2021-01-03 18:51:11 +00:00
|
|
|
#ifndef FACTORY_AP_CHANNEL
|
|
|
|
#define FACTORY_AP_CHANNEL 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_AP_SSID_HIDDEN
|
|
|
|
#define FACTORY_AP_SSID_HIDDEN false
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_AP_MAX_CLIENTS
|
|
|
|
#define FACTORY_AP_MAX_CLIENTS 4
|
|
|
|
#endif
|
|
|
|
|
2018-02-26 00:11:31 +00:00
|
|
|
#define AP_SETTINGS_FILE "/config/apSettings.json"
|
2018-04-01 09:35:23 +00:00
|
|
|
#define AP_SETTINGS_SERVICE_PATH "/rest/apSettings"
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2021-01-03 17:00:36 +00:00
|
|
|
#define AP_MODE_ALWAYS 0
|
|
|
|
#define AP_MODE_DISCONNECTED 1
|
|
|
|
#define AP_MODE_NEVER 2
|
|
|
|
|
2021-01-03 18:51:11 +00:00
|
|
|
#define MANAGE_NETWORK_DELAY 10000
|
2021-01-03 17:00:36 +00:00
|
|
|
#define DNS_PORT 53
|
|
|
|
|
2020-07-07 21:22:38 +00:00
|
|
|
enum APNetworkStatus { ACTIVE = 0, INACTIVE, LINGERING };
|
2020-05-30 08:47:24 +00:00
|
|
|
|
2020-02-01 08:44:26 +00:00
|
|
|
class APSettings {
|
|
|
|
public:
|
|
|
|
uint8_t provisionMode;
|
|
|
|
String ssid;
|
|
|
|
String password;
|
2021-01-03 18:51:11 +00:00
|
|
|
uint8_t channel;
|
|
|
|
bool ssidHidden;
|
|
|
|
uint8_t maxClients;
|
|
|
|
|
2020-07-07 21:22:38 +00:00
|
|
|
IPAddress localIP;
|
|
|
|
IPAddress gatewayIP;
|
|
|
|
IPAddress subnetMask;
|
|
|
|
|
|
|
|
bool operator==(const APSettings& settings) const {
|
|
|
|
return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password &&
|
2021-01-03 18:51:11 +00:00
|
|
|
channel == settings.channel && ssidHidden == settings.ssidHidden && maxClients == settings.maxClients &&
|
2020-07-07 21:22:38 +00:00
|
|
|
localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
|
|
|
|
}
|
2020-05-14 22:23:45 +00:00
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static void read(APSettings& settings, JsonObject& root) {
|
2020-05-14 22:23:45 +00:00
|
|
|
root["provision_mode"] = settings.provisionMode;
|
|
|
|
root["ssid"] = settings.ssid;
|
|
|
|
root["password"] = settings.password;
|
2021-01-03 18:51:11 +00:00
|
|
|
root["channel"] = settings.channel;
|
|
|
|
root["ssid_hidden"] = settings.ssidHidden;
|
|
|
|
root["max_clients"] = settings.maxClients;
|
2020-07-07 21:22:38 +00:00
|
|
|
root["local_ip"] = settings.localIP.toString();
|
|
|
|
root["gateway_ip"] = settings.gatewayIP.toString();
|
|
|
|
root["subnet_mask"] = settings.subnetMask.toString();
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static StateUpdateResult update(JsonObject& root, APSettings& settings) {
|
2020-05-30 08:47:24 +00:00
|
|
|
APSettings newSettings = {};
|
|
|
|
newSettings.provisionMode = root["provision_mode"] | FACTORY_AP_PROVISION_MODE;
|
2020-05-14 22:23:45 +00:00
|
|
|
switch (settings.provisionMode) {
|
|
|
|
case AP_MODE_ALWAYS:
|
|
|
|
case AP_MODE_DISCONNECTED:
|
|
|
|
case AP_MODE_NEVER:
|
|
|
|
break;
|
|
|
|
default:
|
2020-05-30 08:47:24 +00:00
|
|
|
newSettings.provisionMode = AP_MODE_ALWAYS;
|
|
|
|
}
|
2021-01-03 17:00:36 +00:00
|
|
|
newSettings.ssid = root["ssid"] | SettingValue::format(FACTORY_AP_SSID);
|
2020-05-30 08:47:24 +00:00
|
|
|
newSettings.password = root["password"] | FACTORY_AP_PASSWORD;
|
2021-01-03 18:51:11 +00:00
|
|
|
newSettings.channel = root["channel"] | FACTORY_AP_CHANNEL;
|
|
|
|
newSettings.ssidHidden = root["ssid_hidden"] | FACTORY_AP_SSID_HIDDEN;
|
|
|
|
newSettings.maxClients = root["max_clients"] | FACTORY_AP_MAX_CLIENTS;
|
2020-07-07 21:22:38 +00:00
|
|
|
|
|
|
|
JsonUtils::readIP(root, "local_ip", newSettings.localIP, FACTORY_AP_LOCAL_IP);
|
|
|
|
JsonUtils::readIP(root, "gateway_ip", newSettings.gatewayIP, FACTORY_AP_GATEWAY_IP);
|
|
|
|
JsonUtils::readIP(root, "subnet_mask", newSettings.subnetMask, FACTORY_AP_SUBNET_MASK);
|
|
|
|
|
|
|
|
if (newSettings == settings) {
|
2020-05-30 08:47:24 +00:00
|
|
|
return StateUpdateResult::UNCHANGED;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
2020-05-30 08:47:24 +00:00
|
|
|
settings = newSettings;
|
2020-05-29 19:18:43 +00:00
|
|
|
return StateUpdateResult::CHANGED;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
2020-02-01 08:44:26 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
class APSettingsService : public StatefulService<APSettings> {
|
2019-12-03 23:16:06 +00:00
|
|
|
public:
|
|
|
|
APSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
|
|
|
|
|
|
|
void begin();
|
|
|
|
void loop();
|
2020-05-30 08:47:24 +00:00
|
|
|
APNetworkStatus getAPNetworkStatus();
|
2019-12-03 23:16:06 +00:00
|
|
|
|
|
|
|
private:
|
2020-05-14 22:23:45 +00:00
|
|
|
HttpEndpoint<APSettings> _httpEndpoint;
|
|
|
|
FSPersistence<APSettings> _fsPersistence;
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
// for the captive portal
|
|
|
|
DNSServer* _dnsServer;
|
|
|
|
|
2020-05-21 22:41:29 +00:00
|
|
|
// for the mangement delay loop
|
2020-05-30 08:47:24 +00:00
|
|
|
volatile unsigned long _lastManaged;
|
|
|
|
volatile boolean _reconfigureAp;
|
2020-05-21 22:41:29 +00:00
|
|
|
|
2020-02-01 08:44:26 +00:00
|
|
|
void reconfigureAP();
|
2019-12-03 23:16:06 +00:00
|
|
|
void manageAP();
|
|
|
|
void startAP();
|
|
|
|
void stopAP();
|
|
|
|
void handleDNS();
|
2018-02-26 00:11:31 +00:00
|
|
|
};
|
|
|
|
|
2020-06-09 20:57:44 +00:00
|
|
|
#endif // end APSettingsConfig_h
|