use crate::ingress::{buffer::Buffer, ingress_manager::BUFF_COUNT};
#[derive(Copy, Clone)]
pub struct Notification {
inner: Buffer,
}
impl Notification {
pub const fn default() -> Notification {
Notification {
inner: Buffer {
btype: crate::ingress::buffer::Type::Unknown,
payload: [0u8; crate::ingress::ingress_manager::BUFF_SIZE],
payload_idx: 0,
},
}
}
pub fn buffer(&self) -> &[u8] {
&self.inner.payload[..self.inner.payload_idx]
}
pub fn parse_buffer(&mut self, buffer: &Buffer) -> Result<(), NotificationError> {
self.inner = buffer.clone();
Ok(())
}
}
#[derive(Copy, Clone, Debug)]
pub enum NotificationError {
Parsing,
}
pub struct NotificationManager {
pool: &'static mut [Notification; BUFF_COUNT],
idx: usize,
}
impl NotificationManager {
pub fn new(notifications: &'static mut [Notification; BUFF_COUNT]) -> NotificationManager {
NotificationManager {
pool: notifications,
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) -> Result<(), NotificationError> {
self.pool[self.idx].parse_buffer(buffer)?;
self.idx += 1;
if self.idx + 1 > self.pool.len() {
self.idx = 0;
}
Ok(())
}
}