10000 Fixes, optimizations and additions to WiFi by Eloquence4 · Pull Request #86 · nkolban/esp32-snippets · GitHub
[go: up one dir, main page]

Skip to content

Fixes, optimizations and additions to WiFi #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to 8000 load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 70 additions & 26 deletions cpp_utils/WiFi.cpp
10000
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ static void setDNSServer(char *ip) {
}
*/


WiFi::WiFi() {
ip = "";
gw = "";
netmask = "";
/**
* @brief Creates and uses a default event handler
*/
WiFi::WiFi()
: ip(0)
, gw(0)
, netmask(0)
, wifiEventHandler(nullptr)
{
wifiEventHandler = new WiFiEventHandler();
}

/**
* @brief Deletes the event handler that was used by the class
*/
WiFi::~WiFi() {
delete wifiEventHandler;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check that wifiEventHandler isn't null before deleting?

Copy link
Contributor Author
@Eloquence4 Eloquence4 Sep 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, C++ does that automatically. If it's null, then delete just does nothing, so the if statement is irrelevant.

}

/**
* @brief Add a reference to a DNS server.
Expand All @@ -72,10 +82,14 @@ void WiFi::addDNSServer(const std::string& ip) {
} // addDNSServer

void WiFi::addDNSServer(const char* ip) {
ip_addr_t dnsserver;
ESP_LOGD(tag, "Setting DNS[%d] to %s", m_dnsCount, ip);
inet_pton(AF_INET, ip, &dnsserver);
::dns_setserver(m_dnsCount, &dnsserver);
ip_addr_t dns_server;
if(inet_pton(AF_INET, ip, &dns_server))
addDNSServer(ip);
} // addDNSServer

void WiFi::addDNSServer(ip_addr_t ip) {
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]);
::dns_setserver(m_dnsCount, &ip);
m_dnsCount++;
m_dnsCount %= 2;
} // addDNSServer
Expand All @@ -101,10 +115,14 @@ void WiFi::setDNSServer(int numdns, const std::string& ip) {
} // setDNSServer

void WiFi::setDNSServer(int numdns, const char* ip) {
ip_addr_t dnsserver;
ESP_LOGD(tag, "Setting DNS[%d] to %s", numdns, ip);
inet_pton(AF_INET, ip, &dnsserver);
::dns_setserver(numdns, &dnsserver);
ip_addr_t dns_server;
if(inet_pton(AF_INET, ip, &dns_server))
setDNSServer(numdns, dns_server);
} // setDNSServer

void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
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]);
::dns_setserver(numdns, &ip);
} // setDNSServer

/**
Expand All @@ -119,20 +137,19 @@ void WiFi::setDNSServer(int numdns, const char* ip) {
void WiFi::connectAP(const std::string& ssid, const std::string& password){
::nvs_flash_init();
::tcpip_adapter_init();
if (ip.length() > 0 && gw.length() > 0 && netmask.length() > 0) {
if (ip != 0 && gw != 0 && netmask != 0) {
::tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA); // Don't run a DHCP client

tcpip_adapter_ip_info_t ipInfo;
ipInfo.ip.addr = ip;
ipInfo.gw.addr = gw;
ipInfo.netmask.addr = netmask;

inet_pton(AF_INET, ip.data(), &ipInfo.ip);
inet_pton(AF_INET, gw.data(), &ipInfo.gw);
inet_pton(AF_INET, netmask.data(), &ipInfo.netmask);
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
}


ESP_ERROR_CHECK( esp_event_loop_init(wifiEventHandler->getEventHandler(), wifiEventHandler));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(::esp_wifi_init(&cfg));
ESP_ERROR_CHECK(::esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(::esp_wifi_set_mode(WIFI_MODE_STA));
wifi_config_t sta_config;
Expand Down Expand Up @@ -352,7 +369,8 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {


/**
* @brief Set the IP info used when connecting as a station to an external access point.
* @brief Set the IP info and enable DHCP if ip != 0. If called with ip == 0 then DHCP is enabled.
* If called with bad values it will do nothing.
*
* Do not call this method if we are being an access point ourselves.
*
Expand All @@ -368,17 +386,43 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
* @return N/A.
*/
void WiFi::setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask) {
setIPInfo(ip.c_str(), gw.c_str(), netmask.c_str());
} // setIPInfo

void WiFi::setIPInfo(const char* ip, const char* gw, const char* netmask) {
uint32_t new_ip;
uint32_t new_gw;
uint32_t new_netmask;

auto success = (bool)inet_pton(AF_INET, ip, &new_ip);
success = success && inet_pton(AF_INET, gw, &new_gw);
success = success && inet_pton(AF_INET, netmask, &new_netmask);

if(!success) {
return;
}

setIPInfo(new_ip, new_gw, new_netmask);
} // setIPInfo

