Skip to content
File

Blob: firmware/vendor/sctp-proto/src/param/param_reconfig_response.rs

rust136 lines
1use super::{param_header::*, param_type::*, *};
2 
3use bytes::BufMut;
4 
5#[derive(Debug, Copy, Clone, PartialEq)]
6#[repr(C)]
7#[derive(Default)]
8pub(crate) enum ReconfigResult {
9 SuccessNop = 0,
10 SuccessPerformed = 1,
11 Denied = 2,
12 ErrorWrongSsn = 3,
13 ErrorRequestAlreadyInProgress = 4,
14 ErrorBadSequenceNumber = 5,
15 InProgress = 6,
16 #[default]
17 Unknown,
18}
19 
20impl fmt::Display for ReconfigResult {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 let s = match *self {
23 ReconfigResult::SuccessNop => "0: Success - Nothing to do",
24 ReconfigResult::SuccessPerformed => "1: Success - Performed",
25 ReconfigResult::Denied => "2: Denied",
26 ReconfigResult::ErrorWrongSsn => "3: Error - Wrong SSN",
27 ReconfigResult::ErrorRequestAlreadyInProgress => {
28 "4: Error - Request already in progress"
29 }
30 ReconfigResult::ErrorBadSequenceNumber => "5: Error - Bad Sequence Number",
31 ReconfigResult::InProgress => "6: In progress",
32 _ => "Unknown ReconfigResult",
33 };
34 write!(f, "{}", s)
35 }
36}
37 
38impl From<u32> for ReconfigResult {
39 fn from(v: u32) -> ReconfigResult {
40 match v {
41 0 => ReconfigResult::SuccessNop,
42 1 => ReconfigResult::SuccessPerformed,
43 2 => ReconfigResult::Denied,
44 3 => ReconfigResult::ErrorWrongSsn,
45 4 => ReconfigResult::ErrorRequestAlreadyInProgress,
46 5 => ReconfigResult::ErrorBadSequenceNumber,
47 6 => ReconfigResult::InProgress,
48 _ => ReconfigResult::Unknown,
49 }
50 }
51}
52 
53///This parameter is used by the receiver of a Re-configuration Request
54///Parameter to respond to the request.
55///
56///0 1 2 3
57///0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
58///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59///| Parameter Type = 16 | Parameter Length |
60///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61///| Re-configuration Response Sequence Number |
62///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63///| Result |
64///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65///| Sender's Next TSN (optional) |
66///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67///| Receiver's Next TSN (optional) |
68///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69#[derive(Default, Debug, Clone, PartialEq)]
70pub(crate) struct ParamReconfigResponse {
71 /// This value is copied from the request parameter and is used by the
72 /// receiver of the Re-configuration Response Parameter to tie the
73 /// response to the request.
74 pub(crate) reconfig_response_sequence_number: u32,
75 /// This value describes the result of the processing of the request.
76 pub(crate) result: ReconfigResult,
77}
78 
79impl fmt::Display for ParamReconfigResponse {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 write!(
82 f,
83 "{} {} {}",
84 self.header(),
85 self.reconfig_response_sequence_number,
86 self.result
87 )
88 }
89}
90 
91impl Param for ParamReconfigResponse {
92 fn header(&self) -> ParamHeader {
93 ParamHeader {
94 typ: ParamType::ReconfigResp,
95 value_length: self.value_length() as u16,
96 }
97 }
98 
99 fn unmarshal(raw: &Bytes) -> Result<Self> {
100 let header = ParamHeader::unmarshal(raw)?;
101 if header.value_length() < 8 {
102 return Err(Error::ErrReconfigRespParamTooShort);
103 }
104 
105 let reader =
106 &mut raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length());
107 
108 let reconfig_response_sequence_number = reader.get_u32();
109 let result = reader.get_u32().into();
110 
111 Ok(ParamReconfigResponse {
112 reconfig_response_sequence_number,
113 result,
114 })
115 }
116 
117 fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> {
118 self.header().marshal_to(buf)?;
119 buf.put_u32(self.reconfig_response_sequence_number);
120 buf.put_u32(self.result as u32);
121 Ok(buf.len())
122 }
123 
124 fn value_length(&self) -> usize {
125 8
126 }
127 
128 fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
129 Box::new(self.clone())
130 }
131 
132 fn as_any(&self) -> &(dyn Any + Send + Sync) {
133 self
134 }
135}