Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/rust/cycle-benchmark/src/activities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
********************************************************************************/

use feo::activity::Activity;
use feo::error::ActivityError;
use feo::ids::ActivityId;
use feo_tracing::{instrument, tracing};

Expand Down Expand Up @@ -42,10 +43,13 @@ impl Activity for DummyActivity {
fn startup(&mut self) {}

#[instrument(name = "Activity step")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
tracing::event!(tracing::Level::TRACE, id = self._id_str);
Ok(())
}

#[instrument(name = "Activity shutdown")]
fn shutdown(&mut self) {}
fn shutdown(&mut self) -> Result<(), ActivityError> {
Ok(())
}
}
11 changes: 7 additions & 4 deletions examples/rust/cycle-benchmark/src/composites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use crate::activities::DummyActivity;
use crate::config::ActivityDependencies;
use feo::activity::{Activity, ActivityBuilder, ActivityIdAndBuilder};
use feo::error::ActivityError;
use feo::ids::{ActivityId, WorkerId};
use feo_tracing::{instrument, tracing};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -55,18 +56,20 @@ impl Activity for CompositeActivity {
}

#[instrument(name = "Composite step")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
tracing::event!(tracing::Level::TRACE, id = self._activity_id_str);
for activity in &mut self.activities {
activity.step();
activity.step()?;
}
Ok(())
}

#[instrument(name = "Composite shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
for activity in &mut self.activities {
activity.shutdown();
activity.shutdown()?;
}
Ok(())
}
}

Expand Down
44 changes: 29 additions & 15 deletions examples/rust/mini-adas/src/activities/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use core::mem::MaybeUninit;
use core::ops::{Deref, DerefMut, Range};
use core::time::Duration;
use feo::activity::Activity;
use feo::error::ActivityError;
use feo::ids::ActivityId;
use feo_com::interface::{ActivityInput, ActivityOutput};
#[cfg(feature = "com_iox2")]
Expand All @@ -28,7 +29,6 @@ use feo_log::debug;
use feo_tracing::instrument;
use std::hash::RandomState;
use std::thread;

const SLEEP_RANGE: Range<i64> = 10..45;

/// Camera activity
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Activity for Camera {
fn startup(&mut self) {}

#[instrument(name = "Camera")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping Camera");
sleep_random();

Expand All @@ -95,11 +95,13 @@ impl Activity for Camera {
let camera = camera.write_payload(image);
camera.send().unwrap();
}
Ok(())
}

#[instrument(name = "Camera shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!("Shutting down Camera activity {}", self.activity_id);
Ok(())
}
}

Expand Down Expand Up @@ -150,7 +152,7 @@ impl Activity for Radar {
fn startup(&mut self) {}

#[instrument(name = "Radar")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping Radar");
sleep_random();

Expand All @@ -160,11 +162,13 @@ impl Activity for Radar {
let radar = radar.write_payload(scan);
radar.send().unwrap();
}
Ok(())
}

#[instrument(name = "Radar shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!("Shutting down Radar activity {}", self.activity_id);
Ok(())
}
}

Expand Down Expand Up @@ -235,7 +239,7 @@ impl Activity for NeuralNet {
fn startup(&mut self) {}

#[instrument(name = "NeuralNet")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping NeuralNet");
sleep_random();

Expand All @@ -252,11 +256,13 @@ impl Activity for NeuralNet {
debug!("Sending Scene {:?}", scene.deref());
scene.send().unwrap();
}
Ok(())
}

#[instrument(name = "NeuralNet shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!("Shutting down NeuralNet activity {}", self.activity_id);
Ok(())
}
}

Expand Down Expand Up @@ -299,7 +305,7 @@ impl Activity for EmergencyBraking {
fn startup(&mut self) {}

#[instrument(name = "EmergencyBraking")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping EmergencyBraking");
sleep_random();

Expand Down Expand Up @@ -331,14 +337,16 @@ impl Activity for EmergencyBraking {
brake_instruction.send().unwrap();
}
}
Ok(())
}

#[instrument(name = "EmergencyBraking shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!(
"Shutting down EmergencyBraking activity {}",
self.activity_id
);
Ok(())
}
}

Expand Down Expand Up @@ -374,7 +382,7 @@ impl Activity for BrakeController {
fn startup(&mut self) {}

#[instrument(name = "BrakeController")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping BrakeController");
sleep_random();

Expand All @@ -386,14 +394,16 @@ impl Activity for BrakeController {
)
}
}
Ok(())
}

