blob: bab5d3ba709fa0eece6ddadddcf063b3a0c29429 [file] [log] [blame]
Lei YU2f9c0cc2017-01-20 14:02:03 +08001#include "epoch_base.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5#include <iomanip>
6#include <sstream>
7
Lei YUdd8e9e42017-04-19 17:46:58 +08008namespace // anonymous
9{
10constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1";
11constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1";
12constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1";
13constexpr auto METHOD_SET_TIME = "SetTime";
14}
15
Lei YU2f9c0cc2017-01-20 14:02:03 +080016namespace phosphor
17{
18namespace time
19{
20
21using namespace phosphor::logging;
22
Lei YU2f9c0cc2017-01-20 14:02:03 +080023EpochBase::EpochBase(sdbusplus::bus::bus& bus,
24 const char* objPath)
Lei YU415b9642017-02-09 11:37:26 +080025 : sdbusplus::server::object::object<EpochTime>(bus, objPath),
Lei YU2f9c0cc2017-01-20 14:02:03 +080026 bus(bus)
27{
Lei YU2f9c0cc2017-01-20 14:02:03 +080028}
29
Lei YU415b9642017-02-09 11:37:26 +080030void EpochBase::onModeChanged(Mode mode)
Lei YU2f9c0cc2017-01-20 14:02:03 +080031{
Lei YU415b9642017-02-09 11:37:26 +080032 timeMode = mode;
Lei YU2f9c0cc2017-01-20 14:02:03 +080033}
34
Lei YU415b9642017-02-09 11:37:26 +080035void EpochBase::onOwnerChanged(Owner owner)
Lei YU2f9c0cc2017-01-20 14:02:03 +080036{
Lei YU415b9642017-02-09 11:37:26 +080037 timeOwner = owner;
Lei YU2f9c0cc2017-01-20 14:02:03 +080038}
39
40using namespace std::chrono;
Lei YUfa024882017-11-09 10:49:26 +080041bool EpochBase::setTime(const microseconds& usec)
Lei YU2f9c0cc2017-01-20 14:02:03 +080042{
Lei YUdd8e9e42017-04-19 17:46:58 +080043 auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE,
44 SYSTEMD_TIME_PATH,
45 SYSTEMD_TIME_INTERFACE,
46 METHOD_SET_TIME);
Lei YU2f9c0cc2017-01-20 14:02:03 +080047 method.append(static_cast<int64_t>(usec.count()),
48 false, // relative
49 false); // user_interaction
Lei YU1c2ce822017-08-01 17:37:41 +080050 auto reply = bus.call(method);
51 if (reply.is_method_error())
52 {
Lei YUfa024882017-11-09 10:49:26 +080053 // TODO: When sdbus supports exception on property
54 // it can just throw exception instead of returning bool
Lei YU1c2ce822017-08-01 17:37:41 +080055 log<level::ERR>("Error in setting system time");
Lei YUfa024882017-11-09 10:49:26 +080056 return false;
Lei YU1c2ce822017-08-01 17:37:41 +080057 }
Lei YUfa024882017-11-09 10:49:26 +080058 return true;
Lei YU2f9c0cc2017-01-20 14:02:03 +080059}
60
61microseconds EpochBase::getTime() const
62{
63 auto now = system_clock::now();
64 return duration_cast<microseconds>
65 (now.time_since_epoch());
66}
67
68} // namespace time
69} // namespace phosphor