PUS Event Generation #17

Merged
muellerr merged 13 commits from event-pus-generation into main 2022-10-22 17:34:00 +02:00
2 changed files with 213 additions and 62 deletions
Showing only changes of commit 1eee8da4de - Show all commits

View File

@ -5,15 +5,33 @@ use spacepackets::tm::PusTmSecondaryHeader;
use spacepackets::{SpHeader, MAX_APID}; use spacepackets::{SpHeader, MAX_APID};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
pub use allocvec::EventReporterWithVec; pub use allocvec::EventReporter;
pub struct EventReporter { #[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Subservices {
TmInfoReport = 1,
TmLowSeverityReport = 2,
TmMediumSeverityReport = 3,
TmHighSeverityReport = 4,
TcEnableEventGeneration = 5,
TcDisableEventGeneration = 6,
TcReportDisabledList = 7,
TmDisabledEventsReport = 8,
}
impl From<Subservices> for u8 {
fn from(enumeration: Subservices) -> Self {
enumeration as u8
}
}
pub struct EventReporterBase {
msg_count: u16, msg_count: u16,
apid: u16, apid: u16,
pub dest_id: u16, pub dest_id: u16,
} }
impl EventReporter { impl EventReporterBase {
pub fn new(apid: u16) -> Option<Self> { pub fn new(apid: u16) -> Option<Self> {
if apid > MAX_APID { if apid > MAX_APID {
return None; return None;
@ -33,13 +51,106 @@ impl EventReporter {
event_id: impl EcssEnumeration, event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>, aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> { ) -> Result<(), EcssTmError<E>> {
self.generate_and_send_generic_tm(
buf,
Subservices::TmInfoReport,
sender,
time_stamp,
event_id,
aux_data,
)
}
pub fn event_low_severity<E>(
&mut self,
buf: &mut [u8],
sender: &mut (impl EcssTmSender<E> + ?Sized),
time_stamp: &[u8],
event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> {
self.generate_and_send_generic_tm(
buf,
Subservices::TmLowSeverityReport,
sender,
time_stamp,
event_id,
aux_data,
)
}
pub fn event_medium_severity<E>(
&mut self,
buf: &mut [u8],
sender: &mut (impl EcssTmSender<E> + ?Sized),
time_stamp: &[u8],
event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> {
self.generate_and_send_generic_tm(
buf,
Subservices::TmMediumSeverityReport,
sender,
time_stamp,
event_id,
aux_data,
)
}
pub fn event_high_severity<E>(
&mut self,
buf: &mut [u8],
sender: &mut (impl EcssTmSender<E> + ?Sized),
time_stamp: &[u8],
event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> {
self.generate_and_send_generic_tm(
buf,
Subservices::TmHighSeverityReport,
sender,
time_stamp,
event_id,
aux_data,
)
}
fn generate_and_send_generic_tm<E>(
&mut self,
buf: &mut [u8],
subservice: Subservices,
sender: &mut (impl EcssTmSender<E> + ?Sized),
time_stamp: &[u8],
event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> {
let tm = self.generate_generic_event_tm(buf, subservice, time_stamp, event_id, aux_data)?;
sender.send_tm(tm)?;
self.msg_count += 1;
Ok(())
}
fn generate_generic_event_tm<'a, E>(
&'a self,
buf: &'a mut [u8],
subservice: Subservices,
time_stamp: &'a [u8],
event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>,
) -> Result<PusTm, EcssTmError<E>> {
let mut src_data_len = event_id.byte_width(); let mut src_data_len = event_id.byte_width();
if let Some(aux_data) = aux_data { if let Some(aux_data) = aux_data {
src_data_len += aux_data.len(); src_data_len += aux_data.len();
} }
source_buffer_large_enough(buf.len(), src_data_len)?; source_buffer_large_enough(buf.len(), src_data_len)?;
let mut sp_header = SpHeader::tm(self.apid, 0, 0).unwrap(); let mut sp_header = SpHeader::tm(self.apid, 0, 0).unwrap();
let sec_header = PusTmSecondaryHeader::new(5, 0, self.msg_count, self.dest_id, time_stamp); let sec_header = PusTmSecondaryHeader::new(
5,
subservice.into(),
self.msg_count,
self.dest_id,
time_stamp,
);
let mut current_idx = 0; let mut current_idx = 0;
event_id.write_to_bytes(&mut buf[0..event_id.byte_width()])?; event_id.write_to_bytes(&mut buf[0..event_id.byte_width()])?;
current_idx += event_id.byte_width(); current_idx += event_id.byte_width();
@ -47,10 +158,12 @@ impl EventReporter {
buf[current_idx..current_idx + aux_data.len()].copy_from_slice(aux_data); buf[current_idx..current_idx + aux_data.len()].copy_from_slice(aux_data);
current_idx += aux_data.len(); current_idx += aux_data.len();
} }
let tm = PusTm::new(&mut sp_header, sec_header, Some(&buf[0..current_idx]), true); Ok(PusTm::new(
sender.send_tm(tm)?; &mut sp_header,
self.msg_count += 1; sec_header,
Ok(()) Some(&buf[0..current_idx]),
true,
))
} }
} }
@ -60,14 +173,14 @@ mod allocvec {
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
pub struct EventReporterWithVec { pub struct EventReporter {
source_data_buf: Vec<u8>, source_data_buf: Vec<u8>,
pub reporter: EventReporter, pub reporter: EventReporterBase,
} }
impl EventReporterWithVec { impl EventReporter {
pub fn new(apid: u16, max_event_id_and_aux_data: usize) -> Option<Self> { pub fn new(apid: u16, max_event_id_and_aux_data: usize) -> Option<Self> {
let reporter = EventReporter::new(apid)?; let reporter = EventReporterBase::new(apid)?;
Some(Self { Some(Self {
source_data_buf: vec![0; max_event_id_and_aux_data], source_data_buf: vec![0; max_event_id_and_aux_data],
reporter, reporter,
@ -88,28 +201,53 @@ mod allocvec {
aux_data, aux_data,
) )
} }
// pub fn event_low_severity<E>(
// &mut self, pub fn event_low_severity<E>(
// _sender: &mut (impl EcssTmSender<E> + ?Sized), &mut self,
// _event_id: impl EcssEnumeration, sender: &mut (impl EcssTmSender<E> + ?Sized),
// _aux_data: Option<&[u8]>, time_stamp: &[u8],
// ) { event_id: impl EcssEnumeration,
// } aux_data: Option<&[u8]>,
// ) -> Result<(), EcssTmError<E>> {
// pub fn event_medium_severity<E>( self.reporter.event_low_severity(
// &mut self, self.source_data_buf.as_mut_slice(),
// _sender: &mut (impl EcssTmSender<E> + ?Sized), sender,
// _event_id: impl EcssEnumeration, time_stamp,
// _aux_data: Option<&[u8]>, event_id,
// ) { aux_data,
// } )
// }
// pub fn event_high_severity<E>(
// &mut self, pub fn event_medium_severity<E>(
// _sender: &mut (impl EcssTmSender<E> + ?Sized), &mut self,
// _event_id: impl EcssEnumeration, sender: &mut (impl EcssTmSender<E> + ?Sized),
// _aux_data: Option<&[u8]>, time_stamp: &[u8],
// ) { event_id: impl EcssEnumeration,
// } aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> {
self.reporter.event_medium_severity(
self.source_data_buf.as_mut_slice(),
sender,
time_stamp,
event_id,
aux_data,
)
}
pub fn event_high_severity<E>(
&mut self,
sender: &mut (impl EcssTmSender<E> + ?Sized),
time_stamp: &[u8],
event_id: impl EcssEnumeration,
aux_data: Option<&[u8]>,
) -> Result<(), EcssTmError<E>> {
self.reporter.event_high_severity(
self.source_data_buf.as_mut_slice(),
sender,
time_stamp,
event_id,
aux_data,
)
}
} }
} }

View File

@ -92,6 +92,24 @@ pub use stdmod::{
StdVerifReporterWithSender, StdVerifSenderError, StdVerifReporterWithSender, StdVerifSenderError,
}; };
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Subservices {
TmAcceptanceSuccess = 1,
TmAcceptanceFailure = 2,
TmStartSuccess = 3,
TmStartFailure = 4,
TmStepSuccess = 5,
TmStepFailure = 6,
TmCompletionSuccess = 7,
TmCompletionFailure = 8,
}
impl From<Subservices> for u8 {
fn from(enumeration: Subservices) -> Self {
enumeration as u8
}
}
/// This is a request identifier as specified in 5.4.11.2 c. of the PUS standard /// This is a request identifier as specified in 5.4.11.2 c. of the PUS standard
/// This field equivalent to the first two bytes of the CCSDS space packet header. /// This field equivalent to the first two bytes of the CCSDS space packet header.
#[derive(Debug, Eq, Copy, Clone)] #[derive(Debug, Eq, Copy, Clone)]
@ -294,8 +312,7 @@ impl VerificationReporterBasic {
let tm = self let tm = self
.create_pus_verif_success_tm( .create_pus_verif_success_tm(
buf, buf,
1, Subservices::TmAcceptanceSuccess.into(),
1,
&token.req_id, &token.req_id,
time_stamp, time_stamp,
None::<&dyn EcssEnumeration>, None::<&dyn EcssEnumeration>,
@ -322,8 +339,7 @@ impl VerificationReporterBasic {
let tm = self let tm = self
.create_pus_verif_fail_tm( .create_pus_verif_fail_tm(
buf, buf,
1, Subservices::TmAcceptanceFailure.into(),
2,
&token.req_id, &token.req_id,
None::<&dyn EcssEnumeration>, None::<&dyn EcssEnumeration>,
&params, &params,
@ -349,8 +365,7 @@ impl VerificationReporterBasic {
let tm = self let tm = self
.create_pus_verif_success_tm( .create_pus_verif_success_tm(
buf, buf,
1, Subservices::TmStartSuccess.into(),
3,
&token.req_id, &token.req_id,
time_stamp, time_stamp,
None::<&dyn EcssEnumeration>, None::<&dyn EcssEnumeration>,
@ -380,8 +395,7 @@ impl VerificationReporterBasic {
let tm = self let tm = self
.create_pus_verif_fail_tm( .create_pus_verif_fail_tm(
buf, buf,
1, Subservices::TmStartFailure.into(),
4,
&token.req_id, &token.req_id,
None::<&dyn EcssEnumeration>, None::<&dyn EcssEnumeration>,
&params, &params,
@ -405,8 +419,13 @@ impl VerificationReporterBasic {
time_stamp: &[u8], time_stamp: &[u8],
step: impl EcssEnumeration, step: impl EcssEnumeration,
) -> Result<(), EcssTmError<E>> { ) -> Result<(), EcssTmError<E>> {
let tm = let tm = self.create_pus_verif_success_tm(
self.create_pus_verif_success_tm(buf, 1, 5, &token.req_id, time_stamp, Some(&step))?; buf,
Subservices::TmStepSuccess.into(),
&token.req_id,
time_stamp,
Some(&step),
)?;
sender.send_tm(tm)?; sender.send_tm(tm)?;
self.msg_count += 1; self.msg_count += 1;
Ok(()) Ok(())
@ -424,7 +443,13 @@ impl VerificationReporterBasic {
params: FailParamsWithStep, params: FailParamsWithStep,
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> { ) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
let tm = self let tm = self
.create_pus_verif_fail_tm(buf, 1, 6, &token.req_id, Some(params.step), &params.bp) .create_pus_verif_fail_tm(
buf,
Subservices::TmStepFailure.into(),
&token.req_id,
Some(params.step),
&params.bp,
)
.map_err(|e| VerificationErrorWithToken(e, token))?; .map_err(|e| VerificationErrorWithToken(e, token))?;
sender sender
.send_tm(tm) .send_tm(tm)
@ -447,8 +472,7 @@ impl VerificationReporterBasic {
let tm = self let tm = self
.create_pus_verif_success_tm( .create_pus_verif_success_tm(
buf, buf,
1, Subservices::TmCompletionSuccess.into(),
7,
&token.req_id, &token.req_id,
time_stamp, time_stamp,
None::<&dyn EcssEnumeration>, None::<&dyn EcssEnumeration>,
@ -475,8 +499,7 @@ impl VerificationReporterBasic {
let tm = self let tm = self
.create_pus_verif_fail_tm( .create_pus_verif_fail_tm(
buf, buf,
1, Subservices::TmCompletionFailure.into(),
8,
&token.req_id, &token.req_id,
None::<&dyn EcssEnumeration>, None::<&dyn EcssEnumeration>,
&params, &params,
@ -492,7 +515,6 @@ impl VerificationReporterBasic {
fn create_pus_verif_success_tm<'a, E>( fn create_pus_verif_success_tm<'a, E>(
&'a mut self, &'a mut self,
buf: &'a mut [u8], buf: &'a mut [u8],
service: u8,
subservice: u8, subservice: u8,
req_id: &RequestId, req_id: &RequestId,
time_stamp: &'a [u8], time_stamp: &'a [u8],
@ -514,7 +536,6 @@ impl VerificationReporterBasic {
let mut sp_header = SpHeader::tm(self.apid(), 0, 0).unwrap(); let mut sp_header = SpHeader::tm(self.apid(), 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base( Ok(self.create_pus_verif_tm_base(
buf, buf,
service,
subservice, subservice,
&mut sp_header, &mut sp_header,
time_stamp, time_stamp,
@ -525,7 +546,6 @@ impl VerificationReporterBasic {
fn create_pus_verif_fail_tm<'a, E>( fn create_pus_verif_fail_tm<'a, E>(
&'a mut self, &'a mut self,
buf: &'a mut [u8], buf: &'a mut [u8],
service: u8,
subservice: u8, subservice: u8,
req_id: &RequestId, req_id: &RequestId,
step: Option<&(impl EcssEnumeration + ?Sized)>, step: Option<&(impl EcssEnumeration + ?Sized)>,
@ -559,7 +579,6 @@ impl VerificationReporterBasic {
let mut sp_header = SpHeader::tm(self.apid(), 0, 0).unwrap(); let mut sp_header = SpHeader::tm(self.apid(), 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base( Ok(self.create_pus_verif_tm_base(
buf, buf,
service,
subservice, subservice,
&mut sp_header, &mut sp_header,
params.time_stamp, params.time_stamp,
@ -570,19 +589,13 @@ impl VerificationReporterBasic {
fn create_pus_verif_tm_base<'a>( fn create_pus_verif_tm_base<'a>(
&'a mut self, &'a mut self,
buf: &'a mut [u8], buf: &'a mut [u8],
service: u8,
subservice: u8, subservice: u8,
sp_header: &mut SpHeader, sp_header: &mut SpHeader,
time_stamp: &'a [u8], time_stamp: &'a [u8],
source_data_len: usize, source_data_len: usize,
) -> PusTm { ) -> PusTm {
let tm_sec_header = PusTmSecondaryHeader::new( let tm_sec_header =
service, PusTmSecondaryHeader::new(1, subservice, self.msg_count, self.dest_id, time_stamp);
subservice,
self.msg_count,
self.dest_id,
time_stamp,
);
PusTm::new( PusTm::new(
sp_header, sp_header,
tm_sec_header, tm_sec_header,