Skip to content
File

Blob: firmware/vendor/sctp-proto/src/association/state.rs

rust103 lines
1use core::fmt;
2 
3/// association state enums
4#[derive(Debug, Copy, Clone, PartialEq, Default)]
5pub(crate) enum AssociationState {
6 #[default]
7 Closed = 0,
8 CookieWait = 1,
9 CookieEchoed = 2,
10 Established = 3,
11 ShutdownAckSent = 4,
12 ShutdownPending = 5,
13 ShutdownReceived = 6,
14 ShutdownSent = 7,
15}
16 
17impl From<u8> for AssociationState {
18 fn from(v: u8) -> AssociationState {
19 match v {
20 1 => AssociationState::CookieWait,
21 2 => AssociationState::CookieEchoed,
22 3 => AssociationState::Established,
23 4 => AssociationState::ShutdownAckSent,
24 5 => AssociationState::ShutdownPending,
25 6 => AssociationState::ShutdownReceived,
26 7 => AssociationState::ShutdownSent,
27 _ => AssociationState::Closed,
28 }
29 }
30}
31 
32impl fmt::Display for AssociationState {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 let s = match *self {
35 AssociationState::Closed => "Closed",
36 AssociationState::CookieWait => "CookieWait",
37 AssociationState::CookieEchoed => "CookieEchoed",
38 AssociationState::Established => "Established",
39 AssociationState::ShutdownPending => "ShutdownPending",
40 AssociationState::ShutdownSent => "ShutdownSent",
41 AssociationState::ShutdownReceived => "ShutdownReceived",
42 AssociationState::ShutdownAckSent => "ShutdownAckSent",
43 };
44 write!(f, "{}", s)
45 }
46}
47 
48impl AssociationState {
49 pub(crate) fn is_closing(&self) -> bool {
50 matches!(
51 *self,
52 AssociationState::ShutdownSent
53 | AssociationState::ShutdownAckSent
54 | AssociationState::ShutdownPending
55 | AssociationState::ShutdownReceived
56 )
57 }
58 
59 pub(crate) fn is_drained(&self) -> bool {
60 self.is_closing()
61 }
62}
63 
64/// ack mode (for testing)
65#[derive(Debug, Copy, Clone, PartialEq, Default)]
66pub(crate) enum AckMode {
67 #[default]
68 Normal,
69 NoDelay,
70 AlwaysDelay,
71}
72 
73impl fmt::Display for AckMode {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 let s = match *self {
76 AckMode::Normal => "Normal",
77 AckMode::NoDelay => "NoDelay",
78 AckMode::AlwaysDelay => "AlwaysDelay",
79 };
80 write!(f, "{}", s)
81 }
82}
83 
84/// ack transmission state
85#[derive(Debug, Copy, Clone, PartialEq, Default)]
86pub(crate) enum AckState {
87 #[default]
88 Idle, // ack timer is off
89 Immediate, // will send ack immediately
90 Delay, // ack timer is on (ack is being delayed)
91}
92 
93impl fmt::Display for AckState {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 let s = match *self {
96 AckState::Idle => "Idle",
97 AckState::Immediate => "Immediate",
98 AckState::Delay => "Delay",
99 };
100 write!(f, "{}", s)
101 }
102}