File
Blob: firmware/vendor/sctp-proto/src/chunk/chunk_type.rs
| 1 | use 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)] |
| 7 | pub(crate) struct ChunkType(pub(crate) u8); |
| 8 | |
| 9 | pub(crate) const CT_PAYLOAD_DATA: ChunkType = ChunkType(0); |
| 10 | pub(crate) const CT_INIT: ChunkType = ChunkType(1); |
| 11 | pub(crate) const CT_INIT_ACK: ChunkType = ChunkType(2); |
| 12 | pub(crate) const CT_SACK: ChunkType = ChunkType(3); |
| 13 | pub(crate) const CT_HEARTBEAT: ChunkType = ChunkType(4); |
| 14 | pub(crate) const CT_HEARTBEAT_ACK: ChunkType = ChunkType(5); |
| 15 | pub(crate) const CT_ABORT: ChunkType = ChunkType(6); |
| 16 | pub(crate) const CT_SHUTDOWN: ChunkType = ChunkType(7); |
| 17 | pub(crate) const CT_SHUTDOWN_ACK: ChunkType = ChunkType(8); |
| 18 | pub(crate) const CT_ERROR: ChunkType = ChunkType(9); |
| 19 | pub(crate) const CT_COOKIE_ECHO: ChunkType = ChunkType(10); |
| 20 | pub(crate) const CT_COOKIE_ACK: ChunkType = ChunkType(11); |
| 21 | pub(crate) const CT_CWR: ChunkType = ChunkType(13); |
| 22 | pub(crate) const CT_SHUTDOWN_COMPLETE: ChunkType = ChunkType(14); |
| 23 | pub(crate) const CT_RECONFIG: ChunkType = ChunkType(130); |
| 24 | pub(crate) const CT_FORWARD_TSN: ChunkType = ChunkType(192); |
| 25 | pub(crate) const CT_I_FORWARD_TSN: ChunkType = ChunkType(194); |
| 26 | |
| 27 | impl 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)] |
| 55 | mod 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 | } |