Skip to content
File

Blob: firmware/vendor/sctp-proto/src/chunk/chunk_type.rs

rust95 lines
1use core::fmt;
2 
3// chunkType is an enum for SCTP Chunk Type field
4// This field identifies the type of information contained in the
5// Chunk Value field.
6#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
7pub(crate) struct ChunkType(pub(crate) u8);
8 
9pub(crate) const CT_PAYLOAD_DATA: ChunkType = ChunkType(0);
10pub(crate) const CT_INIT: ChunkType = ChunkType(1);
11pub(crate) const CT_INIT_ACK: ChunkType = ChunkType(2);
12pub(crate) const CT_SACK: ChunkType = ChunkType(3);
13pub(crate) const CT_HEARTBEAT: ChunkType = ChunkType(4);
14pub(crate) const CT_HEARTBEAT_ACK: ChunkType = ChunkType(5);
15pub(crate) const CT_ABORT: ChunkType = ChunkType(6);
16pub(crate) const CT_SHUTDOWN: ChunkType = ChunkType(7);
17pub(crate) const CT_SHUTDOWN_ACK: ChunkType = ChunkType(8);
18pub(crate) const CT_ERROR: ChunkType = ChunkType(9);
19pub(crate) const CT_COOKIE_ECHO: ChunkType = ChunkType(10);
20pub(crate) const CT_COOKIE_ACK: ChunkType = ChunkType(11);
21pub(crate) const CT_CWR: ChunkType = ChunkType(13);
22pub(crate) const CT_SHUTDOWN_COMPLETE: ChunkType = ChunkType(14);
23pub(crate) const CT_RECONFIG: ChunkType = ChunkType(130);
24pub(crate) const CT_FORWARD_TSN: ChunkType = ChunkType(192);
25pub(crate) const CT_I_FORWARD_TSN: ChunkType = ChunkType(194);
26 
27impl fmt::Display for ChunkType {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 let others = format!("Unknown ChunkType: {}", self.0);
30 let s = match *self {
31 CT_PAYLOAD_DATA => "DATA",
32 CT_INIT => "INIT",
33 CT_INIT_ACK => "INIT-ACK",
34 CT_SACK => "SACK",
35 CT_HEARTBEAT => "HEARTBEAT",
36 CT_HEARTBEAT_ACK => "HEARTBEAT-ACK",
37 CT_ABORT => "ABORT",
38 CT_SHUTDOWN => "SHUTDOWN",
39 CT_SHUTDOWN_ACK => "SHUTDOWN-ACK",
40 CT_ERROR => "ERROR",
41 CT_COOKIE_ECHO => "COOKIE-ECHO",
42 CT_COOKIE_ACK => "COOKIE-ACK",
43 CT_CWR => "ECNE", // Explicit Congestion Notification Echo
44 CT_SHUTDOWN_COMPLETE => "SHUTDOWN-COMPLETE",
45 CT_RECONFIG => "RECONFIG", // Re-configuration
46 CT_FORWARD_TSN => "FORWARD-TSN",
47 CT_I_FORWARD_TSN => "I-FORWARD-TSN",
48 _ => others.as_str(),
49 };
50 write!(f, "{}", s)
51 }
52}
53 
54#[cfg(test)]
55mod test {
56 use alloc::string::ToString;
57 use alloc::vec;
58 
59 use super::*;
60 
61 #[test]
62 fn test_chunk_type_string() {
63 let tests = vec![
64 (CT_PAYLOAD_DATA, "DATA"),
65 (CT_INIT, "INIT"),
66 (CT_INIT_ACK, "INIT-ACK"),
67 (CT_SACK, "SACK"),
68 (CT_HEARTBEAT, "HEARTBEAT"),
69 (CT_HEARTBEAT_ACK, "HEARTBEAT-ACK"),
70 (CT_ABORT, "ABORT"),
71 (CT_SHUTDOWN, "SHUTDOWN"),
72 (CT_SHUTDOWN_ACK, "SHUTDOWN-ACK"),
73 (CT_ERROR, "ERROR"),
74 (CT_COOKIE_ECHO, "COOKIE-ECHO"),
75 (CT_COOKIE_ACK, "COOKIE-ACK"),
76 (CT_CWR, "ECNE"),
77 (CT_SHUTDOWN_COMPLETE, "SHUTDOWN-COMPLETE"),
78 (CT_RECONFIG, "RECONFIG"),
79 (CT_FORWARD_TSN, "FORWARD-TSN"),
80 (CT_I_FORWARD_TSN, "I-FORWARD-TSN"),
81 (ChunkType(255), "Unknown ChunkType: 255"),
82 ];
83 
84 for (ct, expected) in tests {
85 assert_eq!(
86 ct.to_string(),
87 expected,
88 "failed to stringify chunkType {}, expected {}",
89 ct,
90 expected
91 );
92 }
93 }
94}