template <typename StateMachine, typename State, typename Input>
class StateMachineBase
Defined at line 29 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
Scaffolding for implementing a state machine in the USB PD spec.
`StateMachine` is the class deriving from this base class.
`State` is the type representing a state. It should be a value type,
equivalent to a scoped enum whose underlying representation is a signed
integer. In particular, instances must be copyable (and the copy operation is
assumed to be cheap), must be convertible to int64_t, and the copies must be
safe to store for the state machine instance's entire lifetime.
`Input` is the type representing an `EnterState` input. It must be trivially
copyable.
Public Methods
void StateMachineBase<StateMachine, State, Input> (State initial_state, inspect::Node inspect_root, const char * debug_name)
Defined at line 31 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
void StateMachineBase<StateMachine, State, Input> (const StateMachineBase<StateMachine, State, Input> & )
Defined at line 36 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
StateMachineBase<StateMachine, State, Input> & operator= (const StateMachineBase<StateMachine, State, Input> & )
Defined at line 37 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
void ~StateMachineBase<StateMachine, State, Input> ()
Defined at line 39 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
State current_state ()
Defined at line 41 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
void Run (Input input)
Evaluates `input` against the state machine until it stops transitioning.
Defined at line 96 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
Protected Methods
void EnterState (State state)
Called on a transition into `state`.
Implementations must not change the current state, e.g. by calling
`ForceStateTransition()`.
State NextState (Input input, State current_state)
Decides if `input` received in `current_state` warrants a state change.
Returns the state machine's new state, which can be `current_state` if no
transition is warranted.
Implementations must not change the current state, e.g. via
`ForceStateTransition()`.
Implementation advice: "return current_state;" is a readable way to express
the lack of a state transition.
void ExitState (State state)
Called on a transition out of `state`.
Implementations must not change the current state, e.g. by calling
`ForceStateTransition()`.
const char * StateToString (State state)
Returns a developer-friendly description of `state` suitable for logging.
inspect::Node & inspect_root ()
Defined at line 86 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h
void ForceStateTransition (State new_state)
Subclass hook for changing the state outside of Run().
This method can be used to allow changing a state machine's state without
invoking the `Run()` / `NextState()` machinery. This can help implement
specifications that allow "out-of-band" state changes, without having to
burden `NextState()` with knowledge of these out-of-band state changes.
This method is not public so that subclasses can enforce boundaries on the
arbitrary state transitions. For example, a subclass may choose to only
allow transitioning to designated states, such as "disabled" or "ready".
Defined at line 123 of file ../../src/devices/power/drivers/fusb302/state-machine-base.h