From 3463c977dd2f8510ead343de4f167339edaed462 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 30 Jan 2021 02:14:12 -0700 Subject: [PATCH] Implement the rest of TimeServiceImpl --- src/LibHac/FsSrv/TimeService.cs | 54 ++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/src/LibHac/FsSrv/TimeService.cs b/src/LibHac/FsSrv/TimeService.cs index 4776b25e..93484802 100644 --- a/src/LibHac/FsSrv/TimeService.cs +++ b/src/LibHac/FsSrv/TimeService.cs @@ -1,6 +1,7 @@ -using System; +using System.Runtime.CompilerServices; using LibHac.Fs; using LibHac.FsSrv.Impl; +using LibHac.Os; namespace LibHac.FsSrv { @@ -23,7 +24,8 @@ namespace LibHac.FsSrv if (!programInfo.AccessControl.CanCall(OperationType.SetCurrentPosixTime)) return ResultFs.PermissionDenied.Log(); - return _serviceImpl.SetCurrentPosixTimeWithTimeDifference(currentTime, timeDifference); + _serviceImpl.SetCurrentPosixTimeWithTimeDifference(currentTime, timeDifference); + return Result.Success; } private Result GetProgramInfo(out ProgramInfo programInfo) @@ -35,16 +37,16 @@ namespace LibHac.FsSrv public class TimeServiceImpl { private Configuration _config; - private long _baseTime; + private long _basePosixTime; private int _timeDifference; - private object _lockObject; + private SdkMutexType _mutex; public TimeServiceImpl(in Configuration configuration) { _config = configuration; - _baseTime = 0; + _basePosixTime = 0; _timeDifference = 0; - _lockObject = new object(); + _mutex.Initialize(); } // The entire Configuration struct is a LibHac addition to avoid using global state @@ -54,19 +56,49 @@ namespace LibHac.FsSrv public ProgramRegistryImpl ProgramRegistry; } - public Result GetCurrentPosixTime(out long time) + private long GetSystemSeconds() { - throw new NotImplementedException(); + OsState os = _config.HorizonClient.Os; + + Tick tick = os.GetSystemTick(); + TimeSpan timeSpan = os.ConvertToTimeSpan(tick); + return timeSpan.GetSeconds(); + } + + public Result GetCurrentPosixTime(out long currentTime) + { + return GetCurrentPosixTimeWithTimeDifference(out currentTime, out int _); } public Result GetCurrentPosixTimeWithTimeDifference(out long currentTime, out int timeDifference) { - throw new NotImplementedException(); + Unsafe.SkipInit(out currentTime); + Unsafe.SkipInit(out timeDifference); + + using ScopedLock lk = ScopedLock.Lock(ref _mutex); + + if (_basePosixTime == 0) + return ResultFs.NotInitialized.Log(); + + if (!Unsafe.IsNullRef(ref currentTime)) + { + currentTime = _basePosixTime + GetSystemSeconds(); + } + + if (!Unsafe.IsNullRef(ref timeDifference)) + { + timeDifference = _timeDifference; + } + + return Result.Success; } - public Result SetCurrentPosixTimeWithTimeDifference(long currentTime, int timeDifference) + public void SetCurrentPosixTimeWithTimeDifference(long currentTime, int timeDifference) { - throw new NotImplementedException(); + using ScopedLock lk = ScopedLock.Lock(ref _mutex); + + _basePosixTime = currentTime - GetSystemSeconds(); + _timeDifference = timeDifference; } internal Result GetProgramInfo(out ProgramInfo programInfo, ulong processId)