ITimeZoneServiceForPsc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Cpu;
  4. using Ryujinx.HLE.HOS.Services.Time.Clock;
  5. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  6. using Ryujinx.HLE.Utilities;
  7. using System;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Text;
  11. namespace Ryujinx.HLE.HOS.Services.Time.StaticService
  12. {
  13. class ITimeZoneServiceForPsc : IpcService
  14. {
  15. private TimeZoneManager _timeZoneManager;
  16. private bool _writePermission;
  17. public ITimeZoneServiceForPsc(TimeZoneManager timeZoneManager, bool writePermission)
  18. {
  19. _timeZoneManager = timeZoneManager;
  20. _writePermission = writePermission;
  21. }
  22. [CommandHipc(0)]
  23. // GetDeviceLocationName() -> nn::time::LocationName
  24. public ResultCode GetDeviceLocationName(ServiceCtx context)
  25. {
  26. ResultCode result = _timeZoneManager.GetDeviceLocationName(out string deviceLocationName);
  27. if (result == ResultCode.Success)
  28. {
  29. WriteLocationName(context, deviceLocationName);
  30. }
  31. return result;
  32. }
  33. [CommandHipc(1)]
  34. // SetDeviceLocationName(nn::time::LocationName)
  35. public ResultCode SetDeviceLocationName(ServiceCtx context)
  36. {
  37. if (!_writePermission)
  38. {
  39. return ResultCode.PermissionDenied;
  40. }
  41. return ResultCode.NotImplemented;
  42. }
  43. [CommandHipc(2)]
  44. // GetTotalLocationNameCount() -> u32
  45. public ResultCode GetTotalLocationNameCount(ServiceCtx context)
  46. {
  47. ResultCode result = _timeZoneManager.GetTotalLocationNameCount(out uint totalLocationNameCount);
  48. if (result == ResultCode.Success)
  49. {
  50. context.ResponseData.Write(totalLocationNameCount);
  51. }
  52. return ResultCode.Success;
  53. }
  54. [CommandHipc(3)]
  55. // LoadLocationNameList(u32 index) -> (u32 outCount, buffer<nn::time::LocationName, 6>)
  56. public ResultCode LoadLocationNameList(ServiceCtx context)
  57. {
  58. return ResultCode.NotImplemented;
  59. }
  60. [CommandHipc(4)]
  61. // LoadTimeZoneRule(nn::time::LocationName locationName) -> buffer<nn::time::TimeZoneRule, 0x16>
  62. public ResultCode LoadTimeZoneRule(ServiceCtx context)
  63. {
  64. return ResultCode.NotImplemented;
  65. }
  66. [CommandHipc(5)] // 2.0.0+
  67. // GetTimeZoneRuleVersion() -> nn::time::TimeZoneRuleVersion
  68. public ResultCode GetTimeZoneRuleVersion(ServiceCtx context)
  69. {
  70. ResultCode result = _timeZoneManager.GetTimeZoneRuleVersion(out UInt128 timeZoneRuleVersion);
  71. if (result == ResultCode.Success)
  72. {
  73. context.ResponseData.WriteStruct(timeZoneRuleVersion);
  74. }
  75. return result;
  76. }
  77. [CommandHipc(6)] // 5.0.0+
  78. // GetDeviceLocationNameAndUpdatedTime() -> (nn::time::LocationName, nn::time::SteadyClockTimePoint)
  79. public ResultCode GetDeviceLocationNameAndUpdatedTime(ServiceCtx context)
  80. {
  81. ResultCode result = _timeZoneManager.GetDeviceLocationName(out string deviceLocationName);
  82. if (result == ResultCode.Success)
  83. {
  84. result = _timeZoneManager.GetUpdatedTime(out SteadyClockTimePoint timeZoneUpdateTimePoint);
  85. if (result == ResultCode.Success)
  86. {
  87. WriteLocationName(context, deviceLocationName);
  88. // Skip padding
  89. context.ResponseData.BaseStream.Position += 0x4;
  90. context.ResponseData.WriteStruct(timeZoneUpdateTimePoint);
  91. }
  92. }
  93. return result;
  94. }
  95. [CommandHipc(7)] // 9.0.0+
  96. // SetDeviceLocationNameWithTimeZoneRule(nn::time::LocationName locationName, buffer<nn::time::TimeZoneBinary, 0x21> timeZoneBinary)
  97. public ResultCode SetDeviceLocationNameWithTimeZoneRule(ServiceCtx context)
  98. {
  99. if (!_writePermission)
  100. {
  101. return ResultCode.PermissionDenied;
  102. }
  103. (ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
  104. string locationName = StringUtils.ReadInlinedAsciiString(context.RequestData, 0x24);
  105. ResultCode result;
  106. byte[] temp = new byte[bufferSize];
  107. context.Memory.Read(bufferPosition, temp);
  108. using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp))
  109. {
  110. result = _timeZoneManager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream);
  111. }
  112. return result;
  113. }
  114. [CommandHipc(8)] // 9.0.0+
  115. // ParseTimeZoneBinary(buffer<nn::time::TimeZoneBinary, 0x21> timeZoneBinary) -> buffer<nn::time::TimeZoneRule, 0x16>
  116. public ResultCode ParseTimeZoneBinary(ServiceCtx context)
  117. {
  118. (ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
  119. ulong timeZoneRuleBufferPosition = context.Request.ReceiveBuff[0].Position;
  120. ulong timeZoneRuleBufferSize = context.Request.ReceiveBuff[0].Size;
  121. if (timeZoneRuleBufferSize != 0x4000)
  122. {
  123. // TODO: find error code here
  124. Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{timeZoneRuleBufferSize:x} (expected 0x4000)");
  125. throw new InvalidOperationException();
  126. }
  127. ResultCode result;
  128. byte[] temp = new byte[bufferSize];
  129. context.Memory.Read(bufferPosition, temp);
  130. using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp))
  131. {
  132. result = _timeZoneManager.ParseTimeZoneRuleBinary(out TimeZoneRule timeZoneRule, timeZoneBinaryStream);
  133. if (result == ResultCode.Success)
  134. {
  135. MemoryHelper.Write(context.Memory, timeZoneRuleBufferPosition, timeZoneRule);
  136. }
  137. }
  138. return result;
  139. }
  140. [CommandHipc(20)] // 9.0.0+
  141. // GetDeviceLocationNameOperationEventReadableHandle() -> handle<copy>
  142. public ResultCode GetDeviceLocationNameOperationEventReadableHandle(ServiceCtx context)
  143. {
  144. return ResultCode.NotImplemented;
  145. }
  146. [CommandHipc(100)]
  147. // ToCalendarTime(nn::time::PosixTime time, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
  148. public ResultCode ToCalendarTime(ServiceCtx context)
  149. {
  150. long posixTime = context.RequestData.ReadInt64();
  151. ulong bufferPosition = context.Request.SendBuff[0].Position;
  152. ulong bufferSize = context.Request.SendBuff[0].Size;
  153. if (bufferSize != 0x4000)
  154. {
  155. // TODO: find error code here
  156. Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
  157. throw new InvalidOperationException();
  158. }
  159. TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, bufferPosition);
  160. ResultCode resultCode = _timeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar);
  161. if (resultCode == 0)
  162. {
  163. context.ResponseData.WriteStruct(calendar);
  164. }
  165. return resultCode;
  166. }
  167. [CommandHipc(101)]
  168. // ToCalendarTimeWithMyRule(nn::time::PosixTime) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
  169. public ResultCode ToCalendarTimeWithMyRule(ServiceCtx context)
  170. {
  171. long posixTime = context.RequestData.ReadInt64();
  172. ResultCode resultCode = _timeZoneManager.ToCalendarTimeWithMyRules(posixTime, out CalendarInfo calendar);
  173. if (resultCode == ResultCode.Success)
  174. {
  175. context.ResponseData.WriteStruct(calendar);
  176. }
  177. return resultCode;
  178. }
  179. [CommandHipc(201)]
  180. // ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
  181. public ResultCode ToPosixTime(ServiceCtx context)
  182. {
  183. ulong inBufferPosition = context.Request.SendBuff[0].Position;
  184. ulong inBufferSize = context.Request.SendBuff[0].Size;
  185. CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
  186. if (inBufferSize != 0x4000)
  187. {
  188. // TODO: find error code here
  189. Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{inBufferSize:x} (expected 0x4000)");
  190. throw new InvalidOperationException();
  191. }
  192. TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, inBufferPosition);
  193. ResultCode resultCode = _timeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
  194. if (resultCode == ResultCode.Success)
  195. {
  196. ulong outBufferPosition = context.Request.RecvListBuff[0].Position;
  197. ulong outBufferSize = context.Request.RecvListBuff[0].Size;
  198. context.Memory.Write(outBufferPosition, posixTime);
  199. context.ResponseData.Write(1);
  200. }
  201. return resultCode;
  202. }
  203. [CommandHipc(202)]
  204. // ToPosixTimeWithMyRule(nn::time::CalendarTime calendarTime) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
  205. public ResultCode ToPosixTimeWithMyRule(ServiceCtx context)
  206. {
  207. CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
  208. ResultCode resultCode = _timeZoneManager.ToPosixTimeWithMyRules(calendarTime, out long posixTime);
  209. if (resultCode == ResultCode.Success)
  210. {
  211. ulong outBufferPosition = context.Request.RecvListBuff[0].Position;
  212. ulong outBufferSize = context.Request.RecvListBuff[0].Size;
  213. context.Memory.Write(outBufferPosition, posixTime);
  214. // There could be only one result on one calendar as leap seconds aren't supported.
  215. context.ResponseData.Write(1);
  216. }
  217. return resultCode;
  218. }
  219. private void WriteLocationName(ServiceCtx context, string locationName)
  220. {
  221. char[] locationNameArray = locationName.ToCharArray();
  222. int padding = 0x24 - locationNameArray.Length;
  223. Debug.Assert(padding >= 0, "LocationName exceeded limit (0x24 bytes)");
  224. context.ResponseData.Write(locationNameArray);
  225. for (int index = 0; index < padding; index++)
  226. {
  227. context.ResponseData.Write((byte)0);
  228. }
  229. }
  230. }
  231. }