IStaticService.cs 14 KB

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