Matthew Barth | c95c527 | 2020-06-15 19:51:13 -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 | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 16 | #include "system.hpp" |
| 17 | |
| 18 | #include "fan.hpp" |
| 19 | #include "fan_defs.hpp" |
| 20 | #include "tach_sensor.hpp" |
| 21 | #include "trust_manager.hpp" |
| 22 | #include "types.hpp" |
| 23 | #ifdef MONITOR_USE_JSON |
| 24 | #include "json_parser.hpp" |
| 25 | #endif |
| 26 | |
| 27 | #include <nlohmann/json.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 28 | #include <phosphor-logging/log.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 29 | #include <sdbusplus/bus.hpp> |
| 30 | #include <sdeventplus/event.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 31 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 32 | |
| 33 | namespace phosphor::fan::monitor |
| 34 | { |
| 35 | |
| 36 | using json = nlohmann::json; |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 37 | using namespace phosphor::logging; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 38 | |
| 39 | System::System(Mode mode, sdbusplus::bus::bus& bus, |
| 40 | const sdeventplus::Event& event) : |
| 41 | _mode(mode), |
| 42 | _bus(bus), _event(event) |
| 43 | { |
| 44 | json jsonObj = json::object(); |
| 45 | #ifdef MONITOR_USE_JSON |
| 46 | jsonObj = getJsonObj(bus); |
| 47 | #endif |
| 48 | // Retrieve and set trust groups within the trust manager |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 49 | setTrustMgr(getTrustGroups(jsonObj)); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 50 | // Retrieve fan definitions and create fan objects to be monitored |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 51 | setFans(getFanDefinitions(jsonObj)); |
| 52 | log<level::INFO>("Configuration loaded"); |
| 53 | } |
| 54 | |
| 55 | void System::sighupHandler(sdeventplus::source::Signal&, |
| 56 | const struct signalfd_siginfo*) |
| 57 | { |
| 58 | try |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 59 | { |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 60 | json jsonObj = json::object(); |
| 61 | #ifdef MONITOR_USE_JSON |
| 62 | jsonObj = getJsonObj(_bus); |
| 63 | #endif |
| 64 | auto trustGrps = getTrustGroups(jsonObj); |
| 65 | auto fanDefs = getFanDefinitions(jsonObj); |
| 66 | // Set configured trust groups |
| 67 | setTrustMgr(trustGrps); |
| 68 | // Clear/set configured fan definitions |
| 69 | _fans.clear(); |
| 70 | setFans(fanDefs); |
| 71 | log<level::INFO>("Configuration reloaded successfully"); |
| 72 | } |
| 73 | catch (std::runtime_error& re) |
| 74 | { |
| 75 | log<level::ERR>("Error reloading config, no config changes made", |
| 76 | entry("LOAD_ERROR=%s", re.what())); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | const std::vector<CreateGroupFunction> |
| 81 | System::getTrustGroups(const json& jsonObj) |
| 82 | { |
| 83 | #ifdef MONITOR_USE_JSON |
| 84 | return getTrustGrps(jsonObj); |
| 85 | #else |
| 86 | return trustGroups; |
| 87 | #endif |
| 88 | } |
| 89 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 90 | void System::setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs) |
| 91 | { |
| 92 | _trust = std::make_unique<trust::Manager>(groupFuncs); |
| 93 | } |
| 94 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 95 | const std::vector<FanDefinition> System::getFanDefinitions(const json& jsonObj) |
| 96 | { |
| 97 | #ifdef MONITOR_USE_JSON |
| 98 | return getFanDefs(jsonObj); |
| 99 | #else |
| 100 | return fanDefinitions; |
| 101 | #endif |
| 102 | } |
| 103 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 104 | void System::setFans(const std::vector<FanDefinition>& fanDefs) |
| 105 | { |
| 106 | for (const auto& fanDef : fanDefs) |
| 107 | { |
| 108 | // Check if a condition exists on the fan |
| 109 | auto condition = std::get<conditionField>(fanDef); |
| 110 | if (condition) |
| 111 | { |
| 112 | // Condition exists, skip adding fan if it fails |
| 113 | if (!(*condition)(_bus)) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | } |
| 118 | _fans.emplace_back( |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame^] | 119 | std::make_unique<Fan>(_mode, _bus, _event, _trust, fanDef, *this)); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 123 | } // namespace phosphor::fan::monitor |