From 2d470ee2af7a3d1fe4bb3786968bf94f571698de Mon Sep 17 00:00:00 2001 From: SciresM Date: Wed, 2 Dec 2020 08:42:09 -0800 Subject: [PATCH] sm: add smDetachClient (#509) --- nx/include/switch/services/sm.h | 7 +++++++ nx/source/services/sm.c | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/nx/include/switch/services/sm.h b/nx/include/switch/services/sm.h index 1b8f5721..0b037d43 100644 --- a/nx/include/switch/services/sm.h +++ b/nx/include/switch/services/sm.h @@ -124,6 +124,13 @@ Result smRegisterService(Handle* handle_out, SmServiceName name, bool is_light, */ Result smUnregisterService(SmServiceName name); +/** + * @brief Detaches the current SM session. + * @note After this function is called, the rest of the SM API cannot be used. + * @note Only available on [11.0.0+]. + */ +Result smDetachClient(void); + /** * @brief Gets the Service session used to communicate with SM. * @return Pointer to service session used to communicate with SM. diff --git a/nx/source/services/sm.c b/nx/source/services/sm.c index a271d746..b0ca8788 100644 --- a/nx/source/services/sm.c +++ b/nx/source/services/sm.c @@ -1,5 +1,6 @@ #define NX_SERVICE_ASSUME_NON_DOMAIN #include "service_guard.h" +#include "runtime/hosversion.h" #include "runtime/diag.h" static Service g_smSrv; @@ -35,6 +36,11 @@ Handle smGetServiceOverride(SmServiceName name) { NX_GENERATE_SERVICE_GUARD(sm); +static Result _smCmdInPid(u32 cmd_id) { + u64 pid_placeholder = 0; + return serviceDispatchIn(&g_smSrv, cmd_id, pid_placeholder, .in_send_pid = true); +} + Result _smInitialize(void) { Handle sm_handle; Result rc = svcConnectToNamedPort(&sm_handle, "sm:"); @@ -49,8 +55,7 @@ Result _smInitialize(void) { Handle tmp; if (R_SUCCEEDED(rc) && R_VALUE(smGetServiceOriginal(&tmp, (SmServiceName){})) == 0x415) { - u64 pid_placeholder = 0; - rc = serviceDispatchIn(&g_smSrv, 0, pid_placeholder, .in_send_pid = true); + rc = _smCmdInPid(0); } return rc; @@ -105,3 +110,8 @@ Result smRegisterService(Handle* handle_out, SmServiceName name, bool is_light, Result smUnregisterService(SmServiceName name) { return serviceDispatchIn(&g_smSrv, 3, name); } + +Result smDetachClient(void) { + if (hosversionBefore(11,0,0)) return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + return _smCmdInPid(4); +}