https://toitlang.org/ logo
sending numerical values via notification
# help
a
Hi, I'm trying to get my c enevlope to send a float value as a notification (it doesn't expect a response) to a toit container. tried segmenting the float value into a uint_8 array then sending that via toit_msg_notify however i keep getting a "Guru Meditation" error, plus i don't think it's viable to do it this way afterall, so wondering if there's a better way. Abdalrahim
f
Is the communication working otherwise? Is this specific to the float value? Can you send a test buffer from the C side to the Toit side? If yes, then you should just encode your float into a byte array and send it to the Toit side. There, you can then use
io.LITTLE-ENDIAN
to read the float out again.
a
here's my test buffer: extern "C" { static toit_err_t on_created(void* user_data, toit_msg_context_t* context) { echo_service_t* echo_service = static_cast(user_data); echo_service->msg_context = context; uint8_t temp[4]; int length = 4; for(int i = 0; i < 4; i++){ temp[i] = (uint8_t) 'x' + i; } if (toit_msg_notify(context, 3, temp, length, true) != TOIT_OK) { std::printf("unable to send\n"); } return TOIT_OK; }
and this is the error i'm getting: ELF file SHA256: c9ba8f10c5206ec7 Rebooting... ESP-ROM:esp32c6-20220919 Build:Sep 19 2022 rst:0xc (SW_CPU),boot:0xc (SPI_FAST_FLASH_BOOT) Saved PC:0x4001975a SPIWP:0xee mode:DIO, clock div:2 load:0x40875720,len:0x9c load:0x4086c110,len:0xb6c load:0x4086e610,len:0x23b8 entry 0x4086c110 [toit] INFO: starting [toit] INFO: running on ESP32C6 - revision 0.1 unable to send Guru Meditation Error: Core 0 panic'ed (Store access fault). Exception was unhandled. Core 0 register dump: MEPC : 0x40806b22 RA : 0x40807016 SP : 0x40830e90 GP : 0x40822da4
f
Try to use triple-backticks (`) for the code. That should make it easier to read.
I think there are three things here: 1. we need to figure out what the return code of
toit_msg_notify
is. Most likely it's
TOIT_ERR_NO_SUCH_RECEIVER
. 2. you are passing in a buffer that is allocated on the stack (
uint8_t temp[4]
). Since you are leaving the function just after sending the data (
return TOIT_OK
) the buffer will get invalid. So use a
malloc
(or
toit_malloc
) instead. It's also a problem, as
toit_msg_notify
takes ownership of the buffer (see the comment of the function) and will eventually free the buffer. 3. the last parameter of the
toit_msg_notify
is
free_on_failure
(https://github.com/toitlang/toit/blob/1f4f86faa2c5be88f0b0df3f7aa401b71c4d7052/include/toit/toit.h#L221). This means that the toit call will delete the message if it couldn't send it. And that's exactly what's happening here. Since the sending didn't work, Toit tried to free the buffer, which is your stack-allocated object. And that's not a good idea.
I don't see any container starting at the beginning. So either they didn't have time yet, or it's not correctly configured. Either way, that's probably the reason the sending is failing: the other side is not (yet) there.