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