Skip to content

Commit

Permalink
UpdateFlags(..) in the FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamcrain committed May 23, 2024
1 parent 4f47e99 commit 61f78f1
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dnp3/examples/outstation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ async fn run_outstation(
7,
UpdateFlagsType::AnalogInput,
Flags::COMM_LOST,
get_current_time(),
Some(get_current_time()),
UpdateOptions::detect_event(),
);
}),
Expand Down
4 changes: 2 additions & 2 deletions dnp3/src/outstation/database/details/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Database {
index: u16,
flags_type: UpdateFlagsType,
flags: Flags,
time: Time,
time: Option<Time>,
options: UpdateOptions,
) -> UpdateInfo {
match flags_type {
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Database {
&mut self,
index: u16,
flags: Flags,
timestamp: Time,
timestamp: Option<Time>,
options: UpdateOptions,
) -> UpdateInfo {
match self.static_db.get::<T>(index) {
Expand Down
30 changes: 15 additions & 15 deletions dnp3/src/outstation/database/details/range/static_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) trait Updatable: Insertable + Clone + Default {
}

pub(crate) trait UpdatableFlags: Updatable {
fn update_flags(&mut self, flags: Flags, time: Time);
fn update_flags(&mut self, flags: Flags, time: Option<Time>);
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -748,9 +748,9 @@ impl Updatable for BinaryInput {
}

impl UpdatableFlags for BinaryInput {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}

Expand All @@ -776,9 +776,9 @@ impl Updatable for DoubleBitBinaryInput {
}

impl UpdatableFlags for DoubleBitBinaryInput {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}

Expand All @@ -804,9 +804,9 @@ impl Updatable for BinaryOutputStatus {
}

impl UpdatableFlags for BinaryOutputStatus {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}
impl Updatable for Counter {
Expand All @@ -831,9 +831,9 @@ impl Updatable for Counter {
}

impl UpdatableFlags for Counter {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}

Expand All @@ -859,9 +859,9 @@ impl Updatable for FrozenCounter {
}

impl UpdatableFlags for FrozenCounter {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}

Expand All @@ -887,9 +887,9 @@ impl Updatable for AnalogInput {
}

impl UpdatableFlags for AnalogInput {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}

Expand All @@ -915,9 +915,9 @@ impl Updatable for AnalogOutputStatus {
}

impl UpdatableFlags for AnalogOutputStatus {
fn update_flags(&mut self, flags: Flags, time: Time) {
fn update_flags(&mut self, flags: Flags, time: Option<Time>) {
self.flags = flags;
self.time = Some(time);
self.time = time;
}
}

Expand Down
8 changes: 4 additions & 4 deletions dnp3/src/outstation/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub trait Update<T> {
fn update2(&mut self, index: u16, value: &T, options: UpdateOptions) -> UpdateInfo;
}

/// Point on which to update the flags
/// Point type on which to update the flags
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum UpdateFlagsType {
Expand All @@ -331,13 +331,13 @@ pub enum UpdateFlagsType {
pub trait UpdateFlags {
/// Update the flags for the specified point without changing the value
///
/// This is equivalent to getting the current value, changing the flags and the value, then updating
/// This is equivalent to getting the current value, changing the flags and the timestamp, then calling update
fn update_flags(
&mut self,
index: u16,
flags_type: UpdateFlagsType,
flags: Flags,
time: Time,
time: Option<Time>,
options: UpdateOptions,
) -> UpdateInfo;
}
Expand Down Expand Up @@ -518,7 +518,7 @@ impl UpdateFlags for Database {
index: u16,
flags_type: UpdateFlagsType,
flags: Flags,
time: Time,
time: Option<Time>,
options: UpdateOptions,
) -> UpdateInfo {
self.inner
Expand Down
8 changes: 8 additions & 0 deletions ffi/bindings/dotnet/examples/outstation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ private static void RunOutstation(Outstation outstation)
});
break;
}
case "aif":
{
outstation.Transaction(db =>
{
db.UpdateFlags(7, UpdateFlagsType.AnalogInput, new Flags(Flag.CommLost), Now(), detectEvent);
});
break;
}
case "aos":
{
outstation.Transaction(db =>
Expand Down
37 changes: 37 additions & 0 deletions ffi/dnp3-ffi/src/outstation/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,43 @@ macro_rules! implement_database_point_operations {
};
}

pub(crate) unsafe fn database_update_flags(
instance: *mut crate::Database,
index: u16,
flags_type: ffi::UpdateFlagsType,
flags: ffi::Flags,
time: ffi::Timestamp,
options: ffi::UpdateOptions,
) -> ffi::UpdateInfo {
let db = match instance.as_mut() {
None => return ffi::UpdateInfoFields::default().into(),
Some(db) => db,
};

db.update_flags(
index,
flags_type.into(),
(&flags).into(),
(&time).into(),
options.into(),
)
.into()
}

impl From<ffi::UpdateFlagsType> for UpdateFlagsType {
fn from(value: ffi::UpdateFlagsType) -> Self {
match value {
ffi::UpdateFlagsType::BinaryInput => Self::BinaryInput,
ffi::UpdateFlagsType::DoubleBitBinaryInput => Self::DoubleBitBinaryInput,
ffi::UpdateFlagsType::BinaryOutputStatus => Self::BinaryOutputStatus,
ffi::UpdateFlagsType::Counter => Self::Counter,
ffi::UpdateFlagsType::FrozenCounter => Self::FrozenCounter,
ffi::UpdateFlagsType::AnalogInput => Self::AnalogInput,
ffi::UpdateFlagsType::AnalogOutputStatus => Self::AnalogOutputStatus,
}
}
}

implement_database_point_operations!(
database_add_binary_input,
database_remove_binary_input,
Expand Down
35 changes: 34 additions & 1 deletion ffi/dnp3-schema/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ fn define_update_options(lib: &mut LibraryBuilder) -> BackTraced<FunctionArgStru
Ok(update_options)
}

fn define_update_flags_type(lib: &mut LibraryBuilder) -> BackTraced<EnumHandle> {
let x = lib
.define_enum("update_flags_type")?
.doc("Point type on which to update the flags")?
.push("binary_input", "Binary input")?
.push("double_bit_binary_input", "Doubl-bit binary input ")?
.push("binary_output_status", "Binary output status")?
.push("counter", "Counter")?
.push("frozen_counter", "Frozen counter")?
.push("analog_input", "Analog input")?
.push("analog_output_status", "Analog output status")?
.build()?;

Ok(x)
}

fn define_binary_config(lib: &mut LibraryBuilder) -> BackTraced<FunctionArgStructHandle> {
let binary_static_variation = lib
.define_enum("static_binary_input_variation")?
Expand Down Expand Up @@ -543,9 +559,24 @@ pub(crate) fn define_database(
.build()?;

let update_info = define_update_info(lib)?;

let update_options = define_update_options(lib)?;

let update_flags_type = define_update_flags_type(lib)?;

let update_flags = lib
.define_method("update_flags", database.clone())?
.doc(
doc("Update the flags for the specified point without changing the value")
.details("This is equivalent to getting the current value, changing the flags and the timestamp, then calling update")
)?
.param("index", Primitive::U16, "Index on which to perform the operation")?
.param("flags_type", update_flags_type, "Point type on which to perform the operation")?
.param("flags", shared_def.flags.clone(), "New flags applied to the point")?
.param("time", shared_def.timestamp.clone(), "New timestamp applied to the point")?
.param("options", update_options.clone(), "Options that control how events and static values are handled")?
.returns(update_info.clone(), "Provides detailed information about what occurred during the update operation")?
.build()?;

// Binary Input
let binary_config = define_binary_config(lib)?;

Expand Down Expand Up @@ -1220,6 +1251,8 @@ pub(crate) fn define_database(

let database = lib
.define_class(&database)?
// works on all types
.method(update_flags)?
// binary methods
.method(add_binary)?
.method(remove_binary)?
Expand Down
4 changes: 4 additions & 0 deletions ffi/dnp3-schema/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub(crate) struct SharedDefinitions {
pub control_field_struct: CallbackArgStructHandle,
pub g12v1_struct: UniversalStructHandle,
pub function_code: EnumHandle,
pub flags: UniversalStructHandle,
pub timestamp: UniversalStructHandle,
pub binary_point: UniversalStructHandle,
pub binary_it: AbstractIteratorHandle,
pub double_bit_binary_point: UniversalStructHandle,
Expand Down Expand Up @@ -335,6 +337,8 @@ pub(crate) fn define(lib: &mut LibraryBuilder) -> BackTraced<SharedDefinitions>
control_field_struct,
g12v1_struct,
function_code: define_function_code(lib)?,
flags: flags_struct,
timestamp: timestamp_struct,
binary_point,
binary_it,
double_bit_binary_point,
Expand Down

0 comments on commit 61f78f1

Please sign in to comment.