Skip to content

Commit 2064d43

Browse files
authored
ESP8266WiFi - document event handler lifetime, add [[nodiscard]] (#9087)
* disallow not assigning wifieventhandler somewhere * fix markdown syntax in rst * mention lifetime in example and docs
1 parent b0d9e75 commit 2064d43

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

doc/esp8266wifi/generic-examples.rst

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,29 @@ Register the Events
3838

3939
To get events to work we need to complete just two steps:
4040

41-
1. Declare the event handler:
41+
1. Declare the event handler in global scope.
4242

43-
``cpp WiFiEventHandler disconnectedEventHandler;``
43+
.. code:: cpp
44+
WiFiEventHandler disconnectedEventHandler;
45+
46+
Alternatively, it can be declared as ``static`` in both function and global scopes.
4447

45-
2. Select particular event (in this case ``onStationModeDisconnected``)
46-
and add the code to be executed when event is fired.
4748

48-
``cpp disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) { Serial.println("Station disconnected"); });`` If this event is fired the code will print out information that station has been disconnected.
49+
2. Select particular event (in this case ``onStationModeDisconnected``).
50+
When this event is fired the code will print out information that station has been disconnected:
51+
52+
.. code:: cpp
53+
disconnectedEventHandler = WiFi.onStationModeDisconnected(
54+
[](auto&& event) {
55+
Serial.println("Station disconnected");
56+
});
57+
58+
3. Disable ``disconnectedEventHandler``, so the event is no longer handled by our callback:
59+
60+
.. code:: cpp
61+
disconnectedEventHandler = nullptr;
4962
50-
That's it. It is all we need to do.
63+
Take note that lifetime of the callback handler is up to the app. e.g. if ``onStationModeDisconnected`` is declared in the function scope, it would be discarded immediately after the function exists.
5164

5265
The Code
5366
~~~~~~~~

doc/esp8266wifi/soft-access-point-examples.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ Sketch is small so analysis shouldn't be difficult. In first line we are includi
7979
8080
Setting up of the access point ``ESPsoftAP_01`` is done by executing:
8181

82-
``cpp boolean result = WiFi.softAP("ESPsoftAP_01", "pass-to-soft-AP");``
82+
.. code:: cpp
83+
84+
boolean result = WiFi.softAP("ESPsoftAP_01", "pass-to-soft-AP");
8385
8486
If this operation is successful then ``result`` will be ``true`` or ``false`` if otherwise. Basing on that either ``Ready`` or ``Failed!`` will be printed out by the following ``if - else`` conditional statement.
8587

libraries/ESP8266WiFi/examples/WiFiEvents/WiFiEvents.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
const char* ssid = APSSID;
2525
const char* password = APPSK;
2626

27+
// WiFi.on* methods **must** only be called **after** entering setup().
28+
// Assigning immediately in global scope is not adviced, neither is assigning them within any other object constructors.
29+
// These variables **may** exist in function block, but **only** if they are declared as `static`
2730
WiFiEventHandler stationConnectedHandler;
2831
WiFiEventHandler stationDisconnectedHandler;
2932
WiFiEventHandler probeRequestPrintHandler;
@@ -43,12 +46,12 @@ void setup() {
4346
WiFi.mode(WIFI_AP);
4447
WiFi.softAP(ssid, password);
4548

46-
// Register event handlers.
47-
// Callback functions will be called as long as these handler objects exist.
4849
// Call "onStationConnected" each time a station connects
4950
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
51+
5052
// Call "onStationDisconnected" each time a station disconnects
5153
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
54+
5255
// Call "onProbeRequestPrint" and "onProbeRequestBlink" each time
5356
// a probe request is received.
5457
// Former will print MAC address of the station and RSSI to Serial,

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ class ESP8266WiFiGenericClass {
7171
void onEvent(WiFiEventCb cb, WiFiEvent_t event = WIFI_EVENT_ANY) __attribute__((deprecated));
7272

7373
// Subscribe to specific event and get event information as an argument to the callback
74-
WiFiEventHandler onStationModeConnected(std::function<void(const WiFiEventStationModeConnected&)>);
75-
WiFiEventHandler onStationModeDisconnected(std::function<void(const WiFiEventStationModeDisconnected&)>);
76-
WiFiEventHandler onStationModeAuthModeChanged(std::function<void(const WiFiEventStationModeAuthModeChanged&)>);
77-
WiFiEventHandler onStationModeGotIP(std::function<void(const WiFiEventStationModeGotIP&)>);
78-
WiFiEventHandler onStationModeDHCPTimeout(std::function<void(void)>);
79-
WiFiEventHandler onSoftAPModeStationConnected(std::function<void(const WiFiEventSoftAPModeStationConnected&)>);
80-
WiFiEventHandler onSoftAPModeStationDisconnected(std::function<void(const WiFiEventSoftAPModeStationDisconnected&)>);
81-
WiFiEventHandler onSoftAPModeProbeRequestReceived(std::function<void(const WiFiEventSoftAPModeProbeRequestReceived&)>);
82-
WiFiEventHandler onWiFiModeChange(std::function<void(const WiFiEventModeChange&)>);
74+
[[nodiscard]] WiFiEventHandler onStationModeConnected(std::function<void(const WiFiEventStationModeConnected&)>);
75+
[[nodiscard]] WiFiEventHandler onStationModeDisconnected(std::function<void(const WiFiEventStationModeDisconnected&)>);
76+
[[nodiscard]] WiFiEventHandler onStationModeAuthModeChanged(std::function<void(const WiFiEventStationModeAuthModeChanged&)>);
77+
[[nodiscard]] WiFiEventHandler onStationModeGotIP(std::function<void(const WiFiEventStationModeGotIP&)>);
78+
[[nodiscard]] WiFiEventHandler onStationModeDHCPTimeout(std::function<void(void)>);
79+
[[nodiscard]] WiFiEventHandler onSoftAPModeStationConnected(std::function<void(const WiFiEventSoftAPModeStationConnected&)>);
80+
[[nodiscard]] WiFiEventHandler onSoftAPModeStationDisconnected(std::function<void(const WiFiEventSoftAPModeStationDisconnected&)>);
81+
[[nodiscard]] WiFiEventHandler onSoftAPModeProbeRequestReceived(std::function<void(const WiFiEventSoftAPModeProbeRequestReceived&)>);
82+
[[nodiscard]] WiFiEventHandler onWiFiModeChange(std::function<void(const WiFiEventModeChange&)>);
8383

8484
uint8_t channel(void);
8585

0 commit comments

Comments
 (0)