2019-10-18 18:47:50 -04:00
|
|
|
using Ryujinx.Common;
|
2018-10-17 13:15:50 -04:00
|
|
|
using Ryujinx.Common.Logging;
|
2019-09-18 20:45:11 -04:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nifm.StaticService.GeneralService;
|
2019-10-18 18:47:50 -04:00
|
|
|
using Ryujinx.HLE.HOS.Services.Nifm.StaticService.Types;
|
2018-07-18 21:06:45 -04:00
|
|
|
using System;
|
|
|
|
using System.Net.NetworkInformation;
|
2018-08-16 19:47:36 -04:00
|
|
|
|
2019-09-18 20:45:11 -04:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService
|
2018-02-27 22:31:52 -05:00
|
|
|
{
|
2019-09-04 12:09:20 -04:00
|
|
|
class IGeneralService : IpcService, IDisposable
|
2018-02-27 22:31:52 -05:00
|
|
|
{
|
2019-09-04 12:09:20 -04:00
|
|
|
private GeneralServiceDetail _generalServiceDetail;
|
|
|
|
|
|
|
|
public IGeneralService()
|
|
|
|
{
|
|
|
|
_generalServiceDetail = new GeneralServiceDetail
|
|
|
|
{
|
|
|
|
ClientId = GeneralServiceManager.Count,
|
|
|
|
IsAnyInternetRequestAccepted = true // NOTE: Why not accept any internet request?
|
|
|
|
};
|
|
|
|
|
|
|
|
GeneralServiceManager.Add(_generalServiceDetail);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Command(1)]
|
|
|
|
// GetClientId() -> buffer<nn::nifm::ClientId, 0x1a, 4>
|
|
|
|
public ResultCode GetClientId(ServiceCtx context)
|
|
|
|
{
|
|
|
|
long position = context.Request.RecvListBuff[0].Position;
|
2021-02-19 14:18:13 -05:00
|
|
|
|
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(4);
|
2019-09-04 12:09:20 -04:00
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
context.Memory.Write((ulong)position, _generalServiceDetail.ClientId);
|
2019-09-04 12:09:20 -04:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
2018-02-27 22:31:52 -05:00
|
|
|
|
2019-07-11 21:13:43 -04:00
|
|
|
[Command(4)]
|
2019-09-04 12:09:20 -04:00
|
|
|
// CreateRequest(u32 version) -> object<nn::nifm::detail::IRequest>
|
2019-07-14 15:04:38 -04:00
|
|
|
public ResultCode CreateRequest(ServiceCtx context)
|
2018-02-27 22:31:52 -05:00
|
|
|
{
|
2019-09-04 12:09:20 -04:00
|
|
|
uint version = context.RequestData.ReadUInt32();
|
2018-02-27 22:31:52 -05:00
|
|
|
|
2019-09-04 12:09:20 -04:00
|
|
|
MakeObject(context, new IRequest(context.Device.System, version));
|
2018-02-27 22:31:52 -05:00
|
|
|
|
2019-09-04 12:09:20 -04:00
|
|
|
// Doesn't occur in our case.
|
|
|
|
// return ResultCode.ObjectIsNull;
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
Logger.Stub?.PrintStub(LogClass.ServiceNifm, new { version });
|
2018-02-27 22:31:52 -05:00
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
return ResultCode.Success;
|
2018-02-27 22:31:52 -05:00
|
|
|
}
|
2018-07-18 21:06:45 -04:00
|
|
|
|
2019-07-11 21:13:43 -04:00
|
|
|
[Command(12)]
|
|
|
|
// GetCurrentIpAddress() -> nn::nifm::IpV4Address
|
2019-07-14 15:04:38 -04:00
|
|
|
public ResultCode GetCurrentIpAddress(ServiceCtx context)
|
2018-07-18 21:06:45 -04:00
|
|
|
{
|
2020-02-17 10:28:41 -05:00
|
|
|
(_, UnicastIPAddressInformation unicastAddress) = GetLocalInterface();
|
|
|
|
|
|
|
|
if (unicastAddress == null)
|
2018-07-18 21:06:45 -04:00
|
|
|
{
|
2019-07-14 15:04:38 -04:00
|
|
|
return ResultCode.NoInternetConnection;
|
2018-07-18 21:06:45 -04:00
|
|
|
}
|
|
|
|
|
2020-02-17 10:28:41 -05:00
|
|
|
context.ResponseData.WriteStruct(new IpV4Address(unicastAddress.Address));
|
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
2020-02-17 10:28:41 -05:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Command(15)]
|
|
|
|
// GetCurrentIpConfigInfo() -> (nn::nifm::IpAddressSetting, nn::nifm::DnsSetting)
|
|
|
|
public ResultCode GetCurrentIpConfigInfo(ServiceCtx context)
|
|
|
|
{
|
|
|
|
(IPInterfaceProperties interfaceProperties, UnicastIPAddressInformation unicastAddress) = GetLocalInterface();
|
2018-08-16 19:47:36 -04:00
|
|
|
|
2020-02-17 10:28:41 -05:00
|
|
|
if (interfaceProperties == null)
|
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
2018-07-18 21:06:45 -04:00
|
|
|
|
2020-08-03 19:32:53 -04:00
|
|
|
Logger.Info?.Print(LogClass.ServiceNifm, $"Console's local IP is \"{unicastAddress.Address}\".");
|
2018-07-18 21:06:45 -04:00
|
|
|
|
2020-02-17 10:28:41 -05:00
|
|
|
context.ResponseData.WriteStruct(new IpAddressSetting(interfaceProperties, unicastAddress));
|
|
|
|
context.ResponseData.WriteStruct(new DnsSetting(interfaceProperties));
|
2018-07-18 21:06:45 -04:00
|
|
|
|
2019-07-14 15:04:38 -04:00
|
|
|
return ResultCode.Success;
|
2018-07-18 21:06:45 -04:00
|
|
|
}
|
2019-09-04 12:09:20 -04:00
|
|
|
|
2019-10-18 18:47:50 -04:00
|
|
|
[Command(18)]
|
|
|
|
// GetInternetConnectionStatus() -> nn::nifm::detail::sf::InternetConnectionStatus
|
|
|
|
public ResultCode GetInternetConnectionStatus(ServiceCtx context)
|
|
|
|
{
|
|
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
|
|
{
|
|
|
|
return ResultCode.NoInternetConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
InternetConnectionStatus internetConnectionStatus = new InternetConnectionStatus
|
|
|
|
{
|
|
|
|
Type = InternetConnectionType.WiFi,
|
|
|
|
WifiStrength = 3,
|
|
|
|
State = InternetConnectionState.Connected,
|
|
|
|
};
|
|
|
|
|
|
|
|
context.ResponseData.WriteStruct(internetConnectionStatus);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:09:20 -04:00
|
|
|
[Command(21)]
|
|
|
|
// IsAnyInternetRequestAccepted(buffer<nn::nifm::ClientId, 0x19, 4>) -> bool
|
|
|
|
public ResultCode IsAnyInternetRequestAccepted(ServiceCtx context)
|
|
|
|
{
|
|
|
|
long position = context.Request.PtrBuff[0].Position;
|
|
|
|
long size = context.Request.PtrBuff[0].Size;
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
int clientId = context.Memory.Read<int>((ulong)position);
|
2019-09-04 12:09:20 -04:00
|
|
|
|
|
|
|
context.ResponseData.Write(GeneralServiceManager.Get(clientId).IsAnyInternetRequestAccepted);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2020-02-17 10:28:41 -05:00
|
|
|
private (IPInterfaceProperties, UnicastIPAddressInformation) GetLocalInterface()
|
|
|
|
{
|
|
|
|
if (!NetworkInterface.GetIsNetworkAvailable())
|
|
|
|
{
|
|
|
|
return (null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
IPInterfaceProperties targetProperties = null;
|
|
|
|
UnicastIPAddressInformation targetAddressInfo = null;
|
|
|
|
|
|
|
|
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
|
|
|
|
|
|
|
foreach (NetworkInterface adapter in interfaces)
|
|
|
|
{
|
|
|
|
// Ignore loopback and non IPv4 capable interface.
|
|
|
|
if (adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback && adapter.Supports(NetworkInterfaceComponent.IPv4))
|
|
|
|
{
|
|
|
|
IPInterfaceProperties properties = adapter.GetIPProperties();
|
|
|
|
|
|
|
|
if (properties.GatewayAddresses.Count > 0 && properties.DnsAddresses.Count > 1)
|
|
|
|
{
|
|
|
|
foreach (UnicastIPAddressInformation info in properties.UnicastAddresses)
|
|
|
|
{
|
|
|
|
// Only accept an IPv4 address
|
|
|
|
if (info.Address.GetAddressBytes().Length == 4)
|
|
|
|
{
|
|
|
|
targetProperties = properties;
|
|
|
|
targetAddressInfo = info;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found the target interface, stop here.
|
|
|
|
if (targetProperties != null)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (targetProperties, targetAddressInfo);
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:09:20 -04:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
GeneralServiceManager.Remove(_generalServiceDetail.ClientId);
|
|
|
|
}
|
2018-02-27 22:31:52 -05:00
|
|
|
}
|
2019-07-11 21:13:43 -04:00
|
|
|
}
|