1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-27 06:12:15 +00:00
Atmosphere/stratosphere/fatal/source/fatal_task_power.cpp

163 lines
5 KiB
C++
Raw Normal View History

/*
2019-04-08 03:00:49 +01: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 00:41:03 +01:00
#include <switch.h>
#include "fatal_task_power.hpp"
#include "fatal_config.hpp"
2018-11-10 10:42:07 +00:00
bool PowerControlTask::TryShutdown() {
/* Set a timeout of 30 seconds. */
2019-06-18 00:41:03 +01:00
TimeoutHelper timeout_helper(30000000000UL);
2018-11-10 10:42:07 +00:00
bool cancel_shutdown = false;
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
while (true) {
if (timeout_helper.TimedOut()) {
break;
}
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
if (R_FAILED(psmGetBatteryVoltageState(&bv_state)) || bv_state == PsmBatteryVoltageState_NeedsShutdown) {
break;
}
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
if (bv_state == PsmBatteryVoltageState_Normal) {
cancel_shutdown = true;
break;
}
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
/* Query voltage state every 5 seconds, for 30 seconds. */
svcSleepThread(5000000000UL);
}
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
if (!cancel_shutdown) {
bpcShutdownSystem();
return true;
} else {
return false;
}
}
void PowerControlTask::MonitorBatteryState() {
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00: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. */
eventWait(this->erpt_event, TimeoutHelper::NsToTick(1000000000UL));
this->TryShutdown();
return;
}
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
/* Signal we've checked the battery at least once. */
eventFire(this->battery_event);
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00:00
while (true) {
if (R_FAILED(psmGetBatteryVoltageState(&bv_state))) {
bv_state = PsmBatteryVoltageState_NeedsShutdown;
}
2019-06-18 00:41:03 +01:00
2018-11-10 10:42:07 +00: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
2018-11-10 10:42:07 +00:00
/* Query voltage state every 5 seconds. */
svcSleepThread(5000000000UL);
}
}
void PowerButtonObserveTask::WaitForPowerButton() {
/* Wait up to a second for error report generation to finish. */
eventWait(this->erpt_event, TimeoutHelper::NsToTick(1000000000UL));
2019-06-18 00:41:03 +01:00
/* Force a reboot after some time if kiosk unit. */
const FatalConfig *config = GetFatalConfig();
TimeoutHelper reboot_helper(config->quest_reboot_interval_second * 1000000000UL);
2019-06-18 00:41:03 +01:00
TimeoutHelper auto_reboot_helper(config->fatal_auto_reboot_interval * 1000000);
2019-06-18 00:41:03 +01: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;
}
2019-06-18 00:41:03 +01:00
/* 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); } };
2019-06-18 00:41:03 +01:00
/* Set direction input. */
if (check_vol_up) {
gpioPadSetDirection(&vol_up_btn, GpioDirection_Input);
}
if (check_vol_down) {
gpioPadSetDirection(&vol_down_btn, GpioDirection_Input);
}
2019-06-18 00:41:03 +01:00
BpcSleepButtonState state;
GpioValue val;
while (true) {
if (config->is_auto_reboot_enabled && auto_reboot_helper.TimedOut() ) {
bpcRebootSystem();
return;
}
2019-06-18 00:41:03 +01:00
2019-06-20 10:00:59 +01:00
if (check_vol_up && R_SUCCEEDED(gpioPadGetValue(&vol_up_btn, &val)) && val == GpioValue_Low) {
bpcRebootSystem();
}
2019-06-18 00:41:03 +01:00
2019-06-20 10:00:59 +01:00
if (check_vol_down && R_SUCCEEDED(gpioPadGetValue(&vol_down_btn, &val)) && val == GpioValue_Low) {
bpcRebootSystem();
}
2019-06-18 00:41:03 +01:00
2019-06-20 10:00:59 +01:00
if ((R_SUCCEEDED(bpcGetSleepButtonState(&state)) && state == BpcSleepButtonState_Held) || (config->quest_flag && reboot_helper.TimedOut())) {
bpcRebootSystem();
return;
}
2019-06-18 00:41:03 +01:00
/* Wait 100 ms between button checks. */
2018-11-10 11:22:21 +00:00
svcSleepThread(100000000UL);
}
}
2018-11-10 10:42:07 +00:00
Result PowerControlTask::Run() {
MonitorBatteryState();
2019-03-29 05:39:39 +00:00
return ResultSuccess;
2018-11-10 10:42:07 +00:00
}
Result PowerButtonObserveTask::Run() {
WaitForPowerButton();
2019-03-29 05:39:39 +00:00
return ResultSuccess;
}
Result StateTransitionStopTask::Run() {
/* Nintendo ignores the output of this call... */
spsmPutErrorState();
2019-03-29 05:39:39 +00:00
return ResultSuccess;
}