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