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