mJs callback problem

I want to create a callback (I use mJs on FREERTOS esp8266 SDK)

I have this code:

c:

static my_rcv_cb *rcv_cb = NULL;
void *rcv_cb_arg = NULL;

void mqtt_receive_cb(my_rcv_cb *cb, void *userdata) {
ESP_LOGI(TAG, “my_receive_cb: %08x”, (int)cb);
rcv_cb = cb;
rcv_cb_arg = userdata;
}

void my_receive(char* data) {
if (rcv_cb != NULL) {
(*rcv_cb)(data);
}
}

js:

let CB = {
receive_cb: ffi(‘void my_receive_cb(void(*)(userdata), userdata)’),
send: ffi(‘int my_send(char *)’),
};
CB.receive_cb(function(t) {
print('–mjs: CB msg: ', t);
}, null);

In my case CB.send works fine
But when I invoke my_receive system crash.

The function names don’t quite line up so it’s difficult to follow but I’m not sure you have enough parameters in the function call signature.

I believe your intention is that the callback receives a char * parameter to point to some data and, for the purposes of mJS, it must also receive the userdata parameter, which is a void *. If so, that would be more like:

ffi(‘void my_receive_cb(void(*)(char *, userdata)’)

This code return:

MJS error: bad ffi signature: “void my_receive_cb(void(*)(char *, userdata)”: 2

Can me show a really simple/minimal example of callback?

I can’t find one.

But, from the code you’ve typed, you don’t actually have a function called my_receive_cb(). You have ones called mqtt_receive_cb() and my_receive() but not one called my_receive_cb(), unless you’ve mistyped it…?

Is solved, I’ve some simple errors

Thanks to all

1 Like