blob: cf8b0705418d2fc03da4ddd7ba73c176795aa630 [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 */
16#include "manager.hpp"
17
Matthew Barthde90fb42021-03-04 16:34:28 -060018#include "fan.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050019#include "json_config.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060020#include "profile.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060021#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050022
Matthew Barthacd737c2021-03-04 11:04:01 -060023#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050024#include <sdbusplus/bus.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060025#include <sdeventplus/event.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050026
Matthew Barthde90fb42021-03-04 16:34:28 -060027#include <algorithm>
Matthew Bartha227a162020-08-05 10:51:45 -050028#include <filesystem>
Matthew Barth06764942021-03-04 09:28:40 -060029#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050030
31namespace phosphor::fan::control::json
32{
33
Matthew Barthacd737c2021-03-04 11:04:01 -060034using json = nlohmann::json;
35
36std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060037std::map<std::string,
38 std::map<std::pair<std::string, bool>, std::vector<std::string>>>
39 Manager::_servTree;
Matthew Barthacd737c2021-03-04 11:04:01 -060040
Matthew Barth06764942021-03-04 09:28:40 -060041Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthacd737c2021-03-04 11:04:01 -060042 _bus(bus), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050043{
44 // Manager JSON config file is optional
45 auto confFile =
46 fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true);
47 if (!confFile.empty())
48 {
49 _jsonObj = fan::JsonConfig::load(confFile);
50 }
Matthew Barth06764942021-03-04 09:28:40 -060051
Matthew Barthacd737c2021-03-04 11:04:01 -060052 // Parse and set the available profiles and which are active
53 setProfiles();
54
55 // Load the zone configurations
56 _zones = getConfig<Zone>(bus);
Matthew Barthde90fb42021-03-04 16:34:28 -060057
58 // Load the fan configurations and move each fan into its zone
59 auto fans = getConfig<Fan>(bus);
60 for (auto& fan : fans)
61 {
62 auto itZone =
63 std::find_if(_zones.begin(), _zones.end(),
64 [&fanZone = fan.second->getZone()](const auto& zone) {
65 return fanZone == zone.second->getName();
66 });
67 if (itZone != _zones.end())
68 {
69 itZone->second->addFan(std::move(fan.second));
70 }
71 }
Matthew Barthacd737c2021-03-04 11:04:01 -060072}
73
74const std::vector<std::string>& Manager::getActiveProfiles()
75{
76 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -050077}
78
Matthew Barth12cb1252021-03-08 16:47:30 -060079bool Manager::hasOwner(const std::string& path, const std::string& intf)
80{
81 auto itServ = _servTree.find(path);
82 if (itServ == _servTree.end())
83 {
84 // Path not found in cache, therefore owner missing
85 return false;
86 }
87 for (const auto& serv : itServ->second)
88 {
89 auto itIntf = std::find_if(
90 serv.second.begin(), serv.second.end(),
91 [&intf](const auto& interface) { return intf == interface; });
92 if (itIntf != std::end(serv.second))
93 {
94 // Service found, return owner state
95 return serv.first.second;
96 }
97 }
98 // Interface not found in cache, therefore owner missing
99 return false;
100}
101
Matthew Bartha227a162020-08-05 10:51:45 -0500102unsigned int Manager::getPowerOnDelay()
103{
104 auto powerOnDelay = 0;
105
106 // Parse optional "power_on_delay" from JSON object
107 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
108 {
109 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
110 }
111
112 return powerOnDelay;
113}
114
Matthew Barthacd737c2021-03-04 11:04:01 -0600115void Manager::setProfiles()
116{
117 // Profiles JSON config file is optional
118 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
119 Profile::confFileName, true);
120 if (!confFile.empty())
121 {
122 for (const auto& entry : fan::JsonConfig::load(confFile))
123 {
124 auto obj = std::make_unique<Profile>(entry);
125 _profiles.emplace(
126 std::make_pair(obj->getName(), obj->getProfiles()),
127 std::move(obj));
128 }
129 }
130 // Ensure all configurations use the same set of active profiles
131 // (In case a profile's active state changes during configuration)
132 for (const auto& profile : _profiles)
133 {
134 if (profile.second->isActive())
135 {
136 _activeProfiles.emplace_back(profile.first.first);
137 }
138 }
139}
140
Matthew Bartha227a162020-08-05 10:51:45 -0500141} // namespace phosphor::fan::control::json