Determine size of event_data in esp_event_handler

Bascy65
Posts: 5
Joined: Tue Dec 26, 2017 2:32 pm

Determine size of event_data in esp_event_handler

Postby Bascy65 » Thu Jan 25, 2024 7:54 pm

I'm using an eventloop to distribute events accross my system, and I really love this system. I'ts perfect for decoupling dependencies between parts of the system and its helped me a lot.

One thing i dont understand is that, when posting an event, you provide a pointer to the event data, and the size of the data. But when an event_handler gets called, it is only provided with a pointer to the (copy of) the event_data and there is no way to determine the size of the data.

Now mostly this isnt a problem as most of the events I use have a known, fixed size data, but now I need to allow for a dynamically sized data structure (BLE advertisements to be precise) and it will be very hard to handle the event if I don't know the data-size in the event_handler...

Anybody else struggling with this problem? Or am I doning something wrong / overlooking something?

Any help is appreciated

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Determine size of event_data in esp_event_handler

Postby lbernstone » Thu Jan 25, 2024 11:49 pm

Is your problem variable sized data, or that you will be using a struct? A struct typically has a fixed size, so it shouldn't be a problem. If you have variable sized data, take a look at ring buffers, and see if your use case will support a stack like that.

Bascy65
Posts: 5
Joined: Tue Dec 26, 2017 2:32 pm

Re: Determine size of event_data in esp_event_handler

Postby Bascy65 » Fri Jan 26, 2024 7:17 am

The problem is the variable size of the data, and the fact that the eventHandler has no way of determining the size.

I have solved it now by prefixing the actual event_data with a magic number and the size, so the event_handler method can check if the data contains a size prefix by checking the magic number, and then extracting the size and the actual data
  1. // POSTING EVENTS
  2.  
  3. const size_t EVENTLOOP_MAGIC_NUMBER = 0xEFE00AE3; // some random number to indicate size prefix in event_data
  4.  
  5. bool EventloopHandler::postEventWithSize(esp_event_base_t eventBase, int32_t eventId, void* event_data, size_t event_data_size) {
  6.   char buffer[event_data_size + sizeof(size_t) + sizeof(EVENTLOOP_MAGIC_NUMBER)];
  7.   *(size_t*)buffer = EVENTLOOP_MAGIC_NUMBER;
  8.   *(size_t*)(buffer + sizeof(size_t)) = event_data_size;
  9.   memcpy(buffer + sizeof(EVENTLOOP_MAGIC_NUMBER) + sizeof(size_t), event_data, event_data_size);
  10.   return postEvent(eventBase, eventId, buffer, sizeof(buffer)); // calls esp_event_post_to(...)
  11. }
  12.  
  13.  
  14. // LISTENING FOR EVENTS
  15.  
  16. bool EventListener::eventDataHasSizePrefix(const void* event_data) {
  17.   return memcmp(event_data, &EVENTLOOP_MAGIC_NUMBER, sizeof(EVENTLOOP_MAGIC_NUMBER)) == 0;
  18. }
  19.  
  20. void EventListener::allEventHandler(void* event_handler_arg, esp_event_base_t eventBase, int32_t eventId, void* event_data) {
  21.      // shorted for clearity .....
  22.       if (eventDataHasSizePrefix(event_data)) {
  23.         size_t size = *(size_t*)((uint8_t*)event_data + sizeof(EVENTLOOP_MAGIC_NUMBER));
  24.         void* startOfData = (uint8_t*)event_data + sizeof(EVENTLOOP_MAGIC_NUMBER) + sizeof(size_t);
  25.         listener->eventHandler(eventBase, eventId, startOfData, size);
  26.       } else {
  27.         listener->eventHandler(eventBase, eventId, event_data);
  28.       }
  29.       // ...
  30. }

Who is online

Users browsing this forum: No registered users and 46 guests