xref: /openbmc/pldm/softoff/main.cpp (revision 3fcfaa12)
1*3fcfaa12SManojkiran Eda #include "common/instance_id.hpp"
2184f6026SChicago Duan #include "common/utils.hpp"
3184f6026SChicago Duan #include "softoff.hpp"
44c1a3fdaSGeorge Liu 
549cfb138SRiya Dixit #include <phosphor-logging/lg2.hpp>
649cfb138SRiya Dixit 
749cfb138SRiya Dixit PHOSPHOR_LOG2_USING;
849cfb138SRiya Dixit 
94c1a3fdaSGeorge Liu int main()
104c1a3fdaSGeorge Liu {
11184f6026SChicago Duan     // Get a default event loop
12184f6026SChicago Duan     auto event = sdeventplus::Event::get_default();
13184f6026SChicago Duan 
14184f6026SChicago Duan     // Get a handle to system D-Bus.
15184f6026SChicago Duan     auto& bus = pldm::utils::DBusHandler::getBus();
16184f6026SChicago Duan 
17*3fcfaa12SManojkiran Eda     // Obtain the instance database
18*3fcfaa12SManojkiran Eda     pldm::InstanceIdDb instanceIdDb;
19*3fcfaa12SManojkiran Eda 
20184f6026SChicago Duan     // Attach the bus to sd_event to service user requests
21184f6026SChicago Duan     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
22184f6026SChicago Duan 
23*3fcfaa12SManojkiran Eda     pldm::SoftPowerOff softPower(bus, event.get(), instanceIdDb);
24184f6026SChicago Duan 
25184f6026SChicago Duan     if (softPower.isError())
26184f6026SChicago Duan     {
2749cfb138SRiya Dixit         error(
2849cfb138SRiya Dixit             "Host failed to gracefully shutdown, exiting pldm-softpoweroff app");
29184f6026SChicago Duan         return -1;
30184f6026SChicago Duan     }
31184f6026SChicago Duan 
32184f6026SChicago Duan     if (softPower.isCompleted())
33184f6026SChicago Duan     {
3449cfb138SRiya Dixit         error(
3549cfb138SRiya Dixit             "Host current state is not Running, exiting pldm-softpoweroff app");
36184f6026SChicago Duan         return 0;
37184f6026SChicago Duan     }
38184f6026SChicago Duan 
39184f6026SChicago Duan     // Send the gracefully shutdown request to the host and
40184f6026SChicago Duan     // wait the host gracefully shutdown.
41184f6026SChicago Duan     if (softPower.hostSoftOff(event))
42184f6026SChicago Duan     {
4349cfb138SRiya Dixit         error(
4449cfb138SRiya Dixit             "pldm-softpoweroff:Failure in sending soft off request to the host. Exiting pldm-softpoweroff app");
45184f6026SChicago Duan         return -1;
46184f6026SChicago Duan     }
47184f6026SChicago Duan 
48184f6026SChicago Duan     if (softPower.isTimerExpired() && softPower.isReceiveResponse())
49184f6026SChicago Duan     {
50184f6026SChicago Duan         pldm::utils::reportError(
51184f6026SChicago Duan             "pldm soft off: Waiting for the host soft off timeout");
5249cfb138SRiya Dixit         error(
5349cfb138SRiya Dixit             "PLDM host soft off: ERROR! Wait for the host soft off timeout. Exit the pldm-softpoweroff");
54184f6026SChicago Duan         return -1;
55184f6026SChicago Duan     }
56184f6026SChicago Duan 
574c1a3fdaSGeorge Liu     return 0;
584c1a3fdaSGeorge Liu }
59