summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/psc
diff options
context:
space:
mode:
authorKelebek1 <eeeedddccc@hotmail.co.uk>2023-10-29 14:50:55 +0100
committerKelebek1 <eeeedddccc@hotmail.co.uk>2024-01-24 05:26:55 +0100
commite4915fb7d2077584a11a15141bc81d28ed2b0125 (patch)
tree1783055dc2e98eaf9099e8e7b194b55f8f607747 /src/core/hle/service/psc
parentMerge pull request #12678 from german77/settings_impl (diff)
downloadyuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.tar
yuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.tar.gz
yuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.tar.bz2
yuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.tar.lz
yuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.tar.xz
yuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.tar.zst
yuzu-e4915fb7d2077584a11a15141bc81d28ed2b0125.zip
Diffstat (limited to 'src/core/hle/service/psc')
-rw-r--r--src/core/hle/service/psc/psc.cpp17
-rw-r--r--src/core/hle/service/psc/time/alarms.cpp209
-rw-r--r--src/core/hle/service/psc/time/alarms.h139
-rw-r--r--src/core/hle/service/psc/time/clocks/context_writers.cpp83
-rw-r--r--src/core/hle/service/psc/time/clocks/context_writers.h79
-rw-r--r--src/core/hle/service/psc/time/clocks/ephemeral_network_system_clock_core.h21
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.cpp20
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.h23
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.cpp42
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.h30
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_steady_clock_core.cpp101
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_steady_clock_core.h54
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.cpp63
-rw-r--r--src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.h55
-rw-r--r--src/core/hle/service/psc/time/clocks/steady_clock_core.h81
-rw-r--r--src/core/hle/service/psc/time/clocks/system_clock_core.cpp75
-rw-r--r--src/core/hle/service/psc/time/clocks/system_clock_core.h55
-rw-r--r--src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.cpp43
-rw-r--r--src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.h41
-rw-r--r--src/core/hle/service/psc/time/common.cpp16
-rw-r--r--src/core/hle/service/psc/time/common.h168
-rw-r--r--src/core/hle/service/psc/time/errors.h24
-rw-r--r--src/core/hle/service/psc/time/manager.h56
-rw-r--r--src/core/hle/service/psc/time/power_state_request_manager.cpp50
-rw-r--r--src/core/hle/service/psc/time/power_state_request_manager.h42
-rw-r--r--src/core/hle/service/psc/time/power_state_service.cpp49
-rw-r--r--src/core/hle/service/psc/time/power_state_service.h32
-rw-r--r--src/core/hle/service/psc/time/service_manager.cpp494
-rw-r--r--src/core/hle/service/psc/time/service_manager.h101
-rw-r--r--src/core/hle/service/psc/time/shared_memory.cpp84
-rw-r--r--src/core/hle/service/psc/time/shared_memory.h70
-rw-r--r--src/core/hle/service/psc/time/static.cpp500
-rw-r--r--src/core/hle/service/psc/time/static.h95
-rw-r--r--src/core/hle/service/psc/time/steady_clock.cpp164
-rw-r--r--src/core/hle/service/psc/time/steady_clock.h49
-rw-r--r--src/core/hle/service/psc/time/system_clock.cpp127
-rw-r--r--src/core/hle/service/psc/time/system_clock.h46
-rw-r--r--src/core/hle/service/psc/time/time_zone.cpp280
-rw-r--r--src/core/hle/service/psc/time/time_zone.h62
-rw-r--r--src/core/hle/service/psc/time/time_zone_service.cpp289
-rw-r--r--src/core/hle/service/psc/time/time_zone_service.h69
41 files changed, 4097 insertions, 1 deletions
diff --git a/src/core/hle/service/psc/psc.cpp b/src/core/hle/service/psc/psc.cpp
index cd0cc9287..44310756b 100644
--- a/src/core/hle/service/psc/psc.cpp
+++ b/src/core/hle/service/psc/psc.cpp
@@ -4,9 +4,13 @@
#include <memory>
#include "common/logging/log.h"
+#include "core/core.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/psc/psc.h"
-#include "core/hle/service/server_manager.h"
+#include "core/hle/service/psc/time/manager.h"
+#include "core/hle/service/psc/time/power_state_service.h"
+#include "core/hle/service/psc/time/service_manager.h"
+#include "core/hle/service/psc/time/static.h"
#include "core/hle/service/service.h"
namespace Service::PSC {
@@ -76,6 +80,17 @@ void LoopProcess(Core::System& system) {
server_manager->RegisterNamedService("psc:c", std::make_shared<IPmControl>(system));
server_manager->RegisterNamedService("psc:m", std::make_shared<IPmService>(system));
+
+ auto time = std::make_shared<Time::TimeManager>(system);
+
+ server_manager->RegisterNamedService(
+ "time:m", std::make_shared<Time::ServiceManager>(system, time, server_manager.get()));
+ server_manager->RegisterNamedService(
+ "time:su", std::make_shared<Time::StaticService>(
+ system, Time::StaticServiceSetupInfo{0, 0, 0, 0, 0, 1}, time, "time:su"));
+ server_manager->RegisterNamedService("time:al",
+ std::make_shared<Time::IAlarmService>(system, time));
+
ServerManager::RunServer(std::move(server_manager));
}
diff --git a/src/core/hle/service/psc/time/alarms.cpp b/src/core/hle/service/psc/time/alarms.cpp
new file mode 100644
index 000000000..5e52c19f8
--- /dev/null
+++ b/src/core/hle/service/psc/time/alarms.cpp
@@ -0,0 +1,209 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/alarms.h"
+#include "core/hle/service/psc/time/manager.h"
+
+namespace Service::PSC::Time {
+Alarm::Alarm(Core::System& system, KernelHelpers::ServiceContext& ctx, AlarmType type)
+ : m_ctx{ctx}, m_event{ctx.CreateEvent("Psc:Alarm:Event")} {
+ m_event->Clear();
+
+ switch (type) {
+ case WakeupAlarm:
+ m_priority = 1;
+ break;
+ case BackgroundTaskAlarm:
+ m_priority = 0;
+ break;
+ default:
+ UNREACHABLE();
+ return;
+ }
+}
+
+Alarm::~Alarm() {
+ m_ctx.CloseEvent(m_event);
+}
+
+Alarms::Alarms(Core::System& system, StandardSteadyClockCore& steady_clock,
+ PowerStateRequestManager& power_state_request_manager)
+ : m_system{system}, m_ctx{system, "Psc:Alarms"}, m_steady_clock{steady_clock},
+ m_power_state_request_manager{power_state_request_manager}, m_event{m_ctx.CreateEvent(
+ "Psc:Alarms:Event")} {}
+
+Alarms::~Alarms() {
+ m_ctx.CloseEvent(m_event);
+}
+
+Result Alarms::Enable(Alarm& alarm, s64 time) {
+ R_UNLESS(m_steady_clock.IsInitialized(), ResultClockUninitialized);
+
+ std::scoped_lock l{m_mutex};
+ R_UNLESS(alarm.IsLinked(), ResultAlarmNotRegistered);
+
+ auto time_ns{time + m_steady_clock.GetRawTime()};
+ auto one_second_ns{
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(1)).count()};
+ time_ns = Common::AlignUp(time_ns, one_second_ns);
+ alarm.SetAlertTime(time_ns);
+
+ Insert(alarm);
+ R_RETURN(UpdateClosestAndSignal());
+}
+
+void Alarms::Disable(Alarm& alarm) {
+ std::scoped_lock l{m_mutex};
+ if (!alarm.IsLinked()) {
+ return;
+ }
+
+ Erase(alarm);
+ UpdateClosestAndSignal();
+}
+
+void Alarms::CheckAndSignal() {
+ std::scoped_lock l{m_mutex};
+ if (m_alarms.empty()) {
+ return;
+ }
+
+ bool alarm_signalled{false};
+ for (auto& alarm : m_alarms) {
+ if (m_steady_clock.GetRawTime() >= alarm.GetAlertTime()) {
+ alarm.Signal();
+ alarm.Lock();
+ Erase(alarm);
+
+ m_power_state_request_manager.UpdatePendingPowerStateRequestPriority(
+ alarm.GetPriority());
+ alarm_signalled = true;
+ }
+ }
+
+ if (!alarm_signalled) {
+ return;
+ }
+
+ m_power_state_request_manager.SignalPowerStateRequestAvailability();
+ UpdateClosestAndSignal();
+}
+
+bool Alarms::GetClosestAlarm(Alarm** out_alarm) {
+ std::scoped_lock l{m_mutex};
+ auto alarm = m_alarms.empty() ? nullptr : std::addressof(m_alarms.front());
+ *out_alarm = alarm;
+ return alarm != nullptr;
+}
+
+void Alarms::Insert(Alarm& alarm) {
+ // Alarms are sorted by alert time, then priority
+ auto it{m_alarms.begin()};
+ while (it != m_alarms.end()) {
+ if (alarm.GetAlertTime() < it->GetAlertTime() ||
+ (alarm.GetAlertTime() == it->GetAlertTime() &&
+ alarm.GetPriority() < it->GetPriority())) {
+ m_alarms.insert(it, alarm);
+ return;
+ }
+ it++;
+ }
+
+ m_alarms.push_back(alarm);
+}
+
+void Alarms::Erase(Alarm& alarm) {
+ m_alarms.erase(m_alarms.iterator_to(alarm));
+}
+
+Result Alarms::UpdateClosestAndSignal() {
+ m_closest_alarm = m_alarms.empty() ? nullptr : std::addressof(m_alarms.front());
+ R_SUCCEED_IF(m_closest_alarm == nullptr);
+
+ m_event->Signal();
+
+ R_SUCCEED();
+}
+
+IAlarmService::IAlarmService(Core::System& system_, std::shared_ptr<TimeManager> manager)
+ : ServiceFramework{system_, "time:al"}, m_system{system}, m_alarms{manager->m_alarms} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &IAlarmService::CreateWakeupAlarm, "CreateWakeupAlarm"},
+ {1, &IAlarmService::CreateBackgroundTaskAlarm, "CreateBackgroundTaskAlarm"},
+ };
+ // clang-format on
+ RegisterHandlers(functions);
+}
+
+void IAlarmService::CreateWakeupAlarm(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(ResultSuccess);
+ rb.PushIpcInterface<ISteadyClockAlarm>(system, m_alarms, AlarmType::WakeupAlarm);
+}
+
+void IAlarmService::CreateBackgroundTaskAlarm(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(ResultSuccess);
+ rb.PushIpcInterface<ISteadyClockAlarm>(system, m_alarms, AlarmType::BackgroundTaskAlarm);
+}
+
+ISteadyClockAlarm::ISteadyClockAlarm(Core::System& system_, Alarms& alarms, AlarmType type)
+ : ServiceFramework{system_, "ISteadyClockAlarm"}, m_ctx{system, "Psc:ISteadyClockAlarm"},
+ m_alarms{alarms}, m_alarm{system, m_ctx, type} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &ISteadyClockAlarm::GetAlarmEvent, "GetAlarmEvent"},
+ {1, &ISteadyClockAlarm::Enable, "Enable"},
+ {2, &ISteadyClockAlarm::Disable, "Disable"},
+ {3, &ISteadyClockAlarm::IsEnabled, "IsEnabled"},
+ {10, nullptr, "CreateWakeLock"},
+ {11, nullptr, "DestroyWakeLock"},
+ };
+ // clang-format on
+ RegisterHandlers(functions);
+}
+
+void ISteadyClockAlarm::GetAlarmEvent(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(ResultSuccess);
+ rb.PushCopyObjects(m_alarm.GetEventHandle());
+}
+
+void ISteadyClockAlarm::Enable(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto time{rp.Pop<s64>()};
+
+ auto res = m_alarms.Enable(m_alarm, time);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ISteadyClockAlarm::Disable(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ m_alarms.Disable(m_alarm);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultSuccess);
+}
+
+void ISteadyClockAlarm::IsEnabled(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(ResultSuccess);
+ rb.Push<bool>(m_alarm.IsLinked());
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/alarms.h b/src/core/hle/service/psc/time/alarms.h
new file mode 100644
index 000000000..597770028
--- /dev/null
+++ b/src/core/hle/service/psc/time/alarms.h
@@ -0,0 +1,139 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <mutex>
+
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/kernel_helpers.h"
+#include "core/hle/service/psc/time/clocks/standard_steady_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/psc/time/power_state_request_manager.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+class TimeManager;
+
+enum AlarmType : u32 {
+ WakeupAlarm = 0,
+ BackgroundTaskAlarm = 1,
+};
+
+struct Alarm : public Common::IntrusiveListBaseNode<Alarm> {
+ using AlarmList = Common::IntrusiveListBaseTraits<Alarm>::ListType;
+
+ Alarm(Core::System& system, KernelHelpers::ServiceContext& ctx, AlarmType type);
+ ~Alarm();
+
+ Kernel::KReadableEvent& GetEventHandle() {
+ return m_event->GetReadableEvent();
+ }
+
+ s64 GetAlertTime() const {
+ return m_alert_time;
+ }
+
+ void SetAlertTime(s64 time) {
+ m_alert_time = time;
+ }
+
+ u32 GetPriority() const {
+ return m_priority;
+ }
+
+ void Signal() {
+ m_event->Signal();
+ }
+
+ Result Lock() {
+ // TODO
+ // if (m_lock_service) {
+ // return m_lock_service->Lock();
+ // }
+ R_SUCCEED();
+ }
+
+ KernelHelpers::ServiceContext& m_ctx;
+
+ u32 m_priority;
+ Kernel::KEvent* m_event{};
+ s64 m_alert_time{};
+ // TODO
+ // nn::psc::sf::IPmStateLock* m_lock_service{};
+};
+
+class Alarms {
+public:
+ explicit Alarms(Core::System& system, StandardSteadyClockCore& steady_clock,
+ PowerStateRequestManager& power_state_request_manager);
+ ~Alarms();
+
+ Kernel::KEvent& GetEvent() {
+ return *m_event;
+ }
+
+ s64 GetRawTime() {
+ return m_steady_clock.GetRawTime();
+ }
+
+ Result Enable(Alarm& alarm, s64 time);
+ void Disable(Alarm& alarm);
+ void CheckAndSignal();
+ bool GetClosestAlarm(Alarm** out_alarm);
+
+private:
+ void Insert(Alarm& alarm);
+ void Erase(Alarm& alarm);
+ Result UpdateClosestAndSignal();
+
+ Core::System& m_system;
+ KernelHelpers::ServiceContext m_ctx;
+
+ StandardSteadyClockCore& m_steady_clock;
+ PowerStateRequestManager& m_power_state_request_manager;
+ Alarm::AlarmList m_alarms;
+ Kernel::KEvent* m_event{};
+ Alarm* m_closest_alarm{};
+ std::mutex m_mutex;
+};
+
+class IAlarmService final : public ServiceFramework<IAlarmService> {
+public:
+ explicit IAlarmService(Core::System& system, std::shared_ptr<TimeManager> manager);
+
+ ~IAlarmService() override = default;
+
+private:
+ void CreateWakeupAlarm(HLERequestContext& ctx);
+ void CreateBackgroundTaskAlarm(HLERequestContext& ctx);
+
+ Core::System& m_system;
+ Alarms& m_alarms;
+};
+
+class ISteadyClockAlarm final : public ServiceFramework<ISteadyClockAlarm> {
+public:
+ explicit ISteadyClockAlarm(Core::System& system, Alarms& alarms, AlarmType type);
+
+ ~ISteadyClockAlarm() override = default;
+
+private:
+ void GetAlarmEvent(HLERequestContext& ctx);
+ void Enable(HLERequestContext& ctx);
+ void Disable(HLERequestContext& ctx);
+ void IsEnabled(HLERequestContext& ctx);
+
+ KernelHelpers::ServiceContext m_ctx;
+
+ Alarms& m_alarms;
+ Alarm m_alarm;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/context_writers.cpp b/src/core/hle/service/psc/time/clocks/context_writers.cpp
new file mode 100644
index 000000000..ac8700f76
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/context_writers.cpp
@@ -0,0 +1,83 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+
+namespace Service::PSC::Time {
+
+void ContextWriter::SignalAllNodes() {
+ std::scoped_lock l{m_mutex};
+ for (auto& operation : m_operation_events) {
+ operation.m_event->Signal();
+ }
+}
+
+void ContextWriter::Link(OperationEvent& operation_event) {
+ std::scoped_lock l{m_mutex};
+ m_operation_events.push_back(operation_event);
+}
+
+LocalSystemClockContextWriter::LocalSystemClockContextWriter(Core::System& system,
+ SharedMemory& shared_memory)
+ : m_system{system}, m_shared_memory{shared_memory} {}
+
+Result LocalSystemClockContextWriter::Write(SystemClockContext& context) {
+ if (m_in_use) {
+ R_SUCCEED_IF(context == m_context);
+ m_context = context;
+ } else {
+ m_context = context;
+ m_in_use = true;
+ }
+
+ m_shared_memory.SetLocalSystemContext(context);
+
+ SignalAllNodes();
+
+ R_SUCCEED();
+}
+
+NetworkSystemClockContextWriter::NetworkSystemClockContextWriter(Core::System& system,
+ SharedMemory& shared_memory,
+ SystemClockCore& system_clock)
+ : m_system{system}, m_shared_memory{shared_memory}, m_system_clock{system_clock} {}
+
+Result NetworkSystemClockContextWriter::Write(SystemClockContext& context) {
+ s64 time{};
+ [[maybe_unused]] auto res = m_system_clock.GetCurrentTime(&time);
+
+ if (m_in_use) {
+ R_SUCCEED_IF(context == m_context);
+ m_context = context;
+ } else {
+ m_context = context;
+ m_in_use = true;
+ }
+
+ m_shared_memory.SetNetworkSystemContext(context);
+
+ SignalAllNodes();
+
+ R_SUCCEED();
+}
+
+EphemeralNetworkSystemClockContextWriter::EphemeralNetworkSystemClockContextWriter(
+ Core::System& system)
+ : m_system{system} {}
+
+Result EphemeralNetworkSystemClockContextWriter::Write(SystemClockContext& context) {
+ if (m_in_use) {
+ R_SUCCEED_IF(context == m_context);
+ m_context = context;
+ } else {
+ m_context = context;
+ m_in_use = true;
+ }
+
+ SignalAllNodes();
+
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/context_writers.h b/src/core/hle/service/psc/time/clocks/context_writers.h
new file mode 100644
index 000000000..afd3725d4
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/context_writers.h
@@ -0,0 +1,79 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <list>
+
+#include "common/common_types.h"
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/service/psc/time/clocks/system_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/psc/time/shared_memory.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+
+class ContextWriter {
+private:
+ using OperationEventList = Common::IntrusiveListBaseTraits<OperationEvent>::ListType;
+
+public:
+ virtual ~ContextWriter() = default;
+
+ virtual Result Write(SystemClockContext& context) = 0;
+ void SignalAllNodes();
+ void Link(OperationEvent& operation_event);
+
+private:
+ OperationEventList m_operation_events;
+ std::mutex m_mutex;
+};
+
+class LocalSystemClockContextWriter : public ContextWriter {
+public:
+ explicit LocalSystemClockContextWriter(Core::System& system, SharedMemory& shared_memory);
+
+ Result Write(SystemClockContext& context) override;
+
+private:
+ Core::System& m_system;
+
+ SharedMemory& m_shared_memory;
+ bool m_in_use{};
+ SystemClockContext m_context{};
+};
+
+class NetworkSystemClockContextWriter : public ContextWriter {
+public:
+ explicit NetworkSystemClockContextWriter(Core::System& system, SharedMemory& shared_memory,
+ SystemClockCore& system_clock);
+
+ Result Write(SystemClockContext& context) override;
+
+private:
+ Core::System& m_system;
+
+ SharedMemory& m_shared_memory;
+ bool m_in_use{};
+ SystemClockContext m_context{};
+ SystemClockCore& m_system_clock;
+};
+
+class EphemeralNetworkSystemClockContextWriter : public ContextWriter {
+public:
+ EphemeralNetworkSystemClockContextWriter(Core::System& system);
+
+ Result Write(SystemClockContext& context) override;
+
+private:
+ Core::System& m_system;
+
+ bool m_in_use{};
+ SystemClockContext m_context{};
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/ephemeral_network_system_clock_core.h b/src/core/hle/service/psc/time/clocks/ephemeral_network_system_clock_core.h
new file mode 100644
index 000000000..0a68867d9
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/ephemeral_network_system_clock_core.h
@@ -0,0 +1,21 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/result.h"
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+#include "core/hle/service/psc/time/clocks/system_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+
+class EphemeralNetworkSystemClockCore : public SystemClockCore {
+public:
+ explicit EphemeralNetworkSystemClockCore(SteadyClockCore& steady_clock)
+ : SystemClockCore{steady_clock} {}
+ ~EphemeralNetworkSystemClockCore() override = default;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.cpp b/src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.cpp
new file mode 100644
index 000000000..36dca6689
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.cpp
@@ -0,0 +1,20 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/psc/time/clocks/standard_local_system_clock_core.h"
+
+namespace Service::PSC::Time {
+
+void StandardLocalSystemClockCore::Initialize(SystemClockContext& context, s64 time) {
+ SteadyClockTimePoint time_point{};
+ if (GetCurrentTimePoint(time_point) == ResultSuccess &&
+ context.steady_time_point.IdMatches(time_point)) {
+ SetContextAndWrite(context);
+ } else if (SetCurrentTime(time) != ResultSuccess) {
+ LOG_ERROR(Service_Time, "Failed to SetCurrentTime");
+ }
+
+ SetInitialized();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.h b/src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.h
new file mode 100644
index 000000000..176ba3e94
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_local_system_clock_core.h
@@ -0,0 +1,23 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/result.h"
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+#include "core/hle/service/psc/time/clocks/system_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+
+class StandardLocalSystemClockCore : public SystemClockCore {
+public:
+ explicit StandardLocalSystemClockCore(SteadyClockCore& steady_clock)
+ : SystemClockCore{steady_clock} {}
+ ~StandardLocalSystemClockCore() override = default;
+
+ void Initialize(SystemClockContext& context, s64 time);
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.cpp b/src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.cpp
new file mode 100644
index 000000000..8d6cb7db1
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.cpp
@@ -0,0 +1,42 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/psc/time/clocks/standard_network_system_clock_core.h"
+
+namespace Service::PSC::Time {
+
+void StandardNetworkSystemClockCore::Initialize(SystemClockContext& context, s64 accuracy) {
+ if (SetContextAndWrite(context) != ResultSuccess) {
+ LOG_ERROR(Service_Time, "Failed to SetContext");
+ }
+ m_sufficient_accuracy = accuracy;
+ SetInitialized();
+}
+
+bool StandardNetworkSystemClockCore::IsAccuracySufficient() {
+ if (!IsInitialized()) {
+ return false;
+ }
+
+ SystemClockContext context{};
+ SteadyClockTimePoint current_time_point{};
+ if (GetCurrentTimePoint(current_time_point) != ResultSuccess ||
+ GetContext(context) != ResultSuccess) {
+ return false;
+ }
+
+ s64 seconds{};
+ if (GetSpanBetweenTimePoints(&seconds, context.steady_time_point, current_time_point) !=
+ ResultSuccess) {
+ return false;
+ }
+
+ if (std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(seconds))
+ .count() < m_sufficient_accuracy) {
+ return true;
+ }
+
+ return false;
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.h b/src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.h
new file mode 100644
index 000000000..933d2c8e3
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_network_system_clock_core.h
@@ -0,0 +1,30 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <chrono>
+
+#include "core/hle/result.h"
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+#include "core/hle/service/psc/time/clocks/system_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+
+class StandardNetworkSystemClockCore : public SystemClockCore {
+public:
+ explicit StandardNetworkSystemClockCore(SteadyClockCore& steady_clock)
+ : SystemClockCore{steady_clock} {}
+ ~StandardNetworkSystemClockCore() override = default;
+
+ void Initialize(SystemClockContext& context, s64 accuracy);
+ bool IsAccuracySufficient();
+
+private:
+ s64 m_sufficient_accuracy{
+ std::chrono ::duration_cast<std::chrono::nanoseconds>(std::chrono::days(10)).count()};
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_steady_clock_core.cpp b/src/core/hle/service/psc/time/clocks/standard_steady_clock_core.cpp
new file mode 100644
index 000000000..7a72d7aa2
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_steady_clock_core.cpp
@@ -0,0 +1,101 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <chrono>
+
+#include "core/core.h"
+#include "core/core_timing.h"
+#include "core/hle/service/psc/time/clocks/standard_steady_clock_core.h"
+
+namespace Service::PSC::Time {
+
+void StandardSteadyClockCore::Initialize(ClockSourceId clock_source_id, s64 rtc_offset,
+ s64 internal_offset, s64 test_offset,
+ bool is_rtc_reset_detected) {
+ m_clock_source_id = clock_source_id;
+ m_rtc_offset = rtc_offset;
+ m_internal_offset = internal_offset;
+ m_test_offset = test_offset;
+ if (is_rtc_reset_detected) {
+ SetResetDetected();
+ }
+ SetInitialized();
+}
+
+void StandardSteadyClockCore::SetRtcOffset(s64 offset) {
+ m_rtc_offset = offset;
+}
+
+void StandardSteadyClockCore::SetContinuousAdjustment(ClockSourceId clock_source_id, s64 time) {
+ auto ticks{m_system.CoreTiming().GetClockTicks()};
+
+ m_continuous_adjustment_time_point.rtc_offset = ConvertToTimeSpan(ticks).count();
+ m_continuous_adjustment_time_point.diff_scale = 0;
+ m_continuous_adjustment_time_point.shift_amount = 0;
+ m_continuous_adjustment_time_point.lower = time;
+ m_continuous_adjustment_time_point.upper = time;
+ m_continuous_adjustment_time_point.clock_source_id = clock_source_id;
+}
+
+void StandardSteadyClockCore::GetContinuousAdjustment(
+ ContinuousAdjustmentTimePoint& out_time_point) const {
+ out_time_point = m_continuous_adjustment_time_point;
+}
+
+void StandardSteadyClockCore::UpdateContinuousAdjustmentTime(s64 in_time) {
+ auto ticks{m_system.CoreTiming().GetClockTicks()};
+ auto uptime_ns{ConvertToTimeSpan(ticks).count()};
+ auto adjusted_time{((uptime_ns - m_continuous_adjustment_time_point.rtc_offset) *
+ m_continuous_adjustment_time_point.diff_scale) >>
+ m_continuous_adjustment_time_point.shift_amount};
+ auto expected_time{adjusted_time + m_continuous_adjustment_time_point.lower};
+
+ auto last_time_point{m_continuous_adjustment_time_point.upper};
+ m_continuous_adjustment_time_point.upper = in_time;
+ auto t1{std::min<s64>(expected_time, last_time_point)};
+ expected_time = std::max<s64>(expected_time, last_time_point);
+ expected_time = m_continuous_adjustment_time_point.diff_scale >= 0 ? t1 : expected_time;
+
+ auto new_diff{in_time < expected_time ? -55 : 55};
+
+ m_continuous_adjustment_time_point.rtc_offset = uptime_ns;
+ m_continuous_adjustment_time_point.shift_amount = expected_time == in_time ? 0 : 14;
+ m_continuous_adjustment_time_point.diff_scale = expected_time == in_time ? 0 : new_diff;
+ m_continuous_adjustment_time_point.lower = expected_time;
+}
+
+Result StandardSteadyClockCore::GetCurrentTimePointImpl(SteadyClockTimePoint& out_time_point) {
+ auto current_time_ns = GetCurrentRawTimePointImpl();
+ auto current_time_s =
+ std::chrono::duration_cast<std::chrono::seconds>(std::chrono::nanoseconds(current_time_ns));
+ out_time_point.time_point = current_time_s.count();
+ out_time_point.clock_source_id = m_clock_source_id;
+ R_SUCCEED();
+}
+
+s64 StandardSteadyClockCore::GetCurrentRawTimePointImpl() {
+ std::scoped_lock l{m_mutex};
+ auto ticks{static_cast<s64>(m_system.CoreTiming().GetClockTicks())};
+ auto current_time_ns = m_rtc_offset + ConvertToTimeSpan(ticks).count();
+ auto time_point = std::max<s64>(current_time_ns, m_cached_time_point);
+ m_cached_time_point = time_point;
+ return time_point;
+}
+
+s64 StandardSteadyClockCore::GetTestOffsetImpl() const {
+ return m_test_offset;
+}
+
+void StandardSteadyClockCore::SetTestOffsetImpl(s64 offset) {
+ m_test_offset = offset;
+}
+
+s64 StandardSteadyClockCore::GetInternalOffsetImpl() const {
+ return m_internal_offset;
+}
+
+void StandardSteadyClockCore::SetInternalOffsetImpl(s64 offset) {
+ m_internal_offset = offset;
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_steady_clock_core.h b/src/core/hle/service/psc/time/clocks/standard_steady_clock_core.h
new file mode 100644
index 000000000..bbf98fcd5
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_steady_clock_core.h
@@ -0,0 +1,54 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <mutex>
+
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+class StandardSteadyClockCore : public SteadyClockCore {
+public:
+ explicit StandardSteadyClockCore(Core::System& system) : m_system{system} {}
+ ~StandardSteadyClockCore() override = default;
+
+ void Initialize(ClockSourceId clock_source_id, s64 rtc_offset, s64 internal_offset,
+ s64 test_offset, bool is_rtc_reset_detected);
+
+ void SetRtcOffset(s64 offset);
+ void SetContinuousAdjustment(ClockSourceId clock_source_id, s64 time);
+ void GetContinuousAdjustment(ContinuousAdjustmentTimePoint& out_time_point) const;
+ void UpdateContinuousAdjustmentTime(s64 time);
+
+ Result GetCurrentTimePointImpl(SteadyClockTimePoint& out_time_point) override;
+ s64 GetCurrentRawTimePointImpl() override;
+ s64 GetTestOffsetImpl() const override;
+ void SetTestOffsetImpl(s64 offset) override;
+ s64 GetInternalOffsetImpl() const override;
+ void SetInternalOffsetImpl(s64 offset) override;
+
+ Result GetRtcValueImpl(s64& out_value) override {
+ R_RETURN(ResultNotImplemented);
+ }
+
+ Result GetSetupResultValueImpl() override {
+ R_SUCCEED();
+ }
+
+private:
+ Core::System& m_system;
+
+ std::mutex m_mutex;
+ s64 m_test_offset{};
+ s64 m_internal_offset{};
+ ClockSourceId m_clock_source_id{};
+ s64 m_rtc_offset{};
+ s64 m_cached_time_point{};
+ ContinuousAdjustmentTimePoint m_continuous_adjustment_time_point{};
+};
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.cpp b/src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.cpp
new file mode 100644
index 000000000..9e9be05d6
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.cpp
@@ -0,0 +1,63 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/clocks/standard_user_system_clock_core.h"
+
+namespace Service::PSC::Time {
+
+StandardUserSystemClockCore::StandardUserSystemClockCore(
+ Core::System& system, StandardLocalSystemClockCore& local_clock,
+ StandardNetworkSystemClockCore& network_clock)
+ : SystemClockCore{local_clock.GetSteadyClock()}, m_system{system},
+ m_ctx{m_system, "Psc:StandardUserSystemClockCore"}, m_local_system_clock{local_clock},
+ m_network_system_clock{network_clock}, m_event{m_ctx.CreateEvent(
+ "Psc:StandardUserSystemClockCore:Event")} {}
+
+StandardUserSystemClockCore::~StandardUserSystemClockCore() {
+ m_ctx.CloseEvent(m_event);
+}
+
+Result StandardUserSystemClockCore::SetAutomaticCorrection(bool automatic_correction) {
+ R_SUCCEED_IF(m_automatic_correction == automatic_correction);
+ R_SUCCEED_IF(!m_network_system_clock.CheckClockSourceMatches());
+
+ SystemClockContext context{};
+ R_TRY(m_network_system_clock.GetContext(context));
+ R_TRY(m_local_system_clock.SetContextAndWrite(context));
+
+ m_automatic_correction = automatic_correction;
+ R_SUCCEED();
+}
+
+Result StandardUserSystemClockCore::GetContext(SystemClockContext& out_context) const {
+ if (!m_automatic_correction) {
+ R_RETURN(m_local_system_clock.GetContext(out_context));
+ }
+
+ if (!m_network_system_clock.CheckClockSourceMatches()) {
+ R_RETURN(m_local_system_clock.GetContext(out_context));
+ }
+
+ SystemClockContext context{};
+ R_TRY(m_network_system_clock.GetContext(context));
+ R_TRY(m_local_system_clock.SetContextAndWrite(context));
+
+ R_RETURN(m_local_system_clock.GetContext(out_context));
+}
+
+Result StandardUserSystemClockCore::SetContext(SystemClockContext& context) {
+ R_RETURN(ResultNotImplemented);
+}
+
+Result StandardUserSystemClockCore::GetTimePoint(SteadyClockTimePoint& out_time_point) {
+ out_time_point = m_time_point;
+ R_SUCCEED();
+}
+
+void StandardUserSystemClockCore::SetTimePointAndSignal(SteadyClockTimePoint& time_point) {
+ m_time_point = time_point;
+ m_event->Signal();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.h b/src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.h
new file mode 100644
index 000000000..a7fe7648d
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/standard_user_system_clock_core.h
@@ -0,0 +1,55 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/result.h"
+#include "core/hle/service/kernel_helpers.h"
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+#include "core/hle/service/psc/time/clocks/standard_local_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_network_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+#include "core/hle/service/psc/time/clocks/system_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+
+class StandardUserSystemClockCore : public SystemClockCore {
+public:
+ explicit StandardUserSystemClockCore(Core::System& system,
+ StandardLocalSystemClockCore& local_clock,
+ StandardNetworkSystemClockCore& network_clock);
+ ~StandardUserSystemClockCore() override;
+
+ Kernel::KEvent& GetEvent() {
+ return *m_event;
+ }
+
+ bool GetAutomaticCorrection() const {
+ return m_automatic_correction;
+ }
+ Result SetAutomaticCorrection(bool automatic_correction);
+
+ Result GetContext(SystemClockContext& out_context) const override;
+ Result SetContext(SystemClockContext& context) override;
+
+ Result GetTimePoint(SteadyClockTimePoint& out_time_point);
+ void SetTimePointAndSignal(SteadyClockTimePoint& time_point);
+
+private:
+ Core::System& m_system;
+ KernelHelpers::ServiceContext m_ctx;
+
+ bool m_automatic_correction{};
+ StandardLocalSystemClockCore& m_local_system_clock;
+ StandardNetworkSystemClockCore& m_network_system_clock;
+ SteadyClockTimePoint m_time_point{};
+ Kernel::KEvent* m_event{};
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/steady_clock_core.h b/src/core/hle/service/psc/time/clocks/steady_clock_core.h
new file mode 100644
index 000000000..f933cc15f
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/steady_clock_core.h
@@ -0,0 +1,81 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <chrono>
+
+#include "core/hle/result.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+
+class SteadyClockCore {
+public:
+ SteadyClockCore() = default;
+ virtual ~SteadyClockCore() = default;
+
+ void SetInitialized() {
+ m_initialized = true;
+ }
+
+ bool IsInitialized() const {
+ return m_initialized;
+ }
+
+ void SetResetDetected() {
+ m_reset_detected = true;
+ }
+
+ bool IsResetDetected() const {
+ return m_reset_detected;
+ }
+
+ Result GetCurrentTimePoint(SteadyClockTimePoint& out_time_point) {
+ R_TRY(GetCurrentTimePointImpl(out_time_point));
+
+ auto one_second_ns{
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(1)).count()};
+ out_time_point.time_point += GetTestOffsetImpl() / one_second_ns;
+ out_time_point.time_point += GetInternalOffsetImpl() / one_second_ns;
+ R_SUCCEED();
+ }
+
+ s64 GetTestOffset() const {
+ return GetTestOffsetImpl();
+ }
+
+ void SetTestOffset(s64 offset) {
+ SetTestOffsetImpl(offset);
+ }
+
+ s64 GetInternalOffset() const {
+ return GetInternalOffsetImpl();
+ }
+
+ s64 GetRawTime() {
+ return GetCurrentRawTimePointImpl() + GetTestOffsetImpl() + GetInternalOffsetImpl();
+ }
+
+ Result GetRtcValue(s64& out_value) {
+ R_RETURN(GetRtcValueImpl(out_value));
+ }
+
+ Result GetSetupResultValue() {
+ R_RETURN(GetSetupResultValueImpl());
+ }
+
+private:
+ virtual Result GetCurrentTimePointImpl(SteadyClockTimePoint& out_time_point) = 0;
+ virtual s64 GetCurrentRawTimePointImpl() = 0;
+ virtual s64 GetTestOffsetImpl() const = 0;
+ virtual void SetTestOffsetImpl(s64 offset) = 0;
+ virtual s64 GetInternalOffsetImpl() const = 0;
+ virtual void SetInternalOffsetImpl(s64 offset) = 0;
+ virtual Result GetRtcValueImpl(s64& out_value) = 0;
+ virtual Result GetSetupResultValueImpl() = 0;
+
+ bool m_initialized{};
+ bool m_reset_detected{};
+};
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/system_clock_core.cpp b/src/core/hle/service/psc/time/clocks/system_clock_core.cpp
new file mode 100644
index 000000000..c507ef517
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/system_clock_core.cpp
@@ -0,0 +1,75 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+#include "core/hle/service/psc/time/clocks/system_clock_core.h"
+
+namespace Service::PSC::Time {
+
+bool SystemClockCore::CheckClockSourceMatches() {
+ SystemClockContext context{};
+ if (GetContext(context) != ResultSuccess) {
+ return false;
+ }
+
+ SteadyClockTimePoint time_point{};
+ if (m_steady_clock.GetCurrentTimePoint(time_point) != ResultSuccess) {
+ return false;
+ }
+
+ return context.steady_time_point.IdMatches(time_point);
+}
+
+Result SystemClockCore::GetCurrentTime(s64* out_time) const {
+ R_UNLESS(out_time != nullptr, ResultInvalidArgument);
+
+ SystemClockContext context{};
+ SteadyClockTimePoint time_point{};
+
+ R_TRY(m_steady_clock.GetCurrentTimePoint(time_point));
+ R_TRY(GetContext(context));
+
+ R_UNLESS(context.steady_time_point.IdMatches(time_point), ResultClockMismatch);
+
+ *out_time = context.offset + time_point.time_point;
+ R_SUCCEED();
+}
+
+Result SystemClockCore::SetCurrentTime(s64 time) {
+ SteadyClockTimePoint time_point{};
+ R_TRY(m_steady_clock.GetCurrentTimePoint(time_point));
+
+ SystemClockContext context{
+ .offset = time - time_point.time_point,
+ .steady_time_point = time_point,
+ };
+ R_RETURN(SetContextAndWrite(context));
+}
+
+Result SystemClockCore::GetContext(SystemClockContext& out_context) const {
+ out_context = m_context;
+ R_SUCCEED();
+}
+
+Result SystemClockCore::SetContext(SystemClockContext& context) {
+ m_context = context;
+ R_SUCCEED();
+}
+
+Result SystemClockCore::SetContextAndWrite(SystemClockContext& context) {
+ R_TRY(SetContext(context));
+
+ if (m_context_writer) {
+ R_RETURN(m_context_writer->Write(context));
+ }
+
+ R_SUCCEED();
+}
+
+void SystemClockCore::LinkOperationEvent(OperationEvent& operation_event) {
+ if (m_context_writer) {
+ m_context_writer->Link(operation_event);
+ }
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/system_clock_core.h b/src/core/hle/service/psc/time/clocks/system_clock_core.h
new file mode 100644
index 000000000..73811712e
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/system_clock_core.h
@@ -0,0 +1,55 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/result.h"
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+class ContextWriter;
+
+class SystemClockCore {
+public:
+ explicit SystemClockCore(SteadyClockCore& steady_clock) : m_steady_clock{steady_clock} {}
+ virtual ~SystemClockCore() = default;
+
+ SteadyClockCore& GetSteadyClock() {
+ return m_steady_clock;
+ }
+
+ bool IsInitialized() const {
+ return m_initialized;
+ }
+
+ void SetInitialized() {
+ m_initialized = true;
+ }
+
+ void SetContextWriter(ContextWriter& context_writer) {
+ m_context_writer = &context_writer;
+ }
+
+ bool CheckClockSourceMatches();
+
+ Result GetCurrentTime(s64* out_time) const;
+ Result SetCurrentTime(s64 time);
+
+ Result GetCurrentTimePoint(SteadyClockTimePoint& out_time_point) {
+ R_RETURN(m_steady_clock.GetCurrentTimePoint(out_time_point));
+ }
+
+ virtual Result GetContext(SystemClockContext& out_context) const;
+ virtual Result SetContext(SystemClockContext& context);
+ Result SetContextAndWrite(SystemClockContext& context);
+
+ void LinkOperationEvent(OperationEvent& operation_event);
+
+private:
+ bool m_initialized{};
+ ContextWriter* m_context_writer{};
+ SteadyClockCore& m_steady_clock;
+ SystemClockContext m_context{};
+};
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.cpp b/src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.cpp
new file mode 100644
index 000000000..22da1fbcc
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.cpp
@@ -0,0 +1,43 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <chrono>
+
+#include "core/core.h"
+#include "core/core_timing.h"
+#include "core/hle/service/psc/time/clocks/tick_based_steady_clock_core.h"
+
+namespace Service::PSC::Time {
+
+Result TickBasedSteadyClockCore::GetCurrentTimePointImpl(SteadyClockTimePoint& out_time_point) {
+ auto ticks{m_system.CoreTiming().GetClockTicks()};
+ auto current_time_s =
+ std::chrono::duration_cast<std::chrono::seconds>(ConvertToTimeSpan(ticks)).count();
+ out_time_point.time_point = current_time_s;
+ out_time_point.clock_source_id = m_clock_source_id;
+ R_SUCCEED();
+}
+
+s64 TickBasedSteadyClockCore::GetCurrentRawTimePointImpl() {
+ SteadyClockTimePoint time_point{};
+ if (GetCurrentTimePointImpl(time_point) != ResultSuccess) {
+ LOG_ERROR(Service_Time, "Failed to GetCurrentTimePoint!");
+ }
+ return std::chrono::duration_cast<std::chrono::nanoseconds>(
+ std::chrono::seconds(time_point.time_point))
+ .count();
+}
+
+s64 TickBasedSteadyClockCore::GetTestOffsetImpl() const {
+ return 0;
+}
+
+void TickBasedSteadyClockCore::SetTestOffsetImpl(s64 offset) {}
+
+s64 TickBasedSteadyClockCore::GetInternalOffsetImpl() const {
+ return 0;
+}
+
+void TickBasedSteadyClockCore::SetInternalOffsetImpl(s64 offset) {}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.h b/src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.h
new file mode 100644
index 000000000..a7bea86a2
--- /dev/null
+++ b/src/core/hle/service/psc/time/clocks/tick_based_steady_clock_core.h
@@ -0,0 +1,41 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <mutex>
+
+#include "common/uuid.h"
+#include "core/hle/service/psc/time/clocks/steady_clock_core.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+class TickBasedSteadyClockCore : public SteadyClockCore {
+public:
+ explicit TickBasedSteadyClockCore(Core::System& system) : m_system{system} {}
+ ~TickBasedSteadyClockCore() override = default;
+
+ Result GetCurrentTimePointImpl(SteadyClockTimePoint& out_time_point) override;
+ s64 GetCurrentRawTimePointImpl() override;
+ s64 GetTestOffsetImpl() const override;
+ void SetTestOffsetImpl(s64 offset) override;
+ s64 GetInternalOffsetImpl() const override;
+ void SetInternalOffsetImpl(s64 offset) override;
+
+ Result GetRtcValueImpl(s64& out_value) override {
+ R_RETURN(ResultNotImplemented);
+ }
+
+ Result GetSetupResultValueImpl() override {
+ R_SUCCEED();
+ }
+
+private:
+ Core::System& m_system;
+
+ ClockSourceId m_clock_source_id{Common::UUID::MakeRandom()};
+};
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/common.cpp b/src/core/hle/service/psc/time/common.cpp
new file mode 100644
index 000000000..a6d9f02ca
--- /dev/null
+++ b/src/core/hle/service/psc/time/common.cpp
@@ -0,0 +1,16 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+OperationEvent::OperationEvent(Core::System& system)
+ : m_ctx{system, "Time:OperationEvent"}, m_event{
+ m_ctx.CreateEvent("Time:OperationEvent:Event")} {}
+
+OperationEvent::~OperationEvent() {
+ m_ctx.CloseEvent(m_event);
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/common.h b/src/core/hle/service/psc/time/common.h
new file mode 100644
index 000000000..d17b31143
--- /dev/null
+++ b/src/core/hle/service/psc/time/common.h
@@ -0,0 +1,168 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <array>
+#include <chrono>
+
+#include "common/common_types.h"
+#include "common/intrusive_list.h"
+#include "common/uuid.h"
+#include "common/wall_clock.h"
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/service/kernel_helpers.h"
+#include "core/hle/service/psc/time/errors.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+using ClockSourceId = Common::UUID;
+
+struct SteadyClockTimePoint {
+ constexpr bool IdMatches(SteadyClockTimePoint& other) {
+ return clock_source_id == other.clock_source_id;
+ }
+ bool operator==(const SteadyClockTimePoint& other) const = default;
+
+ s64 time_point;
+ ClockSourceId clock_source_id;
+};
+static_assert(sizeof(SteadyClockTimePoint) == 0x18, "SteadyClockTimePoint has the wrong size!");
+static_assert(std::is_trivial_v<ClockSourceId>);
+
+struct SystemClockContext {
+ bool operator==(const SystemClockContext& other) const = default;
+
+ s64 offset;
+ SteadyClockTimePoint steady_time_point;
+};
+static_assert(sizeof(SystemClockContext) == 0x20, "SystemClockContext has the wrong size!");
+static_assert(std::is_trivial_v<SystemClockContext>);
+
+enum class TimeType : u8 {
+ UserSystemClock,
+ NetworkSystemClock,
+ LocalSystemClock,
+};
+
+struct CalendarTime {
+ s16 year;
+ s8 month;
+ s8 day;
+ s8 hour;
+ s8 minute;
+ s8 second;
+};
+static_assert(sizeof(CalendarTime) == 0x8, "CalendarTime has the wrong size!");
+
+struct CalendarAdditionalInfo {
+ s32 day_of_week;
+ s32 day_of_year;
+ std::array<char, 8> name;
+ s32 is_dst;
+ s32 ut_offset;
+};
+static_assert(sizeof(CalendarAdditionalInfo) == 0x18, "CalendarAdditionalInfo has the wrong size!");
+
+struct LocationName {
+ std::array<char, 36> name;
+};
+static_assert(sizeof(LocationName) == 0x24, "LocationName has the wrong size!");
+
+struct RuleVersion {
+ std::array<char, 16> version;
+};
+static_assert(sizeof(RuleVersion) == 0x10, "RuleVersion has the wrong size!");
+
+struct ClockSnapshot {
+ SystemClockContext user_context;
+ SystemClockContext network_context;
+ s64 user_time;
+ s64 network_time;
+ CalendarTime user_calendar_time;
+ CalendarTime network_calendar_time;
+ CalendarAdditionalInfo user_calendar_additional_time;
+ CalendarAdditionalInfo network_calendar_additional_time;
+ SteadyClockTimePoint steady_clock_time_point;
+ LocationName location_name;
+ bool is_automatic_correction_enabled;
+ TimeType type;
+ u16 unk_CE;
+};
+static_assert(sizeof(ClockSnapshot) == 0xD0, "ClockSnapshot has the wrong size!");
+static_assert(std::is_trivial_v<ClockSnapshot>);
+
+struct ContinuousAdjustmentTimePoint {
+ s64 rtc_offset;
+ s64 diff_scale;
+ s64 shift_amount;
+ s64 lower;
+ s64 upper;
+ ClockSourceId clock_source_id;
+};
+static_assert(sizeof(ContinuousAdjustmentTimePoint) == 0x38,
+ "ContinuousAdjustmentTimePoint has the wrong size!");
+static_assert(std::is_trivial_v<ContinuousAdjustmentTimePoint>);
+
+struct AlarmInfo {
+ s64 alert_time;
+ u32 priority;
+};
+static_assert(sizeof(AlarmInfo) == 0x10, "AlarmInfo has the wrong size!");
+
+struct StaticServiceSetupInfo {
+ bool can_write_local_clock;
+ bool can_write_user_clock;
+ bool can_write_network_clock;
+ bool can_write_timezone_device_location;
+ bool can_write_steady_clock;
+ bool can_write_uninitialized_clock;
+};
+static_assert(sizeof(StaticServiceSetupInfo) == 0x6, "StaticServiceSetupInfo has the wrong size!");
+
+struct OperationEvent : public Common::IntrusiveListBaseNode<OperationEvent> {
+ using OperationEventList = Common::IntrusiveListBaseTraits<OperationEvent>::ListType;
+
+ OperationEvent(Core::System& system);
+ ~OperationEvent();
+
+ KernelHelpers::ServiceContext m_ctx;
+ Kernel::KEvent* m_event{};
+};
+
+constexpr inline std::chrono::nanoseconds ConvertToTimeSpan(s64 ticks) {
+ constexpr auto one_second_ns{
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(1)).count()};
+
+ constexpr s64 max{Common::WallClock::CNTFRQ *
+ (std::numeric_limits<s64>::max() / one_second_ns)};
+
+ if (ticks > max) {
+ return std::chrono::nanoseconds(std::numeric_limits<s64>::max());
+ } else if (ticks < -max) {
+ return std::chrono::nanoseconds(std::numeric_limits<s64>::min());
+ }
+
+ auto a{ticks / Common::WallClock::CNTFRQ * one_second_ns};
+ auto b{((ticks % Common::WallClock::CNTFRQ) * one_second_ns) / Common::WallClock::CNTFRQ};
+
+ return std::chrono::nanoseconds(a + b);
+}
+
+constexpr inline Result GetSpanBetweenTimePoints(s64* out_seconds, SteadyClockTimePoint& a,
+ SteadyClockTimePoint& b) {
+ R_UNLESS(out_seconds, ResultInvalidArgument);
+ R_UNLESS(a.IdMatches(b), ResultInvalidArgument);
+ R_UNLESS(a.time_point >= 0 || b.time_point <= a.time_point + std::numeric_limits<s64>::max(),
+ ResultOverflow);
+ R_UNLESS(a.time_point < 0 || b.time_point >= a.time_point + std::numeric_limits<s64>::min(),
+ ResultOverflow);
+
+ *out_seconds = b.time_point - a.time_point;
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/errors.h b/src/core/hle/service/psc/time/errors.h
new file mode 100644
index 000000000..6d833a006
--- /dev/null
+++ b/src/core/hle/service/psc/time/errors.h
@@ -0,0 +1,24 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/result.h"
+
+namespace Service::PSC::Time {
+
+constexpr Result ResultPermissionDenied{ErrorModule::Time, 1};
+constexpr Result ResultClockMismatch{ErrorModule::Time, 102};
+constexpr Result ResultClockUninitialized{ErrorModule::Time, 103};
+constexpr Result ResultTimeNotFound{ErrorModule::Time, 200};
+constexpr Result ResultOverflow{ErrorModule::Time, 201};
+constexpr Result ResultFailed{ErrorModule::Time, 801};
+constexpr Result ResultInvalidArgument{ErrorModule::Time, 901};
+constexpr Result ResultTimeZoneOutOfRange{ErrorModule::Time, 902};
+constexpr Result ResultTimeZoneParseFailed{ErrorModule::Time, 903};
+constexpr Result ResultRtcTimeout{ErrorModule::Time, 988};
+constexpr Result ResultTimeZoneNotFound{ErrorModule::Time, 989};
+constexpr Result ResultNotImplemented{ErrorModule::Time, 990};
+constexpr Result ResultAlarmNotRegistered{ErrorModule::Time, 1502};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/manager.h b/src/core/hle/service/psc/time/manager.h
new file mode 100644
index 000000000..62ded1247
--- /dev/null
+++ b/src/core/hle/service/psc/time/manager.h
@@ -0,0 +1,56 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/psc/time/alarms.h"
+#include "core/hle/service/psc/time/clocks/context_writers.h"
+#include "core/hle/service/psc/time/clocks/ephemeral_network_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_local_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_network_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_steady_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_user_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/tick_based_steady_clock_core.h"
+#include "core/hle/service/psc/time/power_state_request_manager.h"
+#include "core/hle/service/psc/time/shared_memory.h"
+#include "core/hle/service/psc/time/time_zone.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+class TimeManager {
+public:
+ explicit TimeManager(Core::System& system)
+ : m_system{system}, m_standard_steady_clock{system}, m_tick_based_steady_clock{m_system},
+ m_standard_local_system_clock{m_standard_steady_clock},
+ m_standard_network_system_clock{m_standard_steady_clock},
+ m_standard_user_system_clock{m_system, m_standard_local_system_clock,
+ m_standard_network_system_clock},
+ m_ephemeral_network_clock{m_tick_based_steady_clock}, m_shared_memory{m_system},
+ m_power_state_request_manager{m_system}, m_alarms{m_system, m_standard_steady_clock,
+ m_power_state_request_manager},
+ m_local_system_clock_context_writer{m_system, m_shared_memory},
+ m_network_system_clock_context_writer{m_system, m_shared_memory,
+ m_standard_user_system_clock},
+ m_ephemeral_network_clock_context_writer{m_system} {}
+
+ Core::System& m_system;
+
+ StandardSteadyClockCore m_standard_steady_clock;
+ TickBasedSteadyClockCore m_tick_based_steady_clock;
+ StandardLocalSystemClockCore m_standard_local_system_clock;
+ StandardNetworkSystemClockCore m_standard_network_system_clock;
+ StandardUserSystemClockCore m_standard_user_system_clock;
+ EphemeralNetworkSystemClockCore m_ephemeral_network_clock;
+ TimeZone m_time_zone;
+ SharedMemory m_shared_memory;
+ PowerStateRequestManager m_power_state_request_manager;
+ Alarms m_alarms;
+ LocalSystemClockContextWriter m_local_system_clock_context_writer;
+ NetworkSystemClockContextWriter m_network_system_clock_context_writer;
+ EphemeralNetworkSystemClockContextWriter m_ephemeral_network_clock_context_writer;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/power_state_request_manager.cpp b/src/core/hle/service/psc/time/power_state_request_manager.cpp
new file mode 100644
index 000000000..17de0bf4d
--- /dev/null
+++ b/src/core/hle/service/psc/time/power_state_request_manager.cpp
@@ -0,0 +1,50 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/power_state_request_manager.h"
+
+namespace Service::PSC::Time {
+
+PowerStateRequestManager::PowerStateRequestManager(Core::System& system)
+ : m_system{system}, m_ctx{system, "Psc:PowerStateRequestManager"},
+ m_event{m_ctx.CreateEvent("Psc:PowerStateRequestManager:Event")} {}
+
+PowerStateRequestManager::~PowerStateRequestManager() {
+ m_ctx.CloseEvent(m_event);
+}
+
+void PowerStateRequestManager::UpdatePendingPowerStateRequestPriority(u32 priority) {
+ std::scoped_lock l{m_mutex};
+ if (m_has_pending_request) {
+ m_pending_request_priority = std::max(m_pending_request_priority, priority);
+ } else {
+ m_pending_request_priority = priority;
+ m_has_pending_request = true;
+ }
+}
+
+void PowerStateRequestManager::SignalPowerStateRequestAvailability() {
+ std::scoped_lock l{m_mutex};
+ if (m_has_pending_request) {
+ if (!m_has_available_request) {
+ m_has_available_request = true;
+ }
+ m_has_pending_request = false;
+ m_available_request_priority = m_pending_request_priority;
+ m_event->Signal();
+ }
+}
+
+bool PowerStateRequestManager::GetAndClearPowerStateRequest(u32& out_priority) {
+ std::scoped_lock l{m_mutex};
+ auto had_request{m_has_available_request};
+ if (m_has_available_request) {
+ out_priority = m_available_request_priority;
+ m_has_available_request = false;
+ m_event->Clear();
+ }
+ return had_request;
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/power_state_request_manager.h b/src/core/hle/service/psc/time/power_state_request_manager.h
new file mode 100644
index 000000000..30a0c947d
--- /dev/null
+++ b/src/core/hle/service/psc/time/power_state_request_manager.h
@@ -0,0 +1,42 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <mutex>
+
+#include "core/hle/kernel/k_event.h"
+#include "core/hle/service/kernel_helpers.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+
+class PowerStateRequestManager {
+public:
+ explicit PowerStateRequestManager(Core::System& system);
+ ~PowerStateRequestManager();
+
+ Kernel::KReadableEvent& GetReadableEvent() {
+ return m_event->GetReadableEvent();
+ }
+
+ void UpdatePendingPowerStateRequestPriority(u32 priority);
+ void SignalPowerStateRequestAvailability();
+ bool GetAndClearPowerStateRequest(u32& out_priority);
+
+private:
+ Core::System& m_system;
+ KernelHelpers::ServiceContext m_ctx;
+
+ Kernel::KEvent* m_event{};
+ bool m_has_pending_request{};
+ u32 m_pending_request_priority{};
+ bool m_has_available_request{};
+ u32 m_available_request_priority{};
+ std::mutex m_mutex;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/power_state_service.cpp b/src/core/hle/service/psc/time/power_state_service.cpp
new file mode 100644
index 000000000..b0ae71bf9
--- /dev/null
+++ b/src/core/hle/service/psc/time/power_state_service.cpp
@@ -0,0 +1,49 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/psc/time/power_state_service.h"
+
+namespace Service::PSC::Time {
+
+IPowerStateRequestHandler::IPowerStateRequestHandler(
+ Core::System& system_, PowerStateRequestManager& power_state_request_manager)
+ : ServiceFramework{system_, "time:p"}, m_system{system}, m_power_state_request_manager{
+ power_state_request_manager} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &IPowerStateRequestHandler::GetPowerStateRequestEventReadableHandle, "GetPowerStateRequestEventReadableHandle"},
+ {1, &IPowerStateRequestHandler::GetAndClearPowerStateRequest, "GetAndClearPowerStateRequest"},
+ };
+ // clang-format on
+
+ RegisterHandlers(functions);
+}
+
+void IPowerStateRequestHandler::GetPowerStateRequestEventReadableHandle(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(ResultSuccess);
+ rb.PushCopyObjects(m_power_state_request_manager.GetReadableEvent());
+}
+
+void IPowerStateRequestHandler::GetAndClearPowerStateRequest(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ u32 priority{};
+ auto cleared = m_power_state_request_manager.GetAndClearPowerStateRequest(priority);
+
+ if (cleared) {
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(ResultSuccess);
+ rb.Push(priority);
+ rb.Push(cleared);
+ return;
+ }
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(ResultSuccess);
+ rb.Push(cleared);
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/power_state_service.h b/src/core/hle/service/psc/time/power_state_service.h
new file mode 100644
index 000000000..3ebfddb79
--- /dev/null
+++ b/src/core/hle/service/psc/time/power_state_service.h
@@ -0,0 +1,32 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/psc/time/power_state_request_manager.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+
+class IPowerStateRequestHandler final : public ServiceFramework<IPowerStateRequestHandler> {
+public:
+ explicit IPowerStateRequestHandler(Core::System& system,
+ PowerStateRequestManager& power_state_request_manager);
+
+ ~IPowerStateRequestHandler() override = default;
+
+private:
+ void GetPowerStateRequestEventReadableHandle(HLERequestContext& ctx);
+ void GetAndClearPowerStateRequest(HLERequestContext& ctx);
+
+ Core::System& m_system;
+ PowerStateRequestManager& m_power_state_request_manager;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/service_manager.cpp b/src/core/hle/service/psc/time/service_manager.cpp
new file mode 100644
index 000000000..60820aa9b
--- /dev/null
+++ b/src/core/hle/service/psc/time/service_manager.cpp
@@ -0,0 +1,494 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/core_timing.h"
+#include "core/hle/service/psc/time/power_state_service.h"
+#include "core/hle/service/psc/time/service_manager.h"
+#include "core/hle/service/psc/time/static.h"
+
+namespace Service::PSC::Time {
+
+ServiceManager::ServiceManager(Core::System& system_, std::shared_ptr<TimeManager> time,
+ ServerManager* server_manager)
+ : ServiceFramework{system_, "time:m"}, m_system{system}, m_time{std::move(time)},
+ m_server_manager{*server_manager},
+ m_local_system_clock{m_time->m_standard_local_system_clock},
+ m_user_system_clock{m_time->m_standard_user_system_clock},
+ m_network_system_clock{m_time->m_standard_network_system_clock},
+ m_steady_clock{m_time->m_standard_steady_clock}, m_time_zone{m_time->m_time_zone},
+ m_ephemeral_network_clock{m_time->m_ephemeral_network_clock},
+ m_shared_memory{m_time->m_shared_memory}, m_alarms{m_time->m_alarms},
+ m_local_system_context_writer{m_time->m_local_system_clock_context_writer},
+ m_network_system_context_writer{m_time->m_network_system_clock_context_writer},
+ m_ephemeral_system_context_writer{m_time->m_ephemeral_network_clock_context_writer},
+ m_local_operation{m_system}, m_network_operation{m_system}, m_ephemeral_operation{m_system} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &ServiceManager::Handle_GetStaticServiceAsUser, "GetStaticServiceAsUser"},
+ {5, &ServiceManager::Handle_GetStaticServiceAsAdmin, "GetStaticServiceAsAdmin"},
+ {6, &ServiceManager::Handle_GetStaticServiceAsRepair, "GetStaticServiceAsRepair"},
+ {9, &ServiceManager::Handle_GetStaticServiceAsServiceManager, "GetStaticServiceAsServiceManager"},
+ {10, &ServiceManager::Handle_SetupStandardSteadyClockCore, "SetupStandardSteadyClockCore"},
+ {11, &ServiceManager::Handle_SetupStandardLocalSystemClockCore, "SetupStandardLocalSystemClockCore"},
+ {12, &ServiceManager::Handle_SetupStandardNetworkSystemClockCore, "SetupStandardNetworkSystemClockCore"},
+ {13, &ServiceManager::Handle_SetupStandardUserSystemClockCore, "SetupStandardUserSystemClockCore"},
+ {14, &ServiceManager::Handle_SetupTimeZoneServiceCore, "SetupTimeZoneServiceCore"},
+ {15, &ServiceManager::Handle_SetupEphemeralNetworkSystemClockCore, "SetupEphemeralNetworkSystemClockCore"},
+ {50, &ServiceManager::Handle_GetStandardLocalClockOperationEvent, "GetStandardLocalClockOperationEvent"},
+ {51, &ServiceManager::Handle_GetStandardNetworkClockOperationEventForServiceManager, "GetStandardNetworkClockOperationEventForServiceManager"},
+ {52, &ServiceManager::Handle_GetEphemeralNetworkClockOperationEventForServiceManager, "GetEphemeralNetworkClockOperationEventForServiceManager"},
+ {60, &ServiceManager::Handle_GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent, "GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent"},
+ {100, &ServiceManager::Handle_SetStandardSteadyClockBaseTime, "SetStandardSteadyClockBaseTime"},
+ {200, &ServiceManager::Handle_GetClosestAlarmUpdatedEvent, "GetClosestAlarmUpdatedEvent"},
+ {201, &ServiceManager::Handle_CheckAndSignalAlarms, "CheckAndSignalAlarms"},
+ {202, &ServiceManager::Handle_GetClosestAlarmInfo, "GetClosestAlarmInfo "},
+ };
+ // clang-format on
+ RegisterHandlers(functions);
+
+ m_local_system_context_writer.Link(m_local_operation);
+ m_network_system_context_writer.Link(m_network_operation);
+ m_ephemeral_system_context_writer.Link(m_ephemeral_operation);
+}
+
+void ServiceManager::SetupSAndP() {
+ if (!m_is_s_and_p_setup) {
+ m_is_s_and_p_setup = true;
+ m_server_manager.RegisterNamedService(
+ "time:s", std::make_shared<StaticService>(
+ m_system, StaticServiceSetupInfo{0, 0, 1, 0, 0, 0}, m_time, "time:s"));
+ m_server_manager.RegisterNamedService("time:p",
+ std::make_shared<IPowerStateRequestHandler>(
+ m_system, m_time->m_power_state_request_manager));
+ }
+}
+
+void ServiceManager::CheckAndSetupServicesSAndP() {
+ if (m_local_system_clock.IsInitialized() && m_user_system_clock.IsInitialized() &&
+ m_network_system_clock.IsInitialized() && m_steady_clock.IsInitialized() &&
+ m_time_zone.IsInitialized() && m_ephemeral_network_clock.IsInitialized()) {
+ SetupSAndP();
+ }
+}
+
+void ServiceManager::Handle_GetStaticServiceAsUser(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<StaticService> service{};
+ auto res = GetStaticServiceAsUser(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<StaticService>(std::move(service));
+}
+
+void ServiceManager::Handle_GetStaticServiceAsAdmin(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<StaticService> service{};
+ auto res = GetStaticServiceAsAdmin(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<StaticService>(std::move(service));
+}
+
+void ServiceManager::Handle_GetStaticServiceAsRepair(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<StaticService> service{};
+ auto res = GetStaticServiceAsRepair(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<StaticService>(std::move(service));
+}
+
+void ServiceManager::Handle_GetStaticServiceAsServiceManager(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<StaticService> service{};
+ auto res = GetStaticServiceAsServiceManager(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<StaticService>(std::move(service));
+}
+
+void ServiceManager::Handle_SetupStandardSteadyClockCore(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ struct Parameters {
+ bool reset_detected;
+ Common::UUID clock_source_id;
+ s64 rtc_offset;
+ s64 internal_offset;
+ s64 test_offset;
+ };
+ static_assert(sizeof(Parameters) == 0x30);
+
+ IPC::RequestParser rp{ctx};
+ auto params{rp.PopRaw<Parameters>()};
+
+ auto res = SetupStandardSteadyClockCore(params.clock_source_id, params.rtc_offset,
+ params.internal_offset, params.test_offset,
+ params.reset_detected);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_SetupStandardLocalSystemClockCore(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto context{rp.PopRaw<SystemClockContext>()};
+ auto time{rp.Pop<s64>()};
+
+ auto res = SetupStandardLocalSystemClockCore(context, time);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_SetupStandardNetworkSystemClockCore(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto context{rp.PopRaw<SystemClockContext>()};
+ auto accuracy{rp.Pop<s64>()};
+
+ auto res = SetupStandardNetworkSystemClockCore(context, accuracy);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_SetupStandardUserSystemClockCore(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ struct Parameters {
+ bool automatic_correction;
+ SteadyClockTimePoint time_point;
+ };
+ static_assert(sizeof(Parameters) == 0x20);
+
+ IPC::RequestParser rp{ctx};
+ auto params{rp.PopRaw<Parameters>()};
+
+ auto res = SetupStandardUserSystemClockCore(params.time_point, params.automatic_correction);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_SetupTimeZoneServiceCore(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ struct Parameters {
+ u32 location_count;
+ LocationName name;
+ SteadyClockTimePoint time_point;
+ RuleVersion rule_version;
+ };
+ static_assert(sizeof(Parameters) == 0x50);
+
+ IPC::RequestParser rp{ctx};
+ auto params{rp.PopRaw<Parameters>()};
+
+ auto rule_buffer{ctx.ReadBuffer()};
+
+ auto res = SetupTimeZoneServiceCore(params.name, params.time_point, params.rule_version,
+ params.location_count, rule_buffer);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_SetupEphemeralNetworkSystemClockCore(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ auto res = SetupEphemeralNetworkSystemClockCore();
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_GetStandardLocalClockOperationEvent(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KEvent* event{};
+ auto res = GetStandardLocalClockOperationEvent(&event);
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(res);
+ rb.PushCopyObjects(event->GetReadableEvent());
+}
+
+void ServiceManager::Handle_GetStandardNetworkClockOperationEventForServiceManager(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KEvent* event{};
+ auto res = GetStandardNetworkClockOperationEventForServiceManager(&event);
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(res);
+ rb.PushCopyObjects(event);
+}
+
+void ServiceManager::Handle_GetEphemeralNetworkClockOperationEventForServiceManager(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KEvent* event{};
+ auto res = GetEphemeralNetworkClockOperationEventForServiceManager(&event);
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(res);
+ rb.PushCopyObjects(event);
+}
+
+void ServiceManager::Handle_GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KEvent* event{};
+ auto res = GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent(&event);
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(res);
+ rb.PushCopyObjects(event);
+}
+
+void ServiceManager::Handle_SetStandardSteadyClockBaseTime(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto base_time{rp.Pop<s64>()};
+
+ auto res = SetStandardSteadyClockBaseTime(base_time);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_GetClosestAlarmUpdatedEvent(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KEvent* event{};
+ auto res = GetClosestAlarmUpdatedEvent(&event);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.PushCopyObjects(event->GetReadableEvent());
+}
+
+void ServiceManager::Handle_CheckAndSignalAlarms(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ auto res = CheckAndSignalAlarms();
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void ServiceManager::Handle_GetClosestAlarmInfo(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ AlarmInfo alarm_info{};
+ bool is_valid{};
+ s64 time{};
+ auto res = GetClosestAlarmInfo(is_valid, alarm_info, time);
+
+ struct OutParameters {
+ bool is_valid;
+ AlarmInfo alarm_info;
+ s64 time;
+ };
+ static_assert(sizeof(OutParameters) == 0x20);
+
+ OutParameters out_params{
+ .is_valid = is_valid,
+ .alarm_info = alarm_info,
+ .time = time,
+ };
+
+ IPC::ResponseBuilder rb{ctx, 2 + sizeof(OutParameters) / sizeof(u32)};
+ rb.Push(res);
+ rb.PushRaw<OutParameters>(out_params);
+}
+
+// =============================== Implementations ===========================
+
+Result ServiceManager::GetStaticService(std::shared_ptr<StaticService>& out_service,
+ StaticServiceSetupInfo setup_info, const char* name) {
+ out_service = std::make_shared<StaticService>(m_system, setup_info, m_time, name);
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetStaticServiceAsUser(std::shared_ptr<StaticService>& out_service) {
+ R_RETURN(GetStaticService(out_service, StaticServiceSetupInfo{0, 0, 0, 0, 0, 0}, "time:u"));
+}
+
+Result ServiceManager::GetStaticServiceAsAdmin(std::shared_ptr<StaticService>& out_service) {
+ R_RETURN(GetStaticService(out_service, StaticServiceSetupInfo{1, 1, 0, 1, 0, 0}, "time:a"));
+}
+
+Result ServiceManager::GetStaticServiceAsRepair(std::shared_ptr<StaticService>& out_service) {
+ R_RETURN(GetStaticService(out_service, StaticServiceSetupInfo{0, 0, 0, 0, 1, 0}, "time:r"));
+}
+
+Result ServiceManager::GetStaticServiceAsServiceManager(
+ std::shared_ptr<StaticService>& out_service) {
+ R_RETURN(GetStaticService(out_service, StaticServiceSetupInfo{1, 1, 1, 1, 1, 0}, "time:sm"));
+}
+
+Result ServiceManager::SetupStandardSteadyClockCore(Common::UUID& clock_source_id, s64 rtc_offset,
+ s64 internal_offset, s64 test_offset,
+ bool is_rtc_reset_detected) {
+ m_steady_clock.Initialize(clock_source_id, rtc_offset, internal_offset, test_offset,
+ is_rtc_reset_detected);
+ auto time = m_steady_clock.GetRawTime();
+ auto ticks = m_system.CoreTiming().GetClockTicks();
+ auto boot_time = time - ConvertToTimeSpan(ticks).count();
+ m_shared_memory.SetSteadyClockTimePoint(clock_source_id, boot_time);
+ m_steady_clock.SetContinuousAdjustment(clock_source_id, boot_time);
+
+ ContinuousAdjustmentTimePoint time_point{};
+ m_steady_clock.GetContinuousAdjustment(time_point);
+ m_shared_memory.SetContinuousAdjustment(time_point);
+
+ CheckAndSetupServicesSAndP();
+ R_SUCCEED();
+}
+
+Result ServiceManager::SetupStandardLocalSystemClockCore(SystemClockContext& context, s64 time) {
+ m_local_system_clock.SetContextWriter(m_local_system_context_writer);
+ m_local_system_clock.Initialize(context, time);
+
+ CheckAndSetupServicesSAndP();
+ R_SUCCEED();
+}
+
+Result ServiceManager::SetupStandardNetworkSystemClockCore(SystemClockContext& context,
+ s64 accuracy) {
+ // TODO this is a hack! The network clock should be updated independently, from the ntc service
+ // and maybe elsewhere. We do not do that, so fix the clock to the local clock on first boot
+ // to avoid it being stuck at 0.
+ if (context == Service::PSC::Time::SystemClockContext{}) {
+ m_local_system_clock.GetContext(context);
+ }
+
+ m_network_system_clock.SetContextWriter(m_network_system_context_writer);
+ m_network_system_clock.Initialize(context, accuracy);
+
+ CheckAndSetupServicesSAndP();
+ R_SUCCEED();
+}
+
+Result ServiceManager::SetupStandardUserSystemClockCore(SteadyClockTimePoint& time_point,
+ bool automatic_correction) {
+ // TODO this is a hack! The user clock should be updated independently, from the ntc service
+ // and maybe elsewhere. We do not do that, so fix the clock to the local clock on first boot
+ // to avoid it being stuck at 0.
+ if (time_point == Service::PSC::Time::SteadyClockTimePoint{}) {
+ m_local_system_clock.GetCurrentTimePoint(time_point);
+ }
+
+ m_user_system_clock.SetAutomaticCorrection(automatic_correction);
+ m_user_system_clock.SetTimePointAndSignal(time_point);
+ m_user_system_clock.SetInitialized();
+ m_shared_memory.SetAutomaticCorrection(automatic_correction);
+
+ CheckAndSetupServicesSAndP();
+ R_SUCCEED();
+}
+
+Result ServiceManager::SetupTimeZoneServiceCore(LocationName& name,
+ SteadyClockTimePoint& time_point,
+ RuleVersion& rule_version, u32 location_count,
+ std::span<const u8> rule_buffer) {
+ if (m_time_zone.ParseBinary(name, rule_buffer) != ResultSuccess) {
+ LOG_ERROR(Service_Time, "Failed to parse time zone binary!");
+ }
+
+ m_time_zone.SetTimePoint(time_point);
+ m_time_zone.SetTotalLocationNameCount(location_count);
+ m_time_zone.SetRuleVersion(rule_version);
+ m_time_zone.SetInitialized();
+
+ CheckAndSetupServicesSAndP();
+ R_SUCCEED();
+}
+
+Result ServiceManager::SetupEphemeralNetworkSystemClockCore() {
+ m_ephemeral_network_clock.SetContextWriter(m_ephemeral_system_context_writer);
+ m_ephemeral_network_clock.SetInitialized();
+
+ CheckAndSetupServicesSAndP();
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetStandardLocalClockOperationEvent(Kernel::KEvent** out_event) {
+ *out_event = m_local_operation.m_event;
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetStandardNetworkClockOperationEventForServiceManager(
+ Kernel::KEvent** out_event) {
+ *out_event = m_network_operation.m_event;
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetEphemeralNetworkClockOperationEventForServiceManager(
+ Kernel::KEvent** out_event) {
+ *out_event = m_ephemeral_operation.m_event;
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent(
+ Kernel::KEvent** out_event) {
+ *out_event = &m_user_system_clock.GetEvent();
+ R_SUCCEED();
+}
+
+Result ServiceManager::SetStandardSteadyClockBaseTime(s64 base_time) {
+ m_steady_clock.SetRtcOffset(base_time);
+ auto time = m_steady_clock.GetRawTime();
+ auto ticks = m_system.CoreTiming().GetClockTicks();
+ auto diff = time - ConvertToTimeSpan(ticks).count();
+ m_shared_memory.UpdateBaseTime(diff);
+ m_steady_clock.UpdateContinuousAdjustmentTime(diff);
+
+ ContinuousAdjustmentTimePoint time_point{};
+ m_steady_clock.GetContinuousAdjustment(time_point);
+ m_shared_memory.SetContinuousAdjustment(time_point);
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetClosestAlarmUpdatedEvent(Kernel::KEvent** out_event) {
+ *out_event = &m_alarms.GetEvent();
+ R_SUCCEED();
+}
+
+Result ServiceManager::CheckAndSignalAlarms() {
+ m_alarms.CheckAndSignal();
+ R_SUCCEED();
+}
+
+Result ServiceManager::GetClosestAlarmInfo(bool& out_is_valid, AlarmInfo& out_info, s64& out_time) {
+ Alarm* alarm{nullptr};
+ out_is_valid = m_alarms.GetClosestAlarm(&alarm);
+ if (out_is_valid) {
+ out_info = {
+ .alert_time = alarm->GetAlertTime(),
+ .priority = alarm->GetPriority(),
+ };
+ out_time = m_alarms.GetRawTime();
+ }
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/service_manager.h b/src/core/hle/service/psc/time/service_manager.h
new file mode 100644
index 000000000..1d9952317
--- /dev/null
+++ b/src/core/hle/service/psc/time/service_manager.h
@@ -0,0 +1,101 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <list>
+#include <memory>
+
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/psc/time/manager.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Kernel {
+class KReadableEvent;
+}
+
+namespace Service::PSC::Time {
+class StaticService;
+
+class ServiceManager final : public ServiceFramework<ServiceManager> {
+public:
+ explicit ServiceManager(Core::System& system, std::shared_ptr<TimeManager> time,
+ ServerManager* server_manager);
+ ~ServiceManager() override = default;
+
+ Result GetStaticServiceAsUser(std::shared_ptr<StaticService>& out_service);
+ Result GetStaticServiceAsAdmin(std::shared_ptr<StaticService>& out_service);
+ Result GetStaticServiceAsRepair(std::shared_ptr<StaticService>& out_service);
+ Result GetStaticServiceAsServiceManager(std::shared_ptr<StaticService>& out_service);
+ Result SetupStandardSteadyClockCore(Common::UUID& clock_source_id, s64 rtc_offset,
+ s64 internal_offset, s64 test_offset,
+ bool is_rtc_reset_detected);
+ Result SetupStandardLocalSystemClockCore(SystemClockContext& context, s64 time);
+ Result SetupStandardNetworkSystemClockCore(SystemClockContext& context, s64 accuracy);
+ Result SetupStandardUserSystemClockCore(SteadyClockTimePoint& time_point,
+ bool automatic_correction);
+ Result SetupTimeZoneServiceCore(LocationName& name, SteadyClockTimePoint& time_point,
+ RuleVersion& rule_version, u32 location_count,
+ std::span<const u8> rule_buffer);
+ Result SetupEphemeralNetworkSystemClockCore();
+ Result GetStandardLocalClockOperationEvent(Kernel::KEvent** out_event);
+ Result GetStandardNetworkClockOperationEventForServiceManager(Kernel::KEvent** out_event);
+ Result GetEphemeralNetworkClockOperationEventForServiceManager(Kernel::KEvent** out_event);
+ Result GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent(Kernel::KEvent** out_event);
+ Result SetStandardSteadyClockBaseTime(s64 base_time);
+ Result GetClosestAlarmUpdatedEvent(Kernel::KEvent** out_event);
+ Result CheckAndSignalAlarms();
+ Result GetClosestAlarmInfo(bool& out_is_valid, AlarmInfo& out_info, s64& out_time);
+
+private:
+ void CheckAndSetupServicesSAndP();
+ void SetupSAndP();
+ Result GetStaticService(std::shared_ptr<StaticService>& out_service,
+ StaticServiceSetupInfo setup_info, const char* name);
+
+ void Handle_GetStaticServiceAsUser(HLERequestContext& ctx);
+ void Handle_GetStaticServiceAsAdmin(HLERequestContext& ctx);
+ void Handle_GetStaticServiceAsRepair(HLERequestContext& ctx);
+ void Handle_GetStaticServiceAsServiceManager(HLERequestContext& ctx);
+ void Handle_SetupStandardSteadyClockCore(HLERequestContext& ctx);
+ void Handle_SetupStandardLocalSystemClockCore(HLERequestContext& ctx);
+ void Handle_SetupStandardNetworkSystemClockCore(HLERequestContext& ctx);
+ void Handle_SetupStandardUserSystemClockCore(HLERequestContext& ctx);
+ void Handle_SetupTimeZoneServiceCore(HLERequestContext& ctx);
+ void Handle_SetupEphemeralNetworkSystemClockCore(HLERequestContext& ctx);
+ void Handle_GetStandardLocalClockOperationEvent(HLERequestContext& ctx);
+ void Handle_GetStandardNetworkClockOperationEventForServiceManager(HLERequestContext& ctx);
+ void Handle_GetEphemeralNetworkClockOperationEventForServiceManager(HLERequestContext& ctx);
+ void Handle_GetStandardUserSystemClockAutomaticCorrectionUpdatedEvent(HLERequestContext& ctx);
+ void Handle_SetStandardSteadyClockBaseTime(HLERequestContext& ctx);
+ void Handle_GetClosestAlarmUpdatedEvent(HLERequestContext& ctx);
+ void Handle_CheckAndSignalAlarms(HLERequestContext& ctx);
+ void Handle_GetClosestAlarmInfo(HLERequestContext& ctx);
+
+ Core::System& m_system;
+ std::shared_ptr<TimeManager> m_time;
+ ServerManager& m_server_manager;
+ bool m_is_s_and_p_setup{};
+ StandardLocalSystemClockCore& m_local_system_clock;
+ StandardUserSystemClockCore& m_user_system_clock;
+ StandardNetworkSystemClockCore& m_network_system_clock;
+ StandardSteadyClockCore& m_steady_clock;
+ TimeZone& m_time_zone;
+ EphemeralNetworkSystemClockCore& m_ephemeral_network_clock;
+ SharedMemory& m_shared_memory;
+ Alarms& m_alarms;
+ LocalSystemClockContextWriter& m_local_system_context_writer;
+ NetworkSystemClockContextWriter& m_network_system_context_writer;
+ EphemeralNetworkSystemClockContextWriter& m_ephemeral_system_context_writer;
+ OperationEvent m_local_operation;
+ OperationEvent m_network_operation;
+ OperationEvent m_ephemeral_operation;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/shared_memory.cpp b/src/core/hle/service/psc/time/shared_memory.cpp
new file mode 100644
index 000000000..defaceebe
--- /dev/null
+++ b/src/core/hle/service/psc/time/shared_memory.cpp
@@ -0,0 +1,84 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/kernel/k_shared_memory.h"
+#include "core/hle/service/psc/time/shared_memory.h"
+
+namespace Service::PSC::Time {
+namespace {
+template <typename T>
+constexpr inline T ReadFromLockFreeAtomicType(const LockFreeAtomicType<T>* p) {
+ while (true) {
+ // Get the counter.
+ auto counter = p->m_counter;
+
+ // Get the value.
+ auto value = p->m_value[counter % 2];
+
+ // Fence memory.
+ std::atomic_thread_fence(std::memory_order_acquire);
+
+ // Check that the counter matches.
+ if (counter == p->m_counter) {
+ return value;
+ }
+ }
+}
+
+template <typename T>
+constexpr inline void WriteToLockFreeAtomicType(LockFreeAtomicType<T>* p, const T& value) {
+ // Get the current counter.
+ auto counter = p->m_counter;
+
+ // Increment the counter.
+ ++counter;
+
+ // Store the updated value.
+ p->m_value[counter % 2] = value;
+
+ // Fence memory.
+ std::atomic_thread_fence(std::memory_order_release);
+
+ // Set the updated counter.
+ p->m_counter = counter;
+}
+} // namespace
+
+SharedMemory::SharedMemory(Core::System& system)
+ : m_system{system}, m_k_shared_memory{m_system.Kernel().GetTimeSharedMem()},
+ m_shared_memory_ptr{reinterpret_cast<SharedMemoryStruct*>(m_k_shared_memory.GetPointer())} {
+ std::memset(m_shared_memory_ptr, 0, sizeof(*m_shared_memory_ptr));
+}
+
+void SharedMemory::SetLocalSystemContext(SystemClockContext& context) {
+ WriteToLockFreeAtomicType(&m_shared_memory_ptr->local_system_clock_contexts, context);
+}
+
+void SharedMemory::SetNetworkSystemContext(SystemClockContext& context) {
+ WriteToLockFreeAtomicType(&m_shared_memory_ptr->network_system_clock_contexts, context);
+}
+
+void SharedMemory::SetSteadyClockTimePoint(ClockSourceId clock_source_id, s64 time_point) {
+ WriteToLockFreeAtomicType(&m_shared_memory_ptr->steady_time_points,
+ {time_point, clock_source_id});
+}
+
+void SharedMemory::SetContinuousAdjustment(ContinuousAdjustmentTimePoint& time_point) {
+ WriteToLockFreeAtomicType(&m_shared_memory_ptr->continuous_adjustment_time_points, time_point);
+}
+
+void SharedMemory::SetAutomaticCorrection(bool automatic_correction) {
+ WriteToLockFreeAtomicType(&m_shared_memory_ptr->automatic_corrections, automatic_correction);
+}
+
+void SharedMemory::UpdateBaseTime(s64 time) {
+ SteadyClockTimePoint time_point{
+ ReadFromLockFreeAtomicType(&m_shared_memory_ptr->steady_time_points)};
+
+ time_point.time_point = time;
+
+ WriteToLockFreeAtomicType(&m_shared_memory_ptr->steady_time_points, time_point);
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/shared_memory.h b/src/core/hle/service/psc/time/shared_memory.h
new file mode 100644
index 000000000..f9bf97d5c
--- /dev/null
+++ b/src/core/hle/service/psc/time/shared_memory.h
@@ -0,0 +1,70 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <array>
+
+#include "common/common_types.h"
+#include "core/hle/service/psc/time/common.h"
+
+namespace Core {
+class System;
+}
+
+namespace Kernel {
+class KSharedMemory;
+}
+
+namespace Service::PSC::Time {
+
+template <typename T>
+struct LockFreeAtomicType {
+ u32 m_counter;
+ std::array<T, 2> m_value;
+};
+
+struct SharedMemoryStruct {
+ LockFreeAtomicType<SteadyClockTimePoint> steady_time_points;
+ LockFreeAtomicType<SystemClockContext> local_system_clock_contexts;
+ LockFreeAtomicType<SystemClockContext> network_system_clock_contexts;
+ LockFreeAtomicType<bool> automatic_corrections;
+ LockFreeAtomicType<ContinuousAdjustmentTimePoint> continuous_adjustment_time_points;
+ std::array<char, 0xEB8> pad0148;
+};
+static_assert(offsetof(SharedMemoryStruct, steady_time_points) == 0x0,
+ "steady_time_points are in the wrong place!");
+static_assert(offsetof(SharedMemoryStruct, local_system_clock_contexts) == 0x38,
+ "local_system_clock_contexts are in the wrong place!");
+static_assert(offsetof(SharedMemoryStruct, network_system_clock_contexts) == 0x80,
+ "network_system_clock_contexts are in the wrong place!");
+static_assert(offsetof(SharedMemoryStruct, automatic_corrections) == 0xC8,
+ "automatic_corrections are in the wrong place!");
+static_assert(offsetof(SharedMemoryStruct, continuous_adjustment_time_points) == 0xD0,
+ "continuous_adjustment_time_points are in the wrong place!");
+static_assert(sizeof(SharedMemoryStruct) == 0x1000,
+ "Time's SharedMemoryStruct has the wrong size!");
+static_assert(std::is_trivial_v<SharedMemoryStruct>);
+
+class SharedMemory {
+public:
+ explicit SharedMemory(Core::System& system);
+
+ Kernel::KSharedMemory& GetKSharedMemory() {
+ return m_k_shared_memory;
+ }
+
+ void SetLocalSystemContext(SystemClockContext& context);
+ void SetNetworkSystemContext(SystemClockContext& context);
+ void SetSteadyClockTimePoint(ClockSourceId clock_source_id, s64 time_diff);
+ void SetContinuousAdjustment(ContinuousAdjustmentTimePoint& time_point);
+ void SetAutomaticCorrection(bool automatic_correction);
+ void UpdateBaseTime(s64 time);
+
+private:
+ Core::System& m_system;
+ Kernel::KSharedMemory& m_k_shared_memory;
+ SharedMemoryStruct* m_shared_memory_ptr;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/static.cpp b/src/core/hle/service/psc/time/static.cpp
new file mode 100644
index 000000000..6f8cf3f88
--- /dev/null
+++ b/src/core/hle/service/psc/time/static.cpp
@@ -0,0 +1,500 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/core_timing.h"
+#include "core/hle/kernel/k_shared_memory.h"
+#include "core/hle/service/psc/time/clocks/ephemeral_network_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_local_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_network_system_clock_core.h"
+#include "core/hle/service/psc/time/clocks/standard_user_system_clock_core.h"
+#include "core/hle/service/psc/time/manager.h"
+#include "core/hle/service/psc/time/shared_memory.h"
+#include "core/hle/service/psc/time/static.h"
+#include "core/hle/service/psc/time/steady_clock.h"
+#include "core/hle/service/psc/time/system_clock.h"
+#include "core/hle/service/psc/time/time_zone.h"
+#include "core/hle/service/psc/time/time_zone_service.h"
+
+namespace Service::PSC::Time {
+namespace {
+constexpr Result GetTimeFromTimePointAndContext(s64* out_time, SteadyClockTimePoint& time_point,
+ SystemClockContext& context) {
+ R_UNLESS(out_time != nullptr, ResultInvalidArgument);
+ R_UNLESS(time_point.IdMatches(context.steady_time_point), ResultClockMismatch);
+
+ *out_time = context.offset + time_point.time_point;
+ R_SUCCEED();
+}
+} // namespace
+
+StaticService::StaticService(Core::System& system_, StaticServiceSetupInfo setup_info,
+ std::shared_ptr<TimeManager> time, const char* name)
+ : ServiceFramework{system_, name}, m_system{system}, m_setup_info{setup_info}, m_time{time},
+ m_local_system_clock{m_time->m_standard_local_system_clock},
+ m_user_system_clock{m_time->m_standard_user_system_clock},
+ m_network_system_clock{m_time->m_standard_network_system_clock},
+ m_time_zone{m_time->m_time_zone},
+ m_ephemeral_network_clock{m_time->m_ephemeral_network_clock}, m_shared_memory{
+ m_time->m_shared_memory} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &StaticService::Handle_GetStandardUserSystemClock, "GetStandardUserSystemClock"},
+ {1, &StaticService::Handle_GetStandardNetworkSystemClock, "GetStandardNetworkSystemClock"},
+ {2, &StaticService::Handle_GetStandardSteadyClock, "GetStandardSteadyClock"},
+ {3, &StaticService::Handle_GetTimeZoneService, "GetTimeZoneService"},
+ {4, &StaticService::Handle_GetStandardLocalSystemClock, "GetStandardLocalSystemClock"},
+ {5, &StaticService::Handle_GetEphemeralNetworkSystemClock, "GetEphemeralNetworkSystemClock"},
+ {20, &StaticService::Handle_GetSharedMemoryNativeHandle, "GetSharedMemoryNativeHandle"},
+ {50, &StaticService::Handle_SetStandardSteadyClockInternalOffset, "SetStandardSteadyClockInternalOffset"},
+ {51, &StaticService::Handle_GetStandardSteadyClockRtcValue, "GetStandardSteadyClockRtcValue"},
+ {100, &StaticService::Handle_IsStandardUserSystemClockAutomaticCorrectionEnabled, "IsStandardUserSystemClockAutomaticCorrectionEnabled"},
+ {101, &StaticService::Handle_SetStandardUserSystemClockAutomaticCorrectionEnabled, "SetStandardUserSystemClockAutomaticCorrectionEnabled"},
+ {102, &StaticService::Handle_GetStandardUserSystemClockInitialYear, "GetStandardUserSystemClockInitialYear"},
+ {200, &StaticService::Handle_IsStandardNetworkSystemClockAccuracySufficient, "IsStandardNetworkSystemClockAccuracySufficient"},
+ {201, &StaticService::Handle_GetStandardUserSystemClockAutomaticCorrectionUpdatedTime, "GetStandardUserSystemClockAutomaticCorrectionUpdatedTime"},
+ {300, &StaticService::Handle_CalculateMonotonicSystemClockBaseTimePoint, "CalculateMonotonicSystemClockBaseTimePoint"},
+ {400, &StaticService::Handle_GetClockSnapshot, "GetClockSnapshot"},
+ {401, &StaticService::Handle_GetClockSnapshotFromSystemClockContext, "GetClockSnapshotFromSystemClockContext"},
+ {500, &StaticService::Handle_CalculateStandardUserSystemClockDifferenceByUser, "CalculateStandardUserSystemClockDifferenceByUser"},
+ {501, &StaticService::Handle_CalculateSpanBetween, "CalculateSpanBetween"},
+ };
+ // clang-format on
+
+ RegisterHandlers(functions);
+}
+
+Result StaticService::GetClockSnapshotImpl(ClockSnapshot& out_snapshot,
+ SystemClockContext& user_context,
+ SystemClockContext& network_context, TimeType type) {
+ out_snapshot.user_context = user_context;
+ out_snapshot.network_context = network_context;
+
+ R_TRY(
+ m_time->m_standard_steady_clock.GetCurrentTimePoint(out_snapshot.steady_clock_time_point));
+
+ out_snapshot.is_automatic_correction_enabled = m_user_system_clock.GetAutomaticCorrection();
+
+ R_TRY(m_time_zone.GetLocationName(out_snapshot.location_name));
+
+ R_TRY(GetTimeFromTimePointAndContext(
+ &out_snapshot.user_time, out_snapshot.steady_clock_time_point, out_snapshot.user_context));
+
+ R_TRY(m_time_zone.ToCalendarTimeWithMyRule(out_snapshot.user_calendar_time,
+ out_snapshot.user_calendar_additional_time,
+ out_snapshot.user_time));
+
+ if (GetTimeFromTimePointAndContext(&out_snapshot.network_time,
+ out_snapshot.steady_clock_time_point,
+ out_snapshot.network_context) != ResultSuccess) {
+ out_snapshot.network_time = 0;
+ }
+
+ R_TRY(m_time_zone.ToCalendarTimeWithMyRule(out_snapshot.network_calendar_time,
+ out_snapshot.network_calendar_additional_time,
+ out_snapshot.network_time));
+ out_snapshot.type = type;
+ out_snapshot.unk_CE = 0;
+ R_SUCCEED();
+}
+
+void StaticService::Handle_GetStandardUserSystemClock(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<SystemClock> service{};
+ auto res = GetStandardUserSystemClock(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<SystemClock>(std::move(service));
+}
+
+void StaticService::Handle_GetStandardNetworkSystemClock(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<SystemClock> service{};
+ auto res = GetStandardNetworkSystemClock(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<SystemClock>(std::move(service));
+}
+
+void StaticService::Handle_GetStandardSteadyClock(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<SteadyClock> service{};
+ auto res = GetStandardSteadyClock(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface(std::move(service));
+}
+
+void StaticService::Handle_GetTimeZoneService(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<TimeZoneService> service{};
+ auto res = GetTimeZoneService(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface(std::move(service));
+}
+
+void StaticService::Handle_GetStandardLocalSystemClock(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<SystemClock> service{};
+ auto res = GetStandardLocalSystemClock(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<SystemClock>(std::move(service));
+}
+
+void StaticService::Handle_GetEphemeralNetworkSystemClock(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ std::shared_ptr<SystemClock> service{};
+ auto res = GetEphemeralNetworkSystemClock(service);
+
+ IPC::ResponseBuilder rb{ctx, 2, 0, 1};
+ rb.Push(res);
+ rb.PushIpcInterface<SystemClock>(std::move(service));
+}
+
+void StaticService::Handle_GetSharedMemoryNativeHandle(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KSharedMemory* shared_memory{};
+ auto res = GetSharedMemoryNativeHandle(&shared_memory);
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(res);
+ rb.PushCopyObjects(shared_memory);
+}
+
+void StaticService::Handle_SetStandardSteadyClockInternalOffset(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(m_setup_info.can_write_steady_clock ? ResultNotImplemented : ResultPermissionDenied);
+}
+
+void StaticService::Handle_GetStandardSteadyClockRtcValue(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultNotImplemented);
+}
+
+void StaticService::Handle_IsStandardUserSystemClockAutomaticCorrectionEnabled(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ bool is_enabled{};
+ auto res = IsStandardUserSystemClockAutomaticCorrectionEnabled(is_enabled);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push<bool>(is_enabled);
+}
+
+void StaticService::Handle_SetStandardUserSystemClockAutomaticCorrectionEnabled(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto automatic_correction{rp.Pop<bool>()};
+
+ auto res = SetStandardUserSystemClockAutomaticCorrectionEnabled(automatic_correction);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void StaticService::Handle_GetStandardUserSystemClockInitialYear(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultNotImplemented);
+}
+
+void StaticService::Handle_IsStandardNetworkSystemClockAccuracySufficient(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ bool is_sufficient{};
+ auto res = IsStandardNetworkSystemClockAccuracySufficient(is_sufficient);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push<bool>(is_sufficient);
+}
+
+void StaticService::Handle_GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ SteadyClockTimePoint time_point{};
+ auto res = GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(time_point);
+
+ IPC::ResponseBuilder rb{ctx, 2 + sizeof(SteadyClockTimePoint) / sizeof(u32)};
+ rb.Push(res);
+ rb.PushRaw<SteadyClockTimePoint>(time_point);
+}
+
+void StaticService::Handle_CalculateMonotonicSystemClockBaseTimePoint(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto context{rp.PopRaw<SystemClockContext>()};
+
+ s64 time{};
+ auto res = CalculateMonotonicSystemClockBaseTimePoint(time, context);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push<s64>(time);
+}
+
+void StaticService::Handle_GetClockSnapshot(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto type{rp.PopEnum<TimeType>()};
+
+ ClockSnapshot snapshot{};
+ auto res = GetClockSnapshot(snapshot, type);
+
+ ctx.WriteBuffer(snapshot);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void StaticService::Handle_GetClockSnapshotFromSystemClockContext(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto clock_type{rp.PopEnum<TimeType>()};
+ [[maybe_unused]] auto alignment{rp.Pop<u32>()};
+ auto user_context{rp.PopRaw<SystemClockContext>()};
+ auto network_context{rp.PopRaw<SystemClockContext>()};
+
+ ClockSnapshot snapshot{};
+ auto res =
+ GetClockSnapshotFromSystemClockContext(snapshot, user_context, network_context, clock_type);
+
+ ctx.WriteBuffer(snapshot);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void StaticService::Handle_CalculateStandardUserSystemClockDifferenceByUser(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ ClockSnapshot a{};
+ ClockSnapshot b{};
+
+ auto a_buffer{ctx.ReadBuffer(0)};
+ auto b_buffer{ctx.ReadBuffer(1)};
+
+ std::memcpy(&a, a_buffer.data(), sizeof(ClockSnapshot));
+ std::memcpy(&b, b_buffer.data(), sizeof(ClockSnapshot));
+
+ s64 difference{};
+ auto res = CalculateStandardUserSystemClockDifferenceByUser(difference, a, b);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push(difference);
+}
+
+void StaticService::Handle_CalculateSpanBetween(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ ClockSnapshot a{};
+ ClockSnapshot b{};
+
+ auto a_buffer{ctx.ReadBuffer(0)};
+ auto b_buffer{ctx.ReadBuffer(1)};
+
+ std::memcpy(&a, a_buffer.data(), sizeof(ClockSnapshot));
+ std::memcpy(&b, b_buffer.data(), sizeof(ClockSnapshot));
+
+ s64 time{};
+ auto res = CalculateSpanBetween(time, a, b);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push(time);
+}
+
+// =============================== Implementations ===========================
+
+Result StaticService::GetStandardUserSystemClock(std::shared_ptr<SystemClock>& out_service) {
+ out_service = std::make_shared<SystemClock>(m_system, m_user_system_clock,
+ m_setup_info.can_write_user_clock,
+ m_setup_info.can_write_uninitialized_clock);
+ R_SUCCEED();
+}
+
+Result StaticService::GetStandardNetworkSystemClock(std::shared_ptr<SystemClock>& out_service) {
+ out_service = std::make_shared<SystemClock>(m_system, m_network_system_clock,
+ m_setup_info.can_write_network_clock,
+ m_setup_info.can_write_uninitialized_clock);
+ R_SUCCEED();
+}
+
+Result StaticService::GetStandardSteadyClock(std::shared_ptr<SteadyClock>& out_service) {
+ out_service =
+ std::make_shared<SteadyClock>(m_system, m_time, m_setup_info.can_write_steady_clock,
+ m_setup_info.can_write_uninitialized_clock);
+ R_SUCCEED();
+}
+
+Result StaticService::GetTimeZoneService(std::shared_ptr<TimeZoneService>& out_service) {
+ out_service =
+ std::make_shared<TimeZoneService>(m_system, m_time->m_standard_steady_clock, m_time_zone,
+ m_setup_info.can_write_timezone_device_location);
+ R_SUCCEED();
+}
+
+Result StaticService::GetStandardLocalSystemClock(std::shared_ptr<SystemClock>& out_service) {
+ out_service = std::make_shared<SystemClock>(m_system, m_local_system_clock,
+ m_setup_info.can_write_local_clock,
+ m_setup_info.can_write_uninitialized_clock);
+ R_SUCCEED();
+}
+
+Result StaticService::GetEphemeralNetworkSystemClock(std::shared_ptr<SystemClock>& out_service) {
+ out_service = std::make_shared<SystemClock>(m_system, m_ephemeral_network_clock,
+ m_setup_info.can_write_network_clock,
+ m_setup_info.can_write_uninitialized_clock);
+ R_SUCCEED();
+}
+
+Result StaticService::GetSharedMemoryNativeHandle(Kernel::KSharedMemory** out_shared_memory) {
+ *out_shared_memory = &m_shared_memory.GetKSharedMemory();
+ R_SUCCEED();
+}
+
+Result StaticService::IsStandardUserSystemClockAutomaticCorrectionEnabled(bool& out_is_enabled) {
+ R_UNLESS(m_user_system_clock.IsInitialized(), ResultClockUninitialized);
+
+ out_is_enabled = m_user_system_clock.GetAutomaticCorrection();
+ R_SUCCEED();
+}
+
+Result StaticService::SetStandardUserSystemClockAutomaticCorrectionEnabled(
+ bool automatic_correction) {
+ R_UNLESS(m_user_system_clock.IsInitialized() && m_time->m_standard_steady_clock.IsInitialized(),
+ ResultClockUninitialized);
+ R_UNLESS(m_setup_info.can_write_user_clock, ResultPermissionDenied);
+
+ R_TRY(m_user_system_clock.SetAutomaticCorrection(automatic_correction));
+
+ m_shared_memory.SetAutomaticCorrection(automatic_correction);
+
+ SteadyClockTimePoint time_point{};
+ R_TRY(m_time->m_standard_steady_clock.GetCurrentTimePoint(time_point));
+
+ m_user_system_clock.SetTimePointAndSignal(time_point);
+ m_user_system_clock.GetEvent().Signal();
+ R_SUCCEED();
+}
+
+Result StaticService::IsStandardNetworkSystemClockAccuracySufficient(bool& out_is_sufficient) {
+ out_is_sufficient = m_network_system_clock.IsAccuracySufficient();
+ R_SUCCEED();
+}
+
+Result StaticService::GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(
+ SteadyClockTimePoint& out_time_point) {
+ R_UNLESS(m_user_system_clock.IsInitialized(), ResultClockUninitialized);
+
+ m_user_system_clock.GetTimePoint(out_time_point);
+
+ R_SUCCEED();
+}
+
+Result StaticService::CalculateMonotonicSystemClockBaseTimePoint(s64& out_time,
+ SystemClockContext& context) {
+ R_UNLESS(m_time->m_standard_steady_clock.IsInitialized(), ResultClockUninitialized);
+
+ SteadyClockTimePoint time_point{};
+ R_TRY(m_time->m_standard_steady_clock.GetCurrentTimePoint(time_point));
+
+ R_UNLESS(time_point.IdMatches(context.steady_time_point), ResultClockMismatch);
+
+ auto one_second_ns{
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(1)).count()};
+ auto ticks{m_system.CoreTiming().GetClockTicks()};
+ auto current_time{ConvertToTimeSpan(ticks).count()};
+ out_time = ((context.offset + time_point.time_point) - (current_time / one_second_ns));
+ R_SUCCEED();
+}
+
+Result StaticService::GetClockSnapshot(ClockSnapshot& out_snapshot, TimeType type) {
+ SystemClockContext user_context{};
+ R_TRY(m_user_system_clock.GetContext(user_context));
+
+ SystemClockContext network_context{};
+ R_TRY(m_network_system_clock.GetContext(network_context));
+
+ R_RETURN(GetClockSnapshotImpl(out_snapshot, user_context, network_context, type));
+}
+
+Result StaticService::GetClockSnapshotFromSystemClockContext(ClockSnapshot& out_snapshot,
+ SystemClockContext& user_context,
+ SystemClockContext& network_context,
+ TimeType type) {
+ R_RETURN(GetClockSnapshotImpl(out_snapshot, user_context, network_context, type));
+}
+
+Result StaticService::CalculateStandardUserSystemClockDifferenceByUser(s64& out_time,
+ ClockSnapshot& a,
+ ClockSnapshot& b) {
+ auto diff_s =
+ std::chrono::seconds(b.user_context.offset) - std::chrono::seconds(a.user_context.offset);
+
+ if (a.user_context == b.user_context ||
+ !a.user_context.steady_time_point.IdMatches(b.user_context.steady_time_point)) {
+ out_time = 0;
+ R_SUCCEED();
+ }
+
+ if (!a.is_automatic_correction_enabled || !b.is_automatic_correction_enabled) {
+ out_time = std::chrono::duration_cast<std::chrono::nanoseconds>(diff_s).count();
+ R_SUCCEED();
+ }
+
+ if (a.network_context.steady_time_point.IdMatches(a.steady_clock_time_point) ||
+ b.network_context.steady_time_point.IdMatches(b.steady_clock_time_point)) {
+ out_time = 0;
+ R_SUCCEED();
+ }
+
+ out_time = std::chrono::duration_cast<std::chrono::nanoseconds>(diff_s).count();
+ R_SUCCEED();
+}
+
+Result StaticService::CalculateSpanBetween(s64& out_time, ClockSnapshot& a, ClockSnapshot& b) {
+ s64 time_s{};
+ auto res =
+ GetSpanBetweenTimePoints(&time_s, a.steady_clock_time_point, b.steady_clock_time_point);
+
+ if (res != ResultSuccess) {
+ R_UNLESS(a.network_time != 0 && b.network_time != 0, ResultTimeNotFound);
+ time_s = b.network_time - a.network_time;
+ }
+
+ out_time =
+ std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::seconds(time_s)).count();
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/static.h b/src/core/hle/service/psc/time/static.h
new file mode 100644
index 000000000..498cd5ab5
--- /dev/null
+++ b/src/core/hle/service/psc/time/static.h
@@ -0,0 +1,95 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Kernel {
+class KSharedMemory;
+}
+
+namespace Service::PSC::Time {
+class TimeManager;
+class StandardLocalSystemClockCore;
+class StandardUserSystemClockCore;
+class StandardNetworkSystemClockCore;
+class TimeZone;
+class SystemClock;
+class SteadyClock;
+class TimeZoneService;
+class EphemeralNetworkSystemClockCore;
+class SharedMemory;
+
+class StaticService final : public ServiceFramework<StaticService> {
+public:
+ explicit StaticService(Core::System& system, StaticServiceSetupInfo setup_info,
+ std::shared_ptr<TimeManager> time, const char* name);
+
+ ~StaticService() override = default;
+
+ Result GetStandardUserSystemClock(std::shared_ptr<SystemClock>& out_service);
+ Result GetStandardNetworkSystemClock(std::shared_ptr<SystemClock>& out_service);
+ Result GetStandardSteadyClock(std::shared_ptr<SteadyClock>& out_service);
+ Result GetTimeZoneService(std::shared_ptr<TimeZoneService>& out_service);
+ Result GetStandardLocalSystemClock(std::shared_ptr<SystemClock>& out_service);
+ Result GetEphemeralNetworkSystemClock(std::shared_ptr<SystemClock>& out_service);
+ Result GetSharedMemoryNativeHandle(Kernel::KSharedMemory** out_shared_memory);
+ Result IsStandardUserSystemClockAutomaticCorrectionEnabled(bool& out_is_enabled);
+ Result SetStandardUserSystemClockAutomaticCorrectionEnabled(bool automatic_correction);
+ Result IsStandardNetworkSystemClockAccuracySufficient(bool& out_is_sufficient);
+ Result GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(
+ SteadyClockTimePoint& out_time_point);
+ Result CalculateMonotonicSystemClockBaseTimePoint(s64& out_time, SystemClockContext& context);
+ Result GetClockSnapshot(ClockSnapshot& out_snapshot, TimeType type);
+ Result GetClockSnapshotFromSystemClockContext(ClockSnapshot& out_snapshot,
+ SystemClockContext& user_context,
+ SystemClockContext& network_context,
+ TimeType type);
+ Result CalculateStandardUserSystemClockDifferenceByUser(s64& out_time, ClockSnapshot& a,
+ ClockSnapshot& b);
+ Result CalculateSpanBetween(s64& out_time, ClockSnapshot& a, ClockSnapshot& b);
+
+private:
+ Result GetClockSnapshotImpl(ClockSnapshot& out_snapshot, SystemClockContext& user_context,
+ SystemClockContext& network_context, TimeType type);
+
+ void Handle_GetStandardUserSystemClock(HLERequestContext& ctx);
+ void Handle_GetStandardNetworkSystemClock(HLERequestContext& ctx);
+ void Handle_GetStandardSteadyClock(HLERequestContext& ctx);
+ void Handle_GetTimeZoneService(HLERequestContext& ctx);
+ void Handle_GetStandardLocalSystemClock(HLERequestContext& ctx);
+ void Handle_GetEphemeralNetworkSystemClock(HLERequestContext& ctx);
+ void Handle_GetSharedMemoryNativeHandle(HLERequestContext& ctx);
+ void Handle_SetStandardSteadyClockInternalOffset(HLERequestContext& ctx);
+ void Handle_GetStandardSteadyClockRtcValue(HLERequestContext& ctx);
+ void Handle_IsStandardUserSystemClockAutomaticCorrectionEnabled(HLERequestContext& ctx);
+ void Handle_SetStandardUserSystemClockAutomaticCorrectionEnabled(HLERequestContext& ctx);
+ void Handle_GetStandardUserSystemClockInitialYear(HLERequestContext& ctx);
+ void Handle_IsStandardNetworkSystemClockAccuracySufficient(HLERequestContext& ctx);
+ void Handle_GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(HLERequestContext& ctx);
+ void Handle_CalculateMonotonicSystemClockBaseTimePoint(HLERequestContext& ctx);
+ void Handle_GetClockSnapshot(HLERequestContext& ctx);
+ void Handle_GetClockSnapshotFromSystemClockContext(HLERequestContext& ctx);
+ void Handle_CalculateStandardUserSystemClockDifferenceByUser(HLERequestContext& ctx);
+ void Handle_CalculateSpanBetween(HLERequestContext& ctx);
+
+ Core::System& m_system;
+ StaticServiceSetupInfo m_setup_info;
+ std::shared_ptr<TimeManager> m_time;
+ StandardLocalSystemClockCore& m_local_system_clock;
+ StandardUserSystemClockCore& m_user_system_clock;
+ StandardNetworkSystemClockCore& m_network_system_clock;
+ TimeZone& m_time_zone;
+ EphemeralNetworkSystemClockCore& m_ephemeral_network_clock;
+ SharedMemory& m_shared_memory;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/steady_clock.cpp b/src/core/hle/service/psc/time/steady_clock.cpp
new file mode 100644
index 000000000..1ed5c7679
--- /dev/null
+++ b/src/core/hle/service/psc/time/steady_clock.cpp
@@ -0,0 +1,164 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/steady_clock.h"
+
+namespace Service::PSC::Time {
+
+SteadyClock::SteadyClock(Core::System& system_, std::shared_ptr<TimeManager> manager,
+ bool can_write_steady_clock, bool can_write_uninitialized_clock)
+ : ServiceFramework{system_, "ISteadyClock"}, m_system{system},
+ m_clock_core{manager->m_standard_steady_clock},
+ m_can_write_steady_clock{can_write_steady_clock}, m_can_write_uninitialized_clock{
+ can_write_uninitialized_clock} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &SteadyClock::Handle_GetCurrentTimePoint, "GetCurrentTimePoint"},
+ {2, &SteadyClock::Handle_GetTestOffset, "GetTestOffset"},
+ {3, &SteadyClock::Handle_SetTestOffset, "SetTestOffset"},
+ {100, &SteadyClock::Handle_GetRtcValue, "GetRtcValue"},
+ {101, &SteadyClock::Handle_IsRtcResetDetected, "IsRtcResetDetected"},
+ {102, &SteadyClock::Handle_GetSetupResultValue, "GetSetupResultValue"},
+ {200, &SteadyClock::Handle_GetInternalOffset, "GetInternalOffset"},
+ };
+ // clang-format on
+ RegisterHandlers(functions);
+}
+
+void SteadyClock::Handle_GetCurrentTimePoint(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ SteadyClockTimePoint time_point{};
+ auto res = GetCurrentTimePoint(time_point);
+
+ IPC::ResponseBuilder rb{ctx, 2 + sizeof(SteadyClockTimePoint) / sizeof(u32)};
+ rb.Push(res);
+ rb.PushRaw<SteadyClockTimePoint>(time_point);
+}
+
+void SteadyClock::Handle_GetTestOffset(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ s64 test_offset{};
+ auto res = GetTestOffset(test_offset);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push(test_offset);
+}
+
+void SteadyClock::Handle_SetTestOffset(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto test_offset{rp.Pop<s64>()};
+
+ auto res = SetTestOffset(test_offset);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void SteadyClock::Handle_GetRtcValue(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ s64 rtc_value{};
+ auto res = GetRtcValue(rtc_value);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push(rtc_value);
+}
+
+void SteadyClock::Handle_IsRtcResetDetected(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ bool reset_detected{false};
+ auto res = IsRtcResetDetected(reset_detected);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push(reset_detected);
+}
+
+void SteadyClock::Handle_GetSetupResultValue(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Result result_value{ResultSuccess};
+ auto res = GetSetupResultValue(result_value);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push(result_value);
+}
+
+void SteadyClock::Handle_GetInternalOffset(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ s64 internal_offset{};
+ auto res = GetInternalOffset(internal_offset);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push(internal_offset);
+}
+
+// =============================== Implementations ===========================
+
+Result SteadyClock::GetCurrentTimePoint(SteadyClockTimePoint& out_time_point) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ R_RETURN(m_clock_core.GetCurrentTimePoint(out_time_point));
+}
+
+Result SteadyClock::GetTestOffset(s64& out_test_offset) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ out_test_offset = m_clock_core.GetTestOffset();
+ R_SUCCEED();
+}
+
+Result SteadyClock::SetTestOffset(s64 test_offset) {
+ R_UNLESS(m_can_write_steady_clock, ResultPermissionDenied);
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ m_clock_core.SetTestOffset(test_offset);
+ R_SUCCEED();
+}
+
+Result SteadyClock::GetRtcValue(s64& out_rtc_value) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ R_RETURN(m_clock_core.GetRtcValue(out_rtc_value));
+}
+
+Result SteadyClock::IsRtcResetDetected(bool& out_is_detected) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ out_is_detected = m_clock_core.IsResetDetected();
+ R_SUCCEED();
+}
+
+Result SteadyClock::GetSetupResultValue(Result& out_result) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ out_result = m_clock_core.GetSetupResultValue();
+ R_SUCCEED();
+}
+
+Result SteadyClock::GetInternalOffset(s64& out_internal_offset) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ out_internal_offset = m_clock_core.GetInternalOffset();
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/steady_clock.h b/src/core/hle/service/psc/time/steady_clock.h
new file mode 100644
index 000000000..115e9b138
--- /dev/null
+++ b/src/core/hle/service/psc/time/steady_clock.h
@@ -0,0 +1,49 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/psc/time/manager.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+
+class SteadyClock final : public ServiceFramework<SteadyClock> {
+public:
+ explicit SteadyClock(Core::System& system, std::shared_ptr<TimeManager> manager,
+ bool can_write_steady_clock, bool can_write_uninitialized_clock);
+
+ ~SteadyClock() override = default;
+
+ Result GetCurrentTimePoint(SteadyClockTimePoint& out_time_point);
+ Result GetTestOffset(s64& out_test_offset);
+ Result SetTestOffset(s64 test_offset);
+ Result GetRtcValue(s64& out_rtc_value);
+ Result IsRtcResetDetected(bool& out_is_detected);
+ Result GetSetupResultValue(Result& out_result);
+ Result GetInternalOffset(s64& out_internal_offset);
+
+private:
+ void Handle_GetCurrentTimePoint(HLERequestContext& ctx);
+ void Handle_GetTestOffset(HLERequestContext& ctx);
+ void Handle_SetTestOffset(HLERequestContext& ctx);
+ void Handle_GetRtcValue(HLERequestContext& ctx);
+ void Handle_IsRtcResetDetected(HLERequestContext& ctx);
+ void Handle_GetSetupResultValue(HLERequestContext& ctx);
+ void Handle_GetInternalOffset(HLERequestContext& ctx);
+
+ Core::System& m_system;
+
+ StandardSteadyClockCore& m_clock_core;
+ bool m_can_write_steady_clock;
+ bool m_can_write_uninitialized_clock;
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/system_clock.cpp b/src/core/hle/service/psc/time/system_clock.cpp
new file mode 100644
index 000000000..13d2f1d11
--- /dev/null
+++ b/src/core/hle/service/psc/time/system_clock.cpp
@@ -0,0 +1,127 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/core.h"
+#include "core/hle/service/psc/time/system_clock.h"
+
+namespace Service::PSC::Time {
+
+SystemClock::SystemClock(Core::System& system_, SystemClockCore& clock_core, bool can_write_clock,
+ bool can_write_uninitialized_clock)
+ : ServiceFramework{system_, "ISystemClock"}, m_system{system}, m_clock_core{clock_core},
+ m_can_write_clock{can_write_clock}, m_can_write_uninitialized_clock{
+ can_write_uninitialized_clock} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &SystemClock::Handle_GetCurrentTime, "GetCurrentTime"},
+ {1, &SystemClock::Handle_SetCurrentTime, "SetCurrentTime"},
+ {2, &SystemClock::Handle_GetSystemClockContext, "GetSystemClockContext"},
+ {3, &SystemClock::Handle_SetSystemClockContext, "SetSystemClockContext"},
+ {4, &SystemClock::Handle_GetOperationEventReadableHandle, "GetOperationEventReadableHandle"},
+ };
+ // clang-format on
+ RegisterHandlers(functions);
+}
+
+void SystemClock::Handle_GetCurrentTime(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ s64 time{};
+ auto res = GetCurrentTime(time);
+
+ IPC::ResponseBuilder rb{ctx, 4};
+ rb.Push(res);
+ rb.Push<s64>(time);
+}
+
+void SystemClock::Handle_SetCurrentTime(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto time{rp.Pop<s64>()};
+
+ auto res = SetCurrentTime(time);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void SystemClock::Handle_GetSystemClockContext(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ SystemClockContext context{};
+ auto res = GetSystemClockContext(context);
+
+ IPC::ResponseBuilder rb{ctx, 2 + sizeof(SystemClockContext) / sizeof(u32)};
+ rb.Push(res);
+ rb.PushRaw<SystemClockContext>(context);
+}
+
+void SystemClock::Handle_SetSystemClockContext(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto context{rp.PopRaw<SystemClockContext>()};
+
+ auto res = SetSystemClockContext(context);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void SystemClock::Handle_GetOperationEventReadableHandle(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ Kernel::KEvent* event{};
+ auto res = GetOperationEventReadableHandle(&event);
+
+ IPC::ResponseBuilder rb{ctx, 2, 1};
+ rb.Push(res);
+ rb.PushCopyObjects(event->GetReadableEvent());
+}
+
+// =============================== Implementations ===========================
+
+Result SystemClock::GetCurrentTime(s64& out_time) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ R_RETURN(m_clock_core.GetCurrentTime(&out_time));
+}
+
+Result SystemClock::SetCurrentTime(s64 time) {
+ R_UNLESS(m_can_write_clock, ResultPermissionDenied);
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ R_RETURN(m_clock_core.SetCurrentTime(time));
+}
+
+Result SystemClock::GetSystemClockContext(SystemClockContext& out_context) {
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ R_RETURN(m_clock_core.GetContext(out_context));
+}
+
+Result SystemClock::SetSystemClockContext(SystemClockContext& context) {
+ R_UNLESS(m_can_write_clock, ResultPermissionDenied);
+ R_UNLESS(m_can_write_uninitialized_clock || m_clock_core.IsInitialized(),
+ ResultClockUninitialized);
+
+ R_RETURN(m_clock_core.SetContextAndWrite(context));
+}
+
+Result SystemClock::GetOperationEventReadableHandle(Kernel::KEvent** out_event) {
+ if (!m_operation_event) {
+ m_operation_event = std::make_unique<OperationEvent>(m_system);
+ R_UNLESS(m_operation_event != nullptr, ResultFailed);
+
+ m_clock_core.LinkOperationEvent(*m_operation_event);
+ }
+
+ *out_event = m_operation_event->m_event;
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/system_clock.h b/src/core/hle/service/psc/time/system_clock.h
new file mode 100644
index 000000000..f30027e7b
--- /dev/null
+++ b/src/core/hle/service/psc/time/system_clock.h
@@ -0,0 +1,46 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/psc/time/manager.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Service::PSC::Time {
+
+class SystemClock final : public ServiceFramework<SystemClock> {
+public:
+ explicit SystemClock(Core::System& system, SystemClockCore& system_clock_core,
+ bool can_write_clock, bool can_write_uninitialized_clock);
+
+ ~SystemClock() override = default;
+
+ Result GetCurrentTime(s64& out_time);
+ Result SetCurrentTime(s64 time);
+ Result GetSystemClockContext(SystemClockContext& out_context);
+ Result SetSystemClockContext(SystemClockContext& context);
+ Result GetOperationEventReadableHandle(Kernel::KEvent** out_event);
+
+private:
+ void Handle_GetCurrentTime(HLERequestContext& ctx);
+ void Handle_SetCurrentTime(HLERequestContext& ctx);
+ void Handle_GetSystemClockContext(HLERequestContext& ctx);
+ void Handle_SetSystemClockContext(HLERequestContext& ctx);
+ void Handle_GetOperationEventReadableHandle(HLERequestContext& ctx);
+
+ Core::System& m_system;
+
+ SystemClockCore& m_clock_core;
+ bool m_can_write_clock;
+ bool m_can_write_uninitialized_clock;
+ std::unique_ptr<OperationEvent> m_operation_event{};
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/time_zone.cpp b/src/core/hle/service/psc/time/time_zone.cpp
new file mode 100644
index 000000000..cfee8f866
--- /dev/null
+++ b/src/core/hle/service/psc/time/time_zone.cpp
@@ -0,0 +1,280 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/service/psc/time/time_zone.h"
+
+namespace Service::PSC::Time {
+namespace {
+constexpr Result ValidateRule(Tz::Rule& rule) {
+ if (rule.typecnt > static_cast<s32>(Tz::TZ_MAX_TYPES) ||
+ rule.timecnt > static_cast<s32>(Tz::TZ_MAX_TIMES) ||
+ rule.charcnt > static_cast<s32>(Tz::TZ_MAX_CHARS)) {
+ R_RETURN(ResultTimeZoneOutOfRange);
+ }
+
+ for (s32 i = 0; i < rule.timecnt; i++) {
+ if (rule.types[i] >= rule.typecnt) {
+ R_RETURN(ResultTimeZoneOutOfRange);
+ }
+ }
+
+ for (s32 i = 0; i < rule.typecnt; i++) {
+ if (rule.ttis[i].tt_desigidx >= static_cast<s32>(rule.chars.size())) {
+ R_RETURN(ResultTimeZoneOutOfRange);
+ }
+ }
+ R_SUCCEED();
+}
+
+constexpr bool GetTimeZoneTime(s64& out_time, Tz::Rule& rule, s64 time, s32 index,
+ s32 index_offset) {
+ s32 found_idx{};
+ s32 expected_index{index + index_offset};
+ s64 time_to_find{time + rule.ttis[rule.types[index]].tt_utoff -
+ rule.ttis[rule.types[expected_index]].tt_utoff};
+
+ if (rule.timecnt > 1 && rule.ats[0] <= time_to_find) {
+ s32 low{1};
+ s32 high{rule.timecnt};
+
+ while (low < high) {
+ auto mid{(low + high) / 2};
+ if (rule.ats[mid] <= time_to_find) {
+ low = mid + 1;
+ } else if (rule.ats[mid] > time_to_find) {
+ high = mid;
+ }
+ }
+ found_idx = low - 1;
+ }
+
+ if (found_idx == expected_index) {
+ out_time = time_to_find;
+ }
+ return found_idx == expected_index;
+}
+} // namespace
+
+void TimeZone::SetTimePoint(SteadyClockTimePoint& time_point) {
+ std::scoped_lock l{m_mutex};
+ m_steady_clock_time_point = time_point;
+}
+
+void TimeZone::SetTotalLocationNameCount(u32 count) {
+ std::scoped_lock l{m_mutex};
+ m_total_location_name_count = count;
+}
+
+void TimeZone::SetRuleVersion(RuleVersion& rule_version) {
+ std::scoped_lock l{m_mutex};
+ m_rule_version = rule_version;
+}
+
+Result TimeZone::GetLocationName(LocationName& out_name) {
+ std::scoped_lock l{m_mutex};
+ R_UNLESS(m_initialized, ResultClockUninitialized);
+ out_name = m_location;
+ R_SUCCEED();
+}
+
+Result TimeZone::GetTotalLocationCount(u32& out_count) {
+ std::scoped_lock l{m_mutex};
+ if (!m_initialized) {
+ return ResultClockUninitialized;
+ }
+
+ out_count = m_total_location_name_count;
+ R_SUCCEED();
+}
+
+Result TimeZone::GetRuleVersion(RuleVersion& out_rule_version) {
+ std::scoped_lock l{m_mutex};
+ if (!m_initialized) {
+ return ResultClockUninitialized;
+ }
+ out_rule_version = m_rule_version;
+ R_SUCCEED();
+}
+
+Result TimeZone::GetTimePoint(SteadyClockTimePoint& out_time_point) {
+ std::scoped_lock l{m_mutex};
+ if (!m_initialized) {
+ return ResultClockUninitialized;
+ }
+ out_time_point = m_steady_clock_time_point;
+ R_SUCCEED();
+}
+
+Result TimeZone::ToCalendarTime(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time,
+ Tz::Rule& rule) {
+ std::scoped_lock l{m_mutex};
+ R_RETURN(ToCalendarTimeImpl(out_calendar_time, out_additional_info, time, rule));
+}
+
+Result TimeZone::ToCalendarTimeWithMyRule(CalendarTime& calendar_time,
+ CalendarAdditionalInfo& calendar_additional, s64 time) {
+ // This is checked outside the mutex. Bug?
+ if (!m_initialized) {
+ return ResultClockUninitialized;
+ }
+
+ std::scoped_lock l{m_mutex};
+ R_RETURN(ToCalendarTimeImpl(calendar_time, calendar_additional, time, m_my_rule));
+}
+
+Result TimeZone::ParseBinary(LocationName& name, std::span<const u8> binary) {
+ std::scoped_lock l{m_mutex};
+
+ Tz::Rule tmp_rule{};
+ R_TRY(ParseBinaryImpl(tmp_rule, binary));
+
+ m_my_rule = tmp_rule;
+ m_location = name;
+
+ R_SUCCEED();
+}
+
+Result TimeZone::ParseBinaryInto(Tz::Rule& out_rule, std::span<const u8> binary) {
+ std::scoped_lock l{m_mutex};
+ R_RETURN(ParseBinaryImpl(out_rule, binary));
+}
+
+Result TimeZone::ToPosixTime(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar, Tz::Rule& rule) {
+ std::scoped_lock l{m_mutex};
+
+ auto res = ToPosixTimeImpl(out_count, out_times, out_times_count, calendar, rule, -1);
+
+ if (res != ResultSuccess) {
+ if (res == ResultTimeZoneNotFound) {
+ res = ResultSuccess;
+ out_count = 0;
+ }
+ } else if (out_count == 2 && out_times[0] > out_times[1]) {
+ std::swap(out_times[0], out_times[1]);
+ }
+ R_RETURN(res);
+}
+
+Result TimeZone::ToPosixTimeWithMyRule(u32& out_count, std::span<s64, 2> out_times,
+ u32 out_times_count, CalendarTime& calendar) {
+ std::scoped_lock l{m_mutex};
+
+ auto res = ToPosixTimeImpl(out_count, out_times, out_times_count, calendar, m_my_rule, -1);
+
+ if (res != ResultSuccess) {
+ if (res == ResultTimeZoneNotFound) {
+ res = ResultSuccess;
+ out_count = 0;
+ }
+ } else if (out_count == 2 && out_times[0] > out_times[1]) {
+ std::swap(out_times[0], out_times[1]);
+ }
+ R_RETURN(res);
+}
+
+Result TimeZone::ParseBinaryImpl(Tz::Rule& out_rule, std::span<const u8> binary) {
+ if (Tz::ParseTimeZoneBinary(out_rule, binary)) {
+ R_RETURN(ResultTimeZoneParseFailed);
+ }
+ R_SUCCEED();
+}
+
+Result TimeZone::ToCalendarTimeImpl(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time,
+ Tz::Rule& rule) {
+ R_TRY(ValidateRule(rule));
+
+ Tz::CalendarTimeInternal calendar_internal{};
+ time_t time_tmp{static_cast<time_t>(time)};
+ if (Tz::localtime_rz(&calendar_internal, &rule, &time_tmp)) {
+ R_RETURN(ResultOverflow);
+ }
+
+ out_calendar_time.year = static_cast<s16>(calendar_internal.tm_year + 1900);
+ out_calendar_time.month = static_cast<s8>(calendar_internal.tm_mon + 1);
+ out_calendar_time.day = static_cast<s8>(calendar_internal.tm_mday);
+ out_calendar_time.hour = static_cast<s8>(calendar_internal.tm_hour);
+ out_calendar_time.minute = static_cast<s8>(calendar_internal.tm_min);
+ out_calendar_time.second = static_cast<s8>(calendar_internal.tm_sec);
+
+ out_additional_info.day_of_week = calendar_internal.tm_wday;
+ out_additional_info.day_of_year = calendar_internal.tm_yday;
+
+ std::memcpy(out_additional_info.name.data(), calendar_internal.tm_zone.data(),
+ out_additional_info.name.size());
+ out_additional_info.name[out_additional_info.name.size() - 1] = '\0';
+
+ out_additional_info.is_dst = calendar_internal.tm_isdst;
+ out_additional_info.ut_offset = calendar_internal.tm_utoff;
+
+ R_SUCCEED();
+}
+
+Result TimeZone::ToPosixTimeImpl(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar, Tz::Rule& rule, s32 is_dst) {
+ R_TRY(ValidateRule(rule));
+
+ calendar.month -= 1;
+ calendar.year -= 1900;
+
+ Tz::CalendarTimeInternal internal{
+ .tm_sec = calendar.second,
+ .tm_min = calendar.minute,
+ .tm_hour = calendar.hour,
+ .tm_mday = calendar.day,
+ .tm_mon = calendar.month,
+ .tm_year = calendar.year,
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = is_dst,
+ .tm_zone = {},
+ .tm_utoff = 0,
+ .time_index = 0,
+ };
+ time_t time_tmp{};
+ auto res = Tz::mktime_tzname(&time_tmp, &rule, &internal);
+ s64 time = static_cast<s64>(time_tmp);
+
+ if (res == 1) {
+ R_RETURN(ResultOverflow);
+ } else if (res == 2) {
+ R_RETURN(ResultTimeZoneNotFound);
+ }
+
+ if (internal.tm_sec != calendar.second || internal.tm_min != calendar.minute ||
+ internal.tm_hour != calendar.hour || internal.tm_mday != calendar.day ||
+ internal.tm_mon != calendar.month || internal.tm_year != calendar.year) {
+ R_RETURN(ResultTimeZoneNotFound);
+ }
+
+ if (res != 0) {
+ ASSERT(false);
+ }
+
+ out_times[0] = time;
+ if (out_times_count < 2) {
+ out_count = 1;
+ R_SUCCEED();
+ }
+
+ s64 time2{};
+ if (internal.time_index > 0 && GetTimeZoneTime(time2, rule, time, internal.time_index, -1)) {
+ out_times[1] = time2;
+ out_count = 2;
+ R_SUCCEED();
+ }
+
+ if (((internal.time_index + 1) < rule.timecnt) &&
+ GetTimeZoneTime(time2, rule, time, internal.time_index, 1)) {
+ out_times[1] = time2;
+ out_count = 2;
+ R_SUCCEED();
+ }
+
+ out_count = 1;
+ R_SUCCEED();
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/time_zone.h b/src/core/hle/service/psc/time/time_zone.h
new file mode 100644
index 000000000..ce2acca17
--- /dev/null
+++ b/src/core/hle/service/psc/time/time_zone.h
@@ -0,0 +1,62 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <mutex>
+#include <span>
+
+#include <tz/tz.h>
+#include "core/hle/service/psc/time/common.h"
+
+namespace Service::PSC::Time {
+
+class TimeZone {
+public:
+ TimeZone() = default;
+
+ bool IsInitialized() const {
+ return m_initialized;
+ }
+
+ void SetInitialized() {
+ m_initialized = true;
+ }
+
+ void SetTimePoint(SteadyClockTimePoint& time_point);
+ void SetTotalLocationNameCount(u32 count);
+ void SetRuleVersion(RuleVersion& rule_version);
+ Result GetLocationName(LocationName& out_name);
+ Result GetTotalLocationCount(u32& out_count);
+ Result GetRuleVersion(RuleVersion& out_rule_version);
+ Result GetTimePoint(SteadyClockTimePoint& out_time_point);
+
+ Result ToCalendarTime(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time, Tz::Rule& rule);
+ Result ToCalendarTimeWithMyRule(CalendarTime& calendar_time,
+ CalendarAdditionalInfo& calendar_additional, s64 time);
+ Result ParseBinary(LocationName& name, std::span<const u8> binary);
+ Result ParseBinaryInto(Tz::Rule& out_rule, std::span<const u8> binary);
+ Result ToPosixTime(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar, Tz::Rule& rule);
+ Result ToPosixTimeWithMyRule(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar);
+
+private:
+ Result ParseBinaryImpl(Tz::Rule& out_rule, std::span<const u8> binary);
+ Result ToCalendarTimeImpl(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time,
+ Tz::Rule& rule);
+ Result ToPosixTimeImpl(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar, Tz::Rule& rule, s32 is_dst);
+
+ bool m_initialized{};
+ std::recursive_mutex m_mutex;
+ LocationName m_location{};
+ Tz::Rule m_my_rule{};
+ SteadyClockTimePoint m_steady_clock_time_point{};
+ u32 m_total_location_name_count{};
+ RuleVersion m_rule_version{};
+};
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/time_zone_service.cpp b/src/core/hle/service/psc/time/time_zone_service.cpp
new file mode 100644
index 000000000..e304c8387
--- /dev/null
+++ b/src/core/hle/service/psc/time/time_zone_service.cpp
@@ -0,0 +1,289 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <tz/tz.h>
+#include "core/core.h"
+#include "core/hle/service/psc/time/time_zone_service.h"
+
+namespace Service::PSC::Time {
+
+TimeZoneService::TimeZoneService(Core::System& system_, StandardSteadyClockCore& clock_core,
+ TimeZone& time_zone, bool can_write_timezone_device_location)
+ : ServiceFramework{system_, "ITimeZoneService"}, m_system{system}, m_clock_core{clock_core},
+ m_time_zone{time_zone}, m_can_write_timezone_device_location{
+ can_write_timezone_device_location} {
+ // clang-format off
+ static const FunctionInfo functions[] = {
+ {0, &TimeZoneService::Handle_GetDeviceLocationName, "GetDeviceLocationName"},
+ {1, &TimeZoneService::Handle_SetDeviceLocationName, "SetDeviceLocationName"},
+ {2, &TimeZoneService::Handle_GetTotalLocationNameCount, "GetTotalLocationNameCount"},
+ {3, &TimeZoneService::Handle_LoadLocationNameList, "LoadLocationNameList"},
+ {4, &TimeZoneService::Handle_LoadTimeZoneRule, "LoadTimeZoneRule"},
+ {5, &TimeZoneService::Handle_GetTimeZoneRuleVersion, "GetTimeZoneRuleVersion"},
+ {6, &TimeZoneService::Handle_GetDeviceLocationNameAndUpdatedTime, "GetDeviceLocationNameAndUpdatedTime"},
+ {7, &TimeZoneService::Handle_SetDeviceLocationNameWithTimeZoneRule, "SetDeviceLocationNameWithTimeZoneRule"},
+ {8, &TimeZoneService::Handle_ParseTimeZoneBinary, "ParseTimeZoneBinary"},
+ {20, &TimeZoneService::Handle_GetDeviceLocationNameOperationEventReadableHandle, "GetDeviceLocationNameOperationEventReadableHandle"},
+ {100, &TimeZoneService::Handle_ToCalendarTime, "ToCalendarTime"},
+ {101, &TimeZoneService::Handle_ToCalendarTimeWithMyRule, "ToCalendarTimeWithMyRule"},
+ {201, &TimeZoneService::Handle_ToPosixTime, "ToPosixTime"},
+ {202, &TimeZoneService::Handle_ToPosixTimeWithMyRule, "ToPosixTimeWithMyRule"},
+ };
+ // clang-format on
+ RegisterHandlers(functions);
+}
+
+void TimeZoneService::Handle_GetDeviceLocationName(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ LocationName name{};
+ auto res = GetDeviceLocationName(name);
+
+ IPC::ResponseBuilder rb{ctx, 2 + sizeof(LocationName) / sizeof(u32)};
+ rb.Push(res);
+ rb.PushRaw<LocationName>(name);
+}
+
+void TimeZoneService::Handle_SetDeviceLocationName(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ [[maybe_unused]] auto name{rp.PopRaw<LocationName>()};
+
+ if (!m_can_write_timezone_device_location) {
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultPermissionDenied);
+ return;
+ }
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultNotImplemented);
+}
+
+void TimeZoneService::Handle_GetTotalLocationNameCount(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ u32 count{};
+ auto res = GetTotalLocationNameCount(count);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push(count);
+}
+
+void TimeZoneService::Handle_LoadLocationNameList(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultNotImplemented);
+}
+
+void TimeZoneService::Handle_LoadTimeZoneRule(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultNotImplemented);
+}
+
+void TimeZoneService::Handle_GetTimeZoneRuleVersion(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ RuleVersion rule_version{};
+ auto res = GetTimeZoneRuleVersion(rule_version);
+
+ IPC::ResponseBuilder rb{ctx, 2 + sizeof(RuleVersion) / sizeof(u32)};
+ rb.Push(res);
+ rb.PushRaw<RuleVersion>(rule_version);
+}
+
+void TimeZoneService::Handle_GetDeviceLocationNameAndUpdatedTime(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ LocationName name{};
+ SteadyClockTimePoint time_point{};
+ auto res = GetDeviceLocationNameAndUpdatedTime(time_point, name);
+
+ IPC::ResponseBuilder rb{ctx, 2 + (sizeof(LocationName) / sizeof(u32)) +
+ (sizeof(SteadyClockTimePoint) / sizeof(u32))};
+ rb.Push(res);
+ rb.PushRaw<LocationName>(name);
+ rb.PushRaw<SteadyClockTimePoint>(time_point);
+}
+
+void TimeZoneService::Handle_SetDeviceLocationNameWithTimeZoneRule(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto name{rp.PopRaw<LocationName>()};
+
+ auto binary{ctx.ReadBuffer()};
+ auto res = SetDeviceLocationNameWithTimeZoneRule(name, binary);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void TimeZoneService::Handle_ParseTimeZoneBinary(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ auto binary{ctx.ReadBuffer()};
+
+ Tz::Rule rule{};
+ auto res = ParseTimeZoneBinary(rule, binary);
+
+ ctx.WriteBuffer(rule);
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(res);
+}
+
+void TimeZoneService::Handle_GetDeviceLocationNameOperationEventReadableHandle(
+ HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::ResponseBuilder rb{ctx, 2};
+ rb.Push(ResultNotImplemented);
+}
+
+void TimeZoneService::Handle_ToCalendarTime(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto time{rp.Pop<s64>()};
+
+ auto rule_buffer{ctx.ReadBuffer()};
+ Tz::Rule rule{};
+ std::memcpy(&rule, rule_buffer.data(), sizeof(Tz::Rule));
+
+ CalendarTime calendar_time{};
+ CalendarAdditionalInfo additional_info{};
+ auto res = ToCalendarTime(calendar_time, additional_info, time, rule);
+
+ IPC::ResponseBuilder rb{ctx, 2 + (sizeof(CalendarTime) / sizeof(u32)) +
+ (sizeof(CalendarAdditionalInfo) / sizeof(u32))};
+ rb.Push(res);
+ rb.PushRaw<CalendarTime>(calendar_time);
+ rb.PushRaw<CalendarAdditionalInfo>(additional_info);
+}
+
+void TimeZoneService::Handle_ToCalendarTimeWithMyRule(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto time{rp.Pop<s64>()};
+
+ CalendarTime calendar_time{};
+ CalendarAdditionalInfo additional_info{};
+ auto res = ToCalendarTimeWithMyRule(calendar_time, additional_info, time);
+
+ IPC::ResponseBuilder rb{ctx, 2 + (sizeof(CalendarTime) / sizeof(u32)) +
+ (sizeof(CalendarAdditionalInfo) / sizeof(u32))};
+ rb.Push(res);
+ rb.PushRaw<CalendarTime>(calendar_time);
+ rb.PushRaw<CalendarAdditionalInfo>(additional_info);
+}
+
+void TimeZoneService::Handle_ToPosixTime(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto calendar{rp.PopRaw<CalendarTime>()};
+
+ auto binary{ctx.ReadBuffer()};
+
+ Tz::Rule rule{};
+ std::memcpy(&rule, binary.data(), sizeof(Tz::Rule));
+
+ u32 count{};
+ std::array<s64, 2> times{};
+ u32 times_count{static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(s64))};
+
+ auto res = ToPosixTime(count, times, times_count, calendar, rule);
+
+ ctx.WriteBuffer(times);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push(count);
+}
+
+void TimeZoneService::Handle_ToPosixTimeWithMyRule(HLERequestContext& ctx) {
+ LOG_DEBUG(Service_Time, "called.");
+
+ IPC::RequestParser rp{ctx};
+ auto calendar{rp.PopRaw<CalendarTime>()};
+
+ u32 count{};
+ std::array<s64, 2> times{};
+ u32 times_count{static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(s64))};
+
+ auto res = ToPosixTimeWithMyRule(count, times, times_count, calendar);
+
+ ctx.WriteBuffer(times);
+
+ IPC::ResponseBuilder rb{ctx, 3};
+ rb.Push(res);
+ rb.Push(count);
+}
+
+// =============================== Implementations ===========================
+
+Result TimeZoneService::GetDeviceLocationName(LocationName& out_location_name) {
+ R_RETURN(m_time_zone.GetLocationName(out_location_name));
+}
+
+Result TimeZoneService::GetTotalLocationNameCount(u32& out_count) {
+ R_RETURN(m_time_zone.GetTotalLocationCount(out_count));
+}
+
+Result TimeZoneService::GetTimeZoneRuleVersion(RuleVersion& out_rule_version) {
+ R_RETURN(m_time_zone.GetRuleVersion(out_rule_version));
+}
+
+Result TimeZoneService::GetDeviceLocationNameAndUpdatedTime(SteadyClockTimePoint& out_time_point,
+ LocationName& location_name) {
+ R_TRY(m_time_zone.GetLocationName(location_name));
+ R_RETURN(m_time_zone.GetTimePoint(out_time_point));
+}
+
+Result TimeZoneService::SetDeviceLocationNameWithTimeZoneRule(LocationName& location_name,
+ std::span<const u8> binary) {
+ R_UNLESS(m_can_write_timezone_device_location, ResultPermissionDenied);
+ R_TRY(m_time_zone.ParseBinary(location_name, binary));
+
+ SteadyClockTimePoint time_point{};
+ R_TRY(m_clock_core.GetCurrentTimePoint(time_point));
+
+ m_time_zone.SetTimePoint(time_point);
+ R_SUCCEED();
+}
+
+Result TimeZoneService::ParseTimeZoneBinary(Tz::Rule& out_rule, std::span<const u8> binary) {
+ R_RETURN(m_time_zone.ParseBinaryInto(out_rule, binary));
+}
+
+Result TimeZoneService::ToCalendarTime(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time,
+ Tz::Rule& rule) {
+ R_RETURN(m_time_zone.ToCalendarTime(out_calendar_time, out_additional_info, time, rule));
+}
+
+Result TimeZoneService::ToCalendarTimeWithMyRule(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info,
+ s64 time) {
+ R_RETURN(m_time_zone.ToCalendarTimeWithMyRule(out_calendar_time, out_additional_info, time));
+}
+
+Result TimeZoneService::ToPosixTime(u32& out_count, std::span<s64, 2> out_times,
+ u32 out_times_count, CalendarTime& calendar_time,
+ Tz::Rule& rule) {
+ R_RETURN(m_time_zone.ToPosixTime(out_count, out_times, out_times_count, calendar_time, rule));
+}
+
+Result TimeZoneService::ToPosixTimeWithMyRule(u32& out_count, std::span<s64, 2> out_times,
+ u32 out_times_count, CalendarTime& calendar_time) {
+ R_RETURN(
+ m_time_zone.ToPosixTimeWithMyRule(out_count, out_times, out_times_count, calendar_time));
+}
+
+} // namespace Service::PSC::Time
diff --git a/src/core/hle/service/psc/time/time_zone_service.h b/src/core/hle/service/psc/time/time_zone_service.h
new file mode 100644
index 000000000..074c1d4ae
--- /dev/null
+++ b/src/core/hle/service/psc/time/time_zone_service.h
@@ -0,0 +1,69 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/ipc_helpers.h"
+#include "core/hle/service/psc/time/common.h"
+#include "core/hle/service/psc/time/manager.h"
+#include "core/hle/service/server_manager.h"
+#include "core/hle/service/service.h"
+
+namespace Core {
+class System;
+}
+
+namespace Tz {
+struct Rule;
+}
+
+namespace Service::PSC::Time {
+
+class TimeZoneService final : public ServiceFramework<TimeZoneService> {
+public:
+ explicit TimeZoneService(Core::System& system, StandardSteadyClockCore& clock_core,
+ TimeZone& time_zone, bool can_write_timezone_device_location);
+
+ ~TimeZoneService() override = default;
+
+ Result GetDeviceLocationName(LocationName& out_location_name);
+ Result GetTotalLocationNameCount(u32& out_count);
+ Result GetTimeZoneRuleVersion(RuleVersion& out_rule_version);
+ Result GetDeviceLocationNameAndUpdatedTime(SteadyClockTimePoint& out_time_point,
+ LocationName& location_name);
+ Result SetDeviceLocationNameWithTimeZoneRule(LocationName& location_name,
+ std::span<const u8> binary);
+ Result ParseTimeZoneBinary(Tz::Rule& out_rule, std::span<const u8> binary);
+ Result ToCalendarTime(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time, Tz::Rule& rule);
+ Result ToCalendarTimeWithMyRule(CalendarTime& out_calendar_time,
+ CalendarAdditionalInfo& out_additional_info, s64 time);
+ Result ToPosixTime(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar_time, Tz::Rule& rule);
+ Result ToPosixTimeWithMyRule(u32& out_count, std::span<s64, 2> out_times, u32 out_times_count,
+ CalendarTime& calendar_time);
+
+private:
+ void Handle_GetDeviceLocationName(HLERequestContext& ctx);
+ void Handle_SetDeviceLocationName(HLERequestContext& ctx);
+ void Handle_GetTotalLocationNameCount(HLERequestContext& ctx);
+ void Handle_LoadLocationNameList(HLERequestContext& ctx);
+ void Handle_LoadTimeZoneRule(HLERequestContext& ctx);
+ void Handle_GetTimeZoneRuleVersion(HLERequestContext& ctx);
+ void Handle_GetDeviceLocationNameAndUpdatedTime(HLERequestContext& ctx);
+ void Handle_SetDeviceLocationNameWithTimeZoneRule(HLERequestContext& ctx);
+ void Handle_ParseTimeZoneBinary(HLERequestContext& ctx);
+ void Handle_GetDeviceLocationNameOperationEventReadableHandle(HLERequestContext& ctx);
+ void Handle_ToCalendarTime(HLERequestContext& ctx);
+ void Handle_ToCalendarTimeWithMyRule(HLERequestContext& ctx);
+ void Handle_ToPosixTime(HLERequestContext& ctx);
+ void Handle_ToPosixTimeWithMyRule(HLERequestContext& ctx);
+
+ Core::System& m_system;
+
+ StandardSteadyClockCore& m_clock_core;
+ TimeZone& m_time_zone;
+ bool m_can_write_timezone_device_location;
+};
+
+} // namespace Service::PSC::Time