/// Decode RTU PDU frames from a buffer.
Decode RTU PDU frames from a buffer.
Decode RTU PDU frames from a buffer .
pub fn decode(
    decoder_type: DecoderType,
    buf: &[u8],
) -> Result<Option<(DecodedFrame, FrameLocation)>> {
    use DecoderType::*;
    let mut drop_cnt = 0;
    loop {
        let mut retry = false;
        if drop_cnt + 1 >= buf.len() {
            return Ok(None);
        }
        let raw_frame = &buf[drop_cnt..];
        let res = match decoder_type {
            Request => request_pdu_len(raw_frame),
            Response => response_pdu_len(raw_frame),
        }
        .and_then(|pdu_len| {
            retry = false;
            if let Some(pdu_len) = pdu_len {
                extract_frame(raw_frame, pdu_len).map(|x| {
                    x.map(|res| {
                        (
                            res,
                            FrameLocation {
                                start: drop_cnt,
                                size: pdu_len + 3,
                            },
                        )
                    })
                })
            } else {
                // Incomplete frame
                Ok(None)
            }
        })
        .or_else(|err| {
            let pdu_type = match decoder_type {
                Request => "request",
                Response => "response",
            };
            if drop_cnt + 1 >= MAX_FRAME_LEN {
                error!(
                    "Giving up to decode frame after dropping {} byte(s): {:X?}",
                    drop_cnt,
                    &buf[0..drop_cnt]
                );
                return Err(err);
            }
            warn!("Failed to decode {} frame: {}", pdu_type, err);
            drop_cnt += 1;
            retry = true;
            Ok(None)
        });
        if !retry {
            return res;
        }
    }
}
pub fn decode ( decoder_type : DecoderType , buf : & [ u8 ] , ) -> Result < Option < ( DecodedFrame , FrameLocation ) > > { use DecoderType :: * ; let mut drop_cnt = 0 ; loop { let mut retry = false ; if drop_cnt + 1 >= buf . len ( ) { return Ok ( None ) ; } let raw_frame = & buf [ drop_cnt .. ] ; let res = match decoder_type { Request => request_pdu_len ( raw_frame ) , Response => response_pdu_len ( raw_frame ) , } . and_then ( | pdu_len | { retry = false ; if let Some ( pdu_len ) = pdu_len { extract_frame ( raw_frame , pdu_len ) . map ( | x | { x . map ( | res | { ( res , FrameLocation { start : drop_cnt , size : pdu_len + 3 , } , ) } ) } ) } else { // Incomplete frame Ok ( None ) } } ) . or_else ( | err | { let pdu_type = match decoder_type { Request => " " , Response => " " , } ; if drop_cnt + 1 >= MAX_FRAME_LEN { error ! ( " " drop_cnt buf [ 0 drop_cnt ] ) ; return Err ( err ) ; } warn ! ( " " pdu_type err ) ; drop_cnt += 1 ; retry = true ; Ok ( None ) } ) ; if ! retry { return res ; } } }
Decode RTU PDU frames from a buffer.
Decode RTU PDU frames from a buffer .
decoder_type
DecoderType
buf
&[u8]