#[instrument(name = "BrakeController shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!(
"Shutting down BrakeController activity {}",
self.activity_id
);
Ok(())
}
}

Expand Down Expand Up @@ -428,21 +438,23 @@ impl Activity for EnvironmentRenderer {
fn startup(&mut self) {}

#[instrument(name = "EnvironmentRenderer")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping EnvironmentRenderer");
sleep_random();

if let Ok(_scene) = self.input_scene.read() {
debug!("Rendering scene");
}
Ok(())
}

#[instrument(name = "EnvironmentRenderer shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!(
"Shutting down EnvironmentRenderer activity {}",
self.activity_id
);
Ok(())
}
}

Expand Down Expand Up @@ -478,7 +490,7 @@ impl Activity for SteeringController {
fn startup(&mut self) {}

#[instrument(name = "SteeringController")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
debug!("Stepping SteeringController");
sleep_random();

Expand All @@ -488,14 +500,16 @@ impl Activity for SteeringController {
steering.angle
)
}
Ok(())
}

#[instrument(name = "SteeringController shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
debug!(
"Shutting down SteeringController activity {}",
self.activity_id
);
Ok(())
}
}

Expand Down
5 changes: 3 additions & 2 deletions feo/src/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
********************************************************************************/

//! Activity and related structs and traits
use crate::error::ActivityError;
use crate::ids::ActivityId;
use alloc::boxed::Box;

Expand All @@ -24,10 +25,10 @@ pub trait Activity {
fn startup(&mut self);

/// Called upon each step
fn step(&mut self);
fn step(&mut self) -> Result<(), ActivityError>;

/// Called upon shutdown
fn shutdown(&mut self);
fn shutdown(&mut self) -> Result<(), ActivityError>;
}

/// Activity Builder trait.
Expand Down
9 changes: 6 additions & 3 deletions feo/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

//! Provides a convenience macro creating ffi definitions and Rust glue code for C++ components

/// Create ffi definitions and Rust glue code for a C++ component
/// Create ffi definitions and Rust glue code for a C++ component
///
/// Call: `cpp_activity!(name, library)`
///
Expand All @@ -34,6 +34,7 @@ macro_rules! cpp_activity {
( $name:ident, $library:literal ) => {
pub mod $name {
use feo::activity::Activity;
use feo::error::ActivityError;
use feo::ids::ActivityId;
use feo_cpp_macros::{make_fn, make_fn_call};
use feo_tracing::{instrument, tracing};
Expand Down Expand Up @@ -91,15 +92,17 @@ macro_rules! cpp_activity {
}

#[instrument(name = "step")]
fn step(&mut self) {
fn step(&mut self) -> Result<(), ActivityError> {
// Safety: Call of external C functions belonging to C++ activitiy, to be reviewed
unsafe { make_fn_call!($name, _step, (self.cpp_activity)) };
Ok(())
}

#[instrument(name = "shutdown")]
fn shutdown(&mut self) {
fn shutdown(&mut self) -> Result<(), ActivityError> {
// Safety: Call of external C functions belonging to C++ activitiy, to be reviewed
unsafe { make_fn_call!($name, _shutdown, (self.cpp_activity)) };
Ok(())
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions feo/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
use crate::ids::{ActivityId, ChannelId, WorkerId};
use crate::signalling::common::signals::Signal;
use core::time::Duration;
#[cfg(feature = "recording")]
use postcard::experimental::max_size::MaxSize;
#[cfg(feature = "recording")]
use serde::Deserialize;
#[cfg(feature = "recording")]
use serde::Serialize;

/// FEO Error type
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
ActivityFailed(ActivityId, ActivityError),
ActivityNotFound(ActivityId),
Channel(&'static str),
ChannelClosed,
Expand All @@ -32,11 +39,24 @@ pub enum Error {
WorkerNotFound(WorkerId),
}

/// Defines the types of failures an Activity can report.
#[cfg_attr(feature = "recording", derive(Serialize, Deserialize, MaxSize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ActivityError {
/// A failure during a regular `step()` execution.
Step,
/// A failure during the `shutdown()` method.
Shutdown,
}

impl core::error::Error for Error {}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
Error::ActivityFailed(id, err) => {
write!(f, "activity {id} reported a failure: {err:?}")
}
Error::ActivityNotFound(id) => write!(f, "failed to find activity with ID {id}"),
Error::Channel(description) => write!(f, "channel error: {description}"),
Error::ChannelClosed => write!(f, "channel closed by peer"),
Expand Down
Loading
Loading