Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [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 | */ |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 18 | #include "manager.hpp" |
| 19 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 20 | #include "action.hpp" |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 21 | #include "event.hpp" |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 22 | #include "fan.hpp" |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 23 | #include "group.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 24 | #include "json_config.hpp" |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 25 | #include "power_state.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 26 | #include "profile.hpp" |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 27 | #include "sdbusplus.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 28 | #include "zone.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 29 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 30 | #include <nlohmann/json.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 31 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 1542fb5 | 2021-06-10 14:09:09 -0500 | [diff] [blame] | 32 | #include <sdbusplus/server/manager.hpp> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 33 | #include <sdeventplus/event.hpp> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 34 | #include <sdeventplus/utility/timer.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 35 | |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 36 | #include <algorithm> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 37 | #include <chrono> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 38 | #include <filesystem> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 39 | #include <functional> |
| 40 | #include <map> |
| 41 | #include <memory> |
| 42 | #include <tuple> |
| 43 | #include <utility> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 44 | #include <vector> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 45 | |
| 46 | namespace phosphor::fan::control::json |
| 47 | { |
| 48 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 49 | using json = nlohmann::json; |
| 50 | |
| 51 | std::vector<std::string> Manager::_activeProfiles; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 52 | std::map<std::string, |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 53 | std::map<std::string, std::pair<bool, std::vector<std::string>>>> |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 54 | Manager::_servTree; |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 55 | std::map<std::string, |
| 56 | std::map<std::string, std::map<std::string, PropertyVariantType>>> |
| 57 | Manager::_objects; |
Matt Spinler | d76351b | 2021-08-05 16:23:09 -0500 | [diff] [blame] | 58 | std::unordered_map<std::string, PropertyVariantType> Manager::_parameters; |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 59 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 60 | Manager::Manager(const sdeventplus::Event& event) : |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 61 | _bus(util::SDBusPlus::getBus()), _event(event), |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 62 | _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true), |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 63 | _powerState(std::make_unique<PGoodState>( |
| 64 | util::SDBusPlus::getBus(), |
| 65 | std::bind(std::mem_fn(&Manager::powerStateChanged), this, |
| 66 | std::placeholders::_1))) |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 67 | {} |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 68 | |
| 69 | void Manager::sighupHandler(sdeventplus::source::Signal&, |
| 70 | const struct signalfd_siginfo*) |
| 71 | { |
| 72 | // Save current set of available and active profiles |
| 73 | std::map<configKey, std::unique_ptr<Profile>> profiles; |
| 74 | profiles.swap(_profiles); |
| 75 | std::vector<std::string> activeProfiles; |
| 76 | activeProfiles.swap(_activeProfiles); |
| 77 | |
| 78 | try |
| 79 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 80 | _loadAllowed = true; |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 81 | load(); |
| 82 | } |
| 83 | catch (std::runtime_error& re) |
| 84 | { |
| 85 | // Restore saved available and active profiles |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 86 | _loadAllowed = false; |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 87 | _profiles.swap(profiles); |
| 88 | _activeProfiles.swap(activeProfiles); |
| 89 | log<level::ERR>("Error reloading configs, no changes made", |
| 90 | entry("LOAD_ERROR=%s", re.what())); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void Manager::load() |
| 95 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 96 | if (_loadAllowed) |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 97 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 98 | // Load the available profiles and which are active |
| 99 | setProfiles(); |
| 100 | |
| 101 | // Load the zone configurations |
| 102 | auto zones = getConfig<Zone>(false, _event, this); |
| 103 | // Load the fan configurations and move each fan into its zone |
| 104 | auto fans = getConfig<Fan>(false); |
| 105 | for (auto& fan : fans) |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 106 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 107 | configKey fanProfile = |
| 108 | std::make_pair(fan.second->getZone(), fan.first.second); |
| 109 | auto itZone = std::find_if( |
| 110 | zones.begin(), zones.end(), [&fanProfile](const auto& zone) { |
| 111 | return Manager::inConfig(fanProfile, zone.first); |
| 112 | }); |
| 113 | if (itZone != zones.end()) |
Matthew Barth | 6f78730 | 2021-03-25 15:01:01 -0500 | [diff] [blame] | 114 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 115 | if (itZone->second->getTarget() != fan.second->getTarget() && |
| 116 | fan.second->getTarget() != 0) |
| 117 | { |
| 118 | // Update zone target to current target of the fan in the |
| 119 | // zone |
| 120 | itZone->second->setTarget(fan.second->getTarget()); |
| 121 | } |
| 122 | itZone->second->addFan(std::move(fan.second)); |
Matthew Barth | 6f78730 | 2021-03-25 15:01:01 -0500 | [diff] [blame] | 123 | } |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 124 | } |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 125 | |
| 126 | // Load any events configured |
| 127 | auto events = getConfig<Event>(true, this, zones); |
| 128 | |
| 129 | // Enable zones |
| 130 | _zones = std::move(zones); |
| 131 | std::for_each(_zones.begin(), _zones.end(), |
| 132 | [](const auto& entry) { entry.second->enable(); }); |
| 133 | |
| 134 | // Clear current timers and signal subscriptions before enabling events |
| 135 | // To save reloading services and/or objects into cache, do not clear |
| 136 | // cache |
| 137 | _timers.clear(); |
| 138 | _signals.clear(); |
| 139 | |
| 140 | // Enable events |
| 141 | _events = std::move(events); |
| 142 | std::for_each(_events.begin(), _events.end(), |
| 143 | [](const auto& entry) { entry.second->enable(); }); |
| 144 | |
| 145 | _loadAllowed = false; |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 146 | } |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 147 | } |
| 148 | |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 149 | void Manager::powerStateChanged(bool powerStateOn) |
| 150 | { |
| 151 | if (powerStateOn) |
| 152 | { |
| 153 | std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) { |
| 154 | entry.second->setTarget(entry.second->getPoweronTarget()); |
| 155 | }); |
| 156 | } |
| 157 | } |
| 158 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 159 | const std::vector<std::string>& Manager::getActiveProfiles() |
| 160 | { |
| 161 | return _activeProfiles; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 164 | bool Manager::inConfig(const configKey& input, const configKey& comp) |
| 165 | { |
| 166 | // Config names dont match, do not include in config |
| 167 | if (input.first != comp.first) |
| 168 | { |
| 169 | return false; |
| 170 | } |
| 171 | // No profiles specified by input config, can be used in any config |
| 172 | if (input.second.empty()) |
| 173 | { |
| 174 | return true; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | // Profiles must have one match in the other's profiles(and they must be |
| 179 | // an active profile) to be used in the config |
| 180 | return std::any_of( |
| 181 | input.second.begin(), input.second.end(), |
| 182 | [&comp](const auto& lProfile) { |
| 183 | return std::any_of( |
| 184 | comp.second.begin(), comp.second.end(), |
| 185 | [&lProfile](const auto& rProfile) { |
| 186 | if (lProfile != rProfile) |
| 187 | { |
| 188 | return false; |
| 189 | } |
| 190 | auto activeProfs = getActiveProfiles(); |
| 191 | return std::find(activeProfs.begin(), activeProfs.end(), |
| 192 | lProfile) != activeProfs.end(); |
| 193 | }); |
| 194 | }); |
| 195 | } |
| 196 | } |
| 197 | |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 198 | bool Manager::hasOwner(const std::string& path, const std::string& intf) |
| 199 | { |
| 200 | auto itServ = _servTree.find(path); |
| 201 | if (itServ == _servTree.end()) |
| 202 | { |
| 203 | // Path not found in cache, therefore owner missing |
| 204 | return false; |
| 205 | } |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 206 | for (const auto& service : itServ->second) |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 207 | { |
| 208 | auto itIntf = std::find_if( |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 209 | service.second.second.begin(), service.second.second.end(), |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 210 | [&intf](const auto& interface) { return intf == interface; }); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 211 | if (itIntf != std::end(service.second.second)) |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 212 | { |
| 213 | // Service found, return owner state |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 214 | return service.second.first; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | // Interface not found in cache, therefore owner missing |
| 218 | return false; |
| 219 | } |
| 220 | |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 221 | void Manager::setOwner(const std::string& path, const std::string& serv, |
| 222 | const std::string& intf, bool isOwned) |
| 223 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 224 | // Set owner state for specific object given |
| 225 | auto& ownIntf = _servTree[path][serv]; |
| 226 | ownIntf.first = isOwned; |
| 227 | auto itIntf = std::find_if( |
| 228 | ownIntf.second.begin(), ownIntf.second.end(), |
| 229 | [&intf](const auto& interface) { return intf == interface; }); |
| 230 | if (itIntf == std::end(ownIntf.second)) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 231 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 232 | ownIntf.second.emplace_back(intf); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 233 | } |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 234 | |
| 235 | // Update owner state on all entries of the same `serv` & `intf` |
| 236 | for (auto& itPath : _servTree) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 237 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 238 | if (itPath.first == path) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 239 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 240 | // Already set/updated owner on this path for `serv` & `intf` |
| 241 | continue; |
| 242 | } |
| 243 | for (auto& itServ : itPath.second) |
| 244 | { |
| 245 | if (itServ.first != serv) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 246 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 247 | continue; |
| 248 | } |
| 249 | auto itIntf = std::find_if( |
| 250 | itServ.second.second.begin(), itServ.second.second.end(), |
| 251 | [&intf](const auto& interface) { return intf == interface; }); |
| 252 | if (itIntf != std::end(itServ.second.second)) |
| 253 | { |
| 254 | itServ.second.first = isOwned; |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | const std::string& Manager::findService(const std::string& path, |
| 261 | const std::string& intf) |
| 262 | { |
| 263 | static const std::string empty = ""; |
| 264 | |
| 265 | auto itServ = _servTree.find(path); |
| 266 | if (itServ != _servTree.end()) |
| 267 | { |
| 268 | for (const auto& service : itServ->second) |
| 269 | { |
| 270 | auto itIntf = std::find_if( |
| 271 | service.second.second.begin(), service.second.second.end(), |
| 272 | [&intf](const auto& interface) { return intf == interface; }); |
| 273 | if (itIntf != std::end(service.second.second)) |
| 274 | { |
| 275 | // Service found, return service name |
| 276 | return service.first; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return empty; |
| 282 | } |
| 283 | |
Matthew Barth | 98f6fc1 | 2021-04-16 10:48:37 -0500 | [diff] [blame] | 284 | void Manager::addServices(const std::string& intf, int32_t depth) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 285 | { |
| 286 | // Get all subtree objects for the given interface |
Matt Spinler | 3483515 | 2021-07-01 12:28:58 -0600 | [diff] [blame] | 287 | auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(), |
| 288 | "/", intf, depth); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 289 | // Add what's returned to the cache of path->services |
| 290 | for (auto& itPath : objects) |
| 291 | { |
| 292 | auto pathIter = _servTree.find(itPath.first); |
| 293 | if (pathIter != _servTree.end()) |
| 294 | { |
| 295 | // Path found in cache |
| 296 | for (auto& itServ : itPath.second) |
| 297 | { |
| 298 | auto servIter = pathIter->second.find(itServ.first); |
| 299 | if (servIter != pathIter->second.end()) |
| 300 | { |
| 301 | // Service found in cache |
| 302 | for (auto& itIntf : itServ.second) |
| 303 | { |
| 304 | if (std::find(servIter->second.second.begin(), |
| 305 | servIter->second.second.end(), |
| 306 | itIntf) == servIter->second.second.end()) |
| 307 | { |
| 308 | // Add interface to cache |
| 309 | servIter->second.second.emplace_back(itIntf); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | // Service not found in cache |
| 316 | auto intfs = {intf}; |
| 317 | pathIter->second[itServ.first] = |
| 318 | std::make_pair(true, intfs); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | // Path not found in cache |
| 325 | auto intfs = {intf}; |
| 326 | _servTree[itPath.first] = { |
| 327 | {itPath.second.begin()->first, std::make_pair(true, intfs)}}; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | const std::string& Manager::getService(const std::string& path, |
| 333 | const std::string& intf) |
| 334 | { |
| 335 | // Retrieve service from cache |
| 336 | const auto& serviceName = findService(path, intf); |
| 337 | if (serviceName.empty()) |
| 338 | { |
Matthew Barth | 98f6fc1 | 2021-04-16 10:48:37 -0500 | [diff] [blame] | 339 | addServices(intf, 0); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 340 | return findService(path, intf); |
| 341 | } |
| 342 | |
| 343 | return serviceName; |
| 344 | } |
| 345 | |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 346 | std::vector<std::string> Manager::findPaths(const std::string& serv, |
| 347 | const std::string& intf) |
| 348 | { |
| 349 | std::vector<std::string> paths; |
| 350 | |
| 351 | for (const auto& path : _servTree) |
| 352 | { |
| 353 | auto itServ = path.second.find(serv); |
| 354 | if (itServ != path.second.end()) |
| 355 | { |
| 356 | if (std::find(itServ->second.second.begin(), |
| 357 | itServ->second.second.end(), |
| 358 | intf) != itServ->second.second.end()) |
| 359 | { |
| 360 | if (std::find(paths.begin(), paths.end(), path.first) == |
| 361 | paths.end()) |
| 362 | { |
| 363 | paths.push_back(path.first); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return paths; |
| 370 | } |
| 371 | |
| 372 | std::vector<std::string> Manager::getPaths(const std::string& serv, |
| 373 | const std::string& intf) |
| 374 | { |
| 375 | auto paths = findPaths(serv, intf); |
| 376 | if (paths.empty()) |
| 377 | { |
| 378 | addServices(intf, 0); |
| 379 | return findPaths(serv, intf); |
| 380 | } |
| 381 | |
| 382 | return paths; |
| 383 | } |
| 384 | |
| 385 | void Manager::addObjects(const std::string& path, const std::string& intf, |
| 386 | const std::string& prop) |
| 387 | { |
| 388 | auto service = getService(path, intf); |
| 389 | if (service.empty()) |
| 390 | { |
| 391 | // Log service not found for object |
Matt Spinler | 3483515 | 2021-07-01 12:28:58 -0600 | [diff] [blame] | 392 | log<level::DEBUG>( |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 393 | fmt::format("Unable to get service name for path {}, interface {}", |
| 394 | path, intf) |
| 395 | .c_str()); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager"); |
| 400 | if (objMgrPaths.empty()) |
| 401 | { |
| 402 | // No object manager interface provided by service? |
| 403 | // Attempt to retrieve property directly |
| 404 | auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>( |
| 405 | _bus, service, path, intf, prop); |
| 406 | _objects[path][intf][prop] = variant; |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | for (const auto& objMgrPath : objMgrPaths) |
| 411 | { |
| 412 | // Get all managed objects of service |
| 413 | auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>( |
| 414 | _bus, service, objMgrPath); |
| 415 | |
| 416 | // Add what's returned to the cache of objects |
| 417 | for (auto& object : objects) |
| 418 | { |
| 419 | auto itPath = _objects.find(object.first); |
| 420 | if (itPath != _objects.end()) |
| 421 | { |
| 422 | // Path found in cache |
| 423 | for (auto& interface : itPath->second) |
| 424 | { |
| 425 | auto itIntf = itPath->second.find(interface.first); |
| 426 | if (itIntf != itPath->second.end()) |
| 427 | { |
| 428 | // Interface found in cache |
| 429 | for (auto& property : itIntf->second) |
| 430 | { |
| 431 | auto itProp = itIntf->second.find(property.first); |
| 432 | if (itProp != itIntf->second.end()) |
| 433 | { |
| 434 | // Property found, update value |
| 435 | itProp->second = property.second; |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | itIntf->second.insert(property); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | // Interface not found in cache |
| 446 | itPath->second.insert(interface); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | // Path not found in cache |
| 453 | _objects.insert(object); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | const std::optional<PropertyVariantType> |
| 460 | Manager::getProperty(const std::string& path, const std::string& intf, |
| 461 | const std::string& prop) |
| 462 | { |
| 463 | // TODO Objects hosted by fan control (i.e. ThermalMode) are required to |
| 464 | // update the cache upon being set/updated |
| 465 | auto itPath = _objects.find(path); |
| 466 | if (itPath != _objects.end()) |
| 467 | { |
| 468 | auto itIntf = itPath->second.find(intf); |
| 469 | if (itIntf != itPath->second.end()) |
| 470 | { |
| 471 | auto itProp = itIntf->second.find(prop); |
| 472 | if (itProp != itIntf->second.end()) |
| 473 | { |
| 474 | return itProp->second; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return std::nullopt; |
| 480 | } |
| 481 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 482 | void Manager::addTimer(const TimerType type, |
| 483 | const std::chrono::microseconds interval, |
| 484 | std::unique_ptr<TimerPkg> pkg) |
| 485 | { |
| 486 | auto dataPtr = |
| 487 | std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg))); |
| 488 | Timer timer(_event, |
| 489 | std::bind(&Manager::timerExpired, this, std::ref(*dataPtr))); |
| 490 | if (type == TimerType::repeating) |
| 491 | { |
| 492 | timer.restart(interval); |
| 493 | } |
| 494 | else if (type == TimerType::oneshot) |
| 495 | { |
| 496 | timer.restartOnce(interval); |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | throw std::invalid_argument("Invalid Timer Type"); |
| 501 | } |
| 502 | _timers.emplace_back(std::move(dataPtr), std::move(timer)); |
| 503 | } |
| 504 | |
| 505 | void Manager::timerExpired(TimerData& data) |
| 506 | { |
| 507 | auto& actions = |
| 508 | std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second); |
| 509 | // Perform the actions in the timer data |
| 510 | std::for_each(actions.begin(), actions.end(), |
Matthew Barth | 00f6aa0 | 2021-04-09 10:49:47 -0500 | [diff] [blame] | 511 | [](auto& action) { action->run(); }); |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 512 | |
| 513 | // Remove oneshot timers after they expired |
| 514 | if (data.first == TimerType::oneshot) |
| 515 | { |
| 516 | auto itTimer = std::find_if( |
| 517 | _timers.begin(), _timers.end(), [&data](const auto& timer) { |
| 518 | return (data.first == timer.first->first && |
| 519 | (std::get<std::string>(data.second) == |
| 520 | std::get<std::string>(timer.first->second))); |
| 521 | }); |
| 522 | if (itTimer != std::end(_timers)) |
| 523 | { |
| 524 | _timers.erase(itTimer); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 529 | void Manager::handleSignal(sdbusplus::message::message& msg, |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 530 | const std::vector<SignalPkg>& pkgs) |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 531 | { |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 532 | for (auto& pkg : pkgs) |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 533 | { |
| 534 | // Handle the signal callback and only run the actions if the handler |
| 535 | // updated the cache for the given SignalObject |
| 536 | if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg), |
| 537 | *this)) |
| 538 | { |
| 539 | // Perform the actions in the handler package |
| 540 | auto& actions = std::get<SignalActions>(pkg); |
| 541 | std::for_each(actions.begin(), actions.end(), |
| 542 | [](auto& action) { action.get()->run(); }); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 547 | void Manager::setProfiles() |
| 548 | { |
| 549 | // Profiles JSON config file is optional |
| 550 | auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName, |
| 551 | Profile::confFileName, true); |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 552 | |
| 553 | _profiles.clear(); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 554 | if (!confFile.empty()) |
| 555 | { |
| 556 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 557 | { |
| 558 | auto obj = std::make_unique<Profile>(entry); |
| 559 | _profiles.emplace( |
| 560 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 561 | std::move(obj)); |
| 562 | } |
| 563 | } |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 564 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 565 | // Ensure all configurations use the same set of active profiles |
| 566 | // (In case a profile's active state changes during configuration) |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 567 | _activeProfiles.clear(); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 568 | for (const auto& profile : _profiles) |
| 569 | { |
| 570 | if (profile.second->isActive()) |
| 571 | { |
| 572 | _activeProfiles.emplace_back(profile.first.first); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 577 | } // namespace phosphor::fan::control::json |