blob: 0cbb9d7c94e730adfc558ef38ab080fd01cb6cb2 [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 Barthde90fb42021-03-04 16:34:28 -060020#include "fan.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050021#include "json_config.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060022#include "profile.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060023#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050024
Matthew Barthacd737c2021-03-04 11:04:01 -060025#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050026#include <sdbusplus/bus.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060027#include <sdeventplus/event.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050028
Matthew Barthde90fb42021-03-04 16:34:28 -060029#include <algorithm>
Matthew Bartha227a162020-08-05 10:51:45 -050030#include <filesystem>
Matthew Barth06764942021-03-04 09:28:40 -060031#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050032
33namespace phosphor::fan::control::json
34{
35
Matthew Barthacd737c2021-03-04 11:04:01 -060036using json = nlohmann::json;
37
38std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060039std::map<std::string,
40 std::map<std::pair<std::string, bool>, std::vector<std::string>>>
41 Manager::_servTree;
Matthew Barthacd737c2021-03-04 11:04:01 -060042
Matthew Barth06764942021-03-04 09:28:40 -060043Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthacd737c2021-03-04 11:04:01 -060044 _bus(bus), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050045{
46 // Manager JSON config file is optional
47 auto confFile =
48 fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true);
49 if (!confFile.empty())
50 {
51 _jsonObj = fan::JsonConfig::load(confFile);
52 }
Matthew Barth06764942021-03-04 09:28:40 -060053
Matthew Barthacd737c2021-03-04 11:04:01 -060054 // Parse and set the available profiles and which are active
55 setProfiles();
56
57 // Load the zone configurations
58 _zones = getConfig<Zone>(bus);
Matthew Barthde90fb42021-03-04 16:34:28 -060059
60 // Load the fan configurations and move each fan into its zone
61 auto fans = getConfig<Fan>(bus);
62 for (auto& fan : fans)
63 {
64 auto itZone =
65 std::find_if(_zones.begin(), _zones.end(),
66 [&fanZone = fan.second->getZone()](const auto& zone) {
67 return fanZone == zone.second->getName();
68 });
69 if (itZone != _zones.end())
70 {
71 itZone->second->addFan(std::move(fan.second));
72 }
73 }
Matthew Barthb584d812021-03-11 15:55:04 -060074
75 bus.request_name(CONTROL_BUSNAME);
Matthew Barthacd737c2021-03-04 11:04:01 -060076}
77
78const std::vector<std::string>& Manager::getActiveProfiles()
79{
80 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -050081}
82
Matthew Barth12cb1252021-03-08 16:47:30 -060083bool Manager::hasOwner(const std::string& path, const std::string& intf)
84{
85 auto itServ = _servTree.find(path);
86 if (itServ == _servTree.end())
87 {
88 // Path not found in cache, therefore owner missing
89 return false;
90 }
91 for (const auto& serv : itServ->second)
92 {
93 auto itIntf = std::find_if(
94 serv.second.begin(), serv.second.end(),
95 [&intf](const auto& interface) { return intf == interface; });
96 if (itIntf != std::end(serv.second))
97 {
98 // Service found, return owner state
99 return serv.first.second;
100 }
101 }
102 // Interface not found in cache, therefore owner missing
103 return false;
104}
105
Matthew Bartha227a162020-08-05 10:51:45 -0500106unsigned int Manager::getPowerOnDelay()
107{
108 auto powerOnDelay = 0;
109
110 // Parse optional "power_on_delay" from JSON object
111 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
112 {
113 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
114 }
115
116 return powerOnDelay;
117}
118
Matthew Barthacd737c2021-03-04 11:04:01 -0600119void Manager::setProfiles()
120{
121 // Profiles JSON config file is optional
122 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
123 Profile::confFileName, true);
124 if (!confFile.empty())
125 {
126 for (const auto& entry : fan::JsonConfig::load(confFile))
127 {
128 auto obj = std::make_unique<Profile>(entry);
129 _profiles.emplace(
130 std::make_pair(obj->getName(), obj->getProfiles()),
131 std::move(obj));
132 }
133 }
134 // Ensure all configurations use the same set of active profiles
135 // (In case a profile's active state changes during configuration)
136 for (const auto& profile : _profiles)
137 {
138 if (profile.second->isActive())
139 {
140 _activeProfiles.emplace_back(profile.first.first);
141 }
142 }
143}
144
Matthew Bartha227a162020-08-05 10:51:45 -0500145} // namespace phosphor::fan::control::json