SensESP 3.6.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
network_state.cpp
Go to the documentation of this file.
1#include "sensesp.h"
2
3#include "network_state.h"
4
5#include "esp_arduino_version.h"
6
7#if ESP_ARDUINO_VERSION_MAJOR >= 3
8#include <Network.h>
9#include <WiFi.h>
10#else
11#include <WiFi.h>
12#endif
13
14namespace sensesp {
15
16namespace {
17
28void defer_emit(NetworkStateProducer* producer, NetworkState state) {
29 event_loop()->onDelay(0, [producer, state]() { producer->emit(state); });
30}
31
32} // namespace
33
36
37#if ESP_ARDUINO_VERSION_MAJOR >= 3
38 // ----- Arduino-ESP32 3.x: unified Network event bus ---------------------
39 wifi_got_ip_handle_ = Network.onEvent(
40 [this](arduino_event_id_t, arduino_event_info_t) {
41 ESP_LOGI("net_state", "WiFi station got IP %s",
42 WiFi.localIP().toString().c_str());
43 defer_emit(this, kNetworkConnected);
44 },
45 ARDUINO_EVENT_WIFI_STA_GOT_IP);
46
47 wifi_disconnected_handle_ = Network.onEvent(
48 [this](arduino_event_id_t, arduino_event_info_t) {
49 ESP_LOGI("net_state", "WiFi station disconnected");
50 defer_emit(this, kNetworkDisconnected);
51 },
52 ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
53
54 wifi_ap_start_handle_ = Network.onEvent(
55 [this](arduino_event_id_t, arduino_event_info_t) {
56 ESP_LOGI("net_state", "WiFi soft-AP started, IP %s",
57 WiFi.softAPIP().toString().c_str());
58 defer_emit(this, kNetworkAPMode);
59 },
60 ARDUINO_EVENT_WIFI_AP_START);
61
62 wifi_ap_stop_handle_ = Network.onEvent(
63 [this](arduino_event_id_t, arduino_event_info_t) {
64 ESP_LOGI("net_state", "WiFi soft-AP stopped");
65 // Stopping the soft-AP is not an outage while the station is up.
66 if (!WiFi.isConnected()) {
67 defer_emit(this, kNetworkDisconnected);
68 }
69 },
70 ARDUINO_EVENT_WIFI_AP_STOP);
71
72 // ----- Ethernet events (3.x only) --------------------------------------
73 eth_got_ip_handle_ = Network.onEvent(
74 [this](arduino_event_id_t, arduino_event_info_t) {
75 ESP_LOGI("net_state", "Ethernet got IP");
76 defer_emit(this, kNetworkConnected);
77 },
78 ARDUINO_EVENT_ETH_GOT_IP);
79
80 eth_lost_ip_handle_ = Network.onEvent(
81 [this](arduino_event_id_t, arduino_event_info_t) {
82 ESP_LOGI("net_state", "Ethernet lost IP");
83 defer_emit(this, kNetworkDisconnected);
84 },
85 ARDUINO_EVENT_ETH_LOST_IP);
86
87 eth_disconnected_handle_ = Network.onEvent(
88 [this](arduino_event_id_t, arduino_event_info_t) {
89 ESP_LOGI("net_state", "Ethernet PHY disconnected");
90 defer_emit(this, kNetworkDisconnected);
91 },
92 ARDUINO_EVENT_ETH_DISCONNECTED);
93
94#else
95 // ----- Arduino-ESP32 2.x: WiFi-only event bus --------------------------
96 wifi_got_ip_handle_ = WiFi.onEvent(
97 [this](WiFiEvent_t, WiFiEventInfo_t) {
98 ESP_LOGI("net_state", "WiFi station got IP %s",
99 WiFi.localIP().toString().c_str());
100 defer_emit(this, kNetworkConnected);
101 },
102 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
103
104 wifi_disconnected_handle_ = WiFi.onEvent(
105 [this](WiFiEvent_t, WiFiEventInfo_t) {
106 ESP_LOGI("net_state", "WiFi station disconnected");
107 defer_emit(this, kNetworkDisconnected);
108 },
109 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
110
111 wifi_ap_start_handle_ = WiFi.onEvent(
112 [this](WiFiEvent_t, WiFiEventInfo_t) {
113 ESP_LOGI("net_state", "WiFi soft-AP started, IP %s",
114 WiFi.softAPIP().toString().c_str());
115 defer_emit(this, kNetworkAPMode);
116 },
117 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_START);
118
119 wifi_ap_stop_handle_ = WiFi.onEvent(
120 [this](WiFiEvent_t, WiFiEventInfo_t) {
121 ESP_LOGI("net_state", "WiFi soft-AP stopped");
122 // Stopping the soft-AP is not an outage while the station is up.
123 if (!WiFi.isConnected()) {
124 defer_emit(this, kNetworkDisconnected);
125 }
126 },
127 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STOP);
128
129 // Ethernet events not available on Arduino-ESP32 2.x; eth_*_handle_
130 // members remain zero-initialized.
131#endif
132
133 // Re-emit the current (initial) state once the event loop is running so
134 // late subscribers (added in SensESPApp::setup) see the latest value.
135 event_loop()->onDelay(0, [this]() { this->emit(this->output_); });
136}
137
139#if ESP_ARDUINO_VERSION_MAJOR >= 3
140 Network.removeEvent(wifi_got_ip_handle_);
141 Network.removeEvent(wifi_disconnected_handle_);
142 Network.removeEvent(wifi_ap_start_handle_);
143 Network.removeEvent(wifi_ap_stop_handle_);
144 Network.removeEvent(eth_got_ip_handle_);
145 Network.removeEvent(eth_lost_ip_handle_);
146 Network.removeEvent(eth_disconnected_handle_);
147#else
148 WiFi.removeEvent(wifi_got_ip_handle_);
149 WiFi.removeEvent(wifi_disconnected_handle_);
150 WiFi.removeEvent(wifi_ap_start_handle_);
151 WiFi.removeEvent(wifi_ap_stop_handle_);
152#endif
153}
154
155} // namespace sensesp
void emit(const NetworkState &new_value)
WiFiState NetworkState
Transport-agnostic network state.
constexpr NetworkState kNetworkConnected
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
constexpr NetworkState kNetworkDisconnected
constexpr NetworkState kNetworkNoConnection
constexpr NetworkState kNetworkAPMode