#include "core/state_reducer.h" #include "core/remote_behavior.h" namespace borla { namespace { void ResetMediaState(TransitionResult& result) { result.next.conversation = ConversationState::kIdle; result.next.call = CallState::kInactive; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.pending_soft_off_after_playback = false; result.next.idle_check_prompt = false; result.next.idle_sleeping = false; result.next.alert_ringing = false; result.next.pending_alert = false; result.next.friend_mode = false; result.next.local_prompt_active = false; result.next.active_local_prompt = LocalPrompt::kError; } void AddIdlePolicy(TransitionResult& result) { result.AddEffect(AppEffect::Audio(AudioPolicy::kWakeWordOnly)); result.AddEffect(AppEffect::Led(LedPattern::kIdle)); } void AddListeningPolicy(TransitionResult& result) { // Open the protocol's uplink gate before capture can enqueue the first // Opus frame. Effects execute synchronously in insertion order. result.AddEffect(AppEffect::Simple(AppEffectType::kSendStartListening)); result.AddEffect(AppEffect::Audio(AudioPolicy::kConversationCapture)); result.AddEffect(AppEffect::Led(LedPattern::kListening)); } void FinishLocalPrompt( TransitionResult& result, const RuntimeState& current) { if (!current.local_prompt_active) { return; } result.AddEffect(AppEffect::LocalPlaybackState( AppEffectType::kSendLocalPlaybackDone, current.active_local_prompt)); result.next.local_prompt_active = false; result.next.active_local_prompt = LocalPrompt::kError; } void AddDisconnectedRecovery(TransitionResult& result) { ResetMediaState(result); result.next.realtime = RealtimeState::kClosed; result.next.connectivity = ConnectivityState::kConnecting; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kWakeWordOnly)); result.AddEffect(AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect(AppEffect::Simple(AppEffectType::kStartConnectivity)); } // Audible wake acknowledgement ("我在"). Deliberately a detached prompt: the // device has already entered listening and must stay there, so this must not // run through the kLocalPromptStarted lifecycle. Unconditional by product // decision: every wake word gets an audible answer, even right after the // welcome greeting already addressed the child. void AddWakeAcknowledgement(TransitionResult& result) { result.AddEffect(AppEffect::DetachedPrompt( LocalPrompt::kWakeResponse, PlaybackPriority::kLocalPrompt)); } void RequestConversationStart(TransitionResult& result, const RuntimeState& current, bool allow_button_controls, ConversationInterruptSource interrupt_source) { if (current.lifecycle != LifecycleState::kRunning || current.friend_mode) { return; } if (current.alert_ringing) { result.next.alert_ringing = false; result.AddEffect( AppEffect::Simple(AppEffectType::kStopAlert)); } result.next.idle_sleeping = false; result.next.idle_check_prompt = false; if (current.call == CallState::kRinging) { if (allow_button_controls) { result.AddEffect( AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Simple(AppEffectType::kSendAcceptCall)); } return; } if (current.call == CallState::kCalling || current.call == CallState::kActive) { if (allow_button_controls) { result.AddEffect( AppEffect::Simple(AppEffectType::kSendHangupCall)); } return; } if (current.conversation == ConversationState::kListening) { if (!allow_button_controls) { return; } result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.AddEffect( AppEffect::Simple(AppEffectType::kSendStopListening)); result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); AddIdlePolicy(result); return; } if (current.conversation == ConversationState::kSpeaking) { FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kListening; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Abort(interrupt_source)); AddListeningPolicy(result); if (interrupt_source == ConversationInterruptSource::kWakeWordDetected) { // "你好播乐" mid-reply cuts the playback like any barge-in, but // unlike a button press it always gets the audible "我在". AddWakeAcknowledgement(result); } return; } // Everything below is the idle -> conversation entry, the only place the // legacy firmware answered the wake word out loud. const bool acknowledge_wake = interrupt_source == ConversationInterruptSource::kWakeWordDetected; if (current.realtime == RealtimeState::kOpen) { result.next.conversation = ConversationState::kListening; AddListeningPolicy(result); if (acknowledge_wake) { AddWakeAcknowledgement(result); } return; } result.next.pending_conversation_start = true; if (current.connectivity == ConnectivityState::kOnline) { result.next.realtime = RealtimeState::kOpening; result.AddEffect(AppEffect::Simple(AppEffectType::kOpenRealtime)); } else { result.next.connectivity = ConnectivityState::kConnecting; result.AddEffect( AppEffect::Simple(AppEffectType::kStartConnectivity)); } result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); if (acknowledge_wake) { AddWakeAcknowledgement(result); } } void EnterProvisioning(TransitionResult& result) { result.next.lifecycle = LifecycleState::kProvisioning; result.next.connectivity = ConnectivityState::kOffline; result.next.realtime = RealtimeState::kClosed; result.next.pending_conversation_start = false; ResetMediaState(result); result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect(AppEffect::Simple(AppEffectType::kSendGoodbye)); result.AddEffect(AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopConnectivity)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopFriendMode)); result.AddEffect(AppEffect::Led(LedPattern::kProvisioning)); // "请用小程序帮我连接网络". Without this the device is completely silent on // a first boot with no credentials — the blinking LED is the only signal, // which is not something a child or a parent reads. Detached because the // provisioning lifecycle must keep playback at kIdle; kSystem priority so // it wins over whatever kCancelPlayback just tore down. result.AddEffect(AppEffect::DetachedPrompt( LocalPrompt::kProvisioning, PlaybackPriority::kSystem)); result.AddEffect(AppEffect::Simple(AppEffectType::kEnterProvisioning)); } void EnterSoftOff(TransitionResult& result) { result.next.lifecycle = LifecycleState::kShuttingDown; result.next.connectivity = ConnectivityState::kOffline; result.next.realtime = RealtimeState::kClosed; result.next.pending_conversation_start = false; ResetMediaState(result); result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect(AppEffect::Simple(AppEffectType::kSendGoodbye)); result.AddEffect(AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopConnectivity)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopFriendMode)); result.AddEffect(AppEffect::Led(LedPattern::kOff)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopAudio)); result.AddEffect(AppEffect::Simple(AppEffectType::kEnterSoftOff)); } void BeginSoftOffPrompt( TransitionResult& result, LocalPrompt prompt) { result.next.lifecycle = LifecycleState::kShuttingDown; result.next.connectivity = ConnectivityState::kOffline; result.next.realtime = RealtimeState::kClosed; result.next.pending_conversation_start = false; ResetMediaState(result); result.next.playback = PlaybackState::kDraining; result.next.pending_soft_off_after_playback = true; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Simple(AppEffectType::kSendGoodbye)); result.AddEffect(AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopConnectivity)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopFriendMode)); result.AddEffect(AppEffect::Led(LedPattern::kOff)); result.AddEffect( AppEffect::Prompt( prompt, PlaybackPriority::kSystem)); } void BeginShutdownPrompt(TransitionResult& result) { BeginSoftOffPrompt(result, LocalPrompt::kShutdown); } void CompleteShutdownPrompt(TransitionResult& result) { result.next.lifecycle = LifecycleState::kSoftOff; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.pending_soft_off_after_playback = false; result.AddEffect(AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect(AppEffect::Led(LedPattern::kOff)); result.AddEffect(AppEffect::Simple(AppEffectType::kStopAudio)); result.AddEffect(AppEffect::Simple(AppEffectType::kEnterSoftOff)); } void EnterIdleSleeping( TransitionResult& result, const RuntimeState& current) { FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.idle_check_prompt = false; result.next.idle_sleeping = true; result.next.realtime = RealtimeState::kClosed; result.AddEffect(AppEffect::Simple( AppEffectType::kSendStopListening)); result.AddEffect( AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Simple(AppEffectType::kSendGoodbye)); result.AddEffect( AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Simple( AppEffectType::kSetWakeWordEnabled, 1)); result.AddEffect(AppEffect::Audio(AudioPolicy::kWakeWordOnly)); result.AddEffect(AppEffect::Led(LedPattern::kOff)); } void WakeIdleChannel(TransitionResult& result, const RuntimeState& current) { if (!current.idle_sleeping || current.lifecycle != LifecycleState::kRunning) { return; } result.next.idle_sleeping = false; if (current.connectivity == ConnectivityState::kOnline) { result.next.realtime = RealtimeState::kOpening; result.AddEffect( AppEffect::Simple(AppEffectType::kOpenRealtime)); result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); } else { result.next.connectivity = ConnectivityState::kConnecting; result.AddEffect( AppEffect::Simple(AppEffectType::kStartConnectivity)); result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); } } void BeginAlert(TransitionResult& result, const RuntimeState& current, AlertKind kind) { if (current.lifecycle != LifecycleState::kRunning) { return; } FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.idle_check_prompt = false; result.next.alert_ringing = true; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Simple(AppEffectType::kSendStopListening)); result.AddEffect( AppEffect::Simple(AppEffectType::kSendAbortSpeaking)); result.AddEffect(AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Led(LedPattern::kRinging)); result.AddEffect(AppEffect::Alert(kind)); WakeIdleChannel(result, current); } void PrepareLocalSequence( TransitionResult& result, const RuntimeState& current) { if (current.lifecycle != LifecycleState::kRunning || current.realtime != RealtimeState::kOpen || current.call != CallState::kInactive || current.friend_mode || current.alert_ringing) { return; } FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.pending_conversation_start = false; result.next.idle_check_prompt = false; if (current.conversation == ConversationState::kListening) { result.AddEffect( AppEffect::Simple(AppEffectType::kSendStopListening)); } else if ( current.conversation == ConversationState::kSpeaking) { result.AddEffect( AppEffect::Simple(AppEffectType::kSendAbortSpeaking)); } result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Led(LedPattern::kSpeaking)); } ButtonAction ResolveButtonAction(const AppEvent& event) { const auto configured = static_cast(event.flags); if (configured == ButtonAction::kFriendMode || configured == ButtonAction::kNetworkSwitch || configured == ButtonAction::kNothing) { return configured; } if (event.value == 3) { return ButtonAction::kFriendMode; } if (event.value == 4) { return ButtonAction::kNetworkSwitch; } return ButtonAction::kUnspecified; } } // namespace TransitionResult ReduceState(const RuntimeState& current, const AppEvent& event) { TransitionResult result; result.next = current; switch (event.type) { case AppEventType::kBootRequested: if (current.lifecycle != LifecycleState::kCold) { break; } result.next.lifecycle = LifecycleState::kStarting; result.next.connectivity = ConnectivityState::kConnecting; //result.AddEffect(AppEffect::Simple(AppEffectType::kStartConnectivity));//2026-9-7 ckkj mod result.AddEffect(AppEffect::Simple(AppEffectType::kStartAudio)); result.AddEffect(AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect(AppEffect::Led(LedPattern::kStarting)); result.AddEffect( AppEffect::Prompt(LocalPrompt::kBoot, PlaybackPriority::kSystem)); result.AddEffect(AppEffect::Simple(AppEffectType::kStartConnectivity)); break; case AppEventType::kNetworkConnected: if ((current.lifecycle != LifecycleState::kStarting && current.lifecycle != LifecycleState::kRunning) || current.friend_mode) { break; } result.next.connectivity = ConnectivityState::kOnline; result.next.realtime = RealtimeState::kOpening; result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect(AppEffect::Simple(AppEffectType::kOpenRealtime)); break; case AppEventType::kNetworkDisconnected: if (current.lifecycle == LifecycleState::kStarting || current.lifecycle == LifecycleState::kRunning) { if (current.alert_ringing) { result.AddEffect( AppEffect::Simple(AppEffectType::kStopAlert)); } if (current.friend_mode) { result.AddEffect( AppEffect::Simple(AppEffectType::kStopFriendMode)); } AddDisconnectedRecovery(result); } break; case AppEventType::kRealtimeConnected: // An in-flight connect can land after the device has already left the // conversational lifecycle (upgrade, provisioning, shutdown). Accepting // it there would force lifecycle back to kRunning mid-OTA. if (current.connectivity != ConnectivityState::kOnline || (current.lifecycle != LifecycleState::kStarting && current.lifecycle != LifecycleState::kRunning)) { break; } result.next.lifecycle = LifecycleState::kRunning; result.next.realtime = RealtimeState::kOpen; if (current.pending_alert) { result.next.pending_alert = false; result.next.alert_ringing = true; result.AddEffect( AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Led(LedPattern::kRinging)); result.AddEffect( AppEffect::Alert(current.pending_alert_kind)); } else if (current.pending_conversation_start) { result.next.pending_conversation_start = false; result.next.conversation = ConversationState::kListening; AddListeningPolicy(result); } else { AddIdlePolicy(result); } break; case AppEventType::kRealtimeDisconnected: if (current.lifecycle == LifecycleState::kStarting || current.lifecycle == LifecycleState::kRunning) { if (current.idle_sleeping || current.friend_mode) { break; } ResetMediaState(result); result.next.realtime = RealtimeState::kOpening; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kWakeWordOnly)); result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect(AppEffect::Simple(AppEffectType::kOpenRealtime)); } break; case AppEventType::kRealtimeProtocolError: if (current.lifecycle == LifecycleState::kStarting || current.lifecycle == LifecycleState::kRunning) { result.AddEffect(AppEffect::Simple( AppEffectType::kReportError, event.correlation_id)); } break; case AppEventType::kButtonClicked: { const ButtonAction button_action = ResolveButtonAction(event); if (event.value == 3 || event.value == 4) { result.AddEffect(AppEffect::Simple( AppEffectType::kSendButtonClick, static_cast(event.value))); } if (current.friend_mode) { if (button_action == ButtonAction::kFriendMode) { // Keep the exclusive state until NimBLE has stopped. Opening // realtime here can race the BLE controller shutdown. result.AddEffect(AppEffect::Simple( AppEffectType::kStopFriendMode)); } break; } if (event.value == 1) { RequestConversationStart( result, current, true, ConversationInterruptSource::kUserInitiated); } else if (event.value == 2) { EnterProvisioning(result); } else if (button_action == ButtonAction::kFriendMode && current.lifecycle == LifecycleState::kRunning && current.call == CallState::kInactive && !current.alert_ringing && current.playback == PlaybackState::kIdle) { result.next.friend_mode = true; result.next.realtime = RealtimeState::kClosed; result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.pending_conversation_start = false; result.AddEffect( AppEffect::Simple(AppEffectType::kSendStopListening)); result.AddEffect( AppEffect::Simple(AppEffectType::kSendAbortSpeaking)); result.AddEffect( AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect( AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Led(LedPattern::kFriendMode)); result.AddEffect( AppEffect::Simple(AppEffectType::kStartFriendMode)); } else if ( button_action == ButtonAction::kNetworkSwitch && current.lifecycle == LifecycleState::kRunning) { result.next.realtime = RealtimeState::kClosed; result.AddEffect(AppEffect::Simple(AppEffectType::kCloseRealtime));//2026-9 ckkj add result.AddEffect(AppEffect::Simple(AppEffectType::kSwitchNetwork)); } break; } case AppEventType::kButtonLongPressed: // Long press must be audible before the device goes dark. The previous // firmware played the shutdown prompt and waited up to 15 s for it to // drain before entering soft off (borla_s3_board.cc OnLongPress); this // went straight to kEnterSoftOff, so holding the button just killed the // device silently and a child had no confirmation it worked. // BeginShutdownPrompt arms pending_soft_off_after_playback, and // kPlaybackDrained then runs CompleteShutdownPrompt. BeginShutdownPrompt(result); break; case AppEventType::kAudioVolumeRequested: if (current.lifecycle == LifecycleState::kRunning && event.value >= 0 && event.value <= 100) { result.AddEffect(AppEffect::Simple( AppEffectType::kSetAudioVolume, static_cast(event.value))); } break; case AppEventType::kWakeWordControlRequested: if (current.lifecycle == LifecycleState::kRunning) { result.AddEffect(AppEffect::Simple( AppEffectType::kSetWakeWordEnabled, event.value != 0 ? 1U : 0U)); } break; case AppEventType::kBargeInControlRequested: if (current.lifecycle == LifecycleState::kRunning) { result.AddEffect(AppEffect::Simple( AppEffectType::kSetBargeInEnabled, event.value != 0 ? 1U : 0U)); } break; case AppEventType::kRemoteIdleSleepRequested: if (current.lifecycle == LifecycleState::kRunning && current.call == CallState::kInactive && !current.alert_ringing && !current.friend_mode) { EnterIdleSleeping(result, current); } break; case AppEventType::kLedAlertRequested: if (current.lifecycle == LifecycleState::kRunning && event.value >= 100 && event.value <= 60000) { result.AddEffect(AppEffect::Simple( AppEffectType::kShowTemporaryLedAlert, static_cast(event.value))); } break; case AppEventType::kWakeWordDetected: RequestConversationStart( result, current, false, ConversationInterruptSource::kWakeWordDetected); break; case AppEventType::kBargeInCandidate: if (current.conversation == ConversationState::kSpeaking && current.playback != PlaybackState::kIdle) { result.AddEffect(AppEffect::Simple( AppEffectType::kPausePlayback)); } break; case AppEventType::kBargeInRejected: if (current.conversation == ConversationState::kSpeaking && current.playback != PlaybackState::kIdle) { result.AddEffect(AppEffect::Simple( AppEffectType::kResumePlayback)); } break; case AppEventType::kBargeInConfirmed: if (current.conversation == ConversationState::kSpeaking) { result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.AddEffect(AppEffect::Simple( AppEffectType::kResumePlayback)); result.AddEffect(AppEffect::Simple( AppEffectType::kCancelPlayback)); AddIdlePolicy(result); } break; case AppEventType::kConversationStopRequested: if (current.lifecycle != LifecycleState::kRunning) { break; } FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.AddEffect(AppEffect::Simple(AppEffectType::kSendStopListening)); result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); AddIdlePolicy(result); break; case AppEventType::kTtsStarted: if (current.lifecycle != LifecycleState::kRunning || current.realtime != RealtimeState::kOpen || current.call != CallState::kInactive) { break; } FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kSpeaking; result.next.playback = PlaybackState::kStreaming; result.next.playback_session_id = event.correlation_id; result.AddEffect(AppEffect::Simple( AppEffectType::kResumePlayback)); result.AddEffect(AppEffect::RemotePlayback( AppEffectType::kBeginRemotePlayback, event.correlation_id, PlaybackPriority::kConversation)); result.AddEffect(AppEffect::Audio(AudioPolicy::kBargeInDuplex)); result.AddEffect(AppEffect::Led(LedPattern::kSpeaking)); break; case AppEventType::kTtsStreamEnded: if (current.playback == PlaybackState::kStreaming && (event.correlation_id == 0 || event.correlation_id == current.playback_session_id)) { result.next.playback = PlaybackState::kDraining; result.AddEffect(AppEffect::Simple( AppEffectType::kResumePlayback)); result.AddEffect(AppEffect::RemotePlayback( AppEffectType::kEndPlaybackInput, current.playback_session_id)); } break; case AppEventType::kPlaybackPreempted: if (current.playback != PlaybackState::kIdle && event.correlation_id == current.playback_session_id) { FinishLocalPrompt(result, current); result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; if (current.conversation == ConversationState::kSpeaking) { result.next.conversation = ConversationState::kIdle; result.AddEffect( AppEffect::Simple(AppEffectType::kSendAbortSpeaking)); AddIdlePolicy(result); } } break; case AppEventType::kPlaybackDrained: if (current.pending_soft_off_after_playback) { FinishLocalPrompt(result, current); CompleteShutdownPrompt(result); break; } if (current.playback != PlaybackState::kIdle && (event.correlation_id == 0 || event.correlation_id == current.playback_session_id)) { FinishLocalPrompt(result, current); result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; if (current.idle_check_prompt) { result.next.idle_check_prompt = false; } if (current.lifecycle == LifecycleState::kRunning && current.call == CallState::kInactive) { if (current.realtime == RealtimeState::kOpen) { result.next.conversation = ConversationState::kListening; AddListeningPolicy(result); } else { result.next.conversation = ConversationState::kIdle; AddIdlePolicy(result); } } } break; case AppEventType::kPlaybackFailed: if (current.pending_soft_off_after_playback) { FinishLocalPrompt(result, current); result.AddEffect(AppEffect::Simple( AppEffectType::kReportError, event.correlation_id)); CompleteShutdownPrompt(result); break; } if (event.correlation_id == 0 || event.correlation_id == current.playback_session_id) { // A remote reply that dies mid-sentence must not just go quiet: // the child gets an audible "something broke" before the device // returns to listening. Local prompts already own their own // failure feedback, so they are excluded. const bool interrupted_remote_reply = current.conversation == ConversationState::kSpeaking && !current.local_prompt_active && current.call == CallState::kInactive; FinishLocalPrompt(result, current); result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.conversation = ConversationState::kIdle; result.next.idle_check_prompt = false; result.AddEffect(AppEffect::Simple(AppEffectType::kReportError, event.correlation_id)); AddIdlePolicy(result); if (interrupted_remote_reply) { result.AddEffect(AppEffect::Simple( AppEffectType::kRequestConnectionErrorPrompt)); } } break; case AppEventType::kLocalPromptRequested: if (current.lifecycle != LifecycleState::kStarting && current.lifecycle != LifecycleState::kRunning) { break; } if (current.lifecycle == LifecycleState::kRunning && (current.call != CallState::kInactive || current.alert_ringing || current.friend_mode)) { break; } { const auto prompt = static_cast(event.value); const auto priority = static_cast(event.flags); const auto selector = event.correlation_id == 0 ? static_cast(-1) : static_cast(event.correlation_id - 1); if (prompt == LocalPrompt::kWelcome && current.conversation == ConversationState::kListening) { // The server greets every new websocket session with // play_local welcome. When the session was opened BY the wake // word the child already heard "我在" and the microphone is // live — playing the greeting here cancelled that // acknowledgement and closed the uplink for the prompt's // whole duration. Ack it unplayed (started+done releases the // server's playing_local lock immediately) and stay // listening. result.AddEffect(AppEffect::LocalPlaybackState( AppEffectType::kSendLocalPlaybackStarted, prompt)); result.AddEffect(AppEffect::LocalPlaybackState( AppEffectType::kSendLocalPlaybackDone, prompt)); break; } if (current.lifecycle == LifecycleState::kRunning) { FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; if (current.conversation == ConversationState::kListening) { result.AddEffect(AppEffect::Simple( AppEffectType::kSendStopListening)); } else if (current.conversation == ConversationState::kSpeaking) { result.AddEffect(AppEffect::Simple( AppEffectType::kSendAbortSpeaking)); } result.AddEffect(AppEffect::Simple( AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Led( LedPattern::kSpeaking)); } result.AddEffect( AppEffect::Prompt(prompt, priority, selector)); } break; case AppEventType::kLocalPromptStarted: if ((current.pending_soft_off_after_playback || current.idle_check_prompt) && current.playback == PlaybackState::kDraining) { result.next.playback_session_id = event.correlation_id; result.next.local_prompt_active = true; result.next.active_local_prompt = static_cast(event.value); result.AddEffect(AppEffect::LocalPlaybackState( AppEffectType::kSendLocalPlaybackStarted, result.next.active_local_prompt)); } else if ( current.lifecycle == LifecycleState::kRunning && current.call == CallState::kInactive && !current.alert_ringing && !current.friend_mode && event.correlation_id != 0) { result.next.conversation = ConversationState::kSpeaking; result.next.playback = PlaybackState::kDraining; result.next.playback_session_id = event.correlation_id; result.next.local_prompt_active = true; result.next.active_local_prompt = static_cast(event.value); result.AddEffect(AppEffect::LocalPlaybackState( AppEffectType::kSendLocalPlaybackStarted, result.next.active_local_prompt)); } break; case AppEventType::kLocalSequenceRequested: case AppEventType::kActivationReplayDue: PrepareLocalSequence(result, current); break; case AppEventType::kLocalSequenceStarted: if (current.lifecycle == LifecycleState::kRunning && current.realtime == RealtimeState::kOpen && current.call == CallState::kInactive && !current.friend_mode && !current.alert_ringing) { result.next.conversation = ConversationState::kSpeaking; result.next.playback = PlaybackState::kDraining; result.next.playback_session_id = event.correlation_id; } break; case AppEventType::kShutdownPromptRequested: if (current.lifecycle == LifecycleState::kStarting || current.lifecycle == LifecycleState::kRunning) { BeginShutdownPrompt(result); } break; case AppEventType::kUserActivityDetected: WakeIdleChannel(result, current); break; case AppEventType::kIdleCheckDue: if (current.lifecycle == LifecycleState::kRunning && current.call == CallState::kInactive && current.conversation != ConversationState::kSpeaking && current.playback == PlaybackState::kIdle && !current.alert_ringing) { result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kDraining; result.next.playback_session_id = 0; result.next.idle_check_prompt = true; result.AddEffect( AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Led(LedPattern::kSpeaking)); result.AddEffect(AppEffect::Prompt( LocalPrompt::kIdleCheck, PlaybackPriority::kSystem)); } break; case AppEventType::kIdleResponseExpired: if (current.lifecycle == LifecycleState::kRunning && !current.alert_ringing) { EnterIdleSleeping(result, current); } break; case AppEventType::kIdleGoodbyeDue: if (current.lifecycle == LifecycleState::kRunning && current.idle_sleeping) { BeginSoftOffPrompt( result, LocalPrompt::kIdleGoodbye); } break; case AppEventType::kReminderDue: if (current.friend_mode) { result.next.pending_alert = true; result.next.pending_alert_kind = AlertKind::kReminder; result.AddEffect( AppEffect::Simple(AppEffectType::kStopFriendMode)); break; } if (current.lifecycle == LifecycleState::kStarting) { result.next.pending_alert = true; result.next.pending_alert_kind = AlertKind::kReminder; } else { BeginAlert(result, current, AlertKind::kReminder); } break; case AppEventType::kAlarmDue: if (current.friend_mode) { result.next.pending_alert = true; result.next.pending_alert_kind = AlertKind::kAlarm; result.AddEffect( AppEffect::Simple(AppEffectType::kStopFriendMode)); break; } if (current.lifecycle == LifecycleState::kStarting) { result.next.pending_alert = true; result.next.pending_alert_kind = AlertKind::kAlarm; } else { BeginAlert(result, current, AlertKind::kAlarm); } break; case AppEventType::kFirstDayAlarmDue: WakeIdleChannel(result, current); break; case AppEventType::kAlertFinished: if (current.alert_ringing) { result.next.alert_ringing = false; result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; AddIdlePolicy(result); } break; case AppEventType::kLowBatteryWarning: if (current.lifecycle == LifecycleState::kRunning && !current.alert_ringing) { result.AddEffect(AppEffect::Prompt( LocalPrompt::kLowBattery20, PlaybackPriority::kSystem)); } break; case AppEventType::kCriticalBattery: if (current.lifecycle == LifecycleState::kStarting || current.lifecycle == LifecycleState::kRunning) { BeginSoftOffPrompt( result, LocalPrompt::kLowBattery10); } break; case AppEventType::kOutgoingCall: if (current.lifecycle != LifecycleState::kRunning || current.realtime != RealtimeState::kOpen) { break; } FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.call = CallState::kCalling; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kPlaybackOnly)); result.AddEffect(AppEffect::Led(LedPattern::kCalling)); result.AddEffect(AppEffect::Prompt( LocalPrompt::kCallWait, PlaybackPriority::kCall)); break; case AppEventType::kIncomingCall: if (current.lifecycle != LifecycleState::kRunning || current.realtime != RealtimeState::kOpen) { break; } FinishLocalPrompt(result, current); result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.next.call = CallState::kRinging; result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::Audio(AudioPolicy::kCallDuplex)); result.AddEffect(AppEffect::Led(LedPattern::kRinging)); result.AddEffect(AppEffect::Prompt( LocalPrompt::kCallRing, PlaybackPriority::kCall)); break; case AppEventType::kCallConnected: if (current.lifecycle == LifecycleState::kRunning && (current.call == CallState::kRinging || current.call == CallState::kCalling)) { result.next.call = CallState::kActive; result.next.playback = PlaybackState::kStreaming; result.next.playback_session_id = event.correlation_id; result.AddEffect( AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect(AppEffect::RemotePlayback( AppEffectType::kBeginRemotePlayback, event.correlation_id, PlaybackPriority::kCall)); result.AddEffect(AppEffect::Audio(AudioPolicy::kCallDuplex)); result.AddEffect(AppEffect::Led(LedPattern::kCalling)); } break; case AppEventType::kCallEnded: if (current.call != CallState::kInactive) { result.next.call = CallState::kInactive; result.next.conversation = ConversationState::kIdle; result.next.playback = PlaybackState::kIdle; result.next.playback_session_id = 0; result.AddEffect( AppEffect::Simple(AppEffectType::kCancelPlayback)); AddIdlePolicy(result); result.AddEffect(AppEffect::Prompt( LocalPrompt::kCallEnded, PlaybackPriority::kSystem)); } break; case AppEventType::kFriendModeEnded: if (!current.friend_mode) { break; } result.next.friend_mode = false; result.AddEffect( AppEffect::Audio(AudioPolicy::kWakeWordOnly)); if (event.correlation_id != 0) { result.AddEffect(AppEffect::Simple( AppEffectType::kReportError, event.correlation_id)); } if (current.connectivity == ConnectivityState::kOnline) { result.next.realtime = RealtimeState::kOpening; result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect( AppEffect::Simple(AppEffectType::kOpenRealtime)); } else { result.next.connectivity = ConnectivityState::kConnecting; result.next.realtime = RealtimeState::kClosed; result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect( AppEffect::Simple(AppEffectType::kStartConnectivity)); } break; case AppEventType::kProvisioningRequested: EnterProvisioning(result); break; case AppEventType::kProvisioningWifiSaved: // "配网成功啦". Stay in provisioning: the miniprogram drives the reboot // that actually leaves this mode, exactly as the previous firmware did. if (current.lifecycle == LifecycleState::kProvisioning) { result.AddEffect(AppEffect::DetachedPrompt( LocalPrompt::kWifiSuccess, PlaybackPriority::kSystem)); } break; case AppEventType::kProvisioningCompleted: if (current.lifecycle == LifecycleState::kProvisioning) { result.next.lifecycle = LifecycleState::kStarting; result.next.connectivity = ConnectivityState::kConnecting; result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect(AppEffect::Simple(AppEffectType::kStartConnectivity)); } break; case AppEventType::kUpgradeRequested: result.next.lifecycle = LifecycleState::kUpgrading; result.next.realtime = RealtimeState::kClosed; result.next.pending_conversation_start = false; ResetMediaState(result); result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Simple(AppEffectType::kStopFriendMode)); result.AddEffect(AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect(AppEffect::Simple(AppEffectType::kCloseRealtime)); result.AddEffect(AppEffect::Led(LedPattern::kUpgrading)); result.AddEffect(AppEffect::Simple(AppEffectType::kStartUpgrade, event.correlation_id)); break; case AppEventType::kUpgradeCompleted: if (current.lifecycle == LifecycleState::kUpgrading) { result.AddEffect(AppEffect::Simple(AppEffectType::kReboot)); } break; case AppEventType::kUpgradeFailed: if (current.lifecycle == LifecycleState::kUpgrading) { result.next.lifecycle = LifecycleState::kRunning; result.next.realtime = RealtimeState::kOpening; result.AddEffect(AppEffect::Simple(AppEffectType::kReportError, event.correlation_id)); result.AddEffect(AppEffect::Led(LedPattern::kConnecting)); result.AddEffect( AppEffect::Simple(AppEffectType::kOpenRealtime)); } break; case AppEventType::kSoftOffRequested: EnterSoftOff(result); break; case AppEventType::kFatalError: result.next.lifecycle = LifecycleState::kFaulted; result.next.connectivity = ConnectivityState::kOffline; result.next.realtime = RealtimeState::kClosed; result.next.pending_conversation_start = false; ResetMediaState(result); result.AddEffect(AppEffect::Simple(AppEffectType::kCancelPlayback)); result.AddEffect( AppEffect::Simple(AppEffectType::kStopFriendMode)); result.AddEffect(AppEffect::Audio(AudioPolicy::kOff)); result.AddEffect(AppEffect::Led(LedPattern::kError)); result.AddEffect(AppEffect::Simple(AppEffectType::kReportError, event.correlation_id)); break; } return result; } } // namespace borla