Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "manager.hpp" |
| 18 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 19 | #include "chassis.hpp" |
| 20 | #include "config_file_parser.hpp" |
| 21 | #include "exception_utils.hpp" |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 22 | #include "rule.hpp" |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 23 | #include "utility.hpp" |
| 24 | |
Shawn McCarney | 415094c | 2021-02-15 11:08:19 -0600 | [diff] [blame] | 25 | #include <xyz/openbmc_project/Common/error.hpp> |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 26 | #include <xyz/openbmc_project/State/Chassis/server.hpp> |
Shawn McCarney | 415094c | 2021-02-15 11:08:19 -0600 | [diff] [blame] | 27 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 28 | #include <algorithm> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 29 | #include <chrono> |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 30 | #include <exception> |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 31 | #include <functional> |
| 32 | #include <map> |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 33 | #include <thread> |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 34 | #include <tuple> |
| 35 | #include <utility> |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 36 | #include <variant> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 37 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame] | 38 | namespace phosphor::power::regulators |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 39 | { |
| 40 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 41 | namespace fs = std::filesystem; |
| 42 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 43 | constexpr auto busName = "xyz.openbmc_project.Power.Regulators"; |
| 44 | constexpr auto managerObjPath = "/xyz/openbmc_project/power/regulators/manager"; |
| 45 | constexpr auto compatibleIntf = |
| 46 | "xyz.openbmc_project.Configuration.IBMCompatibleSystem"; |
| 47 | constexpr auto compatibleNamesProp = "Names"; |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 48 | constexpr auto chassisStatePath = "/xyz/openbmc_project/state/chassis0"; |
| 49 | constexpr auto chassisStateIntf = "xyz.openbmc_project.State.Chassis"; |
| 50 | constexpr auto chassisStateProp = "CurrentPowerState"; |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 51 | constexpr std::chrono::minutes maxTimeToWaitForCompatTypes{5}; |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 52 | |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 53 | using PowerState = |
| 54 | sdbusplus::xyz::openbmc_project::State::server::Chassis::PowerState; |
| 55 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 56 | /** |
| 57 | * Default configuration file name. This is used when the system does not |
| 58 | * implement the D-Bus compatible interface. |
| 59 | */ |
| 60 | constexpr auto defaultConfigFileName = "config.json"; |
| 61 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 62 | /** |
| 63 | * Standard configuration file directory. This directory is part of the |
| 64 | * firmware install image. It contains the standard version of the config file. |
| 65 | */ |
| 66 | const fs::path standardConfigFileDir{"/usr/share/phosphor-regulators"}; |
| 67 | |
| 68 | /** |
| 69 | * Test configuration file directory. This directory can contain a test version |
| 70 | * of the config file. The test version will override the standard version. |
| 71 | */ |
| 72 | const fs::path testConfigFileDir{"/etc/phosphor-regulators"}; |
| 73 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 74 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 75 | ManagerObject{bus, managerObjPath, true}, bus{bus}, eventLoop{event}, |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 76 | services{bus}, timer{event, std::bind(&Manager::timerExpired, this)} |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 77 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 78 | // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This |
| 79 | // notifies us if the compatible interface becomes available later. |
| 80 | std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() + |
| 81 | sdbusplus::bus::match::rules::sender( |
| 82 | "xyz.openbmc_project.EntityManager"); |
| 83 | std::unique_ptr<sdbusplus::server::match::match> matchPtr = |
| 84 | std::make_unique<sdbusplus::server::match::match>( |
| 85 | bus, matchStr, |
| 86 | std::bind(&Manager::interfacesAddedHandler, this, |
| 87 | std::placeholders::_1)); |
| 88 | signals.emplace_back(std::move(matchPtr)); |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 89 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 90 | // Try to find compatible system types using D-Bus compatible interface. |
| 91 | // Note that it might not be supported on this system, or the service that |
| 92 | // provides the interface might not be running yet. |
| 93 | findCompatibleSystemTypes(); |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame] | 94 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 95 | // Try to find and load the JSON configuration file |
| 96 | loadConfigFile(); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 97 | |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 98 | // Obtain D-Bus service name |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 99 | bus.request_name(busName); |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 100 | |
| 101 | // If system is already powered on, enable monitoring |
| 102 | if (isSystemPoweredOn()) |
| 103 | { |
| 104 | monitor(true); |
| 105 | } |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void Manager::configure() |
| 109 | { |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 110 | // Clear any cached data or error history related to hardware devices |
| 111 | clearHardwareData(); |
| 112 | |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 113 | // Wait until the config file has been loaded or hit max wait time |
| 114 | waitUntilConfigFileLoaded(); |
| 115 | |
| 116 | // Verify config file has been loaded and System object is valid |
| 117 | if (isConfigFileLoaded()) |
Shawn McCarney | 6345c6c | 2020-05-04 10:35:05 -0500 | [diff] [blame] | 118 | { |
| 119 | // Configure the regulator devices in the system |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 120 | system->configure(services); |
Shawn McCarney | 6345c6c | 2020-05-04 10:35:05 -0500 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | { |
Shawn McCarney | 415094c | 2021-02-15 11:08:19 -0600 | [diff] [blame] | 124 | // Write error message to journal |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 125 | services.getJournal().logError("Unable to configure regulator devices: " |
| 126 | "Configuration file not loaded"); |
Shawn McCarney | 6345c6c | 2020-05-04 10:35:05 -0500 | [diff] [blame] | 127 | |
Shawn McCarney | 415094c | 2021-02-15 11:08:19 -0600 | [diff] [blame] | 128 | // Log critical error since regulators could not be configured. Could |
| 129 | // cause hardware damage if default regulator settings are very wrong. |
| 130 | services.getErrorLogging().logConfigFileError(Entry::Level::Critical, |
| 131 | services.getJournal()); |
| 132 | |
| 133 | // Throw InternalFailure to propogate error status to D-Bus client |
| 134 | throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure{}; |
| 135 | } |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 136 | } |
| 137 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 138 | void Manager::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 139 | { |
| 140 | // Verify message is valid |
| 141 | if (!msg) |
| 142 | { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | try |
| 147 | { |
| 148 | // Read object path for object that was created or had interface added |
| 149 | sdbusplus::message::object_path objPath; |
| 150 | msg.read(objPath); |
| 151 | |
| 152 | // Read the dictionary whose keys are interface names and whose values |
| 153 | // are dictionaries containing the interface property names and values |
| 154 | std::map<std::string, |
| 155 | std::map<std::string, std::variant<std::vector<std::string>>>> |
| 156 | intfProp; |
| 157 | msg.read(intfProp); |
| 158 | |
| 159 | // Find the compatible interface, if present |
| 160 | auto itIntf = intfProp.find(compatibleIntf); |
| 161 | if (itIntf != intfProp.cend()) |
| 162 | { |
| 163 | // Find the Names property of the compatible interface, if present |
| 164 | auto itProp = itIntf->second.find(compatibleNamesProp); |
| 165 | if (itProp != itIntf->second.cend()) |
| 166 | { |
| 167 | // Get value of Names property |
| 168 | auto propValue = std::get<0>(itProp->second); |
| 169 | if (!propValue.empty()) |
| 170 | { |
| 171 | // Store list of compatible system types |
| 172 | compatibleSystemTypes = propValue; |
| 173 | |
| 174 | // Find and load JSON config file based on system types |
| 175 | loadConfigFile(); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | catch (const std::exception&) |
| 181 | { |
| 182 | // Error trying to read interfacesAdded message. One possible cause |
| 183 | // could be a property whose value is not a std::vector<std::string>. |
| 184 | } |
| 185 | } |
| 186 | |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 187 | void Manager::monitor(bool enable) |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 188 | { |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 189 | // Check whether already in the requested monitoring state |
| 190 | if (enable == isMonitoringEnabled) |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 191 | { |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
| 195 | isMonitoringEnabled = enable; |
| 196 | if (isMonitoringEnabled) |
| 197 | { |
| 198 | services.getJournal().logDebug("Monitoring enabled"); |
| 199 | |
| 200 | // Restart timer to have a repeating 1 second interval |
| 201 | timer.restart(std::chrono::seconds(1)); |
| 202 | |
| 203 | // Enable sensors service; put all sensors in an active state |
| 204 | services.getSensors().enable(); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 205 | } |
| 206 | else |
| 207 | { |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 208 | services.getJournal().logDebug("Monitoring disabled"); |
| 209 | |
| 210 | // Disable timer |
| 211 | timer.setEnabled(false); |
| 212 | |
| 213 | // Disable sensors service; put all sensors in an inactive state |
| 214 | services.getSensors().disable(); |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 215 | |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 216 | // Verify config file has been loaded and System object is valid |
| 217 | if (isConfigFileLoaded()) |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 218 | { |
| 219 | // Close the regulator devices in the system. Monitoring is |
| 220 | // normally disabled because the system is being powered off. The |
| 221 | // devices should be closed in case hardware is removed or replaced |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 222 | // while the system is powered off. |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 223 | system->closeDevices(services); |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 224 | } |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 228 | void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/, |
| 229 | const struct signalfd_siginfo* /*sigInfo*/) |
| 230 | { |
| 231 | // Reload the JSON configuration file |
| 232 | loadConfigFile(); |
| 233 | } |
| 234 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 235 | void Manager::timerExpired() |
| 236 | { |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 237 | // Notify sensors service that a sensor monitoring cycle is starting |
| 238 | services.getSensors().startCycle(); |
| 239 | |
| 240 | // Verify config file has been loaded and System object is valid |
| 241 | if (isConfigFileLoaded()) |
| 242 | { |
| 243 | // Monitor sensors for the voltage rails in the system |
| 244 | system->monitorSensors(services); |
| 245 | } |
| 246 | |
| 247 | // Notify sensors service that current sensor monitoring cycle has ended |
| 248 | services.getSensors().endCycle(); |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 251 | void Manager::clearHardwareData() |
| 252 | { |
Shawn McCarney | 4e0402c | 2021-02-05 00:08:33 -0600 | [diff] [blame] | 253 | // Clear any cached hardware presence data and VPD values |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 254 | services.getPresenceService().clearCache(); |
Shawn McCarney | 4e0402c | 2021-02-05 00:08:33 -0600 | [diff] [blame] | 255 | services.getVPD().clearCache(); |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 256 | |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 257 | // Verify config file has been loaded and System object is valid |
| 258 | if (isConfigFileLoaded()) |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 259 | { |
| 260 | // Clear any cached hardware data in the System object |
| 261 | system->clearCache(); |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 262 | |
Shawn McCarney | ce540f3 | 2021-05-14 17:08:41 -0500 | [diff] [blame] | 263 | // Clear error history related to hardware devices in the System object |
| 264 | system->clearErrorHistory(); |
| 265 | } |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 266 | } |
| 267 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 268 | void Manager::findCompatibleSystemTypes() |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 269 | { |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 270 | using namespace phosphor::power::util; |
| 271 | |
| 272 | try |
| 273 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 274 | // Query object mapper for object paths that implement the compatible |
| 275 | // interface. Returns a map of object paths to a map of services names |
| 276 | // to their interfaces. |
| 277 | DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory", |
| 278 | compatibleIntf, 0); |
| 279 | |
| 280 | // Get the first object path |
| 281 | auto objectIt = subTree.cbegin(); |
| 282 | if (objectIt != subTree.cend()) |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 283 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 284 | std::string objPath = objectIt->first; |
| 285 | |
| 286 | // Get the first service name |
| 287 | auto serviceIt = objectIt->second.cbegin(); |
| 288 | if (serviceIt != objectIt->second.cend()) |
| 289 | { |
| 290 | std::string service = serviceIt->first; |
| 291 | if (!service.empty()) |
| 292 | { |
| 293 | // Get compatible system types property value |
| 294 | getProperty(compatibleIntf, compatibleNamesProp, objPath, |
| 295 | service, bus, compatibleSystemTypes); |
| 296 | } |
| 297 | } |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 298 | } |
| 299 | } |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 300 | catch (const std::exception&) |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 301 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 302 | // Compatible system types information is not available. The current |
| 303 | // system might not support the interface, or the service that |
| 304 | // implements the interface might not be running yet. |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 305 | } |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 306 | } |
| 307 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 308 | fs::path Manager::findConfigFile() |
| 309 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 310 | // Build list of possible base file names |
| 311 | std::vector<std::string> fileNames{}; |
| 312 | |
| 313 | // Add possible file names based on compatible system types (if any) |
| 314 | for (const std::string& systemType : compatibleSystemTypes) |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 315 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 316 | // Replace all spaces and commas in system type name with underscores |
| 317 | std::string fileName{systemType}; |
| 318 | std::replace(fileName.begin(), fileName.end(), ' ', '_'); |
| 319 | std::replace(fileName.begin(), fileName.end(), ',', '_'); |
| 320 | |
| 321 | // Append .json suffix and add to list |
| 322 | fileName.append(".json"); |
| 323 | fileNames.emplace_back(fileName); |
| 324 | } |
| 325 | |
| 326 | // Add default file name for systems that don't use compatible interface |
| 327 | fileNames.emplace_back(defaultConfigFileName); |
| 328 | |
| 329 | // Look for a config file with one of the possible base names |
| 330 | for (const std::string& fileName : fileNames) |
| 331 | { |
| 332 | // Check if file exists in test directory |
| 333 | fs::path pathName{testConfigFileDir / fileName}; |
| 334 | if (fs::exists(pathName)) |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 335 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 336 | return pathName; |
| 337 | } |
| 338 | |
| 339 | // Check if file exists in standard directory |
| 340 | pathName = standardConfigFileDir / fileName; |
| 341 | if (fs::exists(pathName)) |
| 342 | { |
| 343 | return pathName; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 347 | // No config file found; return empty path |
| 348 | return fs::path{}; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 351 | bool Manager::isSystemPoweredOn() |
| 352 | { |
| 353 | bool isOn{false}; |
| 354 | |
| 355 | try |
| 356 | { |
| 357 | // Get D-Bus property that contains the current power state for |
| 358 | // chassis0, which represents the entire system (all chassis) |
| 359 | using namespace phosphor::power::util; |
| 360 | auto service = getService(chassisStatePath, chassisStateIntf, bus); |
| 361 | if (!service.empty()) |
| 362 | { |
| 363 | PowerState currentPowerState; |
| 364 | getProperty(chassisStateIntf, chassisStateProp, chassisStatePath, |
| 365 | service, bus, currentPowerState); |
| 366 | if (currentPowerState == PowerState::On) |
| 367 | { |
| 368 | isOn = true; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | catch (const std::exception& e) |
| 373 | { |
| 374 | // Current power state might not be available yet. The regulators |
| 375 | // application can start before the power state is published on D-Bus. |
| 376 | } |
| 377 | |
| 378 | return isOn; |
| 379 | } |
| 380 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 381 | void Manager::loadConfigFile() |
| 382 | { |
| 383 | try |
| 384 | { |
| 385 | // Find the absolute path to the config file |
| 386 | fs::path pathName = findConfigFile(); |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 387 | if (!pathName.empty()) |
| 388 | { |
| 389 | // Log info message in journal; config file path is important |
| 390 | services.getJournal().logInfo("Loading configuration file " + |
| 391 | pathName.string()); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 392 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 393 | // Parse the config file |
| 394 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 395 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 396 | std::tie(rules, chassis) = config_file_parser::parse(pathName); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 397 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 398 | // Store config file information in a new System object. The old |
| 399 | // System object, if any, is automatically deleted. |
| 400 | system = |
| 401 | std::make_unique<System>(std::move(rules), std::move(chassis)); |
| 402 | } |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 403 | } |
| 404 | catch (const std::exception& e) |
| 405 | { |
| 406 | // Log error messages in journal |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 407 | services.getJournal().logError(exception_utils::getMessages(e)); |
| 408 | services.getJournal().logError("Unable to load configuration file"); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 409 | |
Shawn McCarney | 415094c | 2021-02-15 11:08:19 -0600 | [diff] [blame] | 410 | // Log error |
| 411 | services.getErrorLogging().logConfigFileError(Entry::Level::Error, |
| 412 | services.getJournal()); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 416 | void Manager::waitUntilConfigFileLoaded() |
| 417 | { |
| 418 | // If config file not loaded and list of compatible system types is empty |
| 419 | if (!isConfigFileLoaded() && compatibleSystemTypes.empty()) |
| 420 | { |
| 421 | // Loop until compatible system types found or waited max amount of time |
| 422 | auto start = std::chrono::system_clock::now(); |
| 423 | std::chrono::system_clock::duration timeWaited{0}; |
| 424 | while (compatibleSystemTypes.empty() && |
| 425 | (timeWaited <= maxTimeToWaitForCompatTypes)) |
| 426 | { |
| 427 | // Try to find list of compatible system types |
| 428 | findCompatibleSystemTypes(); |
| 429 | if (!compatibleSystemTypes.empty()) |
| 430 | { |
| 431 | // Compatible system types found; try to load config file |
| 432 | loadConfigFile(); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | // Sleep 5 seconds |
| 437 | using namespace std::chrono_literals; |
| 438 | std::this_thread::sleep_for(5s); |
| 439 | } |
| 440 | timeWaited = std::chrono::system_clock::now() - start; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame] | 445 | } // namespace phosphor::power::regulators |