Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tlv refactor #230

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions statime/src/bmc/bmca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
datastructures::{
common::{PortIdentity, TimeInterval, WireTimestamp},
datasets::DefaultDS,
messages::AnnounceMessage,
messages::{AnnounceMessage, Header},
},
port::state::PortState,
};
Expand Down Expand Up @@ -51,13 +51,17 @@ impl Bmca {
/// Register a received announce message to the BMC algorithm
pub(crate) fn register_announce_message(
&mut self,
header: &Header,
announce_message: &AnnounceMessage,
current_time: WireTimestamp,
) {
// Ignore messages comming from the same port
if announce_message.header.source_port_identity != self.own_port_identity {
self.foreign_master_list
.register_announce_message(announce_message, current_time);
self.foreign_master_list.register_announce_message(
header,
announce_message,
current_time,
);
}
}

Expand All @@ -73,20 +77,20 @@ impl Bmca {
.take_qualified_announce_messages(current_time);

// The best of the foreign master messages is our erbest
let erbest =
Self::find_best_announce_message(announce_messages.map(|(message, timestamp)| {
BestAnnounceMessage {
message,
timestamp,
identity: self.own_port_identity,
}
}));
let erbest = Self::find_best_announce_message(announce_messages.map(|message| {
BestAnnounceMessage {
header: message.header,
message: message.message,
timestamp: message.timestamp,
identity: self.own_port_identity,
}
}));

if let Some(best) = &erbest {
// All messages that were considered have been removed from the
// foreignmasterlist. However, the one that has been selected as the
// Erbest must not be removed, so let's just reregister it.
self.register_announce_message(&best.message, best.timestamp);
self.register_announce_message(&best.header, &best.message, best.timestamp);
}

erbest
Expand Down Expand Up @@ -231,6 +235,7 @@ enum MessageComparison {

#[derive(Copy, Clone, Debug)]
pub(crate) struct BestAnnounceMessage {
header: Header,
message: AnnounceMessage,
timestamp: WireTimestamp,
identity: PortIdentity,
Expand Down Expand Up @@ -271,8 +276,8 @@ mod tests {
ClockIdentity,
};

fn default_announce_message() -> AnnounceMessage {
let header = Header {
fn default_announce_message_header() -> Header {
Header {
sdo_id: Default::default(),
version: PtpVersion::new(2, 1).unwrap(),
domain_number: Default::default(),
Expand All @@ -292,10 +297,12 @@ mod tests {
source_port_identity: Default::default(),
sequence_id: Default::default(),
log_message_interval: Default::default(),
};
}
}

fn default_announce_message() -> AnnounceMessage {
AnnounceMessage {
header,
header: default_announce_message_header(),
origin_timestamp: Default::default(),
current_utc_offset: Default::default(),
grandmaster_priority_1: Default::default(),
Expand All @@ -308,6 +315,7 @@ mod tests {
}

fn default_best_announce_message() -> BestAnnounceMessage {
let header = default_announce_message_header();
let message = default_announce_message();

let timestamp = WireTimestamp {
Expand All @@ -321,6 +329,7 @@ mod tests {
};

BestAnnounceMessage {
header,
message,
timestamp,
identity,
Expand Down
54 changes: 38 additions & 16 deletions statime/src/bmc/foreign_master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use arrayvec::ArrayVec;
use crate::{
datastructures::{
common::{PortIdentity, TimeInterval, WireTimestamp},
messages::AnnounceMessage,
messages::{AnnounceMessage, Header},
},
time::{Duration, Time},
};
Expand All @@ -28,13 +28,27 @@ const MAX_FOREIGN_MASTERS: usize = 8;
pub struct ForeignMaster {
foreign_master_port_identity: PortIdentity,
// Must have a capacity of at least 2
announce_messages: ArrayVec<(AnnounceMessage, WireTimestamp), MAX_ANNOUNCE_MESSAGES>,
announce_messages: ArrayVec<ForeignAnnounceMessage, MAX_ANNOUNCE_MESSAGES>,
}

#[derive(Debug)]
pub(crate) struct ForeignAnnounceMessage {
pub(crate) header: Header,
pub(crate) message: AnnounceMessage,
pub(crate) timestamp: WireTimestamp,
}

impl ForeignMaster {
fn new(announce_message: AnnounceMessage, current_time: WireTimestamp) -> Self {
fn new(header: Header, announce_message: AnnounceMessage, current_time: WireTimestamp) -> Self {
let message = ForeignAnnounceMessage {
header,
message: announce_message,
timestamp: current_time,
};

let mut messages = ArrayVec::<_, MAX_ANNOUNCE_MESSAGES>::new();
messages.push((announce_message, current_time));
messages.push(message);

Self {
foreign_master_port_identity: announce_message.header.source_port_identity,
announce_messages: messages,
Expand All @@ -57,27 +71,30 @@ impl ForeignMaster {
let cutoff_time = Time::from(current_time)
- Duration::from(announce_interval) * FOREIGN_MASTER_TIME_WINDOW;
self.announce_messages
.retain(|(_, ts)| Time::from(*ts) > cutoff_time);
.retain(|m| Time::from(m.timestamp) > cutoff_time);

self.announce_messages.is_empty()
}

fn register_announce_message(
&mut self,
header: Header,
announce_message: AnnounceMessage,
current_time: WireTimestamp,
announce_interval: TimeInterval,
) {
self.purge_old_messages(current_time, announce_interval);

let new_message = ForeignAnnounceMessage {
header,
message: announce_message,
timestamp: current_time,
};

// Try to add new message; otherwise remove the first message and then add
if self
.announce_messages
.try_push((announce_message, current_time))
.is_err()
{
if let Err(e) = self.announce_messages.try_push(new_message) {
self.announce_messages.remove(0);
self.announce_messages
.push((announce_message, current_time));
self.announce_messages.push(e.element());
}
}
}
Expand Down Expand Up @@ -110,7 +127,7 @@ impl ForeignMasterList {
pub(crate) fn take_qualified_announce_messages(
&mut self,
current_time: WireTimestamp,
) -> impl Iterator<Item = (AnnounceMessage, WireTimestamp)> {
) -> impl Iterator<Item = ForeignAnnounceMessage> {
let mut qualified_foreign_masters = ArrayVec::<_, MAX_FOREIGN_MASTERS>::new();

for i in (0..self.foreign_masters.len()).rev() {
Expand Down Expand Up @@ -141,6 +158,7 @@ impl ForeignMasterList {

pub(crate) fn register_announce_message(
&mut self,
header: &Header,
announce_message: &AnnounceMessage,
current_time: WireTimestamp,
) {
Expand All @@ -157,15 +175,19 @@ impl ForeignMasterList {
{
// Yes, so add the announce message to it
foreign_master.register_announce_message(
*header,
*announce_message,
current_time,
port_announce_interval,
);
} else {
// No, insert a new foreign master, if there is room in the array
if self.foreign_masters.len() < MAX_FOREIGN_MASTERS {
self.foreign_masters
.push(ForeignMaster::new(*announce_message, current_time));
self.foreign_masters.push(ForeignMaster::new(
*header,
*announce_message,
current_time,
));
}
}
}
Expand Down Expand Up @@ -198,7 +220,7 @@ impl ForeignMasterList {
// 2. The announce message must be newer than the one(s) we already have
// We can check the sequence id for that (with some logic for u16 rollover)
if let Some(foreign_master) = self.get_foreign_master(source_identity) {
if let Some((last_announce_message, _)) = foreign_master.announce_messages.last() {
if let Some(last_announce_message) = foreign_master.announce_messages.last() {
let announce_sequence_id = announce_message.header.sequence_id;
let last_sequence_id = last_announce_message.header.sequence_id;

Expand Down
16 changes: 3 additions & 13 deletions statime/src/datastructures/messages/delay_req.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::Header;
use crate::datastructures::{common::WireTimestamp, WireFormat, WireFormatError};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct DelayReqMessage {
pub(crate) header: Header,
pub(crate) origin_timestamp: WireTimestamp,
}

Expand All @@ -18,17 +16,11 @@ impl DelayReqMessage {
Ok(())
}

pub(crate) fn deserialize_content(
header: Header,
buffer: &[u8],
) -> Result<Self, WireFormatError> {
pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, WireFormatError> {
let slice = buffer.get(0..10).ok_or(WireFormatError::BufferTooShort)?;
let origin_timestamp = WireTimestamp::deserialize(slice)?;

Ok(Self {
header,
origin_timestamp,
})
Ok(Self { origin_timestamp })
}
}

Expand All @@ -41,7 +33,6 @@ mod tests {
let representations = [(
[0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0],
DelayReqMessage {
header: Header::default(),
origin_timestamp: WireTimestamp {
seconds: 1169232218,
nanos: 174389936,
Expand All @@ -59,8 +50,7 @@ mod tests {

// Test the deserialization output
let deserialized_data =
DelayReqMessage::deserialize_content(Header::default(), &byte_representation)
.unwrap();
DelayReqMessage::deserialize_content(&byte_representation).unwrap();
assert_eq!(deserialized_data, object_representation);
}
}
Expand Down
12 changes: 2 additions & 10 deletions statime/src/datastructures/messages/delay_resp.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use super::Header;
use crate::datastructures::{
common::{PortIdentity, WireTimestamp},
WireFormat, WireFormatError,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct DelayRespMessage {
pub(crate) header: Header,
pub(crate) receive_timestamp: WireTimestamp,
pub(crate) requesting_port_identity: PortIdentity,
}
Expand All @@ -24,16 +22,12 @@ impl DelayRespMessage {
Ok(())
}

pub(crate) fn deserialize_content(
header: Header,
buffer: &[u8],
) -> Result<Self, WireFormatError> {
pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, WireFormatError> {
let slice = buffer.get(0..20).ok_or(WireFormatError::BufferTooShort)?;
let receive_timestamp = WireTimestamp::deserialize(&slice[0..10])?;
let requesting_port_identity = PortIdentity::deserialize(&slice[10..20])?;

Ok(Self {
header,
receive_timestamp,
requesting_port_identity,
})
Expand All @@ -53,7 +47,6 @@ mod tests {
0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
],
DelayRespMessage {
header: Header::default(),
receive_timestamp: WireTimestamp {
seconds: 1169232218,
nanos: 174389936,
Expand All @@ -75,8 +68,7 @@ mod tests {

// Test the deserialization output
let deserialized_data =
DelayRespMessage::deserialize_content(Header::default(), &byte_representation)
.unwrap();
DelayRespMessage::deserialize_content(&byte_representation).unwrap();
assert_eq!(deserialized_data, object_representation);
}
}
Expand Down
13 changes: 2 additions & 11 deletions statime/src/datastructures/messages/follow_up.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::Header;
use crate::datastructures::{common::WireTimestamp, WireFormat, WireFormatError};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct FollowUpMessage {
pub(crate) header: Header,
pub(crate) precise_origin_timestamp: WireTimestamp,
}

Expand All @@ -19,15 +17,11 @@ impl FollowUpMessage {
Ok(())
}

pub(crate) fn deserialize_content(
header: Header,
buffer: &[u8],
) -> Result<Self, WireFormatError> {
pub(crate) fn deserialize_content(buffer: &[u8]) -> Result<Self, WireFormatError> {
let slice = buffer.get(0..10).ok_or(WireFormatError::BufferTooShort)?;
let precise_origin_timestamp = WireTimestamp::deserialize(slice)?;

Ok(Self {
header,
precise_origin_timestamp,
})
}
Expand All @@ -43,7 +37,6 @@ mod tests {
(
[0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0],
FollowUpMessage {
header: Header::default(),
precise_origin_timestamp: WireTimestamp {
seconds: 1169232218,
nanos: 174389936,
Expand All @@ -53,7 +46,6 @@ mod tests {
(
[0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01u8],
FollowUpMessage {
header: Header::default(),
precise_origin_timestamp: WireTimestamp {
seconds: 0x0000_0000_0002,
nanos: 0x0000_0001,
Expand All @@ -72,8 +64,7 @@ mod tests {

// Test the deserialization output
let deserialized_data =
FollowUpMessage::deserialize_content(Header::default(), &byte_representation)
.unwrap();
FollowUpMessage::deserialize_content(&byte_representation).unwrap();
assert_eq!(deserialized_data, object_representation);
}
}
Expand Down
Loading
Loading