Add some power commands

This commit is contained in:
Joel16 2018-09-09 20:00:44 -05:00
parent d865bdfb2c
commit 950ea5e85f
4 changed files with 347 additions and 9 deletions

302
common/power.c Normal file
View File

@ -0,0 +1,302 @@
#include <switch.h>
#include "power.h"
static Result psmGetChargerType(Service *srv, u32 *out)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 1;
Result rc = serviceIpcDispatch(srv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u32 voltage;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->voltage;
}
}
return rc;
}
static Result psmIsBatteryChargingEnabled(Service *srv, bool *out)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 4;
Result rc = serviceIpcDispatch(srv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u8 enable;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->enable;
}
}
return rc;
}
static Result psmGetBatteryVoltageState(Service *srv, u32 *out)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 12;
Result rc = serviceIpcDispatch(srv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u32 voltage;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->voltage;
}
}
return rc;
}
static Result psmGetRawBatteryChargePercentage(Service *srv, u64 *out)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 13;
Result rc = serviceIpcDispatch(srv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u64 age_percentage;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->age_percentage;
}
}
return rc;
}
static Result psmIsEnoughPowerSupplied(Service *srv, bool *out)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 14;
Result rc = serviceIpcDispatch(srv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u8 power_supplied;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->power_supplied;
}
}
return rc;
}
static Result psmGetBatteryAgePercentage(Service *srv, u64 *out)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = ipcPrepareHeader(&c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 15;
Result rc = serviceIpcDispatch(srv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u64 age_percentage;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->age_percentage;
}
}
return rc;
}
u32 SwitchIdent_GetBatteryPercent(void)
{
Result ret = 0;
u32 out = 0;
if (R_FAILED(ret = psmGetBatteryChargePercentage(&out)))
return -1;
return out;
}
u32 SwitchIdent_GetChargerType(Service *srv)
{
Result ret = 0;
u32 out = 0;
if (R_FAILED(ret = psmGetChargerType(srv, &out)))
return -1;
return out;
}
bool SwitchIdent_IsChargingEnabled(Service *srv)
{
Result ret = 0;
bool out = 0;
if (R_FAILED(ret = psmIsBatteryChargingEnabled(srv, &out)))
return -1;
return out;
}
u32 SwitchIdent_GetVoltage(Service *srv)
{
Result ret = 0;
u32 out = 0;
if (R_FAILED(ret = psmGetBatteryVoltageState(srv, &out)))
return -1;
return out;
}
u64 SwitchIdent_GetRawBatteryChargePercentage(Service *srv)
{
Result ret = 0;
u64 out = 0;
if (R_FAILED(ret = psmGetRawBatteryChargePercentage(srv, &out)))
return -1;
return out;
}
bool SwitchIdent_IsEnoughPowerSupplied(Service *srv)
{
Result ret = 0;
bool out = 0;
if (R_FAILED(ret = psmIsEnoughPowerSupplied(srv, &out)))
return -1;
return out;
}
u64 SwitchIdent_GetBatteryAgePercent(Service *srv)
{
Result ret = 0;
u64 out = 0;
if (R_FAILED(ret = psmGetBatteryAgePercentage(srv, &out)))
return -1;
return out;
}

12
common/power.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _SWITCHIDENT_POWER_H_
#define _SWITCHIDENT_POWER_H_
u32 SwitchIdent_GetBatteryPercent(void);
u32 SwitchIdent_GetChargerType(Service *srv);
bool SwitchIdent_IsChargingEnabled(Service *srv);
u32 SwitchIdent_GetVoltage(Service *srv);
u64 SwitchIdent_GetRawBatteryChargePercentage(Service *srv);
bool SwitchIdent_IsEnoughPowerSupplied(Service *srv);
u64 SwitchIdent_GetBatteryAgePercent(Service *srv);
#endif

View File

@ -5,6 +5,7 @@
static void Term_Services(void)
{
psmExit();
nsExit();
apmExit();
appletExit();
@ -82,6 +83,9 @@ static void Init_Services(void)
if (R_FAILED(ret = nsInitialize()))
printf("nsInitialize() failed: 0x%x.\n\n", ret);
if (R_FAILED(ret = psmInitialize()))
printf("psmInitialize() failed: 0x%x.\n\n", ret);
}
int main(int argc, char **argv)

