Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use common (de)serialization routines with DNP3 via external crate #100

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dep_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
},
"rodbus-ffi-java": {
"url": "https://github.com/stepfunc/rodbus"
},
"scursor": {
"url": "https://github.com/stepfunc/scursor"
}
},
"third_party": {
Expand Down
1 change: 1 addition & 0 deletions rodbus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ readme = "../README.md"

[dependencies]
crc = "2.0"
scursor = { git = "https://github.com/stepfunc/scursor.git", tag="0.1.0" }
tokio = { version = "1", features = ["net", "sync", "io-util", "io-std", "time", "rt", "rt-multi-thread", "macros"] }
tracing = "0.1"

Expand Down
5 changes: 3 additions & 2 deletions rodbus/src/client/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use crate::client::requests::read_bits::ReadBits;
use crate::client::requests::read_registers::ReadRegisters;
use crate::client::requests::write_multiple::MultipleWriteRequest;
use crate::client::requests::write_single::SingleWrite;
use crate::common::cursor::{ReadCursor, WriteCursor};
use crate::common::traits::Serialize;
use crate::types::{Indexed, UnitId};

use scursor::{ReadCursor, WriteCursor};
use std::time::Duration;

pub(crate) enum Setting {
Expand Down Expand Up @@ -97,7 +98,7 @@ impl Request {
RequestError::Exception(exception)
} else {
tracing::warn!("invalid modbus exception");
RequestError::BadResponse(AduParseError::TrailingBytes(cursor.len()))
RequestError::BadResponse(AduParseError::TrailingBytes(cursor.remaining()))
}
}
Err(err) => err.into(),
Expand Down
3 changes: 2 additions & 1 deletion rodbus/src/client/requests/read_bits.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::common::cursor::{ReadCursor, WriteCursor};
use crate::common::function::FunctionCode;
use crate::common::traits::Serialize;
use crate::decode::AppDecodeLevel;
use crate::error::RequestError;
use crate::types::{AddressRange, BitIterator, BitIteratorDisplay, ReadBitsRange};
use crate::Indexed;

use scursor::{ReadCursor, WriteCursor};

pub(crate) trait BitsCallback:
FnOnce(Result<BitIterator, RequestError>) + Send + Sync + 'static
{
Expand Down
3 changes: 2 additions & 1 deletion rodbus/src/client/requests/read_registers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::common::cursor::{ReadCursor, WriteCursor};
use crate::common::function::FunctionCode;
use crate::common::traits::Serialize;
use crate::decode::AppDecodeLevel;
Expand All @@ -7,6 +6,8 @@ use crate::types::{
AddressRange, Indexed, ReadRegistersRange, RegisterIterator, RegisterIteratorDisplay,
};

use scursor::{ReadCursor, WriteCursor};

pub(crate) trait RegistersCallback:
FnOnce(Result<RegisterIterator, RequestError>) + Send + Sync + 'static
{
Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/client/requests/write_multiple.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::client::message::Promise;
use crate::common::cursor::{ReadCursor, WriteCursor};
use crate::common::function::FunctionCode;
use crate::common::traits::{Parse, Serialize};
use crate::decode::AppDecodeLevel;
use crate::error::RequestError;
use crate::error::{AduParseError, InvalidRequest};
use crate::types::{AddressRange, Indexed};

use scursor::{ReadCursor, WriteCursor};
use std::convert::TryFrom;

/// Collection of values and starting address
Expand Down
3 changes: 2 additions & 1 deletion rodbus/src/client/requests/write_single.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::fmt::Display;

use crate::client::message::Promise;
use crate::common::cursor::{ReadCursor, WriteCursor};
use crate::common::function::FunctionCode;
use crate::decode::AppDecodeLevel;
use crate::error::AduParseError;
use crate::error::RequestError;
use crate::types::{coil_from_u16, coil_to_u16, Indexed};

use scursor::{ReadCursor, WriteCursor};

pub(crate) trait SingleWriteOperation: Sized + PartialEq {
fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError>;
fn parse(cursor: &mut ReadCursor) -> Result<Self, RequestError>;
Expand Down
10 changes: 0 additions & 10 deletions rodbus/src/common/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::common::phys::PhysLayer;

#[cfg(feature = "no-panic")]
use no_panic::no_panic;

use crate::error::InternalError;
use crate::PhysDecodeLevel;

Expand All @@ -21,17 +18,14 @@ impl ReadBuffer {
}
}

#[cfg_attr(feature = "no-panic", no_panic)]
pub(crate) fn len(&self) -> usize {
self.end - self.begin
}

#[cfg_attr(feature = "no-panic", no_panic)]
pub(crate) fn is_empty(&self) -> bool {
self.begin == self.end
}

#[cfg_attr(feature = "no-panic", no_panic)]
pub(crate) fn read(&mut self, count: usize) -> Result<&[u8], InternalError> {
if self.len() < count {
return Err(InternalError::InsufficientBytesForRead(count, self.len()));
Expand All @@ -46,7 +40,6 @@ impl ReadBuffer {
}
}

#[cfg_attr(feature = "no-panic", no_panic)]
pub(crate) fn read_u8(&mut self) -> Result<u8, InternalError> {
if self.is_empty() {
return Err(InternalError::InsufficientBytesForRead(1, 0));
Expand All @@ -60,7 +53,6 @@ impl ReadBuffer {
}
}

#[cfg_attr(feature = "no-panic", no_panic)]
#[cfg(feature = "serial")]
pub(crate) fn peek_at(&mut self, idx: usize) -> Result<u8, InternalError> {
let len = self.len();
Expand All @@ -73,14 +65,12 @@ impl ReadBuffer {
.ok_or(InternalError::InsufficientBytesForRead(idx + 1, len))
}

#[cfg_attr(feature = "no-panic", no_panic)]
pub(crate) fn read_u16_be(&mut self) -> Result<u16, InternalError> {
let b1 = self.read_u8()? as u16;
let b2 = self.read_u8()? as u16;
Ok((b1 << 8) | b2)
}

#[cfg_attr(feature = "no-panic", no_panic)]
#[cfg(feature = "serial")]
pub(crate) fn read_u16_le(&mut self) -> Result<u16, InternalError> {
let b1 = self.read_u8()? as u16;
Expand Down
157 changes: 0 additions & 157 deletions rodbus/src/common/cursor.rs

This file was deleted.

3 changes: 2 additions & 1 deletion rodbus/src/common/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use crate::common::phys::PhysLayer;
use std::ops::Range;

use crate::common::buffer::ReadBuffer;
use crate::common::cursor::WriteCursor;
use crate::common::function::FunctionCode;
use crate::common::traits::{Loggable, LoggableDisplay, Serialize};
use crate::error::RequestError;
use crate::tcp::frame::{MbapDisplay, MbapHeader, MbapParser};
use crate::types::UnitId;
use crate::{DecodeLevel, ExceptionCode, FrameDecodeLevel};

use scursor::WriteCursor;

pub(crate) mod constants {
const fn max(lhs: usize, rhs: usize) -> usize {
if lhs > rhs {
Expand Down
1 change: 0 additions & 1 deletion rodbus/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub(crate) mod traits;

pub(crate) mod bits;
pub(crate) mod buffer;
pub(crate) mod cursor;
pub(crate) mod frame;
mod parse;
pub(crate) mod phys;
Expand Down
6 changes: 4 additions & 2 deletions rodbus/src/common/parse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::common::cursor::ReadCursor;
use crate::common::traits::Parse;
use crate::error::*;
use crate::types::{coil_from_u16, AddressRange, Indexed};

use scursor::ReadCursor;

impl Parse for AddressRange {
fn parse(cursor: &mut ReadCursor) -> Result<Self, RequestError> {
Ok(AddressRange::try_from(
Expand All @@ -29,11 +30,12 @@ impl Parse for Indexed<u16> {

#[cfg(test)]
mod coils {
use crate::common::cursor::ReadCursor;
use crate::common::traits::Parse;
use crate::error::AduParseError;
use crate::types::Indexed;

use scursor::ReadCursor;

#[test]
fn parse_fails_for_unknown_coil_value() {
let mut cursor = ReadCursor::new(&[0x00, 0x01, 0xAB, 0xCD]);
Expand Down
Loading