2018-02-10 01:14:55 +01:00
|
|
|
using System;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
class ISystemClock : IpcService
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2018-04-11 02:16:27 +02:00
|
|
|
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
2018-02-10 01:14:55 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private SystemClockType _clockType;
|
2019-07-12 03:13:43 +02:00
|
|
|
private DateTime _systemClockContextEpoch;
|
|
|
|
private long _systemClockTimePoint;
|
|
|
|
private byte[] _systemClockContextEnding;
|
|
|
|
private long _timeOffset;
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public ISystemClock(SystemClockType clockType)
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2019-07-12 03:13:43 +02:00
|
|
|
_clockType = clockType;
|
2018-12-06 12:16:24 +01:00
|
|
|
_systemClockContextEpoch = System.Diagnostics.Process.GetCurrentProcess().StartTime;
|
|
|
|
_systemClockContextEnding = new byte[0x10];
|
|
|
|
_timeOffset = 0;
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (clockType == SystemClockType.User ||
|
|
|
|
clockType == SystemClockType.Network)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_systemClockContextEpoch = _systemClockContextEpoch.ToUniversalTime();
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_systemClockTimePoint = (long)(_systemClockContextEpoch - Epoch).TotalSeconds;
|
2018-02-10 01:14:55 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(0)]
|
|
|
|
// GetCurrentTime() -> nn::time::PosixTime
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetCurrentTime(ServiceCtx context)
|
2018-02-10 01:14:55 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
DateTime currentTime = DateTime.Now;
|
2018-02-14 06:43:21 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_clockType == SystemClockType.User ||
|
|
|
|
_clockType == SystemClockType.Network)
|
2018-02-14 06:43:21 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currentTime = currentTime.ToUniversalTime();
|
2018-02-14 06:43:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write((long)((currentTime - Epoch).TotalSeconds) + _timeOffset);
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(1)]
|
|
|
|
// SetCurrentTime(nn::time::PosixTime)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode SetCurrentTime(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
DateTime currentTime = DateTime.Now;
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_clockType == SystemClockType.User ||
|
|
|
|
_clockType == SystemClockType.Network)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currentTime = currentTime.ToUniversalTime();
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_timeOffset = (context.RequestData.ReadInt64() - (long)(currentTime - Epoch).TotalSeconds);
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(2)]
|
|
|
|
// GetSystemClockContext() -> nn::time::SystemClockContext
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetSystemClockContext(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write((long)(_systemClockContextEpoch - Epoch).TotalSeconds);
|
2018-07-02 02:03:05 +02:00
|
|
|
|
|
|
|
// The point in time, TODO: is there a link between epoch and this?
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write(_systemClockTimePoint);
|
2018-07-02 02:03:05 +02:00
|
|
|
|
|
|
|
// This seems to be some kind of identifier?
|
|
|
|
for (int i = 0; i < 0x10; i++)
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write(_systemClockContextEnding[i]);
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-07-02 02:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(3)]
|
|
|
|
// SetSystemClockContext(nn::time::SystemClockContext)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode SetSystemClockContext(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long newSystemClockEpoch = context.RequestData.ReadInt64();
|
|
|
|
long newSystemClockTimePoint = context.RequestData.ReadInt64();
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_systemClockContextEpoch = Epoch.Add(TimeSpan.FromSeconds(newSystemClockEpoch));
|
|
|
|
_systemClockTimePoint = newSystemClockTimePoint;
|
|
|
|
_systemClockContextEnding = context.RequestData.ReadBytes(0x10);
|
2018-02-10 01:14:55 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-10 01:14:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|