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(); |
| 28 | if (timeOwner == Owner::SPLIT) |
| 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 |
| 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); |
| 59 | if (timeOwner == Owner::SPLIT) |
| 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; |
| 85 | if (timeOwner != Owner::SPLIT) |
| 86 | { |
| 87 | offset = microseconds(0); |
| 88 | saveOffset(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void HostEpoch::saveOffset() |
| 93 | { |
| 94 | // Store the offset to file |
| 95 | utils::writeData(offsetFile, offset.count()); |
| 96 | } |
| 97 | |
| 98 | void HostEpoch::onBmcTimeChanged(const microseconds& bmcTime) |
| 99 | { |
| 100 | // If owner is split and BMC time is changed, |
| 101 | // the offset shall be adjusted |
| 102 | if (timeOwner == Owner::SPLIT) |
| 103 | { |
| 104 | auto steadyTime = duration_cast<microseconds>( |
| 105 | steady_clock::now().time_since_epoch()); |
| 106 | auto hostTime = steadyTime + diffToSteadyClock; |
| 107 | offset = hostTime - bmcTime; |
| 108 | |
| 109 | saveOffset(); |
| 110 | } |
| 111 | } |
| 112 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 113 | } // namespace time |
| 114 | } // namespace phosphor |
| 115 | |