felixwrt / sml-rs

Smart Message Language parser written in Rust
Other
11 stars 3 forks source link

let _ = reader.read::<File>(); creates a stack overflow #31

Closed thinkrapido closed 1 week ago

thinkrapido commented 5 months ago

let _ = reader.read::<File>(); creates a stack overflow Maybe this is a problem with the buffer I created. I haven't figured it out, yet.

    let mut buffer = [0u8;2048];
    loop {
        sleep(Duration::from_secs(1));
        let len = uart2.read(&mut buffer, 20)?;
        writeln!(uart0, "buffer: {:?}", &buffer[..len])?;
        writeln!(uart0, "hello from esp32")?;
        let mut read = &buffer[..len];
        let mut reader = sml_rs::SmlReader::from_reader(read);
        let _ = reader.read::<File>();
    }
felixwrt commented 5 months ago

Hi Romeo,

it's nice to hear from you again, how are you? We've been co-organizing Rust meetup Stuttgart if you remember.

What platform are you running this code on? It it a microcontroller? What's the type of uart2?

SmlReader contains an internal buffer, which is 8KiB by default. You can use a different size, see SmlReader::with_static_buffer<N>(). Ideally, your uart port would implement either std::io::Read or embedded_hals equivalent trait. you could then pass your port into SmlReader and wouldn't need another buffer.

Let me know if that helps

felixwrt commented 5 months ago

Looking at this again today:

You're using esp_idf_hal, right? Its UartRxDriver (which I'm pretty sure you're using in the example above) also implements the embedded_hal::serial::Read trait. SmlReader has support for this when using the optional feature "embedded-hal". You can use it with sml-rs like this:

# Cargo.toml
[dependencies]
sml-rs = { path = "...", features = ["embedded_hal"] }
let mut reader = SmlReader::from_eh_reader(uart2);
// if you want to customize the internal buffer size:
// let mut reader = SmlReader::with_static_buffer::<1024>().from_eh_reader(uart2);

while let Some(res) = reader.next::<File>() {
    match res {
        Ok(file) => println!("{:#?}", file),
        Err(e) => println!("Err({:?})", e),
    }
}

Let me know whether that works for you and if you have suggestions regarding the crate.

felixwrt commented 1 week ago

Hi Romeo, I'm closing this issue as I haven't heard back from you. Feel free to reopen if you still have issues with the lib.