The result I see is: The callback is called from several sources in quick succession, and although mgos_invoke_cb is invoked each time, the data it passes is only the most recent information.
My expectation & question is: What do I need to ensure that mgos_invoke_cb correctly passes the relevant information to the js callback, rather than just the most recently received info?
I think it might be because I’m statically allocating the original struct at the top of my main.c, so it’s getting over-written each time the original callback is invoked before mgos_invoke_cb gets a chance. But, I’m not sure how to change it so that each time the callback is invoked a new version of the struct is initiated (and then freed in the mgos_invoke_cb.
You are right. The static structure data is overwritten on each invocation.
Allocate the structure in esp_now_recv_cb, pass it to esp_now_cb and free it there.
That’s brilliant, many thanks @nliviu. The only thing that’s not now working is that the callback isn’t remembered as the struct is being dynamically allocated. I think I just need to create a seperate (static) struct for the original callback, and keep the Mac/response data separately dynamically allocated as per your example?
I wanted to ask how do you fill the callback field, but I forgot…
Maybe you can keep the callback in a static variable and copy it in the dynamic structure?