blob: 96552ac45ff3340498c515a90a8bcb3cd6b01df4 [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>
5#include <phosphor-logging/log.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
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050027EpochBase::EpochBase(sdbusplus::bus::bus& bus, const char* objPath) :
28 sdbusplus::server::object::object<EpochTime>(bus, objPath), bus(bus)
George Liuc6d33972020-06-22 10:35:29 +080029{}
Lei YU2f9c0cc2017-01-20 14:02:03 +080030
Lei YU415b9642017-02-09 11:37:26 +080031void EpochBase::onModeChanged(Mode mode)
Lei YU2f9c0cc2017-01-20 14:02:03 +080032{
Lei YU415b9642017-02-09 11:37:26 +080033 timeMode = mode;
Lei YU2f9c0cc2017-01-20 14:02:03 +080034}
35
Lei YU2f9c0cc2017-01-20 14:02:03 +080036using namespace std::chrono;
Lei YUfa024882017-11-09 10:49:26 +080037bool EpochBase::setTime(const microseconds& usec)
Lei YU2f9c0cc2017-01-20 14:02:03 +080038{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050039 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
40 SYSTEMD_TIME_INTERFACE, METHOD_SET_TIME);
Lei YU2f9c0cc2017-01-20 14:02:03 +080041 method.append(static_cast<int64_t>(usec.count()),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050042 false, // relative
Lei YU2f9c0cc2017-01-20 14:02:03 +080043 false); // user_interaction
Lei YUbae8d7a2018-07-24 10:09:35 +080044
45 try
46 {
47 bus.call_noreply(method);
48 }
49 catch (const sdbusplus::exception::SdBusError& ex)
Lei YU1c2ce822017-08-01 17:37:41 +080050 {
51 log<level::ERR>("Error in setting system time");
Lei YUf6fad822018-07-13 16:35:45 +080052 using namespace xyz::openbmc_project::Time;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050053 elog<FailedError>(Failed::REASON(ex.what()));
Lei YU1c2ce822017-08-01 17:37:41 +080054 }
Lei YUfa024882017-11-09 10:49:26 +080055 return true;
Lei YU2f9c0cc2017-01-20 14:02:03 +080056}
57
58microseconds EpochBase::getTime() const
59{
60 auto now = system_clock::now();
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050061 return duration_cast<microseconds>(now.time_since_epoch());
Lei YU2f9c0cc2017-01-20 14:02:03 +080062}
63
64} // namespace time
65} // namespace phosphor