1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-28 21:26:14 +00:00
Atmosphere/stratosphere/fatal/source/fatal_task_power.cpp

242 lines
8.9 KiB
C++
Raw Normal View History

/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "fatal_config.hpp"
2019-07-19 03:09:35 +01:00
#include "fatal_task_power.hpp"
namespace ams::fatal::srv {
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
namespace {
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
/* Task types. */
class PowerControlTask : public ITaskWithDefaultStack {
2019-07-19 03:09:35 +01:00
private:
bool TryShutdown();
void MonitorBatteryState();
public:
virtual Result Run() override;
virtual const char *GetName() const override {
return "PowerControlTask";
}
};
class PowerButtonObserveTask : public ITaskWithDefaultStack {
2019-07-19 03:09:35 +01:00
private:
void WaitForPowerButton();
public:
virtual Result Run() override;
virtual const char *GetName() const override {
return "PowerButtonObserveTask";
}
};
2019-06-18 00:41:03 +01:00
class StateTransitionStopTask : public ITaskWithDefaultStack {
2019-07-19 03:09:35 +01:00
public:
virtual Result Run() override;
virtual const char *GetName() const override {
return "StateTransitionStopTask";
}
};
2019-06-18 00:41:03 +01:00
2020-04-08 10:21:35 +01:00
class RebootTimingObserver {
private:
2021-10-10 08:14:06 +01:00
os::Tick m_start_tick;
TimeSpan m_interval;
bool m_flag;
2020-04-08 10:21:35 +01:00
public:
2021-10-10 08:14:06 +01:00
RebootTimingObserver(bool flag, TimeSpan iv) : m_start_tick(os::GetSystemTick()), m_interval(iv), m_flag(flag) {
2020-04-08 10:21:35 +01:00
/* ... */
}
bool IsRebootTiming() const {
auto current_tick = os::GetSystemTick();
2021-10-10 08:14:06 +01:00
return m_flag && (current_tick - m_start_tick).ToTimeSpan() >= m_interval;
2020-04-08 10:21:35 +01:00
}
};
2019-07-19 03:09:35 +01:00
/* Task globals. */
PowerControlTask g_power_control_task;
PowerButtonObserveTask g_power_button_observe_task;
StateTransitionStopTask g_state_transition_stop_task;
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
/* Task Implementations. */
bool PowerControlTask::TryShutdown() {
/* Set a timeout of 30 seconds. */
2020-08-04 03:52:53 +01:00
constexpr auto MaxShutdownWaitInterval = TimeSpan::FromSeconds(30);
2020-04-08 10:21:35 +01:00
auto start_tick = os::GetSystemTick();
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
bool perform_shutdown = true;
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
while (true) {
2020-04-08 10:21:35 +01:00
auto cur_tick = os::GetSystemTick();
2020-08-04 03:52:53 +01:00
if ((cur_tick - start_tick).ToTimeSpan() > MaxShutdownWaitInterval) {
2019-07-19 03:09:35 +01:00
break;
}
2019-06-18 00:41:03 +01:00
if (R_FAILED(psmGetBatteryVoltageState(std::addressof(bv_state))) || bv_state == PsmBatteryVoltageState_NeedsShutdown) {
2019-07-19 03:09:35 +01:00
break;
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
if (bv_state == PsmBatteryVoltageState_Normal) {
perform_shutdown = false;
break;
2018-11-10 10:42:07 +00:00
}
2019-07-19 03:09:35 +01:00
2020-04-08 10:21:35 +01:00
/* Query voltage state every 1 seconds, for 30 seconds. */
os::SleepThread(TimeSpan::FromSeconds(1));
2019-07-19 03:09:35 +01:00
}
if (perform_shutdown) {
bpcShutdownSystem();
}
return perform_shutdown;
2018-11-10 10:42:07 +00:00
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
void PowerControlTask::MonitorBatteryState() {
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
2018-11-10 10:42:07 +00:00
2019-07-19 03:09:35 +01:00
/* Check the battery state, and shutdown on low voltage. */
if (R_FAILED(psmGetBatteryVoltageState(std::addressof(bv_state))) || bv_state == PsmBatteryVoltageState_NeedsShutdown) {
2019-07-19 03:09:35 +01:00
/* Wait a second for the error report task to finish. */
2021-10-10 08:14:06 +01:00
m_context->erpt_event->TimedWait(TimeSpan::FromSeconds(1));
2019-07-19 03:09:35 +01:00
this->TryShutdown();
return;
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
/* Signal we've checked the battery at least once. */
2021-10-10 08:14:06 +01:00
m_context->battery_event->Signal();
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
/* Loop querying voltage state every 5 seconds. */
while (true) {
if (R_FAILED(psmGetBatteryVoltageState(std::addressof(bv_state)))) {
2019-07-19 03:09:35 +01:00
bv_state = PsmBatteryVoltageState_NeedsShutdown;
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
switch (bv_state) {
case PsmBatteryVoltageState_NeedsShutdown:
case PsmBatteryVoltageState_NeedsSleep:
{
bool shutdown = this->TryShutdown();
if (shutdown) {
return;
}
}
break;
default:
break;
}
2019-06-18 00:41:03 +01:00
2020-04-08 10:21:35 +01:00
os::SleepThread(TimeSpan::FromSeconds(5));
2019-07-19 03:09:35 +01:00
}
}
bool IsPowerButtonHeld() {
if (hos::GetVersion() >= hos::Version_14_0_0) {
bool held = false;
return R_SUCCEEDED(bpcGetPowerButton(std::addressof(held))) && held;
} else if (hos::GetVersion() >= hos::Version_2_0_0) {
BpcSleepButtonState state;
return R_SUCCEEDED(bpcGetSleepButtonState(std::addressof(state))) && state == BpcSleepButtonState_Held;
} else {
return false;
}
}
2019-07-19 03:09:35 +01:00
void PowerButtonObserveTask::WaitForPowerButton() {
/* Wait up to a second for error report generation to finish. */
2021-10-10 08:14:06 +01:00
m_context->erpt_event->TimedWait(TimeSpan::FromSeconds(1));
2019-07-19 03:09:35 +01:00
/* Force a reboot after some time if kiosk unit. */
const auto &config = GetFatalConfig();
2020-04-08 10:21:35 +01:00
RebootTimingObserver quest_reboot_helper(config.IsQuest(), config.GetQuestRebootTimeoutInterval());
RebootTimingObserver fatal_reboot_helper(config.IsFatalRebootEnabled(), config.GetFatalRebootTimeoutInterval());
2019-07-19 03:09:35 +01:00
bool check_vol_up = true, check_vol_down = true;
gpio::GpioPadSession vol_up_btn, vol_down_btn;
if (R_FAILED(gpio::OpenSession(std::addressof(vol_up_btn), gpio::DeviceCode_ButtonVolUp))) {
2019-07-19 03:09:35 +01:00
check_vol_up = false;
}
if (R_FAILED(gpio::OpenSession(std::addressof(vol_down_btn), gpio::DeviceCode_ButtonVolDn))) {
2019-07-19 03:09:35 +01:00
check_vol_down = false;
}
/* Ensure we close on early return. */
ON_SCOPE_EXIT { if (check_vol_up) { gpio::CloseSession(std::addressof(vol_up_btn)); } };
ON_SCOPE_EXIT { if (check_vol_down) { gpio::CloseSession(std::addressof(vol_down_btn)); } };
2019-07-19 03:09:35 +01:00
/* Set direction input. */
if (check_vol_up) {
gpio::SetDirection(std::addressof(vol_up_btn), gpio::Direction_Input);
2019-07-19 03:09:35 +01:00
}
if (check_vol_down) {
gpio::SetDirection(std::addressof(vol_down_btn), gpio::Direction_Input);
2019-07-19 03:09:35 +01:00
}
while (true) {
2020-04-08 10:21:35 +01:00
if (fatal_reboot_helper.IsRebootTiming() || (quest_reboot_helper.IsRebootTiming()) ||
(check_vol_up && gpio::GetValue(std::addressof(vol_up_btn)) == gpio::GpioValue_Low) ||
(check_vol_down && gpio::GetValue(std::addressof(vol_down_btn)) == gpio::GpioValue_Low) ||
IsPowerButtonHeld())
{
2019-07-19 03:09:35 +01:00
/* If any of the above conditions succeeded, we should reboot. */
bpcRebootSystem();
return;
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
/* Wait 100 ms between button checks. */
2020-04-08 10:21:35 +01:00
os::SleepThread(TimeSpan::FromMilliSeconds(100));
2019-07-19 03:09:35 +01:00
}
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
Result PowerControlTask::Run() {
this->MonitorBatteryState();
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
Result PowerButtonObserveTask::Run() {
this->WaitForPowerButton();
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
2019-06-18 00:41:03 +01:00
2019-07-19 03:09:35 +01:00
Result StateTransitionStopTask::Run() {
/* Nintendo ignores the output of this call... */
spsmPutErrorState();
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
2019-06-18 00:41:03 +01:00
}
2019-07-19 03:09:35 +01:00
ITask *GetPowerControlTask(const ThrowContext *ctx) {
g_power_control_task.Initialize(ctx);
return std::addressof(g_power_control_task);
2019-07-19 03:09:35 +01:00
}
2018-11-10 10:42:07 +00:00
2019-07-19 03:09:35 +01:00
ITask *GetPowerButtonObserveTask(const ThrowContext *ctx) {
g_power_button_observe_task.Initialize(ctx);
return std::addressof(g_power_button_observe_task);
2019-07-19 03:09:35 +01:00
}
ITask *GetStateTransitionStopTask(const ThrowContext *ctx) {
g_state_transition_stop_task.Initialize(ctx);
return std::addressof(g_state_transition_stop_task);
2019-07-19 03:09:35 +01:00
}
}