diff --git a/cpp_utils/WiFi.cpp b/cpp_utils/WiFi.cpp index 38ac9811..d3c44a9c 100644 --- a/cpp_utils/WiFi.cpp +++ b/cpp_utils/WiFi.cpp @@ -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; +} /** * @brief Add a reference to a DNS server. @@ -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 @@ -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 /** @@ -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; @@ -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. * @@ -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. diff --git a/cpp_utils/WiFi.h b/cpp_utils/WiFi.h index e525ce5d..b7499c48 100644 --- a/cpp_utils/WiFi.h +++ b/cpp_utils/WiFi.h @@ -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); @@ -128,17 +131,15 @@ class WiFi { std::vector 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: diff --git a/cpp_utils/WiFiEventHandler.cpp b/cpp_utils/WiFiEventHandler.cpp index 1c454d54..59519ed4 100644 --- a/cpp_utils/WiFiEventHandler.cpp +++ b/cpp_utils/WiFiEventHandler.cpp @@ -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() { @@ -79,7 +79,7 @@ WiFiEventHandler::WiFiEventHandler() { * @return The event handler function. */ system_event_cb_t WiFiEventHandler::getEventHandler() { - return eventHandler; + return eventHandler; } // getEventHandler @@ -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 /** @@ -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 /** @@ -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 diff --git a/cpp_utils/WiFiEventHandler.h b/cpp_utils/WiFiEventHandler.h index 9bfb7e19..0596e92c 100644 --- a/cpp_utils/WiFiEventHandler.h +++ b/cpp_utils/WiFiEventHandler.h @@ -79,39 +79,39 @@ */ class WiFiEventHandler { public: - WiFiEventHandler(); - virtual ~WiFiEventHandler(); - system_event_cb_t getEventHandler(); - virtual esp_err_t apStaConnected(); - virtual esp_err_t apStaDisconnected(); - virtual esp_err_t apStart(); - virtual esp_err_t apStop(); - virtual esp_err_t staConnected(); - virtual esp_err_t staDisconnected(); - virtual esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip); - virtual esp_err_t staStart(); - virtual esp_err_t staStop(); - virtual esp_err_t wifiReady(); + WiFiEventHandler(); + virtual ~WiFiEventHandler(); + system_event_cb_t getEventHandler(); + virtual esp_err_t apStaConnected(); + virtual esp_err_t apStaDisconnected(); + virtual esp_err_t apStart(); + virtual esp_err_t apStop(); + virtual esp_err_t staConnected(); + virtual esp_err_t staDisconnected(); + virtual esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip); + virtual esp_err_t staStart(); + virtual esp_err_t staStop(); + virtual esp_err_t wifiReady(); - /** - * Get the next WiFi event handler in the chain, if there is one. - * @return The next WiFi event handler in the chain or nullptr if there is none. - */ - WiFiEventHandler *getNextHandler() { - return nextHandler; - } + /** + * Get the next WiFi event handler in the chain, if there is one. + * @return The next WiFi event handler in the chain or nullptr if there is none. + */ + WiFiEventHandler *getNextHandler() { + return nextHandler; + } - /** - * Set the next WiFi event handler in the chain. - * @param [in] nextHandler The next WiFi event handler in the chain. - */ - void setNextHandler(WiFiEventHandler* nextHandler) { - this->nextHandler = nextHandler; - } + /** + * Set the next WiFi event handler in the chain. + * @param [in] nextHandler The next WiFi event handler in the chain. + */ + void setNextHandler(WiFiEventHandler* nextHandler) { + this->nextHandler = nextHandler; + } private: - WiFiEventHandler *nextHandler = nullptr; - static esp_err_t eventHandler(void *ctx, system_event_t *event); + WiFiEventHandler *nextHandler = nullptr; + static esp_err_t eventHandler(void *ctx, system_event_t *event); }; #endif /* MAIN_WIFIEVENTHANDLER_H_ */