Skip to main content

cu29_runtime/
simulation.rs

1//! # `cu29::simulation` Module
2//!
3//! The `cu29::simulation` module provides an interface to simulate tasks in Copper-based systems.
4//! It offers structures, traits, and enums that enable hooking into the lifecycle of tasks, adapting
5//! their behavior, and integrating them with simulated hardware environments.
6//!
7//! ## Overview
8//!
9//! This module is specifically designed to manage the lifecycle of tasks during simulation, allowing
10//! users to override specific simulation steps and simulate sensor data or hardware interaction using
11//! placeholders for real drivers. It includes the following components:
12//!
13//! - **`CuTaskCallbackState`**: Represents the lifecycle states of tasks during simulation.
14//! - **`SimOverride`**: Defines how the simulator should handle specific task callbacks, either
15//!   executing the logic in the simulator or deferring to the real implementation.
16//!
17//! ## Hooking Simulation Events
18//!
19//! You can control and simulate task behavior using a callback mechanism. A task in the Copper framework
20//! has a lifecycle, and for each stage of the lifecycle, a corresponding callback state is passed to
21//! the simulation. This allows you to inject custom logic for each task stage.
22//!
23//! ### `CuTaskCallbackState` Enum
24//!
25//! The `CuTaskCallbackState` enum represents different stages in the lifecycle of a Copper task during a simulation:
26//!
27//! - **`New(Option<ComponentConfig>)`**: Triggered when a task is created. Use this state to adapt the simulation
28//!   to a specific component configuration if needed.
29//! - **`Start`**: Triggered when a task starts. This state allows you to initialize or set up any necessary data
30//!   before the task processes any input.
31//! - **`Preprocess`**: Called before the main processing step. Useful for preparing or validating data.
32//! - **`Process(I, O)`**: The core processing state, where you can handle the input (`I`) and output (`O`) of
33//!   the task. For source tasks, `I` is `CuMsg<()>`, and for sink tasks, `O` is `CuMsg<()>`.
34//! - **`Postprocess`**: Called after the main processing step. Allows for cleanup or final adjustments.
35//! - **`Stop`**: Triggered when a task is stopped. Use this to finalize any data or state before task termination.
36//!
37//! ### Example Usage: Callback
38//!
39//! You can combine the expressiveness of the enum matching to intercept and override the task lifecycle for the simulation.
40//!
41//! ```rust,ignore
42//! let mut sim_callback = move |step: SimStep<'_>| -> SimOverride {
43//!     match step {
44//!         // Handle the creation of source tasks, potentially adapting the simulation based on configuration
45//!         SimStep::SourceTask(CuTaskCallbackState::New(Some(config))) => {
46//!             println!("Creating Source Task with configuration: {:?}", config);
47//!             // You can adapt the simulation using the configuration here
48//!             SimOverride::ExecuteByRuntime
49//!         }
50//!         SimStep::SourceTask(CuTaskCallbackState::New(None)) => {
51//!             println!("Creating Source Task without configuration.");
52//!             SimOverride::ExecuteByRuntime
53//!         }
54//!         // Handle the processing step for sink tasks, simulating the response
55//!         SimStep::SinkTask(CuTaskCallbackState::Process(input, output)) => {
56//!             println!("Processing Sink Task...");
57//!             println!("Received input: {:?}", input);
58//!
59//!             // Simulate a response by setting the output payload
60//!             output.set_payload(your_simulated_response());
61//!             println!("Set simulated output for Sink Task.");
62//!
63//!             SimOverride::ExecutedBySim
64//!         }
65//!         // Generic handling for other phases like Start, Preprocess, Postprocess, or Stop
66//!         SimStep::SourceTask(CuTaskCallbackState::Start)
67//!         | SimStep::SinkTask(CuTaskCallbackState::Start) => {
68//!             println!("Task started.");
69//!             SimOverride::ExecuteByRuntime
70//!         }
71//!         SimStep::SourceTask(CuTaskCallbackState::Stop)
72//!         | SimStep::SinkTask(CuTaskCallbackState::Stop) => {
73//!             println!("Task stopped.");
74//!             SimOverride::ExecuteByRuntime
75//!         }
76//!         // Default fallback for any unhandled cases
77//!         _ => {
78//!             println!("Unhandled simulation step: {:?}", step);
79//!             SimOverride::ExecuteByRuntime
80//!         }
81//!     }
82//! };
83//! ```
84//!
85//! In this example, `example_callback` is a function that matches against the current step in the simulation and
86//! determines if the simulation should handle it (`SimOverride::ExecutedBySim`) or defer to the runtime's real
87//! implementation (`SimOverride::ExecuteByRuntime`).
88//!
89//! ## Task Simulation with `CuSimSrcTask` and `CuSimSinkTask`
90//!
91//! The module provides placeholder tasks for source and sink tasks, which do not interact with real hardware but
92//! instead simulate the presence of it.
93//!
94//! - **`CuSimSrcTask<T>`**: A placeholder for a source task that simulates a sensor or data acquisition hardware.
95//!   This task provides the ability to simulate incoming data without requiring actual hardware initialization.
96//!
97//! - **`CuSimSinkTask<T>`**: A placeholder for a sink task that simulates sending data to hardware. It serves as a
98//!   mock for hardware actuators or output devices during simulations.
99//!
100//! ## Controlling Simulation Flow: `SimOverride` Enum
101//!
102//! The `SimOverride` enum is used to control how the simulator should proceed at each step. This allows
103//! for fine-grained control of task behavior in the simulation context:
104//!
105//! - **`ExecutedBySim`**: Indicates that the simulator has handled the task logic, and the real implementation
106//!   should be skipped.
107//! - **`ExecuteByRuntime`**: Indicates that the real implementation should proceed as normal.
108//!
109
110use crate::config::ComponentConfig;
111use crate::context::CuContext;
112use crate::copperlist::CopperList;
113use crate::cubridge::{
114    BridgeChannel, BridgeChannelConfig, BridgeChannelInfo, BridgeChannelSet, CuBridge,
115};
116use crate::cutask::CuMsgPack;
117
118use crate::cutask::{CuMsg, CuMsgPayload, CuSinkTask, CuSrcTask, Freezable};
119use crate::reflect::{Reflect, TypePath};
120use crate::{input_msg, output_msg};
121use bincode::de::Decoder;
122use bincode::enc::Encoder;
123use bincode::error::{DecodeError, EncodeError};
124use bincode::{Decode, Encode};
125use core::marker::PhantomData;
126use cu29_clock::CuTime;
127use cu29_traits::{CopperListTuple, CuResult, ErasedCuStampedDataSet};
128
129/// Returns the earliest recorded `process_time.start` found in a CopperList.
130///
131/// This is the default timestamp used by exact-output replay when no matching
132/// recorded keyframe is being injected for the current CL.
133pub fn recorded_copperlist_timestamp<P: CopperListTuple>(
134    copperlist: &CopperList<P>,
135) -> Option<CuTime> {
136    <CopperList<P> as ErasedCuStampedDataSet>::cumsgs(copperlist)
137        .into_iter()
138        .filter_map(|msg| Option::<CuTime>::from(msg.metadata().process_time().start))
139        .min()
140}
141
142/// This is the state that will be passed to the simulation support to hook
143/// into the lifecycle of the tasks.
144pub enum CuTaskCallbackState<I, O> {
145    /// Callbacked when a task is created.
146    /// It gives you the opportunity to adapt the sim to the given config.
147    New(Option<ComponentConfig>),
148    /// Callbacked when a task is started.
149    Start,
150    /// Callbacked when a task is getting called on pre-process.
151    Preprocess,
152    /// Callbacked when a task is getting called on process.
153    /// I and O are the input and output messages of the task.
154    /// if this is a source task, I will be CuMsg<()>
155    /// if this is a sink task, O will be CuMsg<()>
156    Process(I, O),
157    /// Callbacked when a task is getting called on post-process.
158    Postprocess,
159    /// Callbacked when a task is stopped.
160    Stop,
161}
162
163/// This is the answer the simulator can give to control the simulation flow.
164#[derive(PartialEq)]
165pub enum SimOverride {
166    /// The callback took care of the logic on the simulation side and the actual
167    /// implementation needs to be skipped.
168    ExecutedBySim,
169    /// The actual implementation needs to be executed.
170    ExecuteByRuntime,
171    /// Emulated the behavior of an erroring task (same as return Err(..) in the normal tasks methods).
172    Errored(String),
173}
174
175/// Lifecycle callbacks for bridges when running in simulation.
176///
177/// These mirror the CuBridge trait hooks so a simulator can choose to
178/// bypass the real implementation (e.g. to avoid opening hardware) or
179/// inject faults.
180pub enum CuBridgeLifecycleState {
181    /// The bridge is about to be constructed. Gives access to config.
182    New(Option<ComponentConfig>),
183    /// The bridge is starting.
184    Start,
185    /// Called before the I/O cycle.
186    Preprocess,
187    /// Called after the I/O cycle.
188    Postprocess,
189    /// The bridge is stopping.
190    Stop,
191}
192
193/// This is a placeholder task for a source task for the simulations.
194/// It basically does nothing in place of a real driver so it won't try to initialize any hardware.
195#[derive(Reflect)]
196#[reflect(no_field_bounds, from_reflect = false, type_path = false)]
197pub struct CuSimSrcTask<T> {
198    #[reflect(ignore)]
199    boo: PhantomData<fn() -> T>,
200    state: bool,
201}
202
203impl<T: 'static> TypePath for CuSimSrcTask<T> {
204    fn type_path() -> &'static str {
205        "cu29_runtime::simulation::CuSimSrcTask"
206    }
207
208    fn short_type_path() -> &'static str {
209        "CuSimSrcTask"
210    }
211
212    fn type_ident() -> Option<&'static str> {
213        Some("CuSimSrcTask")
214    }
215
216    fn crate_name() -> Option<&'static str> {
217        Some("cu29_runtime")
218    }
219
220    fn module_path() -> Option<&'static str> {
221        Some("simulation")
222    }
223}
224
225impl<T> Freezable for CuSimSrcTask<T> {
226    fn freeze<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
227        Encode::encode(&self.state, encoder)
228    }
229
230    fn thaw<D: Decoder>(&mut self, decoder: &mut D) -> Result<(), DecodeError> {
231        self.state = Decode::decode(decoder)?;
232        Ok(())
233    }
234}
235
236impl<T: CuMsgPayload + 'static> CuSrcTask for CuSimSrcTask<T> {
237    type Resources<'r> = ();
238    type Output<'m> = output_msg!(T);
239
240    fn new(_config: Option<&ComponentConfig>, _resources: Self::Resources<'_>) -> CuResult<Self>
241    where
242        Self: Sized,
243    {
244        // Default to true to mirror typical source initial state; deterministic across runs.
245        Ok(Self {
246            boo: PhantomData,
247            state: true,
248        })
249    }
250
251    fn process(&mut self, _ctx: &CuContext, _new_msg: &mut Self::Output<'_>) -> CuResult<()> {
252        unimplemented!(
253            "A placeholder for sim was called for a source, you need answer SimOverride to ExecutedBySim for the Process step."
254        )
255    }
256}
257
258impl<T> CuSimSrcTask<T> {
259    /// Placeholder hook for simulation-driven sources.
260    ///
261    /// In the sim placeholder we don't advance any internal state because the
262    /// simulator is responsible for providing deterministic outputs and state
263    /// snapshots are carried by the real task (when run_in_sim = true).
264    /// Keeping this as a no-op avoids baking any fake behavior into keyframes.
265    pub fn sim_tick(&mut self) {}
266}
267
268/// Helper to map a payload type (or tuple of payload types) to the corresponding `input_msg!` form.
269pub trait CuSimSinkInput {
270    type With<'m>: CuMsgPack
271    where
272        Self: 'm;
273}
274
275macro_rules! impl_sim_sink_input_tuple {
276    ($($name:ident),+) => {
277        impl<$($name: CuMsgPayload),+> CuSimSinkInput for ($($name,)+) {
278            type With<'m> = input_msg!('m, $($name),+) where Self: 'm;
279        }
280    };
281}
282
283macro_rules! impl_sim_sink_input_up_to {
284    ($first:ident $(, $rest:ident)* $(,)?) => {
285        impl_sim_sink_input_tuple!($first);
286        impl_sim_sink_input_up_to!(@accumulate ($first); $($rest),*);
287    };
288    (@accumulate ($($acc:ident),+);) => {};
289    (@accumulate ($($acc:ident),+); $next:ident $(, $rest:ident)*) => {
290        impl_sim_sink_input_tuple!($($acc),+, $next);
291        impl_sim_sink_input_up_to!(@accumulate ($($acc),+, $next); $($rest),*);
292    };
293}
294
295impl_sim_sink_input_up_to!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
296
297/// This is a placeholder task for a sink task for the simulations.
298/// It basically does nothing in place of a real driver so it won't try to initialize any hardware.
299#[derive(Reflect)]
300#[reflect(no_field_bounds, from_reflect = false, type_path = false)]
301pub struct CuSimSinkTask<I> {
302    #[reflect(ignore)]
303    boo: PhantomData<fn() -> I>,
304}
305
306impl<I: 'static> TypePath for CuSimSinkTask<I> {
307    fn type_path() -> &'static str {
308        "cu29_runtime::simulation::CuSimSinkTask"
309    }
310
311    fn short_type_path() -> &'static str {
312        "CuSimSinkTask"
313    }
314
315    fn type_ident() -> Option<&'static str> {
316        Some("CuSimSinkTask")
317    }
318
319    fn crate_name() -> Option<&'static str> {
320        Some("cu29_runtime")
321    }
322
323    fn module_path() -> Option<&'static str> {
324        Some("simulation")
325    }
326}
327
328impl<I> Freezable for CuSimSinkTask<I> {}
329
330impl<I: CuSimSinkInput + 'static> CuSinkTask for CuSimSinkTask<I> {
331    type Resources<'r> = ();
332    type Input<'m> = <I as CuSimSinkInput>::With<'m>;
333
334    fn new(_config: Option<&ComponentConfig>, _resources: Self::Resources<'_>) -> CuResult<Self>
335    where
336        Self: Sized,
337    {
338        Ok(Self { boo: PhantomData })
339    }
340
341    fn process(&mut self, _ctx: &CuContext, _input: &Self::Input<'_>) -> CuResult<()> {
342        unimplemented!(
343            "A placeholder for sim was called for a sink, you need answer SimOverride to ExecutedBySim for the Process step."
344        )
345    }
346}
347
348/// Empty channel-id enum used when a simulated bridge has no channel on one side.
349#[derive(Copy, Clone, Debug, Eq, PartialEq)]
350pub enum CuNoBridgeChannelId {}
351
352/// Empty channel set used when a simulated bridge has no channel on one side.
353pub struct CuNoBridgeChannels;
354
355impl BridgeChannelSet for CuNoBridgeChannels {
356    type Id = CuNoBridgeChannelId;
357
358    const STATIC_CHANNELS: &'static [&'static dyn BridgeChannelInfo<Self::Id>] = &[];
359}
360
361/// Placeholder bridge used in simulation when a bridge is configured with
362/// `run_in_sim: false`.
363///
364/// This bridge is parameterized directly by the Tx/Rx channel sets generated
365/// from configuration, so the original bridge type does not need to compile in
366/// simulation mode.
367#[derive(Reflect)]
368#[reflect(no_field_bounds, from_reflect = false, type_path = false)]
369pub struct CuSimBridge<Tx: BridgeChannelSet + 'static, Rx: BridgeChannelSet + 'static> {
370    #[reflect(ignore)]
371    boo: PhantomData<fn() -> (Tx, Rx)>,
372}
373
374impl<Tx: BridgeChannelSet + 'static, Rx: BridgeChannelSet + 'static> TypePath
375    for CuSimBridge<Tx, Rx>
376{
377    fn type_path() -> &'static str {
378        "cu29_runtime::simulation::CuSimBridge"
379    }
380
381    fn short_type_path() -> &'static str {
382        "CuSimBridge"
383    }
384
385    fn type_ident() -> Option<&'static str> {
386        Some("CuSimBridge")
387    }
388
389    fn crate_name() -> Option<&'static str> {
390        Some("cu29_runtime")
391    }
392
393    fn module_path() -> Option<&'static str> {
394        Some("simulation")
395    }
396}
397
398impl<Tx: BridgeChannelSet + 'static, Rx: BridgeChannelSet + 'static> Freezable
399    for CuSimBridge<Tx, Rx>
400{
401}
402
403impl<Tx: BridgeChannelSet + 'static, Rx: BridgeChannelSet + 'static> CuBridge
404    for CuSimBridge<Tx, Rx>
405{
406    type Tx = Tx;
407    type Rx = Rx;
408    type Resources<'r> = ();
409
410    fn new(
411        _config: Option<&ComponentConfig>,
412        _tx_channels: &[BridgeChannelConfig<<Self::Tx as BridgeChannelSet>::Id>],
413        _rx_channels: &[BridgeChannelConfig<<Self::Rx as BridgeChannelSet>::Id>],
414        _resources: Self::Resources<'_>,
415    ) -> CuResult<Self>
416    where
417        Self: Sized,
418    {
419        Ok(Self { boo: PhantomData })
420    }
421
422    fn send<'a, Payload>(
423        &mut self,
424        _ctx: &CuContext,
425        _channel: &'static BridgeChannel<<Self::Tx as BridgeChannelSet>::Id, Payload>,
426        _msg: &CuMsg<Payload>,
427    ) -> CuResult<()>
428    where
429        Payload: CuMsgPayload + 'a,
430    {
431        Ok(())
432    }
433
434    fn receive<'a, Payload>(
435        &mut self,
436        _ctx: &CuContext,
437        _channel: &'static BridgeChannel<<Self::Rx as BridgeChannelSet>::Id, Payload>,
438        _msg: &mut CuMsg<Payload>,
439    ) -> CuResult<()>
440    where
441        Payload: CuMsgPayload + 'a,
442    {
443        Ok(())
444    }
445}