2019-07-04 15:20:40 +00:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using Ryujinx.Common;
|
2018-10-17 17:15:50 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-16 23:47:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2019-07-04 15:20:40 +00:00
|
|
|
using Ryujinx.HLE.HOS.Services.Time.TimeZone;
|
2018-02-25 18:58:16 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-07-13 21:35:19 +00:00
|
|
|
using System.Text;
|
2018-02-25 18:58:16 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Time
|
2018-02-25 18:58:16 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
class ITimeZoneService : IpcService
|
2018-02-25 18:58:16 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-02-25 18:58:16 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-02-25 18:58:16 +00:00
|
|
|
|
|
|
|
public ITimeZoneService()
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-02-25 18:58:16 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
{ 0, GetDeviceLocationName },
|
|
|
|
{ 1, SetDeviceLocationName },
|
|
|
|
{ 2, GetTotalLocationNameCount },
|
|
|
|
{ 3, LoadLocationNameList },
|
|
|
|
{ 4, LoadTimeZoneRule },
|
|
|
|
//{ 5, GetTimeZoneRuleVersion }, // 2.0.0+
|
|
|
|
//{ 6, GetDeviceLocationNameAndUpdatedTime }, // 5.0.0+
|
|
|
|
{ 100, ToCalendarTime },
|
|
|
|
{ 101, ToCalendarTimeWithMyRule },
|
|
|
|
{ 201, ToPosixTime },
|
|
|
|
{ 202, ToPosixTimeWithMyRule }
|
2018-02-25 18:58:16 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// GetDeviceLocationName() -> nn::time::LocationName
|
2018-12-06 11:16:24 +00:00
|
|
|
public long GetDeviceLocationName(ServiceCtx context)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
char[] tzName = TimeZoneManager.Instance.GetDeviceLocationName().ToCharArray();
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int padding = 0x24 - tzName.Length;
|
2018-04-22 04:21:49 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (padding < 0)
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Time, TimeError.LocationNameTooLong);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ResponseData.Write(tzName);
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int index = 0; index < padding; index++)
|
2018-04-22 04:21:49 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write((byte)0);
|
2018-04-22 04:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// SetDeviceLocationName(nn::time::LocationName)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long SetDeviceLocationName(ServiceCtx context)
|
2018-02-25 18:58:16 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
string locationName = Encoding.ASCII.GetString(context.RequestData.ReadBytes(0x24)).TrimEnd('\0');
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
return TimeZoneManager.Instance.SetDeviceLocationName(locationName);
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// GetTotalLocationNameCount() -> u32
|
2018-12-06 11:16:24 +00:00
|
|
|
public long GetTotalLocationNameCount(ServiceCtx context)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
context.ResponseData.Write(TimeZoneManager.Instance.GetTotalLocationNameCount());
|
2018-07-13 21:35:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// LoadLocationNameList(u32 index) -> (u32 outCount, buffer<nn::time::LocationName, 6>)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long LoadLocationNameList(ServiceCtx context)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
// TODO: fix logic to use index
|
|
|
|
uint index = context.RequestData.ReadUInt32();
|
|
|
|
long bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
long bufferSize = context.Request.ReceiveBuff[0].Size;
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
uint errorCode = TimeZoneManager.Instance.LoadLocationNameList(index, out string[] locationNameArray, (uint)bufferSize / 0x24);
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (errorCode == 0)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
uint offset = 0;
|
|
|
|
|
|
|
|
foreach (string locationName in locationNameArray)
|
|
|
|
{
|
|
|
|
int padding = 0x24 - locationName.Length;
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (padding < 0)
|
|
|
|
{
|
|
|
|
return MakeError(ErrorModule.Time, TimeError.LocationNameTooLong);
|
|
|
|
}
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
context.Memory.WriteBytes(bufferPosition + offset, Encoding.ASCII.GetBytes(locationName));
|
|
|
|
MemoryHelper.FillWithZeros(context.Memory, bufferPosition + offset + locationName.Length, padding);
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
offset += 0x24;
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
context.ResponseData.Write((uint)locationNameArray.Length);
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
return errorCode;
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// LoadTimeZoneRule(nn::time::LocationName locationName) -> buffer<nn::time::TimeZoneRule, 0x16>
|
2018-12-06 11:16:24 +00:00
|
|
|
public long LoadTimeZoneRule(ServiceCtx context)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
long bufferSize = context.Request.ReceiveBuff[0].Size;
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (bufferSize != 0x4000)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
// TODO: find error code here
|
|
|
|
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
|
2018-02-25 18:58:16 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
string locationName = Encoding.ASCII.GetString(context.RequestData.ReadBytes(0x24)).TrimEnd('\0');
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
long resultCode = TimeZoneManager.Instance.LoadTimeZoneRules(out TimeZoneRule rules, locationName);
|
|
|
|
|
|
|
|
// Write TimeZoneRule if success
|
|
|
|
if (resultCode == 0)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
MemoryHelper.Write(context.Memory, bufferPosition, rules);
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return resultCode;
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// ToCalendarTime(nn::time::PosixTime time, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long ToCalendarTime(ServiceCtx context)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long posixTime = context.RequestData.ReadInt64();
|
|
|
|
long bufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
long bufferSize = context.Request.SendBuff[0].Size;
|
2018-04-11 00:16:27 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (bufferSize != 0x4000)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
// TODO: find error code here
|
|
|
|
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{bufferSize:x} (expected 0x4000)");
|
2018-02-25 18:58:16 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, bufferPosition);
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
long resultCode = TimeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar);
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (resultCode == 0)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
context.ResponseData.WriteStruct(calendar);
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return resultCode;
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// ToCalendarTimeWithMyRule(nn::time::PosixTime) -> (nn::time::CalendarTime, nn::time::sf::CalendarAdditionalInfo)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long ToCalendarTimeWithMyRule(ServiceCtx context)
|
2018-07-13 21:35:19 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long posixTime = context.RequestData.ReadInt64();
|
2018-07-13 21:35:19 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
long resultCode = TimeZoneManager.Instance.ToCalendarTimeWithMyRules(posixTime, out CalendarInfo calendar);
|
|
|
|
|
|
|
|
if (resultCode == 0)
|
|
|
|
{
|
|
|
|
context.ResponseData.WriteStruct(calendar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultCode;
|
2018-02-25 18:58:16 +00:00
|
|
|
}
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// ToPosixTime(nn::time::CalendarTime calendarTime, buffer<nn::time::TimeZoneRule, 0x15> rules) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long ToPosixTime(ServiceCtx context)
|
2018-08-15 18:53:23 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
long inBufferPosition = context.Request.SendBuff[0].Position;
|
|
|
|
long inBufferSize = context.Request.SendBuff[0].Size;
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (inBufferSize != 0x4000)
|
2018-08-15 18:53:23 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
// TODO: find error code here
|
|
|
|
Logger.PrintError(LogClass.ServiceTime, $"TimeZoneRule buffer size is 0x{inBufferSize:x} (expected 0x4000)");
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
TimeZoneRule rules = MemoryHelper.Read<TimeZoneRule>(context.Memory, inBufferPosition);
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
long resultCode = TimeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime);
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (resultCode == 0)
|
2018-08-15 18:53:23 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
long outBufferPosition = context.Request.RecvListBuff[0].Position;
|
|
|
|
long outBufferSize = context.Request.RecvListBuff[0].Size;
|
2018-08-16 23:47:36 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
context.Memory.WriteInt64(outBufferPosition, posixTime);
|
|
|
|
context.ResponseData.Write(1);
|
2018-08-15 18:53:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return resultCode;
|
2018-08-15 18:53:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// ToPosixTimeWithMyRule(nn::time::CalendarTime calendarTime) -> (u32 outCount, buffer<nn::time::PosixTime, 0xa>)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long ToPosixTimeWithMyRule(ServiceCtx context)
|
2018-08-15 18:53:23 +00:00
|
|
|
{
|
2019-07-04 15:20:40 +00:00
|
|
|
CalendarTime calendarTime = context.RequestData.ReadStruct<CalendarTime>();
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
long resultCode = TimeZoneManager.Instance.ToPosixTimeWithMyRules(calendarTime, out long posixTime);
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
if (resultCode == 0)
|
|
|
|
{
|
|
|
|
long outBufferPosition = context.Request.RecvListBuff[0].Position;
|
|
|
|
long outBufferSize = context.Request.RecvListBuff[0].Size;
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
context.Memory.WriteInt64(outBufferPosition, posixTime);
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
// There could be only one result on one calendar as leap seconds aren't supported.
|
|
|
|
context.ResponseData.Write(1);
|
|
|
|
}
|
2018-08-15 18:53:23 +00:00
|
|
|
|
2019-07-04 15:20:40 +00:00
|
|
|
return resultCode;
|
2018-08-15 18:53:23 +00:00
|
|
|
}
|
2018-02-25 18:58:16 +00:00
|
|
|
}
|
2018-07-13 21:35:19 +00:00
|
|
|
}
|