ITimeZoneServiceForGlue.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  4. using Ryujinx.HLE.Utilities;
  5. using System;
  6. using System.Text;
  7. namespace Ryujinx.HLE.HOS.Services.Time.StaticService
  8. {
  9. class ITimeZoneServiceForGlue : IpcService
  10. {
  11. private TimeZoneContentManager _timeZoneContentManager;
  12. private ITimeZoneServiceForPsc _inner;
  13. private bool _writePermission;
  14. public ITimeZoneServiceForGlue(TimeZoneContentManager timeZoneContentManager, bool writePermission)
  15. {
  16. _timeZoneContentManager = timeZoneContentManager;
  17. _writePermission = writePermission;
  18. _inner = new ITimeZoneServiceForPsc(timeZoneContentManager.Manager, writePermission);
  19. }
  20. [CommandHipc(0)]
  21. // GetDeviceLocationName() -> nn::time::LocationName
  22. public ResultCode GetDeviceLocationName(ServiceCtx context)
  23. {
  24. return _inner.GetDeviceLocationName(context);
  25. }
  26. [CommandHipc(1)]
  27. // SetDeviceLocationName(nn::time::LocationName)
  28. public ResultCode SetDeviceLocationName(ServiceCtx context)
  29. {
  30. if (!_writePermission)
  31. {
  32. return ResultCode.PermissionDenied;
  33. }
  34. string locationName = StringUtils.ReadInlinedAsciiString(context.RequestData, 0x24);
  35. return _timeZoneContentManager.SetDeviceLocationName(locationName);
  36. }
  37. [CommandHipc(2)]
  38. // GetTotalLocationNameCount() -> u32
  39. public ResultCode GetTotalLocationNameCount(ServiceCtx context)
  40. {
  41. return _inner.GetTotalLocationNameCount(context);
  42. }
  43. [CommandHipc(3)]
  44. // LoadLocationNameList(u32 index) -> (u32 outCount, buffer<nn::time::LocationName, 6>)
  45. public ResultCode LoadLocationNameList(ServiceCtx context)
  46. {
  47. uint index = context.RequestData.ReadUInt32();
  48. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  49. ulong bufferSize = context.Request.ReceiveBuff[0].Size;
  50. ResultCode errorCode = _timeZoneContentManager.LoadLocationNameList(index, out string[] locationNameArray, (uint)bufferSize / 0x24);
  51. if (errorCode == 0)
  52. {
  53. uint offset = 0;
  54. foreach (string locationName in locationNameArray)
  55. {
  56. int padding = 0x24 - locationName.Length;
  57. if (padding < 0)
  58. {
  59. return ResultCode.LocationNameTooLong;
  60. }
  61. context.Memory.Write(bufferPosition + offset, Encoding.ASCII.GetBytes(locationName));
  62. MemoryHelper.FillWithZeros(context.Memory, bufferPosition + offset + (ulong)locationName.Length, padding);
  63. offset += 0x24;
  64. }
  65. context.ResponseData.Write((uint)locationNameArray.Length);
  66. }
  67. return errorCode;
  68. }
  69. [CommandHipc(4)]
  70. // LoadTimeZoneRule(nn::time::LocationName locationName) -> buffer<nn::time::TimeZoneRule, 0x16>
  71. public ResultCode LoadTimeZoneRule(ServiceCtx context)
  72. {
  73. ulong bufferPosition = context.Request.ReceiveBuff[0].Position;
  74. ulong bufferSize = context.Request.ReceiveBuff[0].Size;
  75. if (bufferSize != 0x4000)
  76. {
  77. // TODO: find error code here
  78. Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
  79. throw new InvalidOperationException();
  80. }
  81. string locationName = StringUtils.ReadInlinedAsciiString(context.RequestData, 0x24);
  82. ResultCode resultCode = _timeZoneContentManager.LoadTimeZoneRule(out TimeZoneRule rules, locationName);
  83. // Write TimeZoneRule if success
  84. if (resultCode == ResultCode.Success)
  85. {
  86. MemoryHelper.Write(context.Memory, bufferPosition, rules);
  87. }
  88. return resultCode;
  89. }
  90. [CommandHipc(100)]
  91. // ToCalendarTime(nn::time::PosixTime time, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
  92. public ResultCode ToCalendarTime(ServiceCtx context)
  93. {
  94. return _inner.ToCalendarTime(context);
  95. }
  96. [CommandHipc(101)]
  97. // ToCalendarTimeWithMyRule(nn::time::PosixTime) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
  98. public ResultCode ToCalendarTimeWithMyRule(ServiceCtx context)
  99. {
  100. return _inner.ToCalendarTimeWithMyRule(context);
  101. }
  102. [CommandHipc(201)]
  103. // ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
  104. public ResultCode ToPosixTime(ServiceCtx context)
  105. {
  106. return _inner.ToPosixTime(context);
  107. }
  108. [CommandHipc(202)]
  109. // ToPosixTimeWithMyRule(nn::time::CalendarTime calendarTime) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
  110. public ResultCode ToPosixTimeWithMyRule(ServiceCtx context)
  111. {
  112. return _inner.ToPosixTimeWithMyRule(context);
  113. }
  114. }
  115. }