IStaticService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Ryujinx.HLE.HOS.Ipc;
  2. using Ryujinx.HLE.HOS.Kernel.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.HLE.HOS.Services.Time
  6. {
  7. class IStaticService : IpcService
  8. {
  9. private int _timeSharedMemoryNativeHandle = 0;
  10. private Dictionary<int, ServiceProcessRequest> _commands;
  11. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  12. private static readonly DateTime StartupDate = DateTime.UtcNow;
  13. public IStaticService()
  14. {
  15. _commands = new Dictionary<int, ServiceProcessRequest>
  16. {
  17. { 0, GetStandardUserSystemClock },
  18. { 1, GetStandardNetworkSystemClock },
  19. { 2, GetStandardSteadyClock },
  20. { 3, GetTimeZoneService },
  21. { 4, GetStandardLocalSystemClock },
  22. //{ 5, GetEphemeralNetworkSystemClock }, // 4.0.0+
  23. { 20, GetSharedMemoryNativeHandle }, // 6.0.0+
  24. //{ 30, GetStandardNetworkClockOperationEventReadableHandle }, // 6.0.0+
  25. //{ 31, GetEphemeralNetworkClockOperationEventReadableHandle }, // 6.0.0+
  26. //{ 50, SetStandardSteadyClockInternalOffset }, // 4.0.0+
  27. //{ 100, IsStandardUserSystemClockAutomaticCorrectionEnabled },
  28. //{ 101, SetStandardUserSystemClockAutomaticCorrectionEnabled },
  29. //{ 102, GetStandardUserSystemClockInitialYear }, // 5.0.0+
  30. //{ 200, IsStandardNetworkSystemClockAccuracySufficient }, // 3.0.0+
  31. //{ 201, GetStandardUserSystemClockAutomaticCorrectionUpdatedTime }, // 6.0.0+
  32. { 300, CalculateMonotonicSystemClockBaseTimePoint }, // 4.0.0+
  33. //{ 400, GetClockSnapshot }, // 4.0.0+
  34. //{ 401, GetClockSnapshotFromSystemClockContext }, // 4.0.0+
  35. //{ 500, CalculateStandardUserSystemClockDifferenceByUser }, // 4.0.0+
  36. //{ 501, CalculateSpanBetween }, // 4.0.0+
  37. };
  38. }
  39. // GetStandardUserSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  40. public long GetStandardUserSystemClock(ServiceCtx context)
  41. {
  42. MakeObject(context, new ISystemClock(SystemClockType.User));
  43. return 0;
  44. }
  45. // GetStandardNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  46. public long GetStandardNetworkSystemClock(ServiceCtx context)
  47. {
  48. MakeObject(context, new ISystemClock(SystemClockType.Network));
  49. return 0;
  50. }
  51. // GetStandardSteadyClock() -> object<nn::timesrv::detail::service::ISteadyClock>
  52. public long GetStandardSteadyClock(ServiceCtx context)
  53. {
  54. MakeObject(context, new ISteadyClock());
  55. return 0;
  56. }
  57. // GetTimeZoneService() -> object<nn::timesrv::detail::service::ITimeZoneService>
  58. public long GetTimeZoneService(ServiceCtx context)
  59. {
  60. MakeObject(context, new ITimeZoneService());
  61. return 0;
  62. }
  63. // GetStandardLocalSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  64. public long GetStandardLocalSystemClock(ServiceCtx context)
  65. {
  66. MakeObject(context, new ISystemClock(SystemClockType.Local));
  67. return 0;
  68. }
  69. // GetSharedMemoryNativeHandle() -> handle<copy>
  70. public long GetSharedMemoryNativeHandle(ServiceCtx context)
  71. {
  72. if (_timeSharedMemoryNativeHandle == 0)
  73. {
  74. if (context.Process.HandleTable.GenerateHandle(context.Device.System.TimeSharedMem, out _timeSharedMemoryNativeHandle) != KernelResult.Success)
  75. {
  76. throw new InvalidOperationException("Out of handles!");
  77. }
  78. }
  79. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_timeSharedMemoryNativeHandle);
  80. return 0;
  81. }
  82. // CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> u64
  83. public long CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
  84. {
  85. long timeOffset = (long)(DateTime.UtcNow - StartupDate).TotalSeconds;
  86. long systemClockContextEpoch = context.RequestData.ReadInt64();
  87. context.ResponseData.Write(timeOffset + systemClockContextEpoch);
  88. return 0;
  89. }
  90. }
  91. }