1 /**
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  */
16 #include "config.h"
17 
18 #ifndef MONITOR_USE_JSON
19 #include "argument.hpp"
20 #endif
21 #include "fan.hpp"
22 #ifdef MONITOR_USE_JSON
23 #include "json_config.hpp"
24 #include "json_parser.hpp"
25 #endif
26 #include "system.hpp"
27 #include "trust_manager.hpp"
28 
29 #include <sdbusplus/bus.hpp>
30 #include <sdeventplus/event.hpp>
31 #include <sdeventplus/source/signal.hpp>
32 #include <stdplus/signal.hpp>
33 
34 using namespace phosphor::fan::monitor;
35 
36 int main(int argc, char* argv[])
37 {
38     auto event = sdeventplus::Event::get_default();
39     auto bus = sdbusplus::bus::new_default();
40     Mode mode = Mode::init;
41 
42 #ifndef MONITOR_USE_JSON
43     phosphor::fan::util::ArgumentParser args(argc, argv);
44 
45     if (argc != 2)
46     {
47         args.usage(argv);
48         return 1;
49     }
50 
51     if (args["init"] == "true")
52     {
53         mode = Mode::init;
54     }
55     else if (args["monitor"] == "true")
56     {
57         mode = Mode::monitor;
58     }
59     else
60     {
61         args.usage(argv);
62         return 1;
63     }
64 #endif
65 
66     // Attach the event object to the bus object so we can
67     // handle both sd_events (for the timers) and dbus signals.
68     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
69 
70     System system(mode, bus, event);
71 
72 #ifdef MONITOR_USE_JSON
73 
74     phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
75 
76     // Enable SIGHUP handling to reload JSON config
77     stdplus::signal::block(SIGHUP);
78     sdeventplus::source::Signal signal(event, SIGHUP,
79                                        std::bind(&System::sighupHandler,
80                                                  &system, std::placeholders::_1,
81                                                  std::placeholders::_2));
82     bus.request_name(THERMAL_ALERT_BUSNAME);
83 #else
84     system.start();
85 
86     if (mode == Mode::init)
87     {
88         // Fans were initialized to be functional, exit
89         return 0;
90     }
91 #endif
92 
93     return event.loop();
94 }
95