198a23840SMatthew Barth #include "globalhandler.h"
237af7331SPatrick Williams #include "host-ipmid/ipmid-api.h"
398a23840SMatthew Barth #include <stdio.h>
4*34e3d3f1SNagaraju Goruganti #include <string>
5*34e3d3f1SNagaraju Goruganti #include <utils.hpp>
6*34e3d3f1SNagaraju Goruganti #include <phosphor-logging/log.hpp>
7*34e3d3f1SNagaraju Goruganti #include <phosphor-logging/elog-errors.hpp>
8*34e3d3f1SNagaraju Goruganti #include "xyz/openbmc_project/Common/error.hpp"
9*34e3d3f1SNagaraju Goruganti #include "xyz/openbmc_project/State/BMC/server.hpp"
1098a23840SMatthew Barth 
11*34e3d3f1SNagaraju Goruganti static constexpr auto bmcStateRoot = "/xyz/openbmc_project/state";
12*34e3d3f1SNagaraju Goruganti static constexpr auto bmcStateIntf = "xyz.openbmc_project.State.BMC";
13*34e3d3f1SNagaraju Goruganti static constexpr auto reqTransition = "RequestedBMCTransition";
14*34e3d3f1SNagaraju Goruganti static constexpr auto match = "bmc0";
15*34e3d3f1SNagaraju Goruganti 
16*34e3d3f1SNagaraju Goruganti using namespace phosphor::logging;
17*34e3d3f1SNagaraju Goruganti using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
1898a23840SMatthew Barth 
1998a23840SMatthew Barth void register_netfn_global_functions() __attribute__((constructor));
2098a23840SMatthew Barth 
21*34e3d3f1SNagaraju Goruganti void resetBMC()
2298a23840SMatthew Barth {
23*34e3d3f1SNagaraju Goruganti     sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
2498a23840SMatthew Barth 
25*34e3d3f1SNagaraju Goruganti     auto bmcStateObj = ipmi::getDbusObject(bus, bmcStateIntf, bmcStateRoot,
26*34e3d3f1SNagaraju Goruganti                                            match);
27*34e3d3f1SNagaraju Goruganti 
28*34e3d3f1SNagaraju Goruganti     auto service = ipmi::getService(bus, bmcStateIntf, bmcStateObj.first);
29*34e3d3f1SNagaraju Goruganti 
30*34e3d3f1SNagaraju Goruganti     ipmi::setDbusProperty(bus, service, bmcStateObj.first, bmcStateIntf,
31*34e3d3f1SNagaraju Goruganti                     reqTransition, convertForMessage(BMC::Transition::Reboot));
3298a23840SMatthew Barth }
3398a23840SMatthew Barth 
3498a23840SMatthew Barth 
35*34e3d3f1SNagaraju Goruganti ipmi_ret_t ipmi_global_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
3698a23840SMatthew Barth                              ipmi_request_t request, ipmi_response_t response,
3798a23840SMatthew Barth                              ipmi_data_len_t data_len, ipmi_context_t context)
3898a23840SMatthew Barth {
39*34e3d3f1SNagaraju Goruganti     try
40bc759884SNan Li     {
41*34e3d3f1SNagaraju Goruganti         resetBMC();
42*34e3d3f1SNagaraju Goruganti     }
43*34e3d3f1SNagaraju Goruganti     catch (std::exception& e)
44*34e3d3f1SNagaraju Goruganti     {
45*34e3d3f1SNagaraju Goruganti         log<level::ERR>(e.what());
46*34e3d3f1SNagaraju Goruganti         return IPMI_CC_UNSPECIFIED_ERROR;
47*34e3d3f1SNagaraju Goruganti     }
48bc759884SNan Li 
49bc759884SNan Li     // Status code.
50bc759884SNan Li     ipmi_ret_t rc = IPMI_CC_OK;
51bc759884SNan Li     *data_len = 0;
52bc759884SNan Li     return rc;
53bc759884SNan Li }
5498a23840SMatthew Barth 
5598a23840SMatthew Barth void register_netfn_global_functions()
5698a23840SMatthew Barth {
570573237fSTom     // Cold Reset
58*34e3d3f1SNagaraju Goruganti     ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL,
59*34e3d3f1SNagaraju Goruganti                            ipmi_global_reset, PRIVILEGE_ADMIN);
60bc759884SNan Li 
610573237fSTom     // <Warm Reset>
62*34e3d3f1SNagaraju Goruganti     ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL,
63*34e3d3f1SNagaraju Goruganti                            ipmi_global_reset, PRIVILEGE_ADMIN);
6498a23840SMatthew Barth 
6598a23840SMatthew Barth     return;
6698a23840SMatthew Barth }
67