blob: 74cc62da79035e1bcdf8a46ac0323bad175c7ff6 [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 Barth6ad28432017-08-22 11:18:19 -050016#include "argument.hpp"
Matt Spinlerc36168a2017-04-27 14:32:43 -050017#include "fan.hpp"
18#include "fan_defs.hpp"
Matt Spinlerc39e8592017-09-28 13:13:08 -050019#include "trust_manager.hpp"
Matt Spinlere567dd22017-04-27 12:27:17 -050020
Matthew Barth177fe982020-05-26 11:05:19 -050021#include <phosphor-logging/log.hpp>
22#include <sdbusplus/bus.hpp>
23#include <sdeventplus/event.hpp>
24
Matt Spinlerc36168a2017-04-27 14:32:43 -050025using namespace phosphor::fan::monitor;
26using namespace phosphor::logging;
27
Matthew Barth6ad28432017-08-22 11:18:19 -050028int main(int argc, char* argv[])
Matt Spinlere567dd22017-04-27 12:27:17 -050029{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070030 auto event = sdeventplus::Event::get_default();
Matt Spinlerc36168a2017-04-27 14:32:43 -050031 auto bus = sdbusplus::bus::new_default();
Matt Spinler84201112017-05-12 11:31:53 -050032 std::vector<std::unique_ptr<Fan>> fans;
Matthew Barth6ad28432017-08-22 11:18:19 -050033 phosphor::fan::util::ArgumentParser args(argc, argv);
34
35 if (argc != 2)
36 {
37 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070038 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050039 }
40
41 Mode mode;
42 if (args["init"] == "true")
43 {
44 mode = Mode::init;
45 }
46 else if (args["monitor"] == "true")
47 {
48 mode = Mode::monitor;
49 }
50 else
51 {
52 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070053 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050054 }
Matt Spinlerc36168a2017-04-27 14:32:43 -050055
Matt Spinlerc39e8592017-09-28 13:13:08 -050056 std::unique_ptr<phosphor::fan::trust::Manager> trust =
Matthew Barth177fe982020-05-26 11:05:19 -050057 std::make_unique<phosphor::fan::trust::Manager>(trustGroups);
Matt Spinlerc39e8592017-09-28 13:13:08 -050058
Matthew Barth177fe982020-05-26 11:05:19 -050059 // Attach the event object to the bus object so we can
60 // handle both sd_events (for the timers) and dbus signals.
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070061 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matt Spinlerc36168a2017-04-27 14:32:43 -050062
63 for (const auto& fanDef : fanDefinitions)
64 {
Matthew Barth81748b12018-05-02 16:03:48 -050065 // Check if a condition exists on the fan
66 auto condition = std::get<conditionField>(fanDef);
67 if (condition)
68 {
69 // Condition exists, skip adding fan if it fails
70 if (!(*condition)(bus))
71 {
72 continue;
73 }
74 }
Matthew Barth177fe982020-05-26 11:05:19 -050075 fans.emplace_back(
76 std::make_unique<Fan>(mode, bus, event, trust, fanDef));
Matt Spinlerc36168a2017-04-27 14:32:43 -050077 }
78
Matthew Barth6ad28432017-08-22 11:18:19 -050079 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -050080 {
Matthew Barth6ad28432017-08-22 11:18:19 -050081 // Fans were initialized to be functional, exit
82 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -050083 }
Matt Spinlerc36168a2017-04-27 14:32:43 -050084
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070085 return event.loop();
Matt Spinlere567dd22017-04-27 12:27:17 -050086}