-
Notifications
You must be signed in to change notification settings - Fork 45
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
Sorting of scanned wifi networks #661
base: main
Are you sure you want to change the base?
Changes from all commits
f234c27
efb6f43
28296a1
36453d1
806690f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
#include "SPI.h" | ||
#include "WiFiNINA.h" | ||
#include "Wippersnapper.h" | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
#define NINAFWVER \ | ||
"1.7.7" /*!< min. nina-fw version compatible with this library. */ | ||
|
@@ -101,10 +103,34 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { | |
_pass = WS._config.network.pass; | ||
} | ||
|
||
/****************************************************************/ | ||
/*! | ||
@brief a structure to hold network information for sorting | ||
*/ | ||
/****************************************************************/ | ||
struct WiFiNetwork { | ||
char ssid[33]; /*!< SSID (Max 32 characters + null terminator */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like |
||
int rssi; /*!< Received Signal Strength Indicator */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RSSI should be type |
||
}; | ||
|
||
/*******************************************************************/ | ||
/*! | ||
@brief Comparison function to sort by RSSI in descending order | ||
@param a | ||
WiFiNetwork object | ||
@param b | ||
WiFiNetwork object | ||
@returns True if a.rssi > b.rssi | ||
*/ | ||
/*******************************************************************/ | ||
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) { | ||
return a.rssi > b.rssi; | ||
} | ||
|
||
/***********************************************************/ | ||
/*! | ||
@brief Performs a scan of local WiFi networks. | ||
@returns True if `_network_ssid` is found, False otherwise. | ||
@brief Performs a scan of local WiFi networks. | ||
@returns True if `_network_ssid` is found, False otherwise. | ||
*/ | ||
/***********************************************************/ | ||
bool check_valid_ssid() { | ||
|
@@ -119,22 +145,38 @@ class Wippersnapper_AIRLIFT : public Wippersnapper { | |
return false; | ||
} | ||
|
||
// Was the network within secrets.json found? | ||
// Dynamically allocate memory for the network list | ||
std::vector<WiFiNetwork> networks(n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about using a vector/dynamic alloc. here, allowing too many possible networks will increase sort time. Instead, could you define a global |
||
|
||
// Store the scanned networks in the vector | ||
for (int i = 0; i < n; ++i) { | ||
if (strcmp(_ssid, WiFi.SSID(i)) == 0) { | ||
WS_DEBUG_PRINT("SSID found! RSSI: "); | ||
WS_DEBUG_PRINTLN(WiFi.RSSI(i)); | ||
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1); | ||
networks[i].ssid[sizeof(networks[i].ssid) - 1] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you switch SSID to a String type, it will be null-terminated during compilation. |
||
'\0'; // Ensure null termination | ||
networks[i].rssi = WiFi.RSSI(i); | ||
} | ||
|
||
// Sort the networks by RSSI in descending order | ||
std::sort(networks.begin(), networks.end(), compareByRSSI); | ||
|
||
// Was the network within secrets.json found? | ||
for (const auto &network : networks) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we using |
||
if (strcmp(_ssid, network.ssid) == 0) { | ||
WS_DEBUG_PRINT("SSID ("); | ||
WS_DEBUG_PRINT(_ssid); | ||
WS_DEBUG_PRINT(") found! RSSI: "); | ||
WS_DEBUG_PRINTLN(network.rssi); | ||
return true; | ||
} | ||
} | ||
|
||
// User-set network not found, print scan results to serial console | ||
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!"); | ||
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: "); | ||
for (int i = 0; i < n; ++i) { | ||
WS_DEBUG_PRINT(WiFi.SSID(i)); | ||
for (const auto &network : networks) { | ||
WS_DEBUG_PRINT(network.ssid); | ||
WS_DEBUG_PRINT(" "); | ||
WS_DEBUG_PRINT(WiFi.RSSI(i)); | ||
WS_DEBUG_PRINT(network.rssi); | ||
WS_DEBUG_PRINTLN("dB"); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want this here, or moved to be included by WipperSnapper.h or another file (networking.h?) so it's included by all the drivers?