time_manager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <ctime>
  6. #include "common/settings.h"
  7. #include "common/time_zone.h"
  8. #include "core/hle/service/time/ephemeral_network_system_clock_context_writer.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/time_manager.h"
  12. namespace Service::Time {
  13. constexpr Clock::TimeSpanType standard_network_clock_accuracy{0x0009356907420000ULL};
  14. static std::chrono::seconds GetSecondsSinceEpoch() {
  15. return std::chrono::duration_cast<std::chrono::seconds>(
  16. std::chrono::system_clock::now().time_since_epoch()) +
  17. Settings::values.custom_rtc_differential;
  18. }
  19. static s64 GetExternalRtcValue() {
  20. return GetSecondsSinceEpoch().count() + TimeManager::GetExternalTimeZoneOffset();
  21. }
  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(GetExternalRtcValue())};
  37. SetupStandardSteadyClock(system, Common::UUID::Generate(), 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::size_t total_location_name_count, u128 time_zone_rule_version,
  85. FileSys::VirtualFile& vfs_file) {
  86. if (time_zone_content_manager.GetTimeZoneManager().SetDeviceLocationNameWithTimeZoneRule(
  87. location_name, vfs_file) != RESULT_SUCCESS) {
  88. UNREACHABLE();
  89. return;
  90. }
  91. time_zone_content_manager.GetTimeZoneManager().SetUpdatedTime(time_zone_updated_time_point);
  92. time_zone_content_manager.GetTimeZoneManager().SetTotalLocationNameCount(
  93. total_location_name_count);
  94. time_zone_content_manager.GetTimeZoneManager().SetTimeZoneRuleVersion(
  95. time_zone_rule_version);
  96. time_zone_content_manager.GetTimeZoneManager().MarkAsInitialized();
  97. }
  98. static s64 GetExternalTimeZoneOffset() {
  99. // With "auto" timezone setting, we use the external system's timezone offset
  100. if (Settings::GetTimeZoneString() == "auto") {
  101. return Common::TimeZone::GetCurrentOffsetSeconds().count();
  102. }
  103. return 0;
  104. }
  105. void SetupStandardSteadyClock(Core::System& system_, Common::UUID clock_source_id,
  106. Clock::TimeSpanType setup_value,
  107. Clock::TimeSpanType internal_offset, bool is_rtc_reset_detected) {
  108. standard_steady_clock_core.SetClockSourceId(clock_source_id);
  109. standard_steady_clock_core.SetSetupValue(setup_value);
  110. standard_steady_clock_core.SetInternalOffset(internal_offset);
  111. standard_steady_clock_core.MarkAsInitialized();
  112. const auto current_time_point{standard_steady_clock_core.GetCurrentRawTimePoint(system_)};
  113. shared_memory.SetupStandardSteadyClock(clock_source_id, current_time_point);
  114. }
  115. void SetupStandardLocalSystemClock(Core::System& system_,
  116. Clock::SystemClockContext clock_context, s64 posix_time) {
  117. standard_local_system_clock_core.SetUpdateCallbackInstance(
  118. local_system_clock_context_writer);
  119. const auto current_time_point{
  120. standard_local_system_clock_core.GetSteadyClockCore().GetCurrentTimePoint(system_)};
  121. if (current_time_point.clock_source_id == clock_context.steady_time_point.clock_source_id) {
  122. standard_local_system_clock_core.SetSystemClockContext(clock_context);
  123. } else {
  124. if (standard_local_system_clock_core.SetCurrentTime(system_, posix_time) !=
  125. RESULT_SUCCESS) {
  126. UNREACHABLE();
  127. return;
  128. }
  129. }
  130. standard_local_system_clock_core.MarkAsInitialized();
  131. }
  132. void SetupStandardNetworkSystemClock(Clock::SystemClockContext clock_context,
  133. Clock::TimeSpanType sufficient_accuracy) {
  134. standard_network_system_clock_core.SetUpdateCallbackInstance(
  135. network_system_clock_context_writer);
  136. if (standard_network_system_clock_core.SetSystemClockContext(clock_context) !=
  137. RESULT_SUCCESS) {
  138. UNREACHABLE();
  139. return;
  140. }
  141. standard_network_system_clock_core.SetStandardNetworkClockSufficientAccuracy(
  142. sufficient_accuracy);
  143. standard_network_system_clock_core.MarkAsInitialized();
  144. }
  145. void SetupStandardUserSystemClock(Core::System& system_, bool is_automatic_correction_enabled,
  146. Clock::SteadyClockTimePoint steady_clock_time_point) {
  147. if (standard_user_system_clock_core.SetAutomaticCorrectionEnabled(
  148. system_, is_automatic_correction_enabled) != RESULT_SUCCESS) {
  149. UNREACHABLE();
  150. return;
  151. }
  152. standard_user_system_clock_core.SetAutomaticCorrectionUpdatedTime(steady_clock_time_point);
  153. standard_user_system_clock_core.MarkAsInitialized();
  154. shared_memory.SetAutomaticCorrectionEnabled(is_automatic_correction_enabled);
  155. }
  156. void SetupEphemeralNetworkSystemClock() {
  157. ephemeral_network_system_clock_core.SetUpdateCallbackInstance(
  158. ephemeral_network_system_clock_context_writer);
  159. ephemeral_network_system_clock_core.MarkAsInitialized();
  160. }
  161. void UpdateLocalSystemClockTime(Core::System& system_, s64 posix_time) {
  162. const auto timespan{Clock::TimeSpanType::FromSeconds(posix_time)};
  163. if (GetStandardLocalSystemClockCore()
  164. .SetCurrentTime(system_, timespan.ToSeconds())
  165. .IsError()) {
  166. UNREACHABLE();
  167. return;
  168. }
  169. }
  170. SharedMemory shared_memory;
  171. Clock::StandardSteadyClockCore standard_steady_clock_core;
  172. Clock::TickBasedSteadyClockCore tick_based_steady_clock_core;
  173. Clock::StandardLocalSystemClockCore standard_local_system_clock_core;
  174. Clock::StandardNetworkSystemClockCore standard_network_system_clock_core;
  175. Clock::StandardUserSystemClockCore standard_user_system_clock_core;
  176. Clock::EphemeralNetworkSystemClockCore ephemeral_network_system_clock_core;
  177. std::shared_ptr<Clock::LocalSystemClockContextWriter> local_system_clock_context_writer;
  178. std::shared_ptr<Clock::NetworkSystemClockContextWriter> network_system_clock_context_writer;
  179. std::shared_ptr<Clock::EphemeralNetworkSystemClockContextWriter>
  180. ephemeral_network_system_clock_context_writer;
  181. TimeZone::TimeZoneContentManager time_zone_content_manager;
  182. };
  183. TimeManager::TimeManager(Core::System& system_) : system{system_} {}
  184. TimeManager::~TimeManager() = default;
  185. void TimeManager::Initialize() {
  186. impl = std::make_unique<Impl>(system);
  187. // Time zones can only be initialized after impl is valid
  188. impl->time_zone_content_manager.Initialize(*this);
  189. }
  190. Clock::StandardSteadyClockCore& TimeManager::GetStandardSteadyClockCore() {
  191. return impl->standard_steady_clock_core;
  192. }
  193. const Clock::StandardSteadyClockCore& TimeManager::GetStandardSteadyClockCore() const {
  194. return impl->standard_steady_clock_core;
  195. }
  196. Clock::StandardLocalSystemClockCore& TimeManager::GetStandardLocalSystemClockCore() {
  197. return impl->standard_local_system_clock_core;
  198. }
  199. const Clock::StandardLocalSystemClockCore& TimeManager::GetStandardLocalSystemClockCore() const {
  200. return impl->standard_local_system_clock_core;
  201. }
  202. Clock::StandardNetworkSystemClockCore& TimeManager::GetStandardNetworkSystemClockCore() {
  203. return impl->standard_network_system_clock_core;
  204. }
  205. const Clock::StandardNetworkSystemClockCore& TimeManager::GetStandardNetworkSystemClockCore()
  206. const {
  207. return impl->standard_network_system_clock_core;
  208. }
  209. Clock::StandardUserSystemClockCore& TimeManager::GetStandardUserSystemClockCore() {
  210. return impl->standard_user_system_clock_core;
  211. }
  212. const Clock::StandardUserSystemClockCore& TimeManager::GetStandardUserSystemClockCore() const {
  213. return impl->standard_user_system_clock_core;
  214. }
  215. TimeZone::TimeZoneContentManager& TimeManager::GetTimeZoneContentManager() {
  216. return impl->time_zone_content_manager;
  217. }
  218. const TimeZone::TimeZoneContentManager& TimeManager::GetTimeZoneContentManager() const {
  219. return impl->time_zone_content_manager;
  220. }
  221. SharedMemory& TimeManager::GetSharedMemory() {
  222. return impl->shared_memory;
  223. }
  224. const SharedMemory& TimeManager::GetSharedMemory() const {
  225. return impl->shared_memory;
  226. }
  227. void TimeManager::Shutdown() {
  228. impl.reset();
  229. }
  230. void TimeManager::UpdateLocalSystemClockTime(s64 posix_time) {
  231. impl->UpdateLocalSystemClockTime(system, posix_time);
  232. }
  233. void TimeManager::SetupTimeZoneManager(std::string location_name,
  234. Clock::SteadyClockTimePoint time_zone_updated_time_point,
  235. std::size_t total_location_name_count,
  236. u128 time_zone_rule_version,
  237. FileSys::VirtualFile& vfs_file) {
  238. impl->SetupTimeZoneManager(location_name, time_zone_updated_time_point,
  239. total_location_name_count, time_zone_rule_version, vfs_file);
  240. }
  241. /*static*/ s64 TimeManager::GetExternalTimeZoneOffset() {
  242. // With "auto" timezone setting, we use the external system's timezone offset
  243. if (Settings::GetTimeZoneString() == "auto") {
  244. return Common::TimeZone::GetCurrentOffsetSeconds().count();
  245. }
  246. return 0;
  247. }
  248. } // namespace Service::Time