guild icon
Toit
#sending numerical values via notification
Thread channel in help
AbedN
AbedN 02/22/2025 02:41 PM
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
AbedNOPAbedN
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 arra...
floitsch
floitsch 02/23/2025 12:25 PM
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.
floitschfloitsch
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 f...
AbedN
AbedN 02/25/2025 03:30 PM
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<echo_service_t*>(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;
}
AbedN
AbedN 02/25/2025 03:31 PM
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 <v2.0.0-alpha.174>
[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
floitsch
floitsch 02/25/2025 03:37 PM
Try to use triple-backticks (`) for the code. That should make it easier to read.
👍1
floitsch
floitsch 02/25/2025 03:43 PM
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.
Program your microcontrollers in a fast and robust high-level language. - toitlang/toit
floitsch
floitsch 02/25/2025 03:45 PM
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.
8 messages in total