Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFi event handling refactoring #2119

Merged
merged 1 commit into from
Jun 9, 2016
Merged

WiFi event handling refactoring #2119

merged 1 commit into from
Jun 9, 2016

Conversation

igrr
Copy link
Member

@igrr igrr commented Jun 8, 2016

Related: #2026.
This commit changes the API for subscribing to WiFi events.

Currently there are two functions: WiFi.onEvent and WiFi.removeEvent. The first one allows one to listen to WiFi events but doesn't pass along any useful information about the event. Then other one has a bug and doesn't work in some cases.
Proposed changes are as follows:

  • WiFi.onEvent is kept but marked deprecated. It should not be used in new code.

  • WiFi.removeEvent is removed.

  • A bunch of new functions are added:

    WiFiEventHandler onStationModeConnected(std::function<void(const WiFiEventStationModeConnected&)>);
    WiFiEventHandler onStationModeDisconnected(std::function<void(const WiFiEventStationModeDisconnected&)>);
    WiFiEventHandler onStationModeAuthModeChanged(std::function<void(const WiFiEventStationModeAuthModeChanged&)>);
    WiFiEventHandler onStationModeGotIP(std::function<void(const WiFiEventStationModeGotIP&)>);
    WiFiEventHandler onStationModeDHCPTimeout(std::function<void(void)>);
    WiFiEventHandler onSoftAPModeStationConnected(std::function<void(const WiFiEventSoftAPModeStationConnected&)>);
    WiFiEventHandler onSoftAPModeStationDisconnected(std::function<void(const WiFiEventSoftAPModeStationDisconnected&)>);

    These functions take a function object or a function pointer, and return a WiFiEventHandler instance. WiFiEventHandler is an opaque type which represents an active subscription to WiFi events. It helps manage un-subscription from WiFi events by binding it to object lifetime.

Consider the following example

#include <Schedule.h>
#include <ESP8266WiFi.h>
class FooServer {
public:
    FooServer()
    : mServer(5555)
    {
    }

    void begin()
    {
        mConnectHandler = WiFi.onStationModeGotIP(std::bind(&FooServer::onGotIP, this, std::placeholders::_1));
        mDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&FooServer::onDisconnected, this, std::placeholders::_1));      
    }

protected:
    void onGotIP(const WiFiEventStationModeGotIP& event) 
    {
        // the module is connected to WiFi and got IP from DHCP server
        // run startServer function on main loop
        schedule_function(std::bind(&FooServer::startServer, this));
    }

    void onDisconnected(const WiFiEventStationModeDisconnected& event)
    {
        Serial.println("Stopping server");
        mServer.stop();
    }

    void startServer()
    {
        Serial.println("Starting server");
        mServer.begin();
    }

protected:
    IPAddress mAddr;
    WiFiEventHandler mConnectHandler;
    WiFiEventHandler mDisconnectHandler;
    WiFiServer mServer;
};

FooServer fooServer;

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  fooServer.begin();
}

void loop() {
}

If a FooServer instance goes out of scope or is deleted, mConnectHandler and mDisconnectHandler will be deleted as well, automatically cancelling subscription to GotIP and Disconnected events.

@codecov-io
Copy link

Current coverage is 42.59%

Merging #2119 into master will increase coverage by 16.29%

@@             master      #2119   diff @@
==========================================
  Files            19         19          
  Lines          3634       3634          
  Methods         328        328          
  Messages          0          0          
  Branches        585        585          
==========================================
+ Hits            956       1548   +592   
+ Misses         2513       2086   -427   
+ Partials        165          0   -165   

Powered by Codecov. Last updated by 533a600...7009296

@igrr igrr merged commit de166c9 into master Jun 9, 2016
@igrr igrr deleted the wifi_events branch June 9, 2016 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants