blob: 804b15e92bcdece4c24046480818a607d02303d8 [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
18#include "json_config.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060019#include "profile.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060020#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050021
Matthew Barthacd737c2021-03-04 11:04:01 -060022#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050023#include <sdbusplus/bus.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060024#include <sdeventplus/event.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050025
26#include <filesystem>
Matthew Barth06764942021-03-04 09:28:40 -060027#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050028
29namespace phosphor::fan::control::json
30{
31
Matthew Barthacd737c2021-03-04 11:04:01 -060032using json = nlohmann::json;
33
34std::vector<std::string> Manager::_activeProfiles;
35
Matthew Barth06764942021-03-04 09:28:40 -060036Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthacd737c2021-03-04 11:04:01 -060037 _bus(bus), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050038{
39 // Manager JSON config file is optional
40 auto confFile =
41 fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true);
42 if (!confFile.empty())
43 {
44 _jsonObj = fan::JsonConfig::load(confFile);
45 }
Matthew Barth06764942021-03-04 09:28:40 -060046
Matthew Barthacd737c2021-03-04 11:04:01 -060047 // Parse and set the available profiles and which are active
48 setProfiles();
49
50 // Load the zone configurations
51 _zones = getConfig<Zone>(bus);
52}
53
54const std::vector<std::string>& Manager::getActiveProfiles()
55{
56 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -050057}
58
59unsigned int Manager::getPowerOnDelay()
60{
61 auto powerOnDelay = 0;
62
63 // Parse optional "power_on_delay" from JSON object
64 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
65 {
66 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
67 }
68
69 return powerOnDelay;
70}
71
Matthew Barthacd737c2021-03-04 11:04:01 -060072void Manager::setProfiles()
73{
74 // Profiles JSON config file is optional
75 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
76 Profile::confFileName, true);
77 if (!confFile.empty())
78 {
79 for (const auto& entry : fan::JsonConfig::load(confFile))
80 {
81 auto obj = std::make_unique<Profile>(entry);
82 _profiles.emplace(
83 std::make_pair(obj->getName(), obj->getProfiles()),
84 std::move(obj));
85 }
86 }
87 // Ensure all configurations use the same set of active profiles
88 // (In case a profile's active state changes during configuration)
89 for (const auto& profile : _profiles)
90 {
91 if (profile.second->isActive())
92 {
93 _activeProfiles.emplace_back(profile.first.first);
94 }
95 }
96}
97
Matthew Bartha227a162020-08-05 10:51:45 -050098} // namespace phosphor::fan::control::json