1 #include "config.h"
2 
3 #include "host-interface.hpp"
4 
5 #include "systemintfcmds.hpp"
6 
7 #include <functional>
8 #include <ipmid-host/cmd-utils.hpp>
9 #include <ipmid-host/cmd.hpp>
10 #include <ipmid/utils.hpp>
11 #include <phosphor-logging/log.hpp>
12 
13 namespace phosphor
14 {
15 namespace host
16 {
17 namespace command
18 {
19 
20 // When you see Base:: you know we're referencing our base class
21 namespace Base = sdbusplus::xyz::openbmc_project::Control::server;
22 
23 // IPMI OEM command.
24 // https://github.com/openbmc/openbmc/issues/2082 for handling
25 // Non-OEM commands that need to send SMS_ATN
26 using OEMCmd = uint8_t;
27 
28 // Map of IPMI OEM command to its equivalent interface command.
29 // This is needed when invoking the callback handler to indicate
30 // the status of the executed command.
31 static const std::map<OEMCmd, Host::Command> intfCommand = {
32     {CMD_HEARTBEAT, Base::Host::Command::Heartbeat},
33     {CMD_POWER, Base::Host::Command::SoftOff}};
34 
35 // Map of Interface command to its corresponding IPMI OEM command.
36 // This is needed when pushing IPMI commands to command manager's
37 // queue. The same pair will be returned when IPMI asks us
38 // why a SMS_ATN was sent
39 static const std::map<Host::Command, IpmiCmdData> ipmiCommand = {
40     {Base::Host::Command::Heartbeat, std::make_pair(CMD_HEARTBEAT, 0x00)},
41     {Base::Host::Command::SoftOff, std::make_pair(CMD_POWER, SOFT_OFF)}};
42 
43 // Called at user request
44 void Host::execute(Base::Host::Command command)
45 {
46     using namespace phosphor::logging;
47 
48     log<level::DEBUG>(
49         "Pushing cmd on to queue",
50         entry("CONTROL_HOST_CMD=%s", convertForMessage(command).c_str()));
51 
52     auto cmd = std::make_tuple(ipmiCommand.at(command),
53                                std::bind(&Host::commandStatusHandler, this,
54                                          std::placeholders::_1,
55                                          std::placeholders::_2));
56 
57     ipmid_send_cmd_to_host(std::move(cmd));
58 }
59 
60 // Called into by Command Manager
61 void Host::commandStatusHandler(IpmiCmdData cmd, bool status)
62 {
63     // Need to convert <cmd> to the equivalent one mentioned in spec
64     auto value = status ? Result::Success : Result::Failure;
65 
66     // Fire a signal
67     this->commandComplete(intfCommand.at(std::get<0>(cmd)), value);
68 }
69 
70 } // namespace command
71 } // namespace host
72 } // namespace phosphor
73