Implementation:78 Xiaozhi esp32 DeviceStateMachine
| Knowledge Sources | |
|---|---|
| Domains | State_Management, Embedded_Systems |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for device state transition validation provided by the XiaoZhi firmware, implementing a finite state machine with 12 states, a validated transition table, and an observer pattern for state change notifications.
Description
DeviceStateMachine manages the runtime state of the voice assistant. It uses an std::atomic<DeviceState> for the current state, ensuring thread-safe reads without locking. State transitions are validated against a static transition table; invalid transitions are rejected.
The state machine implements the observer pattern: listeners registered via AddStateChangeListener() are notified synchronously on each successful transition. The Application registers a listener that sets MAIN_EVENT_STATE_CHANGED, deferring heavy side effects (display/audio/power updates) to the main event loop.
Usage
Reference this implementation when:
- Understanding which state transitions are legal
- Adding a new device state
- Registering a state change listener
- Debugging state transition failures
Code Reference
Source Location
- Repository: XiaoZhi ESP32
- Files:
main/device_state.h,main/device_state_machine.h,main/device_state_machine.cc
Signature
// device_state.h
enum DeviceState {
kDeviceStateUnknown,
kDeviceStateStarting,
kDeviceStateWifiConfiguring,
kDeviceStateIdle,
kDeviceStateConnecting,
kDeviceStateListening,
kDeviceStateSpeaking,
kDeviceStateNotifying,
kDeviceStateUpgrading,
kDeviceStateActivating,
kDeviceStateAudioTesting,
kDeviceStateFatalError
};
// device_state_machine.h
class DeviceStateMachine {
public:
DeviceStateMachine();
DeviceState GetState() const;
bool TransitionTo(DeviceState new_state);
bool CanTransitionTo(DeviceState target) const;
using StateCallback = std::function<void(DeviceState, DeviceState)>;
int AddStateChangeListener(StateCallback callback);
void RemoveStateChangeListener(int listener_id);
static const char* GetStateName(DeviceState state);
private:
std::atomic<DeviceState> current_state_{kDeviceStateUnknown};
std::vector<std::pair<int, StateCallback>> listeners_;
int next_listener_id_{0};
std::mutex mutex_;
bool IsValidTransition(DeviceState from, DeviceState to) const;
void NotifyStateChange(DeviceState old_state, DeviceState new_state);
};
Import
#include "device_state.h"
#include "device_state_machine.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| new_state | DeviceState | Target state for transition | |
| callback | StateCallback | Function called with (old_state, new_state) on transition |
Outputs
| Name | Type | Description |
|---|---|---|
| TransitionTo() | True if transition was valid and applied | |
| CanTransitionTo() | True if transition is valid from current state | |
| GetState() | Current state (atomic read, lock-free) | |
| GetStateName() | Human-readable state name for logging |
Usage Examples
Transitioning States
// In Application — all transitions go through SetDeviceState
bool Application::SetDeviceState(DeviceState state) {
return state_machine_.TransitionTo(state);
}
// Example: idle → connecting (valid)
SetDeviceState(kDeviceStateConnecting);
// Example: idle → speaking (invalid, returns false)
SetDeviceState(kDeviceStateSpeaking); // rejected
Registering a State Change Listener
// In Application::Initialize()
state_machine_.AddStateChangeListener(
[this](DeviceState old_state, DeviceState new_state) {
// Runs in the context of TransitionTo() caller
// Signal main loop to handle side effects
xEventGroupSetBits(event_group_, MAIN_EVENT_STATE_CHANGED);
}
);
// In Run() event loop, on MAIN_EVENT_STATE_CHANGED:
HandleStateChangedEvent();
// → updates display, LED, audio, power based on GetDeviceState()
Checking Valid Transitions
// Pre-check before attempting a transition
if (state_machine_.CanTransitionTo(kDeviceStateIdle)) {
state_machine_.TransitionTo(kDeviceStateIdle);
}