Implement GetSnapshotClock & GetSnapshotClockFromSystemClockContext

This commit is contained in:
Thog 2019-07-23 00:18:34 +02:00
parent fa26ee7b99
commit 9cc173eb0f
No known key found for this signature in database
GPG key ID: 0CD291558FAFDBC6
6 changed files with 181 additions and 10 deletions

View file

@ -952,6 +952,21 @@ namespace ChocolArm64.Memory
}
}
public unsafe void WriteStruct<T>(long position, T value)
where T : struct
{
long size = Marshal.SizeOf<T>();
byte[] data = new byte[size];
fixed (byte* ptr = data)
{
Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false);
}
WriteBytes(position, data);
}
public void CopyBytes(long src, long dst, long size)
{
// Note: This will be moved later.

View file

@ -59,4 +59,42 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public long Offset;
public SteadyClockTimePoint SteadyTimePoint;
}
[StructLayout(LayoutKind.Sequential, Size = 0xD0)]
struct ClockSnapshot
{
public SystemClockContext UserContext;
public SystemClockContext NetworkContext;
public long UserTime;
public long NetworkTime;
public CalendarTime UserCalendarTime;
public CalendarTime NetworkCalendarTime;
public CalendarAdditionalInfo UserCalendarAdditionalTime;
public CalendarAdditionalInfo NetworkCalendarAdditionalTime;
public SteadyClockTimePoint SteadyClockTimePoint;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x24)]
public char[] LocationName;
[MarshalAs(UnmanagedType.I1)]
public bool IsAutomaticCorrectionEnabled;
public byte Type;
public ushort Unknown;
public static ResultCode GetCurrentTime(out long currentTime, SteadyClockTimePoint steadyClockTimePoint, SystemClockContext context)
{
currentTime = 0;
if (steadyClockTimePoint.ClockSourceId == context.SteadyTimePoint.ClockSourceId)
{
currentTime = steadyClockTimePoint.TimePoint + context.Offset;
return ResultCode.Success;
}
return ResultCode.TimeMismatch;
}
}
}

View file

@ -35,7 +35,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public bool IsStandardNetworkSystemClockAccuracySufficient(KThread thread)
{
StandardSteadyClockCore steadyClockCore = GetSteadyClockCore();
SteadyClockCore steadyClockCore = GetSteadyClockCore();
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread);
bool isStandardNetworkClockSufficientAccuracy = false;

View file

