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)) {
|
2023-06-03 03:21:35 +00:00
|
|
|
if (out) {
|
2021-11-05 04:59:45 +00:00
|
|
|
*out = outval & 1;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Result psmIsBatteryChargingEnabled(bool *out) {
|
|
|
|
return _psmCmdNoInOutBool(psmGetServiceSession(), out, 4);
|
|
|
|
}
|
2023-06-08 21:11:58 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmGetBatteryChargePercentage(&percentage))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return -1;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return percentage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetChargerType(void) {
|
|
|
|
Result ret = 0;
|
|
|
|
PsmChargerType charger_type;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmGetChargerType(&charger_type))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return nullptr;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (charger_type == PsmChargerType_EnoughPower) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return "Official charger or dock";
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
|
|
|
else if (charger_type == PsmChargerType_LowPower) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return "USB-C charger";
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-11-05 04:59:45 +00:00
|
|
|
return "No charger connected";
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCharging(void) {
|
|
|
|
Result ret = 0;
|
|
|
|
PsmChargerType charger_type;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmGetChargerType(&charger_type))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return false;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return charger_type != PsmChargerType_Unconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsChargingEnabled(void) {
|
|
|
|
Result ret = 0;
|
|
|
|
bool is_charing_enabled = 0;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmIsBatteryChargingEnabled(&is_charing_enabled))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return -1;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
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))) {
|
2023-06-03 03:21:35 +00:00
|
|
|
if (voltage_state < 4) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return states[voltage_state];
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmGetRawBatteryChargePercentage(&raw_percentage))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return -1;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return raw_percentage;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEnoughPowerSupplied(void) {
|
|
|
|
Result ret = 0;
|
|
|
|
bool is_power_supplied = 0;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmIsEnoughPowerSupplied(&is_power_supplied))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return -1;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return is_power_supplied;
|
|
|
|
}
|
|
|
|
|
|
|
|
double GetBatteryAgePercentage(void) {
|
|
|
|
Result ret = 0;
|
|
|
|
double age_percentage = 0;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = psmGetBatteryAgePercentage(&age_percentage))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
return -1;
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return age_percentage;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetBatteryLot GetBatteryLot(void) {
|
|
|
|
Result ret = 0;
|
|
|
|
SetBatteryLot battery_lot;
|
|
|
|
|
2023-06-03 03:21:35 +00:00
|
|
|
if (R_FAILED(ret = setcalGetBatteryLot(&battery_lot))) {
|
2021-11-05 04:59:45 +00:00
|
|
|
std::printf("setcalGetBatteryLot() failed: 0x%x.\n\n", ret);
|
2023-06-03 03:21:35 +00:00
|
|
|
}
|
2021-11-05 04:59:45 +00:00
|
|
|
|
|
|
|
return battery_lot;
|
|
|
|
}
|
2023-06-03 03:21:35 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-06-08 21:11:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|