1*184f6026SChicago Duan #include "common/utils.hpp" 2*184f6026SChicago Duan #include "softoff.hpp" 34c1a3fdaSGeorge Liu 44c1a3fdaSGeorge Liu #include <iostream> 54c1a3fdaSGeorge Liu 64c1a3fdaSGeorge Liu int main() 74c1a3fdaSGeorge Liu { 8*184f6026SChicago Duan // Get a default event loop 9*184f6026SChicago Duan auto event = sdeventplus::Event::get_default(); 10*184f6026SChicago Duan 11*184f6026SChicago Duan // Get a handle to system D-Bus. 12*184f6026SChicago Duan auto& bus = pldm::utils::DBusHandler::getBus(); 13*184f6026SChicago Duan 14*184f6026SChicago Duan // Attach the bus to sd_event to service user requests 15*184f6026SChicago Duan bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 16*184f6026SChicago Duan 17*184f6026SChicago Duan pldm::SoftPowerOff softPower(bus, event.get()); 18*184f6026SChicago Duan 19*184f6026SChicago Duan if (softPower.isError()) 20*184f6026SChicago Duan { 21*184f6026SChicago Duan std::cerr << "Host failed to gracefully shutdown, exiting " 22*184f6026SChicago Duan "pldm-softpoweroff app\n"; 23*184f6026SChicago Duan return -1; 24*184f6026SChicago Duan } 25*184f6026SChicago Duan 26*184f6026SChicago Duan if (softPower.isCompleted()) 27*184f6026SChicago Duan { 28*184f6026SChicago Duan std::cerr << "Host current state is not Running, exiting " 29*184f6026SChicago Duan "pldm-softpoweroff app\n"; 30*184f6026SChicago Duan return 0; 31*184f6026SChicago Duan } 32*184f6026SChicago Duan 33*184f6026SChicago Duan // Send the gracefully shutdown request to the host and 34*184f6026SChicago Duan // wait the host gracefully shutdown. 35*184f6026SChicago Duan if (softPower.hostSoftOff(event)) 36*184f6026SChicago Duan { 37*184f6026SChicago Duan std::cerr << "pldm-softpoweroff:Failure in sending soft off request to " 38*184f6026SChicago Duan "the host. Exiting pldm-softpoweroff app\n"; 39*184f6026SChicago Duan 40*184f6026SChicago Duan return -1; 41*184f6026SChicago Duan } 42*184f6026SChicago Duan 43*184f6026SChicago Duan if (softPower.isTimerExpired() && softPower.isReceiveResponse()) 44*184f6026SChicago Duan { 45*184f6026SChicago Duan pldm::utils::reportError( 46*184f6026SChicago Duan "pldm soft off: Waiting for the host soft off timeout"); 47*184f6026SChicago Duan std::cerr 48*184f6026SChicago Duan << "PLDM host soft off: ERROR! Wait for the host soft off timeout." 49*184f6026SChicago Duan << "Exit the pldm-softpoweroff " 50*184f6026SChicago Duan << "\n"; 51*184f6026SChicago Duan return -1; 52*184f6026SChicago Duan } 53*184f6026SChicago Duan 544c1a3fdaSGeorge Liu return 0; 554c1a3fdaSGeorge Liu } 56