blob: f45581dad5c9d55966d21efeb2f4dbe33d5b2596 [file] [log] [blame]
Lei YUaf5abc52017-03-07 17:49:17 +08001#include "host_epoch.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5#include <fstream>
6
7namespace phosphor
8{
9namespace time
10{
11using namespace sdbusplus::xyz::openbmc_project::Time;
12using namespace phosphor::logging;
13
14HostEpoch::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
22uint64_t HostEpoch::elapsed() const
23{
24 // It does not needs to check owner when getting time
25 return (getTime() + offset).count();
26}
27
28uint64_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
50template <typename T>
51T 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
62template <typename T>
63void 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