1 #include "globalhandler.hpp"
2 
3 #include <ipmid/api.hpp>
4 #include <ipmid/utils.hpp>
5 #include <phosphor-logging/elog-errors.hpp>
6 #include <phosphor-logging/log.hpp>
7 #include <string>
8 #include <xyz/openbmc_project/Common/error.hpp>
9 #include <xyz/openbmc_project/State/BMC/server.hpp>
10 
11 static constexpr auto bmcStateRoot = "/xyz/openbmc_project/state";
12 static constexpr auto bmcStateIntf = "xyz.openbmc_project.State.BMC";
13 static constexpr auto reqTransition = "RequestedBMCTransition";
14 static constexpr auto match = "bmc0";
15 
16 using namespace phosphor::logging;
17 using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
18 
19 void register_netfn_global_functions() __attribute__((constructor));
20 
21 void resetBMC()
22 {
23     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
24 
25     auto bmcStateObj =
26         ipmi::getDbusObject(bus, bmcStateIntf, bmcStateRoot, match);
27 
28     auto service = ipmi::getService(bus, bmcStateIntf, bmcStateObj.first);
29 
30     ipmi::setDbusProperty(bus, service, bmcStateObj.first, bmcStateIntf,
31                           reqTransition,
32                           convertForMessage(BMC::Transition::Reboot));
33 }
34 
35 /** @brief implements cold and warm reset commands
36  *  @param - None
37  *  @returns IPMI completion code.
38  */
39 ipmi::RspType<> ipmiGlobalReset()
40 {
41     try
42     {
43         resetBMC();
44     }
45     catch (const std::exception& e)
46     {
47         log<level::ERR>(e.what());
48         return ipmi::responseUnspecifiedError();
49     }
50 
51     // Status code.
52     return ipmi::responseSuccess();
53 }
54 
55 void register_netfn_global_functions()
56 {
57 
58     // Cold Reset
59     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
60                           ipmi::app::cmdColdReset, ipmi::Privilege::Admin,
61                           ipmiGlobalReset);
62     return;
63 }
64