@ -1,7 +1,11 @@
using Ryujinx.Common;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Time.Clock;
using Ryujinx.HLE.HOS.Services.Time.TimeZone;
using System;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Time
{
@ -125,16 +129,131 @@ namespace Ryujinx.HLE.HOS.Services.Time
}
[Command(300)] // 4.0.0+
// CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> u64
// CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64
public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
{
// TODO: reimplement this
long timeOffset = (long)(DateTime.UtcNow - StartupDate).TotalSeconds;
long systemClockContextEpoch = context.RequestData.ReadInt64();
SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
context.ResponseData.Write(timeOffset + systemClockContextEpoch);
SteadyClockTimePoint currentTimePoint = StandardSteadyClockCore.Instance.GetCurrentTimePoint(context.Thread);
return ResultCode.Success;
ResultCode result = ResultCode.TimeMismatch;
if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId)
{
TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(context.Thread.Context.ThreadState.CntpctEl0, context.Thread.Context.ThreadState.CntfrqEl0);
long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
context.ResponseData.Write(baseTimePoint);
result = 0;
}
return result;
}
[Command(400)] // 4.0.0+
// GetClockSnapshot(u8) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
public ResultCode GetClockSnapshot(ServiceCtx context)
{
byte type = context.RequestData.ReadByte();
long bufferPosition = context.Request.RecvListBuff[0].Position;
long bufferSize = context.Request.RecvListBuff[0].Size;
ResultCode result = StandardUserSystemClockCore.Instance.GetSystemClockContext(context.Thread, out SystemClockContext userContext);
if (result == ResultCode.Success)
{
result = StandardNetworkSystemClockCore.Instance.GetSystemClockContext(context.Thread, out SystemClockContext networkContext);
if (result == ResultCode.Success)
{
result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
if (result == ResultCode.Success)
{
context.Memory.WriteStruct(bufferPosition, clockSnapshot);
}
}
}
return result;
}
[Command(401)] // 4.0.0+
// GetClockSnapshotFromSystemClockContext(u8, nn::time::SystemClockContext, nn::time::SystemClockContext) -> buffer<nn::time::sf::-*, 0x1a>
public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context)
{
byte type = context.RequestData.ReadByte();
context.RequestData.BaseStream.Position += 7;
SystemClockContext userContext = context.RequestData.ReadStruct<SystemClockContext>();
SystemClockContext networkContext = context.RequestData.ReadStruct<SystemClockContext>();
long bufferPosition = context.Request.RecvListBuff[0].Position;
long bufferSize = context.Request.RecvListBuff[0].Size;
ResultCode result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
if (result == ResultCode.Success)
{
context.Memory.WriteStruct(bufferPosition, clockSnapshot);
}
return result;
}
private ResultCode GetClockSnapshotFromSystemClockContextInternal(KThread thread, SystemClockContext userContext, SystemClockContext networkContext, byte type, out ClockSnapshot clockSnapshot)
{
clockSnapshot = new ClockSnapshot();
SteadyClockCore steadyClockCore = StandardSteadyClockCore.Instance;
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread);
clockSnapshot.IsAutomaticCorrectionEnabled = StandardUserSystemClockCore.Instance.IsAutomaticCorrectionEnabled();
clockSnapshot.UserContext = userContext;
clockSnapshot.NetworkContext = networkContext;
char[] tzName = TimeZoneManager.Instance.GetDeviceLocationName().ToCharArray();
char[] locationName = new char[0x24];
Array.Copy(tzName, locationName, locationName.Length);
clockSnapshot.LocationName = locationName;
ResultCode result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
if (result == ResultCode.Success)
{
result = TimeZoneManager.Instance.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
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;
}
result = TimeZoneManager.Instance.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
if (result == ResultCode.Success)
{
clockSnapshot.NetworkCalendarTime = networkCalendarInfo.Time;
clockSnapshot.NetworkCalendarAdditionalTime = networkCalendarInfo.AdditionalInfo;
clockSnapshot.Type = type;
// Probably a version field?
clockSnapshot.Unknown = 0;
}
}
}
return result;
}
}
}

View file

@ -18,7 +18,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
// GetCurrentTime() -> nn::time::PosixTime
public ResultCode GetCurrentTime(ServiceCtx context)
{
StandardSteadyClockCore steadyClockCore = _clockCore.GetSteadyClockCore();
SteadyClockCore steadyClockCore = _clockCore.GetSteadyClockCore();
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(context.Thread);
ResultCode result = _clockCore.GetSystemClockContext(context.Thread, out SystemClockContext clockContext);
@ -50,7 +50,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
}
long posixTime = context.RequestData.ReadInt64();
StandardSteadyClockCore steadyClockCore = _clockCore.GetSteadyClockCore();
SteadyClockCore steadyClockCore = _clockCore.GetSteadyClockCore();
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(context.Thread);
SystemClockContext clockContext = new SystemClockContext()

View file

@ -56,7 +56,6 @@ namespace Ryujinx.HLE.HOS.Services.Time
// LoadLocationNameList(u32 index) -> (u32 outCount, buffer<nn::time::LocationName, 6>)
public ResultCode LoadLocationNameList(ServiceCtx context)
{
// TODO: fix logic to use index
uint index = context.RequestData.ReadUInt32();
long bufferPosition = context.Request.ReceiveBuff[0].Position;
long bufferSize = context.Request.ReceiveBuff[0].Size;