Skip to content

Commit

Permalink
No longer expose defaultds but instead use separate instance config.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 authored and folkertdev committed Jul 27, 2023
1 parent 2ed8bfc commit 5c27c8a
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 234 deletions.
25 changes: 11 additions & 14 deletions statime-linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use clap::Parser;
use fern::colors::Color;
use rand::{rngs::StdRng, SeedableRng};
use statime::{
BasicFilter, Clock, ClockIdentity, DefaultDS, DelayMechanism, Duration, Interval, PortAction,
PortActionIterator, PortConfig, PortIdentity, PtpInstance, SdoId, Time, TimePropertiesDS,
BasicFilter, Clock, ClockIdentity, DelayMechanism, Duration, InstanceConfig, Interval,
PortAction, PortActionIterator, PortConfig, PtpInstance, SdoId, Time, TimePropertiesDS,
TimeSource, TimestampContext,
};
use statime_linux::{
Expand Down Expand Up @@ -207,21 +207,18 @@ async fn actual_main() {
let mut network_runtime = LinuxRuntime::new(timestamping_mode, local_clock.clone());
let clock_identity = ClockIdentity(get_clock_id().expect("Could not get clock identity"));

let default_ds = DefaultDS::new_ordinary_clock(
let config = InstanceConfig {
clock_identity,
args.priority_1,
args.priority_2,
args.domain,
false,
args.sdo,
);
priority_1: args.priority_1,
priority_2: args.priority_2,
domain_number: args.domain,
slave_only: false,
sdo_id: args.sdo,
};

let time_properties_ds =
TimePropertiesDS::new_arbitrary_time(false, false, TimeSource::InternalOscillator);
let port_config = PortConfig {
port_identity: PortIdentity {
clock_identity,
port_number: 1,
},
delay_mechanism: DelayMechanism::E2E {
interval: Interval::TWO_SECONDS,
},
Expand All @@ -233,7 +230,7 @@ async fn actual_main() {
};

let instance = PtpInstance::new(
default_ds,
config,
time_properties_ds,
local_clock.clone(),
BasicFilter::new(0.25),
Expand Down
13 changes: 7 additions & 6 deletions statime/src/bmc/bmca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Bmca {
///
/// If None is returned, then the port should remain in the same state as it
/// is now.
pub fn calculate_recommended_state(
pub(crate) fn calculate_recommended_state(
own_data: &DefaultDS,
best_global_announce_message: Option<BestAnnounceMessage>,
best_port_announce_message: Option<BestAnnounceMessage>,
Expand Down Expand Up @@ -248,7 +248,7 @@ impl BestAnnounceMessage {
}

#[derive(Debug, PartialEq, Eq)]
pub enum RecommendedState {
pub(crate) enum RecommendedState {
M1(DefaultDS),
M2(DefaultDS),
M3(AnnounceMessage),
Expand All @@ -262,6 +262,7 @@ pub enum RecommendedState {
mod tests {
use super::*;
use crate::{
config::InstanceConfig,
datastructures::messages::{Header, PtpVersion},
ClockIdentity,
};
Expand Down Expand Up @@ -384,14 +385,14 @@ mod tests {
let slave_only = false;
let sdo_id = Default::default();

DefaultDS::new_ordinary_clock(
DefaultDS::new(InstanceConfig {
clock_identity,
priority_1,
priority_2,
domain_number,
slave_only,
sdo_id,
)
})
}

#[test]
Expand Down Expand Up @@ -424,14 +425,14 @@ mod tests {
let slave_only = false;
let sdo_id = Default::default();

let mut own_data = DefaultDS::new_ordinary_clock(
let mut own_data = DefaultDS::new(InstanceConfig {
clock_identity,
priority_1,
priority_2,
domain_number,
slave_only,
sdo_id,
);
});

own_data.clock_quality.clock_class = 1;
assert!((1..=127).contains(&own_data.clock_quality.clock_class));
Expand Down
8 changes: 4 additions & 4 deletions statime/src/bmc/dataset_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::datastructures::{
/// used to find out which source is better according to the dataset comparison
/// algorithm.
#[derive(Eq, PartialEq, Default, Debug)]
pub struct ComparisonDataset {
pub(crate) struct ComparisonDataset {
gm_priority_1: u8,
gm_identity: ClockIdentity,
gm_clock_quality: ClockQuality,
Expand All @@ -27,7 +27,7 @@ pub struct ComparisonDataset {
impl ComparisonDataset {
/// Create a ComparisonDataset from the data in an announce message and the
/// port identity of the port that received the announce message
pub fn from_announce_message(
pub(crate) fn from_announce_message(
message: &AnnounceMessage,
port_receiver_identity: &PortIdentity,
) -> Self {
Expand All @@ -42,7 +42,7 @@ impl ComparisonDataset {
}
}

pub fn from_own_data(data: &DefaultDS) -> Self {
pub(crate) fn from_own_data(data: &DefaultDS) -> Self {
Self {
gm_priority_1: data.priority_1,
gm_identity: data.clock_identity,
Expand All @@ -58,7 +58,7 @@ impl ComparisonDataset {
}

/// Returns the ordering of `self` in comparison to other.
pub fn compare(&self, other: &Self) -> DatasetOrdering {
pub(crate) fn compare(&self, other: &Self) -> DatasetOrdering {
if self.gm_identity == other.gm_identity {
Self::compare_same_identity(self, other)
} else {
Expand Down
11 changes: 11 additions & 0 deletions statime/src/config/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::{ClockIdentity, SdoId};

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct InstanceConfig {
pub clock_identity: ClockIdentity,
pub priority_1: u8,
pub priority_2: u8,
pub domain_number: u8,
pub slave_only: bool,
pub sdo_id: SdoId,
}
2 changes: 2 additions & 0 deletions statime/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod instance;
mod port;

pub use instance::InstanceConfig;
pub use port::{DelayMechanism, PortConfig};
6 changes: 3 additions & 3 deletions statime/src/config/port.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use rand::Rng;

use crate::{time::Interval, Duration, PortIdentity};
use crate::{time::Interval, Duration};

/// Which delay mechanism a port is using.
///
/// Currently, statime only supports the end to end (E2E) delay mechanism.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum DelayMechanism {
/// End to end delay mechanism. Delay measurement is done directly to the
/// chosen master, across potential transparent nodes in between.
Expand All @@ -17,8 +17,8 @@ pub enum DelayMechanism {

/// Configuration items of the PTP PortDS dataset. Dynamical fields are kept
/// as part of [crate::port::Port].
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct PortConfig {
pub port_identity: PortIdentity,
pub delay_mechanism: DelayMechanism,
pub announce_interval: Interval,
// more like announce_message_retries. Specifies how many announce_intervals to wait until the
Expand Down
2 changes: 1 addition & 1 deletion statime/src/datastructures/common/clock_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::datastructures::{WireFormat, WireFormatError};
///
/// Must have a unique value for each node in a ptp network. For notes on
/// generating these, see IEEE1588-2019 section 7.5.2.2
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
pub struct ClockIdentity(pub [u8; 8]);

impl WireFormat for ClockIdentity {
Expand Down
10 changes: 0 additions & 10 deletions statime/src/datastructures/common/instance_type.rs

This file was deleted.

2 changes: 0 additions & 2 deletions statime/src/datastructures/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod clock_accuracy;
mod clock_identity;
mod clock_quality;
mod instance_type;
mod leap_indicator;
mod port_identity;
mod time_interval;
Expand All @@ -14,7 +13,6 @@ mod tlv;
pub use clock_accuracy::*;
pub use clock_identity::*;
pub use clock_quality::*;
pub use instance_type::*;
pub use leap_indicator::*;
pub use port_identity::*;
pub use time_interval::*;
Expand Down
107 changes: 14 additions & 93 deletions statime/src/datastructures/datasets/default.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
config::InstanceConfig,
datastructures::{
common::{ClockIdentity, ClockQuality, InstanceType},
common::{ClockIdentity, ClockQuality},
messages::SdoId,
},
time::Time,
};

/// A concrete implementation of the PTP Default dataset (IEEE1588-2019 section
Expand All @@ -14,107 +14,28 @@ use crate::{
/// those related to timebase, which is contained in the
/// [TimePropertiesDS](crate::TimePropertiesDS) dataset.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct DefaultDS {
pub(crate) struct DefaultDS {
pub(crate) clock_identity: ClockIdentity,
number_ports: u16,
pub(crate) number_ports: u16,
pub(crate) clock_quality: ClockQuality,
pub(crate) priority_1: u8,
pub(crate) priority_2: u8,
pub(crate) domain_number: u8,
slave_only: bool,
pub(crate) slave_only: bool,
pub(crate) sdo_id: SdoId,
current_time: Time,
pub(crate) instance_enable: bool,
external_port_configuration_enabled: bool,
max_steps_removed: u8,
pub(crate) instance_type: InstanceType,
}

impl DefaultDS {
/// Create a Default dataset for an ordinary clock
///
/// The `domain_number` and `sdo_id` together control which time network the
/// clock connects to. SDO id's are assigned through the PTP standard, and
/// allowed values for domain numbers are specified in the assignment. A PTP
/// instance will only communicate with instances with matching domain and
/// sdo id.
///
/// The priority values are used in selecting the primary time source within
/// a ptp network. A clock with a higher `priority_1` is always preferred.
/// The `priority_2` field is only used as a tiebreaker among nodes in the
/// PTP network that have both identical `priority_1` values and advertise
/// clock/time precision that are identical.
///
/// `clock_identity` should be the identifier for this clock. It should
/// typically be derived from the mac address of one of the interfaces of
/// the device running the PTP instance, as described in IEEE1588-2019
/// section 7.5.2.2.
pub fn new_ordinary_clock(
clock_identity: ClockIdentity,
priority_1: u8,
priority_2: u8,
domain_number: u8,
slave_only: bool,
sdo_id: SdoId,
) -> Self {
DefaultDS {
clock_identity,
number_ports: 1,
pub(crate) fn new(config: InstanceConfig) -> Self {
Self {
clock_identity: config.clock_identity,
number_ports: 0,
clock_quality: Default::default(),
priority_1,
priority_2,
domain_number,
slave_only,
sdo_id,
current_time: Default::default(),
instance_enable: true,
external_port_configuration_enabled: false,
max_steps_removed: 255,
instance_type: InstanceType::OrdinaryClock,
}
}

/// Create a Default dataset for an boundary clock
///
/// The `domain_number` and `sdo_id` together control which time network the
/// clock connects to. SDO id's are assigned through the PTP standard, and
/// allowed values for domain numbers are specified in the assignment. A PTP
/// instance will only communicate with instances with matching domain and
/// sdo id.
///
/// The priority values are used in selecting the primary time source within
/// a ptp network. A clock with a higher `priority_1` is always preferred.
/// The `priority_2` field is only used as a tiebreaker among nodes in the
/// PTP network that have both identical `priority_1` values and advertise
/// clock/time precision that are identical.
///
/// `clock_identity` should be the identifier for this clock. It should
/// typically be derived from the mac address of one of the interfaces of
/// the device running the PTP instance, as described in IEEE1588-2019
/// section 7.5.2.2.
pub fn new_boundary_clock(
clock_identity: ClockIdentity,
number_ports: u16,
priority_1: u8,
priority_2: u8,
domain_number: u8,
sdo_id: SdoId,
) -> Self {
DefaultDS {
clock_identity,
number_ports,
clock_quality: Default::default(),
priority_1,
priority_2,
domain_number,
// Not applicable
slave_only: false,
sdo_id,
current_time: Default::default(),
instance_enable: true,
external_port_configuration_enabled: false,
max_steps_removed: 255,
instance_type: InstanceType::BoundaryClock,
priority_1: config.priority_1,
priority_2: config.priority_2,
domain_number: config.domain_number,
slave_only: config.slave_only,
sdo_id: config.sdo_id,
}
}
}
6 changes: 3 additions & 3 deletions statime/src/datastructures/datasets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use current::CurrentDS;
pub use default::DefaultDS;
pub use parent::ParentDS;
pub(crate) use current::CurrentDS;
pub(crate) use default::DefaultDS;
pub(crate) use parent::ParentDS;
pub use time_properties::TimePropertiesDS;

mod current;
Expand Down
8 changes: 3 additions & 5 deletions statime/src/datastructures/datasets/parent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
datastructures::common::{ClockIdentity, ClockQuality, PortIdentity},
DefaultDS,
};
use super::DefaultDS;
use crate::datastructures::common::{ClockIdentity, ClockQuality, PortIdentity};

// TODO: Discuss moving this (and TimePropertiesDS, ...) to slave?
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -17,7 +15,7 @@ pub struct ParentDS {
}

impl ParentDS {
pub fn new(default_ds: DefaultDS) -> Self {
pub(crate) fn new(default_ds: DefaultDS) -> Self {
ParentDS {
parent_port_identity: PortIdentity {
clock_identity: default_ds.clock_identity,
Expand Down
2 changes: 1 addition & 1 deletion statime/src/datastructures/messages/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Default for Header {
/// A wrapper type for PTP Sdo Identifiers.
///
/// This is a separate type as sdo identifiers should be in the range 0-4095
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct SdoId(u16);

impl core::fmt::Display for SdoId {
Expand Down
Loading

0 comments on commit 5c27c8a

Please sign in to comment.