Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 1 | /** |
| 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 Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame^] | 18 | #include "fan.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 19 | #include "json_config.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 20 | #include "profile.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 21 | #include "zone.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 22 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 24 | #include <sdbusplus/bus.hpp> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 25 | #include <sdeventplus/event.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 26 | |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame^] | 27 | #include <algorithm> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 28 | #include <filesystem> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 29 | #include <vector> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 30 | |
| 31 | namespace phosphor::fan::control::json |
| 32 | { |
| 33 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 34 | using json = nlohmann::json; |
| 35 | |
| 36 | std::vector<std::string> Manager::_activeProfiles; |
| 37 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 38 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 39 | _bus(bus), _event(event) |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 40 | { |
| 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 Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 48 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 49 | // Parse and set the available profiles and which are active |
| 50 | setProfiles(); |
| 51 | |
| 52 | // Load the zone configurations |
| 53 | _zones = getConfig<Zone>(bus); |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame^] | 54 | |
| 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 Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | const std::vector<std::string>& Manager::getActiveProfiles() |
| 72 | { |
| 73 | return _activeProfiles; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | unsigned 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 Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 89 | void 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 Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 115 | } // namespace phosphor::fan::control::json |