Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 1 | #include "host_epoch.hpp" |
| 2 | |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | #include <fstream> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace time |
| 10 | { |
| 11 | using namespace sdbusplus::xyz::openbmc_project::Time; |
| 12 | using namespace phosphor::logging; |
| 13 | |
| 14 | HostEpoch::HostEpoch(sdbusplus::bus::bus& bus, |
| 15 | const char* objPath) |
| 16 | : EpochBase(bus, objPath), |
| 17 | offset(readData<decltype(offset)::rep>(offsetFile)) |
| 18 | { |
| 19 | // Empty |
| 20 | } |
| 21 | |
| 22 | uint64_t HostEpoch::elapsed() const |
| 23 | { |
| 24 | // It does not needs to check owner when getting time |
| 25 | return (getTime() + offset).count(); |
| 26 | } |
| 27 | |
| 28 | uint64_t HostEpoch::elapsed(uint64_t value) |
| 29 | { |
| 30 | if (timeOwner == Owner::BMC) |
| 31 | { |
| 32 | log<level::ERR>("Setting HostTime in BMC owner is not allowed"); |
| 33 | // TODO: throw NotAllowed exception |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | // TODO: implement the logic of setting host time |
| 38 | // based on timeOwner and timeMode |
| 39 | |
| 40 | auto time = std::chrono::microseconds(value); |
| 41 | offset = time - getTime(); |
| 42 | |
| 43 | // Store the offset to file |
| 44 | writeData(offsetFile, offset.count()); |
| 45 | |
| 46 | server::EpochTime::elapsed(value); |
| 47 | return value; |
| 48 | } |
| 49 | |
| 50 | template <typename T> |
| 51 | T HostEpoch::readData(const char* fileName) |
| 52 | { |
| 53 | T data{}; |
| 54 | std::ifstream fs(fileName); |
| 55 | if (fs.is_open()) |
| 56 | { |
| 57 | fs >> data; |
| 58 | } |
| 59 | return data; |
| 60 | } |
| 61 | |
| 62 | template <typename T> |
| 63 | void HostEpoch::writeData(const char* fileName, T&& data) |
| 64 | { |
| 65 | std::ofstream fs(fileName, std::ios::out); |
| 66 | if (fs.is_open()) |
| 67 | { |
| 68 | fs << std::forward<T>(data); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | } // namespace time |
| 73 | } // namespace phosphor |
| 74 | |