blob: dc21c67ae0d673b2f9cb552576c5a5a10c1a559f [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;
41void EpochBase::setTime(const microseconds& usec)
42{
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 {
53 log<level::ERR>("Error in setting system time");
54 }
Lei YU2f9c0cc2017-01-20 14:02:03 +080055}
56
57microseconds EpochBase::getTime() const
58{
59 auto now = system_clock::now();
60 return duration_cast<microseconds>
61 (now.time_since_epoch());
62}
63
64} // namespace time
65} // namespace phosphor