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

FC43 Frame Writer and Improvements #142

Open
wants to merge 42 commits into
base: iware/review
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e8efaa6
Merge remote-tracking branch 'origin/iware/review' into feature/fc43-…
Feb 26, 2024
5f4c64f
refactor(read_device_info_handler): change return type.
Feb 26, 2024
9bf03e8
refactor(get_reply): store the data returned by the handler, add todo!()
Feb 26, 2024
2324bde
refactor(response): add option arg to callback, change return type.
Feb 27, 2024
232d8e2
refactor(ServerDeviceInfo): add derived traits to struct.
Feb 27, 2024
a475ec9
refactor(response_message_count): comment out old code for now
Feb 27, 2024
38b8a8f
refactor(integration_tests): change response of integration tests.
Feb 27, 2024
cbfb469
refactor(get_reply): do some calculations before passing the index on
Feb 27, 2024
fedc5aa
refactor(server): testing how to pass slices from functions...
Feb 27, 2024
6d92494
refactor(trait serialize): change the serialization of read device info
Feb 27, 2024
babd8be
refactor(server): remove import and use the direct path of the enum.
Feb 27, 2024
6f4e62d
chore(read device identification): rustfmt
Feb 27, 2024
274399f
refactor(frame): add FrameRecords struct and change serialize trait.
Feb 27, 2024
3c144b3
refactor(serialize trait): add recorder into serialize trait
Feb 27, 2024
a5979ab
refactor(serialize): pass none where no recorder is necessary
Feb 27, 2024
5d09fb2
chore(grammar): remove double "the" from doc comment
Feb 27, 2024
5011f28
tests(frame): fix the frame tests by adding the now necessary frame rec.
Feb 28, 2024
6fc31f5
refactor(frame_recorder): Improve the API of the FrameRecorder.
Feb 28, 2024
8b085f2
feat(RecorderError): add a new error type for the frame recorder
Feb 28, 2024
5b6c4e0
refactor(server_device_info): add read_device_code back into the struct
Feb 28, 2024
69ea1b3
refactor(server_device_info): add read_device_code field.
Feb 28, 2024
1b401bf
refactor(serialize): change usage of FrameRecorder API
Feb 28, 2024
823cbff
refactor(server): temporary add field to returned dummy value.
Feb 28, 2024
899355c
refactor(handler): add current_object_id to ReadDeviceInfo struct
May 22, 2024
02ebb9a
refactor(serialize): object by object serialization for FC43
May 22, 2024
c097976
feat(error): add frame record errors to the InternalError enum
May 22, 2024
3df08a3
refactor(frame): get rid of the unwraps in the fill_record method
May 22, 2024
488e085
feat(frame): add handling of non-empty frame recorders before sending
May 22, 2024
1cfe7b9
refactor(request): remove unnecessary stuff from the way fc43 is handled
May 22, 2024
76e5e1d
test(integration_tests): change the code that generates the response
May 22, 2024
049e7ea
feat(ffi): add FrameRecorderNotEmpty conversion into InternalError
May 22, 2024
d338561
refactor(message): make the FrameRecorder argument invisible.
May 22, 2024
e6a817c
chore(read_device_ident): cargo fmt improvements
May 22, 2024
9c7b75f
refactor(frame): remove unnecessary imports, unnecessary clone, run fmt
May 22, 2024
084197c
refactor(serialize): remove unnecessary imports, make records invisible
May 22, 2024
7e355c6
chore(traits): cargo fmt formatting applied
May 22, 2024
b26e1a4
chore(frame): cargo fmt formatting applied
May 22, 2024
edcfc09
refactor(error): remove unnecessary import, apply cargo formatting
May 22, 2024
01196a8
chore(request): apply cargo formatting
May 22, 2024
342edb4
refactor(response): remove unnecessary imports, apply cargo formatting
May 22, 2024
b032c00
chore(frame): apply cargo formatting
May 22, 2024
0ec01f4
chore(e2e_read_device): remove commented code, apply cargo formatting
May 22, 2024
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
1 change: 1 addition & 0 deletions ffi/rodbus-ffi/src/helpers/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl From<rodbus::RequestError> for ffi::RequestError {
rodbus::RequestError::Exception(ex) => ex.into(),
rodbus::RequestError::Io(_) => ffi::RequestError::IoError,
rodbus::RequestError::BadResponse(_) => ffi::RequestError::BadResponse,
rodbus::RequestError::FrameRecorderNotEmpty => ffi::RequestError::InternalError,
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions rodbus/examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ impl RequestHandler for SimpleHandler {
mei_code: MeiCode,
read_dev_id: ReadDeviceCode,
object_id: Option<u8>,
) -> Result<DeviceInfo, ExceptionCode> {
let mut device_info = DeviceInfo::new(
) -> Result<ServerDeviceInfo, ExceptionCode> {
let server = ServerDeviceInfo {
read_device_code: read_dev_id,
conformity_level: DeviceConformityLevel::ExtendedIdentificationIndividual,
next_object_id: None,
object_data: &[0x00,0x04,0x41,0x41,0x41,0x41],
};
/*let mut device_info = DeviceInfo::new(
mei_code,
read_dev_id,
DeviceConformityLevel::ExtendedIdentificationIndividual,
Expand Down Expand Up @@ -170,7 +176,7 @@ impl RequestHandler for SimpleHandler {
message.as_bytes(),
)];

return Ok(device_info);
return Ok(server);
} else {
device_info.number_objects = response_data.len() as u8;
device_info.storage = vec![];
Expand All @@ -184,9 +190,9 @@ impl RequestHandler for SimpleHandler {
);
device_info.storage.push(obj);
}
}
}*/

Ok(device_info)
Ok(server)
}

fn wrap(self) -> std::sync::Arc<std::sync::Mutex<Box<Self>>>
Expand Down
7 changes: 6 additions & 1 deletion rodbus/src/client/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::client::requests::write_single::SingleWrite;
use crate::common::traits::Serialize;
use crate::types::{Indexed, UnitId};

use crate::common::frame::FrameRecords;
use scursor::{ReadCursor, WriteCursor};
use std::time::Duration;

Expand Down Expand Up @@ -174,7 +175,11 @@ impl RequestDetails {
}

impl Serialize for RequestDetails {
fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
fn serialize(
&self,
cursor: &mut WriteCursor,
_: Option<&mut FrameRecords>,
) -> Result<(), RequestError> {
match self {
RequestDetails::ReadCoils(x) => x.serialize(cursor),
RequestDetails::ReadDiscreteInputs(x) => x.serialize(cursor),
Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/client/requests/read_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ReadBits {
}

pub(crate) fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
self.request.get().serialize(cursor)
self.request.get().serialize(cursor, None)
}

pub(crate) fn failure(&mut self, err: RequestError) {
Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/client/requests/read_device_identification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl ReadDevice {
}

pub(crate) fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
self.request.serialize(cursor)
self.request.serialize(cursor, None)
}

pub(crate) fn channel(
Expand Down
2 changes: 1 addition & 1 deletion rodbus/src/client/requests/read_registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ReadRegisters {
}

pub(crate) fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
self.request.get().serialize(cursor)
self.request.get().serialize(cursor, None)
}

pub(crate) fn failure(&mut self, err: RequestError) {
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
Expand Up @@ -95,7 +95,7 @@ where
}

pub(crate) fn serialize(&self, cursor: &mut WriteCursor) -> Result<(), RequestError> {
self.request.serialize(cursor)
self.request.serialize(cursor, None)
}

pub(crate) fn failure(&mut self, err: RequestError) {
Expand Down
62 changes: 61 additions & 1 deletion rodbus/src/common/frame.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::common::phys::PhysLayer;
use std::collections::HashMap;
use std::ops::Range;

use crate::common::buffer::ReadBuffer;
Expand All @@ -7,7 +8,7 @@ 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 crate::{DecodeLevel, ExceptionCode, FrameDecodeLevel, RecorderError};

use scursor::WriteCursor;

Expand Down Expand Up @@ -251,6 +252,11 @@ impl FormatType {
}
}

pub(crate) struct FrameRecords {
records: HashMap<&'static str, usize>,
//records: HashSet<usize>,
}

pub(crate) struct FrameWriter {
format_type: FormatType,
buffer: [u8; constants::MAX_FRAME_LENGTH],
Expand Down Expand Up @@ -280,6 +286,60 @@ impl std::fmt::Display for FunctionField {
}
}

impl FrameRecords {
pub(crate) fn new() -> Self {
Self {
records: HashMap::new(),
//records: HashSet::new(),
}
}

///Record a offset to fill in the value at a later point, but before it's send.
/// NOTE: Currently only works with byte values.
pub(crate) fn record(
&mut self,
key: &'static str,
cursor: &mut WriteCursor,
) -> Result<(), crate::InternalError> {
if self.records.contains_key(key) {
return Err(RecorderError::RecordKeyExists(key).into());
}

//Insert our new key and advance the cursor position to the next byte.
self.records.insert(key, cursor.position());
cursor.skip(1).unwrap();

Ok(())
}

///Tries to fill in the value at the recorded offset, returns an error if there is no corresponding
/// record found
pub(crate) fn fill_record(
&mut self,
cursor: &mut WriteCursor,
key: &'static str,
value: u8,
) -> Result<(), crate::InternalError> {
if let Some(position) = self.records.remove(key) {
let current_position = cursor.position();

//TODO(Kay): Handle possible errors of the cursor !
cursor.seek_to(position)?;
cursor.write_u8(value)?;
cursor.seek_to(current_position)?;

return Ok(());
}

Err(RecorderError::RecordDoesNotExist(key).into())
}

///Return true if there are no recorded offsets in our store.
pub(crate) fn records_empty(&self) -> bool {
self.records.is_empty()
}
}

impl FunctionField {
pub(crate) fn unknown(fc: u8) -> Self {
Self::UnknownFunction(fc)
Expand Down
Loading