View File

@ -4,16 +4,18 @@
#include "kernel.h"
#include "menus.h"
#include "misc.h"
#include "power.h"
#include "SDL_helper.h"
#include "storage.h"
#include "system.h"
#include "utils.h"
#define MENU_Y_DIST 67
#define MENU_Y_DIST 67
#define MAX_MENU_ITEMS 5
static int item_height = 0;
static Service setsys_service;
static Service setsys_service, psm_service;
static void Menu_DrawItem(int x, int y, char *item_title, const char* text, ...)
{
@ -50,6 +52,15 @@ static void Menu_System(void)
Menu_DrawItem(450, 250 + ((MENU_Y_DIST - item_height) / 2) + 300, "NFC:", SwitchIdent_GetFlag(SetSysFlag_NfcEnable)? "Enabled" : "Disabled");
}
static void Menu_Power(void)
{
Menu_DrawItem(450, 250 + ((MENU_Y_DIST - item_height) / 2) + 50, "Battery percentage:", "%lu %%", SwitchIdent_GetBatteryPercent());
Menu_DrawItem(450, 250 + ((MENU_Y_DIST - item_height) / 2) + 100, "Battery voltage:", "%lu", SwitchIdent_GetVoltage(&psm_service));
Menu_DrawItem(450, 250 + ((MENU_Y_DIST - item_height) / 2) + 150, "Battery charger type:", "%d", SwitchIdent_GetChargerType(&psm_service));
Menu_DrawItem(450, 250 + ((MENU_Y_DIST - item_height) / 2) + 200, "Battery charging status:", SwitchIdent_IsChargingEnabled(&psm_service)? "Enabled" : "Disabled");
Menu_DrawItem(450, 250 + ((MENU_Y_DIST - item_height) / 2) + 250, "Battery ample power supplied:", SwitchIdent_IsEnoughPowerSupplied(&psm_service)? "Yes" : "No");
}
static void Menu_Storage(void)
{
u64 sd_used = SwitchIdent_GetUsedStorage(FsStorageId_SdCard);
@ -132,9 +143,13 @@ void Menu_Main(void)
int selection = 0;
Result ret = 0;
if (R_FAILED(ret = smGetService(&setsys_service, "set:sys")))
printf("setsysInitialize() failed: 0x%x.\n\n", ret);
if (R_FAILED(ret = smGetService(&psm_service, "psm")))
printf("psmInitialize() failed: 0x%x.\n\n", ret);
while(appletMainLoop())
{
SDL_ClearScreen(RENDERER, BACKGROUND_COLOUR);
@ -150,9 +165,10 @@ void Menu_Main(void)
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2), selection == 0? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Kernel");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 1), selection == 1? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "System");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 2), selection == 2? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Storage");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 3), selection == 3? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Misc");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 4), selection == 4? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Exit");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 2), selection == 2? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Power");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 3), selection == 3? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Storage");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 4), selection == 4? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Misc");
SDL_DrawText(RENDERER, Ubuntu_R, 30, 50 + ((MENU_Y_DIST - item_height) / 2) + (MENU_Y_DIST * 5), selection == 5? ITEM_SELECTED_COLOUR : ITEM_COLOUR, "Exit");
hidScanInput();
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
@ -162,10 +178,10 @@ void Menu_Main(void)
else if (kDown & KEY_DUP)
selection--;
if (selection > 4)
if (selection > MAX_MENU_ITEMS)
selection = 0;
if (selection < 0)
selection = 4;
selection = MAX_MENU_ITEMS;
switch (selection)
{
@ -176,18 +192,22 @@ void Menu_Main(void)
Menu_System();
break;
case 2:
Menu_Storage();
Menu_Power();
break;
case 3:
Menu_Storage();
break;
case 4:
Menu_Misc();
break;
}
SDL_RenderPresent(RENDERER);
if ((kDown & KEY_PLUS) || ((kDown & KEY_A) && (selection == 4)))
if ((kDown & KEY_PLUS) || ((kDown & KEY_A) && (selection == MAX_MENU_ITEMS)))
break;
}
serviceClose(&setsys_service);
serviceClose(&psm_service);
}