Skip to content

Commit

Permalink
wifi_common optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolven committed Nov 16, 2024
1 parent 883486d commit 1a1d43a
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 64 deletions.
8 changes: 4 additions & 4 deletions src/core/menu_items/WifiMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
void WifiMenu::optionsMenu() {
if(!wifiConnected) {
options = {
{"Connect Wifi", [=]() { wifiConnectMenu(); }}, //wifi_common.h
{"WiFi AP", [=]() { wifiConnectMenu(true); displayInfo("pwd: " + bruceConfig.wifiAp.pwd, true); }},//wifi_common.h
{"Connect Wifi", [=]() { wifiConnectMenu(WIFI_STA); }},
{"WiFi AP", [=]() { wifiConnectMenu(WIFI_AP); displayInfo("pwd: " + bruceConfig.wifiAp.pwd, true); }},
};
} else {
options = {
{"Disconnect", [=]() { wifiDisconnect(); }}, //wifi_common.h
{"AP info", [=]() { displayAPInfo(); }},
{"Disconnect", [=]() { wifiDisconnect(); }},
{"AP info", [=]() { displayAPInfo(); }},
};
}
options.push_back({"Wifi Atks", [=]() { wifi_atk_menu(); }});
Expand Down
2 changes: 1 addition & 1 deletion src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ bool processSerialCommand(String cmd_str) {
// start the webui
if(!wifiConnected) {
Serial.println("wifiConnect");
wifiApConnect(); // TODO: read mode from config file
wifiConnectMenu(WIFI_AP); // TODO: read mode from config file
}
Serial.println("startWebUi");
startWebUi(true); // MEMO: will quit when checkEscPress
Expand Down
17 changes: 17 additions & 0 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "utils.h"
#include "globals.h"

void updateClockTimezone(){
timeClient.begin();
timeClient.update();

timeClient.setTimeOffset(bruceConfig.tmz * 3600);

localTime = myTZ.toLocal(timeClient.getEpochTime());

#if !defined(HAS_RTC)
rtc.setTime(timeClient.getEpochTime());
updateTimeStr(rtc.getTimeStruct());
clock_set = true;
#endif
}
1 change: 1 addition & 0 deletions src/core/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void updateClockTimezone();
75 changes: 28 additions & 47 deletions src/core/wifi_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
#include "mykeyboard.h" // usinf keyboard when calling rename
#include "display.h" // using displayRedStripe and loop options
#include "settings.h"
#include "eeprom.h"
#include "powerSave.h"
#include "utils.h"

/***************************************************************************************
** Function name: wifiConnect
** Description: Connects to wifiNetwork
***************************************************************************************/
bool wifiConnect(String ssid, int encryption, bool isAP)
bool _wifiConnect(const String& ssid, int encryption)
{
if (isAP) return wifiApConnect();

String password = bruceConfig.getWifiPassword(ssid);
if (password == "" && encryption > 0) {
password = keyboard(password, 63, "Network Password:");
Expand Down Expand Up @@ -44,14 +38,14 @@ bool wifiConnect(String ssid, int encryption, bool isAP)
wifiConnected = true;
wifiIP = WiFi.localIP().toString();
bruceConfig.addWifiCredential(ssid, password);
_updateClockTimezone();
updateClockTimezone();
}

delay(200);
return connected;
}

bool _connectToWifiNetwork(String ssid, String pwd) {
bool _connectToWifiNetwork(const String& ssid, const String& pwd) {
drawMainBorderWithTitle("WiFi Connect");
padprintln("");
padprint("Connecting to: " + ssid + ".");
Expand Down Expand Up @@ -79,29 +73,9 @@ bool _connectToWifiNetwork(String ssid, String pwd) {
return WiFi.status() == WL_CONNECTED;
}

void _updateClockTimezone() {
timeClient.begin();
timeClient.update();

timeClient.setTimeOffset(bruceConfig.tmz * 3600);

localTime = myTZ.toLocal(timeClient.getEpochTime());

#if !defined(HAS_RTC)
rtc.setTime(timeClient.getEpochTime());
updateTimeStr(rtc.getTimeStruct());
clock_set = true;
#endif
}

/***************************************************************************************
** Function name: wifiApConnect
** Description: Create wifi Access Point
***************************************************************************************/
bool wifiApConnect()
bool _setupAP()
{
IPAddress AP_GATEWAY(172, 0, 0, 1);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(AP_GATEWAY, AP_GATEWAY, IPAddress(255, 255, 255, 0));
WiFi.softAP(bruceConfig.wifiAp.ssid, bruceConfig.wifiAp.pwd, 6, 0, 4, false);
wifiIP = WiFi.softAPIP().toString(); // update global var
Expand All @@ -110,10 +84,6 @@ bool wifiApConnect()
return true;
}

/***************************************************************************************
** Function name: wifiDisconnect
** Description: disconnects and turn off the WIFI module
***************************************************************************************/
void wifiDisconnect()
{
WiFi.softAPdisconnect(true); // turn off AP mode
Expand All @@ -123,30 +93,41 @@ void wifiDisconnect()
returnToMenu = true;
}

/***************************************************************************************
** Function name: wifiConnectMenu
** Description: Opens a menu to connect to a wifi
***************************************************************************************/
bool wifiConnectMenu(bool isAP)
bool wifiConnectMenu(wifi_mode_t mode)
{
if (isAP) return wifiApConnect();
if( WiFi.isConnected() ) return false; // safeguard

if (WiFi.status() != WL_CONNECTED) {
switch (mode)
{
case WIFI_AP: // access point
WiFi.mode(WIFI_AP);
return _setupAP();
break;

case WIFI_STA: // station mode
int nets;
WiFi.mode(WIFI_MODE_STA);
displayRedStripe("Scanning..", TFT_WHITE, bruceConfig.priColor);
nets = WiFi.scanNetworks();
options = {};
for (int i = 0; i < nets; i++) {
options.push_back(
{WiFi.SSID(i).c_str(), [=]() { wifiConnect(WiFi.SSID(i).c_str(), int(WiFi.encryptionType(i))); }}
options.emplace_back(
WiFi.SSID(i).c_str(), [=]() { _wifiConnect(WiFi.SSID(i), int(WiFi.encryptionType(i))); }
);
}
options.push_back({"Main Menu", [=]()
{ backToMenu(); }});
options.emplace_back( "Main Menu", [=](){ backToMenu(); });
delay(200);
loopOptions(options);
delay(200);
break;

case WIFI_AP_STA: // repeater mode
// _setupRepeater();
break;

default: // error handling
Serial.println("Unknown wifi mode: " + String(mode));
break;
}

if (returnToMenu)
Expand All @@ -173,7 +154,7 @@ void wifiConnectTask(int maxSearch)
if (WiFi.status() == WL_CONNECTED) {
wifiConnected = true;
wifiIP = WiFi.localIP().toString();
_updateClockTimezone();
updateClockTimezone();
return;
}
delay(300);
Expand Down
41 changes: 35 additions & 6 deletions src/core/wifi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,45 @@
#include <NTPClient.h>
#include <Timezone.h>

bool wifiConnect(String ssid, int encryption, bool isAP = false);
bool wifiApConnect();

bool _connectToWifiNetwork(String ssid, String pwd);
void _updateClockTimezone();
// TODO wrap in a class

// public
/**
* @brief disconnects and turns off wifi module
*/
void wifiDisconnect();

bool wifiConnectMenu( bool isAP = false);
/**
* @brief Opens a menu to connect to a wifi
* @param mode connection mode(AP, STA, AP_STA)
* @note This is the primary entry point for establishing connections
* @note returns false if wifi is already connected
*/
bool wifiConnectMenu(wifi_mode_t = WIFI_MODE_STA);


/**
* @brief displays MAC adress
*/
void checkMAC();

/**
* @brief tries to connect to min(found_networks, maxSearch) networks
* using stored passwords
* @TODO fix: rn it skips open networks due to password == "" check
*/
void wifiConnectTask(int maxSearch = 5);


// private
/**
* @brief Connects to wifiNetwork
*/
bool _wifiConnect(const String& ssid, int encryption);
bool _connectToWifiNetwork(const String& ssid, const String& pwd);

/**
* @brief sets up wifi in AP mode
* @note wifi.mode should be set before calling the method
*/
bool _setupAP();
6 changes: 4 additions & 2 deletions src/modules/others/webInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,10 @@ void startWebUi(bool mode_ap) {
setupSdCard();

if (WiFi.status() != WL_CONNECTED) {
// Choose wifi access mode
wifiConnectMenu(mode_ap);
if( mode_ap )
wifiConnectMenu(WIFI_AP);
else
wifiConnectMenu(WIFI_STA);
}

// configure web server
Expand Down
4 changes: 2 additions & 2 deletions src/modules/wifi/clients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ char* stringTochar(String s)
bool filterAnsiSequences = true; // Set to false to disable ANSI sequence filtering

void ssh_setup(String host) {
if(!wifiConnected) wifiConnectMenu(false);
if(!wifiConnected) wifiConnectMenu();

tft.fillScreen(bruceConfig.bgColor);
tft.setCursor(0, 0);
Expand Down Expand Up @@ -392,7 +392,7 @@ void telnet_loop() {
}

void telnet_setup() {
if(!wifiConnected) wifiConnectMenu(false);
if(!wifiConnected) wifiConnectMenu();

tft.fillScreen(bruceConfig.bgColor);
tft.setCursor(0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/wifi/scan_hosts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void readArpTable(netif * iface) {

void local_scan_setup() {
bool doScan = true;
if(!wifiConnected) doScan=wifiConnectMenu(false);
if(!wifiConnected) doScan=wifiConnectMenu();

if (doScan) {
hostslist.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/wifi/wigle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool Wigle::_check_token() {

auth_header = "Basic " + bruceConfig.wigleBasicToken;

if(!wifiConnected) wifiConnectMenu(false);
if(!wifiConnected) wifiConnectMenu();

return true;
}
Expand Down

0 comments on commit 1a1d43a

Please sign in to comment.