IStaticService.cs 4.8 KB

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