mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-06 11:22:15 +02:00
Merge ab33169116
into 80439a186b
This commit is contained in:
commit
f9686b052c
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