blob: 17be2c6efc714b371f33f92970667033ee5efa1f [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
Patrick Williams38679262022-07-22 19:26:55 -050027EpochBase::EpochBase(sdbusplus::bus_t& bus, const char* objPath) :
28 sdbusplus::server::object_t<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 }
Patrick Williams38679262022-07-22 19:26:55 -050049 catch (const sdbusplus::exception_t& ex)
Lei YU1c2ce822017-08-01 17:37:41 +080050 {
George Liu947b5342022-07-01 16:12:18 +080051 lg2::error("Error in setting system time: {ERROR}", "ERROR", ex);
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