IStaticService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using Ryujinx.HLE.HOS.Services.Time.Clock;
  6. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. namespace Ryujinx.HLE.HOS.Services.Time
  12. {
  13. [Service("time:a", TimePermissions.Applet)]
  14. [Service("time:s", TimePermissions.System)]
  15. [Service("time:u", TimePermissions.User)]
  16. class IStaticService : IpcService
  17. {
  18. private TimePermissions _permissions;
  19. private int _timeSharedMemoryNativeHandle = 0;
  20. private static readonly DateTime StartupDate = DateTime.UtcNow;
  21. public IStaticService(ServiceCtx context, TimePermissions permissions)
  22. {
  23. _permissions = permissions;
  24. }
  25. [Command(0)]
  26. // GetStandardUserSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  27. public ResultCode GetStandardUserSystemClock(ServiceCtx context)
  28. {
  29. MakeObject(context, new ISystemClock(StandardUserSystemClockCore.Instance, (_permissions & TimePermissions.UserSystemClockWritableMask) != 0));
  30. return ResultCode.Success;
  31. }
  32. [Command(1)]
  33. // GetStandardNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  34. public ResultCode GetStandardNetworkSystemClock(ServiceCtx context)
  35. {
  36. MakeObject(context, new ISystemClock(StandardNetworkSystemClockCore.Instance, (_permissions & TimePermissions.NetworkSystemClockWritableMask) != 0));
  37. return ResultCode.Success;
  38. }
  39. [Command(2)]
  40. // GetStandardSteadyClock() -> object<nn::timesrv::detail::service::ISteadyClock>
  41. public ResultCode GetStandardSteadyClock(ServiceCtx context)
  42. {
  43. MakeObject(context, new ISteadyClock());
  44. return ResultCode.Success;
  45. }
  46. [Command(3)]
  47. // GetTimeZoneService() -> object<nn::timesrv::detail::service::ITimeZoneService>
  48. public ResultCode GetTimeZoneService(ServiceCtx context)
  49. {
  50. MakeObject(context, new ITimeZoneService());
  51. return ResultCode.Success;
  52. }
  53. [Command(4)]
  54. // GetStandardLocalSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  55. public ResultCode GetStandardLocalSystemClock(ServiceCtx context)
  56. {
  57. MakeObject(context, new ISystemClock(StandardLocalSystemClockCore.Instance, (_permissions & TimePermissions.LocalSystemClockWritableMask) != 0));
  58. return ResultCode.Success;
  59. }
  60. [Command(5)] // 4.0.0+
  61. // GetEphemeralNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  62. public ResultCode GetEphemeralNetworkSystemClock(ServiceCtx context)
  63. {
  64. MakeObject(context, new ISystemClock(StandardNetworkSystemClockCore.Instance, false));
  65. return ResultCode.Success;
  66. }
  67. [Command(20)] // 6.0.0+
  68. // GetSharedMemoryNativeHandle() -> handle<copy>
  69. public ResultCode GetSharedMemoryNativeHandle(ServiceCtx context)
  70. {
  71. if (_timeSharedMemoryNativeHandle == 0)
  72. {
  73. if (context.Process.HandleTable.GenerateHandle(context.Device.System.TimeSharedMem, out _timeSharedMemoryNativeHandle) != KernelResult.Success)
  74. {
  75. throw new InvalidOperationException("Out of handles!");
  76. }
  77. }
  78. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_timeSharedMemoryNativeHandle);
  79. return ResultCode.Success;
  80. }
  81. [Command(100)]
  82. // IsStandardUserSystemClockAutomaticCorrectionEnabled() -> bool
  83. public ResultCode IsStandardUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
  84. {
  85. context.ResponseData.Write(StandardUserSystemClockCore.Instance.IsAutomaticCorrectionEnabled());
  86. return ResultCode.Success;
  87. }
  88. [Command(101)]
  89. // SetStandardUserSystemClockAutomaticCorrectionEnabled(b8)
  90. public ResultCode SetStandardUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
  91. {
  92. if ((_permissions & TimePermissions.UserSystemClockWritableMask) == 0)
  93. {
  94. return ResultCode.PermissionDenied;
  95. }
  96. bool autoCorrectionEnabled = context.RequestData.ReadBoolean();
  97. return StandardUserSystemClockCore.Instance.SetAutomaticCorrectionEnabled(context.Thread, autoCorrectionEnabled);
  98. }
  99. [Command(200)] // 3.0.0+
  100. // IsStandardNetworkSystemClockAccuracySufficient() -> bool
  101. public ResultCode IsStandardNetworkSystemClockAccuracySufficient(ServiceCtx context)
  102. {
  103. context.ResponseData.Write(StandardNetworkSystemClockCore.Instance.IsStandardNetworkSystemClockAccuracySufficient(context.Thread));
  104. return ResultCode.Success;
  105. }
  106. [Command(300)] // 4.0.0+
  107. // CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64
  108. public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
  109. {
  110. SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
  111. SteadyClockTimePoint currentTimePoint = StandardSteadyClockCore.Instance.GetCurrentTimePoint(context.Thread);
  112. ResultCode result = ResultCode.TimeMismatch;
  113. if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId)
  114. {
  115. TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(context.Thread.Context.CntpctEl0, context.Thread.Context.CntfrqEl0);
  116. long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
  117. context.ResponseData.Write(baseTimePoint);
  118. result = 0;
  119. }
  120. return result;
  121. }
  122. [Command(400)] // 4.0.0+
  123. // GetClockSnapshot(u8) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
  124. public ResultCode GetClockSnapshot(ServiceCtx context)
  125. {
  126. byte type = context.RequestData.ReadByte();
  127. ResultCode result = StandardUserSystemClockCore.Instance.GetSystemClockContext(context.Thread, out SystemClockContext userContext);
  128. if (result == ResultCode.Success)
  129. {
  130. result = StandardNetworkSystemClockCore.Instance.GetSystemClockContext(context.Thread, out SystemClockContext networkContext);
  131. if (result == ResultCode.Success)
  132. {
  133. result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
  134. if (result == ResultCode.Success)
  135. {
  136. WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
  137. }
  138. }
  139. }
  140. return result;
  141. }
  142. [Command(401)] // 4.0.0+
  143. // GetClockSnapshotFromSystemClockContext(u8, nn::time::SystemClockContext, nn::time::SystemClockContext) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
  144. public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context)
  145. {
  146. byte type = context.RequestData.ReadByte();
  147. context.RequestData.BaseStream.Position += 7;
  148. SystemClockContext userContext = context.RequestData.ReadStruct<SystemClockContext>();
  149. SystemClockContext networkContext = context.RequestData.ReadStruct<SystemClockContext>();
  150. ResultCode result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
  151. if (result == ResultCode.Success)
  152. {
  153. WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
  154. }
  155. return result;
  156. }
  157. [Command(500)] // 4.0.0+
  158. // CalculateStandardUserSystemClockDifferenceByUser(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
  159. public ResultCode CalculateStandardUserSystemClockDifferenceByUser(ServiceCtx context)
  160. {
  161. ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[0]);
  162. ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[1]);
  163. TimeSpanType difference = TimeSpanType.FromSeconds(clockSnapshotB.UserContext.Offset - clockSnapshotA.UserContext.Offset);
  164. if (clockSnapshotB.UserContext.SteadyTimePoint.ClockSourceId != clockSnapshotA.UserContext.SteadyTimePoint.ClockSourceId || (clockSnapshotB.IsAutomaticCorrectionEnabled && clockSnapshotA.IsAutomaticCorrectionEnabled))
  165. {
  166. difference = new TimeSpanType(0);
  167. }
  168. context.ResponseData.Write(difference.NanoSeconds);
  169. return ResultCode.Success;
  170. }
  171. [Command(501)] // 4.0.0+
  172. // CalculateSpanBetween(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
  173. public ResultCode CalculateSpanBetween(ServiceCtx context)
  174. {
  175. ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[0]);
  176. ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[1]);
  177. TimeSpanType result;
  178. ResultCode resultCode = clockSnapshotA.SteadyClockTimePoint.GetSpanBetween(clockSnapshotB.SteadyClockTimePoint, out long timeSpan);
  179. if (resultCode != ResultCode.Success)
  180. {
  181. resultCode = ResultCode.TimeNotFound;
  182. if (clockSnapshotA.NetworkTime != 0 && clockSnapshotB.NetworkTime != 0)
  183. {
  184. result = TimeSpanType.FromSeconds(clockSnapshotB.NetworkTime - clockSnapshotA.NetworkTime);
  185. resultCode = ResultCode.Success;
  186. }
  187. else
  188. {
  189. return resultCode;
  190. }
  191. }
  192. else
  193. {
  194. result = TimeSpanType.FromSeconds(timeSpan);
  195. }
  196. context.ResponseData.Write(result.NanoSeconds);
  197. return resultCode;
  198. }
  199. private ResultCode GetClockSnapshotFromSystemClockContextInternal(KThread thread, SystemClockContext userContext, SystemClockContext networkContext, byte type, out ClockSnapshot clockSnapshot)
  200. {
  201. clockSnapshot = new ClockSnapshot();
  202. SteadyClockCore steadyClockCore = StandardSteadyClockCore.Instance;
  203. SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread);
  204. clockSnapshot.IsAutomaticCorrectionEnabled = StandardUserSystemClockCore.Instance.IsAutomaticCorrectionEnabled();
  205. clockSnapshot.UserContext = userContext;
  206. clockSnapshot.NetworkContext = networkContext;
  207. char[] tzName = TimeZoneManager.Instance.GetDeviceLocationName().ToCharArray();
  208. char[] locationName = new char[0x24];
  209. Array.Copy(tzName, locationName, tzName.Length);
  210. clockSnapshot.LocationName = locationName;
  211. ResultCode result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
  212. if (result == ResultCode.Success)
  213. {
  214. result = TimeZoneManager.Instance.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
  215. if (result == ResultCode.Success)
  216. {
  217. clockSnapshot.UserCalendarTime = userCalendarInfo.Time;
  218. clockSnapshot.UserCalendarAdditionalTime = userCalendarInfo.AdditionalInfo;
  219. if (ClockSnapshot.GetCurrentTime(out clockSnapshot.NetworkTime, currentTimePoint, clockSnapshot.NetworkContext) != ResultCode.Success)
  220. {
  221. clockSnapshot.NetworkTime = 0;
  222. }
  223. result = TimeZoneManager.Instance.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
  224. if (result == ResultCode.Success)
  225. {
  226. clockSnapshot.NetworkCalendarTime = networkCalendarInfo.Time;
  227. clockSnapshot.NetworkCalendarAdditionalTime = networkCalendarInfo.AdditionalInfo;
  228. clockSnapshot.Type = type;
  229. // Probably a version field?
  230. clockSnapshot.Unknown = 0;
  231. }
  232. }
  233. }
  234. return result;
  235. }
  236. private ClockSnapshot ReadClockSnapshotFromBuffer(ServiceCtx context, IpcBuffDesc ipcDesc)
  237. {
  238. Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
  239. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(ipcDesc.Position, ipcDesc.Size))))
  240. {
  241. return bufferReader.ReadStruct<ClockSnapshot>();
  242. }
  243. }
  244. private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot)
  245. {
  246. Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
  247. MemoryStream memory = new MemoryStream((int)ipcDesc.Size);
  248. using (BinaryWriter bufferWriter = new BinaryWriter(memory))
  249. {
  250. bufferWriter.WriteStruct(clockSnapshot);
  251. }
  252. context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray());
  253. memory.Dispose();
  254. }
  255. }
  256. }