blob: d1588180e693cdd911e0d2434fc58aafc6ef16f1 [file] [log] [blame]
Andrew Geisslerfe270d32021-01-27 14:06:46 -06001#include "config.h"
2
3#include "hypervisor_state_manager.hpp"
4
Andrew Geisslerfe270d32021-01-27 14:06:46 -06005#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -05006#include <phosphor-logging/lg2.hpp>
Andrew Geisslerfe270d32021-01-27 14:06:46 -06007#include <sdbusplus/exception.hpp>
8#include <sdbusplus/server.hpp>
9
10#include <fstream>
11#include <iostream>
12#include <map>
13#include <string>
14
15namespace phosphor
16{
17namespace state
18{
19namespace manager
20{
21
Andrew Geissler8ffdb262021-09-20 15:25:19 -050022PHOSPHOR_LOG2_USING;
23
Andrew Geisslerfe270d32021-01-27 14:06:46 -060024// When you see server:: you know we're referencing our base class
25namespace server = sdbusplus::xyz::openbmc_project::State::server;
26using namespace phosphor::logging;
Andrew Geisslerfe270d32021-01-27 14:06:46 -060027
28server::Host::Transition Hypervisor::requestedHostTransition(Transition value)
29{
Andrew Geissler8ffdb262021-09-20 15:25:19 -050030 info("Hypervisor state transition request of {TRAN_REQUEST}",
31 "TRAN_REQUEST", value);
Andrew Geisslerfe270d32021-01-27 14:06:46 -060032
33 // Only support the transition to On
34 if (value != server::Host::Transition::On)
35 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050036 error("Hypervisor state only supports a transition to On");
Andrew Geisslerfe270d32021-01-27 14:06:46 -060037 // TODO raise appropriate error exception
38 return server::Host::Transition::Off;
39 }
40
41 // This property is monitored by a separate application (for example PLDM)
42 // which is responsible for propagating the On request to the hypervisor
43
44 return server::Host::requestedHostTransition(value);
45}
46
Andrew Geisslerfe270d32021-01-27 14:06:46 -060047server::Host::HostState Hypervisor::currentHostState(HostState value)
48{
Andrew Geissler9f444d42023-02-03 12:40:55 -070049 // Only log a message if this has changed since last
50 if (value != server::Host::currentHostState())
51 {
52 info("Change to Hypervisor State: {HYP_STATE}", "HYP_STATE", value);
53 }
Andrew Geisslerfe270d32021-01-27 14:06:46 -060054 return server::Host::currentHostState(value);
55}
56
Andrew Geisslerc74716e2021-02-09 15:11:10 -060057server::Host::HostState Hypervisor::currentHostState()
58{
59 return server::Host::currentHostState();
60}
61
62void Hypervisor::updateCurrentHostState(std::string& bootProgress)
63{
Andrew Geissler8ffdb262021-09-20 15:25:19 -050064 debug("New BootProgress: {BOOTPROGRESS}", "BOOTPROGRESS", bootProgress);
Andrew Geisslerc74716e2021-02-09 15:11:10 -060065
66 if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
67 "ProgressStages.SystemInitComplete")
68 {
69 currentHostState(server::Host::HostState::Standby);
70 }
71 else if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
72 "ProgressStages.OSStart")
73 {
74 currentHostState(server::Host::HostState::TransitioningToRunning);
75 }
76 else if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
77 "ProgressStages.OSRunning")
78 {
79 currentHostState(server::Host::HostState::Running);
80 }
Andrew Geissler75f38ee2021-02-10 13:50:21 -060081 else if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
82 "ProgressStages.Unspecified")
83 {
84 // Unspecified is set when the system is powered off so
85 // set the state to off and reset the requested host state
86 // back to its default
87 currentHostState(server::Host::HostState::Off);
88 server::Host::requestedHostTransition(server::Host::Transition::Off);
89 }
Andrew Geisslerc74716e2021-02-09 15:11:10 -060090 else
91 {
92 // BootProgress changed and it is not one of the above so
93 // set hypervisor state to off
94 currentHostState(server::Host::HostState::Off);
95 }
96}
97
Patrick Williamsf053e6f2022-07-22 19:26:54 -050098void Hypervisor::bootProgressChangeEvent(sdbusplus::message_t& msg)
Andrew Geisslerc74716e2021-02-09 15:11:10 -060099{
100 std::string statusInterface;
101 std::map<std::string, std::variant<std::string>> msgData;
102 msg.read(statusInterface, msgData);
103
104 auto propertyMap = msgData.find("BootProgress");
105 if (propertyMap != msgData.end())
106 {
107 // Extract the BootProgress
108 auto& bootProgress = std::get<std::string>(propertyMap->second);
109 updateCurrentHostState(bootProgress);
110 }
111}
112
Andrew Geisslerfe270d32021-01-27 14:06:46 -0600113} // namespace manager
114} // namespace state
115} // namespace phosphor