Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 1 | /** |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 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" |
Mike Capps | bf8e56f | 2022-06-29 14:23:07 -0400 | [diff] [blame] | 21 | #include "dbus_paths.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 | bd52ed0 | 2022-02-07 15:15:10 -0600 | [diff] [blame] | 29 | #include "utils/flight_recorder.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 30 | #include "zone.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 31 | |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 32 | #include <systemd/sd-bus.h> |
| 33 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 34 | #include <nlohmann/json.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 35 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 1542fb5 | 2021-06-10 14:09:09 -0500 | [diff] [blame] | 36 | #include <sdbusplus/server/manager.hpp> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 37 | #include <sdeventplus/event.hpp> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 38 | #include <sdeventplus/utility/timer.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 39 | |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 40 | #include <algorithm> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 41 | #include <chrono> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 42 | #include <filesystem> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 43 | #include <functional> |
| 44 | #include <map> |
| 45 | #include <memory> |
| 46 | #include <tuple> |
| 47 | #include <utility> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 48 | #include <vector> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 49 | |
| 50 | namespace phosphor::fan::control::json |
| 51 | { |
| 52 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 53 | using json = nlohmann::json; |
| 54 | |
| 55 | std::vector<std::string> Manager::_activeProfiles; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 56 | std::map<std::string, |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 57 | std::map<std::string, std::pair<bool, std::vector<std::string>>>> |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 58 | Manager::_servTree; |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 59 | std::map<std::string, |
| 60 | std::map<std::string, std::map<std::string, PropertyVariantType>>> |
| 61 | Manager::_objects; |
Matt Spinler | d76351b | 2021-08-05 16:23:09 -0500 | [diff] [blame] | 62 | std::unordered_map<std::string, PropertyVariantType> Manager::_parameters; |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 63 | std::unordered_map<std::string, TriggerActions> Manager::_parameterTriggers; |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 64 | |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 65 | const std::string Manager::dumpFile = "/tmp/fan_control_dump.json"; |
| 66 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 67 | Manager::Manager(const sdeventplus::Event& event) : |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 68 | _bus(util::SDBusPlus::getBus()), _event(event), |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 69 | _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true), |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 70 | _powerState(std::make_unique<PGoodState>( |
| 71 | util::SDBusPlus::getBus(), |
| 72 | std::bind(std::mem_fn(&Manager::powerStateChanged), this, |
| 73 | std::placeholders::_1))) |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 74 | {} |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 75 | |
| 76 | void Manager::sighupHandler(sdeventplus::source::Signal&, |
| 77 | const struct signalfd_siginfo*) |
| 78 | { |
Matthew Barth | bd52ed0 | 2022-02-07 15:15:10 -0600 | [diff] [blame] | 79 | FlightRecorder::instance().log("main", "SIGHUP received"); |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 80 | // Save current set of available and active profiles |
| 81 | std::map<configKey, std::unique_ptr<Profile>> profiles; |
| 82 | profiles.swap(_profiles); |
| 83 | std::vector<std::string> activeProfiles; |
| 84 | activeProfiles.swap(_activeProfiles); |
| 85 | |
| 86 | try |
| 87 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 88 | _loadAllowed = true; |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 89 | load(); |
| 90 | } |
Patrick Williams | ddb773b | 2021-10-06 11:24:49 -0500 | [diff] [blame] | 91 | catch (const std::runtime_error& re) |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 92 | { |
| 93 | // Restore saved available and active profiles |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 94 | _loadAllowed = false; |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 95 | _profiles.swap(profiles); |
| 96 | _activeProfiles.swap(activeProfiles); |
| 97 | log<level::ERR>("Error reloading configs, no changes made", |
| 98 | entry("LOAD_ERROR=%s", re.what())); |
Matthew Barth | bd52ed0 | 2022-02-07 15:15:10 -0600 | [diff] [blame] | 99 | FlightRecorder::instance().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 100 | "main", std::format("Error reloading configs, no changes made: {}", |
Matthew Barth | bd52ed0 | 2022-02-07 15:15:10 -0600 | [diff] [blame] | 101 | re.what())); |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Matt Spinler | 27f5f4e | 2022-09-01 14:57:39 -0500 | [diff] [blame] | 105 | void Manager::dumpDebugData(sdeventplus::source::Signal&, |
| 106 | const struct signalfd_siginfo*) |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 107 | { |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 108 | json data; |
| 109 | FlightRecorder::instance().dump(data); |
Matt Spinler | b5c21a2 | 2021-10-14 16:52:12 -0500 | [diff] [blame] | 110 | dumpCache(data); |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 111 | |
Matt Spinler | 9db6dd1 | 2021-10-29 16:10:08 -0500 | [diff] [blame] | 112 | std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) { |
| 113 | data["zones"][zone.second->getName()] = zone.second->dump(); |
| 114 | }); |
| 115 | |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 116 | std::ofstream file{Manager::dumpFile}; |
| 117 | if (!file) |
| 118 | { |
| 119 | log<level::ERR>("Could not open file for fan dump"); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | file << std::setw(4) << data; |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Matt Spinler | b5c21a2 | 2021-10-14 16:52:12 -0500 | [diff] [blame] | 126 | void Manager::dumpCache(json& data) |
| 127 | { |
| 128 | auto& objects = data["objects"]; |
| 129 | for (const auto& [path, interfaces] : _objects) |
| 130 | { |
| 131 | auto& interfaceJSON = objects[path]; |
| 132 | |
| 133 | for (const auto& [interface, properties] : interfaces) |
| 134 | { |
| 135 | auto& propertyJSON = interfaceJSON[interface]; |
| 136 | for (const auto& [propName, propValue] : properties) |
| 137 | { |
| 138 | std::visit( |
| 139 | [&obj = propertyJSON[propName]](auto&& val) { obj = val; }, |
| 140 | propValue); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | auto& parameters = data["parameters"]; |
| 146 | for (const auto& [name, value] : _parameters) |
| 147 | { |
Matt Spinler | 29088e7 | 2021-11-08 16:23:27 -0600 | [diff] [blame] | 148 | std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value); |
Matt Spinler | b5c21a2 | 2021-10-14 16:52:12 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Matt Spinler | c3eb7b3 | 2022-04-25 15:44:16 -0500 | [diff] [blame] | 151 | std::for_each(_events.begin(), _events.end(), [&data](const auto& event) { |
| 152 | data["events"][event.second->getName()] = event.second->dump(); |
| 153 | }); |
| 154 | |
Matt Spinler | b5c21a2 | 2021-10-14 16:52:12 -0500 | [diff] [blame] | 155 | data["services"] = _servTree; |
| 156 | } |
| 157 | |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 158 | void Manager::load() |
| 159 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 160 | if (_loadAllowed) |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 161 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 162 | // Load the available profiles and which are active |
| 163 | setProfiles(); |
| 164 | |
| 165 | // Load the zone configurations |
| 166 | auto zones = getConfig<Zone>(false, _event, this); |
| 167 | // Load the fan configurations and move each fan into its zone |
| 168 | auto fans = getConfig<Fan>(false); |
| 169 | for (auto& fan : fans) |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 170 | { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 171 | configKey fanProfile = std::make_pair(fan.second->getZone(), |
| 172 | fan.first.second); |
| 173 | auto itZone = std::find_if(zones.begin(), zones.end(), |
| 174 | [&fanProfile](const auto& zone) { |
| 175 | return Manager::inConfig(fanProfile, zone.first); |
| 176 | }); |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 177 | if (itZone != zones.end()) |
Matthew Barth | 6f78730 | 2021-03-25 15:01:01 -0500 | [diff] [blame] | 178 | { |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 179 | if (itZone->second->getTarget() != fan.second->getTarget() && |
| 180 | fan.second->getTarget() != 0) |
| 181 | { |
| 182 | // Update zone target to current target of the fan in the |
| 183 | // zone |
| 184 | itZone->second->setTarget(fan.second->getTarget()); |
| 185 | } |
| 186 | itZone->second->addFan(std::move(fan.second)); |
Matthew Barth | 6f78730 | 2021-03-25 15:01:01 -0500 | [diff] [blame] | 187 | } |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 188 | } |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 189 | |
Matthew Barth | 3695ac3 | 2021-10-06 14:55:30 -0500 | [diff] [blame] | 190 | // Save all currently available groups, if any, then clear for reloading |
| 191 | auto groups = std::move(Event::getAllGroups(false)); |
| 192 | Event::clearAllGroups(); |
| 193 | |
| 194 | std::map<configKey, std::unique_ptr<Event>> events; |
| 195 | try |
| 196 | { |
| 197 | // Load any events configured, including all the groups |
| 198 | events = getConfig<Event>(true, this, zones); |
| 199 | } |
| 200 | catch (const std::runtime_error& re) |
| 201 | { |
| 202 | // Restore saved set of all available groups for current events |
| 203 | Event::setAllGroups(std::move(groups)); |
| 204 | throw re; |
| 205 | } |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 206 | |
| 207 | // Enable zones |
| 208 | _zones = std::move(zones); |
| 209 | std::for_each(_zones.begin(), _zones.end(), |
| 210 | [](const auto& entry) { entry.second->enable(); }); |
| 211 | |
| 212 | // Clear current timers and signal subscriptions before enabling events |
| 213 | // To save reloading services and/or objects into cache, do not clear |
| 214 | // cache |
| 215 | _timers.clear(); |
| 216 | _signals.clear(); |
| 217 | |
| 218 | // Enable events |
| 219 | _events = std::move(events); |
Matt Spinler | e56672d | 2023-01-10 16:05:11 -0600 | [diff] [blame] | 220 | FlightRecorder::instance().log("main", "Enabling events"); |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 221 | std::for_each(_events.begin(), _events.end(), |
| 222 | [](const auto& entry) { entry.second->enable(); }); |
Matt Spinler | e56672d | 2023-01-10 16:05:11 -0600 | [diff] [blame] | 223 | FlightRecorder::instance().log("main", "Done enabling events"); |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 224 | |
| 225 | _loadAllowed = false; |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 226 | } |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 229 | void Manager::powerStateChanged(bool powerStateOn) |
| 230 | { |
| 231 | if (powerStateOn) |
| 232 | { |
Matthew Barth | 6a2418a | 2021-09-01 09:10:09 -0500 | [diff] [blame] | 233 | if (_zones.empty()) |
| 234 | { |
| 235 | throw std::runtime_error("No configured zones found at poweron"); |
| 236 | } |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 237 | std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) { |
| 238 | entry.second->setTarget(entry.second->getPoweronTarget()); |
| 239 | }); |
Matt Spinler | d1f97f4 | 2021-10-29 16:19:24 -0500 | [diff] [blame] | 240 | |
| 241 | // Tell events to run their power on triggers |
| 242 | std::for_each(_events.begin(), _events.end(), |
| 243 | [](const auto& entry) { entry.second->powerOn(); }); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | // Tell events to run their power off triggers |
| 248 | std::for_each(_events.begin(), _events.end(), |
| 249 | [](const auto& entry) { entry.second->powerOff(); }); |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 253 | const std::vector<std::string>& Manager::getActiveProfiles() |
| 254 | { |
| 255 | return _activeProfiles; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 258 | bool Manager::inConfig(const configKey& input, const configKey& comp) |
| 259 | { |
| 260 | // Config names dont match, do not include in config |
| 261 | if (input.first != comp.first) |
| 262 | { |
| 263 | return false; |
| 264 | } |
| 265 | // No profiles specified by input config, can be used in any config |
| 266 | if (input.second.empty()) |
| 267 | { |
| 268 | return true; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | // Profiles must have one match in the other's profiles(and they must be |
| 273 | // an active profile) to be used in the config |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 274 | return std::any_of(input.second.begin(), input.second.end(), |
| 275 | [&comp](const auto& lProfile) { |
| 276 | return std::any_of(comp.second.begin(), comp.second.end(), |
| 277 | [&lProfile](const auto& rProfile) { |
| 278 | if (lProfile != rProfile) |
| 279 | { |
| 280 | return false; |
| 281 | } |
| 282 | auto activeProfs = getActiveProfiles(); |
| 283 | return std::find(activeProfs.begin(), activeProfs.end(), |
| 284 | lProfile) != activeProfs.end(); |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 285 | }); |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 286 | }); |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 290 | bool Manager::hasOwner(const std::string& path, const std::string& intf) |
| 291 | { |
| 292 | auto itServ = _servTree.find(path); |
| 293 | if (itServ == _servTree.end()) |
| 294 | { |
| 295 | // Path not found in cache, therefore owner missing |
| 296 | return false; |
| 297 | } |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 298 | for (const auto& service : itServ->second) |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 299 | { |
| 300 | auto itIntf = std::find_if( |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 301 | service.second.second.begin(), service.second.second.end(), |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 302 | [&intf](const auto& interface) { return intf == interface; }); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 303 | if (itIntf != std::end(service.second.second)) |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 304 | { |
| 305 | // Service found, return owner state |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 306 | return service.second.first; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | // Interface not found in cache, therefore owner missing |
| 310 | return false; |
| 311 | } |
| 312 | |
Matthew Barth | 6d8e2d3 | 2022-02-01 16:47:08 -0600 | [diff] [blame] | 313 | void Manager::setOwner(const std::string& serv, bool hasOwner) |
| 314 | { |
| 315 | // Update owner state on all entries of `serv` |
| 316 | for (auto& itPath : _servTree) |
| 317 | { |
| 318 | auto itServ = itPath.second.find(serv); |
| 319 | if (itServ != itPath.second.end()) |
| 320 | { |
| 321 | itServ->second.first = hasOwner; |
| 322 | |
| 323 | // Remove associated interfaces from object cache when service no |
| 324 | // longer has an owner |
| 325 | if (!hasOwner && _objects.find(itPath.first) != _objects.end()) |
| 326 | { |
| 327 | for (auto& intf : itServ->second.second) |
| 328 | { |
| 329 | _objects[itPath.first].erase(intf); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 336 | void Manager::setOwner(const std::string& path, const std::string& serv, |
| 337 | const std::string& intf, bool isOwned) |
| 338 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 339 | // Set owner state for specific object given |
| 340 | auto& ownIntf = _servTree[path][serv]; |
| 341 | ownIntf.first = isOwned; |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 342 | auto itIntf = std::find_if(ownIntf.second.begin(), ownIntf.second.end(), |
| 343 | [&intf](const auto& interface) { |
| 344 | return intf == interface; |
| 345 | }); |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 346 | if (itIntf == std::end(ownIntf.second)) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 347 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 348 | ownIntf.second.emplace_back(intf); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 349 | } |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 350 | |
| 351 | // Update owner state on all entries of the same `serv` & `intf` |
| 352 | for (auto& itPath : _servTree) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 353 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 354 | if (itPath.first == path) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 355 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 356 | // Already set/updated owner on this path for `serv` & `intf` |
| 357 | continue; |
| 358 | } |
| 359 | for (auto& itServ : itPath.second) |
| 360 | { |
| 361 | if (itServ.first != serv) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 362 | { |
Matthew Barth | 2a9e7b2 | 2021-05-17 11:54:54 -0500 | [diff] [blame] | 363 | continue; |
| 364 | } |
| 365 | auto itIntf = std::find_if( |
| 366 | itServ.second.second.begin(), itServ.second.second.end(), |
| 367 | [&intf](const auto& interface) { return intf == interface; }); |
| 368 | if (itIntf != std::end(itServ.second.second)) |
| 369 | { |
| 370 | itServ.second.first = isOwned; |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | const std::string& Manager::findService(const std::string& path, |
| 377 | const std::string& intf) |
| 378 | { |
| 379 | static const std::string empty = ""; |
| 380 | |
| 381 | auto itServ = _servTree.find(path); |
| 382 | if (itServ != _servTree.end()) |
| 383 | { |
| 384 | for (const auto& service : itServ->second) |
| 385 | { |
| 386 | auto itIntf = std::find_if( |
| 387 | service.second.second.begin(), service.second.second.end(), |
| 388 | [&intf](const auto& interface) { return intf == interface; }); |
| 389 | if (itIntf != std::end(service.second.second)) |
| 390 | { |
| 391 | // Service found, return service name |
| 392 | return service.first; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return empty; |
| 398 | } |
| 399 | |
Matthew Barth | 98f6fc1 | 2021-04-16 10:48:37 -0500 | [diff] [blame] | 400 | void Manager::addServices(const std::string& intf, int32_t depth) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 401 | { |
| 402 | // Get all subtree objects for the given interface |
Matt Spinler | 3483515 | 2021-07-01 12:28:58 -0600 | [diff] [blame] | 403 | auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(), |
| 404 | "/", intf, depth); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 405 | // Add what's returned to the cache of path->services |
| 406 | for (auto& itPath : objects) |
| 407 | { |
| 408 | auto pathIter = _servTree.find(itPath.first); |
| 409 | if (pathIter != _servTree.end()) |
| 410 | { |
| 411 | // Path found in cache |
| 412 | for (auto& itServ : itPath.second) |
| 413 | { |
| 414 | auto servIter = pathIter->second.find(itServ.first); |
| 415 | if (servIter != pathIter->second.end()) |
| 416 | { |
Matt Spinler | d9f85c9 | 2022-04-07 12:55:48 -0500 | [diff] [blame] | 417 | if (std::find(servIter->second.second.begin(), |
| 418 | servIter->second.second.end(), |
| 419 | intf) == servIter->second.second.end()) |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 420 | { |
Matt Spinler | d9f85c9 | 2022-04-07 12:55:48 -0500 | [diff] [blame] | 421 | // Add interface to cache |
| 422 | servIter->second.second.emplace_back(intf); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | // Service not found in cache |
| 428 | auto intfs = {intf}; |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 429 | pathIter->second[itServ.first] = std::make_pair(true, |
| 430 | intfs); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | // Path not found in cache |
| 437 | auto intfs = {intf}; |
Matt Spinler | a0b8a68 | 2021-10-14 15:38:48 -0500 | [diff] [blame] | 438 | for (const auto& [servName, servIntfs] : itPath.second) |
| 439 | { |
| 440 | _servTree[itPath.first][servName] = std::make_pair(true, intfs); |
| 441 | } |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | const std::string& Manager::getService(const std::string& path, |
| 447 | const std::string& intf) |
| 448 | { |
| 449 | // Retrieve service from cache |
| 450 | const auto& serviceName = findService(path, intf); |
| 451 | if (serviceName.empty()) |
| 452 | { |
Matthew Barth | 98f6fc1 | 2021-04-16 10:48:37 -0500 | [diff] [blame] | 453 | addServices(intf, 0); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 454 | return findService(path, intf); |
| 455 | } |
| 456 | |
| 457 | return serviceName; |
| 458 | } |
| 459 | |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 460 | std::vector<std::string> Manager::findPaths(const std::string& serv, |
| 461 | const std::string& intf) |
| 462 | { |
| 463 | std::vector<std::string> paths; |
| 464 | |
| 465 | for (const auto& path : _servTree) |
| 466 | { |
| 467 | auto itServ = path.second.find(serv); |
| 468 | if (itServ != path.second.end()) |
| 469 | { |
| 470 | if (std::find(itServ->second.second.begin(), |
| 471 | itServ->second.second.end(), |
| 472 | intf) != itServ->second.second.end()) |
| 473 | { |
| 474 | if (std::find(paths.begin(), paths.end(), path.first) == |
| 475 | paths.end()) |
| 476 | { |
| 477 | paths.push_back(path.first); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return paths; |
| 484 | } |
| 485 | |
| 486 | std::vector<std::string> Manager::getPaths(const std::string& serv, |
| 487 | const std::string& intf) |
| 488 | { |
| 489 | auto paths = findPaths(serv, intf); |
| 490 | if (paths.empty()) |
| 491 | { |
| 492 | addServices(intf, 0); |
| 493 | return findPaths(serv, intf); |
| 494 | } |
| 495 | |
| 496 | return paths; |
| 497 | } |
| 498 | |
Mike Capps | 1a19ead | 2021-10-22 09:15:14 -0400 | [diff] [blame] | 499 | void Manager::insertFilteredObjects(ManagedObjects& ref) |
| 500 | { |
Matt Spinler | c2c2db7 | 2022-04-07 13:59:37 -0500 | [diff] [blame] | 501 | // Filter out objects that aren't part of a group |
| 502 | const auto& allGroupMembers = Group::getAllMembers(); |
| 503 | auto it = ref.begin(); |
| 504 | |
| 505 | while (it != ref.end()) |
| 506 | { |
| 507 | if (allGroupMembers.find(it->first) == allGroupMembers.end()) |
| 508 | { |
| 509 | it = ref.erase(it); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | it++; |
| 514 | } |
| 515 | } |
| 516 | |
Mike Capps | 1a19ead | 2021-10-22 09:15:14 -0400 | [diff] [blame] | 517 | for (auto& [path, pathMap] : ref) |
| 518 | { |
| 519 | for (auto& [intf, intfMap] : pathMap) |
| 520 | { |
| 521 | // for each property on this path+interface |
| 522 | for (auto& [prop, value] : intfMap) |
| 523 | { |
| 524 | setProperty(path, intf, prop, value); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 530 | void Manager::addObjects(const std::string& path, const std::string& intf, |
Matt Spinler | 9ac325c | 2022-04-25 14:13:49 -0500 | [diff] [blame] | 531 | const std::string& prop, |
| 532 | const std::string& serviceName) |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 533 | { |
Matt Spinler | 9ac325c | 2022-04-25 14:13:49 -0500 | [diff] [blame] | 534 | auto service = serviceName; |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 535 | if (service.empty()) |
| 536 | { |
Matt Spinler | 9ac325c | 2022-04-25 14:13:49 -0500 | [diff] [blame] | 537 | service = getService(path, intf); |
| 538 | if (service.empty()) |
| 539 | { |
| 540 | // Log service not found for object |
| 541 | log<level::DEBUG>( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 542 | std::format( |
Matt Spinler | 9ac325c | 2022-04-25 14:13:49 -0500 | [diff] [blame] | 543 | "Unable to get service name for path {}, interface {}", |
| 544 | path, intf) |
| 545 | .c_str()); |
| 546 | return; |
| 547 | } |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | // The service is known, so the service cache can be |
| 552 | // populated even if the path itself isn't present. |
| 553 | const auto& s = findService(path, intf); |
| 554 | if (s.empty()) |
| 555 | { |
| 556 | addServices(intf, 0); |
| 557 | } |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager"); |
| 561 | if (objMgrPaths.empty()) |
| 562 | { |
| 563 | // No object manager interface provided by service? |
| 564 | // Attempt to retrieve property directly |
Matt Spinler | f16f063 | 2022-05-09 14:27:46 -0500 | [diff] [blame] | 565 | try |
| 566 | { |
| 567 | auto value = |
| 568 | util::SDBusPlus::getPropertyVariant<PropertyVariantType>( |
| 569 | _bus, service, path, intf, prop); |
Mike Capps | 1a19ead | 2021-10-22 09:15:14 -0400 | [diff] [blame] | 570 | |
Matt Spinler | f16f063 | 2022-05-09 14:27:46 -0500 | [diff] [blame] | 571 | setProperty(path, intf, prop, value); |
| 572 | } |
| 573 | catch (const std::exception& e) |
| 574 | {} |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | |
| 578 | for (const auto& objMgrPath : objMgrPaths) |
| 579 | { |
| 580 | // Get all managed objects of service |
| 581 | auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>( |
| 582 | _bus, service, objMgrPath); |
| 583 | |
Matt Spinler | c2c2db7 | 2022-04-07 13:59:37 -0500 | [diff] [blame] | 584 | // insert all objects that are in groups but remove any NaN values |
Mike Capps | 1a19ead | 2021-10-22 09:15:14 -0400 | [diff] [blame] | 585 | insertFilteredObjects(objects); |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | const std::optional<PropertyVariantType> |
| 590 | Manager::getProperty(const std::string& path, const std::string& intf, |
| 591 | const std::string& prop) |
| 592 | { |
| 593 | // TODO Objects hosted by fan control (i.e. ThermalMode) are required to |
| 594 | // update the cache upon being set/updated |
| 595 | auto itPath = _objects.find(path); |
| 596 | if (itPath != _objects.end()) |
| 597 | { |
| 598 | auto itIntf = itPath->second.find(intf); |
| 599 | if (itIntf != itPath->second.end()) |
| 600 | { |
| 601 | auto itProp = itIntf->second.find(prop); |
| 602 | if (itProp != itIntf->second.end()) |
| 603 | { |
| 604 | return itProp->second; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return std::nullopt; |
| 610 | } |
| 611 | |
Mike Capps | 1a19ead | 2021-10-22 09:15:14 -0400 | [diff] [blame] | 612 | void Manager::setProperty(const std::string& path, const std::string& intf, |
| 613 | const std::string& prop, PropertyVariantType value) |
| 614 | { |
| 615 | // filter NaNs out of the cache |
| 616 | if (PropertyContainsNan(value)) |
| 617 | { |
| 618 | // dont use operator [] if paths dont exist |
| 619 | if (_objects.find(path) != _objects.end() && |
| 620 | _objects[path].find(intf) != _objects[path].end()) |
| 621 | { |
| 622 | _objects[path][intf].erase(prop); |
| 623 | } |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | _objects[path][intf][prop] = std::move(value); |
| 628 | } |
| 629 | } |
| 630 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 631 | void Manager::addTimer(const TimerType type, |
| 632 | const std::chrono::microseconds interval, |
| 633 | std::unique_ptr<TimerPkg> pkg) |
| 634 | { |
| 635 | auto dataPtr = |
| 636 | std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg))); |
| 637 | Timer timer(_event, |
| 638 | std::bind(&Manager::timerExpired, this, std::ref(*dataPtr))); |
| 639 | if (type == TimerType::repeating) |
| 640 | { |
| 641 | timer.restart(interval); |
| 642 | } |
| 643 | else if (type == TimerType::oneshot) |
| 644 | { |
| 645 | timer.restartOnce(interval); |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | throw std::invalid_argument("Invalid Timer Type"); |
| 650 | } |
| 651 | _timers.emplace_back(std::move(dataPtr), std::move(timer)); |
| 652 | } |
| 653 | |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 654 | void Manager::addGroups(const std::vector<Group>& groups) |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 655 | { |
Matthew Barth | 1a5c623 | 2022-02-14 14:28:41 -0600 | [diff] [blame] | 656 | std::string lastServ; |
| 657 | std::vector<std::string> objMgrPaths; |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 658 | std::set<std::string> services; |
| 659 | for (const auto& group : groups) |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 660 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 661 | for (const auto& member : group.getMembers()) |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 662 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 663 | try |
Matthew Barth | 95d7349 | 2022-02-09 11:30:35 -0600 | [diff] [blame] | 664 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 665 | auto service = group.getService(); |
| 666 | if (service.empty()) |
Matthew Barth | 55627ad | 2021-12-02 22:28:29 -0600 | [diff] [blame] | 667 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 668 | service = getService(member, group.getInterface()); |
Matthew Barth | 1a5c623 | 2022-02-14 14:28:41 -0600 | [diff] [blame] | 669 | } |
Matthew Barth | 55627ad | 2021-12-02 22:28:29 -0600 | [diff] [blame] | 670 | |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 671 | if (!service.empty()) |
Matthew Barth | 1a5c623 | 2022-02-14 14:28:41 -0600 | [diff] [blame] | 672 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 673 | if (lastServ != service) |
Matthew Barth | 1a5c623 | 2022-02-14 14:28:41 -0600 | [diff] [blame] | 674 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 675 | objMgrPaths = getPaths( |
| 676 | service, "org.freedesktop.DBus.ObjectManager"); |
| 677 | lastServ = service; |
| 678 | } |
Matthew Barth | 1a5c623 | 2022-02-14 14:28:41 -0600 | [diff] [blame] | 679 | |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 680 | // Look for the ObjectManager as an ancestor from the |
| 681 | // member. |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 682 | auto hasObjMgr = std::any_of(objMgrPaths.begin(), |
| 683 | objMgrPaths.end(), |
| 684 | [&member](const auto& path) { |
| 685 | return member.find(path) != std::string::npos; |
| 686 | }); |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 687 | |
| 688 | if (!hasObjMgr) |
| 689 | { |
| 690 | // No object manager interface provided for group member |
| 691 | // Attempt to retrieve group member property directly |
Matt Spinler | f16f063 | 2022-05-09 14:27:46 -0500 | [diff] [blame] | 692 | try |
| 693 | { |
| 694 | auto value = util::SDBusPlus::getPropertyVariant< |
| 695 | PropertyVariantType>(_bus, service, member, |
| 696 | group.getInterface(), |
| 697 | group.getProperty()); |
| 698 | setProperty(member, group.getInterface(), |
| 699 | group.getProperty(), value); |
| 700 | } |
| 701 | catch (const std::exception& e) |
| 702 | {} |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 703 | continue; |
| 704 | } |
| 705 | |
| 706 | if (services.find(service) == services.end()) |
| 707 | { |
| 708 | services.insert(service); |
| 709 | for (const auto& objMgrPath : objMgrPaths) |
| 710 | { |
| 711 | // Get all managed objects from the service |
| 712 | auto objects = util::SDBusPlus::getManagedObjects< |
| 713 | PropertyVariantType>(_bus, service, objMgrPath); |
| 714 | |
| 715 | // Insert objects into cache |
| 716 | insertFilteredObjects(objects); |
| 717 | } |
Matthew Barth | 1a5c623 | 2022-02-14 14:28:41 -0600 | [diff] [blame] | 718 | } |
Matthew Barth | 55627ad | 2021-12-02 22:28:29 -0600 | [diff] [blame] | 719 | } |
| 720 | } |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 721 | catch (const util::DBusError&) |
| 722 | { |
| 723 | // No service or property found for group member with the |
| 724 | // group's configured interface |
| 725 | continue; |
| 726 | } |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 731 | void Manager::timerExpired(TimerData& data) |
| 732 | { |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 733 | if (std::get<bool>(data.second)) |
| 734 | { |
Matthew Barth | 2f359f7 | 2022-02-15 10:00:26 -0600 | [diff] [blame] | 735 | addGroups(std::get<const std::vector<Group>&>(data.second)); |
Matt Spinler | ade0c37 | 2021-10-28 16:09:44 -0500 | [diff] [blame] | 736 | } |
| 737 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 738 | auto& actions = |
| 739 | std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second); |
| 740 | // Perform the actions in the timer data |
| 741 | std::for_each(actions.begin(), actions.end(), |
Matthew Barth | 00f6aa0 | 2021-04-09 10:49:47 -0500 | [diff] [blame] | 742 | [](auto& action) { action->run(); }); |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 743 | |
| 744 | // Remove oneshot timers after they expired |
| 745 | if (data.first == TimerType::oneshot) |
| 746 | { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 747 | auto itTimer = std::find_if(_timers.begin(), _timers.end(), |
| 748 | [&data](const auto& timer) { |
| 749 | return (data.first == timer.first->first && |
| 750 | (std::get<std::string>(data.second) == |
| 751 | std::get<std::string>(timer.first->second))); |
| 752 | }); |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 753 | if (itTimer != std::end(_timers)) |
| 754 | { |
| 755 | _timers.erase(itTimer); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 760 | void Manager::handleSignal(sdbusplus::message_t& msg, |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 761 | const std::vector<SignalPkg>* pkgs) |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 762 | { |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 763 | for (auto& pkg : *pkgs) |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 764 | { |
| 765 | // Handle the signal callback and only run the actions if the handler |
| 766 | // updated the cache for the given SignalObject |
| 767 | if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg), |
| 768 | *this)) |
| 769 | { |
| 770 | // Perform the actions in the handler package |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 771 | auto& actions = std::get<TriggerActions>(pkg); |
Matthew Barth | c3a6908 | 2021-11-15 14:32:48 -0600 | [diff] [blame] | 772 | std::for_each(actions.begin(), actions.end(), [](auto& action) { |
| 773 | if (action.get()) |
| 774 | { |
| 775 | action.get()->run(); |
| 776 | } |
| 777 | }); |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 778 | } |
Matthew Barth | c024d78 | 2021-11-09 16:15:49 -0600 | [diff] [blame] | 779 | // Only rewind message when not last package |
| 780 | if (&pkg != &pkgs->back()) |
| 781 | { |
| 782 | sd_bus_message_rewind(msg.get(), true); |
| 783 | } |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 787 | void Manager::setProfiles() |
| 788 | { |
| 789 | // Profiles JSON config file is optional |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 790 | auto confFile = fan::JsonConfig::getConfFile(confAppName, |
| 791 | Profile::confFileName, true); |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 792 | |
| 793 | _profiles.clear(); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 794 | if (!confFile.empty()) |
| 795 | { |
| 796 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 797 | { |
| 798 | auto obj = std::make_unique<Profile>(entry); |
| 799 | _profiles.emplace( |
| 800 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 801 | std::move(obj)); |
| 802 | } |
| 803 | } |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 804 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 805 | // Ensure all configurations use the same set of active profiles |
| 806 | // (In case a profile's active state changes during configuration) |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 807 | _activeProfiles.clear(); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 808 | for (const auto& profile : _profiles) |
| 809 | { |
| 810 | if (profile.second->isActive()) |
| 811 | { |
| 812 | _activeProfiles.emplace_back(profile.first.first); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 817 | void Manager::addParameterTrigger( |
| 818 | const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions) |
| 819 | { |
| 820 | auto it = _parameterTriggers.find(name); |
| 821 | if (it != _parameterTriggers.end()) |
| 822 | { |
| 823 | std::for_each(actions.begin(), actions.end(), |
| 824 | [&actList = it->second](auto& action) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 825 | actList.emplace_back(std::ref(action)); |
| 826 | }); |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 827 | } |
| 828 | else |
| 829 | { |
| 830 | TriggerActions triggerActions; |
| 831 | std::for_each(actions.begin(), actions.end(), |
| 832 | [&triggerActions](auto& action) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 833 | triggerActions.emplace_back(std::ref(action)); |
| 834 | }); |
Matt Spinler | d0ba86a | 2021-11-09 10:09:13 -0600 | [diff] [blame] | 835 | _parameterTriggers[name] = std::move(triggerActions); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | void Manager::runParameterActions(const std::string& name) |
| 840 | { |
| 841 | auto it = _parameterTriggers.find(name); |
| 842 | if (it != _parameterTriggers.end()) |
| 843 | { |
| 844 | std::for_each(it->second.begin(), it->second.end(), |
| 845 | [](auto& action) { action.get()->run(); }); |
| 846 | } |
| 847 | } |
| 848 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 849 | } // namespace phosphor::fan::control::json |