Skip to content

Commit cdb96c7

Browse files
committed
clang format "sort networks by RSSI"
1 parent c6216b1 commit cdb96c7

File tree

5 files changed

+106
-114
lines changed

5 files changed

+106
-114
lines changed

src/network_interfaces/Wippersnapper_AIRLIFT.h

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include "Adafruit_MQTT.h"
2424
#include "Adafruit_MQTT_Client.h"
2525
#include "Arduino.h"
26-
#include <vector>
27-
#include <algorithm>
2826
#include "SPI.h"
2927
#include "WiFiNINA.h"
3028
#include "Wippersnapper.h"
29+
#include <algorithm>
30+
#include <vector>
3131

3232
#define NINAFWVER \
3333
"1.7.7" /*!< min. nina-fw version compatible with this library. */
@@ -103,71 +103,72 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
103103
_pass = WS._config.network.pass;
104104
}
105105

106-
// Define a structure to hold network information
107-
struct WiFiNetwork {
106+
// Define a structure to hold network information
107+
struct WiFiNetwork {
108108
char ssid[33]; // Maximum SSID length is 32 characters + null terminator
109109
int rssi;
110-
};
110+
};
111111

112-
// Comparison function to sort by RSSI in descending order
113-
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) {
112+
// Comparison function to sort by RSSI in descending order
113+
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) {
114114
return a.rssi > b.rssi;
115-
}
115+
}
116116

117-
/***********************************************************/
118-
/*!
119-
@brief Performs a scan of local WiFi networks.
120-
@returns True if `_network_ssid` is found, False otherwise.
121-
*/
122-
/***********************************************************/
123-
bool check_valid_ssid() {
117+
/***********************************************************/
118+
/*!
119+
@brief Performs a scan of local WiFi networks.
120+
@returns True if `_network_ssid` is found, False otherwise.
121+
*/
122+
/***********************************************************/
123+
bool check_valid_ssid() {
124124
// Disconnect WiFi from an AP if it was previously connected
125125
WiFi.disconnect();
126126
delay(100);
127127

128128
// Perform a network scan
129129
int n = WiFi.scanNetworks();
130130
if (n == 0) {
131-
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
132-
return false;
131+
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
132+
return false;
133133
}
134134

135135
// Dynamically allocate memory for the network list
136136
std::vector<WiFiNetwork> networks(n);
137137

138138
// Store the scanned networks in the vector
139139
for (int i = 0; i < n; ++i) {
140-
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
141-
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination
142-
networks[i].rssi = WiFi.RSSI(i);
140+
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
141+
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
142+
'\0'; // Ensure null termination
143+
networks[i].rssi = WiFi.RSSI(i);
143144
}
144145

145146
// Sort the networks by RSSI in descending order
146147
std::sort(networks.begin(), networks.end(), compareByRSSI);
147148

148149
// Was the network within secrets.json found?
149150
for (const auto &network : networks) {
150-
if (strcmp(_ssid, network.ssid) == 0) {
151-
WS_DEBUG_PRINT("SSID (");
152-
WS_DEBUG_PRINT(_ssid);
153-
WS_DEBUG_PRINT(") found! RSSI: ");
154-
WS_DEBUG_PRINTLN(network.rssi);
155-
return true;
156-
}
151+
if (strcmp(_ssid, network.ssid) == 0) {
152+
WS_DEBUG_PRINT("SSID (");
153+
WS_DEBUG_PRINT(_ssid);
154+
WS_DEBUG_PRINT(") found! RSSI: ");
155+
WS_DEBUG_PRINTLN(network.rssi);
156+
return true;
157+
}
157158
}
158159

159160
// User-set network not found, print scan results to serial console
160161
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
161162
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
162163
for (const auto &network : networks) {
163-
WS_DEBUG_PRINT(network.ssid);
164-
WS_DEBUG_PRINT(" ");
165-
WS_DEBUG_PRINT(network.rssi);
166-
WS_DEBUG_PRINTLN("dB");
164+
WS_DEBUG_PRINT(network.ssid);
165+
WS_DEBUG_PRINT(" ");
166+
WS_DEBUG_PRINT(network.rssi);
167+
WS_DEBUG_PRINTLN("dB");
167168
}
168169

169170
return false;
170-
}
171+
}
171172

172173
/********************************************************/
173174
/*!

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
#include "Adafruit_MQTT.h"
2424
#include "Adafruit_MQTT_Client.h"
2525
#include "Arduino.h"
26-
#include <vector>
27-
#include <algorithm>
2826
#include "WiFi.h"
2927
#include "WiFiMulti.h"
3028
#include <NetworkClient.h>
3129
#include <NetworkClientSecure.h>
30+
#include <algorithm>
31+
#include <vector>
3232
extern Wippersnapper WS;
3333

3434
/****************************************************************************/
@@ -94,13 +94,13 @@ class Wippersnapper_ESP32 : public Wippersnapper {
9494

9595
// Define a structure to hold network information
9696
struct WiFiNetwork {
97-
char ssid[33]; // Maximum SSID length is 32 characters + null terminator
98-
int rssi;
97+
char ssid[33]; // Maximum SSID length is 32 characters + null terminator
98+
int rssi;
9999
};
100100

101101
// Comparison function to sort by RSSI in descending order
102102
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) {
103-
return a.rssi > b.rssi;
103+
return a.rssi > b.rssi;
104104
}
105105

