| 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 | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 26 | #include "zone.hpp" | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 27 |  | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 28 | #include <nlohmann/json.hpp> | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 29 | #include <sdbusplus/bus.hpp> | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 30 | #include <sdeventplus/event.hpp> | 
| Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 31 | #include <sdeventplus/utility/timer.hpp> | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 32 |  | 
| Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 33 | #include <algorithm> | 
| Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 34 | #include <chrono> | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 35 | #include <filesystem> | 
| Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 36 | #include <functional> | 
|  | 37 | #include <map> | 
|  | 38 | #include <memory> | 
|  | 39 | #include <tuple> | 
|  | 40 | #include <utility> | 
| Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 41 | #include <vector> | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 42 |  | 
|  | 43 | namespace phosphor::fan::control::json | 
|  | 44 | { | 
|  | 45 |  | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 46 | using json = nlohmann::json; | 
|  | 47 |  | 
|  | 48 | std::vector<std::string> Manager::_activeProfiles; | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 49 | std::map<std::string, | 
| Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame^] | 50 | std::map<std::string, std::pair<bool, std::vector<std::string>>>> | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 51 | Manager::_servTree; | 
| Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 52 | std::map<std::string, | 
|  | 53 | std::map<std::string, std::map<std::string, PropertyVariantType>>> | 
|  | 54 | Manager::_objects; | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 55 |  | 
| Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 56 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 57 | _bus(bus), _event(event) | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 58 | { | 
|  | 59 | // Manager JSON config file is optional | 
|  | 60 | auto confFile = | 
|  | 61 | fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true); | 
|  | 62 | if (!confFile.empty()) | 
|  | 63 | { | 
|  | 64 | _jsonObj = fan::JsonConfig::load(confFile); | 
|  | 65 | } | 
| Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 66 |  | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 67 | // Parse and set the available profiles and which are active | 
|  | 68 | setProfiles(); | 
|  | 69 |  | 
|  | 70 | // Load the zone configurations | 
| Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 71 | _zones = getConfig<Zone>(false, bus, bus, event, this); | 
| Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 72 |  | 
|  | 73 | // Load the fan configurations and move each fan into its zone | 
| Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 74 | auto fans = getConfig<Fan>(false, bus, bus); | 
| 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 | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 94 |  | 
| Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 95 | // Load the configured groups that are copied into events where they're used | 
|  | 96 | auto groups = getConfig<Group>(true, bus); | 
|  | 97 |  | 
|  | 98 | // Load any events configured | 
| Matthew Barth | 9f1632e | 2021-03-31 15:51:50 -0500 | [diff] [blame] | 99 | _events = getConfig<Event>(true, bus, bus, groups, _zones); | 
| Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 100 |  | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 101 | bus.request_name(CONTROL_BUSNAME); | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | const std::vector<std::string>& Manager::getActiveProfiles() | 
|  | 105 | { | 
|  | 106 | return _activeProfiles; | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 109 | bool Manager::inConfig(const configKey& input, const configKey& comp) | 
|  | 110 | { | 
|  | 111 | // Config names dont match, do not include in config | 
|  | 112 | if (input.first != comp.first) | 
|  | 113 | { | 
|  | 114 | return false; | 
|  | 115 | } | 
|  | 116 | // No profiles specified by input config, can be used in any config | 
|  | 117 | if (input.second.empty()) | 
|  | 118 | { | 
|  | 119 | return true; | 
|  | 120 | } | 
|  | 121 | else | 
|  | 122 | { | 
|  | 123 | // Profiles must have one match in the other's profiles(and they must be | 
|  | 124 | // an active profile) to be used in the config | 
|  | 125 | return std::any_of( | 
|  | 126 | input.second.begin(), input.second.end(), | 
|  | 127 | [&comp](const auto& lProfile) { | 
|  | 128 | return std::any_of( | 
|  | 129 | comp.second.begin(), comp.second.end(), | 
|  | 130 | [&lProfile](const auto& rProfile) { | 
|  | 131 | if (lProfile != rProfile) | 
|  | 132 | { | 
|  | 133 | return false; | 
|  | 134 | } | 
|  | 135 | auto activeProfs = getActiveProfiles(); | 
|  | 136 | return std::find(activeProfs.begin(), activeProfs.end(), | 
|  | 137 | lProfile) != activeProfs.end(); | 
|  | 138 | }); | 
|  | 139 | }); | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 |  | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 143 | bool Manager::hasOwner(const std::string& path, const std::string& intf) | 
|  | 144 | { | 
|  | 145 | auto itServ = _servTree.find(path); | 
|  | 146 | if (itServ == _servTree.end()) | 
|  | 147 | { | 
|  | 148 | // Path not found in cache, therefore owner missing | 
|  | 149 | return false; | 
|  | 150 | } | 
| Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame^] | 151 | for (const auto& service : itServ->second) | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 152 | { | 
|  | 153 | auto itIntf = std::find_if( | 
| Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame^] | 154 | service.second.second.begin(), service.second.second.end(), | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 155 | [&intf](const auto& interface) { return intf == interface; }); | 
| Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame^] | 156 | if (itIntf != std::end(service.second.second)) | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 157 | { | 
|  | 158 | // Service found, return owner state | 
| Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame^] | 159 | return service.second.first; | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 160 | } | 
|  | 161 | } | 
|  | 162 | // Interface not found in cache, therefore owner missing | 
|  | 163 | return false; | 
|  | 164 | } | 
|  | 165 |  | 
| Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame^] | 166 | void Manager::setOwner(const std::string& path, const std::string& serv, | 
|  | 167 | const std::string& intf, bool isOwned) | 
|  | 168 | { | 
|  | 169 | auto itServ = _servTree.find(path); | 
|  | 170 | if (itServ == _servTree.end()) | 
|  | 171 | { | 
|  | 172 | auto intfs = {intf}; | 
|  | 173 | _servTree[path] = {{serv, std::make_pair(isOwned, intfs)}}; | 
|  | 174 | return; | 
|  | 175 | } | 
|  | 176 | for (auto& service : itServ->second) | 
|  | 177 | { | 
|  | 178 | auto itIntf = std::find_if( | 
|  | 179 | service.second.second.begin(), service.second.second.end(), | 
|  | 180 | [&intf](const auto& interface) { return intf == interface; }); | 
|  | 181 | if (itIntf != std::end(service.second.second)) | 
|  | 182 | { | 
|  | 183 | if (service.first == serv) | 
|  | 184 | { | 
|  | 185 | service.second.first = isOwned; | 
|  | 186 | return; | 
|  | 187 | } | 
|  | 188 | } | 
|  | 189 | } | 
|  | 190 | auto intfs = {intf}; | 
|  | 191 | itServ->second[serv] = std::make_pair(isOwned, intfs); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | const std::string& Manager::findService(const std::string& path, | 
|  | 195 | const std::string& intf) | 
|  | 196 | { | 
|  | 197 | static const std::string empty = ""; | 
|  | 198 |  | 
|  | 199 | auto itServ = _servTree.find(path); | 
|  | 200 | if (itServ != _servTree.end()) | 
|  | 201 | { | 
|  | 202 | for (const auto& service : itServ->second) | 
|  | 203 | { | 
|  | 204 | auto itIntf = std::find_if( | 
|  | 205 | service.second.second.begin(), service.second.second.end(), | 
|  | 206 | [&intf](const auto& interface) { return intf == interface; }); | 
|  | 207 | if (itIntf != std::end(service.second.second)) | 
|  | 208 | { | 
|  | 209 | // Service found, return service name | 
|  | 210 | return service.first; | 
|  | 211 | } | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | return empty; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | void Manager::addServices(const std::string& path, const std::string& intf, | 
|  | 219 | int32_t depth) | 
|  | 220 | { | 
|  | 221 | // Get all subtree objects for the given interface | 
|  | 222 | auto objects = util::SDBusPlus::getSubTree(util::SDBusPlus::getBus(), "/", | 
|  | 223 | intf, depth); | 
|  | 224 | // Add what's returned to the cache of path->services | 
|  | 225 | for (auto& itPath : objects) | 
|  | 226 | { | 
|  | 227 | auto pathIter = _servTree.find(itPath.first); | 
|  | 228 | if (pathIter != _servTree.end()) | 
|  | 229 | { | 
|  | 230 | // Path found in cache | 
|  | 231 | for (auto& itServ : itPath.second) | 
|  | 232 | { | 
|  | 233 | auto servIter = pathIter->second.find(itServ.first); | 
|  | 234 | if (servIter != pathIter->second.end()) | 
|  | 235 | { | 
|  | 236 | // Service found in cache | 
|  | 237 | for (auto& itIntf : itServ.second) | 
|  | 238 | { | 
|  | 239 | if (std::find(servIter->second.second.begin(), | 
|  | 240 | servIter->second.second.end(), | 
|  | 241 | itIntf) == servIter->second.second.end()) | 
|  | 242 | { | 
|  | 243 | // Add interface to cache | 
|  | 244 | servIter->second.second.emplace_back(itIntf); | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 | } | 
|  | 248 | else | 
|  | 249 | { | 
|  | 250 | // Service not found in cache | 
|  | 251 | auto intfs = {intf}; | 
|  | 252 | pathIter->second[itServ.first] = | 
|  | 253 | std::make_pair(true, intfs); | 
|  | 254 | } | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 | else | 
|  | 258 | { | 
|  | 259 | // Path not found in cache | 
|  | 260 | auto intfs = {intf}; | 
|  | 261 | _servTree[itPath.first] = { | 
|  | 262 | {itPath.second.begin()->first, std::make_pair(true, intfs)}}; | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | const std::string& Manager::getService(const std::string& path, | 
|  | 268 | const std::string& intf) | 
|  | 269 | { | 
|  | 270 | // Retrieve service from cache | 
|  | 271 | const auto& serviceName = findService(path, intf); | 
|  | 272 | if (serviceName.empty()) | 
|  | 273 | { | 
|  | 274 | addServices(path, intf, 0); | 
|  | 275 | return findService(path, intf); | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | return serviceName; | 
|  | 279 | } | 
|  | 280 |  | 
| Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 281 | void Manager::addTimer(const TimerType type, | 
|  | 282 | const std::chrono::microseconds interval, | 
|  | 283 | std::unique_ptr<TimerPkg> pkg) | 
|  | 284 | { | 
|  | 285 | auto dataPtr = | 
|  | 286 | std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg))); | 
|  | 287 | Timer timer(_event, | 
|  | 288 | std::bind(&Manager::timerExpired, this, std::ref(*dataPtr))); | 
|  | 289 | if (type == TimerType::repeating) | 
|  | 290 | { | 
|  | 291 | timer.restart(interval); | 
|  | 292 | } | 
|  | 293 | else if (type == TimerType::oneshot) | 
|  | 294 | { | 
|  | 295 | timer.restartOnce(interval); | 
|  | 296 | } | 
|  | 297 | else | 
|  | 298 | { | 
|  | 299 | throw std::invalid_argument("Invalid Timer Type"); | 
|  | 300 | } | 
|  | 301 | _timers.emplace_back(std::move(dataPtr), std::move(timer)); | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | void Manager::timerExpired(TimerData& data) | 
|  | 305 | { | 
|  | 306 | auto& actions = | 
|  | 307 | std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second); | 
|  | 308 | // Perform the actions in the timer data | 
|  | 309 | std::for_each(actions.begin(), actions.end(), | 
| Matthew Barth | 00f6aa0 | 2021-04-09 10:49:47 -0500 | [diff] [blame] | 310 | [](auto& action) { action->run(); }); | 
| Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 311 |  | 
|  | 312 | // Remove oneshot timers after they expired | 
|  | 313 | if (data.first == TimerType::oneshot) | 
|  | 314 | { | 
|  | 315 | auto itTimer = std::find_if( | 
|  | 316 | _timers.begin(), _timers.end(), [&data](const auto& timer) { | 
|  | 317 | return (data.first == timer.first->first && | 
|  | 318 | (std::get<std::string>(data.second) == | 
|  | 319 | std::get<std::string>(timer.first->second))); | 
|  | 320 | }); | 
|  | 321 | if (itTimer != std::end(_timers)) | 
|  | 322 | { | 
|  | 323 | _timers.erase(itTimer); | 
|  | 324 | } | 
|  | 325 | } | 
|  | 326 | } | 
|  | 327 |  | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 328 | unsigned int Manager::getPowerOnDelay() | 
|  | 329 | { | 
|  | 330 | auto powerOnDelay = 0; | 
|  | 331 |  | 
|  | 332 | // Parse optional "power_on_delay" from JSON object | 
|  | 333 | if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay")) | 
|  | 334 | { | 
|  | 335 | powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>(); | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | return powerOnDelay; | 
|  | 339 | } | 
|  | 340 |  | 
| Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 341 | void Manager::setProfiles() | 
|  | 342 | { | 
|  | 343 | // Profiles JSON config file is optional | 
|  | 344 | auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName, | 
|  | 345 | Profile::confFileName, true); | 
|  | 346 | if (!confFile.empty()) | 
|  | 347 | { | 
|  | 348 | for (const auto& entry : fan::JsonConfig::load(confFile)) | 
|  | 349 | { | 
|  | 350 | auto obj = std::make_unique<Profile>(entry); | 
|  | 351 | _profiles.emplace( | 
|  | 352 | std::make_pair(obj->getName(), obj->getProfiles()), | 
|  | 353 | std::move(obj)); | 
|  | 354 | } | 
|  | 355 | } | 
|  | 356 | // Ensure all configurations use the same set of active profiles | 
|  | 357 | // (In case a profile's active state changes during configuration) | 
|  | 358 | for (const auto& profile : _profiles) | 
|  | 359 | { | 
|  | 360 | if (profile.second->isActive()) | 
|  | 361 | { | 
|  | 362 | _activeProfiles.emplace_back(profile.first.first); | 
|  | 363 | } | 
|  | 364 | } | 
|  | 365 | } | 
|  | 366 |  | 
| Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 367 | } // namespace phosphor::fan::control::json |