1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::ingress::buffer::Buffer;
pub const BUFF_SIZE: usize = 512;
pub const BUFF_COUNT: usize = 4;
#[derive(Copy, Clone)]
pub struct Notification {
section_indexes: [usize; 3],
inner: Buffer,
}
impl Notification {
pub const fn default() -> Notification {
Notification {
section_indexes: [0usize; 3],
inner: Buffer {
btype: crate::ingress::buffer::Type::Unknown,
payload: [0u8; BUFF_SIZE],
payload_idx: 0,
},
}
}
pub fn buffer(&self) -> &[u8] {
&self.inner.payload[..self.inner.payload_idx]
}
pub fn from_buffer(buffer: &Buffer, idxs: &[usize; 3]) -> Result<Notification, NotificationError> {
Ok(Notification {
section_indexes: idxs.clone(),
inner: buffer.clone()
})
}
pub fn source(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.inner.payload[0..self.section_indexes[1]]) }
}
pub fn title(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.inner.payload[self.section_indexes[0]..self.section_indexes[1]]) }
}
pub fn body(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.inner.payload[self.section_indexes[1]..self.section_indexes[2]]) }
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum NotificationError {
Parsing,
}
pub struct NotificationManager {
pool: [Notification; BUFF_COUNT],
idx: usize,
}
impl NotificationManager {
pub fn new() -> NotificationManager {
NotificationManager {
pool: [Notification::default(); BUFF_COUNT],
idx: 0,
}
}
pub fn peek_notification<F>(&mut self, index: usize, f: F)
where
F: FnOnce(&Notification),
{
let notification = &self.pool[index];
f(¬ification);
}
pub fn idx(&self) -> usize {
self.idx
}
pub fn add(&mut self, buffer: &Buffer, idxs: &[usize; 3]) -> Result<(), NotificationError> {
self.pool[self.idx] = Notification::from_buffer(buffer, idxs)?;
self.idx += 1;
if self.idx + 1 > self.pool.len() {
self.idx = 0;
}
Ok(())
}
}