106106
/***********************************************************/
@@ -129,54 +129,56 @@ class Wippersnapper_ESP32 : public Wippersnapper {
129129
// Perform a network scan
130130
int n = WiFi.scanNetworks();
131131
if (n == 0) {
132-
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
133-
return false;
132+
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
133+
return false;
134134
}
135135

136136
// Dynamically allocate memory for the network list
137137
std::vector<WiFiNetwork> networks(n);
138138

139139
// Store the scanned networks in the vector
140140
for (int i = 0; i < n; ++i) {
141-
strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1);
142-
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination
143-
networks[i].rssi = WiFi.RSSI(i);
141+
strncpy(networks[i].ssid, WiFi.SSID(i).c_str(),
142+
sizeof(networks[i].ssid) - 1);
143+
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
144+
'\0'; // Ensure null termination
145+
networks[i].rssi = WiFi.RSSI(i);
144146
}
145147

146148
// Sort the networks by RSSI in descending order
147149
std::sort(networks.begin(), networks.end(), compareByRSSI);
148150

149151
// Was the network within secrets.json found?
150152
for (const auto &network : networks) {
151-
if (strcmp(_ssid, network.ssid) == 0) {
153+
if (strcmp(_ssid, network.ssid) == 0) {
154+
WS_DEBUG_PRINT("SSID (");
155+
WS_DEBUG_PRINT(_ssid);
156+
WS_DEBUG_PRINT(") found! RSSI: ");
157+
WS_DEBUG_PRINTLN(network.rssi);
158+
return true;
159+
}
160+
if (WS._isWiFiMulti) {
161+
// multi network mode
162+
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
163+
if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) {
152164
WS_DEBUG_PRINT("SSID (");
153-
WS_DEBUG_PRINT(_ssid);
165+
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
154166
WS_DEBUG_PRINT(") found! RSSI: ");
155167
WS_DEBUG_PRINTLN(network.rssi);
156168
return true;
169+
}
157170
}
158-
if (WS._isWiFiMulti) {
159-
// multi network mode
160-
for (int j = 0; j < WS_MAX_ALT_WIFI_NETWORKS; j++) {
161-
if (strcmp(WS._multiNetworks[j].ssid, network.ssid) == 0) {
162-
WS_DEBUG_PRINT("SSID (");
163-
WS_DEBUG_PRINT(WS._multiNetworks[j].ssid);
164-
WS_DEBUG_PRINT(") found! RSSI: ");
165-
WS_DEBUG_PRINTLN(network.rssi);
166-
return true;
167-
}
168-
}
169-
}
171+
}
170172
}
171173

172174
// User-set network not found, print scan results to serial console
173175
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
174176
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
175177
for (const auto &network : networks) {
176-
WS_DEBUG_PRINT(network.ssid);
177-
WS_DEBUG_PRINT(" ");
178-
WS_DEBUG_PRINT(network.rssi);
179-
WS_DEBUG_PRINTLN("dB");
178+
WS_DEBUG_PRINT(network.ssid);
179+
WS_DEBUG_PRINT(" ");
180+
WS_DEBUG_PRINT(network.rssi);
181+
WS_DEBUG_PRINTLN("dB");
180182
}
181183

182184
return false;

src/network_interfaces/Wippersnapper_ESP8266.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
#define WIPPERSNAPPER_ESP8266_H
1919

2020
#ifdef ARDUINO_ARCH_ESP8266
21-
#include <vector>
22-
#include <algorithm>
2321
#include "Adafruit_MQTT.h"
2422
#include "Adafruit_MQTT_Client.h"
2523
#include "ESP8266WiFi.h"
2624
#include "ESP8266WiFiMulti.h"
2725
#include "Wippersnapper.h"
26+
#include <algorithm>
27+
#include <vector>
2828

