Skip to content

Commit

Permalink
feat: add support for msg type 7
Browse files Browse the repository at this point in the history
  • Loading branch information
salsabiljb committed Aug 21, 2024
1 parent 22993c9 commit a14522b
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 12 deletions.
110 changes: 110 additions & 0 deletions src/messages/binary_acknowledge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Binary Acknowledge (type 7)
use super::AisMessageType;
use crate::errors::Result;
use crate::lib;
use nom::bits::{bits, complete::take as take_bits};
use nom::multi::many_m_n;
use nom::IResult;

#[derive(Debug, PartialEq, Eq)]
pub struct Acknowledgement {
pub mmsi: u32,
pub seq_num: u8,
}

impl Acknowledgement {
pub fn parse(data: (&[u8], usize)) -> IResult<(&[u8], usize), Self> {
let (data, mmsi) = take_bits(30u32)(data)?;
let (data, seq_num) = take_bits(2u8)(data)?;
Ok((data, Self { mmsi, seq_num }))
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
pub type AcknowledgementList = lib::std::vec::Vec<Acknowledgement>;
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
pub type AcknowledgementList = lib::std::vec::Vec<Acknowledgement, 4>;

#[derive(Debug, PartialEq, Eq)]
pub struct BinaryAcknowledge {
pub message_type: u8,
pub repeat_indicator: u8,
pub mmsi: u32,
pub acks: AcknowledgementList,
}

impl<'a> AisMessageType<'a> for BinaryAcknowledge {
fn name(&self) -> &'static str {
"Binary Acknowledge"
}

fn parse(data: &'a [u8]) -> Result<Self> {
let (_, report) = parse_base(data)?;
Ok(report)
}
}

fn parse_base<'a>(data: &'a [u8]) -> IResult<&'a [u8], BinaryAcknowledge> {
bits(move |data: (&'a [u8], usize)| -> IResult<_, _> {
let (data, message_type) = take_bits(6u8)(data)?;
let (data, repeat_indicator) = take_bits(2u8)(data)?;
let (data, mmsi) = take_bits(30u32)(data)?;
let (data, _spare) = take_bits::<_, u8, _, _>(2u8)(data)?;

#[cfg(any(feature = "std", feature = "alloc"))]
let (data, acks) = many_m_n(1, 4, Acknowledgement::parse)(data)?;
#[cfg(all(not(feature = "std"), not(feature = "alloc")))]
let (data, acks) = many_m_n::<_, _, _, _, 4>(1, Acknowledgement::parse)(data)?;

Ok((
data,
BinaryAcknowledge {
message_type,
repeat_indicator,
mmsi,
acks,
},
))
})(data)
}

#[cfg(test)]
mod tests {
#![allow(clippy::unreadable_literal)]
use super::*;

#[test]
fn test_type7_example_1() {
let bytestream = b"702R5`hwCt40";
let bitstream = crate::messages::unarmor(bytestream, 0).unwrap();
let report = BinaryAcknowledge::parse(bitstream.as_ref()).unwrap();

assert_eq!(report.message_type, 7);
assert_eq!(report.repeat_indicator, 0);
assert_eq!(report.mmsi, 2655651);
assert_eq!(report.acks.len(), 1);
let ack = &report.acks[0];
assert_eq!(ack.mmsi, 265547840);
assert_eq!(ack.seq_num, 0);
}

#[test]
fn test_type7_example_2() {
let bytestream = b"702R5`hwCjq8";
let bitstream = crate::messages::unarmor(bytestream, 0).unwrap();
let report = BinaryAcknowledge::parse(bitstream.as_ref()).unwrap();

assert_eq!(report.message_type, 7);
assert_eq!(report.repeat_indicator, 0);
assert_eq!(report.mmsi, 2655651);
assert_eq!(report.acks.len(), 1);
let ack = &report.acks[0];
assert_eq!(ack.mmsi, 265538450);
assert_eq!(ack.seq_num, 0);

// Ensure other MMSIs are not present
assert!(report.acks.get(1).is_none());
assert!(report.acks.get(2).is_none());
assert!(report.acks.get(3).is_none());
}
}
16 changes: 4 additions & 12 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub mod static_data_report;
pub mod types;
pub mod utc_date_inquiry;
pub mod utc_date_response;
pub mod standard_aircraft_position_report;

pub mod binary_acknowledge;
pub use parsers::message_type;

#[cfg(feature = "alloc")]
Expand All @@ -50,14 +49,7 @@ pub enum AisMessage {
AidToNavigationReport(aid_to_navigation_report::AidToNavigationReport),
StaticDataReport(static_data_report::StaticDataReport),
UtcDateResponse(utc_date_response::UtcDateResponse),
UtcDateInquiry(utc_date_inquiry::UtcDateInquiry),
AddressedSafetyRelatedMessage(addressed_safety_related::AddressedSafetyRelatedMessage),
SafetyRelatedBroadcastMessage(safety_related_broadcast::SafetyRelatedBroadcastMessage),
SafetyRelatedAcknowledgment(safety_related_acknowledgment::SafetyRelatedAcknowledge),
LongRangeAisBroadcastMessage(long_range_ais_broadcast::LongRangeAisBroadcastMessage),
BinaryAddressedMessage(binary_addressed::BinaryAddressedMessage),
AssignmentModeCommand(assignment_mode_command::AssignmentModeCommand),
StandardAircraftPositionReport(standard_aircraft_position_report::SARPositionReport),
BinaryAcknowledgeMessage(binary_acknowledge::BinaryAcknowledge),
}

/// Trait that describes specific types of AIS messages
Expand All @@ -84,8 +76,8 @@ pub fn parse(unarmored: &[u8]) -> Result<AisMessage> {
5 => Ok(AisMessage::StaticAndVoyageRelatedData(
static_and_voyage_related_data::StaticAndVoyageRelatedData::parse(unarmored)?,
)),
6 => Ok(AisMessage::BinaryAddressedMessage(
binary_addressed::BinaryAddressedMessage::parse(unarmored)?,
7 => Ok(AisMessage::BinaryAcknowledgeMessage(
binary_acknowledge::BinaryAcknowledge::parse(unarmored)?,
)),
8 => Ok(AisMessage::BinaryBroadcastMessage(
binary_broadcast_message::BinaryBroadcastMessage::parse(unarmored)?,
Expand Down

0 comments on commit a14522b

Please sign in to comment.