time.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright 2018 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/logging/log.h"
  7. #include "core/core.h"
  8. #include "core/core_timing.h"
  9. #include "core/core_timing_util.h"
  10. #include "core/hle/ipc_helpers.h"
  11. #include "core/hle/kernel/client_port.h"
  12. #include "core/hle/kernel/client_session.h"
  13. #include "core/hle/service/time/interface.h"
  14. #include "core/hle/service/time/time.h"
  15. #include "core/hle/service/time/time_sharedmemory.h"
  16. #include "core/settings.h"
  17. namespace Service::Time {
  18. static std::chrono::seconds GetSecondsSinceEpoch() {
  19. return std::chrono::duration_cast<std::chrono::seconds>(
  20. std::chrono::system_clock::now().time_since_epoch()) +
  21. Settings::values.custom_rtc_differential;
  22. }
  23. static void PosixToCalendar(u64 posix_time, CalendarTime& calendar_time,
  24. CalendarAdditionalInfo& additional_info,
  25. [[maybe_unused]] const TimeZoneRule& /*rule*/) {
  26. const std::time_t time(posix_time);
  27. const std::tm* tm = std::localtime(&time);
  28. if (tm == nullptr) {
  29. calendar_time = {};
  30. additional_info = {};
  31. return;
  32. }
  33. calendar_time.year = static_cast<u16_le>(tm->tm_year + 1900);
  34. calendar_time.month = static_cast<u8>(tm->tm_mon + 1);
  35. calendar_time.day = static_cast<u8>(tm->tm_mday);
  36. calendar_time.hour = static_cast<u8>(tm->tm_hour);
  37. calendar_time.minute = static_cast<u8>(tm->tm_min);
  38. calendar_time.second = static_cast<u8>(tm->tm_sec);
  39. additional_info.day_of_week = tm->tm_wday;
  40. additional_info.day_of_year = tm->tm_yday;
  41. std::memcpy(additional_info.name.data(), "UTC", sizeof("UTC"));
  42. additional_info.utc_offset = 0;
  43. }
  44. static u64 CalendarToPosix(const CalendarTime& calendar_time,
  45. [[maybe_unused]] const TimeZoneRule& /*rule*/) {
  46. std::tm time{};
  47. time.tm_year = calendar_time.year - 1900;
  48. time.tm_mon = calendar_time.month - 1;
  49. time.tm_mday = calendar_time.day;
  50. time.tm_hour = calendar_time.hour;
  51. time.tm_min = calendar_time.minute;
  52. time.tm_sec = calendar_time.second;
  53. std::time_t epoch_time = std::mktime(&time);
  54. return static_cast<u64>(epoch_time);
  55. }
  56. enum class ClockContextType {
  57. StandardSteady,
  58. StandardUserSystem,
  59. StandardNetworkSystem,
  60. StandardLocalSystem,
  61. };
  62. class ISystemClock final : public ServiceFramework<ISystemClock> {
  63. public:
  64. ISystemClock(std::shared_ptr<Service::Time::SharedMemory> shared_memory,
  65. ClockContextType clock_type)
  66. : ServiceFramework("ISystemClock"), shared_memory(shared_memory), clock_type(clock_type) {
  67. // clang-format off
  68. static const FunctionInfo functions[] = {
  69. {0, &ISystemClock::GetCurrentTime, "GetCurrentTime"},
  70. {1, nullptr, "SetCurrentTime"},
  71. {2, &ISystemClock::GetSystemClockContext, "GetSystemClockContext"},
  72. {3, nullptr, "SetSystemClockContext"},
  73. {4, nullptr, "GetOperationEventReadableHandle"},
  74. };
  75. // clang-format on
  76. RegisterHandlers(functions);
  77. UpdateSharedMemoryContext(system_clock_context);
  78. }
  79. private:
  80. void GetCurrentTime(Kernel::HLERequestContext& ctx) {
  81. const s64 time_since_epoch{GetSecondsSinceEpoch().count()};
  82. LOG_DEBUG(Service_Time, "called");
  83. IPC::ResponseBuilder rb{ctx, 4};
  84. rb.Push(RESULT_SUCCESS);
  85. rb.Push<u64>(time_since_epoch);
  86. }
  87. void GetSystemClockContext(Kernel::HLERequestContext& ctx) {
  88. LOG_WARNING(Service_Time, "(STUBBED) called");
  89. // TODO(ogniK): This should be updated periodically however since we have it stubbed we'll
  90. // only update when we get a new context
  91. UpdateSharedMemoryContext(system_clock_context);
  92. IPC::ResponseBuilder rb{ctx, (sizeof(SystemClockContext) / 4) + 2};
  93. rb.Push(RESULT_SUCCESS);
  94. rb.PushRaw(system_clock_context);
  95. }
  96. void UpdateSharedMemoryContext(const SystemClockContext& clock_context) {
  97. switch (clock_type) {
  98. case ClockContextType::StandardLocalSystem:
  99. shared_memory->SetStandardLocalSystemClockContext(clock_context);
  100. break;
  101. case ClockContextType::StandardNetworkSystem:
  102. shared_memory->SetStandardNetworkSystemClockContext(clock_context);
  103. break;
  104. }
  105. }
  106. SystemClockContext system_clock_context{};
  107. std::shared_ptr<Service::Time::SharedMemory> shared_memory;
  108. ClockContextType clock_type;
  109. };
  110. class ISteadyClock final : public ServiceFramework<ISteadyClock> {
  111. public:
  112. ISteadyClock(std::shared_ptr<SharedMemory> shared_memory, Core::System& system)
  113. : ServiceFramework("ISteadyClock"), shared_memory(shared_memory), system(system) {
  114. static const FunctionInfo functions[] = {
  115. {0, &ISteadyClock::GetCurrentTimePoint, "GetCurrentTimePoint"},
  116. };
  117. RegisterHandlers(functions);
  118. shared_memory->SetStandardSteadyClockTimepoint(GetCurrentTimePoint());
  119. }
  120. private:
  121. void GetCurrentTimePoint(Kernel::HLERequestContext& ctx) {
  122. LOG_DEBUG(Service_Time, "called");
  123. const auto time_point = GetCurrentTimePoint();
  124. // TODO(ogniK): This should be updated periodically
  125. shared_memory->SetStandardSteadyClockTimepoint(time_point);
  126. IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2};
  127. rb.Push(RESULT_SUCCESS);
  128. rb.PushRaw(time_point);
  129. }
  130. SteadyClockTimePoint GetCurrentTimePoint() const {
  131. const auto& core_timing = system.CoreTiming();
  132. const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks());
  133. return {static_cast<u64_le>(ms.count() / 1000), {}};
  134. }
  135. std::shared_ptr<SharedMemory> shared_memory;
  136. Core::System& system;
  137. };
  138. class ITimeZoneService final : public ServiceFramework<ITimeZoneService> {
  139. public:
  140. ITimeZoneService() : ServiceFramework("ITimeZoneService") {
  141. // clang-format off
  142. static const FunctionInfo functions[] = {
  143. {0, &ITimeZoneService::GetDeviceLocationName, "GetDeviceLocationName"},
  144. {1, nullptr, "SetDeviceLocationName"},
  145. {2, &ITimeZoneService::GetTotalLocationNameCount, "GetTotalLocationNameCount"},
  146. {3, nullptr, "LoadLocationNameList"},
  147. {4, &ITimeZoneService::LoadTimeZoneRule, "LoadTimeZoneRule"},
  148. {5, nullptr, "GetTimeZoneRuleVersion"},
  149. {6, nullptr, "GetDeviceLocationNameAndUpdatedTime"},
  150. {7, nullptr, "SetDeviceLocationNameWithTimeZoneRule"},
  151. {8, nullptr, "ParseTimeZoneBinary"},
  152. {20, nullptr, "GetDeviceLocationNameOperationEventReadableHandle"},
  153. {100, &ITimeZoneService::ToCalendarTime, "ToCalendarTime"},
  154. {101, &ITimeZoneService::ToCalendarTimeWithMyRule, "ToCalendarTimeWithMyRule"},
  155. {201, &ITimeZoneService::ToPosixTime, "ToPosixTime"},
  156. {202, &ITimeZoneService::ToPosixTimeWithMyRule, "ToPosixTimeWithMyRule"},
  157. };
  158. // clang-format on
  159. RegisterHandlers(functions);
  160. }
  161. private:
  162. LocationName location_name{"UTC"};
  163. TimeZoneRule my_time_zone_rule{};
  164. void GetDeviceLocationName(Kernel::HLERequestContext& ctx) {
  165. LOG_DEBUG(Service_Time, "called");
  166. IPC::ResponseBuilder rb{ctx, (sizeof(LocationName) / 4) + 2};
  167. rb.Push(RESULT_SUCCESS);
  168. rb.PushRaw(location_name);
  169. }
  170. void GetTotalLocationNameCount(Kernel::HLERequestContext& ctx) {
  171. LOG_WARNING(Service_Time, "(STUBBED) called");
  172. IPC::ResponseBuilder rb{ctx, 3};
  173. rb.Push(RESULT_SUCCESS);
  174. rb.Push<u32>(0);
  175. }
  176. void LoadTimeZoneRule(Kernel::HLERequestContext& ctx) {
  177. LOG_WARNING(Service_Time, "(STUBBED) called");
  178. ctx.WriteBuffer(&my_time_zone_rule, sizeof(TimeZoneRule));
  179. IPC::ResponseBuilder rb{ctx, 2};
  180. rb.Push(RESULT_SUCCESS);
  181. }
  182. void ToCalendarTime(Kernel::HLERequestContext& ctx) {
  183. IPC::RequestParser rp{ctx};
  184. const u64 posix_time = rp.Pop<u64>();
  185. LOG_WARNING(Service_Time, "(STUBBED) called, posix_time=0x{:016X}", posix_time);
  186. TimeZoneRule time_zone_rule{};
  187. auto buffer = ctx.ReadBuffer();
  188. std::memcpy(&time_zone_rule, buffer.data(), buffer.size());
  189. CalendarTime calendar_time{2018, 1, 1, 0, 0, 0};
  190. CalendarAdditionalInfo additional_info{};
  191. PosixToCalendar(posix_time, calendar_time, additional_info, time_zone_rule);
  192. IPC::ResponseBuilder rb{ctx, 10};
  193. rb.Push(RESULT_SUCCESS);
  194. rb.PushRaw(calendar_time);
  195. rb.PushRaw(additional_info);
  196. }
  197. void ToCalendarTimeWithMyRule(Kernel::HLERequestContext& ctx) {
  198. IPC::RequestParser rp{ctx};
  199. const u64 posix_time = rp.Pop<u64>();
  200. LOG_WARNING(Service_Time, "(STUBBED) called, posix_time=0x{:016X}", posix_time);
  201. CalendarTime calendar_time{2018, 1, 1, 0, 0, 0};
  202. CalendarAdditionalInfo additional_info{};
  203. PosixToCalendar(posix_time, calendar_time, additional_info, my_time_zone_rule);
  204. IPC::ResponseBuilder rb{ctx, 10};
  205. rb.Push(RESULT_SUCCESS);
  206. rb.PushRaw(calendar_time);
  207. rb.PushRaw(additional_info);
  208. }
  209. void ToPosixTime(Kernel::HLERequestContext& ctx) {
  210. // TODO(ogniK): Figure out how to handle multiple times
  211. LOG_WARNING(Service_Time, "(STUBBED) called");
  212. IPC::RequestParser rp{ctx};
  213. auto calendar_time = rp.PopRaw<CalendarTime>();
  214. auto posix_time = CalendarToPosix(calendar_time, {});
  215. IPC::ResponseBuilder rb{ctx, 3};
  216. rb.Push(RESULT_SUCCESS);
  217. rb.PushRaw<u32>(1); // Amount of times we're returning
  218. ctx.WriteBuffer(&posix_time, sizeof(u64));
  219. }
  220. void ToPosixTimeWithMyRule(Kernel::HLERequestContext& ctx) {
  221. LOG_WARNING(Service_Time, "(STUBBED) called");
  222. IPC::RequestParser rp{ctx};
  223. auto calendar_time = rp.PopRaw<CalendarTime>();
  224. auto posix_time = CalendarToPosix(calendar_time, {});
  225. IPC::ResponseBuilder rb{ctx, 3};
  226. rb.Push(RESULT_SUCCESS);
  227. rb.PushRaw<u32>(1); // Amount of times we're returning
  228. ctx.WriteBuffer(&posix_time, sizeof(u64));
  229. }
  230. };
  231. void Module::Interface::GetStandardUserSystemClock(Kernel::HLERequestContext& ctx) {
  232. LOG_DEBUG(Service_Time, "called");
  233. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  234. rb.Push(RESULT_SUCCESS);
  235. rb.PushIpcInterface<ISystemClock>(shared_memory, ClockContextType::StandardUserSystem);
  236. }
  237. void Module::Interface::GetStandardNetworkSystemClock(Kernel::HLERequestContext& ctx) {
  238. LOG_DEBUG(Service_Time, "called");
  239. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  240. rb.Push(RESULT_SUCCESS);
  241. rb.PushIpcInterface<ISystemClock>(shared_memory, ClockContextType::StandardNetworkSystem);
  242. }
  243. void Module::Interface::GetStandardSteadyClock(Kernel::HLERequestContext& ctx) {
  244. LOG_DEBUG(Service_Time, "called");
  245. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  246. rb.Push(RESULT_SUCCESS);
  247. rb.PushIpcInterface<ISteadyClock>(shared_memory, system);
  248. }
  249. void Module::Interface::GetTimeZoneService(Kernel::HLERequestContext& ctx) {
  250. LOG_DEBUG(Service_Time, "called");
  251. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  252. rb.Push(RESULT_SUCCESS);
  253. rb.PushIpcInterface<ITimeZoneService>();
  254. }
  255. void Module::Interface::GetStandardLocalSystemClock(Kernel::HLERequestContext& ctx) {
  256. LOG_DEBUG(Service_Time, "called");
  257. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  258. rb.Push(RESULT_SUCCESS);
  259. rb.PushIpcInterface<ISystemClock>(shared_memory, ClockContextType::StandardLocalSystem);
  260. }
  261. void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) {
  262. LOG_DEBUG(Service_Time, "called");
  263. IPC::RequestParser rp{ctx};
  264. const auto initial_type = rp.PopRaw<u8>();
  265. const s64 time_since_epoch{GetSecondsSinceEpoch().count()};
  266. const std::time_t time(time_since_epoch);
  267. const std::tm* tm = std::localtime(&time);
  268. if (tm == nullptr) {
  269. LOG_ERROR(Service_Time, "tm is a nullptr");
  270. IPC::ResponseBuilder rb{ctx, 2};
  271. rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Find appropriate error code
  272. return;
  273. }
  274. const auto& core_timing = system.CoreTiming();
  275. const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks());
  276. const SteadyClockTimePoint steady_clock_time_point{static_cast<u64_le>(ms.count() / 1000), {}};
  277. CalendarTime calendar_time{};
  278. calendar_time.year = static_cast<u16_le>(tm->tm_year + 1900);
  279. calendar_time.month = static_cast<u8>(tm->tm_mon + 1);
  280. calendar_time.day = static_cast<u8>(tm->tm_mday);
  281. calendar_time.hour = static_cast<u8>(tm->tm_hour);
  282. calendar_time.minute = static_cast<u8>(tm->tm_min);
  283. calendar_time.second = static_cast<u8>(tm->tm_sec);
  284. ClockSnapshot clock_snapshot{};
  285. clock_snapshot.system_posix_time = time_since_epoch;
  286. clock_snapshot.network_posix_time = time_since_epoch;
  287. clock_snapshot.system_calendar_time = calendar_time;
  288. clock_snapshot.network_calendar_time = calendar_time;
  289. CalendarAdditionalInfo additional_info{};
  290. PosixToCalendar(time_since_epoch, calendar_time, additional_info, {});
  291. clock_snapshot.system_calendar_info = additional_info;
  292. clock_snapshot.network_calendar_info = additional_info;
  293. clock_snapshot.steady_clock_timepoint = steady_clock_time_point;
  294. clock_snapshot.location_name = LocationName{"UTC"};
  295. clock_snapshot.clock_auto_adjustment_enabled = 1;
  296. clock_snapshot.type = initial_type;
  297. IPC::ResponseBuilder rb{ctx, 2};
  298. rb.Push(RESULT_SUCCESS);
  299. ctx.WriteBuffer(&clock_snapshot, sizeof(ClockSnapshot));
  300. }
  301. void Module::Interface::CalculateStandardUserSystemClockDifferenceByUser(
  302. Kernel::HLERequestContext& ctx) {
  303. LOG_DEBUG(Service_Time, "called");
  304. IPC::RequestParser rp{ctx};
  305. const auto snapshot_a = rp.PopRaw<ClockSnapshot>();
  306. const auto snapshot_b = rp.PopRaw<ClockSnapshot>();
  307. const u64 difference =
  308. snapshot_b.user_clock_context.offset - snapshot_a.user_clock_context.offset;
  309. IPC::ResponseBuilder rb{ctx, 4};
  310. rb.Push(RESULT_SUCCESS);
  311. rb.PushRaw<u64>(difference);
  312. }
  313. void Module::Interface::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
  314. LOG_DEBUG(Service_Time, "called");
  315. IPC::ResponseBuilder rb{ctx, 2, 1};
  316. rb.Push(RESULT_SUCCESS);
  317. rb.PushCopyObjects(shared_memory->GetSharedMemoryHolder());
  318. }
  319. void Module::Interface::IsStandardUserSystemClockAutomaticCorrectionEnabled(
  320. Kernel::HLERequestContext& ctx) {
  321. // ogniK(TODO): When clock contexts are implemented, the value should be read from the context
  322. // instead of our shared memory holder
  323. LOG_DEBUG(Service_Time, "called");
  324. IPC::ResponseBuilder rb{ctx, 3};
  325. rb.Push(RESULT_SUCCESS);
  326. rb.Push<u8>(shared_memory->GetStandardUserSystemClockAutomaticCorrectionEnabled());
  327. }
  328. void Module::Interface::SetStandardUserSystemClockAutomaticCorrectionEnabled(
  329. Kernel::HLERequestContext& ctx) {
  330. IPC::RequestParser rp{ctx};
  331. const auto enabled = rp.Pop<u8>();
  332. LOG_WARNING(Service_Time, "(PARTIAL IMPLEMENTATION) called");
  333. // TODO(ogniK): Update clock contexts and correct timespans
  334. shared_memory->SetStandardUserSystemClockAutomaticCorrectionEnabled(enabled > 0);
  335. IPC::ResponseBuilder rb{ctx, 2};
  336. rb.Push(RESULT_SUCCESS);
  337. }
  338. Module::Interface::Interface(std::shared_ptr<Module> time,
  339. std::shared_ptr<SharedMemory> shared_memory, Core::System& system,
  340. const char* name)
  341. : ServiceFramework(name), time(std::move(time)), shared_memory(std::move(shared_memory)),
  342. system(system) {}
  343. Module::Interface::~Interface() = default;
  344. void InstallInterfaces(Core::System& system) {
  345. auto time = std::make_shared<Module>();
  346. auto shared_mem = std::make_shared<SharedMemory>(system);
  347. std::make_shared<Time>(time, shared_mem, system, "time:a")
  348. ->InstallAsService(system.ServiceManager());
  349. std::make_shared<Time>(time, shared_mem, system, "time:s")
  350. ->InstallAsService(system.ServiceManager());
  351. std::make_shared<Time>(std::move(time), shared_mem, system, "time:u")
  352. ->InstallAsService(system.ServiceManager());
  353. }
  354. } // namespace Service::Time