blob: 93463910288807bb7c7102267fb05e171a6b708a [file] [log] [blame]
Lei YU2f9c0cc2017-01-20 14:02:03 +08001#include "epoch_base.hpp"
2
Lei YU2f9c0cc2017-01-20 14:02:03 +08003#include <iomanip>
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05004#include <phosphor-logging/elog-errors.hpp>
5#include <phosphor-logging/elog.hpp>
6#include <phosphor-logging/log.hpp>
Lei YU2f9c0cc2017-01-20 14:02:03 +08007#include <sstream>
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05008#include <xyz/openbmc_project/Time/error.hpp>
Lei YU2f9c0cc2017-01-20 14:02:03 +08009
Lei YUdd8e9e42017-04-19 17:46:58 +080010namespace // anonymous
11{
12constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1";
13constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1";
14constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1";
15constexpr auto METHOD_SET_TIME = "SetTime";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050016} // namespace
Lei YUdd8e9e42017-04-19 17:46:58 +080017
Lei YU2f9c0cc2017-01-20 14:02:03 +080018namespace phosphor
19{
20namespace time
21{
22
23using namespace phosphor::logging;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050024using FailedError = sdbusplus::xyz::openbmc_project::Time::Error::Failed;
Lei YU2f9c0cc2017-01-20 14:02:03 +080025
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050026EpochBase::EpochBase(sdbusplus::bus::bus& bus, const char* objPath) :
27 sdbusplus::server::object::object<EpochTime>(bus, objPath), bus(bus)
Lei YU2f9c0cc2017-01-20 14:02:03 +080028{
Lei YU2f9c0cc2017-01-20 14:02:03 +080029}
30
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 YU415b9642017-02-09 11:37:26 +080036void EpochBase::onOwnerChanged(Owner owner)
Lei YU2f9c0cc2017-01-20 14:02:03 +080037{
Lei YU415b9642017-02-09 11:37:26 +080038 timeOwner = owner;
Lei YU2f9c0cc2017-01-20 14:02:03 +080039}
40
41using namespace std::chrono;
Lei YUfa024882017-11-09 10:49:26 +080042bool EpochBase::setTime(const microseconds& usec)
Lei YU2f9c0cc2017-01-20 14:02:03 +080043{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050044 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH,
45 SYSTEMD_TIME_INTERFACE, METHOD_SET_TIME);
Lei YU2f9c0cc2017-01-20 14:02:03 +080046 method.append(static_cast<int64_t>(usec.count()),
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050047 false, // relative
Lei YU2f9c0cc2017-01-20 14:02:03 +080048 false); // user_interaction
Lei YUbae8d7a2018-07-24 10:09:35 +080049
50 try
51 {
52 bus.call_noreply(method);
53 }
54 catch (const sdbusplus::exception::SdBusError& ex)
Lei YU1c2ce822017-08-01 17:37:41 +080055 {
56 log<level::ERR>("Error in setting system time");
Lei YUf6fad822018-07-13 16:35:45 +080057 using namespace xyz::openbmc_project::Time;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050058 elog<FailedError>(Failed::REASON(ex.what()));
Lei YU1c2ce822017-08-01 17:37:41 +080059 }
Lei YUfa024882017-11-09 10:49:26 +080060 return true;
Lei YU2f9c0cc2017-01-20 14:02:03 +080061}
62
63microseconds EpochBase::getTime() const
64{
65 auto now = system_clock::now();
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050066 return duration_cast<microseconds>(now.time_since_epoch());
Lei YU2f9c0cc2017-01-20 14:02:03 +080067}
68
69} // namespace time
70} // namespace phosphor