use alloc::string::String;
use alloc::vec::Vec;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
#[allow(missing_docs)] pub enum ServiceType {
GetServerDirectory,
Associate,
Abort,
Release,
GetLogicalDeviceDirectory,
GetAllDataValues,
GetDataValues,
SetDataValues,
GetDataDirectory,
GetDataDefinition,
GetDataSetValues,
SetDataSetValues,
CreateDataSet,
DeleteDataSet,
GetDataSetDirectory,
SelectActiveSG,
SelectEditSG,
SetEditSGValue,
ConfirmEditSGValues,
GetEditSGValue,
GetSGCBValues,
Report,
GetBRCBValues,
SetBRCBValues,
GetURCBValues,
SetURCBValues,
GetLCBValues,
SetLCBValues,
QueryLogByTime,
QueryLogAfter,
GetLogStatusValues,
SendGOOSEMessage,
GetGoCBValues,
SetGoCBValues,
GetGoReference,
GetGOOSEElementNumber,
SendMSVMessage,
GetMSVCBValues,
SetMSVCBValues,
SendUSVMessage,
GetUSVCBValues,
SetUSVCBValues,
Select,
SelectWithValue,
Cancel,
Operate,
CommandTermination,
TimeActivatedOperate,
GetFile,
SetFile,
DeleteFile,
GetFileAttributeValues,
TimeSynchronization,
InternalChange,
}
const SERVICE_TYPES: [(ServiceType, &str); 54] = [
(ServiceType::GetServerDirectory, "GetServerDirectory"),
(ServiceType::Associate, "Associate"),
(ServiceType::Abort, "Abort"),
(ServiceType::Release, "Release"),
(ServiceType::GetLogicalDeviceDirectory, "GetLogicalDeviceDirectory"),
(ServiceType::GetAllDataValues, "GetAllDataValues"),
(ServiceType::GetDataValues, "GetDataValues"),
(ServiceType::SetDataValues, "SetDataValues"),
(ServiceType::GetDataDirectory, "GetDataDirectory"),
(ServiceType::GetDataDefinition, "GetDataDefinition"),
(ServiceType::GetDataSetValues, "GetDataSetValues"),
(ServiceType::SetDataSetValues, "SetDataSetValues"),
(ServiceType::CreateDataSet, "CreateDataSet"),
(ServiceType::DeleteDataSet, "DeleteDataSet"),
(ServiceType::GetDataSetDirectory, "GetDataSetDirectory"),
(ServiceType::SelectActiveSG, "SelectActiveSG"),
(ServiceType::SelectEditSG, "SelectEditSG"),
(ServiceType::SetEditSGValue, "SetEditSGValue"),
(ServiceType::ConfirmEditSGValues, "ConfirmEditSGValues"),
(ServiceType::GetEditSGValue, "GetEditSGValue"),
(ServiceType::GetSGCBValues, "GetSGCBValues"),
(ServiceType::Report, "Report"),
(ServiceType::GetBRCBValues, "GetBRCBValues"),
(ServiceType::SetBRCBValues, "SetBRCBValues"),
(ServiceType::GetURCBValues, "GetURCBValues"),
(ServiceType::SetURCBValues, "SetURCBValues"),
(ServiceType::GetLCBValues, "GetLCBValues"),
(ServiceType::SetLCBValues, "SetLCBValues"),
(ServiceType::QueryLogByTime, "QueryLogByTime"),
(ServiceType::QueryLogAfter, "QueryLogAfter"),
(ServiceType::GetLogStatusValues, "GetLogStatusValues"),
(ServiceType::SendGOOSEMessage, "SendGOOSEMessage"),
(ServiceType::GetGoCBValues, "GetGoCBValues"),
(ServiceType::SetGoCBValues, "SetGoCBValues"),
(ServiceType::GetGoReference, "GetGoReference"),
(ServiceType::GetGOOSEElementNumber, "GetGOOSEElementNumber"),
(ServiceType::SendMSVMessage, "SendMSVMessage"),
(ServiceType::GetMSVCBValues, "GetMSVCBValues"),
(ServiceType::SetMSVCBValues, "SetMSVCBValues"),
(ServiceType::SendUSVMessage, "SendUSVMessage"),
(ServiceType::GetUSVCBValues, "GetUSVCBValues"),
(ServiceType::SetUSVCBValues, "SetUSVCBValues"),
(ServiceType::Select, "Select"),
(ServiceType::SelectWithValue, "SelectWithValue"),
(ServiceType::Cancel, "Cancel"),
(ServiceType::Operate, "Operate"),
(ServiceType::CommandTermination, "CommandTermination"),
(ServiceType::TimeActivatedOperate, "TimeActivatedOperate"),
(ServiceType::GetFile, "GetFile"),
(ServiceType::SetFile, "SetFile"),
(ServiceType::DeleteFile, "DeleteFile"),
(ServiceType::GetFileAttributeValues, "GetFileAttributeValues"),
(ServiceType::TimeSynchronization, "TimeSynchronization"),
(ServiceType::InternalChange, "InternalChange"),
];
impl ServiceType {
pub fn as_str(self) -> &'static str {
SERVICE_TYPES.iter().find(|(v, _)| *v == self).map_or("InternalChange", |(_, n)| *n)
}
pub fn parse(name: &str) -> Option<ServiceType> {
SERVICE_TYPES.iter().find(|(_, n)| *n == name).map(|(v, _)| *v)
}
pub fn table_ordinal(self) -> i64 {
SERVICE_TYPES.iter().position(|(v, _)| *v == self).map_or(0, |i| i as i64)
}
}
impl core::fmt::Display for ServiceType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
#[allow(missing_docs)] pub enum ServiceError {
InstanceNotAvailable,
InstanceInUse,
AccessViolation,
AccessNotAllowedInCurrentState,
ParameterValueInappropriate,
ParameterValueInconsistent,
ClassNotSupported,
InstanceLockedByOtherClient,
ControlMustBeSelected,
TypeConflict,
FailedDueToCommunicationsConstraint,
FailedDueToServerConstraint,
NoError,
}
const SERVICE_ERRORS: [(ServiceError, &str); 13] = [
(ServiceError::InstanceNotAvailable, "instance-not-available"),
(ServiceError::InstanceInUse, "instance-in-use"),
(ServiceError::AccessViolation, "access-violation"),
(ServiceError::AccessNotAllowedInCurrentState, "access-not-allowed-in-current-state"),
(ServiceError::ParameterValueInappropriate, "parameter-value-inappropriate"),
(ServiceError::ParameterValueInconsistent, "parameter-value-inconsistent"),
(ServiceError::ClassNotSupported, "class-not-supported"),
(ServiceError::InstanceLockedByOtherClient, "instance-locked-by-other-client"),
(ServiceError::ControlMustBeSelected, "control-must-be-selected"),
(ServiceError::TypeConflict, "type-conflict"),
(ServiceError::FailedDueToCommunicationsConstraint, "failed-due-to-communications-constraint"),
(ServiceError::FailedDueToServerConstraint, "failed-due-to-server-constraint"),
(ServiceError::NoError, "no-error"),
];
impl ServiceError {
pub fn as_str(self) -> &'static str {
SERVICE_ERRORS.iter().find(|(v, _)| *v == self).map_or("no-error", |(_, n)| *n)
}
pub fn parse(name: &str) -> Option<ServiceError> {
SERVICE_ERRORS.iter().find(|(_, n)| *n == name).map(|(v, _)| *v)
}
pub fn table_ordinal(self) -> i64 {
SERVICE_ERRORS.iter().position(|(v, _)| *v == self).map_or(0, |i| i as i64)
}
pub const fn from_data_access(code: i64) -> ServiceError {
match code {
0 | 4 | 10 => ServiceError::InstanceNotAvailable,
2 => ServiceError::AccessNotAllowedInCurrentState,
3 => ServiceError::AccessViolation,
5 | 11 => ServiceError::ParameterValueInappropriate,
6 | 7 => ServiceError::TypeConflict,
8 => ServiceError::ParameterValueInconsistent,
9 => ServiceError::ClassNotSupported,
_ => ServiceError::FailedDueToServerConstraint,
}
}
}
impl core::fmt::Display for ServiceError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TrackingCdc {
Cst,
Bts,
Uts,
Lts,
Ots,
Gts,
Mts,
Nts,
Sts,
Cts,
}
impl TrackingCdc {
pub const fn as_str(self) -> &'static str {
match self {
TrackingCdc::Cst => "CST",
TrackingCdc::Bts => "BTS",
TrackingCdc::Uts => "UTS",
TrackingCdc::Lts => "LTS",
TrackingCdc::Ots => "OTS",
TrackingCdc::Gts => "GTS",
TrackingCdc::Mts => "MTS",
TrackingCdc::Nts => "NTS",
TrackingCdc::Sts => "STS",
TrackingCdc::Cts => "CTS",
}
}
pub fn parse(cdc: &str) -> Option<TrackingCdc> {
[
TrackingCdc::Cst,
TrackingCdc::Bts,
TrackingCdc::Uts,
TrackingCdc::Lts,
TrackingCdc::Ots,
TrackingCdc::Gts,
TrackingCdc::Mts,
TrackingCdc::Nts,
TrackingCdc::Sts,
TrackingCdc::Cts,
]
.into_iter()
.find(|c| c.as_str() == cdc)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Tracked {
pub cdc: TrackingCdc,
pub obj_ref: String,
pub service: ServiceType,
pub error: ServiceError,
pub originator: Vec<u8>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_lists_are_the_standards_lists() {
for (v, n) in SERVICE_TYPES {
assert_eq!(ServiceType::parse(n), Some(v));
assert_eq!(v.as_str(), n);
}
for (v, n) in SERVICE_ERRORS {
assert_eq!(ServiceError::parse(n), Some(v));
assert_eq!(v.as_str(), n);
}
assert_eq!(ServiceType::GetServerDirectory.table_ordinal(), 0);
assert_eq!(ServiceType::SetBRCBValues.table_ordinal(), 23);
assert_eq!(ServiceError::NoError.table_ordinal(), 12);
assert_eq!(ServiceType::parse("NoSuchService"), None);
}
#[test]
fn a_refused_service_keeps_its_meaning_across_the_two_tables() {
assert_eq!(ServiceError::from_data_access(3), ServiceError::AccessViolation);
assert_eq!(ServiceError::from_data_access(10), ServiceError::InstanceNotAvailable);
assert_eq!(ServiceError::from_data_access(0), ServiceError::InstanceNotAvailable);
assert_eq!(ServiceError::from_data_access(7), ServiceError::TypeConflict);
assert_eq!(ServiceError::from_data_access(99), ServiceError::FailedDueToServerConstraint);
}
#[test]
fn a_tracking_cdc_is_recognised_by_the_cdc_the_file_declares() {
assert_eq!(TrackingCdc::parse("BTS"), Some(TrackingCdc::Bts));
assert_eq!(TrackingCdc::parse("CTS"), Some(TrackingCdc::Cts));
assert_eq!(TrackingCdc::parse("DPC"), None);
}
}