Skip to content

Commit

Permalink
move related types to ReadHandler module
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamcrain committed May 20, 2024
1 parent 9541d3c commit df9ae61
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 69 deletions.
66 changes: 0 additions & 66 deletions dnp3/src/master/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::net::SocketAddr;
use std::time::{Duration, SystemTime};

use crate::app::attr::*;
use crate::app::*;

use crate::decode::DecodeLevel;
Expand Down Expand Up @@ -595,68 +594,3 @@ pub trait AssociationInformation: Send + Sync {
/// Called when an unsolicited response is received
fn unsolicited_response(&mut self, _is_duplicate: bool, _seq: Sequence) {}
}

/// Information about the object header and specific variation
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct HeaderInfo {
/// underlying variation in the response
pub variation: Variation,
/// qualifier code used in the response
pub qualifier: QualifierCode,
/// true if the received variation is an event type, false otherwise
pub is_event: bool,
/// true if a flags byte is present on the underlying variation, false otherwise
pub has_flags: bool,
}

impl HeaderInfo {
pub(crate) fn new(
variation: Variation,
qualifier: QualifierCode,
is_event: bool,
has_flags: bool,
) -> Self {
Self {
variation,
qualifier,
is_event,
has_flags,
}
}
}

/// Describes the source of a read event
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ReadType {
/// Startup integrity poll
StartupIntegrity,
/// Unsolicited message
Unsolicited,
/// Single poll requested by the user
SinglePoll,
/// Periodic poll configured by the user
PeriodicPoll,
}

pub(crate) fn handle_attribute(
var: Variation,
qualifier: QualifierCode,
attr: &Option<Attribute>,
handler: &mut dyn ReadHandler,
) {
if let Some(attr) = attr {
match AnyAttribute::try_from(attr) {
Ok(attr) => {
handler
.handle_device_attribute(HeaderInfo::new(var, qualifier, false, false), attr);
}
Err(err) => {
tracing::warn!(
"Expected attribute type {:?} but received {:?}",
err.expected,
err.actual
);
}
}
}
}
70 changes: 67 additions & 3 deletions dnp3/src/master/read_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::app::attr::AnyAttribute;
use crate::app::attr::{AnyAttribute, Attribute};
use crate::app::measurement::*;
use crate::app::{MaybeAsync, ResponseHeader};
use crate::master::{HeaderInfo, ReadType};
use crate::app::{MaybeAsync, QualifierCode, ResponseHeader, Variation};

/// Trait used to process measurement data received from an outstation
#[allow(unused_variables)]
Expand Down Expand Up @@ -130,3 +129,68 @@ pub trait ReadHandler: Send + Sync {
/// Process a device attribute
fn handle_device_attribute(&mut self, info: HeaderInfo, attr: AnyAttribute) {}
}

pub(crate) fn handle_attribute(
var: Variation,
qualifier: QualifierCode,
attr: &Option<Attribute>,
handler: &mut dyn ReadHandler,
) {
if let Some(attr) = attr {
match AnyAttribute::try_from(attr) {
Ok(attr) => {
handler
.handle_device_attribute(HeaderInfo::new(var, qualifier, false, false), attr);
}
Err(err) => {
tracing::warn!(
"Expected attribute type {:?} but received {:?}",
err.expected,
err.actual
);
}
}
}
}

/// Information about the object header and specific variation
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct HeaderInfo {
/// underlying variation in the response
pub variation: Variation,
/// qualifier code used in the response
pub qualifier: QualifierCode,
/// true if the received variation is an event type, false otherwise
pub is_event: bool,
/// true if a flags byte is present on the underlying variation, false otherwise
pub has_flags: bool,
}

impl HeaderInfo {
pub(crate) fn new(
variation: Variation,
qualifier: QualifierCode,
is_event: bool,
has_flags: bool,
) -> Self {
Self {
variation,
qualifier,
is_event,
has_flags,
}
}
}

/// Describes the source of a read event
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ReadType {
/// Startup integrity poll
StartupIntegrity,
/// Unsolicited message
Unsolicited,
/// Single poll requested by the user
SinglePoll,
/// Periodic poll configured by the user
PeriodicPoll,
}

0 comments on commit df9ae61

Please sign in to comment.