1 #include "config.h"
2 
3 #include "hypervisor_state_manager.hpp"
4 
5 #include <phosphor-logging/elog-errors.hpp>
6 #include <phosphor-logging/lg2.hpp>
7 #include <sdbusplus/exception.hpp>
8 #include <sdbusplus/server.hpp>
9 
10 #include <fstream>
11 #include <iostream>
12 #include <map>
13 #include <string>
14 
15 namespace phosphor
16 {
17 namespace state
18 {
19 namespace manager
20 {
21 
22 PHOSPHOR_LOG2_USING;
23 
24 // When you see server:: you know we're referencing our base class
25 namespace server = sdbusplus::xyz::openbmc_project::State::server;
26 using namespace phosphor::logging;
27 
28 server::Host::Transition Hypervisor::requestedHostTransition(Transition value)
29 {
30     info("Hypervisor state transition request of {TRAN_REQUEST}",
31          "TRAN_REQUEST", value);
32 
33     // Only support the transition to On
34     if (value != server::Host::Transition::On)
35     {
36         error("Hypervisor state only supports a transition to On");
37         // 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 
47 server::Host::HostState Hypervisor::currentHostState(HostState value)
48 {
49     info("Change to Hypervisor State: {HYP_STATE}", "HYP_STATE", value);
50     return server::Host::currentHostState(value);
51 }
52 
53 server::Host::HostState Hypervisor::currentHostState()
54 {
55     return server::Host::currentHostState();
56 }
57 
58 void Hypervisor::updateCurrentHostState(std::string& bootProgress)
59 {
60     debug("New BootProgress: {BOOTPROGRESS}", "BOOTPROGRESS", bootProgress);
61 
62     if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
63                         "ProgressStages.SystemInitComplete")
64     {
65         currentHostState(server::Host::HostState::Standby);
66     }
67     else if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
68                              "ProgressStages.OSStart")
69     {
70         currentHostState(server::Host::HostState::TransitioningToRunning);
71     }
72     else if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
73                              "ProgressStages.OSRunning")
74     {
75         currentHostState(server::Host::HostState::Running);
76     }
77     else if (bootProgress == "xyz.openbmc_project.State.Boot.Progress."
78                              "ProgressStages.Unspecified")
79     {
80         // Unspecified is set when the system is powered off so
81         // set the state to off and reset the requested host state
82         // back to its default
83         currentHostState(server::Host::HostState::Off);
84         server::Host::requestedHostTransition(server::Host::Transition::Off);
85     }
86     else
87     {
88         // BootProgress changed and it is not one of the above so
89         // set hypervisor state to off
90         currentHostState(server::Host::HostState::Off);
91     }
92 }
93 
94 void Hypervisor::bootProgressChangeEvent(sdbusplus::message::message& msg)
95 {
96     std::string statusInterface;
97     std::map<std::string, std::variant<std::string>> msgData;
98     msg.read(statusInterface, msgData);
99 
100     auto propertyMap = msgData.find("BootProgress");
101     if (propertyMap != msgData.end())
102     {
103         // Extract the BootProgress
104         auto& bootProgress = std::get<std::string>(propertyMap->second);
105         updateCurrentHostState(bootProgress);
106     }
107 }
108 
109 } // namespace manager
110 } // namespace state
111 } // namespace phosphor
112