ITimeZoneServiceForGlue.cs 5.3 KB

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