blob: 4d59ff981d34f2c24c065e50e6c42970b94bf6e9 [file] [log] [blame]
Carol Wang71230ef2020-02-18 17:39:49 +08001#include "scheduled_host_transition.hpp"
2
Carol Wang4ca6f3f2020-02-19 16:28:59 +08003#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/elog.hpp>
5#include <phosphor-logging/log.hpp>
6#include <xyz/openbmc_project/ScheduledTime/error.hpp>
7#include <chrono>
8
Carol Wang71230ef2020-02-18 17:39:49 +08009namespace phosphor
10{
11namespace state
12{
13namespace manager
14{
15
Carol Wang4ca6f3f2020-02-19 16:28:59 +080016using namespace std::chrono;
17using namespace phosphor::logging;
18using namespace xyz::openbmc_project::ScheduledTime;
19using InvalidTimeError =
20 sdbusplus::xyz::openbmc_project::ScheduledTime::Error::InvalidTime;
Carol Wang71230ef2020-02-18 17:39:49 +080021using HostTransition =
22 sdbusplus::xyz::openbmc_project::State::server::ScheduledHostTransition;
23
24uint64_t ScheduledHostTransition::scheduledTime(uint64_t value)
25{
Carol Wang4ca6f3f2020-02-19 16:28:59 +080026 if (value == 0)
27 {
28 // 0 means the function Scheduled Host Transition is disabled
29 // to do: check timer, stop timer
30 log<level::INFO>("The function Scheduled Host Transition is disabled.");
31 return HostTransition::scheduledTime(value);
32 }
33
34 auto deltaTime = seconds(value) - getTime();
35 if (deltaTime < seconds(0))
36 {
37 log<level::ERR>("Scheduled time is earlier than current time. Fail to "
38 "do host transition.");
39 elog<InvalidTimeError>(
40 InvalidTime::REASON("Scheduled time is in the past"));
41 }
42 else
43 {
44 // start timer
45 }
Carol Wang71230ef2020-02-18 17:39:49 +080046 return HostTransition::scheduledTime(value);
47}
48
Carol Wang4ca6f3f2020-02-19 16:28:59 +080049seconds ScheduledHostTransition::getTime()
50{
51 auto now = system_clock::now();
52 return duration_cast<seconds>(now.time_since_epoch());
53}
54
Carol Wang71230ef2020-02-18 17:39:49 +080055} // namespace manager
56} // namespace state
57} // namespace phosphor