2021-03-18 21:40:20 +01:00
|
|
|
|
using Ryujinx.Common.Memory;
|
|
|
|
|
using Ryujinx.Cpu;
|
|
|
|
|
using Ryujinx.HLE.Exceptions;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid;
|
2019-09-19 02:45:11 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid.HidServer;
|
2021-06-24 01:05:40 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Nfc.Nfp.NfpManager;
|
2023-01-04 23:15:45 +01:00
|
|
|
|
using Ryujinx.Horizon.Common;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
using System;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp
|
|
|
|
|
{
|
2021-06-24 01:05:40 +02:00
|
|
|
|
class INfp : IpcService
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
private ulong _appletResourceUserId;
|
|
|
|
|
private ulong _mcuVersionData;
|
|
|
|
|
private byte[] _mcuData;
|
|
|
|
|
|
2019-06-28 13:08:23 +02:00
|
|
|
|
private State _state = State.NonInitialized;
|
|
|
|
|
|
|
|
|
|
private KEvent _availabilityChangeEvent;
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
private CancellationTokenSource _cancelTokenSource;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-06-24 01:05:40 +02:00
|
|
|
|
private NfpPermissionLevel _permissionLevel;
|
|
|
|
|
|
|
|
|
|
public INfp(NfpPermissionLevel permissionLevel)
|
|
|
|
|
{
|
|
|
|
|
_permissionLevel = permissionLevel;
|
|
|
|
|
}
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(0)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// Initialize(u64, u64, pid, buffer<unknown, 5>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Initialize(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
_appletResourceUserId = context.RequestData.ReadUInt64();
|
|
|
|
|
_mcuVersionData = context.RequestData.ReadUInt64();
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong inputPosition = context.Request.SendBuff[0].Position;
|
|
|
|
|
ulong inputSize = context.Request.SendBuff[0].Size;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
_mcuData = new byte[inputSize];
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Read(inputPosition, _mcuData);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
// TODO: The mcuData buffer seems to contains entries with a size of 0x40 bytes each. Usage of the data needs to be determined.
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
// TODO: Handle this in a controller class directly.
|
|
|
|
|
// Every functions which use the Handle call nn::hid::system::GetXcdHandleForNpadWithNfc().
|
|
|
|
|
NfpDevice devicePlayer1 = new NfpDevice
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2020-04-03 02:10:02 +02:00
|
|
|
|
NpadIdType = NpadIdType.Player1,
|
|
|
|
|
Handle = HidUtils.GetIndexFromNpadIdType(NpadIdType.Player1),
|
2021-03-18 21:40:20 +01:00
|
|
|
|
State = NfpDeviceState.Initialized
|
2019-06-28 13:08:23 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Device.System.NfpDevices.Add(devicePlayer1);
|
|
|
|
|
|
|
|
|
|
// TODO: It mounts 0x8000000000000020 save data and stores a random generate value inside. Usage of the data needs to be determined.
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
|
|
|
|
_state = State.Initialized;
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(1)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// Finalize()
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Finalize(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if (_state == State.Initialized)
|
|
|
|
|
{
|
|
|
|
|
if (_cancelTokenSource != null)
|
|
|
|
|
{
|
|
|
|
|
_cancelTokenSource.Cancel();
|
|
|
|
|
}
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
// NOTE: All events are destroyed here.
|
|
|
|
|
context.Device.System.NfpDevices.Clear();
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
_state = State.NonInitialized;
|
|
|
|
|
}
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(2)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// ListDevices() -> (u32, buffer<unknown, 0xa>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode ListDevices(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
|
|
|
|
if (context.Request.RecvListBuff.Count == 0)
|
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
return ResultCode.WrongArgument;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong outputPosition = context.Request.RecvListBuff[0].Position;
|
|
|
|
|
ulong outputSize = context.Request.RecvListBuff[0].Size;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.DeviceNotFound;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
|
|
|
|
|
|
|
|
|
|
if (CheckNfcIsEnabled() == ResultCode.Success)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Write(outputPosition + ((uint)i * sizeof(long)), (uint)context.Device.System.NfpDevices[i].Handle);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
}
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.ResponseData.Write(context.Device.System.NfpDevices.Count);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
context.ResponseData.Write(0);
|
|
|
|
|
}
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(3)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// StartDetection(bytes<8, 4>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode StartDetection(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
context.Device.System.NfpDevices[i].State = NfpDeviceState.SearchingForTag;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_cancelTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (_cancelTokenSource.Token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagFound)
|
|
|
|
|
{
|
|
|
|
|
context.Device.System.NfpDevices[i].SignalActivate();
|
2021-04-30 19:32:58 +02:00
|
|
|
|
Thread.Sleep(125); // NOTE: Simulate amiibo scanning delay.
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Device.System.NfpDevices[i].SignalDeactivate();
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, _cancelTokenSource.Token);
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(4)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// StopDetection(bytes<8, 4>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode StopDetection(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_cancelTokenSource != null)
|
|
|
|
|
{
|
|
|
|
|
_cancelTokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
context.Device.System.NfpDevices[i].State = NfpDeviceState.Initialized;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(5)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// Mount(bytes<8, 4>, u32, u32)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Mount(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 01:05:40 +02:00
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
DeviceType deviceType = (DeviceType)context.RequestData.ReadUInt32();
|
|
|
|
|
MountTarget mountTarget = (MountTarget)context.RequestData.ReadUInt32();
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
if (deviceType != 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.WrongArgument;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (((uint)mountTarget & 3) == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.WrongArgument;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Found how the MountTarget is handled.
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagFound)
|
|
|
|
|
{
|
|
|
|
|
// NOTE: This mount the amiibo data, which isn't needed in our case.
|
|
|
|
|
|
|
|
|
|
context.Device.System.NfpDevices[i].State = NfpDeviceState.TagMounted;
|
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(6)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// Unmount(bytes<8, 4>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Unmount(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// NOTE: This mount the amiibo data, which isn't needed in our case.
|
|
|
|
|
|
|
|
|
|
context.Device.System.NfpDevices[i].State = NfpDeviceState.TagFound;
|
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(7)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// OpenApplicationArea(bytes<8, 4>, u32)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode OpenApplicationArea(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint applicationAreaId = context.RequestData.ReadUInt32();
|
|
|
|
|
|
|
|
|
|
bool isOpened = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
|
|
|
|
isOpened = VirtualAmiibo.OpenApplicationArea(context.Device.System.NfpDevices[i].AmiiboId, applicationAreaId);
|
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isOpened)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.ApplicationAreaIsNull;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(8)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetApplicationArea(bytes<8, 4>) -> (u32, buffer<unknown, 6>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetApplicationArea(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
|
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
|
|
|
|
|
|
|
|
|
|
uint size = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
|
|
|
|
byte[] applicationArea = VirtualAmiibo.GetApplicationArea(context.Device.System.NfpDevices[i].AmiiboId);
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Write(outputPosition, applicationArea);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
size = (uint)applicationArea.Length;
|
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.ApplicationAreaIsNull;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.ResponseData.Write(size);
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(9)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// SetApplicationArea(bytes<8, 4>, buffer<unknown, 5>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode SetApplicationArea(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong inputPosition = context.Request.SendBuff[0].Position;
|
|
|
|
|
ulong inputSize = context.Request.SendBuff[0].Size;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
byte[] applicationArea = new byte[inputSize];
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Read(inputPosition, applicationArea);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
|
|
|
|
VirtualAmiibo.SetApplicationArea(context.Device.System.NfpDevices[i].AmiiboId, applicationArea);
|
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(10)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// Flush(bytes<8, 4>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Flush(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: Since we handle amiibo through VirtualAmiibo, we don't have to flush anything in our case.
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(11)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// Restore(bytes<8, 4>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Restore(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-08-11 21:16:42 +02:00
|
|
|
|
throw new ServiceNotImplementedException(this, context);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(12)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// CreateApplicationArea(bytes<8, 4>, u32, buffer<unknown, 5>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode CreateApplicationArea(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint applicationAreaId = context.RequestData.ReadUInt32();
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong inputPosition = context.Request.SendBuff[0].Position;
|
|
|
|
|
ulong inputSize = context.Request.SendBuff[0].Size;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
byte[] applicationArea = new byte[inputSize];
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Read(inputPosition, applicationArea);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
bool isCreated = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
|
|
|
|
isCreated = VirtualAmiibo.CreateApplicationArea(context.Device.System.NfpDevices[i].AmiiboId, applicationAreaId, applicationArea);
|
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isCreated)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.ApplicationAreaIsNull;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(13)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetTagInfo(bytes<8, 4>) -> buffer<unknown<0x58>, 0x1a>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetTagInfo(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.RecvListBuff.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.WrongArgument;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong outputPosition = context.Request.RecvListBuff[0].Position;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Marshal.SizeOf<TagInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf<TagInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted || context.Device.System.NfpDevices[i].State == NfpDeviceState.TagFound)
|
|
|
|
|
{
|
|
|
|
|
byte[] Uuid = VirtualAmiibo.GenerateUuid(context.Device.System.NfpDevices[i].AmiiboId, context.Device.System.NfpDevices[i].UseRandomUuid);
|
|
|
|
|
|
|
|
|
|
if (Uuid.Length > AmiiboConstants.UuidMaxLength)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TagInfo tagInfo = new TagInfo
|
|
|
|
|
{
|
|
|
|
|
UuidLength = (byte)Uuid.Length,
|
|
|
|
|
Reserved1 = new Array21<byte>(),
|
|
|
|
|
Protocol = uint.MaxValue, // All Protocol
|
|
|
|
|
TagType = uint.MaxValue, // All Type
|
|
|
|
|
Reserved2 = new Array6<byte>()
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-11 23:07:37 +02:00
|
|
|
|
Uuid.CopyTo(tagInfo.Uuid.AsSpan());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Write(outputPosition, tagInfo);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(14)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetRegisterInfo(bytes<8, 4>) -> buffer<unknown<0x100>, 0x1a>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetRegisterInfo(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.RecvListBuff.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.WrongArgument;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong outputPosition = context.Request.RecvListBuff[0].Position;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Marshal.SizeOf<RegisterInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf<RegisterInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
2022-05-31 21:29:35 +02:00
|
|
|
|
RegisterInfo registerInfo = VirtualAmiibo.GetRegisterInfo(
|
|
|
|
|
context.Device.System.TickSource,
|
|
|
|
|
context.Device.System.NfpDevices[i].AmiiboId,
|
|
|
|
|
context.Device.System.AccountManager.LastOpenedUser.Name);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Write(outputPosition, registerInfo);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(15)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetCommonInfo(bytes<8, 4>) -> buffer<unknown<0x40>, 0x1a>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetCommonInfo(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.RecvListBuff.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.WrongArgument;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong outputPosition = context.Request.RecvListBuff[0].Position;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Marshal.SizeOf<CommonInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf<CommonInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
|
|
|
|
CommonInfo commonInfo = VirtualAmiibo.GetCommonInfo(context.Device.System.NfpDevices[i].AmiiboId);
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Write(outputPosition, commonInfo);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(16)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetModelInfo(bytes<8, 4>) -> buffer<unknown<0x40>, 0x1a>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetModelInfo(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
ResultCode resultCode = CheckNfcIsEnabled();
|
|
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.RecvListBuff.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.WrongArgument;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
ulong outputPosition = context.Request.RecvListBuff[0].Position;
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Marshal.SizeOf<ModelInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2022-12-12 15:10:05 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, outputPosition, Marshal.SizeOf<ModelInfo>());
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
uint deviceHandle = (uint)context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
|
|
if (context.Device.System.NfpDevices.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].Handle == (PlayerIndex)deviceHandle)
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagRemoved)
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.TagNotFound;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (context.Device.System.NfpDevices[i].State == NfpDeviceState.TagMounted)
|
|
|
|
|
{
|
|
|
|
|
ModelInfo modelInfo = new ModelInfo
|
|
|
|
|
{
|
|
|
|
|
Reserved = new Array57<byte>()
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-19 01:31:34 +02:00
|
|
|
|
modelInfo.CharacterId = BinaryPrimitives.ReverseEndianness(ushort.Parse(context.Device.System.NfpDevices[i].AmiiboId.AsSpan(0, 4), NumberStyles.HexNumber));
|
|
|
|
|
modelInfo.CharacterVariant = byte.Parse(context.Device.System.NfpDevices[i].AmiiboId.AsSpan(4, 2), NumberStyles.HexNumber);
|
|
|
|
|
modelInfo.Series = byte.Parse(context.Device.System.NfpDevices[i].AmiiboId.AsSpan(12, 2), NumberStyles.HexNumber);
|
|
|
|
|
modelInfo.ModelNumber = ushort.Parse(context.Device.System.NfpDevices[i].AmiiboId.AsSpan(8, 4), NumberStyles.HexNumber);
|
|
|
|
|
modelInfo.Type = byte.Parse(context.Device.System.NfpDevices[i].AmiiboId.AsSpan(6, 2), NumberStyles.HexNumber);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
|
context.Memory.Write(outputPosition, modelInfo);
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultCode = ResultCode.WrongDeviceState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(17)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// AttachActivateEvent(bytes<8, 4>) -> handle<copy>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode AttachActivateEvent(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
|
|
|
|
uint deviceHandle = context.RequestData.ReadUInt32();
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Device.System.NfpDevices[i].ActivateEvent = new KEvent(context.Device.System.KernelContext);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
if (context.Process.HandleTable.GenerateHandle(context.Device.System.NfpDevices[i].ActivateEvent.ReadableEvent, out int activateEventHandle) != Result.Success)
|
2021-03-18 21:40:20 +01:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(activateEventHandle);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.DeviceNotFound;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(18)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// AttachDeactivateEvent(bytes<8, 4>) -> handle<copy>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode AttachDeactivateEvent(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
|
|
|
|
uint deviceHandle = context.RequestData.ReadUInt32();
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Device.System.NfpDevices[i].DeactivateEvent = new KEvent(context.Device.System.KernelContext);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
if (context.Process.HandleTable.GenerateHandle(context.Device.System.NfpDevices[i].DeactivateEvent.ReadableEvent, out int deactivateEventHandle) != Result.Success)
|
2021-03-18 21:40:20 +01:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(deactivateEventHandle);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.DeviceNotFound;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(19)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetState() -> u32
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetState(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
|
|
|
|
context.ResponseData.Write((int)_state);
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(20)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetDeviceState(bytes<8, 4>) -> u32
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetDeviceState(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
|
|
|
|
uint deviceHandle = context.RequestData.ReadUInt32();
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if (context.Device.System.NfpDevices[i].State > NfpDeviceState.Finalized)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
2022-05-31 21:29:35 +02:00
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.ResponseData.Write((uint)context.Device.System.NfpDevices[i].State);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.ResponseData.Write((uint)NfpDeviceState.Unavailable);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.DeviceNotFound;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(21)]
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// GetNpadId(bytes<8, 4>) -> u32
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetNpadId(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
|
|
|
|
uint deviceHandle = context.RequestData.ReadUInt32();
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
for (int i = 0; i < context.Device.System.NfpDevices.Count; i++)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
if ((uint)context.Device.System.NfpDevices[i].Handle == deviceHandle)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.ResponseData.Write((uint)HidUtils.GetNpadIdTypeFromIndex(context.Device.System.NfpDevices[i].Handle));
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.DeviceNotFound;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(22)]
|
2021-03-18 21:40:20 +01:00
|
|
|
|
// GetApplicationAreaSize() -> u32
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode GetApplicationAreaSize(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.ResponseData.Write(AmiiboConstants.ApplicationAreaSize);
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(23)] // 3.0.0+
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// AttachAvailabilityChangeEvent() -> handle<copy>
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode AttachAvailabilityChangeEvent(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-03-18 21:40:20 +01:00
|
|
|
|
_availabilityChangeEvent = new KEvent(context.Device.System.KernelContext);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
if (context.Process.HandleTable.GenerateHandle(_availabilityChangeEvent.ReadableEvent, out int availabilityChangeEventHandle) != Result.Success)
|
2021-03-18 21:40:20 +01:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(availabilityChangeEventHandle);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
|
[CommandHipc(24)] // 3.0.0+
|
2019-06-28 13:08:23 +02:00
|
|
|
|
// RecreateApplicationArea(bytes<8, 4>, u32, buffer<unknown, 5>)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode RecreateApplicationArea(ServiceCtx context)
|
2019-06-28 13:08:23 +02:00
|
|
|
|
{
|
2021-08-11 21:16:42 +02:00
|
|
|
|
throw new ServiceNotImplementedException(this, context);
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
2021-03-18 21:40:20 +01:00
|
|
|
|
|
2021-06-24 01:05:40 +02:00
|
|
|
|
[CommandHipc(102)]
|
|
|
|
|
// GetRegisterInfo2(bytes<8, 4>) -> buffer<unknown<0x100>, 0x1a>
|
|
|
|
|
public ResultCode GetRegisterInfo2(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Find the differencies between IUser and ISystem/IDebug.
|
|
|
|
|
|
|
|
|
|
if (_permissionLevel == NfpPermissionLevel.Debug || _permissionLevel == NfpPermissionLevel.System)
|
|
|
|
|
{
|
|
|
|
|
return GetRegisterInfo(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.DeviceNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 21:40:20 +01:00
|
|
|
|
private ResultCode CheckNfcIsEnabled()
|
|
|
|
|
{
|
|
|
|
|
// TODO: Call nn::settings::detail::GetNfcEnableFlag when it will be implemented.
|
|
|
|
|
return true ? ResultCode.Success : ResultCode.NfcDisabled;
|
|
|
|
|
}
|
2019-06-28 13:08:23 +02:00
|
|
|
|
}
|
2021-11-16 00:03:17 +01:00
|
|
|
|
}
|