summaryrefslogtreecommitdiffstats
path: root/host_epoch.cpp
blob: 96cbfa9d16c5ce3bf3b7c21b0d6bd3701e17652a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "host_epoch.hpp"

#include "utils.hpp"

#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/log.hpp>
#include <xyz/openbmc_project/Time/error.hpp>

namespace phosphor
{
namespace time
{
using namespace sdbusplus::xyz::openbmc_project::Time;
using namespace phosphor::logging;
using namespace std::chrono;

using NotAllowedError =
    sdbusplus::xyz::openbmc_project::Time::Error::NotAllowed;

HostEpoch::HostEpoch(sdbusplus::bus::bus& bus, const char* objPath) :
    EpochBase(bus, objPath),
    offset(utils::readData<decltype(offset)::rep>(offsetFile))
{
    // Initialize the diffToSteadyClock
    auto steadyTime =
        duration_cast<microseconds>(steady_clock::now().time_since_epoch());
    diffToSteadyClock = getTime() + offset - steadyTime;
}

uint64_t HostEpoch::elapsed() const
{
    auto ret = getTime();
    if (timeOwner == Owner::Split)
    {
        ret += offset;
    }
    return ret.count();
}

uint64_t HostEpoch::elapsed(uint64_t value)
{
    /*
        Mode  | Owner | Set Host Time
        ----- | ----- | -------------
        NTP   | BMC   | Not allowed
        NTP   | HOST  | Not allowed
        NTP   | SPLIT | OK, and just save offset
        NTP   | BOTH  | Not allowed
        MANUAL| BMC   | Not allowed
        MANUAL| HOST  | OK, and set time to BMC
        MANUAL| SPLIT | OK, and just save offset
        MANUAL| BOTH  | OK, and set time to BMC
    */
    if (timeOwner == Owner::BMC ||
        (timeMode == Mode::NTP &&
         (timeOwner == Owner::Host || timeOwner == Owner::Both)))
    {
        using namespace xyz::openbmc_project::Time;
        elog<NotAllowedError>(
            NotAllowed::OWNER(utils::ownerToStr(timeOwner).c_str()),
            NotAllowed::SYNC_METHOD(utils::modeToStr(timeMode).c_str()),
            NotAllowed::REASON("Setting HostTime is not allowed"));
    }

    auto time = microseconds(value);
    if (timeOwner == Owner::Split)
    {
        // Calculate the offset between host and bmc time
        offset = time - getTime();
        saveOffset();

        // Calculate the diff between host and steady time
        auto steadyTime =
            duration_cast<microseconds>(steady_clock::now().time_since_epoch());
        diffToSteadyClock = time - steadyTime;
    }
    else
    {
        // Set time to BMC
        setTime(time);
    }

    server::EpochTime::elapsed(value);
    return value;
}

void HostEpoch::onOwnerChanged(Owner owner)
{
    // If timeOwner is changed to SPLIT, the offset shall be preserved
    // Otherwise it shall be cleared;
    timeOwner = owner;
    if (timeOwner != Owner::Split)
    {
        offset = microseconds(0);
        saveOffset();
    }
    else
    {
        // In SPLIT, need to re-calculate the diff between
        // host and steady time
        auto steadyTime =
            duration_cast<microseconds>(steady_clock::now().time_since_epoch());
        diffToSteadyClock = getTime() - steadyTime;
    }
}

void HostEpoch::saveOffset()
{
    // Store the offset to file
    utils::writeData(offsetFile, offset.count());
}

void HostEpoch::onBmcTimeChanged(const microseconds& bmcTime)
{
    // If owner is split and BMC time is changed,
    // the offset shall be adjusted
    if (timeOwner == Owner::Split)
    {
        auto steadyTime =
            duration_cast<microseconds>(steady_clock::now().time_since_epoch());
        auto hostTime = steadyTime + diffToSteadyClock;
        offset = hostTime - bmcTime;

        saveOffset();
    }
}

} // namespace time
} // namespace phosphor
OpenPOWER on IntegriCloud