2929
/* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new
3030
* SSL certificate every year. If adding Secure MQTT to your ESP8266 project is
@@ -116,15 +116,13 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
116116
}
117117

118118
// Define a structure to hold network information
119-
struct WiFiNetwork
120-
{
119+
struct WiFiNetwork {
121120
char ssid[33]; // Maximum SSID length is 32 characters + null terminator
122121
int rssi;
123122
};
124123

125124
// Comparison function to sort by RSSI in descending order
126-
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b)
127-
{
125+
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) {
128126
return a.rssi > b.rssi;
129127
}
130128

@@ -153,8 +151,10 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
153151

154152
// Store the scanned networks in the vector
155153
for (int i = 0; i < n; ++i) {
156-
strncpy(networks[i].ssid, WiFi.SSID(i).c_str(), sizeof(networks[i].ssid) - 1);
157-
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination
154+
strncpy(networks[i].ssid, WiFi.SSID(i).c_str(),
155+
sizeof(networks[i].ssid) - 1);
156+
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
157+
'\0'; // Ensure null termination
158158
networks[i].rssi = WiFi.RSSI(i);
159159
}
160160

@@ -187,8 +187,7 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
187187
// User-set network not found, print scan results to serial console
188188
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
189189
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
190-
for (const auto &network : networks)
191-
{
190+
for (const auto &network : networks) {
192191
WS_DEBUG_PRINT(network.ssid);
193192
WS_DEBUG_PRINT(" ");
194193
WS_DEBUG_PRINT(network.rssi);

src/network_interfaces/Wippersnapper_WIFININA.h

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
#include <Adafruit_MQTT.h>
2020
#include <Adafruit_MQTT_Client.h>
2121
#include <Arduino.h>
22-
#include <vector>
23-
#include <algorithm>
2422
#include <SPI.h>
2523
#include <WiFiNINA.h>
24+
#include <algorithm>
25+
#include <vector>
2626

2727
#include "Wippersnapper.h"
2828

@@ -111,17 +111,14 @@ class Wippersnapper_WIFININA : public Wippersnapper {
111111
strlcpy(WS._config.network.pass, _pass, sizeof(WS._config.network.pass));
112112
}
113113

114-
115114
// Define a structure to hold network information
116-
struct WiFiNetwork
117-
{
115+
struct WiFiNetwork {
118116
char ssid[33]; // Maximum SSID length is 32 characters + null terminator
119117
int rssi;
120118
};
121119

122120
// Comparison function to sort by RSSI in descending order
123-
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b)
124-
{
121+
bool static compareByRSSI(const WiFiNetwork &a, const WiFiNetwork &b) {
125122
return a.rssi > b.rssi;
126123
}
127124

@@ -140,42 +137,43 @@ class Wippersnapper_WIFININA : public Wippersnapper {
140137
// Perform a network scan
141138
int n = WiFi.scanNetworks();
142139
if (n == 0) {
143-
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
144-
return false;
140+
WS_DEBUG_PRINTLN("ERROR: No WiFi networks found!");
141+
return false;
145142
}
146143

147144
// Dynamically allocate memory for the network list
148145
std::vector<WiFiNetwork> networks(n);
149146

150147
// Store the scanned networks in the vector
151148
for (int i = 0; i < n; ++i) {
152-
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
153-
networks[i].ssid[sizeof(networks[i].ssid) - 1] = '\0'; // Ensure null termination
154-
networks[i].rssi = WiFi.RSSI(i);
149+
strncpy(networks[i].ssid, WiFi.SSID(i), sizeof(networks[i].ssid) - 1);
150+
networks[i].ssid[sizeof(networks[i].ssid) - 1] =
151+
'\0'; // Ensure null termination
152+
networks[i].rssi = WiFi.RSSI(i);
155153
}
156154

157155
// Sort the networks by RSSI in descending order
158156
std::sort(networks.begin(), networks.end(), compareByRSSI);
159157

160158
// Was the network within secrets.json found?
161159
for (const auto &network : networks) {
162-
if (strcmp(_ssid, network.ssid) == 0) {
163-
WS_DEBUG_PRINT("SSID (");
164-
WS_DEBUG_PRINT(_ssid);
165-
WS_DEBUG_PRINT(") found! RSSI: ");
166-
WS_DEBUG_PRINTLN(network.rssi);
167-
return true;
168-
}
160+
if (strcmp(_ssid, network.ssid) == 0) {
161+
WS_DEBUG_PRINT("SSID (");
162+
WS_DEBUG_PRINT(_ssid);
163+
WS_DEBUG_PRINT(") found! RSSI: ");
164+
WS_DEBUG_PRINTLN(network.rssi);
165+
return true;
166+
}
169167
}
170168

171169
// User-set network not found, print scan results to serial console
172170
WS_DEBUG_PRINTLN("ERROR: Your requested WiFi network was not found!");
173171
WS_DEBUG_PRINTLN("WipperSnapper found these WiFi networks: ");
174172
for (const auto &network : networks) {
175-
WS_DEBUG_PRINT(network.ssid);
176-
WS_DEBUG_PRINT(" ");
177-
WS_DEBUG_PRINT(network.rssi);
178-
WS_DEBUG_PRINTLN("dB");
173+
WS_DEBUG_PRINT(network.ssid);
174+
WS_DEBUG_PRINT(" ");
175+
WS_DEBUG_PRINT(network.rssi);
176+
WS_DEBUG_PRINTLN("dB");
179177
}
180178

181179
return false;

0 commit comments

Comments
 (0)