Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi Wlan improvements #266

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ bool JSONToSettings(JsonObject doc) {
}
if (doc.containsKey("wifi")) {
// WiFi settings
static String hostName = doc["wifi"]["hostname"];
String hostName = doc["wifi"]["hostname"];
if (!Wlan_ValidateHostname(hostName)) {
Log_Println("Invalid hostname", LOGLEVEL_ERROR);
return false;
Expand Down Expand Up @@ -726,15 +726,11 @@ static void settingsToJSON(JsonObject obj, const String section) {
if (section == "ssids") {
// saved SSID's
JsonObject ssidsObj = obj.createNestedObject("ssids");
static String ssids[10];

JsonArray ssidArr = ssidsObj.createNestedArray("savedSSIDs");
size_t len = Wlan_GetSSIDs(ssids, 10);
if (len > 0) {
for (int i = 0; i < len; i++) {
ssidArr.add(ssids[i]);
}
}
Wlan_GetSavedNetworks([ssidArr](const WiFiSettings &network) {
ssidArr.add(network.ssid);
});

// active SSID
if (Wlan_IsConnected()) {
ssidsObj["active"] = Wlan_GetCurrentSSID();
Expand Down Expand Up @@ -1491,12 +1487,9 @@ void explorerHandleAudioRequest(AsyncWebServerRequest *request) {
void handleGetSavedSSIDs(AsyncWebServerRequest *request) {
AsyncJsonResponse *response = new AsyncJsonResponse(true);
JsonArray json_ssids = response->getRoot();
static String ssids[10];

size_t len = Wlan_GetSSIDs(ssids, 10);
for (int i = 0; i < len; i++) {
json_ssids.add(ssids[i]);
}
Wlan_GetSavedNetworks([json_ssids](const WiFiSettings &network) {
json_ssids.add(network.ssid);
});

response->setLength();
request->send(response);
Expand Down
13 changes: 3 additions & 10 deletions src/Wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,10 @@ uint8_t Wlan_NumSavedNetworks() {
return numKnownNetworks;
}

// write saved ssids into the passed array.
size_t Wlan_GetSSIDs(String *ssids, size_t max_len) {
if (numKnownNetworks > max_len) {
return 0;
}

void Wlan_GetSavedNetworks(std::function<void(const WiFiSettings &)> handler) {
for (uint8_t i = 0; i < numKnownNetworks; i++) {
ssids[i] = knownNetworks[i].ssid;
handler(knownNetworks[i]);
}

return numKnownNetworks;
}

const String Wlan_GetCurrentSSID() {
Expand Down Expand Up @@ -620,5 +613,5 @@ void writeWifiStatusToNVS(bool wifiStatus) {
}

bool Wlan_IsConnected(void) {
return (WiFi.status() == WL_CONNECTED);
return (wifiState == WIFI_STATE_CONNECTED);
}
4 changes: 3 additions & 1 deletion src/Wlan.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <functional>

// be very careful changing this struct, as it is used for NVS storage and will corrupt existing entries
struct WiFiSettings {
char ssid[33];
Expand All @@ -16,7 +18,7 @@ void Wlan_Init(void);
void Wlan_Cyclic(void);
bool Wlan_AddNetworkSettings(WiFiSettings);
uint8_t Wlan_NumSavedNetworks();
size_t Wlan_GetSSIDs(String *, size_t);
void Wlan_GetSavedNetworks(std::function<void(const WiFiSettings &)>);
const String Wlan_GetCurrentSSID();
const String Wlan_GetHostname();
bool Wlan_DeleteNetwork(String);
Expand Down