xref: /openbmc/phosphor-fan-presence/monitor/main.cpp (revision dfddd648cb81b27492afead4e2346f5fcd1397cb)
1e567dd24SMatt Spinler /**
2e567dd24SMatt Spinler  * Copyright © 2017 IBM Corporation
3e567dd24SMatt Spinler  *
4e567dd24SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5e567dd24SMatt Spinler  * you may not use this file except in compliance with the License.
6e567dd24SMatt Spinler  * You may obtain a copy of the License at
7e567dd24SMatt Spinler  *
8e567dd24SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9e567dd24SMatt Spinler  *
10e567dd24SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11e567dd24SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12e567dd24SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e567dd24SMatt Spinler  * See the License for the specific language governing permissions and
14e567dd24SMatt Spinler  * limitations under the License.
15e567dd24SMatt Spinler  */
16d06905c9SMatthew Barth #include "config.h"
17d06905c9SMatthew Barth 
187d135641SMatt Spinler #ifndef MONITOR_USE_JSON
19cb6129e0SGeorge Liu #include <CLI/CLI.hpp>
207d135641SMatt Spinler #endif
21c36168a1SMatt Spinler #include "fan.hpp"
227d135641SMatt Spinler #ifdef MONITOR_USE_JSON
23bf8e56f6SMike Capps #include "dbus_paths.hpp"
247d135641SMatt Spinler #include "json_config.hpp"
257d135641SMatt Spinler #include "json_parser.hpp"
267d135641SMatt Spinler #endif
27c95c527aSMatthew Barth #include "system.hpp"
28c39e859bSMatt Spinler #include "trust_manager.hpp"
29e567dd24SMatt Spinler 
30177fe986SMatthew Barth #include <sdbusplus/bus.hpp>
31177fe986SMatthew Barth #include <sdeventplus/event.hpp>
32d06905c9SMatthew Barth #include <sdeventplus/source/signal.hpp>
33d06905c9SMatthew Barth #include <stdplus/signal.hpp>
34177fe986SMatthew Barth 
35c36168a1SMatt Spinler using namespace phosphor::fan::monitor;
36c36168a1SMatt Spinler 
main(int argc,char * argv[])37808d7fe8SMike Capps int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
38e567dd24SMatt Spinler {
391cfc2f11SWilliam A. Kennington III     auto event = sdeventplus::Event::get_default();
40c36168a1SMatt Spinler     auto bus = sdbusplus::bus::new_default();
417d135641SMatt Spinler     Mode mode = Mode::init;
427d135641SMatt Spinler 
437d135641SMatt Spinler #ifndef MONITOR_USE_JSON
44cb6129e0SGeorge Liu     CLI::App app{"Phosphor Fan Monitor"};
456ad28430SMatthew Barth 
46cb6129e0SGeorge Liu     bool init = false;
47cb6129e0SGeorge Liu     bool monitor = false;
48cb6129e0SGeorge Liu     app.add_flag("-i,--init", init, "Set fans to functional");
49cb6129e0SGeorge Liu     app.add_flag("-m,--monitor", monitor, "Start fan functional monitoring");
50cb6129e0SGeorge Liu     app.require_option();
51cb6129e0SGeorge Liu 
52cb6129e0SGeorge Liu     try
536ad28430SMatthew Barth     {
54cb6129e0SGeorge Liu         app.parse(argc, argv);
55cb6129e0SGeorge Liu     }
56cb6129e0SGeorge Liu     catch (const CLI::Error& e)
57cb6129e0SGeorge Liu     {
58cb6129e0SGeorge Liu         return app.exit(e);
596ad28430SMatthew Barth     }
606ad28430SMatthew Barth 
61cb6129e0SGeorge Liu     if (init)
626ad28430SMatthew Barth     {
636ad28430SMatthew Barth         mode = Mode::init;
646ad28430SMatthew Barth     }
65cb6129e0SGeorge Liu     else if (monitor)
666ad28430SMatthew Barth     {
676ad28430SMatthew Barth         mode = Mode::monitor;
686ad28430SMatthew Barth     }
69b0412d07SMatt Spinler #endif
70b0412d07SMatt Spinler 
71c36168a1SMatt Spinler     // Attach the event object to the bus object so we can
72c36168a1SMatt Spinler     // handle both sd_events (for the timers) and dbus signals.
731cfc2f11SWilliam A. Kennington III     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
74c36168a1SMatt Spinler 
75c95c527aSMatthew Barth     System system(mode, bus, event);
76c36168a1SMatt Spinler 
77d06905c9SMatthew Barth #ifdef MONITOR_USE_JSON
787d135641SMatt Spinler 
79823bc49eSMatthew Barth     phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
807d135641SMatt Spinler 
81d06905c9SMatthew Barth     // Enable SIGHUP handling to reload JSON config
82d06905c9SMatthew Barth     stdplus::signal::block(SIGHUP);
83*dfddd648SPatrick Williams     sdeventplus::source::Signal signal(
84*dfddd648SPatrick Williams         event, SIGHUP,
85*dfddd648SPatrick Williams         std::bind(&System::sighupHandler, &system, std::placeholders::_1,
86d06905c9SMatthew Barth                   std::placeholders::_2));
874f472a86SMatt Spinler 
884f472a86SMatt Spinler     // Enable SIGUSR1 handling to dump debug data
894f472a86SMatt Spinler     stdplus::signal::block(SIGUSR1);
904f472a86SMatt Spinler     sdeventplus::source::Signal sigUsr1(
914f472a86SMatt Spinler         event, SIGUSR1,
924f472a86SMatt Spinler         std::bind(&System::dumpDebugData, &system, std::placeholders::_1,
934f472a86SMatt Spinler                   std::placeholders::_2));
944f472a86SMatt Spinler 
95c8d3c51fSMatt Spinler     bus.request_name(THERMAL_ALERT_BUSNAME);
967d135641SMatt Spinler #else
977d135641SMatt Spinler     system.start();
98d06905c9SMatthew Barth 
996ad28430SMatthew Barth     if (mode == Mode::init)
100a5763ff8SMatt Spinler     {
1016ad28430SMatthew Barth         // Fans were initialized to be functional, exit
1026ad28430SMatthew Barth         return 0;
103a5763ff8SMatt Spinler     }
104b0412d07SMatt Spinler #endif
105c36168a1SMatt Spinler 
1061cfc2f11SWilliam A. Kennington III     return event.loop();
107e567dd24SMatt Spinler }
108