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