1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
//! Abstraction of a network [`Port`] of a device.
//!
//! See [`Port`] for a detailed description.
use core::{cell::RefCell, ops::ControlFlow};
pub use actions::{
ForwardedTLV, ForwardedTLVProvider, NoForwardedTLVs, PortAction, PortActionIterator,
TimestampContext,
};
pub use measurement::Measurement;
use rand::Rng;
use state::PortState;
use self::sequence_id::SequenceIdGenerator;
pub use crate::datastructures::messages::is_compatible as is_message_buffer_compatible;
pub use crate::datastructures::messages::MAX_DATA_LEN;
#[cfg(doc)]
use crate::PtpInstance;
use crate::{
bmc::{
acceptable_master::AcceptableMasterList,
bmca::{BestAnnounceMessage, Bmca},
},
clock::Clock,
config::PortConfig,
datastructures::{
common::PortIdentity,
messages::{Message, MessageBody},
},
filters::Filter,
ptp_instance::{PtpInstanceState, PtpInstanceStateMutex},
time::{Duration, Time},
};
// Needs to be here because of use rules
macro_rules! actions {
[] => {
{
crate::port::PortActionIterator::from(::arrayvec::ArrayVec::new())
}
};
[$action:expr] => {
{
let mut list = ::arrayvec::ArrayVec::new();
list.push($action);
crate::port::PortActionIterator::from(list)
}
};
[$action1:expr, $action2:expr] => {
{
let mut list = ::arrayvec::ArrayVec::new();
list.push($action1);
list.push($action2);
crate::port::PortActionIterator::from(list)
}
};
}
mod actions;
mod bmca;
mod master;
mod measurement;
mod sequence_id;
mod slave;
pub(crate) mod state;
/// A single port of the PTP instance
///
/// One of these needs to be created per port of the PTP instance. They are
/// created by calling [`PtpInstance::add_port`].
///
/// # Generics
/// A [`Port`] is generic over:
/// * **`L`**: The state of the `Port`, either [`InBmca`], or [`Running`].
/// * **`A`**: The type of the [`PortConfig::acceptable_master_list`] which
/// should implement [`AcceptableMasterList`]
/// * **`R`**: The type of the random number generator ([`Rng`]) used to
/// randomize timing
/// * **`C`**: The type of the [`Clock`] used by this [`Port`]
/// * **`F`**: The type of the [`Filter`] used by this [`Port`]
///
///
/// ## Type States
/// A [`Port`] can be in two states. Either in [`Running`] allowing access to
/// the [`handle_*`](`Port::handle_send_timestamp`) methods. Or in [`InBmca`]
/// state where it can be used with a [`PtpInstance`](`crate::PtpInstance`) to
/// run the best master clock algotithm (BMCA).
///
/// To transition from [`InBmca`] to [`Running`] use [`Port::end_bmca`]. To
/// transition from [`Running`] to [`InBmca`] use [`Port::start_bmca`].
///
/// # Example
///
/// ## Initialization
/// A [`Port`] can be created from a [`PtpInstance`]. It requires a
/// [`PortConfig`], a [`Filter::Config`], a [`Clock`], and a [`Rng`].
///
/// ```no_run
/// # mod system {
/// # pub struct Clock;
/// # impl statime::Clock for Clock {
/// # type Error = ();
/// # fn now(&self) -> statime::time::Time {
/// # unimplemented!()
/// # }
/// # fn step_clock(&mut self, _: statime::time::Duration) -> Result<statime::time::Time, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn set_frequency(&mut self, _: f64) -> Result<statime::time::Time, Self::Error> {
/// # unimplemented!()
/// # }
/// # fn set_properties(&mut self, _: &statime::config::TimePropertiesDS) -> Result<(), Self::Error> {
/// # unimplemented!()
/// # }
/// # }
/// # }
/// # let (instance_config, time_properties_ds) = unimplemented!();
/// use rand::thread_rng;
/// use statime::config::{AcceptAnyMaster, DelayMechanism, PortConfig};
/// use statime::filters::BasicFilter;
/// use statime::PtpInstance;
/// use statime::time::Interval;
///
/// let mut instance = PtpInstance::<BasicFilter>::new(instance_config, time_properties_ds);
///
/// // TODO make these values sensible
/// let interval = Interval::from_log_2(-2); // 2^(-2)s = 250ms
/// let port_config = PortConfig {
/// acceptable_master_list: AcceptAnyMaster,
/// delay_mechanism: DelayMechanism::E2E { interval },
/// announce_interval: interval,
/// announce_receipt_timeout: 0,
/// sync_interval: interval,
/// master_only: false,
/// delay_asymmetry: Default::default(),
/// };
/// let filter_config = 1.0;
/// let clock = system::Clock {};
/// let rng = thread_rng();
///
/// let port_in_bmca = instance.add_port(port_config, filter_config, clock, rng);
///
/// // To handle events for the port it needs to change to running mode
/// let (running_port, actions) = port_in_bmca.end_bmca();
///
/// // This returns the first actions that need to be handled for the port
/// # fn handle_actions<T>(_:T){}
/// handle_actions(actions);
///
/// # running_port.start_bmca(); // Make sure we check `A` bound
/// ```
///
/// ## Handling actions
/// The [`Port`] informs the user about any actions it needs the user to handle
/// via [`PortAction`]s returned from its methods. The user is responsible for
/// handling these events in their system specific way.
///
/// ```no_run
/// use statime::port::{PortAction, PortActionIterator, TimestampContext};
/// use statime::time::Time;
///
/// # mod system {
/// # pub struct Timer;
/// # impl Timer {
/// # pub fn expire_in(&mut self, time: core::time::Duration) {}
/// # }
/// # pub struct UdpSocket;
/// # impl UdpSocket {
/// # pub fn send(&mut self, buf: &[u8], link_local: bool) -> statime::time::Time { unimplemented!() }
/// # }
/// # }
/// struct MyPortResources {
/// announce_timer: system::Timer,
/// sync_timer: system::Timer,
/// delay_req_timer: system::Timer,
/// announce_receipt_timer: system::Timer,
/// filter_update_timer: system::Timer,
/// time_critical_socket: system::UdpSocket,
/// general_socket: system::UdpSocket,
/// send_timestamp: Option<(TimestampContext, Time)>
/// }
///
/// fn handle_actions(resources: &mut MyPortResources, actions: PortActionIterator) {
/// for action in actions {
/// match action {
/// PortAction::SendEvent { context, data, link_local } => {
/// let timestamp = resources.time_critical_socket.send(data, link_local);
/// resources.send_timestamp = Some((context, timestamp));
/// }
/// PortAction::SendGeneral { data, link_local } => {
/// resources.general_socket.send(data, link_local);
/// }
/// PortAction::ResetAnnounceTimer { duration } => {
/// resources.announce_timer.expire_in(duration)
/// }
/// PortAction::ResetSyncTimer { duration } => resources.sync_timer.expire_in(duration),
/// PortAction::ResetDelayRequestTimer { duration } => {
/// resources.delay_req_timer.expire_in(duration)
/// }
/// PortAction::ResetAnnounceReceiptTimer { duration } => {
/// resources.announce_receipt_timer.expire_in(duration)
/// }
/// PortAction::ResetFilterUpdateTimer { duration } => {
/// resources.filter_update_timer.expire_in(duration)
/// }
/// PortAction::ForwardTLV { .. } => {}
/// }
/// }
/// }
/// ```
///
/// ## Handling system events
/// After the initialization the user has to inform the [`Port`] about any
/// events relevant to it via the [`handle_*`](`Port::handle_send_timestamp`)
/// methods.
///
/// ```no_run
/// # mod system {
/// # pub struct Timer;
/// # impl Timer {
/// # pub fn has_expired(&self) -> bool { true }
/// # }
/// # pub struct UdpSocket;
/// # impl UdpSocket {
/// # pub fn recv(&mut self) -> Option<(&'static [u8], statime::time::Time)> { unimplemented!() }
/// # }
/// # }
/// # struct MyPortResources {
/// # announce_timer: system::Timer,
/// # sync_timer: system::Timer,
/// # delay_req_timer: system::Timer,
/// # announce_receipt_timer: system::Timer,
/// # filter_update_timer: system::Timer,
/// # time_critical_socket: system::UdpSocket,
/// # general_socket: system::UdpSocket,
/// # send_timestamp: Option<(statime::port::TimestampContext, statime::time::Time)>
/// # }
///
/// use rand::Rng;
/// use statime::Clock;
/// use statime::config::AcceptableMasterList;
/// use statime::filters::Filter;
/// use statime::port::{NoForwardedTLVs, Port, PortActionIterator, Running};
///
/// fn something_happend(resources: &mut MyPortResources, running_port: &mut Port<Running, impl AcceptableMasterList, impl Rng, impl Clock, impl Filter>) {
/// let actions = if resources.announce_timer.has_expired() {
/// running_port.handle_announce_timer(&mut NoForwardedTLVs)
/// } else if resources.sync_timer.has_expired() {
/// running_port.handle_sync_timer()
/// } else if resources.delay_req_timer.has_expired() {
/// running_port.handle_delay_request_timer()
/// } else if resources.announce_receipt_timer.has_expired() {
/// running_port.handle_announce_receipt_timer()
/// } else if resources.filter_update_timer.has_expired() {
/// running_port.handle_filter_update_timer()
/// } else if let Some((data, timestamp)) = resources.time_critical_socket.recv() {
/// running_port.handle_event_receive(data, timestamp)
/// } else if let Some((data, _timestamp)) = resources.general_socket.recv() {
/// running_port.handle_general_receive(data)
/// } else if let Some((context, timestamp)) = resources.send_timestamp.take() {
/// running_port.handle_send_timestamp(context, timestamp)
/// } else {
/// PortActionIterator::empty()
/// };
///
/// # fn handle_actions<T,U>(_:T,_:U){}
/// handle_actions(resources, actions);
/// }
/// ```
#[derive(Debug)]
pub struct Port<'a, L, A, R, C, F: Filter, S = RefCell<PtpInstanceState>> {
config: PortConfig<()>,
filter_config: F::Config,
clock: C,
// PortDS port_identity
pub(crate) port_identity: PortIdentity,
// Corresponds with PortDS port_state and enabled
port_state: PortState,
instance_state: &'a S,
bmca: Bmca<A>,
packet_buffer: [u8; MAX_DATA_LEN],
lifecycle: L,
rng: R,
// Age of the last announce message that triggered
// multiport disable. Once this gets larger than the
// port announce interval, we can once again become
// master.
multiport_disable: Option<Duration>,
announce_seq_ids: SequenceIdGenerator,
sync_seq_ids: SequenceIdGenerator,
delay_seq_ids: SequenceIdGenerator,
pdelay_seq_ids: SequenceIdGenerator,
filter: F,
/// Mean delay means either `mean_path_delay` when DelayMechanism is E2E,
/// or `mean_link_delay` when DelayMechanism is P2P.
mean_delay: Option<Duration>,
peer_delay_state: PeerDelayState,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PeerDelayState {
Empty,
Measuring {
id: u16,
responder_identity: Option<PortIdentity>,
request_send_time: Option<Time>,
request_recv_time: Option<Time>,
response_send_time: Option<Time>,
response_recv_time: Option<Time>,
},
PostMeasurement {
id: u16,
responder_identity: PortIdentity,
},
}
/// Type state of [`Port`] entered by [`Port::end_bmca`]
#[derive(Debug)]
pub struct Running;
/// Type state of [`Port`] entered by [`Port::start_bmca`]
#[derive(Debug)]
pub struct InBmca {
pending_action: PortActionIterator<'static>,
local_best: Option<BestAnnounceMessage>,
}
impl<'a, A: AcceptableMasterList, C: Clock, F: Filter, R: Rng, S: PtpInstanceStateMutex>
Port<'a, Running, A, R, C, F, S>
{
/// Inform the port about a transmit timestamp being available
///
/// `context` is the handle of the packet that was send from the
/// [`PortAction::SendEvent`] that caused the send.
pub fn handle_send_timestamp(
&mut self,
context: TimestampContext,
timestamp: Time,
) -> PortActionIterator<'_> {
match context.inner {
actions::TimestampContextInner::Sync { id } => {
self.handle_sync_timestamp(id, timestamp)
}
actions::TimestampContextInner::DelayReq { id } => {
self.handle_delay_timestamp(id, timestamp)
}
actions::TimestampContextInner::PDelayReq { id } => {
self.handle_pdelay_timestamp(id, timestamp)
}
actions::TimestampContextInner::PDelayResp {
id,
requestor_identity,
} => self.handle_pdelay_response_timestamp(id, requestor_identity, timestamp),
}
}
/// Handle the announce timer going off
pub fn handle_announce_timer(
&mut self,
tlv_provider: &mut impl ForwardedTLVProvider,
) -> PortActionIterator<'_> {
self.send_announce(tlv_provider)
}
/// Handle the sync timer going off
pub fn handle_sync_timer(&mut self) -> PortActionIterator<'_> {
self.send_sync()
}
/// Handle the delay request timer going off
pub fn handle_delay_request_timer(&mut self) -> PortActionIterator<'_> {
self.send_delay_request()
}
/// Handle the announce receipt timer going off
pub fn handle_announce_receipt_timer(&mut self) -> PortActionIterator<'_> {
// we didn't hear announce messages from other masters, so become master
// ourselves
match self.port_state {
PortState::Master => (),
_ => self.set_forced_port_state(PortState::Master),
}
// Immediately start sending syncs and announces
actions![
PortAction::ResetAnnounceTimer {
duration: core::time::Duration::from_secs(0)
},
PortAction::ResetSyncTimer {
duration: core::time::Duration::from_secs(0)
}
]
}
/// Handle the filter update timer going off
pub fn handle_filter_update_timer(&mut self) -> PortActionIterator {
let update = self.filter.update(&mut self.clock);
if update.mean_delay.is_some() {
self.mean_delay = update.mean_delay;
}
PortActionIterator::from_filter(update)
}
/// Set this [`Port`] into [`InBmca`] mode to use it with
/// [`PtpInstance::bmca`].
pub fn start_bmca(self) -> Port<'a, InBmca, A, R, C, F, S> {
Port {
port_state: self.port_state,
instance_state: self.instance_state,
config: self.config,
filter_config: self.filter_config,
clock: self.clock,
port_identity: self.port_identity,
bmca: self.bmca,
rng: self.rng,
multiport_disable: self.multiport_disable,
packet_buffer: [0; MAX_DATA_LEN],
lifecycle: InBmca {
pending_action: actions![],
local_best: None,
},
announce_seq_ids: self.announce_seq_ids,
sync_seq_ids: self.sync_seq_ids,
delay_seq_ids: self.delay_seq_ids,
pdelay_seq_ids: self.pdelay_seq_ids,
filter: self.filter,
mean_delay: self.mean_delay,
peer_delay_state: self.peer_delay_state,
}
}
// parse and do basic domain filtering on message
fn parse_and_filter<'b>(
&mut self,
data: &'b [u8],
) -> ControlFlow<PortActionIterator<'b>, Message<'b>> {
if !is_message_buffer_compatible(data) {
// do not spam with parse error in mixed-version PTPv1+v2 networks
return ControlFlow::Break(actions![]);
}
let message = match Message::deserialize(data) {
Ok(message) => message,
Err(error) => {
log::warn!("Could not parse packet: {:?}", error);
return ControlFlow::Break(actions![]);
}
};
let domain_matches = self.instance_state.with_ref(|state| {
message.header().sdo_id == state.default_ds.sdo_id
&& message.header().domain_number == state.default_ds.domain_number
});
if !domain_matches {
return ControlFlow::Break(actions![]);
}
ControlFlow::Continue(message)
}
/// Handle a message over the event channel
pub fn handle_event_receive<'b>(
&'b mut self,
data: &'b [u8],
timestamp: Time,
) -> PortActionIterator<'b> {
let message = match self.parse_and_filter(data) {
ControlFlow::Continue(value) => value,
ControlFlow::Break(value) => return value,
};
match message.body {
MessageBody::Sync(sync) => self.handle_sync(message.header, sync, timestamp),
MessageBody::DelayReq(delay_request) => {
self.handle_delay_req(message.header, delay_request, timestamp)
}
MessageBody::PDelayReq(_) => self.handle_pdelay_req(message.header, timestamp),
MessageBody::PDelayResp(peer_delay_response) => {
self.handle_peer_delay_response(message.header, peer_delay_response, timestamp)
}
_ => self.handle_general_internal(message),
}
}
/// Handle a general ptp message
pub fn handle_general_receive<'b>(&'b mut self, data: &'b [u8]) -> PortActionIterator<'b> {
let message = match self.parse_and_filter(data) {
ControlFlow::Continue(value) => value,
ControlFlow::Break(value) => return value,
};
self.handle_general_internal(message)
}
fn handle_general_internal<'b>(&'b mut self, message: Message<'b>) -> PortActionIterator<'b> {
match message.body {
MessageBody::Announce(announce) => self.handle_announce(&message, announce),
MessageBody::FollowUp(follow_up) => self.handle_follow_up(message.header, follow_up),
MessageBody::DelayResp(delay_response) => {
self.handle_delay_resp(message.header, delay_response)
}
MessageBody::PDelayRespFollowUp(peer_delay_follow_up) => {
self.handle_peer_delay_response_follow_up(message.header, peer_delay_follow_up)
}
MessageBody::Sync(_)
| MessageBody::DelayReq(_)
| MessageBody::PDelayReq(_)
| MessageBody::PDelayResp(_) => {
log::warn!("Received event message over general interface");
actions![]
}
MessageBody::Management(_) | MessageBody::Signaling(_) => actions![],
}
}
}
impl<'a, A, C, F: Filter, R, S> Port<'a, InBmca, A, R, C, F, S> {
/// End a BMCA cycle and make the
/// [`handle_*`](`Port::handle_send_timestamp`) methods available again
pub fn end_bmca(
self,
) -> (
Port<'a, Running, A, R, C, F, S>,
PortActionIterator<'static>,
) {
(
Port {
port_state: self.port_state,
instance_state: self.instance_state,
config: self.config,
filter_config: self.filter_config,
clock: self.clock,
port_identity: self.port_identity,
bmca: self.bmca,
rng: self.rng,
multiport_disable: self.multiport_disable,
packet_buffer: [0; MAX_DATA_LEN],
lifecycle: Running,
announce_seq_ids: self.announce_seq_ids,
sync_seq_ids: self.sync_seq_ids,
delay_seq_ids: self.delay_seq_ids,
pdelay_seq_ids: self.pdelay_seq_ids,
filter: self.filter,
mean_delay: self.mean_delay,
peer_delay_state: self.peer_delay_state,
},
self.lifecycle.pending_action,
)
}
}
impl<L, A, R, C: Clock, F: Filter, S> Port<'_, L, A, R, C, F, S> {
fn set_forced_port_state(&mut self, mut state: PortState) {
log::info!(
"new state for port {}: {} -> {}",
self.port_identity.port_number,
self.port_state,
state
);
core::mem::swap(&mut self.port_state, &mut state);
if matches!(state, PortState::Slave(_) | PortState::Faulty)
|| matches!(self.port_state, PortState::Faulty)
{
let mut filter = F::new(self.filter_config.clone());
core::mem::swap(&mut filter, &mut self.filter);
filter.demobilize(&mut self.clock);
}
}
}
impl<L, A, R, C, F: Filter, S> Port<'_, L, A, R, C, F, S> {
/// Indicate whether this [`Port`] is steering its clock.
pub fn is_steering(&self) -> bool {
matches!(self.port_state, PortState::Slave(_))
}
/// Indicate whether this [`Port`] is in the master state.
pub fn is_master(&self) -> bool {
matches!(self.port_state, PortState::Master)
}
pub(crate) fn state(&self) -> &PortState {
&self.port_state
}
pub(crate) fn number(&self) -> u16 {
self.port_identity.port_number
}
}
impl<'a, A, C, F: Filter, R: Rng, S: PtpInstanceStateMutex> Port<'a, InBmca, A, R, C, F, S> {
/// Create a new port from a port dataset on a given interface.
pub(crate) fn new(
instance_state: &'a S,
config: PortConfig<A>,
filter_config: F::Config,
clock: C,
port_identity: PortIdentity,
mut rng: R,
) -> Self {
let duration = config.announce_duration(&mut rng);
let bmca = Bmca::new(
config.acceptable_master_list,
config.announce_interval.as_duration().into(),
port_identity,
);
let filter = F::new(filter_config.clone());
Port {
config: PortConfig {
acceptable_master_list: (),
delay_mechanism: config.delay_mechanism,
announce_interval: config.announce_interval,
announce_receipt_timeout: config.announce_receipt_timeout,
sync_interval: config.sync_interval,
master_only: config.master_only,
delay_asymmetry: config.delay_asymmetry,
},
filter_config,
clock,
port_identity,
port_state: PortState::Listening,
instance_state,
bmca,
rng,
multiport_disable: None,
packet_buffer: [0; MAX_DATA_LEN],
lifecycle: InBmca {
pending_action: actions![PortAction::ResetAnnounceReceiptTimer { duration }],
local_best: None,
},
announce_seq_ids: SequenceIdGenerator::new(),
sync_seq_ids: SequenceIdGenerator::new(),
delay_seq_ids: SequenceIdGenerator::new(),
pdelay_seq_ids: SequenceIdGenerator::new(),
filter,
mean_delay: None,
peer_delay_state: PeerDelayState::Empty,
}
}
}
#[cfg(test)]
mod tests {
use core::cell::RefCell;
use super::*;
use crate::{
config::{AcceptAnyMaster, DelayMechanism, InstanceConfig, TimePropertiesDS},
datastructures::datasets::{InternalDefaultDS, InternalParentDS, PathTraceDS},
filters::BasicFilter,
time::{Duration, Interval, Time},
Clock,
};
// General test infra
pub(super) struct TestClock;
impl Clock for TestClock {
type Error = ();
fn set_frequency(&mut self, _freq: f64) -> Result<Time, Self::Error> {
Ok(Time::default())
}
fn now(&self) -> Time {
panic!("Shouldn't be called");
}
fn set_properties(
&mut self,
_time_properties_ds: &TimePropertiesDS,
) -> Result<(), Self::Error> {
Ok(())
}
fn step_clock(&mut self, _offset: Duration) -> Result<Time, Self::Error> {
Ok(Time::default())
}
}
pub(super) fn setup_test_port(
state: &RefCell<PtpInstanceState>,
) -> Port<'_, Running, AcceptAnyMaster, rand::rngs::mock::StepRng, TestClock, BasicFilter> {
let port = Port::<_, _, _, _, BasicFilter>::new(
state,
PortConfig {
acceptable_master_list: AcceptAnyMaster,
delay_mechanism: DelayMechanism::E2E {
interval: Interval::from_log_2(1),
},
announce_interval: Interval::from_log_2(1),
announce_receipt_timeout: 3,
sync_interval: Interval::from_log_2(0),
master_only: false,
delay_asymmetry: Duration::ZERO,
},
0.25,
TestClock,
Default::default(),
rand::rngs::mock::StepRng::new(2, 1),
);
let (port, _) = port.end_bmca();
port
}
pub(super) fn setup_test_port_custom_identity(
state: &RefCell<PtpInstanceState>,
port_identity: PortIdentity,
) -> Port<'_, Running, AcceptAnyMaster, rand::rngs::mock::StepRng, TestClock, BasicFilter> {
let port = Port::<_, _, _, _, BasicFilter>::new(
&state,
PortConfig {
acceptable_master_list: AcceptAnyMaster,
delay_mechanism: DelayMechanism::E2E {
interval: Interval::from_log_2(1),
},
announce_interval: Interval::from_log_2(1),
announce_receipt_timeout: 3,
sync_interval: Interval::from_log_2(0),
master_only: false,
delay_asymmetry: Duration::ZERO,
},
0.25,
TestClock,
port_identity,
rand::rngs::mock::StepRng::new(2, 1),
);
let (port, _) = port.end_bmca();
port
}
pub(super) fn setup_test_port_custom_filter<F: Filter>(
state: &RefCell<PtpInstanceState>,
filter_config: F::Config,
) -> Port<'_, Running, AcceptAnyMaster, rand::rngs::mock::StepRng, TestClock, F> {
let port = Port::<_, _, _, _, F>::new(
state,
PortConfig {
acceptable_master_list: AcceptAnyMaster,
delay_mechanism: DelayMechanism::E2E {
interval: Interval::from_log_2(1),
},
announce_interval: Interval::from_log_2(1),
announce_receipt_timeout: 3,
sync_interval: Interval::from_log_2(0),
master_only: false,
delay_asymmetry: Duration::ZERO,
},
filter_config,
TestClock,
Default::default(),
rand::rngs::mock::StepRng::new(2, 1),
);
let (port, _) = port.end_bmca();
port
}
pub(super) fn setup_test_state() -> RefCell<PtpInstanceState> {
let default_ds = InternalDefaultDS::new(InstanceConfig {
clock_identity: Default::default(),
priority_1: 255,
priority_2: 255,
domain_number: 0,
slave_only: false,
sdo_id: Default::default(),
path_trace: false,
});
let parent_ds = InternalParentDS::new(default_ds);
let state = RefCell::new(PtpInstanceState {
default_ds,
current_ds: Default::default(),
parent_ds,
time_properties_ds: Default::default(),
path_trace_ds: PathTraceDS::new(false),
});
state
}
}