146470a38SPatrick Venture #include "globalhandler.hpp"
20b02be92SPatrick Venture 
3194375f2SWilliam A. Kennington III #include <ipmid/api.h>
40b02be92SPatrick Venture 
5*6a98fe7fSVernon Mauery #include <ipmid/utils.hpp>
60b02be92SPatrick Venture #include <phosphor-logging/elog-errors.hpp>
70b02be92SPatrick Venture #include <phosphor-logging/log.hpp>
834e3d3f1SNagaraju Goruganti #include <string>
90b02be92SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp>
100b02be92SPatrick Venture #include <xyz/openbmc_project/State/BMC/server.hpp>
110b02be92SPatrick Venture 
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 {
2434e3d3f1SNagaraju Goruganti     sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
2598a23840SMatthew Barth 
260b02be92SPatrick Venture     auto bmcStateObj =
270b02be92SPatrick Venture         ipmi::getDbusObject(bus, bmcStateIntf, bmcStateRoot, 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 
3634e3d3f1SNagaraju Goruganti ipmi_ret_t ipmi_global_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
3798a23840SMatthew Barth                              ipmi_request_t request, ipmi_response_t response,
3898a23840SMatthew Barth                              ipmi_data_len_t data_len, ipmi_context_t context)
3998a23840SMatthew Barth {
4034e3d3f1SNagaraju Goruganti     try
41bc759884SNan Li     {
4234e3d3f1SNagaraju Goruganti         resetBMC();
4334e3d3f1SNagaraju Goruganti     }
4434e3d3f1SNagaraju Goruganti     catch (std::exception& e)
4534e3d3f1SNagaraju Goruganti     {
4634e3d3f1SNagaraju Goruganti         log<level::ERR>(e.what());
4734e3d3f1SNagaraju Goruganti         return IPMI_CC_UNSPECIFIED_ERROR;
4834e3d3f1SNagaraju Goruganti     }
49bc759884SNan Li 
50bc759884SNan Li     // Status code.
51bc759884SNan Li     ipmi_ret_t rc = IPMI_CC_OK;
52bc759884SNan Li     *data_len = 0;
53bc759884SNan Li     return rc;
54bc759884SNan Li }
5598a23840SMatthew Barth 
5698a23840SMatthew Barth void register_netfn_global_functions()
5798a23840SMatthew Barth {
580573237fSTom     // Cold Reset
5934e3d3f1SNagaraju Goruganti     ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL,
6034e3d3f1SNagaraju Goruganti                            ipmi_global_reset, PRIVILEGE_ADMIN);
61bc759884SNan Li 
620573237fSTom     // <Warm Reset>
6334e3d3f1SNagaraju Goruganti     ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL,
6434e3d3f1SNagaraju Goruganti                            ipmi_global_reset, PRIVILEGE_ADMIN);
6598a23840SMatthew Barth 
6698a23840SMatthew Barth     return;
6798a23840SMatthew Barth }
68