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