eurosim-obsw/src/can_ids.rs
lkoester 322fc70ac7 ...
2023-04-26 17:17:35 +02:00

417 lines
18 KiB
Rust

use embedded_can::{Id, StandardId};
use log::warn;
use std::collections::HashMap;
use crate::tmtc::{CSS_APID, GPS_APID, MGM_APID, MGT_APID, PLD_APID, PWR_APID, RWL_APID, STR_APID};
pub use num_derive::{FromPrimitive, ToPrimitive};
pub use num_traits::{FromPrimitive, ToPrimitive};
pub use strum::IntoEnumIterator; // 0.17.1
pub use strum_macros::EnumIter; // 0.17.1
#[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)]
pub enum ThreadId {
AOCSThread,
PowerThread,
PLDThread,
DeviceHandlerThread,
}
#[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)]
pub enum MessageId {
PCDUStatusRequest = 10,
DevicePowerOnRequest = 11,
DevicePowerOffRequest = 12,
DevicePowerStatusRequest = 13,
PCDUStatusResponse = 20,
DevicePowerStatusResponse = 21,
DevicePowerOnRequestConfirmation = 22,
DevicePowerOffRequestConfirmation = 23,
DevicePowerOnConfirmation = 24,
DevicePowerOffConfirmation = 25,
AOCSControlMGT1 = 31,
AOCSControlMGT2 = 32,
AOCSControlMGT3 = 33,
AOCSControlMGT4 = 34,
AOCSControlRWL1 = 35,
AOCSControlRWL2 = 36,
AOCSControlRWL3 = 37,
AOCSControlRWL4 = 38,
AOCSDataRequestMGM1 = 41,
AOCSDataRequestMGM2 = 42,
AOCSDataRequestMGM3 = 43,
AOCSDataRequestMGM4 = 44,
AOCSDataRequestSunSensor1 = 45,
AOCSDataRequestSunSensor2 = 46,
AOCSDataRequestSunSensor3 = 47,
AOCSDataRequestSunSensor4 = 48,
AOCSDataRequestSunSensor5 = 49,
AOCSDataRequestSunSensor6 = 50,
AOCSDataRequestStarTracker = 51,
AOCSDataRequestGPS = 55,
AOCSDataMGM1 = 61,
AOCSDataMGM2 = 62,
AOCSDataMGM3 = 63,
AOCSDataMGM4 = 64,
AOCSDataSunSensor1 = 65,
AOCSDataSunSensor2 = 66,
AOCSDataSunSensor3 = 67,
AOCSDataSunSensor4 = 68,
AOCSDataSunSensor5 = 69,
AOCSDataSunSensor6 = 70,
AOCSDataStarTracker1 = 71,
AOCSDataStarTracker2 = 72,
AOCSDataStarTracker3 = 73,
AOCSDataStarTracker4 = 74,
AOCSDataGPSLatitude = 81,
AOCSDataGPSLongitude = 82,
AOCSDataGPSAltitude = 83,
CameraImageRequest = 101,
CameraImageRequestConfirmation = 102,
CameraImageExecutionStart = 103,
CameraImageExectutionEnd = 104,
}
#[derive(Debug, EnumIter, Eq, Hash, PartialEq, Copy, Clone, FromPrimitive)]
pub enum DeviceId {
OBC = 1,
PCDU = 2,
MGM1 = 3,
MGM2 = 4,
MGM3 = 5,
MGM4 = 6,
SunSensor1 = 7,
SunSensor2 = 8,
SunSensor3 = 9,
SunSensor4 = 10,
SunSensor5 = 11,
SunSensor6 = 12,
StarTracker = 13,
MGT1 = 14,
MGT2 = 15,
MGT3 = 16,
MGT4 = 17,
RWL1 = 18,
RWL2 = 19,
RWL3 = 20,
RWL4 = 21,
Camera = 22,
All = 23,
GPS = 24,
}
impl TryFrom<u16> for DeviceId {
type Error = ();
fn try_from(v: u16) -> Result<Self, Self::Error> {
match v {
x if x == DeviceId::OBC as u16 => Ok(DeviceId::OBC),
x if x == DeviceId::PCDU as u16 => Ok(DeviceId::PCDU),
x if x == DeviceId::MGM1 as u16 => Ok(DeviceId::MGM1),
x if x == DeviceId::MGM2 as u16 => Ok(DeviceId::MGM2),
x if x == DeviceId::MGM3 as u16 => Ok(DeviceId::MGM3),
x if x == DeviceId::MGM4 as u16 => Ok(DeviceId::MGM4),
x if x == DeviceId::SunSensor1 as u16 => Ok(DeviceId::SunSensor1),
x if x == DeviceId::SunSensor2 as u16 => Ok(DeviceId::SunSensor2),
x if x == DeviceId::SunSensor3 as u16 => Ok(DeviceId::SunSensor3),
x if x == DeviceId::SunSensor4 as u16 => Ok(DeviceId::SunSensor4),
x if x == DeviceId::SunSensor5 as u16 => Ok(DeviceId::SunSensor5),
x if x == DeviceId::SunSensor6 as u16 => Ok(DeviceId::SunSensor6),
x if x == DeviceId::StarTracker as u16 => Ok(DeviceId::StarTracker),
x if x == DeviceId::MGT1 as u16 => Ok(DeviceId::MGT1),
x if x == DeviceId::MGT2 as u16 => Ok(DeviceId::MGT2),
x if x == DeviceId::MGT3 as u16 => Ok(DeviceId::MGT3),
x if x == DeviceId::MGT4 as u16 => Ok(DeviceId::MGT4),
x if x == DeviceId::RWL1 as u16 => Ok(DeviceId::RWL1),
x if x == DeviceId::RWL2 as u16 => Ok(DeviceId::RWL2),
x if x == DeviceId::RWL3 as u16 => Ok(DeviceId::RWL3),
x if x == DeviceId::RWL4 as u16 => Ok(DeviceId::RWL4),
x if x == DeviceId::Camera as u16 => Ok(DeviceId::Camera),
x if x == DeviceId::All as u16 => Ok(DeviceId::All),
x if x == DeviceId::GPS as u16 => Ok(DeviceId::GPS),
_ => Err(()),
}
}
}
#[derive(Debug)]
pub struct MessageModel {
message_id: MessageId,
data: Vec<u8>,
}
impl MessageModel {
pub fn new(message_id: MessageId, data: &[u8]) -> Result<MessageModel, ()> {
if data.len() > 8 {
warn!("Data exceeds maximum length.");
return Err(());
}
let vec = Vec::from(data);
return Ok(MessageModel {
message_id,
data: vec,
});
}
pub fn message_id(&self) -> MessageId {
self.message_id
}
pub fn data(&self) -> &Vec<u8> {
&self.data
}
}
// TODO delete!!
#[derive(Debug, Copy, Clone)]
pub struct SenderReceiverThread {
sender: DeviceId,
receiver: DeviceId,
thread: ThreadId,
}
impl SenderReceiverThread {
pub fn new(sender: DeviceId, receiver: DeviceId, thread: ThreadId) -> SenderReceiverThread {
SenderReceiverThread {
sender,
receiver,
thread,
}
}
pub fn get_sender(&self) -> DeviceId {
self.sender
}
pub fn get_receiver(&self) -> DeviceId {
self.receiver
}
pub fn get_thread(&self) -> ThreadId {
self.thread
}
}
pub fn load_message_id_to_threads() -> HashMap<MessageId, SenderReceiverThread> {
let mut message_map: HashMap<MessageId, SenderReceiverThread> = HashMap::new();
let properties = vec![
SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::PCDU, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::PCDU, DeviceId::OBC, ThreadId::PowerThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT1, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT2, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT3, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGT4, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL1, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL2, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL3, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::RWL4, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM1, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM2, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM3, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::MGM4, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor1, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor2, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor3, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor4, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor5, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::SunSensor6, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::StarTracker, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::GPS, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::MGM1, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::MGM2, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::MGM3, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::MGM4, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::SunSensor1, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::SunSensor2, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::SunSensor3, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::SunSensor4, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::SunSensor5, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::SunSensor6, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::StarTracker, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::StarTracker, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::StarTracker, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::StarTracker, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::GPS, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::GPS, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::GPS, DeviceId::OBC, ThreadId::AOCSThread),
SenderReceiverThread::new(DeviceId::OBC, DeviceId::Camera, ThreadId::PLDThread),
SenderReceiverThread::new(DeviceId::Camera, DeviceId::OBC, ThreadId::PLDThread),
SenderReceiverThread::new(DeviceId::Camera, DeviceId::OBC, ThreadId::PLDThread),
SenderReceiverThread::new(DeviceId::Camera, DeviceId::OBC, ThreadId::PLDThread),
];
let mut i = 0;
for id in MessageId::iter() {
let value = properties.get(i).unwrap();
message_map.insert(id, *value);
i += 1;
}
return message_map;
}
#[derive(Debug, Copy, Clone)]
pub struct SenderReceiverApid {
sender: DeviceId,
receiver: DeviceId,
apid: u16,
}
impl SenderReceiverApid {
pub fn new(sender: DeviceId, receiver: DeviceId, apid: u16) -> Self {
Self {
sender,
receiver,
apid,
}
}
pub fn get_sender(&self) -> DeviceId {
self.sender
}
pub fn get_receiver(&self) -> DeviceId {
self.receiver
}
pub fn get_apid(&self) -> u16 {
self.apid
}
}
pub fn load_message_id_to_apids() -> HashMap<MessageId, SenderReceiverApid> {
let mut message_map: HashMap<MessageId, SenderReceiverApid> = HashMap::new();
let properties = vec![
SenderReceiverApid::new(DeviceId::OBC, DeviceId::PCDU, PWR_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::PCDU, PWR_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::PCDU, PWR_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::PCDU, PWR_APID),
SenderReceiverApid::new(DeviceId::PCDU, DeviceId::OBC, PWR_APID),
SenderReceiverApid::new(DeviceId::PCDU, DeviceId::OBC, PWR_APID),
SenderReceiverApid::new(DeviceId::PCDU, DeviceId::OBC, PWR_APID),
SenderReceiverApid::new(DeviceId::PCDU, DeviceId::OBC, PWR_APID),
SenderReceiverApid::new(DeviceId::PCDU, DeviceId::OBC, PWR_APID),
SenderReceiverApid::new(DeviceId::PCDU, DeviceId::OBC, PWR_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGT1, MGT_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGT2, MGT_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGT3, MGT_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGT4, MGT_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::RWL1, RWL_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::RWL2, RWL_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::RWL3, RWL_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::RWL4, RWL_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGM1, MGM_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGM2, MGM_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGM3, MGM_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::MGM4, MGM_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::SunSensor1, CSS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::SunSensor2, CSS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::SunSensor3, CSS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::SunSensor4, CSS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::SunSensor5, CSS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::SunSensor6, CSS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::StarTracker, STR_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::GPS, GPS_APID),
SenderReceiverApid::new(DeviceId::MGM1, DeviceId::OBC, MGM_APID),
SenderReceiverApid::new(DeviceId::MGM2, DeviceId::OBC, MGM_APID),
SenderReceiverApid::new(DeviceId::MGM3, DeviceId::OBC, MGM_APID),
SenderReceiverApid::new(DeviceId::MGM4, DeviceId::OBC, MGM_APID),
SenderReceiverApid::new(DeviceId::SunSensor1, DeviceId::OBC, CSS_APID),
SenderReceiverApid::new(DeviceId::SunSensor2, DeviceId::OBC, CSS_APID),
SenderReceiverApid::new(DeviceId::SunSensor3, DeviceId::OBC, CSS_APID),
SenderReceiverApid::new(DeviceId::SunSensor4, DeviceId::OBC, CSS_APID),
SenderReceiverApid::new(DeviceId::SunSensor5, DeviceId::OBC, CSS_APID),
SenderReceiverApid::new(DeviceId::SunSensor6, DeviceId::OBC, CSS_APID),
SenderReceiverApid::new(DeviceId::StarTracker, DeviceId::OBC, STR_APID),
SenderReceiverApid::new(DeviceId::StarTracker, DeviceId::OBC, STR_APID),
SenderReceiverApid::new(DeviceId::StarTracker, DeviceId::OBC, STR_APID),
SenderReceiverApid::new(DeviceId::StarTracker, DeviceId::OBC, STR_APID),
SenderReceiverApid::new(DeviceId::GPS, DeviceId::OBC, GPS_APID),
SenderReceiverApid::new(DeviceId::GPS, DeviceId::OBC, GPS_APID),
SenderReceiverApid::new(DeviceId::GPS, DeviceId::OBC, GPS_APID),
SenderReceiverApid::new(DeviceId::OBC, DeviceId::Camera, PLD_APID),
SenderReceiverApid::new(DeviceId::Camera, DeviceId::OBC, PLD_APID),
SenderReceiverApid::new(DeviceId::Camera, DeviceId::OBC, PLD_APID),
SenderReceiverApid::new(DeviceId::Camera, DeviceId::OBC, PLD_APID),
];
let mut i = 0;
for id in MessageId::iter() {
let value = properties.get(i).unwrap();
message_map.insert(id, *value);
i += 1;
}
return message_map;
}
/*
pub fn load_device_ids() {
let mut package_map: HashMap<DeviceId, DeviceProperties> = HashMap::new();
let properties = vec![
DeviceProperties::new(DeviceType::OBC, DeviceState::Unknown),
DeviceProperties::new(DeviceType::PCDU, DeviceState::Unknown),
DeviceProperties::new(DeviceType::MGM, DeviceState::Unknown),
DeviceProperties::new(DeviceType::MGM, DeviceState::Unknown),
DeviceProperties::new(DeviceType::MGM, DeviceState::Unknown),
DeviceProperties::new(DeviceType::CRS, DeviceState::Unknown),
DeviceProperties::new(DeviceType::CRS, DeviceState::Unknown),
DeviceProperties::new(DeviceType::CRS, DeviceState::Unknown),
DeviceProperties::new(DeviceType::STR, DeviceState::Unknown),
DeviceProperties::new(DeviceType::MGT, DeviceState::Unknown),
DeviceProperties::new(DeviceType::MGT, DeviceState::Unknown),
DeviceProperties::new(DeviceType::MGT, DeviceState::Unknown),
DeviceProperties::new(DeviceType::RWL, DeviceState::Unknown),
DeviceProperties::new(DeviceType::RWL, DeviceState::Unknown),
DeviceProperties::new(DeviceType::RWL, DeviceState::Unknown),
DeviceProperties::new(DeviceType::CAM, DeviceState::Unknown),
];
let mut i = 0;
for id in DeviceId::iter() {
let value = properties.get(i).unwrap();
package_map.insert(id, *value);
i += 1;
}
}
*/
//TODO: change ids from u32 to embeddedcan ids
pub fn message_id_to_value(packageid: &MessageId) -> u32 {
*packageid as u32
}
pub fn message_id_to_can_id(packageid: &MessageId) -> Id {
let x = *packageid as u16;
Id::Standard(StandardId::new(x).unwrap())
}
pub fn device_id_to_value(deviceid: &DeviceId) -> u32 {
*deviceid as u32
}
pub fn value_to_message_id(value: u32) -> Option<MessageId> {
let element = FromPrimitive::from_u32(value);
return element;
}
pub fn can_id_to_message_id(value: Id) -> Option<MessageId> {
let buf = match value {
Id::Standard(id) => id.as_raw() as u32,
Id::Extended(id) => id.as_raw(),
};
let element = FromPrimitive::from_u32(buf);
return element;
}
pub fn value_to_device_id(value: u32) -> Option<DeviceId> {
let element = FromPrimitive::from_u32(value);
return element;
}