Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 1 | #include <cstdlib> |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 2 | #include <unistd.h> |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <phosphor-logging/log.hpp> |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Control/Host/server.hpp> |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 6 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame^] | 7 | using namespace std::literals; |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 8 | using namespace phosphor::logging; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 9 | using namespace sdbusplus::xyz::openbmc_project::Control::server; |
| 10 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame^] | 11 | // Required strings for sending the msg to check on host |
| 12 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 13 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 14 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 15 | constexpr auto CONTROL_HOST_PATH = "/xyz/openbmc_project/control/host0"; |
| 16 | constexpr auto CONTROL_HOST_INTERFACE = "xyz.openbmc_project.Control.Host"; |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 17 | |
| 18 | bool cmdDone = false; |
| 19 | bool hostRunning = false; |
| 20 | |
| 21 | // Function called on host control signals |
| 22 | static 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 Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 49 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame^] | 50 | // Send hearbeat to host to determine if it's running |
| 51 | void 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 Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 96 | int main(int argc, char *argv[]) |
| 97 | { |
| 98 | log<level::INFO>("Check if host is running"); |
| 99 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 100 | auto bus = sdbusplus::bus::new_default(); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 101 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame^] | 102 | auto s = "type='signal',member='CommandComplete',path='"s + |
| 103 | CONTROL_HOST_PATH + "',interface='" + |
| 104 | CONTROL_HOST_INTERFACE + "'"; |
| 105 | |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 106 | // Setup Signal Handler |
| 107 | sdbusplus::bus::match::match hostControlSignals(bus, |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame^] | 108 | s.c_str(), |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 109 | hostControlSignal, |
| 110 | nullptr); |
| 111 | |
Andrew Geissler | eeaccf8 | 2017-03-28 15:23:27 -0500 | [diff] [blame^] | 112 | sendHeartbeat(bus); |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 113 | |
| 114 | // Wait for signal |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 115 | while(!cmdDone) |
| 116 | { |
| 117 | bus.process_discard(); |
| 118 | if (cmdDone) break; |
| 119 | bus.wait(); |
| 120 | } |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 121 | |
| 122 | // If host running then create file |
Andrew Geissler | 0971dea | 2017-03-28 14:32:40 -0500 | [diff] [blame] | 123 | if(hostRunning) |
| 124 | { |
| 125 | // TODO - Add file creation |
| 126 | log<level::INFO>("Host is running!"); |
| 127 | } |
Andrew Geissler | b09463d | 2017-03-24 15:55:17 -0500 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |