blob: 8aa41dd5cdb2a18a3e1f5d6ff57ed9862d7a1242 [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 YUbae8d7a2018-07-24 10:09:35 +080055
56 try
57 {
58 bus.call_noreply(method);
59 }
60 catch (const sdbusplus::exception::SdBusError& ex)
Lei YU1c2ce822017-08-01 17:37:41 +080061 {
62 log<level::ERR>("Error in setting system time");
Lei YUf6fad822018-07-13 16:35:45 +080063 using namespace xyz::openbmc_project::Time;
64 elog<FailedError>(
Lei YUbae8d7a2018-07-24 10:09:35 +080065 Failed::REASON(ex.what()));
Lei YU1c2ce822017-08-01 17:37:41 +080066 }
Lei YUfa024882017-11-09 10:49:26 +080067 return true;
Lei YU2f9c0cc2017-01-20 14:02:03 +080068}
69
70microseconds EpochBase::getTime() const
71{
72 auto now = system_clock::now();
73 return duration_cast<microseconds>
74 (now.time_since_epoch());
75}
76
77} // namespace time
78} // namespace phosphor