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