Skip to content

Commit 476660f

Browse files
NathanJPhillipsme-no-dev
authored andcommitted
Cleanup WiFiMulti (#2955)
* Use macros for switch case labels * Fixed spelling error in "too" * Fix spacing * Inline private functions that are only called once
1 parent f558e69 commit 476660f

File tree

2 files changed

+50
-68
lines changed

2 files changed

+50
-68
lines changed

libraries/WiFi/src/WiFiMulti.cpp

+50-65
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,64 @@ WiFiMulti::WiFiMulti()
3434

3535
WiFiMulti::~WiFiMulti()
3636
{
37-
APlistClean();
37+
for(uint32_t i = 0; i < APlist.size(); i++) {
38+
WifiAPlist_t entry = APlist[i];
39+
if(entry.ssid) {
40+
free(entry.ssid);
41+
}
42+
if(entry.passphrase) {
43+
free(entry.passphrase);
44+
}
45+
}
46+
APlist.clear();
3847
}
3948

4049
bool WiFiMulti::addAP(const char* ssid, const char *passphrase)
4150
{
42-
return APlistAdd(ssid, passphrase);
51+
WifiAPlist_t newAP;
52+
53+
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
54+
// fail SSID too long or missing!
55+
log_e("[WIFI][APlistAdd] no ssid or ssid too long");
56+
return false;
57+
}
58+
59+
if(passphrase && strlen(passphrase) > 63) {
60+
// fail passphrase too long!
61+
log_e("[WIFI][APlistAdd] passphrase too long");
62+
return false;
63+
}
64+
65+
newAP.ssid = strdup(ssid);
66+
67+
if(!newAP.ssid) {
68+
log_e("[WIFI][APlistAdd] fail newAP.ssid == 0");
69+
return false;
70+
}
71+
72+
if(passphrase && *passphrase != 0x00) {
73+
newAP.passphrase = strdup(passphrase);
74+
if(!newAP.passphrase) {
75+
log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0");
76+
free(newAP.ssid);
77+
return false;
78+
}
79+
} else {
80+
newAP.passphrase = NULL;
81+
}
82+
83+
APlist.push_back(newAP);
84+
log_i("[WIFI][APlistAdd] add SSID: %s", newAP.ssid);
85+
return true;
4386
}
4487

4588
uint8_t WiFiMulti::run(uint32_t connectTimeout)
4689
{
47-
4890
int8_t scanResult;
4991
uint8_t status = WiFi.status();
5092
if(status == WL_CONNECTED) {
5193
for(uint32_t x = 0; x < APlist.size(); x++) {
52-
if(WiFi.SSID()==APlist[x].ssid){
94+
if(WiFi.SSID()==APlist[x].ssid) {
5395
return status;
5496
}
5597
}
@@ -119,7 +161,7 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout)
119161

120162
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID);
121163
status = WiFi.status();
122-
164+
123165
auto startTime = millis();
124166
// wait for connection, fail, or timeout
125167
while(status != WL_CONNECTED && status != WL_NO_SSID_AVAIL && status != WL_CONNECT_FAILED && (millis() - startTime) <= connectTimeout) {
@@ -128,17 +170,17 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout)
128170
}
129171

130172
switch(status) {
131-
case 3:
173+
case WL_CONNECTED:
132174
log_i("[WIFI] Connecting done.");
133175
log_d("[WIFI] SSID: %s", WiFi.SSID().c_str());
134176
log_d("[WIFI] IP: %s", WiFi.localIP().toString().c_str());
135177
log_d("[WIFI] MAC: %s", WiFi.BSSIDstr().c_str());
136178
log_d("[WIFI] Channel: %d", WiFi.channel());
137179
break;
138-
case 1:
180+
case WL_NO_SSID_AVAIL:
139181
log_e("[WIFI] Connecting Failed AP not found.");
140182
break;
141-
case 4:
183+
case WL_CONNECT_FAILED:
142184
log_e("[WIFI] Connecting Failed.");
143185
break;
144186
default:
@@ -160,60 +202,3 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout)
160202

161203
return status;
162204
}
163-
164-
// ##################################################################################
165-
166-
bool WiFiMulti::APlistAdd(const char* ssid, const char *passphrase)
167-
{
168-
169-
WifiAPlist_t newAP;
170-
171-
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
172-
// fail SSID to long or missing!
173-
log_e("[WIFI][APlistAdd] no ssid or ssid to long");
174-
return false;
175-
}
176-
177-
if(passphrase && strlen(passphrase) > 63) {
178-
// fail passphrase to long!
179-
log_e("[WIFI][APlistAdd] passphrase to long");
180-
return false;
181-
}
182-
183-
newAP.ssid = strdup(ssid);
184-
185-
if(!newAP.ssid) {
186-
log_e("[WIFI][APlistAdd] fail newAP.ssid == 0");
187-
return false;
188-
}
189-
190-
if(passphrase && *passphrase != 0x00) {
191-
newAP.passphrase = strdup(passphrase);
192-
if(!newAP.passphrase) {
193-
log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0");
194-
free(newAP.ssid);
195-
return false;
196-
}
197-
} else {
198-
newAP.passphrase = NULL;
199-
}
200-
201-
APlist.push_back(newAP);
202-
log_i("[WIFI][APlistAdd] add SSID: %s", newAP.ssid);
203-
return true;
204-
}
205-
206-
void WiFiMulti::APlistClean(void)
207-
{
208-
for(uint32_t i = 0; i < APlist.size(); i++) {
209-
WifiAPlist_t entry = APlist[i];
210-
if(entry.ssid) {
211-
free(entry.ssid);
212-
}
213-
if(entry.passphrase) {
214-
free(entry.passphrase);
215-
}
216-
}
217-
APlist.clear();
218-
}
219-

libraries/WiFi/src/WiFiMulti.h

-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class WiFiMulti
4646

4747
private:
4848
std::vector<WifiAPlist_t> APlist;
49-
bool APlistAdd(const char* ssid, const char *passphrase = NULL);
50-
void APlistClean(void);
51-
5249
};
5350

5451
#endif /* WIFICLIENTMULTI_H_ */

0 commit comments

Comments
 (0)