From 0945be043b76778cce22e5e1106cf095e067faf3 Mon Sep 17 00:00:00 2001 From: Luis Scheurenbrand Date: Wed, 2 Dec 2020 03:46:22 +0100 Subject: [PATCH] add capmtp --- nx/include/switch/services/capmtp.h | 21 +++++ nx/source/services/capmtp.c | 121 ++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 nx/include/switch/services/capmtp.h create mode 100644 nx/source/services/capmtp.c diff --git a/nx/include/switch/services/capmtp.h b/nx/include/switch/services/capmtp.h new file mode 100644 index 00000000..23300f41 --- /dev/null +++ b/nx/include/switch/services/capmtp.h @@ -0,0 +1,21 @@ +/** + * @file capmtp.h + * @brief capmtp service IPC wrapper. + * @note Only available on [11.0.0+]. + * @author Behemoth + */ +#pragma once +#include "../types.h" +#include "../kernel/event.h" +#include "../sf/service.h" + +Result capmtpInitialize(void* mem, size_t size, u32 app_count, u32 max_img, u32 max_vid, const char *other_name); +void capmtpExit(void); + +Result capmtpStartCommandHandler(void); +Result capmtpStopCommandHandler(void); +Event *capmtpGetEvent1(void); +Event *capmtpGetEvent2(void); +Result capmtpIsRunning(bool *out); +Result capmtpUnkBool(bool *out); +Result capmtpGetResult(void); diff --git a/nx/source/services/capmtp.c b/nx/source/services/capmtp.c new file mode 100644 index 00000000..ae010170 --- /dev/null +++ b/nx/source/services/capmtp.c @@ -0,0 +1,121 @@ +#include + +#include "kernel/tmem.h" +#include "runtime/hosversion.h" +#include "services/capmtp.h" +#include "services/sm.h" + +static Service g_capmtpRoot, g_capmtp; +static TransferMemory g_tmem; +static Event g_event1, g_event2; + +static Result _capmtpOpenSession(Service *srv); +static Result _capmtpOpen(u32 app_count, u32 max_img, u32 max_vid, const char *other_name); +static Result _capmtpClose(void); +static Result _capmtpNoInEventOut(u32 id, Event* event, bool autoclear); + +Result capmtpInitialize(void* mem, size_t size, u32 app_count, u32 max_img, u32 max_vid, const char *other_name) { + Result rc=0; + + if (hosversionBefore(11,0,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + rc = tmemCreateFromMemory(&g_tmem, mem, size, Perm_None); + + if (R_SUCCEEDED(rc)) rc = smGetService(&g_capmtpRoot, "capmtp"); + + if (R_SUCCEEDED(rc)) rc = _capmtpOpenSession(&g_capmtp); + if (R_SUCCEEDED(rc)) rc = _capmtpOpen(app_count, max_img, max_vid, other_name); + if (R_SUCCEEDED(rc)) rc = _capmtpNoInEventOut(5, &g_event1, false); + if (R_SUCCEEDED(rc)) rc = _capmtpNoInEventOut(7, &g_event2, false); + + if (R_FAILED(rc)) capmtpExit(); + + return rc; +} + +void capmtpExit(void) { + _capmtpClose(); + eventClose(&g_event2); + eventClose(&g_event1); + serviceClose(&g_capmtpRoot); + serviceClose(&g_capmtp); + tmemClose(&g_tmem); +} + +static Result _capmtpNoIO(u32 id) { + return serviceDispatch(&g_capmtp, id); +} + +static Result _capmtpNoInEventOut(u32 id, Event* event, bool autoclear) { + Result rc=0; + Handle handle=0; + + rc = serviceDispatch(&g_capmtp, id, + .out_handle_attrs = { SfOutHandleAttr_HipcCopy }, + .out_handles = &handle, + ); + + eventLoadRemote(event, handle, autoclear); + + return rc; +} + +static Result _capmtpNoInBoolOut(u32 id, bool* out) { + u8 tmp=0; + Result rc = serviceDispatchOut(&g_capmtp, id, tmp); + if (R_SUCCEEDED(rc)) *out = tmp & 1; + return rc; +} + +static Result _capmtpOpenSession(Service *srv) { + return serviceDispatch(&g_capmtpRoot, 0, + .out_num_objects = 1, + .out_objects = srv, + ); +} + +static Result _capmtpOpen(u32 app_count, u32 max_img, u32 max_vid, const char *other_name) { + const struct { + u32 c[4]; + } in = { { app_count, max_img, max_vid, g_tmem.size } }; + + return serviceDispatchIn(&g_capmtp, 0, in, + .buffer_attrs = { SfBufferAttr_In | SfBufferAttr_HipcMapAlias }, + .buffers = { { other_name, strlen(other_name) + 1 } }, + .in_num_handles = 1, + .in_handles = { g_tmem.handle }, + ); +} + +static Result _capmtpClose() { + return _capmtpNoIO(1); +} + +Result capmtpStartCommandHandler(void) { + return _capmtpNoIO(2); +} + +Result capmtpStopCommandHandler(void) { + return _capmtpNoIO(3); +} + +Event *capmtpGetEvent1(void) { + return &g_event1; +} + +Event *capmtpGetEvent2(void) { + return &g_event2; +} + +Result capmtpIsRunning(bool *out) { + return _capmtpNoInBoolOut(4, out); +} + +Result capmtpUnkBool(bool *out) { + return _capmtpNoInBoolOut(6, out); +} + +Result capmtpGetResult(void) { + return _capmtpNoIO(8); +}