Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 1 | #include "epoch_base.hpp" |
| 2 | |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/elog.hpp> |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Time/error.hpp> |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 7 | |
| 8 | #include <iomanip> |
| 9 | #include <sstream> |
| 10 | |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 11 | namespace // anonymous |
| 12 | { |
| 13 | constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1"; |
| 14 | constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1"; |
| 15 | constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1"; |
| 16 | constexpr auto METHOD_SET_TIME = "SetTime"; |
| 17 | } |
| 18 | |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 19 | namespace phosphor |
| 20 | { |
| 21 | namespace time |
| 22 | { |
| 23 | |
| 24 | using namespace phosphor::logging; |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 25 | using FailedError = |
| 26 | sdbusplus::xyz::openbmc_project::Time::Error::Failed; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 27 | |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 28 | EpochBase::EpochBase(sdbusplus::bus::bus& bus, |
| 29 | const char* objPath) |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 30 | : sdbusplus::server::object::object<EpochTime>(bus, objPath), |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 31 | bus(bus) |
| 32 | { |
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::onModeChanged(Mode mode) |
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 | timeMode = mode; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 38 | } |
| 39 | |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 40 | void EpochBase::onOwnerChanged(Owner owner) |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 41 | { |
Lei YU | 415b964 | 2017-02-09 11:37:26 +0800 | [diff] [blame] | 42 | timeOwner = owner; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | using namespace std::chrono; |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 46 | bool EpochBase::setTime(const microseconds& usec) |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 47 | { |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 48 | auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, |
| 49 | SYSTEMD_TIME_PATH, |
| 50 | SYSTEMD_TIME_INTERFACE, |
| 51 | METHOD_SET_TIME); |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 52 | method.append(static_cast<int64_t>(usec.count()), |
| 53 | false, // relative |
| 54 | false); // user_interaction |
Lei YU | bae8d7a | 2018-07-24 10:09:35 +0800 | [diff] [blame] | 55 | |
| 56 | try |
| 57 | { |
| 58 | bus.call_noreply(method); |
| 59 | } |
| 60 | catch (const sdbusplus::exception::SdBusError& ex) |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 61 | { |
| 62 | log<level::ERR>("Error in setting system time"); |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 63 | using namespace xyz::openbmc_project::Time; |
| 64 | elog<FailedError>( |
Lei YU | bae8d7a | 2018-07-24 10:09:35 +0800 | [diff] [blame] | 65 | Failed::REASON(ex.what())); |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 66 | } |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 67 | return true; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | microseconds 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 |