FactbirdHQ / atat

no_std crate for parsing AT commands
Apache License 2.0
115 stars 31 forks source link

custom handlers in AtDigester should support returning nom::Err::Incomplete #140

Closed rmja closed 1 year ago

rmja commented 1 year ago

The current digest() method for AtDigester looks like this:

https://github.com/BlackbirdHQ/atat/blob/a4608e9020ea8f1cd40e2677ee39d67bae2df699/atat/src/digest.rs#L110-L160

I have a custom_succes that handles reading binary data from the modem. For example:

AtDigester::new()
            .with_custom_success(|buf| {
                let (_reminder, (head, data, tail)) = branch::alt((
                    sequence::tuple((
                        combinator::success(&b""[..]),
                        combinator::success(&b""[..]),
                        bytes::streaming::tag(b"\r\nSHUT OK\r\n"),
                    )),
                    sequence::tuple((
                        bytes::streaming::tag(b"\r\n"),
                        combinator::recognize(sequence::tuple((
                            bytes::streaming::tag(b"+CIPRXGET: 2,"),
                            character::streaming::u8,
                            bytes::streaming::tag(","),
                            combinator::flat_map(character::streaming::u16, |data_len| {
                                combinator::recognize(sequence::tuple((
                                    bytes::streaming::tag(","),
                                    character::streaming::u16,
                                    bytes::streaming::tag("\r\n"),
                                    bytes::streaming::take(data_len),
                                )))
                            }),
                        ))),
                        bytes::streaming::tag(b"\r\nOK\r\n"),
                    )),
                ))(buf)?;

                Ok((data, head.len() + data.len() + tail.len()))
            })

The +CIPRXGET is basically a header followed by the binary data followed by OK.

It returns ParseError::Incomplete until the entire sequence of bytes is received. Currently the digest method does not honor this. It simply continues to the next "possible DigestResult candidate" in digest() because the result from the custom success handler is not Ok. Effectively this often finds a prompt "> " in the binary data.