rustflexstack/facilities/decentralized_environmental_notification_service/
denm_ssp.rs1use super::denm_coder::{CauseCodeChoice, Denm};
14
15pub fn cause_code_ssp_bit(cause_code: u8) -> Option<(usize, u8)> {
17 Some(match cause_code {
18 1 => (1, 0x80),
19 2 => (1, 0x40),
20 3 => (1, 0x20),
21 6 => (1, 0x10),
22 9 => (1, 0x08),
23 10 => (1, 0x04),
24 11 => (1, 0x02),
25 12 => (1, 0x01),
26 14 => (2, 0x80),
27 15 => (2, 0x40),
28 17 => (2, 0x20),
29 18 => (2, 0x10),
30 19 => (2, 0x08),
31 26 => (2, 0x04),
32 27 => (2, 0x02),
33 91 => (2, 0x01),
34 92 => (3, 0x80),
35 93 => (3, 0x40),
36 94 => (3, 0x20),
37 95 => (3, 0x10),
38 96 => (3, 0x08),
39 97 => (3, 0x04),
40 98 => (3, 0x02),
41 99 => (3, 0x01),
42 5 => (4, 0x80),
43 7 => (4, 0x40),
44 28 => (4, 0x20),
45 100 => (4, 0x10),
46 4 => (4, 0x08),
47 20 => (4, 0x04),
48 _ => return None,
49 })
50}
51
52pub fn cause_code_of_choice(choice: &CauseCodeChoice) -> Option<u8> {
55 let debug = format!("{choice:?}");
56 let name = debug.split('(').next().unwrap_or("");
57 let digits: String = name
58 .chars()
59 .rev()
60 .take_while(|c| c.is_ascii_digit())
61 .collect::<Vec<_>>()
62 .into_iter()
63 .rev()
64 .collect();
65 digits.parse().ok()
66}
67
68pub fn cause_code_value(denm: &Denm) -> Option<u8> {
70 denm.denm
71 .situation
72 .as_ref()
73 .and_then(|s| cause_code_of_choice(&s.event_type.cc_and_scc))
74}
75
76pub fn ssp_permits_cause_code(ssp: &[u8], cause_code: u8) -> bool {
80 match cause_code_ssp_bit(cause_code) {
81 Some((octet, mask)) => octet < ssp.len() && ssp[octet] & mask != 0,
82 None => false,
83 }
84}
85
86pub fn ssp_consistent(ssp: Option<&[u8]>, denm: &Denm) -> bool {
90 match ssp {
91 None | Some([]) => true,
92 Some(ssp) => match cause_code_value(denm) {
93 None => true,
94 Some(cause) => ssp_permits_cause_code(ssp, cause),
95 },
96 }
97}
98
99pub fn permissions_bitmap(denm: &Denm) -> Vec<u8> {
104 match cause_code_value(denm).and_then(cause_code_ssp_bit) {
105 Some((octet, mask)) => {
106 let mut bitmap = vec![0u8; octet + 1];
107 bitmap[octet] = mask;
108 bitmap
109 }
110 None => vec![0],
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::super::denm_coder::{AccidentSubCauseCode, CauseCodeChoice};
117 use super::*;
118
119 #[test]
120 fn bits_of_table_10() {
121 let ssp = [2u8, 0x40, 0x00, 0x10, 0x00]; assert!(ssp_permits_cause_code(&ssp, 2));
123 assert!(ssp_permits_cause_code(&ssp, 95));
124 assert!(!ssp_permits_cause_code(&ssp, 1));
125 assert!(!ssp_permits_cause_code(&ssp, 99));
126 }
127
128 #[test]
129 fn release_2_code_needs_version_2_ssp() {
130 let version_1 = [1u8, 0xFF, 0xFF, 0xFF];
131 assert!(ssp_permits_cause_code(&version_1, 99));
132 assert!(!ssp_permits_cause_code(&version_1, 5)); assert!(ssp_permits_cause_code(&[1u8, 0xFF, 0xFF, 0xFF, 0x80], 5));
134 }
135
136 #[test]
137 fn cause_code_from_choice_name() {
138 let choice = CauseCodeChoice::accident2(AccidentSubCauseCode(3));
139 assert_eq!(cause_code_of_choice(&choice), Some(2));
140 }
141}