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; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 37 | std::map<std::string, |
| 38 | std::map<std::pair<std::string, bool>, std::vector<std::string>>> |
| 39 | Manager::_servTree; |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 40 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 41 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 42 | _bus(bus), _event(event) |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 43 | { |
| 44 | // Manager JSON config file is optional |
| 45 | auto confFile = |
| 46 | fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true); |
| 47 | if (!confFile.empty()) |
| 48 | { |
| 49 | _jsonObj = fan::JsonConfig::load(confFile); |
| 50 | } |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 51 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 52 | // Parse and set the available profiles and which are active |
| 53 | setProfiles(); |
| 54 | |
| 55 | // Load the zone configurations |
| 56 | _zones = getConfig<Zone>(bus); |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 57 | |
| 58 | // Load the fan configurations and move each fan into its zone |
| 59 | auto fans = getConfig<Fan>(bus); |
| 60 | for (auto& fan : fans) |
| 61 | { |
| 62 | auto itZone = |
| 63 | std::find_if(_zones.begin(), _zones.end(), |
| 64 | [&fanZone = fan.second->getZone()](const auto& zone) { |
| 65 | return fanZone == zone.second->getName(); |
| 66 | }); |
| 67 | if (itZone != _zones.end()) |
| 68 | { |
| 69 | itZone->second->addFan(std::move(fan.second)); |
| 70 | } |
| 71 | } |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | const std::vector<std::string>& Manager::getActiveProfiles() |
| 75 | { |
| 76 | return _activeProfiles; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 79 | bool Manager::hasOwner(const std::string& path, const std::string& intf) |
| 80 | { |
| 81 | auto itServ = _servTree.find(path); |
| 82 | if (itServ == _servTree.end()) |
| 83 | { |
| 84 | // Path not found in cache, therefore owner missing |
| 85 | return false; |
| 86 | } |
| 87 | for (const auto& serv : itServ->second) |
| 88 | { |
| 89 | auto itIntf = std::find_if( |
| 90 | serv.second.begin(), serv.second.end(), |
| 91 | [&intf](const auto& interface) { return intf == interface; }); |
| 92 | if (itIntf != std::end(serv.second)) |
| 93 | { |
| 94 | // Service found, return owner state |
| 95 | return serv.first.second; |
| 96 | } |
| 97 | } |
| 98 | // Interface not found in cache, therefore owner missing |
| 99 | return false; |
| 100 | } |
| 101 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 102 | unsigned int Manager::getPowerOnDelay() |
| 103 | { |
| 104 | auto powerOnDelay = 0; |
| 105 | |
| 106 | // Parse optional "power_on_delay" from JSON object |
| 107 | if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay")) |
| 108 | { |
| 109 | powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>(); |
| 110 | } |
| 111 | |
| 112 | return powerOnDelay; |
| 113 | } |
| 114 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 115 | void Manager::setProfiles() |
| 116 | { |
| 117 | // Profiles JSON config file is optional |
| 118 | auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName, |
| 119 | Profile::confFileName, true); |
| 120 | if (!confFile.empty()) |
| 121 | { |
| 122 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 123 | { |
| 124 | auto obj = std::make_unique<Profile>(entry); |
| 125 | _profiles.emplace( |
| 126 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 127 | std::move(obj)); |
| 128 | } |
| 129 | } |
| 130 | // Ensure all configurations use the same set of active profiles |
| 131 | // (In case a profile's active state changes during configuration) |
| 132 | for (const auto& profile : _profiles) |
| 133 | { |
| 134 | if (profile.second->isActive()) |
| 135 | { |
| 136 | _activeProfiles.emplace_back(profile.first.first); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 141 | } // namespace phosphor::fan::control::json |