2019-07-25 16:44:51 +02:00
|
|
|
using Ryujinx.Common;
|
2020-12-12 04:06:20 +01:00
|
|
|
using Ryujinx.Cpu;
|
2018-08-17 01:47:36 +02:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2019-06-17 01:56:46 +02:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2019-07-14 22:50:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
2019-09-19 02:45:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time.StaticService;
|
2019-07-25 16:44:51 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time.TimeZone;
|
2018-07-02 02:03:05 +02:00
|
|
|
using System;
|
2019-07-25 16:44:51 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
2022-11-24 15:26:29 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-07-25 16:44:51 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2022-11-24 15:26:29 +01:00
|
|
|
using System.Text;
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-07-14 22:50:11 +02:00
|
|
|
[Service("time:s", TimePermissions.System)]
|
2019-10-08 05:48:49 +02:00
|
|
|
[Service("time:su", TimePermissions.SystemUpdate)]
|
|
|
|
class IStaticServiceForPsc : IpcService
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
private TimeManager _timeManager;
|
2019-07-14 22:50:11 +02:00
|
|
|
private TimePermissions _permissions;
|
|
|
|
|
2019-06-17 01:56:46 +02:00
|
|
|
private int _timeSharedMemoryNativeHandle = 0;
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
public IStaticServiceForPsc(ServiceCtx context, TimePermissions permissions) : this(TimeManager.Instance, permissions) {}
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
public IStaticServiceForPsc(TimeManager manager, TimePermissions permissions)
|
2019-07-14 22:50:11 +02:00
|
|
|
{
|
|
|
|
_permissions = permissions;
|
2019-10-08 05:48:49 +02:00
|
|
|
_timeManager = manager;
|
2019-07-14 22:50:11 +02:00
|
|
|
}
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(0)]
|
2019-06-17 01:56:46 +02:00
|
|
|
// GetStandardUserSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetStandardUserSystemClock(ServiceCtx context)
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
MakeObject(context, new ISystemClock(_timeManager.StandardUserSystemClock,
|
|
|
|
(_permissions & TimePermissions.UserSystemClockWritableMask) != 0,
|
|
|
|
(_permissions & TimePermissions.BypassUninitialized) != 0));
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(1)]
|
2019-06-17 01:56:46 +02:00
|
|
|
// GetStandardNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetStandardNetworkSystemClock(ServiceCtx context)
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
MakeObject(context, new ISystemClock(_timeManager.StandardNetworkSystemClock,
|
|
|
|
(_permissions & TimePermissions.NetworkSystemClockWritableMask) != 0,
|
|
|
|
(_permissions & TimePermissions.BypassUninitialized) != 0));
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(2)]
|
2019-06-17 01:56:46 +02:00
|
|
|
// GetStandardSteadyClock() -> object<nn::timesrv::detail::service::ISteadyClock>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetStandardSteadyClock(ServiceCtx context)
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
MakeObject(context, new ISteadyClock(_timeManager.StandardSteadyClock,
|
|
|
|
(_permissions & TimePermissions.SteadyClockWritableMask) != 0,
|
|
|
|
(_permissions & TimePermissions.BypassUninitialized) != 0));
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(3)]
|
2019-06-17 01:56:46 +02:00
|
|
|
// GetTimeZoneService() -> object<nn::timesrv::detail::service::ITimeZoneService>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetTimeZoneService(ServiceCtx context)
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
MakeObject(context, new ITimeZoneServiceForPsc(_timeManager.TimeZone.Manager,
|
|
|
|
(_permissions & TimePermissions.TimeZoneWritableMask) != 0));
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(4)]
|
2019-06-17 01:56:46 +02:00
|
|
|
// GetStandardLocalSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetStandardLocalSystemClock(ServiceCtx context)
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
MakeObject(context, new ISystemClock(_timeManager.StandardLocalSystemClock,
|
|
|
|
(_permissions & TimePermissions.LocalSystemClockWritableMask) != 0,
|
|
|
|
(_permissions & TimePermissions.BypassUninitialized) != 0));
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(5)] // 4.0.0+
|
2019-07-25 16:44:51 +02:00
|
|
|
// GetEphemeralNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
|
|
|
|
public ResultCode GetEphemeralNetworkSystemClock(ServiceCtx context)
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
MakeObject(context, new ISystemClock(_timeManager.StandardNetworkSystemClock,
|
|
|
|
(_permissions & TimePermissions.NetworkSystemClockWritableMask) != 0,
|
|
|
|
(_permissions & TimePermissions.BypassUninitialized) != 0));
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(20)] // 6.0.0+
|
2019-06-17 01:56:46 +02:00
|
|
|
// GetSharedMemoryNativeHandle() -> handle<copy>
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode GetSharedMemoryNativeHandle(ServiceCtx context)
|
2019-06-17 01:56:46 +02:00
|
|
|
{
|
|
|
|
if (_timeSharedMemoryNativeHandle == 0)
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
if (context.Process.HandleTable.GenerateHandle(_timeManager.SharedMemory.GetSharedMemory(), out _timeSharedMemoryNativeHandle) != KernelResult.Success)
|
2019-06-17 01:56:46 +02:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Out of handles!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_timeSharedMemoryNativeHandle);
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2019-06-17 01:56:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(50)] // 4.0.0+
|
2019-10-08 05:48:49 +02:00
|
|
|
// SetStandardSteadyClockInternalOffset(nn::TimeSpanType internal_offset)
|
|
|
|
public ResultCode SetStandardSteadyClockInternalOffset(ServiceCtx context)
|
|
|
|
{
|
|
|
|
// This is only implemented in glue's StaticService.
|
|
|
|
return ResultCode.NotImplemented;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(51)] // 9.0.0+
|
2019-10-08 05:48:49 +02:00
|
|
|
// GetStandardSteadyClockRtcValue() -> u64
|
|
|
|
public ResultCode GetStandardSteadyClockRtcValue(ServiceCtx context)
|
|
|
|
{
|
|
|
|
// This is only implemented in glue's StaticService.
|
|
|
|
return ResultCode.NotImplemented;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(100)]
|
2019-07-14 22:50:11 +02:00
|
|
|
// IsStandardUserSystemClockAutomaticCorrectionEnabled() -> bool
|
|
|
|
public ResultCode IsStandardUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
|
|
|
|
|
|
|
|
if (!userClock.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ResponseData.Write(userClock.IsAutomaticCorrectionEnabled());
|
2019-07-14 22:50:11 +02:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(101)]
|
2019-07-14 22:50:11 +02:00
|
|
|
// SetStandardUserSystemClockAutomaticCorrectionEnabled(b8)
|
|
|
|
public ResultCode SetStandardUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
SteadyClockCore steadyClock = _timeManager.StandardSteadyClock;
|
|
|
|
StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
|
|
|
|
|
|
|
|
if (!userClock.IsInitialized() || !steadyClock.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
2019-07-14 22:50:11 +02:00
|
|
|
if ((_permissions & TimePermissions.UserSystemClockWritableMask) == 0)
|
|
|
|
{
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool autoCorrectionEnabled = context.RequestData.ReadBoolean();
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
ResultCode result = userClock.SetAutomaticCorrectionEnabled(tickSource, autoCorrectionEnabled);
|
2019-10-08 05:48:49 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
_timeManager.SharedMemory.SetAutomaticCorrectionEnabled(autoCorrectionEnabled);
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
SteadyClockTimePoint currentTimePoint = userClock.GetSteadyClockCore().GetCurrentTimePoint(tickSource);
|
2019-10-08 05:48:49 +02:00
|
|
|
|
|
|
|
userClock.SetAutomaticCorrectionUpdatedTime(currentTimePoint);
|
|
|
|
userClock.SignalAutomaticCorrectionEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(102)] // 5.0.0+
|
2019-10-08 05:48:49 +02:00
|
|
|
// GetStandardUserSystemClockInitialYear() -> u32
|
|
|
|
public ResultCode GetStandardUserSystemClockInitialYear(ServiceCtx context)
|
|
|
|
{
|
|
|
|
// This is only implemented in glue's StaticService.
|
|
|
|
return ResultCode.NotImplemented;
|
2019-07-14 22:50:11 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(200)] // 3.0.0+
|
2019-07-15 19:52:35 +02:00
|
|
|
// IsStandardNetworkSystemClockAccuracySufficient() -> bool
|
|
|
|
public ResultCode IsStandardNetworkSystemClockAccuracySufficient(ServiceCtx context)
|
|
|
|
{
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
context.ResponseData.Write(_timeManager.StandardNetworkSystemClock.IsStandardNetworkSystemClockAccuracySufficient(tickSource));
|
2019-10-08 05:48:49 +02:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(201)] // 6.0.0+
|
2019-10-08 05:48:49 +02:00
|
|
|
// GetStandardUserSystemClockAutomaticCorrectionUpdatedTime() -> nn::time::SteadyClockTimePoint
|
|
|
|
public ResultCode GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(ServiceCtx context)
|
|
|
|
{
|
|
|
|
StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
|
|
|
|
|
|
|
|
if (!userClock.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ResponseData.WriteStruct(userClock.GetAutomaticCorrectionUpdatedTime());
|
2019-07-15 19:52:35 +02:00
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(300)] // 4.0.0+
|
2019-07-25 16:44:51 +02:00
|
|
|
// CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
|
2018-07-02 02:03:05 +02:00
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
SteadyClockCore steadyClock = _timeManager.StandardSteadyClock;
|
|
|
|
|
|
|
|
if (!steadyClock.IsInitialized())
|
|
|
|
{
|
|
|
|
return ResultCode.UninitializedClock;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
2019-07-25 16:44:51 +02:00
|
|
|
SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
|
2022-05-31 21:29:35 +02:00
|
|
|
SteadyClockTimePoint currentTimePoint = steadyClock.GetCurrentTimePoint(tickSource);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
ResultCode result = ResultCode.TimeMismatch;
|
|
|
|
|
|
|
|
if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId)
|
|
|
|
{
|
2022-05-31 21:29:35 +02:00
|
|
|
TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(tickSource.Counter, tickSource.Frequency);
|
2019-07-25 16:44:51 +02:00
|
|
|
long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
|
|
|
|
|
|
|
|
context.ResponseData.Write(baseTimePoint);
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
result = ResultCode.Success;
|
2019-07-25 16:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(400)] // 4.0.0+
|
2019-07-25 16:44:51 +02:00
|
|
|
// GetClockSnapshot(u8) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
|
|
|
|
public ResultCode GetClockSnapshot(ServiceCtx context)
|
|
|
|
{
|
|
|
|
byte type = context.RequestData.ReadByte();
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Marshal.SizeOf<ClockSnapshot>());
|
2020-12-12 04:06:20 +01:00
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
ResultCode result = _timeManager.StandardUserSystemClock.GetClockContext(tickSource, out SystemClockContext userContext);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
2022-05-31 21:29:35 +02:00
|
|
|
result = _timeManager.StandardNetworkSystemClock.GetClockContext(tickSource, out SystemClockContext networkContext);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
2022-05-31 21:29:35 +02:00
|
|
|
result = GetClockSnapshotFromSystemClockContextInternal(tickSource, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(401)] // 4.0.0+
|
2019-07-25 16:44:51 +02:00
|
|
|
// GetClockSnapshotFromSystemClockContext(u8, nn::time::SystemClockContext, nn::time::SystemClockContext) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
|
|
|
|
public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context)
|
|
|
|
{
|
|
|
|
byte type = context.RequestData.ReadByte();
|
|
|
|
|
2022-11-24 15:26:29 +01:00
|
|
|
context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Unsafe.SizeOf<ClockSnapshot>());
|
2020-12-12 04:06:20 +01:00
|
|
|
|
2019-07-25 16:44:51 +02:00
|
|
|
context.RequestData.BaseStream.Position += 7;
|
|
|
|
|
|
|
|
SystemClockContext userContext = context.RequestData.ReadStruct<SystemClockContext>();
|
|
|
|
SystemClockContext networkContext = context.RequestData.ReadStruct<SystemClockContext>();
|
2018-07-02 02:03:05 +02:00
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
ITickSource tickSource = context.Device.System.TickSource;
|
|
|
|
|
|
|
|
ResultCode result = GetClockSnapshotFromSystemClockContextInternal(tickSource, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(500)] // 4.0.0+
|
2019-07-25 16:44:51 +02:00
|
|
|
// CalculateStandardUserSystemClockDifferenceByUser(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
|
|
|
|
public ResultCode CalculateStandardUserSystemClockDifferenceByUser(ServiceCtx context)
|
|
|
|
{
|
2019-11-27 19:27:30 +01:00
|
|
|
ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[0]);
|
|
|
|
ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[1]);
|
2019-07-25 16:44:51 +02:00
|
|
|
TimeSpanType difference = TimeSpanType.FromSeconds(clockSnapshotB.UserContext.Offset - clockSnapshotA.UserContext.Offset);
|
|
|
|
|
|
|
|
if (clockSnapshotB.UserContext.SteadyTimePoint.ClockSourceId != clockSnapshotA.UserContext.SteadyTimePoint.ClockSourceId || (clockSnapshotB.IsAutomaticCorrectionEnabled && clockSnapshotA.IsAutomaticCorrectionEnabled))
|
|
|
|
{
|
|
|
|
difference = new TimeSpanType(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ResponseData.Write(difference.NanoSeconds);
|
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-25 16:44:51 +02:00
|
|
|
|
2021-04-14 00:01:24 +02:00
|
|
|
[CommandHipc(501)] // 4.0.0+
|
2019-07-25 16:44:51 +02:00
|
|
|
// CalculateSpanBetween(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
|
|
|
|
public ResultCode CalculateSpanBetween(ServiceCtx context)
|
|
|
|
{
|
2019-11-27 19:27:30 +01:00
|
|
|
ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[0]);
|
|
|
|
ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[1]);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
TimeSpanType result;
|
|
|
|
|
|
|
|
ResultCode resultCode = clockSnapshotA.SteadyClockTimePoint.GetSpanBetween(clockSnapshotB.SteadyClockTimePoint, out long timeSpan);
|
|
|
|
|
|
|
|
if (resultCode != ResultCode.Success)
|
|
|
|
{
|
|
|
|
resultCode = ResultCode.TimeNotFound;
|
|
|
|
|
|
|
|
if (clockSnapshotA.NetworkTime != 0 && clockSnapshotB.NetworkTime != 0)
|
|
|
|
{
|
|
|
|
result = TimeSpanType.FromSeconds(clockSnapshotB.NetworkTime - clockSnapshotA.NetworkTime);
|
|
|
|
resultCode = ResultCode.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return resultCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = TimeSpanType.FromSeconds(timeSpan);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ResponseData.Write(result.NanoSeconds);
|
|
|
|
|
|
|
|
return resultCode;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:29:35 +02:00
|
|
|
private ResultCode GetClockSnapshotFromSystemClockContextInternal(ITickSource tickSource, SystemClockContext userContext, SystemClockContext networkContext, byte type, out ClockSnapshot clockSnapshot)
|
2019-07-25 16:44:51 +02:00
|
|
|
{
|
|
|
|
clockSnapshot = new ClockSnapshot();
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
SteadyClockCore steadyClockCore = _timeManager.StandardSteadyClock;
|
2022-05-31 21:29:35 +02:00
|
|
|
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(tickSource);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
clockSnapshot.IsAutomaticCorrectionEnabled = _timeManager.StandardUserSystemClock.IsAutomaticCorrectionEnabled();
|
2019-07-25 16:44:51 +02:00
|
|
|
clockSnapshot.UserContext = userContext;
|
|
|
|
clockSnapshot.NetworkContext = networkContext;
|
2021-04-26 23:56:14 +02:00
|
|
|
clockSnapshot.SteadyClockTimePoint = currentTimePoint;
|
2019-07-25 16:44:51 +02:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
ResultCode result = _timeManager.TimeZone.Manager.GetDeviceLocationName(out string deviceLocationName);
|
|
|
|
|
|
|
|
if (result != ResultCode.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-24 15:26:29 +01:00
|
|
|
ReadOnlySpan<byte> tzName = Encoding.ASCII.GetBytes(deviceLocationName);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
2022-11-24 15:26:29 +01:00
|
|
|
tzName.CopyTo(clockSnapshot.LocationName);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
2019-10-08 05:48:49 +02:00
|
|
|
result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
clockSnapshot.UserCalendarTime = userCalendarInfo.Time;
|
|
|
|
clockSnapshot.UserCalendarAdditionalTime = userCalendarInfo.AdditionalInfo;
|
|
|
|
|
|
|
|
if (ClockSnapshot.GetCurrentTime(out clockSnapshot.NetworkTime, currentTimePoint, clockSnapshot.NetworkContext) != ResultCode.Success)
|
|
|
|
{
|
|
|
|
clockSnapshot.NetworkTime = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-08 05:48:49 +02:00
|
|
|
result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
|
2019-07-25 16:44:51 +02:00
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
{
|
|
|
|
clockSnapshot.NetworkCalendarTime = networkCalendarInfo.Time;
|
|
|
|
clockSnapshot.NetworkCalendarAdditionalTime = networkCalendarInfo.AdditionalInfo;
|
|
|
|
clockSnapshot.Type = type;
|
|
|
|
|
|
|
|
// Probably a version field?
|
|
|
|
clockSnapshot.Unknown = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-27 19:27:30 +01:00
|
|
|
private ClockSnapshot ReadClockSnapshotFromBuffer(ServiceCtx context, IpcPtrBuffDesc ipcDesc)
|
2019-07-25 16:44:51 +02:00
|
|
|
{
|
2022-11-24 15:26:29 +01:00
|
|
|
Debug.Assert(ipcDesc.Size == (ulong)Unsafe.SizeOf<ClockSnapshot>());
|
2019-07-25 16:44:51 +02:00
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
byte[] temp = new byte[ipcDesc.Size];
|
|
|
|
|
2021-04-24 12:16:01 +02:00
|
|
|
context.Memory.Read(ipcDesc.Position, temp);
|
2020-05-04 00:54:50 +02:00
|
|
|
|
|
|
|
using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(temp)))
|
2019-07-25 16:44:51 +02:00
|
|
|
{
|
|
|
|
return bufferReader.ReadStruct<ClockSnapshot>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot)
|
|
|
|
{
|
2020-12-12 04:06:20 +01:00
|
|
|
MemoryHelper.Write(context.Memory, ipcDesc.Position, clockSnapshot);
|
2019-07-25 16:44:51 +02:00
|
|
|
}
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
}
|