diff --git a/Tactility/Include/Tactility/service/wifi/Wifi.h b/Tactility/Include/Tactility/service/wifi/Wifi.h index a43e7799..b41ad0a8 100644 --- a/Tactility/Include/Tactility/service/wifi/Wifi.h +++ b/Tactility/Include/Tactility/service/wifi/Wifi.h @@ -113,6 +113,11 @@ void setScanRecords(uint16_t records); */ void setEnabled(bool enabled); +/** + * @return the IPv4 address or empty string + */ +std::string getIp(); + /** * @brief Connect to a network. Disconnects any existing connection. * Returns immediately but runs in the background. Results are through pubsub. diff --git a/Tactility/Source/service/wifi/WifiEsp.cpp b/Tactility/Source/service/wifi/WifiEsp.cpp index 959ae1aa..8fa75838 100644 --- a/Tactility/Source/service/wifi/WifiEsp.cpp +++ b/Tactility/Source/service/wifi/WifiEsp.cpp @@ -1,5 +1,6 @@ #ifdef ESP_PLATFORM +#include #include "Tactility/service/wifi/Wifi.h" #include "Tactility/TactilityHeadless.h" @@ -72,6 +73,7 @@ public: }; bool pause_auto_connect = false; // Pause when manually disconnecting until manually connecting again bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not + esp_netif_ip_info_t ip_info; RadioState getRadioState() const { auto lock = dataMutex.asScopedLock(); @@ -231,6 +233,19 @@ void disconnect() { getMainDispatcher().dispatch([wifi]() { dispatchDisconnectButKeepActive(wifi); }); } +void clearIp() { + auto wifi = wifi_singleton; + if (wifi == nullptr) { + return; + } + + auto lock = wifi->dataMutex.asScopedLock(); + if (!lock.lock(10 / portTICK_PERIOD_MS)) { + return; + } + + memset(&wifi->ip_info, 0, sizeof(esp_netif_ip_info_t)); +} void setScanRecords(uint16_t records) { TT_LOG_I(TAG, "setScanRecords(%d)", records); auto wifi = wifi_singleton; @@ -464,6 +479,7 @@ static void eventHandler(TT_UNUSED void* arg, esp_event_base_t event_base, int32 } } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { TT_LOG_I(TAG, "eventHandler: disconnected"); + clearIp(); switch (wifi->getRadioState()) { case RadioState::ConnectionPending: wifi->connection_wait_flags.set(WIFI_FAIL_BIT); @@ -480,6 +496,7 @@ static void eventHandler(TT_UNUSED void* arg, esp_event_base_t event_base, int32 kernel::publishSystemEvent(kernel::SystemEvent::NetworkDisconnected); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { auto* event = static_cast(event_data); + memcpy(&wifi->ip_info, &event->ip_info, sizeof(esp_netif_ip_info_t)); TT_LOG_I(TAG, "eventHandler: got ip:" IPSTR, IP2STR(&event->ip_info.ip)); if (wifi->getRadioState() == RadioState::ConnectionPending) { wifi->connection_wait_flags.set(WIFI_CONNECTED_BIT); @@ -877,6 +894,15 @@ void onAutoConnectTimer() { } } +std::string getIp() { + auto wifi = std::static_pointer_cast(wifi_singleton); + + auto lock = wifi->dataMutex.asScopedLock(); + lock.lock(); + + return std::format("{}.{}.{}.{}", IP2STR(&wifi->ip_info.ip)); +} + class WifiService final : public Service { public: diff --git a/Tactility/Source/service/wifi/WifiMock.cpp b/Tactility/Source/service/wifi/WifiMock.cpp index af84a9e2..a019a110 100644 --- a/Tactility/Source/service/wifi/WifiMock.cpp +++ b/Tactility/Source/service/wifi/WifiMock.cpp @@ -135,6 +135,10 @@ int getRssi() { } } +std::string getIp() { + return "192.168.1.2" +} + // endregion Public functions class WifiService final : public Service {