Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 1 | #include "host_epoch.hpp" |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 2 | #include "utils.hpp" |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 3 | |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace time |
| 9 | { |
| 10 | using namespace sdbusplus::xyz::openbmc_project::Time; |
| 11 | using namespace phosphor::logging; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 12 | using namespace std::chrono; |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 13 | |
| 14 | HostEpoch::HostEpoch(sdbusplus::bus::bus& bus, |
| 15 | const char* objPath) |
| 16 | : EpochBase(bus, objPath), |
Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 17 | offset(utils::readData<decltype(offset)::rep>(offsetFile)) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 18 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 19 | // Initialize the diffToSteadyClock |
| 20 | auto steadyTime = duration_cast<microseconds>( |
| 21 | steady_clock::now().time_since_epoch()); |
| 22 | diffToSteadyClock = getTime() + offset - steadyTime; |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | uint64_t HostEpoch::elapsed() const |
| 26 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 27 | auto ret = getTime(); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 28 | if (timeOwner == Owner::Split) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 29 | { |
| 30 | ret += offset; |
| 31 | } |
| 32 | return ret.count(); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | uint64_t HostEpoch::elapsed(uint64_t value) |
| 36 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 37 | /* |
| 38 | Mode | Owner | Set Host Time |
| 39 | ----- | ----- | ------------- |
| 40 | NTP | BMC | Not allowed |
| 41 | NTP | HOST | Not allowed |
| 42 | NTP | SPLIT | OK, and just save offset |
| 43 | NTP | BOTH | Not allowed |
| 44 | MANUAL| BMC | Not allowed |
| 45 | MANUAL| HOST | OK, and set time to BMC |
| 46 | MANUAL| SPLIT | OK, and just save offset |
| 47 | MANUAL| BOTH | OK, and set time to BMC |
| 48 | */ |
| 49 | if (timeOwner == Owner::BMC || |
| 50 | (timeMode == Mode::NTP |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 51 | && (timeOwner == Owner::Host || timeOwner == Owner::Both))) |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 52 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 53 | log<level::ERR>("Setting HostTime is not allowed"); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 54 | // TODO: throw NotAllowed exception |
| 55 | return 0; |
| 56 | } |
| 57 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 58 | auto time = microseconds(value); |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 59 | if (timeOwner == Owner::Split) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 60 | { |
| 61 | // Calculate the offset between host and bmc time |
| 62 | offset = time - getTime(); |
| 63 | saveOffset(); |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 64 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 65 | // Calculate the diff between host and steady time |
| 66 | auto steadyTime = duration_cast<microseconds>( |
| 67 | steady_clock::now().time_since_epoch()); |
| 68 | diffToSteadyClock = time - steadyTime; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | // Set time to BMC |
| 73 | setTime(time); |
| 74 | } |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 75 | |
| 76 | server::EpochTime::elapsed(value); |
| 77 | return value; |
| 78 | } |
| 79 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 80 | void HostEpoch::onOwnerChanged(Owner owner) |
| 81 | { |
| 82 | // If timeOwner is changed to SPLIT, the offset shall be preserved |
| 83 | // Otherwise it shall be cleared; |
| 84 | timeOwner = owner; |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 85 | if (timeOwner != Owner::Split) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 86 | { |
| 87 | offset = microseconds(0); |
| 88 | saveOffset(); |
| 89 | } |
Lei YU | 4726380 | 2017-11-08 17:30:04 +0800 | [diff] [blame^] | 90 | else |
| 91 | { |
| 92 | // In SPLIT, need to re-calculate the diff between |
| 93 | // host and steady time |
| 94 | auto steadyTime = duration_cast<microseconds>( |
| 95 | steady_clock::now().time_since_epoch()); |
| 96 | diffToSteadyClock = getTime() - steadyTime; |
| 97 | } |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void HostEpoch::saveOffset() |
| 101 | { |
| 102 | // Store the offset to file |
| 103 | utils::writeData(offsetFile, offset.count()); |
| 104 | } |
| 105 | |
| 106 | void HostEpoch::onBmcTimeChanged(const microseconds& bmcTime) |
| 107 | { |
| 108 | // If owner is split and BMC time is changed, |
| 109 | // the offset shall be adjusted |
Lei YU | ad14354 | 2017-07-25 14:27:07 +0800 | [diff] [blame] | 110 | if (timeOwner == Owner::Split) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 111 | { |
| 112 | auto steadyTime = duration_cast<microseconds>( |
| 113 | steady_clock::now().time_since_epoch()); |
| 114 | auto hostTime = steadyTime + diffToSteadyClock; |
| 115 | offset = hostTime - bmcTime; |
| 116 | |
| 117 | saveOffset(); |
| 118 | } |
| 119 | } |
| 120 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 121 | } // namespace time |
| 122 | } // namespace phosphor |
| 123 | |