Skip to content

Commit

Permalink
Merge pull request #33 from mjhouse/refactor_data_words
Browse files Browse the repository at this point in the history
Added methods to data words
  • Loading branch information
mjhouse authored Mar 5, 2024
2 parents 329dba4 + 1f820f0 commit da80fa8
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 169 deletions.
19 changes: 15 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//! Error enums and flags

use core::array::TryFromSliceError;

/// A result type which uses the [Error] enum as the error type.
pub type Result<T> = core::result::Result<T, Error>;

/// Calculate a parity bit given a u16 word value
///
/// MIL STD 1553B uses an odd parity bit (1 if the
/// MIL STD 1553B uses an odd parity bit (1 if the
/// bit count of the data is even, 0 if not)[^1].
///
/// [^1]: [MIL-STD-1553 Tutorial](http://www.horntech.cn/techDocuments/MIL-STD-1553Tutorial.pdf)
/// [^1]: [MIL-STD-1553 Tutorial](http://www.horntech.cn/techDocuments/MIL-STD-1553Tutorial.pdf)
#[inline]
#[must_use = "Returned value is not used"]
pub(crate) fn parity<T: Into<u16>>(v: T) -> u8 {
match v.into().count_ones() % 2 {
pub(crate) const fn parity(v: u16) -> u8 {
match v.count_ones() % 2 {
0 => 1,
_ => 0,
}
Expand Down Expand Up @@ -41,6 +43,9 @@ pub enum Error {
/// A word was found to be invalid while building a message
WordIsInvalid,

/// An array could not be created from a given slice
FromSliceError,

/// A byte array could not be converted to a string
StringIsInvalid,

Expand Down Expand Up @@ -75,6 +80,12 @@ pub enum Error {
SystemError(SystemError),
}

impl From<TryFromSliceError> for Error {
fn from(_: TryFromSliceError) -> Self {
Self::FromSliceError
}
}

/// An error deriving from a remote terminal or bus controller.
///
/// These errors are generated during runtime by terminals and
Expand Down
25 changes: 5 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,11 @@ pub mod word;

pub use message::Message;

pub use errors::{
Result,
Error
};
pub use errors::{Error, Result};

pub use word::{
CommandWord,
StatusWord,
DataWord
};
pub use word::{CommandWord, DataWord, StatusWord};

pub use flags::{
ModeCode,
TransmitReceive,
Address,
SubAddress,
Instrumentation,
ServiceRequest,
Reserved,
BroadcastCommand,
TerminalBusy,
BusControlAccept,
};
Address, BroadcastCommand, BusControlAccept, Instrumentation, ModeCode, Reserved,
ServiceRequest, SubAddress, TerminalBusy, TransmitReceive,
};
4 changes: 2 additions & 2 deletions src/message/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mod tests {
let word = Word::Command(CommandWord::from(0b0001100001100010));
message.add(word.clone()).unwrap();

let data = Word::Data(DataWord::from(0b0110100001101001));
let data = Word::Data(DataWord::from_data(0b0110100001101001));
message.add(data.clone()).unwrap();

assert_eq!(message.word_count(), 2);
Expand Down Expand Up @@ -282,7 +282,7 @@ mod tests {
let status = Word::Status(StatusWord::from(0b0001100000000000));
message.add(status.clone()).unwrap();

let data = Word::Data(DataWord::from(0b0110100001101001));
let data = Word::Data(DataWord::from_data(0b0110100001101001));
message.add(data.clone()).unwrap();

assert_eq!(message.word_count(), 2);
Expand Down
34 changes: 17 additions & 17 deletions src/message/packets.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::errors::{parity, Error, Result};
use crate::word::{CommandWord, DataWord, StatusWord};
use crate::word::Sync;
use crate::word::{CommandWord, DataWord, StatusWord};

/// A packet of data parsed from binary
///
/// Incoming data is parsed into a triplet of (sync,data,parity)
/// using this struct, and then may be further parsed as an
/// using this struct, and then may be further parsed as an
/// explicit command, status, or data word.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Packet {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Packet {
/// Convert this packet into a data word
pub fn to_data(&self) -> Result<DataWord> {
if self.is_valid() & self.is_data() {
Ok(DataWord::from(self.value()))
Ok(DataWord::from_data(self.value()))
} else {
Err(Error::PacketIsInvalid)
}
Expand Down Expand Up @@ -103,82 +103,82 @@ mod tests {

#[test]
fn test_packet_value() {
let packet = Packet::new(DATA_SYNC,[0b01000000, 0b00100000],1);
let packet = Packet::new(DATA_SYNC, [0b01000000, 0b00100000], 1);
let value = packet.value();
assert_eq!(value, 0b0100000000100000);
}

#[test]
fn test_new_data_packet() {
let packet = Packet::new(DATA_SYNC,[0b00000000, 0b00000000],1);
let packet = Packet::new(DATA_SYNC, [0b00000000, 0b00000000], 1);
assert!(packet.is_data());
}

#[test]
fn test_new_service_packet() {
let packet = Packet::new(SERV_SYNC,[0b01000000, 0b00100000],1);
let packet = Packet::new(SERV_SYNC, [0b01000000, 0b00100000], 1);
assert!(packet.is_service());
}

#[test]
fn test_packet_parity_even_both() {
let packet = Packet::new(DATA_SYNC,[0b01000000, 0b00100000],1);
let packet = Packet::new(DATA_SYNC, [0b01000000, 0b00100000], 1);
assert_eq!(packet.parity, 1);
assert!(packet.is_valid());
}

#[test]
fn test_packet_parity_even_first() {
let packet = Packet::new(DATA_SYNC,[0b01100000, 0b00000000],1);
let packet = Packet::new(DATA_SYNC, [0b01100000, 0b00000000], 1);
assert_eq!(packet.parity, 1);
assert!(packet.is_valid());
}

#[test]
fn test_packet_parity_even_second() {
let packet = Packet::new(DATA_SYNC,[0b00000000, 0b00110000],1);
let packet = Packet::new(DATA_SYNC, [0b00000000, 0b00110000], 1);
assert_eq!(packet.parity, 1);
assert!(packet.is_valid());
}

#[test]
fn test_packet_parity_odd_both() {
let packet = Packet::new(DATA_SYNC,[0b01100000, 0b00100000],0);
let packet = Packet::new(DATA_SYNC, [0b01100000, 0b00100000], 0);
assert_eq!(packet.parity, 0);
assert!(packet.is_valid());
}

#[test]
fn test_packet_parity_odd_first() {
let packet = Packet::new(DATA_SYNC,[0b01110000, 0b00000000],0);
let packet = Packet::new(DATA_SYNC, [0b01110000, 0b00000000], 0);
assert_eq!(packet.parity, 0);
assert!(packet.is_valid());
}

#[test]
fn test_packet_parity_odd_second() {
let packet = Packet::new(DATA_SYNC,[0b00000000, 0b00111000],0);
let packet = Packet::new(DATA_SYNC, [0b00000000, 0b00111000], 0);
assert_eq!(packet.parity, 0);
assert!(packet.is_valid());
}

#[test]
fn test_packet_bad_parity_odd() {
let packet = Packet::new(DATA_SYNC,[0b00000000, 0b00111000],1);
let packet = Packet::new(DATA_SYNC, [0b00000000, 0b00111000], 1);
assert_eq!(packet.parity, 1);
assert!(!packet.is_valid());
}

#[test]
fn test_packet_bad_parity_even() {
let packet = Packet::new(DATA_SYNC,[0b00000000, 0b00110000],0);
let packet = Packet::new(DATA_SYNC, [0b00000000, 0b00110000], 0);
assert_eq!(packet.parity, 0);
assert!(!packet.is_valid());
}

#[test]
fn test_packet_convert_command() {
let packet = Packet::new(SERV_SYNC,[0b00011000, 0b01100010],0);
let packet = Packet::new(SERV_SYNC, [0b00011000, 0b01100010], 0);
let word = packet.to_command().unwrap();

assert_eq!(word.address(), Address::new(3));
Expand All @@ -190,10 +190,10 @@ mod tests {

#[test]
fn test_packet_convert_status() {
let packet = Packet::new(SERV_SYNC,[0b00011000, 0b00010000],0);
let packet = Packet::new(SERV_SYNC, [0b00011000, 0b00010000], 0);
let word = packet.to_status().unwrap();

assert_eq!(word.address(), Address::new(3));
assert_eq!(word.broadcast_received(),BroadcastCommand::Received);
assert_eq!(word.broadcast_received(), BroadcastCommand::Received);
}
}
2 changes: 1 addition & 1 deletion src/word/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ impl From<DataWord> for Type {
fn from(value: DataWord) -> Self {
Type::Data(value)
}
}
}
Loading

0 comments on commit da80fa8

Please sign in to comment.