time_manager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <chrono>
  4. #include <ctime>
  5. #include "common/settings.h"
  6. #include "common/time_zone.h"
  7. #include "core/hle/service/time/ephemeral_network_system_clock_context_writer.h"
  8. #include "core/hle/service/time/ephemeral_network_system_clock_core.h"
  9. #include "core/hle/service/time/local_system_clock_context_writer.h"
  10. #include "core/hle/service/time/network_system_clock_context_writer.h"
  11. #include "core/hle/service/time/tick_based_steady_clock_core.h"
  12. #include "core/hle/service/time/time_manager.h"
  13. namespace Service::Time {
  14. namespace {
  15. constexpr Clock::TimeSpanType standard_network_clock_accuracy{0x0009356907420000ULL};
  16. s64 GetSecondsSinceEpoch() {
  17. const auto time_since_epoch = std::chrono::system_clock::now().time_since_epoch();
  18. return std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch).count() +
  19. Settings::values.custom_rtc_differential;
  20. }
  21. } // Anonymous namespace
  22. struct TimeManager::Impl final {
  23. explicit Impl(Core::System& system)
  24. : shared_memory{system}, standard_local_system_clock_core{standard_steady_clock_core},
  25. standard_network_system_clock_core{standard_steady_clock_core},
  26. standard_user_system_clock_core{standard_local_system_clock_core,
  27. standard_network_system_clock_core, system},
  28. ephemeral_network_system_clock_core{tick_based_steady_clock_core},
  29. local_system_clock_context_writer{
  30. std::make_shared<Clock::LocalSystemClockContextWriter>(shared_memory)},
  31. network_system_clock_context_writer{
  32. std::make_shared<Clock::NetworkSystemClockContextWriter>(shared_memory)},
  33. ephemeral_network_system_clock_context_writer{
  34. std::make_shared<Clock::EphemeralNetworkSystemClockContextWriter>()},
  35. time_zone_content_manager{system} {
  36. const auto system_time{Clock::TimeSpanType::FromSeconds(GetSecondsSinceEpoch())};
  37. SetupStandardSteadyClock(system, Common::UUID::MakeRandom(), system_time, {}, {});
  38. SetupStandardLocalSystemClock(system, {}, system_time.ToSeconds());
  39. Clock::SystemClockContext clock_context{};
  40. standard_local_system_clock_core.GetClockContext(system, clock_context);
  41. SetupStandardNetworkSystemClock(clock_context, standard_network_clock_accuracy);
  42. SetupStandardUserSystemClock(system, {}, Clock::SteadyClockTimePoint::GetRandom());
  43. SetupEphemeralNetworkSystemClock();
  44. }
  45. ~Impl() = default;
  46. Clock::StandardSteadyClockCore& GetStandardSteadyClockCore() {
  47. return standard_steady_clock_core;
  48. }
  49. const Clock::StandardSteadyClockCore& GetStandardSteadyClockCore() const {
  50. return standard_steady_clock_core;
  51. }
  52. Clock::StandardLocalSystemClockCore& GetStandardLocalSystemClockCore() {
  53. return standard_local_system_clock_core;
  54. }
  55. const Clock::StandardLocalSystemClockCore& GetStandardLocalSystemClockCore() const {
  56. return standard_local_system_clock_core;
  57. }
  58. Clock::StandardNetworkSystemClockCore& GetStandardNetworkSystemClockCore() {
  59. return standard_network_system_clock_core;
  60. }
  61. const Clock::StandardNetworkSystemClockCore& GetStandardNetworkSystemClockCore() const {
  62. return standard_network_system_clock_core;
  63. }
  64. Clock::StandardUserSystemClockCore& GetStandardUserSystemClockCore() {
  65. return standard_user_system_clock_core;
  66. }
  67. const Clock::StandardUserSystemClockCore& GetStandardUserSystemClockCore() const {
  68. return standard_user_system_clock_core;
  69. }
  70. TimeZone::TimeZoneContentManager& GetTimeZoneContentManager() {
  71. return time_zone_content_manager;
  72. }
  73. const TimeZone::TimeZoneContentManager& GetTimeZoneContentManager() const {
  74. return time_zone_content_manager;
  75. }
  76. SharedMemory& GetSharedMemory() {
  77. return shared_memory;
  78. }
  79. const SharedMemory& GetSharedMemory() const {
  80. return shared_memory;
  81. }
  82. void SetupTimeZoneManager(std::string location_name,
  83. Clock::SteadyClockTimePoint time_zone_updated_time_point,
  84. std::vector<std::string> location_names, u128 time_zone_rule_version,
  85. FileSys::VirtualFile& vfs_file) {
  86. if (time_zone_content_manager.GetTimeZoneManager().SetDeviceLocationNameWithTimeZoneRule(
  87. location_name, vfs_file) != ResultSuccess) {
  88. ASSERT(false);
  89. return;
  90. }
  91. time_zone_content_manager.GetTimeZoneManager().SetUpdatedTime(time_zone_updated_time_point);
  92. time_zone_content_manager.GetTimeZoneManager().SetTotalLocationNameCount(
  93. location_names.size());
  94. time_zone_content_manager.GetTimeZoneManager().SetLocationNames(location_names);
  95. time_zone_content_manager.GetTimeZoneManager().SetTimeZoneRuleVersion(
  96. time_zone_rule_version);
  97. time_zone_content_manager.GetTimeZoneManager().MarkAsInitialized();
  98. }
  99. void SetupStandardSteadyClock(Core::System& system_, Common::UUID clock_source_id,
  100. Clock::TimeSpanType setup_value,
  101. Clock::TimeSpanType internal_offset, bool is_rtc_reset_detected) {
  102. standard_steady_clock_core.SetClockSourceId(clock_source_id);
  103. standard_steady_clock_core.SetSetupValue(setup_value);
  104. standard_steady_clock_core.SetInternalOffset(internal_offset);
  105. standard_steady_clock_core.MarkAsInitialized();
  106. const auto current_time_point{standard_steady_clock_core.GetCurrentRawTimePoint(system_)};
  107. shared_memory.SetupStandardSteadyClock(clock_source_id, current_time_point);
  108. }
  109. void SetupStandardLocalSystemClock(Core::System& system_,
  110. Clock::SystemClockContext clock_context, s64 posix_time) {
  111. standard_local_system_clock_core.SetUpdateCallbackInstance(
  112. local_system_clock_context_writer);
  113. const auto current_time_point{
  114. standard_local_system_clock_core.GetSteadyClockCore().GetCurrentTimePoint(system_)};
  115. if (current_time_point.clock_source_id == clock_context.steady_time_point.clock_source_id) {
  116. standard_local_system_clock_core.SetSystemClockContext(clock_context);
  117. } else {
  118. if (standard_local_system_clock_core.SetCurrentTime(system_, posix_time) !=
  119. ResultSuccess) {
  120. ASSERT(false);
  121. return;
  122. }
  123. }
  124. standard_local_system_clock_core.MarkAsInitialized();
  125. }
  126. void SetupStandardNetworkSystemClock(Clock::SystemClockContext clock_context,
  127. Clock::TimeSpanType sufficient_accuracy) {
  128. standard_network_system_clock_core.SetUpdateCallbackInstance(
  129. network_system_clock_context_writer);
  130. if (standard_network_system_clock_core.SetSystemClockContext(clock_context) !=
  131. ResultSuccess) {
  132. ASSERT(false);
  133. return;
  134. }
  135. standard_network_system_clock_core.SetStandardNetworkClockSufficientAccuracy(
  136. sufficient_accuracy);
  137. standard_network_system_clock_core.MarkAsInitialized();
  138. }
  139. void SetupStandardUserSystemClock(Core::System& system_, bool is_automatic_correction_enabled,
  140. Clock::SteadyClockTimePoint steady_clock_time_point) {
  141. if (standard_user_system_clock_core.SetAutomaticCorrectionEnabled(
  142. system_, is_automatic_correction_enabled) != ResultSuccess) {
  143. ASSERT(false);
  144. return;
  145. }
  146. standard_user_system_clock_core.SetAutomaticCorrectionUpdatedTime(steady_clock_time_point);
  147. standard_user_system_clock_core.MarkAsInitialized();
  148. shared_memory.SetAutomaticCorrectionEnabled(is_automatic_correction_enabled);
  149. }
  150. void SetupEphemeralNetworkSystemClock() {
  151. ephemeral_network_system_clock_core.SetUpdateCallbackInstance(
  152. ephemeral_network_system_clock_context_writer);
  153. ephemeral_network_system_clock_core.MarkAsInitialized();
  154. }
  155. void UpdateLocalSystemClockTime(Core::System& system_, s64 posix_time) {
  156. const auto timespan{Clock::TimeSpanType::FromSeconds(posix_time)};
  157. if (GetStandardLocalSystemClockCore()
  158. .SetCurrentTime(system_, timespan.ToSeconds())
  159. .IsError()) {
  160. ASSERT(false);
  161. return;
  162. }
  163. }
  164. SharedMemory shared_memory;
  165. Clock::StandardSteadyClockCore standard_steady_clock_core;
  166. Clock::TickBasedSteadyClockCore tick_based_steady_clock_core;
  167. Clock::StandardLocalSystemClockCore standard_local_system_clock_core;
  168. Clock::StandardNetworkSystemClockCore standard_network_system_clock_core;
  169. Clock::StandardUserSystemClockCore standard_user_system_clock_core;
  170. Clock::EphemeralNetworkSystemClockCore ephemeral_network_system_clock_core;
  171. std::shared_ptr<Clock::LocalSystemClockContextWriter> local_system_clock_context_writer;
  172. std::shared_ptr<Clock::NetworkSystemClockContextWriter> network_system_clock_context_writer;
  173. std::shared_ptr<Clock::EphemeralNetworkSystemClockContextWriter>
  174. ephemeral_network_system_clock_context_writer;
  175. TimeZone::TimeZoneContentManager time_zone_content_manager;
  176. };
  177. TimeManager::TimeManager(Core::System& system_) : system{system_} {}
  178. TimeManager::~TimeManager() = default;
  179. void TimeManager::Initialize() {
  180. impl = std::make_unique<Impl>(system);
  181. // Time zones can only be initialized after impl is valid
  182. impl->time_zone_content_manager.Initialize(*this);
  183. }
  184. Clock::StandardSteadyClockCore& TimeManager::GetStandardSteadyClockCore() {
  185. return impl->standard_steady_clock_core;
  186. }
  187. const Clock::StandardSteadyClockCore& TimeManager::GetStandardSteadyClockCore() const {
  188. return impl->standard_steady_clock_core;
  189. }
  190. Clock::StandardLocalSystemClockCore& TimeManager::GetStandardLocalSystemClockCore() {
  191. return impl->standard_local_system_clock_core;
  192. }
  193. const Clock::StandardLocalSystemClockCore& TimeManager::GetStandardLocalSystemClockCore() const {
  194. return impl->standard_local_system_clock_core;
  195. }
  196. Clock::StandardNetworkSystemClockCore& TimeManager::GetStandardNetworkSystemClockCore() {
  197. return impl->standard_network_system_clock_core;
  198. }
  199. const Clock::StandardNetworkSystemClockCore& TimeManager::GetStandardNetworkSystemClockCore()
  200. const {
  201. return impl->standard_network_system_clock_core;
  202. }
  203. Clock::StandardUserSystemClockCore& TimeManager::GetStandardUserSystemClockCore() {
  204. return impl->standard_user_system_clock_core;
  205. }
  206. const Clock::StandardUserSystemClockCore& TimeManager::GetStandardUserSystemClockCore() const {
  207. return impl->standard_user_system_clock_core;
  208. }
  209. TimeZone::TimeZoneContentManager& TimeManager::GetTimeZoneContentManager() {
  210. return impl->time_zone_content_manager;
  211. }
  212. const TimeZone::TimeZoneContentManager& TimeManager::GetTimeZoneContentManager() const {
  213. return impl->time_zone_content_manager;
  214. }
  215. SharedMemory& TimeManager::GetSharedMemory() {
  216. return impl->shared_memory;
  217. }
  218. const SharedMemory& TimeManager::GetSharedMemory() const {
  219. return impl->shared_memory;
  220. }
  221. void TimeManager::Shutdown() {
  222. impl.reset();
  223. }
  224. void TimeManager::UpdateLocalSystemClockTime(s64 posix_time) {
  225. impl->UpdateLocalSystemClockTime(system, posix_time);
  226. }
  227. void TimeManager::SetupTimeZoneManager(std::string location_name,
  228. Clock::SteadyClockTimePoint time_zone_updated_time_point,
  229. std::vector<std::string> location_names,
  230. u128 time_zone_rule_version,
  231. FileSys::VirtualFile& vfs_file) {
  232. impl->SetupTimeZoneManager(location_name, time_zone_updated_time_point, location_names,
  233. time_zone_rule_version, vfs_file);
  234. }
  235. } // namespace Service::Time