blob: e130578203ee135b64f7607e9fe858ac185d3491 [file] [log] [blame]
Lei YU2f9c0cc2017-01-20 14:02:03 +08001#include "epoch_base.hpp"
2
Lei YUf6fad822018-07-13 16:35:45 +08003#include <phosphor-logging/elog.hpp>
4#include <phosphor-logging/elog-errors.hpp>
Lei YU2f9c0cc2017-01-20 14:02:03 +08005#include <phosphor-logging/log.hpp>
Lei YUf6fad822018-07-13 16:35:45 +08006#include <xyz/openbmc_project/Time/error.hpp>
Lei YU2f9c0cc2017-01-20 14:02:03 +08007
8#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";
17}
18
Lei YU2f9c0cc2017-01-20 14:02:03 +080019namespace phosphor
20{
21namespace time
22{
23
24using namespace phosphor::logging;
Lei YUf6fad822018-07-13 16:35:45 +080025using FailedError =
26 sdbusplus::xyz::openbmc_project::Time::Error::Failed;
Lei YU2f9c0cc2017-01-20 14:02:03 +080027
Lei YU2f9c0cc2017-01-20 14:02:03 +080028EpochBase::EpochBase(sdbusplus::bus::bus& bus,
29 const char* objPath)
Lei YU415b9642017-02-09 11:37:26 +080030 : sdbusplus::server::object::object<EpochTime>(bus, objPath),
Lei YU2f9c0cc2017-01-20 14:02:03 +080031 bus(bus)
32{
Lei YU2f9c0cc2017-01-20 14:02:03 +080033}
34
Lei YU415b9642017-02-09 11:37:26 +080035void EpochBase::onModeChanged(Mode mode)
Lei YU2f9c0cc2017-01-20 14:02:03 +080036{
Lei YU415b9642017-02-09 11:37:26 +080037 timeMode = mode;
Lei YU2f9c0cc2017-01-20 14:02:03 +080038}
39
Lei YU415b9642017-02-09 11:37:26 +080040void EpochBase::onOwnerChanged(Owner owner)
Lei YU2f9c0cc2017-01-20 14:02:03 +080041{
Lei YU415b9642017-02-09 11:37:26 +080042 timeOwner = owner;
Lei YU2f9c0cc2017-01-20 14:02:03 +080043}
44
45using namespace std::chrono;
Lei YUfa024882017-11-09 10:49:26 +080046bool EpochBase::setTime(const microseconds& usec)
Lei YU2f9c0cc2017-01-20 14:02:03 +080047{
Lei YUdd8e9e42017-04-19 17:46:58 +080048 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE,
49 SYSTEMD_TIME_PATH,
50 SYSTEMD_TIME_INTERFACE,
51 METHOD_SET_TIME);
Lei YU2f9c0cc2017-01-20 14:02:03 +080052 method.append(static_cast<int64_t>(usec.count()),
53 false, // relative
54 false); // user_interaction
Lei YU1c2ce822017-08-01 17:37:41 +080055 auto reply = bus.call(method);
56 if (reply.is_method_error())
57 {
58 log<level::ERR>("Error in setting system time");
Lei YUf6fad822018-07-13 16:35:45 +080059 using namespace xyz::openbmc_project::Time;
60 elog<FailedError>(
61 Failed::REASON("Systemd failed to set time"));
Lei YU1c2ce822017-08-01 17:37:41 +080062 }
Lei YUfa024882017-11-09 10:49:26 +080063 return true;
Lei YU2f9c0cc2017-01-20 14:02:03 +080064}
65
66microseconds EpochBase::getTime() const
67{
68 auto now = system_clock::now();
69 return duration_cast<microseconds>
70 (now.time_since_epoch());
71}
72
73} // namespace time
74} // namespace phosphor