SensESP 3.4.0
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 defer_emit(this, kNetworkDisconnected);
66 },
67 ARDUINO_EVENT_WIFI_AP_STOP);
68
69 // ----- Ethernet events (3.x only) --------------------------------------
70 eth_got_ip_handle_ = Network.onEvent(
71 [this](arduino_event_id_t, arduino_event_info_t) {
72 ESP_LOGI("net_state", "Ethernet got IP");
73 defer_emit(this, kNetworkConnected);
74 },
75 ARDUINO_EVENT_ETH_GOT_IP);
76
77 eth_lost_ip_handle_ = Network.onEvent(
78 [this](arduino_event_id_t, arduino_event_info_t) {
79 ESP_LOGI("net_state", "Ethernet lost IP");
80 defer_emit(this, kNetworkDisconnected);
81 },
82 ARDUINO_EVENT_ETH_LOST_IP);
83
84 eth_disconnected_handle_ = Network.onEvent(
85 [this](arduino_event_id_t, arduino_event_info_t) {
86 ESP_LOGI("net_state", "Ethernet PHY disconnected");
87 defer_emit(this, kNetworkDisconnected);
88 },
89 ARDUINO_EVENT_ETH_DISCONNECTED);
90
91#else
92 // ----- Arduino-ESP32 2.x: WiFi-only event bus --------------------------
93 wifi_got_ip_handle_ = WiFi.onEvent(
94 [this](WiFiEvent_t, WiFiEventInfo_t) {
95 ESP_LOGI("net_state", "WiFi station got IP %s",
96 WiFi.localIP().toString().c_str());
97 defer_emit(this, kNetworkConnected);
98 },
99 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
100
101 wifi_disconnected_handle_ = WiFi.onEvent(
102 [this](WiFiEvent_t, WiFiEventInfo_t) {
103 ESP_LOGI("net_state", "WiFi station disconnected");
104 defer_emit(this, kNetworkDisconnected);
105 },
106 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
107
108 wifi_ap_start_handle_ = WiFi.onEvent(
109 [this](WiFiEvent_t, WiFiEventInfo_t) {
110 ESP_LOGI("net_state", "WiFi soft-AP started, IP %s",
111 WiFi.softAPIP().toString().c_str());
112 defer_emit(this, kNetworkAPMode);
113 },
114 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_START);
115
116 wifi_ap_stop_handle_ = WiFi.onEvent(
117 [this](WiFiEvent_t, WiFiEventInfo_t) {
118 ESP_LOGI("net_state", "WiFi soft-AP stopped");
119 defer_emit(this, kNetworkDisconnected);
120 },
121 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STOP);
122
123 // Ethernet events not available on Arduino-ESP32 2.x; eth_*_handle_
124 // members remain zero-initialized.
125#endif
126
127 // Re-emit the current (initial) state once the event loop is running so
128 // late subscribers (added in SensESPApp::setup) see the latest value.
129 event_loop()->onDelay(0, [this]() { this->emit(this->output_); });
130}
131
133#if ESP_ARDUINO_VERSION_MAJOR >= 3
134 Network.removeEvent(wifi_got_ip_handle_);
135 Network.removeEvent(wifi_disconnected_handle_);
136 Network.removeEvent(wifi_ap_start_handle_);
137 Network.removeEvent(wifi_ap_stop_handle_);
138 Network.removeEvent(eth_got_ip_handle_);
139 Network.removeEvent(eth_lost_ip_handle_);
140 Network.removeEvent(eth_disconnected_handle_);
141#else
142 WiFi.removeEvent(wifi_got_ip_handle_);
143 WiFi.removeEvent(wifi_disconnected_handle_);
144 WiFi.removeEvent(wifi_ap_start_handle_);
145 WiFi.removeEvent(wifi_ap_stop_handle_);
146#endif
147}
148
149} // namespace sensesp
Unified producer that emits NetworkState transitions.
void emit(const NetworkState &new_value)
constexpr NetworkState kNetworkConnected
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
constexpr NetworkState kNetworkDisconnected
constexpr NetworkState kNetworkNoConnection
WiFiState NetworkState
Transport-agnostic network state.
constexpr NetworkState kNetworkAPMode