migrating to v4.1 event handlers

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

migrating to v4.1 event handlers

Postby mzimmers » Tue Feb 04, 2020 7:16 pm

Hi all -

I'm assessing the work required to move from v4.0 to v4.1. One area of concern is event handlers. In v4.0, I registered my handler:

Code: Select all

esp_err_t event_handler(void *ctx, system_event_t *event)
{
    Tasks *tasks = reinterpret_cast<Tasks *> (event);

    // disconnect reason code stuff.
    char ssid[32];
    uint8_t ssid_len;
    uint8_t reason;

    switch(event_id)
    {
    case SYSTEM_EVENT_STA_DISCONNECTED:
        reason = event->event_info.disconnected.reason;
        ssid_len = event->event_info.disconnected.ssid_len;
        strncpy(ssid, (char *) event->event_info.disconnected.ssid, ssid_len);
        ...
}
ESP_ERROR_CHECK(esp_event_loop_init((system_event_cb_t) event_handler, m_tasks)); // v4.0 and below.
In v4.1, the event handling doesn't seem to make use of the system_event_t struct. What do I do in order to retrieve information like this?

Thanks...

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: migrating to v4.1 event handlers

Postby ESP_igrr » Tue Feb 04, 2020 8:53 pm

With the new event handlers based on esp_event, you obtain the extra info by casting the event_data pointer. You need to cast it to the structure corresponding to the event type. For instance, see https://github.com/espressif/esp-idf/bl ... main.c#L40

For the WIFI_EVENT_STA_DISCONNECTED event, corresponding event structure is wifi_event_sta_disconnected_t, defined here: https://github.com/espressif/esp-idf/bl ... pes.h#L551

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: migrating to v4.1 event handlers

Postby mzimmers » Tue Feb 04, 2020 9:25 pm

Ahh...thanks, Igrr.

On a related subject, my app isn't progressing out of the Wifi connected state. I'm still using the old SYSTEM_EVENT_STA_* symbols, and it looks like the Wifi state machine might have changed in v4.1. The example you provided doesn't really cover this; is there a doc that explains the new state machine?

EDIT: oh, I get it -- it's not enough to merely check the event ID; you have to check the event base as well, right? OK, let me work with this a little; I'll report back.

Thanks again.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: migrating to v4.1 event handlers

Postby mzimmers » Tue Feb 04, 2020 11:07 pm

I've changed my handler to match my understanding of the v4.1 Wifi/IP events. Here's a snippet:

Code: Select all

static void event_handler(void *ctx,
                          esp_event_base_t event_base,
                          int32_t event_id,
                          void *event_data)
{
    std::string currentTime;
    Tasks *tasks = reinterpret_cast<Tasks *> (ctx);
    time_t now;
    union
    {
        wifi_event_sta_disconnected_t *disc;
        wifi_event_sta_scan_done_t *scan;
        void *pVoid;
    } eventData;

    // get the data pointer from the argument.
    eventData.pVoid = event_data;

    time(&now);
    currentTime.assign(tasks->wifi->getTimeString(now));

    if (event_base == WIFI_EVENT)
    {
        switch(event_id)
        {
        case WIFI_EVENT_STA_CONNECTED:
            ESP_LOGI(TAG, "event_handler(): Wifi connected at %s.", currentTime.c_str());
            tasks->wifi->setCommsState(WIFI_STATE_CONNECTED_TO_AP);
            tasks->wifi->setDisconnectReason(INVALID_DISCONNECT_REASON);
            // reset the connection delay.
            tasks->wifi->setConnectDelay(WIFI_CONNECT_DELAY_DEFAULT);
            break;
		...
	}
    }
    else if (event_base == IP_EVENT)
    {
        switch(event_id)
        {
        //      case SYSTEM_EVENT_STA_GOT_IP:
        case IP_EVENT_STA_GOT_IP:
            ESP_LOGI(TAG, "event_handler(): device received IP address from wifi server at %s.", currentTime.c_str());
            tasks->wifi->setCommsState(WIFI_STATE_IP_ADDRESS_ASSIGNED);
            tasks->wifi->storeIPinfo();
            break;
		...
	}
    }
}

ESP_ERROR_CHECK(esp_event_handler_register(ESP_EVENT_ANY_BASE,
										   ESP_EVENT_ANY_ID,
										   &event_handler,
										   m_tasks)); // v4.1
It all works...I get the WIFI_EVENT_STA_CONNECTED event, but not the IP_EVENT_STA_GOT_IP. Can someone see what I'm missing? Thanks.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: migrating to v4.1 event handlers

Postby ESP_igrr » Wed Feb 05, 2020 7:26 am

Please try enabling verbose log output — there should be logs of all events being delivered to the callbacks.

Also please check that you aren't calling both the legacy event loop functions (esp_event_loop_init) and the new ones. Either the old ones, OR the new ones, should work.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: migrating to v4.1 event handlers

Postby mzimmers » Wed Feb 05, 2020 3:18 pm

Hi Igrr --

Confirmed that I'm not calling esp_event_loop_init().

When I select verbose output, I can't build:

Code: Select all

C:/esp-idf/components/log/include/esp_log.h:281:13: error: 'len' may be used uninitialized in this function [-Werror=maybe-uninitialized]
             ets_printf(LOG_FORMAT(log_tag_letter, format), esp_log_timestamp(), tag, ##__VA_ARGS__); \
             ^~~~~~~~~~
C:/esp-idf/components/soc/src/hal/sdio_slave_hal.c:375:14: note: 'len' was declared here
     uint32_t len;
              ^~~
When I select debug output, I get a panic early in startup (not enough stack displayed to trace).

Thanks...

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: migrating to v4.1 event handlers

Postby mzimmers » Tue Feb 11, 2020 4:26 pm

BTT: anyone have a suggestion here? I'd really like to get this functional.

By the way, I think this document https://docs.espressif.com/projects/esp ... /wifi.html is out of date:
s1.2: The main task calls esp_event_loop_init() to create a system Event task and initialize an application event’s callback function. In the scenario above, the application event’s callback function does nothing but relaying the event to the application task.
Thanks.

Who is online

Users browsing this forum: Bing [Bot] and 226 guests