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