aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Nim/Ntc/StaticService/IEnsureNetworkClockAvailabilityService.cs
blob: 3b533f0ff1f0ada92348d058742c43ae497df4f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.Horizon.Common;
using System;

namespace Ryujinx.HLE.HOS.Services.Nim.Ntc.StaticService
{
    class IEnsureNetworkClockAvailabilityService : IpcService
    {
        private KEvent     _finishNotificationEvent;
        private ResultCode _taskResultCode;

        public IEnsureNetworkClockAvailabilityService(ServiceCtx context)
        {
            _finishNotificationEvent = new KEvent(context.Device.System.KernelContext);
            _taskResultCode          = ResultCode.Success;

            // NOTE: The service starts a thread that polls Nintendo NTP server and syncs the time with it.
            //       Additionnally it gets and uses some settings too:
            //       autonomic_correction_interval_seconds, autonomic_correction_failed_retry_interval_seconds,
            //       autonomic_correction_immediate_try_count_max, autonomic_correction_immediate_try_interval_milliseconds
        }

        [CommandHipc(0)]
        // StartTask()
        public ResultCode StartTask(ServiceCtx context)
        {
            if (!context.Device.Configuration.EnableInternetAccess)
            {
                return (ResultCode)Time.ResultCode.NetworkTimeNotAvailable;
            }

            // NOTE: Since we don't support the Nintendo NTP server, we can signal the event now to confirm the update task is done.
            _finishNotificationEvent.ReadableEvent.Signal();

            Logger.Stub?.PrintStub(LogClass.ServiceNtc);

            return ResultCode.Success;
        }

        [CommandHipc(1)]
        // GetFinishNotificationEvent() -> handle<copy>
        public ResultCode GetFinishNotificationEvent(ServiceCtx context)
        {
            if (context.Process.HandleTable.GenerateHandle(_finishNotificationEvent.ReadableEvent, out int finishNotificationEventHandle) != Result.Success)
            {
                throw new InvalidOperationException("Out of handles!");
            }

            context.Response.HandleDesc = IpcHandleDesc.MakeCopy(finishNotificationEventHandle);

            return ResultCode.Success;
        }

        [CommandHipc(2)]
        // GetResult()
        public ResultCode GetResult(ServiceCtx context)
        {
            return _taskResultCode;
        }

        [CommandHipc(3)]
        // Cancel()
        public ResultCode Cancel(ServiceCtx context)
        {
            // NOTE: The update task should be canceled here.
            _finishNotificationEvent.ReadableEvent.Signal();

            _taskResultCode = (ResultCode)Time.ResultCode.NetworkTimeTaskCanceled;

            Logger.Stub?.PrintStub(LogClass.ServiceNtc);

            return ResultCode.Success;
        }
    }
}