2020-05-03 23:54:50 +01:00
|
|
|
|
using Ryujinx.Common;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
using Ryujinx.Common.Logging;
|
2020-05-03 23:54:50 +01:00
|
|
|
|
using Ryujinx.Cpu;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Time.TimeZone;
|
|
|
|
|
using Ryujinx.HLE.Utilities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time.StaticService
|
|
|
|
|
{
|
|
|
|
|
class ITimeZoneServiceForPsc : IpcService
|
|
|
|
|
{
|
|
|
|
|
private TimeZoneManager _timeZoneManager;
|
|
|
|
|
private bool _writePermission;
|
|
|
|
|
|
|
|
|
|
public ITimeZoneServiceForPsc(TimeZoneManager timeZoneManager, bool writePermission)
|
|
|
|
|
{
|
|
|
|
|
_timeZoneManager = timeZoneManager;
|
|
|
|
|
_writePermission = writePermission;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(0)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// GetDeviceLocationName() -> nn::time::LocationName
|
|
|
|
|
public ResultCode GetDeviceLocationName(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result = _timeZoneManager.GetDeviceLocationName(out string deviceLocationName);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
WriteLocationName(context, deviceLocationName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(1)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// SetDeviceLocationName(nn::time::LocationName)
|
|
|
|
|
public ResultCode SetDeviceLocationName(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
if (!_writePermission)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(2)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// GetTotalLocationNameCount() -> u32
|
|
|
|
|
public ResultCode GetTotalLocationNameCount(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result = _timeZoneManager.GetTotalLocationNameCount(out uint totalLocationNameCount);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
context.ResponseData.Write(totalLocationNameCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(3)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// LoadLocationNameList(u32 index) -> (u32 outCount, buffer<nn::time::LocationName, 6>)
|
|
|
|
|
public ResultCode LoadLocationNameList(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(4)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// LoadTimeZoneRule(nn::time::LocationName locationName) -> buffer<nn::time::TimeZoneRule, 0x16>
|
|
|
|
|
public ResultCode LoadTimeZoneRule(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(5)] // 2.0.0+
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// GetTimeZoneRuleVersion() -> nn::time::TimeZoneRuleVersion
|
|
|
|
|
public ResultCode GetTimeZoneRuleVersion(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result = _timeZoneManager.GetTimeZoneRuleVersion(out UInt128 timeZoneRuleVersion);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
context.ResponseData.WriteStruct(timeZoneRuleVersion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(6)] // 5.0.0+
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// GetDeviceLocationNameAndUpdatedTime() -> (nn::time::LocationName, nn::time::SteadyClockTimePoint)
|
|
|
|
|
public ResultCode GetDeviceLocationNameAndUpdatedTime(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
ResultCode result = _timeZoneManager.GetDeviceLocationName(out string deviceLocationName);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
result = _timeZoneManager.GetUpdatedTime(out SteadyClockTimePoint timeZoneUpdateTimePoint);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
WriteLocationName(context, deviceLocationName);
|
|
|
|
|
|
|
|
|
|
// Skip padding
|
|
|
|
|
context.ResponseData.BaseStream.Position += 0x4;
|
|
|
|
|
|
|
|
|
|
context.ResponseData.WriteStruct(timeZoneUpdateTimePoint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(7)] // 9.0.0+
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// SetDeviceLocationNameWithTimeZoneRule(nn::time::LocationName locationName, buffer<nn::time::TimeZoneBinary, 0x21> timeZoneBinary)
|
|
|
|
|
public ResultCode SetDeviceLocationNameWithTimeZoneRule(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
if (!_writePermission)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.PermissionDenied;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
2021-10-24 23:13:20 +01:00
|
|
|
|
string locationName = StringUtils.ReadInlinedAsciiString(context.RequestData, 0x24);
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
ResultCode result;
|
|
|
|
|
|
2020-05-03 23:54:50 +01:00
|
|
|
|
byte[] temp = new byte[bufferSize];
|
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
context.Memory.Read(bufferPosition, temp);
|
2020-05-03 23:54:50 +01:00
|
|
|
|
|
|
|
|
|
using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp))
|
2019-10-08 04:48:49 +01:00
|
|
|
|
{
|
|
|
|
|
result = _timeZoneManager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(8)] // 9.0.0+
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// ParseTimeZoneBinary(buffer<nn::time::TimeZoneBinary, 0x21> timeZoneBinary) -> buffer<nn::time::TimeZoneRule, 0x16>
|
|
|
|
|
public ResultCode ParseTimeZoneBinary(ServiceCtx context)
|
|
|
|
|
{
|
2021-04-24 11:16:01 +01:00
|
|
|
|
(ulong bufferPosition, ulong bufferSize) = context.Request.GetBufferType0x21();
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
ulong timeZoneRuleBufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
|
ulong timeZoneRuleBufferSize = context.Request.ReceiveBuff[0].Size;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
if (timeZoneRuleBufferSize != 0x4000)
|
|
|
|
|
{
|
|
|
|
|
// TODO: find error code here
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{timeZoneRuleBufferSize:x} (expected 0x4000)");
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResultCode result;
|
|
|
|
|
|
2020-05-03 23:54:50 +01:00
|
|
|
|
byte[] temp = new byte[bufferSize];
|
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
context.Memory.Read(bufferPosition, temp);
|
2020-05-03 23:54:50 +01:00
|
|
|
|
|
|
|
|
|
using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp))
|
2019-10-08 04:48:49 +01:00
|
|
|
|
{
|
|
|
|
|
result = _timeZoneManager.ParseTimeZoneRuleBinary(out TimeZoneRule timeZoneRule, timeZoneBinaryStream);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
|
|
|
|
MemoryHelper.Write(context.Memory, timeZoneRuleBufferPosition, timeZoneRule);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(20)] // 9.0.0+
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// GetDeviceLocationNameOperationEventReadableHandle() -> handle<copy>
|
|
|
|
|
public ResultCode GetDeviceLocationNameOperationEventReadableHandle(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.NotImplemented;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(100)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// ToCalendarTime(nn::time::PosixTime time, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
|
|
|
|
|
public ResultCode ToCalendarTime(ServiceCtx context)
|
|
|
|
|
{
|
2021-04-24 11:16:01 +01:00
|
|
|
|
long posixTime = context.RequestData.ReadInt64();
|
|
|
|
|
ulong bufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
|
ulong bufferSize = context.Request.SendBuff[0].Size;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
if (bufferSize != 0x4000)
|
|
|
|
|
{
|
|
|
|
|
// TODO: find error code here
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, bufferPosition);
|
|
|
|
|
|
|
|
|
|
ResultCode resultCode = _timeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar);
|
|
|
|
|
|
|
|
|
|
if (resultCode == 0)
|
|
|
|
|
{
|
|
|
|
|
context.ResponseData.WriteStruct(calendar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(101)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// ToCalendarTimeWithMyRule(nn::time::PosixTime) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
|
|
|
|
|
public ResultCode ToCalendarTimeWithMyRule(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
long posixTime = context.RequestData.ReadInt64();
|
|
|
|
|
|
|
|
|
|
ResultCode resultCode = _timeZoneManager.ToCalendarTimeWithMyRules(posixTime, out CalendarInfo calendar);
|
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
if (resultCode == ResultCode.Success)
|
2019-10-08 04:48:49 +01:00
|
|
|
|
{
|
|
|
|
|
context.ResponseData.WriteStruct(calendar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(201)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
|
|
|
|
|
public ResultCode ToPosixTime(ServiceCtx context)
|
|
|
|
|
{
|
2021-04-24 11:16:01 +01:00
|
|
|
|
ulong inBufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
|
ulong inBufferSize = context.Request.SendBuff[0].Size;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
|
|
|
|
|
|
|
|
|
|
if (inBufferSize != 0x4000)
|
|
|
|
|
{
|
|
|
|
|
// TODO: find error code here
|
2020-08-04 00:32:53 +01:00
|
|
|
|
Logger.Error?.Print(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{inBufferSize:x} (expected 0x4000)");
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, inBufferPosition);
|
|
|
|
|
|
|
|
|
|
ResultCode resultCode = _timeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
|
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
if (resultCode == ResultCode.Success)
|
2019-10-08 04:48:49 +01:00
|
|
|
|
{
|
2021-04-24 11:16:01 +01:00
|
|
|
|
ulong outBufferPosition = context.Request.RecvListBuff[0].Position;
|
|
|
|
|
ulong outBufferSize = context.Request.RecvListBuff[0].Size;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
context.Memory.Write(outBufferPosition, posixTime);
|
2019-10-08 04:48:49 +01:00
|
|
|
|
context.ResponseData.Write(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 23:01:24 +01:00
|
|
|
|
[CommandHipc(202)]
|
2019-10-08 04:48:49 +01:00
|
|
|
|
// ToPosixTimeWithMyRule(nn::time::CalendarTime calendarTime) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
|
|
|
|
|
public ResultCode ToPosixTimeWithMyRule(ServiceCtx context)
|
|
|
|
|
{
|
|
|
|
|
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
|
|
|
|
|
|
|
|
|
|
ResultCode resultCode = _timeZoneManager.ToPosixTimeWithMyRules(calendarTime, out long posixTime);
|
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
if (resultCode == ResultCode.Success)
|
2019-10-08 04:48:49 +01:00
|
|
|
|
{
|
2021-04-24 11:16:01 +01:00
|
|
|
|
ulong outBufferPosition = context.Request.RecvListBuff[0].Position;
|
|
|
|
|
ulong outBufferSize = context.Request.RecvListBuff[0].Size;
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
2021-04-24 11:16:01 +01:00
|
|
|
|
context.Memory.Write(outBufferPosition, posixTime);
|
2019-10-08 04:48:49 +01:00
|
|
|
|
|
|
|
|
|
// There could be only one result on one calendar as leap seconds aren't supported.
|
|
|
|
|
context.ResponseData.Write(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteLocationName(ServiceCtx context, string locationName)
|
|
|
|
|
{
|
|
|
|
|
char[] locationNameArray = locationName.ToCharArray();
|
|
|
|
|
|
|
|
|
|
int padding = 0x24 - locationNameArray.Length;
|
|
|
|
|
|
|
|
|
|
Debug.Assert(padding >= 0, "LocationName exceeded limit (0x24 bytes)");
|
|
|
|
|
|
|
|
|
|
context.ResponseData.Write(locationNameArray);
|
|
|
|
|
|
|
|
|
|
for (int index = 0; index < padding; index++)
|
|
|
|
|
{
|
|
|
|
|
context.ResponseData.Write((byte)0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|