Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 1 | #include <getopt.h> |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 2 | #include <iostream> |
| 3 | #include <map> |
| 4 | #include <string> |
| 5 | #include <config.h> |
| 6 | #include <systemd/sd-bus.h> |
| 7 | #include <sdbusplus/server.hpp> |
| 8 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 10 | #include "host_state_manager.hpp" |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 11 | #include "settings.hpp" |
| 12 | #include "xyz/openbmc_project/Common/error.hpp" |
| 13 | #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp" |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace state |
| 18 | { |
| 19 | namespace manager |
| 20 | { |
| 21 | |
| 22 | using namespace phosphor::logging; |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 23 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 24 | using namespace sdbusplus::xyz::openbmc_project::Control::Power::server; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 25 | |
| 26 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 27 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 28 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 29 | |
| 30 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 31 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 32 | std::string getService(sdbusplus::bus::bus& bus, std::string path, |
| 33 | std::string interface) |
| 34 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 35 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 36 | MAPPER_INTERFACE, "GetObject"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 37 | |
| 38 | mapper.append(path, std::vector<std::string>({interface})); |
| 39 | auto mapperResponseMsg = bus.call(mapper); |
| 40 | |
| 41 | if (mapperResponseMsg.is_method_error()) |
| 42 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 43 | log<level::ERR>("Error in mapper call", entry("PATH=%s", path.c_str()), |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 44 | entry("INTERFACE=%s", interface.c_str())); |
| 45 | throw std::runtime_error("Error in mapper call"); |
| 46 | } |
| 47 | |
| 48 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 49 | mapperResponseMsg.read(mapperResponse); |
| 50 | if (mapperResponse.empty()) |
| 51 | { |
| 52 | log<level::ERR>("Error reading mapper response", |
| 53 | entry("PATH=%s", path.c_str()), |
| 54 | entry("INTERFACE=%s", interface.c_str())); |
| 55 | throw std::runtime_error("Error reading mapper response"); |
| 56 | } |
| 57 | |
| 58 | return mapperResponse.begin()->first; |
| 59 | } |
| 60 | |
| 61 | std::string getProperty(sdbusplus::bus::bus& bus, std::string path, |
| 62 | std::string interface, std::string propertyName) |
| 63 | { |
| 64 | sdbusplus::message::variant<std::string> property; |
| 65 | std::string service = getService(bus, path, interface); |
| 66 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 67 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 68 | PROPERTY_INTERFACE, "Get"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 69 | |
| 70 | method.append(interface, propertyName); |
| 71 | auto reply = bus.call(method); |
| 72 | |
| 73 | if (reply.is_method_error()) |
| 74 | { |
| 75 | log<level::ERR>("Error in property Get", |
| 76 | entry("PROPERTY=%s", propertyName.c_str())); |
| 77 | throw std::runtime_error("Error in property Get"); |
| 78 | } |
| 79 | |
| 80 | reply.read(property); |
| 81 | |
| 82 | if (sdbusplus::message::variant_ns::get<std::string>(property).empty()) |
| 83 | { |
| 84 | log<level::ERR>("Error reading property response", |
| 85 | entry("PROPERTY=%s", propertyName.c_str())); |
| 86 | throw std::runtime_error("Error reading property response"); |
| 87 | } |
| 88 | |
| 89 | return sdbusplus::message::variant_ns::get<std::string>(property); |
| 90 | } |
| 91 | |
| 92 | void setProperty(sdbusplus::bus::bus& bus, std::string path, |
| 93 | std::string interface, std::string property, std::string value) |
| 94 | { |
| 95 | sdbusplus::message::variant<std::string> variantValue = value; |
| 96 | std::string service = getService(bus, path, interface); |
| 97 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 98 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 99 | PROPERTY_INTERFACE, "Set"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 100 | |
| 101 | method.append(interface, property, variantValue); |
| 102 | bus.call_noreply(method); |
| 103 | |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | } // namespace manager |
| 108 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame^] | 109 | } // namespace phosphor |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 110 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 111 | int main(int argc, char** argv) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 112 | { |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 113 | using namespace phosphor::logging; |
| 114 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 115 | std::string hostPath = "/xyz/openbmc_project/state/host0"; |
| 116 | int arg; |
| 117 | int optIndex = 0; |
| 118 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 119 | static struct option longOpts[] = {{"host", required_argument, 0, 'h'}, |
| 120 | {0, 0, 0, 0}}; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 121 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 122 | while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 123 | { |
| 124 | switch (arg) |
| 125 | { |
| 126 | case 'h': |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 127 | hostPath = |
| 128 | std::string("/xyz/openbmc_project/state/host") + optarg; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 129 | break; |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 135 | auto bus = sdbusplus::bus::new_default(); |
| 136 | |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 137 | using namespace settings; |
| 138 | Objects settings(bus); |
| 139 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 140 | using namespace phosphor::state::manager; |
| 141 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 142 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 143 | // This application is only run if chassis power is off |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 144 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 145 | auto method = bus.new_method_call( |
| 146 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 147 | settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties", |
| 148 | "Get"); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 149 | method.append(powerRestoreIntf, "PowerRestorePolicy"); |
| 150 | auto reply = bus.call(method); |
| 151 | if (reply.is_method_error()) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 152 | { |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 153 | log<level::ERR>("Error in PowerRestorePolicy Get"); |
| 154 | elog<InternalFailure>(); |
| 155 | } |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 156 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 157 | sdbusplus::message::variant<std::string> result; |
| 158 | reply.read(result); |
| 159 | auto powerPolicy = result.get<std::string>(); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 160 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 161 | log<level::INFO>("Host power is off, checking power policy", |
| 162 | entry("POWER_POLICY=%s", powerPolicy.c_str())); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 163 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 164 | if (RestorePolicy::Policy::AlwaysOn == |
| 165 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 166 | { |
| 167 | log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 168 | setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 169 | convertForMessage(server::Host::Transition::On)); |
| 170 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 171 | else if (RestorePolicy::Policy::Restore == |
| 172 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 173 | { |
| 174 | log<level::INFO>("power_policy=RESTORE, restoring last state"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 175 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 176 | // Read last requested state and re-request it to execute it |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 177 | auto hostReqState = |
| 178 | getProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition"); |
| 179 | setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 180 | hostReqState); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |