Skip to content

Event Service

suraj inamdar edited this page Mar 21, 2021 · 2 revisions

Event Service

This service is added to handle wifi and custom events. this service gives event driven idea to use in application.

How to set custom events

we can add custom event handlers as well. currently below default events are available to use. In this event list we can add our custom event and we can fire that event wherever we need. make sure that EVENT_WIFI_MAX event should be last in the list.

/**
* available event names
*/
typedef enum event_name{

  EVENT_WIFI_STA_CONNECTED = EVENT_STAMODE_CONNECTED,
  EVENT_WIFI_STA_DISCONNECTED,
  EVENT_WIFI_STA_AUTHMODE_CHANGE,
  EVENT_WIFI_STA_GOT_IP,
  EVENT_WIFI_STA_DHCP_TIMEOUT,
  EVENT_WIFI_AP_STACONNECTED,
  EVENT_WIFI_AP_STADISCONNECTED,
  EVENT_WIFI_AP_PROBEREQRECVED,
  EVENT_WIFI_OPMODE_CHANGED,
  EVENT_WIFI_AP_DISTRIBUTE_STA_IP,
  EVENT_WIFI_MAX,
} event_name_t;

Event Api's

below are event api's to use, which will register listeners to event provided. execute api is used to fire specific event with user defined arguments.

/**
 * add event listener. added listener will be executed on given event.
 * it return true on successfull addition of listener
 *
 * @param		event_name_t	_event
 * @param		CallBackVoidPointerArgFn	_handler
 * @return  bool
 */
bool EventServiceProvider::add_event_listener( event_name_t _event, CallBackVoidPointerArgFn _handler );

/**
 * here we will execute perticular event listeners with their accepted argument
 *
 * @param		event_name_t	_event
 * @param		void*|nullptr	_arg
 */
void EventServiceProvider::execute_event( event_name_t _event, void* _arg );

Example

below is example which shows how to use event service. here we are adding one listener(event handler callback function) to EVENT_WIFI_STA_CONNECTED event. So whenever wifi connects to any station, below callback function will get called. Here we are expecting to log on wifi connect event.

  __event_service.add_event_listener( EVENT_WIFI_STA_CONNECTED, [&](void*sta_connected) {
      #ifdef EW_SERIAL_LOG
        Logln(F("WiFi connected !"));
      #endif
  } );
Clone this wiki locally