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