Principle:78 Xiaozhi esp32 Device State Machine
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, Embedded_Systems, State_Management |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
A finite state machine (FSM) that governs the runtime behavior of an embedded voice assistant by validating all state transitions against a predefined transition table, ensuring the device never enters an illegal combination of audio, network, and UI states.
Description
The device state machine is a deterministic finite automaton with 12 states representing the device's operational modes. Every state transition is validated against a static transition table; invalid transitions are rejected and logged. This prevents race conditions where, for example, the device might attempt to play TTS audio while upgrading firmware.
The 12 device states:
kDeviceStateUnknown— initial/unset statekDeviceStateStarting— boot and initializationkDeviceStateWifiConfiguring— WiFi provisioning modekDeviceStateIdle— standby, wake word activekDeviceStateConnecting— opening audio channel to serverkDeviceStateListening— capturing user speechkDeviceStateSpeaking— playing TTS responsekDeviceStateNotifying— playing notification audiokDeviceStateUpgrading— OTA firmware update in progresskDeviceStateActivating— server activationkDeviceStateAudioTesting— audio hardware test modekDeviceStateFatalError— unrecoverable error
The state machine uses the observer pattern: registered listeners (AddStateChangeListener) are notified on each successful transition. The main application registers a listener that sets MAIN_EVENT_STATE_CHANGED, causing the event loop to call HandleStateChangedEvent(), which updates display, LED, audio, and power settings based on the new state.
Usage
Use this principle when:
- A device has distinct operational modes with strict ordering requirements
- Multiple asynchronous events could cause conflicting state changes
- UI, audio, and network behavior must be coordinated based on a single source of truth
- Invalid state combinations must be prevented (e.g., listening while upgrading)
Theoretical Basis
The FSM is defined as a 5-tuple (Q, Σ, δ, q₀, F):
Q = {Unknown, Starting, WifiConfiguring, Idle, Connecting,
Listening, Speaking, Notifying, Upgrading, Activating,
AudioTesting, FatalError}
δ = transition function (validated table)
q₀ = Unknown
Key transitions:
Starting → Idle, WifiConfiguring, Activating
Idle → Connecting, Notifying, Upgrading, AudioTesting
Connecting → Listening, Idle
Listening → Speaking, Idle
Speaking → Listening, Idle, Notifying
Notifying → Idle
Upgrading → FatalError (on failure → reboot)
The transition validation is a simple lookup table:
bool IsValidTransition(DeviceState from, DeviceState to) const {
// Returns true only if (from, to) is in the allowed transition set
}
The observer pattern decouples state changes from side effects:
TransitionTo(target):
if IsValidTransition(current, target):
old = current
current = target
for each listener:
listener(old, target) // notified in caller's context
return true
return false
Practical Guide
When implementing a device state machine:
- Route all transitions through one method:
Application::SetDeviceState()delegates toTransitionTo(), ensuring validation. - Handle side effects in a single handler:
HandleStateChangedEvent()updates display, LED, audio, and power based on the new state. - Use thread-safe state reads:
GetState()usesstd::atomicfor lock-free reads. - Notify listeners synchronously: callbacks run in the context of
TransitionTo(); use event bits to defer heavy work to the main loop.