rosalina/sm: properly interact with ndm

This commit is contained in:
TuxSH 2020-07-15 18:57:53 +01:00
parent ba26ae0f1c
commit e096aaabc4
5 changed files with 22 additions and 47 deletions

View File

@ -16,27 +16,12 @@ static u32 socContextAddr = 0x08000000;
static u32 socContextSize = 0x60000;
static Handle miniSocHandle;
static Handle miniSocMemHandle;
static bool exclusiveStateEntered = false;
bool miniSocEnabled = false;
s32 _net_convert_error(s32 sock_retval);
// To prevent ndm:u from disconnecting us
static Result srvExtAddToNdmuWorkaroundCount(s32 count)
{
Result ret = 0;
u32 *cmdbuf = getThreadCommandBuffer();
cmdbuf[0] = IPC_MakeHeader(0x1000,1,0);
cmdbuf[1] = (u32)count;
ret = svcSendSyncRequest(*srvGetSessionHandle());
if(ret != 0)
return ret;
return cmdbuf[1];
}
static Result SOCU_Initialize(Handle memhandle, u32 memsize)
{
Result ret = 0;
@ -102,9 +87,18 @@ Result miniSocInit(void)
ret = SOCU_Initialize(miniSocMemHandle, socContextSize);
if(ret != 0) goto cleanup;
if (!exclusiveStateEntered)
{
ndmuInit();
ret = NDMU_EnterExclusiveState(NDM_EXCLUSIVE_STATE_INFRASTRUCTURE);
if (R_SUCCEEDED(ret))
ret = NDMU_LockState(); // prevents ndm from switching to StreetPass when the lid is closed
//ndmuExit();
exclusiveStateEntered = R_SUCCEEDED(ret);
}
svcKernelSetState(0x10000, 0x10);
miniSocEnabled = true;
srvExtAddToNdmuWorkaroundCount(1);
return 0;
@ -132,7 +126,6 @@ cleanup:
Result miniSocExitDirect(void)
{
//if (miniSocRefCount != 0) __builtin_trap();
Result ret = 0;
u32 tmp;
@ -147,8 +140,17 @@ Result miniSocExitDirect(void)
svcControlMemory(&tmp, socContextAddr, socContextAddr, socContextSize, MEMOP_FREE, MEMPERM_DONTCARE);
if(ret == 0)
{
if (exclusiveStateEntered)
{
//ndmuInit();
ret = NDMU_UnlockState();
if (R_SUCCEEDED(ret))
ret = NDMU_LeaveExclusiveState();
ndmuExit();
exclusiveStateEntered = R_FAILED(ret);
}
miniSocEnabled = false;
srvExtAddToNdmuWorkaroundCount(-1);
svcKernelSetState(0x10000, 0x10);
}
return ret;

View File

@ -17,7 +17,6 @@ This is part of 3ds_sm, which is licensed under the MIT license (see LICENSE for
extern u32 nbSection0Modules;
extern Handle resumeGetServiceHandleOrPortRegisteredSemaphore;
extern u32 ndmuServicePid;
struct SessionDataList;

View File

@ -10,17 +10,11 @@ This is part of 3ds_sm, which is licensed under the MIT license (see LICENSE for
#include <stdatomic.h>
static atomic_int ndmuWorkaroundCount;
static bool isNotificationInhibited(const ProcessData *processData, u32 notificationId)
{
u32 pid = processData->pid;
(void)processData;
switch(notificationId)
{
// Shell opened, shell closed
case 0x213:
case 0x214:
return pid == ndmuServicePid && atomic_load(&ndmuWorkaroundCount) > 0;
default:
return false;
}
@ -207,10 +201,3 @@ Result PublishToAll(u32 notificationId)
return 0;
}
Result AddToNdmuWorkaroundCount(s32 count)
{
// Note: no check is made to (current value)+count
atomic_fetch_add(&ndmuWorkaroundCount, count);
return 0;
}

View File

@ -13,8 +13,6 @@ This is part of 3ds_sm, which is licensed under the MIT license (see LICENSE for
ServiceInfo servicesInfo[0xA0] = { 0 };
u32 nbServices = 0; // including "ports" registered with getPort
u32 ndmuServicePid = 3; // use our PID as default.
static Result checkServiceName(const char *name, s32 nameSize)
{
if(nameSize <= 0 || nameSize > 8)
@ -98,9 +96,6 @@ static Result doRegisterServiceOrPort(u32 pid, Handle *serverPort, Handle client
if(!isNamedPort)
*serverPort = portServer;
if(R_SUCCEEDED(res) && strcmp(name, "ndm:u") == 0)
ndmuServicePid = pid;
return res;
}

View File

@ -167,14 +167,6 @@ Result srvHandleCommands(SessionData *sessionData)
break;
}
case 0x1000: // Custom command: AddToNdmuWorkaroundCount
{
res = AddToNdmuWorkaroundCount((s32)cmdbuf[1]);
cmdbuf[0] = IPC_MakeHeader(0x1000, 1, 0);;
cmdbuf[1] = (u32)res;
break;
}
default:
goto invalid_command;
break;