Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #include "bmc_epoch.hpp" |
| 2 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 3 | #include "utils.hpp" |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 4 | |
| 5 | #include <sys/timerfd.h> |
| 6 | #include <unistd.h> |
| 7 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/elog.hpp> |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Time/error.hpp> |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 13 | |
Andrew Geissler | 714a20b | 2023-07-24 16:58:27 -0400 | [diff] [blame] | 14 | #include <chrono> |
| 15 | |
Gunnar Mills | 75710b6 | 2018-04-08 14:50:11 -0500 | [diff] [blame] | 16 | // Need to do this since its not exported outside of the kernel. |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 17 | // Refer : https://gist.github.com/lethean/446cea944b7441228298 |
| 18 | #ifndef TFD_TIMER_CANCEL_ON_SET |
| 19 | #define TFD_TIMER_CANCEL_ON_SET (1 << 1) |
| 20 | #endif |
| 21 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace time |
| 25 | { |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 26 | namespace // anonymous |
| 27 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 28 | constexpr auto systemdTimeService = "org.freedesktop.timedate1"; |
| 29 | constexpr auto systemdTimePath = "/org/freedesktop/timedate1"; |
| 30 | constexpr auto systemdTimeInterface = "org.freedesktop.timedate1"; |
| 31 | constexpr auto methodSetTime = "SetTime"; |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 32 | } // namespace |
| 33 | |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 34 | PHOSPHOR_LOG2_USING; |
| 35 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 36 | namespace server = sdbusplus::xyz::openbmc_project::Time::server; |
| 37 | using namespace phosphor::logging; |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 38 | using FailedError = sdbusplus::xyz::openbmc_project::Time::Error::Failed; |
Andrew Geissler | 714a20b | 2023-07-24 16:58:27 -0400 | [diff] [blame] | 39 | using namespace std::chrono; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 40 | |
| 41 | void BmcEpoch::initialize() |
| 42 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 43 | using InternalFailure = |
| 44 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 45 | |
| 46 | // Subscribe time change event |
| 47 | // Choose the MAX time that is possible to avoid mis fires. |
| 48 | constexpr itimerspec maxTime = { |
Andrew Geissler | 714a20b | 2023-07-24 16:58:27 -0400 | [diff] [blame] | 49 | {0, 0}, // it_interval |
| 50 | {system_clock::duration::max().count(), 0}, // it_value |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 54 | if (timeFd == -1) |
| 55 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 56 | error("Failed to create timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 57 | elog<InternalFailure>(); |
| 58 | } |
| 59 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 60 | auto r = timerfd_settime( |
| 61 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 62 | if (r != 0) |
| 63 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 64 | error("Failed to set timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 65 | elog<InternalFailure>(); |
| 66 | } |
| 67 | |
Pavithra Barithaya | 8af2a1e | 2023-04-27 00:46:35 -0500 | [diff] [blame] | 68 | sd_event_source* es = nullptr; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 69 | r = sd_event_add_io(bus.get_event(), &es, timeFd, EPOLLIN, onTimeChange, |
| 70 | this); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 71 | if (r < 0) |
| 72 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 73 | error("Failed to add event: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 74 | elog<InternalFailure>(); |
| 75 | } |
| 76 | timeChangeEventSource.reset(es); |
| 77 | } |
| 78 | |
| 79 | BmcEpoch::~BmcEpoch() |
| 80 | { |
| 81 | close(timeFd); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | uint64_t BmcEpoch::elapsed() const |
| 85 | { |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 86 | return getTime().count(); |
| 87 | } |
| 88 | |
| 89 | uint64_t BmcEpoch::elapsed(uint64_t value) |
| 90 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 91 | /* |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 92 | Mode | Set BMC Time |
| 93 | ----- | ------------- |
| 94 | NTP | Fail to set |
| 95 | MANUAL| OK |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 96 | */ |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 97 | auto time = microseconds(value); |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 98 | setTime(time); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 99 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 100 | server::EpochTime::elapsed(value); |
| 101 | return value; |
| 102 | } |
| 103 | |
Ratan Gupta | 1e1dc44 | 2021-02-02 05:51:08 -0600 | [diff] [blame] | 104 | int BmcEpoch::onTimeChange(sd_event_source* /* es */, int fd, |
| 105 | uint32_t /* revents */, void* /* userdata */) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 106 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 107 | std::array<char, 64> time{}; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 108 | |
| 109 | // We are not interested in the data here. |
| 110 | // So read until there is no new data here in the FD |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 111 | while (read(fd, time.data(), time.max_size()) > 0) |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 112 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 113 | ; |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 114 | } |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 115 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 119 | void BmcEpoch::onModeChanged(Mode mode) |
| 120 | { |
| 121 | manager.setTimeMode(mode); |
| 122 | } |
| 123 | |
| 124 | bool BmcEpoch::setTime(const microseconds& usec) |
| 125 | { |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 126 | auto method = bus.new_method_call(systemdTimeService, systemdTimePath, |
| 127 | systemdTimeInterface, methodSetTime); |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 128 | method.append(static_cast<int64_t>(usec.count()), |
| 129 | false, // relative |
| 130 | false); // user_interaction |
| 131 | |
| 132 | try |
| 133 | { |
| 134 | bus.call_noreply(method); |
| 135 | } |
| 136 | catch (const sdbusplus::exception_t& ex) |
| 137 | { |
Pavithra Barithaya | dd42c7f | 2022-08-11 05:09:02 -0500 | [diff] [blame] | 138 | error("Error in setting system time: {ERROR}", "ERROR", ex); |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 139 | using namespace xyz::openbmc_project::Time; |
| 140 | elog<FailedError>(Failed::REASON(ex.what())); |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
Pavithra Barithaya | 864e173 | 2023-04-11 04:30:23 -0500 | [diff] [blame] | 145 | microseconds BmcEpoch::getTime() |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 146 | { |
| 147 | auto now = system_clock::now(); |
| 148 | return duration_cast<microseconds>(now.time_since_epoch()); |
| 149 | } |
| 150 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 151 | } // namespace time |
| 152 | } // namespace phosphor |