void WiFi::setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask) {
this->ip = ip;
this->gw = gw;
this->netmask = netmask;
} // setIPInfo

void WiFi::setIPInfo(std::string&& ip, std::string&& gw, std::string&& netmask) {
this->ip = std::move(ip);
this->gw = std::move(gw);
this->netmask = std::move(netmask);
} // setIPInfo
if(ip != 0 && gw != 0 && netmask != 0) {
tcpip_adapter_ip_info_t ipInfo;
ipInfo.ip.addr = ip;
ipInfo.gw.addr = gw;
ipInfo.netmask.addr = netmask;

::tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
} else {
ip = 0;
::tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA);
}
}

/**
* @brief Return a string representation of the WiFi access point record.
Expand Down
17 changes: 9 additions & 8 deletions cpp_utils/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,20 @@ class WiFiAPRecord {
*/
class WiFi {
private:
std::string ip;
std::string gw;
std::string netmask;
uint32_t ip;
uint32_t gw;
uint32_t netmask;
WiFiEventHandler *wifiEventHandler;

public:
WiFi();
~WiFi();
void addDNSServer(const std::string& ip);
void addDNSServer(const char* ip);
void addDNSServer(ip_addr_t ip);
void setDNSServer(int numdns, const std::string& ip);
void setDNSServer(int numdns, const char* ip);
void setDNSServer(int numdns, ip_addr_t ip);
struct in_addr getHostByName(const std::string& hostName);
struct in_addr getHostByName(const char* hostName);
void connectAP(const std::string& ssid, const std::string& password);
Expand All @@ -128,17 +131,15 @@ class WiFi {
std::vector<WiFiAPRecord> scan();
void startAP(const std::string& ssid, const std::string& passwd);
void setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask);
void setIPInfo(std::string&& ip, std::string&& gw, std::string&& netmask);




void setIPInfo(const char* ip, const char* gw, const char* netmask);
void setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask);

/**
* Set the event handler to use to process detected events.
* @param[in] wifiEventHandler The class that will be used to process events.
*/
void setWifiEventHandler(WiFiEventHandler *wifiEventHandler) {
delete this->wifiEventHandler;
this->wifiEventHandler = wifiEventHandler;
}
private:
Expand Down
132 changes: 65 additions & 67 deletions cpp_utils/WiFiEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,49 @@ static char tag[] = "WiFiEventHandler";
* @return ESP_OK if the event was handled otherwise an error.
*/
esp_err_t WiFiEventHandler::eventHandler(void *ctx, system_event_t *event) {
ESP_LOGD(tag, "eventHandler called");
WiFiEventHandler *pWiFiEventHandler = (WiFiEventHandler *)ctx;
if (ctx == nullptr) {
ESP_LOGD(tag, "No context");
return ESP_OK;
}
esp_err_t rc = ESP_OK;
switch(event->event_id) {

case SYSTEM_EVENT_AP_START:
rc = pWiFiEventHandler->apStart();
break;
case SYSTEM_EVENT_AP_STOP:
rc = pWiFiEventHandler->apStop();
break;
case SYSTEM_EVENT_STA_CONNECTED:
rc = pWiFiEventHandler->staConnected();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
rc = pWiFiEventHandler->staDisconnected();
break;
case SYSTEM_EVENT_STA_GOT_IP:
rc = pWiFiEventHandler->staGotIp(event->event_info.got_ip);
break;
case SYSTEM_EVENT_STA_START:
rc = pWiFiEventHandler->staStart();
break;
case SYSTEM_EVENT_STA_STOP:
rc = pWiFiEventHandler->staStop();
break;
case SYSTEM_EVENT_WIFI_READY:
rc = pWiFiEventHandler->wifiReady();
break;
default:
break;
}
if (pWiFiEventHandler->nextHandler != nullptr) {
printf("Found a next handler\n");
rc = eventHandler(pWiFiEventHandler->nextHandler, event);
} else {
//printf("NOT Found a next handler\n");
}
return rc;
ESP_LOGD(tag, "eventHandler called");
WiFiEventHandler *pWiFiEventHandler = (WiFiEventHandler *)ctx;
if (ctx == nullptr) {
ESP_LOGD(tag, "No context");
return ESP_OK;
}
esp_err_t rc = ESP_OK;
switch(event->event_id) {

case SYSTEM_EVENT_AP_START:
rc = pWiFiEventHandler->apStart();
break;
case SYSTEM_EVENT_AP_STOP:
rc = pWiFiEventHandler->apStop();
break;
case SYSTEM_EVENT_STA_CONNECTED:
rc = pWiFiEventHandler->staConnected();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
rc = pWiFiEventHandler->staDisconnected();
break;
case SYSTEM_EVENT_STA_GOT_IP:
rc = pWiFiEventHandler->staGotIp(event->event_info.got_ip);
break;
case SYSTEM_EVENT_STA_START:
rc = pWiFiEventHandler->staStart();
break;
case SYSTEM_EVENT_STA_STOP:
rc = pWiFiEventHandler->staStop();
break;
case SYSTEM_EVENT_WIFI_READY:
rc = pWiFiEventHandler->wifiReady();
break;
default:
break;
}
if (pWiFiEventHandler->nextHandler != nullptr) {
printf("Found a next handler\n");
rc = eventHandler(pWiFiEventHandler->nextHandler, event);
} else {
//printf("NOT Found a next handler\n");
}
return rc;
}

