Skip to content

Commit

Permalink
Get the transport layer vaguely parsing
Browse files Browse the repository at this point in the history
That is to say, it can nearly correctly parse my test data, though
there's still a bunch of `todo!()`s in there that need fixing and of
course the BCD problems
  • Loading branch information
Lexicality committed Mar 29, 2024
1 parent b507f11 commit c3af71b
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 66 deletions.
12 changes: 10 additions & 2 deletions src/bin/test_parse.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// Copyright 2023 Lexi Robinson
// Licensed under the EUPL-1.2

use libmbus::parse::iec_60870_5_2::{parse_packet, Packet};
use libmbus::parse::link_layer::{parse_packet, Packet};
use libmbus::parse::transport_layer::CICode;
use std::error;
use winnow::Parser;

fn do_file(fname: &str) -> Result<(), Box<dyn error::Error>> {
let data = std::fs::read(fname).map_err(Box::new)?;

let packet: Packet = parse_packet.parse(&data[..]).map_err(|e| e.to_string())?;

println!("{packet:?}");

match packet {
Packet::Long { mut data, .. } => {
let ci = CICode::parse.parse_next(&mut data);
println!("{ci:?}");
}
_ => todo!(),
}
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

pub mod dib;
pub mod error;
pub mod iec_60870_5_2;
pub mod manufacturer;
pub mod link_layer;
pub mod transport_layer;
pub mod types;
pub mod vib;

Expand Down
52 changes: 30 additions & 22 deletions src/parse/iec_60870_5_2.rs → src/parse/link_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,38 @@ use winnow::error::ParserError;
use winnow::prelude::*;
use winnow::stream::Stream;

const LONG_FRAME_HEADER: u8 = 0x68;
const SHORT_FRAME_HEADER: u8 = 0x10;
const FRAME_TAIL: u8 = 0x16;
const ACK_FRAME: u8 = 0xE5;

#[derive(Debug)]
pub enum Packet<'a> {
Ack,
Data(DataPacket<'a>),
}

#[derive(Debug)]
pub struct DataPacket<'a> {
pub control: u8,
pub address: u8,
pub data: &'a [u8],
Short {
control: u8,
address: u8,
},
Long {
control: u8,
address: u8,
data: &'a [u8],
},
}

fn parse_variable<'a>(input: &mut &'a [u8]) -> PResult<Packet<'a>> {
0x68.parse_next(input)?;
LONG_FRAME_HEADER.void().parse_next(input)?;
let length = parse_u8.parse_next(input)?;
parse_u8.verify(|v| *v == length).parse_next(input)?;
0x68.parse_next(input)?;
parse_u8.verify(|v| *v == length).void().parse_next(input)?;
LONG_FRAME_HEADER.void().parse_next(input)?;
let (control, address) = (parse_u8, parse_u8).parse_next(input)?;
let length = length.into();
// There are two bytes after the input
if input.len() < length {
return Err(ErrMode::from_error_kind(input, ErrorKind::Slice));
}
let data = input.next_slice(length - 2);
let (checksum, _) = (parse_u8, 0x16).parse_next(input)?;
let (checksum, _) = (parse_u8, FRAME_TAIL.void()).parse_next(input)?;

let sum = data
.iter()
Expand All @@ -48,32 +54,34 @@ fn parse_variable<'a>(input: &mut &'a [u8]) -> PResult<Packet<'a>> {
return Err(ErrMode::from_error_kind(input, ErrorKind::Verify));
}

Ok(Packet::Data(DataPacket {
Ok(Packet::Long {
control,
address,
data,
}))
})
}

fn parse_fixed<'a>(input: &mut &'a [u8]) -> PResult<Packet<'a>> {
// mbus's fixed length datagrams are 2 bytes long, only control & address
let (_, control, address, checksum, _) =
(0x10, parse_u8, parse_u8, parse_u8, 0x16).parse_next(input)?;
let (_, control, address, checksum, _) = (
SHORT_FRAME_HEADER.void(),
parse_u8,
parse_u8,
parse_u8,
FRAME_TAIL.void(),
)
.parse_next(input)?;

let sum = control.wrapping_add(address);
if sum != checksum {
return Err(ErrMode::from_error_kind(input, ErrorKind::Verify));
}

Ok(Packet::Data(DataPacket {
control,
address,
data: &[],
}))
Ok(Packet::Short { control, address })
}

fn parse_ack<'a>(input: &mut &'a [u8]) -> PResult<Packet<'a>> {
0xE5.map(|_| Packet::Ack).parse_next(input)
ACK_FRAME.map(|_| Packet::Ack).parse_next(input)
}

pub fn parse_packet<'a>(input: &mut &'a [u8]) -> PResult<Packet<'a>> {
Expand Down
7 changes: 7 additions & 0 deletions src/parse/transport_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2024 Lexi Robinson
// Licensed under the EUPL-1.2
pub mod control_info;
pub mod header;
pub mod manufacturer;

pub use control_info::CICode;
56 changes: 56 additions & 0 deletions src/parse/transport_layer/control_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 Lexi Robinson
// Licensed under the EUPL-1.2
#![allow(dead_code)]

use winnow::binary;
use winnow::PResult;
use winnow::Parser;

use super::header::LongHeader;
use super::header::TPLHeader;

#[derive(Debug)]
pub enum BaudRate {
Rate300,
Rate600,
Rate1200,
Rate2400,
Rate4800,
Rate9600,
Rate19200,
Rate38400,
}

#[derive(Debug)]
pub enum CICode {
Dlms(u8, TPLHeader), // TODO: Unsupported "see EN 13757–1"
Reserved,
ApplicationReset(TPLHeader), // or "Select To Device", EN 13757–3:2018, Clause 7
CommandToDevice(TPLHeader), // EN 13757–3:2018, Clause 6
ResponseFromDevice(TPLHeader), // EN 13757–3:2018, Clause 6, Annex G
SelectionOfDevice, // EN 13757-7:2018, Clause 8.4
SelectedApplicationRequest(TPLHeader), // EN 13757–3:2018, Clause 7
SelectedApplicationResponse(TPLHeader), // EN 13757–3:2018, Clause 7
SynchroniseAction, // EN 13757–3:2018, Clause 12
SpecificUsage(u8), // "Used for specific national implementations"
TimeSyncToDevice(TPLHeader), // EN 13757–3:2018, Clause 8
TimeAdjustmentToDevice(TPLHeader), // EN 13757–3:2018, Clause 8
ApplicationErrorFromDevice(TPLHeader), // EN 13757–3:2018, Clause 10
AlarmFromDevice(TPLHeader), // EN 13757–3:2018, Clause 9
Wireless(u8, TPLHeader), // TODO: Unsupported - EN 13757–4, EN 13757–5
Afl, // EN 13757-7:2018, Clause 6
ManufacturerSpecific(u8),
SetBaudRate(BaudRate),
ImageTransfer(u8), // TODO: Unsupported - EN 13757–3:2018, Annex 1
SecurityTransfer(u8), // TODO: Unsupported - EN 13757–3:2018, Annex A
}

impl CICode {
pub fn parse(input: &mut &[u8]) -> PResult<CICode> {
let ci = binary::u8.parse_next(input)?;
Ok(match ci {
0x72 => CICode::ResponseFromDevice(LongHeader::parse.parse_next(input)?),
_ => todo!(),
})
}
}
Loading

0 comments on commit c3af71b

Please sign in to comment.