2019-07-14 21:50:11 +01:00
|
|
|
using Ryujinx.Common;
|
2019-10-08 04:48:49 +01:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2019-07-14 21:50:11 +01:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
2019-10-08 04:48:49 +01:00
|
|
|
using System;
|
2018-02-10 00:14:55 +00:00
|
|
|
|
2019-09-19 01:45:11 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
class ISystemClock : IpcService
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2019-07-14 21:50:11 +01:00
|
|
|
private SystemClockCore _clockCore;
|
|
|
|
private bool _writePermission;
|
2019-10-08 04:48:49 +01:00
|
|
|
private bool _bypassUninitializedClock;
|
|
|
|
private int _operationEventReadableHandle;
|
2018-02-10 00:14:55 +00:00
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
public ISystemClock(SystemClockCore clockCore, bool writePermission, bool bypassUninitializedClock)
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2019-10-08 04:48:49 +01:00
|
|
|
_clockCore = clockCore;
|
|
|
|
_writePermission = writePermission;
|
|
|
|
_bypassUninitializedClock = bypassUninitializedClock;
|
|
|
|
_operationEventReadableHandle = 0;
|
2018-02-10 00:14:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 02:13:43 +01:00
|
|
|
[Command(0)]
|
|
|
|
// GetCurrentTime() -> nn::time::PosixTime
|
2019-07-14 20:04:38 +01:00
|
|
|
public ResultCode GetCurrentTime(ServiceCtx context)
|
2018-02-10 00:14:55 +00:00
|
|
|
{
|
2019-10-08 04:48:49 +01:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
2019-07-14 21:50:11 +01:00
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
ResultCode result = _clockCore.GetCurrentTime(context.Thread, out long posixTime);
|
2018-02-14 05:43:21 +00:00
|
|
|
|
2019-07-14 21:50:11 +01:00
|
|
|
if (result == ResultCode.Success)
|
2018-02-14 05:43:21 +00:00
|
|
|
{
|
2019-10-08 04:48:49 +01:00
|
|
|
context.ResponseData.Write(posixTime);
|
2019-07-14 21:50:11 +01:00
|
|
|
}
|
2018-07-02 01:03:05 +01:00
|
|
|
|
2019-07-14 21:50:11 +01:00
|
|
|
return result;
|
2018-07-02 01:03:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 02:13:43 +01:00
|
|
|
[Command(1)]
|
|
|
|
// SetCurrentTime(nn::time::PosixTime)
|
2019-07-14 20:04:38 +01:00
|
|
|
public ResultCode SetCurrentTime(ServiceCtx context)
|
2018-07-02 01:03:05 +01:00
|
|
|
{
|
2019-07-14 21:50:11 +01:00
|
|
|
if (!_writePermission)
|
2018-07-02 01:03:05 +01:00
|
|
|
{
|
2019-07-14 21:50:11 +01:00
|
|
|
return ResultCode.PermissionDenied;
|
2018-07-02 01:03:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
2019-07-14 21:50:11 +01:00
|
|
|
{
|
2019-10-08 04:48:49 +01:00
|
|
|
return ResultCode.UninitializedClock;
|
2019-07-14 21:50:11 +01:00
|
|
|
}
|
2018-07-02 01:03:05 +01:00
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
long posixTime = context.RequestData.ReadInt64();
|
|
|
|
|
|
|
|
return _clockCore.SetCurrentTime(context.Thread, posixTime);
|
2018-07-02 01:03:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 02:13:43 +01:00
|
|
|
[Command(2)]
|
2019-10-08 04:48:49 +01:00
|
|
|
// GetClockContext() -> nn::time::SystemClockContext
|
2019-07-14 20:04:38 +01:00
|
|
|
public ResultCode GetSystemClockContext(ServiceCtx context)
|
2018-07-02 01:03:05 +01:00
|
|
|
{
|
2019-10-08 04:48:49 +01:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode result = _clockCore.GetClockContext(context.Thread, out SystemClockContext clockContext);
|
2018-07-02 01:03:05 +01:00
|
|
|
|
2019-07-14 21:50:11 +01:00
|
|
|
if (result == ResultCode.Success)
|
2018-07-02 01:03:05 +01:00
|
|
|
{
|
2019-07-14 21:50:11 +01:00
|
|
|
context.ResponseData.WriteStruct(clockContext);
|
2018-07-02 01:03:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-14 21:50:11 +01:00
|
|
|
return result;
|
2018-07-02 01:03:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 02:13:43 +01:00
|
|
|
[Command(3)]
|
2019-10-08 04:48:49 +01:00
|
|
|
// SetClockContext(nn::time::SystemClockContext)
|
2019-07-14 20:04:38 +01:00
|
|
|
public ResultCode SetSystemClockContext(ServiceCtx context)
|
2018-07-02 01:03:05 +01:00
|
|
|
{
|
2019-07-14 21:50:11 +01:00
|
|
|
if (!_writePermission)
|
|
|
|
{
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
}
|
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
2019-07-14 21:50:11 +01:00
|
|
|
SystemClockContext clockContext = context.RequestData.ReadStruct<SystemClockContext>();
|
2018-07-02 01:03:05 +01:00
|
|
|
|
2019-07-14 21:50:11 +01:00
|
|
|
ResultCode result = _clockCore.SetSystemClockContext(clockContext);
|
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Command(4)] // 9.0.0+
|
|
|
|
// GetOperationEventReadableHandle() -> handle<copy>
|
|
|
|
public ResultCode GetOperationEventReadableHandle(ServiceCtx context)
|
|
|
|
{
|
|
|
|
if (_operationEventReadableHandle == 0)
|
2019-07-14 21:50:11 +01:00
|
|
|
{
|
2019-10-08 04:48:49 +01:00
|
|
|
KEvent kEvent = new KEvent(context.Device.System);
|
|
|
|
|
|
|
|
_clockCore.RegisterOperationEvent(kEvent.WritableEvent);
|
|
|
|
|
|
|
|
if (context.Process.HandleTable.GenerateHandle(kEvent.ReadableEvent, out _operationEventReadableHandle) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
2019-07-14 21:50:11 +01:00
|
|
|
}
|
2018-02-10 00:14:55 +00:00
|
|
|
|
2019-10-08 04:48:49 +01:00
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_operationEventReadableHandle);
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
2018-02-10 00:14:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|