use super::{push_length, read_length, ServiceError};
pub const GENERAL_BLOCK_TRANSFER: u8 = 0xE0;
const LAST_BLOCK: u8 = 0x80;
const STREAMING: u8 = 0x40;
const WINDOW_MASK: u8 = 0x3F;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneralBlockTransfer {
pub last_block: bool,
pub streaming: bool,
pub window: u8,
pub block_number: u16,
pub block_number_ack: u16,
pub block_data: Vec<u8>,
}
impl GeneralBlockTransfer {
pub fn encode(&self) -> Vec<u8> {
let block_control = if self.last_block { LAST_BLOCK } else { 0 }
| if self.streaming { STREAMING } else { 0 }
| (self.window & WINDOW_MASK);
let mut buf = vec![GENERAL_BLOCK_TRANSFER, block_control];
buf.extend_from_slice(&self.block_number.to_be_bytes());
buf.extend_from_slice(&self.block_number_ack.to_be_bytes());
push_length(self.block_data.len(), &mut buf);
buf.extend_from_slice(&self.block_data);
buf
}
pub fn decode(bytes: &[u8]) -> Result<GeneralBlockTransfer, ServiceError> {
if bytes.len() < 6 {
return Err(ServiceError::Truncated);
}
if bytes[0] != GENERAL_BLOCK_TRANSFER {
return Err(ServiceError::UnexpectedTag(bytes[0]));
}
let block_control = bytes[1];
let block_number = u16::from_be_bytes([bytes[2], bytes[3]]);
let block_number_ack = u16::from_be_bytes([bytes[4], bytes[5]]);
let (len, header) = read_length(&bytes[6..])?;
let start = 6 + header;
let block_data = bytes.get(start..start + len).ok_or(ServiceError::Truncated)?.to_vec();
Ok(GeneralBlockTransfer {
last_block: block_control & LAST_BLOCK != 0,
streaming: block_control & STREAMING != 0,
window: block_control & WINDOW_MASK,
block_number,
block_number_ack,
block_data,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn general_block_transfer_round_trips() {
let gbt = GeneralBlockTransfer {
last_block: false,
streaming: false,
window: 1,
block_number: 1,
block_number_ack: 0,
block_data: vec![0xC0, 0x01, 0xC1, 0x00, 0x08],
};
let bytes = gbt.encode();
assert_eq!(bytes, vec![0xE0, 0x01, 0x00, 0x01, 0x00, 0x00, 0x05, 0xC0, 0x01, 0xC1, 0x00, 0x08]);
assert_eq!(GeneralBlockTransfer::decode(&bytes).unwrap(), gbt);
}
#[test]
fn last_block_and_streaming_flags_round_trip() {
let gbt = GeneralBlockTransfer {
last_block: true,
streaming: true,
window: 5,
block_number: 3,
block_number_ack: 2,
block_data: vec![0xAA],
};
let bytes = gbt.encode();
assert_eq!(bytes[1], LAST_BLOCK | STREAMING | 5);
assert_eq!(GeneralBlockTransfer::decode(&bytes).unwrap(), gbt);
}
#[test]
fn decode_rejects_wrong_tag() {
assert_eq!(GeneralBlockTransfer::decode(&[0xC0, 0x00, 0, 0, 0, 0, 0]), Err(ServiceError::UnexpectedTag(0xC0)));
}
}