Skip to content

Commit

Permalink
Extract function to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Oct 14, 2024
1 parent adf854a commit 3af6122
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,68 +163,67 @@ fn encode_response_result_pdu(
}
}

fn read_u16_be(reader: &mut impl io::Read) -> io::Result<u16> {
reader.read_u16::<BigEndian>()
}

impl TryFrom<Bytes> for Request<'static> {
type Error = Error;

fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
use crate::frame::Request::*;
let mut rdr = Cursor::new(&bytes);
let rdr = &mut Cursor::new(&bytes);
let fn_code = rdr.read_u8()?;
let req = match fn_code {
0x01 => ReadCoils(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?),
0x02 => ReadDiscreteInputs(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?),
0x05 => WriteSingleCoil(
rdr.read_u16::<BigEndian>()?,
coil_to_bool(rdr.read_u16::<BigEndian>()?)?,
),
0x01 => ReadCoils(read_u16_be(rdr)?, read_u16_be(rdr)?),
0x02 => ReadDiscreteInputs(read_u16_be(rdr)?, read_u16_be(rdr)?),
0x05 => WriteSingleCoil(read_u16_be(rdr)?, coil_to_bool(read_u16_be(rdr)?)?),
0x0F => {
let address = rdr.read_u16::<BigEndian>()?;
let quantity = rdr.read_u16::<BigEndian>()?;
let address = read_u16_be(rdr)?;
let quantity = read_u16_be(rdr)?;
let byte_count = rdr.read_u8()?;
if bytes.len() < 6 + usize::from(byte_count) {
return Err(Error::new(ErrorKind::InvalidData, "Invalid byte count"));
}
let x = &bytes[6..];
WriteMultipleCoils(address, decode_packed_coils(x, quantity).into())
}
0x04 => ReadInputRegisters(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?),
0x03 => {
ReadHoldingRegisters(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?)
}
0x06 => WriteSingleRegister(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?),
0x04 => ReadInputRegisters(read_u16_be(rdr)?, read_u16_be(rdr)?),
0x03 => ReadHoldingRegisters(read_u16_be(rdr)?, read_u16_be(rdr)?),
0x06 => WriteSingleRegister(read_u16_be(rdr)?, read_u16_be(rdr)?),

0x10 => {
let address = rdr.read_u16::<BigEndian>()?;
let quantity = rdr.read_u16::<BigEndian>()?;
let address = read_u16_be(rdr)?;
let quantity = read_u16_be(rdr)?;
let byte_count = rdr.read_u8()?;
if bytes.len() < 6 + usize::from(byte_count) {
return Err(Error::new(ErrorKind::InvalidData, "Invalid byte count"));
}
let mut data = Vec::with_capacity(quantity.into());
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
data.push(read_u16_be(rdr)?);
}
WriteMultipleRegisters(address, data.into())
}
0x11 => ReportServerId,
0x16 => {
let address = rdr.read_u16::<BigEndian>()?;
let and_mask = rdr.read_u16::<BigEndian>()?;
let or_mask = rdr.read_u16::<BigEndian>()?;
let address = read_u16_be(rdr)?;
let and_mask = read_u16_be(rdr)?;
let or_mask = read_u16_be(rdr)?;
MaskWriteRegister(address, and_mask, or_mask)
}
0x17 => {
let read_address = rdr.read_u16::<BigEndian>()?;
let read_quantity = rdr.read_u16::<BigEndian>()?;
let write_address = rdr.read_u16::<BigEndian>()?;
let write_quantity = rdr.read_u16::<BigEndian>()?;
let read_address = read_u16_be(rdr)?;
let read_quantity = read_u16_be(rdr)?;
let write_address = read_u16_be(rdr)?;
let write_quantity = read_u16_be(rdr)?;
let write_count = rdr.read_u8()?;
if bytes.len() < 10 + usize::from(write_count) {
return Err(Error::new(ErrorKind::InvalidData, "Invalid byte count"));
}
let mut data = Vec::with_capacity(write_quantity.into());
for _ in 0..write_quantity {
data.push(rdr.read_u16::<BigEndian>()?);
data.push(read_u16_be(rdr)?);
}
ReadWriteMultipleRegisters(read_address, read_quantity, write_address, data.into())
}
Expand Down Expand Up @@ -254,7 +253,7 @@ impl TryFrom<Bytes> for Response {

fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
use crate::frame::Response::*;
let mut rdr = Cursor::new(&bytes);
let rdr = &mut Cursor::new(&bytes);
let fn_code = rdr.read_u8()?;
let rsp = match fn_code {
0x01 => {
Expand All @@ -273,17 +272,14 @@ impl TryFrom<Bytes> for Response {
let quantity = u16::from(byte_count) * 8;
ReadDiscreteInputs(decode_packed_coils(x, quantity))
}
0x05 => WriteSingleCoil(
rdr.read_u16::<BigEndian>()?,
coil_to_bool(rdr.read_u16::<BigEndian>()?)?,
),
0x0F => WriteMultipleCoils(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?),
0x05 => WriteSingleCoil(read_u16_be(rdr)?, coil_to_bool(read_u16_be(rdr)?)?),
0x0F => WriteMultipleCoils(read_u16_be(rdr)?, read_u16_be(rdr)?),
0x04 => {
let byte_count = rdr.read_u8()?;
let quantity = byte_count / 2;
let mut data = Vec::with_capacity(quantity.into());
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
data.push(read_u16_be(rdr)?);
}
ReadInputRegisters(data)
}
Expand All @@ -292,15 +288,13 @@ impl TryFrom<Bytes> for Response {
let quantity = byte_count / 2;
let mut data = Vec::with_capacity(quantity.into());
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
data.push(read_u16_be(rdr)?);
}
ReadHoldingRegisters(data)
}
0x06 => WriteSingleRegister(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?),
0x06 => WriteSingleRegister(read_u16_be(rdr)?, read_u16_be(rdr)?),

0x10 => {
WriteMultipleRegisters(rdr.read_u16::<BigEndian>()?, rdr.read_u16::<BigEndian>()?)
}
0x10 => WriteMultipleRegisters(read_u16_be(rdr)?, read_u16_be(rdr)?),
0x11 => {
let byte_count = rdr.read_u8()?;
let server_id = rdr.read_u8()?;
Expand All @@ -322,17 +316,17 @@ impl TryFrom<Bytes> for Response {
ReportServerId(server_id, run_indication_status, data)
}
0x16 => {
let address = rdr.read_u16::<BigEndian>()?;
let and_mask = rdr.read_u16::<BigEndian>()?;
let or_mask = rdr.read_u16::<BigEndian>()?;
let address = read_u16_be(rdr)?;
let and_mask = read_u16_be(rdr)?;
let or_mask = read_u16_be(rdr)?;
MaskWriteRegister(address, and_mask, or_mask)
}
0x17 => {
let byte_count = rdr.read_u8()?;
let quantity = byte_count / 2;
let mut data = Vec::with_capacity(quantity.into());
for _ in 0..quantity {
data.push(rdr.read_u16::<BigEndian>()?);
data.push(read_u16_be(rdr)?);
}
ReadWriteMultipleRegisters(data)
}
Expand Down

0 comments on commit 3af6122

Please sign in to comment.