fatal: refactor for R_TRY

This commit is contained in:
Michael Scire 2019-06-17 16:41:03 -07:00
parent f9bf8923b1
commit 31fde233e1
30 changed files with 226 additions and 285 deletions

View file

@ -13,35 +13,35 @@
* 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 <switch.h>
#include "fatal_task_power.hpp"
#include "fatal_config.hpp"
bool PowerControlTask::TryShutdown() {
/* Set a timeout of 30 seconds. */
TimeoutHelper timeout_helper(30000000000UL);
TimeoutHelper timeout_helper(30000000000UL);
bool cancel_shutdown = false;
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
while (true) {
if (timeout_helper.TimedOut()) {
break;
}
if (R_FAILED(psmGetBatteryVoltageState(&bv_state)) || bv_state == PsmBatteryVoltageState_NeedsShutdown) {
break;
}
if (bv_state == PsmBatteryVoltageState_Normal) {
cancel_shutdown = true;
break;
}
/* Query voltage state every 5 seconds, for 30 seconds. */
svcSleepThread(5000000000UL);
}
if (!cancel_shutdown) {
bpcShutdownSystem();
return true;
@ -52,7 +52,7 @@ bool PowerControlTask::TryShutdown() {
void PowerControlTask::MonitorBatteryState() {
PsmBatteryVoltageState bv_state = PsmBatteryVoltageState_Normal;
/* 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. */
@ -60,15 +60,15 @@ void PowerControlTask::MonitorBatteryState() {
this->TryShutdown();
return;
}
/* Signal we've checked the battery at least once. */
eventFire(this->battery_event);
while (true) {
if (R_FAILED(psmGetBatteryVoltageState(&bv_state))) {
bv_state = PsmBatteryVoltageState_NeedsShutdown;
}
switch (bv_state) {
case PsmBatteryVoltageState_NeedsShutdown:
case PsmBatteryVoltageState_NeedsSleep:
@ -82,7 +82,7 @@ void PowerControlTask::MonitorBatteryState() {
default:
break;
}
/* Query voltage state every 5 seconds. */
svcSleepThread(5000000000UL);
}
@ -91,13 +91,13 @@ void PowerControlTask::MonitorBatteryState() {
void PowerButtonObserveTask::WaitForPowerButton() {
/* Wait up to a second for error report generation to finish. */
eventWait(this->erpt_event, TimeoutHelper::NsToTick(1000000000UL));
/* Force a reboot after some time if kiosk unit. */
const FatalConfig *config = GetFatalConfig();
TimeoutHelper reboot_helper(config->quest_reboot_interval_second * 1000000000UL);
TimeoutHelper auto_reboot_helper(config->fatal_auto_reboot_interval * 1000000);
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))) {
@ -106,11 +106,11 @@ void PowerButtonObserveTask::WaitForPowerButton() {
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);
@ -118,7 +118,7 @@ void PowerButtonObserveTask::WaitForPowerButton() {
if (check_vol_down) {
gpioPadSetDirection(&vol_down_btn, GpioDirection_Input);
}
BpcSleepButtonState state;
GpioValue val;
while (true) {
@ -128,20 +128,20 @@ void PowerButtonObserveTask::WaitForPowerButton() {
bpcRebootSystem();
return;
}
if (check_vol_up && R_SUCCEEDED((rc = gpioPadGetValue(&vol_up_btn, &val))) && val == GpioValue_Low) {
bpcRebootSystem();
}
if (check_vol_down && R_SUCCEEDED((rc = gpioPadGetValue(&vol_down_btn, &val))) && val == GpioValue_Low) {
bpcRebootSystem();
}
if ((R_SUCCEEDED(rc = bpcGetSleepButtonState(&state)) && state == BpcSleepButtonState_Held) || (config->quest_flag && reboot_helper.TimedOut())) {
bpcRebootSystem();
return;
}
/* Wait 100 ms between button checks. */
svcSleepThread(100000000UL);
}