Change nifm initialization mode and add SetWirelessCommunicationEnabled (#247)

* Init nifm in the proper context, add SetWirelessCommunicationEnabled
* add nifmSetServiceType to set which nifm service to init
This commit is contained in:
exelix 2019-03-15 22:11:40 +01:00 committed by yellows8
parent a7facade0a
commit 093471609d
2 changed files with 78 additions and 4 deletions

View File

@ -1,7 +1,7 @@
/** /**
* @file nifm.h * @file nifm.h
* @brief Network interface service IPC wrapper. * @brief Network interface service IPC wrapper.
* @author shadowninja108, shibboleet * @author shadowninja108, shibboleet, exelix
* @copyright libnx Authors * @copyright libnx Authors
*/ */
@ -9,6 +9,19 @@
#include "../kernel/ipc.h" #include "../kernel/ipc.h"
#include "../services/sm.h" #include "../services/sm.h"
typedef enum {
NifmServiceType_NotInitialized = 0, ///< Initializes nifm:u.
NifmServiceType_User = 1, ///< Initializes nifm:u.
NifmServiceType_System = 2, ///< Initializes nifm:s.
NifmServiceType_Admin = 3, ///< Initializes nifm:a.
} NifmServiceType;
/**
* @brief Sets the \ref NifmServiceType for initialization. Call this function before \ref nifmInitialize.
* @note By default nifm:u will be used.
*/
void nifmSetServiceType(NifmServiceType serviceType);
Result nifmInitialize(void); Result nifmInitialize(void);
void nifmExit(void); void nifmExit(void);
@ -16,6 +29,11 @@ Result nifmGetCurrentIpAddress(u32* out);
Result nifmIsWirelessCommunicationEnabled(bool* out); Result nifmIsWirelessCommunicationEnabled(bool* out);
/**
* @note Works only if called from nifm:a or nifm:s.
*/
Result nifmSetWirelessCommunicationEnabled(bool enable);
Result nifmIsEthernetCommunicationEnabled(bool* out); Result nifmIsEthernetCommunicationEnabled(bool* out);
Result nifmIsAnyForegroundRequestAccepted(bool* out); Result nifmIsAnyForegroundRequestAccepted(bool* out);
Result nifmPutToSleep(void); Result nifmPutToSleep(void);

View File

@ -9,6 +9,8 @@
#include "arm/atomics.h" #include "arm/atomics.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
static NifmServiceType g_nifmServiceType = NifmServiceType_NotInitialized;
static Service g_nifmSrv; static Service g_nifmSrv;
static Service g_nifmIGS; static Service g_nifmIGS;
static u64 g_refCnt; static u64 g_refCnt;
@ -16,14 +18,30 @@ static u64 g_refCnt;
static Result _nifmCreateGeneralService(Service* out, u64 in); static Result _nifmCreateGeneralService(Service* out, u64 in);
static Result _nifmCreateGeneralServiceOld(Service* out); static Result _nifmCreateGeneralServiceOld(Service* out);
void nifmSetServiceType(NifmServiceType serviceType) {
g_nifmServiceType = serviceType;
}
Result nifmInitialize(void) { Result nifmInitialize(void) {
atomicIncrement64(&g_refCnt); atomicIncrement64(&g_refCnt);
if (serviceIsActive(&g_nifmSrv)) if (serviceIsActive(&g_nifmSrv))
return 0; return 0;
Result rc; Result rc = 0;
switch (g_nifmServiceType) {
case NifmServiceType_NotInitialized:
case NifmServiceType_User:
g_nifmServiceType = NifmServiceType_User;
rc = smGetService(&g_nifmSrv, "nifm:u"); rc = smGetService(&g_nifmSrv, "nifm:u");
break;
case NifmServiceType_System:
rc = smGetService(&g_nifmSrv, "nifm:s");
break;
case NifmServiceType_Admin:
rc = smGetService(&g_nifmSrv, "nifm:a");
break;
}
if (R_SUCCEEDED(rc)) rc = serviceConvertToDomain(&g_nifmSrv); if (R_SUCCEEDED(rc)) rc = serviceConvertToDomain(&g_nifmSrv);
@ -44,6 +62,7 @@ void nifmExit(void) {
if (atomicDecrement64(&g_refCnt) == 0) { if (atomicDecrement64(&g_refCnt) == 0) {
serviceClose(&g_nifmIGS); serviceClose(&g_nifmIGS);
serviceClose(&g_nifmSrv); serviceClose(&g_nifmSrv);
g_nifmServiceType = NifmServiceType_NotInitialized;
} }
} }
@ -119,6 +138,43 @@ Result nifmIsWirelessCommunicationEnabled(bool* out) {
return rc; return rc;
} }
Result nifmSetWirelessCommunicationEnabled(bool enable) {
if (g_nifmServiceType < NifmServiceType_System)
return MAKERESULT(Module_Libnx, LibnxError_NotInitialized);
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
u8 value;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 16;
raw->value = enable!= 0;
Result rc = serviceIpcDispatch(&g_nifmIGS);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
} *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
}
return rc;
}
Result nifmIsEthernetCommunicationEnabled(bool* out) { Result nifmIsEthernetCommunicationEnabled(bool* out) {
IpcCommand c; IpcCommand c;
ipcInitialize(&c); ipcInitialize(&c);