Skip to content

Commit

Permalink
Added cors and ap scan flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mp-se committed Aug 21, 2024
1 parent 6a62f37 commit ead0c4e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 8 deletions.
1 change: 0 additions & 1 deletion example/src/demo-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ void setup() {
myWifi.startAP();
} else {
PERF_BEGIN("wifi-connect");
myWifi.enableStrongestAP(); // Will do a scan and choose the AP with best RSSI
myWifi.connect(WIFI_AP_STA);
myWifi.setAP("extra", "password"); // Will create an AP as well as
// connecting to the defined wifi
Expand Down
7 changes: 7 additions & 0 deletions src/baseconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void BaseConfig::createJsonWifi(JsonObject& doc) {
doc[PARAM_DIRECT_PASS] = getWifiDirectPass();
doc[PARAM_WIFI_PORTAL_TIMEOUT] = getWifiPortalTimeout();
doc[PARAM_WIFI_CONNECT_TIMEOUT] = getWifiConnectionTimeout();
doc[PARAM_SCAN_AP] = getWifiScanAP();
}

void BaseConfig::parseJsonWifi(JsonObject& doc) {
Expand All @@ -81,6 +82,8 @@ void BaseConfig::parseJsonWifi(JsonObject& doc) {
this->setWifiPortalTimeout(doc[PARAM_WIFI_PORTAL_TIMEOUT].as<int>());
if (!doc[PARAM_WIFI_CONNECT_TIMEOUT].isNull())
this->setWifiConnectionTimeout(doc[PARAM_WIFI_CONNECT_TIMEOUT].as<int>());
if (!doc[PARAM_SCAN_AP].isNull())
setWifiScanAP(doc[PARAM_SCAN_AP].as<bool>());

_saveNeeded = true;
}
Expand Down Expand Up @@ -181,6 +184,7 @@ void BaseConfig::createJsonBase(JsonObject& doc) {
doc[PARAM_TEMP_FORMAT] = String(getTempFormat());
doc[PARAM_TEMP_UNIT] = String(getTempFormat());
doc[PARAM_DARK_MODE] = getDarkMode();
doc[PARAM_CORS_ALLOWED] = getCorsAllowed();
}

void BaseConfig::parseJsonBase(JsonObject& doc) {
Expand All @@ -202,6 +206,9 @@ void BaseConfig::parseJsonBase(JsonObject& doc) {
if (!doc[PARAM_DARK_MODE].isNull())
setDarkMode(doc[PARAM_DARK_MODE].as<bool>());

if (!doc[PARAM_CORS_ALLOWED].isNull())
setCorsAllowed(doc[PARAM_CORS_ALLOWED].as<bool>());

_saveNeeded = true;
}

Expand Down
21 changes: 21 additions & 0 deletions src/baseconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BaseConfig : public WifiConfig,
String _wifiDirectPASS = "";
int _wifiConnectionTimeout = 30;
int _wifiPortalTimeout = 120;
bool _wifiScanAP = false;

// OtaConfig
String _otaURL;
Expand Down Expand Up @@ -71,6 +72,13 @@ class BaseConfig : public WifiConfig,
int _dynamicJsonSize;
bool _darkMode = false;

// WebServer
#if defined(ENABLE_REMOTE_UI_DEVELOPMENT)
bool _corsAllowed = true;
#else
bool _corsAllowed = false;
#endif

void formatFileSystem();

// Internal
Expand All @@ -90,6 +98,14 @@ class BaseConfig : public WifiConfig,
public:
BaseConfig(String baseMDNS, String fileName, int dynamicJsonSize);

// WebServer
bool getCorsAllowed() { return _corsAllowed; }
void setCorsAllowed(bool b) {
_corsAllowed = b;
_saveNeeded = true;
}
bool isCorsAllowed() { return getCorsAllowed(); }

// WifiConfig
const char* getMDNS() { return _mDNS.c_str(); }
void setMDNS(String s) {
Expand Down Expand Up @@ -141,6 +157,11 @@ class BaseConfig : public WifiConfig,
_wifiPortalTimeout = t;
_saveNeeded = true;
}
bool getWifiScanAP() { return _wifiScanAP; }
void setWifiScanAP(bool t) {
_wifiScanAP = t;
_saveNeeded = true;
}

int getPushTimeout() { return _pushTimeout; }
void setPushTimeout(int t) { _pushTimeout = t; }
Expand Down
8 changes: 4 additions & 4 deletions src/basewebserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ void BaseWebServer::webHandlePageNotFound(AsyncWebServerRequest *request) {
request->url().c_str());

request->redirect("/");
// request->send(404, "application/json", "{\"message\":\"URL not found\"}");
}

void BaseWebServer::webHandleAuth(AsyncWebServerRequest *request) {
Expand Down Expand Up @@ -527,9 +526,10 @@ bool BaseWebServer::setupWebServer() {
#endif

setupWebHandlers();
#if defined(ENABLE_REMOTE_UI_DEVELOPMENT)
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
#endif
if (_webConfig->isCorsAllowed()) {
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
}

_server->begin();
Log.notice(F("WEB : Web server started." CR));
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/espframework.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ constexpr auto JSON_BUFFER_SIZE_XL = 5000;
// Config
constexpr auto PARAM_ID = "id";
constexpr auto PARAM_MDNS = "mdns";
constexpr auto PARAM_CORS_ALLOWED = "cors_allowed";
constexpr auto PARAM_SSID = "wifi_ssid";
constexpr auto PARAM_PASS = "wifi_pass";
constexpr auto PARAM_SSID2 = "wifi_ssid2";
constexpr auto PARAM_PASS2 = "wifi_pass2";
constexpr auto PARAM_DIRECT_SSID = "wifi_direct_ssid";
constexpr auto PARAM_DIRECT_PASS = "wifi_direct_pass";
constexpr auto PARAM_SCAN_AP = "wifi_scan_ap";
constexpr auto PARAM_OTA_URL = "ota_url";
constexpr auto PARAM_TEMP_FORMAT = "temp_format"; // Alias is temp_unit
constexpr auto PARAM_TEMP_UNIT = "temp_unit";
Expand Down
5 changes: 5 additions & 0 deletions src/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class WebConfig {
virtual const char* getID();
virtual const char* getMDNS();
virtual int getWifiPortalTimeout();

virtual bool isCorsAllowed();
};

class WifiConfig {
Expand All @@ -53,6 +55,9 @@ class WifiConfig {
virtual int getWifiPortalTimeout();
virtual void setWifiPortalTimeout(int t);

virtual bool getWifiScanAP();
virtual void setWifiScanAP(bool b);

virtual const char* getMDNS();
virtual void setMDNS(String s);
virtual const char* getWifiSSID(int idx);
Expand Down
2 changes: 1 addition & 1 deletion src/wificonnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void WifiConnection::connectAsync(String ssid, String pass, wifi_mode_t mode) {
Log.notice(F("WIFI: Connecting to wifi using stored settings %s." CR),
ssid);

if (_enableStrongestAP) {
if (_wifiConfig->getWifiScanAP()) {
const uint8_t *bssid = findStrongestAP(ssid);
WiFi.begin(ssid, pass, 0, bssid);
// WiFi.begin(ssid, pass, findStrongestChannel(ssid));
Expand Down
2 changes: 0 additions & 2 deletions src/wificonnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class WifiConnection {
String _userPWD;
WifiConfig* _wifiConfig;
DNSServer* _dnsServer = NULL;
bool _enableStrongestAP = false;

// Double reset
uint32_t _timer = 0;
Expand All @@ -70,7 +69,6 @@ class WifiConnection {
void init();
void timeSync(String timeZone = "");

void enableStrongestAP() { _enableStrongestAP = true; }
bool connect(bool wifiDirect, wifi_mode_t mode = WIFI_STA);
bool connect(wifi_mode_t mode = WIFI_STA) { return connect(false, mode); }
bool disconnect();
Expand Down

0 comments on commit ead0c4e

Please sign in to comment.