SwitchIdent/source/power.cpp

165 lines
4.4 KiB
C++
Raw Permalink Normal View History

2021-11-05 04:59:45 +00:00
#include <cstdio>
#include "common.hpp"
namespace SwitchIdent {
static Result _psmCmdNoInOutBool(Service* srv, bool *out, u32 cmd_id) {
u8 outval = 0;
Result ret = serviceDispatchOut(srv, cmd_id, outval);
if (R_SUCCEEDED(ret)) {
if (out) {
2021-11-05 04:59:45 +00:00
*out = outval & 1;
}
2021-11-05 04:59:45 +00:00
}
return ret;
}
static Result psmIsBatteryChargingEnabled(bool *out) {
return _psmCmdNoInOutBool(psmGetServiceSession(), out, 4);
}
static Result psmGetBatteryChargeInfoFields(BatteryChargeInfoFields *batteryChargeInfoFields) {
return serviceDispatchOut(psmGetServiceSession(), 17, *batteryChargeInfoFields);
}
2021-11-05 04:59:45 +00:00
u32 GetBatteryPercentage(void) {
Result ret = 0;
u32 percentage = 0;
if (R_FAILED(ret = psmGetBatteryChargePercentage(&percentage))) {
2021-11-05 04:59:45 +00:00
return -1;
}
2021-11-05 04:59:45 +00:00
return percentage;
}
const char *GetChargerType(void) {
Result ret = 0;
PsmChargerType charger_type;
if (R_FAILED(ret = psmGetChargerType(&charger_type))) {
2021-11-05 04:59:45 +00:00
return nullptr;
}
2021-11-05 04:59:45 +00:00
if (charger_type == PsmChargerType_EnoughPower) {
2021-11-05 04:59:45 +00:00
return "Official charger or dock";
}
else if (charger_type == PsmChargerType_LowPower) {
2021-11-05 04:59:45 +00:00
return "USB-C charger";
}
else {
2021-11-05 04:59:45 +00:00
return "No charger connected";
}
2021-11-05 04:59:45 +00:00
return nullptr;
}
bool IsCharging(void) {
Result ret = 0;
PsmChargerType charger_type;
if (R_FAILED(ret = psmGetChargerType(&charger_type))) {
2021-11-05 04:59:45 +00:00
return false;
}
2021-11-05 04:59:45 +00:00
return charger_type != PsmChargerType_Unconnected;
}
bool IsChargingEnabled(void) {
Result ret = 0;
bool is_charing_enabled = 0;
if (R_FAILED(ret = psmIsBatteryChargingEnabled(&is_charing_enabled))) {
2021-11-05 04:59:45 +00:00
return -1;
}
2021-11-05 04:59:45 +00:00
return is_charing_enabled;
}
const char *GetVoltageState(void) {
Result ret = 0;
PsmBatteryVoltageState voltage_state;
const char *states[] = {
"Power state needs shutdown",
"Power state needs sleep",
"Performance boost cannot be entered",
"Normal",
"Unknown"
};
if (R_SUCCEEDED(ret = psmGetBatteryVoltageState(&voltage_state))) {
if (voltage_state < 4) {
2021-11-05 04:59:45 +00:00
return states[voltage_state];
}
2021-11-05 04:59:45 +00:00
}
std::printf("psmGetBatteryVoltageState() failed: 0x%x.\n\n", ret);
return states[4];
}
double GetRawBatteryChargePercentage(void) {
Result ret = 0;
double raw_percentage = 0;
if (R_FAILED(ret = psmGetRawBatteryChargePercentage(&raw_percentage))) {
2021-11-05 04:59:45 +00:00
return -1;
}
2021-11-05 04:59:45 +00:00
return raw_percentage;
}
bool IsEnoughPowerSupplied(void) {
Result ret = 0;
bool is_power_supplied = 0;
if (R_FAILED(ret = psmIsEnoughPowerSupplied(&is_power_supplied))) {
2021-11-05 04:59:45 +00:00
return -1;
}
2021-11-05 04:59:45 +00:00
return is_power_supplied;
}
double GetBatteryAgePercentage(void) {
Result ret = 0;
double age_percentage = 0;
if (R_FAILED(ret = psmGetBatteryAgePercentage(&age_percentage))) {
2021-11-05 04:59:45 +00:00
return -1;
}
2021-11-05 04:59:45 +00:00
return age_percentage;
}
SetBatteryLot GetBatteryLot(void) {
Result ret = 0;
SetBatteryLot battery_lot;
if (R_FAILED(ret = setcalGetBatteryLot(&battery_lot))) {
2021-11-05 04:59:45 +00:00
std::printf("setcalGetBatteryLot() failed: 0x%x.\n\n", ret);
}
2021-11-05 04:59:45 +00:00
return battery_lot;
}
s32 GetBatteryTemperature(TsLocation location) {
Result ret = 0;
s32 temp = 0;
if (R_FAILED(ret = tsGetTemperature(location, &temp))) {
std::printf("tsGetTemperature() failed: 0x%x.\n\n", ret);
}
return temp;
}
s32 GetBatteryChargeInfoFields(BatteryChargeInfoFields * batteryChargeInfoFields) {
Result ret = 0;
if (R_FAILED(ret = psmGetBatteryChargeInfoFields(batteryChargeInfoFields))) {
std::printf("psmGetBatteryChargeInfoFields() failed: 0x%x.\n\n", ret);
}
return ret;
}
2021-11-05 04:59:45 +00:00
}