Skip to content

Commit

Permalink
Provide Default and simple UI constructor for Ax25Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
thombles committed Apr 15, 2024
1 parent a551bc1 commit 5c502d1
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions ax25/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,12 @@ impl FrameContent {
}
}

/// A source or destination of an AX.25 frame, combining a callsign with an SSID.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
/// A source, destination or repeater in an AX.25 frame.
///
/// An `Address` is a combination of a callsign and a numeric SSID.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Address {
// An alphanumeric ASCII callsign of maximum length 6, e.g. "VK7NTK"
callsign: String,
/// Secondary Station Identifier, from 0 to 15
ssid: u8,
}

Expand Down Expand Up @@ -477,19 +477,6 @@ pub struct Ax25Frame {
}

impl Ax25Frame {
/// Returns a UTF-8 string that is a "best effort" at displaying the information
/// content of this frame. Returns None if there is no information field present.
/// Most applications will need to work with the Vec<u8> info directly.
pub fn info_string_lossy(&self) -> Option<String> {
match self.content {
FrameContent::Information(ref i) => Some(String::from_utf8_lossy(&i.info).into_owned()),
FrameContent::UnnumberedInformation(ref ui) => {
Some(String::from_utf8_lossy(&ui.info).into_owned())
}
_ => None,
}
}

/// Parse raw bytes into an Ax25Frame if possible.
pub fn from_bytes(bytes: &[u8]) -> Result<Ax25Frame, FrameParseError> {
// Skip over leading null bytes
Expand Down Expand Up @@ -545,6 +532,20 @@ impl Ax25Frame {
})
}

/// Construct a basic UnnumberedInformation (connectionless) frame with chosen data.
pub fn new_simple_ui_frame(source: Address, destination: Address, info: Vec<u8>) -> Self {
Self {
source,
destination,
content: FrameContent::UnnumberedInformation(UnnumberedInformation {
pid: ProtocolIdentifier::None,
info,
poll_or_final: false,
}),
..Default::default()
}
}

/// Encode an Ax25Frame struct as raw bytes for transmission
pub fn to_bytes(&self) -> Vec<u8> {
let mut frame = Vec::new();
Expand All @@ -567,6 +568,35 @@ impl Ax25Frame {
frame.extend(self.content.encode());
frame
}

/// Returns a UTF-8 string that is a "best effort" at displaying the information
/// content of this frame. Returns None if there is no information field present.
/// Most applications will need to work with the Vec<u8> info directly.
pub fn info_string_lossy(&self) -> Option<String> {
match self.content {
FrameContent::Information(ref i) => Some(String::from_utf8_lossy(&i.info).into_owned()),
FrameContent::UnnumberedInformation(ref ui) => {
Some(String::from_utf8_lossy(&ui.info).into_owned())
}
_ => None,
}
}
}

impl Default for Ax25Frame {
fn default() -> Self {
Self {
source: Address::default(),
destination: Address::default(),
route: vec![],
command_or_response: Some(CommandResponse::Command),
content: FrameContent::UnnumberedInformation(UnnumberedInformation {
pid: ProtocolIdentifier::None,
info: vec![],
poll_or_final: false,
}),
}
}
}

impl fmt::Display for Ax25Frame {
Expand Down

0 comments on commit 5c502d1

Please sign in to comment.