Files
bolar_2/main/core/runtime_state.cc
2026-11-27 16:38:11 +08:00

217 lines
7.5 KiB
C++

#include "core/runtime_state.h"
namespace borla {
StateValidation ValidateRuntimeState(const RuntimeState& state)
{
if (state.realtime == RealtimeState::kOpen &&
state.connectivity != ConnectivityState::kOnline) {
return {false, "open realtime channel requires online connectivity"};
}
if (state.conversation != ConversationState::kIdle &&
state.lifecycle != LifecycleState::kRunning) {
return {false, "active conversation requires running lifecycle"};
}
if (state.conversation == ConversationState::kSpeaking &&
state.playback == PlaybackState::kIdle) {
return {false, "speaking requires an active playback session"};
}
if (state.playback != PlaybackState::kIdle &&
state.playback_session_id == 0 &&
!state.pending_soft_off_after_playback &&
!state.idle_check_prompt) {
return {false, "active playback requires a non-zero session id"};
}
if (state.local_prompt_active &&
state.playback == PlaybackState::kIdle) {
return {false, "active local prompt requires playback"};
}
if (state.idle_sleeping &&
(state.lifecycle != LifecycleState::kRunning ||
state.realtime != RealtimeState::kClosed ||
state.conversation != ConversationState::kIdle ||
state.call != CallState::kInactive ||
state.playback != PlaybackState::kIdle)) {
return {false, "idle sleep requires running offline media state"};
}
if (state.idle_check_prompt &&
(state.lifecycle != LifecycleState::kRunning ||
state.playback != PlaybackState::kDraining)) {
return {false, "idle check requires draining prompt playback"};
}
if (state.alert_ringing &&
state.lifecycle != LifecycleState::kRunning) {
return {false, "alert ringing requires running lifecycle"};
}
if (state.call != CallState::kInactive &&
state.lifecycle != LifecycleState::kRunning) {
return {false, "call requires running lifecycle"};
}
if (state.friend_mode &&
(state.lifecycle != LifecycleState::kRunning ||
state.realtime != RealtimeState::kClosed ||
state.conversation != ConversationState::kIdle ||
state.call != CallState::kInactive ||
state.playback != PlaybackState::kIdle ||
state.alert_ringing)) {
return {false, "friend mode requires exclusive idle media state"};
}
const bool exclusive_lifecycle =
state.lifecycle == LifecycleState::kProvisioning ||
state.lifecycle == LifecycleState::kUpgrading ||
state.lifecycle == LifecycleState::kShuttingDown ||
state.lifecycle == LifecycleState::kSoftOff ||
state.lifecycle == LifecycleState::kFaulted;
const bool shutdown_prompt_playing =
state.lifecycle == LifecycleState::kShuttingDown &&
state.pending_soft_off_after_playback &&
state.realtime == RealtimeState::kClosed &&
state.conversation == ConversationState::kIdle &&
state.call == CallState::kInactive &&
state.playback == PlaybackState::kDraining;
if (exclusive_lifecycle && !shutdown_prompt_playing &&
(state.realtime != RealtimeState::kClosed ||
state.conversation != ConversationState::kIdle ||
state.call != CallState::kInactive ||
state.playback != PlaybackState::kIdle)) {
return {false, "exclusive lifecycle requires inactive realtime and media state"};
}
if (state.pending_soft_off_after_playback &&
!shutdown_prompt_playing) {
return {false, "pending soft-off requires shutdown prompt playback"};
}
return {true, "ok"};
}
#define BORLA_ENUM_TO_STRING_CASE(value) \
case value: \
return #value
const char* ToString(LifecycleState state)
{
switch (state) {
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kCold);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kStarting);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kRunning);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kProvisioning);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kUpgrading);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kShuttingDown);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kSoftOff);
BORLA_ENUM_TO_STRING_CASE(LifecycleState::kFaulted);
}
return "LifecycleState::unknown";
}
const char* ToString(ConnectivityState state)
{
switch (state) {
BORLA_ENUM_TO_STRING_CASE(ConnectivityState::kOffline);
BORLA_ENUM_TO_STRING_CASE(ConnectivityState::kConnecting);
BORLA_ENUM_TO_STRING_CASE(ConnectivityState::kOnline);
}
return "ConnectivityState::unknown";
}
const char* ToString(RealtimeState state)
{
switch (state) {
BORLA_ENUM_TO_STRING_CASE(RealtimeState::kClosed);
BORLA_ENUM_TO_STRING_CASE(RealtimeState::kOpening);
BORLA_ENUM_TO_STRING_CASE(RealtimeState::kOpen);
}
return "RealtimeState::unknown";
}
const char* ToString(ConversationState state)
{
switch (state) {
BORLA_ENUM_TO_STRING_CASE(ConversationState::kIdle);
BORLA_ENUM_TO_STRING_CASE(ConversationState::kListening);
BORLA_ENUM_TO_STRING_CASE(ConversationState::kSpeaking);
}
return "ConversationState::unknown";
}
const char* ToString(CallState state)
{
switch (state) {
BORLA_ENUM_TO_STRING_CASE(CallState::kInactive);
BORLA_ENUM_TO_STRING_CASE(CallState::kRinging);
BORLA_ENUM_TO_STRING_CASE(CallState::kCalling);
BORLA_ENUM_TO_STRING_CASE(CallState::kActive);
}
return "CallState::unknown";
}
const char* ToString(PlaybackState state)
{
switch (state) {
BORLA_ENUM_TO_STRING_CASE(PlaybackState::kIdle);
BORLA_ENUM_TO_STRING_CASE(PlaybackState::kStreaming);
BORLA_ENUM_TO_STRING_CASE(PlaybackState::kDraining);
}
return "PlaybackState::unknown";
}
const char* ToString(AudioPolicy policy)
{
switch (policy) {
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kOff);
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kWakeWordOnly);
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kWakeWordDuplex);
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kConversationCapture);
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kBargeInDuplex);
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kCallDuplex);
BORLA_ENUM_TO_STRING_CASE(AudioPolicy::kPlaybackOnly);
}
return "AudioPolicy::unknown";
}
const char* ToString(ConversationInterruptSource source)
{
switch (source) {
BORLA_ENUM_TO_STRING_CASE(
ConversationInterruptSource::kUserInitiated);
BORLA_ENUM_TO_STRING_CASE(
ConversationInterruptSource::kWakeWordDetected);
}
return "ConversationInterruptSource::unknown";
}
const char* ToString(LedPattern pattern)
{
switch (pattern) {
BORLA_ENUM_TO_STRING_CASE(LedPattern::kOff);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kStarting);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kConnecting);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kIdle);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kListening);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kSpeaking);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kRinging);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kCalling);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kFriendMode);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kProvisioning);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kUpgrading);
BORLA_ENUM_TO_STRING_CASE(LedPattern::kError);
}
return "LedPattern::unknown";
}
#undef BORLA_ENUM_TO_STRING_CASE
} // namespace borla