sat-rs/satrs-core/src/mode.rs

95 lines
2.2 KiB
Rust
Raw Normal View History

2023-02-15 00:33:33 +01:00
use crate::tmtc::TargetId;
2023-02-15 11:19:23 +01:00
use core::mem::size_of;
2023-07-12 13:48:59 +02:00
#[cfg(feature = "serde")]
2023-02-15 00:33:33 +01:00
use serde::{Deserialize, Serialize};
2023-08-18 10:21:15 +02:00
use spacepackets::ByteConversionError;
2023-02-15 00:33:33 +01:00
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2023-02-15 11:05:32 +01:00
pub struct ModeAndSubmode {
2023-02-15 00:33:33 +01:00
mode: u32,
submode: u16,
}
2023-02-15 11:05:32 +01:00
impl ModeAndSubmode {
pub const fn new_mode_only(mode: u32) -> Self {
2023-02-15 11:19:23 +01:00
Self { mode, submode: 0 }
2023-02-15 11:05:32 +01:00
}
pub const fn new(mode: u32, submode: u16) -> Self {
2023-02-15 11:19:23 +01:00
Self { mode, submode }
2023-02-15 11:05:32 +01:00
}
pub fn raw_len() -> usize {
size_of::<u32>() + size_of::<u16>()
}
pub fn from_be_bytes(buf: &[u8]) -> Result<Self, ByteConversionError> {
if buf.len() < 6 {
2023-08-18 10:21:15 +02:00
return Err(ByteConversionError::FromSliceTooSmall {
2023-02-15 11:05:32 +01:00
expected: 6,
2023-02-15 11:19:23 +01:00
found: buf.len(),
2023-08-18 10:21:15 +02:00
});
2023-02-15 11:05:32 +01:00
}
Ok(Self {
mode: u32::from_be_bytes(buf[0..4].try_into().unwrap()),
2023-02-15 11:19:23 +01:00
submode: u16::from_be_bytes(buf[4..6].try_into().unwrap()),
2023-02-15 11:05:32 +01:00
})
}
2023-02-28 12:02:18 +01:00
pub fn mode(&self) -> u32 {
self.mode
}
2023-02-28 12:02:18 +01:00
pub fn submode(&self) -> u16 {
self.submode
}
2023-02-15 11:05:32 +01:00
}
2023-02-15 00:33:33 +01:00
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ModeCommand {
2023-02-28 11:59:56 +01:00
pub address: TargetId,
pub mode_submode: ModeAndSubmode,
2023-02-15 11:05:32 +01:00
}
impl ModeCommand {
pub const fn new(address: TargetId, mode_submode: ModeAndSubmode) -> Self {
Self {
address,
2023-02-15 11:19:23 +01:00
mode_submode,
2023-02-15 11:05:32 +01:00
}
}
2023-02-16 15:43:10 +01:00
pub fn address(&self) -> TargetId {
self.address
}
pub fn mode_submode(&self) -> ModeAndSubmode {
self.mode_submode
}
2023-02-16 16:37:28 +01:00
pub fn mode(&self) -> u32 {
2023-02-16 15:43:10 +01:00
self.mode_submode.mode
}
2023-02-16 16:37:28 +01:00
pub fn submode(&self) -> u16 {
2023-02-16 15:43:10 +01:00
self.mode_submode.submode
}
2023-02-15 00:33:33 +01:00
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ModeRequest {
2023-02-27 11:51:48 +01:00
SetMode(ModeAndSubmode),
2023-02-27 13:44:24 +01:00
ReadMode,
AnnounceMode,
AnnounceModeRecursive,
2023-02-27 11:51:48 +01:00
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TargetedModeRequest {
target_id: TargetId,
2023-02-27 13:44:24 +01:00
mode_request: ModeRequest,
2023-02-15 00:33:33 +01:00
}