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 | |
| 18 | #include "json_config.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 19 | #include "profile.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 20 | #include "zone.hpp" |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 21 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 22 | #include <nlohmann/json.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 24 | #include <sdeventplus/event.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 25 | |
| 26 | #include <filesystem> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 27 | #include <vector> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 28 | |
| 29 | namespace phosphor::fan::control::json |
| 30 | { |
| 31 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 32 | using json = nlohmann::json; |
| 33 | |
| 34 | std::vector<std::string> Manager::_activeProfiles; |
| 35 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 36 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 37 | _bus(bus), _event(event) |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 38 | { |
| 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 Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 46 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 47 | // 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 | |
| 54 | const std::vector<std::string>& Manager::getActiveProfiles() |
| 55 | { |
| 56 | return _activeProfiles; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | unsigned 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 Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame^] | 72 | void 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 Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 98 | } // namespace phosphor::fan::control::json |