blob: 9b9399968ee50b60e09b607d3ef94d8e44f0e265 [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 */
Matthew Barthc95c5272020-06-15 19:51:13 -050016#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 Barthd06905c2020-06-12 08:13:06 -050028#include <phosphor-logging/log.hpp>
Matthew Barthc95c5272020-06-15 19:51:13 -050029#include <sdbusplus/bus.hpp>
30#include <sdeventplus/event.hpp>
Matthew Barthd06905c2020-06-12 08:13:06 -050031#include <sdeventplus/source/signal.hpp>
Matthew Barthc95c5272020-06-15 19:51:13 -050032
33namespace phosphor::fan::monitor
34{
35
36using json = nlohmann::json;
Matthew Barthd06905c2020-06-12 08:13:06 -050037using namespace phosphor::logging;
Matthew Barthc95c5272020-06-15 19:51:13 -050038
39System::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 Barthd06905c2020-06-12 08:13:06 -050049 setTrustMgr(getTrustGroups(jsonObj));
Matthew Barthc95c5272020-06-15 19:51:13 -050050 // Retrieve fan definitions and create fan objects to be monitored
Matthew Barthd06905c2020-06-12 08:13:06 -050051 setFans(getFanDefinitions(jsonObj));
52 log<level::INFO>("Configuration loaded");
53}
54
55void System::sighupHandler(sdeventplus::source::Signal&,
56 const struct signalfd_siginfo*)
57{
58 try
Matthew Barthc95c5272020-06-15 19:51:13 -050059 {
Matthew Barthd06905c2020-06-12 08:13:06 -050060 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 Barthc95c5272020-06-15 19:51:13 -050077 }
78}
79
80const 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 Barthd06905c2020-06-12 08:13:06 -050090void System::setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs)
91{
92 _trust = std::make_unique<trust::Manager>(groupFuncs);
93}
94
Matthew Barthc95c5272020-06-15 19:51:13 -050095const 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 Barthd06905c2020-06-12 08:13:06 -0500104void 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 Spinlerb0412d02020-10-12 16:53:52 -0500119 std::make_unique<Fan>(_mode, _bus, _event, _trust, fanDef, *this));
Matthew Barthd06905c2020-06-12 08:13:06 -0500120 }
121}
122
Matthew Barthc95c5272020-06-15 19:51:13 -0500123} // namespace phosphor::fan::monitor