TimeManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.IO;
  3. using Ryujinx.HLE.Exceptions;
  4. using Ryujinx.HLE.HOS.Kernel.Memory;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using Ryujinx.HLE.HOS.Services.Time.Clock;
  7. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  8. using Ryujinx.HLE.Utilities;
  9. namespace Ryujinx.HLE.HOS.Services.Time
  10. {
  11. class TimeManager
  12. {
  13. private static TimeManager _instance;
  14. public static TimeManager Instance
  15. {
  16. get
  17. {
  18. if (_instance == null)
  19. {
  20. _instance = new TimeManager();
  21. }
  22. return _instance;
  23. }
  24. }
  25. public StandardSteadyClockCore StandardSteadyClock { get; }
  26. public TickBasedSteadyClockCore TickBasedSteadyClock { get; }
  27. public StandardLocalSystemClockCore StandardLocalSystemClock { get; }
  28. public StandardNetworkSystemClockCore StandardNetworkSystemClock { get; }
  29. public StandardUserSystemClockCore StandardUserSystemClock { get; }
  30. public TimeZoneContentManager TimeZone { get; }
  31. public EphemeralNetworkSystemClockCore EphemeralNetworkSystemClock { get; }
  32. public TimeSharedMemory SharedMemory { get; }
  33. public LocalSystemClockContextWriter LocalClockContextWriter { get; }
  34. public NetworkSystemClockContextWriter NetworkClockContextWriter { get; }
  35. public EphemeralNetworkSystemClockContextWriter EphemeralClockContextWriter { get; }
  36. // TODO: 9.0.0+ power states and alarms
  37. public TimeManager()
  38. {
  39. StandardSteadyClock = new StandardSteadyClockCore();
  40. TickBasedSteadyClock = new TickBasedSteadyClockCore();
  41. StandardLocalSystemClock = new StandardLocalSystemClockCore(StandardSteadyClock);
  42. StandardNetworkSystemClock = new StandardNetworkSystemClockCore(StandardSteadyClock);
  43. StandardUserSystemClock = new StandardUserSystemClockCore(StandardLocalSystemClock, StandardNetworkSystemClock);
  44. TimeZone = new TimeZoneContentManager();
  45. EphemeralNetworkSystemClock = new EphemeralNetworkSystemClockCore(StandardSteadyClock);
  46. SharedMemory = new TimeSharedMemory();
  47. LocalClockContextWriter = new LocalSystemClockContextWriter(SharedMemory);
  48. NetworkClockContextWriter = new NetworkSystemClockContextWriter(SharedMemory);
  49. EphemeralClockContextWriter = new EphemeralNetworkSystemClockContextWriter();
  50. }
  51. public void Initialize(Switch device, Horizon system, KSharedMemory sharedMemory, long timeSharedMemoryAddress, int timeSharedMemorySize)
  52. {
  53. SharedMemory.Initialize(device, sharedMemory, timeSharedMemoryAddress, timeSharedMemorySize);
  54. // Here we use system on purpose as device. System isn't initialized at this point.
  55. StandardUserSystemClock.CreateAutomaticCorrectionEvent(system);
  56. }
  57. public void InitializeTimeZone(Switch device)
  58. {
  59. TimeZone.Initialize(this, device);
  60. }
  61. public void SetupStandardSteadyClock(KThread thread, UInt128 clockSourceId, TimeSpanType setupValue, TimeSpanType internalOffset, TimeSpanType testOffset, bool isRtcResetDetected)
  62. {
  63. SetupInternalStandardSteadyClock(clockSourceId, setupValue, internalOffset, testOffset, isRtcResetDetected);
  64. TimeSpanType currentTimePoint = StandardSteadyClock.GetCurrentRawTimePoint(thread);
  65. SharedMemory.SetupStandardSteadyClock(thread, clockSourceId, currentTimePoint);
  66. // TODO: propagate IPC late binding of "time:s" and "time:p"
  67. }
  68. private void SetupInternalStandardSteadyClock(UInt128 clockSourceId, TimeSpanType setupValue, TimeSpanType internalOffset, TimeSpanType testOffset, bool isRtcResetDetected)
  69. {
  70. StandardSteadyClock.SetClockSourceId(clockSourceId);
  71. StandardSteadyClock.SetSetupValue(setupValue);
  72. StandardSteadyClock.SetInternalOffset(internalOffset);
  73. StandardSteadyClock.SetTestOffset(testOffset);
  74. if (isRtcResetDetected)
  75. {
  76. StandardSteadyClock.SetRtcReset();
  77. }
  78. StandardSteadyClock.MarkInitialized();
  79. // TODO: propagate IPC late binding of "time:s" and "time:p"
  80. }
  81. public void SetupStandardLocalSystemClock(KThread thread, SystemClockContext clockContext, long posixTime)
  82. {
  83. StandardLocalSystemClock.SetUpdateCallbackInstance(LocalClockContextWriter);
  84. SteadyClockTimePoint currentTimePoint = StandardLocalSystemClock.GetSteadyClockCore().GetCurrentTimePoint(thread);
  85. if (currentTimePoint.ClockSourceId == clockContext.SteadyTimePoint.ClockSourceId)
  86. {
  87. StandardLocalSystemClock.SetSystemClockContext(clockContext);
  88. }
  89. else
  90. {
  91. if (StandardLocalSystemClock.SetCurrentTime(thread, posixTime) != ResultCode.Success)
  92. {
  93. throw new InternalServiceException("Cannot set current local time");
  94. }
  95. }
  96. StandardLocalSystemClock.MarkInitialized();
  97. // TODO: propagate IPC late binding of "time:s" and "time:p"
  98. }
  99. public void SetupStandardNetworkSystemClock(SystemClockContext clockContext, TimeSpanType sufficientAccuracy)
  100. {
  101. StandardNetworkSystemClock.SetUpdateCallbackInstance(NetworkClockContextWriter);
  102. if (StandardNetworkSystemClock.SetSystemClockContext(clockContext) != ResultCode.Success)
  103. {
  104. throw new InternalServiceException("Cannot set network SystemClockContext");
  105. }
  106. StandardNetworkSystemClock.SetStandardNetworkClockSufficientAccuracy(sufficientAccuracy);
  107. StandardNetworkSystemClock.MarkInitialized();
  108. // TODO: propagate IPC late binding of "time:s" and "time:p"
  109. }
  110. public void SetupTimeZoneManager(string locationName, SteadyClockTimePoint timeZoneUpdatedTimePoint, uint totalLocationNameCount, UInt128 timeZoneRuleVersion, Stream timeZoneBinaryStream)
  111. {
  112. if (TimeZone.Manager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream) != ResultCode.Success)
  113. {
  114. throw new InternalServiceException("Cannot set DeviceLocationName with a given TimeZoneBinary");
  115. }
  116. TimeZone.Manager.SetUpdatedTime(timeZoneUpdatedTimePoint, true);
  117. TimeZone.Manager.SetTotalLocationNameCount(totalLocationNameCount);
  118. TimeZone.Manager.SetTimeZoneRuleVersion(timeZoneRuleVersion);
  119. TimeZone.Manager.MarkInitialized();
  120. // TODO: propagate IPC late binding of "time:s" and "time:p"
  121. }
  122. public void SetupEphemeralNetworkSystemClock()
  123. {
  124. EphemeralNetworkSystemClock.SetUpdateCallbackInstance(EphemeralClockContextWriter);
  125. EphemeralNetworkSystemClock.MarkInitialized();
  126. // TODO: propagate IPC late binding of "time:s" and "time:p"
  127. }
  128. public void SetupStandardUserSystemClock(KThread thread, bool isAutomaticCorrectionEnabled, SteadyClockTimePoint steadyClockTimePoint)
  129. {
  130. if (StandardUserSystemClock.SetAutomaticCorrectionEnabled(thread, isAutomaticCorrectionEnabled) != ResultCode.Success)
  131. {
  132. throw new InternalServiceException("Cannot set automatic user time correction state");
  133. }
  134. StandardUserSystemClock.SetAutomaticCorrectionUpdatedTime(steadyClockTimePoint);
  135. StandardUserSystemClock.MarkInitialized();
  136. SharedMemory.SetAutomaticCorrectionEnabled(isAutomaticCorrectionEnabled);
  137. // TODO: propagate IPC late binding of "time:s" and "time:p"
  138. }
  139. public void SetStandardSteadyClockRtcOffset(KThread thread, TimeSpanType rtcOffset)
  140. {
  141. StandardSteadyClock.SetSetupValue(rtcOffset);
  142. TimeSpanType currentTimePoint = StandardSteadyClock.GetCurrentRawTimePoint(thread);
  143. SharedMemory.SetSteadyClockRawTimePoint(thread, currentTimePoint);
  144. }
  145. }
  146. }