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