8000 Lots of WiFi optimizations · Tomato1107/esp32-snippets@2bc0d66 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2bc0d66

Browse files
committed
Lots of WiFi optimizations
1 parent f19354c commit 2bc0d66

File tree

2 files changed

+159
-56
lines changed

2 files changed

+159
-56
lines changed

cpp_utils/WiFi.cpp

Lines changed: 134 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <Task.h>
2929

3030

31-
3231
static char tag[]= "WiFi";
3332

3433

@@ -68,14 +67,46 @@ WiFi::WiFi() {
6867
* @param [in] ip The IP address of the DNS Server.
6968
* @return N/A.
7069
*/
71-
void WiFi::addDNSServer(std::string ip) {
72-
ip_addr_t dnsserver;
73-
ESP_LOGD(tag, "Setting DNS[%d] to %s", m_dnsCount, ip.c_str());
74-
inet_pton(AF_INET, ip.c_str(), &dnsserver);
75-
::dns_setserver(m_dnsCount, &dnsserver);
76-
m_dnsCount++;
70+
void WiFi::addDNSServer(const std::string& ip) {
71+
addDNSServer(ip.c_str());
72+
} // addDNSServer
73+
74+
void WiFi::addDNSServer(const char* ip) {
75+
ip_addr_t dnsserver;
76+
ESP_LOGD(tag, "Setting DNS[%d] to %s", m_dnsCount, ip);
77+
inet_pton(AF_INET, ip, &dnsserver);
78+
::dns_setserver(m_dnsCount, &dnsserver);
79+
m_dnsCount++;
80+
m_dnsCount %= 2;
7781
} // addDNSServer
7882

83+
/**
84+
* @brief Set a reference to a DNS server.
85+
*
86+
* Here we define a server that will act as a DNS server. We use numdns to specify which DNS server to set
87+
*
88+
* For example:
89+
*
90+
* @code{.cpp}
91+
* wifi.setDNSServer(0, "8.8.8.8");
92+
* wifi.setDNSServer(1, "8.8.4.4");
93+
* @endcode
94+
*
95+
* @param [in] numdns The DNS number we wish to set
96+
* @param [in] ip The IP address of the DNS Server.
97+
* @return N/A.
98+
*/
99+
void WiFi::setDNSServer(int numdns, const std::string& ip) {
100+
setDNSServer(numdns, ip.c_str());
101+
} // setDNSServer
102+
103+
void WiFi::setDNSServer(int numdns, const char* ip) {
104+
ip_addr_t dnsserver;
105+
ESP_LOGD(tag, "Setting DNS[%d] to %s", numdns, ip);
106+
inet_pton(AF_INET, ip, &dnsserver);
107+
::dns_setserver(numdns, &dnsserver);
108+
} // setDNSServer
109+
79110
/**
80111
* @brief Connect to an external access point.
81112
*
@@ -85,7 +116,7 @@ void WiFi::addDNSServer(std::string ip) {
85116
* @param[in] password The password of the access point to which we wish to connect.
86117
* @return N/A.
87118
*/
88-
void WiFi::connectAP(std::string ssid, std::string password){
119+
void WiFi::connectAP(const std::string& ssid, const std::string& password){
89120
::nvs_flash_init();
90121
::tcpip_adapter_init();
91122
if (ip.length() > 0 && gw.length() > 0 && netmask.length() > 0) {
@@ -115,7 +146,6 @@ void WiFi::connectAP(std::string ssid, std::string password){
115146
ESP_ERROR_CHECK(::esp_wifi_connect());
116147
} // connectAP
117148

118-
119149
/**
120150
* @brief Dump diagnostics to the log.
121151
*/
@@ -147,9 +177,9 @@ tcpip_adapter_ip_info_t WiFi::getApIpInfo() {
147177
std::string WiFi::getApMac() {
148178
uint8_t mac[6];
149179
esp_wifi_get_mac(WIFI_IF_AP, mac);
150-
std::stringstream s;
151-
s << std::hex << std::setfill('0') << std::setw(2) << (int) mac[0] << ':' << (int) mac[1] << ':' << (int) mac[2] << ':' << (int) mac[3] << ':' << (int) mac[4] << ':' << (int) mac[5];
152-
return s.str();
180+
auto mac_str = (char*) malloc(18);
181+
sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
182+
return std::string(std::move(mac_str));
153183
} // getApMac
154184

155185

@@ -171,18 +201,21 @@ std::string WiFi::getApSSID() {
171201
*
172202
* @return The IP address of the host or 0.0.0.0 if not found.
173203
*/
174-
struct in_addr WiFi::getHostByName(std::string hostName) {
175-
struct in_addr retAddr;
176-
struct hostent *he = gethostbyname(hostName.c_str());
177-
if (he == nullptr) {
178-
retAddr.s_addr = 0;
179-
ESP_LOGD(tag, "Unable to resolve %s - %d", hostName.c_str(), h_errno);
180-
} else {
181-
retAddr = *(struct in_addr *)(he->h_addr_list[0]);
182-
//ESP_LOGD(tag, "resolved %s to %.8x", hostName, *(uint32_t *)&retAddr);
204+
struct in_addr WiFi::getHostByName(const std::string& hostName) {
205+
return getHostByName(hostName.c_str());
206+
} // getHostByName
183207

184-
}
185-
return retAddr;
208+
struct in_addr WiFi::getHostByName(const char* hostName) {
209+
struct in_addr retAddr;
210+
struct hostent *he = gethostbyname(hostName);
211+
if (he == nullptr) {
212+
retAddr.s_addr = 0;
213+
ESP_LOGD(tag, "Unable to resolve %s - %d", hostName, h_errno);
214+
} else {
215+
retAddr = *(struct in_addr *)(he->h_addr_list[0]);
216+
ESP_LOGD(tag, "resolved %s to %.8x", hostName, *(uint32_t *)&retAddr);
217+
}
218+
return retAddr;
186219
} // getHostByName
187220

188221

@@ -226,9 +259,9 @@ tcpip_adapter_ip_info_t WiFi::getStaIpInfo() {
226259
std::string WiFi::getStaMac() {
227260
uint8_t mac[6];
228261
esp_wifi_get_mac(WIFI_IF_STA, mac);
229-
std::stringstream s;
230-
s << std::hex << std::setfill('0') << std::setw(2) << (int) mac[0] << ':' << (int) mac[1] << ':' << (int) mac[2] << ':' << (int) mac[3] << ':' << (int) mac[4] << ':' << (int) mac[5];
231-
return s.str();
262+
auto mac_str = (char*) malloc(18);
263+
sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
264+
return std::string(std::move(mac_str));
232265
} // getStaMac
233266

234267

@@ -295,7 +328,7 @@ std::vector<WiFiAPRecord> WiFi::scan() {
295328
* @param[in] password The password to use for station connections.
296329
* @return N/A.
297330
*/
298-
void WiFi::startAP(std::string ssid, std::string password) {
331+
void WiFi::startAP(const std::string& ssid, const std::string& password) {
299332
::nvs_flash_init();
300333
::tcpip_adapter_init();
301334
ESP_ERROR_CHECK(esp_event_loop_init(wifiEventHandler->getEventHandler(), wifiEventHandler));
@@ -306,7 +339,7 @@ void WiFi::startAP(std::string ssid, std::string password) {
306339
wifi_config_t apConfig;
307340
::memset(&apConfig, 0, sizeof(apConfig));
308341
::memcpy(apConfig.ap.ssid, ssid.data(), ssid.size());
309-
apConfig.ap.ssid_len = 0;
342+
apConfig.ap.ssid_len = ssid.size();
310343
::memcpy(apConfig.ap.password, password.data(), password.size());
311344
apConfig.ap.channel = 0;
312345
apConfig.ap.authmode = WIFI_AUTH_OPEN;
@@ -334,12 +367,18 @@ void WiFi::startAP(std::string ssid, std::string password) {
334367
* @param [in] netmask Netmask value.
335368
* @return N/A.
336369
*/
337-
void WiFi::setIPInfo(std::string ip, std::string gw, std::string netmask) {
370+
void WiFi::setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask) {
338371
this->ip = ip;
339372
this->gw = gw;
340373
this->netmask = netmask;
341374
} // setIPInfo
342375

376+
void WiFi::setIPInfo(std::string&& ip, std::string&& gw, std::string&& netmask) {
377+
this->ip = std::move(ip);
378+
this->gw = std::move(gw);
379+
this->netmask = std::move(netmask);
380+
} // setIPInfo
381+
343382

344383
/**
345384
* @brief Return a string representation of the WiFi access point record.
@@ -368,9 +407,11 @@ std::string WiFiAPRecord::toString() {
368407
auth = "<unknown>";
369408
break;
370409
}
371-
std::stringstream s;
372-
s<< "ssid: " << m_ssid << ", auth: " << auth << ", rssi: " << m_rssi;
373-
return s.str();
410+
// std::stringstream s;
411+
// s<< "ssid: " << m_ssid << ", auth: " << auth << ", rssi: " << m_rssi;
412+
auto info_str = (char*) malloc(6 + 32 + 8 + 22 + 8 + 3 + 1);
413+
sprintf(info_str, "ssid: %s, auth: %s, rssi: %d", m_ssid.c_str(), auth.c_str(), (int) m_rssi);
414+
return std::string(std::move(info_str));
374415
} // toString
375416

376417
MDNS::MDNS() {
@@ -392,23 +433,72 @@ MDNS::~MDNS() {
392433
* @param [in] port
393434
* @return N/A.
394435
*/
395-
void MDNS::serviceAdd(std::string service, std::string proto, uint16_t port) {
396-
ESP_ERROR_CHECK(mdns_service_add(m_mdns_server, service.c_str(), proto.c_str(), port));
436+
void MDNS::serviceAdd(const std::string& service, const std::string& proto, uint16_t port) {
437+
serviceAdd(service.c_str(), proto.c_str(), port);
438+
} // serviceAdd
439+
440+
441+
void MDNS::serviceInstanceSet(const std::string& service, const std::string& proto, const std::string& instance) {
442+
serviceInstanceSet(service.c_str(), proto.c_str(), instance.c_str());
443+
} // serviceInstanceSet
444+
445+
446+
void MDNS::servicePortSet(const std::string& service, const std::string& proto, uint16_t port) {
447+
servicePortSet(service.c_str(), proto.c_str(), port);
448+
} // servicePortSet
449+
450+
451+
void MDNS::serviceRemove(const std::string& service, const std::string& proto) {
452+
serviceRemove(service.c_str(), proto.c_str());
453+
} // serviceRemove
454+
455+
456+
/**
457+
* @brief Set the mDNS hostname.
458+
*
459+
* @param [in] hostname The host name to set against the mDNS.
460+
* @return N/A.
461+
*/
462+
void MDNS::setHostname(const std::string& hostname) {
463+
setHostname(hostname.c_str());
464+
} // setHostname
465+
466+
467+
/**
468+
* @brief Set the mDNS instance.
469+
*
470+
* @param [in] instance The instance name to set against the mDNS.
471+
* @return N/A.
472+
*/
473+
void MDNS::setInstance(const std::string& instance) {
474+
setInstance(instance.c_str());
475+
} // setInstance
476+
477+
/**
478+
* @brief Define the service for mDNS.
479+
*
480+
* @param [in] service
481+
* @param [in] proto
482+
* @param [in] port
483+
* @return N/A.
484+
*/
485+
void MDNS::serviceAdd(const char* service, const char* proto, uint16_t port) {
486+
ESP_ERROR_CHECK(mdns_service_add(m_mdns_server, service, proto, port));
397487
} // serviceAdd
398488

399489

400-
void MDNS::serviceInstanceSet(std::string service, std::string proto, std::string instance) {
401-
ESP_ERROR_CHECK(mdns_service_instance_set(m_mdns_server, service.c_str(), proto.c_str(), instance.c_str()));
490+
void MDNS::serviceInstanceSet(const char* service, const char* proto, const char* instance) {
491+
ESP_ERROR_CHECK(mdns_service_instance_set(m_mdns_server, service, proto, instance));
402492
} // serviceInstanceSet
403493

404494

405-
void MDNS::servicePortSet(std::string service, std::string proto, uint16_t port) {
406-
ESP_ERROR_CHECK(mdns_service_port_set(m_mdns_server, service.c_str(), proto.c_str(), port));
495+
void MDNS::servicePortSet(const char* service, const char* proto, uint16_t port) {
496+
ESP_ERROR_CHECK(mdns_service_port_set(m_mdns_server, service, proto, port));
407497
} // servicePortSet
408498

409499

410-
void MDNS::serviceRemove(std::string service, std::string proto) {
411-
ESP_ERROR_CHECK(mdns_service_remove(m_mdns_server, service.c_str(), proto.c_str()));
500+
void MDNS::serviceRemove(const char* service, const char* proto) {
501+
ESP_ERROR_CHECK(mdns_service_remove(m_mdns_server, service, proto));
412502
} // serviceRemove
413503

414504

@@ -418,8 +508,8 @@ void MDNS::serviceRemove(std::string service, std::string proto) {
418508
* @param [in] hostname The host name to set against the mDNS.
419509
* @return N/A.
420510
*/
421-
void MDNS::setHostname(std::string hostname) {
422-
ESP_ERROR_CHECK(mdns_set_hostname(m_mdns_server,hostname.c_str()));
511+
void MDNS::setHostname(const char* hostname) {
512+
ESP_ERROR_CHECK(mdns_set_hostname(m_mdns_server,hostname));
423513
} // setHostname
424514

425515

@@ -429,6 +519,6 @@ void MDNS::setHostname(std::string hostname) {
429519
* @param [in] instance The instance name to set against the mDNS.
430520
* @return N/A.
431521
*/
432-
void MDNS::setInstance(std::string instance) {
433-
ESP_ERROR_CHECK(mdns_set_instance(m_mdns_server, instance.c_str()));
522+
void MDNS::setInstance(const char* instance) {
523+
ESP_ERROR_CHECK(mdns_set_instance(m_mdns_server, instance));
434524
} // setInstance

cpp_utils/WiFi.h

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@ class MDNS {
2121
public:
2222
MDNS();
2323
~MDNS();
24-
void serviceAdd(std::string service, std::string proto, uint16_t port);
25-
void serviceInstanceSet(std::string service, std::string proto, std::string instance);
26-
void servicePortSet(std::string service, std::string proto, uint16_t port);
27-
void serviceRemove(std::string service, std::string proto);
28-
void setHostname(std::string hostname);
29-
void setInstance(std::string instance);
24+
void serviceAdd(const std::string& service, const std::string& proto, uint16_t port);
25+
void serviceInstanceSet(const std::string& service, const std::string& proto, const std::string& instance);
26+
void servicePortSet(const std::string& service, const std::string& proto, uint16_t port);
27+
void serviceRemove(const std::string& service, const std::string& proto);
28+
void setHostname(const std::string& hostname);
29+
void setInstance(const std::string& instance);
30+
// If we the above functions with a basic char*, a copy would be created into an std::string,
31+
// making the whole thing require twice as much processing power and speed
32+
void serviceAdd(const char* service, const char* proto, uint16_t port);
33+
void serviceInstanceSet(const char* service, const char* proto, const char* instance);
34+
void servicePortSet(const char* service, const char* proto, uint16_t port);
35+
void serviceRemove(const char* service, const char* proto);
36+
void setHostname(const char* hostname);
37+
void setInstance(const char* instance);
3038
private:
3139
mdns_server_t *m_mdns_server = nullptr;
3240
};
@@ -102,9 +110,13 @@ class WiFi {
102110

103111
public:
104112
WiFi();
105-
void addDNSServer(std::string ip);
106-
struct in_addr getHostByName(std::string hostName);
107-
void connectAP(std::string ssid, std::string passwd);
113+
void addDNSServer(const std::string& ip);
114+
void addDNSServer(const char* ip);
115+
void setDNSServer(int numdns, const std::string& ip);
116+
void setDNSServer(int numdns, const char* ip);
117+
struct in_addr getHostByName(const std::string& hostName);
118+
struct in_addr getHostByName(const char* hostName);
119+
void connectAP(const std::string& ssid, const std::string& password);
108120
void dump();
109121
static std::string getApMac();
110122
static tcpip_adapter_ip_info_t getApIpInfo();
@@ -114,8 +126,9 @@ class WiFi {
114126
static std::string getStaMac();
115127
static std::string getStaSSID();
116128
std::vector<WiFiAPRecord> scan();
117-
void startAP(std::string ssid, std::string passwd);
118-
void setIPInfo(std::string ip, std::string gw, std::string netmask);
129+
void startAP(const std::string& ssid, const std::string& passwd);
130+
void setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask);
131+
void setIPInfo(std::string&& ip, std::string&& gw, std::string&& netmask);
119132

120133

121134

@@ -129,7 +142,7 @@ class WiFi {
129142
this->wifiEventHandler = wifiEventHandler;
130143
}
131144
private:
132-
int m_dnsCount=0;
145+
uint8_t m_dnsCount=0;
133146
//char *m_dnsServer = nullptr;
134147

135148
};

0 commit comments

Comments
 (0)
0