Add psm service and psmGetBatteryChargePercentage function (fixed) (#166)

This commit is contained in:
XorTroll 2018-09-09 19:55:36 +02:00 committed by fincs
parent 6ef26bff1a
commit cd813ddb60
3 changed files with 81 additions and 0 deletions

View File

@ -48,6 +48,7 @@ extern "C" {
#include "switch/services/audren.h" #include "switch/services/audren.h"
#include "switch/services/csrng.h" #include "switch/services/csrng.h"
#include "switch/services/bpc.h" #include "switch/services/bpc.h"
#include "switch/services/psm.h"
//#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead //#include "switch/services/bsd.h" Use switch/runtime/devices/socket.h instead
#include "switch/services/fatal.h" #include "switch/services/fatal.h"
#include "switch/services/time.h" #include "switch/services/time.h"

View File

@ -0,0 +1,13 @@
/**
* @file psm.h
* @brief PSM service IPC wrapper.
* @author XorTroll
* @copyright libnx Authors
*/
#pragma once
#include "../types.h"
Result psmInitialize(void);
void psmExit(void);
Result psmGetBatteryChargePercentage(u32 *out);

67
nx/source/services/psm.c Normal file
View File

@ -0,0 +1,67 @@
#include "types.h"
#include "result.h"
#include "arm/atomics.h"
#include "kernel/ipc.h"
#include "kernel/detect.h"
#include "services/psm.h"
#include "services/sm.h"
static Service g_psmSrv;
static u64 g_refCnt;
Result psmInitialize(void)
{
Result rc = 0;
atomicIncrement64(&g_refCnt);
if (serviceIsActive(&g_psmSrv))
return 0;
rc = smGetService(&g_psmSrv, "psm");
return rc;
}
void psmExit(void)
{
if (atomicDecrement64(&g_refCnt) == 0)
serviceClose(&g_psmSrv);
}
Result psmGetBatteryChargePercentage(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 = 0;
Result rc = serviceIpcDispatch(&g_psmSrv);
if(R_SUCCEEDED(rc)) {
IpcParsedCommand r;
ipcParse(&r);
struct {
u64 magic;
u64 result;
u32 percentage;
} *resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
*out = resp->percentage;
}
}
return rc;
}