event_handler_arg in Esp_mqtt component. How do I access the arg from the handler?

lukilukeskywalker
Posts: 21
Joined: Wed Feb 22, 2023 4:25 pm

event_handler_arg in Esp_mqtt component. How do I access the arg from the handler?

Postby lukilukeskywalker » Fri May 05, 2023 12:26 pm

Hi,

I think this question has already been answered, because I think I read it a few days ago, but I swear I can't get to find the discussion anymore. So my question is, in the esp_mqtt component, when I register an event like this:

Code: Select all

esp_mqtt_client_register_event(client, MQTT_EVENT_CONNECTED, mqtt_connected_handler, mqtt_topic_bk);
being mqtt_topic_bk a pointer to a struct... how do I get in the event handler, the pointer to that struct back? Is it inside of:

Code: Select all

((esp_mqtt_event_handle_t *)event_data)->data
I mean... How is it structured? data is a pointer to? "Data associated with this event" https://docs.espressif.com/projects/esp ... tt_event_tThat means? Is it the data I passed when I registered the event or when it gets data from the broker (Looking at the examples provided, that seems to be what is happening). But then I dont get where does my event handler get the event_handler_arg

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: event_handler_arg in Esp_mqtt component. How do I access the arg from the handler?

Postby MicroController » Fri May 05, 2023 2:56 pm

The first parameter of your event handling function is the void* event_handler_arg which receives the event_handler_arg value you provided upon registration of the event handler. If you pass in a "my_struct*" at registration, you can get it back in an event handling function

Code: Select all

void my_evt_handler_func(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
via

Code: Select all

my_struct* p_my_struct = (my_struct*)event_handler_arg;

lukilukeskywalker
Posts: 21
Joined: Wed Feb 22, 2023 4:25 pm

Re: event_handler_arg in Esp_mqtt component. How do I access the arg from the handler?

Postby lukilukeskywalker » Fri May 05, 2023 3:42 pm

:oops:
I see... I feel so stupid right now... I was looking for something that was literaly in front of me...
Thank you :oops:

Who is online

Users browsing this forum: No registered users and 139 guests