nifm: Updated for new-ipc. Added nifmGetServiceSession_StaticService and nifmGetServiceSession_GeneralService. Fixed order of cmds and improved docs. Improved _nifmCreateGeneralService.

This commit is contained in:
yellows8 2019-10-05 12:38:42 -04:00
parent be2cb63722
commit 75795933a7
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43
2 changed files with 105 additions and 375 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, exelix * @author shadowninja108, shibboleet, exelix, yellows8
* @copyright libnx Authors * @copyright libnx Authors
*/ */
@ -35,18 +35,27 @@ typedef enum {
*/ */
void nifmSetServiceType(NifmServiceType serviceType); void nifmSetServiceType(NifmServiceType serviceType);
/// Initialize nifm. This is used automatically by \ref socketInitialize.
Result nifmInitialize(void); Result nifmInitialize(void);
/// Exit nifm. This is used automatically by \ref socketExit.
void nifmExit(void); void nifmExit(void);
Result nifmGetCurrentIpAddress(u32* out); /// Gets the Service object for the actual nifm:* service session.
Service* nifmGetServiceSession_StaticService(void);
Result nifmIsWirelessCommunicationEnabled(bool* out); /// Gets the Service object for IGeneralService.
Service* nifmGetServiceSession_GeneralService(void);
Result nifmGetCurrentIpAddress(u32* out);
/** /**
* @note Works only if called from nifm:a or nifm:s. * @note Works only if called from nifm:a or nifm:s.
*/ */
Result nifmSetWirelessCommunicationEnabled(bool enable); Result nifmSetWirelessCommunicationEnabled(bool enable);
Result nifmIsWirelessCommunicationEnabled(bool* out);
/** /**
* @note Will fail with 0xd46ed if Internet is neither connecting or connected (airplane mode or no known network in reach). * @note Will fail with 0xd46ed if Internet is neither connecting or connected (airplane mode or no known network in reach).
* @param wifiStrength Strength of the Wi-Fi signal in number of bars from 0 to 3. * @param wifiStrength Strength of the Wi-Fi signal in number of bars from 0 to 3.

View File

@ -1,33 +1,22 @@
/** #include "service_guard.h"
* @file nifm.c
* @brief Network interface service IPC wrapper.
* @author shadowninja108, shibboleet
* @copyright libnx Authors
*/
#include "services/nifm.h" #include "services/nifm.h"
#include "arm/atomics.h"
#include "runtime/hosversion.h" #include "runtime/hosversion.h"
static NifmServiceType g_nifmServiceType = NifmServiceType_NotInitialized; 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 Result _nifmCreateGeneralService(Service* out, u64 in); static Result _nifmCreateGeneralService(Service* srv_out);
static Result _nifmCreateGeneralServiceOld(Service* out); static Result _nifmCreateGeneralServiceOld(Service* srv_out);
NX_GENERATE_SERVICE_GUARD(nifm);
void nifmSetServiceType(NifmServiceType serviceType) { void nifmSetServiceType(NifmServiceType serviceType) {
g_nifmServiceType = serviceType; g_nifmServiceType = serviceType;
} }
Result nifmInitialize(void) { Result _nifmInitialize(void) {
atomicIncrement64(&g_refCnt);
if (serviceIsActive(&g_nifmSrv))
return 0;
Result rc = 0; Result rc = 0;
switch (g_nifmServiceType) { switch (g_nifmServiceType) {
case NifmServiceType_NotInitialized: case NifmServiceType_NotInitialized:
@ -47,393 +36,125 @@ Result nifmInitialize(void) {
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
if (hosversionAtLeast(3,0,0)) if (hosversionAtLeast(3,0,0))
rc = _nifmCreateGeneralService(&g_nifmIGS, 0); // What does this parameter do? rc = _nifmCreateGeneralService(&g_nifmIGS);
else else
rc = _nifmCreateGeneralServiceOld(&g_nifmIGS); rc = _nifmCreateGeneralServiceOld(&g_nifmIGS);
} }
if (R_FAILED(rc))
nifmExit();
return rc; return rc;
} }
void nifmExit(void) { void _nifmCleanup(void) {
if (atomicDecrement64(&g_refCnt) == 0) { serviceClose(&g_nifmIGS);
serviceClose(&g_nifmIGS); serviceClose(&g_nifmSrv);
serviceClose(&g_nifmSrv); g_nifmServiceType = NifmServiceType_NotInitialized;
g_nifmServiceType = NifmServiceType_NotInitialized; }
}
Service* nifmGetServiceSession_StaticService(void) {
return &g_nifmSrv;
}
Service* nifmGetServiceSession_GeneralService(void) {
return &g_nifmIGS;
}
static Result _nifmCmdNoIO(Service* srv, u32 cmd_id) {
serviceAssumeDomain(srv);
return serviceDispatch(srv, cmd_id);
}
static Result _nifmCmdGetSession(Service* srv, Service* srv_out, u32 cmd_id) {
serviceAssumeDomain(srv);
return serviceDispatch(srv, cmd_id,
.out_num_objects = 1,
.out_objects = srv_out,
);
}
static Result _nifmCmdNoInOutU32(Service* srv, u32 *out, u32 cmd_id) {
serviceAssumeDomain(srv);
return serviceDispatchOut(srv, cmd_id, *out);
}
static Result _nifmCmdNoInOutU8(Service* srv, u8 *out, u32 cmd_id) {
serviceAssumeDomain(srv);
return serviceDispatchOut(srv, cmd_id, *out);
}
static Result _nifmCmdNoInOutBool(Service* srv, bool *out, u32 cmd_id) {
u8 tmp=0;
Result rc = _nifmCmdNoInOutU8(srv, &tmp, cmd_id);
if (R_SUCCEEDED(rc) && out) *out = tmp!=0;
return rc;
}
static Result _nifmCmdInU8NoOut(Service* srv, u8 inval, u64 cmd_id) {
serviceAssumeDomain(srv);
return serviceDispatchIn(srv, cmd_id, inval);
}
static Result _nifmCmdInBoolNoOut(Service* srv, bool inval, u32 cmd_id) {
return _nifmCmdInU8NoOut(srv, inval!=0, cmd_id);
}
static Result _nifmCreateGeneralServiceOld(Service* srv_out) {
return _nifmCmdGetSession(&g_nifmSrv, srv_out, 4);
}
static Result _nifmCreateGeneralService(Service* srv_out) {
u64 reserved=0;
serviceAssumeDomain(&g_nifmSrv);
return serviceDispatchIn(&g_nifmSrv, 5, reserved,
.in_send_pid = true,
.out_num_objects = 1,
.out_objects = srv_out,
);
} }
Result nifmGetCurrentIpAddress(u32* out) { Result nifmGetCurrentIpAddress(u32* out) {
IpcCommand c; return _nifmCmdNoInOutU32(&g_nifmIGS, out, 12);
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 12;
Result rc = serviceIpcDispatch(&g_nifmIGS);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
u32 out;
} *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
*out = resp->out;
}
return rc;
}
Result nifmIsWirelessCommunicationEnabled(bool* out) {
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 17;
Result rc = serviceIpcDispatch(&g_nifmIGS);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
u8 out;
} *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc) && out)
*out = resp->out != 0;
}
return rc;
} }
Result nifmSetWirelessCommunicationEnabled(bool enable) { Result nifmSetWirelessCommunicationEnabled(bool enable) {
if (g_nifmServiceType < NifmServiceType_System) if (g_nifmServiceType < NifmServiceType_System)
return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); return MAKERESULT(Module_Libnx, LibnxError_NotInitialized);
IpcCommand c; return _nifmCmdInBoolNoOut(&g_nifmIGS, enable, 16);
ipcInitialize(&c); }
Result nifmIsWirelessCommunicationEnabled(bool* out) {
return _nifmCmdNoInOutBool(&g_nifmIGS, out, 17);
}
Result nifmGetInternetConnectionStatus(NifmInternetConnectionType* connectionType, u32* wifiStrength, NifmInternetConnectionStatus* connectionStatus) {
struct { struct {
u64 magic; u8 out1;
u64 cmd_id; u8 out2;
u8 value; u8 out3;
} *raw; } out;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 16;
raw->value = enable!= 0;
Result rc = serviceIpcDispatch(&g_nifmIGS);
serviceAssumeDomain(&g_nifmIGS);
Result rc = serviceDispatchOut(&g_nifmIGS, 18, out);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
IpcParsedCommand r; if (connectionType) *connectionType = out.out1;
struct { if (wifiStrength) *wifiStrength = out.out2;
u64 magic; if (connectionStatus) *connectionStatus = out.out3;
u64 result;
} *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
} }
return rc; return rc;
} }
Result nifmIsEthernetCommunicationEnabled(bool* out) { Result nifmIsEthernetCommunicationEnabled(bool* out) {
IpcCommand c; return _nifmCmdNoInOutBool(&g_nifmIGS, out, 20);
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 20;
Result rc = serviceIpcDispatch(&g_nifmIGS);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
u8 out;
} *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc) && out)
*out = resp->out != 0;
}
return rc;
} }
Result nifmIsAnyForegroundRequestAccepted(bool* out) { Result nifmIsAnyForegroundRequestAccepted(bool* out) {
IpcCommand c; return _nifmCmdNoInOutBool(&g_nifmIGS, out, 22);
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 22;
Result rc = serviceIpcDispatch(&g_nifmIGS);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
u8 out;
} *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc) && out)
*out = resp->out != 0;
}
return rc;
} }
Result nifmPutToSleep(void) { Result nifmPutToSleep(void) {
IpcCommand c; return _nifmCmdNoIO(&g_nifmIGS, 23);
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 23;
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 nifmWakeUp(void) { Result nifmWakeUp(void) {
IpcCommand c; return _nifmCmdNoIO(&g_nifmIGS, 24);
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 24;
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 nifmGetInternetConnectionStatus(NifmInternetConnectionType* connectionType, u32* wifiStrength, NifmInternetConnectionStatus* connectionStatus)
{
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} *raw;
raw = serviceIpcPrepareHeader(&g_nifmIGS, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 18;
Result rc = serviceIpcDispatch(&g_nifmIGS);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
u8 out1;
u8 out2;
u8 out3;
} PACKED *resp;
serviceIpcParse(&g_nifmIGS, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc)) {
if (connectionType)
*connectionType = resp->out1;
if (wifiStrength)
*wifiStrength = resp->out2;
if (connectionStatus)
*connectionStatus = resp->out3;
}
}
return rc;
}
static Result _nifmCreateGeneralService(Service* out, u64 in) {
IpcCommand c;
ipcInitialize(&c);
ipcSendPid(&c);
struct {
u64 magic;
u64 cmd_id;
u64 param;
} PACKED *raw;
raw = serviceIpcPrepareHeader(&g_nifmSrv, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 5;
raw->param = in;
Result rc = serviceIpcDispatch(&g_nifmSrv);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
} *resp;
serviceIpcParse(&g_nifmSrv, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc))
serviceCreateSubservice(out, &g_nifmSrv, &r, 0);
}
return rc;
}
static Result _nifmCreateGeneralServiceOld(Service* out) {
IpcCommand c;
ipcInitialize(&c);
struct {
u64 magic;
u64 cmd_id;
} PACKED *raw;
raw = serviceIpcPrepareHeader(&g_nifmSrv, &c, sizeof(*raw));
raw->magic = SFCI_MAGIC;
raw->cmd_id = 4;
Result rc = serviceIpcDispatch(&g_nifmSrv);
if (R_SUCCEEDED(rc)) {
IpcParsedCommand r;
struct {
u64 magic;
u64 result;
} *resp;
serviceIpcParse(&g_nifmSrv, &r, sizeof(*resp));
resp = r.Raw;
rc = resp->result;
if (R_SUCCEEDED(rc))
serviceCreateSubservice(out, &g_nifmSrv, &r, 0);
}
return rc;
} }