blob: 84f2fcc31fdeac035a6e4af38f221a486b0ff1f4 [file] [log] [blame]
Matt Spinlere567dd22017-04-27 12:27:17 -05001/**
2 * Copyright © 2017 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 Barthccaf2db2020-06-04 13:09:35 -050016
17#include "config.h"
18
Matthew Barth6ad28432017-08-22 11:18:19 -050019#include "argument.hpp"
Matt Spinlerc36168a2017-04-27 14:32:43 -050020#include "fan.hpp"
Matthew Barthccaf2db2020-06-04 13:09:35 -050021#ifdef MONITOR_USE_JSON
22#include "json_parser.hpp"
23#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -050024#include "fan_defs.hpp"
Matt Spinlerc39e8592017-09-28 13:13:08 -050025#include "trust_manager.hpp"
Matt Spinlere567dd22017-04-27 12:27:17 -050026
Matthew Barth177fe982020-05-26 11:05:19 -050027#include <phosphor-logging/log.hpp>
28#include <sdbusplus/bus.hpp>
29#include <sdeventplus/event.hpp>
30
Matt Spinlerc36168a2017-04-27 14:32:43 -050031using namespace phosphor::fan::monitor;
32using namespace phosphor::logging;
33
Matthew Barth6ad28432017-08-22 11:18:19 -050034int main(int argc, char* argv[])
Matt Spinlere567dd22017-04-27 12:27:17 -050035{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070036 auto event = sdeventplus::Event::get_default();
Matt Spinlerc36168a2017-04-27 14:32:43 -050037 auto bus = sdbusplus::bus::new_default();
Matt Spinler84201112017-05-12 11:31:53 -050038 std::vector<std::unique_ptr<Fan>> fans;
Matthew Barth6ad28432017-08-22 11:18:19 -050039 phosphor::fan::util::ArgumentParser args(argc, argv);
40
41 if (argc != 2)
42 {
43 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070044 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050045 }
46
47 Mode mode;
48 if (args["init"] == "true")
49 {
50 mode = Mode::init;
51 }
52 else if (args["monitor"] == "true")
53 {
54 mode = Mode::monitor;
55 }
56 else
57 {
58 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070059 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050060 }
Matt Spinlerc36168a2017-04-27 14:32:43 -050061
Matthew Barth177fe982020-05-26 11:05:19 -050062 // Attach the event object to the bus object so we can
63 // handle both sd_events (for the timers) and dbus signals.
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070064 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matt Spinlerc36168a2017-04-27 14:32:43 -050065
Matthew Barthccaf2db2020-06-04 13:09:35 -050066#ifdef MONITOR_USE_JSON
67 // Get JSON object from monitor JSON config file
68 const auto& jsonObj = getJsonObj(bus);
Matthew Barth9ea8bee2020-06-04 14:27:19 -050069
70 // Retrieve and set trust groups within the trust manager
71 auto trust =
72 std::make_unique<phosphor::fan::trust::Manager>(getTrustGrps(jsonObj));
Matthew Barth3ad14342020-06-08 16:17:42 -050073
74 // Retrieve fan definitions and create fan objects to be monitored
75 for (const auto& fanDef : getFanDefs(jsonObj))
76 {
77 // Check if a condition exists on the fan
78 auto condition = std::get<conditionField>(fanDef);
79 if (condition)
80 {
81 // Condition exists, skip adding fan if it fails
82 if (!(*condition)(bus))
83 {
84 continue;
85 }
86 }
87 fans.emplace_back(
88 std::make_unique<Fan>(mode, bus, event, trust, fanDef));
89 }
Matthew Barthccaf2db2020-06-04 13:09:35 -050090#else
91 auto trust = std::make_unique<phosphor::fan::trust::Manager>(trustGroups);
92
Matt Spinlerc36168a2017-04-27 14:32:43 -050093 for (const auto& fanDef : fanDefinitions)
94 {
Matthew Barth81748b12018-05-02 16:03:48 -050095 // Check if a condition exists on the fan
96 auto condition = std::get<conditionField>(fanDef);
97 if (condition)
98 {
99 // Condition exists, skip adding fan if it fails
100 if (!(*condition)(bus))
101 {
102 continue;
103 }
104 }
Matthew Barth177fe982020-05-26 11:05:19 -0500105 fans.emplace_back(
106 std::make_unique<Fan>(mode, bus, event, trust, fanDef));
Matt Spinlerc36168a2017-04-27 14:32:43 -0500107 }
Matthew Barthccaf2db2020-06-04 13:09:35 -0500108#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -0500109
Matthew Barth6ad28432017-08-22 11:18:19 -0500110 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -0500111 {
Matthew Barth6ad28432017-08-22 11:18:19 -0500112 // Fans were initialized to be functional, exit
113 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -0500114 }
Matt Spinlerc36168a2017-04-27 14:32:43 -0500115
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700116 return event.loop();
Matt Spinlere567dd22017-04-27 12:27:17 -0500117}