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
Determine size of event_data in esp_event_handler
-
- Posts: 831
- Joined: Mon Jul 22, 2019 3:20 pm
Re: Determine size of event_data in esp_event_handler
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.
Re: Determine size of event_data in esp_event_handler
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
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
- // POSTING EVENTS
- const size_t EVENTLOOP_MAGIC_NUMBER = 0xEFE00AE3; // some random number to indicate size prefix in event_data
- bool EventloopHandler::postEventWithSize(esp_event_base_t eventBase, int32_t eventId, void* event_data, size_t event_data_size) {
- char buffer[event_data_size + sizeof(size_t) + sizeof(EVENTLOOP_MAGIC_NUMBER)];
- *(size_t*)buffer = EVENTLOOP_MAGIC_NUMBER;
- *(size_t*)(buffer + sizeof(size_t)) = event_data_size;
- memcpy(buffer + sizeof(EVENTLOOP_MAGIC_NUMBER) + sizeof(size_t), event_data, event_data_size);
- return postEvent(eventBase, eventId, buffer, sizeof(buffer)); // calls esp_event_post_to(...)
- }
- // LISTENING FOR EVENTS
- bool EventListener::eventDataHasSizePrefix(const void* event_data) {
- return memcmp(event_data, &EVENTLOOP_MAGIC_NUMBER, sizeof(EVENTLOOP_MAGIC_NUMBER)) == 0;
- }
- void EventListener::allEventHandler(void* event_handler_arg, esp_event_base_t eventBase, int32_t eventId, void* event_data) {
- // shorted for clearity .....
- if (eventDataHasSizePrefix(event_data)) {
- size_t size = *(size_t*)((uint8_t*)event_data + sizeof(EVENTLOOP_MAGIC_NUMBER));
- void* startOfData = (uint8_t*)event_data + sizeof(EVENTLOOP_MAGIC_NUMBER) + sizeof(size_t);
- listener->eventHandler(eventBase, eventId, startOfData, size);
- } else {
- listener->eventHandler(eventBase, eventId, event_data);
- }
- // ...
- }
Who is online
Users browsing this forum: No registered users and 29 guests