From c8795b1b79e77b1ada88afca3d32a010f5708d49 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Sat, 1 Nov 2014 21:39:18 -0400 Subject: [PATCH] Added APT CheckNew3DS code. Added code for attempting to use the other APT services when APT:U isn't accessible. --- libctru/include/3ds/services/apt.h | 4 ++ libctru/source/services/apt.c | 70 +++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/libctru/include/3ds/services/apt.h b/libctru/include/3ds/services/apt.h index cf2fb31..3092f99 100644 --- a/libctru/include/3ds/services/apt.h +++ b/libctru/include/3ds/services/apt.h @@ -69,3 +69,7 @@ Result APT_PrepareToCloseApplication(Handle* handle, u8 a); Result APT_CloseApplication(Handle* handle, u32 a, u32 b, u32 c); Result APT_SetAppCpuTimeLimit(Handle* handle, u32 percent); Result APT_GetAppCpuTimeLimit(Handle* handle, u32 *percent); +Result APT_CheckNew3DS_Application(Handle* handle, u8 *out);//*Application and *System use APT commands 0x01010000 and 0x01020000. Using APT_CheckNew3DS() is recommended, this determines which of those two funcs to use automatically. +Result APT_CheckNew3DS_System(Handle* handle, u8 *out); +Result APT_CheckNew3DS(Handle* handle, u8 *out); + diff --git a/libctru/source/services/apt.c b/libctru/source/services/apt.c index 3b995a8..d13d15e 100644 --- a/libctru/source/services/apt.c +++ b/libctru/source/services/apt.c @@ -14,6 +14,9 @@ extern u32 __system_runflags; NS_APPID currentAppId; +static char *__apt_servicestr = NULL; +static char *__apt_servicenames[3] = {"APT:U", "APT:S", "APT:A"}; + Handle aptLockHandle; Handle aptuHandle; Handle aptEvents[3]; @@ -32,6 +35,29 @@ u32 aptParameters[0x1000/4]; //TEMP static void aptAppStarted(void); +static Result __apt_initservicehandle() +{ + Result ret=0; + u32 i; + + if(__apt_servicestr) + { + return srvGetServiceHandle(&aptuHandle, __apt_servicestr); + } + + for(i=0; i<3; i++) + { + ret = srvGetServiceHandle(&aptuHandle, __apt_servicenames[i]); + if(ret==0) + { + __apt_servicestr = __apt_servicenames[i]; + return ret; + } + } + + return ret; +} + void aptInitCaptureInfo(u32 *ns_capinfo) { u32 tmp=0; @@ -303,7 +329,8 @@ Result aptInit(void) Result ret=0; // Initialize APT stuff, escape load screen. - srvGetServiceHandle(&aptuHandle, "APT:U"); + ret = __apt_initservicehandle(); + if(ret!=0)return ret; if((ret=APT_GetLockHandle(&aptuHandle, 0x0, &aptLockHandle)))return ret; svcCloseHandle(aptuHandle); @@ -439,8 +466,10 @@ void aptSetStatusPower(u32 status) void aptOpenSession() { + //Result ret; + svcWaitSynchronization(aptLockHandle, U64_MAX); - srvGetServiceHandle(&aptuHandle, "APT:U"); + __apt_initservicehandle(); } void aptCloseSession() @@ -768,3 +797,40 @@ Result APT_GetAppCpuTimeLimit(Handle* handle, u32 *percent) return cmdbuf[1]; } + +Result APT_CheckNew3DS_Application(Handle* handle, u8 *out) +{ + if(!handle)handle=&aptuHandle; + + u32* cmdbuf=getThreadCommandBuffer(); + cmdbuf[0]=0x01010000; + + Result ret=0; + if((ret=svcSendSyncRequest(*handle)))return ret; + + if(out)*out=cmdbuf[2]; + + return cmdbuf[1]; +} + +Result APT_CheckNew3DS_System(Handle* handle, u8 *out) +{ + if(!handle)handle=&aptuHandle; + + u32* cmdbuf=getThreadCommandBuffer(); + cmdbuf[0]=0x01020000; + + Result ret=0; + if((ret=svcSendSyncRequest(*handle)))return ret; + + if(out)*out=cmdbuf[2]; + + return cmdbuf[1]; +} + +Result APT_CheckNew3DS(Handle* handle, u8 *out) +{ + if(currentAppId==APPID_APPLICATION)return APT_CheckNew3DS_Application(NULL, out); + return APT_CheckNew3DS_System(NULL, out); +} +