Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 1 | #include "epoch_base.hpp" |
| 2 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Time/error.hpp> |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 7 | |
George Liu | c6d3397 | 2020-06-22 10:35:29 +0800 | [diff] [blame] | 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"; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 17 | } // namespace |
Lei YU | dd8e9e4 | 2017-04-19 17:46:58 +0800 | [diff] [blame] | 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; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 25 | using FailedError = sdbusplus::xyz::openbmc_project::Time::Error::Failed; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 26 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 27 | EpochBase::EpochBase(sdbusplus::bus_t& bus, const char* objPath) : |
| 28 | sdbusplus::server::object_t<EpochTime>(bus, objPath), bus(bus) |
George Liu | c6d3397 | 2020-06-22 10:35:29 +0800 | [diff] [blame] | 29 | {} |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 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 | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 36 | using namespace std::chrono; |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 37 | bool EpochBase::setTime(const microseconds& usec) |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 38 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 39 | auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH, |
| 40 | SYSTEMD_TIME_INTERFACE, METHOD_SET_TIME); |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 41 | method.append(static_cast<int64_t>(usec.count()), |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 42 | false, // relative |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 43 | false); // user_interaction |
Lei YU | bae8d7a | 2018-07-24 10:09:35 +0800 | [diff] [blame] | 44 | |
| 45 | try |
| 46 | { |
| 47 | bus.call_noreply(method); |
| 48 | } |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame^] | 49 | catch (const sdbusplus::exception_t& ex) |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 50 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 51 | lg2::error("Error in setting system time: {ERROR}", "ERROR", ex); |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 52 | using namespace xyz::openbmc_project::Time; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 53 | elog<FailedError>(Failed::REASON(ex.what())); |
Lei YU | 1c2ce82 | 2017-08-01 17:37:41 +0800 | [diff] [blame] | 54 | } |
Lei YU | fa02488 | 2017-11-09 10:49:26 +0800 | [diff] [blame] | 55 | return true; |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | microseconds EpochBase::getTime() const |
| 59 | { |
| 60 | auto now = system_clock::now(); |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 61 | return duration_cast<microseconds>(now.time_since_epoch()); |
Lei YU | 2f9c0cc | 2017-01-20 14:02:03 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace time |
| 65 | } // namespace phosphor |