@@ -11,17 +11,13 @@ use crate::{
1111
1212impl < ' a , A , C , F : Filter , R > Port < Running < ' a > , A , R , C , F > {
1313 pub ( super ) fn send_sync ( & mut self ) -> PortActionIterator {
14- match self . port_state {
15- PortState :: Master ( ref mut state) => {
16- log:: trace!( "sending sync message" ) ;
17-
18- let seq_id = state. sync_seq_ids . generate ( ) ;
19- let packet_length = match Message :: sync (
20- & self . lifecycle . state . default_ds ,
21- self . port_identity ,
22- seq_id,
23- )
24- . serialize ( & mut self . packet_buffer )
14+ if matches ! ( self . port_state, PortState :: Master ) {
15+ log:: trace!( "sending sync message" ) ;
16+
17+ let seq_id = self . sync_seq_ids . generate ( ) ;
18+ let packet_length =
19+ match Message :: sync ( & self . lifecycle . state . default_ds , self . port_identity , seq_id)
20+ . serialize ( & mut self . packet_buffer )
2521 {
2622 Ok ( message) => message,
2723 Err ( error) => {
@@ -30,24 +26,24 @@ impl<'a, A, C, F: Filter, R> Port<Running<'a>, A, R, C, F> {
3026 }
3127 } ;
3228
33- actions ! [
34- PortAction :: ResetSyncTimer {
35- duration: self . config. sync_interval. as_core_duration( ) ,
29+ actions ! [
30+ PortAction :: ResetSyncTimer {
31+ duration: self . config. sync_interval. as_core_duration( ) ,
32+ } ,
33+ PortAction :: SendEvent {
34+ context: TimestampContext {
35+ inner: TimestampContextInner :: Sync { id: seq_id } ,
3636 } ,
37- PortAction :: SendEvent {
38- context: TimestampContext {
39- inner: TimestampContextInner :: Sync { id: seq_id } ,
40- } ,
41- data: & self . packet_buffer[ ..packet_length] ,
42- }
43- ]
44- }
45- _ => actions ! [ ] ,
37+ data: & self . packet_buffer[ ..packet_length] ,
38+ }
39+ ]
40+ } else {
41+ actions ! [ ]
4642 }
4743 }
4844
4945 pub ( super ) fn handle_sync_timestamp ( & mut self , id : u16 , timestamp : Time ) -> PortActionIterator {
50- if matches ! ( self . port_state, PortState :: Master ( _ ) ) {
46+ if matches ! ( self . port_state, PortState :: Master ) {
5147 let packet_length = match Message :: follow_up (
5248 & self . lifecycle . state . default_ds ,
5349 self . port_identity ,
@@ -78,61 +74,60 @@ impl<'a, A, C, F: Filter, R> Port<Running<'a>, A, R, C, F> {
7874 & mut self ,
7975 tlv_provider : & mut impl ForwardedTLVProvider ,
8076 ) -> PortActionIterator {
81- match self . port_state {
82- PortState :: Master ( ref mut state) => {
83- log:: trace!( "sending announce message" ) ;
84-
85- let mut tlv_buffer = [ 0 ; MAX_DATA_LEN ] ;
86- let mut tlv_builder = TlvSetBuilder :: new ( & mut tlv_buffer) ;
87-
88- let mut message = Message :: announce (
89- & self . lifecycle . state ,
90- self . port_identity ,
91- state. announce_seq_ids . generate ( ) ,
92- ) ;
93- let mut tlv_margin = MAX_DATA_LEN - message. wire_size ( ) ;
94-
95- while let Some ( tlv) = tlv_provider. next_if_smaller ( tlv_margin) {
96- assert ! ( tlv. size( ) < tlv_margin) ;
97- if self . lifecycle . state . parent_ds . parent_port_identity != tlv. sender_identity {
98- // Ignore, shouldn't be forwarded
99- continue ;
100- }
77+ if matches ! ( self . port_state, PortState :: Master ) {
78+ log:: trace!( "sending announce message" ) ;
10179
102- tlv_margin -= tlv. size ( ) ;
103- // Will not fail as previous checks ensure sufficient space in buffer.
104- tlv_builder. add ( tlv. tlv ) . unwrap ( ) ;
105- }
80+ let mut tlv_buffer = [ 0 ; MAX_DATA_LEN ] ;
81+ let mut tlv_builder = TlvSetBuilder :: new ( & mut tlv_buffer) ;
10682
107- message. suffix = tlv_builder. build ( ) ;
83+ let mut message = Message :: announce (
84+ & self . lifecycle . state ,
85+ self . port_identity ,
86+ self . announce_seq_ids . generate ( ) ,
87+ ) ;
88+ let mut tlv_margin = MAX_DATA_LEN - message. wire_size ( ) ;
10889
109- let packet_length = match Message :: announce (
110- & self . lifecycle . state ,
111- self . port_identity ,
112- state. announce_seq_ids . generate ( ) ,
113- )
114- . serialize ( & mut self . packet_buffer )
115- {
116- Ok ( length) => length,
117- Err ( error) => {
118- log:: error!(
119- "Statime bug: Could not serialize announce message {:?}" ,
120- error
121- ) ;
122- return actions ! [ ] ;
123- }
124- } ;
90+ while let Some ( tlv) = tlv_provider. next_if_smaller ( tlv_margin) {
91+ assert ! ( tlv. size( ) < tlv_margin) ;
92+ if self . lifecycle . state . parent_ds . parent_port_identity != tlv. sender_identity {
93+ // Ignore, shouldn't be forwarded
94+ continue ;
95+ }
12596
126- actions ! [
127- PortAction :: ResetAnnounceTimer {
128- duration: self . config. announce_interval. as_core_duration( ) ,
129- } ,
130- PortAction :: SendGeneral {
131- data: & self . packet_buffer[ ..packet_length]
132- }
133- ]
97+ tlv_margin -= tlv. size ( ) ;
98+ // Will not fail as previous checks ensure sufficient space in buffer.
99+ tlv_builder. add ( tlv. tlv ) . unwrap ( ) ;
134100 }
135- _ => actions ! [ ] ,
101+
102+ message. suffix = tlv_builder. build ( ) ;
103+
104+ let packet_length = match Message :: announce (
105+ & self . lifecycle . state ,
106+ self . port_identity ,
107+ self . announce_seq_ids . generate ( ) ,
108+ )
109+ . serialize ( & mut self . packet_buffer )
110+ {
111+ Ok ( length) => length,
112+ Err ( error) => {
113+ log:: error!(
114+ "Statime bug: Could not serialize announce message {:?}" ,
115+ error
116+ ) ;
117+ return actions ! [ ] ;
118+ }
119+ } ;
120+
121+ actions ! [
122+ PortAction :: ResetAnnounceTimer {
123+ duration: self . config. announce_interval. as_core_duration( ) ,
124+ } ,
125+ PortAction :: SendGeneral {
126+ data: & self . packet_buffer[ ..packet_length]
127+ }
128+ ]
129+ } else {
130+ actions ! [ ]
136131 }
137132 }
138133
@@ -142,7 +137,7 @@ impl<'a, A, C, F: Filter, R> Port<Running<'a>, A, R, C, F> {
142137 message : DelayReqMessage ,
143138 timestamp : Time ,
144139 ) -> PortActionIterator {
145- if matches ! ( self . port_state, PortState :: Master ( _ ) ) {
140+ if matches ! ( self . port_state, PortState :: Master ) {
146141 log:: debug!( "Received DelayReq" ) ;
147142 let delay_resp_message = Message :: delay_resp (
148143 header,
@@ -181,7 +176,6 @@ mod tests {
181176 messages:: { Header , MessageBody } ,
182177 } ,
183178 port:: {
184- state:: MasterState ,
185179 tests:: { setup_test_port, setup_test_state} ,
186180 NoForwardedTLVs ,
187181 } ,
@@ -194,7 +188,7 @@ mod tests {
194188
195189 let mut port = setup_test_port ( & state) ;
196190
197- port. set_forced_port_state ( PortState :: Master ( MasterState :: default ( ) ) ) ;
191+ port. set_forced_port_state ( PortState :: Master ) ;
198192
199193 port. config . delay_mechanism = DelayMechanism :: E2E {
200194 interval : Interval :: from_log_2 ( 2 ) ,
@@ -308,7 +302,7 @@ mod tests {
308302
309303 let mut port = setup_test_port ( & state) ;
310304
311- port. set_forced_port_state ( PortState :: Master ( MasterState :: default ( ) ) ) ;
305+ port. set_forced_port_state ( PortState :: Master ) ;
312306
313307 let mut actions = port. send_announce ( & mut NoForwardedTLVs ) ;
314308
@@ -369,7 +363,7 @@ mod tests {
369363
370364 let mut port = setup_test_port ( & state) ;
371365
372- port. set_forced_port_state ( PortState :: Master ( MasterState :: default ( ) ) ) ;
366+ port. set_forced_port_state ( PortState :: Master ) ;
373367 let mut actions = port. send_sync ( ) ;
374368
375369 assert ! ( matches!(
0 commit comments