Skip to content

Commit ec15716

Browse files
fabik111pennam
authored andcommitted
WiFiS3: scanNetwors refactor
1 parent 2850297 commit ec15716

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

Diff for: libraries/WiFiS3/src/WiFi.cpp

+39-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "WiFi.h"
22

3+
#define WIFI_MAX_BSSID_STRING_LENGTH 17
4+
35
using namespace std;
46

57
/* -------------------------------------------------------------------------- */
@@ -306,44 +308,48 @@ int8_t CWifi::scanNetworks() {
306308
if(modem.write(string(PROMPT(_WIFISCAN)),res,CMD(_WIFISCAN))) {
307309
char *startAp = (char*)res.c_str();
308310
char *endAP = strstr(startAp, "\r\n");
309-
for(; endAP != NULL; startAp = endAP, endAP = strstr(startAp, "\r\n")) {
310-
CAccessPoint ap;
311+
for(; endAP != NULL; startAp = endAP + 2, endAP = strstr(startAp, "\r\n")) {
312+
/* split the modem response in multiple lines and parse once at time.
313+
* The output will be something like:
314+
* SSID | BSSID | RSSI | CHANNEL | SECURITY
315+
*/
316+
*endAP = '\0'; // Replace \r with \0
317+
311318
char *token[5];
312-
*endAP++ = '\0'; // Replace \r with \0
313-
endAP++;
314-
char *startToken = startAp;
315-
char *endToken = strstr(startAp, " | ");
316-
uint8_t i = 0;
317-
for(; i < 5 && endToken != NULL; i++){
318-
token[i] = startToken;
319-
*endToken++ = '\0';
320-
endToken = endToken + 2;
321-
startToken = endToken;
322-
endToken = strstr(startToken, " | ");
319+
uint8_t i = 1;
320+
token[0] = startAp;
321+
for(; i < 5; i++){
322+
char *endToken = strstr(token[i-1], " | ");
323323
if(endToken == NULL){
324-
token[++i] = startToken;
324+
break;
325325
}
326+
memset(endToken, '\0', 3);
327+
token[i] = endToken + 3;
326328
}
327329

328-
if(i>=5) {
329-
if(strlen(token[0]) > WL_SSID_MAX_LENGTH || strlen(token[1]) != WL_MAX_BSSID_LENGTH){
330-
continue;
331-
}
332-
strcpy(ap.ssid, token[0]);
333-
macStr2macArray(ap.uint_bssid, token[1]);
334-
ap.rssi = atoi(token[2]);
335-
ap.channel = atoi(token[3]);
336-
ap.encryption_mode = Encr2wl_enc(token[4]);
337-
// insert in list
338-
if( _apsFound < WL_MAX_AP_LIST ){
339-
access_points[_apsFound] = ap;
340-
_apsFound++;
341-
_sortAPlist(_apsFound);
342-
}else{
343-
if (ap.rssi > access_points[WL_MAX_AP_LIST-1].rssi){
344-
access_points[WL_MAX_AP_LIST-1] = ap;
345-
_sortAPlist(WL_MAX_AP_LIST);
346-
}
330+
if(i < 5 || strlen(token[0]) == 0 || strlen(token[0]) > WL_SSID_MAX_LENGTH ||
331+
strlen(token[1]) != WIFI_MAX_BSSID_STRING_LENGTH ||
332+
strlen(token[2]) == 0 || strlen(token[3]) == 0 || strlen(token[4]) == 0){
333+
/* Skip the row and process the next one */
334+
continue;
335+
}
336+
337+
CAccessPoint ap;
338+
strcpy(ap.ssid, token[0]);
339+
macStr2macArray(ap.uint_bssid, token[1]);
340+
ap.rssi = atoi(token[2]);
341+
ap.channel = atoi(token[3]);
342+
ap.encryption_mode = Encr2wl_enc(token[4]);
343+
344+
// insert in list
345+
if( _apsFound < WIFI_MAX_SSID_COUNT ){
346+
access_points[_apsFound] = ap;
347+
_apsFound++;
348+
_sortAPlist(_apsFound);
349+
}else{
350+
if (ap.rssi > access_points[WIFI_MAX_SSID_COUNT-1].rssi){
351+
access_points[WIFI_MAX_SSID_COUNT-1] = ap;
352+
_sortAPlist(WIFI_MAX_SSID_COUNT);
347353
}
348354
}
349355
}

Diff for: libraries/WiFiS3/src/WiFi.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
#define WIFI_FIRMWARE_LATEST_VERSION "0.5.2"
2121

22-
#define WL_MAX_AP_LIST 10
23-
#define WL_MAX_BSSID_LENGTH 17
24-
#define WL_MAX_SSID_LENGHT_S WL_SSID_MAX_LENGTH + 1 // +1 for null terminator
22+
#ifndef WIFI_MAX_SSID_COUNT
23+
#define WIFI_MAX_SSID_COUNT 10
24+
#endif
2525

2626
class CAccessPoint {
2727
public:
@@ -41,7 +41,7 @@ class CAccessPoint {
4141
encryption_mode = obj.encryption_mode;
4242
memcpy(uint_bssid, obj.uint_bssid, sizeof(uint_bssid));
4343
}
44-
char ssid[WL_MAX_SSID_LENGHT_S];
44+
char ssid[WL_SSID_MAX_LENGTH + 1]; // +1 for null terminator
4545
uint8_t uint_bssid[6];
4646
int rssi;
4747
uint8_t channel;
@@ -54,7 +54,7 @@ class CWifi {
5454
void _config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
5555
void _sortAPlist(uint8_t num);
5656
unsigned long _timeout;
57-
CAccessPoint access_points[WL_MAX_AP_LIST];
57+
CAccessPoint access_points[WIFI_MAX_SSID_COUNT];
5858
uint8_t _apsFound = 0;
5959
std::string ssid;
6060
std::string apssid;

0 commit comments

Comments
 (0)