Skip to main content

ygw_mil1553/
lib.rs

1pub mod mil1553dev;
2use std::time::Duration;
3
4use bitflags::bitflags;
5use mil1553dev::MsgId;
6use thiserror::Error;
7use ygw::YgwError;
8
9/// maximum number of words (u16) in a MIL-1553 message
10pub const MAX_DATA_SIZE: usize = 32;
11
12#[derive(Error, Debug)]
13pub enum Mil1553Error {
14    #[error("device driver error ({0}): {1}")]
15    DevError(i32, String),
16    #[error("invalid message id: {0}")]
17    InvalidMessageId(MsgId),
18    #[error("duplicate message id: {0}")]
19    DuplicateMessageId(MsgId),
20    #[error("too many messages: {0}")]
21    TooManyMessages(MsgId),
22    #[error("too many frames: {0}")]
23    TooManyFrames(MsgId),
24    #[error("too many opcodes: {0}")]
25    TooManyOpCodes(MsgId),
26    #[error("invalid state: {0}")]
27    InvalidState(String),
28    #[error("Frame too long: {0:?}")]
29    FrameTooLong(Duration),
30}
31
32impl From<Mil1553Error> for YgwError {
33    fn from(value: Mil1553Error) -> Self {
34        YgwError::Other(Box::new(value))
35    }
36}
37pub type Result<T> = std::result::Result<T, Mil1553Error>;
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40pub struct RtAddr {
41    a: u8,
42}
43
44impl RtAddr {
45    pub const fn new(a: u8) -> RtAddr {
46        if a > 31 {
47            panic!("Address should be between 0 and 31")
48        }
49        RtAddr { a }
50    }
51    pub const fn addr(&self) -> u8 {
52        self.a
53    }
54}
55
56pub enum TxRx {
57    BcToRt = 0,
58    RtToBc = 1,
59}
60
61#[derive(Debug)]
62pub enum MilbusData {
63    HealthStatus(Vec<u8>),
64    Telemetry(Vec<u8>),
65}
66
67#[derive(Debug)]
68pub enum MilbusCommand {
69    Telecommand(RtAddr, Vec<u8>),
70}
71
72bitflags! {
73    /// A set of possible errors related to message transmission
74    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
75    pub struct MsgError: u32 {
76        /// no error
77        const NOERROR = 0x0000_0000;
78        /// No response was received from the RT
79        const NORESP = 0x0000_0001;
80        ///  protocol error occurred
81        const PROTOCOL = 0x0000_0002;
82        /// Wrong polarity of the sync pulse
83        const SYNC = 0x0000_0004;
84        /// Too many/too few data words
85        const DATACOUNT = 0x0000_0008;
86        /// Manchester error
87        const MANCHESTER = 0x0000_0010;
88        /// Parity error
89        const PARITY = 0x0000_0020;
90        /// Internal error
91        const INTERNAL = 0x0000_0040;
92        /// Concurrency error - the message was being transmitted/received when being read/changed
93        const CONCURRENCY = 0x0000_0080;
94        /// Test loop error
95        const TESTLOOP = 0x0000_0100;
96    }
97}
98#[derive(Clone, Copy)]
99pub struct RtMessageId {
100    pub id: u16,
101    pub sub_addr: RtAddr,
102}
103
104/// a message transmitted on the bus
105#[derive(Debug)]
106pub struct BusMessage {
107    pub timetag: u64,
108    cmdwrd: u16,
109
110    pub data: Vec<u16>,
111
112    /// status received from the BC if any
113    pub status: Option<u16>,
114    /// if there was an error transmitting/receiving
115    pub error: MsgError,
116}
117
118impl BusMessage {
119    pub fn new(
120        timetag: u64,
121        cmdwrd: u16,
122        data: Vec<u16>,
123        error: MsgError,
124        status: Option<u16>,
125    ) -> Self {
126        BusMessage {
127            timetag,
128            cmdwrd,
129            data,
130            error,
131            status,
132        }
133    }
134
135    pub fn addr(&self) -> RtAddr {
136        RtAddr::new((self.cmdwrd >> 11) as u8)
137    }
138
139    pub fn sub_addr(&self) -> RtAddr {
140        RtAddr::new(((self.cmdwrd >> 5) & 0x1F) as u8)
141    }
142
143    pub fn txrx(&self) -> TxRx {
144        if (self.cmdwrd >> 10) & 1 == 0 {
145            TxRx::BcToRt
146        } else {
147            TxRx::RtToBc
148        }
149    }
150}
151pub fn hex(data: &[u16]) -> String {
152    let hex_strings: Vec<String> = data.iter().map(|x| format!("{:04X}", x)).collect();
153    hex_strings.join(" ")
154}
155
156pub fn hex8(data: &[u8]) -> String {
157    let hex_strings: Vec<String> = data.iter().map(|x| format!("{:02X}", x)).collect();
158    hex_strings.join(" ")
159}
160
161//builds the u16 command word
162pub fn pack_cwd(addr: RtAddr, sub_addr: RtAddr, txrx: TxRx, len_or_mode: u8) -> u16 {
163    ((addr.a as u16) << 11)
164        | ((txrx as u16) << 10)
165        | ((sub_addr.a as u16) << 5)
166        | ((len_or_mode & 0x1F) as u16)
167}