xref: /openbmc/phosphor-fan-presence/control/main.cpp (revision 0676494690fcbe19234c9ba9599aa6a6dfc3e9af)
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 "argument.hpp"
17 #include "manager.hpp"
18 #include "sdbusplus.hpp"
19 #ifdef CONTROL_USE_JSON
20 #include "json/manager.hpp"
21 #endif
22 
23 #include <phosphor-logging/log.hpp>
24 #include <sdbusplus/bus.hpp>
25 #include <sdeventplus/event.hpp>
26 
27 using namespace phosphor::fan::control;
28 using namespace phosphor::logging;
29 
30 int main(int argc, char* argv[])
31 {
32     auto event = sdeventplus::Event::get_default();
33     auto bus = sdbusplus::bus::new_default();
34 
35 #ifndef CONTROL_USE_JSON
36     phosphor::fan::util::ArgumentParser args(argc, argv);
37     if (argc != 2)
38     {
39         args.usage(argv);
40         return 1;
41     }
42 
43     Mode mode;
44 
45     if (args["init"] == "true")
46     {
47         mode = Mode::init;
48     }
49     else if (args["control"] == "true")
50     {
51         mode = Mode::control;
52     }
53     else
54     {
55         args.usage(argv);
56         return 1;
57     }
58 #endif
59 
60     // Attach the event object to the bus object so we can
61     // handle both sd_events (for the timers) and dbus signals.
62     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
63 
64     try
65     {
66 #ifdef CONTROL_USE_JSON
67         json::Manager manager(bus, event);
68 #else
69         Manager manager(bus, event, mode);
70 
71         // Init mode will just set fans to max and delay
72         if (mode == Mode::init)
73         {
74             manager.doInit(event);
75             return 0;
76         }
77 #endif
78         return event.loop();
79     }
80     // Log the useful metadata on these exceptions and let the app
81     // return 1 so it is restarted without a core dump.
82     catch (phosphor::fan::util::DBusServiceError& e)
83     {
84         log<level::ERR>("Uncaught DBus service lookup failure exception",
85                         entry("PATH=%s", e.path.c_str()),
86                         entry("INTERFACE=%s", e.interface.c_str()));
87     }
88     catch (phosphor::fan::util::DBusMethodError& e)
89     {
90         log<level::ERR>("Uncaught DBus method failure exception",
91                         entry("BUSNAME=%s", e.busName.c_str()),
92                         entry("PATH=%s", e.path.c_str()),
93                         entry("INTERFACE=%s", e.interface.c_str()),
94                         entry("METHOD=%s", e.method.c_str()));
95     }
96     catch (phosphor::fan::util::DBusPropertyError& e)
97     {
98         log<level::ERR>("Uncaught DBus property access failure exception",
99                         entry("BUSNAME=%s", e.busName.c_str()),
100                         entry("PATH=%s", e.path.c_str()),
101                         entry("INTERFACE=%s", e.interface.c_str()),
102                         entry("PROPERTY=%s", e.property.c_str()));
103     }
104 
105     return 1;
106 }
107