time.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/settings.h"
  16. namespace Service::Time {
  17. static std::chrono::seconds GetSecondsSinceEpoch() {
  18. return std::chrono::duration_cast<std::chrono::seconds>(
  19. std::chrono::system_clock::now().time_since_epoch()) +
  20. Settings::values.custom_rtc_differential;
  21. }
  22. static void PosixToCalendar(u64 posix_time, CalendarTime& calendar_time,
  23. CalendarAdditionalInfo& additional_info,
  24. [[maybe_unused]] const TimeZoneRule& /*rule*/) {
  25. const std::time_t time(posix_time);
  26. const std::tm* tm = std::localtime(&time);
  27. if (tm == nullptr) {
  28. calendar_time = {};
  29. additional_info = {};
  30. return;
  31. }
  32. calendar_time.year = tm->tm_year + 1900;
  33. calendar_time.month = tm->tm_mon + 1;
  34. calendar_time.day = tm->tm_mday;
  35. calendar_time.hour = tm->tm_hour;
  36. calendar_time.minute = tm->tm_min;
  37. calendar_time.second = tm->tm_sec;
  38. additional_info.day_of_week = tm->tm_wday;
  39. additional_info.day_of_year = tm->tm_yday;
  40. std::memcpy(additional_info.name.data(), "UTC", sizeof("UTC"));
  41. additional_info.utc_offset = 0;
  42. }
  43. static u64 CalendarToPosix(const CalendarTime& calendar_time,
  44. [[maybe_unused]] const TimeZoneRule& /*rule*/) {
  45. std::tm time{};
  46. time.tm_year = calendar_time.year - 1900;
  47. time.tm_mon = calendar_time.month - 1;
  48. time.tm_mday = calendar_time.day;
  49. time.tm_hour = calendar_time.hour;
  50. time.tm_min = calendar_time.minute;
  51. time.tm_sec = calendar_time.second;
  52. std::time_t epoch_time = std::mktime(&time);
  53. return static_cast<u64>(epoch_time);
  54. }
  55. class ISystemClock final : public ServiceFramework<ISystemClock> {
  56. public:
  57. ISystemClock() : ServiceFramework("ISystemClock") {
  58. static const FunctionInfo functions[] = {
  59. {0, &ISystemClock::GetCurrentTime, "GetCurrentTime"},
  60. {1, nullptr, "SetCurrentTime"},
  61. {2, &ISystemClock::GetSystemClockContext, "GetSystemClockContext"},
  62. {3, nullptr, "SetSystemClockContext"},
  63. };
  64. RegisterHandlers(functions);
  65. }
  66. private:
  67. void GetCurrentTime(Kernel::HLERequestContext& ctx) {
  68. const s64 time_since_epoch{GetSecondsSinceEpoch().count()};
  69. LOG_DEBUG(Service_Time, "called");
  70. IPC::ResponseBuilder rb{ctx, 4};
  71. rb.Push(RESULT_SUCCESS);
  72. rb.Push<u64>(time_since_epoch);
  73. }
  74. void GetSystemClockContext(Kernel::HLERequestContext& ctx) {
  75. LOG_WARNING(Service_Time, "(STUBBED) called");
  76. SystemClockContext system_clock_ontext{};
  77. IPC::ResponseBuilder rb{ctx, (sizeof(SystemClockContext) / 4) + 2};
  78. rb.Push(RESULT_SUCCESS);
  79. rb.PushRaw(system_clock_ontext);
  80. }
  81. };
  82. class ISteadyClock final : public ServiceFramework<ISteadyClock> {
  83. public:
  84. ISteadyClock() : ServiceFramework("ISteadyClock") {
  85. static const FunctionInfo functions[] = {
  86. {0, &ISteadyClock::GetCurrentTimePoint, "GetCurrentTimePoint"},
  87. };
  88. RegisterHandlers(functions);
  89. }
  90. private:
  91. void GetCurrentTimePoint(Kernel::HLERequestContext& ctx) {
  92. LOG_DEBUG(Service_Time, "called");
  93. const auto& core_timing = Core::System::GetInstance().CoreTiming();
  94. const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks());
  95. const SteadyClockTimePoint steady_clock_time_point{static_cast<u64_le>(ms.count() / 1000),
  96. {}};
  97. IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2};
  98. rb.Push(RESULT_SUCCESS);
  99. rb.PushRaw(steady_clock_time_point);
  100. }
  101. };
  102. class ITimeZoneService final : public ServiceFramework<ITimeZoneService> {
  103. public:
  104. ITimeZoneService() : ServiceFramework("ITimeZoneService") {
  105. static const FunctionInfo functions[] = {
  106. {0, &ITimeZoneService::GetDeviceLocationName, "GetDeviceLocationName"},
  107. {1, nullptr, "SetDeviceLocationName"},
  108. {2, &ITimeZoneService::GetTotalLocationNameCount, "GetTotalLocationNameCount"},
  109. {3, nullptr, "LoadLocationNameList"},
  110. {4, &ITimeZoneService::LoadTimeZoneRule, "LoadTimeZoneRule"},
  111. {5, nullptr, "GetTimeZoneRuleVersion"},
  112. {100, &ITimeZoneService::ToCalendarTime, "ToCalendarTime"},
  113. {101, &ITimeZoneService::ToCalendarTimeWithMyRule, "ToCalendarTimeWithMyRule"},
  114. {201, &ITimeZoneService::ToPosixTime, "ToPosixTime"},
  115. {202, &ITimeZoneService::ToPosixTimeWithMyRule, "ToPosixTimeWithMyRule"},
  116. };
  117. RegisterHandlers(functions);
  118. }
  119. private:
  120. LocationName location_name{"UTC"};
  121. TimeZoneRule my_time_zone_rule{};
  122. void GetDeviceLocationName(Kernel::HLERequestContext& ctx) {
  123. LOG_DEBUG(Service_Time, "called");
  124. IPC::ResponseBuilder rb{ctx, (sizeof(LocationName) / 4) + 2};
  125. rb.Push(RESULT_SUCCESS);
  126. rb.PushRaw(location_name);
  127. }
  128. void GetTotalLocationNameCount(Kernel::HLERequestContext& ctx) {
  129. LOG_WARNING(Service_Time, "(STUBBED) called");
  130. IPC::ResponseBuilder rb{ctx, 3};
  131. rb.Push(RESULT_SUCCESS);
  132. rb.Push<u32>(0);
  133. }
  134. void LoadTimeZoneRule(Kernel::HLERequestContext& ctx) {
  135. LOG_WARNING(Service_Time, "(STUBBED) called");
  136. ctx.WriteBuffer(&my_time_zone_rule, sizeof(TimeZoneRule));
  137. IPC::ResponseBuilder rb{ctx, 2};
  138. rb.Push(RESULT_SUCCESS);
  139. }
  140. void ToCalendarTime(Kernel::HLERequestContext& ctx) {
  141. IPC::RequestParser rp{ctx};
  142. const u64 posix_time = rp.Pop<u64>();
  143. LOG_WARNING(Service_Time, "(STUBBED) called, posix_time=0x{:016X}", posix_time);
  144. TimeZoneRule time_zone_rule{};
  145. auto buffer = ctx.ReadBuffer();
  146. std::memcpy(&time_zone_rule, buffer.data(), buffer.size());
  147. CalendarTime calendar_time{2018, 1, 1, 0, 0, 0};
  148. CalendarAdditionalInfo additional_info{};
  149. PosixToCalendar(posix_time, calendar_time, additional_info, time_zone_rule);
  150. IPC::ResponseBuilder rb{ctx, 10};
  151. rb.Push(RESULT_SUCCESS);
  152. rb.PushRaw(calendar_time);
  153. rb.PushRaw(additional_info);
  154. }
  155. void ToCalendarTimeWithMyRule(Kernel::HLERequestContext& ctx) {
  156. IPC::RequestParser rp{ctx};
  157. const u64 posix_time = rp.Pop<u64>();
  158. LOG_WARNING(Service_Time, "(STUBBED) called, posix_time=0x{:016X}", posix_time);
  159. CalendarTime calendar_time{2018, 1, 1, 0, 0, 0};
  160. CalendarAdditionalInfo additional_info{};
  161. PosixToCalendar(posix_time, calendar_time, additional_info, my_time_zone_rule);
  162. IPC::ResponseBuilder rb{ctx, 10};
  163. rb.Push(RESULT_SUCCESS);
  164. rb.PushRaw(calendar_time);
  165. rb.PushRaw(additional_info);
  166. }
  167. void ToPosixTime(Kernel::HLERequestContext& ctx) {
  168. // TODO(ogniK): Figure out how to handle multiple times
  169. LOG_WARNING(Service_Time, "(STUBBED) called");
  170. IPC::RequestParser rp{ctx};
  171. auto calendar_time = rp.PopRaw<CalendarTime>();
  172. auto posix_time = CalendarToPosix(calendar_time, {});
  173. IPC::ResponseBuilder rb{ctx, 3};
  174. rb.Push(RESULT_SUCCESS);
  175. rb.PushRaw<u32>(1); // Amount of times we're returning
  176. ctx.WriteBuffer(&posix_time, sizeof(u64));
  177. }
  178. void ToPosixTimeWithMyRule(Kernel::HLERequestContext& ctx) {
  179. LOG_WARNING(Service_Time, "(STUBBED) called");
  180. IPC::RequestParser rp{ctx};
  181. auto calendar_time = rp.PopRaw<CalendarTime>();
  182. auto posix_time = CalendarToPosix(calendar_time, {});
  183. IPC::ResponseBuilder rb{ctx, 3};
  184. rb.Push(RESULT_SUCCESS);
  185. rb.PushRaw<u32>(1); // Amount of times we're returning
  186. ctx.WriteBuffer(&posix_time, sizeof(u64));
  187. }
  188. };
  189. void Module::Interface::GetStandardUserSystemClock(Kernel::HLERequestContext& ctx) {
  190. LOG_DEBUG(Service_Time, "called");
  191. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  192. rb.Push(RESULT_SUCCESS);
  193. rb.PushIpcInterface<ISystemClock>();
  194. }
  195. void Module::Interface::GetStandardNetworkSystemClock(Kernel::HLERequestContext& ctx) {
  196. LOG_DEBUG(Service_Time, "called");
  197. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  198. rb.Push(RESULT_SUCCESS);
  199. rb.PushIpcInterface<ISystemClock>();
  200. }
  201. void Module::Interface::GetStandardSteadyClock(Kernel::HLERequestContext& ctx) {
  202. LOG_DEBUG(Service_Time, "called");
  203. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  204. rb.Push(RESULT_SUCCESS);
  205. rb.PushIpcInterface<ISteadyClock>();
  206. }
  207. void Module::Interface::GetTimeZoneService(Kernel::HLERequestContext& ctx) {
  208. LOG_DEBUG(Service_Time, "called");
  209. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  210. rb.Push(RESULT_SUCCESS);
  211. rb.PushIpcInterface<ITimeZoneService>();
  212. }
  213. void Module::Interface::GetStandardLocalSystemClock(Kernel::HLERequestContext& ctx) {
  214. LOG_DEBUG(Service_Time, "called");
  215. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  216. rb.Push(RESULT_SUCCESS);
  217. rb.PushIpcInterface<ISystemClock>();
  218. }
  219. void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) {
  220. LOG_DEBUG(Service_Time, "called");
  221. IPC::RequestParser rp{ctx};
  222. const auto initial_type = rp.PopRaw<u8>();
  223. const s64 time_since_epoch{GetSecondsSinceEpoch().count()};
  224. const std::time_t time(time_since_epoch);
  225. const std::tm* tm = std::localtime(&time);
  226. if (tm == nullptr) {
  227. LOG_ERROR(Service_Time, "tm is a nullptr");
  228. IPC::ResponseBuilder rb{ctx, 2};
  229. rb.Push(ResultCode(-1)); // TODO(ogniK): Find appropriate error code
  230. return;
  231. }
  232. const auto& core_timing = Core::System::GetInstance().CoreTiming();
  233. const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks());
  234. const SteadyClockTimePoint steady_clock_time_point{static_cast<u64_le>(ms.count() / 1000), {}};
  235. CalendarTime calendar_time{};
  236. calendar_time.year = tm->tm_year + 1900;
  237. calendar_time.month = tm->tm_mon + 1;
  238. calendar_time.day = tm->tm_mday;
  239. calendar_time.hour = tm->tm_hour;
  240. calendar_time.minute = tm->tm_min;
  241. calendar_time.second = tm->tm_sec;
  242. ClockSnapshot clock_snapshot{};
  243. clock_snapshot.system_posix_time = time_since_epoch;
  244. clock_snapshot.network_posix_time = time_since_epoch;
  245. clock_snapshot.system_calendar_time = calendar_time;
  246. clock_snapshot.network_calendar_time = calendar_time;
  247. CalendarAdditionalInfo additional_info{};
  248. PosixToCalendar(time_since_epoch, calendar_time, additional_info, {});
  249. clock_snapshot.system_calendar_info = additional_info;
  250. clock_snapshot.network_calendar_info = additional_info;
  251. clock_snapshot.steady_clock_timepoint = steady_clock_time_point;
  252. clock_snapshot.location_name = LocationName{"UTC"};
  253. clock_snapshot.clock_auto_adjustment_enabled = 1;
  254. clock_snapshot.type = initial_type;
  255. IPC::ResponseBuilder rb{ctx, 2};
  256. rb.Push(RESULT_SUCCESS);
  257. ctx.WriteBuffer(&clock_snapshot, sizeof(ClockSnapshot));
  258. }
  259. void Module::Interface::CalculateStandardUserSystemClockDifferenceByUser(
  260. Kernel::HLERequestContext& ctx) {
  261. LOG_DEBUG(Service_Time, "called");
  262. IPC::RequestParser rp{ctx};
  263. const auto snapshot_a = rp.PopRaw<ClockSnapshot>();
  264. const auto snapshot_b = rp.PopRaw<ClockSnapshot>();
  265. const u64 difference =
  266. snapshot_b.user_clock_context.offset - snapshot_a.user_clock_context.offset;
  267. IPC::ResponseBuilder rb{ctx, 4};
  268. rb.Push(RESULT_SUCCESS);
  269. rb.PushRaw<u64>(difference);
  270. }
  271. Module::Interface::Interface(std::shared_ptr<Module> time, const char* name)
  272. : ServiceFramework(name), time(std::move(time)) {}
  273. Module::Interface::~Interface() = default;
  274. void InstallInterfaces(SM::ServiceManager& service_manager) {
  275. auto time = std::make_shared<Module>();
  276. std::make_shared<Time>(time, "time:a")->InstallAsService(service_manager);
  277. std::make_shared<Time>(time, "time:s")->InstallAsService(service_manager);
  278. std::make_shared<Time>(time, "time:u")->InstallAsService(service_manager);
  279. }
  280. } // namespace Service::Time