0
0
Fork 0
mirror of https://github.com/ryujinx-mirror/ryujinx.git synced 2024-12-27 20:25:44 +00:00
ryujinx-fork/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs
Thomas Guillemard 1aba033ba7 Update time implementation to 9.0.0 (#783)
* Fix 9.0.0 related services bindings

This was wrong because of a mistake on switchbrew.

* Fix wronog cmdid for ISteadyClock::GetTestOffset/SetTestOffset

* Update ClockCore logics to 9.0.0

Also apply 9.0.0 permissions and comment time:u, and time:a (as those
are going to be moved)

* Move every clocks instances + timezone to a global manager

* Start implementing time:m

Also prepare the skeleton of the shared memory

* Implement SystemClockContextUpdateCallback and co

* Update StaticService to 9.0.0

* Update ISystemClock to 9.0.0

* Rename IStaticService and add glue's IStaticService

* Implement psc's ITimeZoneService

* Integrate psc layer into glue for TimeZoneService

* Rename TimeZoneManagerForPsc => TimeZoneManager

* Use correct TimeZoneService interface for both StaticService implementations

* Accurately implement time shared memory operations

* Fix two critical flaws in TimeZone logic

The first one was the month range being different fron Nintendo one
(0-11 instead of 1-12)

The other flaw was a bad incrementation order during days & months
computation.

* Follow Nintendo's abort logic for TimeManager

* Avoid crashing when timezone sysarchive isn't present

* Update Readme

* Address comments

* Correctly align fields in ISystemClock

* Fix code style and some typos

* Improve timezone system archive warning/error messages

* Rearrange using definitions in Horizon.cs

* Address comments
2019-10-08 14:48:49 +11:00

294 lines
11 KiB
C#

using ARMeilleure.Memory;
using Ryujinx.Common;
using Ryujinx.Common.Logging;
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;
}
[Command(0)]
// 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;
}
[Command(1)]
// SetDeviceLocationName(nn::time::LocationName)
public ResultCode SetDeviceLocationName(ServiceCtx context)
{
if (!_writePermission)
{
return ResultCode.PermissionDenied;
}
return ResultCode.NotImplemented;
}
[Command(2)]
// 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;
}
[Command(3)]
// LoadLocationNameList(u32 index) -> (u32 outCount, buffer<nn::time::LocationName, 6>)
public ResultCode LoadLocationNameList(ServiceCtx context)
{
return ResultCode.NotImplemented;
}
[Command(4)]
// LoadTimeZoneRule(nn::time::LocationName locationName) -> buffer<nn::time::TimeZoneRule, 0x16>
public ResultCode LoadTimeZoneRule(ServiceCtx context)
{
return ResultCode.NotImplemented;
}
[Command(5)] // 2.0.0+
// 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;
}
[Command(6)] // 5.0.0+
// 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;
}
[Command(7)] // 9.0.0+
// SetDeviceLocationNameWithTimeZoneRule(nn::time::LocationName locationName, buffer<nn::time::TimeZoneBinary, 0x21> timeZoneBinary)
public ResultCode SetDeviceLocationNameWithTimeZoneRule(ServiceCtx context)
{
if (!_writePermission)
{
return ResultCode.PermissionDenied;
}
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
string locationName = Encoding.ASCII.GetString(context.RequestData.ReadBytes(0x24)).TrimEnd('\0');
ResultCode result;
using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize)))
{
result = _timeZoneManager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream);
}
return result;
}
[Command(8)] // 9.0.0+
// ParseTimeZoneBinary(buffer<nn::time::TimeZoneBinary, 0x21> timeZoneBinary) -> buffer<nn::time::TimeZoneRule, 0x16>
public ResultCode ParseTimeZoneBinary(ServiceCtx context)
{
(long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21();
long timeZoneRuleBufferPosition = context.Request.ReceiveBuff[0].Position;
long timeZoneRuleBufferSize = context.Request.ReceiveBuff[0].Size;
if (timeZoneRuleBufferSize != 0x4000)
{
// TODO: find error code here
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{timeZoneRuleBufferSize:x} (expected 0x4000)");
throw new InvalidOperationException();
}
ResultCode result;
using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize)))
{
result = _timeZoneManager.ParseTimeZoneRuleBinary(out TimeZoneRule timeZoneRule, timeZoneBinaryStream);
if (result == ResultCode.Success)
{
MemoryHelper.Write(context.Memory, timeZoneRuleBufferPosition, timeZoneRule);
}
}
return result;
}
[Command(20)] // 9.0.0+
// GetDeviceLocationNameOperationEventReadableHandle() -> handle<copy>
public ResultCode GetDeviceLocationNameOperationEventReadableHandle(ServiceCtx context)
{
return ResultCode.NotImplemented;
}
[Command(100)]
// ToCalendarTime(nn::time::PosixTime time, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
public ResultCode ToCalendarTime(ServiceCtx context)
{
long posixTime = context.RequestData.ReadInt64();
long bufferPosition = context.Request.SendBuff[0].Position;
long bufferSize = context.Request.SendBuff[0].Size;
if (bufferSize != 0x4000)
{
// TODO: find error code here
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
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;
}
[Command(101)]
// 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);
if (resultCode == 0)
{
context.ResponseData.WriteStruct(calendar);
}
return resultCode;
}
[Command(201)]
// ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
public ResultCode ToPosixTime(ServiceCtx context)
{
long inBufferPosition = context.Request.SendBuff[0].Position;
long inBufferSize = context.Request.SendBuff[0].Size;
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
if (inBufferSize != 0x4000)
{
// TODO: find error code here
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{inBufferSize:x} (expected 0x4000)");
throw new InvalidOperationException();
}
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, inBufferPosition);
ResultCode resultCode = _timeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
if (resultCode == 0)
{
long outBufferPosition = context.Request.RecvListBuff[0].Position;
long outBufferSize = context.Request.RecvListBuff[0].Size;
context.Memory.WriteInt64(outBufferPosition, posixTime);
context.ResponseData.Write(1);
}
return resultCode;
}
[Command(202)]
// 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);
if (resultCode == 0)
{
long outBufferPosition = context.Request.RecvListBuff[0].Position;
long outBufferSize = context.Request.RecvListBuff[0].Size;
context.Memory.WriteInt64(outBufferPosition, posixTime);
// 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);
}
}
}
}