WiFiEventHandler::WiFiEventHandler() {
Expand All @@ -79,7 +79,7 @@ WiFiEventHandler::WiFiEventHandler() {
* @return The event handler function.
*/
system_event_cb_t WiFiEventHandler::getEventHandler() {
return eventHandler;
return eventHandler;
} // getEventHandler


Expand All @@ -90,8 +90,8 @@ system_event_cb_t WiFiEventHandler::getEventHandler() {
* @return An indication of whether or not we processed the event successfully.
*/
esp_err_t WiFiEventHandler::staGotIp(system_event_sta_got_ip_t event_sta_got_ip) {
ESP_LOGD(tag, "default staGotIp");
return ESP_OK;
ESP_LOGD(tag, "default staGotIp");
return ESP_OK;
} // staGotIp

/**
Expand All @@ -100,8 +100,8 @@ esp_err_t WiFiEventHandler::staGotIp(system_event_sta_got_ip_t event_sta_got_ip)
* @return An indication of whether or not we processed the event successfully.
*/
esp_err_t WiFiEventHandler::apStart() {
ESP_LOGD(tag, "default apStart");
return ESP_OK;
ESP_LOGD(tag, "default apStart");
return ESP_OK;
} // apStart

/**
Expand All @@ -110,47 +110,45 @@ esp_err_t WiFiEventHandler::apStart() {
* @return An indication of whether or not we processed the event successfully.
*/
esp_err_t WiFiEventHandler::apStop() {
ESP_LOGD(tag, "default apStop");
return ESP_OK;
ESP_LOGD(tag, "default apStop");
return ESP_OK;
} // apStop

esp_err_t WiFiEventHandler::wifiReady() {
ESP_LOGD(tag, "default wifiReady");
return ESP_OK;
ESP_LOGD(tag, "default wifiReady");
return ESP_OK;
} // wifiReady

esp_err_t WiFiEventHandler::staStart() {
ESP_LOGD(tag, "default staStart");
return ESP_OK;
ESP_LOGD(tag, "default staStart");
return ESP_OK;
} // staStart

esp_err_t WiFiEventHandler::staStop() {
ESP_LOGD(tag, "default staStop");
return ESP_OK;
ESP_LOGD(tag, "default staStop");
return ESP_OK;
} // staStop

esp_err_t WiFiEventHandler::staConnected() {
ESP_LOGD(tag, "default staConnected");
return ESP_OK;
ESP_LOGD(tag, "default staConnected");
return ESP_OK;
} // staConnected

esp_err_t WiFiEventHandler::staDisconnected() {
ESP_LOGD(tag, "default staDisconnected");
return ESP_OK;
ESP_LOGD(tag, "default staDisconnected");
return ESP_OK;
} // staDisconnected

esp_err_t WiFiEventHandler::apStaConnected() {
ESP_LOGD(tag, "default apStaConnected");
return ESP_OK;
ESP_LOGD(tag, "default apStaConnected");
return ESP_OK;
} // apStaConnected

esp_err_t WiFiEventHandler::apStaDisconnected() {
ESP_LOGD(tag, "default apStaDisconnected");
return ESP_OK;
ESP_LOGD(tag, "default apStaDisconnected");
return ESP_OK;
} // apStaDisconnected

WiFiEventHandler::~WiFiEventHandler() {
if (nextHandler != nullptr) {
delete nextHandler;
}
delete nextHandler;
} // ~WiFiEventHandler
Loading
0