8000 File i forgot to stage in the last commit · headcloudmonkey/esp32-snippets@2174296 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2174296

Browse files
committed
File i forgot to stage in the last commit
1 parent 4c04ce1 commit 2174296

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

cpp_utils/WiFi.cpp

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ static void setDNSServer(char *ip) {
4141
}
4242
*/
4343

44-
45-
}
46-
*/
47-
4844
/**
4945
* @brief Creates and uses a default event handler
5046
*/
5147
WiFi::WiFi()
52-
: ip("")
53-
, gw("")
54-
, netmask("")
48+
: ip(0)
49+
, gw(0)
50+
, netmask(0)
5551
, wifiEventHandler(nullptr)
5652
{
5753
wifiEventHandler = new WiFiEventHandler();
@@ -86,10 +82,14 @@ void WiFi::addDNSServer(const std::string& ip) {
8682
} // addDNSServer
8783

8884
void WiFi::addDNSServer(const char* ip) {
89-
ip_addr_t dnsserver;
90-
ESP_LOGD(tag, "Setting DNS[%d] to %s", m_dnsCount, ip);
91-
inet_pton(AF_INET, ip, &dnsserver);
92-
::dns_setserver(m_dnsCount, &dnsserver);
85+
ip_addr_t dns_server;
86+
if(inet_pton(AF_INET, ip, &dns_server))
87+
addDNSServer(ip);
88+
} // addDNSServer
89+
90+
void WiFi::addDNSServer(ip_addr_t ip) {
91+
ESP_LOGD(tag, "Setting DNS[%d] to %d.%d.%d.%d", m_dnsCount, ((uint8_t*)(&ip))[0], ((uint8_t*)(&ip))[1], ((uint8_t*)(&ip))[2], ((uint8_t*)(&ip))[3]);
92+
::dns_setserver(m_dnsCount, &ip);
9393
m_dnsCount++;
9494
m_dnsCount %= 2;
9595
} // addDNSServer
@@ -115,10 +115,14 @@ void WiFi::setDNSServer(int numdns, const std::string& ip) {
115115
} // setDNSServer
116116

117117
void WiFi::setDNSServer(int numdns, const char* ip) {
118-
ip_addr_t dnsserver;
119-
ESP_LOGD(tag, "Setting DNS[%d] to %s", numdns, ip);
120-
inet_pton(AF_INET, ip, &dnsserver);
121-
::dns_setserver(numdns, &dnsserver);
118+
ip_addr_t dns_server;
119+
if(inet_pton(AF_INET, ip, &dns_server))
120+
setDNSServer(numdns, dns_server);
121+
} // setDNSServer
122+
123+
void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
124+
ESP_LOGD(tag, "Setting DNS[%d] to %d.%d.%d.%d", m_dnsCount, ((uint8_t*)(&ip))[0], ((uint8_t*)(&ip))[1], ((uint8_t*)(&ip))[2], ((uint8_t*)(&ip))[3]);
125+
::dns_setserver(numdns, &ip);
122126
} // setDNSServer
123127

124128
/**
@@ -133,20 +137,19 @@ void WiFi::setDNSServer(int numdns, const char* ip) {
133137
void WiFi::connectAP(const std::string& ssid, const std::string& password){
134138
::nvs_flash_init();
135139
::tcpip_adapter_init();
136-
if (ip.length() > 0 && gw.length() > 0 && netmask.length() > 0) {
140+
if (ip != 0 && gw != 0 && netmask != 0) {
137141
::tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); // Don't run a DHCP client
142+
138143
tcpip_adapter_ip_info_t ipInfo;
144+
ipInfo.ip.addr = ip;
145+
ipInfo.gw.addr = gw;
146+
ipInfo.netmask.addr = netmask;
139147

140-
inet_pton(AF_INET, ip.data(), &ipInfo.ip);
141-
inet_pton(AF_INET, gw.data(), &ipInfo.gw);
142-
inet_pton(AF_INET, netmask.data(), &ipInfo.netmask);
143148
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
144149
}
145150

146151

147152
ESP_ERROR_CHECK( esp_event_loop_init(wifiEventHandler->getEventHandler(), wifiEventHandler));
148-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
149-
ESP_ERROR_CHECK(::esp_wifi_init(&cfg));
150153
ESP_ERROR_CHECK(::esp_wifi_set_storage(WIFI_STORAGE_RAM));
151154
ESP_ERROR_CHECK(::esp_wifi_set_mode(WIFI_MODE_STA));
152155
wifi_config_t sta_config;
@@ -366,7 +369,8 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
366369

367370

368371
/**
369-
* @brief Set the IP info used when connecting as a station to an external access point.
372+
* @brief Set the IP info and enable DHCP if ip != 0. If called with ip == 0 then DHCP is enabled.
373+
* If called with bad values it will do nothing.
370374
*
371375
* Do not call this method if we are being an access point ourselves.
372376
*
@@ -382,17 +386,43 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
382386
* @return N/A.
383387
*/
384388
void WiFi::setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask) {
389+
setIPInfo(ip.c_str(), gw.c_str(), netmask.c_str());
390+
} // setIPInfo
391+
392+
void WiFi::setIPInfo(const char* ip, const char* gw, const char* netmask) {
393+
uint32_t new_ip;
394+
uint32_t new_gw;
395+
uint32_t new_netmask;
396+
397+
auto success = (bool)inet_pton(AF_INET, ip, &new_ip);
398+
success = success && inet_pton(AF_INET, gw, &new_gw);
399+
success = success && inet_pton(AF_INET, netmask, &new_netmask);
400+
401+
if(!success) {
402+
return;
403+
}
404+
405+
setIPInfo(new_ip, new_gw, new_netmask);
406+
} // setIPInfo
407+
408+
void WiFi::setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask) {
385409
this->ip = ip;
386410
this->gw = gw;
387411
this->netmask = netmask;
388-
} // setIPInfo
389412

390-
void WiFi::setIPInfo(std::string&& ip, std::string&& gw, std::string&& netmask) {
391-
this->ip = std::move(ip);
392-
this->gw = std::move(gw);
393-
this->netmask = std::move(netmask);
394-
} // setIPInfo
413+
if(ip != 0 && gw != 0 && netmask != 0) {
414+
tcpip_adapter_ip_info_t ipInfo;
415+
ipInfo.ip.addr = ip;
416+
ipInfo.gw.addr = gw;
417+
ipInfo.netmask.addr = netmask;
395418

419+
::tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
420+
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
421+
} else {
422+
ip = 0;
423+
::tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
424+
}
425+
}
396426

397427
/**
398428
* @brief Return a string representation of the WiFi access point record.

0 commit comments

Comments
 (0)
0