TimeManager.cs 8.3 KB

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