blob: 09337de5c63831dac524fc4b65710de1248ebd14 [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
50 bus.call_noreply(method);
51}
52
53microseconds EpochBase::getTime() const
54{
55 auto now = system_clock::now();
56 return duration_cast<microseconds>
57 (now.time_since_epoch());
58}
59
60} // namespace time
61} // namespace phosphor