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