- My goal is: I’m trying to pass data received in a c callback into my js script
- My actions are:
I’ve created a struct (mgos_esp_now_struct) which holds the data I want to pass:
struct mgos_esp_now_struct {
void (callback)(char, char*, void *);
char mac[13];
char response[30];
void *ud;
};
When the callback is fired, I populate the struct with my data:
void esp_now_recv_cb(const uint8_t *mac_addr, const uint8_t *data,
int data_len) {
sprintf(esp_now_struct.mac, “%.2x%.2x%.2x%.2x%.2x%.2x”, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);LOG(LL_INFO, (“RECV %s %.*s”, esp_now_struct.mac, data_len, data));
for (int i=0; i < data_len; ++i) {
esp_now_struct.response[i] = data[i];
}
esp_now_struct.response[data_len] = ‘\0’;
LOG(LL_INFO, (“Invoking esp_now_cb for %s”,esp_now_struct.mac));
mgos_invoke_cb(esp_now_cb, NULL, false);}
void esp_now_cb(void *arg) {
if (esp_now_struct.callback != NULL) esp_now_struct.callback(esp_now_struct.mac, esp_now_struct.response, esp_now_struct.ud);
}
-
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.
Any help appreciated!