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 | |
Patrick Venture | f287059 | 2018-10-14 14:11:30 -0700 | [diff] [blame^] | 92 | void setProperty(sdbusplus::bus::bus& bus, const std::string& path, |
| 93 | const std::string& interface, const std::string& property, |
| 94 | const std::string& value) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 95 | { |
| 96 | sdbusplus::message::variant<std::string> variantValue = value; |
| 97 | std::string service = getService(bus, path, interface); |
| 98 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 99 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 100 | PROPERTY_INTERFACE, "Set"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 101 | |
| 102 | method.append(interface, property, variantValue); |
| 103 | bus.call_noreply(method); |
| 104 | |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | } // namespace manager |
| 109 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 110 | } // namespace phosphor |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 111 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 112 | int main(int argc, char** argv) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 113 | { |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 114 | using namespace phosphor::logging; |
| 115 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 116 | std::string hostPath = "/xyz/openbmc_project/state/host0"; |
| 117 | int arg; |
| 118 | int optIndex = 0; |
| 119 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 120 | static struct option longOpts[] = {{"host", required_argument, 0, 'h'}, |
| 121 | {0, 0, 0, 0}}; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 122 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 123 | while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 124 | { |
| 125 | switch (arg) |
| 126 | { |
| 127 | case 'h': |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 128 | hostPath = |
| 129 | std::string("/xyz/openbmc_project/state/host") + optarg; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 130 | break; |
| 131 | default: |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 136 | auto bus = sdbusplus::bus::new_default(); |
| 137 | |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 138 | using namespace settings; |
| 139 | Objects settings(bus); |
| 140 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 141 | using namespace phosphor::state::manager; |
| 142 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 143 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 144 | // This application is only run if chassis power is off |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 145 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 146 | auto method = bus.new_method_call( |
| 147 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 148 | settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties", |
| 149 | "Get"); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 150 | method.append(powerRestoreIntf, "PowerRestorePolicy"); |
| 151 | auto reply = bus.call(method); |
| 152 | if (reply.is_method_error()) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 153 | { |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 154 | log<level::ERR>("Error in PowerRestorePolicy Get"); |
| 155 | elog<InternalFailure>(); |
| 156 | } |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 157 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 158 | sdbusplus::message::variant<std::string> result; |
| 159 | reply.read(result); |
| 160 | auto powerPolicy = result.get<std::string>(); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 161 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 162 | log<level::INFO>("Host power is off, checking power policy", |
| 163 | entry("POWER_POLICY=%s", powerPolicy.c_str())); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 164 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 165 | if (RestorePolicy::Policy::AlwaysOn == |
| 166 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 167 | { |
| 168 | log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 169 | setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 170 | convertForMessage(server::Host::Transition::On)); |
| 171 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 172 | else if (RestorePolicy::Policy::Restore == |
| 173 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 174 | { |
| 175 | log<level::INFO>("power_policy=RESTORE, restoring last state"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 176 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 177 | // Read last requested state and re-request it to execute it |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 178 | auto hostReqState = |
| 179 | getProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition"); |
| 180 | setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 181 | hostReqState); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |