use smol::lock::Mutex;
use crate::messages::Sender;
pub(crate) const CMD_LEN_MAX: usize = 255 + 6;
#[derive(Debug)]
pub(crate) struct Metadata<const CHANNELS: usize> {
pub(super) id: [u8; 2],
pub(crate) length: usize,
}
impl<const CH: usize> Metadata<CH> {
pub(crate) const fn payload(id: [u8; 2], length: usize) -> Self {
if length < 6 || length > CMD_LEN_MAX {
panic!("Invalid command length"); }
Self { id, length }
}
pub(crate) const fn header(id: [u8; 2]) -> Self {
Self::payload(id, 6)
}
}
#[derive(Debug)]
pub(crate) struct Command<const CH: usize> {
pub(super) id: [u8; 2],
pub(crate) length: usize,
pub(super) senders: [Mutex<Option<Sender>>; CH],
}
impl<const CH: usize> Command<CH> {
pub(super) const fn new(m: &Metadata<CH>) -> ([u8; 2], Command<CH>) {
let cmd = Command {
id: m.id,
length: m.length,
senders: [const { Mutex::new(None) }; CH],
};
(m.id, cmd)
}
pub(super) const fn sender(&self, channel: usize) -> &Mutex<Option<Sender>> {
let i = if channel <= 1 { 0 } else { channel - 1 };
&self.senders[i]
}
}