146470a38SPatrick Venture #include "globalhandler.hpp"
20b02be92SPatrick Venture 
3e08fbffcSVernon Mauery #include <ipmid/api.hpp>
46a98fe7fSVernon Mauery #include <ipmid/utils.hpp>
50b02be92SPatrick Venture #include <phosphor-logging/elog-errors.hpp>
60b02be92SPatrick Venture #include <phosphor-logging/log.hpp>
70b02be92SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp>
80b02be92SPatrick Venture #include <xyz/openbmc_project/State/BMC/server.hpp>
90b02be92SPatrick Venture 
10*fbc6c9d7SPatrick Williams #include <string>
11*fbc6c9d7SPatrick Williams 
1234e3d3f1SNagaraju Goruganti static constexpr auto bmcStateRoot = "/xyz/openbmc_project/state";
1334e3d3f1SNagaraju Goruganti static constexpr auto bmcStateIntf = "xyz.openbmc_project.State.BMC";
1434e3d3f1SNagaraju Goruganti static constexpr auto reqTransition = "RequestedBMCTransition";
1534e3d3f1SNagaraju Goruganti static constexpr auto match = "bmc0";
1634e3d3f1SNagaraju Goruganti 
1734e3d3f1SNagaraju Goruganti using namespace phosphor::logging;
1834e3d3f1SNagaraju Goruganti using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
1998a23840SMatthew Barth 
2098a23840SMatthew Barth void register_netfn_global_functions() __attribute__((constructor));
2198a23840SMatthew Barth 
2234e3d3f1SNagaraju Goruganti void resetBMC()
2398a23840SMatthew Barth {
245d82f474SPatrick Williams     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
2598a23840SMatthew Barth 
26*fbc6c9d7SPatrick Williams     auto bmcStateObj = ipmi::getDbusObject(bus, bmcStateIntf, bmcStateRoot,
27*fbc6c9d7SPatrick Williams                                            match);
2834e3d3f1SNagaraju Goruganti 
2934e3d3f1SNagaraju Goruganti     auto service = ipmi::getService(bus, bmcStateIntf, bmcStateObj.first);
3034e3d3f1SNagaraju Goruganti 
3134e3d3f1SNagaraju Goruganti     ipmi::setDbusProperty(bus, service, bmcStateObj.first, bmcStateIntf,
320b02be92SPatrick Venture                           reqTransition,
330b02be92SPatrick Venture                           convertForMessage(BMC::Transition::Reboot));
3498a23840SMatthew Barth }
3598a23840SMatthew Barth 
36e9e99d93Sanil kumar appana /** @brief implements cold and warm reset commands
37e9e99d93Sanil kumar appana  *  @param - None
38e9e99d93Sanil kumar appana  *  @returns IPMI completion code.
39e9e99d93Sanil kumar appana  */
40e9e99d93Sanil kumar appana ipmi::RspType<> ipmiGlobalReset()
4198a23840SMatthew Barth {
4234e3d3f1SNagaraju Goruganti     try
43bc759884SNan Li     {
4434e3d3f1SNagaraju Goruganti         resetBMC();
4534e3d3f1SNagaraju Goruganti     }
46a2ad2da8SPatrick Williams     catch (const std::exception& e)
4734e3d3f1SNagaraju Goruganti     {
4834e3d3f1SNagaraju Goruganti         log<level::ERR>(e.what());
49e9e99d93Sanil kumar appana         return ipmi::responseUnspecifiedError();
5034e3d3f1SNagaraju Goruganti     }
51bc759884SNan Li 
52bc759884SNan Li     // Status code.
53e9e99d93Sanil kumar appana     return ipmi::responseSuccess();
54bc759884SNan Li }
5598a23840SMatthew Barth 
5698a23840SMatthew Barth void register_netfn_global_functions()
5798a23840SMatthew Barth {
580573237fSTom     // Cold Reset
59e9e99d93Sanil kumar appana     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
60e9e99d93Sanil kumar appana                           ipmi::app::cmdColdReset, ipmi::Privilege::Admin,
61e9e99d93Sanil kumar appana                           ipmiGlobalReset);
6298a23840SMatthew Barth     return;
6398a23840SMatthew Barth }
64