WiFi service: implemented getIp()

This commit is contained in:
Ken Van Hoeylandt 2025-06-21 20:25:14 +02:00
parent c3ceda3072
commit 06f9051b52
3 changed files with 35 additions and 0 deletions

View File

@ -113,6 +113,11 @@ void setScanRecords(uint16_t records);
*/ */
void setEnabled(bool enabled); void setEnabled(bool enabled);
/**
* @return the IPv4 address or empty string
*/
std::string getIp();
/** /**
* @brief Connect to a network. Disconnects any existing connection. * @brief Connect to a network. Disconnects any existing connection.
* Returns immediately but runs in the background. Results are through pubsub. * Returns immediately but runs in the background. Results are through pubsub.

View File

@ -1,5 +1,6 @@
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
#include <lwip/esp_netif_net_stack.h>
#include "Tactility/service/wifi/Wifi.h" #include "Tactility/service/wifi/Wifi.h"
#include "Tactility/TactilityHeadless.h" #include "Tactility/TactilityHeadless.h"
@ -72,6 +73,7 @@ public:
}; };
bool pause_auto_connect = false; // Pause when manually disconnecting until manually connecting again 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 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 { RadioState getRadioState() const {
auto lock = dataMutex.asScopedLock(); auto lock = dataMutex.asScopedLock();
@ -231,6 +233,19 @@ void disconnect() {
getMainDispatcher().dispatch([wifi]() { dispatchDisconnectButKeepActive(wifi); }); 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) { void setScanRecords(uint16_t records) {
TT_LOG_I(TAG, "setScanRecords(%d)", records); TT_LOG_I(TAG, "setScanRecords(%d)", records);
auto wifi = wifi_singleton; 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) { } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
TT_LOG_I(TAG, "eventHandler: disconnected"); TT_LOG_I(TAG, "eventHandler: disconnected");
clearIp();
switch (wifi->getRadioState()) { switch (wifi->getRadioState()) {
case RadioState::ConnectionPending: case RadioState::ConnectionPending:
wifi->connection_wait_flags.set(WIFI_FAIL_BIT); 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); kernel::publishSystemEvent(kernel::SystemEvent::NetworkDisconnected);
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
auto* event = static_cast<ip_event_got_ip_t*>(event_data); auto* event = static_cast<ip_event_got_ip_t*>(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)); TT_LOG_I(TAG, "eventHandler: got ip:" IPSTR, IP2STR(&event->ip_info.ip));
if (wifi->getRadioState() == RadioState::ConnectionPending) { if (wifi->getRadioState() == RadioState::ConnectionPending) {
wifi->connection_wait_flags.set(WIFI_CONNECTED_BIT); wifi->connection_wait_flags.set(WIFI_CONNECTED_BIT);
@ -877,6 +894,15 @@ void onAutoConnectTimer() {
} }
} }
std::string getIp() {
auto wifi = std::static_pointer_cast<Wifi>(wifi_singleton);
auto lock = wifi->dataMutex.asScopedLock();
lock.lock();
return std::format("{}.{}.{}.{}", IP2STR(&wifi->ip_info.ip));
}
class WifiService final : public Service { class WifiService final : public Service {
public: public:

View File

@ -135,6 +135,10 @@ int getRssi() {
} }
} }
std::string getIp() {
return "192.168.1.2"
}
// endregion Public functions // endregion Public functions
class WifiService final : public Service { class WifiService final : public Service {