2019-07-18 04:04:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-10-20 01:42:53 +01:00
|
|
|
#define NX_SERVICE_ASSUME_NON_DOMAIN
|
|
|
|
#include "../service_guard.h"
|
2019-07-18 04:04:00 +01:00
|
|
|
#include "sm_ams.h"
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
static Result _smAtmosphereCmdHas(bool *out, SmServiceName name, u32 cmd_id) {
|
2019-10-20 01:42:53 +01:00
|
|
|
u8 tmp;
|
2019-10-23 08:07:20 +01:00
|
|
|
Result rc = serviceDispatchInOut(smGetServiceSession(), cmd_id, name, tmp);
|
2019-10-20 01:42:53 +01:00
|
|
|
if (R_SUCCEEDED(rc) && out) *out = tmp & 1;
|
2019-07-18 04:04:00 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
static Result _smAtmosphereCmdInServiceNameNoOut(SmServiceName name, Service *srv, u32 cmd_id) {
|
|
|
|
return serviceDispatchIn(srv, cmd_id, name);
|
2019-10-20 01:42:53 +01:00
|
|
|
}
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereHasService(bool *out, SmServiceName name) {
|
|
|
|
return _smAtmosphereCmdHas(out, name, 65100);
|
2019-10-20 01:42:53 +01:00
|
|
|
}
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereWaitService(SmServiceName name) {
|
|
|
|
return _smAtmosphereCmdInServiceNameNoOut(name, smGetServiceSession(), 65101);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereHasMitm(bool *out, SmServiceName name) {
|
|
|
|
return _smAtmosphereCmdHas(out, name, 65004);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereWaitMitm(SmServiceName name) {
|
|
|
|
return _smAtmosphereCmdInServiceNameNoOut(name, smGetServiceSession(), 65005);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:53 +01:00
|
|
|
static Service g_smAtmosphereMitmSrv;
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-20 01:42:53 +01:00
|
|
|
NX_GENERATE_SERVICE_GUARD(smAtmosphereMitm);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-20 01:42:53 +01:00
|
|
|
Result _smAtmosphereMitmInitialize(void) {
|
2019-07-18 04:04:00 +01:00
|
|
|
Handle sm_handle;
|
|
|
|
Result rc = svcConnectToNamedPort(&sm_handle, "sm:");
|
|
|
|
while (R_VALUE(rc) == KERNELRESULT(NotFound)) {
|
|
|
|
svcSleepThread(50000000ul);
|
|
|
|
rc = svcConnectToNamedPort(&sm_handle, "sm:");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-10-20 01:42:53 +01:00
|
|
|
serviceCreate(&g_smAtmosphereMitmSrv, sm_handle);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-10-20 01:42:53 +01:00
|
|
|
const u64 pid_placeholder = 0;
|
|
|
|
rc = serviceDispatchIn(&g_smAtmosphereMitmSrv, 0, pid_placeholder, .in_send_pid = true);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:53 +01:00
|
|
|
void _smAtmosphereMitmCleanup(void) {
|
|
|
|
serviceClose(&g_smAtmosphereMitmSrv);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:53 +01:00
|
|
|
Service* smAtmosphereMitmGetServiceSession(void) {
|
|
|
|
return &g_smAtmosphereMitmSrv;
|
|
|
|
}
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereMitmInstall(Handle *handle_out, Handle *query_out, SmServiceName name) {
|
2019-10-20 01:42:53 +01:00
|
|
|
Handle tmp_handles[2];
|
2019-10-23 08:07:20 +01:00
|
|
|
Result rc = serviceDispatchIn(&g_smAtmosphereMitmSrv, 65000, name,
|
2019-10-20 01:42:53 +01:00
|
|
|
.out_handle_attrs = { SfOutHandleAttr_HipcMove, SfOutHandleAttr_HipcMove },
|
|
|
|
.out_handles = tmp_handles,
|
|
|
|
);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-10-20 01:42:53 +01:00
|
|
|
*handle_out = tmp_handles[0];
|
|
|
|
*query_out = tmp_handles[1];
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereMitmUninstall(SmServiceName name) {
|
|
|
|
return _smAtmosphereCmdInServiceNameNoOut(name, &g_smAtmosphereMitmSrv, 65001);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereMitmDeclareFuture(SmServiceName name) {
|
|
|
|
return _smAtmosphereCmdInServiceNameNoOut(name, &g_smAtmosphereMitmSrv, 65006);
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, u64 *tid_out, SmServiceName name) {
|
2019-07-18 04:04:00 +01:00
|
|
|
struct {
|
2019-10-20 01:42:53 +01:00
|
|
|
u64 pid;
|
|
|
|
u64 tid;
|
|
|
|
} out;
|
2019-07-18 04:04:00 +01:00
|
|
|
|
2019-10-23 08:07:20 +01:00
|
|
|
Result rc = serviceDispatchInOut(&g_smAtmosphereMitmSrv, 65003, name, out,
|
2019-10-20 01:42:53 +01:00
|
|
|
.out_num_objects = 1,
|
|
|
|
.out_objects = srv_out,
|
|
|
|
);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
2019-10-20 01:42:53 +01:00
|
|
|
if (pid_out) *pid_out = out.pid;
|
|
|
|
if (tid_out) *tid_out = out.tid;
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|