use alloc::string::String;
use alloc::vec::Vec;
use crate::common::{Edition, Error, Result, UtcTime};
pub use crate::common::ControlModel;
use crate::proto::data::{Typed, Value};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum OriginCategory {
NotSupported,
BayControl,
StationControl,
#[default]
RemoteControl,
AutomaticBay,
AutomaticStation,
AutomaticRemote,
Maintenance,
Process,
Other(i64),
}
impl OriginCategory {
pub const fn from_code(code: i64) -> OriginCategory {
match code {
0 => OriginCategory::NotSupported,
1 => OriginCategory::BayControl,
2 => OriginCategory::StationControl,
3 => OriginCategory::RemoteControl,
4 => OriginCategory::AutomaticBay,
5 => OriginCategory::AutomaticStation,
6 => OriginCategory::AutomaticRemote,
7 => OriginCategory::Maintenance,
8 => OriginCategory::Process,
other => OriginCategory::Other(other),
}
}
pub const fn to_code(self) -> i64 {
match self {
OriginCategory::NotSupported => 0,
OriginCategory::BayControl => 1,
OriginCategory::StationControl => 2,
OriginCategory::RemoteControl => 3,
OriginCategory::AutomaticBay => 4,
OriginCategory::AutomaticStation => 5,
OriginCategory::AutomaticRemote => 6,
OriginCategory::Maintenance => 7,
OriginCategory::Process => 8,
OriginCategory::Other(v) => v,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Origin {
pub category: OriginCategory,
pub identifier: Vec<u8>,
}
impl Origin {
pub fn new(category: OriginCategory, identifier: &str) -> Origin {
Origin { category, identifier: identifier.as_bytes().to_vec() }
}
pub fn to_value(&self) -> Value {
Value::Structure(alloc::vec![Value::Integer(self.category.to_code()), Value::OctetString(self.identifier.clone())])
}
pub fn from_value(v: &Value) -> Option<Origin> {
let members = v.members()?;
let category = match members.first()? {
Value::Integer(i) => OriginCategory::from_code(*i),
Value::Unsigned(u) => OriginCategory::from_code(i64::try_from(*u).ok()?),
_ => return None,
};
let identifier = match members.get(1)? {
Value::OctetString(b) => b.clone(),
Value::VisibleString(s) | Value::MmsString(s) => s.as_bytes().to_vec(),
_ => return None,
};
Some(Origin { category, identifier })
}
pub fn identifier_str(&self) -> Option<&str> {
core::str::from_utf8(&self.identifier).ok()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Check {
pub synchro: bool,
pub interlock: bool,
}
impl Check {
pub const NONE: Check = Check { synchro: false, interlock: false };
pub const BOTH: Check = Check { synchro: true, interlock: true };
pub fn to_value(self) -> Value {
let byte = u8::from(self.synchro) << 7 | u8::from(self.interlock) << 6;
Value::BitString { unused: 6, bytes: alloc::vec![byte] }
}
pub fn from_value(v: &Value) -> Option<Check> {
match v {
Value::BitString { unused: 6, bytes } => {
let b = *bytes.first()?;
Some(Check { synchro: b & 0x80 != 0, interlock: b & 0x40 != 0 })
}
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ControlRequest {
pub ctl_val: Value,
pub oper_tm: Option<UtcTime>,
pub origin: Origin,
pub ctl_num: u8,
pub t: UtcTime,
pub test: bool,
pub check: Check,
}
impl ControlRequest {
pub fn new(ctl_val: Value, ctl_num: u8, t: UtcTime) -> ControlRequest {
ControlRequest { ctl_val, oper_tm: None, origin: Origin::default(), ctl_num, t, test: false, check: Check::NONE }
}
pub fn to_value(&self) -> Value {
let mut members = Vec::with_capacity(7);
members.push(self.ctl_val.clone());
if let Some(tm) = self.oper_tm {
members.push(Value::UtcTime(tm));
}
members.push(self.origin.to_value());
members.push(Value::Unsigned(u64::from(self.ctl_num)));
members.push(Value::UtcTime(self.t));
members.push(Value::Boolean(self.test));
members.push(self.check.to_value());
Value::Structure(members)
}
pub fn to_cancel_value(&self) -> Value {
let mut v = self.to_value();
if let Value::Structure(members) = &mut v {
members.pop();
}
v
}
pub fn from_value(v: &Value) -> Result<ControlRequest> {
let members = v.members().ok_or(Error::InvalidValue("a control request is a structure"))?;
let bad = || Error::InvalidValue("control request structure");
let ctl_val = members.first().ok_or_else(bad)?.clone();
let mut at = 1usize;
let oper_tm = match members.get(at) {
Some(Value::UtcTime(t)) => {
at += 1;
Some(*t)
}
_ => None,
};
let origin = Origin::from_value(members.get(at).ok_or_else(bad)?).ok_or_else(bad)?;
at += 1;
let ctl_num = match members.get(at) {
Some(Value::UtcTime(_)) | None => 0,
Some(v) => {
at += 1;
u8::try_from(v.as_u64().ok_or_else(bad)?).map_err(|_| bad())?
}
};
let t = members.get(at).ok_or_else(bad)?.as_utc_time().ok_or_else(bad)?;
at += 1;
let test = members.get(at).ok_or_else(bad)?.as_bool().ok_or_else(bad)?;
at += 1;
let check = match members.get(at) {
Some(v) => Check::from_value(v).ok_or_else(bad)?,
None => Check::NONE,
};
Ok(ControlRequest { ctl_val, oper_tm, origin, ctl_num, t, test, check })
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum ControlError {
#[default]
NoError,
Unknown,
TimeoutTestNotOk,
OperatorTestNotOk,
Other(i64),
}
impl ControlError {
pub const fn from_code(code: i64) -> ControlError {
match code {
0 => ControlError::NoError,
1 => ControlError::Unknown,
2 => ControlError::TimeoutTestNotOk,
3 => ControlError::OperatorTestNotOk,
other => ControlError::Other(other),
}
}
pub const fn to_code(self) -> i64 {
match self {
ControlError::NoError => 0,
ControlError::Unknown => 1,
ControlError::TimeoutTestNotOk => 2,
ControlError::OperatorTestNotOk => 3,
ControlError::Other(v) => v,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum AddCause {
#[default]
Unknown,
NotSupported,
BlockedBySwitchingHierarchy,
SelectFailed,
InvalidPosition,
PositionReached,
ParameterChangeInExecution,
StepLimit,
BlockedByMode,
BlockedByProcess,
BlockedByInterlocking,
BlockedBySynchrocheck,
CommandAlreadyInExecution,
BlockedByHealth,
OneOfNControl,
AbortionByCancel,
TimeLimitOver,
AbortionByTrip,
ObjectNotSelected,
ObjectAlreadySelected,
NoAccessAuthority,
EndedWithOvershoot,
AbortionDueToDeviation,
AbortionByCommunicationLoss,
AbortionByCommand,
None,
InconsistentParameters,
LockedByOtherClient,
Other(i64),
}
impl AddCause {
pub const fn from_code(code: i64) -> AddCause {
match code {
0 => AddCause::Unknown,
1 => AddCause::NotSupported,
2 => AddCause::BlockedBySwitchingHierarchy,
3 => AddCause::SelectFailed,
4 => AddCause::InvalidPosition,
5 => AddCause::PositionReached,
6 => AddCause::ParameterChangeInExecution,
7 => AddCause::StepLimit,
8 => AddCause::BlockedByMode,
9 => AddCause::BlockedByProcess,
10 => AddCause::BlockedByInterlocking,
11 => AddCause::BlockedBySynchrocheck,
12 => AddCause::CommandAlreadyInExecution,
13 => AddCause::BlockedByHealth,
14 => AddCause::OneOfNControl,
15 => AddCause::AbortionByCancel,
16 => AddCause::TimeLimitOver,
17 => AddCause::AbortionByTrip,
18 => AddCause::ObjectNotSelected,
19 => AddCause::ObjectAlreadySelected,
20 => AddCause::NoAccessAuthority,
21 => AddCause::EndedWithOvershoot,
22 => AddCause::AbortionDueToDeviation,
23 => AddCause::AbortionByCommunicationLoss,
24 => AddCause::AbortionByCommand,
25 => AddCause::None,
26 => AddCause::InconsistentParameters,
27 => AddCause::LockedByOtherClient,
other => AddCause::Other(other),
}
}
pub const fn to_code(self) -> i64 {
match self {
AddCause::Unknown => 0,
AddCause::NotSupported => 1,
AddCause::BlockedBySwitchingHierarchy => 2,
AddCause::SelectFailed => 3,
AddCause::InvalidPosition => 4,
AddCause::PositionReached => 5,
AddCause::ParameterChangeInExecution => 6,
AddCause::StepLimit => 7,
AddCause::BlockedByMode => 8,
AddCause::BlockedByProcess => 9,
AddCause::BlockedByInterlocking => 10,
AddCause::BlockedBySynchrocheck => 11,
AddCause::CommandAlreadyInExecution => 12,
AddCause::BlockedByHealth => 13,
AddCause::OneOfNControl => 14,
AddCause::AbortionByCancel => 15,
AddCause::TimeLimitOver => 16,
AddCause::AbortionByTrip => 17,
AddCause::ObjectNotSelected => 18,
AddCause::ObjectAlreadySelected => 19,
AddCause::NoAccessAuthority => 20,
AddCause::EndedWithOvershoot => 21,
AddCause::AbortionDueToDeviation => 22,
AddCause::AbortionByCommunicationLoss => 23,
AddCause::AbortionByCommand => 24,
AddCause::None => 25,
AddCause::InconsistentParameters => 26,
AddCause::LockedByOtherClient => 27,
AddCause::Other(v) => v,
}
}
pub const fn since(self) -> Edition {
match self.to_code() {
0..=18 => Edition::Ed1,
19..=27 => Edition::Ed2,
_ => Edition::Ed2_1,
}
}
#[must_use]
pub const fn for_edition(self, edition: Edition) -> AddCause {
let defined = matches!((self.since(), edition), (Edition::Ed1, _) | (Edition::Ed2, Edition::Ed2 | Edition::Ed2_1) | (Edition::Ed2_1, Edition::Ed2_1));
if defined {
self
} else if matches!((self, edition), (AddCause::ObjectAlreadySelected, Edition::Ed1)) {
AddCause::SelectFailed
} else {
AddCause::Unknown
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LastApplError {
pub control_object: String,
pub error: ControlError,
pub origin: Origin,
pub ctl_num: u8,
pub add_cause: AddCause,
}
impl LastApplError {
pub fn from_value(v: &Value) -> Result<LastApplError> {
let m = v.members().ok_or(Error::InvalidValue("LastApplError is a structure"))?;
let bad = || Error::InvalidValue("LastApplError structure");
Ok(LastApplError {
control_object: String::from(m.first().ok_or_else(bad)?.as_str().ok_or_else(bad)?),
error: ControlError::from_code(m.get(1).ok_or_else(bad)?.as_i64().ok_or_else(bad)?),
origin: Origin::from_value(m.get(2).ok_or_else(bad)?).ok_or_else(bad)?,
ctl_num: u8::try_from(m.get(3).ok_or_else(bad)?.as_u64().ok_or_else(bad)?).map_err(|_| bad())?,
add_cause: AddCause::from_code(m.get(4).ok_or_else(bad)?.as_i64().ok_or_else(bad)?),
})
}
pub fn to_value(&self) -> Value {
Value::Structure(alloc::vec![
Value::VisibleString(self.control_object.clone()),
Value::Integer(self.error.to_code()),
self.origin.to_value(),
Value::Unsigned(u64::from(self.ctl_num)),
Value::Integer(self.add_cause.to_code()),
])
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum CommandTermination {
Positive {
control_object: String,
request: ControlRequest,
},
Negative {
error: LastApplError,
request: Option<ControlRequest>,
},
}
impl CommandTermination {
pub fn ctl_num(&self) -> u8 {
match self {
CommandTermination::Positive { request, .. } => request.ctl_num,
CommandTermination::Negative { error, .. } => error.ctl_num,
}
}
pub fn control_object(&self) -> &str {
match self {
CommandTermination::Positive { control_object, .. } => control_object,
CommandTermination::Negative { error, .. } => &error.control_object,
}
}
pub const fn is_positive(&self) -> bool {
matches!(self, CommandTermination::Positive { .. })
}
pub const fn add_cause(&self) -> Option<AddCause> {
match self {
CommandTermination::Negative { error, .. } => Some(error.add_cause),
CommandTermination::Positive { .. } => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::TimeQuality;
use crate::proto::data::Dbpos;
fn now() -> UtcTime {
UtcTime::from_unix(1_700_000_000, 0, TimeQuality::SYNCHRONIZED)
}
#[test]
fn an_oper_round_trips_with_and_without_oper_tm() {
let mut r = ControlRequest::new(Value::dbpos(Dbpos::On), 4, now());
r.origin = Origin::new(OriginCategory::StationControl, "hmi-1");
r.check = Check::BOTH;
let v = r.to_value();
assert_eq!(v.members().map(<[Value]>::len), Some(6), "ctlVal, origin, ctlNum, T, Test, Check");
assert_eq!(ControlRequest::from_value(&v).unwrap(), r);
r.oper_tm = Some(now());
let v = r.to_value();
assert_eq!(v.members().map(<[Value]>::len), Some(7));
assert_eq!(ControlRequest::from_value(&v).unwrap(), r);
}
#[test]
fn an_oper_from_a_device_without_ctl_num_still_decodes() {
let v = Value::Structure(alloc::vec![
Value::Boolean(true),
Origin::new(OriginCategory::StationControl, "hmi").to_value(),
Value::UtcTime(now()),
Value::Boolean(false),
Check::NONE.to_value(),
]);
let r = ControlRequest::from_value(&v).unwrap();
assert_eq!((r.ctl_num, r.t, r.oper_tm), (0, now(), None));
assert_eq!(r.ctl_val, Value::Boolean(true));
}
#[test]
fn a_cancel_is_an_oper_without_the_check() {
let r = ControlRequest::new(Value::Boolean(true), 9, now());
let v = r.to_cancel_value();
assert_eq!(v.members().map(<[Value]>::len), Some(5));
let back = ControlRequest::from_value(&v).unwrap();
assert_eq!(back.check, Check::NONE, "a cancel asks for no checks because it carries none");
assert_eq!((back.ctl_num, back.ctl_val), (9, Value::Boolean(true)));
}
#[test]
fn the_check_bits_are_synchro_then_interlock() {
assert_eq!(Check { synchro: true, interlock: false }.to_value(), Value::BitString { unused: 6, bytes: alloc::vec![0b1000_0000] });
assert_eq!(Check { synchro: false, interlock: true }.to_value(), Value::BitString { unused: 6, bytes: alloc::vec![0b0100_0000] });
for c in [Check::NONE, Check::BOTH, Check { synchro: true, interlock: false }, Check { synchro: false, interlock: true }] {
assert_eq!(Check::from_value(&c.to_value()), Some(c));
}
assert_eq!(Check::from_value(&Value::quality(crate::common::Quality::GOOD)), None);
assert_eq!(Check::from_value(&Value::Boolean(true)), None);
assert_eq!(Check::from_value(&Value::dbpos(Dbpos::On)), Some(Check { synchro: true, interlock: false }));
}
#[test]
fn last_appl_error_names_the_reason_a_breaker_did_not_move() {
let e = LastApplError {
control_object: String::from("IED1LD0/CSWI1$CO$Pos$Oper"),
error: ControlError::Unknown,
origin: Origin::new(OriginCategory::RemoteControl, "scada"),
ctl_num: 4,
add_cause: AddCause::BlockedByInterlocking,
};
assert_eq!(LastApplError::from_value(&e.to_value()).unwrap(), e);
assert_eq!(e.add_cause.to_code(), 10);
for code in 0..=27 {
assert_eq!(AddCause::from_code(code).to_code(), code);
}
assert_eq!(AddCause::from_code(99), AddCause::Other(99));
for code in 0..=3 {
assert_eq!(ControlError::from_code(code).to_code(), code);
}
}
#[test]
fn the_control_model_says_which_services_are_legal() {
assert!(!ControlModel::DirectNormal.needs_select());
assert!(ControlModel::SboNormal.needs_select() && !ControlModel::SboNormal.select_carries_value());
assert!(ControlModel::SboEnhanced.select_carries_value());
assert!(ControlModel::SboEnhanced.enhanced_security() && ControlModel::DirectEnhanced.enhanced_security());
assert!(!ControlModel::DirectNormal.enhanced_security());
for code in 0..=4 {
assert_eq!(ControlModel::from_code(code).unwrap().to_code(), code);
}
assert_eq!(ControlModel::from_code(5), None);
}
#[test]
fn an_origin_identifier_may_be_octets_or_text() {
let o = Origin::new(OriginCategory::Maintenance, "tool");
assert_eq!(Origin::from_value(&o.to_value()), Some(o.clone()));
assert_eq!(o.identifier_str(), Some("tool"));
let binary = Origin { category: OriginCategory::Process, identifier: alloc::vec![0x00, 0xFF, 0x80] };
assert_eq!(binary.identifier_str(), None);
assert_eq!(Origin::from_value(&binary.to_value()), Some(binary));
assert_eq!(OriginCategory::from_code(42), OriginCategory::Other(42));
}
#[test]
fn a_termination_is_tied_to_its_command_by_ctl_num() {
let r = ControlRequest::new(Value::Boolean(true), 7, now());
let positive = CommandTermination::Positive { control_object: String::from("IED1LD0/CSWI1$CO$Pos$Oper"), request: r.clone() };
assert!(positive.is_positive());
assert_eq!(positive.ctl_num(), 7);
let negative = CommandTermination::Negative {
error: LastApplError {
control_object: String::from("IED1LD0/CSWI1$CO$Pos$Oper"),
ctl_num: 7,
add_cause: AddCause::TimeLimitOver,
..Default::default()
},
request: Some(r),
};
assert!(!negative.is_positive());
assert_eq!((negative.ctl_num(), negative.control_object()), (7, "IED1LD0/CSWI1$CO$Pos$Oper"));
}
#[test]
fn every_add_cause_is_sent_as_its_edition_defines_it() {
use AddCause as A;
let ed1_fallback = |c: A| match c {
A::ObjectAlreadySelected => A::SelectFailed,
_ => A::Unknown,
};
for code in 0..=28 {
let c = A::from_code(code);
assert_eq!(c.to_code(), code, "round trip of {code}");
let (ed1, ed2, ed21) = (c.for_edition(Edition::Ed1), c.for_edition(Edition::Ed2), c.for_edition(Edition::Ed2_1));
match code {
0..=18 => {
assert_eq!(c.since(), Edition::Ed1, "{code}");
assert_eq!((ed1, ed2, ed21), (c, c, c), "{code}");
}
19..=27 => {
assert_eq!(c.since(), Edition::Ed2, "{code}");
assert_eq!((ed1, ed2, ed21), (ed1_fallback(c), c, c), "{code}");
}
_ => {
assert_eq!(c.since(), Edition::Ed2_1, "{code}");
assert_eq!((ed1, ed2, ed21), (A::Unknown, A::Unknown, c), "{code}");
}
}
for e in [Edition::Ed1, Edition::Ed2, Edition::Ed2_1] {
assert!(c.for_edition(e).since() <= e, "{code} at {e:?} is a value that edition defines");
}
}
assert_eq!(A::ObjectAlreadySelected.for_edition(Edition::Ed1).to_code(), 3);
assert_eq!(A::LockedByOtherClient.for_edition(Edition::Ed1).to_code(), 0);
}
}