Atmosphere/stratosphere/fatal/source/fatal_task_power.cpp

213 lines
7.7 KiB
C++
Raw Normal View History

/*
2019-04-08 04:00:49 +02:00
* Copyright (c) 2018-2019 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/>.
*/
2019-06-18 01:41:03 +02:00
#include "fatal_config.hpp"
2019-07-19 04:09:35 +02:00
#include "fatal_task_power.hpp"
namespace ams::fatal::srv {
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
namespace {
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
/* Task types. */
class PowerControlTask : public ITaskWithDefaultStack {
2019-07-19 04:09:35 +02: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 04:09:35 +02:00
private:
void WaitForPowerButton();
public:
virtual Result Run() override;
virtual const char *GetName() const override {
return "PowerButtonObserveTask";
}
};
2019-06-18 01:41:03 +02:00
class StateTransitionStopTask : public ITaskWithDefaultStack {
2019-07-19 04:09:35 +02:00
public:
virtual Result Run() override;
virtual const char *GetName() const override {
return "StateTransitionStopTask";
}
};
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
/* Task globals. */
PowerControlTask g_power_control_task;
PowerButtonObserveTask g_power_button_observe_task;
StateTransitionStopTask g_state_transition_stop_task;
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
/* Task Implementations. */
bool PowerControlTask::TryShutdown() {
/* Set a timeout of 30 seconds. */
2019-09-24 12:15:36 +02:00
os::TimeoutHelper timeout_helper(30'000'000'000ul);
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
bool perform_shutdown = true;
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
while (true) {
if (timeout_helper.TimedOut()) {
break;
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
if (R_FAILED(psmGetBatteryVoltageState(&bv_state)) || bv_state == PsmBatteryVoltageState_NeedsShutdown) {
break;
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
if (bv_state == PsmBatteryVoltageState_Normal) {
perform_shutdown = false;
break;
2018-11-10 11:42:07 +01:00
}
2019-07-19 04:09:35 +02:00
/* Query voltage state every 5 seconds, for 30 seconds. */
svcSleepThread(5'000'000'000ul);
}
if (perform_shutdown) {
bpcShutdownSystem();
}
return perform_shutdown;
2018-11-10 11:42:07 +01:00
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
void PowerControlTask::MonitorBatteryState() {
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
2018-11-10 11:42:07 +01:00
2019-07-19 04:09:35 +02:00
/* Check the battery state, and shutdown on low voltage. */
if (R_FAILED(psmGetBatteryVoltageState(&bv_state)) || bv_state == PsmBatteryVoltageState_NeedsShutdown) {
/* Wait a second for the error report task to finish. */
2019-09-24 12:15:36 +02:00
eventWait(const_cast<Event *>(&this->context->erpt_event), os::TimeoutHelper::NsToTick(1'000'000'000ul));
2019-07-19 04:09:35 +02:00
this->TryShutdown();
return;
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
/* Signal we've checked the battery at least once. */
eventFire(const_cast<Event *>(&this->context->battery_event));
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
/* Loop querying voltage state every 5 seconds. */
while (true) {
if (R_FAILED(psmGetBatteryVoltageState(&bv_state))) {
bv_state = PsmBatteryVoltageState_NeedsShutdown;
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
switch (bv_state) {
case PsmBatteryVoltageState_NeedsShutdown:
case PsmBatteryVoltageState_NeedsSleep:
{
bool shutdown = this->TryShutdown();
if (shutdown) {
return;
}
}
break;
default:
break;
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
svcSleepThread(5'000'000'000ul);
}
}
void PowerButtonObserveTask::WaitForPowerButton() {
/* Wait up to a second for error report generation to finish. */
2019-09-24 12:15:36 +02:00
eventWait(const_cast<Event *>(&this->context->erpt_event), os::TimeoutHelper::NsToTick(1'000'000'000ul));
2019-07-19 04:09:35 +02:00
/* Force a reboot after some time if kiosk unit. */
const auto &config = GetFatalConfig();
2019-09-24 12:15:36 +02:00
os::TimeoutHelper quest_reboot_helper(config.GetQuestRebootTimeoutInterval());
os::TimeoutHelper fatal_reboot_helper(config.GetFatalRebootTimeoutInterval());
2019-07-19 04:09:35 +02:00
bool check_vol_up = true, check_vol_down = true;
GpioPadSession vol_up_btn, vol_down_btn;
if (R_FAILED(gpioOpenSession(&vol_up_btn, GpioPadName_ButtonVolUp))) {
check_vol_up = false;
}
if (R_FAILED(gpioOpenSession(&vol_down_btn, GpioPadName_ButtonVolDown))) {
check_vol_down = false;
}
/* Ensure we close on early return. */
ON_SCOPE_EXIT { if (check_vol_up) { gpioPadClose(&vol_up_btn); } };
ON_SCOPE_EXIT { if (check_vol_down) { gpioPadClose(&vol_down_btn); } };
/* Set direction input. */
if (check_vol_up) {
gpioPadSetDirection(&vol_up_btn, GpioDirection_Input);
}
if (check_vol_down) {
gpioPadSetDirection(&vol_down_btn, GpioDirection_Input);
}
BpcSleepButtonState state;
GpioValue val;
while (true) {
if ((config.IsFatalRebootEnabled() && fatal_reboot_helper.TimedOut()) ||
(check_vol_up && R_SUCCEEDED(gpioPadGetValue(&vol_up_btn, &val)) && val == GpioValue_Low) ||
(check_vol_down && R_SUCCEEDED(gpioPadGetValue(&vol_down_btn, &val)) && val == GpioValue_Low) ||
(R_SUCCEEDED(bpcGetSleepButtonState(&state)) && state == BpcSleepButtonState_Held) ||
(config.IsQuest() && quest_reboot_helper.TimedOut())) {
/* If any of the above conditions succeeded, we should reboot. */
bpcRebootSystem();
return;
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
/* Wait 100 ms between button checks. */
svcSleepThread(100'000'000ul);
}
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
Result PowerControlTask::Run() {
this->MonitorBatteryState();
return ResultSuccess();
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
Result PowerButtonObserveTask::Run() {
this->WaitForPowerButton();
return ResultSuccess();
}
2019-06-18 01:41:03 +02:00
2019-07-19 04:09:35 +02:00
Result StateTransitionStopTask::Run() {
/* Nintendo ignores the output of this call... */
spsmPutErrorState();
return ResultSuccess();
}
2019-06-18 01:41:03 +02:00
}
2019-07-19 04:09:35 +02:00
ITask *GetPowerControlTask(const ThrowContext *ctx) {
g_power_control_task.Initialize(ctx);
return &g_power_control_task;
}
2018-11-10 11:42:07 +01:00
2019-07-19 04:09:35 +02:00
ITask *GetPowerButtonObserveTask(const ThrowContext *ctx) {
g_power_button_observe_task.Initialize(ctx);
return &g_power_button_observe_task;
}
ITask *GetStateTransitionStopTask(const ThrowContext *ctx) {
g_state_transition_stop_task.Initialize(ctx);
return &g_state_transition_stop_task;
}
}