blob: 3eab1e336eef22ccb920714519a8f3a3ef9aed09 [file] [log] [blame]
Andrew Geisslerb09463d2017-03-24 15:55:17 -05001#include <cstdlib>
Andrew Geissler0971dea2017-03-28 14:32:40 -05002#include <unistd.h>
Andrew Geisslerb09463d2017-03-24 15:55:17 -05003#include <sdbusplus/bus.hpp>
4#include <phosphor-logging/log.hpp>
Andrew Geissler0971dea2017-03-28 14:32:40 -05005#include <xyz/openbmc_project/Control/Host/server.hpp>
Andrew Geisslerb09463d2017-03-24 15:55:17 -05006
Andrew Geisslereeaccf82017-03-28 15:23:27 -05007using namespace std::literals;
Andrew Geisslerb09463d2017-03-24 15:55:17 -05008using namespace phosphor::logging;
Andrew Geissler0971dea2017-03-28 14:32:40 -05009using namespace sdbusplus::xyz::openbmc_project::Control::server;
10
Andrew Geisslereeaccf82017-03-28 15:23:27 -050011// Required strings for sending the msg to check on host
12constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
13constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
14constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
15constexpr auto CONTROL_HOST_PATH = "/xyz/openbmc_project/control/host0";
16constexpr auto CONTROL_HOST_INTERFACE = "xyz.openbmc_project.Control.Host";
Andrew Geissler0971dea2017-03-28 14:32:40 -050017
18bool cmdDone = false;
19bool hostRunning = false;
20
21// Function called on host control signals
22static int hostControlSignal(sd_bus_message* msg,
23 void* userData,
24 sd_bus_error* retError)
25{
26 std::string cmdCompleted{};
27 std::string cmdStatus{};
28
29 auto sdPlusMsg = sdbusplus::message::message(msg);
30 sdPlusMsg.read(cmdCompleted, cmdStatus);
31
32 log<level::DEBUG>("Host control signal values",
33 entry("COMMAND=%s",cmdCompleted.c_str()),
34 entry("STATUS=%s",cmdStatus.c_str()));
35
36 // Verify it's the command this code is interested in and then check status
37 if(Host::convertCommandFromString(cmdCompleted) == Host::Command::Heartbeat)
38 {
39 cmdDone = true;
40
41 if(Host::convertResultFromString(cmdStatus) == Host::Result::Success)
42 {
43 hostRunning = true;
44 }
45 }
46
47 return 0;
48}
Andrew Geisslerb09463d2017-03-24 15:55:17 -050049
Andrew Geisslereeaccf82017-03-28 15:23:27 -050050// Send hearbeat to host to determine if it's running
51void sendHeartbeat(sdbusplus::bus::bus& bus)
52{
53 auto mapper = bus.new_method_call(MAPPER_BUSNAME,
54 MAPPER_PATH,
55 MAPPER_INTERFACE,
56 "GetObject");
57
58 mapper.append(CONTROL_HOST_PATH,
59 std::vector<std::string>({CONTROL_HOST_INTERFACE}));
60 auto mapperResponseMsg = bus.call(mapper);
61
62 if (mapperResponseMsg.is_method_error())
63 {
64 log<level::ERR>("Error in mapper call for control host");
65 // TODO openbmc/openbmc#851 - Once available, throw returned error
66 throw std::runtime_error("Error in mapper call for control host");
67 }
68
69 std::map<std::string, std::vector<std::string>> mapperResponse;
70 mapperResponseMsg.read(mapperResponse);
71 if (mapperResponse.empty())
72 {
73 log<level::ERR>("Error reading mapper resp for control host");
74 // TODO openbmc/openbmc#851 - Once available, throw returned error
75 throw std::runtime_error("Error reading mapper resp for control host");
76 }
77
78 const auto& host = mapperResponse.begin()->first;
79
80 auto method = bus.new_method_call(host.c_str(),
81 CONTROL_HOST_PATH,
82 CONTROL_HOST_INTERFACE,
83 "Execute");
84 method.append(convertForMessage(Host::Command::Heartbeat).c_str());
85
86 auto reply = bus.call(method);
87 if (reply.is_method_error())
88 {
89 log<level::ERR>("Error in call to control host Execute");
90 throw std::runtime_error("Error in call to control host Execute");
91 }
92
93 return;
94}
95
Andrew Geisslerb09463d2017-03-24 15:55:17 -050096int main(int argc, char *argv[])
97{
98 log<level::INFO>("Check if host is running");
99
Andrew Geissler0971dea2017-03-28 14:32:40 -0500100 auto bus = sdbusplus::bus::new_default();
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500101
Andrew Geisslereeaccf82017-03-28 15:23:27 -0500102 auto s = "type='signal',member='CommandComplete',path='"s +
103 CONTROL_HOST_PATH + "',interface='" +
104 CONTROL_HOST_INTERFACE + "'";
105
Andrew Geissler0971dea2017-03-28 14:32:40 -0500106 // Setup Signal Handler
107 sdbusplus::bus::match::match hostControlSignals(bus,
Andrew Geisslereeaccf82017-03-28 15:23:27 -0500108 s.c_str(),
Andrew Geissler0971dea2017-03-28 14:32:40 -0500109 hostControlSignal,
110 nullptr);
111
Andrew Geisslereeaccf82017-03-28 15:23:27 -0500112 sendHeartbeat(bus);
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500113
114 // Wait for signal
Andrew Geissler0971dea2017-03-28 14:32:40 -0500115 while(!cmdDone)
116 {
117 bus.process_discard();
118 if (cmdDone) break;
119 bus.wait();
120 }
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500121
122 // If host running then create file
Andrew Geissler0971dea2017-03-28 14:32:40 -0500123 if(hostRunning)
124 {
125 // TODO - Add file creation
126 log<level::INFO>("Host is running!");
127 }
Andrew Geisslerb09463d2017-03-24 15:55:17 -0500128
129 return 0;
130}