blob: 78aac2996992521af8d410a318ffed6ee6d29207 [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;
37
Matthew Barth06764942021-03-04 09:28:40 -060038Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthacd737c2021-03-04 11:04:01 -060039 _bus(bus), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050040{
41 // Manager JSON config file is optional
42 auto confFile =
43 fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true);
44 if (!confFile.empty())
45 {
46 _jsonObj = fan::JsonConfig::load(confFile);
47 }
Matthew Barth06764942021-03-04 09:28:40 -060048
Matthew Barthacd737c2021-03-04 11:04:01 -060049 // Parse and set the available profiles and which are active
50 setProfiles();
51
52 // Load the zone configurations
53 _zones = getConfig<Zone>(bus);
Matthew Barthde90fb42021-03-04 16:34:28 -060054
55 // Load the fan configurations and move each fan into its zone
56 auto fans = getConfig<Fan>(bus);
57 for (auto& fan : fans)
58 {
59 auto itZone =
60 std::find_if(_zones.begin(), _zones.end(),
61 [&fanZone = fan.second->getZone()](const auto& zone) {
62 return fanZone == zone.second->getName();
63 });
64 if (itZone != _zones.end())
65 {
66 itZone->second->addFan(std::move(fan.second));
67 }
68 }
Matthew Barthacd737c2021-03-04 11:04:01 -060069}
70
71const std::vector<std::string>& Manager::getActiveProfiles()
72{
73 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -050074}
75
76unsigned int Manager::getPowerOnDelay()
77{
78 auto powerOnDelay = 0;
79
80 // Parse optional "power_on_delay" from JSON object
81 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
82 {
83 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
84 }
85
86 return powerOnDelay;
87}
88
Matthew Barthacd737c2021-03-04 11:04:01 -060089void Manager::setProfiles()
90{
91 // Profiles JSON config file is optional
92 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
93 Profile::confFileName, true);
94 if (!confFile.empty())
95 {
96 for (const auto& entry : fan::JsonConfig::load(confFile))
97 {
98 auto obj = std::make_unique<Profile>(entry);
99 _profiles.emplace(
100 std::make_pair(obj->getName(), obj->getProfiles()),
101 std::move(obj));
102 }
103 }
104 // Ensure all configurations use the same set of active profiles
105 // (In case a profile's active state changes during configuration)
106 for (const auto& profile : _profiles)
107 {
108 if (profile.second->isActive())
109 {
110 _activeProfiles.emplace_back(profile.first.first);
111 }
112 }
113}
114
Matthew Bartha227a162020-08-05 10:51:45 -0500115} // namespace phosphor::fan::control::json