mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 20:42:44 +02:00
Add psm service and psmGetBatteryChargePercentage function (fixed) (#166)
This commit is contained in:
parent
6ef26bff1a
commit
cd813ddb60
@ -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"
|
||||||
|
13
nx/include/switch/services/psm.h
Normal file
13
nx/include/switch/services/psm.h
Normal 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
67
nx/source/services/psm.c
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user