blob: ae6d034967e06c4aa05e366bf22e82d302ee00ed [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 Barthd06905c2020-06-12 08:13:06 -050016#include "config.h"
17
Matthew Barth6ad28432017-08-22 11:18:19 -050018#include "argument.hpp"
Matt Spinlerc36168a2017-04-27 14:32:43 -050019#include "fan.hpp"
Matthew Barthc95c5272020-06-15 19:51:13 -050020#include "system.hpp"
Matt Spinlerc39e8592017-09-28 13:13:08 -050021#include "trust_manager.hpp"
Matt Spinlere567dd22017-04-27 12:27:17 -050022
Matthew Barth177fe982020-05-26 11:05:19 -050023#include <sdbusplus/bus.hpp>
24#include <sdeventplus/event.hpp>
Matthew Barthd06905c2020-06-12 08:13:06 -050025#include <sdeventplus/source/signal.hpp>
26#include <stdplus/signal.hpp>
Matthew Barth177fe982020-05-26 11:05:19 -050027
Matt Spinlerc36168a2017-04-27 14:32:43 -050028using namespace phosphor::fan::monitor;
Matt Spinlerc36168a2017-04-27 14:32:43 -050029
Matthew Barth6ad28432017-08-22 11:18:19 -050030int main(int argc, char* argv[])
Matt Spinlere567dd22017-04-27 12:27:17 -050031{
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070032 auto event = sdeventplus::Event::get_default();
Matt Spinlerc36168a2017-04-27 14:32:43 -050033 auto bus = sdbusplus::bus::new_default();
Matthew Barth6ad28432017-08-22 11:18:19 -050034 phosphor::fan::util::ArgumentParser args(argc, argv);
35
36 if (argc != 2)
37 {
38 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070039 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050040 }
41
42 Mode mode;
43 if (args["init"] == "true")
44 {
45 mode = Mode::init;
46 }
47 else if (args["monitor"] == "true")
48 {
49 mode = Mode::monitor;
50 }
51 else
52 {
53 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070054 return 1;
Matthew Barth6ad28432017-08-22 11:18:19 -050055 }
Matt Spinlerc36168a2017-04-27 14:32:43 -050056
Matt Spinlerb0412d02020-10-12 16:53:52 -050057 // If using JSON, then everything is handled in a single
58 // step - the init step. Hopefully these can eventually be
59 // reduced into a single invocation.
60#ifdef MONITOR_USE_JSON
61 if (mode == Mode::monitor)
62 {
63 return 0;
64 }
65#endif
66
Matthew Barth177fe982020-05-26 11:05:19 -050067 // Attach the event object to the bus object so we can
68 // handle both sd_events (for the timers) and dbus signals.
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070069 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Matt Spinlerc36168a2017-04-27 14:32:43 -050070
Matthew Barthc95c5272020-06-15 19:51:13 -050071 System system(mode, bus, event);
Matt Spinlerc36168a2017-04-27 14:32:43 -050072
Matthew Barthd06905c2020-06-12 08:13:06 -050073#ifdef MONITOR_USE_JSON
74 // Enable SIGHUP handling to reload JSON config
75 stdplus::signal::block(SIGHUP);
76 sdeventplus::source::Signal signal(event, SIGHUP,
77 std::bind(&System::sighupHandler,
78 &system, std::placeholders::_1,
79 std::placeholders::_2));
Matt Spinlerc8d3c512021-01-06 14:22:25 -060080
81 bus.request_name(THERMAL_ALERT_BUSNAME);
Matthew Barthd06905c2020-06-12 08:13:06 -050082#endif
83
Matt Spinlerb0412d02020-10-12 16:53:52 -050084#ifndef MONITOR_USE_JSON
Matthew Barth6ad28432017-08-22 11:18:19 -050085 if (mode == Mode::init)
Matt Spinlera5763ff2017-06-14 15:54:12 -050086 {
Matthew Barth6ad28432017-08-22 11:18:19 -050087 // Fans were initialized to be functional, exit
88 return 0;
Matt Spinlera5763ff2017-06-14 15:54:12 -050089 }
Matt Spinlerb0412d02020-10-12 16:53:52 -050090#endif
Matt Spinlerc36168a2017-04-27 14:32:43 -050091
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070092 return event.loop();
Matt Spinlere567dd22017-04-27 12:27:17 -050093}