blob: 1761bea44ca8b3fc0b8952e9ec1b176d403407a2 [file] [log] [blame]
Lei YU2f9c0cc2017-01-20 14:02:03 +08001#include "epoch_base.hpp"
2
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05003#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/elog.hpp>
George Liu947b5342022-07-01 16:12:18 +08005#include <phosphor-logging/lg2.hpp>
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05006#include <xyz/openbmc_project/Time/error.hpp>
Lei YU2f9c0cc2017-01-20 14:02:03 +08007
George Liuc6d33972020-06-22 10:35:29 +08008#include <iomanip>
9#include <sstream>
10
Lei YUdd8e9e42017-04-19 17:46:58 +080011namespace // anonymous
12{
13constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1";
14constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1";
15constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1";
16constexpr auto METHOD_SET_TIME = "SetTime";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050017} // namespace
Lei YUdd8e9e42017-04-19 17:46:58 +080018
Lei YU2f9c0cc2017-01-20 14:02:03 +080019namespace phosphor
20{
21namespace time
22{
23
24using namespace phosphor::logging;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050025using FailedError = sdbusplus::xyz::openbmc_project::Time::Error::Failed;
Lei YU2f9c0cc2017-01-20 14:02:03 +080026
George Liucb421092022-08-16 17:02:31 +080027EpochBase::EpochBase(sdbusplus::bus_t& bus, const char* objPath,
28 Manager& manager) :
29 sdbusplus::server::object_t<EpochTime>(bus, objPath),
30 bus(bus), manager(manager)
George Liuc6d33972020-06-22 10:35:29 +080031{}
Lei YU2f9c0cc2017-01-20 14:02:03 +080032
Lei YU415b9642017-02-09 11:37:26 +080033void EpochBase::onModeChanged(Mode mode)
Lei YU2f9c0cc2017-01-20 14:02:03 +080034{
George Liucb421092022-08-16 17:02:31 +080035 manager.setTimeMode(mode);
Lei YU2f9c0cc2017-01-20 14:02:03 +080036}
37
Lei YU2f9c0cc2017-01-20 14:02:03 +080038using namespace std::chrono;
Lei YUfa024882017-11-09 10:49:26 +080039bool EpochBase::setTime(const microseconds& usec)
Lei YU2f9c0cc2017-01-20 14:02:03 +080040{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050041 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
42 SYSTEMD_TIME_INTERFACE, METHOD_SET_TIME);
Lei YU2f9c0cc2017-01-20 14:02:03 +080043 method.append(static_cast<int64_t>(usec.count()),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050044 false, // relative
Lei YU2f9c0cc2017-01-20 14:02:03 +080045 false); // user_interaction
Lei YUbae8d7a2018-07-24 10:09:35 +080046
47 try
48 {
49 bus.call_noreply(method);
50 }
Patrick Williams38679262022-07-22 19:26:55 -050051 catch (const sdbusplus::exception_t& ex)
Lei YU1c2ce822017-08-01 17:37:41 +080052 {
George Liu947b5342022-07-01 16:12:18 +080053 lg2::error("Error in setting system time: {ERROR}", "ERROR", ex);
Lei YUf6fad822018-07-13 16:35:45 +080054 using namespace xyz::openbmc_project::Time;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050055 elog<FailedError>(Failed::REASON(ex.what()));
Lei YU1c2ce822017-08-01 17:37:41 +080056 }
Lei YUfa024882017-11-09 10:49:26 +080057 return true;
Lei YU2f9c0cc2017-01-20 14:02:03 +080058}
59
60microseconds EpochBase::getTime() const
61{
62 auto now = system_clock::now();
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050063 return duration_cast<microseconds>(now.time_since_epoch());
Lei YU2f9c0cc2017-01-20 14:02:03 +080064}
65
66} // namespace time
67} // namespace phosphor