bolar_2 backup
This commit is contained in:
167
main/power/idle_service.cc
Normal file
167
main/power/idle_service.cc
Normal file
@@ -0,0 +1,167 @@
|
||||
#include "power/idle_service.h"
|
||||
|
||||
#include "core/playback_types.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "IdleService2";
|
||||
|
||||
} // namespace
|
||||
|
||||
bool IdleService::Initialize(IEventPublisher& publisher)
|
||||
{
|
||||
if (running_.load()) {
|
||||
return true;
|
||||
}
|
||||
if (config_.check_after_ms == 0 ||
|
||||
config_.soft_off_after_ms <= config_.check_after_ms ||
|
||||
config_.response_window_ms == 0) {
|
||||
return false;
|
||||
}
|
||||
publisher_ = &publisher;
|
||||
running_.store(true);
|
||||
task_ = xTaskCreateStatic(
|
||||
&IdleService::TaskEntry,
|
||||
"idle_service2",
|
||||
task_stack_.size(),
|
||||
this,
|
||||
3,
|
||||
task_stack_.data(),
|
||||
&task_control_);
|
||||
if (task_ == nullptr) {
|
||||
running_.store(false);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void IdleService::Stop()
|
||||
{
|
||||
running_.store(false);
|
||||
phase_.store(IdlePhase::kStopped);
|
||||
}
|
||||
|
||||
void IdleService::OnApplicationEvent(
|
||||
const AppEvent& event,
|
||||
const RuntimeState& state)
|
||||
{
|
||||
busy_.store(
|
||||
state.call != CallState::kInactive ||
|
||||
state.conversation == ConversationState::kSpeaking ||
|
||||
state.playback != PlaybackState::kIdle ||
|
||||
state.alert_ringing);
|
||||
|
||||
if (state.lifecycle != LifecycleState::kRunning) {
|
||||
phase_.store(IdlePhase::kStopped);
|
||||
return;
|
||||
}
|
||||
if (event.type == AppEventType::kRealtimeConnected &&
|
||||
phase_.load() == IdlePhase::kStopped) {
|
||||
Reset(NowMs());
|
||||
return;
|
||||
}
|
||||
if (IsActivity(event)) {
|
||||
Reset(NowMs());
|
||||
return;
|
||||
}
|
||||
if (event.type == AppEventType::kLocalPromptStarted &&
|
||||
event.value ==
|
||||
static_cast<int32_t>(LocalPrompt::kIdleCheck) &&
|
||||
phase_.load() == IdlePhase::kPromptingCheck) {
|
||||
prompt_session_id_.store(event.correlation_id);
|
||||
return;
|
||||
}
|
||||
if ((event.type == AppEventType::kPlaybackDrained ||
|
||||
event.type == AppEventType::kPlaybackFailed) &&
|
||||
prompt_session_id_.load() != 0 &&
|
||||
event.correlation_id == prompt_session_id_.load() &&
|
||||
phase_.load() == IdlePhase::kPromptingCheck) {
|
||||
prompt_session_id_.store(0);
|
||||
phase_.store(IdlePhase::kAwaitingResponse);
|
||||
phase_deadline_ms_.store(
|
||||
NowMs() + config_.response_window_ms);
|
||||
}
|
||||
}
|
||||
|
||||
void IdleService::TaskEntry(void* context)
|
||||
{
|
||||
static_cast<IdleService*>(context)->Run();
|
||||
}
|
||||
|
||||
void IdleService::Run()
|
||||
{
|
||||
while (running_.load()) {
|
||||
const uint64_t now_ms = NowMs();
|
||||
const IdlePhase phase = phase_.load();
|
||||
const uint64_t idle_since = idle_since_ms_.load();
|
||||
|
||||
if (phase == IdlePhase::kRunning &&
|
||||
idle_since != 0 &&
|
||||
now_ms - idle_since >= config_.check_after_ms &&
|
||||
!busy_.load()) {
|
||||
phase_.store(IdlePhase::kPromptingCheck);
|
||||
phase_deadline_ms_.store(
|
||||
now_ms + config_.prompt_start_timeout_ms);
|
||||
publisher_->Publish(
|
||||
AppEvent::Make(AppEventType::kIdleCheckDue));
|
||||
} else if (phase == IdlePhase::kPromptingCheck &&
|
||||
phase_deadline_ms_.load() <= now_ms &&
|
||||
prompt_session_id_.load() == 0) {
|
||||
// Missing/rejected prompt still receives the normal response
|
||||
// window instead of immediately sleeping.
|
||||
phase_.store(IdlePhase::kAwaitingResponse);
|
||||
phase_deadline_ms_.store(
|
||||
now_ms + config_.response_window_ms);
|
||||
} else if (phase == IdlePhase::kAwaitingResponse &&
|
||||
phase_deadline_ms_.load() <= now_ms) {
|
||||
phase_.store(IdlePhase::kIdleSleeping);
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kIdleResponseExpired));
|
||||
} else if (phase == IdlePhase::kIdleSleeping &&
|
||||
idle_since != 0 &&
|
||||
now_ms - idle_since >=
|
||||
config_.soft_off_after_ms) {
|
||||
phase_.store(IdlePhase::kGoodbyeTriggered);
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kIdleGoodbyeDue));
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
task_ = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
void IdleService::Reset(uint64_t now_ms)
|
||||
{
|
||||
idle_since_ms_.store(now_ms);
|
||||
phase_deadline_ms_.store(0);
|
||||
prompt_session_id_.store(0);
|
||||
phase_.store(IdlePhase::kRunning);
|
||||
}
|
||||
|
||||
bool IdleService::IsActivity(const AppEvent& event)
|
||||
{
|
||||
switch (event.type) {
|
||||
case AppEventType::kButtonClicked:
|
||||
case AppEventType::kButtonLongPressed:
|
||||
case AppEventType::kWakeWordDetected:
|
||||
case AppEventType::kUserActivityDetected:
|
||||
case AppEventType::kTtsStarted:
|
||||
case AppEventType::kIncomingCall:
|
||||
case AppEventType::kCallConnected:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t IdleService::NowMs()
|
||||
{
|
||||
return static_cast<uint64_t>(esp_timer_get_time() / 1000);
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
68
main/power/idle_service.h
Normal file
68
main/power/idle_service.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include "ports/application_observer.h"
|
||||
#include "ports/event_publisher.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
namespace borla {
|
||||
|
||||
struct IdleServiceConfig {
|
||||
uint32_t check_after_ms = 3U * 60U * 1000U;
|
||||
uint32_t soft_off_after_ms = 10U * 60U * 1000U;
|
||||
uint32_t response_window_ms = 15U * 1000U;
|
||||
uint32_t prompt_start_timeout_ms = 10U * 1000U;
|
||||
};
|
||||
|
||||
enum class IdlePhase : uint8_t {
|
||||
kStopped,
|
||||
kRunning,
|
||||
kPromptingCheck,
|
||||
kAwaitingResponse,
|
||||
kIdleSleeping,
|
||||
kGoodbyeTriggered,
|
||||
};
|
||||
|
||||
class IdleService final : public IApplicationObserver {
|
||||
public:
|
||||
explicit IdleService(
|
||||
const IdleServiceConfig& config = IdleServiceConfig{})
|
||||
: config_(config)
|
||||
{
|
||||
}
|
||||
|
||||
bool Initialize(IEventPublisher& publisher);
|
||||
void Stop();
|
||||
void OnApplicationEvent(
|
||||
const AppEvent& event,
|
||||
const RuntimeState& state) override;
|
||||
|
||||
IdlePhase phase() const { return phase_.load(); }
|
||||
|
||||
private:
|
||||
static constexpr uint32_t kTaskStackDepth = 2048;
|
||||
static void TaskEntry(void* context);
|
||||
void Run();
|
||||
void Reset(uint64_t now_ms);
|
||||
static bool IsActivity(const AppEvent& event);
|
||||
static uint64_t NowMs();
|
||||
|
||||
IdleServiceConfig config_;
|
||||
IEventPublisher* publisher_ = nullptr;
|
||||
std::atomic<IdlePhase> phase_{IdlePhase::kStopped};
|
||||
std::atomic<uint64_t> idle_since_ms_{0};
|
||||
std::atomic<uint64_t> phase_deadline_ms_{0};
|
||||
std::atomic<uint32_t> prompt_session_id_{0};
|
||||
std::atomic<bool> busy_{false};
|
||||
std::atomic<bool> running_{false};
|
||||
TaskHandle_t task_ = nullptr;
|
||||
StaticTask_t task_control_{};
|
||||
std::array<StackType_t, kTaskStackDepth> task_stack_{};
|
||||
};
|
||||
|
||||
} // namespace borla
|
||||
129
main/power/power_service.cc
Normal file
129
main/power/power_service.cc
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "power/power_service.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <esp_attr.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
namespace borla {
|
||||
namespace {
|
||||
|
||||
constexpr const char* kTag = "PowerService2";
|
||||
constexpr uint8_t kLowBatteryPercent = 20;
|
||||
constexpr uint8_t kCriticalBatteryPercent = 10;
|
||||
constexpr uint8_t kWarningResetPercent = 25;
|
||||
RTC_DATA_ATTR uint32_t g_reminder_timer_wake_pending = 0;
|
||||
|
||||
uint64_t MinimumDelay(uint64_t left, uint64_t right)
|
||||
{
|
||||
if (left == 0) {
|
||||
return right;
|
||||
}
|
||||
if (right == 0) {
|
||||
return left;
|
||||
}
|
||||
return std::min(left, right);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool PowerService::Initialize(IEventPublisher& publisher)
|
||||
{
|
||||
if (running_.load()) {
|
||||
return true;
|
||||
}
|
||||
publisher_ = &publisher;
|
||||
const SoftOffWakeReason wake_reason =
|
||||
platform_.ConsumeSoftOffWakeReason();
|
||||
if (wake_reason == SoftOffWakeReason::kTimer &&
|
||||
g_reminder_timer_wake_pending != 0) {
|
||||
publisher_->Publish(
|
||||
AppEvent::Make(AppEventType::kReminderDue));
|
||||
}
|
||||
g_reminder_timer_wake_pending = 0;
|
||||
|
||||
running_.store(true);
|
||||
task_ = xTaskCreateStatic(
|
||||
&PowerService::TaskEntry,
|
||||
"power_service2",
|
||||
task_stack_.size(),
|
||||
this,
|
||||
3,
|
||||
task_stack_.data(),
|
||||
&task_control_);
|
||||
if (task_ == nullptr) {
|
||||
running_.store(false);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void PowerService::Stop()
|
||||
{
|
||||
running_.store(false);
|
||||
}
|
||||
|
||||
void PowerService::EnterSoftOff()
|
||||
{
|
||||
if (entering_soft_off_.exchange(true)) {
|
||||
return;
|
||||
}
|
||||
const uint64_t alarm_delay = alarms_.NextWakeupDelayUs();
|
||||
const uint64_t reminder_delay = reminders_.NextWakeupDelayUs();
|
||||
const uint64_t wake_delay =
|
||||
MinimumDelay(alarm_delay, reminder_delay);
|
||||
g_reminder_timer_wake_pending =
|
||||
reminder_delay != 0 &&
|
||||
(alarm_delay == 0 || reminder_delay <= alarm_delay)
|
||||
? 1
|
||||
: 0;
|
||||
|
||||
ESP_LOGI(kTag,
|
||||
"soft-off prepare alarm_us=%llu reminder_us=%llu selected_us=%llu",
|
||||
static_cast<unsigned long long>(alarm_delay),
|
||||
static_cast<unsigned long long>(reminder_delay),
|
||||
static_cast<unsigned long long>(wake_delay));
|
||||
running_.store(false);
|
||||
sensors_.Stop();
|
||||
alarms_.Stop();
|
||||
reminders_.Stop();
|
||||
platform_.EnterSoftOff(wake_delay);
|
||||
}
|
||||
|
||||
void PowerService::TaskEntry(void* context)
|
||||
{
|
||||
static_cast<PowerService*>(context)->Run();
|
||||
}
|
||||
|
||||
void PowerService::Run()
|
||||
{
|
||||
bool warned = false;
|
||||
bool critical = false;
|
||||
while (running_.load()) {
|
||||
const BatteryStatus status = battery_.GetBatteryStatus();
|
||||
if (status.valid) {
|
||||
if (status.charging || status.percent > kWarningResetPercent) {
|
||||
warned = false;
|
||||
critical = false;
|
||||
} else if (!critical &&
|
||||
status.percent <= kCriticalBatteryPercent) {
|
||||
critical = true;
|
||||
publisher_->Publish(
|
||||
AppEvent::Make(AppEventType::kCriticalBattery));
|
||||
} else if (!warned &&
|
||||
status.percent <= kLowBatteryPercent) {
|
||||
warned = true;
|
||||
publisher_->Publish(AppEvent::Make(
|
||||
AppEventType::kLowBatteryWarning));
|
||||
}
|
||||
}
|
||||
for (uint32_t waited = 0;running_.load() && waited < 30000;waited += 100)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
}
|
||||
task_ = nullptr;
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
} // namespace borla
|
||||
57
main/power/power_service.h
Normal file
57
main/power/power_service.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "ports/battery_status_provider.h"
|
||||
#include "ports/device_platform.h"
|
||||
#include "ports/event_publisher.h"
|
||||
#include "ports/power_service.h"
|
||||
#include "schedule/alarm_service.h"
|
||||
#include "schedule/reminder_service.h"
|
||||
#include "sensor/sensor_service.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
namespace borla {
|
||||
|
||||
class PowerService final : public IPowerService {
|
||||
public:
|
||||
PowerService(
|
||||
IBatteryStatusProvider& battery,
|
||||
SensorService& sensors,
|
||||
AlarmService& alarms,
|
||||
ReminderService& reminders,
|
||||
IDevicePlatform& platform)
|
||||
: battery_(battery),
|
||||
sensors_(sensors),
|
||||
alarms_(alarms),
|
||||
reminders_(reminders),
|
||||
platform_(platform)
|
||||
{
|
||||
}
|
||||
|
||||
bool Initialize(IEventPublisher& publisher);
|
||||
void Stop();
|
||||
void EnterSoftOff() override;
|
||||
|
||||
private:
|
||||
static constexpr uint32_t kTaskStackDepth = 2048;
|
||||
static void TaskEntry(void* context);
|
||||
void Run();
|
||||
|
||||
IBatteryStatusProvider& battery_;
|
||||
SensorService& sensors_;
|
||||
AlarmService& alarms_;
|
||||
ReminderService& reminders_;
|
||||
IDevicePlatform& platform_;
|
||||
IEventPublisher* publisher_ = nullptr;
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> entering_soft_off_{false};
|
||||
TaskHandle_t task_ = nullptr;
|
||||
StaticTask_t task_control_{};
|
||||
std::array<StackType_t, kTaskStackDepth> task_stack_{};
|
||||
};
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user