2023-01-04 23:15:45 +01:00
|
|
|
|
using Ryujinx.Common.Memory;
|
|
|
|
|
using Ryujinx.Horizon.Common;
|
|
|
|
|
using Ryujinx.Horizon.Sdk.Sf.Cmif;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Horizon.Sdk.Sm
|
|
|
|
|
{
|
|
|
|
|
class SmApi
|
|
|
|
|
{
|
2023-01-08 13:13:39 +01:00
|
|
|
|
private const string SmName = "sm:";
|
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
private int _portHandle;
|
|
|
|
|
|
|
|
|
|
public Result Initialize()
|
|
|
|
|
{
|
2023-01-08 13:13:39 +01:00
|
|
|
|
Result result = HorizonStatic.Syscall.ConnectToNamedPort(out int portHandle, SmName);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
|
|
while (result == KernelResult.NotFound)
|
|
|
|
|
{
|
|
|
|
|
HorizonStatic.Syscall.SleepThread(50000000L);
|
2023-01-08 13:13:39 +01:00
|
|
|
|
result = HorizonStatic.Syscall.ConnectToNamedPort(out portHandle, SmName);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.IsFailure)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_portHandle = portHandle;
|
|
|
|
|
|
|
|
|
|
return RegisterClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Result RegisterClient()
|
|
|
|
|
{
|
|
|
|
|
Span<byte> data = stackalloc byte[8];
|
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
|
SpanWriter writer = new(data);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
|
|
writer.Write(0UL);
|
|
|
|
|
|
|
|
|
|
return ServiceUtil.SendRequest(out _, _portHandle, 0, sendPid: true, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result GetServiceHandle(out int handle, ServiceName name)
|
|
|
|
|
{
|
|
|
|
|
Span<byte> data = stackalloc byte[8];
|
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
|
SpanWriter writer = new(data);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
|
|
writer.Write(name);
|
|
|
|
|
|
|
|
|
|
Result result = ServiceUtil.SendRequest(out CmifResponse response, _portHandle, 1, sendPid: false, data);
|
|
|
|
|
|
|
|
|
|
if (result.IsFailure)
|
|
|
|
|
{
|
|
|
|
|
handle = 0;
|
2023-01-08 13:13:39 +01:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle = response.MoveHandles[0];
|
2023-01-08 13:13:39 +01:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result RegisterService(out int handle, ServiceName name, int maxSessions, bool isLight)
|
|
|
|
|
{
|
|
|
|
|
Span<byte> data = stackalloc byte[16];
|
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
|
SpanWriter writer = new(data);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
|
|
writer.Write(name);
|
|
|
|
|
writer.Write(isLight ? 1 : 0);
|
|
|
|
|
writer.Write(maxSessions);
|
|
|
|
|
|
|
|
|
|
Result result = ServiceUtil.SendRequest(out CmifResponse response, _portHandle, 2, sendPid: false, data);
|
|
|
|
|
|
|
|
|
|
if (result.IsFailure)
|
|
|
|
|
{
|
|
|
|
|
handle = 0;
|
2023-01-08 13:13:39 +01:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle = response.MoveHandles[0];
|
2023-01-08 13:13:39 +01:00
|
|
|
|
|
2023-01-04 23:15:45 +01:00
|
|
|
|
return Result.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result UnregisterService(ServiceName name)
|
|
|
|
|
{
|
|
|
|
|
Span<byte> data = stackalloc byte[8];
|
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
|
SpanWriter writer = new(data);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
|
|
writer.Write(name);
|
|
|
|
|
|
|
|
|
|
return ServiceUtil.SendRequest(out _, _portHandle, 3, sendPid: false, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Result DetachClient()
|
|
|
|
|
{
|
|
|
|
|
Span<byte> data = stackalloc byte[8];
|
|
|
|
|
|
2023-01-08 13:13:39 +01:00
|
|
|
|
SpanWriter writer = new(data);
|
2023-01-04 23:15:45 +01:00
|
|
|
|
|
|
|
|
|
writer.Write(0UL);
|
|
|
|
|
|
|
|
|
|
return ServiceUtil.SendRequest(out _, _portHandle, 4, sendPid: true, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-08 13:13:39 +01:00
|
|
|
|
}
|