Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 1 | #include "config.h" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 3 | #include "host_state_manager.hpp" |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 4 | #include "settings.hpp" |
| 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp" |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 7 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | #include <getopt.h> |
| 9 | #include <systemd/sd-bus.h> |
| 10 | |
| 11 | #include <phosphor-logging/elog-errors.hpp> |
| 12 | #include <phosphor-logging/log.hpp> |
| 13 | #include <sdbusplus/exception.hpp> |
| 14 | #include <sdbusplus/server.hpp> |
| 15 | |
| 16 | #include <iostream> |
| 17 | #include <map> |
| 18 | #include <string> |
| 19 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 20 | namespace phosphor |
| 21 | { |
| 22 | namespace state |
| 23 | { |
| 24 | namespace manager |
| 25 | { |
| 26 | |
| 27 | using namespace phosphor::logging; |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 29 | using namespace sdbusplus::xyz::openbmc_project::Control::Power::server; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 30 | |
| 31 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 32 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 33 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 34 | |
| 35 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 36 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 37 | std::string getService(sdbusplus::bus::bus& bus, std::string path, |
| 38 | std::string interface) |
| 39 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 40 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 41 | MAPPER_INTERFACE, "GetObject"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 42 | |
| 43 | mapper.append(path, std::vector<std::string>({interface})); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 44 | |
| 45 | std::map<std::string, std::vector<std::string>> mapperResponse; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 46 | try |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 47 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 48 | auto mapperResponseMsg = bus.call(mapper); |
| 49 | |
| 50 | mapperResponseMsg.read(mapperResponse); |
| 51 | if (mapperResponse.empty()) |
| 52 | { |
| 53 | log<level::ERR>("Error reading mapper response", |
| 54 | entry("PATH=%s", path.c_str()), |
| 55 | entry("INTERFACE=%s", interface.c_str())); |
| 56 | throw std::runtime_error("Error reading mapper response"); |
| 57 | } |
| 58 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 59 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 60 | { |
| 61 | log<level::ERR>("Error in mapper call", entry("ERROR=%s", e.what()), |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 62 | entry("PATH=%s", path.c_str()), |
| 63 | entry("INTERFACE=%s", interface.c_str())); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 64 | throw; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return mapperResponse.begin()->first; |
| 68 | } |
| 69 | |
| 70 | std::string getProperty(sdbusplus::bus::bus& bus, std::string path, |
| 71 | std::string interface, std::string propertyName) |
| 72 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 73 | std::variant<std::string> property; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 74 | std::string service = getService(bus, path, interface); |
| 75 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 76 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 77 | PROPERTY_INTERFACE, "Get"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 78 | |
| 79 | method.append(interface, propertyName); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 80 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 81 | try |
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 | auto reply = bus.call(method); |
| 84 | reply.read(property); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 85 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 86 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 87 | { |
| 88 | log<level::ERR>("Error in property Get", entry("ERROR=%s", e.what()), |
| 89 | entry("PROPERTY=%s", propertyName.c_str())); |
| 90 | throw; |
| 91 | } |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 92 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 93 | if (std::get<std::string>(property).empty()) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 94 | { |
| 95 | log<level::ERR>("Error reading property response", |
| 96 | entry("PROPERTY=%s", propertyName.c_str())); |
| 97 | throw std::runtime_error("Error reading property response"); |
| 98 | } |
| 99 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 100 | return std::get<std::string>(property); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Patrick Venture | f287059 | 2018-10-14 14:11:30 -0700 | [diff] [blame] | 103 | void setProperty(sdbusplus::bus::bus& bus, const std::string& path, |
| 104 | const std::string& interface, const std::string& property, |
| 105 | const std::string& value) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 106 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 107 | std::variant<std::string> variantValue = value; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 108 | std::string service = getService(bus, path, interface); |
| 109 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 110 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 111 | PROPERTY_INTERFACE, "Set"); |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 112 | |
| 113 | method.append(interface, property, variantValue); |
| 114 | bus.call_noreply(method); |
| 115 | |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | } // namespace manager |
| 120 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 121 | } // namespace phosphor |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 122 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 123 | int main(int argc, char** argv) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 124 | { |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 125 | using namespace phosphor::logging; |
| 126 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 127 | std::string hostPath = "/xyz/openbmc_project/state/host0"; |
| 128 | int arg; |
| 129 | int optIndex = 0; |
| 130 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 131 | static struct option longOpts[] = {{"host", required_argument, 0, 'h'}, |
| 132 | {0, 0, 0, 0}}; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 133 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 134 | while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 135 | { |
| 136 | switch (arg) |
| 137 | { |
| 138 | case 'h': |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 139 | hostPath = |
| 140 | std::string("/xyz/openbmc_project/state/host") + optarg; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 141 | break; |
| 142 | default: |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 147 | auto bus = sdbusplus::bus::new_default(); |
| 148 | |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 149 | using namespace settings; |
| 150 | Objects settings(bus); |
| 151 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 152 | using namespace phosphor::state::manager; |
| 153 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 154 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 155 | // This application is only run if chassis power is off |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 156 | |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 157 | /* The logic here is to first check the one-time PowerRestorePolicy setting. |
| 158 | * If this property is not the default then look at the persistent |
| 159 | * user setting in the non one-time object, otherwise honor the one-time |
| 160 | * setting. |
| 161 | */ |
| 162 | auto methodOneTime = bus.new_method_call( |
| 163 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 164 | settings.powerRestorePolicyOneTime.c_str(), |
| 165 | "org.freedesktop.DBus.Properties", "Get"); |
| 166 | methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy"); |
| 167 | |
| 168 | auto methodUserSetting = bus.new_method_call( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 169 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 170 | settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties", |
| 171 | "Get"); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 172 | methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 173 | |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 174 | std::variant<std::string> result; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 175 | try |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 176 | { |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 177 | auto reply = bus.call(methodOneTime); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 178 | reply.read(result); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 179 | auto powerPolicy = std::get<std::string>(result); |
| 180 | |
| 181 | if (RestorePolicy::Policy::None == |
| 182 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 183 | { |
| 184 | // one_time is set to None so use the customer setting |
| 185 | log<level::INFO>( |
| 186 | "One time not set, check user setting of power policy"); |
| 187 | auto reply = bus.call(methodUserSetting); |
| 188 | reply.read(result); |
| 189 | powerPolicy = std::get<std::string>(result); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | // one_time setting was set so we're going to use it. Reset it |
| 194 | // to default for next time. |
| 195 | log<level::INFO>("One time set, use it and reset to default"); |
| 196 | setProperty(bus, settings.powerRestorePolicyOneTime.c_str(), |
| 197 | powerRestoreIntf, "PowerRestorePolicy", |
| 198 | convertForMessage(RestorePolicy::Policy::None)); |
| 199 | } |
| 200 | |
| 201 | log<level::INFO>("Host power is off, processing power policy", |
| 202 | entry("POWER_POLICY=%s", powerPolicy.c_str())); |
| 203 | |
| 204 | if (RestorePolicy::Policy::AlwaysOn == |
| 205 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 206 | { |
| 207 | log<level::INFO>("power_policy=ALWAYS_POWER_ON, powering host on"); |
| 208 | setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
| 209 | convertForMessage(server::Host::Transition::On)); |
| 210 | } |
| 211 | else if (RestorePolicy::Policy::Restore == |
| 212 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 213 | { |
| 214 | log<level::INFO>("power_policy=RESTORE, restoring last state"); |
| 215 | |
| 216 | // Read last requested state and re-request it to execute it |
| 217 | auto hostReqState = getProperty(bus, hostPath, HOST_BUSNAME, |
| 218 | "RequestedHostTransition"); |
| 219 | setProperty(bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
| 220 | hostReqState); |
| 221 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 222 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 223 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 224 | { |
| 225 | log<level::ERR>("Error in PowerRestorePolicy Get", |
| 226 | entry("ERROR=%s", e.what())); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 227 | elog<InternalFailure>(); |
| 228 | } |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 229 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 230 | return 0; |
| 231 | } |