blob: b1b5f91736406b582bdfcbccb3588ba7a1b7311b [file] [log] [blame]
Matthew Bartha227a162020-08-05 10:51:45 -05001/**
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 Barthb584d812021-03-11 15:55:04 -060016#include "config.h"
17
Matthew Bartha227a162020-08-05 10:51:45 -050018#include "manager.hpp"
19
Matthew Barthd9cb63b2021-03-24 14:36:49 -050020#include "action.hpp"
Matthew Barth44ab7692021-03-26 11:40:10 -050021#include "event.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060022#include "fan.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050023#include "group.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050024#include "json_config.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060025#include "profile.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060026#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050027
Matthew Barthacd737c2021-03-04 11:04:01 -060028#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050029#include <sdbusplus/bus.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060030#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050031#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050032
Matthew Barthde90fb42021-03-04 16:34:28 -060033#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050034#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050035#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050036#include <functional>
37#include <map>
38#include <memory>
39#include <tuple>
40#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060041#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050042
43namespace phosphor::fan::control::json
44{
45
Matthew Barthacd737c2021-03-04 11:04:01 -060046using json = nlohmann::json;
47
48std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060049std::map<std::string,
50 std::map<std::pair<std::string, bool>, std::vector<std::string>>>
51 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060052std::map<std::string,
53 std::map<std::string, std::map<std::string, PropertyVariantType>>>
54 Manager::_objects;
Matthew Barthacd737c2021-03-04 11:04:01 -060055
Matthew Barth06764942021-03-04 09:28:40 -060056Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthacd737c2021-03-04 11:04:01 -060057 _bus(bus), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050058{
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 Barth06764942021-03-04 09:28:40 -060066
Matthew Barthacd737c2021-03-04 11:04:01 -060067 // Parse and set the available profiles and which are active
68 setProfiles();
69
70 // Load the zone configurations
Matthew Barth603ef162021-03-24 15:34:53 -050071 _zones = getConfig<Zone>(false, bus, bus, event, this);
Matthew Barthde90fb42021-03-04 16:34:28 -060072
73 // Load the fan configurations and move each fan into its zone
Matthew Barth603ef162021-03-24 15:34:53 -050074 auto fans = getConfig<Fan>(false, bus, bus);
Matthew Barthde90fb42021-03-04 16:34:28 -060075 for (auto& fan : fans)
76 {
77 auto itZone =
78 std::find_if(_zones.begin(), _zones.end(),
79 [&fanZone = fan.second->getZone()](const auto& zone) {
80 return fanZone == zone.second->getName();
81 });
82 if (itZone != _zones.end())
83 {
Matthew Barth6f787302021-03-25 15:01:01 -050084 if (itZone->second->getTarget() != fan.second->getTarget() &&
85 fan.second->getTarget() != 0)
86 {
87 // Update zone target to current target of the fan in the zone
88 itZone->second->setTarget(fan.second->getTarget());
89 }
Matthew Barthde90fb42021-03-04 16:34:28 -060090 itZone->second->addFan(std::move(fan.second));
91 }
92 }
Matthew Barthb584d812021-03-11 15:55:04 -060093
Matthew Barth44ab7692021-03-26 11:40:10 -050094 // Load the configured groups that are copied into events where they're used
95 auto groups = getConfig<Group>(true, bus);
96
97 // Load any events configured
98 _events = getConfig<Event>(true, bus, bus, groups);
99
Matthew Barthb584d812021-03-11 15:55:04 -0600100 bus.request_name(CONTROL_BUSNAME);
Matthew Barthacd737c2021-03-04 11:04:01 -0600101}
102
103const std::vector<std::string>& Manager::getActiveProfiles()
104{
105 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500106}
107
Matthew Barth12cb1252021-03-08 16:47:30 -0600108bool Manager::hasOwner(const std::string& path, const std::string& intf)
109{
110 auto itServ = _servTree.find(path);
111 if (itServ == _servTree.end())
112 {
113 // Path not found in cache, therefore owner missing
114 return false;
115 }
116 for (const auto& serv : itServ->second)
117 {
118 auto itIntf = std::find_if(
119 serv.second.begin(), serv.second.end(),
120 [&intf](const auto& interface) { return intf == interface; });
121 if (itIntf != std::end(serv.second))
122 {
123 // Service found, return owner state
124 return serv.first.second;
125 }
126 }
127 // Interface not found in cache, therefore owner missing
128 return false;
129}
130
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500131void Manager::addTimer(const TimerType type,
132 const std::chrono::microseconds interval,
133 std::unique_ptr<TimerPkg> pkg)
134{
135 auto dataPtr =
136 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
137 Timer timer(_event,
138 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
139 if (type == TimerType::repeating)
140 {
141 timer.restart(interval);
142 }
143 else if (type == TimerType::oneshot)
144 {
145 timer.restartOnce(interval);
146 }
147 else
148 {
149 throw std::invalid_argument("Invalid Timer Type");
150 }
151 _timers.emplace_back(std::move(dataPtr), std::move(timer));
152}
153
154void Manager::timerExpired(TimerData& data)
155{
156 auto& actions =
157 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
158 // Perform the actions in the timer data
159 std::for_each(actions.begin(), actions.end(),
160 [zone = std::ref(std::get<Zone&>(data.second)),
161 group = std::cref(std::get<const Group&>(data.second))](
162 auto& action) { action->run(zone, group); });
163
164 // Remove oneshot timers after they expired
165 if (data.first == TimerType::oneshot)
166 {
167 auto itTimer = std::find_if(
168 _timers.begin(), _timers.end(), [&data](const auto& timer) {
169 return (data.first == timer.first->first &&
170 (std::get<std::string>(data.second) ==
171 std::get<std::string>(timer.first->second)));
172 });
173 if (itTimer != std::end(_timers))
174 {
175 _timers.erase(itTimer);
176 }
177 }
178}
179
Matthew Bartha227a162020-08-05 10:51:45 -0500180unsigned int Manager::getPowerOnDelay()
181{
182 auto powerOnDelay = 0;
183
184 // Parse optional "power_on_delay" from JSON object
185 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
186 {
187 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
188 }
189
190 return powerOnDelay;
191}
192
Matthew Barthacd737c2021-03-04 11:04:01 -0600193void Manager::setProfiles()
194{
195 // Profiles JSON config file is optional
196 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
197 Profile::confFileName, true);
198 if (!confFile.empty())
199 {
200 for (const auto& entry : fan::JsonConfig::load(confFile))
201 {
202 auto obj = std::make_unique<Profile>(entry);
203 _profiles.emplace(
204 std::make_pair(obj->getName(), obj->getProfiles()),
205 std::move(obj));
206 }
207 }
208 // Ensure all configurations use the same set of active profiles
209 // (In case a profile's active state changes during configuration)
210 for (const auto& profile : _profiles)
211 {
212 if (profile.second->isActive())
213 {
214 _activeProfiles.emplace_back(profile.first.first);
215 }
216 }
217}
218
Matthew Bartha227a162020-08-05 10:51:45 -0500219} // namespace phosphor::fan::control::json