SensESP 3.3.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#else
10#include <WiFi.h>
11#endif
12
13namespace sensesp {
14
15namespace {
16
27void defer_emit(NetworkStateProducer* producer, NetworkState state) {
28 event_loop()->onDelay(0, [producer, state]() { producer->emit(state); });
29}
30
31} // namespace
32
35
36#if ESP_ARDUINO_VERSION_MAJOR >= 3
37 // ----- Arduino-ESP32 3.x: unified Network event bus ---------------------
38 wifi_got_ip_handle_ = Network.onEvent(
39 [this](arduino_event_id_t, arduino_event_info_t) {
40 ESP_LOGI("net_state", "WiFi station got IP");
41 defer_emit(this, kNetworkConnected);
42 },
43 ARDUINO_EVENT_WIFI_STA_GOT_IP);
44
45 wifi_disconnected_handle_ = Network.onEvent(
46 [this](arduino_event_id_t, arduino_event_info_t) {
47 ESP_LOGI("net_state", "WiFi station disconnected");
48 defer_emit(this, kNetworkDisconnected);
49 },
50 ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
51
52 wifi_ap_start_handle_ = Network.onEvent(
53 [this](arduino_event_id_t, arduino_event_info_t) {
54 ESP_LOGI("net_state", "WiFi soft-AP started");
55 defer_emit(this, kNetworkAPMode);
56 },
57 ARDUINO_EVENT_WIFI_AP_START);
58
59 wifi_ap_stop_handle_ = Network.onEvent(
60 [this](arduino_event_id_t, arduino_event_info_t) {
61 ESP_LOGI("net_state", "WiFi soft-AP stopped");
62 defer_emit(this, kNetworkDisconnected);
63 },
64 ARDUINO_EVENT_WIFI_AP_STOP);
65
66 // ----- Ethernet events (3.x only) --------------------------------------
67 eth_got_ip_handle_ = Network.onEvent(
68 [this](arduino_event_id_t, arduino_event_info_t) {
69 ESP_LOGI("net_state", "Ethernet got IP");
70 defer_emit(this, kNetworkConnected);
71 },
72 ARDUINO_EVENT_ETH_GOT_IP);
73
74 eth_lost_ip_handle_ = Network.onEvent(
75 [this](arduino_event_id_t, arduino_event_info_t) {
76 ESP_LOGI("net_state", "Ethernet lost IP");
77 defer_emit(this, kNetworkDisconnected);
78 },
79 ARDUINO_EVENT_ETH_LOST_IP);
80
81 eth_disconnected_handle_ = Network.onEvent(
82 [this](arduino_event_id_t, arduino_event_info_t) {
83 ESP_LOGI("net_state", "Ethernet PHY disconnected");
84 defer_emit(this, kNetworkDisconnected);
85 },
86 ARDUINO_EVENT_ETH_DISCONNECTED);
87
88#else
89 // ----- Arduino-ESP32 2.x: WiFi-only event bus --------------------------
90 wifi_got_ip_handle_ = WiFi.onEvent(
91 [this](WiFiEvent_t, WiFiEventInfo_t) {
92 ESP_LOGI("net_state", "WiFi station got IP");
93 defer_emit(this, kNetworkConnected);
94 },
95 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
96
97 wifi_disconnected_handle_ = WiFi.onEvent(
98 [this](WiFiEvent_t, WiFiEventInfo_t) {
99 ESP_LOGI("net_state", "WiFi station disconnected");
100 defer_emit(this, kNetworkDisconnected);
101 },
102 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
103
104 wifi_ap_start_handle_ = WiFi.onEvent(
105 [this](WiFiEvent_t, WiFiEventInfo_t) {
106 ESP_LOGI("net_state", "WiFi soft-AP started");
107 defer_emit(this, kNetworkAPMode);
108 },
109 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_START);
110
111 wifi_ap_stop_handle_ = WiFi.onEvent(
112 [this](WiFiEvent_t, WiFiEventInfo_t) {
113 ESP_LOGI("net_state", "WiFi soft-AP stopped");
114 defer_emit(this, kNetworkDisconnected);
115 },
116 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STOP);
117
118 // Ethernet events not available on Arduino-ESP32 2.x; eth_*_handle_
119 // members remain zero-initialized.
120#endif
121
122 // Re-emit the current (initial) state once the event loop is running so
123 // late subscribers (added in SensESPApp::setup) see the latest value.
124 event_loop()->onDelay(0, [this]() { this->emit(this->output_); });
125}
126
128#if ESP_ARDUINO_VERSION_MAJOR >= 3
129 Network.removeEvent(wifi_got_ip_handle_);
130 Network.removeEvent(wifi_disconnected_handle_);
131 Network.removeEvent(wifi_ap_start_handle_);
132 Network.removeEvent(wifi_ap_stop_handle_);
133 Network.removeEvent(eth_got_ip_handle_);
134 Network.removeEvent(eth_lost_ip_handle_);
135 Network.removeEvent(eth_disconnected_handle_);
136#else
137 WiFi.removeEvent(wifi_got_ip_handle_);
138 WiFi.removeEvent(wifi_disconnected_handle_);
139 WiFi.removeEvent(wifi_ap_start_handle_);
140 WiFi.removeEvent(wifi_ap_stop_handle_);
141#endif
142}
143
144} // 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