Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 1 | #include "epoch_base.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | #include <iomanip> |
| 6 | #include <sstream> |
| 7 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 8 | namespace // anonymous |
| 9 | { |
| 10 | constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1"; |
| 11 | constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1"; |
| 12 | constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1"; |
| 13 | constexpr auto METHOD_SET_TIME = "SetTime"; |
| 14 | } |
| 15 | |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace time |
| 19 | { |
| 20 | |
| 21 | using namespace phosphor::logging; |
| 22 | |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 23 | EpochBase::EpochBase(sdbusplus::bus::bus& bus, |
| 24 | const char* objPath) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 25 | : sdbusplus::server::object::object<EpochTime>(bus, objPath), |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 26 | bus(bus) |
| 27 | { |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 28 | } |
| 29 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 30 | void EpochBase::onModeChanged(Mode mode) |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 31 | { |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 32 | timeMode = mode; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 33 | } |
| 34 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 35 | void EpochBase::onOwnerChanged(Owner owner) |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 36 | { |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 37 | timeOwner = owner; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | using namespace std::chrono; |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 41 | bool EpochBase::setTime(const microseconds& usec) |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 42 | { |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 43 | auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, |
| 44 | SYSTEMD_TIME_PATH, |
| 45 | SYSTEMD_TIME_INTERFACE, |
| 46 | METHOD_SET_TIME); |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 47 | method.append(static_cast<int64_t>(usec.count()), |
| 48 | false, // relative |
| 49 | false); // user_interaction |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 50 | auto reply = bus.call(method); |
| 51 | if (reply.is_method_error()) |
| 52 | { |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 53 | // TODO: When sdbus supports exception on property |
| 54 | // it can just throw exception instead of returning bool |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 55 | log<level::ERR>("Error in setting system time"); |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 56 | return false; |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 57 | } |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 58 | return true; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | microseconds 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 |